[AST] Introduce the ASTPrinter class, and have its callbacks invoked during AST printing.

This provides useful extension points during AST printing.

Swift SVN r11338
This commit is contained in:
Argyrios Kyrtzidis
2013-12-16 01:26:36 +00:00
parent 04b98c7d67
commit 93c15bed50
15 changed files with 588 additions and 382 deletions

View File

@@ -0,0 +1,74 @@
//===--- ASTPrinter.h - Class for printing the AST --------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_ASTPRINTER_H
#define SWIFT_AST_ASTPRINTER_H
#include "swift/Basic/LLVM.h"
#include "llvm/ADT/StringRef.h"
namespace swift {
class Decl;
class Module;
class TypeDecl;
/// An abstract class used to print an AST.
class ASTPrinter {
public:
virtual ~ASTPrinter() {}
virtual void printText(StringRef Text) = 0;
/// Called before printing of a declaration.
virtual void printDeclPre(const Decl *D) {}
/// Called before printing at the point which would be considered the location
/// of the declaration (normally the name of the declaration).
virtual void printDeclLoc(const Decl *D) {}
/// Called after finishing printing of a declaration.
virtual void printDeclPost(const Decl *D) {}
/// Called when printing the referenced name of a type declaration.
virtual void printTypeRef(const TypeDecl *TD, StringRef Text) {
printText(Text);
}
/// Called when printing the referenced name of a module.
virtual void printModuleRef(const Module *Mod, StringRef Text) {
printText(Text);
}
// Helper functions.
ASTPrinter &operator<<(StringRef Text) {
printText(Text);
return *this;
}
ASTPrinter &operator<<(unsigned long long N);
void indent(unsigned NumSpaces);
private:
virtual void anchor();
};
/// An AST printer for a raw_ostream.
class StreamPrinter : public ASTPrinter {
protected:
raw_ostream &OS;
public:
explicit StreamPrinter(raw_ostream &OS) : OS(OS) {}
void printText(StringRef Text) override;
};
} // namespace swift
#endif // LLVM_SWIFT_AST_ASTPRINTER_H

View File

