blob: 87ba5909d99387d0ec46ca9b11d1b9e1eb968c89 [file] [log] [blame]
Douglas Gregor278f52e2009-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 Gregor7ae2d772010-01-31 09:12:51 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000021#include "clang/AST/PrettyPrinter.h"
Douglas Gregor278f52e2009-05-30 00:08:05 +000022#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
25namespace {
Benjamin Kramer26222b62009-11-28 19:03:38 +000026 class DeclPrinter : public DeclVisitor<DeclPrinter> {
Douglas Gregor278f52e2009-05-30 00:08:05 +000027 llvm::raw_ostream &Out;
28 ASTContext &Context;
29 PrintingPolicy Policy;
30 unsigned Indentation;
31
Daniel Dunbar671f45e2009-11-21 09:12:06 +000032 llvm::raw_ostream& Indent() { return Indent(Indentation); }
33 llvm::raw_ostream& Indent(unsigned Indentation);
Eli Friedman79635842009-05-30 04:20:30 +000034 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor278f52e2009-05-30 00:08:05 +000035
Anders Carlsson601d6e42009-08-28 22:39:52 +000036 void Print(AccessSpecifier AS);
Mike Stump11289f42009-09-09 15:08:12 +000037
Douglas Gregor278f52e2009-05-30 00:08:05 +000038 public:
Mike Stump11289f42009-09-09 15:08:12 +000039 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
Douglas Gregor278f52e2009-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 Lattnercab02a62011-02-17 20:34:02 +000054 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000055 void VisitParmVarDecl(ParmVarDecl *D);
56 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000057 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000058 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000059 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000060 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000061 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000062 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000063 void VisitTemplateDecl(const TemplateDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000064 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000065 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000066 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
67 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
68 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
69 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
70 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
71 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
72 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
73 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
74 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000075 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
76 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000077 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000078 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000079 };
80}
81
Anders Carlsson46f87dc2009-09-26 21:58:53 +000082void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000083 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +000084}
85
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000086void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlsson46f87dc2009-09-26 21:58:53 +000087 unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000088 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +000089 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +000090}
91
Eli Friedman79635842009-05-30 04:20:30 +000092static QualType GetBaseType(QualType T) {
93 // FIXME: This should be on the Type class!
94 QualType BaseType = T;
95 while (!BaseType->isSpecifierType()) {
96 if (isa<TypedefType>(BaseType))
97 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +000098 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +000099 BaseType = PTy->getPointeeType();
100 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
101 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000102 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman79635842009-05-30 04:20:30 +0000103 BaseType = FTy->getResultType();
John McCall9dd450b2009-09-21 23:43:11 +0000104 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000105 BaseType = VTy->getElementType();
Eli Friedman79635842009-05-30 04:20:30 +0000106 else
107 assert(0 && "Unknown declarator!");
108 }
109 return BaseType;
110}
111
112static QualType getDeclType(Decl* D) {
113 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
114 return TDD->getUnderlyingType();
115 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
116 return VD->getType();
117 return QualType();
118}
119
120void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000121 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000122 unsigned Indentation) {
123 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000124 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000125 return;
126 }
127
128 Decl** End = Begin + NumDecls;
129 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
130 if (TD)
131 ++Begin;
132
133 PrintingPolicy SubPolicy(Policy);
134 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000135 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000136 Out << " ";
137 SubPolicy.SuppressTag = true;
138 }
139
140 bool isFirst = true;
141 for ( ; Begin != End; ++Begin) {
142 if (isFirst) {
143 SubPolicy.SuppressSpecifiers = false;
144 isFirst = false;
145 } else {
146 if (!isFirst) Out << ", ";
147 SubPolicy.SuppressSpecifiers = true;
148 }
149
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000150 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000151 }
152}
153
Anders Carlsson89112292009-12-14 00:51:04 +0000154void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000155 // Get the translation unit
156 const DeclContext *DC = this;
157 while (!DC->isTranslationUnit())
158 DC = DC->getParent();
159
160 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
161 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
162 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
163}
164
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000165void Decl::dump() const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000166 print(llvm::errs());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000167}
168
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000169llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
170 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000171 Out << " ";
172 return Out;
173}
174
Eli Friedman79635842009-05-30 04:20:30 +0000175void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
176 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000177 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000178 Out << ";\n";
179 Decls.clear();
180
181}
182
Anders Carlsson601d6e42009-08-28 22:39:52 +0000183void DeclPrinter::Print(AccessSpecifier AS) {
184 switch(AS) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000185 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000186 case AS_public: Out << "public"; break;
187 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000188 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000189 }
190}
191
Douglas Gregor278f52e2009-05-30 00:08:05 +0000192//----------------------------------------------------------------------------
193// Common C declarations
194//----------------------------------------------------------------------------
195
196void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
197 if (Indent)
198 Indentation += Policy.Indentation;
199
Eli Friedman79635842009-05-30 04:20:30 +0000200 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000201 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000202 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000203
204 // Don't print ObjCIvarDecls, as they are printed when visiting the
205 // containing ObjCInterfaceDecl.
206 if (isa<ObjCIvarDecl>(*D))
207 continue;
208
Eli Friedmanef334fd2009-05-30 05:03:24 +0000209 if (!Policy.Dump) {
210 // Skip over implicit declarations in pretty-printing mode.
211 if (D->isImplicit()) continue;
Eli Friedman17304242009-05-30 06:35:22 +0000212 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidisfa533e72010-06-17 10:52:11 +0000213 // of __builtin_va_list or __[u]int128_t. There should be some other way
214 // to check that.
215 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
216 if (IdentifierInfo *II = ND->getIdentifier()) {
217 if (II->isStr("__builtin_va_list") ||
218 II->isStr("__int128_t") || II->isStr("__uint128_t"))
219 continue;
220 }
221 }
Eli Friedmanef334fd2009-05-30 05:03:24 +0000222 }
223
Eli Friedman79635842009-05-30 04:20:30 +0000224 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
225 // forced to merge the declarations because there's no other way to
226 // refer to the struct in question. This limited merging is safe without
227 // a bunch of other checks because it only merges declarations directly
228 // referring to the tag, not typedefs.
229 //
230 // Check whether the current declaration should be grouped with a previous
231 // unnamed struct.
232 QualType CurDeclType = getDeclType(*D);
233 if (!Decls.empty() && !CurDeclType.isNull()) {
234 QualType BaseType = GetBaseType(CurDeclType);
235 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
236 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
237 Decls.push_back(*D);
238 continue;
239 }
240 }
241
242 // If we have a merged group waiting to be handled, handle it now.
243 if (!Decls.empty())
244 ProcessDeclGroup(Decls);
245
246 // If the current declaration is an unnamed tag type, save it
247 // so we can merge it with the subsequent declaration(s) using it.
248 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
249 Decls.push_back(*D);
250 continue;
251 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000252
253 if (isa<AccessSpecDecl>(*D)) {
254 Indentation -= Policy.Indentation;
255 this->Indent();
256 Print(D->getAccess());
257 Out << ":\n";
258 Indentation += Policy.Indentation;
259 continue;
260 }
261
Douglas Gregor278f52e2009-05-30 00:08:05 +0000262 this->Indent();
263 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000264
265 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor278f52e2009-05-30 00:08:05 +0000266 const char *Terminator = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000267 if (isa<FunctionDecl>(*D) &&
Douglas Gregor278f52e2009-05-30 00:08:05 +0000268 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
269 Terminator = 0;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000270 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
271 Terminator = 0;
272 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000273 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000274 isa<ObjCInterfaceDecl>(*D) ||
275 isa<ObjCProtocolDecl>(*D) ||
276 isa<ObjCCategoryImplDecl>(*D) ||
277 isa<ObjCCategoryDecl>(*D))
Douglas Gregor278f52e2009-05-30 00:08:05 +0000278 Terminator = 0;
279 else if (isa<EnumConstantDecl>(*D)) {
280 DeclContext::decl_iterator Next = D;
281 ++Next;
282 if (Next != DEnd)
283 Terminator = ",";
284 } else
285 Terminator = ";";
286
287 if (Terminator)
288 Out << Terminator;
289 Out << "\n";
290 }
291
Eli Friedman79635842009-05-30 04:20:30 +0000292 if (!Decls.empty())
293 ProcessDeclGroup(Decls);
294
Douglas Gregor278f52e2009-05-30 00:08:05 +0000295 if (Indent)
296 Indentation -= Policy.Indentation;
297}
298
299void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
300 VisitDeclContext(D, false);
301}
302
303void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
304 std::string S = D->getNameAsString();
305 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000306 if (!Policy.SuppressSpecifiers)
307 Out << "typedef ";
308 Out << S;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000309}
310
311void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000312 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000313 if (D->isScoped()) {
314 if (D->isScopedUsingClassTag())
315 Out << "class ";
316 else
317 Out << "struct ";
318 }
Douglas Gregorec0e3662010-12-01 16:01:08 +0000319 Out << D;
320
321 if (D->isFixed()) {
322 std::string Underlying;
323 D->getIntegerType().getAsStringInternal(Underlying, Policy);
324 Out << " : " << Underlying;
325 }
326
327 if (D->isDefinition()) {
328 Out << " {\n";
329 VisitDeclContext(D);
330 Indent() << "}";
331 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000332}
333
334void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000335 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000336 if (D->getIdentifier())
337 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Douglas Gregor278f52e2009-05-30 00:08:05 +0000339 if (D->isDefinition()) {
340 Out << " {\n";
341 VisitDeclContext(D);
342 Indent() << "}";
343 }
344}
345
346void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000347 Out << D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000348 if (Expr *Init = D->getInitExpr()) {
349 Out << " = ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000350 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000351 }
352}
353
Mike Stump11289f42009-09-09 15:08:12 +0000354void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000355 if (!Policy.SuppressSpecifiers) {
356 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000357 case SC_None: break;
358 case SC_Extern: Out << "extern "; break;
359 case SC_Static: Out << "static "; break;
360 case SC_PrivateExtern: Out << "__private_extern__ "; break;
361 case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000362 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000363
Douglas Gregor35b57532009-10-27 21:01:01 +0000364 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000365 if (D->isVirtualAsWritten()) Out << "virtual ";
366 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000367
Douglas Gregor2d042f12009-05-30 05:39:39 +0000368 PrintingPolicy SubPolicy(Policy);
369 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000370 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000371
Abramo Bagnara6d810632010-12-14 22:11:44 +0000372 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000373 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000374 Proto = '(' + Proto + ')';
375 Ty = PT->getInnerType();
376 }
377
378 if (isa<FunctionType>(Ty)) {
379 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000380 const FunctionProtoType *FT = 0;
381 if (D->hasWrittenPrototype())
382 FT = dyn_cast<FunctionProtoType>(AFT);
383
384 Proto += "(";
385 if (FT) {
386 llvm::raw_string_ostream POut(Proto);
Douglas Gregor2d042f12009-05-30 05:39:39 +0000387 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000388 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
389 if (i) POut << ", ";
390 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
391 }
Mike Stump11289f42009-09-09 15:08:12 +0000392
Douglas Gregor278f52e2009-05-30 00:08:05 +0000393 if (FT->isVariadic()) {
394 if (D->getNumParams()) POut << ", ";
395 POut << "...";
396 }
Eli Friedman79635842009-05-30 04:20:30 +0000397 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
398 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
399 if (i)
400 Proto += ", ";
401 Proto += D->getParamDecl(i)->getNameAsString();
402 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000403 }
404
405 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000406
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000407 if (FT && FT->getTypeQuals()) {
408 unsigned TypeQuals = FT->getTypeQuals();
409 if (TypeQuals & Qualifiers::Const)
410 Proto += " const";
411 if (TypeQuals & Qualifiers::Volatile)
412 Proto += " volatile";
413 if (TypeQuals & Qualifiers::Restrict)
414 Proto += " restrict";
415 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000416
417 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000418 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000419 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000420 Proto += "...";
421 else
422 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
423 if (I)
424 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000425
Douglas Gregor049bdca2009-12-08 17:45:32 +0000426 std::string ExceptionType;
427 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
428 Proto += ExceptionType;
429 }
430 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000431 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
432 Proto += " noexcept";
433 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
434 Proto += "(";
435 llvm::raw_string_ostream EOut(Proto);
436 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
437 Indentation);
438 EOut.flush();
439 Proto += EOut.str();
440 Proto += ")";
441 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000442 }
443
Mike Stump9a9e0c22009-07-27 21:33:40 +0000444 if (D->hasAttr<NoReturnAttr>())
445 Proto += " __attribute((noreturn))";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000446 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Alexis Hunt1d792652011-01-08 20:30:50 +0000447 if (CDecl->getNumCtorInitializers() > 0) {
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000448 Proto += " : ";
449 Out << Proto;
450 Proto.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000451 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000452 E = CDecl->init_end();
453 B != E; ++B) {
Alexis Hunt1d792652011-01-08 20:30:50 +0000454 CXXCtorInitializer * BMInitializer = (*B);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000455 if (B != CDecl->init_begin())
456 Out << ", ";
Francois Pichetd583da02010-12-04 09:14:42 +0000457 if (BMInitializer->isAnyMemberInitializer()) {
458 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000459 Out << FD;
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000460 } else {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000461 Out << QualType(BMInitializer->getBaseClass(),
462 0).getAsString(Policy);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000463 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000464
465 Out << "(";
466 if (!BMInitializer->getInit()) {
467 // Nothing to print
468 } else {
469 Expr *Init = BMInitializer->getInit();
John McCall5d413782010-12-06 08:20:24 +0000470 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000471 Init = Tmp->getSubExpr();
472
473 Init = Init->IgnoreParens();
474
475 Expr *SimpleInit = 0;
476 Expr **Args = 0;
477 unsigned NumArgs = 0;
478 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
479 Args = ParenList->getExprs();
480 NumArgs = ParenList->getNumExprs();
481 } else if (CXXConstructExpr *Construct
482 = dyn_cast<CXXConstructExpr>(Init)) {
483 Args = Construct->getArgs();
484 NumArgs = Construct->getNumArgs();
485 } else
486 SimpleInit = Init;
487
488 if (SimpleInit)
489 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
490 else {
491 for (unsigned I = 0; I != NumArgs; ++I) {
492 if (isa<CXXDefaultArgExpr>(Args[I]))
493 break;
494
495 if (I)
496 Out << ", ";
497 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
498 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000499 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000500 }
501 Out << ")";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000502 }
503 }
504 }
505 else
506 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000507 } else {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000508 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000509 }
510
511 Out << Proto;
512
513 if (D->isPure())
514 Out << " = 0";
515 else if (D->isDeleted())
516 Out << " = delete";
517 else if (D->isThisDeclarationADefinition()) {
518 if (!D->hasPrototype() && D->getNumParams()) {
519 // This is a K&R function definition, so we need to print the
520 // parameters.
521 Out << '\n';
Douglas Gregor2d042f12009-05-30 05:39:39 +0000522 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000523 Indentation += Policy.Indentation;
524 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
525 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000526 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000527 Out << ";\n";
528 }
529 Indentation -= Policy.Indentation;
530 } else
531 Out << ' ';
532
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000533 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000534 Out << '\n';
535 }
536}
537
538void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000539 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000540 Out << "mutable ";
541
542 std::string Name = D->getNameAsString();
543 D->getType().getAsStringInternal(Name, Policy);
544 Out << Name;
545
546 if (D->isBitField()) {
547 Out << " : ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000548 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000549 }
550}
551
Chris Lattnercab02a62011-02-17 20:34:02 +0000552void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
553 Out << D->getNameAsString() << ":";
554}
555
556
Douglas Gregor278f52e2009-05-30 00:08:05 +0000557void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCall8e7d6562010-08-26 03:08:43 +0000558 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000559 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
560
Eli Friedman79635842009-05-30 04:20:30 +0000561 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000562 Out << "__thread ";
563
564 std::string Name = D->getNameAsString();
565 QualType T = D->getType();
John McCall856bbea2009-10-23 21:48:59 +0000566 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor278f52e2009-05-30 00:08:05 +0000567 T = Parm->getOriginalType();
568 T.getAsStringInternal(Name, Policy);
569 Out << Name;
Richard Smith02e85f32011-04-14 22:09:26 +0000570 Expr *Init = D->getInit();
571 if (!Policy.SuppressInitializers && Init) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000572 if (D->hasCXXDirectInitializer())
573 Out << "(";
Ted Kremenek35869382010-09-17 23:04:38 +0000574 else {
575 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
576 if (!CCE || CCE->getConstructor()->isCopyConstructor())
577 Out << " = ";
578 }
Ted Kremenek41994fd2010-09-07 22:21:59 +0000579 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000580 if (D->hasCXXDirectInitializer())
581 Out << ")";
582 }
583}
584
585void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
586 VisitVarDecl(D);
587}
588
589void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
590 Out << "__asm (";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000591 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000592 Out << ")";
593}
594
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000595void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
596 Out << "static_assert(";
597 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
598 Out << ", ";
599 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
600 Out << ")";
601}
602
Douglas Gregor278f52e2009-05-30 00:08:05 +0000603//----------------------------------------------------------------------------
604// C++ declarations
605//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000606void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000607 Out << "namespace " << D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000608 VisitDeclContext(D);
609 Indent() << "}";
610}
611
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000612void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
613 Out << "using namespace ";
614 if (D->getQualifier())
615 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000616 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000617}
618
Douglas Gregor18231932009-05-30 06:48:27 +0000619void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000620 Out << "namespace " << D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000621 if (D->getQualifier())
622 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000623 Out << D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000624}
625
Douglas Gregor5f478b72009-05-30 06:58:37 +0000626void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
627 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000628 if (D->getIdentifier())
629 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregor5f478b72009-05-30 06:58:37 +0000631 if (D->isDefinition()) {
632 // Print the base classes
633 if (D->getNumBases()) {
634 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000635 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
636 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000637 if (Base != D->bases_begin())
638 Out << ", ";
639
640 if (Base->isVirtual())
641 Out << "virtual ";
642
Anders Carlsson6df9e072009-08-29 20:36:12 +0000643 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
644 if (AS != AS_none)
645 Print(AS);
Anders Carlsson601d6e42009-08-28 22:39:52 +0000646 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor5f478b72009-05-30 06:58:37 +0000647 }
648 }
649
650 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000651 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000652 Out << " {\n";
653 VisitDeclContext(D);
654 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000655 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000656}
657
658void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
659 const char *l;
660 if (D->getLanguage() == LinkageSpecDecl::lang_c)
661 l = "C";
662 else {
663 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
664 "unknown language in linkage specification");
665 l = "C++";
666 }
667
668 Out << "extern \"" << l << "\" ";
669 if (D->hasBraces()) {
670 Out << "{\n";
671 VisitDeclContext(D);
672 Indent() << "}";
673 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000674 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000675}
676
Richard Smith030f4992011-04-15 13:38:57 +0000677void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000678 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000679
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000680 TemplateParameterList *Params = D->getTemplateParameters();
681 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
682 if (i != 0)
683 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000684
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000685 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000686 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000687 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000688
689 QualType ParamType =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000690 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
691
692 if (TTP->wasDeclaredWithTypename())
693 Out << "typename ";
694 else
695 Out << "class ";
696
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000697 if (TTP->isParameterPack())
698 Out << "... ";
Mike Stump11289f42009-09-09 15:08:12 +0000699
Douglas Gregor2ebcae12010-06-16 15:23:05 +0000700 Out << ParamType.getAsString(Policy);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000701
702 if (TTP->hasDefaultArgument()) {
703 Out << " = ";
704 Out << TTP->getDefaultArgument().getAsString(Policy);
705 };
Mike Stump11289f42009-09-09 15:08:12 +0000706 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000707 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
708 Out << NTTP->getType().getAsString(Policy);
709
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000710 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
711 Out << "...";
712
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000713 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
714 Out << ' ';
715 Out << Name->getName();
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000718 if (NTTP->hasDefaultArgument()) {
719 Out << " = ";
Mike Stump11289f42009-09-09 15:08:12 +0000720 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000721 Indentation);
722 }
Richard Smith030f4992011-04-15 13:38:57 +0000723 } else if (const TemplateTemplateParmDecl *TTPD =
724 dyn_cast<TemplateTemplateParmDecl>(Param)) {
725 VisitTemplateDecl(TTPD);
726 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000727 }
728 }
Mike Stump11289f42009-09-09 15:08:12 +0000729
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000730 Out << "> ";
731
Richard Smith030f4992011-04-15 13:38:57 +0000732 if (const TemplateTemplateParmDecl *TTP =
733 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000734 Out << "class ";
735 if (TTP->isParameterPack())
736 Out << "...";
737 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000738 } else {
739 Visit(D->getTemplatedDecl());
740 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000741}
742
743//----------------------------------------------------------------------------
744// Objective-C declarations
745//----------------------------------------------------------------------------
746
747void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
748 Out << "@class ";
749 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
750 I != E; ++I) {
751 if (I != D->begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000752 Out << I->getInterface();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000753 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000754}
755
756void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
757 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000758 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000759 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000760 Out << "+ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000761 if (!OMD->getResultType().isNull())
Douglas Gregor5f478b72009-05-30 06:58:37 +0000762 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000763
Douglas Gregor278f52e2009-05-30 00:08:05 +0000764 std::string name = OMD->getSelector().getAsString();
765 std::string::size_type pos, lastPos = 0;
766 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
767 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump11289f42009-09-09 15:08:12 +0000768 // FIXME: selector is missing here!
Douglas Gregor278f52e2009-05-30 00:08:05 +0000769 pos = name.find_first_of(":", lastPos);
770 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000771 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000772 lastPos = pos + 1;
773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Douglas Gregor278f52e2009-05-30 00:08:05 +0000775 if (OMD->param_begin() == OMD->param_end())
776 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +0000777
Douglas Gregor278f52e2009-05-30 00:08:05 +0000778 if (OMD->isVariadic())
779 Out << ", ...";
Mike Stump11289f42009-09-09 15:08:12 +0000780
Douglas Gregor278f52e2009-05-30 00:08:05 +0000781 if (OMD->getBody()) {
782 Out << ' ';
Eli Friedmanef334fd2009-05-30 05:03:24 +0000783 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000784 Out << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000785 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000786}
787
788void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
789 std::string I = OID->getNameAsString();
790 ObjCInterfaceDecl *SID = OID->getSuperClass();
791
792 if (SID)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000793 Out << "@implementation " << I << " : " << SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000794 else
795 Out << "@implementation " << I;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000796 Out << "\n";
797 VisitDeclContext(OID, false);
798 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000799}
800
801void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
802 std::string I = OID->getNameAsString();
803 ObjCInterfaceDecl *SID = OID->getSuperClass();
804
805 if (SID)
Douglas Gregor1c283312010-08-11 12:19:30 +0000806 Out << "@interface " << I << " : " << SID;
807 else
808 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +0000809
Douglas Gregor278f52e2009-05-30 00:08:05 +0000810 // Protocols?
811 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
812 if (!Protocols.empty()) {
813 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
814 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000815 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Douglas Gregor278f52e2009-05-30 00:08:05 +0000818 if (!Protocols.empty())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000819 Out << "> ";
Mike Stump11289f42009-09-09 15:08:12 +0000820
Douglas Gregor278f52e2009-05-30 00:08:05 +0000821 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000822 Out << "{\n";
823 Indentation += Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000824 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
825 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000826 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000827 }
Douglas Gregor36098ff2009-05-30 00:56:08 +0000828 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000829 Out << "}\n";
830 }
Mike Stump11289f42009-09-09 15:08:12 +0000831
Douglas Gregor278f52e2009-05-30 00:08:05 +0000832 VisitDeclContext(OID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000833 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000834 // FIXME: implement the rest...
835}
836
837void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
838 Out << "@protocol ";
Mike Stump11289f42009-09-09 15:08:12 +0000839 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor278f52e2009-05-30 00:08:05 +0000840 E = D->protocol_end();
841 I != E; ++I) {
842 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000843 Out << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000844 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000845}
846
847void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000848 Out << "@protocol " << PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000849 VisitDeclContext(PID, false);
850 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000851}
852
853void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000854 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000855
856 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000857 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000858 // FIXME: implement the rest...
859}
860
861void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000862 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000863 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000864 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +0000865
Douglas Gregor278f52e2009-05-30 00:08:05 +0000866 // FIXME: implement the rest...
867}
868
869void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000870 Out << "@compatibility_alias " << AID
871 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000872}
873
874/// PrintObjCPropertyDecl - print a property declaration.
875///
876void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
877 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
878 Out << "@required\n";
879 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
880 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +0000881
Douglas Gregor278f52e2009-05-30 00:08:05 +0000882 Out << "@property";
883 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
884 bool first = true;
885 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +0000886 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000887 ObjCPropertyDecl::OBJC_PR_readonly) {
888 Out << (first ? ' ' : ',') << "readonly";
889 first = false;
890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregor278f52e2009-05-30 00:08:05 +0000892 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
893 Out << (first ? ' ' : ',') << "getter = "
894 << PDecl->getGetterName().getAsString();
895 first = false;
896 }
897 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
898 Out << (first ? ' ' : ',') << "setter = "
899 << PDecl->getSetterName().getAsString();
900 first = false;
901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregor278f52e2009-05-30 00:08:05 +0000903 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
904 Out << (first ? ' ' : ',') << "assign";
905 first = false;
906 }
Mike Stump11289f42009-09-09 15:08:12 +0000907
Douglas Gregor278f52e2009-05-30 00:08:05 +0000908 if (PDecl->getPropertyAttributes() &
909 ObjCPropertyDecl::OBJC_PR_readwrite) {
910 Out << (first ? ' ' : ',') << "readwrite";
911 first = false;
912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Douglas Gregor278f52e2009-05-30 00:08:05 +0000914 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
915 Out << (first ? ' ' : ',') << "retain";
916 first = false;
917 }
Mike Stump11289f42009-09-09 15:08:12 +0000918
Douglas Gregor278f52e2009-05-30 00:08:05 +0000919 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
920 Out << (first ? ' ' : ',') << "copy";
921 first = false;
922 }
Mike Stump11289f42009-09-09 15:08:12 +0000923
924 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000925 ObjCPropertyDecl::OBJC_PR_nonatomic) {
926 Out << (first ? ' ' : ',') << "nonatomic";
927 first = false;
928 }
929 Out << " )";
930 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000931 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000932}
933
934void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
935 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +0000936 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000937 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000938 Out << "@dynamic ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000939 Out << PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000940 if (PID->getPropertyIvarDecl())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000941 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000942}
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000943
944void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
945 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000946 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000947 Out << D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000948}
949
John McCalle61f2ba2009-11-18 02:36:19 +0000950void
951DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
952 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000953 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000954 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +0000955}
956
957void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000958 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000959 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000960 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000961}
John McCall3f746822009-11-17 05:59:44 +0000962
963void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
964 // ignore
965}