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