<commit_before>//===--- ASTPrinter.cpp - Swift Language AST Printer---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
//  This file implements printing for the Swift ASTs.
//
//===----------------------------------------------------------------------===//

#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/Attr.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/PrintOptions.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeVisitor.h"
#include "swift/AST/Types.h"
#include "swift/Basic/STLExtras.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;

namespace {
  /// \brief AST pretty-printer.
  class PrintAST : public ASTVisitor<PrintAST> {
    raw_ostream &OS;
    const PrintOptions &Options;
    SmallVectorImpl<std::pair<Decl *, uint64_t>> *DeclOffsets;
    unsigned IndentLevel = 0;

    friend DeclVisitor<PrintAST>;

    /// \brief RAII object that increases the indentation level.
    class IndentRAII {
      PrintAST &Self;
      bool DoIndent;

    public:
      IndentRAII(PrintAST &self, bool DoIndent = true)
          : Self(self), DoIndent(DoIndent) {
        if (DoIndent)
          Self.IndentLevel += Self.Options.Indent;
      }

      ~IndentRAII() {
        if (DoIndent)
          Self.IndentLevel -= Self.Options.Indent;
      }
    };

    /// \brief Indent the current number of indentation spaces.
    void indent() {
      OS.indent(IndentLevel);
    }

    /// \brief Record the location of this declaration, which is about to
    /// be printed.
    void recordDeclLoc(Decl *decl) {
      if (DeclOffsets)
        DeclOffsets->push_back({decl, OS.tell()});
    }

    void printTypeLoc(const TypeLoc &TL) {
      // Print a TypeRepr if instructed to do so by options, or if the type
      // is null.
      if ((Options.PreferTypeRepr && TL.hasLocation()) ||
          TL.getType().isNull()) {
        TL.getTypeRepr()->print(OS);
        return;
      }
      TL.getType().print(OS, Options);
    }

    void printAttributes(const DeclAttributes &attrs);
    void printTypedPattern(const TypedPattern *TP,
                           bool StripOuterSliceType = false);
public:
    void printPattern(const Pattern *pattern);

    void printGenericParams(GenericParamList *params);

private:
    void printMembers(ArrayRef<Decl *> members, bool needComma = false);
    void printNominalDeclName(NominalTypeDecl *decl);
    void printInherited(const Decl *decl,
                        ArrayRef<TypeLoc> inherited,
                        ArrayRef<ProtocolDecl *> protos,
                        Type superclass = {},
                        bool PrintAsProtocolComposition = false);

    template <typename DeclWithSuperclass>
    void printInheritedWithSuperclass(DeclWithSuperclass *decl);
    
    void printInherited(const TypeDecl *decl);
    void printInherited(const EnumDecl *D);
    void printInherited(const ExtensionDecl *decl);
    void printInherited(const GenericTypeParamDecl *D);
    void printBraceStmtElements(BraceStmt *stmt, bool NeedIndent = true);

    /// \brief Print the function parameters in curried or selector style,
    /// to match the original function declaration.
    void printFunctionParameters(AbstractFunctionDecl *AFD);

#define DECL(Name,Parent) void visit##Name##Decl(Name##Decl *decl);
#define ABSTRACT_DECL(Name, Parent)
#define DECL_RANGE(Name,Start,End)
#include "swift/AST/DeclNodes.def"

#define STMT(Name, Parent) void visit##Name##Stmt(Name##Stmt *stmt);
#include "swift/AST/StmtNodes.def"
    
  public:
    PrintAST(raw_ostream &os, const PrintOptions &options,
             SmallVectorImpl<std::pair<Decl *, uint64_t>> *declOffsets)
      : OS(os), Options(options), DeclOffsets(declOffsets) { }
  };

class AttributePrinter {
  unsigned AttrCount = 0;
  raw_ostream &OS;

public:
  explicit AttributePrinter(raw_ostream &OS) : OS(OS) {}

  raw_ostream &next() {
    return OS << (AttrCount++ == 0 ? "[" : ", ");
  }

  void finish() {
    if (AttrCount > 0)
      OS << "] ";
  }
};

} // unnamed namespace

void PrintAST::printAttributes(const DeclAttributes &Attrs) {
  if (Attrs.empty())
    return;

  AttributePrinter AP(OS);
  if (Attrs.isAssignment())
    AP.next() << "assignment";
  if (Attrs.isConversion())
    AP.next()<< "conversion";
  if (Attrs.isTransparent())
    AP.next() << "transparent";
  if (Attrs.isInfix())
    AP.next() << "infix";
  switch (Attrs.getResilienceKind()) {
  case Resilience::Default: break;
  case Resilience::Fragile: AP.next() << "fragile"; break;
  case Resilience::InherentlyFragile: AP.next() << "born_fragile"; break;
  case Resilience::Resilient: AP.next() << "resilient"; break;
  }
  if (Attrs.isNoReturn())
    AP.next() << "noreturn";
  if (!Attrs.AsmName.empty())
    AP.next() << "asmname=\"" << Attrs.AsmName << "\"";
  if (Attrs.isPostfix())
    AP.next() << "postfix";
  if (Attrs.isObjC())
    AP.next() << "objc";
  if (Attrs.isIBOutlet())
    AP.next() << "iboutlet";
  if (Attrs.isIBAction())
    AP.next() << "ibaction";
  if (Attrs.isClassProtocol())
    AP.next() << "class_protocol";
  if (Attrs.isExported())
    AP.next() << "exported";

  AP.finish();
}

