Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1 | //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Decl::dump method, which pretty print the |
| 11 | // AST back out to C/Objective-C/C++/Objective-C++ code. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclVisitor.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 21 | #include "clang/AST/PrettyPrinter.h" |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame^] | 22 | #include "clang/Basic/Module.h" |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 27 | class DeclPrinter : public DeclVisitor<DeclPrinter> { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 28 | raw_ostream &Out; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 29 | ASTContext &Context; |
| 30 | PrintingPolicy Policy; |
| 31 | unsigned Indentation; |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 32 | bool PrintInstantiation; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 34 | raw_ostream& Indent() { return Indent(Indentation); } |
| 35 | raw_ostream& Indent(unsigned Indentation); |
| 36 | void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 37 | |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 38 | void Print(AccessSpecifier AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 40 | public: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 41 | DeclPrinter(raw_ostream &Out, ASTContext &Context, |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 42 | const PrintingPolicy &Policy, |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 43 | unsigned Indentation = 0, |
| 44 | bool PrintInstantiation = false) |
| 45 | : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation), |
| 46 | PrintInstantiation(PrintInstantiation) { } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 47 | |
| 48 | void VisitDeclContext(DeclContext *DC, bool Indent = true); |
| 49 | |
| 50 | void VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 51 | void VisitTypedefDecl(TypedefDecl *D); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 52 | void VisitTypeAliasDecl(TypeAliasDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 53 | void VisitEnumDecl(EnumDecl *D); |
| 54 | void VisitRecordDecl(RecordDecl *D); |
| 55 | void VisitEnumConstantDecl(EnumConstantDecl *D); |
| 56 | void VisitFunctionDecl(FunctionDecl *D); |
| 57 | void VisitFieldDecl(FieldDecl *D); |
| 58 | void VisitVarDecl(VarDecl *D); |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 59 | void VisitLabelDecl(LabelDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 60 | void VisitParmVarDecl(ParmVarDecl *D); |
| 61 | void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame^] | 62 | void VisitImportDecl(ImportDecl *D); |
Peter Collingbourne | 28ac87e | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 63 | void VisitStaticAssertDecl(StaticAssertDecl *D); |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 64 | void VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 65 | void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 66 | void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 67 | void VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 68 | void VisitLinkageSpecDecl(LinkageSpecDecl *D); |
Richard Smith | 98a5786 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 69 | void VisitTemplateDecl(const TemplateDecl *D); |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 70 | void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| 71 | void VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 72 | void VisitObjCMethodDecl(ObjCMethodDecl *D); |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 73 | void VisitObjCClassDecl(ObjCClassDecl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 74 | void VisitObjCImplementationDecl(ObjCImplementationDecl *D); |
| 75 | void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); |
| 76 | void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D); |
| 77 | void VisitObjCProtocolDecl(ObjCProtocolDecl *D); |
| 78 | void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); |
| 79 | void VisitObjCCategoryDecl(ObjCCategoryDecl *D); |
| 80 | void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); |
| 81 | void VisitObjCPropertyDecl(ObjCPropertyDecl *D); |
| 82 | void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 83 | void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
| 84 | void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
Anders Carlsson | f53eaa5 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 85 | void VisitUsingDecl(UsingDecl *D); |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 86 | void VisitUsingShadowDecl(UsingShadowDecl *D); |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 87 | |
| 88 | void PrintTemplateParameters(const TemplateParameterList *Params, |
| 89 | const TemplateArgumentList *Args); |
Douglas Gregor | 1bea880 | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 90 | void prettyPrintAttributes(Decl *D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 91 | }; |
| 92 | } |
| 93 | |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 94 | void Decl::print(raw_ostream &Out, unsigned Indentation, |
| 95 | bool PrintInstantiation) const { |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 96 | print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 99 | void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 100 | unsigned Indentation, bool PrintInstantiation) const { |
| 101 | DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation); |
Anders Carlsson | f88df86 | 2009-09-26 21:58:53 +0000 | [diff] [blame] | 102 | Printer.Visit(const_cast<Decl*>(this)); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 105 | static QualType GetBaseType(QualType T) { |
| 106 | // FIXME: This should be on the Type class! |
| 107 | QualType BaseType = T; |
| 108 | while (!BaseType->isSpecifierType()) { |
| 109 | if (isa<TypedefType>(BaseType)) |
| 110 | break; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 111 | else if (const PointerType* PTy = BaseType->getAs<PointerType>()) |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 112 | BaseType = PTy->getPointeeType(); |
| 113 | else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) |
| 114 | BaseType = ATy->getElementType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 115 | else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 116 | BaseType = FTy->getResultType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 117 | else if (const VectorType *VTy = BaseType->getAs<VectorType>()) |
Douglas Gregor | 5068ab6 | 2009-07-01 23:58:14 +0000 | [diff] [blame] | 118 | BaseType = VTy->getElementType(); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 119 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 120 | llvm_unreachable("Unknown declarator!"); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 121 | } |
| 122 | return BaseType; |
| 123 | } |
| 124 | |
| 125 | static QualType getDeclType(Decl* D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 126 | if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 127 | return TDD->getUnderlyingType(); |
| 128 | if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) |
| 129 | return VD->getType(); |
| 130 | return QualType(); |
| 131 | } |
| 132 | |
| 133 | void Decl::printGroup(Decl** Begin, unsigned NumDecls, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 134 | raw_ostream &Out, const PrintingPolicy &Policy, |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 135 | unsigned Indentation) { |
| 136 | if (NumDecls == 1) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 137 | (*Begin)->print(Out, Policy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
| 141 | Decl** End = Begin + NumDecls; |
| 142 | TagDecl* TD = dyn_cast<TagDecl>(*Begin); |
| 143 | if (TD) |
| 144 | ++Begin; |
| 145 | |
| 146 | PrintingPolicy SubPolicy(Policy); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 147 | if (TD && TD->isCompleteDefinition()) { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 148 | TD->print(Out, Policy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 149 | Out << " "; |
| 150 | SubPolicy.SuppressTag = true; |
| 151 | } |
| 152 | |
| 153 | bool isFirst = true; |
| 154 | for ( ; Begin != End; ++Begin) { |
| 155 | if (isFirst) { |
| 156 | SubPolicy.SuppressSpecifiers = false; |
| 157 | isFirst = false; |
| 158 | } else { |
| 159 | if (!isFirst) Out << ", "; |
| 160 | SubPolicy.SuppressSpecifiers = true; |
| 161 | } |
| 162 | |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 163 | (*Begin)->print(Out, SubPolicy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Anders Carlsson | 8483443 | 2009-12-14 00:51:04 +0000 | [diff] [blame] | 167 | void DeclContext::dumpDeclContext() const { |
Anders Carlsson | 2b7d8dd | 2009-12-09 17:27:46 +0000 | [diff] [blame] | 168 | // Get the translation unit |
| 169 | const DeclContext *DC = this; |
| 170 | while (!DC->isTranslationUnit()) |
| 171 | DC = DC->getParent(); |
| 172 | |
| 173 | ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 174 | DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0); |
Anders Carlsson | 2b7d8dd | 2009-12-09 17:27:46 +0000 | [diff] [blame] | 175 | Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); |
| 176 | } |
| 177 | |
Anders Carlsson | f88df86 | 2009-09-26 21:58:53 +0000 | [diff] [blame] | 178 | void Decl::dump() const { |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 179 | print(llvm::errs()); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 182 | raw_ostream& DeclPrinter::Indent(unsigned Indentation) { |
Daniel Dunbar | 512ce60 | 2009-11-21 09:12:06 +0000 | [diff] [blame] | 183 | for (unsigned i = 0; i != Indentation; ++i) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 184 | Out << " "; |
| 185 | return Out; |
| 186 | } |
| 187 | |
Douglas Gregor | 1bea880 | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 188 | void DeclPrinter::prettyPrintAttributes(Decl *D) { |
| 189 | if (D->hasAttrs()) { |
| 190 | AttrVec &Attrs = D->getAttrs(); |
| 191 | for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) { |
| 192 | Attr *A = *i; |
| 193 | A->printPretty(Out, Context); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 198 | void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 199 | this->Indent(); |
Argyrios Kyrtzidis | f1d60ea | 2009-06-30 02:35:04 +0000 | [diff] [blame] | 200 | Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 201 | Out << ";\n"; |
| 202 | Decls.clear(); |
| 203 | |
| 204 | } |
| 205 | |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 206 | void DeclPrinter::Print(AccessSpecifier AS) { |
| 207 | switch(AS) { |
David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 208 | case AS_none: llvm_unreachable("No access specifier!"); |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 209 | case AS_public: Out << "public"; break; |
| 210 | case AS_protected: Out << "protected"; break; |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 211 | case AS_private: Out << "private"; break; |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 215 | //---------------------------------------------------------------------------- |
| 216 | // Common C declarations |
| 217 | //---------------------------------------------------------------------------- |
| 218 | |
| 219 | void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { |
| 220 | if (Indent) |
| 221 | Indentation += Policy.Indentation; |
| 222 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 223 | SmallVector<Decl*, 2> Decls; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 224 | for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 225 | D != DEnd; ++D) { |
Ted Kremenek | 2d6c906 | 2010-07-30 00:47:46 +0000 | [diff] [blame] | 226 | |
| 227 | // Don't print ObjCIvarDecls, as they are printed when visiting the |
| 228 | // containing ObjCInterfaceDecl. |
| 229 | if (isa<ObjCIvarDecl>(*D)) |
| 230 | continue; |
| 231 | |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 232 | if (!Policy.Dump) { |
| 233 | // Skip over implicit declarations in pretty-printing mode. |
| 234 | if (D->isImplicit()) continue; |
Eli Friedman | 3d4a7c9 | 2009-05-30 06:35:22 +0000 | [diff] [blame] | 235 | // FIXME: Ugly hack so we don't pretty-print the builtin declaration |
Argyrios Kyrtzidis | 2574f6f | 2010-06-17 10:52:11 +0000 | [diff] [blame] | 236 | // of __builtin_va_list or __[u]int128_t. There should be some other way |
| 237 | // to check that. |
| 238 | if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) { |
| 239 | if (IdentifierInfo *II = ND->getIdentifier()) { |
| 240 | if (II->isStr("__builtin_va_list") || |
| 241 | II->isStr("__int128_t") || II->isStr("__uint128_t")) |
| 242 | continue; |
| 243 | } |
| 244 | } |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 247 | // The next bits of code handles stuff like "struct {int x;} a,b"; we're |
| 248 | // forced to merge the declarations because there's no other way to |
| 249 | // refer to the struct in question. This limited merging is safe without |
| 250 | // a bunch of other checks because it only merges declarations directly |
| 251 | // referring to the tag, not typedefs. |
| 252 | // |
| 253 | // Check whether the current declaration should be grouped with a previous |
| 254 | // unnamed struct. |
| 255 | QualType CurDeclType = getDeclType(*D); |
| 256 | if (!Decls.empty() && !CurDeclType.isNull()) { |
| 257 | QualType BaseType = GetBaseType(CurDeclType); |
| 258 | if (!BaseType.isNull() && isa<TagType>(BaseType) && |
| 259 | cast<TagType>(BaseType)->getDecl() == Decls[0]) { |
| 260 | Decls.push_back(*D); |
| 261 | continue; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // If we have a merged group waiting to be handled, handle it now. |
| 266 | if (!Decls.empty()) |
| 267 | ProcessDeclGroup(Decls); |
| 268 | |
| 269 | // If the current declaration is an unnamed tag type, save it |
| 270 | // so we can merge it with the subsequent declaration(s) using it. |
| 271 | if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) { |
| 272 | Decls.push_back(*D); |
| 273 | continue; |
| 274 | } |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 275 | |
| 276 | if (isa<AccessSpecDecl>(*D)) { |
| 277 | Indentation -= Policy.Indentation; |
| 278 | this->Indent(); |
| 279 | Print(D->getAccess()); |
| 280 | Out << ":\n"; |
| 281 | Indentation += Policy.Indentation; |
| 282 | continue; |
| 283 | } |
| 284 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 285 | this->Indent(); |
| 286 | Visit(*D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
| 288 | // FIXME: Need to be able to tell the DeclPrinter when |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 289 | const char *Terminator = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | if (isa<FunctionDecl>(*D) && |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 291 | cast<FunctionDecl>(*D)->isThisDeclarationADefinition()) |
| 292 | Terminator = 0; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 293 | else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody()) |
| 294 | Terminator = 0; |
| 295 | else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | isa<ObjCImplementationDecl>(*D) || |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 297 | isa<ObjCInterfaceDecl>(*D) || |
| 298 | isa<ObjCProtocolDecl>(*D) || |
| 299 | isa<ObjCCategoryImplDecl>(*D) || |
| 300 | isa<ObjCCategoryDecl>(*D)) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 301 | Terminator = 0; |
| 302 | else if (isa<EnumConstantDecl>(*D)) { |
| 303 | DeclContext::decl_iterator Next = D; |
| 304 | ++Next; |
| 305 | if (Next != DEnd) |
| 306 | Terminator = ","; |
| 307 | } else |
| 308 | Terminator = ";"; |
| 309 | |
| 310 | if (Terminator) |
| 311 | Out << Terminator; |
| 312 | Out << "\n"; |
| 313 | } |
| 314 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 315 | if (!Decls.empty()) |
| 316 | ProcessDeclGroup(Decls); |
| 317 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 318 | if (Indent) |
| 319 | Indentation -= Policy.Indentation; |
| 320 | } |
| 321 | |
| 322 | void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 323 | VisitDeclContext(D, false); |
| 324 | } |
| 325 | |
| 326 | void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { |
| 327 | std::string S = D->getNameAsString(); |
| 328 | D->getUnderlyingType().getAsStringInternal(S, Policy); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 329 | if (!Policy.SuppressSpecifiers) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 330 | Out << "typedef "; |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 331 | |
| 332 | if (D->isModulePrivate()) |
| 333 | Out << "__module_private__ "; |
| 334 | } |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 335 | Out << S; |
Douglas Gregor | 1bea880 | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 336 | prettyPrintAttributes(D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 339 | void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| 340 | Out << "using " << D->getNameAsString() << " = " |
| 341 | << D->getUnderlyingType().getAsString(Policy); |
| 342 | } |
| 343 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 344 | void DeclPrinter::VisitEnumDecl(EnumDecl *D) { |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 345 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 346 | Out << "__module_private__ "; |
Douglas Gregor | 9fa8c46 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 347 | Out << "enum "; |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 348 | if (D->isScoped()) { |
| 349 | if (D->isScopedUsingClassTag()) |
| 350 | Out << "class "; |
| 351 | else |
| 352 | Out << "struct "; |
| 353 | } |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 354 | Out << *D; |
Douglas Gregor | 9fa8c46 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 355 | |
| 356 | if (D->isFixed()) { |
| 357 | std::string Underlying; |
| 358 | D->getIntegerType().getAsStringInternal(Underlying, Policy); |
| 359 | Out << " : " << Underlying; |
| 360 | } |
| 361 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 362 | if (D->isCompleteDefinition()) { |
Douglas Gregor | 9fa8c46 | 2010-12-01 16:01:08 +0000 | [diff] [blame] | 363 | Out << " {\n"; |
| 364 | VisitDeclContext(D); |
| 365 | Indent() << "}"; |
| 366 | } |
Douglas Gregor | 1bea880 | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 367 | prettyPrintAttributes(D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void DeclPrinter::VisitRecordDecl(RecordDecl *D) { |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 371 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 372 | Out << "__module_private__ "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 373 | Out << D->getKindName(); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 374 | if (D->getIdentifier()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 375 | Out << ' ' << *D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 377 | if (D->isCompleteDefinition()) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 378 | Out << " {\n"; |
| 379 | VisitDeclContext(D); |
| 380 | Indent() << "}"; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 385 | Out << *D; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 386 | if (Expr *Init = D->getInitExpr()) { |
| 387 | Out << " = "; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 388 | Init->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 393 | if (!Policy.SuppressSpecifiers) { |
Peter Collingbourne | 13330c4 | 2011-11-08 02:52:52 +0000 | [diff] [blame] | 394 | switch (D->getStorageClassAsWritten()) { |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 395 | case SC_None: break; |
| 396 | case SC_Extern: Out << "extern "; break; |
| 397 | case SC_Static: Out << "static "; break; |
| 398 | case SC_PrivateExtern: Out << "__private_extern__ "; break; |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 399 | case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: |
| 400 | llvm_unreachable("invalid for functions"); |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 401 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 403 | if (D->isInlineSpecified()) Out << "inline "; |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 404 | if (D->isVirtualAsWritten()) Out << "virtual "; |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 405 | if (D->isModulePrivate()) Out << "__module_private__ "; |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 406 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 408 | PrintingPolicy SubPolicy(Policy); |
| 409 | SubPolicy.SuppressSpecifiers = false; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 410 | std::string Proto = D->getNameInfo().getAsString(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 411 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 412 | QualType Ty = D->getType(); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 413 | while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 414 | Proto = '(' + Proto + ')'; |
| 415 | Ty = PT->getInnerType(); |
| 416 | } |
| 417 | |
| 418 | if (isa<FunctionType>(Ty)) { |
| 419 | const FunctionType *AFT = Ty->getAs<FunctionType>(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 420 | const FunctionProtoType *FT = 0; |
| 421 | if (D->hasWrittenPrototype()) |
| 422 | FT = dyn_cast<FunctionProtoType>(AFT); |
| 423 | |
| 424 | Proto += "("; |
| 425 | if (FT) { |
| 426 | llvm::raw_string_ostream POut(Proto); |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 427 | DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 428 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 429 | if (i) POut << ", "; |
| 430 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
| 431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 433 | if (FT->isVariadic()) { |
| 434 | if (D->getNumParams()) POut << ", "; |
| 435 | POut << "..."; |
| 436 | } |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 437 | } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 438 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 439 | if (i) |
| 440 | Proto += ", "; |
| 441 | Proto += D->getParamDecl(i)->getNameAsString(); |
| 442 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | Proto += ")"; |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 446 | |
Douglas Gregor | b95cfe4 | 2010-11-19 18:44:34 +0000 | [diff] [blame] | 447 | if (FT && FT->getTypeQuals()) { |
| 448 | unsigned TypeQuals = FT->getTypeQuals(); |
| 449 | if (TypeQuals & Qualifiers::Const) |
| 450 | Proto += " const"; |
| 451 | if (TypeQuals & Qualifiers::Volatile) |
| 452 | Proto += " volatile"; |
| 453 | if (TypeQuals & Qualifiers::Restrict) |
| 454 | Proto += " restrict"; |
| 455 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 456 | |
| 457 | if (FT && FT->hasDynamicExceptionSpec()) { |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 458 | Proto += " throw("; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 459 | if (FT->getExceptionSpecType() == EST_MSAny) |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 460 | Proto += "..."; |
| 461 | else |
| 462 | for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { |
| 463 | if (I) |
| 464 | Proto += ", "; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 466 | std::string ExceptionType; |
| 467 | FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy); |
| 468 | Proto += ExceptionType; |
| 469 | } |
| 470 | Proto += ")"; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 471 | } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { |
| 472 | Proto += " noexcept"; |
| 473 | if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { |
| 474 | Proto += "("; |
| 475 | llvm::raw_string_ostream EOut(Proto); |
| 476 | FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy, |
| 477 | Indentation); |
| 478 | EOut.flush(); |
| 479 | Proto += EOut.str(); |
| 480 | Proto += ")"; |
| 481 | } |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 484 | if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 485 | bool HasInitializerList = false; |
| 486 | for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), |
| 487 | E = CDecl->init_end(); |
| 488 | B != E; ++B) { |
| 489 | CXXCtorInitializer * BMInitializer = (*B); |
| 490 | if (BMInitializer->isInClassMemberInitializer()) |
| 491 | continue; |
| 492 | |
| 493 | if (!HasInitializerList) { |
| 494 | Proto += " : "; |
| 495 | Out << Proto; |
| 496 | Proto.clear(); |
| 497 | HasInitializerList = true; |
| 498 | } else |
| 499 | Out << ", "; |
| 500 | |
| 501 | if (BMInitializer->isAnyMemberInitializer()) { |
| 502 | FieldDecl *FD = BMInitializer->getAnyMember(); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 503 | Out << *FD; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 504 | } else { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 505 | Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | Out << "("; |
| 509 | if (!BMInitializer->getInit()) { |
| 510 | // Nothing to print |
| 511 | } else { |
| 512 | Expr *Init = BMInitializer->getInit(); |
| 513 | if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) |
| 514 | Init = Tmp->getSubExpr(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 515 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 516 | Init = Init->IgnoreParens(); |
| 517 | |
| 518 | Expr *SimpleInit = 0; |
| 519 | Expr **Args = 0; |
| 520 | unsigned NumArgs = 0; |
| 521 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 522 | Args = ParenList->getExprs(); |
| 523 | NumArgs = ParenList->getNumExprs(); |
| 524 | } else if (CXXConstructExpr *Construct |
| 525 | = dyn_cast<CXXConstructExpr>(Init)) { |
| 526 | Args = Construct->getArgs(); |
| 527 | NumArgs = Construct->getNumArgs(); |
| 528 | } else |
| 529 | SimpleInit = Init; |
| 530 | |
| 531 | if (SimpleInit) |
| 532 | SimpleInit->printPretty(Out, Context, 0, Policy, Indentation); |
| 533 | else { |
| 534 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 535 | if (isa<CXXDefaultArgExpr>(Args[I])) |
| 536 | break; |
| 537 | |
| 538 | if (I) |
| 539 | Out << ", "; |
| 540 | Args[I]->printPretty(Out, Context, 0, Policy, Indentation); |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 541 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 542 | } |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 543 | } |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 544 | Out << ")"; |
Fariborz Jahanian | 66192ad | 2009-07-13 20:18:13 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | else |
| 548 | AFT->getResultType().getAsStringInternal(Proto, Policy); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 549 | } else { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 550 | Ty.getAsStringInternal(Proto, Policy); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | Out << Proto; |
Douglas Gregor | 1bea880 | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 554 | prettyPrintAttributes(D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 555 | |
| 556 | if (D->isPure()) |
| 557 | Out << " = 0"; |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 558 | else if (D->isDeletedAsWritten()) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 559 | Out << " = delete"; |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 560 | else if (D->doesThisDeclarationHaveABody()) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 561 | if (!D->hasPrototype() && D->getNumParams()) { |
| 562 | // This is a K&R function definition, so we need to print the |
| 563 | // parameters. |
| 564 | Out << '\n'; |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 565 | DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 566 | Indentation += Policy.Indentation; |
| 567 | for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { |
| 568 | Indent(); |
Douglas Gregor | 6620a62 | 2009-05-30 05:39:39 +0000 | [diff] [blame] | 569 | ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 570 | Out << ";\n"; |
| 571 | } |
| 572 | Indentation -= Policy.Indentation; |
| 573 | } else |
| 574 | Out << ' '; |
| 575 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 576 | D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 577 | Out << '\n'; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void DeclPrinter::VisitFieldDecl(FieldDecl *D) { |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 582 | if (!Policy.SuppressSpecifiers && D->isMutable()) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 583 | Out << "mutable "; |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 584 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 585 | Out << "__module_private__ "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 586 | |
| 587 | std::string Name = D->getNameAsString(); |
| 588 | D->getType().getAsStringInternal(Name, Policy); |
| 589 | Out << Name; |
| 590 | |
| 591 | if (D->isBitField()) { |
| 592 | Out << " : "; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 593 | D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 594 | } |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 595 | |
| 596 | Expr *Init = D->getInClassInitializer(); |
| 597 | if (!Policy.SuppressInitializers && Init) { |
| 598 | Out << " = "; |
| 599 | Init->printPretty(Out, Context, 0, Policy, Indentation); |
| 600 | } |
Douglas Gregor | 1bea880 | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 601 | prettyPrintAttributes(D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 604 | void DeclPrinter::VisitLabelDecl(LabelDecl *D) { |
| 605 | Out << D->getNameAsString() << ":"; |
| 606 | } |
| 607 | |
| 608 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 609 | void DeclPrinter::VisitVarDecl(VarDecl *D) { |
Peter Collingbourne | 13330c4 | 2011-11-08 02:52:52 +0000 | [diff] [blame] | 610 | StorageClass SCAsWritten = D->getStorageClassAsWritten(); |
| 611 | if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None) |
| 612 | Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 613 | |
Eli Friedman | 42f42c0 | 2009-05-30 04:20:30 +0000 | [diff] [blame] | 614 | if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 615 | Out << "__thread "; |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 616 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 617 | Out << "__module_private__ "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 618 | |
| 619 | std::string Name = D->getNameAsString(); |
| 620 | QualType T = D->getType(); |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 621 | if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 622 | T = Parm->getOriginalType(); |
| 623 | T.getAsStringInternal(Name, Policy); |
| 624 | Out << Name; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 625 | Expr *Init = D->getInit(); |
| 626 | if (!Policy.SuppressInitializers && Init) { |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 627 | if (D->hasCXXDirectInitializer()) |
| 628 | Out << "("; |
Ted Kremenek | c57d655 | 2010-09-17 23:04:38 +0000 | [diff] [blame] | 629 | else { |
| 630 | CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init); |
Jim Goodnow II | 5eca37c | 2011-10-30 11:17:39 +0000 | [diff] [blame] | 631 | if (!CCE || CCE->getConstructor()->isCopyOrMoveConstructor()) |
Ted Kremenek | c57d655 | 2010-09-17 23:04:38 +0000 | [diff] [blame] | 632 | Out << " = "; |
| 633 | } |
Ted Kremenek | bccfd31 | 2010-09-07 22:21:59 +0000 | [diff] [blame] | 634 | Init->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 635 | if (D->hasCXXDirectInitializer()) |
| 636 | Out << ")"; |
| 637 | } |
Douglas Gregor | 1bea880 | 2011-11-19 19:22:57 +0000 | [diff] [blame] | 638 | prettyPrintAttributes(D); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { |
| 642 | VisitVarDecl(D); |
| 643 | } |
| 644 | |
| 645 | void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { |
| 646 | Out << "__asm ("; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 647 | D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 648 | Out << ")"; |
| 649 | } |
| 650 | |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame^] | 651 | void DeclPrinter::VisitImportDecl(ImportDecl *D) { |
| 652 | Out << "__import_module__ " << D->getImportedModule()->getFullModuleName() |
| 653 | << ";\n"; |
| 654 | } |
| 655 | |
Peter Collingbourne | 28ac87e | 2011-03-16 18:37:27 +0000 | [diff] [blame] | 656 | void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 657 | Out << "static_assert("; |
| 658 | D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation); |
| 659 | Out << ", "; |
| 660 | D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation); |
| 661 | Out << ")"; |
| 662 | } |
| 663 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 664 | //---------------------------------------------------------------------------- |
| 665 | // C++ declarations |
| 666 | //---------------------------------------------------------------------------- |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 667 | void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 668 | Out << "namespace " << *D << " {\n"; |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 669 | VisitDeclContext(D); |
| 670 | Indent() << "}"; |
| 671 | } |
| 672 | |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 673 | void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 674 | Out << "using namespace "; |
| 675 | if (D->getQualifier()) |
| 676 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 677 | Out << *D->getNominatedNamespaceAsWritten(); |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 680 | void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 681 | Out << "namespace " << *D << " = "; |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 682 | if (D->getQualifier()) |
| 683 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 684 | Out << *D->getAliasedNamespace(); |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 687 | void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 688 | if (!Policy.SuppressSpecifiers && D->isModulePrivate()) |
| 689 | Out << "__module_private__ "; |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 690 | Out << D->getKindName(); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 691 | if (D->getIdentifier()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 692 | Out << ' ' << *D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 694 | if (D->isCompleteDefinition()) { |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 695 | // Print the base classes |
| 696 | if (D->getNumBases()) { |
| 697 | Out << " : "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), |
| 699 | BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 700 | if (Base != D->bases_begin()) |
| 701 | Out << ", "; |
| 702 | |
| 703 | if (Base->isVirtual()) |
| 704 | Out << "virtual "; |
| 705 | |
Anders Carlsson | 018e9fe | 2009-08-29 20:36:12 +0000 | [diff] [blame] | 706 | AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); |
| 707 | if (AS != AS_none) |
| 708 | Print(AS); |
Anders Carlsson | 0d59292 | 2009-08-28 22:39:52 +0000 | [diff] [blame] | 709 | Out << " " << Base->getType().getAsString(Policy); |
Douglas Gregor | 34a99e7 | 2011-04-27 17:07:55 +0000 | [diff] [blame] | 710 | |
| 711 | if (Base->isPackExpansion()) |
| 712 | Out << "..."; |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | // Print the class definition |
Douglas Gregor | f757ae7 | 2009-05-31 07:13:39 +0000 | [diff] [blame] | 717 | // FIXME: Doesn't print access specifiers, e.g., "public:" |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 718 | Out << " {\n"; |
| 719 | VisitDeclContext(D); |
| 720 | Indent() << "}"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { |
| 725 | const char *l; |
| 726 | if (D->getLanguage() == LinkageSpecDecl::lang_c) |
| 727 | l = "C"; |
| 728 | else { |
| 729 | assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && |
| 730 | "unknown language in linkage specification"); |
| 731 | l = "C++"; |
| 732 | } |
| 733 | |
| 734 | Out << "extern \"" << l << "\" "; |
| 735 | if (D->hasBraces()) { |
| 736 | Out << "{\n"; |
| 737 | VisitDeclContext(D); |
| 738 | Indent() << "}"; |
| 739 | } else |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 740 | Visit(*D->decls_begin()); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 743 | void DeclPrinter::PrintTemplateParameters( |
| 744 | const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) { |
| 745 | assert(Params); |
| 746 | assert(!Args || Params->size() == Args->size()); |
| 747 | |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 748 | Out << "template <"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 750 | for (unsigned i = 0, e = Params->size(); i != e; ++i) { |
| 751 | if (i != 0) |
| 752 | Out << ", "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 754 | const Decl *Param = Params->getParam(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | if (const TemplateTypeParmDecl *TTP = |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 756 | dyn_cast<TemplateTypeParmDecl>(Param)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 758 | if (TTP->wasDeclaredWithTypename()) |
| 759 | Out << "typename "; |
| 760 | else |
| 761 | Out << "class "; |
| 762 | |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 763 | if (TTP->isParameterPack()) |
| 764 | Out << "... "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 766 | Out << TTP->getNameAsString(); |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 767 | |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 768 | if (Args) { |
| 769 | Out << " = "; |
| 770 | Args->get(i).print(Policy, Out); |
| 771 | } else if (TTP->hasDefaultArgument()) { |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 772 | Out << " = "; |
| 773 | Out << TTP->getDefaultArgument().getAsString(Policy); |
| 774 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | } else if (const NonTypeTemplateParmDecl *NTTP = |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 776 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 777 | Out << NTTP->getType().getAsString(Policy); |
| 778 | |
Douglas Gregor | 56bc983 | 2010-12-24 00:15:10 +0000 | [diff] [blame] | 779 | if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType())) |
| 780 | Out << "..."; |
| 781 | |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 782 | if (IdentifierInfo *Name = NTTP->getIdentifier()) { |
| 783 | Out << ' '; |
| 784 | Out << Name->getName(); |
| 785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 787 | if (Args) { |
| 788 | Out << " = "; |
| 789 | Args->get(i).print(Policy, Out); |
| 790 | } else if (NTTP->hasDefaultArgument()) { |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 791 | Out << " = "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy, |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 793 | Indentation); |
| 794 | } |
Richard Smith | 98a5786 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 795 | } else if (const TemplateTemplateParmDecl *TTPD = |
| 796 | dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 797 | VisitTemplateDecl(TTPD); |
| 798 | // FIXME: print the default argument, if present. |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 802 | Out << "> "; |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { |
| 806 | PrintTemplateParameters(D->getTemplateParameters()); |
Anders Carlsson | 0487f66 | 2009-06-04 05:37:43 +0000 | [diff] [blame] | 807 | |
Richard Smith | 98a5786 | 2011-04-15 13:38:57 +0000 | [diff] [blame] | 808 | if (const TemplateTemplateParmDecl *TTP = |
| 809 | dyn_cast<TemplateTemplateParmDecl>(D)) { |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 810 | Out << "class "; |
| 811 | if (TTP->isParameterPack()) |
| 812 | Out << "..."; |
| 813 | Out << D->getName(); |
Craig Silverstein | 0193a72 | 2010-07-09 20:25:10 +0000 | [diff] [blame] | 814 | } else { |
| 815 | Visit(D->getTemplatedDecl()); |
| 816 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Richard Trieu | 5cb3d69 | 2011-07-28 00:19:05 +0000 | [diff] [blame] | 819 | void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 820 | if (PrintInstantiation) { |
| 821 | TemplateParameterList *Params = D->getTemplateParameters(); |
| 822 | for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); |
| 823 | I != E; ++I) { |
| 824 | PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs()); |
| 825 | Visit(*I); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return VisitRedeclarableTemplateDecl(D); |
| 830 | } |
| 831 | |
| 832 | void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 833 | if (PrintInstantiation) { |
| 834 | TemplateParameterList *Params = D->getTemplateParameters(); |
| 835 | for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); |
| 836 | I != E; ++I) { |
| 837 | PrintTemplateParameters(Params, &(*I)->getTemplateArgs()); |
| 838 | Visit(*I); |
| 839 | Out << '\n'; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return VisitRedeclarableTemplateDecl(D); |
| 844 | } |
| 845 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 846 | //---------------------------------------------------------------------------- |
| 847 | // Objective-C declarations |
| 848 | //---------------------------------------------------------------------------- |
| 849 | |
| 850 | void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 851 | Out << "@class " << *D->getForwardInterfaceDecl(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { |
| 855 | if (OMD->isInstanceMethod()) |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 856 | Out << "- "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | else |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 858 | Out << "+ "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 859 | if (!OMD->getResultType().isNull()) |
Douglas Gregor | 59e6357 | 2009-05-30 06:58:37 +0000 | [diff] [blame] | 860 | Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 862 | std::string name = OMD->getSelector().getAsString(); |
| 863 | std::string::size_type pos, lastPos = 0; |
| 864 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 865 | E = OMD->param_end(); PI != E; ++PI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | // FIXME: selector is missing here! |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 867 | pos = name.find_first_of(':', lastPos); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 868 | Out << " " << name.substr(lastPos, pos - lastPos); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 869 | Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 870 | lastPos = pos + 1; |
| 871 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 873 | if (OMD->param_begin() == OMD->param_end()) |
| 874 | Out << " " << name; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 876 | if (OMD->isVariadic()) |
| 877 | Out << ", ..."; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 879 | if (OMD->getBody()) { |
| 880 | Out << ' '; |
Eli Friedman | 48d14a2 | 2009-05-30 05:03:24 +0000 | [diff] [blame] | 881 | OMD->getBody()->printPretty(Out, Context, 0, Policy); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 882 | Out << '\n'; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 883 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { |
| 887 | std::string I = OID->getNameAsString(); |
| 888 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 889 | |
| 890 | if (SID) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 891 | Out << "@implementation " << I << " : " << *SID; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 892 | else |
| 893 | Out << "@implementation " << I; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 894 | Out << "\n"; |
| 895 | VisitDeclContext(OID, false); |
| 896 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { |
| 900 | std::string I = OID->getNameAsString(); |
| 901 | ObjCInterfaceDecl *SID = OID->getSuperClass(); |
| 902 | |
| 903 | if (SID) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 904 | Out << "@interface " << I << " : " << *SID; |
Douglas Gregor | deacbdc | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 905 | else |
| 906 | Out << "@interface " << I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 908 | // Protocols? |
| 909 | const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); |
| 910 | if (!Protocols.empty()) { |
| 911 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 912 | E = Protocols.end(); I != E; ++I) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 913 | Out << (I == Protocols.begin() ? '<' : ',') << **I; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 914 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 916 | if (!Protocols.empty()) |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 917 | Out << "> "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 919 | if (OID->ivar_size() > 0) { |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 920 | Out << "{\n"; |
| 921 | Indentation += Policy.Indentation; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 922 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 923 | E = OID->ivar_end(); I != E; ++I) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 924 | Indent() << (*I)->getType().getAsString(Policy) << ' ' << **I << ";\n"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 925 | } |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 926 | Indentation -= Policy.Indentation; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 927 | Out << "}\n"; |
| 928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 930 | VisitDeclContext(OID, false); |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 931 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 932 | // FIXME: implement the rest... |
| 933 | } |
| 934 | |
| 935 | void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) { |
| 936 | Out << "@protocol "; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(), |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 938 | E = D->protocol_end(); |
| 939 | I != E; ++I) { |
| 940 | if (I != D->protocol_begin()) Out << ", "; |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 941 | Out << **I; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 942 | } |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 946 | Out << "@protocol " << *PID << '\n'; |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 947 | VisitDeclContext(PID, false); |
| 948 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 952 | Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 953 | |
| 954 | VisitDeclContext(PID, false); |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 955 | Out << "@end"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 956 | // FIXME: implement the rest... |
| 957 | } |
| 958 | |
| 959 | void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 960 | Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 961 | VisitDeclContext(PID, false); |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 962 | Out << "@end"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 964 | // FIXME: implement the rest... |
| 965 | } |
| 966 | |
| 967 | void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 968 | Out << "@compatibility_alias " << *AID |
| 969 | << ' ' << *AID->getClassInterface() << ";\n"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /// PrintObjCPropertyDecl - print a property declaration. |
| 973 | /// |
| 974 | void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { |
| 975 | if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) |
| 976 | Out << "@required\n"; |
| 977 | else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 978 | Out << "@optional\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 980 | Out << "@property"; |
| 981 | if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { |
| 982 | bool first = true; |
| 983 | Out << " ("; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | if (PDecl->getPropertyAttributes() & |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 985 | ObjCPropertyDecl::OBJC_PR_readonly) { |
| 986 | Out << (first ? ' ' : ',') << "readonly"; |
| 987 | first = false; |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 988 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 990 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 991 | Out << (first ? ' ' : ',') << "getter = " |
| 992 | << PDecl->getGetterName().getAsString(); |
| 993 | first = false; |
| 994 | } |
| 995 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 996 | Out << (first ? ' ' : ',') << "setter = " |
| 997 | << PDecl->getSetterName().getAsString(); |
| 998 | first = false; |
| 999 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1001 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { |
| 1002 | Out << (first ? ' ' : ',') << "assign"; |
| 1003 | first = false; |
| 1004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1006 | if (PDecl->getPropertyAttributes() & |
| 1007 | ObjCPropertyDecl::OBJC_PR_readwrite) { |
| 1008 | Out << (first ? ' ' : ',') << "readwrite"; |
| 1009 | first = false; |
| 1010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1012 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { |
| 1013 | Out << (first ? ' ' : ',') << "retain"; |
| 1014 | first = false; |
| 1015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1017 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { |
| 1018 | Out << (first ? ' ' : ',') << "strong"; |
| 1019 | first = false; |
| 1020 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1021 | |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1022 | if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { |
| 1023 | Out << (first ? ' ' : ',') << "copy"; |
| 1024 | first = false; |
| 1025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | |
Ted Kremenek | 07c682a | 2011-08-17 21:09:35 +0000 | [diff] [blame] | 1027 | if (PDecl->getPropertyAttributes() & |
| 1028 | ObjCPropertyDecl::OBJC_PR_nonatomic) { |
| 1029 | Out << (first ? ' ' : ',') << "nonatomic"; |
| 1030 | first = false; |
| 1031 | } |
| 1032 | if (PDecl->getPropertyAttributes() & |
| 1033 | ObjCPropertyDecl::OBJC_PR_atomic) { |
| 1034 | Out << (first ? ' ' : ',') << "atomic"; |
| 1035 | first = false; |
| 1036 | } |
| 1037 | |
| 1038 | (void) first; // Silence dead store warning due to idiomatic code. |
| 1039 | Out << " )"; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1040 | } |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1041 | Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { |
| 1045 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1046 | Out << "@synthesize "; |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1047 | else |
Douglas Gregor | 64f6500 | 2009-05-30 00:56:08 +0000 | [diff] [blame] | 1048 | Out << "@dynamic "; |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1049 | Out << *PID->getPropertyDecl(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1050 | if (PID->getPropertyIvarDecl()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1051 | Out << '=' << *PID->getPropertyIvarDecl(); |
Douglas Gregor | 4fe0c8e | 2009-05-30 00:08:05 +0000 | [diff] [blame] | 1052 | } |
Anders Carlsson | f53eaa5 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1053 | |
| 1054 | void DeclPrinter::VisitUsingDecl(UsingDecl *D) { |
| 1055 | Out << "using "; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1056 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1057 | Out << *D; |
Anders Carlsson | f53eaa5 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1060 | void |
| 1061 | DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
| 1062 | Out << "using typename "; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1063 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1064 | Out << D->getDeclName(); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Anders Carlsson | f53eaa5 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1068 | Out << "using "; |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1069 | D->getQualifier()->print(Out, Policy); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 1070 | Out << D->getDeclName(); |
Anders Carlsson | f53eaa5 | 2009-08-28 19:16:39 +0000 | [diff] [blame] | 1071 | } |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1072 | |
| 1073 | void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| 1074 | // ignore |
| 1075 | } |