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