blob: b665e2e0e19d411ae21ae2aaebd0cea6ba85f28a [file] [log] [blame]
Douglas Gregor996677c2009-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();
35
36 public:
37 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
38 const PrintingPolicy &Policy,
39 unsigned Indentation = 0)
40 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
41
42 void VisitDeclContext(DeclContext *DC, bool Indent = true);
43
44 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
45 void VisitTypedefDecl(TypedefDecl *D);
46 void VisitEnumDecl(EnumDecl *D);
47 void VisitRecordDecl(RecordDecl *D);
48 void VisitEnumConstantDecl(EnumConstantDecl *D);
49 void VisitFunctionDecl(FunctionDecl *D);
50 void VisitFieldDecl(FieldDecl *D);
51 void VisitVarDecl(VarDecl *D);
52 void VisitParmVarDecl(ParmVarDecl *D);
53 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
54 void VisitNamespaceDecl(NamespaceDecl *D);
55 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
56 void VisitTemplateDecl(TemplateDecl *D);
57 void VisitObjCClassDecl(ObjCClassDecl *D);
58 void VisitObjCMethodDecl(ObjCMethodDecl *D);
59 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
60 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
61 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
62 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
63 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
64 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
65 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
66 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
67 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
68 };
69}
70
71void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
72 unsigned Indentation) {
73 print(Out, Context, Context.PrintingPolicy, Indentation);
74}
75
76void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
77 const PrintingPolicy &Policy, unsigned Indentation) {
78 DeclPrinter Printer(Out, Context, Policy, Indentation);
79 Printer.Visit(this);
80}
81
82void Decl::dump(ASTContext &Context) {
83 print(llvm::errs(), Context);
84}
85
86llvm::raw_ostream& DeclPrinter::Indent() {
87 for (unsigned i = 0; i < Indentation; ++i)
88 Out << " ";
89 return Out;
90}
91
92//----------------------------------------------------------------------------
93// Common C declarations
94//----------------------------------------------------------------------------
95
96void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
97 if (Indent)
98 Indentation += Policy.Indentation;
99
100 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
101 DEnd = DC->decls_end(Context);
102 D != DEnd; ++D) {
103 this->Indent();
104 Visit(*D);
105
106 // FIXME: Need to be able to tell the DeclPrinter when
107 const char *Terminator = 0;
108 if (isa<FunctionDecl>(*D) &&
109 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
110 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000111 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
112 Terminator = 0;
113 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
114 isa<ObjCImplementationDecl>(*D) ||
115 isa<ObjCInterfaceDecl>(*D) ||
116 isa<ObjCProtocolDecl>(*D) ||
117 isa<ObjCCategoryImplDecl>(*D) ||
118 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000119 Terminator = 0;
120 else if (isa<EnumConstantDecl>(*D)) {
121 DeclContext::decl_iterator Next = D;
122 ++Next;
123 if (Next != DEnd)
124 Terminator = ",";
125 } else
126 Terminator = ";";
127
128 if (Terminator)
129 Out << Terminator;
130 Out << "\n";
131 }
132
133 if (Indent)
134 Indentation -= Policy.Indentation;
135}
136
137void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
138 VisitDeclContext(D, false);
139}
140
141void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
142 std::string S = D->getNameAsString();
143 D->getUnderlyingType().getAsStringInternal(S, Policy);
144 Out << "typedef " << S;
145}
146
147void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
148 Out << "enum " << D->getNameAsString() << " {\n";
149 VisitDeclContext(D);
150 Indent() << "}";
151}
152
153void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
154 // print a free standing tag decl (e.g. "struct x;").
155 Out << D->getKindName();
156 Out << " ";
157 Out << D->getNameAsString();
158
159 if (D->isDefinition()) {
160 Out << " {\n";
161 VisitDeclContext(D);
162 Indent() << "}";
163 }
164}
165
166void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
167 Out << D->getNameAsString();
168 if (Expr *Init = D->getInitExpr()) {
169 Out << " = ";
170 Init->printPretty(Out, 0, Policy, Indentation);
171 }
172}
173
174void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
175 switch (D->getStorageClass()) {
176 case FunctionDecl::None: break;
177 case FunctionDecl::Extern: Out << "extern "; break;
178 case FunctionDecl::Static: Out << "static "; break;
179 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
180 }
181
182 if (D->isInline()) Out << "inline ";
183 if (D->isVirtualAsWritten()) Out << "virtual ";
184
185 std::string Proto = D->getNameAsString();
186 if (isa<FunctionType>(D->getType().getTypePtr())) {
187 const FunctionType *AFT = D->getType()->getAsFunctionType();
188
189 const FunctionProtoType *FT = 0;
190 if (D->hasWrittenPrototype())
191 FT = dyn_cast<FunctionProtoType>(AFT);
192
193 Proto += "(";
194 if (FT) {
195 llvm::raw_string_ostream POut(Proto);
196 DeclPrinter ParamPrinter(POut, Context, Policy, Indentation);
197 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
198 if (i) POut << ", ";
199 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
200 }
201
202 if (FT->isVariadic()) {
203 if (D->getNumParams()) POut << ", ";
204 POut << "...";
205 }
206 }
207
208 Proto += ")";
209 AFT->getResultType().getAsStringInternal(Proto, Policy);
210 } else {
211 D->getType().getAsStringInternal(Proto, Policy);
212 }
213
214 Out << Proto;
215
216 if (D->isPure())
217 Out << " = 0";
218 else if (D->isDeleted())
219 Out << " = delete";
220 else if (D->isThisDeclarationADefinition()) {
221 if (!D->hasPrototype() && D->getNumParams()) {
222 // This is a K&R function definition, so we need to print the
223 // parameters.
224 Out << '\n';
225 Indentation += Policy.Indentation;
226 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
227 Indent();
228 VisitParmVarDecl(D->getParamDecl(i));
229 Out << ";\n";
230 }
231 Indentation -= Policy.Indentation;
232 } else
233 Out << ' ';
234
235 D->getBody(Context)->printPretty(Out, 0, Policy, Indentation);
236 Out << '\n';
237 }
238}
239
240void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
241 if (D->isMutable())
242 Out << "mutable ";
243
244 std::string Name = D->getNameAsString();
245 D->getType().getAsStringInternal(Name, Policy);
246 Out << Name;
247
248 if (D->isBitField()) {
249 Out << " : ";
250 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation);
251 }
252}
253
254void DeclPrinter::VisitVarDecl(VarDecl *D) {
255 if (D->getStorageClass() != VarDecl::None)
256 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
257
258 if (D->isThreadSpecified())
259 Out << "__thread ";
260
261 std::string Name = D->getNameAsString();
262 QualType T = D->getType();
263 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
264 T = Parm->getOriginalType();
265 T.getAsStringInternal(Name, Policy);
266 Out << Name;
267 if (D->getInit()) {
268 if (D->hasCXXDirectInitializer())
269 Out << "(";
270 else
271 Out << " = ";
272 D->getInit()->printPretty(Out, 0, Policy, Indentation);
273 if (D->hasCXXDirectInitializer())
274 Out << ")";
275 }
276}
277
278void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
279 VisitVarDecl(D);
280}
281
282void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
283 Out << "__asm (";
284 D->getAsmString()->printPretty(Out, 0, Policy, Indentation);
285 Out << ")";
286}
287
288//----------------------------------------------------------------------------
289// C++ declarations
290//----------------------------------------------------------------------------
291void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
292 Out << "namespace " << D->getNameAsString() << " {\n";
293 VisitDeclContext(D);
294 Indent() << "}";
295}
296
297void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
298 const char *l;
299 if (D->getLanguage() == LinkageSpecDecl::lang_c)
300 l = "C";
301 else {
302 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
303 "unknown language in linkage specification");
304 l = "C++";
305 }
306
307 Out << "extern \"" << l << "\" ";
308 if (D->hasBraces()) {
309 Out << "{\n";
310 VisitDeclContext(D);
311 Indent() << "}";
312 } else
313 Visit(*D->decls_begin(Context));
314}
315
316void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
317 // TODO: Write template parameters.
318 Out << "template <...> ";
319 Visit(D->getTemplatedDecl());
320}
321
322//----------------------------------------------------------------------------
323// Objective-C declarations
324//----------------------------------------------------------------------------
325
326void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
327 Out << "@class ";
328 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
329 I != E; ++I) {
330 if (I != D->begin()) Out << ", ";
331 Out << (*I)->getNameAsString();
332 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000333}
334
335void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
336 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000337 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000338 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000339 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000340 if (!OMD->getResultType().isNull())
341 Out << '(' << OMD->getResultType().getAsString() << ")";
342
343 std::string name = OMD->getSelector().getAsString();
344 std::string::size_type pos, lastPos = 0;
345 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
346 E = OMD->param_end(); PI != E; ++PI) {
347 // FIXME: selector is missing here!
348 pos = name.find_first_of(":", lastPos);
349 Out << " " << name.substr(lastPos, pos - lastPos);
350 Out << ":(" << (*PI)->getType().getAsString() << ")"
351 << (*PI)->getNameAsString();
352 lastPos = pos + 1;
353 }
354
355 if (OMD->param_begin() == OMD->param_end())
356 Out << " " << name;
357
358 if (OMD->isVariadic())
359 Out << ", ...";
360
361 if (OMD->getBody()) {
362 Out << ' ';
363 OMD->getBody()->printPretty(Out, 0, Policy);
364 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000365 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000366}
367
368void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
369 std::string I = OID->getNameAsString();
370 ObjCInterfaceDecl *SID = OID->getSuperClass();
371
372 if (SID)
373 Out << "@implementation " << I << " : " << SID->getNameAsString();
374 else
375 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000376 Out << "\n";
377 VisitDeclContext(OID, false);
378 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000379}
380
381void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
382 std::string I = OID->getNameAsString();
383 ObjCInterfaceDecl *SID = OID->getSuperClass();
384
385 if (SID)
386 Out << "@interface " << I << " : " << SID->getNameAsString();
387 else
388 Out << "@interface " << I;
389
390 // Protocols?
391 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
392 if (!Protocols.empty()) {
393 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
394 E = Protocols.end(); I != E; ++I)
395 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
396 }
397
398 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000399 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000400
401 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000402 Out << "{\n";
403 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000404 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
405 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000406 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000407 << ' ' << (*I)->getNameAsString() << ";\n";
408 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000409 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000410 Out << "}\n";
411 }
412
413 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000414 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000415 // FIXME: implement the rest...
416}
417
418void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
419 Out << "@protocol ";
420 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
421 E = D->protocol_end();
422 I != E; ++I) {
423 if (I != D->protocol_begin()) Out << ", ";
424 Out << (*I)->getNameAsString();
425 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000426}
427
428void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
429 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000430 VisitDeclContext(PID, false);
431 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000432}
433
434void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
435 Out << "@implementation "
436 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000437 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000438
439 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000440 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000441 // FIXME: implement the rest...
442}
443
444void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
445 Out << "@interface "
446 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000447 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000448 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000449 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000450
451 // FIXME: implement the rest...
452}
453
454void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
455 Out << "@compatibility_alias " << AID->getNameAsString()
456 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
457}
458
459/// PrintObjCPropertyDecl - print a property declaration.
460///
461void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
462 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
463 Out << "@required\n";
464 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
465 Out << "@optional\n";
466
467 Out << "@property";
468 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
469 bool first = true;
470 Out << " (";
471 if (PDecl->getPropertyAttributes() &
472 ObjCPropertyDecl::OBJC_PR_readonly) {
473 Out << (first ? ' ' : ',') << "readonly";
474 first = false;
475 }
476
477 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
478 Out << (first ? ' ' : ',') << "getter = "
479 << PDecl->getGetterName().getAsString();
480 first = false;
481 }
482 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
483 Out << (first ? ' ' : ',') << "setter = "
484 << PDecl->getSetterName().getAsString();
485 first = false;
486 }
487
488 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
489 Out << (first ? ' ' : ',') << "assign";
490 first = false;
491 }
492
493 if (PDecl->getPropertyAttributes() &
494 ObjCPropertyDecl::OBJC_PR_readwrite) {
495 Out << (first ? ' ' : ',') << "readwrite";
496 first = false;
497 }
498
499 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
500 Out << (first ? ' ' : ',') << "retain";
501 first = false;
502 }
503
504 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
505 Out << (first ? ' ' : ',') << "copy";
506 first = false;
507 }
508
509 if (PDecl->getPropertyAttributes() &
510 ObjCPropertyDecl::OBJC_PR_nonatomic) {
511 Out << (first ? ' ' : ',') << "nonatomic";
512 first = false;
513 }
514 Out << " )";
515 }
516 Out << ' ' << PDecl->getType().getAsString(Policy)
517 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000518}
519
520void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
521 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000522 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000523 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000524 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000525 Out << PID->getPropertyDecl()->getNameAsString();
526 if (PID->getPropertyIvarDecl())
527 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000528}