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