blob: fcc623ec787a49c91dc2ba03a59bdde590990f9e [file] [log] [blame]
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +00001//===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl::dump method, which pretty print the
11// AST back out to C/Objective-C/C++/Objective-C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/PrettyPrinter.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/Streams.h"
23#include "llvm/Support/Format.h"
24#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27namespace {
28 class VISIBILITY_HIDDEN DeclPrinter : public DeclVisitor<DeclPrinter> {
29 llvm::raw_ostream &Out;
30 ASTContext &Context;
31 PrintingPolicy Policy;
32 unsigned Indentation;
33
34 llvm::raw_ostream& Indent();
Eli Friedman42f42c02009-05-30 04:20:30 +000035 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000036
37 public:
38 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
39 const PrintingPolicy &Policy,
40 unsigned Indentation = 0)
41 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
42
43 void VisitDeclContext(DeclContext *DC, bool Indent = true);
44
45 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
46 void VisitTypedefDecl(TypedefDecl *D);
47 void VisitEnumDecl(EnumDecl *D);
48 void VisitRecordDecl(RecordDecl *D);
49 void VisitEnumConstantDecl(EnumConstantDecl *D);
50 void VisitFunctionDecl(FunctionDecl *D);
51 void VisitFieldDecl(FieldDecl *D);
52 void VisitVarDecl(VarDecl *D);
53 void VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000054 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000055 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000056 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000057 void VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000058 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor6c9c9402009-05-30 06:48:27 +000059 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor59e63572009-05-30 06:58:37 +000060 void VisitCXXRecordDecl(CXXRecordDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000061 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
62 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000063 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor8419fa32009-05-30 06:31:56 +000064 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000065 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
66 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
67 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
68 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
69 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
70 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
71 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
72 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
73 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
74 };
75}
76
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000077void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) {
78 print(Out, getASTContext().PrintingPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000079}
80
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +000081void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
82 unsigned Indentation) {
83 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +000084 Printer.Visit(this);
85}
86
Eli Friedman42f42c02009-05-30 04:20:30 +000087static QualType GetBaseType(QualType T) {
88 // FIXME: This should be on the Type class!
89 QualType BaseType = T;
90 while (!BaseType->isSpecifierType()) {
91 if (isa<TypedefType>(BaseType))
92 break;
Ted Kremenek35366a62009-07-17 17:50:17 +000093 else if (const PointerType* PTy = BaseType->getAsPointerType())
Eli Friedman42f42c02009-05-30 04:20:30 +000094 BaseType = PTy->getPointeeType();
95 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
96 BaseType = ATy->getElementType();
97 else if (const FunctionType* FTy = BaseType->getAsFunctionType())
98 BaseType = FTy->getResultType();
Douglas Gregor5068ab62009-07-01 23:58:14 +000099 else if (const VectorType *VTy = BaseType->getAsVectorType())
100 BaseType = VTy->getElementType();
Eli Friedman42f42c02009-05-30 04:20:30 +0000101 else
102 assert(0 && "Unknown declarator!");
103 }
104 return BaseType;
105}
106
107static QualType getDeclType(Decl* D) {
108 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
109 return TDD->getUnderlyingType();
110 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
111 return VD->getType();
112 return QualType();
113}
114
115void Decl::printGroup(Decl** Begin, unsigned NumDecls,
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000116 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
Eli Friedman42f42c02009-05-30 04:20:30 +0000117 unsigned Indentation) {
118 if (NumDecls == 1) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000119 (*Begin)->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000120 return;
121 }
122
123 Decl** End = Begin + NumDecls;
124 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
125 if (TD)
126 ++Begin;
127
128 PrintingPolicy SubPolicy(Policy);
129 if (TD && TD->isDefinition()) {
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000130 TD->print(Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000131 Out << " ";
132 SubPolicy.SuppressTag = true;
133 }
134
135 bool isFirst = true;
136 for ( ; Begin != End; ++Begin) {
137 if (isFirst) {
138 SubPolicy.SuppressSpecifiers = false;
139 isFirst = false;
140 } else {
141 if (!isFirst) Out << ", ";
142 SubPolicy.SuppressSpecifiers = true;
143 }
144
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000145 (*Begin)->print(Out, SubPolicy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000146 }
147}
148
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000149void Decl::dump() {
150 print(llvm::errs());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000151}
152
153llvm::raw_ostream& DeclPrinter::Indent() {
154 for (unsigned i = 0; i < Indentation; ++i)
155 Out << " ";
156 return Out;
157}
158
Eli Friedman42f42c02009-05-30 04:20:30 +0000159void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
160 this->Indent();
Argyrios Kyrtzidisf1d60ea2009-06-30 02:35:04 +0000161 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
Eli Friedman42f42c02009-05-30 04:20:30 +0000162 Out << ";\n";
163 Decls.clear();
164
165}
166
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000167//----------------------------------------------------------------------------
168// Common C declarations
169//----------------------------------------------------------------------------
170
171void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
172 if (Indent)
173 Indentation += Policy.Indentation;
174
Eli Friedman42f42c02009-05-30 04:20:30 +0000175 llvm::SmallVector<Decl*, 2> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000176 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000177 D != DEnd; ++D) {
Eli Friedman48d14a22009-05-30 05:03:24 +0000178 if (!Policy.Dump) {
179 // Skip over implicit declarations in pretty-printing mode.
180 if (D->isImplicit()) continue;
Eli Friedman3d4a7c92009-05-30 06:35:22 +0000181 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
182 // of __builtin_va_list. There should be some other way to check that.
183 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
184 "__builtin_va_list")
185 continue;
Eli Friedman48d14a22009-05-30 05:03:24 +0000186 }
187
Eli Friedman42f42c02009-05-30 04:20:30 +0000188 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
189 // forced to merge the declarations because there's no other way to
190 // refer to the struct in question. This limited merging is safe without
191 // a bunch of other checks because it only merges declarations directly
192 // referring to the tag, not typedefs.
193 //
194 // Check whether the current declaration should be grouped with a previous
195 // unnamed struct.
196 QualType CurDeclType = getDeclType(*D);
197 if (!Decls.empty() && !CurDeclType.isNull()) {
198 QualType BaseType = GetBaseType(CurDeclType);
199 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
200 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
201 Decls.push_back(*D);
202 continue;
203 }
204 }
205
206 // If we have a merged group waiting to be handled, handle it now.
207 if (!Decls.empty())
208 ProcessDeclGroup(Decls);
209
210 // If the current declaration is an unnamed tag type, save it
211 // so we can merge it with the subsequent declaration(s) using it.
212 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
213 Decls.push_back(*D);
214 continue;
215 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000216 this->Indent();
217 Visit(*D);
218
219 // FIXME: Need to be able to tell the DeclPrinter when
220 const char *Terminator = 0;
221 if (isa<FunctionDecl>(*D) &&
222 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
223 Terminator = 0;
Douglas Gregor64f65002009-05-30 00:56:08 +0000224 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
225 Terminator = 0;
226 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
227 isa<ObjCImplementationDecl>(*D) ||
228 isa<ObjCInterfaceDecl>(*D) ||
229 isa<ObjCProtocolDecl>(*D) ||
230 isa<ObjCCategoryImplDecl>(*D) ||
231 isa<ObjCCategoryDecl>(*D))
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000232 Terminator = 0;
233 else if (isa<EnumConstantDecl>(*D)) {
234 DeclContext::decl_iterator Next = D;
235 ++Next;
236 if (Next != DEnd)
237 Terminator = ",";
238 } else
239 Terminator = ";";
240
241 if (Terminator)
242 Out << Terminator;
243 Out << "\n";
244 }
245
Eli Friedman42f42c02009-05-30 04:20:30 +0000246 if (!Decls.empty())
247 ProcessDeclGroup(Decls);
248
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000249 if (Indent)
250 Indentation -= Policy.Indentation;
251}
252
253void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
254 VisitDeclContext(D, false);
255}
256
257void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
258 std::string S = D->getNameAsString();
259 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedman42f42c02009-05-30 04:20:30 +0000260 if (!Policy.SuppressSpecifiers)
261 Out << "typedef ";
262 Out << S;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000263}
264
265void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
266 Out << "enum " << D->getNameAsString() << " {\n";
267 VisitDeclContext(D);
268 Indent() << "}";
269}
270
271void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000272 Out << D->getKindName();
Eli Friedman42f42c02009-05-30 04:20:30 +0000273 if (D->getIdentifier()) {
274 Out << " ";
275 Out << D->getNameAsString();
276 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000277
278 if (D->isDefinition()) {
279 Out << " {\n";
280 VisitDeclContext(D);
281 Indent() << "}";
282 }
283}
284
285void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
286 Out << D->getNameAsString();
287 if (Expr *Init = D->getInitExpr()) {
288 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000289 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000290 }
291}
292
293void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000294 if (!Policy.SuppressSpecifiers) {
295 switch (D->getStorageClass()) {
296 case FunctionDecl::None: break;
297 case FunctionDecl::Extern: Out << "extern "; break;
298 case FunctionDecl::Static: Out << "static "; break;
299 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
300 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000301
Eli Friedman42f42c02009-05-30 04:20:30 +0000302 if (D->isInline()) Out << "inline ";
303 if (D->isVirtualAsWritten()) Out << "virtual ";
304 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000305
Douglas Gregor6620a622009-05-30 05:39:39 +0000306 PrintingPolicy SubPolicy(Policy);
307 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000308 std::string Proto = D->getNameAsString();
309 if (isa<FunctionType>(D->getType().getTypePtr())) {
310 const FunctionType *AFT = D->getType()->getAsFunctionType();
311
312 const FunctionProtoType *FT = 0;
313 if (D->hasWrittenPrototype())
314 FT = dyn_cast<FunctionProtoType>(AFT);
315
316 Proto += "(";
317 if (FT) {
318 llvm::raw_string_ostream POut(Proto);
Douglas Gregor6620a622009-05-30 05:39:39 +0000319 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000320 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
321 if (i) POut << ", ";
322 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
323 }
324
325 if (FT->isVariadic()) {
326 if (D->getNumParams()) POut << ", ";
327 POut << "...";
328 }
Eli Friedman42f42c02009-05-30 04:20:30 +0000329 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
330 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
331 if (i)
332 Proto += ", ";
333 Proto += D->getParamDecl(i)->getNameAsString();
334 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000335 }
336
337 Proto += ")";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000338 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
339 if (CDecl->getNumBaseOrMemberInitializers() > 0) {
340 Proto += " : ";
341 Out << Proto;
342 Proto.clear();
343 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
344 E = CDecl->init_end();
345 B != E; ++B) {
346 CXXBaseOrMemberInitializer * BMInitializer = (*B);
347 if (B != CDecl->init_begin())
348 Out << ", ";
349 bool hasArguments = (BMInitializer->begin() != BMInitializer->end());
350 if (BMInitializer->isMemberInitializer()) {
351 FieldDecl *FD = BMInitializer->getMember();
352 Out << FD->getNameAsString();
353 }
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000354 else // FIXME. skip dependent types for now.
355 if (const RecordType *RT =
Ted Kremenek35366a62009-07-17 17:50:17 +0000356 BMInitializer->getBaseClass()->getAsRecordType()) {
Fariborz Jahanian29146ad2009-07-14 23:41:35 +0000357 const CXXRecordDecl *BaseDecl =
358 cast<CXXRecordDecl>(RT->getDecl());
359 Out << BaseDecl->getNameAsString();
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000360 }
361 if (hasArguments) {
362 Out << "(";
363 for (CXXBaseOrMemberInitializer::arg_const_iterator BE =
364 BMInitializer->begin(), EE = BMInitializer->end();
365 BE != EE; BE++) {
366 if (BE != BMInitializer->begin())
367 Out<< ", ";
368 Expr *Exp = (*BE);
369 Exp->printPretty(Out, Context, 0, Policy, Indentation);
370 }
371 Out << ")";
Fariborz Jahanian939afd82009-07-13 23:31:10 +0000372 } else
373 Out << "()";
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000374 }
375 }
376 }
Fariborz Jahanian560de452009-07-15 22:34:08 +0000377 else if (CXXDestructorDecl *DDecl = dyn_cast<CXXDestructorDecl>(D)) {
378 if (DDecl->getNumBaseOrMemberDestructions() > 0) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000379 // List order of base/member destruction for visualization purposes.
Fariborz Jahanian560de452009-07-15 22:34:08 +0000380 assert (D->isThisDeclarationADefinition() && "Destructor with dtor-list");
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000381 Proto += "/* : ";
382 for (CXXDestructorDecl::destr_const_iterator *B = DDecl->destr_begin(),
383 *E = DDecl->destr_end();
Fariborz Jahanian560de452009-07-15 22:34:08 +0000384 B != E; ++B) {
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000385 uintptr_t BaseOrMember = (*B);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000386 if (B != DDecl->destr_begin())
387 Proto += ", ";
388
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000389 if (DDecl->isMemberToDestroy(BaseOrMember)) {
390 FieldDecl *FD = DDecl->getMemberToDestroy(BaseOrMember);
Fariborz Jahanian560de452009-07-15 22:34:08 +0000391 Proto += "~";
392 Proto += FD->getNameAsString();
393 }
394 else // FIXME. skip dependent types for now.
395 if (const RecordType *RT =
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000396 DDecl->getAnyBaseClassToDestroy(BaseOrMember)
397 ->getAsRecordType()) {
Fariborz Jahanian560de452009-07-15 22:34:08 +0000398 const CXXRecordDecl *BaseDecl =
399 cast<CXXRecordDecl>(RT->getDecl());
400 Proto += "~";
401 Proto += BaseDecl->getNameAsString();
402 }
403 Proto += "()";
404 }
Fariborz Jahanian393612e2009-07-21 22:36:06 +0000405 Proto += " */";
Fariborz Jahanian560de452009-07-15 22:34:08 +0000406 }
407 }
Fariborz Jahanian66192ad2009-07-13 20:18:13 +0000408 else
409 AFT->getResultType().getAsStringInternal(Proto, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000410 } else {
411 D->getType().getAsStringInternal(Proto, Policy);
412 }
413
414 Out << Proto;
415
416 if (D->isPure())
417 Out << " = 0";
418 else if (D->isDeleted())
419 Out << " = delete";
420 else if (D->isThisDeclarationADefinition()) {
421 if (!D->hasPrototype() && D->getNumParams()) {
422 // This is a K&R function definition, so we need to print the
423 // parameters.
424 Out << '\n';
Douglas Gregor6620a622009-05-30 05:39:39 +0000425 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000426 Indentation += Policy.Indentation;
427 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
428 Indent();
Douglas Gregor6620a622009-05-30 05:39:39 +0000429 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000430 Out << ";\n";
431 }
432 Indentation -= Policy.Indentation;
433 } else
434 Out << ' ';
435
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000436 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000437 Out << '\n';
438 }
439}
440
441void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000442 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000443 Out << "mutable ";
444
445 std::string Name = D->getNameAsString();
446 D->getType().getAsStringInternal(Name, Policy);
447 Out << Name;
448
449 if (D->isBitField()) {
450 Out << " : ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000451 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000452 }
453}
454
455void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedman42f42c02009-05-30 04:20:30 +0000456 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000457 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
458
Eli Friedman42f42c02009-05-30 04:20:30 +0000459 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000460 Out << "__thread ";
461
462 std::string Name = D->getNameAsString();
463 QualType T = D->getType();
464 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
465 T = Parm->getOriginalType();
466 T.getAsStringInternal(Name, Policy);
467 Out << Name;
468 if (D->getInit()) {
469 if (D->hasCXXDirectInitializer())
470 Out << "(";
471 else
472 Out << " = ";
Eli Friedman48d14a22009-05-30 05:03:24 +0000473 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000474 if (D->hasCXXDirectInitializer())
475 Out << ")";
476 }
477}
478
479void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
480 VisitVarDecl(D);
481}
482
Douglas Gregor8419fa32009-05-30 06:31:56 +0000483void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
484 VisitVarDecl(D);
485}
486
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000487void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
488 Out << "__asm (";
Eli Friedman48d14a22009-05-30 05:03:24 +0000489 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000490 Out << ")";
491}
492
493//----------------------------------------------------------------------------
494// C++ declarations
495//----------------------------------------------------------------------------
Douglas Gregor8419fa32009-05-30 06:31:56 +0000496void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
497 assert(false &&
498 "OverloadedFunctionDecls aren't really decls and are never printed");
499}
500
Douglas Gregor59e63572009-05-30 06:58:37 +0000501void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
502 Out << "namespace " << D->getNameAsString() << " {\n";
503 VisitDeclContext(D);
504 Indent() << "}";
505}
506
Douglas Gregor8419fa32009-05-30 06:31:56 +0000507void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
508 Out << "using namespace ";
509 if (D->getQualifier())
510 D->getQualifier()->print(Out, Policy);
511 Out << D->getNominatedNamespace()->getNameAsString();
512}
513
Douglas Gregor6c9c9402009-05-30 06:48:27 +0000514void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
515 Out << "namespace " << D->getNameAsString() << " = ";
516 if (D->getQualifier())
517 D->getQualifier()->print(Out, Policy);
518 Out << D->getAliasedNamespace()->getNameAsString();
519}
520
Douglas Gregor59e63572009-05-30 06:58:37 +0000521void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
522 Out << D->getKindName();
523 if (D->getIdentifier()) {
524 Out << " ";
525 Out << D->getNameAsString();
526 }
527
528 if (D->isDefinition()) {
529 // Print the base classes
530 if (D->getNumBases()) {
531 Out << " : ";
532 for(CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
533 BaseEnd = D->bases_end();
534 Base != BaseEnd; ++Base) {
535 if (Base != D->bases_begin())
536 Out << ", ";
537
538 if (Base->isVirtual())
539 Out << "virtual ";
540
541 switch(Base->getAccessSpecifierAsWritten()) {
542 case AS_none: break;
543 case AS_public: Out << "public "; break;
544 case AS_protected: Out << "protected "; break;
545 case AS_private: Out << " private "; break;
546 }
547
548 Out << Base->getType().getAsString(Policy);
549 }
550 }
551
552 // Print the class definition
Douglas Gregorf757ae72009-05-31 07:13:39 +0000553 // FIXME: Doesn't print access specifiers, e.g., "public:"
Douglas Gregor59e63572009-05-30 06:58:37 +0000554 Out << " {\n";
555 VisitDeclContext(D);
556 Indent() << "}";
557 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000558}
559
560void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
561 const char *l;
562 if (D->getLanguage() == LinkageSpecDecl::lang_c)
563 l = "C";
564 else {
565 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
566 "unknown language in linkage specification");
567 l = "C++";
568 }
569
570 Out << "extern \"" << l << "\" ";
571 if (D->hasBraces()) {
572 Out << "{\n";
573 VisitDeclContext(D);
574 Indent() << "}";
575 } else
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000576 Visit(*D->decls_begin());
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000577}
578
579void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
Anders Carlsson0487f662009-06-04 05:37:43 +0000580 Out << "template <";
581
582 TemplateParameterList *Params = D->getTemplateParameters();
583 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
584 if (i != 0)
585 Out << ", ";
586
587 const Decl *Param = Params->getParam(i);
588 if (const TemplateTypeParmDecl *TTP =
589 dyn_cast<TemplateTypeParmDecl>(Param)) {
590
591 QualType ParamType =
592 Context.getTypeDeclType(const_cast<TemplateTypeParmDecl*>(TTP));
593
594 if (TTP->wasDeclaredWithTypename())
595 Out << "typename ";
596 else
597 Out << "class ";
598
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000599 if (TTP->isParameterPack())
600 Out << "... ";
601
Anders Carlsson0487f662009-06-04 05:37:43 +0000602 Out << ParamType.getAsString(Policy);
603
604 if (TTP->hasDefaultArgument()) {
605 Out << " = ";
606 Out << TTP->getDefaultArgument().getAsString(Policy);
607 };
608 } else if (const NonTypeTemplateParmDecl *NTTP =
609 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
610 Out << NTTP->getType().getAsString(Policy);
611
612 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
613 Out << ' ';
614 Out << Name->getName();
615 }
616
617 if (NTTP->hasDefaultArgument()) {
618 Out << " = ";
619 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
620 Indentation);
621 }
622 }
623 }
624
625 Out << "> ";
626
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000627 Visit(D->getTemplatedDecl());
628}
629
630//----------------------------------------------------------------------------
631// Objective-C declarations
632//----------------------------------------------------------------------------
633
634void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
635 Out << "@class ";
636 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
637 I != E; ++I) {
638 if (I != D->begin()) Out << ", ";
639 Out << (*I)->getNameAsString();
640 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000641}
642
643void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
644 if (OMD->isInstanceMethod())
Douglas Gregor64f65002009-05-30 00:56:08 +0000645 Out << "- ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000646 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000647 Out << "+ ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000648 if (!OMD->getResultType().isNull())
Douglas Gregor59e63572009-05-30 06:58:37 +0000649 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000650
651 std::string name = OMD->getSelector().getAsString();
652 std::string::size_type pos, lastPos = 0;
653 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
654 E = OMD->param_end(); PI != E; ++PI) {
655 // FIXME: selector is missing here!
656 pos = name.find_first_of(":", lastPos);
657 Out << " " << name.substr(lastPos, pos - lastPos);
Douglas Gregor59e63572009-05-30 06:58:37 +0000658 Out << ":(" << (*PI)->getType().getAsString(Policy) << ")"
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000659 << (*PI)->getNameAsString();
660 lastPos = pos + 1;
661 }
662
663 if (OMD->param_begin() == OMD->param_end())
664 Out << " " << name;
665
666 if (OMD->isVariadic())
667 Out << ", ...";
668
669 if (OMD->getBody()) {
670 Out << ' ';
Eli Friedman48d14a22009-05-30 05:03:24 +0000671 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000672 Out << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000673 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000674}
675
676void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
677 std::string I = OID->getNameAsString();
678 ObjCInterfaceDecl *SID = OID->getSuperClass();
679
680 if (SID)
681 Out << "@implementation " << I << " : " << SID->getNameAsString();
682 else
683 Out << "@implementation " << I;
Douglas Gregor64f65002009-05-30 00:56:08 +0000684 Out << "\n";
685 VisitDeclContext(OID, false);
686 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000687}
688
689void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
690 std::string I = OID->getNameAsString();
691 ObjCInterfaceDecl *SID = OID->getSuperClass();
692
693 if (SID)
694 Out << "@interface " << I << " : " << SID->getNameAsString();
695 else
696 Out << "@interface " << I;
697
698 // Protocols?
699 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
700 if (!Protocols.empty()) {
701 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
702 E = Protocols.end(); I != E; ++I)
703 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
704 }
705
706 if (!Protocols.empty())
Douglas Gregor64f65002009-05-30 00:56:08 +0000707 Out << "> ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000708
709 if (OID->ivar_size() > 0) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000710 Out << "{\n";
711 Indentation += Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000712 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
713 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor64f65002009-05-30 00:56:08 +0000714 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000715 << ' ' << (*I)->getNameAsString() << ";\n";
716 }
Douglas Gregor64f65002009-05-30 00:56:08 +0000717 Indentation -= Policy.Indentation;
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000718 Out << "}\n";
719 }
720
721 VisitDeclContext(OID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000722 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000723 // FIXME: implement the rest...
724}
725
726void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
727 Out << "@protocol ";
728 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
729 E = D->protocol_end();
730 I != E; ++I) {
731 if (I != D->protocol_begin()) Out << ", ";
732 Out << (*I)->getNameAsString();
733 }
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000734}
735
736void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
737 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor64f65002009-05-30 00:56:08 +0000738 VisitDeclContext(PID, false);
739 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000740}
741
742void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
743 Out << "@implementation "
744 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000745 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000746
747 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000748 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000749 // FIXME: implement the rest...
750}
751
752void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
753 Out << "@interface "
754 << PID->getClassInterface()->getNameAsString()
Douglas Gregor64f65002009-05-30 00:56:08 +0000755 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000756 VisitDeclContext(PID, false);
Douglas Gregor64f65002009-05-30 00:56:08 +0000757 Out << "@end";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000758
759 // FIXME: implement the rest...
760}
761
762void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
763 Out << "@compatibility_alias " << AID->getNameAsString()
764 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
765}
766
767/// PrintObjCPropertyDecl - print a property declaration.
768///
769void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
770 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
771 Out << "@required\n";
772 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
773 Out << "@optional\n";
774
775 Out << "@property";
776 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
777 bool first = true;
778 Out << " (";
779 if (PDecl->getPropertyAttributes() &
780 ObjCPropertyDecl::OBJC_PR_readonly) {
781 Out << (first ? ' ' : ',') << "readonly";
782 first = false;
783 }
784
785 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
786 Out << (first ? ' ' : ',') << "getter = "
787 << PDecl->getGetterName().getAsString();
788 first = false;
789 }
790 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
791 Out << (first ? ' ' : ',') << "setter = "
792 << PDecl->getSetterName().getAsString();
793 first = false;
794 }
795
796 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
797 Out << (first ? ' ' : ',') << "assign";
798 first = false;
799 }
800
801 if (PDecl->getPropertyAttributes() &
802 ObjCPropertyDecl::OBJC_PR_readwrite) {
803 Out << (first ? ' ' : ',') << "readwrite";
804 first = false;
805 }
806
807 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
808 Out << (first ? ' ' : ',') << "retain";
809 first = false;
810 }
811
812 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
813 Out << (first ? ' ' : ',') << "copy";
814 first = false;
815 }
816
817 if (PDecl->getPropertyAttributes() &
818 ObjCPropertyDecl::OBJC_PR_nonatomic) {
819 Out << (first ? ' ' : ',') << "nonatomic";
820 first = false;
821 }
822 Out << " )";
823 }
824 Out << ' ' << PDecl->getType().getAsString(Policy)
825 << ' ' << PDecl->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000826}
827
828void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
829 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor64f65002009-05-30 00:56:08 +0000830 Out << "@synthesize ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000831 else
Douglas Gregor64f65002009-05-30 00:56:08 +0000832 Out << "@dynamic ";
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000833 Out << PID->getPropertyDecl()->getNameAsString();
834 if (PID->getPropertyIvarDecl())
835 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor4fe0c8e2009-05-30 00:08:05 +0000836}