@@ -43,6 +43,7 @@ namespace clang {
namespace swift { namespace swift {
class ArchetypeType; class ArchetypeType;
class ASTContext; class ASTContext;
class ASTPrinter;
class ASTWalker; class ASTWalker;
class Type; class Type;
class Expr; class Expr;
@@ -377,18 +378,14 @@ public:
/// ///
/// \param OS Output stream to which the declaration will be printed. /// \param OS Output stream to which the declaration will be printed.
void print(raw_ostream &OS) const; void print(raw_ostream &OS) const;
void print(raw_ostream &OS, const PrintOptions &Opts) const;
/// \brief Pretty-print the given declaration. /// \brief Pretty-print the given declaration.
/// ///
/// \param os Output stream to which the declaration will be printed. /// \param Printer ASTPrinter object.
/// ///
/// \param options Options to control how pretty-printing is performed. /// \param Opts Options to control how pretty-printing is performed.
/// void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
/// \param declOffsets If non-null, will be populated with the stream offsets
/// at which each declaration encountered is printed.
void print(raw_ostream &os, const PrintOptions &options,
SmallVectorImpl<std::pair<Decl *, uint64_t>> *declOffsets
= nullptr) const;
/// \brief Determine whether this declaration should be printed when /// \brief Determine whether this declaration should be printed when
/// encountered in its declaration context's list of members. /// encountered in its declaration context's list of members.

View File

@@ -325,7 +325,7 @@ namespace swift {
/// \brief The set of declarations for which we have pretty-printed /// \brief The set of declarations for which we have pretty-printed
/// results that we can point to on the command line. /// results that we can point to on the command line.
llvm::DenseMap<Decl *, SourceLoc> PrettyPrintedDeclarations; llvm::DenseMap<const Decl *, SourceLoc> PrettyPrintedDeclarations;
friend class InFlightDiagnostic; friend class InFlightDiagnostic;

View File

@@ -201,6 +201,7 @@ public:
"only for use within the debugger"); "only for use within the debugger");
void dump(raw_ostream &OS) const; void dump(raw_ostream &OS) const;
void print(raw_ostream &OS, unsigned Indent = 0) const; void print(raw_ostream &OS, unsigned Indent = 0) const;
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
// Only allow allocation of Exprs using the allocator in ASTContext // Only allow allocation of Exprs using the allocator in ASTContext
// or by doing a placement new. // or by doing a placement new.

View File

@@ -639,8 +639,9 @@ public:
/// \brief Pretty-print the contents of this source file. /// \brief Pretty-print the contents of this source file.
/// ///
/// \param OS The stream to which the contents will be printed. /// \param Printer The AST printer used for printing the contents.
/// \param PO Options controlling the printing process. /// \param PO Options controlling the printing process.
void print(ASTPrinter &Printer, const PrintOptions &PO);
void print(raw_ostream &OS, const PrintOptions &PO); void print(raw_ostream &OS, const PrintOptions &PO);
static bool classof(const FileUnit *file) { static bool classof(const FileUnit *file) {

View File

@@ -16,11 +16,12 @@
#include "swift/Basic/LLVM.h" #include "swift/Basic/LLVM.h"
namespace swift { namespace swift {
class ASTPrinter;
class Module; class Module;
struct PrintOptions; struct PrintOptions;
void printModuleInterface(Module *M, raw_ostream &OS, void printModuleInterface(Module *M, ASTPrinter &Printer,
const PrintOptions &Options); const PrintOptions &Options);
} // namespace swift } // namespace swift

View File

@@ -25,6 +25,7 @@
namespace swift { namespace swift {
class ASTPrinter;
class ArchetypeType; class ArchetypeType;
class ASTContext; class ASTContext;
class LazyResolver; class LazyResolver;
@@ -106,6 +107,7 @@ public:
void dump() const; void dump() const;
void print(raw_ostream &OS, const PrintOptions &PO = PrintOptions()) const; void print(raw_ostream &OS, const PrintOptions &PO = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &PO) const;
/// Return the name of the type as a string, for use in diagnostics only. /// Return the name of the type as a string, for use in diagnostics only.
std::string getString(const PrintOptions &PO = PrintOptions()) const; std::string getString(const PrintOptions &PO = PrintOptions()) const;

View File

@@ -73,7 +73,8 @@ public:
void operator delete(void *data) = delete; void operator delete(void *data) = delete;
void *operator new(size_t bytes, void *data) = delete; void *operator new(size_t bytes, void *data) = delete;
void print(llvm::raw_ostream &OS) const; void print(raw_ostream &OS, const PrintOptions &Opts = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
void dump() const; void dump() const;
}; };
@@ -100,7 +101,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return Range.Start; } SourceLoc getStartLocImpl() const { return Range.Start; }
SourceLoc getEndLocImpl() const { return Range.End; } SourceLoc getEndLocImpl() const { return Range.End; }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -122,6 +123,7 @@ public:
TypeRepr *getTypeRepr() const { return Ty; } TypeRepr *getTypeRepr() const { return Ty; }
void printAttrs(llvm::raw_ostream &OS) const; void printAttrs(llvm::raw_ostream &OS) const;
void printAttrs(ASTPrinter &Printer) const;
static bool classof(const TypeRepr *T) { static bool classof(const TypeRepr *T) {
return T->getKind() == TypeReprKind::Attributed; return T->getKind() == TypeReprKind::Attributed;
@@ -131,7 +133,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return Attrs.AtLoc; } SourceLoc getStartLocImpl() const { return Attrs.AtLoc; }
SourceLoc getEndLocImpl() const { return Ty->getEndLoc(); } SourceLoc getEndLocImpl() const { return Ty->getEndLoc(); }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -214,7 +216,7 @@ private:
return Components.back().getGenericArgs().back()->getEndLoc(); return Components.back().getGenericArgs().back()->getEndLoc();
return Components.back().getIdLoc(); return Components.back().getIdLoc();
} }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -246,7 +248,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return ArgsTy->getStartLoc(); } SourceLoc getStartLocImpl() const { return ArgsTy->getStartLoc(); }
SourceLoc getEndLocImpl() const { return RetTy->getEndLoc(); } SourceLoc getEndLocImpl() const { return RetTy->getEndLoc(); }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -285,7 +287,7 @@ private:
return Base->getEndLoc(); return Base->getEndLoc();
return Brackets.End; return Brackets.End;
} }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -312,7 +314,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); } SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
SourceLoc getEndLocImpl() const { return QuestionLoc; } SourceLoc getEndLocImpl() const { return QuestionLoc; }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -353,7 +355,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return Parens.Start; } SourceLoc getStartLocImpl() const { return Parens.Start; }
SourceLoc getEndLocImpl() const { return Parens.End; } SourceLoc getEndLocImpl() const { return Parens.End; }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -383,7 +385,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return IdLoc; } SourceLoc getStartLocImpl() const { return IdLoc; }
SourceLoc getEndLocImpl() const { return Ty->getEndLoc(); } SourceLoc getEndLocImpl() const { return Ty->getEndLoc(); }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -421,7 +423,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return ProtocolLoc; } SourceLoc getStartLocImpl() const { return ProtocolLoc; }
SourceLoc getEndLocImpl() const { return AngleBrackets.End; } SourceLoc getEndLocImpl() const { return AngleBrackets.End; }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };
@@ -449,7 +451,7 @@ public:
private: private:
SourceLoc getStartLocImpl() const { return Base->getStartLoc(); } SourceLoc getStartLocImpl() const { return Base->getStartLoc(); }
SourceLoc getEndLocImpl() const { return MetaLoc; } SourceLoc getEndLocImpl() const { return MetaLoc; }
void printImpl(llvm::raw_ostream &OS) const; void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
friend class TypeRepr; friend class TypeRepr;
}; };

