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" 24 using namespace clang; 25 26 namespace { 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 92 void Decl::print(raw_ostream &Out, unsigned Indentation, 93 bool PrintInstantiation) const { 94 print(Out, getASTContext().getPrintingPolicy(), Indentation, PrintInstantiation); 95 } 96 97 void 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 103 static 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 123 static 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 131 void 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 165 void 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 176 void Decl::dump() const { 177 print(llvm::errs()); 178 } 179 180 raw_ostream& DeclPrinter::Indent(unsigned Indentation) { 181 for (unsigned i = 0; i != Indentation; ++i) 182 Out << " "; 183 return Out; 184 } 185 186 void 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 196 void 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 204 void 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 217 void 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 320 void DeclPrinter::VisitTranslationUnitDecl(TranslationUnitDecl *D) { 321 VisitDeclContext(D, false); 322 } 323 324 void DeclPrinter::VisitTypedefDecl(TypedefDecl *D) { 325 if (!Policy.SuppressSpecifiers) { 326 Out << "typedef "; 327 328 if (D->isModulePrivate()) 329 Out << "__module_private__ "; 330 } 331 D->getUnderlyingType().print(Out, Policy, D->getName()); 332 prettyPrintAttributes(D); 333 } 334 335 void DeclPrinter::VisitTypeAliasDecl(TypeAliasDecl *D) { 336 Out << "using " << *D << " = " << D->getUnderlyingType().getAsString(Policy); 337 } 338 339 void DeclPrinter::VisitEnumDecl(EnumDecl *D) { 340 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 341 Out << "__module_private__ "; 342 Out << "enum "; 343 if (D->isScoped()) { 344 if (D->isScopedUsingClassTag()) 345 Out << "class "; 346 else 347 Out << "struct "; 348 } 349 Out << *D; 350 351 if (D->isFixed()) 352 Out << " : " << D->getIntegerType().stream(Policy); 353 354 if (D->isCompleteDefinition()) { 355 Out << " {\n"; 356 VisitDeclContext(D); 357 Indent() << "}"; 358 } 359 prettyPrintAttributes(D); 360 } 361 362 void DeclPrinter::VisitRecordDecl(RecordDecl *D) { 363 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 364 Out << "__module_private__ "; 365 Out << D->getKindName(); 366 if (D->getIdentifier()) 367 Out << ' ' << *D; 368 369 if (D->isCompleteDefinition()) { 370 Out << " {\n"; 371 VisitDeclContext(D); 372 Indent() << "}"; 373 } 374 } 375 376 void DeclPrinter::VisitEnumConstantDecl(EnumConstantDecl *D) { 377 Out << *D; 378 if (Expr *Init = D->getInitExpr()) { 379 Out << " = "; 380 Init->printPretty(Out, Context, 0, Policy, Indentation); 381 } 382 } 383 384 void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { 385 if (!Policy.SuppressSpecifiers) { 386 switch (D->getStorageClassAsWritten()) { 387 case SC_None: break; 388 case SC_Extern: Out << "extern "; break; 389 case SC_Static: Out << "static "; break; 390 case SC_PrivateExtern: Out << "__private_extern__ "; break; 391 case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: 392 llvm_unreachable("invalid for functions"); 393 } 394 395 if (D->isInlineSpecified()) Out << "inline "; 396 if (D->isVirtualAsWritten()) Out << "virtual "; 397 if (D->isModulePrivate()) Out << "__module_private__ "; 398 } 399 400 PrintingPolicy SubPolicy(Policy); 401 SubPolicy.SuppressSpecifiers = false; 402 std::string Proto = D->getNameInfo().getAsString(); 403 404 QualType Ty = D->getType(); 405 while (const ParenType *PT = dyn_cast<ParenType>(Ty)) { 406 Proto = '(' + Proto + ')'; 407 Ty = PT->getInnerType(); 408 } 409 410 if (isa<FunctionType>(Ty)) { 411 const FunctionType *AFT = Ty->getAs<FunctionType>(); 412 const FunctionProtoType *FT = 0; 413 if (D->hasWrittenPrototype()) 414 FT = dyn_cast<FunctionProtoType>(AFT); 415 416 Proto += "("; 417 if (FT) { 418 llvm::raw_string_ostream POut(Proto); 419 DeclPrinter ParamPrinter(POut, Context, SubPolicy, Indentation); 420 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 421 if (i) POut << ", "; 422 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 423 } 424 425 if (FT->isVariadic()) { 426 if (D->getNumParams()) POut << ", "; 427 POut << "..."; 428 } 429 } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) { 430 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 431 if (i) 432 Proto += ", "; 433 Proto += D->getParamDecl(i)->getNameAsString(); 434 } 435 } 436 437 Proto += ")"; 438 439 if (FT && FT->getTypeQuals()) { 440 unsigned TypeQuals = FT->getTypeQuals(); 441 if (TypeQuals & Qualifiers::Const) 442 Proto += " const"; 443 if (TypeQuals & Qualifiers::Volatile) 444 Proto += " volatile"; 445 if (TypeQuals & Qualifiers::Restrict) 446 Proto += " restrict"; 447 } 448 449 if (FT && FT->hasDynamicExceptionSpec()) { 450 Proto += " throw("; 451 if (FT->getExceptionSpecType() == EST_MSAny) 452 Proto += "..."; 453 else 454 for (unsigned I = 0, N = FT->getNumExceptions(); I != N; ++I) { 455 if (I) 456 Proto += ", "; 457 458 Proto += FT->getExceptionType(I).getAsString(SubPolicy);; 459 } 460 Proto += ")"; 461 } else if (FT && isNoexceptExceptionSpec(FT->getExceptionSpecType())) { 462 Proto += " noexcept"; 463 if (FT->getExceptionSpecType() == EST_ComputedNoexcept) { 464 Proto += "("; 465 llvm::raw_string_ostream EOut(Proto); 466 FT->getNoexceptExpr()->printPretty(EOut, Context, 0, SubPolicy, 467 Indentation); 468 EOut.flush(); 469 Proto += EOut.str(); 470 Proto += ")"; 471 } 472 } 473 474 if (CXXConstructorDecl *CDecl = dyn_cast<CXXConstructorDecl>(D)) { 475 bool HasInitializerList = false; 476 for (CXXConstructorDecl::init_const_iterator B = CDecl->init_begin(), 477 E = CDecl->init_end(); 478 B != E; ++B) { 479 CXXCtorInitializer * BMInitializer = (*B); 480 if (BMInitializer->isInClassMemberInitializer()) 481 continue; 482 483 if (!HasInitializerList) { 484 Proto += " : "; 485 Out << Proto; 486 Proto.clear(); 487 HasInitializerList = true; 488 } else 489 Out << ", "; 490 491 if (BMInitializer->isAnyMemberInitializer()) { 492 FieldDecl *FD = BMInitializer->getAnyMember(); 493 Out << *FD; 494 } else { 495 Out << QualType(BMInitializer->getBaseClass(), 0).getAsString(Policy); 496 } 497 498 Out << "("; 499 if (!BMInitializer->getInit()) { 500 // Nothing to print 501 } else { 502 Expr *Init = BMInitializer->getInit(); 503 if (ExprWithCleanups *Tmp = dyn_cast<ExprWithCleanups>(Init)) 504 Init = Tmp->getSubExpr(); 505 506 Init = Init->IgnoreParens(); 507 508 Expr *SimpleInit = 0; 509 Expr **Args = 0; 510 unsigned NumArgs = 0; 511 if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { 512 Args = ParenList->getExprs(); 513 NumArgs = ParenList->getNumExprs(); 514 } else if (CXXConstructExpr *Construct 515 = dyn_cast<CXXConstructExpr>(Init)) { 516 Args = Construct->getArgs(); 517 NumArgs = Construct->getNumArgs(); 518 } else 519 SimpleInit = Init; 520 521 if (SimpleInit) 522 SimpleInit->printPretty(Out, Context, 0, Policy, Indentation); 523 else { 524 for (unsigned I = 0; I != NumArgs; ++I) { 525 if (isa<CXXDefaultArgExpr>(Args[I])) 526 break; 527 528 if (I) 529 Out << ", "; 530 Args[I]->printPretty(Out, Context, 0, Policy, Indentation); 531 } 532 } 533 } 534 Out << ")"; 535 } 536 } 537 else 538 AFT->getResultType().print(Out, Policy, Proto); 539 } else { 540 Ty.print(Out, Policy, Proto); 541 } 542 543 prettyPrintAttributes(D); 544 545 if (D->isPure()) 546 Out << " = 0"; 547 else if (D->isDeletedAsWritten()) 548 Out << " = delete"; 549 else if (D->doesThisDeclarationHaveABody()) { 550 if (!D->hasPrototype() && D->getNumParams()) { 551 // This is a K&R function definition, so we need to print the 552 // parameters. 553 Out << '\n'; 554 DeclPrinter ParamPrinter(Out, Context, SubPolicy, Indentation); 555 Indentation += Policy.Indentation; 556 for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) { 557 Indent(); 558 ParamPrinter.VisitParmVarDecl(D->getParamDecl(i)); 559 Out << ";\n"; 560 } 561 Indentation -= Policy.Indentation; 562 } else 563 Out << ' '; 564 565 D->getBody()->printPretty(Out, Context, 0, SubPolicy, Indentation); 566 Out << '\n'; 567 } 568 } 569 570 void DeclPrinter::VisitFieldDecl(FieldDecl *D) { 571 if (!Policy.SuppressSpecifiers && D->isMutable()) 572 Out << "mutable "; 573 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 574 Out << "__module_private__ "; 575 576 Out << D->getType().stream(Policy, D->getName()); 577 578 if (D->isBitField()) { 579 Out << " : "; 580 D->getBitWidth()->printPretty(Out, Context, 0, Policy, Indentation); 581 } 582 583 Expr *Init = D->getInClassInitializer(); 584 if (!Policy.SuppressInitializers && Init) { 585 if (D->getInClassInitStyle() == ICIS_ListInit) 586 Out << " "; 587 else 588 Out << " = "; 589 Init->printPretty(Out, Context, 0, Policy, Indentation); 590 } 591 prettyPrintAttributes(D); 592 } 593 594 void DeclPrinter::VisitLabelDecl(LabelDecl *D) { 595 Out << *D << ":"; 596 } 597 598 599 void DeclPrinter::VisitVarDecl(VarDecl *D) { 600 StorageClass SCAsWritten = D->getStorageClassAsWritten(); 601 if (!Policy.SuppressSpecifiers && SCAsWritten != SC_None) 602 Out << VarDecl::getStorageClassSpecifierString(SCAsWritten) << " "; 603 604 if (!Policy.SuppressSpecifiers && D->isThreadSpecified()) 605 Out << "__thread "; 606 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 607 Out << "__module_private__ "; 608 609 QualType T = D->getType(); 610 if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) 611 T = Parm->getOriginalType(); 612 T.print(Out, Policy, D->getName()); 613 Expr *Init = D->getInit(); 614 if (!Policy.SuppressInitializers && Init) { 615 bool ImplicitInit = false; 616 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) 617 ImplicitInit = D->getInitStyle() == VarDecl::CallInit && 618 Construct->getNumArgs() == 0 && !Construct->isListInitialization(); 619 if (!ImplicitInit) { 620 if (D->getInitStyle() == VarDecl::CallInit) 621 Out << "("; 622 else if (D->getInitStyle() == VarDecl::CInit) { 623 Out << " = "; 624 } 625 Init->printPretty(Out, Context, 0, Policy, Indentation); 626 if (D->getInitStyle() == VarDecl::CallInit) 627 Out << ")"; 628 } 629 } 630 prettyPrintAttributes(D); 631 } 632 633 void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) { 634 VisitVarDecl(D); 635 } 636 637 void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { 638 Out << "__asm ("; 639 D->getAsmString()->printPretty(Out, Context, 0, Policy, Indentation); 640 Out << ")"; 641 } 642 643 void DeclPrinter::VisitImportDecl(ImportDecl *D) { 644 Out << "@__experimental_modules_import " << D->getImportedModule()->getFullModuleName() 645 << ";\n"; 646 } 647 648 void DeclPrinter::VisitStaticAssertDecl(StaticAssertDecl *D) { 649 Out << "static_assert("; 650 D->getAssertExpr()->printPretty(Out, Context, 0, Policy, Indentation); 651 Out << ", "; 652 D->getMessage()->printPretty(Out, Context, 0, Policy, Indentation); 653 Out << ")"; 654 } 655 656 //---------------------------------------------------------------------------- 657 // C++ declarations 658 //---------------------------------------------------------------------------- 659 void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { 660 if (D->isInline()) 661 Out << "inline "; 662 Out << "namespace " << *D << " {\n"; 663 VisitDeclContext(D); 664 Indent() << "}"; 665 } 666 667 void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { 668 Out << "using namespace "; 669 if (D->getQualifier()) 670 D->getQualifier()->print(Out, Policy); 671 Out << *D->getNominatedNamespaceAsWritten(); 672 } 673 674 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { 675 Out << "namespace " << *D << " = "; 676 if (D->getQualifier()) 677 D->getQualifier()->print(Out, Policy); 678 Out << *D->getAliasedNamespace(); 679 } 680 681 void DeclPrinter::VisitCXXRecordDecl(CXXRecordDecl *D) { 682 if (!Policy.SuppressSpecifiers && D->isModulePrivate()) 683 Out << "__module_private__ "; 684 Out << D->getKindName(); 685 if (D->getIdentifier()) 686 Out << ' ' << *D; 687 688 if (D->isCompleteDefinition()) { 689 // Print the base classes 690 if (D->getNumBases()) { 691 Out << " : "; 692 for (CXXRecordDecl::base_class_iterator Base = D->bases_begin(), 693 BaseEnd = D->bases_end(); Base != BaseEnd; ++Base) { 694 if (Base != D->bases_begin()) 695 Out << ", "; 696 697 if (Base->isVirtual()) 698 Out << "virtual "; 699 700 AccessSpecifier AS = Base->getAccessSpecifierAsWritten(); 701 if (AS != AS_none) 702 Print(AS); 703 Out << " " << Base->getType().getAsString(Policy); 704 705 if (Base->isPackExpansion()) 706 Out << "..."; 707 } 708 } 709 710 // Print the class definition 711 // FIXME: Doesn't print access specifiers, e.g., "public:" 712 Out << " {\n"; 713 VisitDeclContext(D); 714 Indent() << "}"; 715 } 716 } 717 718 void DeclPrinter::VisitLinkageSpecDecl(LinkageSpecDecl *D) { 719 const char *l; 720 if (D->getLanguage() == LinkageSpecDecl::lang_c) 721 l = "C"; 722 else { 723 assert(D->getLanguage() == LinkageSpecDecl::lang_cxx && 724 "unknown language in linkage specification"); 725 l = "C++"; 726 } 727 728 Out << "extern \"" << l << "\" "; 729 if (D->hasBraces()) { 730 Out << "{\n"; 731 VisitDeclContext(D); 732 Indent() << "}"; 733 } else 734 Visit(*D->decls_begin()); 735 } 736 737 void DeclPrinter::PrintTemplateParameters( 738 const TemplateParameterList *Params, const TemplateArgumentList *Args = 0) { 739 assert(Params); 740 assert(!Args || Params->size() == Args->size()); 741 742 Out << "template <"; 743 744 for (unsigned i = 0, e = Params->size(); i != e; ++i) { 745 if (i != 0) 746 Out << ", "; 747 748 const Decl *Param = Params->getParam(i); 749 if (const TemplateTypeParmDecl *TTP = 750 dyn_cast<TemplateTypeParmDecl>(Param)) { 751 752 if (TTP->wasDeclaredWithTypename()) 753 Out << "typename "; 754 else 755 Out << "class "; 756 757 if (TTP->isParameterPack()) 758 Out << "... "; 759 760 Out << *TTP; 761 762 if (Args) { 763 Out << " = "; 764 Args->get(i).print(Policy, Out); 765 } else if (TTP->hasDefaultArgument()) { 766 Out << " = "; 767 Out << TTP->getDefaultArgument().getAsString(Policy); 768 }; 769 } else if (const NonTypeTemplateParmDecl *NTTP = 770 dyn_cast<NonTypeTemplateParmDecl>(Param)) { 771 Out << NTTP->getType().getAsString(Policy); 772 773 if (NTTP->isParameterPack() && !isa<PackExpansionType>(NTTP->getType())) 774 Out << "..."; 775 776 if (IdentifierInfo *Name = NTTP->getIdentifier()) { 777 Out << ' '; 778 Out << Name->getName(); 779 } 780 781 if (Args) { 782 Out << " = "; 783 Args->get(i).print(Policy, Out); 784 } else if (NTTP->hasDefaultArgument()) { 785 Out << " = "; 786 NTTP->getDefaultArgument()->printPretty(Out, Context, 0, Policy, 787 Indentation); 788 } 789 } else if (const TemplateTemplateParmDecl *TTPD = 790 dyn_cast<TemplateTemplateParmDecl>(Param)) { 791 VisitTemplateDecl(TTPD); 792 // FIXME: print the default argument, if present. 793 } 794 } 795 796 Out << "> "; 797 } 798 799 void DeclPrinter::VisitTemplateDecl(const TemplateDecl *D) { 800 PrintTemplateParameters(D->getTemplateParameters()); 801 802 if (const TemplateTemplateParmDecl *TTP = 803 dyn_cast<TemplateTemplateParmDecl>(D)) { 804 Out << "class "; 805 if (TTP->isParameterPack()) 806 Out << "..."; 807 Out << D->getName(); 808 } else { 809 Visit(D->getTemplatedDecl()); 810 } 811 } 812 813 void DeclPrinter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { 814 if (PrintInstantiation) { 815 TemplateParameterList *Params = D->getTemplateParameters(); 816 for (FunctionTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 817 I != E; ++I) { 818 PrintTemplateParameters(Params, (*I)->getTemplateSpecializationArgs()); 819 Visit(*I); 820 } 821 } 822 823 return VisitRedeclarableTemplateDecl(D); 824 } 825 826 void DeclPrinter::VisitClassTemplateDecl(ClassTemplateDecl *D) { 827 if (PrintInstantiation) { 828 TemplateParameterList *Params = D->getTemplateParameters(); 829 for (ClassTemplateDecl::spec_iterator I = D->spec_begin(), E = D->spec_end(); 830 I != E; ++I) { 831 PrintTemplateParameters(Params, &(*I)->getTemplateArgs()); 832 Visit(*I); 833 Out << '\n'; 834 } 835 } 836 837 return VisitRedeclarableTemplateDecl(D); 838 } 839 840 //---------------------------------------------------------------------------- 841 // Objective-C declarations 842 //---------------------------------------------------------------------------- 843 844 void DeclPrinter::VisitObjCMethodDecl(ObjCMethodDecl *OMD) { 845 if (OMD->isInstanceMethod()) 846 Out << "- "; 847 else 848 Out << "+ "; 849 if (!OMD->getResultType().isNull()) 850 Out << '(' << OMD->getResultType().getAsString(Policy) << ")"; 851 852 std::string name = OMD->getSelector().getAsString(); 853 std::string::size_type pos, lastPos = 0; 854 for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), 855 E = OMD->param_end(); PI != E; ++PI) { 856 // FIXME: selector is missing here! 857 pos = name.find_first_of(':', lastPos); 858 Out << " " << name.substr(lastPos, pos - lastPos); 859 Out << ":(" << (*PI)->getType().getAsString(Policy) << ')' << **PI; 860 lastPos = pos + 1; 861 } 862 863 if (OMD->param_begin() == OMD->param_end()) 864 Out << " " << name; 865 866 if (OMD->isVariadic()) 867 Out << ", ..."; 868 869 if (OMD->getBody()) { 870 Out << ' '; 871 OMD->getBody()->printPretty(Out, Context, 0, Policy); 872 Out << '\n'; 873 } 874 } 875 876 void DeclPrinter::VisitObjCImplementationDecl(ObjCImplementationDecl *OID) { 877 std::string I = OID->getNameAsString(); 878 ObjCInterfaceDecl *SID = OID->getSuperClass(); 879 880 if (SID) 881 Out << "@implementation " << I << " : " << *SID; 882 else 883 Out << "@implementation " << I; 884 Out << "\n"; 885 VisitDeclContext(OID, false); 886 Out << "@end"; 887 } 888 889 void DeclPrinter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *OID) { 890 std::string I = OID->getNameAsString(); 891 ObjCInterfaceDecl *SID = OID->getSuperClass(); 892 893 if (!OID->isThisDeclarationADefinition()) { 894 Out << "@class " << I << ";"; 895 return; 896 } 897 898 if (SID) 899 Out << "@interface " << I << " : " << *SID; 900 else 901 Out << "@interface " << I; 902 903 // Protocols? 904 const ObjCList<ObjCProtocolDecl> &Protocols = OID->getReferencedProtocols(); 905 if (!Protocols.empty()) { 906 for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), 907 E = Protocols.end(); I != E; ++I) 908 Out << (I == Protocols.begin() ? '<' : ',') << **I; 909 } 910 911 if (!Protocols.empty()) 912 Out << "> "; 913 914 if (OID->ivar_size() > 0) { 915 Out << "{\n"; 916 Indentation += Policy.Indentation; 917 for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), 918 E = OID->ivar_end(); I != E; ++I) { 919 Indent() << I->getType().getAsString(Policy) << ' ' << **I << ";\n"; 920 } 921 Indentation -= Policy.Indentation; 922 Out << "}\n"; 923 } 924 925 VisitDeclContext(OID, false); 926 Out << "@end"; 927 // FIXME: implement the rest... 928 } 929 930 void DeclPrinter::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) { 931 if (!PID->isThisDeclarationADefinition()) { 932 Out << "@protocol " << PID->getIdentifier() << ";\n"; 933 return; 934 } 935 936 Out << "@protocol " << *PID << '\n'; 937 VisitDeclContext(PID, false); 938 Out << "@end"; 939 } 940 941 void DeclPrinter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *PID) { 942 Out << "@implementation " << *PID->getClassInterface() << '(' << *PID <<")\n"; 943 944 VisitDeclContext(PID, false); 945 Out << "@end"; 946 // FIXME: implement the rest... 947 } 948 949 void DeclPrinter::VisitObjCCategoryDecl(ObjCCategoryDecl *PID) { 950 Out << "@interface " << *PID->getClassInterface() << '(' << *PID << ")\n"; 951 VisitDeclContext(PID, false); 952 Out << "@end"; 953 954 // FIXME: implement the rest... 955 } 956 957 void DeclPrinter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *AID) { 958 Out << "@compatibility_alias " << *AID 959 << ' ' << *AID->getClassInterface() << ";\n"; 960 } 961 962 /// PrintObjCPropertyDecl - print a property declaration. 963 /// 964 void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) { 965 if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Required) 966 Out << "@required\n"; 967 else if (PDecl->getPropertyImplementation() == ObjCPropertyDecl::Optional) 968 Out << "@optional\n"; 969 970 Out << "@property"; 971 if (PDecl->getPropertyAttributes() != ObjCPropertyDecl::OBJC_PR_noattr) { 972 bool first = true; 973 Out << " ("; 974 if (PDecl->getPropertyAttributes() & 975 ObjCPropertyDecl::OBJC_PR_readonly) { 976 Out << (first ? ' ' : ',') << "readonly"; 977 first = false; 978 } 979 980 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { 981 Out << (first ? ' ' : ',') << "getter = " 982 << PDecl->getGetterName().getAsString(); 983 first = false; 984 } 985 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { 986 Out << (first ? ' ' : ',') << "setter = " 987 << PDecl->getSetterName().getAsString(); 988 first = false; 989 } 990 991 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_assign) { 992 Out << (first ? ' ' : ',') << "assign"; 993 first = false; 994 } 995 996 if (PDecl->getPropertyAttributes() & 997 ObjCPropertyDecl::OBJC_PR_readwrite) { 998 Out << (first ? ' ' : ',') << "readwrite"; 999 first = false; 1000 } 1001 1002 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) { 1003 Out << (first ? ' ' : ',') << "retain"; 1004 first = false; 1005 } 1006 1007 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_strong) { 1008 Out << (first ? ' ' : ',') << "strong"; 1009 first = false; 1010 } 1011 1012 if (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) { 1013 Out << (first ? ' ' : ',') << "copy"; 1014 first = false; 1015 } 1016 1017 if (PDecl->getPropertyAttributes() & 1018 ObjCPropertyDecl::OBJC_PR_nonatomic) { 1019 Out << (first ? ' ' : ',') << "nonatomic"; 1020 first = false; 1021 } 1022 if (PDecl->getPropertyAttributes() & 1023 ObjCPropertyDecl::OBJC_PR_atomic) { 1024 Out << (first ? ' ' : ',') << "atomic"; 1025 first = false; 1026 } 1027 1028 (void) first; // Silence dead store warning due to idiomatic code. 1029 Out << " )"; 1030 } 1031 Out << ' ' << PDecl->getType().getAsString(Policy) << ' ' << *PDecl; 1032 } 1033 1034 void DeclPrinter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *PID) { 1035 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1036 Out << "@synthesize "; 1037 else 1038 Out << "@dynamic "; 1039 Out << *PID->getPropertyDecl(); 1040 if (PID->getPropertyIvarDecl()) 1041 Out << '=' << *PID->getPropertyIvarDecl(); 1042 } 1043 1044 void DeclPrinter::VisitUsingDecl(UsingDecl *D) { 1045 Out << "using "; 1046 D->getQualifier()->print(Out, Policy); 1047 Out << *D; 1048 } 1049 1050 void 1051 DeclPrinter::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { 1052 Out << "using typename "; 1053 D->getQualifier()->print(Out, Policy); 1054 Out << D->getDeclName(); 1055 } 1056 1057 void DeclPrinter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 1058 Out << "using "; 1059 D->getQualifier()->print(Out, Policy); 1060 Out << D->getDeclName(); 1061 } 1062 1063 void DeclPrinter::VisitUsingShadowDecl(UsingShadowDecl *D) { 1064 // ignore 1065 } 1066