blob: 2858c0a201e19ef4e2927f3ef444d4227ed09393 [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();
Eli Friedmand73364a2009-05-30 04:20:30 +000035 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
Douglas Gregor996677c2009-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 Gregor1d27d692009-05-30 06:31:56 +000054 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000055 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000056 void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D);
57 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
Douglas Gregor8d8ddca2009-05-30 06:48:27 +000058 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000059 void VisitNamespaceDecl(NamespaceDecl *D);
60 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
61 void VisitTemplateDecl(TemplateDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000062 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor1d27d692009-05-30 06:31:56 +000063 void VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor996677c2009-05-30 00:08:05 +000064 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
65 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
66 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
67 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
68 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
70 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
73 };
74}
75
76void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
77 unsigned Indentation) {
78 print(Out, Context, Context.PrintingPolicy, Indentation);
79}
80
81void Decl::print(llvm::raw_ostream &Out, ASTContext &Context,
82 const PrintingPolicy &Policy, unsigned Indentation) {
83 DeclPrinter Printer(Out, Context, Policy, Indentation);
84 Printer.Visit(this);
85}
86
Eli Friedmand73364a2009-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;
93 else if (const PointerType* PTy = BaseType->getAsPointerType())
94 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();
99 else
100 assert(0 && "Unknown declarator!");
101 }
102 return BaseType;
103}
104
105static QualType getDeclType(Decl* D) {
106 if (TypedefDecl* TDD = dyn_cast<TypedefDecl>(D))
107 return TDD->getUnderlyingType();
108 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
109 return VD->getType();
110 return QualType();
111}
112
113void Decl::printGroup(Decl** Begin, unsigned NumDecls,
114 llvm::raw_ostream &Out, ASTContext &Context,
115 const PrintingPolicy &Policy,
116 unsigned Indentation) {
117 if (NumDecls == 1) {
118 (*Begin)->print(Out, Context, Policy, Indentation);
119 return;
120 }
121
122 Decl** End = Begin + NumDecls;
123 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
124 if (TD)
125 ++Begin;
126
127 PrintingPolicy SubPolicy(Policy);
128 if (TD && TD->isDefinition()) {
129 TD->print(Out, Context, Policy, Indentation);
130 Out << " ";
131 SubPolicy.SuppressTag = true;
132 }
133
134 bool isFirst = true;
135 for ( ; Begin != End; ++Begin) {
136 if (isFirst) {
137 SubPolicy.SuppressSpecifiers = false;
138 isFirst = false;
139 } else {
140 if (!isFirst) Out << ", ";
141 SubPolicy.SuppressSpecifiers = true;
142 }
143
144 (*Begin)->print(Out, Context, SubPolicy, Indentation);
145 }
146}
147
Douglas Gregor996677c2009-05-30 00:08:05 +0000148void Decl::dump(ASTContext &Context) {
149 print(llvm::errs(), Context);
150}
151
152llvm::raw_ostream& DeclPrinter::Indent() {
153 for (unsigned i = 0; i < Indentation; ++i)
154 Out << " ";
155 return Out;
156}
157
Eli Friedmand73364a2009-05-30 04:20:30 +0000158void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
159 this->Indent();
160 Decl::printGroup(Decls.data(), Decls.size(), Out, Context,
161 Policy, Indentation);
162 Out << ";\n";
163 Decls.clear();
164
165}
166
Douglas Gregor996677c2009-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 Friedmand73364a2009-05-30 04:20:30 +0000175 llvm::SmallVector<Decl*, 2> Decls;
Douglas Gregor996677c2009-05-30 00:08:05 +0000176 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
177 DEnd = DC->decls_end(Context);
178 D != DEnd; ++D) {
Eli Friedman4bdae722009-05-30 05:03:24 +0000179 if (!Policy.Dump) {
180 // Skip over implicit declarations in pretty-printing mode.
181 if (D->isImplicit()) continue;
Eli Friedman68ff4682009-05-30 06:35:22 +0000182 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
183 // of __builtin_va_list. There should be some other way to check that.
184 if (isa<NamedDecl>(*D) && cast<NamedDecl>(*D)->getNameAsString() ==
185 "__builtin_va_list")
186 continue;
Eli Friedman4bdae722009-05-30 05:03:24 +0000187 }
188
Eli Friedmand73364a2009-05-30 04:20:30 +0000189 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
190 // forced to merge the declarations because there's no other way to
191 // refer to the struct in question. This limited merging is safe without
192 // a bunch of other checks because it only merges declarations directly
193 // referring to the tag, not typedefs.
194 //
195 // Check whether the current declaration should be grouped with a previous
196 // unnamed struct.
197 QualType CurDeclType = getDeclType(*D);
198 if (!Decls.empty() && !CurDeclType.isNull()) {
199 QualType BaseType = GetBaseType(CurDeclType);
200 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
201 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
202 Decls.push_back(*D);
203 continue;
204 }
205 }
206
207 // If we have a merged group waiting to be handled, handle it now.
208 if (!Decls.empty())
209 ProcessDeclGroup(Decls);
210
211 // If the current declaration is an unnamed tag type, save it
212 // so we can merge it with the subsequent declaration(s) using it.
213 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
214 Decls.push_back(*D);
215 continue;
216 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000217 this->Indent();
218 Visit(*D);
219
220 // FIXME: Need to be able to tell the DeclPrinter when
221 const char *Terminator = 0;
222 if (isa<FunctionDecl>(*D) &&
223 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
224 Terminator = 0;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000225 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
226 Terminator = 0;
227 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
228 isa<ObjCImplementationDecl>(*D) ||
229 isa<ObjCInterfaceDecl>(*D) ||
230 isa<ObjCProtocolDecl>(*D) ||
231 isa<ObjCCategoryImplDecl>(*D) ||
232 isa<ObjCCategoryDecl>(*D))
Douglas Gregor996677c2009-05-30 00:08:05 +0000233 Terminator = 0;
234 else if (isa<EnumConstantDecl>(*D)) {
235 DeclContext::decl_iterator Next = D;
236 ++Next;
237 if (Next != DEnd)
238 Terminator = ",";
239 } else
240 Terminator = ";";
241
242 if (Terminator)
243 Out << Terminator;
244 Out << "\n";
245 }
246
Eli Friedmand73364a2009-05-30 04:20:30 +0000247 if (!Decls.empty())
248 ProcessDeclGroup(Decls);
249
Douglas Gregor996677c2009-05-30 00:08:05 +0000250 if (Indent)
251 Indentation -= Policy.Indentation;
252}
253
254void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
255 VisitDeclContext(D, false);
256}
257
258void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
259 std::string S = D->getNameAsString();
260 D->getUnderlyingType().getAsStringInternal(S, Policy);
Eli Friedmand73364a2009-05-30 04:20:30 +0000261 if (!Policy.SuppressSpecifiers)
262 Out << "typedef ";
263 Out << S;
Douglas Gregor996677c2009-05-30 00:08:05 +0000264}
265
266void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
267 Out << "enum " << D->getNameAsString() << " {\n";
268 VisitDeclContext(D);
269 Indent() << "}";
270}
271
272void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
273 // print a free standing tag decl (e.g. "struct x;").
274 Out << D->getKindName();
Eli Friedmand73364a2009-05-30 04:20:30 +0000275 if (D->getIdentifier()) {
276 Out << " ";
277 Out << D->getNameAsString();
278 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000279
280 if (D->isDefinition()) {
281 Out << " {\n";
282 VisitDeclContext(D);
283 Indent() << "}";
284 }
285}
286
287void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
288 Out << D->getNameAsString();
289 if (Expr *Init = D->getInitExpr()) {
290 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000291 Init->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000292 }
293}
294
295void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000296 if (!Policy.SuppressSpecifiers) {
297 switch (D->getStorageClass()) {
298 case FunctionDecl::None: break;
299 case FunctionDecl::Extern: Out << "extern "; break;
300 case FunctionDecl::Static: Out << "static "; break;
301 case FunctionDecl::PrivateExtern: Out << "__private_extern__ "; break;
302 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000303
Eli Friedmand73364a2009-05-30 04:20:30 +0000304 if (D->isInline()) Out << "inline ";
305 if (D->isVirtualAsWritten()) Out << "virtual ";
306 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000307
Douglas Gregor25133312009-05-30 05:39:39 +0000308 PrintingPolicy SubPolicy(Policy);
309 SubPolicy.SuppressSpecifiers = false;
Douglas Gregor996677c2009-05-30 00:08:05 +0000310 std::string Proto = D->getNameAsString();
311 if (isa<FunctionType>(D->getType().getTypePtr())) {
312 const FunctionType *AFT = D->getType()->getAsFunctionType();
313
314 const FunctionProtoType *FT = 0;
315 if (D->hasWrittenPrototype())
316 FT = dyn_cast<FunctionProtoType>(AFT);
317
318 Proto += "(";
319 if (FT) {
320 llvm::raw_string_ostream POut(Proto);
Douglas Gregor25133312009-05-30 05:39:39 +0000321 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000322 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
323 if (i) POut << ", ";
324 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
325 }
326
327 if (FT->isVariadic()) {
328 if (D->getNumParams()) POut << ", ";
329 POut << "...";
330 }
Eli Friedmand73364a2009-05-30 04:20:30 +0000331 } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
332 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
333 if (i)
334 Proto += ", ";
335 Proto += D->getParamDecl(i)->getNameAsString();
336 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000337 }
338
339 Proto += ")";
340 AFT->getResultType().getAsStringInternal(Proto, Policy);
341 } else {
342 D->getType().getAsStringInternal(Proto, Policy);
343 }
344
345 Out << Proto;
346
347 if (D->isPure())
348 Out << " = 0";
349 else if (D->isDeleted())
350 Out << " = delete";
351 else if (D->isThisDeclarationADefinition()) {
352 if (!D->hasPrototype() && D->getNumParams()) {
353 // This is a K&R function definition, so we need to print the
354 // parameters.
355 Out << '\n';
Douglas Gregor25133312009-05-30 05:39:39 +0000356 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000357 Indentation += Policy.Indentation;
358 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
359 Indent();
Douglas Gregor25133312009-05-30 05:39:39 +0000360 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
Douglas Gregor996677c2009-05-30 00:08:05 +0000361 Out << ";\n";
362 }
363 Indentation -= Policy.Indentation;
364 } else
365 Out << ' ';
366
Douglas Gregor25133312009-05-30 05:39:39 +0000367 D->getBody(Context)->printPretty(Out, Context, 0, SubPolicy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000368 Out << '\n';
369 }
370}
371
372void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000373 if (!Policy.SuppressSpecifiers && D->isMutable())
Douglas Gregor996677c2009-05-30 00:08:05 +0000374 Out << "mutable ";
375
376 std::string Name = D->getNameAsString();
377 D->getType().getAsStringInternal(Name, Policy);
378 Out << Name;
379
380 if (D->isBitField()) {
381 Out << " : ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000382 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000383 }
384}
385
386void DeclPrinter::VisitVarDecl(VarDecl *D) {
Eli Friedmand73364a2009-05-30 04:20:30 +0000387 if (!Policy.SuppressSpecifiers && D->getStorageClass() != VarDecl::None)
Douglas Gregor996677c2009-05-30 00:08:05 +0000388 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
389
Eli Friedmand73364a2009-05-30 04:20:30 +0000390 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
Douglas Gregor996677c2009-05-30 00:08:05 +0000391 Out << "__thread ";
392
393 std::string Name = D->getNameAsString();
394 QualType T = D->getType();
395 if (OriginalParmVarDecl *Parm = dyn_cast<OriginalParmVarDecl>(D))
396 T = Parm->getOriginalType();
397 T.getAsStringInternal(Name, Policy);
398 Out << Name;
399 if (D->getInit()) {
400 if (D->hasCXXDirectInitializer())
401 Out << "(";
402 else
403 Out << " = ";
Eli Friedman4bdae722009-05-30 05:03:24 +0000404 D->getInit()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000405 if (D->hasCXXDirectInitializer())
406 Out << ")";
407 }
408}
409
410void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
411 VisitVarDecl(D);
412}
413
Douglas Gregor1d27d692009-05-30 06:31:56 +0000414void DeclPrinter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
415 VisitVarDecl(D);
416}
417
Douglas Gregor996677c2009-05-30 00:08:05 +0000418void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
419 Out << "__asm (";
Eli Friedman4bdae722009-05-30 05:03:24 +0000420 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
Douglas Gregor996677c2009-05-30 00:08:05 +0000421 Out << ")";
422}
423
424//----------------------------------------------------------------------------
425// C++ declarations
426//----------------------------------------------------------------------------
Douglas Gregor1d27d692009-05-30 06:31:56 +0000427void DeclPrinter::VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D) {
428 assert(false &&
429 "OverloadedFunctionDecls aren't really decls and are never printed");
430}
431
432void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
433 Out << "using namespace ";
434 if (D->getQualifier())
435 D->getQualifier()->print(Out, Policy);
436 Out << D->getNominatedNamespace()->getNameAsString();
437}
438
Douglas Gregor8d8ddca2009-05-30 06:48:27 +0000439void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
440 Out << "namespace " << D->getNameAsString() << " = ";
441 if (D->getQualifier())
442 D->getQualifier()->print(Out, Policy);
443 Out << D->getAliasedNamespace()->getNameAsString();
444}
445
Douglas Gregor996677c2009-05-30 00:08:05 +0000446void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
447 Out << "namespace " << D->getNameAsString() << " {\n";
448 VisitDeclContext(D);
449 Indent() << "}";
450}
451
452void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
453 const char *l;
454 if (D->getLanguage() == LinkageSpecDecl::lang_c)
455 l = "C";
456 else {
457 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
458 "unknown language in linkage specification");
459 l = "C++";
460 }
461
462 Out << "extern \"" << l << "\" ";
463 if (D->hasBraces()) {
464 Out << "{\n";
465 VisitDeclContext(D);
466 Indent() << "}";
467 } else
468 Visit(*D->decls_begin(Context));
469}
470
471void DeclPrinter::VisitTemplateDecl(TemplateDecl *D) {
472 // TODO: Write template parameters.
473 Out << "template <...> ";
474 Visit(D->getTemplatedDecl());
475}
476
477//----------------------------------------------------------------------------
478// Objective-C declarations
479//----------------------------------------------------------------------------
480
481void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
482 Out << "@class ";
483 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
484 I != E; ++I) {
485 if (I != D->begin()) Out << ", ";
486 Out << (*I)->getNameAsString();
487 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000488}
489
490void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
491 if (OMD->isInstanceMethod())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000492 Out << "- ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000493 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000494 Out << "+ ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000495 if (!OMD->getResultType().isNull())
496 Out << '(' << OMD->getResultType().getAsString() << ")";
497
498 std::string name = OMD->getSelector().getAsString();
499 std::string::size_type pos, lastPos = 0;
500 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
501 E = OMD->param_end(); PI != E; ++PI) {
502 // FIXME: selector is missing here!
503 pos = name.find_first_of(":", lastPos);
504 Out << " " << name.substr(lastPos, pos - lastPos);
505 Out << ":(" << (*PI)->getType().getAsString() << ")"
506 << (*PI)->getNameAsString();
507 lastPos = pos + 1;
508 }
509
510 if (OMD->param_begin() == OMD->param_end())
511 Out << " " << name;
512
513 if (OMD->isVariadic())
514 Out << ", ...";
515
516 if (OMD->getBody()) {
517 Out << ' ';
Eli Friedman4bdae722009-05-30 05:03:24 +0000518 OMD->getBody()->printPretty(Out, Context, 0, Policy);
Douglas Gregor996677c2009-05-30 00:08:05 +0000519 Out << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000520 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000521}
522
523void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
524 std::string I = OID->getNameAsString();
525 ObjCInterfaceDecl *SID = OID->getSuperClass();
526
527 if (SID)
528 Out << "@implementation " << I << " : " << SID->getNameAsString();
529 else
530 Out << "@implementation " << I;
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000531 Out << "\n";
532 VisitDeclContext(OID, false);
533 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000534}
535
536void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
537 std::string I = OID->getNameAsString();
538 ObjCInterfaceDecl *SID = OID->getSuperClass();
539
540 if (SID)
541 Out << "@interface " << I << " : " << SID->getNameAsString();
542 else
543 Out << "@interface " << I;
544
545 // Protocols?
546 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
547 if (!Protocols.empty()) {
548 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
549 E = Protocols.end(); I != E; ++I)
550 Out << (I == Protocols.begin() ? '<' : ',') << (*I)->getNameAsString();
551 }
552
553 if (!Protocols.empty())
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000554 Out << "> ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000555
556 if (OID->ivar_size() > 0) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000557 Out << "{\n";
558 Indentation += Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000559 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
560 E = OID->ivar_end(); I != E; ++I) {
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000561 Indent() << (*I)->getType().getAsString(Policy)
Douglas Gregor996677c2009-05-30 00:08:05 +0000562 << ' ' << (*I)->getNameAsString() << ";\n";
563 }
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000564 Indentation -= Policy.Indentation;
Douglas Gregor996677c2009-05-30 00:08:05 +0000565 Out << "}\n";
566 }
567
568 VisitDeclContext(OID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000569 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000570 // FIXME: implement the rest...
571}
572
573void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
574 Out << "@protocol ";
575 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
576 E = D->protocol_end();
577 I != E; ++I) {
578 if (I != D->protocol_begin()) Out << ", ";
579 Out << (*I)->getNameAsString();
580 }
Douglas Gregor996677c2009-05-30 00:08:05 +0000581}
582
583void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
584 Out << "@protocol " << PID->getNameAsString() << '\n';
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000585 VisitDeclContext(PID, false);
586 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000587}
588
589void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
590 Out << "@implementation "
591 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000592 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000593
594 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000595 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000596 // FIXME: implement the rest...
597}
598
599void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
600 Out << "@interface "
601 << PID->getClassInterface()->getNameAsString()
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000602 << '(' << PID->getNameAsString() << ")\n";
Douglas Gregor996677c2009-05-30 00:08:05 +0000603 VisitDeclContext(PID, false);
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000604 Out << "@end";
Douglas Gregor996677c2009-05-30 00:08:05 +0000605
606 // FIXME: implement the rest...
607}
608
609void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
610 Out << "@compatibility_alias " << AID->getNameAsString()
611 << ' ' << AID->getClassInterface()->getNameAsString() << ";\n";
612}
613
614/// PrintObjCPropertyDecl - print a property declaration.
615///
616void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
617 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
618 Out << "@required\n";
619 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
620 Out << "@optional\n";
621
622 Out << "@property";
623 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
624 bool first = true;
625 Out << " (";
626 if (PDecl->getPropertyAttributes() &
627 ObjCPropertyDecl::OBJC_PR_readonly) {
628 Out << (first ? ' ' : ',') << "readonly";
629 first = false;
630 }
631
632 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
633 Out << (first ? ' ' : ',') << "getter = "
634 << PDecl->getGetterName().getAsString();
635 first = false;
636 }
637 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
638 Out << (first ? ' ' : ',') << "setter = "
639 << PDecl->getSetterName().getAsString();
640 first = false;
641 }
642
643 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
644 Out << (first ? ' ' : ',') << "assign";
645 first = false;
646 }
647
648 if (PDecl->getPropertyAttributes() &
649 ObjCPropertyDecl::OBJC_PR_readwrite) {
650 Out << (first ? ' ' : ',') << "readwrite";
651 first = false;
652 }
653
654 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
655 Out << (first ? ' ' : ',') << "retain";
656 first = false;
657 }
658
659 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
660 Out << (first ? ' ' : ',') << "copy";
661 first = false;
662 }
663
664 if (PDecl->getPropertyAttributes() &
665 ObjCPropertyDecl::OBJC_PR_nonatomic) {
666 Out << (first ? ' ' : ',') << "nonatomic";
667 first = false;
668 }
669 Out << " )";
670 }
671 Out << ' ' << PDecl->getType().getAsString(Policy)
672 << ' ' << PDecl->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000673}
674
675void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
676 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000677 Out << "@synthesize ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000678 else
Douglas Gregor1fa5d0d2009-05-30 00:56:08 +0000679 Out << "@dynamic ";
Douglas Gregor996677c2009-05-30 00:08:05 +0000680 Out << PID->getPropertyDecl()->getNameAsString();
681 if (PID->getPropertyIvarDecl())
682 Out << "=" << PID->getPropertyIvarDecl()->getNameAsString();
Douglas Gregor996677c2009-05-30 00:08:05 +0000683}