View File

@@ -379,6 +379,7 @@ public:
void dump() const; void dump() const;
void print(raw_ostream &OS, void print(raw_ostream &OS,
const PrintOptions &PO = PrintOptions()) const; const PrintOptions &PO = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &PO) const;
/// Return the name of the type as a string, for use in diagnostics only. /// Return the name of the type as a string, for use in diagnostics only.
std::string getString(const PrintOptions &PO = PrintOptions()) const; std::string getString(const PrintOptions &PO = PrintOptions()) const;
@@ -1770,6 +1771,7 @@ public:
void dump() const; void dump() const;
void print(llvm::raw_ostream &out, void print(llvm::raw_ostream &out,
const PrintOptions &options = PrintOptions()) const; const PrintOptions &options = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &Options) const;
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out, friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out,
SILParameterInfo type) { SILParameterInfo type) {
type.print(out); type.print(out);
@@ -1849,6 +1851,7 @@ public:
void dump() const; void dump() const;
void print(llvm::raw_ostream &out, void print(llvm::raw_ostream &out,
const PrintOptions &options = PrintOptions()) const; const PrintOptions &options = PrintOptions()) const;
void print(ASTPrinter &Printer, const PrintOptions &Options) const;
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out, friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out,
SILResultInfo type) { SILResultInfo type) {
type.print(out); type.print(out);

View File

@@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/AST/AST.h" #include "swift/AST/AST.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTVisitor.h" #include "swift/AST/ASTVisitor.h"
#include "swift/Basic/STLExtras.h" #include "swift/Basic/STLExtras.h"
#include "llvm/ADT/APFloat.h" #include "llvm/ADT/APFloat.h"
@@ -1446,6 +1447,14 @@ void Expr::print(raw_ostream &OS, unsigned Indent) const {
PrintExpr(OS, Indent).visit(const_cast<Expr*>(this)); PrintExpr(OS, Indent).visit(const_cast<Expr*>(this));
} }
void Expr::print(ASTPrinter &Printer, const PrintOptions &Opts) const {
// FIXME: Fully use the ASTPrinter.
llvm::SmallString<128> Str;
llvm::raw_svector_ostream OS(Str);
print(OS);
Printer << OS.str();
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// Printing for TypeRepr and all subclasses. // Printing for TypeRepr and all subclasses.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

File diff suppressed because it is too large Load Diff

View File

@@ -15,6 +15,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h" #include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticEngine.h" #include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/Module.h" #include "swift/AST/Module.h"
@@ -325,7 +326,20 @@ void DiagnosticEngine::flushActiveDiagnostic() {
// so we can point to it. // so we can point to it.
SourceLoc ppLoc = PrettyPrintedDeclarations[ActiveDiagnosticDecl]; SourceLoc ppLoc = PrettyPrintedDeclarations[ActiveDiagnosticDecl];
if (ppLoc.isInvalid()) { if (ppLoc.isInvalid()) {
SmallVector<std::pair<Decl *, uint64_t>, 8> entries; class TrackingPrinter : public StreamPrinter {
SmallVectorImpl<std::pair<const Decl *, uint64_t>> &Entries;
public:
TrackingPrinter(
SmallVectorImpl<std::pair<const Decl *, uint64_t>> &Entries,
raw_ostream &OS) :
StreamPrinter(OS), Entries(Entries) {}
void printDeclLoc(const Decl *D) override {
Entries.push_back({ D, OS.tell() });
}
};
SmallVector<std::pair<const Decl *, uint64_t>, 8> entries;
llvm::SmallString<128> buffer; llvm::SmallString<128> buffer;
llvm::SmallString<128> bufferName; llvm::SmallString<128> bufferName;
{ {
@@ -397,7 +411,8 @@ void DiagnosticEngine::flushActiveDiagnostic() {
// Pretty-print the declaration we've picked. // Pretty-print the declaration we've picked.
llvm::raw_svector_ostream out(buffer); llvm::raw_svector_ostream out(buffer);
ppDecl->print(out, options, &entries); TrackingPrinter printer(entries, out);
ppDecl->print(printer, options);
} }
// Build a buffer with the pretty-printed declaration. // Build a buffer with the pretty-printed declaration.

View File

@@ -14,6 +14,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTWalker.h" #include "swift/AST/ASTWalker.h"
#include "swift/AST/Diagnostics.h" #include "swift/AST/Diagnostics.h"
#include "swift/AST/LazyResolver.h" #include "swift/AST/LazyResolver.h"
@@ -1057,12 +1058,17 @@ bool Module::walk(ASTWalker &Walker) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
void SourceFile::print(raw_ostream &OS, const PrintOptions &PO) { void SourceFile::print(raw_ostream &OS, const PrintOptions &PO) {
StreamPrinter Printer(OS);
print(Printer, PO);
}
void SourceFile::print(ASTPrinter &Printer, const PrintOptions &PO) {
for (auto decl : Decls) { for (auto decl : Decls) {
if (!decl->shouldPrintInContext(PO)) if (!decl->shouldPrintInContext(PO))
continue; continue;
decl->print(OS, PO); decl->print(Printer, PO);
OS << "\n"; Printer << "\n";
} }
} }

View File

@@ -14,6 +14,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/TypeRepr.h" #include "swift/AST/TypeRepr.h"
#include "swift/AST/ASTContext.h" #include "swift/AST/ASTContext.h"
#include "swift/AST/ExprHandle.h" #include "swift/AST/ExprHandle.h"
@@ -59,9 +60,14 @@ void *TypeRepr::operator new(size_t Bytes, ASTContext &C, unsigned Alignment) {
return C.Allocate(Bytes, Alignment); return C.Allocate(Bytes, Alignment);
} }
void TypeRepr::print(raw_ostream &OS) const { void TypeRepr::print(raw_ostream &OS, const PrintOptions &Opts) const {
StreamPrinter Printer(OS);
print(Printer, Opts);
}
void TypeRepr::print(ASTPrinter &Printer, const PrintOptions &Opts) const {
if (this == nullptr) { if (this == nullptr) {
OS << "<null>"; Printer << "<null>";
return; return;
} }
@@ -69,36 +75,44 @@ void TypeRepr::print(raw_ostream &OS) const {
#define TYPEREPR(CLASS, PARENT) \ #define TYPEREPR(CLASS, PARENT) \
case TypeReprKind::CLASS: { \ case TypeReprKind::CLASS: { \
auto Ty = static_cast<const CLASS##TypeRepr*>(this); \ auto Ty = static_cast<const CLASS##TypeRepr*>(this); \
return Ty->printImpl(OS); \ return Ty->printImpl(Printer, Opts); \
} }
#include "swift/AST/TypeReprNodes.def" #include "swift/AST/TypeReprNodes.def"
} }
llvm_unreachable("unknown kind!"); llvm_unreachable("unknown kind!");
} }
void ErrorTypeRepr::printImpl(llvm::raw_ostream &OS) const { void ErrorTypeRepr::printImpl(ASTPrinter &Printer,
OS << "<<error type>>"; const PrintOptions &Opts) const {
Printer << "<<error type>>";
} }
void AttributedTypeRepr::printImpl(llvm::raw_ostream &OS) const { void AttributedTypeRepr::printImpl(ASTPrinter &Printer,
printAttrs(OS); const PrintOptions &Opts) const {
OS << ' ' << Ty; printAttrs(Printer);
Printer << " ";
Ty->print(Printer, Opts);
} }
void AttributedTypeRepr::printAttrs(llvm::raw_ostream &OS) const { void AttributedTypeRepr::printAttrs(llvm::raw_ostream &OS) const {
StreamPrinter Printer(OS);
printAttrs(Printer);
}
void AttributedTypeRepr::printAttrs(ASTPrinter &Printer) const {
const TypeAttributes &Attrs = getAttrs(); const TypeAttributes &Attrs = getAttrs();
if (Attrs.has(TAK_inout)) OS << "@inout "; if (Attrs.has(TAK_inout)) Printer << "@inout ";
if (Attrs.has(TAK_auto_closure)) OS << "@auto_closure "; if (Attrs.has(TAK_auto_closure)) Printer << "@auto_closure ";
if (Attrs.has(TAK_thin)) OS << "@thin "; if (Attrs.has(TAK_thin)) Printer << "@thin ";
if (Attrs.has(TAK_noreturn)) OS << "@noreturn "; if (Attrs.has(TAK_noreturn)) Printer << "@noreturn ";
if (Attrs.has(TAK_objc_block)) OS << "@objc_block "; if (Attrs.has(TAK_objc_block)) Printer << "@objc_block ";
if (Attrs.cc.hasValue()) { if (Attrs.cc.hasValue()) {
switch (Attrs.cc.getValue()) { switch (Attrs.cc.getValue()) {
case AbstractCC::C: OS << "@cc(cdecl)"; break; case AbstractCC::C: Printer << "@cc(cdecl)"; break;
case AbstractCC::ObjCMethod: OS << "@cc(objc_method)"; break; case AbstractCC::ObjCMethod: Printer << "@cc(objc_method)"; break;
case AbstractCC::Freestanding: OS << "@cc(freestanding)"; break; case AbstractCC::Freestanding: Printer << "@cc(freestanding)"; break;
case AbstractCC::Method: OS << "@cc(method)"; break; case AbstractCC::Method: Printer << "@cc(method)"; break;
case AbstractCC::WitnessMethod: OS << "@cc(witness_method)"; break; case AbstractCC::WitnessMethod: Printer << "@cc(witness_method)"; break;
} }
} }
} }
@@ -114,45 +128,66 @@ IdentTypeRepr *IdentTypeRepr::createSimple(ASTContext &C, SourceLoc Loc,
return create(C, llvm::makeArrayRef(IdTypeComponent)); return create(C, llvm::makeArrayRef(IdTypeComponent));
} }
static void printGenericArgs(raw_ostream &OS, ArrayRef<TypeRepr *> Args) { static void printGenericArgs(ASTPrinter &Printer, const PrintOptions &Opts,
ArrayRef<TypeRepr *> Args) {
if (Args.empty()) if (Args.empty())
return; return;
OS << '<'; Printer << "<";
bool First = true; bool First = true;
for (auto Arg : Args) { for (auto Arg : Args) {
if (First) if (First)
First = false; First = false;
else else
OS << ", "; Printer << ", ";
OS << Arg; Arg->print(Printer, Opts);
} }
OS << '>'; Printer << ">";
} }
void IdentTypeRepr::printImpl(llvm::raw_ostream &OS) const { void IdentTypeRepr::printImpl(ASTPrinter &Printer,
OS << Components[0].getIdentifier(); const PrintOptions &Opts) const {
printGenericArgs(OS, Components[0].getGenericArgs()); bool isFirst = true;
for (const Component &C : Components) {
if (!isFirst)
Printer << ".";
else
isFirst = false;
for (const Component &C : Components.slice(1, Components.size()-1)) { if (Module *Mod = C.getBoundModule()) {
OS << '.' << C.getIdentifier().get(); Printer.printModuleRef(Mod, C.getIdentifier().str());
printGenericArgs(OS, C.getGenericArgs()); } else if (Type Ty = C.getBoundType()) {
if (auto NTD = Ty->getAnyNominal())
Printer.printTypeRef(NTD, C.getIdentifier().str());
else
Printer << C.getIdentifier().str();
} else {
Printer << C.getIdentifier().str();
}
printGenericArgs(Printer, Opts, C.getGenericArgs());
} }
} }
void FunctionTypeRepr::printImpl(llvm::raw_ostream &OS) const { void FunctionTypeRepr::printImpl(ASTPrinter &Printer,
OS << ArgsTy << " -> " << RetTy; const PrintOptions &Opts) const {
ArgsTy->print(Printer, Opts);
Printer << " -> ";
RetTy->print(Printer, Opts);
} }
void ArrayTypeRepr::printImpl(llvm::raw_ostream &OS) const { void ArrayTypeRepr::printImpl(ASTPrinter &Printer,
OS << Base << '['; const PrintOptions &Opts) const {
Base->print(Printer, Opts);
Printer << "[";
if (Size) if (Size)
Size->getExpr()->print(OS); Size->getExpr()->print(Printer, Opts);
OS << ']'; Printer << "]";
} }
void OptionalTypeRepr::printImpl(llvm::raw_ostream &OS) const { void OptionalTypeRepr::printImpl(ASTPrinter &Printer,
OS << Base << '?'; const PrintOptions &Opts) const {
Base->print(Printer, Opts);
Printer << "?";
} }
TupleTypeRepr *TupleTypeRepr::create(ASTContext &C, TupleTypeRepr *TupleTypeRepr::create(ASTContext &C,
@@ -163,22 +198,24 @@ TupleTypeRepr *TupleTypeRepr::create(ASTContext &C,
Parens, Ellipsis); Parens, Ellipsis);
} }
void TupleTypeRepr::printImpl(llvm::raw_ostream &OS) const { void TupleTypeRepr::printImpl(ASTPrinter &Printer,
OS << '('; const PrintOptions &Opts) const {
Printer << "(";
for (unsigned i = 0, e = Elements.size(); i != e; ++i) { for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
if (i) OS << ", "; if (i) Printer << ", ";
OS << Elements[i]; Elements[i]->print(Printer, Opts);
} }
if (hasEllipsis()) if (hasEllipsis())
OS << "..."; Printer << "...";
OS << ')'; Printer << ")";
} }
void NamedTypeRepr::printImpl(llvm::raw_ostream &OS) const { void NamedTypeRepr::printImpl(ASTPrinter &Printer,
const PrintOptions &Opts) const {
if (!Id.empty()) if (!Id.empty())
OS << Id << " : "; Printer << Id.str() << " : ";
OS << Ty; Ty->print(Printer, Opts);
} }
ProtocolCompositionTypeRepr * ProtocolCompositionTypeRepr *
@@ -190,19 +227,22 @@ ProtocolCompositionTypeRepr::create(ASTContext &C,
ProtocolLoc, AngleBrackets); ProtocolLoc, AngleBrackets);
} }
void ProtocolCompositionTypeRepr::printImpl(llvm::raw_ostream &OS) const { void ProtocolCompositionTypeRepr::printImpl(ASTPrinter &Printer,
OS << "protocol<"; const PrintOptions &Opts) const {
Printer << "protocol<";
bool First = true; bool First = true;
for (auto Proto : Protocols) { for (auto Proto : Protocols) {
if (First) if (First)
First = false; First = false;
else else
OS << ", "; Printer << ", ";
OS << Proto; Proto->print(Printer, Opts);
} }
OS << '>'; Printer << ">";
} }
void MetaTypeTypeRepr::printImpl(llvm::raw_ostream &OS) const { void MetaTypeTypeRepr::printImpl(ASTPrinter &Printer,
OS << Base << ".metatype"; const PrintOptions &Opts) const {
Base->print(Printer, Opts);
Printer << ".metatype";
} }

View File

@@ -1251,7 +1251,7 @@ void SILModule::print(llvm::raw_ostream &OS, bool Verbose,
if ((isa<ValueDecl>(D) || isa<OperatorDecl>(D)) && if ((isa<ValueDecl>(D) || isa<OperatorDecl>(D)) &&
!emittedFunctions.count(D) && !emittedFunctions.count(D) &&
!D->isImplicit()) { !D->isImplicit()) {
D->print(OS, Options, nullptr); D->print(OS, Options);
OS << "\n\n"; OS << "\n\n";
} }
} }