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