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