DeclPrinter.cpp revision ff685c562c8fd5dfc6effec17377fde9dad6f271
1//===--- 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/Attr.h" 16#include "clang/AST/Decl.h" 17#include "clang/AST/DeclCXX.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/DeclVisitor.h" 20#include "clang/AST/Expr.h" 21#include "clang/AST/ExprCXX.h" 22#include "clang/AST/PrettyPrinter.h" 23#include "clang/Basic/Module.h" 24#include "llvm/Support/raw_ostream.h" 25using namespace clang; 26 27namespace { 28 class DeclPrinter : public DeclVisitor<DeclPrinter> { 29 raw_ostream &Out; 30 PrintingPolicy Policy; 31 unsigned Indentation; 32 bool PrintInstantiation; 33 34 raw_ostream& Indent() { return Indent(Indentation); } 35 raw_ostream& Indent(unsigned Indentation); 36 void ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls); 37 38 void Print(AccessSpecifier AS); 39 40 public: 41 DeclPrinter(raw_ostream &Out, const PrintingPolicy &Policy, 42 unsigned Indentation = 0, bool PrintInstantiation = false) 43 : Out(Out), Policy(Policy), Indentation(Indentation), 44 PrintInstantiation(PrintInstantiation) { } 45 46 void VisitDeclContext(DeclContext *DC, bool Indent = true); 47 48 void VisitTranslationUnitDecl(TranslationUnitDecl *D); 49 void VisitTypedefDecl(TypedefDecl *D); 50 void VisitTypeAliasDecl(TypeAliasDecl *D); 51 void VisitEnumDecl(EnumDecl *D); 52 void VisitRecordDecl(RecordDecl *D); 53 void VisitEnumConstantDecl(EnumConstantDecl *D); 54 void VisitFunctionDecl(FunctionDecl *D); 55 void VisitFieldDecl(FieldDecl *D); 56 void VisitVarDecl(VarDecl *D); 57 void VisitLabelDecl(LabelDecl *D); 58 void VisitParmVarDecl(ParmVarDecl *D); 59 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); 60 void VisitImportDecl(ImportDecl *D); 61 void VisitStaticAssertDecl(StaticAssertDecl *D); 62 void VisitNamespaceDecl(NamespaceDecl *D); 63 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 64 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 65 void VisitCXXRecordDecl(CXXRecordDecl *D); 66 void VisitLinkageSpecDecl(LinkageSpecDecl *D); 67 void VisitTemplateDecl(const TemplateDecl *D); 68 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 69 void VisitClassTemplateDecl(ClassTemplateDecl *D); 70 void VisitObjCMethodDecl(ObjCMethodDecl *D); 71 void VisitObjCImplementationDecl(ObjCImplementationDecl *D); 72 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); 73 void VisitObjCProtocolDecl(ObjCProtocolDecl *D); 74 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D); 75 void VisitObjCCategoryDecl(ObjCCategoryDecl *D); 76 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D); 77 void VisitObjCPropertyDecl(ObjCPropertyDecl *D); 78 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D); 79 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 80 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 81 void VisitUsingDecl(UsingDecl *D); 82 void VisitUsingShadowDecl(UsingShadowDecl *D); 83 84 void PrintTemplateParameters(const TemplateParameterList *Params, 85 const TemplateArgumentList *Args); 86 void prettyPrintAttributes(Decl *D); 87 }; 88} 89 90void Decl::print(raw_ostream &Out, unsigned Indentation, 91 bool PrintInstantiation) const { 92 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 93} 94 95void Decl::print(raw_ostream &Out, const PrintingPolicy &Policy, 96 unsigned Indentation, bool PrintInstantiation) const { 97 DeclPrinter Printer(Out, Policy, Indentation, PrintInstantiation); 98 Printer.Visit(const_cast<Decl*>(this)); 99} 100 101static QualType GetBaseType(QualType T) { 102 // FIXME: This should be on the Type class! 103 QualType BaseType = T; 104 while (!BaseType->isSpecifierType()) { 105 if (isa<TypedefType>(BaseType)) 106 break; 107 else if (const PointerType* PTy = BaseType->getAs<PointerType>()) 108 BaseType = PTy->getPointeeType(); 109 else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>()) 110 BaseType = BPy->getPointeeType(); 111 else if (const ArrayType* ATy = dyn_cast<ArrayType>(BaseType)) 112 BaseType = ATy->getElementType(); 113 else if (const FunctionType* FTy = BaseType->getAs<FunctionType>()) 114 BaseType = FTy->getResultType(); 115 else if (const VectorType *VTy = BaseType->getAs<VectorType>()) 116 BaseType = VTy->getElementType(); 117 else if (const ReferenceType *RTy = BaseType->getAs<ReferenceType>()) 118 BaseType = RTy->getPointeeType(); 119 else 120 llvm_unreachable("Unknown declarator!"); 121 } 122 return BaseType; 123} 124 125static QualType getDeclType(Decl* D) { 126 if (TypedefNameDecl* TDD = dyn_cast<TypedefNameDecl>(D)) 127 return TDD->getUnderlyingType(); 128 if (ValueDecl* VD = dyn_cast<ValueDecl>(D)) 129 return VD->getType(); 130 return QualType(); 131} 132 133void Decl::printGroup(Decl** Begin, unsigned NumDecls, 134 raw_ostream &Out, const PrintingPolicy &Policy, 135 unsigned Indentation) { 136 if (NumDecls == 1) { 137 (*Begin)->print(Out, Policy, Indentation); 138 return; 139 } 140 141 Decl** End = Begin + NumDecls; 142 TagDecl* TD = dyn_cast<TagDecl>(*Begin); 143 if (TD) 144 ++Begin; 145 146 PrintingPolicy SubPolicy(Policy); 147 if (TD && TD->isCompleteDefinition()) { 148 TD->print(Out, Policy, Indentation); 149 Out << " "; 150 SubPolicy.SuppressTag = true; 151 } 152 153 bool isFirst = true; 154 for ( ; Begin != End; ++Begin) { 155 if (isFirst) { 156 SubPolicy.SuppressSpecifiers = false; 157 isFirst = false; 158 } else { 159 if (!isFirst) Out << ", "; 160 SubPolicy.SuppressSpecifiers = true; 161 } 162 163 (*Begin)->print(Out, SubPolicy, Indentation); 164 } 165} 166 167void DeclContext::dumpDeclContext() const { 168 // Get the translation unit 169 const DeclContext *DC = this; 170 while (!DC->isTranslationUnit()) 171 DC = DC->getParent(); 172 173 ASTContext &Ctx = cast<TranslationUnitDecl>(DC)->getASTContext(); 174 DeclPrinter Printer(llvm::errs(), Ctx.getPrintingPolicy(), 0); 175 Printer.VisitDeclContext(const_cast<DeclContext *>(this), /*Indent=*/false); 176} 177 178void Decl::dump() const { 179 dump(llvm::errs()); 180} 181 182void Decl::dump(raw_ostream &Out) const { 183 PrintingPolicy Policy = getASTContext().getPrintingPolicy(); 184 Policy.DumpSourceManager = &getASTContext().getSourceManager(); 185 print(Out, Policy, /*Indentation*/ 0, /*PrintInstantiation*/ true); 186} 187 188raw_ostream& DeclPrinter::Indent(unsigned Indentation) { 189 for (unsigned i = 0; i != Indentation; ++i) 190 Out << " "; 191 return Out; 192} 193 194void DeclPrinter::prettyPrintAttributes(Decl *D) { 195 if (Policy.SuppressAttributes) 196 return; 197 198 if (D->hasAttrs()) { 199 AttrVec &Attrs = D->getAttrs(); 200 for (AttrVec::const_iterator i=Attrs.begin(), e=Attrs.end(); i!=e; ++i) { 201 Attr *A = *i; 202 A->printPretty(Out, Policy); 203 } 204 } 205} 206 207void DeclPrinter::ProcessDeclGroup(SmallVectorImpl<Decl*>& Decls) { 208 this->Indent(); 209 Decl::printGroup(Decls.data(), Decls.size(), Out, Policy, Indentation); 210 Out << ";\n"; 211 Decls.clear(); 212 213} 214 215void DeclPrinter::Print(AccessSpecifier AS) { 216 switch(AS) { 217 case AS_none: llvm_unreachable("No access specifier!"); 218 case AS_public: Out << "public"; break; 219 case AS_protected: Out << "protected"; break; 220 case AS_private: Out << "private"; break; 221 } 222} 223 224//---------------------------------------------------------------------------- 225// Common C declarations 226//---------------------------------------------------------------------------- 227 228void DeclPrinter::VisitDeclContext(DeclContext *DC, bool Indent) { 229 if (Policy.TerseOutput) 230 return; 231 232 if (Indent) 233 Indentation += Policy.Indentation; 234 235 SmallVector<Decl*, 2> Decls; 236 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); 237 D != DEnd; ++D) { 238 239 // Don't print ObjCIvarDecls, as they are printed when visiting the 240 // containing ObjCInterfaceDecl. 241 if (isa<ObjCIvarDecl>(*D)) 242 continue; 243 244 if (!Policy.DumpSourceManager) { 245 // Skip over implicit declarations in pretty-printing mode. 246 if (D->isImplicit()) continue; 247 // FIXME: Ugly hack so we don't pretty-print the builtin declaration 248 // of __builtin_va_list or __[u]int128_t. There should be some other way 249 // to check that. 250 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D)) { 251 if (IdentifierInfo *II = ND->getIdentifier()) { 252 if (II->isStr("__builtin_va_list") || 253 II->isStr("__int128_t") || II->isStr("__uint128_t")) 254 continue; 255 } 256 } 257 } 258 259 // The next bits of code handles stuff like "struct {int x;} a,b"; we're 260 // forced to merge the declarations because there's no other way to 261 // refer to the struct in question. This limited merging is safe without 262 // a bunch of other checks because it only merges declarations directly 263 // referring to the tag, not typedefs. 264 // 265 // Check whether the current declaration should be grouped with a previous 266 // unnamed struct. 267 QualType CurDeclType = getDeclType(*D); 268 if (!Decls.empty() && !CurDeclType.isNull()) { 269 QualType BaseType = GetBaseType(CurDeclType); 270 if (!BaseType.isNull() && isa<TagType>(BaseType) && 271 cast<TagType>(BaseType)->getDecl() == Decls[0]) { 272 Decls.push_back(*D); 273 continue; 274 } 275 } 276 277 // If we have a merged group waiting to be handled, handle it now. 278 if (!Decls.empty()) 279 ProcessDeclGroup(Decls); 280 281 // If the current declaration is an unnamed tag type, save it 282 // so we can merge it with the subsequent declaration(s) using it. 283 if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) { 284 Decls.push_back(*D); 285 continue; 286 } 287 288 if (isa<AccessSpecDecl>(*D)) { 289 Indentation -= Policy.Indentation; 290 this->Indent(); 291 Print(D->getAccess()); 292 Out << ":\n"; 293 Indentation += Policy.Indentation; 294 continue; 295 } 296 297 this->Indent(); 298 Visit(*D); 299 300 // FIXME: Need to be able to tell the DeclPrinter when 301 const char *Terminator = 0; 302 if (isa<FunctionDecl>(*D) && 303 cast<FunctionDecl>(*D)->isThisDeclarationADefinition()) 304 Terminator = 0; 305 else if (isa<ObjCMethodDecl>(*D) && cast<ObjCMethodDecl>(*D)->getBody()) 306 Terminator = 0; 307 else if (isa<NamespaceDecl>(*D) || isa<LinkageSpecDecl>(*D) || 308 isa<ObjCImplementationDecl>(*D) || 309 isa<ObjCInterfaceDecl>(*D) || 310 isa<ObjCProtocolDecl>(*D) || 311 isa<ObjCCategoryImplDecl>(*D) || 312 isa<ObjCCategoryDecl>(*D)) 313 Terminator = 0; 314 else if (isa<EnumConstantDecl>(*D)) { 315 DeclContext::decl_iterator Next = D; 316 ++Next; 317 if (Next != DEnd) 318 Terminator = ","; 319 } else 320 Terminator = ";"; 321 322 if (Terminator) 323 Out << Terminator; 324 Out << "\n"; 325 } 326 327 if (!Decls.empty()) 328 ProcessDeclGroup(Decls); 329 330 if (Indent) 331 Indentation -= Policy.Indentation; 332} 333 334void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 335 VisitDeclContext(D, false); 336} 337 338void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { 339 if (!Policy.SuppressSpecifiers) { 340 Out << "typedef "; 341 342 if (D->isModulePrivate()) 343 Out << "__module_private__ "; 344 } 345 D->getUnderlyingType().print(Out, Policy, D->getName()); 346 prettyPrintAttributes(D); 347} 348 349void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 350 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy); 351} 352 353void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 354 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 355 Out << "__module_private__ "; 356 Out << "enum "; 357 if (D->isScoped()) { 358 if (D->isScopedUsingClassTag()) 359 Out << "class "; 360 else 361 Out << "struct "; 362 } 363 Out << *D; 364 365 if (D->isFixed()) 366 Out << " : " << D->getIntegerType().stream(Policy); 367 368 if (D->isCompleteDefinition()) { 369 Out << " {\n"; 370 VisitDeclContext(D); 371 Indent() << "}"; 372 } 373 prettyPrintAttributes(D); 374} 375 376void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 377 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 378 Out << "__module_private__ "; 379 Out << D->getKindName(); 380 if (D->getIdentifier()) 381 Out << ' ' << *D; 382 383 if (D->isCompleteDefinition()) { 384 Out << " {\n"; 385 VisitDeclContext(D); 386 Indent() << "}"; 387 } 388} 389 390void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 391 Out << *D; 392 if (Expr *Init = D->getInitExpr()) { 393 Out << " = "; 394 Init->printPretty(Out, 0, Policy, Indentation); 395 } 396} 397 398void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 399 if (!Policy.SuppressSpecifiers) { 400 switch (D->getStorageClassAsWritten()) { 401 case SC_None: break; 402 case SC_Extern: Out << "extern "; break; 403 case SC_Static: Out << "static "; break; 404 case SC_PrivateExtern: Out << "__private_extern__ "; break; 405 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: 406 llvm_unreachable("invalid for functions"); 407 } 408 409 if (D->isInlineSpecified()) Out << "inline "; 410 if (D->isVirtualAsWritten()) Out << "virtual "; 411 if (D->isModulePrivate()) Out << "__module_private__ "; 412 } 413 414 PrintingPolicy SubPolicy(Policy); 415 SubPolicy.SuppressSpecifiers = false; 416 std::string Proto = D->getNameInfo().getAsString(); 417 418 QualType Ty = D->getType(); 419 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 420 Proto = '(' + Proto + ')'; 421 Ty = PT->getInnerType(); 422 } 423 424 if (isa<FunctionType>(Ty)) { 425 const FunctionType *AFT = Ty->getAs<FunctionType>(); 426 const FunctionProtoType *FT = 0; 427 if (D->hasWrittenPrototype()) 428 FT = dyn_cast<FunctionProtoType>(AFT); 429 430 Proto += "("; 431 if (FT) { 432 llvm::raw_string_ostream POut(Proto); 433 DeclPrinter ParamPrinter(POut, SubPolicy, Indentation); 434 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 435 if (i) POut << ", "; 436 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 437 } 438 439 if (FT->isVariadic()) { 440 if (D->getNumParams()) POut << ", "; 441 POut << "..."; 442 } 443 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 444 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 445 if (i) 446 Proto += ", "; 447 Proto += D->getParamDecl(i)->getNameAsString(); 448 } 449 } 450 451 Proto += ")"; 452 453 if (FT) { 454 if (FT->isConst()) 455 Proto += " const"; 456 if (FT->isVolatile()) 457 Proto += " volatile"; 458 if (FT->isRestrict()) 459 Proto += " restrict"; 460 } 461 462 if (FT && FT->hasDynamicExceptionSpec()) { 463 Proto += " throw("; 464 if (FT->getExceptionSpecType() == EST_MSAny) 465 Proto += "..."; 466 else 467 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 468 if (I) 469 Proto += ", "; 470 471 Proto += FT->getExceptionType(I).getAsString(SubPolicy); 472 } 473 Proto += ")"; 474 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 475 Proto += " noexcept"; 476 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 477 Proto += "("; 478 llvm::raw_string_ostream EOut(Proto); 479 FT->getNoexceptExpr()->printPretty(EOut, 0, SubPolicy, 480 Indentation); 481 EOut.flush(); 482 Proto += EOut.str(); 483 Proto += ")"; 484 } 485 } 486 487 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) { 488 bool HasInitializerList = false; 489 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), 490 E = CDecl->init_end(); 491 B != E; ++B) { 492 CXXCtorInitializer * BMInitializer = (*B); 493 if (BMInitializer->isInClassMemberInitializer()) 494 continue; 495 496 if (!HasInitializerList) { 497 Proto += " : "; 498 Out << Proto; 499 Proto.clear(); 500 HasInitializerList = true; 501 } else 502 Out << ", "; 503 504 if (BMInitializer->isAnyMemberInitializer()) { 505 FieldDecl *FD = BMInitializer->getAnyMember(); 506 Out << *FD; 507 } else { 508 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 509 } 510 511 Out << "("; 512 if (!BMInitializer->getInit()) { 513 // Nothing to print 514 } else { 515 Expr *Init = BMInitializer->getInit(); 516 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 517 Init = Tmp->getSubExpr(); 518 519 Init = Init->IgnoreParens(); 520 521 Expr *SimpleInit = 0; 522 Expr **Args = 0; 523 unsigned NumArgs = 0; 524 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 525 Args = ParenList->getExprs(); 526 NumArgs = ParenList->getNumExprs(); 527 } else if (CXXConstructExpr *Construct 528 = dyn_cast<CXXConstructExpr>(Init)) { 529 Args = Construct->getArgs(); 530 NumArgs = Construct->getNumArgs(); 531 } else 532 SimpleInit = Init; 533 534 if (SimpleInit) 535 SimpleInit->printPretty(Out, 0, Policy, Indentation); 536 else { 537 for (unsigned I = 0; I != NumArgs; ++I) { 538 if (isa<CXXDefaultArgExpr>(Args[I])) 539 break; 540 541 if (I) 542 Out << ", "; 543 Args[I]->printPretty(Out, 0, Policy, Indentation); 544 } 545 } 546 } 547 Out << ")"; 548 } 549 } 550 else 551 AFT->getResultType().print(Out, Policy, Proto); 552 } else { 553 Ty.print(Out, Policy, Proto); 554 } 555 556 prettyPrintAttributes(D); 557 558 if (D->isPure()) 559 Out << " = 0"; 560 else if (D->isDeletedAsWritten()) 561 Out << " = delete"; 562 else if (D->doesThisDeclarationHaveABody() && !Policy.TerseOutput) { 563 if (!D->hasPrototype() && D->getNumParams()) { 564 // This is a K&R function definition, so we need to print the 565 // parameters. 566 Out << '\n'; 567 DeclPrinter ParamPrinter(Out, SubPolicy, Indentation); 568 Indentation += Policy.Indentation; 569 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 570 Indent(); 571 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 572 Out << ";\n"; 573 } 574 Indentation -= Policy.Indentation; 575 } else 576 Out << ' '; 577 578 D->getBody()->printPretty(Out, 0, SubPolicy, Indentation); 579 Out << '\n'; 580 } 581} 582 583void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 584 if (!Policy.SuppressSpecifiers && D->isMutable()) 585 Out << "mutable "; 586 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 587 Out << "__module_private__ "; 588 589 Out << D->getType().stream(Policy, D->getName()); 590 591 if (D->isBitField()) { 592 Out << " : "; 593 D->getBitWidth()->printPretty(Out, 0, Policy, Indentation); 594 } 595 596 Expr *Init = D->getInClassInitializer(); 597 if (!Policy.SuppressInitializers && Init) { 598 if (D->getInClassInitStyle() == ICIS_ListInit) 599 Out << " "; 600 else 601 Out << " = "; 602 Init->printPretty(Out, 0, Policy, Indentation); 603 } 604 prettyPrintAttributes(D); 605} 606 607void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 608 Out << *D << ":"; 609} 610 611 612void DeclPrinter::VisitVarDecl(VarDecl *D) { 613 StorageClass SCAsWritten = D->getStorageClassAsWritten(); 614 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None) 615 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " "; 616 617 if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) 618 Out << "__thread "; 619 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 620 Out << "__module_private__ "; 621 622 QualType T = D->getType(); 623 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) 624 T = Parm->getOriginalType(); 625 T.print(Out, Policy, D->getName()); 626 Expr *Init = D->getInit(); 627 if (!Policy.SuppressInitializers && Init) { 628 bool ImplicitInit = false; 629 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) 630 ImplicitInit = D->getInitStyle() == VarDecl::CallInit && 631 Construct->getNumArgs() == 0 && !Construct->isListInitialization(); 632 if (!ImplicitInit) { 633 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 634 Out << "("; 635 else if (D->getInitStyle() == VarDecl::CInit) { 636 Out << " = "; 637 } 638 Init->printPretty(Out, 0, Policy, Indentation); 639 if ((D->getInitStyle() == VarDecl::CallInit) && !isa<ParenListExpr>(Init)) 640 Out << ")"; 641 } 642 } 643 prettyPrintAttributes(D); 644} 645 646void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 647 VisitVarDecl(D); 648} 649 650void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 651 Out << "__asm ("; 652 D->getAsmString()->printPretty(Out, 0, Policy, Indentation); 653 Out << ")"; 654} 655 656void DeclPrinter::VisitImportDecl(ImportDecl *D) { 657 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName() 658 << ";\n"; 659} 660 661void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 662 Out << "static_assert("; 663 D->getAssertExpr()->printPretty(Out, 0, Policy, Indentation); 664 Out << ", "; 665 D->getMessage()->printPretty(Out, 0, Policy, Indentation); 666 Out << ")"; 667} 668 669//---------------------------------------------------------------------------- 670// C++ declarations 671//---------------------------------------------------------------------------- 672void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 673 if (D->isInline()) 674 Out << "inline "; 675 Out << "namespace " << *D << " {\n"; 676 VisitDeclContext(D); 677 Indent() << "}"; 678} 679 680void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 681 Out << "using namespace "; 682 if (D->getQualifier()) 683 D->getQualifier()->print(Out, Policy); 684 Out << *D->getNominatedNamespaceAsWritten(); 685} 686 687void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 688 Out << "namespace " << *D << " = "; 689 if (D->getQualifier()) 690 D->getQualifier()->print(Out, Policy); 691 Out << *D->getAliasedNamespace(); 692} 693 694void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 695 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 696 Out << "__module_private__ "; 697 Out << D->getKindName(); 698 if (D->getIdentifier()) 699 Out << ' ' << *D; 700 701 if (D->isCompleteDefinition()) { 702 // Print the base classes 703 if (D->getNumBases()) { 704 Out << " : "; 705 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 706 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 707 if (Base != D->bases_begin()) 708 Out << ", "; 709 710 if (Base->isVirtual()) 711 Out << "virtual "; 712 713 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 714 if (AS != AS_none) 715 Print(AS); 716 Out << " " << Base->getType().getAsString(Policy); 717 718 if (Base->isPackExpansion()) 719 Out << "..."; 720 } 721 } 722 723 // Print the class definition 724 // FIXME: Doesn't print access specifiers, e.g., "public:" 725 Out << " {\n"; 726 VisitDeclContext(D); 727 Indent() << "}"; 728 } 729} 730 731void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 732 const char *l; 733 if (D->getLanguage() == LinkageSpecDecl::lang_c) 734 l = "C"; 735 else { 736 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 737 "unknown language in linkage specification"); 738 l = "C++"; 739 } 740 741 Out << "extern \"" << l << "\" "; 742 if (D->hasBraces()) { 743 Out << "{\n"; 744 VisitDeclContext(D); 745 Indent() << "}"; 746 } else 747 Visit(*D->decls_begin()); 748} 749 750void DeclPrinter::PrintTemplateParameters( 751 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) { 752 assert(Params); 753 assert(!Args || Params->size() == Args->size()); 754 755 Out << "template <"; 756 757 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 758 if (i != 0) 759 Out << ", "; 760 761 const Decl *Param = Params->getParam(i); 762 if (const TemplateTypeParmDecl *TTP = 763 dyn_cast<TemplateTypeParmDecl>(Param)) { 764 765 if (TTP->wasDeclaredWithTypename()) 766 Out << "typename "; 767 else 768 Out << "class "; 769 770 if (TTP->isParameterPack()) 771 Out << "... "; 772 773 Out << *TTP; 774 775 if (Args) { 776 Out << " = "; 777 Args->get(i).print(Policy, Out); 778 } else if (TTP->hasDefaultArgument()) { 779 Out << " = "; 780 Out << TTP->getDefaultArgument().getAsString(Policy); 781 }; 782 } else if (const NonTypeTemplateParmDecl *NTTP = 783 dyn_cast<NonTypeTemplateParmDecl>(Param)) { 784 Out << NTTP->getType().getAsString(Policy); 785 786 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType())) 787 Out << "..."; 788 789 if (IdentifierInfo *Name = NTTP->getIdentifier()) { 790 Out << ' '; 791 Out << Name->getName(); 792 } 793 794 if (Args) { 795 Out << " = "; 796 Args->get(i).print(Policy, Out); 797 } else if (NTTP->hasDefaultArgument()) { 798 Out << " = "; 799 NTTP->getDefaultArgument()->printPretty(Out, 0, Policy, Indentation); 800 } 801 } else if (const TemplateTemplateParmDecl *TTPD = 802 dyn_cast<TemplateTemplateParmDecl>(Param)) { 803 VisitTemplateDecl(TTPD); 804 // FIXME: print the default argument, if present. 805 } 806 } 807 808 Out << "> "; 809} 810 811void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 812 PrintTemplateParameters(D->getTemplateParameters()); 813 814 if (const TemplateTemplateParmDecl *TTP = 815 dyn_cast<TemplateTemplateParmDecl>(D)) { 816 Out << "class "; 817 if (TTP->isParameterPack()) 818 Out << "..."; 819 Out << D->getName(); 820 } else { 821 Visit(D->getTemplatedDecl()); 822 } 823} 824 825void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 826 if (PrintInstantiation) { 827 TemplateParameterList *Params = D->getTemplateParameters(); 828 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 829 I != E; ++I) { 830 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs()); 831 Visit(*I); 832 } 833 } 834 835 return VisitRedeclarableTemplateDecl(D); 836} 837 838void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 839 if (PrintInstantiation) { 840 TemplateParameterList *Params = D->getTemplateParameters(); 841 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 842 I != E; ++I) { 843 PrintTemplateParameters(Params, &(*I)->getTemplateArgs()); 844 Visit(*I); 845 Out << '\n'; 846 } 847 } 848 849 return VisitRedeclarableTemplateDecl(D); 850} 851 852//---------------------------------------------------------------------------- 853// Objective-C declarations 854//---------------------------------------------------------------------------- 855 856void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 857 if (OMD->isInstanceMethod()) 858 Out << "- "; 859 else 860 Out << "+ "; 861 if (!OMD->getResultType().isNull()) 862 Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; 863 864 std::string name = OMD->getSelector().getAsString(); 865 std::string::size_type pos, lastPos = 0; 866 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), 867 E = OMD->param_end(); PI != E; ++PI) { 868 // FIXME: selector is missing here! 869 pos = name.find_first_of(':', lastPos); 870 Out << " " << name.substr(lastPos, pos - lastPos); 871 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI; 872 lastPos = pos + 1; 873 } 874 875 if (OMD->param_begin() == OMD->param_end()) 876 Out << " " << name; 877 878 if (OMD->isVariadic()) 879 Out << ", ..."; 880 881 if (OMD->getBody() && !Policy.TerseOutput) { 882 Out << ' '; 883 OMD->getBody()->printPretty(Out, 0, Policy); 884 Out << '\n'; 885 } 886} 887 888void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 889 std::string I = OID->getNameAsString(); 890 ObjCInterfaceDecl *SID = OID->getSuperClass(); 891 892 if (SID) 893 Out << "@implementation " << I << " : " << *SID; 894 else 895 Out << "@implementation " << I; 896 897 if (OID->ivar_size() > 0) { 898 Out << "{\n"; 899 Indentation += Policy.Indentation; 900 for (ObjCImplementationDecl::ivar_iterator I = OID->ivar_begin(), 901 E = OID->ivar_end(); I != E; ++I) { 902 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; 903 } 904 Indentation -= Policy.Indentation; 905 Out << "}\n"; 906 } 907 VisitDeclContext(OID, false); 908 Out << "@end"; 909} 910 911void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 912 std::string I = OID->getNameAsString(); 913 ObjCInterfaceDecl *SID = OID->getSuperClass(); 914 915 if (!OID->isThisDeclarationADefinition()) { 916 Out << "@class " << I << ";"; 917 return; 918 } 919 920 if (SID) 921 Out << "@interface " << I << " : " << *SID; 922 else 923 Out << "@interface " << I; 924 925 // Protocols? 926 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 927 if (!Protocols.empty()) { 928 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 929 E = Protocols.end(); I != E; ++I) 930 Out << (I == Protocols.begin() ? '<' : ',') << **I; 931 } 932 933 if (!Protocols.empty()) 934 Out << "> "; 935 936 if (OID->ivar_size() > 0) { 937 Out << "{\n"; 938 Indentation += Policy.Indentation; 939 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), 940 E = OID->ivar_end(); I != E; ++I) { 941 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; 942 } 943 Indentation -= Policy.Indentation; 944 Out << "}\n"; 945 } 946 947 VisitDeclContext(OID, false); 948 Out << "@end"; 949 // FIXME: implement the rest... 950} 951 952void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 953 if (!PID->isThisDeclarationADefinition()) { 954 Out << "@protocol " << PID->getIdentifier() << ";\n"; 955 return; 956 } 957 958 Out << "@protocol " << *PID << '\n'; 959 VisitDeclContext(PID, false); 960 Out << "@end"; 961} 962 963void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 964 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 965 966 VisitDeclContext(PID, false); 967 Out << "@end"; 968 // FIXME: implement the rest... 969} 970 971void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 972 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n"; 973 if (PID->ivar_size() > 0) { 974 Out << "{\n"; 975 Indentation += Policy.Indentation; 976 for (ObjCCategoryDecl::ivar_iterator I = PID->ivar_begin(), 977 E = PID->ivar_end(); I != E; ++I) { 978 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; 979 } 980 Indentation -= Policy.Indentation; 981 Out << "}\n"; 982 } 983 984 VisitDeclContext(PID, false); 985 Out << "@end"; 986 987 // FIXME: implement the rest... 988} 989 990void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 991 Out << "@compatibility_alias " << *AID 992 << ' ' << *AID->getClassInterface() << ";\n"; 993} 994 995/// PrintObjCPropertyDecl - print a property declaration. 996/// 997void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 998 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 999 Out << "@required\n"; 1000 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1001 Out << "@optional\n"; 1002 1003 Out << "@property"; 1004 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 1005 bool first = true; 1006 Out << " ("; 1007 if (PDecl->getPropertyAttributes() & 1008 ObjCPropertyDecl::OBJC_PR_readonly) { 1009 Out << (first ? ' ' : ',') << "readonly"; 1010 first = false; 1011 } 1012 1013 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 1014 Out << (first ? ' ' : ',') << "getter = " 1015 << PDecl->getGetterName().getAsString(); 1016 first = false; 1017 } 1018 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 1019 Out << (first ? ' ' : ',') << "setter = " 1020 << PDecl->getSetterName().getAsString(); 1021 first = false; 1022 } 1023 1024 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 1025 Out << (first ? ' ' : ',') << "assign"; 1026 first = false; 1027 } 1028 1029 if (PDecl->getPropertyAttributes() & 1030 ObjCPropertyDecl::OBJC_PR_readwrite) { 1031 Out << (first ? ' ' : ',') << "readwrite"; 1032 first = false; 1033 } 1034 1035 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 1036 Out << (first ? ' ' : ',') << "retain"; 1037 first = false; 1038 } 1039 1040 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 1041 Out << (first ? ' ' : ',') << "strong"; 1042 first = false; 1043 } 1044 1045 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 1046 Out << (first ? ' ' : ',') << "copy"; 1047 first = false; 1048 } 1049 1050 if (PDecl->getPropertyAttributes() & 1051 ObjCPropertyDecl::OBJC_PR_nonatomic) { 1052 Out << (first ? ' ' : ',') << "nonatomic"; 1053 first = false; 1054 } 1055 if (PDecl->getPropertyAttributes() & 1056 ObjCPropertyDecl::OBJC_PR_atomic) { 1057 Out << (first ? ' ' : ',') << "atomic"; 1058 first = false; 1059 } 1060 1061 (void) first; // Silence dead store warning due to idiomatic code. 1062 Out << " )"; 1063 } 1064 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl; 1065} 1066 1067void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1068 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1069 Out << "@synthesize "; 1070 else 1071 Out << "@dynamic "; 1072 Out << *PID->getPropertyDecl(); 1073 if (PID->getPropertyIvarDecl()) 1074 Out << '=' << *PID->getPropertyIvarDecl(); 1075} 1076 1077void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1078 Out << "using "; 1079 D->getQualifier()->print(Out, Policy); 1080 Out << *D; 1081} 1082 1083void 1084DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1085 Out << "using typename "; 1086 D->getQualifier()->print(Out, Policy); 1087 Out << D->getDeclName(); 1088} 1089 1090void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1091 Out << "using "; 1092 D->getQualifier()->print(Out, Policy); 1093 Out << D->getDeclName(); 1094} 1095 1096void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1097 // ignore 1098} 1099