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