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