blob: 556fc72cec2e15576b3deca822444ce4547c773f [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);
Richard Smithdda56e42011-04-15 14:24:37 +000048 void VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000049 void VisitEnumDecl(EnumDecl *D);
50 void VisitRecordDecl(RecordDecl *D);
51 void VisitEnumConstantDecl(EnumConstantDecl *D);
52 void VisitFunctionDecl(FunctionDecl *D);
53 void VisitFieldDecl(FieldDecl *D);
54 void VisitVarDecl(VarDecl *D);
Chris Lattnercab02a62011-02-17 20:34:02 +000055 void VisitLabelDecl(LabelDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000056 void VisitParmVarDecl(ParmVarDecl *D);
57 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +000058 void VisitStaticAssertDecl(StaticAssertDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000059 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000060 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000061 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000062 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000063 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Richard Smith030f4992011-04-15 13:38:57 +000064 void VisitTemplateDecl(const TemplateDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000065 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000066 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000067 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
68 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
69 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
70 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
71 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
72 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
73 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
74 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
75 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCalle61f2ba2009-11-18 02:36:19 +000076 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
77 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000078 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000079 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000080 };
81}
82
Anders Carlsson46f87dc2009-09-26 21:58:53 +000083void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000084 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +000085}
86
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000087void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlsson46f87dc2009-09-26 21:58:53 +000088 unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000089 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +000090 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +000091}
92
Eli Friedman79635842009-05-30 04:20:30 +000093static QualType GetBaseType(QualType T) {
94 // FIXME: This should be on the Type class!
95 QualType BaseType = T;
96 while (!BaseType->isSpecifierType()) {
97 if (isa<TypedefType>(BaseType))
98 break;
Ted Kremenekc23c7e62009-07-29 21:53:49 +000099 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +0000100 BaseType = PTy->getPointeeType();
101 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
102 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000103 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman79635842009-05-30 04:20:30 +0000104 BaseType = FTy->getResultType();
John McCall9dd450b2009-09-21 23:43:11 +0000105 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000106 BaseType = VTy->getElementType();
Eli Friedman79635842009-05-30 04:20:30 +0000107 else
108 assert(0 && "Unknown declarator!");
109 }
110 return BaseType;
111}
112
113static QualType getDeclType(Decl* D) {
Richard Smithdda56e42011-04-15 14:24:37 +0000114 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
Eli Friedman79635842009-05-30 04:20:30 +0000115 return TDD->getUnderlyingType();
116 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
117 return VD->getType();
118 return QualType();
119}
120
121void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000122 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000123 unsigned Indentation) {
124 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000125 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000126 return;
127 }
128
129 Decl** End = Begin + NumDecls;
130 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
131 if (TD)
132 ++Begin;
133
134 PrintingPolicy SubPolicy(Policy);
135 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000136 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000137 Out << " ";
138 SubPolicy.SuppressTag = true;
139 }
140
141 bool isFirst = true;
142 for ( ; Begin != End; ++Begin) {
143 if (isFirst) {
144 SubPolicy.SuppressSpecifiers = false;
145 isFirst = false;
146 } else {
147 if (!isFirst) Out << ", ";
148 SubPolicy.SuppressSpecifiers = true;
149 }
150
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000151 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000152 }
153}
154
Anders Carlsson89112292009-12-14 00:51:04 +0000155void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-12-09 17:27:46 +0000156 // Get the translation unit
157 const DeclContext *DC = this;
158 while (!DC->isTranslationUnit())
159 DC = DC->getParent();
160
161 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
162 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
163 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
164}
165
Anders Carlsson46f87dc2009-09-26 21:58:53 +0000166void Decl::dump() const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000167 print(llvm::errs());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000168}
169
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000170llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
171 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000172 Out << " ";
173 return Out;
174}
175
Eli Friedman79635842009-05-30 04:20:30 +0000176void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
177 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000178 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000179 Out << ";\n";
180 Decls.clear();
181
182}
183
Anders Carlsson601d6e42009-08-28 22:39:52 +0000184void DeclPrinter::Print(AccessSpecifier AS) {
185 switch(AS) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000186 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000187 case AS_public: Out << "public"; break;
188 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000189 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000190 }
191}
192
Douglas Gregor278f52e2009-05-30 00:08:05 +0000193//----------------------------------------------------------------------------
194// Common C declarations
195//----------------------------------------------------------------------------
196
197void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
198 if (Indent)
199 Indentation += Policy.Indentation;
200
Eli Friedman79635842009-05-30 04:20:30 +0000201 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000202 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000203 D != DEnd; ++D) {
Ted Kremenek28e1c912010-07-30 00:47:46 +0000204
205 // Don't print ObjCIvarDecls, as they are printed when visiting the
206 // containing ObjCInterfaceDecl.
207 if (isa<ObjCIvarDecl>(*D))
208 continue;
209
Eli Friedmanef334fd2009-05-30 05:03:24 +0000210 if (!Policy.Dump) {
211 // Skip over implicit declarations in pretty-printing mode.
212 if (D->isImplicit()) continue;
Eli Friedman17304242009-05-30 06:35:22 +0000213 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidisfa533e72010-06-17 10:52:11 +0000214 // of __builtin_va_list or __[u]int128_t. There should be some other way
215 // to check that.
216 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
217 if (IdentifierInfo *II = ND->getIdentifier()) {
218 if (II->isStr("__builtin_va_list") ||
219 II->isStr("__int128_t") || II->isStr("__uint128_t"))
220 continue;
221 }
222 }
Eli Friedmanef334fd2009-05-30 05:03:24 +0000223 }
224
Eli Friedman79635842009-05-30 04:20:30 +0000225 // 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 }
Abramo Bagnarad7340582010-06-05 05:09:32 +0000253
254 if (isa<AccessSpecDecl>(*D)) {
255 Indentation -= Policy.Indentation;
256 this->Indent();
257 Print(D->getAccess());
258 Out << ":\n";
259 Indentation += Policy.Indentation;
260 continue;
261 }
262
Douglas Gregor278f52e2009-05-30 00:08:05 +0000263 this->Indent();
264 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000265
266 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor278f52e2009-05-30 00:08:05 +0000267 const char *Terminator = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000268 if (isa<FunctionDecl>(*D) &&
Douglas Gregor278f52e2009-05-30 00:08:05 +0000269 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
270 Terminator = 0;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000271 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
272 Terminator = 0;
273 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
Mike Stump11289f42009-09-09 15:08:12 +0000274 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000275 isa<ObjCInterfaceDecl>(*D) ||
276 isa<ObjCProtocolDecl>(*D) ||
277 isa<ObjCCategoryImplDecl>(*D) ||
278 isa<ObjCCategoryDecl>(*D))
Douglas Gregor278f52e2009-05-30 00:08:05 +0000279 Terminator = 0;
280 else if (isa<EnumConstantDecl>(*D)) {
281 DeclContext::decl_iterator Next = D;
282 ++Next;
283 if (Next != DEnd)
284 Terminator = ",";
285 } else
286 Terminator = ";";
287
288 if (Terminator)
289 Out << Terminator;
290 Out << "\n";
291 }
292
Eli Friedman79635842009-05-30 04:20:30 +0000293 if (!Decls.empty())
294 ProcessDeclGroup(Decls);
295
Douglas Gregor278f52e2009-05-30 00:08:05 +0000296 if (Indent)
297 Indentation -= Policy.Indentation;
298}
299
300void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
301 VisitDeclContext(D, false);
302}
303
304void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
305 std::string S = D->getNameAsString();
306 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman79635842009-05-30 04:20:30 +0000307 if (!Policy.SuppressSpecifiers)
308 Out << "typedef ";
309 Out << S;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000310}
311
Richard Smithdda56e42011-04-15 14:24:37 +0000312void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
313 Out << "using " << D->getNameAsString() << " = "
314 << D->getUnderlyingType().getAsString(Policy);
315}
316
Douglas Gregor278f52e2009-05-30 00:08:05 +0000317void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000318 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000319 if (D->isScoped()) {
320 if (D->isScopedUsingClassTag())
321 Out << "class ";
322 else
323 Out << "struct ";
324 }
Douglas Gregorec0e3662010-12-01 16:01:08 +0000325 Out << D;
326
327 if (D->isFixed()) {
328 std::string Underlying;
329 D->getIntegerType().getAsStringInternal(Underlying, Policy);
330 Out << " : " << Underlying;
331 }
332
333 if (D->isDefinition()) {
334 Out << " {\n";
335 VisitDeclContext(D);
336 Indent() << "}";
337 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000338}
339
340void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000341 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000342 if (D->getIdentifier())
343 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Douglas Gregor278f52e2009-05-30 00:08:05 +0000345 if (D->isDefinition()) {
346 Out << " {\n";
347 VisitDeclContext(D);
348 Indent() << "}";
349 }
350}
351
352void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000353 Out << D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000354 if (Expr *Init = D->getInitExpr()) {
355 Out << " = ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000356 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000357 }
358}
359
Mike Stump11289f42009-09-09 15:08:12 +0000360void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000361 if (!Policy.SuppressSpecifiers) {
362 switch (D->getStorageClass()) {
John McCall8e7d6562010-08-26 03:08:43 +0000363 case SC_None: break;
364 case SC_Extern: Out << "extern "; break;
365 case SC_Static: Out << "static "; break;
366 case SC_PrivateExtern: Out << "__private_extern__ "; break;
367 case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions");
Eli Friedman79635842009-05-30 04:20:30 +0000368 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000369
Douglas Gregor35b57532009-10-27 21:01:01 +0000370 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000371 if (D->isVirtualAsWritten()) Out << "virtual ";
372 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000373
Douglas Gregor2d042f12009-05-30 05:39:39 +0000374 PrintingPolicy SubPolicy(Policy);
375 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000376 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000377
Abramo Bagnara6d810632010-12-14 22:11:44 +0000378 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000379 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000380 Proto = '(' + Proto + ')';
381 Ty = PT->getInnerType();
382 }
383
384 if (isa<FunctionType>(Ty)) {
385 const FunctionType *AFT = Ty->getAs<FunctionType>();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000386 const FunctionProtoType *FT = 0;
387 if (D->hasWrittenPrototype())
388 FT = dyn_cast<FunctionProtoType>(AFT);
389
390 Proto += "(";
391 if (FT) {
392 llvm::raw_string_ostream POut(Proto);
Douglas Gregor2d042f12009-05-30 05:39:39 +0000393 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000394 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
395 if (i) POut << ", ";
396 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Douglas Gregor278f52e2009-05-30 00:08:05 +0000399 if (FT->isVariadic()) {
400 if (D->getNumParams()) POut << ", ";
401 POut << "...";
402 }
Eli Friedman79635842009-05-30 04:20:30 +0000403 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
404 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
405 if (i)
406 Proto += ", ";
407 Proto += D->getParamDecl(i)->getNameAsString();
408 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000409 }
410
411 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000412
Douglas Gregor8fc96fc2010-11-19 18:44:34 +0000413 if (FT && FT->getTypeQuals()) {
414 unsigned TypeQuals = FT->getTypeQuals();
415 if (TypeQuals & Qualifiers::Const)
416 Proto += " const";
417 if (TypeQuals & Qualifiers::Volatile)
418 Proto += " volatile";
419 if (TypeQuals & Qualifiers::Restrict)
420 Proto += " restrict";
421 }
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000422
423 if (FT && FT->hasDynamicExceptionSpec()) {
Douglas Gregor049bdca2009-12-08 17:45:32 +0000424 Proto += " throw(";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000425 if (FT->getExceptionSpecType() == EST_MSAny)
Douglas Gregor049bdca2009-12-08 17:45:32 +0000426 Proto += "...";
427 else
428 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
429 if (I)
430 Proto += ", ";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000431
Douglas Gregor049bdca2009-12-08 17:45:32 +0000432 std::string ExceptionType;
433 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
434 Proto += ExceptionType;
435 }
436 Proto += ")";
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000437 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
438 Proto += " noexcept";
439 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
440 Proto += "(";
441 llvm::raw_string_ostream EOut(Proto);
442 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
443 Indentation);
444 EOut.flush();
445 Proto += EOut.str();
446 Proto += ")";
447 }
Douglas Gregor049bdca2009-12-08 17:45:32 +0000448 }
449
Mike Stump9a9e0c22009-07-27 21:33:40 +0000450 if (D->hasAttr<NoReturnAttr>())
451 Proto += " __attribute((noreturn))";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000452 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Alexis Hunt1d792652011-01-08 20:30:50 +0000453 if (CDecl->getNumCtorInitializers() > 0) {
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000454 Proto += " : ";
455 Out << Proto;
456 Proto.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000457 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000458 E = CDecl->init_end();
459 B != E; ++B) {
Alexis Hunt1d792652011-01-08 20:30:50 +0000460 CXXCtorInitializer * BMInitializer = (*B);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000461 if (B != CDecl->init_begin())
462 Out << ", ";
Francois Pichetd583da02010-12-04 09:14:42 +0000463 if (BMInitializer->isAnyMemberInitializer()) {
464 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000465 Out << FD;
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000466 } else {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000467 Out << QualType(BMInitializer->getBaseClass(),
468 0).getAsString(Policy);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000469 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000470
471 Out << "(";
472 if (!BMInitializer->getInit()) {
473 // Nothing to print
474 } else {
475 Expr *Init = BMInitializer->getInit();
John McCall5d413782010-12-06 08:20:24 +0000476 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000477 Init = Tmp->getSubExpr();
478
479 Init = Init->IgnoreParens();
480
481 Expr *SimpleInit = 0;
482 Expr **Args = 0;
483 unsigned NumArgs = 0;
484 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
485 Args = ParenList->getExprs();
486 NumArgs = ParenList->getNumExprs();
487 } else if (CXXConstructExpr *Construct
488 = dyn_cast<CXXConstructExpr>(Init)) {
489 Args = Construct->getArgs();
490 NumArgs = Construct->getNumArgs();
491 } else
492 SimpleInit = Init;
493
494 if (SimpleInit)
495 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
496 else {
497 for (unsigned I = 0; I != NumArgs; ++I) {
498 if (isa<CXXDefaultArgExpr>(Args[I]))
499 break;
500
501 if (I)
502 Out << ", ";
503 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
504 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000505 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000506 }
507 Out << ")";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000508 }
509 }
510 }
511 else
512 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000513 } else {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000514 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000515 }
516
517 Out << Proto;
518
519 if (D->isPure())
520 Out << " = 0";
521 else if (D->isDeleted())
522 Out << " = delete";
523 else if (D->isThisDeclarationADefinition()) {
524 if (!D->hasPrototype() && D->getNumParams()) {
525 // This is a K&R function definition, so we need to print the
526 // parameters.
527 Out << '\n';
Douglas Gregor2d042f12009-05-30 05:39:39 +0000528 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000529 Indentation += Policy.Indentation;
530 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
531 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000532 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000533 Out << ";\n";
534 }
535 Indentation -= Policy.Indentation;
536 } else
537 Out << ' ';
538
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000539 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000540 Out << '\n';
541 }
542}
543
544void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000545 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000546 Out << "mutable ";
547
548 std::string Name = D->getNameAsString();
549 D->getType().getAsStringInternal(Name, Policy);
550 Out << Name;
551
552 if (D->isBitField()) {
553 Out << " : ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000554 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000555 }
556}
557
Chris Lattnercab02a62011-02-17 20:34:02 +0000558void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
559 Out << D->getNameAsString() << ":";
560}
561
562
Douglas Gregor278f52e2009-05-30 00:08:05 +0000563void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCall8e7d6562010-08-26 03:08:43 +0000564 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000565 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
566
Eli Friedman79635842009-05-30 04:20:30 +0000567 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000568 Out << "__thread ";
569
570 std::string Name = D->getNameAsString();
571 QualType T = D->getType();
John McCall856bbea2009-10-23 21:48:59 +0000572 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor278f52e2009-05-30 00:08:05 +0000573 T = Parm->getOriginalType();
574 T.getAsStringInternal(Name, Policy);
575 Out << Name;
Richard Smith02e85f32011-04-14 22:09:26 +0000576 Expr *Init = D->getInit();
577 if (!Policy.SuppressInitializers && Init) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000578 if (D->hasCXXDirectInitializer())
579 Out << "(";
Ted Kremenek35869382010-09-17 23:04:38 +0000580 else {
581 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
582 if (!CCE || CCE->getConstructor()->isCopyConstructor())
583 Out << " = ";
584 }
Ted Kremenek41994fd2010-09-07 22:21:59 +0000585 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000586 if (D->hasCXXDirectInitializer())
587 Out << ")";
588 }
589}
590
591void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
592 VisitVarDecl(D);
593}
594
595void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
596 Out << "__asm (";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000597 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000598 Out << ")";
599}
600
Peter Collingbourne7d8a0b52011-03-16 18:37:27 +0000601void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
602 Out << "static_assert(";
603 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
604 Out << ", ";
605 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
606 Out << ")";
607}
608
Douglas Gregor278f52e2009-05-30 00:08:05 +0000609//----------------------------------------------------------------------------
610// C++ declarations
611//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000612void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000613 Out << "namespace " << D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000614 VisitDeclContext(D);
615 Indent() << "}";
616}
617
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000618void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
619 Out << "using namespace ";
620 if (D->getQualifier())
621 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000622 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000623}
624
Douglas Gregor18231932009-05-30 06:48:27 +0000625void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000626 Out << "namespace " << D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000627 if (D->getQualifier())
628 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000629 Out << D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000630}
631
Douglas Gregor5f478b72009-05-30 06:58:37 +0000632void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
633 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000634 if (D->getIdentifier())
635 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Douglas Gregor5f478b72009-05-30 06:58:37 +0000637 if (D->isDefinition()) {
638 // Print the base classes
639 if (D->getNumBases()) {
640 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000641 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
642 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000643 if (Base != D->bases_begin())
644 Out << ", ";
645
646 if (Base->isVirtual())
647 Out << "virtual ";
648
Anders Carlsson6df9e072009-08-29 20:36:12 +0000649 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
650 if (AS != AS_none)
651 Print(AS);
Anders Carlsson601d6e42009-08-28 22:39:52 +0000652 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor5fc8c9e2011-04-27 17:07:55 +0000653
654 if (Base->isPackExpansion())
655 Out << "...";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000656 }
657 }
658
659 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000660 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000661 Out << " {\n";
662 VisitDeclContext(D);
663 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000664 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000665}
666
667void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
668 const char *l;
669 if (D->getLanguage() == LinkageSpecDecl::lang_c)
670 l = "C";
671 else {
672 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
673 "unknown language in linkage specification");
674 l = "C++";
675 }
676
677 Out << "extern \"" << l << "\" ";
678 if (D->hasBraces()) {
679 Out << "{\n";
680 VisitDeclContext(D);
681 Indent() << "}";
682 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000683 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000684}
685
Richard Smith030f4992011-04-15 13:38:57 +0000686void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000687 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000688
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000689 TemplateParameterList *Params = D->getTemplateParameters();
690 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
691 if (i != 0)
692 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000693
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000694 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000695 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000696 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000697
698 QualType ParamType =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000699 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
700
701 if (TTP->wasDeclaredWithTypename())
702 Out << "typename ";
703 else
704 Out << "class ";
705
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000706 if (TTP->isParameterPack())
707 Out << "... ";
Mike Stump11289f42009-09-09 15:08:12 +0000708
Douglas Gregor2ebcae12010-06-16 15:23:05 +0000709 Out << ParamType.getAsString(Policy);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000710
711 if (TTP->hasDefaultArgument()) {
712 Out << " = ";
713 Out << TTP->getDefaultArgument().getAsString(Policy);
714 };
Mike Stump11289f42009-09-09 15:08:12 +0000715 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000716 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
717 Out << NTTP->getType().getAsString(Policy);
718
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000719 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
720 Out << "...";
721
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000722 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
723 Out << ' ';
724 Out << Name->getName();
725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000727 if (NTTP->hasDefaultArgument()) {
728 Out << " = ";
Mike Stump11289f42009-09-09 15:08:12 +0000729 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000730 Indentation);
731 }
Richard Smith030f4992011-04-15 13:38:57 +0000732 } else if (const TemplateTemplateParmDecl *TTPD =
733 dyn_cast<TemplateTemplateParmDecl>(Param)) {
734 VisitTemplateDecl(TTPD);
735 // FIXME: print the default argument, if present.
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000736 }
737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000739 Out << "> ";
740
Richard Smith030f4992011-04-15 13:38:57 +0000741 if (const TemplateTemplateParmDecl *TTP =
742 dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregorf5500772011-01-05 15:48:55 +0000743 Out << "class ";
744 if (TTP->isParameterPack())
745 Out << "...";
746 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000747 } else {
748 Visit(D->getTemplatedDecl());
749 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000750}
751
752//----------------------------------------------------------------------------
753// Objective-C declarations
754//----------------------------------------------------------------------------
755
756void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
757 Out << "@class ";
758 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
759 I != E; ++I) {
760 if (I != D->begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000761 Out << I->getInterface();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000762 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000763}
764
765void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
766 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000767 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000768 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000769 Out << "+ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000770 if (!OMD->getResultType().isNull())
Douglas Gregor5f478b72009-05-30 06:58:37 +0000771 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000772
Douglas Gregor278f52e2009-05-30 00:08:05 +0000773 std::string name = OMD->getSelector().getAsString();
774 std::string::size_type pos, lastPos = 0;
775 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
776 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump11289f42009-09-09 15:08:12 +0000777 // FIXME: selector is missing here!
Douglas Gregor278f52e2009-05-30 00:08:05 +0000778 pos = name.find_first_of(":", lastPos);
779 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000780 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000781 lastPos = pos + 1;
782 }
Mike Stump11289f42009-09-09 15:08:12 +0000783
Douglas Gregor278f52e2009-05-30 00:08:05 +0000784 if (OMD->param_begin() == OMD->param_end())
785 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +0000786
Douglas Gregor278f52e2009-05-30 00:08:05 +0000787 if (OMD->isVariadic())
788 Out << ", ...";
Mike Stump11289f42009-09-09 15:08:12 +0000789
Douglas Gregor278f52e2009-05-30 00:08:05 +0000790 if (OMD->getBody()) {
791 Out << ' ';
Eli Friedmanef334fd2009-05-30 05:03:24 +0000792 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000793 Out << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000794 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000795}
796
797void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
798 std::string I = OID->getNameAsString();
799 ObjCInterfaceDecl *SID = OID->getSuperClass();
800
801 if (SID)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000802 Out << "@implementation " << I << " : " << SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000803 else
804 Out << "@implementation " << I;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000805 Out << "\n";
806 VisitDeclContext(OID, false);
807 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000808}
809
810void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
811 std::string I = OID->getNameAsString();
812 ObjCInterfaceDecl *SID = OID->getSuperClass();
813
814 if (SID)
Douglas Gregor1c283312010-08-11 12:19:30 +0000815 Out << "@interface " << I << " : " << SID;
816 else
817 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregor278f52e2009-05-30 00:08:05 +0000819 // Protocols?
820 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
821 if (!Protocols.empty()) {
822 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
823 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000824 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000825 }
Mike Stump11289f42009-09-09 15:08:12 +0000826
Douglas Gregor278f52e2009-05-30 00:08:05 +0000827 if (!Protocols.empty())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000828 Out << "> ";
Mike Stump11289f42009-09-09 15:08:12 +0000829
Douglas Gregor278f52e2009-05-30 00:08:05 +0000830 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000831 Out << "{\n";
832 Indentation += Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000833 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
834 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000835 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000836 }
Douglas Gregor36098ff2009-05-30 00:56:08 +0000837 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000838 Out << "}\n";
839 }
Mike Stump11289f42009-09-09 15:08:12 +0000840
Douglas Gregor278f52e2009-05-30 00:08:05 +0000841 VisitDeclContext(OID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000842 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000843 // FIXME: implement the rest...
844}
845
846void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
847 Out << "@protocol ";
Mike Stump11289f42009-09-09 15:08:12 +0000848 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor278f52e2009-05-30 00:08:05 +0000849 E = D->protocol_end();
850 I != E; ++I) {
851 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000852 Out << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000853 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000854}
855
856void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000857 Out << "@protocol " << PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000858 VisitDeclContext(PID, false);
859 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000860}
861
862void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000863 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000864
865 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000866 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000867 // FIXME: implement the rest...
868}
869
870void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000871 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000872 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000873 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +0000874
Douglas Gregor278f52e2009-05-30 00:08:05 +0000875 // FIXME: implement the rest...
876}
877
878void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000879 Out << "@compatibility_alias " << AID
880 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000881}
882
883/// PrintObjCPropertyDecl - print a property declaration.
884///
885void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
886 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
887 Out << "@required\n";
888 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
889 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +0000890
Douglas Gregor278f52e2009-05-30 00:08:05 +0000891 Out << "@property";
892 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
893 bool first = true;
894 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +0000895 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000896 ObjCPropertyDecl::OBJC_PR_readonly) {
897 Out << (first ? ' ' : ',') << "readonly";
898 first = false;
899 }
Mike Stump11289f42009-09-09 15:08:12 +0000900
Douglas Gregor278f52e2009-05-30 00:08:05 +0000901 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
902 Out << (first ? ' ' : ',') << "getter = "
903 << PDecl->getGetterName().getAsString();
904 first = false;
905 }
906 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
907 Out << (first ? ' ' : ',') << "setter = "
908 << PDecl->getSetterName().getAsString();
909 first = false;
910 }
Mike Stump11289f42009-09-09 15:08:12 +0000911
Douglas Gregor278f52e2009-05-30 00:08:05 +0000912 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
913 Out << (first ? ' ' : ',') << "assign";
914 first = false;
915 }
Mike Stump11289f42009-09-09 15:08:12 +0000916
Douglas Gregor278f52e2009-05-30 00:08:05 +0000917 if (PDecl->getPropertyAttributes() &
918 ObjCPropertyDecl::OBJC_PR_readwrite) {
919 Out << (first ? ' ' : ',') << "readwrite";
920 first = false;
921 }
Mike Stump11289f42009-09-09 15:08:12 +0000922
Douglas Gregor278f52e2009-05-30 00:08:05 +0000923 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
924 Out << (first ? ' ' : ',') << "retain";
925 first = false;
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Douglas Gregor278f52e2009-05-30 00:08:05 +0000928 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
929 Out << (first ? ' ' : ',') << "copy";
930 first = false;
931 }
Mike Stump11289f42009-09-09 15:08:12 +0000932
933 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000934 ObjCPropertyDecl::OBJC_PR_nonatomic) {
935 Out << (first ? ' ' : ',') << "nonatomic";
936 first = false;
937 }
938 Out << " )";
939 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000940 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000941}
942
943void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
944 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +0000945 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000946 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000947 Out << "@dynamic ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000948 Out << PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000949 if (PID->getPropertyIvarDecl())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000950 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000951}
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000952
953void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
954 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000955 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000956 Out << D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000957}
958
John McCalle61f2ba2009-11-18 02:36:19 +0000959void
960DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
961 Out << "using typename ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000962 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000963 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +0000964}
965
966void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000967 Out << "using ";
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000968 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000969 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000970}
John McCall3f746822009-11-17 05:59:44 +0000971
972void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
973 // ignore
974}