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