clang API Documentation
00001 //===--- DeclPrinter.cpp - Printing implementation for Decl ASTs ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Decl::dump method, which pretty print the 00011 // AST back out to C/Objective-C/C++/Objective-C++ code. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 #include "clang/AST/ASTContext.h" 00015 #include "clang/AST/DeclVisitor.h" 00016 #include "clang/AST/Decl.h" 00017 #include "clang/AST/DeclCXX.h" 00018 #include "clang/AST/DeclObjC.h" 00019 #include "clang/AST/Expr.h" 00020 #include "clang/AST/ExprCXX.h" 00021 #include "clang/AST/PrettyPrinter.h" 00022 #include "clang/Basic/Module.h" 00023 #include "llvm/Support/raw_ostream.h" 00024 using namespace clang; 00025 00026 namespace { 00027 class DeclPrinter : public DeclVisitor<DeclPrinter> { 00028 raw_ostream &Out; 00029 ASTContext &Context; 00030 PrintingPolicy Policy; 00031 unsigned Indentation; 00032 bool PrintInstantiation; 00033 00034 raw_ostream& Indent() { return Indent(Indentation); } 00035 raw_ostream& Indent(unsigned Indentation); 00036 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); 00037 00038 void Print(AccessSpecifier AS); 00039 00040 public: 00041 DeclPrinter(raw_ostream &Out, ASTContext &Context, 00042 const PrintingPolicy &Policy, 00043 unsigned Indentation = 0, 00044 bool PrintInstantiation = false) 00045 : Out(Out), Context(Context), Policy(Policy), Indentation(Indentation), 00046 PrintInstantiation(PrintInstantiation) { } 00047 00048 void VisitDeclContext(DeclContext *DC, bool Indent = true); 00049 00050 void VisitTranslationUnitDecl(TranslationUnitDecl *D); 00051 void VisitTypedefDecl(TypedefDecl *D); 00052 void VisitTypeAliasDecl(TypeAliasDecl *D); 00053 void VisitEnumDecl(EnumDecl *D); 00054 void VisitRecordDecl(RecordDecl *D); 00055 void VisitEnumConstantDecl(EnumConstantDecl *D); 00056 void VisitFunctionDecl(FunctionDecl *D); 00057 void VisitFieldDecl(FieldDecl *D); 00058 void VisitVarDecl(VarDecl *D); 00059 void VisitLabelDecl(LabelDecl *D); 00060 void VisitParmVarDecl(ParmVarDecl *D); 00061 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); 00062 void VisitImportDecl(ImportDecl *D); 00063 void VisitStaticAssertDecl(StaticAssertDecl *D); 00064 void VisitNamespaceDecl(NamespaceDecl *D); 00065 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 00066 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 00067 void VisitCXXRecordDecl(CXXRecordDecl *D); 00068 void VisitLinkageSpecDecl(LinkageSpecDecl *D); 00069 void VisitTemplateDecl(const TemplateDecl *D); 00070 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 00071 void VisitClassTemplateDecl(ClassTemplateDecl *D); 00072 void VisitObjCMethodDecl(ObjCMethodDecl *D); 00073 void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 00074 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 00075 void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 00076 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 00077 void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 00078 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 00079 void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 00080 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 00081 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 00082 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 00083 void VisitUsingDecl(UsingDecl *D); 00084 void VisitUsingShadowDecl(UsingShadowDecl *D); 00085 00086 void PrintTemplateParameters(const TemplateParameterList *Params, 00087 const TemplateArgumentList *Args); 00088 void prettyPrintAttributes(Decl *D); 00089 }; 00090 } 00091 00092 void Decl::print(raw_ostream &Out, unsigned Indentation, 00093 bool PrintInstantiation) const { 00094 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 00095 } 00096 00097 void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, 00098 unsigned Indentation, bool PrintInstantiation) const { 00099 DeclPrinter Printer(Out, getASTContext(), Policy, Indentation, PrintInstantiation); 00100 Printer.Visit(const_cast<Decl*>(this)); 00101 } 00102 00103 static QualType GetBaseType(QualType T) { 00104 // FIXME: This should be on the Type class! 00105 QualType BaseType = T; 00106 while (!BaseType->isSpecifierType()) { 00107 if (isa<TypedefType>(BaseType)) 00108 break; 00109 else if (const PointerType* PTy = BaseType->getAs<PointerType>()) 00110 BaseType = PTy->getPointeeType(); 00111 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) 00112 BaseType = ATy->getElementType(); 00113 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) 00114 BaseType = FTy->getResultType(); 00115 else if (const VectorType *VTy = BaseType->getAs<VectorType>()) 00116 BaseType = VTy->getElementType(); 00117 else 00118 llvm_unreachable("Unknown declarator!"); 00119 } 00120 return BaseType; 00121 } 00122 00123 static QualType getDeclType(Decl* D) { 00124 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) 00125 return TDD->getUnderlyingType(); 00126 if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) 00127 return VD->getType(); 00128 return QualType(); 00129 } 00130 00131 void Decl::printGroup(Decl** Begin, unsigned NumDecls, 00132 raw_ostream &Out, const PrintingPolicy &Policy, 00133 unsigned Indentation) { 00134 if (NumDecls == 1) { 00135 (*Begin)->print(Out, Policy, Indentation); 00136 return; 00137 } 00138 00139 Decl** End = Begin + NumDecls; 00140 TagDecl* TD = dyn_cast<TagDecl>(*Begin); 00141 if (TD) 00142 ++Begin; 00143 00144 PrintingPolicy SubPolicy(Policy); 00145 if (TD && TD->isCompleteDefinition()) { 00146 TD->print(Out, Policy, Indentation); 00147 Out << " "; 00148 SubPolicy.SuppressTag = true; 00149 } 00150 00151 bool isFirst = true; 00152 for ( ; Begin != End; ++Begin) { 00153 if (isFirst) { 00154 SubPolicy.SuppressSpecifiers = false; 00155 isFirst = false; 00156 } else { 00157 if (!isFirst) Out << ", "; 00158 SubPolicy.SuppressSpecifiers = true; 00159 } 00160 00161 (*Begin)->print(Out, SubPolicy, Indentation); 00162 } 00163 } 00164 00165 void DeclContext::dumpDeclContext() const { 00166 // Get the translation unit 00167 const DeclContext *DC = this; 00168 while (!DC->isTranslationUnit()) 00169 DC = DC->getParent(); 00170 00171 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); 00172 DeclPrinter Printer(llvm::errs(), Ctx, Ctx.getPrintingPolicy(), 0); 00173 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); 00174 } 00175 00176 void Decl::dump() const { 00177 print(llvm::errs()); 00178 } 00179 00180 raw_ostream& DeclPrinter::Indent(unsigned Indentation) { 00181 for (unsigned i = 0; i != Indentation; ++i) 00182 Out << " "; 00183 return Out; 00184 } 00185 00186 void DeclPrinter::prettyPrintAttributes(Decl *D) { 00187 if (D->hasAttrs()) { 00188 AttrVec &Attrs = D->getAttrs(); 00189 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) { 00190 Attr *A = *i; 00191 A->printPretty(Out, Context); 00192 } 00193 } 00194 } 00195 00196 void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { 00197 this->Indent(); 00198 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); 00199 Out << ";\n"; 00200 Decls.clear(); 00201 00202 } 00203 00204 void DeclPrinter::Print(AccessSpecifier AS) { 00205 switch(AS) { 00206 case AS_none: llvm_unreachable("No access specifier!"); 00207 case AS_public: Out << "public"; break; 00208 case AS_protected: Out << "protected"; break; 00209 case AS_private: Out << "private"; break; 00210 } 00211 } 00212 00213 //---------------------------------------------------------------------------- 00214 // Common C declarations 00215 //---------------------------------------------------------------------------- 00216 00217 void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { 00218 if (Indent) 00219 Indentation += Policy.Indentation; 00220 00221 SmallVector<Decl*, 2> Decls; 00222 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 00223 D != DEnd; ++D) { 00224 00225 // Don't print ObjCIvarDecls, as they are printed when visiting the 00226 // containing ObjCInterfaceDecl. 00227 if (isa<ObjCIvarDecl>(*D)) 00228 continue; 00229 00230 if (!Policy.Dump) { 00231 // Skip over implicit declarations in pretty-printing mode. 00232 if (D->isImplicit()) continue; 00233 // FIXME: Ugly hack so we don't pretty-print the builtin declaration 00234 // of __builtin_va_list or __[u]int128_t. There should be some other way 00235 // to check that. 00236 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) { 00237 if (IdentifierInfo *II = ND->getIdentifier()) { 00238 if (II->isStr("__builtin_va_list") || 00239 II->isStr("__int128_t") || II->isStr("__uint128_t")) 00240 continue; 00241 } 00242 } 00243 } 00244 00245 // The next bits of code handles stuff like "struct {int x;} a,b"; we're 00246 // forced to merge the declarations because there's no other way to 00247 // refer to the struct in question. This limited merging is safe without 00248 // a bunch of other checks because it only merges declarations directly 00249 // referring to the tag, not typedefs. 00250 // 00251 // Check whether the current declaration should be grouped with a previous 00252 // unnamed struct. 00253 QualType CurDeclType = getDeclType(*D); 00254 if (!Decls.empty() && !CurDeclType.isNull()) { 00255 QualType BaseType = GetBaseType(CurDeclType); 00256 if (!BaseType.isNull() && isa<TagType>(BaseType) && 00257 cast<TagType>(BaseType)->getDecl() == Decls[0]) { 00258 Decls.push_back(*D); 00259 continue; 00260 } 00261 } 00262 00263 // If we have a merged group waiting to be handled, handle it now. 00264 if (!Decls.empty()) 00265 ProcessDeclGroup(Decls); 00266 00267 // If the current declaration is an unnamed tag type, save it 00268 // so we can merge it with the subsequent declaration(s) using it. 00269 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) { 00270 Decls.push_back(*D); 00271 continue; 00272 } 00273 00274 if (isa<AccessSpecDecl>(*D)) { 00275 Indentation -= Policy.Indentation; 00276 this->Indent(); 00277 Print(D->getAccess()); 00278 Out << ":\n"; 00279 Indentation += Policy.Indentation; 00280 continue; 00281 } 00282 00283 this->Indent(); 00284 Visit(*D); 00285 00286 // FIXME: Need to be able to tell the DeclPrinter when 00287 const char *Terminator = 0; 00288 if (isa<FunctionDecl>(*D) && 00289 cast<FunctionDecl>(*D)->isThisDeclarationADefinition()) 00290 Terminator = 0; 00291 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody()) 00292 Terminator = 0; 00293 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || 00294 isa<ObjCImplementationDecl>(*D) || 00295 isa<ObjCInterfaceDecl>(*D) || 00296 isa<ObjCProtocolDecl>(*D) || 00297 isa<ObjCCategoryImplDecl>(*D) || 00298 isa<ObjCCategoryDecl>(*D)) 00299 Terminator = 0; 00300 else if (isa<EnumConstantDecl>(*D)) { 00301 DeclContext::decl_iterator Next = D; 00302 ++Next; 00303 if (Next != DEnd) 00304 Terminator = ","; 00305 } else 00306 Terminator = ";"; 00307 00308 if (Terminator) 00309 Out << Terminator; 00310 Out << "\n"; 00311 } 00312 00313 if (!Decls.empty()) 00314 ProcessDeclGroup(Decls); 00315 00316 if (Indent) 00317 Indentation -= Policy.Indentation; 00318 } 00319 00320 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 00321 VisitDeclContext(D, false); 00322 } 00323 00324 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { 00325 if (!Policy.SuppressSpecifiers) { 00326 Out << "typedef "; 00327 00328 if (D->isModulePrivate()) 00329 Out << "__module_private__ "; 00330 } 00331 D->getUnderlyingType().print(Out, Policy, D->getName()); 00332 prettyPrintAttributes(D); 00333 } 00334 00335 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 00336 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy); 00337 } 00338 00339 void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 00340 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 00341 Out << "__module_private__ "; 00342 Out << "enum "; 00343 if (D->isScoped()) { 00344 if (D->isScopedUsingClassTag()) 00345 Out << "class "; 00346 else 00347 Out << "struct "; 00348 } 00349 Out << *D; 00350 00351 if (D->isFixed()) 00352 Out << " : " << D->getIntegerType().stream(Policy); 00353 00354 if (D->isCompleteDefinition()) { 00355 Out << " {\n"; 00356 VisitDeclContext(D); 00357 Indent() << "}"; 00358 } 00359 prettyPrintAttributes(D); 00360 } 00361 00362 void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 00363 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 00364 Out << "__module_private__ "; 00365 Out << D->getKindName(); 00366 if (D->getIdentifier()) 00367 Out << ' ' << *D; 00368 00369 if (D->isCompleteDefinition()) { 00370 Out << " {\n"; 00371 VisitDeclContext(D); 00372 Indent() << "}"; 00373 } 00374 } 00375 00376 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 00377 Out << *D; 00378 if (Expr *Init = D->getInitExpr()) { 00379 Out << " = "; 00380 Init->printPretty(Out, Context, 0, Policy, Indentation); 00381 } 00382 } 00383 00384 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 00385 if (!Policy.SuppressSpecifiers) { 00386 switch (D->getStorageClassAsWritten()) { 00387 case SC_None: break; 00388 case SC_Extern: Out << "extern "; break; 00389 case SC_Static: Out << "static "; break; 00390 case SC_PrivateExtern: Out << "__private_extern__ "; break; 00391 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: 00392 llvm_unreachable("invalid for functions"); 00393 } 00394 00395 if (D->isInlineSpecified()) Out << "inline "; 00396 if (D->isVirtualAsWritten()) Out << "virtual "; 00397 if (D->isModulePrivate()) Out << "__module_private__ "; 00398 } 00399 00400 PrintingPolicy SubPolicy(Policy); 00401 SubPolicy.SuppressSpecifiers = false; 00402 std::string Proto = D->getNameInfo().getAsString(); 00403 00404 QualType Ty = D->getType(); 00405 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 00406 Proto = '(' + Proto + ')'; 00407 Ty = PT->getInnerType(); 00408 } 00409 00410 if (isa<FunctionType>(Ty)) { 00411 const FunctionType *AFT = Ty->getAs<FunctionType>(); 00412 const FunctionProtoType *FT = 0; 00413 if (D->hasWrittenPrototype()) 00414 FT = dyn_cast<FunctionProtoType>(AFT); 00415 00416 Proto += "("; 00417 if (FT) { 00418 llvm::raw_string_ostream POut(Proto); 00419 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation); 00420 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 00421 if (i) POut << ", "; 00422 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 00423 } 00424 00425 if (FT->isVariadic()) { 00426 if (D->getNumParams()) POut << ", "; 00427 POut << "..."; 00428 } 00429 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 00430 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 00431 if (i) 00432 Proto += ", "; 00433 Proto += D->getParamDecl(i)->getNameAsString(); 00434 } 00435 } 00436 00437 Proto += ")"; 00438 00439 if (FT && FT->getTypeQuals()) { 00440 unsigned TypeQuals = FT->getTypeQuals(); 00441 if (TypeQuals & Qualifiers::Const) 00442 Proto += " const"; 00443 if (TypeQuals & Qualifiers::Volatile) 00444 Proto += " volatile"; 00445 if (TypeQuals & Qualifiers::Restrict) 00446 Proto += " restrict"; 00447 } 00448 00449 if (FT && FT->hasDynamicExceptionSpec()) { 00450 Proto += " throw("; 00451 if (FT->getExceptionSpecType() == EST_MSAny) 00452 Proto += "..."; 00453 else 00454 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 00455 if (I) 00456 Proto += ", "; 00457 00458 Proto += FT->getExceptionType(I).getAsString(SubPolicy);; 00459 } 00460 Proto += ")"; 00461 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 00462 Proto += " noexcept"; 00463 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 00464 Proto += "("; 00465 llvm::raw_string_ostream EOut(Proto); 00466 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy, 00467 Indentation); 00468 EOut.flush(); 00469 Proto += EOut.str(); 00470 Proto += ")"; 00471 } 00472 } 00473 00474 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) { 00475 bool HasInitializerList = false; 00476 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), 00477 E = CDecl->init_end(); 00478 B != E; ++B) { 00479 CXXCtorInitializer * BMInitializer = (*B); 00480 if (BMInitializer->isInClassMemberInitializer()) 00481 continue; 00482 00483 if (!HasInitializerList) { 00484 Proto += " : "; 00485 Out << Proto; 00486 Proto.clear(); 00487 HasInitializerList = true; 00488 } else 00489 Out << ", "; 00490 00491 if (BMInitializer->isAnyMemberInitializer()) { 00492 FieldDecl *FD = BMInitializer->getAnyMember(); 00493 Out << *FD; 00494 } else { 00495 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 00496 } 00497 00498 Out << "("; 00499 if (!BMInitializer->getInit()) { 00500 // Nothing to print 00501 } else { 00502 Expr *Init = BMInitializer->getInit(); 00503 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 00504 Init = Tmp->getSubExpr(); 00505 00506 Init = Init->IgnoreParens(); 00507 00508 Expr *SimpleInit = 0; 00509 Expr **Args = 0; 00510 unsigned NumArgs = 0; 00511 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 00512 Args = ParenList->getExprs(); 00513 NumArgs = ParenList->getNumExprs(); 00514 } else if (CXXConstructExpr *Construct 00515 = dyn_cast<CXXConstructExpr>(Init)) { 00516 Args = Construct->getArgs(); 00517 NumArgs = Construct->getNumArgs(); 00518 } else 00519 SimpleInit = Init; 00520 00521 if (SimpleInit) 00522 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation); 00523 else { 00524 for (unsigned I = 0; I != NumArgs; ++I) { 00525 if (isa<CXXDefaultArgExpr>(Args[I])) 00526 break; 00527 00528 if (I) 00529 Out << ", "; 00530 Args[I]->printPretty(Out, Context, 0, Policy, Indentation); 00531 } 00532 } 00533 } 00534 Out << ")"; 00535 } 00536 } 00537 else 00538 AFT->getResultType().print(Out, Policy, Proto); 00539 } else { 00540 Ty.print(Out, Policy, Proto); 00541 } 00542 00543 prettyPrintAttributes(D); 00544 00545 if (D->isPure()) 00546 Out << " = 0"; 00547 else if (D->isDeletedAsWritten()) 00548 Out << " = delete"; 00549 else if (D->doesThisDeclarationHaveABody()) { 00550 if (!D->hasPrototype() && D->getNumParams()) { 00551 // This is a K&R function definition, so we need to print the 00552 // parameters. 00553 Out << '\n'; 00554 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation); 00555 Indentation += Policy.Indentation; 00556 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 00557 Indent(); 00558 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 00559 Out << ";\n"; 00560 } 00561 Indentation -= Policy.Indentation; 00562 } else 00563 Out << ' '; 00564 00565 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation); 00566 Out << '\n'; 00567 } 00568 } 00569 00570 void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 00571 if (!Policy.SuppressSpecifiers && D->isMutable()) 00572 Out << "mutable "; 00573 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 00574 Out << "__module_private__ "; 00575 00576 Out << D->getType().stream(Policy, D->getName()); 00577 00578 if (D->isBitField()) { 00579 Out << " : "; 00580 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation); 00581 } 00582 00583 Expr *Init = D->getInClassInitializer(); 00584 if (!Policy.SuppressInitializers && Init) { 00585 Out << " = "; 00586 Init->printPretty(Out, Context, 0, Policy, Indentation); 00587 } 00588 prettyPrintAttributes(D); 00589 } 00590 00591 void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 00592 Out << *D << ":"; 00593 } 00594 00595 00596 void DeclPrinter::VisitVarDecl(VarDecl *D) { 00597 StorageClass SCAsWritten = D->getStorageClassAsWritten(); 00598 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None) 00599 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " "; 00600 00601 if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) 00602 Out << "__thread "; 00603 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 00604 Out << "__module_private__ "; 00605 00606 QualType T = D->getType(); 00607 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) 00608 T = Parm->getOriginalType(); 00609 T.print(Out, Policy, D->getName()); 00610 Expr *Init = D->getInit(); 00611 if (!Policy.SuppressInitializers && Init) { 00612 bool ImplicitInit = false; 00613 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) 00614 ImplicitInit = D->getInitStyle() == VarDecl::CallInit && 00615 Construct->getNumArgs() == 0 && !Construct->isListInitialization(); 00616 if (!ImplicitInit) { 00617 if (D->getInitStyle() == VarDecl::CallInit) 00618 Out << "("; 00619 else if (D->getInitStyle() == VarDecl::CInit) { 00620 Out << " = "; 00621 } 00622 Init->printPretty(Out, Context, 0, Policy, Indentation); 00623 if (D->getInitStyle() == VarDecl::CallInit) 00624 Out << ")"; 00625 } 00626 } 00627 prettyPrintAttributes(D); 00628 } 00629 00630 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 00631 VisitVarDecl(D); 00632 } 00633 00634 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 00635 Out << "__asm ("; 00636 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation); 00637 Out << ")"; 00638 } 00639 00640 void DeclPrinter::VisitImportDecl(ImportDecl *D) { 00641 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName() 00642 << ";\n"; 00643 } 00644 00645 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 00646 Out << "static_assert("; 00647 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation); 00648 Out << ", "; 00649 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation); 00650 Out << ")"; 00651 } 00652 00653 //---------------------------------------------------------------------------- 00654 // C++ declarations 00655 //---------------------------------------------------------------------------- 00656 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 00657 if (D->isInline()) 00658 Out << "inline "; 00659 Out << "namespace " << *D << " {\n"; 00660 VisitDeclContext(D); 00661 Indent() << "}"; 00662 } 00663 00664 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 00665 Out << "using namespace "; 00666 if (D->getQualifier()) 00667 D->getQualifier()->print(Out, Policy); 00668 Out << *D->getNominatedNamespaceAsWritten(); 00669 } 00670 00671 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 00672 Out << "namespace " << *D << " = "; 00673 if (D->getQualifier()) 00674 D->getQualifier()->print(Out, Policy); 00675 Out << *D->getAliasedNamespace(); 00676 } 00677 00678 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 00679 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 00680 Out << "__module_private__ "; 00681 Out << D->getKindName(); 00682 if (D->getIdentifier()) 00683 Out << ' ' << *D; 00684 00685 if (D->isCompleteDefinition()) { 00686 // Print the base classes 00687 if (D->getNumBases()) { 00688 Out << " : "; 00689 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 00690 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 00691 if (Base != D->bases_begin()) 00692 Out << ", "; 00693 00694 if (Base->isVirtual()) 00695 Out << "virtual "; 00696 00697 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 00698 if (AS != AS_none) 00699 Print(AS); 00700 Out << " " << Base->getType().getAsString(Policy); 00701 00702 if (Base->isPackExpansion()) 00703 Out << "..."; 00704 } 00705 } 00706 00707 // Print the class definition 00708 // FIXME: Doesn't print access specifiers, e.g., "public:" 00709 Out << " {\n"; 00710 VisitDeclContext(D); 00711 Indent() << "}"; 00712 } 00713 } 00714 00715 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 00716 const char *l; 00717 if (D->getLanguage() == LinkageSpecDecl::lang_c) 00718 l = "C"; 00719 else { 00720 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 00721 "unknown language in linkage specification"); 00722 l = "C++"; 00723 } 00724 00725 Out << "extern \"" << l << "\" "; 00726 if (D->hasBraces()) { 00727 Out << "{\n"; 00728 VisitDeclContext(D); 00729 Indent() << "}"; 00730 } else 00731 Visit(*D->decls_begin()); 00732 } 00733 00734 void DeclPrinter::PrintTemplateParameters( 00735 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) { 00736 assert(Params); 00737 assert(!Args || Params->size() == Args->size()); 00738 00739 Out << "template <"; 00740 00741 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 00742 if (i != 0) 00743 Out << ", "; 00744 00745 const Decl *Param = Params->getParam(i); 00746 if (const TemplateTypeParmDecl *TTP = 00747 dyn_cast<TemplateTypeParmDecl>(Param)) { 00748 00749 if (TTP->wasDeclaredWithTypename()) 00750 Out << "typename "; 00751 else 00752 Out << "class "; 00753 00754 if (TTP->isParameterPack()) 00755 Out << "... "; 00756 00757 Out << *TTP; 00758 00759 if (Args) { 00760 Out << " = "; 00761 Args->get(i).print(Policy, Out); 00762 } else if (TTP->hasDefaultArgument()) { 00763 Out << " = "; 00764 Out << TTP->getDefaultArgument().getAsString(Policy); 00765 }; 00766 } else if (const NonTypeTemplateParmDecl *NTTP = 00767 dyn_cast<NonTypeTemplateParmDecl>(Param)) { 00768 Out << NTTP->getType().getAsString(Policy); 00769 00770 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType())) 00771 Out << "..."; 00772 00773 if (IdentifierInfo *Name = NTTP->getIdentifier()) { 00774 Out << ' '; 00775 Out << Name->getName(); 00776 } 00777 00778 if (Args) { 00779 Out << " = "; 00780 Args->get(i).print(Policy, Out); 00781 } else if (NTTP->hasDefaultArgument()) { 00782 Out << " = "; 00783 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy, 00784 Indentation); 00785 } 00786 } else if (const TemplateTemplateParmDecl *TTPD = 00787 dyn_cast<TemplateTemplateParmDecl>(Param)) { 00788 VisitTemplateDecl(TTPD); 00789 // FIXME: print the default argument, if present. 00790 } 00791 } 00792 00793 Out << "> "; 00794 } 00795 00796 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 00797 PrintTemplateParameters(D->getTemplateParameters()); 00798 00799 if (const TemplateTemplateParmDecl *TTP = 00800 dyn_cast<TemplateTemplateParmDecl>(D)) { 00801 Out << "class "; 00802 if (TTP->isParameterPack()) 00803 Out << "..."; 00804 Out << D->getName(); 00805 } else { 00806 Visit(D->getTemplatedDecl()); 00807 } 00808 } 00809 00810 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 00811 if (PrintInstantiation) { 00812 TemplateParameterList *Params = D->getTemplateParameters(); 00813 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 00814 I != E; ++I) { 00815 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs()); 00816 Visit(*I); 00817 } 00818 } 00819 00820 return VisitRedeclarableTemplateDecl(D); 00821 } 00822 00823 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 00824 if (PrintInstantiation) { 00825 TemplateParameterList *Params = D->getTemplateParameters(); 00826 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 00827 I != E; ++I) { 00828 PrintTemplateParameters(Params, &(*I)->getTemplateArgs()); 00829 Visit(*I); 00830 Out << '\n'; 00831 } 00832 } 00833 00834 return VisitRedeclarableTemplateDecl(D); 00835 } 00836 00837 //---------------------------------------------------------------------------- 00838 // Objective-C declarations 00839 //---------------------------------------------------------------------------- 00840 00841 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 00842 if (OMD->isInstanceMethod()) 00843 Out << "- "; 00844 else 00845 Out << "+ "; 00846 if (!OMD->getResultType().isNull()) 00847 Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; 00848 00849 std::string name = OMD->getSelector().getAsString(); 00850 std::string::size_type pos, lastPos = 0; 00851 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), 00852 E = OMD->param_end(); PI != E; ++PI) { 00853 // FIXME: selector is missing here! 00854 pos = name.find_first_of(':', lastPos); 00855 Out << " " << name.substr(lastPos, pos - lastPos); 00856 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI; 00857 lastPos = pos + 1; 00858 } 00859 00860 if (OMD->param_begin() == OMD->param_end()) 00861 Out << " " << name; 00862 00863 if (OMD->isVariadic()) 00864 Out << ", ..."; 00865 00866 if (OMD->getBody()) { 00867 Out << ' '; 00868 OMD->getBody()->printPretty(Out, Context, 0, Policy); 00869 Out << '\n'; 00870 } 00871 } 00872 00873 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 00874 std::string I = OID->getNameAsString(); 00875 ObjCInterfaceDecl *SID = OID->getSuperClass(); 00876 00877 if (SID) 00878 Out << "@implementation " << I << " : " << *SID; 00879 else 00880 Out << "@implementation " << I; 00881 Out << "\n"; 00882 VisitDeclContext(OID, false); 00883 Out << "@end"; 00884 } 00885 00886 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 00887 std::string I = OID->getNameAsString(); 00888 ObjCInterfaceDecl *SID = OID->getSuperClass(); 00889 00890 if (!OID->isThisDeclarationADefinition()) { 00891 Out << "@class " << I << ";"; 00892 return; 00893 } 00894 00895 if (SID) 00896 Out << "@interface " << I << " : " << *SID; 00897 else 00898 Out << "@interface " << I; 00899 00900 // Protocols? 00901 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 00902 if (!Protocols.empty()) { 00903 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 00904 E = Protocols.end(); I != E; ++I) 00905 Out << (I == Protocols.begin() ? '<' : ',') << **I; 00906 } 00907 00908 if (!Protocols.empty()) 00909 Out << "> "; 00910 00911 if (OID->ivar_size() > 0) { 00912 Out << "{\n"; 00913 Indentation += Policy.Indentation; 00914 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), 00915 E = OID->ivar_end(); I != E; ++I) { 00916 Indent() << I->getType().getAsString(Policy) << ' ' << *I << ";\n"; 00917 } 00918 Indentation -= Policy.Indentation; 00919 Out << "}\n"; 00920 } 00921 00922 VisitDeclContext(OID, false); 00923 Out << "@end"; 00924 // FIXME: implement the rest... 00925 } 00926 00927 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 00928 if (!PID->isThisDeclarationADefinition()) { 00929 Out << "@protocol " << PID->getIdentifier() << ";\n"; 00930 return; 00931 } 00932 00933 Out << "@protocol " << *PID << '\n'; 00934 VisitDeclContext(PID, false); 00935 Out << "@end"; 00936 } 00937 00938 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 00939 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 00940 00941 VisitDeclContext(PID, false); 00942 Out << "@end"; 00943 // FIXME: implement the rest... 00944 } 00945 00946 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 00947 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n"; 00948 VisitDeclContext(PID, false); 00949 Out << "@end"; 00950 00951 // FIXME: implement the rest... 00952 } 00953 00954 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 00955 Out << "@compatibility_alias " << *AID 00956 << ' ' << *AID->getClassInterface() << ";\n"; 00957 } 00958 00959 /// PrintObjCPropertyDecl - print a property declaration. 00960 /// 00961 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 00962 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 00963 Out << "@required\n"; 00964 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 00965 Out << "@optional\n"; 00966 00967 Out << "@property"; 00968 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 00969 bool first = true; 00970 Out << " ("; 00971 if (PDecl->getPropertyAttributes() & 00972 ObjCPropertyDecl::OBJC_PR_readonly) { 00973 Out << (first ? ' ' : ',') << "readonly"; 00974 first = false; 00975 } 00976 00977 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 00978 Out << (first ? ' ' : ',') << "getter = " 00979 << PDecl->getGetterName().getAsString(); 00980 first = false; 00981 } 00982 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 00983 Out << (first ? ' ' : ',') << "setter = " 00984 << PDecl->getSetterName().getAsString(); 00985 first = false; 00986 } 00987 00988 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 00989 Out << (first ? ' ' : ',') << "assign"; 00990 first = false; 00991 } 00992 00993 if (PDecl->getPropertyAttributes() & 00994 ObjCPropertyDecl::OBJC_PR_readwrite) { 00995 Out << (first ? ' ' : ',') << "readwrite"; 00996 first = false; 00997 } 00998 00999 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 01000 Out << (first ? ' ' : ',') << "retain"; 01001 first = false; 01002 } 01003 01004 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 01005 Out << (first ? ' ' : ',') << "strong"; 01006 first = false; 01007 } 01008 01009 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 01010 Out << (first ? ' ' : ',') << "copy"; 01011 first = false; 01012 } 01013 01014 if (PDecl->getPropertyAttributes() & 01015 ObjCPropertyDecl::OBJC_PR_nonatomic) { 01016 Out << (first ? ' ' : ',') << "nonatomic"; 01017 first = false; 01018 } 01019 if (PDecl->getPropertyAttributes() & 01020 ObjCPropertyDecl::OBJC_PR_atomic) { 01021 Out << (first ? ' ' : ',') << "atomic"; 01022 first = false; 01023 } 01024 01025 (void) first; // Silence dead store warning due to idiomatic code. 01026 Out << " )"; 01027 } 01028 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl; 01029 } 01030 01031 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 01032 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 01033 Out << "@synthesize "; 01034 else 01035 Out << "@dynamic "; 01036 Out << *PID->getPropertyDecl(); 01037 if (PID->getPropertyIvarDecl()) 01038 Out << '=' << *PID->getPropertyIvarDecl(); 01039 } 01040 01041 void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 01042 Out << "using "; 01043 D->getQualifier()->print(Out, Policy); 01044 Out << *D; 01045 } 01046 01047 void 01048 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 01049 Out << "using typename "; 01050 D->getQualifier()->print(Out, Policy); 01051 Out << D->getDeclName(); 01052 } 01053 01054 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 01055 Out << "using "; 01056 D->getQualifier()->print(Out, Policy); 01057 Out << D->getDeclName(); 01058 } 01059 01060 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 01061 // ignore 01062 }