1 //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Decl::dump method, which pretty print the
11 // AST back out to C/Objective-C/C++/Objective-C++ code.
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/ExprCXX.h"
21 #include "clang/AST/PrettyPrinter.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace clang;
26 class DeclPrinter : public DeclVisitor<DeclPrinter> {
27 llvm::raw_ostream &Out;
29 PrintingPolicy Policy;
32 llvm::raw_ostream& Indent() { return Indent(Indentation); }
33 llvm::raw_ostream& Indent(unsigned Indentation);
34 void ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls);
36 void Print(AccessSpecifier AS);
39 DeclPrinter(llvm::raw_ostream &Out, ASTContext &Context,
40 const PrintingPolicy &Policy,
41 unsigned Indentation = 0)
42 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation) { }
44 void VisitDeclContext(DeclContext *DC, bool Indent = true);
46 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
47 void VisitTypedefDecl(TypedefDecl *D);
48 void VisitTypeAliasDecl(TypeAliasDecl *D);
49 void VisitEnumDecl(EnumDecl *D);
50 void VisitRecordDecl(RecordDecl *D);
51 void VisitEnumConstantDecl(EnumConstantDecl *D);
52 void VisitFunctionDecl(FunctionDecl *D);
53 void VisitFieldDecl(FieldDecl *D);
54 void VisitVarDecl(VarDecl *D);
55 void VisitLabelDecl(LabelDecl *D);
56 void VisitParmVarDecl(ParmVarDecl *D);
57 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
58 void VisitStaticAssertDecl(StaticAssertDecl *D);
59 void VisitNamespaceDecl(NamespaceDecl *D);
60 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
61 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
62 void VisitCXXRecordDecl(CXXRecordDecl *D);
63 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
64 void VisitTemplateDecl(const TemplateDecl *D);
65 void VisitObjCMethodDecl(ObjCMethodDecl *D);
66 void VisitObjCClassDecl(ObjCClassDecl *D);
67 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
68 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
69 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
70 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
71 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
72 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
73 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
74 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
75 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
76 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
77 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
78 void VisitUsingDecl(UsingDecl *D);
79 void VisitUsingShadowDecl(UsingShadowDecl *D);
83 void Decl::print(llvm::raw_ostream &Out, unsigned Indentation) const {
84 print(Out, getASTContext().PrintingPolicy, Indentation);
87 void Decl::print(llvm::raw_ostream &Out, const PrintingPolicy &Policy,
88 unsigned Indentation) const {
89 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation);
90 Printer.Visit(const_cast<Decl*>(this));
93 static QualType GetBaseType(QualType T) {
94 // FIXME: This should be on the Type class!
95 QualType BaseType = T;
96 while (!BaseType->isSpecifierType()) {
97 if (isa<TypedefType>(BaseType))
99 else if (const PointerType* PTy = BaseType->getAs<PointerType>())
100 BaseType = PTy->getPointeeType();
101 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType))
102 BaseType = ATy->getElementType();
103 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>())
104 BaseType = FTy->getResultType();
105 else if (const VectorType *VTy = BaseType->getAs<VectorType>())
106 BaseType = VTy->getElementType();
108 assert(0 && "Unknown declarator!");
113 static QualType getDeclType(Decl* D) {
114 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D))
115 return TDD->getUnderlyingType();
116 if (ValueDecl* VD = dyn_cast<ValueDecl>(D))
117 return VD->getType();
121 void Decl::printGroup(Decl** Begin, unsigned NumDecls,
122 llvm::raw_ostream &Out, const PrintingPolicy &Policy,
123 unsigned Indentation) {
125 (*Begin)->print(Out, Policy, Indentation);
129 Decl** End = Begin + NumDecls;
130 TagDecl* TD = dyn_cast<TagDecl>(*Begin);
134 PrintingPolicy SubPolicy(Policy);
135 if (TD && TD->isDefinition()) {
136 TD->print(Out, Policy, Indentation);
138 SubPolicy.SuppressTag = true;
142 for ( ; Begin != End; ++Begin) {
144 SubPolicy.SuppressSpecifiers = false;
147 if (!isFirst) Out << ", ";
148 SubPolicy.SuppressSpecifiers = true;
151 (*Begin)->print(Out, SubPolicy, Indentation);
155 void DeclContext::dumpDeclContext() const {
156 // Get the translation unit
157 const DeclContext *DC = this;
158 while (!DC->isTranslationUnit())
159 DC = DC->getParent();
161 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext();
162 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.PrintingPolicy, 0);
163 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false);
166 void Decl::dump() const {
170 llvm::raw_ostream& DeclPrinter::Indent(unsigned Indentation) {
171 for (unsigned i = 0; i != Indentation; ++i)
176 void DeclPrinter::ProcessDeclGroup(llvm::SmallVectorImpl<Decl*>& Decls) {
178 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation);
184 void DeclPrinter::Print(AccessSpecifier AS) {
186 case AS_none: assert(0 && "No access specifier!"); break;
187 case AS_public: Out << "public"; break;
188 case AS_protected: Out << "protected"; break;
189 case AS_private: Out << "private"; break;
193 //----------------------------------------------------------------------------
194 // Common C declarations
195 //----------------------------------------------------------------------------
197 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) {
199 Indentation += Policy.Indentation;
201 llvm::SmallVector<Decl*, 2> Decls;
202 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
205 // Don't print ObjCIvarDecls, as they are printed when visiting the
206 // containing ObjCInterfaceDecl.
207 if (isa<ObjCIvarDecl>(*D))
211 // Skip over implicit declarations in pretty-printing mode.
212 if (D->isImplicit()) continue;
213 // FIXME: Ugly hack so we don't pretty-print the builtin declaration
214 // of __builtin_va_list or __[u]int128_t. There should be some other way
216 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) {
217 if (IdentifierInfo *II = ND->getIdentifier()) {
218 if (II->isStr("__builtin_va_list") ||
219 II->isStr("__int128_t") || II->isStr("__uint128_t"))
225 // The next bits of code handles stuff like "struct {int x;} a,b"; we're
226 // forced to merge the declarations because there's no other way to
227 // refer to the struct in question. This limited merging is safe without
228 // a bunch of other checks because it only merges declarations directly
229 // referring to the tag, not typedefs.
231 // Check whether the current declaration should be grouped with a previous
233 QualType CurDeclType = getDeclType(*D);
234 if (!Decls.empty() && !CurDeclType.isNull()) {
235 QualType BaseType = GetBaseType(CurDeclType);
236 if (!BaseType.isNull() && isa<TagType>(BaseType) &&
237 cast<TagType>(BaseType)->getDecl() == Decls[0]) {
243 // If we have a merged group waiting to be handled, handle it now.
245 ProcessDeclGroup(Decls);
247 // If the current declaration is an unnamed tag type, save it
248 // so we can merge it with the subsequent declaration(s) using it.
249 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
254 if (isa<AccessSpecDecl>(*D)) {
255 Indentation -= Policy.Indentation;
257 Print(D->getAccess());
259 Indentation += Policy.Indentation;
266 // FIXME: Need to be able to tell the DeclPrinter when
267 const char *Terminator = 0;
268 if (isa<FunctionDecl>(*D) &&
269 cast<FunctionDecl>(*D)->isThisDeclarationADefinition())
271 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody())
273 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) ||
274 isa<ObjCImplementationDecl>(*D) ||
275 isa<ObjCInterfaceDecl>(*D) ||
276 isa<ObjCProtocolDecl>(*D) ||
277 isa<ObjCCategoryImplDecl>(*D) ||
278 isa<ObjCCategoryDecl>(*D))
280 else if (isa<EnumConstantDecl>(*D)) {
281 DeclContext::decl_iterator Next = D;
294 ProcessDeclGroup(Decls);
297 Indentation -= Policy.Indentation;
300 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
301 VisitDeclContext(D, false);
304 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) {
305 std::string S = D->getNameAsString();
306 D->getUnderlyingType().getAsStringInternal(S, Policy);
307 if (!Policy.SuppressSpecifiers)
312 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) {
313 Out << "using " << D->getNameAsString() << " = "
314 << D->getUnderlyingType().getAsString(Policy);
317 void DeclPrinter::VisitEnumDecl(EnumDecl *D) {
320 if (D->isScopedUsingClassTag())
328 std::string Underlying;
329 D->getIntegerType().getAsStringInternal(Underlying, Policy);
330 Out << " : " << Underlying;
333 if (D->isDefinition()) {
340 void DeclPrinter::VisitRecordDecl(RecordDecl *D) {
341 Out << D->getKindName();
342 if (D->getIdentifier())
345 if (D->isDefinition()) {
352 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) {
354 if (Expr *Init = D->getInitExpr()) {
356 Init->printPretty(Out, Context, 0, Policy, Indentation);
360 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
361 if (!Policy.SuppressSpecifiers) {
362 switch (D->getStorageClass()) {
364 case SC_Extern: Out << "extern "; break;
365 case SC_Static: Out << "static "; break;
366 case SC_PrivateExtern: Out << "__private_extern__ "; break;
367 case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions");
370 if (D->isInlineSpecified()) Out << "inline ";
371 if (D->isVirtualAsWritten()) Out << "virtual ";
374 PrintingPolicy SubPolicy(Policy);
375 SubPolicy.SuppressSpecifiers = false;
376 std::string Proto = D->getNameInfo().getAsString();
378 QualType Ty = D->getType();
379 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) {
380 Proto = '(' + Proto + ')';
381 Ty = PT->getInnerType();
384 if (isa<FunctionType>(Ty)) {
385 const FunctionType *AFT = Ty->getAs<FunctionType>();
386 const FunctionProtoType *FT = 0;
387 if (D->hasWrittenPrototype())
388 FT = dyn_cast<FunctionProtoType>(AFT);
392 llvm::raw_string_ostream POut(Proto);
393 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation);
394 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
396 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
399 if (FT->isVariadic()) {
400 if (D->getNumParams()) POut << ", ";
403 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
404 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
407 Proto += D->getParamDecl(i)->getNameAsString();
413 if (FT && FT->getTypeQuals()) {
414 unsigned TypeQuals = FT->getTypeQuals();
415 if (TypeQuals & Qualifiers::Const)
417 if (TypeQuals & Qualifiers::Volatile)
418 Proto += " volatile";
419 if (TypeQuals & Qualifiers::Restrict)
420 Proto += " restrict";
423 if (FT && FT->hasDynamicExceptionSpec()) {
425 if (FT->getExceptionSpecType() == EST_MSAny)
428 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) {
432 std::string ExceptionType;
433 FT->getExceptionType(I).getAsStringInternal(ExceptionType, SubPolicy);
434 Proto += ExceptionType;
437 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) {
438 Proto += " noexcept";
439 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) {
441 llvm::raw_string_ostream EOut(Proto);
442 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy,
450 if (D->hasAttr<NoReturnAttr>())
451 Proto += " __attribute((noreturn))";
452 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) {
453 bool HasInitializerList = false;
454 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(),
455 E = CDecl->init_end();
457 CXXCtorInitializer * BMInitializer = (*B);
458 if (BMInitializer->isInClassMemberInitializer())
461 if (!HasInitializerList) {
465 HasInitializerList = true;
469 if (BMInitializer->isAnyMemberInitializer()) {
470 FieldDecl *FD = BMInitializer->getAnyMember();
473 Out << QualType(BMInitializer->getBaseClass(),
474 0).getAsString(Policy);
478 if (!BMInitializer->getInit()) {
481 Expr *Init = BMInitializer->getInit();
482 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init))
483 Init = Tmp->getSubExpr();
485 Init = Init->IgnoreParens();
487 Expr *SimpleInit = 0;
489 unsigned NumArgs = 0;
490 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) {
491 Args = ParenList->getExprs();
492 NumArgs = ParenList->getNumExprs();
493 } else if (CXXConstructExpr *Construct
494 = dyn_cast<CXXConstructExpr>(Init)) {
495 Args = Construct->getArgs();
496 NumArgs = Construct->getNumArgs();
501 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation);
503 for (unsigned I = 0; I != NumArgs; ++I) {
504 if (isa<CXXDefaultArgExpr>(Args[I]))
509 Args[I]->printPretty(Out, Context, 0, Policy, Indentation);
517 AFT->getResultType().getAsStringInternal(Proto, Policy);
519 Ty.getAsStringInternal(Proto, Policy);
526 else if (D->isDeletedAsWritten())
528 else if (D->doesThisDeclarationHaveABody()) {
529 if (!D->hasPrototype() && D->getNumParams()) {
530 // This is a K&R function definition, so we need to print the
533 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation);
534 Indentation += Policy.Indentation;
535 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
537 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i));
540 Indentation -= Policy.Indentation;
544 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation);
549 void DeclPrinter::VisitFieldDecl(FieldDecl *D) {
550 if (!Policy.SuppressSpecifiers && D->isMutable())
553 std::string Name = D->getNameAsString();
554 D->getType().getAsStringInternal(Name, Policy);
557 if (D->isBitField()) {
559 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation);
562 Expr *Init = D->getInClassInitializer();
563 if (!Policy.SuppressInitializers && Init) {
565 Init->printPretty(Out, Context, 0, Policy, Indentation);
569 void DeclPrinter::VisitLabelDecl(LabelDecl *D) {
570 Out << D->getNameAsString() << ":";
574 void DeclPrinter::VisitVarDecl(VarDecl *D) {
575 if (!Policy.SuppressSpecifiers && D->getStorageClass() != SC_None)
576 Out << VarDecl::getStorageClassSpecifierString(D->getStorageClass()) << " ";
578 if (!Policy.SuppressSpecifiers && D->isThreadSpecified())
581 std::string Name = D->getNameAsString();
582 QualType T = D->getType();
583 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D))
584 T = Parm->getOriginalType();
585 T.getAsStringInternal(Name, Policy);
587 Expr *Init = D->getInit();
588 if (!Policy.SuppressInitializers && Init) {
589 if (D->hasCXXDirectInitializer())
592 CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init);
593 if (!CCE || CCE->getConstructor()->isCopyConstructor())
596 Init->printPretty(Out, Context, 0, Policy, Indentation);
597 if (D->hasCXXDirectInitializer())
602 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
606 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
608 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation);
612 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) {
613 Out << "static_assert(";
614 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation);
616 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation);
620 //----------------------------------------------------------------------------
622 //----------------------------------------------------------------------------
623 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) {
624 Out << "namespace " << D << " {\n";
629 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
630 Out << "using namespace ";
631 if (D->getQualifier())
632 D->getQualifier()->print(Out, Policy);
633 Out << D->getNominatedNamespaceAsWritten();
636 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
637 Out << "namespace " << D << " = ";
638 if (D->getQualifier())
639 D->getQualifier()->print(Out, Policy);
640 Out << D->getAliasedNamespace();
643 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) {
644 Out << D->getKindName();
645 if (D->getIdentifier())
648 if (D->isDefinition()) {
649 // Print the base classes
650 if (D->getNumBases()) {
652 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(),
653 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) {
654 if (Base != D->bases_begin())
657 if (Base->isVirtual())
660 AccessSpecifier AS = Base->getAccessSpecifierAsWritten();
663 Out << " " << Base->getType().getAsString(Policy);
665 if (Base->isPackExpansion())
670 // Print the class definition
671 // FIXME: Doesn't print access specifiers, e.g., "public:"
678 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
680 if (D->getLanguage() == LinkageSpecDecl::lang_c)
683 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx &&
684 "unknown language in linkage specification");
688 Out << "extern \"" << l << "\" ";
689 if (D->hasBraces()) {
694 Visit(*D->decls_begin());
697 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) {
700 TemplateParameterList *Params = D->getTemplateParameters();
701 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
705 const Decl *Param = Params->getParam(i);
706 if (const TemplateTypeParmDecl *TTP =
707 dyn_cast<TemplateTypeParmDecl>(Param)) {
709 if (TTP->wasDeclaredWithTypename())
714 if (TTP->isParameterPack())
717 Out << TTP->getNameAsString();
719 if (TTP->hasDefaultArgument()) {
721 Out << TTP->getDefaultArgument().getAsString(Policy);
723 } else if (const NonTypeTemplateParmDecl *NTTP =
724 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
725 Out << NTTP->getType().getAsString(Policy);
727 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType()))
730 if (IdentifierInfo *Name = NTTP->getIdentifier()) {
732 Out << Name->getName();
735 if (NTTP->hasDefaultArgument()) {
737 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy,
740 } else if (const TemplateTemplateParmDecl *TTPD =
741 dyn_cast<TemplateTemplateParmDecl>(Param)) {
742 VisitTemplateDecl(TTPD);
743 // FIXME: print the default argument, if present.
749 if (const TemplateTemplateParmDecl *TTP =
750 dyn_cast<TemplateTemplateParmDecl>(D)) {
752 if (TTP->isParameterPack())
756 Visit(D->getTemplatedDecl());
760 //----------------------------------------------------------------------------
761 // Objective-C declarations
762 //----------------------------------------------------------------------------
764 void DeclPrinter::VisitObjCClassDecl(ObjCClassDecl *D) {
766 for (ObjCClassDecl::iterator I = D->begin(), E = D->end();
768 if (I != D->begin()) Out << ", ";
769 Out << I->getInterface();
773 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
774 if (OMD->isInstanceMethod())
778 if (!OMD->getResultType().isNull())
779 Out << '(' << OMD->getResultType().getAsString(Policy) << ")";
781 std::string name = OMD->getSelector().getAsString();
782 std::string::size_type pos, lastPos = 0;
783 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
784 E = OMD->param_end(); PI != E; ++PI) {
785 // FIXME: selector is missing here!
786 pos = name.find_first_of(":", lastPos);
787 Out << " " << name.substr(lastPos, pos - lastPos);
788 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << *PI;
792 if (OMD->param_begin() == OMD->param_end())
795 if (OMD->isVariadic())
798 if (OMD->getBody()) {
800 OMD->getBody()->printPretty(Out, Context, 0, Policy);
805 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) {
806 std::string I = OID->getNameAsString();
807 ObjCInterfaceDecl *SID = OID->getSuperClass();
810 Out << "@implementation " << I << " : " << SID;
812 Out << "@implementation " << I;
814 VisitDeclContext(OID, false);
818 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) {
819 std::string I = OID->getNameAsString();
820 ObjCInterfaceDecl *SID = OID->getSuperClass();
823 Out << "@interface " << I << " : " << SID;
825 Out << "@interface " << I;
828 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols();
829 if (!Protocols.empty()) {
830 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
831 E = Protocols.end(); I != E; ++I)
832 Out << (I == Protocols.begin() ? '<' : ',') << *I;
835 if (!Protocols.empty())
838 if (OID->ivar_size() > 0) {
840 Indentation += Policy.Indentation;
841 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(),
842 E = OID->ivar_end(); I != E; ++I) {
843 Indent() << (*I)->getType().getAsString(Policy) << ' ' << *I << ";\n";
845 Indentation -= Policy.Indentation;
849 VisitDeclContext(OID, false);
851 // FIXME: implement the rest...
854 void DeclPrinter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
856 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
857 E = D->protocol_end();
859 if (I != D->protocol_begin()) Out << ", ";
864 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
865 Out << "@protocol " << PID << '\n';
866 VisitDeclContext(PID, false);
870 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) {
871 Out << "@implementation " << PID->getClassInterface() << '(' << PID << ")\n";
873 VisitDeclContext(PID, false);
875 // FIXME: implement the rest...
878 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) {
879 Out << "@interface " << PID->getClassInterface() << '(' << PID << ")\n";
880 VisitDeclContext(PID, false);
883 // FIXME: implement the rest...
886 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) {
887 Out << "@compatibility_alias " << AID
888 << ' ' << AID->getClassInterface() << ";\n";
891 /// PrintObjCPropertyDecl - print a property declaration.
893 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
894 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required)
895 Out << "@required\n";
896 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional)
897 Out << "@optional\n";
900 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) {
903 if (PDecl->getPropertyAttributes() &
904 ObjCPropertyDecl::OBJC_PR_readonly) {
905 Out << (first ? ' ' : ',') << "readonly";
909 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
910 Out << (first ? ' ' : ',') << "getter = "
911 << PDecl->getGetterName().getAsString();
914 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
915 Out << (first ? ' ' : ',') << "setter = "
916 << PDecl->getSetterName().getAsString();
920 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) {
921 Out << (first ? ' ' : ',') << "assign";
925 if (PDecl->getPropertyAttributes() &
926 ObjCPropertyDecl::OBJC_PR_readwrite) {
927 Out << (first ? ' ' : ',') << "readwrite";
931 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) {
932 Out << (first ? ' ' : ',') << "retain";
936 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) {
937 Out << (first ? ' ' : ',') << "strong";
941 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) {
942 Out << (first ? ' ' : ',') << "copy";
946 if (PDecl->getPropertyAttributes() &
947 ObjCPropertyDecl::OBJC_PR_nonatomic) {
948 Out << (first ? ' ' : ',') << "nonatomic";
951 if (PDecl->getPropertyAttributes() &
952 ObjCPropertyDecl::OBJC_PR_atomic) {
953 Out << (first ? ' ' : ',') << "atomic";
958 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << PDecl;
961 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) {
962 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize)
963 Out << "@synthesize ";
966 Out << PID->getPropertyDecl();
967 if (PID->getPropertyIvarDecl())
968 Out << '=' << PID->getPropertyIvarDecl();
971 void DeclPrinter::VisitUsingDecl(UsingDecl *D) {
973 D->getQualifier()->print(Out, Policy);
978 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
979 Out << "using typename ";
980 D->getQualifier()->print(Out, Policy);
981 Out << D->getDeclName();
984 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
986 D->getQualifier()->print(Out, Policy);
987 Out << D->getDeclName();
990 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) {