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