void PrintAST::printTypedPattern(const TypedPattern *TP,
                                 bool StripOuterSliceType) {
  printPattern(TP->getSubPattern());
  OS << ": ";
  if (StripOuterSliceType) {
    Type T = TP->getType();<commit_msg>switch ast printer to print attributes in the new syntax.<commit_after>//===--- ASTPrinter.cpp - Swift Language AST Printer---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
//  This file implements printing for the Swift ASTs.
//
//===----------------------------------------------------------------------===//

#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/Attr.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/PrintOptions.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeVisitor.h"
#include "swift/AST/Types.h"
#include "swift/Basic/STLExtras.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;

namespace {
  /// \brief AST pretty-printer.
  class PrintAST : public ASTVisitor<PrintAST> {
    raw_ostream &OS;
    const PrintOptions &Options;
    SmallVectorImpl<std::pair<Decl *, uint64_t>> *DeclOffsets;
    unsigned IndentLevel = 0;

    friend DeclVisitor<PrintAST>;

    /// \brief RAII object that increases the indentation level.
    class IndentRAII {
      PrintAST &Self;
      bool DoIndent;

    public:
      IndentRAII(PrintAST &self, bool DoIndent = true)
          : Self(self), DoIndent(DoIndent) {
        if (DoIndent)
          Self.IndentLevel += Self.Options.Indent;
      }

      ~IndentRAII() {
        if (DoIndent)
          Self.IndentLevel -= Self.Options.Indent;
      }
    };

    /// \brief Indent the current number of indentation spaces.
    void indent() {
      OS.indent(IndentLevel);
    }

    /// \brief Record the location of this declaration, which is about to
    /// be printed.
    void recordDeclLoc(Decl *decl) {
      if (DeclOffsets)
        DeclOffsets->push_back({decl, OS.tell()});
    }

    void printTypeLoc(const TypeLoc &TL) {
      // Print a TypeRepr if instructed to do so by options, or if the type
      // is null.
      if ((Options.PreferTypeRepr && TL.hasLocation()) ||
          TL.getType().isNull()) {
        TL.getTypeRepr()->print(OS);
        return;
      }
      TL.getType().print(OS, Options);
    }

    void printAttributes(const DeclAttributes &attrs);
    void printTypedPattern(const TypedPattern *TP,
                           bool StripOuterSliceType = false);
public:
    void printPattern(const Pattern *pattern);

    void printGenericParams(GenericParamList *params);

private:
    void printMembers(ArrayRef<Decl *> members, bool needComma = false);
    void printNominalDeclName(NominalTypeDecl *decl);
    void printInherited(const Decl *decl,
                        ArrayRef<TypeLoc> inherited,
                        ArrayRef<ProtocolDecl *> protos,
                        Type superclass = {},
                        bool PrintAsProtocolComposition = false);

    template <typename DeclWithSuperclass>
    void printInheritedWithSuperclass(DeclWithSuperclass *decl);
    
    void printInherited(const TypeDecl *decl);
    void printInherited(const EnumDecl *D);
    void printInherited(const ExtensionDecl *decl);
    void printInherited(const GenericTypeParamDecl *D);
    void printBraceStmtElements(BraceStmt *stmt, bool NeedIndent = true);

    /// \brief Print the function parameters in curried or selector style,
    /// to match the original function declaration.
    void printFunctionParameters(AbstractFunctionDecl *AFD);

#define DECL(Name,Parent) void visit##Name##Decl(Name##Decl *decl);
#define ABSTRACT_DECL(Name, Parent)
#define DECL_RANGE(Name,Start,End)
#include "swift/AST/DeclNodes.def"

#define STMT(Name, Parent) void visit##Name##Stmt(Name##Stmt *stmt);
#include "swift/AST/StmtNodes.def"
    
  public:
    PrintAST(raw_ostream &os, const PrintOptions &options,
             SmallVectorImpl<std::pair<Decl *, uint64_t>> *declOffsets)
      : OS(os), Options(options), DeclOffsets(declOffsets) { }
  };
} // unnamed namespace

void PrintAST::printAttributes(const DeclAttributes &Attrs) {
  if (Attrs.empty())
    return;

  if (Attrs.isAssignment())
    OS << "@assignment ";
  if (Attrs.isConversion())
    OS << "@conversion ";
  if (Attrs.isTransparent())
    OS << "@transparent ";
  if (Attrs.isInfix())
    OS << "@infix ";
  switch (Attrs.getResilienceKind()) {
  case Resilience::Default: break;
  case Resilience::Fragile: OS << "@fragile "; break;
  case Resilience::InherentlyFragile: OS << "@born_fragile "; break;
  case Resilience::Resilient: OS << "@resilient "; break;
  }
  if (Attrs.isNoReturn())
    OS << "@noreturn ";
  if (!Attrs.AsmName.empty())
    OS << "@asmname=\"" << Attrs.AsmName << "\" ";
  if (Attrs.isPostfix())
    OS << "@postfix ";
  if (Attrs.isObjC())
    OS << "@objc ";
  if (Attrs.isIBOutlet())
    OS << "@iboutlet ";
  if (Attrs.isIBAction())
    OS << "@ibaction ";
  if (Attrs.isClassProtocol())
    OS << "@class_protocol ";
  if (Attrs.isExported())
    OS << "@exported ";
}

void PrintAST::printTypedPattern(const TypedPattern *TP,
                                 bool StripOuterSliceType) {
  printPattern(TP->getSubPattern());
  OS << ": ";
  if (StripOuterSliceType) {
    Type T = TP->getType();
{'fraction_non_alphanumeric': 0.16214555350715476, 'fraction_numerical': 0.0028970239662891757, 'mean_word_length': 2.757255936675462, 'pattern_counts': {'":': 2, '<': 99, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4637729', 'n_tokens_mistral': 3594, 'n_tokens_neox': 3492, 'n_words': 853}