blob: 77b42574709f0b0b184d215edae553d58164d543 [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);
Douglas Gregor5f478b72009-05-30 06:58:37 +000057 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000058 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor18231932009-05-30 06:48:27 +000059 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor5f478b72009-05-30 06:58:37 +000060 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000061 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000063 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +000064 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor278f52e2009-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 McCalle61f2ba2009-11-18 02:36:19 +000074 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
75 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Anders Carlssondf5a1c82009-08-28 19:16:39 +000076 void VisitUsingDecl(UsingDecl *D);
John McCall3f746822009-11-17 05:59:44 +000077 void VisitUsingShadowDecl(UsingShadowDecl *D);
Douglas Gregor278f52e2009-05-30 00:08:05 +000078 };
79}
80
Anders Carlsson46f87dc2009-09-26 21:58:53 +000081void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000082 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +000083}
84
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000085void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Anders Carlsson46f87dc2009-09-26 21:58:53 +000086 unsigned Indentation) const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +000087 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Anders Carlsson46f87dc2009-09-26 21:58:53 +000088 Printer.Visit(const_cast<Decl*>(this));
Douglas Gregor278f52e2009-05-30 00:08:05 +000089}
90
Eli Friedman79635842009-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 Kremenekc23c7e62009-07-29 21:53:49 +000097 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
Eli Friedman79635842009-05-30 04:20:30 +000098 BaseType = PTy->getPointeeType();
99 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
100 BaseType = ATy->getElementType();
John McCall9dd450b2009-09-21 23:43:11 +0000101 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
Eli Friedman79635842009-05-30 04:20:30 +0000102 BaseType = FTy->getResultType();
John McCall9dd450b2009-09-21 23:43:11 +0000103 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
Douglas Gregor15548252009-07-01 23:58:14 +0000104 BaseType = VTy->getElementType();
Eli Friedman79635842009-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 Kyrtzidis8a803cc2009-06-30 02:35:04 +0000120 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman79635842009-05-30 04:20:30 +0000121 unsigned Indentation) {
122 if (NumDecls == 1) {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000123 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman79635842009-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 Kyrtzidis8a803cc2009-06-30 02:35:04 +0000134 TD->print(Out, Policy, Indentation);
Eli Friedman79635842009-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 Kyrtzidis8a803cc2009-06-30 02:35:04 +0000149 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000150 }
151}
152
Anders Carlsson89112292009-12-14 00:51:04 +0000153void DeclContext::dumpDeclContext() const {
Anders Carlsson538af092009-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 Carlsson46f87dc2009-09-26 21:58:53 +0000164void Decl::dump() const {
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000165 print(llvm::errs());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000166}
167
Daniel Dunbar671f45e2009-11-21 09:12:06 +0000168llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
169 for (unsigned i = 0; i != Indentation; ++i)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000170 Out << " ";
171 return Out;
172}
173
Eli Friedman79635842009-05-30 04:20:30 +0000174void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
175 this->Indent();
Argyrios Kyrtzidis8a803cc2009-06-30 02:35:04 +0000176 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman79635842009-05-30 04:20:30 +0000177 Out << ";\n";
178 Decls.clear();
179
180}
181
Anders Carlsson601d6e42009-08-28 22:39:52 +0000182void DeclPrinter::Print(AccessSpecifier AS) {
183 switch(AS) {
Anders Carlsson6df9e072009-08-29 20:36:12 +0000184 case AS_none: assert(0 && "No access specifier!"); break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000185 case AS_public: Out << "public"; break;
186 case AS_protected: Out << "protected"; break;
Abramo Bagnarad7340582010-06-05 05:09:32 +0000187 case AS_private: Out << "private"; break;
Anders Carlsson601d6e42009-08-28 22:39:52 +0000188 }
189}
190
Douglas Gregor278f52e2009-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 Friedman79635842009-05-30 04:20:30 +0000199 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000200 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000201 D != DEnd; ++D) {
Ted Kremenek28e1c912010-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 Friedmanef334fd2009-05-30 05:03:24 +0000208 if (!Policy.Dump) {
209 // Skip over implicit declarations in pretty-printing mode.
210 if (D->isImplicit()) continue;
Eli Friedman17304242009-05-30 06:35:22 +0000211 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
Argyrios Kyrtzidisfa533e72010-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 Friedmanef334fd2009-05-30 05:03:24 +0000221 }
222
Eli Friedman79635842009-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 Bagnarad7340582010-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 Gregor278f52e2009-05-30 00:08:05 +0000261 this->Indent();
262 Visit(*D);
Mike Stump11289f42009-09-09 15:08:12 +0000263
264 // FIXME: Need to be able to tell the DeclPrinter when
Douglas Gregor278f52e2009-05-30 00:08:05 +0000265 const char *Terminator = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000266 if (isa<FunctionDecl>(*D) &&
Douglas Gregor278f52e2009-05-30 00:08:05 +0000267 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
268 Terminator = 0;
Douglas Gregor36098ff2009-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 Stump11289f42009-09-09 15:08:12 +0000272 isa<ObjCImplementationDecl>(*D) ||
Douglas Gregor36098ff2009-05-30 00:56:08 +0000273 isa<ObjCInterfaceDecl>(*D) ||
274 isa<ObjCProtocolDecl>(*D) ||
275 isa<ObjCCategoryImplDecl>(*D) ||
276 isa<ObjCCategoryDecl>(*D))
Douglas Gregor278f52e2009-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 Friedman79635842009-05-30 04:20:30 +0000291 if (!Decls.empty())
292 ProcessDeclGroup(Decls);
293
Douglas Gregor278f52e2009-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 Friedman79635842009-05-30 04:20:30 +0000305 if (!Policy.SuppressSpecifiers)
306 Out << "typedef ";
307 Out << S;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000308}
309
310void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
Douglas Gregorec0e3662010-12-01 16:01:08 +0000311 Out << "enum ";
Abramo Bagnara0e05e242010-12-03 18:54:17 +0000312 if (D->isScoped()) {
313 if (D->isScopedUsingClassTag())
314 Out << "class ";
315 else
316 Out << "struct ";
317 }
Douglas Gregorec0e3662010-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 Gregor278f52e2009-05-30 00:08:05 +0000331}
332
333void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000334 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000335 if (D->getIdentifier())
336 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Douglas Gregor278f52e2009-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 Kramerb11416d2010-04-17 09:33:03 +0000346 Out << D;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000347 if (Expr *Init = D->getInitExpr()) {
348 Out << " = ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000349 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000350 }
351}
352
Mike Stump11289f42009-09-09 15:08:12 +0000353void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000354 if (!Policy.SuppressSpecifiers) {
355 switch (D->getStorageClass()) {
John McCall8e7d6562010-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 Friedman79635842009-05-30 04:20:30 +0000361 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000362
Douglas Gregor35b57532009-10-27 21:01:01 +0000363 if (D->isInlineSpecified()) Out << "inline ";
Eli Friedman79635842009-05-30 04:20:30 +0000364 if (D->isVirtualAsWritten()) Out << "virtual ";
365 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000366
Douglas Gregor2d042f12009-05-30 05:39:39 +0000367 PrintingPolicy SubPolicy(Policy);
368 SubPolicy.SuppressSpecifiers = false;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000369 std::string Proto = D->getNameInfo().getAsString();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000370
Abramo Bagnara6d810632010-12-14 22:11:44 +0000371 QualType Ty = D->getType();
John McCall424cec92011-01-19 06:33:43 +0000372 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
Abramo Bagnara6d810632010-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 Gregor278f52e2009-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 Gregor2d042f12009-05-30 05:39:39 +0000386 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-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 Stump11289f42009-09-09 15:08:12 +0000391
Douglas Gregor278f52e2009-05-30 00:08:05 +0000392 if (FT->isVariadic()) {
393 if (D->getNumParams()) POut << ", ";
394 POut << "...";
395 }
Eli Friedman79635842009-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 Gregor278f52e2009-05-30 00:08:05 +0000402 }
403
404 Proto += ")";
Douglas Gregor049bdca2009-12-08 17:45:32 +0000405
Douglas Gregor8fc96fc2010-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 }
415
Douglas Gregor049bdca2009-12-08 17:45:32 +0000416 if (FT && FT->hasExceptionSpec()) {
417 Proto += " throw(";
418 if (FT->hasAnyExceptionSpec())
419 Proto += "...";
420 else
421 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
422 if (I)
423 Proto += ", ";
424
425
426 std::string ExceptionType;
427 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
428 Proto += ExceptionType;
429 }
430 Proto += ")";
431 }
432
Mike Stump9a9e0c22009-07-27 21:33:40 +0000433 if (D->hasAttr<NoReturnAttr>())
434 Proto += " __attribute((noreturn))";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000435 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
Alexis Hunt1d792652011-01-08 20:30:50 +0000436 if (CDecl->getNumCtorInitializers() > 0) {
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000437 Proto += " : ";
438 Out << Proto;
439 Proto.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000440 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000441 E = CDecl->init_end();
442 B != E; ++B) {
Alexis Hunt1d792652011-01-08 20:30:50 +0000443 CXXCtorInitializer * BMInitializer = (*B);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000444 if (B != CDecl->init_begin())
445 Out << ", ";
Francois Pichetd583da02010-12-04 09:14:42 +0000446 if (BMInitializer->isAnyMemberInitializer()) {
447 FieldDecl *FD = BMInitializer->getAnyMember();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000448 Out << FD;
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000449 } else {
Daniel Dunbar219fa692010-06-30 19:16:48 +0000450 Out << QualType(BMInitializer->getBaseClass(),
451 0).getAsString(Policy);
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000452 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000453
454 Out << "(";
455 if (!BMInitializer->getInit()) {
456 // Nothing to print
457 } else {
458 Expr *Init = BMInitializer->getInit();
John McCall5d413782010-12-06 08:20:24 +0000459 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000460 Init = Tmp->getSubExpr();
461
462 Init = Init->IgnoreParens();
463
464 Expr *SimpleInit = 0;
465 Expr **Args = 0;
466 unsigned NumArgs = 0;
467 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
468 Args = ParenList->getExprs();
469 NumArgs = ParenList->getNumExprs();
470 } else if (CXXConstructExpr *Construct
471 = dyn_cast<CXXConstructExpr>(Init)) {
472 Args = Construct->getArgs();
473 NumArgs = Construct->getNumArgs();
474 } else
475 SimpleInit = Init;
476
477 if (SimpleInit)
478 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
479 else {
480 for (unsigned I = 0; I != NumArgs; ++I) {
481 if (isa<CXXDefaultArgExpr>(Args[I]))
482 break;
483
484 if (I)
485 Out << ", ";
486 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
487 }
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000488 }
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000489 }
490 Out << ")";
Fariborz Jahanian494720b2009-07-13 20:18:13 +0000491 }
492 }
493 }
494 else
495 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000496 } else {
Abramo Bagnara6d810632010-12-14 22:11:44 +0000497 Ty.getAsStringInternal(Proto, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000498 }
499
500 Out << Proto;
501
502 if (D->isPure())
503 Out << " = 0";
504 else if (D->isDeleted())
505 Out << " = delete";
506 else if (D->isThisDeclarationADefinition()) {
507 if (!D->hasPrototype() && D->getNumParams()) {
508 // This is a K&R function definition, so we need to print the
509 // parameters.
510 Out << '\n';
Douglas Gregor2d042f12009-05-30 05:39:39 +0000511 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000512 Indentation += Policy.Indentation;
513 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
514 Indent();
Douglas Gregor2d042f12009-05-30 05:39:39 +0000515 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor278f52e2009-05-30 00:08:05 +0000516 Out << ";\n";
517 }
518 Indentation -= Policy.Indentation;
519 } else
520 Out << ' ';
521
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000522 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000523 Out << '\n';
524 }
525}
526
527void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman79635842009-05-30 04:20:30 +0000528 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000529 Out << "mutable ";
530
531 std::string Name = D->getNameAsString();
532 D->getType().getAsStringInternal(Name, Policy);
533 Out << Name;
534
535 if (D->isBitField()) {
536 Out << " : ";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000537 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000538 }
539}
540
Chris Lattnercab02a62011-02-17 20:34:02 +0000541void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
542 Out << D->getNameAsString() << ":";
543}
544
545
Douglas Gregor278f52e2009-05-30 00:08:05 +0000546void DeclPrinter::VisitVarDecl(VarDecl *D) {
John McCall8e7d6562010-08-26 03:08:43 +0000547 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
Douglas Gregor278f52e2009-05-30 00:08:05 +0000548 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
549
Eli Friedman79635842009-05-30 04:20:30 +0000550 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor278f52e2009-05-30 00:08:05 +0000551 Out << "__thread ";
552
553 std::string Name = D->getNameAsString();
554 QualType T = D->getType();
John McCall856bbea2009-10-23 21:48:59 +0000555 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
Douglas Gregor278f52e2009-05-30 00:08:05 +0000556 T = Parm->getOriginalType();
557 T.getAsStringInternal(Name, Policy);
558 Out << Name;
Ted Kremenek41994fd2010-09-07 22:21:59 +0000559 if (Expr *Init = D->getInit()) {
Douglas Gregor278f52e2009-05-30 00:08:05 +0000560 if (D->hasCXXDirectInitializer())
561 Out << "(";
Ted Kremenek35869382010-09-17 23:04:38 +0000562 else {
563 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
564 if (!CCE || CCE->getConstructor()->isCopyConstructor())
565 Out << " = ";
566 }
Ted Kremenek41994fd2010-09-07 22:21:59 +0000567 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000568 if (D->hasCXXDirectInitializer())
569 Out << ")";
570 }
571}
572
573void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
574 VisitVarDecl(D);
575}
576
577void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
578 Out << "__asm (";
Eli Friedmanef334fd2009-05-30 05:03:24 +0000579 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000580 Out << ")";
581}
582
583//----------------------------------------------------------------------------
584// C++ declarations
585//----------------------------------------------------------------------------
Douglas Gregor5f478b72009-05-30 06:58:37 +0000586void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000587 Out << "namespace " << D << " {\n";
Douglas Gregor5f478b72009-05-30 06:58:37 +0000588 VisitDeclContext(D);
589 Indent() << "}";
590}
591
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000592void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
593 Out << "using namespace ";
594 if (D->getQualifier())
595 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000596 Out << D->getNominatedNamespaceAsWritten();
Douglas Gregor3bc6e4c2009-05-30 06:31:56 +0000597}
598
Douglas Gregor18231932009-05-30 06:48:27 +0000599void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000600 Out << "namespace " << D << " = ";
Douglas Gregor18231932009-05-30 06:48:27 +0000601 if (D->getQualifier())
602 D->getQualifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000603 Out << D->getAliasedNamespace();
Douglas Gregor18231932009-05-30 06:48:27 +0000604}
605
Douglas Gregor5f478b72009-05-30 06:58:37 +0000606void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
607 Out << D->getKindName();
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000608 if (D->getIdentifier())
609 Out << ' ' << D;
Mike Stump11289f42009-09-09 15:08:12 +0000610
Douglas Gregor5f478b72009-05-30 06:58:37 +0000611 if (D->isDefinition()) {
612 // Print the base classes
613 if (D->getNumBases()) {
614 Out << " : ";
Mike Stump11289f42009-09-09 15:08:12 +0000615 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
616 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
Douglas Gregor5f478b72009-05-30 06:58:37 +0000617 if (Base != D->bases_begin())
618 Out << ", ";
619
620 if (Base->isVirtual())
621 Out << "virtual ";
622
Anders Carlsson6df9e072009-08-29 20:36:12 +0000623 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
624 if (AS != AS_none)
625 Print(AS);
Anders Carlsson601d6e42009-08-28 22:39:52 +0000626 Out << " " << Base->getType().getAsString(Policy);
Douglas Gregor5f478b72009-05-30 06:58:37 +0000627 }
628 }
629
630 // Print the class definition
Douglas Gregor7a1a7cb2009-05-31 07:13:39 +0000631 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor5f478b72009-05-30 06:58:37 +0000632 Out << " {\n";
633 VisitDeclContext(D);
634 Indent() << "}";
Mike Stump11289f42009-09-09 15:08:12 +0000635 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000636}
637
638void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
639 const char *l;
640 if (D->getLanguage() == LinkageSpecDecl::lang_c)
641 l = "C";
642 else {
643 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
644 "unknown language in linkage specification");
645 l = "C++";
646 }
647
648 Out << "extern \"" << l << "\" ";
649 if (D->hasBraces()) {
650 Out << "{\n";
651 VisitDeclContext(D);
652 Indent() << "}";
653 } else
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000654 Visit(*D->decls_begin());
Douglas Gregor278f52e2009-05-30 00:08:05 +0000655}
656
657void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000658 Out << "template <";
Mike Stump11289f42009-09-09 15:08:12 +0000659
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000660 TemplateParameterList *Params = D->getTemplateParameters();
661 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
662 if (i != 0)
663 Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000664
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000665 const Decl *Param = Params->getParam(i);
Mike Stump11289f42009-09-09 15:08:12 +0000666 if (const TemplateTypeParmDecl *TTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000667 dyn_cast<TemplateTypeParmDecl>(Param)) {
Mike Stump11289f42009-09-09 15:08:12 +0000668
669 QualType ParamType =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000670 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
671
672 if (TTP->wasDeclaredWithTypename())
673 Out << "typename ";
674 else
675 Out << "class ";
676
Anders Carlssonfb1d7762009-06-12 22:23:22 +0000677 if (TTP->isParameterPack())
678 Out << "... ";
Mike Stump11289f42009-09-09 15:08:12 +0000679
Douglas Gregor2ebcae12010-06-16 15:23:05 +0000680 Out << ParamType.getAsString(Policy);
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000681
682 if (TTP->hasDefaultArgument()) {
683 Out << " = ";
684 Out << TTP->getDefaultArgument().getAsString(Policy);
685 };
Mike Stump11289f42009-09-09 15:08:12 +0000686 } else if (const NonTypeTemplateParmDecl *NTTP =
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000687 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
688 Out << NTTP->getType().getAsString(Policy);
689
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000690 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
691 Out << "...";
692
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000693 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
694 Out << ' ';
695 Out << Name->getName();
696 }
Mike Stump11289f42009-09-09 15:08:12 +0000697
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000698 if (NTTP->hasDefaultArgument()) {
699 Out << " = ";
Mike Stump11289f42009-09-09 15:08:12 +0000700 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000701 Indentation);
702 }
703 }
704 }
Mike Stump11289f42009-09-09 15:08:12 +0000705
Anders Carlsson40f8f8d2009-06-04 05:37:43 +0000706 Out << "> ";
707
Douglas Gregorf5500772011-01-05 15:48:55 +0000708 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
709 Out << "class ";
710 if (TTP->isParameterPack())
711 Out << "...";
712 Out << D->getName();
Craig Silverstein4a5d0862010-07-09 20:25:10 +0000713 } else {
714 Visit(D->getTemplatedDecl());
715 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000716}
717
718//----------------------------------------------------------------------------
719// Objective-C declarations
720//----------------------------------------------------------------------------
721
722void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
723 Out << "@class ";
724 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
725 I != E; ++I) {
726 if (I != D->begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000727 Out << I->getInterface();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000728 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000729}
730
731void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
732 if (OMD->isInstanceMethod())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000733 Out << "- ";
Mike Stump11289f42009-09-09 15:08:12 +0000734 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000735 Out << "+ ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000736 if (!OMD->getResultType().isNull())
Douglas Gregor5f478b72009-05-30 06:58:37 +0000737 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor278f52e2009-05-30 00:08:05 +0000739 std::string name = OMD->getSelector().getAsString();
740 std::string::size_type pos, lastPos = 0;
741 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
742 E = OMD->param_end(); PI != E; ++PI) {
Mike Stump11289f42009-09-09 15:08:12 +0000743 // FIXME: selector is missing here!
Douglas Gregor278f52e2009-05-30 00:08:05 +0000744 pos = name.find_first_of(":", lastPos);
745 Out << " " << name.substr(lastPos, pos - lastPos);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000746 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000747 lastPos = pos + 1;
748 }
Mike Stump11289f42009-09-09 15:08:12 +0000749
Douglas Gregor278f52e2009-05-30 00:08:05 +0000750 if (OMD->param_begin() == OMD->param_end())
751 Out << " " << name;
Mike Stump11289f42009-09-09 15:08:12 +0000752
Douglas Gregor278f52e2009-05-30 00:08:05 +0000753 if (OMD->isVariadic())
754 Out << ", ...";
Mike Stump11289f42009-09-09 15:08:12 +0000755
Douglas Gregor278f52e2009-05-30 00:08:05 +0000756 if (OMD->getBody()) {
757 Out << ' ';
Eli Friedmanef334fd2009-05-30 05:03:24 +0000758 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor278f52e2009-05-30 00:08:05 +0000759 Out << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000760 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000761}
762
763void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
764 std::string I = OID->getNameAsString();
765 ObjCInterfaceDecl *SID = OID->getSuperClass();
766
767 if (SID)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000768 Out << "@implementation " << I << " : " << SID;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000769 else
770 Out << "@implementation " << I;
Douglas Gregor36098ff2009-05-30 00:56:08 +0000771 Out << "\n";
772 VisitDeclContext(OID, false);
773 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000774}
775
776void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
777 std::string I = OID->getNameAsString();
778 ObjCInterfaceDecl *SID = OID->getSuperClass();
779
780 if (SID)
Douglas Gregor1c283312010-08-11 12:19:30 +0000781 Out << "@interface " << I << " : " << SID;
782 else
783 Out << "@interface " << I;
Mike Stump11289f42009-09-09 15:08:12 +0000784
Douglas Gregor278f52e2009-05-30 00:08:05 +0000785 // Protocols?
786 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
787 if (!Protocols.empty()) {
788 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
789 E = Protocols.end(); I != E; ++I)
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000790 Out << (I == Protocols.begin() ? '<' : ',') << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000791 }
Mike Stump11289f42009-09-09 15:08:12 +0000792
Douglas Gregor278f52e2009-05-30 00:08:05 +0000793 if (!Protocols.empty())
Douglas Gregor36098ff2009-05-30 00:56:08 +0000794 Out << "> ";
Mike Stump11289f42009-09-09 15:08:12 +0000795
Douglas Gregor278f52e2009-05-30 00:08:05 +0000796 if (OID->ivar_size() > 0) {
Douglas Gregor36098ff2009-05-30 00:56:08 +0000797 Out << "{\n";
798 Indentation += Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000799 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
800 E = OID->ivar_end(); I != E; ++I) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000801 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000802 }
Douglas Gregor36098ff2009-05-30 00:56:08 +0000803 Indentation -= Policy.Indentation;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000804 Out << "}\n";
805 }
Mike Stump11289f42009-09-09 15:08:12 +0000806
Douglas Gregor278f52e2009-05-30 00:08:05 +0000807 VisitDeclContext(OID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000808 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000809 // FIXME: implement the rest...
810}
811
812void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
813 Out << "@protocol ";
Mike Stump11289f42009-09-09 15:08:12 +0000814 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
Douglas Gregor278f52e2009-05-30 00:08:05 +0000815 E = D->protocol_end();
816 I != E; ++I) {
817 if (I != D->protocol_begin()) Out << ", ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000818 Out << *I;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000819 }
Douglas Gregor278f52e2009-05-30 00:08:05 +0000820}
821
822void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000823 Out << "@protocol " << PID << '\n';
Douglas Gregor36098ff2009-05-30 00:56:08 +0000824 VisitDeclContext(PID, false);
825 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000826}
827
828void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000829 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000830
831 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000832 Out << "@end";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000833 // FIXME: implement the rest...
834}
835
836void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000837 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000838 VisitDeclContext(PID, false);
Douglas Gregor36098ff2009-05-30 00:56:08 +0000839 Out << "@end";
Mike Stump11289f42009-09-09 15:08:12 +0000840
Douglas Gregor278f52e2009-05-30 00:08:05 +0000841 // FIXME: implement the rest...
842}
843
844void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000845 Out << "@compatibility_alias " << AID
846 << ' ' << AID->getClassInterface() << ";\n";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000847}
848
849/// PrintObjCPropertyDecl - print a property declaration.
850///
851void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
852 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
853 Out << "@required\n";
854 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
855 Out << "@optional\n";
Mike Stump11289f42009-09-09 15:08:12 +0000856
Douglas Gregor278f52e2009-05-30 00:08:05 +0000857 Out << "@property";
858 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
859 bool first = true;
860 Out << " (";
Mike Stump11289f42009-09-09 15:08:12 +0000861 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000862 ObjCPropertyDecl::OBJC_PR_readonly) {
863 Out << (first ? ' ' : ',') << "readonly";
864 first = false;
865 }
Mike Stump11289f42009-09-09 15:08:12 +0000866
Douglas Gregor278f52e2009-05-30 00:08:05 +0000867 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
868 Out << (first ? ' ' : ',') << "getter = "
869 << PDecl->getGetterName().getAsString();
870 first = false;
871 }
872 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
873 Out << (first ? ' ' : ',') << "setter = "
874 << PDecl->getSetterName().getAsString();
875 first = false;
876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
Douglas Gregor278f52e2009-05-30 00:08:05 +0000878 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
879 Out << (first ? ' ' : ',') << "assign";
880 first = false;
881 }
Mike Stump11289f42009-09-09 15:08:12 +0000882
Douglas Gregor278f52e2009-05-30 00:08:05 +0000883 if (PDecl->getPropertyAttributes() &
884 ObjCPropertyDecl::OBJC_PR_readwrite) {
885 Out << (first ? ' ' : ',') << "readwrite";
886 first = false;
887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Douglas Gregor278f52e2009-05-30 00:08:05 +0000889 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
890 Out << (first ? ' ' : ',') << "retain";
891 first = false;
892 }
Mike Stump11289f42009-09-09 15:08:12 +0000893
Douglas Gregor278f52e2009-05-30 00:08:05 +0000894 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
895 Out << (first ? ' ' : ',') << "copy";
896 first = false;
897 }
Mike Stump11289f42009-09-09 15:08:12 +0000898
899 if (PDecl->getPropertyAttributes() &
Douglas Gregor278f52e2009-05-30 00:08:05 +0000900 ObjCPropertyDecl::OBJC_PR_nonatomic) {
901 Out << (first ? ' ' : ',') << "nonatomic";
902 first = false;
903 }
904 Out << " )";
905 }
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000906 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
Douglas Gregor278f52e2009-05-30 00:08:05 +0000907}
908
909void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
910 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor36098ff2009-05-30 00:56:08 +0000911 Out << "@synthesize ";
Douglas Gregor278f52e2009-05-30 00:08:05 +0000912 else
Douglas Gregor36098ff2009-05-30 00:56:08 +0000913 Out << "@dynamic ";
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000914 Out << PID->getPropertyDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000915 if (PID->getPropertyIvarDecl())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000916 Out << '=' << PID->getPropertyIvarDecl();
Douglas Gregor278f52e2009-05-30 00:08:05 +0000917}
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000918
919void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
920 Out << "using ";
921 D->getTargetNestedNameDecl()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000922 Out << D;
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000923}
924
John McCalle61f2ba2009-11-18 02:36:19 +0000925void
926DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
927 Out << "using typename ";
928 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000929 Out << D->getDeclName();
John McCalle61f2ba2009-11-18 02:36:19 +0000930}
931
932void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000933 Out << "using ";
934 D->getTargetNestedNameSpecifier()->print(Out, Policy);
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000935 Out << D->getDeclName();
Anders Carlssondf5a1c82009-08-28 19:16:39 +0000936}
John McCall3f746822009-11-17 05:59:44 +0000937
938void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {
939 // ignore
940}