blob: 278163238493b742e3e6fa3c853627795e495302 [file] [log] [blame]
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Douglas Gregor9db7dbb2010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000022#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
25namespace {
Benjamin Kramer770b4a82009-11-28 19:03:38 +000026 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000027 llvm::raw_ostream &Out;
28 ASTContext &Context;
29 PrintingPolicy Policy;
30 unsigned Indentation;
31
Daniel Dunbar512ce602009-11-21 09:12:06 +000032 llvm::raw_ostream& Indent() { return Indent(Indentation); }
33 llvm::raw_ostream& Indent(unsigned Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +000034 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000035
Anders Carlsson0d592922009-08-28 22:39:52 +000036 void Print(AccessSpecifier AS);
Mike Stump1eb44332009-09-09 15:08:12 +000037
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000038 public:
Mike Stump1eb44332009-09-09 15:08:12 +000039 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000040 const PrintingPolicy &Policy,
41 unsigned Indentation = 0)
42 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
43
44 void VisitDeclContext(DeclContext *DC, bool Indent = true);
45
46 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
47 void VisitTypedefDecl(TypedefDecl *D);
48 void VisitEnumDecl(EnumDecl *D);
49 void VisitRecordDecl(RecordDecl *D);
50 void VisitEnumConstantDecl(EnumConstantDecl *D);
51 void VisitFunctionDecl(FunctionDecl *D);
52 void VisitFieldDecl(FieldDecl *D);
53 void VisitVarDecl(VarDecl *D);
Chris Lattner57ad3782011-02-17 20:34:02 +000054 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000055 void VisitParmVarDecl(ParmVarDecl *D);
56 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000057 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000058 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000059 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000060 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000061 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000063 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000064 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000065 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
66 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
67 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
68 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
69 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
70 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
71 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
72 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
73 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall7ba107a2009-11-18 02:36:19 +000074 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
75 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssonf53eaa52009-08-28 19:16:39 +000076 void VisitUsingDecl(UsingDecl *D);
John McCall9488ea12009-11-17 05:59:44 +000077 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000078 };
79}
80
Anders Carlssonf88df862009-09-26 21:58:53 +000081void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000082 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000083}
84
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000085void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlssonf88df862009-09-26 21:58:53 +000086 unsigned Indentation) const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000087 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlssonf88df862009-09-26 21:58:53 +000088 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000089}
90
Eli Friedman42f42c02009-05-30 04:20:30 +000091static QualType GetBaseType(QualType T) {
92 // FIXME: This should be on the Type class!
93 QualType BaseType = T;
94 while (!BaseType->isSpecifierType()) {
95 if (isa<TypedefType>(BaseType))
96 break;
Ted Kremenek6217b802009-07-29 21:53:49 +000097 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman42f42c02009-05-30 04:20:30 +000098 BaseType = PTy->getPointeeType();
99 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
100 BaseType = ATy->getElementType();
John McCall183700f2009-09-21 23:43:11 +0000101 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman42f42c02009-05-30 04:20:30 +0000102 BaseType = FTy->getResultType();
John McCall183700f2009-09-21 23:43:11 +0000103 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor5068ab62009-07-01 23:58:14 +0000104 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000105 else
106 assert(0 && "Unknown declarator!");
107 }
108 return BaseType;
109}
110
111static QualType getDeclType(Decl* D) {
112 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
113 return TDD->getUnderlyingType();
114 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
115 return VD->getType();
116 return QualType();
117}
118
119void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000120 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000121 unsigned Indentation) {
122 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000123 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000124 return;
125 }
126
127 Decl** End = Begin + NumDecls;
128 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
129 if (TD)
130 ++Begin;
131
132 PrintingPolicy SubPolicy(Policy);
133 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000134 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000135 Out << " ";
136 SubPolicy.SuppressTag = true;
137 }
138
139 bool isFirst = true;
140 for ( ; Begin != End; ++Begin) {
141 if (isFirst) {
142 SubPolicy.SuppressSpecifiers = false;
143 isFirst = false;
144 } else {
145 if (!isFirst) Out << ", ";
146 SubPolicy.SuppressSpecifiers = true;
147 }
148
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000149 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000150 }
151}
152
Anders Carlsson84834432009-12-14 00:51:04 +0000153void DeclContext::dumpDeclContext() const {
Anders Carlsson2b7d8dd2009-12-09 17:27:46 +0000154 // Get the translation unit
155 const DeclContext *DC = this;
156 while (!DC->isTranslationUnit())
157 DC = DC->getParent();
158
159 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
160 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
161 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
162}
163
Anders Carlssonf88df862009-09-26 21:58:53 +0000164void Decl::dump() const {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000165 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000166}
167
Daniel Dunbar512ce602009-11-21 09:12:06 +0000168llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
169 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000170 Out << " ";
171 return Out;
172}
173
Eli Friedman42f42c02009-05-30 04:20:30 +0000174void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
175 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000176 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000177 Out << ";\n";
178 Decls.clear();
179
180}
181
Anders Carlsson0d592922009-08-28 22:39:52 +0000182void DeclPrinter::Print(AccessSpecifier AS) {
183 switch(AS) {
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000184 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000185 case AS_public: Out << "public"; break;
186 case AS_protected: Out << "protected"; break;
Abramo Bagnara6206d532010-06-05 05:09:32 +0000187 case AS_private: Out << "private"; break;
Anders Carlsson0d592922009-08-28 22:39:52 +0000188 }
189}
190
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000191//----------------------------------------------------------------------------
192// Common C declarations
193//----------------------------------------------------------------------------
194
195void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
196 if (Indent)
197 Indentation += Policy.Indentation;
198
Eli Friedman42f42c02009-05-30 04:20:30 +0000199 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000200 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000201 D != DEnd; ++D) {
Ted Kremenek2d6c9062010-07-30 00:47:46 +0000202
203 // Don't print ObjCIvarDecls, as they are printed when visiting the
204 // containing ObjCInterfaceDecl.
205 if (isa<ObjCIvarDecl>(*D))
206 continue;
207
Eli Friedman48d14a22009-05-30 05:03:24 +0000208 if (!Policy.Dump) {
209 // Skip over implicit declarations in pretty-printing mode.
210 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000211 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidis2574f6f2010-06-17 10:52:11 +0000212 // of __builtin_va_list or __[u]int128_t. There should be some other way
213 // to check that.
214 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
215 if (IdentifierInfo *II = ND->getIdentifier()) {
216 if (II->isStr("__builtin_va_list") ||
217 II->isStr("__int128_t") || II->isStr("__uint128_t"))
218 continue;
219 }
220 }
Eli Friedman48d14a22009-05-30 05:03:24 +0000221 }
222
Eli Friedman42f42c02009-05-30 04:20:30 +0000223 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
224 // forced to merge the declarations because there's no other way to
225 // refer to the struct in question. This limited merging is safe without
226 // a bunch of other checks because it only merges declarations directly
227 // referring to the tag, not typedefs.
228 //
229 // Check whether the current declaration should be grouped with a previous
230 // unnamed struct.
231 QualType CurDeclType = getDeclType(*D);
232 if (!Decls.empty() && !CurDeclType.isNull()) {
233 QualType BaseType = GetBaseType(CurDeclType);
234 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
235 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
236 Decls.push_back(*D);
237 continue;
238 }
239 }
240
241 // If we have a merged group waiting to be handled, handle it now.
242 if (!Decls.empty())
243 ProcessDeclGroup(Decls);
244
245 // If the current declaration is an unnamed tag type, save it
246 // so we can merge it with the subsequent declaration(s) using it.
247 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
248 Decls.push_back(*D);
249 continue;
250 }
Abramo Bagnara6206d532010-06-05 05:09:32 +0000251
252 if (isa<AccessSpecDecl>(*D)) {
253 Indentation -= Policy.Indentation;
254 this->Indent();
255 Print(D->getAccess());
256 Out << ":\n";
257 Indentation += Policy.Indentation;
258 continue;
259 }
260
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000261 this->Indent();
262 Visit(*D);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
264 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000265 const char *Terminator = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000266 if (isa<FunctionDecl>(*D) &&
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000267 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
268 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000269 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
270 Terminator = 0;
271 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump1eb44332009-09-09 15:08:12 +0000272 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor64f65002009-05-30 00:56:08 +0000273 isa<ObjCInterfaceDecl>(*D) ||
274 isa<ObjCProtocolDecl>(*D) ||
275 isa<ObjCCategoryImplDecl>(*D) ||
276 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000277 Terminator = 0;
278 else if (isa<EnumConstantDecl>(*D)) {
279 DeclContext::decl_iterator Next = D;
280 ++Next;
281 if (Next != DEnd)
282 Terminator = ",";
283 } else
284 Terminator = ";";
285
286 if (Terminator)
287 Out << Terminator;
288 Out << "\n";
289 }
290
Eli Friedman42f42c02009-05-30 04:20:30 +0000291 if (!Decls.empty())
292 ProcessDeclGroup(Decls);
293
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000294 if (Indent)
295 Indentation -= Policy.Indentation;
296}
297
298void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
299 VisitDeclContext(D, false);
300}
301
302void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
303 std::string S = D->getNameAsString();
304 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000305 if (!Policy.SuppressSpecifiers)
306 Out << "typedef ";
307 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000308}
309
310void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000311 Out << "enum ";
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +0000312 if (D->isScoped()) {
313 if (D->isScopedUsingClassTag())
314 Out << "class ";
315 else
316 Out << "struct ";
317 }
Douglas Gregor9fa8c462010-12-01 16:01:08 +0000318 Out << D;
319
320 if (D->isFixed()) {
321 std::string Underlying;
322 D->getIntegerType().getAsStringInternal(Underlying, Policy);
323 Out << " : " << Underlying;
324 }
325
326 if (D->isDefinition()) {
327 Out << " {\n";
328 VisitDeclContext(D);
329 Indent() << "}";
330 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000331}
332
333void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000334 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000335 if (D->getIdentifier())
336 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000338 if (D->isDefinition()) {
339 Out << " {\n";
340 VisitDeclContext(D);
341 Indent() << "}";
342 }
343}
344
345void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000346 Out << D;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000347 if (Expr *Init = D->getInitExpr()) {
348 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000349 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000350 }
351}
352
Mike Stump1eb44332009-09-09 15:08:12 +0000353void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000354 if (!Policy.SuppressSpecifiers) {
355 switch (D->getStorageClass()) {
John McCalld931b082010-08-26 03:08:43 +0000356 case SC_None: break;
357 case SC_Extern: Out << "extern "; break;
358 case SC_Static: Out << "static "; break;
359 case SC_PrivateExtern: Out << "__private_extern__ "; break;
360 case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions");
Eli Friedman42f42c02009-05-30 04:20:30 +0000361 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000362
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000363 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman42f42c02009-05-30 04:20:30 +0000364 if (D->isVirtualAsWritten()) Out << "virtual ";
365 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000366
Douglas Gregor6620a622009-05-30 05:39:39 +0000367 PrintingPolicy SubPolicy(Policy);
368 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnara25777432010-08-11 22:01:17 +0000369 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000370
Abramo Bagnara723df242010-12-14 22:11:44 +0000371 QualType Ty = D->getType();
John McCallf4c73712011-01-19 06:33:43 +0000372 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara723df242010-12-14 22:11:44 +0000373 Proto = '(' + Proto + ')';
374 Ty = PT->getInnerType();
375 }
376
377 if (isa<FunctionType>(Ty)) {
378 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000379 const FunctionProtoType *FT = 0;
380 if (D->hasWrittenPrototype())
381 FT = dyn_cast<FunctionProtoType>(AFT);
382
383 Proto += "(";
384 if (FT) {
385 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000386 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000387 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
388 if (i) POut << ", ";
389 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000392 if (FT->isVariadic()) {
393 if (D->getNumParams()) POut << ", ";
394 POut << "...";
395 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000396 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
397 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
398 if (i)
399 Proto += ", ";
400 Proto += D->getParamDecl(i)->getNameAsString();
401 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000402 }
403
404 Proto += ")";
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000405
Douglas Gregorb95cfe42010-11-19 18:44:34 +0000406 if (FT && FT->getTypeQuals()) {
407 unsigned TypeQuals = FT->getTypeQuals();
408 if (TypeQuals & Qualifiers::Const)
409 Proto += " const";
410 if (TypeQuals & Qualifiers::Volatile)
411 Proto += " volatile";
412 if (TypeQuals & Qualifiers::Restrict)
413 Proto += " restrict";
414 }
Sebastian Redl60618fa2011-03-12 11:50:43 +0000415
416 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000417 Proto += " throw(";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000418 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000419 Proto += "...";
420 else
421 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
422 if (I)
423 Proto += ", ";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000424
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000425 std::string ExceptionType;
426 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
427 Proto += ExceptionType;
428 }
429 Proto += ")";
Sebastian Redl60618fa2011-03-12 11:50:43 +0000430 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
431 Proto += " noexcept";
432 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
433 Proto += "(";
434 llvm::raw_string_ostream EOut(Proto);
435 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
436 Indentation);
437 EOut.flush();
438 Proto += EOut.str();
439 Proto += ")";
440 }
Douglas Gregor0ae7b3f2009-12-08 17:45:32 +0000441 }
442
Mike Stumpfd350b52009-07-27 21:33:40 +0000443 if (D->hasAttr<NoReturnAttr>())
444 Proto += " __attribute((noreturn))";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000445 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Sean Huntcbb67482011-01-08 20:30:50 +0000446 if (CDecl->getNumCtorInitializers() > 0) {
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000447 Proto += " : ";
448 Out << Proto;
449 Proto.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000450 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000451 E = CDecl->init_end();
452 B != E; ++B) {
Sean Huntcbb67482011-01-08 20:30:50 +0000453 CXXCtorInitializer * BMInitializer = (*B);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000454 if (B != CDecl->init_begin())
455 Out << ", ";
Francois Pichet00eb3f92010-12-04 09:14:42 +0000456 if (BMInitializer->isAnyMemberInitializer()) {
457 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000458 Out << FD;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000459 } else {
Daniel Dunbar6cb5b5f2010-06-30 19:16:48 +0000460 Out << QualType(BMInitializer->getBaseClass(),
461 0).getAsString(Policy);
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000462 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000463
464 Out << "(";
465 if (!BMInitializer->getInit()) {
466 // Nothing to print
467 } else {
468 Expr *Init = BMInitializer->getInit();
John McCall4765fa02010-12-06 08:20:24 +0000469 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000470 Init = Tmp->getSubExpr();
471
472 Init = Init->IgnoreParens();
473
474 Expr *SimpleInit = 0;
475 Expr **Args = 0;
476 unsigned NumArgs = 0;
477 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
478 Args = ParenList->getExprs();
479 NumArgs = ParenList->getNumExprs();
480 } else if (CXXConstructExpr *Construct
481 = dyn_cast<CXXConstructExpr>(Init)) {
482 Args = Construct->getArgs();
483 NumArgs = Construct->getNumArgs();
484 } else
485 SimpleInit = Init;
486
487 if (SimpleInit)
488 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
489 else {
490 for (unsigned I = 0; I != NumArgs; ++I) {
491 if (isa<CXXDefaultArgExpr>(Args[I]))
492 break;
493
494 if (I)
495 Out << ", ";
496 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
497 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000498 }
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000499 }
500 Out << ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000501 }
502 }
503 }
504 else
505 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000506 } else {
Abramo Bagnara723df242010-12-14 22:11:44 +0000507 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000508 }
509
510 Out << Proto;
511
512 if (D->isPure())
513 Out << " = 0";
514 else if (D->isDeleted())
515 Out << " = delete";
516 else if (D->isThisDeclarationADefinition()) {
517 if (!D->hasPrototype() && D->getNumParams()) {
518 // This is a K&R function definition, so we need to print the
519 // parameters.
520 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000521 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000522 Indentation += Policy.Indentation;
523 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
524 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000525 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000526 Out << ";\n";
527 }
528 Indentation -= Policy.Indentation;
529 } else
530 Out << ' ';
531
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000532 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000533 Out << '\n';
534 }
535}
536
537void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000538 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000539 Out << "mutable ";
540
541 std::string Name = D->getNameAsString();
542 D->getType().getAsStringInternal(Name, Policy);
543 Out << Name;
544
545 if (D->isBitField()) {
546 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000547 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000548 }
549}
550
Chris Lattner57ad3782011-02-17 20:34:02 +0000551void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
552 Out << D->getNameAsString() << ":";
553}
554
555
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000556void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCalld931b082010-08-26 03:08:43 +0000557 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000558 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
559
Eli Friedman42f42c02009-05-30 04:20:30 +0000560 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000561 Out << "__thread ";
562
563 std::string Name = D->getNameAsString();
564 QualType T = D->getType();
John McCall58e46772009-10-23 21:48:59 +0000565 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000566 T = Parm->getOriginalType();
567 T.getAsStringInternal(Name, Policy);
568 Out << Name;
Ted Kremenekbccfd312010-09-07 22:21:59 +0000569 if (Expr *Init = D->getInit()) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000570 if (D->hasCXXDirectInitializer())
571 Out << "(";
Ted Kremenekc57d6552010-09-17 23:04:38 +0000572 else {
573 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
574 if (!CCE || CCE->getConstructor()->isCopyConstructor())
575 Out << " = ";
576 }
Ted Kremenekbccfd312010-09-07 22:21:59 +0000577 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000578 if (D->hasCXXDirectInitializer())
579 Out << ")";
580 }
581}
582
583void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
584 VisitVarDecl(D);
585}
586
587void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
588 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000589 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000590 Out << ")";
591}
592
593//----------------------------------------------------------------------------
594// C++ declarations
595//----------------------------------------------------------------------------
Douglas Gregor59e63572009-05-30 06:58:37 +0000596void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000597 Out << "namespace " << D << " {\n";
Douglas Gregor59e63572009-05-30 06:58:37 +0000598 VisitDeclContext(D);
599 Indent() << "}";
600}
601
Douglas Gregor8419fa32009-05-30 06:31:56 +0000602void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
603 Out << "using namespace ";
604 if (D->getQualifier())
605 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000606 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor8419fa32009-05-30 06:31:56 +0000607}
608
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000609void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000610 Out << "namespace " << D << " = ";
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000611 if (D->getQualifier())
612 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000613 Out << D->getAliasedNamespace();
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000614}
615
Douglas Gregor59e63572009-05-30 06:58:37 +0000616void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
617 Out << D->getKindName();
Benjamin Kramer900fc632010-04-17 09:33:03 +0000618 if (D->getIdentifier())
619 Out << ' ' << D;
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Douglas Gregor59e63572009-05-30 06:58:37 +0000621 if (D->isDefinition()) {
622 // Print the base classes
623 if (D->getNumBases()) {
624 Out << " : ";
Mike Stump1eb44332009-09-09 15:08:12 +0000625 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
626 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor59e63572009-05-30 06:58:37 +0000627 if (Base != D->bases_begin())
628 Out << ", ";
629
630 if (Base->isVirtual())
631 Out << "virtual ";
632
Anders Carlsson018e9fe2009-08-29 20:36:12 +0000633 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
634 if (AS != AS_none)
635 Print(AS);
Anders Carlsson0d592922009-08-28 22:39:52 +0000636 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor59e63572009-05-30 06:58:37 +0000637 }
638 }
639
640 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000641 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000642 Out << " {\n";
643 VisitDeclContext(D);
644 Indent() << "}";
Mike Stump1eb44332009-09-09 15:08:12 +0000645 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000646}
647
648void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
649 const char *l;
650 if (D->getLanguage() == LinkageSpecDecl::lang_c)
651 l = "C";
652 else {
653 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
654 "unknown language in linkage specification");
655 l = "C++";
656 }
657
658 Out << "extern \"" << l << "\" ";
659 if (D->hasBraces()) {
660 Out << "{\n";
661 VisitDeclContext(D);
662 Indent() << "}";
663 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000664 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000665}
666
667void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000668 Out << "template <";
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Anders Carlsson0487f662009-06-04 05:37:43 +0000670 TemplateParameterList *Params = D->getTemplateParameters();
671 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
672 if (i != 0)
673 Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Anders Carlsson0487f662009-06-04 05:37:43 +0000675 const Decl *Param = Params->getParam(i);
Mike Stump1eb44332009-09-09 15:08:12 +0000676 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000677 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000678
679 QualType ParamType =
Anders Carlsson0487f662009-06-04 05:37:43 +0000680 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
681
682 if (TTP->wasDeclaredWithTypename())
683 Out << "typename ";
684 else
685 Out << "class ";
686
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000687 if (TTP->isParameterPack())
688 Out << "... ";
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Douglas Gregorefed5c82010-06-16 15:23:05 +0000690 Out << ParamType.getAsString(Policy);
Anders Carlsson0487f662009-06-04 05:37:43 +0000691
692 if (TTP->hasDefaultArgument()) {
693 Out << " = ";
694 Out << TTP->getDefaultArgument().getAsString(Policy);
695 };
Mike Stump1eb44332009-09-09 15:08:12 +0000696 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson0487f662009-06-04 05:37:43 +0000697 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
698 Out << NTTP->getType().getAsString(Policy);
699
Douglas Gregor56bc9832010-12-24 00:15:10 +0000700 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
701 Out << "...";
702
Anders Carlsson0487f662009-06-04 05:37:43 +0000703 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
704 Out << ' ';
705 Out << Name->getName();
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Anders Carlsson0487f662009-06-04 05:37:43 +0000708 if (NTTP->hasDefaultArgument()) {
709 Out << " = ";
Mike Stump1eb44332009-09-09 15:08:12 +0000710 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson0487f662009-06-04 05:37:43 +0000711 Indentation);
712 }
713 }
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Anders Carlsson0487f662009-06-04 05:37:43 +0000716 Out << "> ";
717
Douglas Gregor61c4d282011-01-05 15:48:55 +0000718 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
719 Out << "class ";
720 if (TTP->isParameterPack())
721 Out << "...";
722 Out << D->getName();
Craig Silverstein0193a722010-07-09 20:25:10 +0000723 } else {
724 Visit(D->getTemplatedDecl());
725 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000726}
727
728//----------------------------------------------------------------------------
729// Objective-C declarations
730//----------------------------------------------------------------------------
731
732void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
733 Out << "@class ";
734 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
735 I != E; ++I) {
736 if (I != D->begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000737 Out << I->getInterface();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000738 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000739}
740
741void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
742 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000743 Out << "- ";
Mike Stump1eb44332009-09-09 15:08:12 +0000744 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000745 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000746 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000747 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000749 std::string name = OMD->getSelector().getAsString();
750 std::string::size_type pos, lastPos = 0;
751 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
752 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump1eb44332009-09-09 15:08:12 +0000753 // FIXME: selector is missing here!
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000754 pos = name.find_first_of(":", lastPos);
755 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000756 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000757 lastPos = pos + 1;
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000760 if (OMD->param_begin() == OMD->param_end())
761 Out << " " << name;
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000763 if (OMD->isVariadic())
764 Out << ", ...";
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000766 if (OMD->getBody()) {
767 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000768 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000769 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000770 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000771}
772
773void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
774 std::string I = OID->getNameAsString();
775 ObjCInterfaceDecl *SID = OID->getSuperClass();
776
777 if (SID)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000778 Out << "@implementation " << I << " : " << SID;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000779 else
780 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000781 Out << "\n";
782 VisitDeclContext(OID, false);
783 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000784}
785
786void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
787 std::string I = OID->getNameAsString();
788 ObjCInterfaceDecl *SID = OID->getSuperClass();
789
790 if (SID)
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000791 Out << "@interface " << I << " : " << SID;
792 else
793 Out << "@interface " << I;
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000795 // Protocols?
796 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
797 if (!Protocols.empty()) {
798 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
799 E = Protocols.end(); I != E; ++I)
Benjamin Kramer900fc632010-04-17 09:33:03 +0000800 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000803 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000804 Out << "> ";
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000806 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000807 Out << "{\n";
808 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000809 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
810 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000811 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000812 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000813 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000814 Out << "}\n";
815 }
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000817 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000818 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000819 // FIXME: implement the rest...
820}
821
822void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
823 Out << "@protocol ";
Mike Stump1eb44332009-09-09 15:08:12 +0000824 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000825 E = D->protocol_end();
826 I != E; ++I) {
827 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000828 Out << *I;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000829 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000830}
831
832void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000833 Out << "@protocol " << PID << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000834 VisitDeclContext(PID, false);
835 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000836}
837
838void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000839 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000840
841 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000842 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000843 // FIXME: implement the rest...
844}
845
846void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000847 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000848 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000849 Out << "@end";
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000851 // FIXME: implement the rest...
852}
853
854void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramer900fc632010-04-17 09:33:03 +0000855 Out << "@compatibility_alias " << AID
856 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000857}
858
859/// PrintObjCPropertyDecl - print a property declaration.
860///
861void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
862 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
863 Out << "@required\n";
864 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
865 Out << "@optional\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000867 Out << "@property";
868 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
869 bool first = true;
870 Out << " (";
Mike Stump1eb44332009-09-09 15:08:12 +0000871 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000872 ObjCPropertyDecl::OBJC_PR_readonly) {
873 Out << (first ? ' ' : ',') << "readonly";
874 first = false;
875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000877 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
878 Out << (first ? ' ' : ',') << "getter = "
879 << PDecl->getGetterName().getAsString();
880 first = false;
881 }
882 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
883 Out << (first ? ' ' : ',') << "setter = "
884 << PDecl->getSetterName().getAsString();
885 first = false;
886 }
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000888 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
889 Out << (first ? ' ' : ',') << "assign";
890 first = false;
891 }
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000893 if (PDecl->getPropertyAttributes() &
894 ObjCPropertyDecl::OBJC_PR_readwrite) {
895 Out << (first ? ' ' : ',') << "readwrite";
896 first = false;
897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000899 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
900 Out << (first ? ' ' : ',') << "retain";
901 first = false;
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000904 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
905 Out << (first ? ' ' : ',') << "copy";
906 first = false;
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
909 if (PDecl->getPropertyAttributes() &
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000910 ObjCPropertyDecl::OBJC_PR_nonatomic) {
911 Out << (first ? ' ' : ',') << "nonatomic";
912 first = false;
913 }
914 Out << " )";
915 }
Benjamin Kramer900fc632010-04-17 09:33:03 +0000916 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000917}
918
919void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
920 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000921 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000922 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000923 Out << "@dynamic ";
Benjamin Kramer900fc632010-04-17 09:33:03 +0000924 Out << PID->getPropertyDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000925 if (PID->getPropertyIvarDecl())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000926 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000927}
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000928
929void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
930 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +0000931 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000932 Out << D;
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000933}
934
John McCall7ba107a2009-11-18 02:36:19 +0000935void
936DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
937 Out << "using typename ";
Douglas Gregordc355712011-02-25 00:36:19 +0000938 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000939 Out << D->getDeclName();
John McCall7ba107a2009-11-18 02:36:19 +0000940}
941
942void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000943 Out << "using ";
Douglas Gregordc355712011-02-25 00:36:19 +0000944 D->getQualifier()->print(Out, Policy);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000945 Out << D->getDeclName();
Anders Carlssonf53eaa52009-08-28 19:16:39 +0000946}
John McCall9488ea12009-11-17 05:59:44 +0000947
948void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
949 // ignore
950}