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