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