1 //===--- TextNodeDumper.cpp - Printing of AST nodes -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements AST dumping of components of individual AST nodes. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/AST/TextNodeDumper.h" 14 #include "clang/AST/DeclFriend.h" 15 #include "clang/AST/DeclOpenMP.h" 16 #include "clang/AST/DeclTemplate.h" 17 #include "clang/AST/LocInfoType.h" 18 19 using namespace clang; 20 21 static void dumpPreviousDeclImpl(raw_ostream &OS, ...) {} 22 23 template <typename T> 24 static void dumpPreviousDeclImpl(raw_ostream &OS, const Mergeable<T> *D) { 25 const T *First = D->getFirstDecl(); 26 if (First != D) 27 OS << " first " << First; 28 } 29 30 template <typename T> 31 static void dumpPreviousDeclImpl(raw_ostream &OS, const Redeclarable<T> *D) { 32 const T *Prev = D->getPreviousDecl(); 33 if (Prev) 34 OS << " prev " << Prev; 35 } 36 37 /// Dump the previous declaration in the redeclaration chain for a declaration, 38 /// if any. 39 static void dumpPreviousDecl(raw_ostream &OS, const Decl *D) { 40 switch (D->getKind()) { 41 #define DECL(DERIVED, BASE) \ 42 case Decl::DERIVED: \ 43 return dumpPreviousDeclImpl(OS, cast<DERIVED##Decl>(D)); 44 #define ABSTRACT_DECL(DECL) 45 #include "clang/AST/DeclNodes.inc" 46 } 47 llvm_unreachable("Decl that isn't part of DeclNodes.inc!"); 48 } 49 50 TextNodeDumper::TextNodeDumper(raw_ostream &OS, bool ShowColors, 51 const SourceManager *SM, 52 const PrintingPolicy &PrintPolicy, 53 const comments::CommandTraits *Traits) 54 : TextTreeStructure(OS, ShowColors), OS(OS), ShowColors(ShowColors), SM(SM), 55 PrintPolicy(PrintPolicy), Traits(Traits) {} 56 57 void TextNodeDumper::Visit(const comments::Comment *C, 58 const comments::FullComment *FC) { 59 if (!C) { 60 ColorScope Color(OS, ShowColors, NullColor); 61 OS << "<<<NULL>>>"; 62 return; 63 } 64 65 { 66 ColorScope Color(OS, ShowColors, CommentColor); 67 OS << C->getCommentKindName(); 68 } 69 dumpPointer(C); 70 dumpSourceRange(C->getSourceRange()); 71 72 ConstCommentVisitor<TextNodeDumper, void, 73 const comments::FullComment *>::visit(C, FC); 74 } 75 76 void TextNodeDumper::Visit(const Attr *A) { 77 { 78 ColorScope Color(OS, ShowColors, AttrColor); 79 80 switch (A->getKind()) { 81 #define ATTR(X) \ 82 case attr::X: \ 83 OS << #X; \ 84 break; 85 #include "clang/Basic/AttrList.inc" 86 } 87 OS << "Attr"; 88 } 89 dumpPointer(A); 90 dumpSourceRange(A->getRange()); 91 if (A->isInherited()) 92 OS << " Inherited"; 93 if (A->isImplicit()) 94 OS << " Implicit"; 95 96 ConstAttrVisitor<TextNodeDumper>::Visit(A); 97 } 98 99 void TextNodeDumper::Visit(const TemplateArgument &TA, SourceRange R, 100 const Decl *From, StringRef Label) { 101 OS << "TemplateArgument"; 102 if (R.isValid()) 103 dumpSourceRange(R); 104 105 if (From) 106 dumpDeclRef(From, Label); 107 108 ConstTemplateArgumentVisitor<TextNodeDumper>::Visit(TA); 109 } 110 111 void TextNodeDumper::Visit(const Stmt *Node) { 112 if (!Node) { 113 ColorScope Color(OS, ShowColors, NullColor); 114 OS << "<<<NULL>>>"; 115 return; 116 } 117 { 118 ColorScope Color(OS, ShowColors, StmtColor); 119 OS << Node->getStmtClassName(); 120 } 121 dumpPointer(Node); 122 dumpSourceRange(Node->getSourceRange()); 123 124 if (Node->isOMPStructuredBlock()) 125 OS << " openmp_structured_block"; 126 127 if (const auto *E = dyn_cast<Expr>(Node)) { 128 dumpType(E->getType()); 129 130 { 131 ColorScope Color(OS, ShowColors, ValueKindColor); 132 switch (E->getValueKind()) { 133 case VK_RValue: 134 break; 135 case VK_LValue: 136 OS << " lvalue"; 137 break; 138 case VK_XValue: 139 OS << " xvalue"; 140 break; 141 } 142 } 143 144 { 145 ColorScope Color(OS, ShowColors, ObjectKindColor); 146 switch (E->getObjectKind()) { 147 case OK_Ordinary: 148 break; 149 case OK_BitField: 150 OS << " bitfield"; 151 break; 152 case OK_ObjCProperty: 153 OS << " objcproperty"; 154 break; 155 case OK_ObjCSubscript: 156 OS << " objcsubscript"; 157 break; 158 case OK_VectorComponent: 159 OS << " vectorcomponent"; 160 break; 161 } 162 } 163 } 164 165 ConstStmtVisitor<TextNodeDumper>::Visit(Node); 166 } 167 168 void TextNodeDumper::Visit(const Type *T) { 169 if (!T) { 170 ColorScope Color(OS, ShowColors, NullColor); 171 OS << "<<<NULL>>>"; 172 return; 173 } 174 if (isa<LocInfoType>(T)) { 175 { 176 ColorScope Color(OS, ShowColors, TypeColor); 177 OS << "LocInfo Type"; 178 } 179 dumpPointer(T); 180 return; 181 } 182 183 { 184 ColorScope Color(OS, ShowColors, TypeColor); 185 OS << T->getTypeClassName() << "Type"; 186 } 187 dumpPointer(T); 188 OS << " "; 189 dumpBareType(QualType(T, 0), false); 190 191 QualType SingleStepDesugar = 192 T->getLocallyUnqualifiedSingleStepDesugaredType(); 193 if (SingleStepDesugar != QualType(T, 0)) 194 OS << " sugar"; 195 196 if (T->isDependentType()) 197 OS << " dependent"; 198 else if (T->isInstantiationDependentType()) 199 OS << " instantiation_dependent"; 200 201 if (T->isVariablyModifiedType()) 202 OS << " variably_modified"; 203 if (T->containsUnexpandedParameterPack()) 204 OS << " contains_unexpanded_pack"; 205 if (T->isFromAST()) 206 OS << " imported"; 207 208 TypeVisitor<TextNodeDumper>::Visit(T); 209 } 210 211 void TextNodeDumper::Visit(QualType T) { 212 OS << "QualType"; 213 dumpPointer(T.getAsOpaquePtr()); 214 OS << " "; 215 dumpBareType(T, false); 216 OS << " " << T.split().Quals.getAsString(); 217 } 218 219 void TextNodeDumper::Visit(const Decl *D) { 220 if (!D) { 221 ColorScope Color(OS, ShowColors, NullColor); 222 OS << "<<<NULL>>>"; 223 return; 224 } 225 226 Context = &D->getASTContext(); 227 { 228 ColorScope Color(OS, ShowColors, DeclKindNameColor); 229 OS << D->getDeclKindName() << "Decl"; 230 } 231 dumpPointer(D); 232 if (D->getLexicalDeclContext() != D->getDeclContext()) 233 OS << " parent " << cast<Decl>(D->getDeclContext()); 234 dumpPreviousDecl(OS, D); 235 dumpSourceRange(D->getSourceRange()); 236 OS << ' '; 237 dumpLocation(D->getLocation()); 238 if (D->isFromASTFile()) 239 OS << " imported"; 240 if (Module *M = D->getOwningModule()) 241 OS << " in " << M->getFullModuleName(); 242 if (auto *ND = dyn_cast<NamedDecl>(D)) 243 for (Module *M : D->getASTContext().getModulesWithMergedDefinition( 244 const_cast<NamedDecl *>(ND))) 245 AddChild([=] { OS << "also in " << M->getFullModuleName(); }); 246 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 247 if (ND->isHidden()) 248 OS << " hidden"; 249 if (D->isImplicit()) 250 OS << " implicit"; 251 252 if (D->isUsed()) 253 OS << " used"; 254 else if (D->isThisDeclarationReferenced()) 255 OS << " referenced"; 256 257 if (D->isInvalidDecl()) 258 OS << " invalid"; 259 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 260 if (FD->isConstexprSpecified()) 261 OS << " constexpr"; 262 if (FD->isConsteval()) 263 OS << " consteval"; 264 } 265 266 if (!isa<FunctionDecl>(*D)) { 267 const auto *MD = dyn_cast<ObjCMethodDecl>(D); 268 if (!MD || !MD->isThisDeclarationADefinition()) { 269 const auto *DC = dyn_cast<DeclContext>(D); 270 if (DC && DC->hasExternalLexicalStorage()) { 271 ColorScope Color(OS, ShowColors, UndeserializedColor); 272 OS << " <undeserialized declarations>"; 273 } 274 } 275 } 276 277 ConstDeclVisitor<TextNodeDumper>::Visit(D); 278 } 279 280 void TextNodeDumper::Visit(const CXXCtorInitializer *Init) { 281 OS << "CXXCtorInitializer"; 282 if (Init->isAnyMemberInitializer()) { 283 OS << ' '; 284 dumpBareDeclRef(Init->getAnyMember()); 285 } else if (Init->isBaseInitializer()) { 286 dumpType(QualType(Init->getBaseClass(), 0)); 287 } else if (Init->isDelegatingInitializer()) { 288 dumpType(Init->getTypeSourceInfo()->getType()); 289 } else { 290 llvm_unreachable("Unknown initializer type"); 291 } 292 } 293 294 void TextNodeDumper::Visit(const BlockDecl::Capture &C) { 295 OS << "capture"; 296 if (C.isByRef()) 297 OS << " byref"; 298 if (C.isNested()) 299 OS << " nested"; 300 if (C.getVariable()) { 301 OS << ' '; 302 dumpBareDeclRef(C.getVariable()); 303 } 304 } 305 306 void TextNodeDumper::Visit(const OMPClause *C) { 307 if (!C) { 308 ColorScope Color(OS, ShowColors, NullColor); 309 OS << "<<<NULL>>> OMPClause"; 310 return; 311 } 312 { 313 ColorScope Color(OS, ShowColors, AttrColor); 314 StringRef ClauseName(getOpenMPClauseName(C->getClauseKind())); 315 OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper() 316 << ClauseName.drop_front() << "Clause"; 317 } 318 dumpPointer(C); 319 dumpSourceRange(SourceRange(C->getBeginLoc(), C->getEndLoc())); 320 if (C->isImplicit()) 321 OS << " <implicit>"; 322 } 323 324 void TextNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) { 325 const TypeSourceInfo *TSI = A.getTypeSourceInfo(); 326 if (TSI) { 327 OS << "case "; 328 dumpType(TSI->getType()); 329 } else { 330 OS << "default"; 331 } 332 333 if (A.isSelected()) 334 OS << " selected"; 335 } 336 337 void TextNodeDumper::dumpPointer(const void *Ptr) { 338 ColorScope Color(OS, ShowColors, AddressColor); 339 OS << ' ' << Ptr; 340 } 341 342 void TextNodeDumper::dumpLocation(SourceLocation Loc) { 343 if (!SM) 344 return; 345 346 ColorScope Color(OS, ShowColors, LocationColor); 347 SourceLocation SpellingLoc = SM->getSpellingLoc(Loc); 348 349 // The general format we print out is filename:line:col, but we drop pieces 350 // that haven't changed since the last loc printed. 351 PresumedLoc PLoc = SM->getPresumedLoc(SpellingLoc); 352 353 if (PLoc.isInvalid()) { 354 OS << "<invalid sloc>"; 355 return; 356 } 357 358 if (strcmp(PLoc.getFilename(), LastLocFilename) != 0) { 359 OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':' 360 << PLoc.getColumn(); 361 LastLocFilename = PLoc.getFilename(); 362 LastLocLine = PLoc.getLine(); 363 } else if (PLoc.getLine() != LastLocLine) { 364 OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); 365 LastLocLine = PLoc.getLine(); 366 } else { 367 OS << "col" << ':' << PLoc.getColumn(); 368 } 369 } 370 371 void TextNodeDumper::dumpSourceRange(SourceRange R) { 372 // Can't translate locations if a SourceManager isn't available. 373 if (!SM) 374 return; 375 376 OS << " <"; 377 dumpLocation(R.getBegin()); 378 if (R.getBegin() != R.getEnd()) { 379 OS << ", "; 380 dumpLocation(R.getEnd()); 381 } 382 OS << ">"; 383 384 // <t2.c:123:421[blah], t2.c:412:321> 385 } 386 387 void TextNodeDumper::dumpBareType(QualType T, bool Desugar) { 388 ColorScope Color(OS, ShowColors, TypeColor); 389 390 SplitQualType T_split = T.split(); 391 OS << "'" << QualType::getAsString(T_split, PrintPolicy) << "'"; 392 393 if (Desugar && !T.isNull()) { 394 // If the type is sugared, also dump a (shallow) desugared type. 395 SplitQualType D_split = T.getSplitDesugaredType(); 396 if (T_split != D_split) 397 OS << ":'" << QualType::getAsString(D_split, PrintPolicy) << "'"; 398 } 399 } 400 401 void TextNodeDumper::dumpType(QualType T) { 402 OS << ' '; 403 dumpBareType(T); 404 } 405 406 void TextNodeDumper::dumpBareDeclRef(const Decl *D) { 407 if (!D) { 408 ColorScope Color(OS, ShowColors, NullColor); 409 OS << "<<<NULL>>>"; 410 return; 411 } 412 413 { 414 ColorScope Color(OS, ShowColors, DeclKindNameColor); 415 OS << D->getDeclKindName(); 416 } 417 dumpPointer(D); 418 419 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) { 420 ColorScope Color(OS, ShowColors, DeclNameColor); 421 OS << " '" << ND->getDeclName() << '\''; 422 } 423 424 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) 425 dumpType(VD->getType()); 426 } 427 428 void TextNodeDumper::dumpName(const NamedDecl *ND) { 429 if (ND->getDeclName()) { 430 ColorScope Color(OS, ShowColors, DeclNameColor); 431 OS << ' ' << ND->getNameAsString(); 432 } 433 } 434 435 void TextNodeDumper::dumpAccessSpecifier(AccessSpecifier AS) { 436 switch (AS) { 437 case AS_none: 438 break; 439 case AS_public: 440 OS << "public"; 441 break; 442 case AS_protected: 443 OS << "protected"; 444 break; 445 case AS_private: 446 OS << "private"; 447 break; 448 } 449 } 450 451 void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) { 452 if (!D) 453 return; 454 455 AddChild([=] { 456 if (!Label.empty()) 457 OS << Label << ' '; 458 dumpBareDeclRef(D); 459 }); 460 } 461 462 const char *TextNodeDumper::getCommandName(unsigned CommandID) { 463 if (Traits) 464 return Traits->getCommandInfo(CommandID)->Name; 465 const comments::CommandInfo *Info = 466 comments::CommandTraits::getBuiltinCommandInfo(CommandID); 467 if (Info) 468 return Info->Name; 469 return "<not a builtin command>"; 470 } 471 472 void TextNodeDumper::visitTextComment(const comments::TextComment *C, 473 const comments::FullComment *) { 474 OS << " Text=\"" << C->getText() << "\""; 475 } 476 477 void TextNodeDumper::visitInlineCommandComment( 478 const comments::InlineCommandComment *C, const comments::FullComment *) { 479 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""; 480 switch (C->getRenderKind()) { 481 case comments::InlineCommandComment::RenderNormal: 482 OS << " RenderNormal"; 483 break; 484 case comments::InlineCommandComment::RenderBold: 485 OS << " RenderBold"; 486 break; 487 case comments::InlineCommandComment::RenderMonospaced: 488 OS << " RenderMonospaced"; 489 break; 490 case comments::InlineCommandComment::RenderEmphasized: 491 OS << " RenderEmphasized"; 492 break; 493 } 494 495 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) 496 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\""; 497 } 498 499 void TextNodeDumper::visitHTMLStartTagComment( 500 const comments::HTMLStartTagComment *C, const comments::FullComment *) { 501 OS << " Name=\"" << C->getTagName() << "\""; 502 if (C->getNumAttrs() != 0) { 503 OS << " Attrs: "; 504 for (unsigned i = 0, e = C->getNumAttrs(); i != e; ++i) { 505 const comments::HTMLStartTagComment::Attribute &Attr = C->getAttr(i); 506 OS << " \"" << Attr.Name << "=\"" << Attr.Value << "\""; 507 } 508 } 509 if (C->isSelfClosing()) 510 OS << " SelfClosing"; 511 } 512 513 void TextNodeDumper::visitHTMLEndTagComment( 514 const comments::HTMLEndTagComment *C, const comments::FullComment *) { 515 OS << " Name=\"" << C->getTagName() << "\""; 516 } 517 518 void TextNodeDumper::visitBlockCommandComment( 519 const comments::BlockCommandComment *C, const comments::FullComment *) { 520 OS << " Name=\"" << getCommandName(C->getCommandID()) << "\""; 521 for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) 522 OS << " Arg[" << i << "]=\"" << C->getArgText(i) << "\""; 523 } 524 525 void TextNodeDumper::visitParamCommandComment( 526 const comments::ParamCommandComment *C, const comments::FullComment *FC) { 527 OS << " " 528 << comments::ParamCommandComment::getDirectionAsString(C->getDirection()); 529 530 if (C->isDirectionExplicit()) 531 OS << " explicitly"; 532 else 533 OS << " implicitly"; 534 535 if (C->hasParamName()) { 536 if (C->isParamIndexValid()) 537 OS << " Param=\"" << C->getParamName(FC) << "\""; 538 else 539 OS << " Param=\"" << C->getParamNameAsWritten() << "\""; 540 } 541 542 if (C->isParamIndexValid() && !C->isVarArgParam()) 543 OS << " ParamIndex=" << C->getParamIndex(); 544 } 545 546 void TextNodeDumper::visitTParamCommandComment( 547 const comments::TParamCommandComment *C, const comments::FullComment *FC) { 548 if (C->hasParamName()) { 549 if (C->isPositionValid()) 550 OS << " Param=\"" << C->getParamName(FC) << "\""; 551 else 552 OS << " Param=\"" << C->getParamNameAsWritten() << "\""; 553 } 554 555 if (C->isPositionValid()) { 556 OS << " Position=<"; 557 for (unsigned i = 0, e = C->getDepth(); i != e; ++i) { 558 OS << C->getIndex(i); 559 if (i != e - 1) 560 OS << ", "; 561 } 562 OS << ">"; 563 } 564 } 565 566 void TextNodeDumper::visitVerbatimBlockComment( 567 const comments::VerbatimBlockComment *C, const comments::FullComment *) { 568 OS << " Name=\"" << getCommandName(C->getCommandID()) 569 << "\"" 570 " CloseName=\"" 571 << C->getCloseName() << "\""; 572 } 573 574 void TextNodeDumper::visitVerbatimBlockLineComment( 575 const comments::VerbatimBlockLineComment *C, 576 const comments::FullComment *) { 577 OS << " Text=\"" << C->getText() << "\""; 578 } 579 580 void TextNodeDumper::visitVerbatimLineComment( 581 const comments::VerbatimLineComment *C, const comments::FullComment *) { 582 OS << " Text=\"" << C->getText() << "\""; 583 } 584 585 void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) { 586 OS << " null"; 587 } 588 589 void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) { 590 OS << " type"; 591 dumpType(TA.getAsType()); 592 } 593 594 void TextNodeDumper::VisitDeclarationTemplateArgument( 595 const TemplateArgument &TA) { 596 OS << " decl"; 597 dumpDeclRef(TA.getAsDecl()); 598 } 599 600 void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) { 601 OS << " nullptr"; 602 } 603 604 void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) { 605 OS << " integral " << TA.getAsIntegral(); 606 } 607 608 void TextNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) { 609 OS << " template "; 610 TA.getAsTemplate().dump(OS); 611 } 612 613 void TextNodeDumper::VisitTemplateExpansionTemplateArgument( 614 const TemplateArgument &TA) { 615 OS << " template expansion "; 616 TA.getAsTemplateOrTemplatePattern().dump(OS); 617 } 618 619 void TextNodeDumper::VisitExpressionTemplateArgument(const TemplateArgument &) { 620 OS << " expr"; 621 } 622 623 void TextNodeDumper::VisitPackTemplateArgument(const TemplateArgument &) { 624 OS << " pack"; 625 } 626 627 static void dumpBasePath(raw_ostream &OS, const CastExpr *Node) { 628 if (Node->path_empty()) 629 return; 630 631 OS << " ("; 632 bool First = true; 633 for (CastExpr::path_const_iterator I = Node->path_begin(), 634 E = Node->path_end(); 635 I != E; ++I) { 636 const CXXBaseSpecifier *Base = *I; 637 if (!First) 638 OS << " -> "; 639 640 const CXXRecordDecl *RD = 641 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 642 643 if (Base->isVirtual()) 644 OS << "virtual "; 645 OS << RD->getName(); 646 First = false; 647 } 648 649 OS << ')'; 650 } 651 652 void TextNodeDumper::VisitIfStmt(const IfStmt *Node) { 653 if (Node->hasInitStorage()) 654 OS << " has_init"; 655 if (Node->hasVarStorage()) 656 OS << " has_var"; 657 if (Node->hasElseStorage()) 658 OS << " has_else"; 659 } 660 661 void TextNodeDumper::VisitSwitchStmt(const SwitchStmt *Node) { 662 if (Node->hasInitStorage()) 663 OS << " has_init"; 664 if (Node->hasVarStorage()) 665 OS << " has_var"; 666 } 667 668 void TextNodeDumper::VisitWhileStmt(const WhileStmt *Node) { 669 if (Node->hasVarStorage()) 670 OS << " has_var"; 671 } 672 673 void TextNodeDumper::VisitLabelStmt(const LabelStmt *Node) { 674 OS << " '" << Node->getName() << "'"; 675 } 676 677 void TextNodeDumper::VisitGotoStmt(const GotoStmt *Node) { 678 OS << " '" << Node->getLabel()->getName() << "'"; 679 dumpPointer(Node->getLabel()); 680 } 681 682 void TextNodeDumper::VisitCaseStmt(const CaseStmt *Node) { 683 if (Node->caseStmtIsGNURange()) 684 OS << " gnu_range"; 685 } 686 687 void TextNodeDumper::VisitConstantExpr(const ConstantExpr *Node) { 688 if (Node->getResultAPValueKind() != APValue::None) { 689 ColorScope Color(OS, ShowColors, ValueColor); 690 OS << " "; 691 Node->getAPValueResult().printPretty(OS, *Context, Node->getType()); 692 } 693 } 694 695 void TextNodeDumper::VisitCallExpr(const CallExpr *Node) { 696 if (Node->usesADL()) 697 OS << " adl"; 698 } 699 700 void TextNodeDumper::VisitCastExpr(const CastExpr *Node) { 701 OS << " <"; 702 { 703 ColorScope Color(OS, ShowColors, CastColor); 704 OS << Node->getCastKindName(); 705 } 706 dumpBasePath(OS, Node); 707 OS << ">"; 708 } 709 710 void TextNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *Node) { 711 VisitCastExpr(Node); 712 if (Node->isPartOfExplicitCast()) 713 OS << " part_of_explicit_cast"; 714 } 715 716 void TextNodeDumper::VisitDeclRefExpr(const DeclRefExpr *Node) { 717 OS << " "; 718 dumpBareDeclRef(Node->getDecl()); 719 if (Node->getDecl() != Node->getFoundDecl()) { 720 OS << " ("; 721 dumpBareDeclRef(Node->getFoundDecl()); 722 OS << ")"; 723 } 724 switch (Node->isNonOdrUse()) { 725 case NOUR_None: break; 726 case NOUR_Unevaluated: OS << " non_odr_use_unevaluated"; break; 727 case NOUR_Constant: OS << " non_odr_use_constant"; break; 728 case NOUR_Discarded: OS << " non_odr_use_discarded"; break; 729 } 730 } 731 732 void TextNodeDumper::VisitUnresolvedLookupExpr( 733 const UnresolvedLookupExpr *Node) { 734 OS << " ("; 735 if (!Node->requiresADL()) 736 OS << "no "; 737 OS << "ADL) = '" << Node->getName() << '\''; 738 739 UnresolvedLookupExpr::decls_iterator I = Node->decls_begin(), 740 E = Node->decls_end(); 741 if (I == E) 742 OS << " empty"; 743 for (; I != E; ++I) 744 dumpPointer(*I); 745 } 746 747 void TextNodeDumper::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *Node) { 748 { 749 ColorScope Color(OS, ShowColors, DeclKindNameColor); 750 OS << " " << Node->getDecl()->getDeclKindName() << "Decl"; 751 } 752 OS << "='" << *Node->getDecl() << "'"; 753 dumpPointer(Node->getDecl()); 754 if (Node->isFreeIvar()) 755 OS << " isFreeIvar"; 756 } 757 758 void TextNodeDumper::VisitPredefinedExpr(const PredefinedExpr *Node) { 759 OS << " " << PredefinedExpr::getIdentKindName(Node->getIdentKind()); 760 } 761 762 void TextNodeDumper::VisitCharacterLiteral(const CharacterLiteral *Node) { 763 ColorScope Color(OS, ShowColors, ValueColor); 764 OS << " " << Node->getValue(); 765 } 766 767 void TextNodeDumper::VisitIntegerLiteral(const IntegerLiteral *Node) { 768 bool isSigned = Node->getType()->isSignedIntegerType(); 769 ColorScope Color(OS, ShowColors, ValueColor); 770 OS << " " << Node->getValue().toString(10, isSigned); 771 } 772 773 void TextNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *Node) { 774 ColorScope Color(OS, ShowColors, ValueColor); 775 OS << " " << Node->getValueAsString(/*Radix=*/10); 776 } 777 778 void TextNodeDumper::VisitFloatingLiteral(const FloatingLiteral *Node) { 779 ColorScope Color(OS, ShowColors, ValueColor); 780 OS << " " << Node->getValueAsApproximateDouble(); 781 } 782 783 void TextNodeDumper::VisitStringLiteral(const StringLiteral *Str) { 784 ColorScope Color(OS, ShowColors, ValueColor); 785 OS << " "; 786 Str->outputString(OS); 787 } 788 789 void TextNodeDumper::VisitInitListExpr(const InitListExpr *ILE) { 790 if (auto *Field = ILE->getInitializedFieldInUnion()) { 791 OS << " field "; 792 dumpBareDeclRef(Field); 793 } 794 } 795 796 void TextNodeDumper::VisitGenericSelectionExpr(const GenericSelectionExpr *E) { 797 if (E->isResultDependent()) 798 OS << " result_dependent"; 799 } 800 801 void TextNodeDumper::VisitUnaryOperator(const UnaryOperator *Node) { 802 OS << " " << (Node->isPostfix() ? "postfix" : "prefix") << " '" 803 << UnaryOperator::getOpcodeStr(Node->getOpcode()) << "'"; 804 if (!Node->canOverflow()) 805 OS << " cannot overflow"; 806 } 807 808 void TextNodeDumper::VisitUnaryExprOrTypeTraitExpr( 809 const UnaryExprOrTypeTraitExpr *Node) { 810 switch (Node->getKind()) { 811 case UETT_SizeOf: 812 OS << " sizeof"; 813 break; 814 case UETT_AlignOf: 815 OS << " alignof"; 816 break; 817 case UETT_VecStep: 818 OS << " vec_step"; 819 break; 820 case UETT_OpenMPRequiredSimdAlign: 821 OS << " __builtin_omp_required_simd_align"; 822 break; 823 case UETT_PreferredAlignOf: 824 OS << " __alignof"; 825 break; 826 } 827 if (Node->isArgumentType()) 828 dumpType(Node->getArgumentType()); 829 } 830 831 void TextNodeDumper::VisitMemberExpr(const MemberExpr *Node) { 832 OS << " " << (Node->isArrow() ? "->" : ".") << *Node->getMemberDecl(); 833 dumpPointer(Node->getMemberDecl()); 834 switch (Node->isNonOdrUse()) { 835 case NOUR_None: break; 836 case NOUR_Unevaluated: OS << " non_odr_use_unevaluated"; break; 837 case NOUR_Constant: OS << " non_odr_use_constant"; break; 838 case NOUR_Discarded: OS << " non_odr_use_discarded"; break; 839 } 840 } 841 842 void TextNodeDumper::VisitExtVectorElementExpr( 843 const ExtVectorElementExpr *Node) { 844 OS << " " << Node->getAccessor().getNameStart(); 845 } 846 847 void TextNodeDumper::VisitBinaryOperator(const BinaryOperator *Node) { 848 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) << "'"; 849 } 850 851 void TextNodeDumper::VisitCompoundAssignOperator( 852 const CompoundAssignOperator *Node) { 853 OS << " '" << BinaryOperator::getOpcodeStr(Node->getOpcode()) 854 << "' ComputeLHSTy="; 855 dumpBareType(Node->getComputationLHSType()); 856 OS << " ComputeResultTy="; 857 dumpBareType(Node->getComputationResultType()); 858 } 859 860 void TextNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *Node) { 861 OS << " " << Node->getLabel()->getName(); 862 dumpPointer(Node->getLabel()); 863 } 864 865 void TextNodeDumper::VisitCXXNamedCastExpr(const CXXNamedCastExpr *Node) { 866 OS << " " << Node->getCastName() << "<" 867 << Node->getTypeAsWritten().getAsString() << ">" 868 << " <" << Node->getCastKindName(); 869 dumpBasePath(OS, Node); 870 OS << ">"; 871 } 872 873 void TextNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *Node) { 874 OS << " " << (Node->getValue() ? "true" : "false"); 875 } 876 877 void TextNodeDumper::VisitCXXThisExpr(const CXXThisExpr *Node) { 878 if (Node->isImplicit()) 879 OS << " implicit"; 880 OS << " this"; 881 } 882 883 void TextNodeDumper::VisitCXXFunctionalCastExpr( 884 const CXXFunctionalCastExpr *Node) { 885 OS << " functional cast to " << Node->getTypeAsWritten().getAsString() << " <" 886 << Node->getCastKindName() << ">"; 887 } 888 889 void TextNodeDumper::VisitCXXUnresolvedConstructExpr( 890 const CXXUnresolvedConstructExpr *Node) { 891 dumpType(Node->getTypeAsWritten()); 892 if (Node->isListInitialization()) 893 OS << " list"; 894 } 895 896 void TextNodeDumper::VisitCXXConstructExpr(const CXXConstructExpr *Node) { 897 CXXConstructorDecl *Ctor = Node->getConstructor(); 898 dumpType(Ctor->getType()); 899 if (Node->isElidable()) 900 OS << " elidable"; 901 if (Node->isListInitialization()) 902 OS << " list"; 903 if (Node->isStdInitListInitialization()) 904 OS << " std::initializer_list"; 905 if (Node->requiresZeroInitialization()) 906 OS << " zeroing"; 907 } 908 909 void TextNodeDumper::VisitCXXBindTemporaryExpr( 910 const CXXBindTemporaryExpr *Node) { 911 OS << " (CXXTemporary"; 912 dumpPointer(Node); 913 OS << ")"; 914 } 915 916 void TextNodeDumper::VisitCXXNewExpr(const CXXNewExpr *Node) { 917 if (Node->isGlobalNew()) 918 OS << " global"; 919 if (Node->isArray()) 920 OS << " array"; 921 if (Node->getOperatorNew()) { 922 OS << ' '; 923 dumpBareDeclRef(Node->getOperatorNew()); 924 } 925 // We could dump the deallocation function used in case of error, but it's 926 // usually not that interesting. 927 } 928 929 void TextNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *Node) { 930 if (Node->isGlobalDelete()) 931 OS << " global"; 932 if (Node->isArrayForm()) 933 OS << " array"; 934 if (Node->getOperatorDelete()) { 935 OS << ' '; 936 dumpBareDeclRef(Node->getOperatorDelete()); 937 } 938 } 939 940 void TextNodeDumper::VisitMaterializeTemporaryExpr( 941 const MaterializeTemporaryExpr *Node) { 942 if (const ValueDecl *VD = Node->getExtendingDecl()) { 943 OS << " extended by "; 944 dumpBareDeclRef(VD); 945 } 946 } 947 948 void TextNodeDumper::VisitExprWithCleanups(const ExprWithCleanups *Node) { 949 for (unsigned i = 0, e = Node->getNumObjects(); i != e; ++i) 950 dumpDeclRef(Node->getObject(i), "cleanup"); 951 } 952 953 void TextNodeDumper::VisitSizeOfPackExpr(const SizeOfPackExpr *Node) { 954 dumpPointer(Node->getPack()); 955 dumpName(Node->getPack()); 956 } 957 958 void TextNodeDumper::VisitCXXDependentScopeMemberExpr( 959 const CXXDependentScopeMemberExpr *Node) { 960 OS << " " << (Node->isArrow() ? "->" : ".") << Node->getMember(); 961 } 962 963 void TextNodeDumper::VisitObjCMessageExpr(const ObjCMessageExpr *Node) { 964 OS << " selector="; 965 Node->getSelector().print(OS); 966 switch (Node->getReceiverKind()) { 967 case ObjCMessageExpr::Instance: 968 break; 969 970 case ObjCMessageExpr::Class: 971 OS << " class="; 972 dumpBareType(Node->getClassReceiver()); 973 break; 974 975 case ObjCMessageExpr::SuperInstance: 976 OS << " super (instance)"; 977 break; 978 979 case ObjCMessageExpr::SuperClass: 980 OS << " super (class)"; 981 break; 982 } 983 } 984 985 void TextNodeDumper::VisitObjCBoxedExpr(const ObjCBoxedExpr *Node) { 986 if (auto *BoxingMethod = Node->getBoxingMethod()) { 987 OS << " selector="; 988 BoxingMethod->getSelector().print(OS); 989 } 990 } 991 992 void TextNodeDumper::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *Node) { 993 if (!Node->getCatchParamDecl()) 994 OS << " catch all"; 995 } 996 997 void TextNodeDumper::VisitObjCEncodeExpr(const ObjCEncodeExpr *Node) { 998 dumpType(Node->getEncodedType()); 999 } 1000 1001 void TextNodeDumper::VisitObjCSelectorExpr(const ObjCSelectorExpr *Node) { 1002 OS << " "; 1003 Node->getSelector().print(OS); 1004 } 1005 1006 void TextNodeDumper::VisitObjCProtocolExpr(const ObjCProtocolExpr *Node) { 1007 OS << ' ' << *Node->getProtocol(); 1008 } 1009 1010 void TextNodeDumper::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *Node) { 1011 if (Node->isImplicitProperty()) { 1012 OS << " Kind=MethodRef Getter=\""; 1013 if (Node->getImplicitPropertyGetter()) 1014 Node->getImplicitPropertyGetter()->getSelector().print(OS); 1015 else 1016 OS << "(null)"; 1017 1018 OS << "\" Setter=\""; 1019 if (ObjCMethodDecl *Setter = Node->getImplicitPropertySetter()) 1020 Setter->getSelector().print(OS); 1021 else 1022 OS << "(null)"; 1023 OS << "\""; 1024 } else { 1025 OS << " Kind=PropertyRef Property=\"" << *Node->getExplicitProperty() 1026 << '"'; 1027 } 1028 1029 if (Node->isSuperReceiver()) 1030 OS << " super"; 1031 1032 OS << " Messaging="; 1033 if (Node->isMessagingGetter() && Node->isMessagingSetter()) 1034 OS << "Getter&Setter"; 1035 else if (Node->isMessagingGetter()) 1036 OS << "Getter"; 1037 else if (Node->isMessagingSetter()) 1038 OS << "Setter"; 1039 } 1040 1041 void TextNodeDumper::VisitObjCSubscriptRefExpr( 1042 const ObjCSubscriptRefExpr *Node) { 1043 if (Node->isArraySubscriptRefExpr()) 1044 OS << " Kind=ArraySubscript GetterForArray=\""; 1045 else 1046 OS << " Kind=DictionarySubscript GetterForDictionary=\""; 1047 if (Node->getAtIndexMethodDecl()) 1048 Node->getAtIndexMethodDecl()->getSelector().print(OS); 1049 else 1050 OS << "(null)"; 1051 1052 if (Node->isArraySubscriptRefExpr()) 1053 OS << "\" SetterForArray=\""; 1054 else 1055 OS << "\" SetterForDictionary=\""; 1056 if (Node->setAtIndexMethodDecl()) 1057 Node->setAtIndexMethodDecl()->getSelector().print(OS); 1058 else 1059 OS << "(null)"; 1060 } 1061 1062 void TextNodeDumper::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *Node) { 1063 OS << " " << (Node->getValue() ? "__objc_yes" : "__objc_no"); 1064 } 1065 1066 void TextNodeDumper::VisitRValueReferenceType(const ReferenceType *T) { 1067 if (T->isSpelledAsLValue()) 1068 OS << " written as lvalue reference"; 1069 } 1070 1071 void TextNodeDumper::VisitArrayType(const ArrayType *T) { 1072 switch (T->getSizeModifier()) { 1073 case ArrayType::Normal: 1074 break; 1075 case ArrayType::Static: 1076 OS << " static"; 1077 break; 1078 case ArrayType::Star: 1079 OS << " *"; 1080 break; 1081 } 1082 OS << " " << T->getIndexTypeQualifiers().getAsString(); 1083 } 1084 1085 void TextNodeDumper::VisitConstantArrayType(const ConstantArrayType *T) { 1086 OS << " " << T->getSize(); 1087 VisitArrayType(T); 1088 } 1089 1090 void TextNodeDumper::VisitVariableArrayType(const VariableArrayType *T) { 1091 OS << " "; 1092 dumpSourceRange(T->getBracketsRange()); 1093 VisitArrayType(T); 1094 } 1095 1096 void TextNodeDumper::VisitDependentSizedArrayType( 1097 const DependentSizedArrayType *T) { 1098 VisitArrayType(T); 1099 OS << " "; 1100 dumpSourceRange(T->getBracketsRange()); 1101 } 1102 1103 void TextNodeDumper::VisitDependentSizedExtVectorType( 1104 const DependentSizedExtVectorType *T) { 1105 OS << " "; 1106 dumpLocation(T->getAttributeLoc()); 1107 } 1108 1109 void TextNodeDumper::VisitVectorType(const VectorType *T) { 1110 switch (T->getVectorKind()) { 1111 case VectorType::GenericVector: 1112 break; 1113 case VectorType::AltiVecVector: 1114 OS << " altivec"; 1115 break; 1116 case VectorType::AltiVecPixel: 1117 OS << " altivec pixel"; 1118 break; 1119 case VectorType::AltiVecBool: 1120 OS << " altivec bool"; 1121 break; 1122 case VectorType::NeonVector: 1123 OS << " neon"; 1124 break; 1125 case VectorType::NeonPolyVector: 1126 OS << " neon poly"; 1127 break; 1128 } 1129 OS << " " << T->getNumElements(); 1130 } 1131 1132 void TextNodeDumper::VisitFunctionType(const FunctionType *T) { 1133 auto EI = T->getExtInfo(); 1134 if (EI.getNoReturn()) 1135 OS << " noreturn"; 1136 if (EI.getProducesResult()) 1137 OS << " produces_result"; 1138 if (EI.getHasRegParm()) 1139 OS << " regparm " << EI.getRegParm(); 1140 OS << " " << FunctionType::getNameForCallConv(EI.getCC()); 1141 } 1142 1143 void TextNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) { 1144 auto EPI = T->getExtProtoInfo(); 1145 if (EPI.HasTrailingReturn) 1146 OS << " trailing_return"; 1147 if (T->isConst()) 1148 OS << " const"; 1149 if (T->isVolatile()) 1150 OS << " volatile"; 1151 if (T->isRestrict()) 1152 OS << " restrict"; 1153 if (T->getExtProtoInfo().Variadic) 1154 OS << " variadic"; 1155 switch (EPI.RefQualifier) { 1156 case RQ_None: 1157 break; 1158 case RQ_LValue: 1159 OS << " &"; 1160 break; 1161 case RQ_RValue: 1162 OS << " &&"; 1163 break; 1164 } 1165 // FIXME: Exception specification. 1166 // FIXME: Consumed parameters. 1167 VisitFunctionType(T); 1168 } 1169 1170 void TextNodeDumper::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { 1171 dumpDeclRef(T->getDecl()); 1172 } 1173 1174 void TextNodeDumper::VisitTypedefType(const TypedefType *T) { 1175 dumpDeclRef(T->getDecl()); 1176 } 1177 1178 void TextNodeDumper::VisitUnaryTransformType(const UnaryTransformType *T) { 1179 switch (T->getUTTKind()) { 1180 case UnaryTransformType::EnumUnderlyingType: 1181 OS << " underlying_type"; 1182 break; 1183 } 1184 } 1185 1186 void TextNodeDumper::VisitTagType(const TagType *T) { 1187 dumpDeclRef(T->getDecl()); 1188 } 1189 1190 void TextNodeDumper::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { 1191 OS << " depth " << T->getDepth() << " index " << T->getIndex(); 1192 if (T->isParameterPack()) 1193 OS << " pack"; 1194 dumpDeclRef(T->getDecl()); 1195 } 1196 1197 void TextNodeDumper::VisitAutoType(const AutoType *T) { 1198 if (T->isDecltypeAuto()) 1199 OS << " decltype(auto)"; 1200 if (!T->isDeduced()) 1201 OS << " undeduced"; 1202 } 1203 1204 void TextNodeDumper::VisitTemplateSpecializationType( 1205 const TemplateSpecializationType *T) { 1206 if (T->isTypeAlias()) 1207 OS << " alias"; 1208 OS << " "; 1209 T->getTemplateName().dump(OS); 1210 } 1211 1212 void TextNodeDumper::VisitInjectedClassNameType( 1213 const InjectedClassNameType *T) { 1214 dumpDeclRef(T->getDecl()); 1215 } 1216 1217 void TextNodeDumper::VisitObjCInterfaceType(const ObjCInterfaceType *T) { 1218 dumpDeclRef(T->getDecl()); 1219 } 1220 1221 void TextNodeDumper::VisitPackExpansionType(const PackExpansionType *T) { 1222 if (auto N = T->getNumExpansions()) 1223 OS << " expansions " << *N; 1224 } 1225 1226 void TextNodeDumper::VisitLabelDecl(const LabelDecl *D) { dumpName(D); } 1227 1228 void TextNodeDumper::VisitTypedefDecl(const TypedefDecl *D) { 1229 dumpName(D); 1230 dumpType(D->getUnderlyingType()); 1231 if (D->isModulePrivate()) 1232 OS << " __module_private__"; 1233 } 1234 1235 void TextNodeDumper::VisitEnumDecl(const EnumDecl *D) { 1236 if (D->isScoped()) { 1237 if (D->isScopedUsingClassTag()) 1238 OS << " class"; 1239 else 1240 OS << " struct"; 1241 } 1242 dumpName(D); 1243 if (D->isModulePrivate()) 1244 OS << " __module_private__"; 1245 if (D->isFixed()) 1246 dumpType(D->getIntegerType()); 1247 } 1248 1249 void TextNodeDumper::VisitRecordDecl(const RecordDecl *D) { 1250 OS << ' ' << D->getKindName(); 1251 dumpName(D); 1252 if (D->isModulePrivate()) 1253 OS << " __module_private__"; 1254 if (D->isCompleteDefinition()) 1255 OS << " definition"; 1256 } 1257 1258 void TextNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *D) { 1259 dumpName(D); 1260 dumpType(D->getType()); 1261 } 1262 1263 void TextNodeDumper::VisitIndirectFieldDecl(const IndirectFieldDecl *D) { 1264 dumpName(D); 1265 dumpType(D->getType()); 1266 1267 for (const auto *Child : D->chain()) 1268 dumpDeclRef(Child); 1269 } 1270 1271 void TextNodeDumper::VisitFunctionDecl(const FunctionDecl *D) { 1272 dumpName(D); 1273 dumpType(D->getType()); 1274 1275 StorageClass SC = D->getStorageClass(); 1276 if (SC != SC_None) 1277 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC); 1278 if (D->isInlineSpecified()) 1279 OS << " inline"; 1280 if (D->isVirtualAsWritten()) 1281 OS << " virtual"; 1282 if (D->isModulePrivate()) 1283 OS << " __module_private__"; 1284 1285 if (D->isPure()) 1286 OS << " pure"; 1287 if (D->isDefaulted()) { 1288 OS << " default"; 1289 if (D->isDeleted()) 1290 OS << "_delete"; 1291 } 1292 if (D->isDeletedAsWritten()) 1293 OS << " delete"; 1294 if (D->isTrivial()) 1295 OS << " trivial"; 1296 1297 if (const auto *FPT = D->getType()->getAs<FunctionProtoType>()) { 1298 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); 1299 switch (EPI.ExceptionSpec.Type) { 1300 default: 1301 break; 1302 case EST_Unevaluated: 1303 OS << " noexcept-unevaluated " << EPI.ExceptionSpec.SourceDecl; 1304 break; 1305 case EST_Uninstantiated: 1306 OS << " noexcept-uninstantiated " << EPI.ExceptionSpec.SourceTemplate; 1307 break; 1308 } 1309 } 1310 1311 if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { 1312 if (MD->size_overridden_methods() != 0) { 1313 auto dumpOverride = [=](const CXXMethodDecl *D) { 1314 SplitQualType T_split = D->getType().split(); 1315 OS << D << " " << D->getParent()->getName() 1316 << "::" << D->getNameAsString() << " '" 1317 << QualType::getAsString(T_split, PrintPolicy) << "'"; 1318 }; 1319 1320 AddChild([=] { 1321 auto Overrides = MD->overridden_methods(); 1322 OS << "Overrides: [ "; 1323 dumpOverride(*Overrides.begin()); 1324 for (const auto *Override : 1325 llvm::make_range(Overrides.begin() + 1, Overrides.end())) { 1326 OS << ", "; 1327 dumpOverride(Override); 1328 } 1329 OS << " ]"; 1330 }); 1331 } 1332 } 1333 1334 // Since NumParams comes from the FunctionProtoType of the FunctionDecl and 1335 // the Params are set later, it is possible for a dump during debugging to 1336 // encounter a FunctionDecl that has been created but hasn't been assigned 1337 // ParmVarDecls yet. 1338 if (!D->param_empty() && !D->param_begin()) 1339 OS << " <<<NULL params x " << D->getNumParams() << ">>>"; 1340 } 1341 1342 void TextNodeDumper::VisitFieldDecl(const FieldDecl *D) { 1343 dumpName(D); 1344 dumpType(D->getType()); 1345 if (D->isMutable()) 1346 OS << " mutable"; 1347 if (D->isModulePrivate()) 1348 OS << " __module_private__"; 1349 } 1350 1351 void TextNodeDumper::VisitVarDecl(const VarDecl *D) { 1352 dumpName(D); 1353 dumpType(D->getType()); 1354 StorageClass SC = D->getStorageClass(); 1355 if (SC != SC_None) 1356 OS << ' ' << VarDecl::getStorageClassSpecifierString(SC); 1357 switch (D->getTLSKind()) { 1358 case VarDecl::TLS_None: 1359 break; 1360 case VarDecl::TLS_Static: 1361 OS << " tls"; 1362 break; 1363 case VarDecl::TLS_Dynamic: 1364 OS << " tls_dynamic"; 1365 break; 1366 } 1367 if (D->isModulePrivate()) 1368 OS << " __module_private__"; 1369 if (D->isNRVOVariable()) 1370 OS << " nrvo"; 1371 if (D->isInline()) 1372 OS << " inline"; 1373 if (D->isConstexpr()) 1374 OS << " constexpr"; 1375 if (D->hasInit()) { 1376 switch (D->getInitStyle()) { 1377 case VarDecl::CInit: 1378 OS << " cinit"; 1379 break; 1380 case VarDecl::CallInit: 1381 OS << " callinit"; 1382 break; 1383 case VarDecl::ListInit: 1384 OS << " listinit"; 1385 break; 1386 } 1387 } 1388 if (D->isParameterPack()) 1389 OS << " pack"; 1390 } 1391 1392 void TextNodeDumper::VisitBindingDecl(const BindingDecl *D) { 1393 dumpName(D); 1394 dumpType(D->getType()); 1395 } 1396 1397 void TextNodeDumper::VisitCapturedDecl(const CapturedDecl *D) { 1398 if (D->isNothrow()) 1399 OS << " nothrow"; 1400 } 1401 1402 void TextNodeDumper::VisitImportDecl(const ImportDecl *D) { 1403 OS << ' ' << D->getImportedModule()->getFullModuleName(); 1404 1405 for (Decl *InitD : 1406 D->getASTContext().getModuleInitializers(D->getImportedModule())) 1407 dumpDeclRef(InitD, "initializer"); 1408 } 1409 1410 void TextNodeDumper::VisitPragmaCommentDecl(const PragmaCommentDecl *D) { 1411 OS << ' '; 1412 switch (D->getCommentKind()) { 1413 case PCK_Unknown: 1414 llvm_unreachable("unexpected pragma comment kind"); 1415 case PCK_Compiler: 1416 OS << "compiler"; 1417 break; 1418 case PCK_ExeStr: 1419 OS << "exestr"; 1420 break; 1421 case PCK_Lib: 1422 OS << "lib"; 1423 break; 1424 case PCK_Linker: 1425 OS << "linker"; 1426 break; 1427 case PCK_User: 1428 OS << "user"; 1429 break; 1430 } 1431 StringRef Arg = D->getArg(); 1432 if (!Arg.empty()) 1433 OS << " \"" << Arg << "\""; 1434 } 1435 1436 void TextNodeDumper::VisitPragmaDetectMismatchDecl( 1437 const PragmaDetectMismatchDecl *D) { 1438 OS << " \"" << D->getName() << "\" \"" << D->getValue() << "\""; 1439 } 1440 1441 void TextNodeDumper::VisitOMPExecutableDirective( 1442 const OMPExecutableDirective *D) { 1443 if (D->isStandaloneDirective()) 1444 OS << " openmp_standalone_directive"; 1445 } 1446 1447 void TextNodeDumper::VisitOMPDeclareReductionDecl( 1448 const OMPDeclareReductionDecl *D) { 1449 dumpName(D); 1450 dumpType(D->getType()); 1451 OS << " combiner"; 1452 dumpPointer(D->getCombiner()); 1453 if (const auto *Initializer = D->getInitializer()) { 1454 OS << " initializer"; 1455 dumpPointer(Initializer); 1456 switch (D->getInitializerKind()) { 1457 case OMPDeclareReductionDecl::DirectInit: 1458 OS << " omp_priv = "; 1459 break; 1460 case OMPDeclareReductionDecl::CopyInit: 1461 OS << " omp_priv ()"; 1462 break; 1463 case OMPDeclareReductionDecl::CallInit: 1464 break; 1465 } 1466 } 1467 } 1468 1469 void TextNodeDumper::VisitOMPRequiresDecl(const OMPRequiresDecl *D) { 1470 for (const auto *C : D->clauselists()) { 1471 AddChild([=] { 1472 if (!C) { 1473 ColorScope Color(OS, ShowColors, NullColor); 1474 OS << "<<<NULL>>> OMPClause"; 1475 return; 1476 } 1477 { 1478 ColorScope Color(OS, ShowColors, AttrColor); 1479 StringRef ClauseName(getOpenMPClauseName(C->getClauseKind())); 1480 OS << "OMP" << ClauseName.substr(/*Start=*/0, /*N=*/1).upper() 1481 << ClauseName.drop_front() << "Clause"; 1482 } 1483 dumpPointer(C); 1484 dumpSourceRange(SourceRange(C->getBeginLoc(), C->getEndLoc())); 1485 }); 1486 } 1487 } 1488 1489 void TextNodeDumper::VisitOMPCapturedExprDecl(const OMPCapturedExprDecl *D) { 1490 dumpName(D); 1491 dumpType(D->getType()); 1492 } 1493 1494 void TextNodeDumper::VisitNamespaceDecl(const NamespaceDecl *D) { 1495 dumpName(D); 1496 if (D->isInline()) 1497 OS << " inline"; 1498 if (!D->isOriginalNamespace()) 1499 dumpDeclRef(D->getOriginalNamespace(), "original"); 1500 } 1501 1502 void TextNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { 1503 OS << ' '; 1504 dumpBareDeclRef(D->getNominatedNamespace()); 1505 } 1506 1507 void TextNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { 1508 dumpName(D); 1509 dumpDeclRef(D->getAliasedNamespace()); 1510 } 1511 1512 void TextNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *D) { 1513 dumpName(D); 1514 dumpType(D->getUnderlyingType()); 1515 } 1516 1517 void TextNodeDumper::VisitTypeAliasTemplateDecl( 1518 const TypeAliasTemplateDecl *D) { 1519 dumpName(D); 1520 } 1521 1522 void TextNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *D) { 1523 VisitRecordDecl(D); 1524 if (!D->isCompleteDefinition()) 1525 return; 1526 1527 AddChild([=] { 1528 { 1529 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1530 OS << "DefinitionData"; 1531 } 1532 #define FLAG(fn, name) \ 1533 if (D->fn()) \ 1534 OS << " " #name; 1535 FLAG(isParsingBaseSpecifiers, parsing_base_specifiers); 1536 1537 FLAG(isGenericLambda, generic); 1538 FLAG(isLambda, lambda); 1539 1540 FLAG(canPassInRegisters, pass_in_registers); 1541 FLAG(isEmpty, empty); 1542 FLAG(isAggregate, aggregate); 1543 FLAG(isStandardLayout, standard_layout); 1544 FLAG(isTriviallyCopyable, trivially_copyable); 1545 FLAG(isPOD, pod); 1546 FLAG(isTrivial, trivial); 1547 FLAG(isPolymorphic, polymorphic); 1548 FLAG(isAbstract, abstract); 1549 FLAG(isLiteral, literal); 1550 1551 FLAG(hasUserDeclaredConstructor, has_user_declared_ctor); 1552 FLAG(hasConstexprNonCopyMoveConstructor, has_constexpr_non_copy_move_ctor); 1553 FLAG(hasMutableFields, has_mutable_fields); 1554 FLAG(hasVariantMembers, has_variant_members); 1555 FLAG(allowConstDefaultInit, can_const_default_init); 1556 1557 AddChild([=] { 1558 { 1559 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1560 OS << "DefaultConstructor"; 1561 } 1562 FLAG(hasDefaultConstructor, exists); 1563 FLAG(hasTrivialDefaultConstructor, trivial); 1564 FLAG(hasNonTrivialDefaultConstructor, non_trivial); 1565 FLAG(hasUserProvidedDefaultConstructor, user_provided); 1566 FLAG(hasConstexprDefaultConstructor, constexpr); 1567 FLAG(needsImplicitDefaultConstructor, needs_implicit); 1568 FLAG(defaultedDefaultConstructorIsConstexpr, defaulted_is_constexpr); 1569 }); 1570 1571 AddChild([=] { 1572 { 1573 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1574 OS << "CopyConstructor"; 1575 } 1576 FLAG(hasSimpleCopyConstructor, simple); 1577 FLAG(hasTrivialCopyConstructor, trivial); 1578 FLAG(hasNonTrivialCopyConstructor, non_trivial); 1579 FLAG(hasUserDeclaredCopyConstructor, user_declared); 1580 FLAG(hasCopyConstructorWithConstParam, has_const_param); 1581 FLAG(needsImplicitCopyConstructor, needs_implicit); 1582 FLAG(needsOverloadResolutionForCopyConstructor, 1583 needs_overload_resolution); 1584 if (!D->needsOverloadResolutionForCopyConstructor()) 1585 FLAG(defaultedCopyConstructorIsDeleted, defaulted_is_deleted); 1586 FLAG(implicitCopyConstructorHasConstParam, implicit_has_const_param); 1587 }); 1588 1589 AddChild([=] { 1590 { 1591 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1592 OS << "MoveConstructor"; 1593 } 1594 FLAG(hasMoveConstructor, exists); 1595 FLAG(hasSimpleMoveConstructor, simple); 1596 FLAG(hasTrivialMoveConstructor, trivial); 1597 FLAG(hasNonTrivialMoveConstructor, non_trivial); 1598 FLAG(hasUserDeclaredMoveConstructor, user_declared); 1599 FLAG(needsImplicitMoveConstructor, needs_implicit); 1600 FLAG(needsOverloadResolutionForMoveConstructor, 1601 needs_overload_resolution); 1602 if (!D->needsOverloadResolutionForMoveConstructor()) 1603 FLAG(defaultedMoveConstructorIsDeleted, defaulted_is_deleted); 1604 }); 1605 1606 AddChild([=] { 1607 { 1608 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1609 OS << "CopyAssignment"; 1610 } 1611 FLAG(hasTrivialCopyAssignment, trivial); 1612 FLAG(hasNonTrivialCopyAssignment, non_trivial); 1613 FLAG(hasCopyAssignmentWithConstParam, has_const_param); 1614 FLAG(hasUserDeclaredCopyAssignment, user_declared); 1615 FLAG(needsImplicitCopyAssignment, needs_implicit); 1616 FLAG(needsOverloadResolutionForCopyAssignment, needs_overload_resolution); 1617 FLAG(implicitCopyAssignmentHasConstParam, implicit_has_const_param); 1618 }); 1619 1620 AddChild([=] { 1621 { 1622 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1623 OS << "MoveAssignment"; 1624 } 1625 FLAG(hasMoveAssignment, exists); 1626 FLAG(hasSimpleMoveAssignment, simple); 1627 FLAG(hasTrivialMoveAssignment, trivial); 1628 FLAG(hasNonTrivialMoveAssignment, non_trivial); 1629 FLAG(hasUserDeclaredMoveAssignment, user_declared); 1630 FLAG(needsImplicitMoveAssignment, needs_implicit); 1631 FLAG(needsOverloadResolutionForMoveAssignment, needs_overload_resolution); 1632 }); 1633 1634 AddChild([=] { 1635 { 1636 ColorScope Color(OS, ShowColors, DeclKindNameColor); 1637 OS << "Destructor"; 1638 } 1639 FLAG(hasSimpleDestructor, simple); 1640 FLAG(hasIrrelevantDestructor, irrelevant); 1641 FLAG(hasTrivialDestructor, trivial); 1642 FLAG(hasNonTrivialDestructor, non_trivial); 1643 FLAG(hasUserDeclaredDestructor, user_declared); 1644 FLAG(needsImplicitDestructor, needs_implicit); 1645 FLAG(needsOverloadResolutionForDestructor, needs_overload_resolution); 1646 if (!D->needsOverloadResolutionForDestructor()) 1647 FLAG(defaultedDestructorIsDeleted, defaulted_is_deleted); 1648 }); 1649 }); 1650 1651 for (const auto &I : D->bases()) { 1652 AddChild([=] { 1653 if (I.isVirtual()) 1654 OS << "virtual "; 1655 dumpAccessSpecifier(I.getAccessSpecifier()); 1656 dumpType(I.getType()); 1657 if (I.isPackExpansion()) 1658 OS << "..."; 1659 }); 1660 } 1661 } 1662 1663 void TextNodeDumper::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { 1664 dumpName(D); 1665 } 1666 1667 void TextNodeDumper::VisitClassTemplateDecl(const ClassTemplateDecl *D) { 1668 dumpName(D); 1669 } 1670 1671 void TextNodeDumper::VisitVarTemplateDecl(const VarTemplateDecl *D) { 1672 dumpName(D); 1673 } 1674 1675 void TextNodeDumper::VisitBuiltinTemplateDecl(const BuiltinTemplateDecl *D) { 1676 dumpName(D); 1677 } 1678 1679 void TextNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { 1680 if (D->wasDeclaredWithTypename()) 1681 OS << " typename"; 1682 else 1683 OS << " class"; 1684 OS << " depth " << D->getDepth() << " index " << D->getIndex(); 1685 if (D->isParameterPack()) 1686 OS << " ..."; 1687 dumpName(D); 1688 } 1689 1690 void TextNodeDumper::VisitNonTypeTemplateParmDecl( 1691 const NonTypeTemplateParmDecl *D) { 1692 dumpType(D->getType()); 1693 OS << " depth " << D->getDepth() << " index " << D->getIndex(); 1694 if (D->isParameterPack()) 1695 OS << " ..."; 1696 dumpName(D); 1697 } 1698 1699 void TextNodeDumper::VisitTemplateTemplateParmDecl( 1700 const TemplateTemplateParmDecl *D) { 1701 OS << " depth " << D->getDepth() << " index " << D->getIndex(); 1702 if (D->isParameterPack()) 1703 OS << " ..."; 1704 dumpName(D); 1705 } 1706 1707 void TextNodeDumper::VisitUsingDecl(const UsingDecl *D) { 1708 OS << ' '; 1709 if (D->getQualifier()) 1710 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy()); 1711 OS << D->getNameAsString(); 1712 } 1713 1714 void TextNodeDumper::VisitUnresolvedUsingTypenameDecl( 1715 const UnresolvedUsingTypenameDecl *D) { 1716 OS << ' '; 1717 if (D->getQualifier()) 1718 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy()); 1719 OS << D->getNameAsString(); 1720 } 1721 1722 void TextNodeDumper::VisitUnresolvedUsingValueDecl( 1723 const UnresolvedUsingValueDecl *D) { 1724 OS << ' '; 1725 if (D->getQualifier()) 1726 D->getQualifier()->print(OS, D->getASTContext().getPrintingPolicy()); 1727 OS << D->getNameAsString(); 1728 dumpType(D->getType()); 1729 } 1730 1731 void TextNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *D) { 1732 OS << ' '; 1733 dumpBareDeclRef(D->getTargetDecl()); 1734 } 1735 1736 void TextNodeDumper::VisitConstructorUsingShadowDecl( 1737 const ConstructorUsingShadowDecl *D) { 1738 if (D->constructsVirtualBase()) 1739 OS << " virtual"; 1740 1741 AddChild([=] { 1742 OS << "target "; 1743 dumpBareDeclRef(D->getTargetDecl()); 1744 }); 1745 1746 AddChild([=] { 1747 OS << "nominated "; 1748 dumpBareDeclRef(D->getNominatedBaseClass()); 1749 OS << ' '; 1750 dumpBareDeclRef(D->getNominatedBaseClassShadowDecl()); 1751 }); 1752 1753 AddChild([=] { 1754 OS << "constructed "; 1755 dumpBareDeclRef(D->getConstructedBaseClass()); 1756 OS << ' '; 1757 dumpBareDeclRef(D->getConstructedBaseClassShadowDecl()); 1758 }); 1759 } 1760 1761 void TextNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *D) { 1762 switch (D->getLanguage()) { 1763 case LinkageSpecDecl::lang_c: 1764 OS << " C"; 1765 break; 1766 case LinkageSpecDecl::lang_cxx: 1767 OS << " C++"; 1768 break; 1769 } 1770 } 1771 1772 void TextNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *D) { 1773 OS << ' '; 1774 dumpAccessSpecifier(D->getAccess()); 1775 } 1776 1777 void TextNodeDumper::VisitFriendDecl(const FriendDecl *D) { 1778 if (TypeSourceInfo *T = D->getFriendType()) 1779 dumpType(T->getType()); 1780 } 1781 1782 void TextNodeDumper::VisitObjCIvarDecl(const ObjCIvarDecl *D) { 1783 dumpName(D); 1784 dumpType(D->getType()); 1785 if (D->getSynthesize()) 1786 OS << " synthesize"; 1787 1788 switch (D->getAccessControl()) { 1789 case ObjCIvarDecl::None: 1790 OS << " none"; 1791 break; 1792 case ObjCIvarDecl::Private: 1793 OS << " private"; 1794 break; 1795 case ObjCIvarDecl::Protected: 1796 OS << " protected"; 1797 break; 1798 case ObjCIvarDecl::Public: 1799 OS << " public"; 1800 break; 1801 case ObjCIvarDecl::Package: 1802 OS << " package"; 1803 break; 1804 } 1805 } 1806 1807 void TextNodeDumper::VisitObjCMethodDecl(const ObjCMethodDecl *D) { 1808 if (D->isInstanceMethod()) 1809 OS << " -"; 1810 else 1811 OS << " +"; 1812 dumpName(D); 1813 dumpType(D->getReturnType()); 1814 1815 if (D->isVariadic()) 1816 OS << " variadic"; 1817 } 1818 1819 void TextNodeDumper::VisitObjCTypeParamDecl(const ObjCTypeParamDecl *D) { 1820 dumpName(D); 1821 switch (D->getVariance()) { 1822 case ObjCTypeParamVariance::Invariant: 1823 break; 1824 1825 case ObjCTypeParamVariance::Covariant: 1826 OS << " covariant"; 1827 break; 1828 1829 case ObjCTypeParamVariance::Contravariant: 1830 OS << " contravariant"; 1831 break; 1832 } 1833 1834 if (D->hasExplicitBound()) 1835 OS << " bounded"; 1836 dumpType(D->getUnderlyingType()); 1837 } 1838 1839 void TextNodeDumper::VisitObjCCategoryDecl(const ObjCCategoryDecl *D) { 1840 dumpName(D); 1841 dumpDeclRef(D->getClassInterface()); 1842 dumpDeclRef(D->getImplementation()); 1843 for (const auto *P : D->protocols()) 1844 dumpDeclRef(P); 1845 } 1846 1847 void TextNodeDumper::VisitObjCCategoryImplDecl(const ObjCCategoryImplDecl *D) { 1848 dumpName(D); 1849 dumpDeclRef(D->getClassInterface()); 1850 dumpDeclRef(D->getCategoryDecl()); 1851 } 1852 1853 void TextNodeDumper::VisitObjCProtocolDecl(const ObjCProtocolDecl *D) { 1854 dumpName(D); 1855 1856 for (const auto *Child : D->protocols()) 1857 dumpDeclRef(Child); 1858 } 1859 1860 void TextNodeDumper::VisitObjCInterfaceDecl(const ObjCInterfaceDecl *D) { 1861 dumpName(D); 1862 dumpDeclRef(D->getSuperClass(), "super"); 1863 1864 dumpDeclRef(D->getImplementation()); 1865 for (const auto *Child : D->protocols()) 1866 dumpDeclRef(Child); 1867 } 1868 1869 void TextNodeDumper::VisitObjCImplementationDecl( 1870 const ObjCImplementationDecl *D) { 1871 dumpName(D); 1872 dumpDeclRef(D->getSuperClass(), "super"); 1873 dumpDeclRef(D->getClassInterface()); 1874 } 1875 1876 void TextNodeDumper::VisitObjCCompatibleAliasDecl( 1877 const ObjCCompatibleAliasDecl *D) { 1878 dumpName(D); 1879 dumpDeclRef(D->getClassInterface()); 1880 } 1881 1882 void TextNodeDumper::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { 1883 dumpName(D); 1884 dumpType(D->getType()); 1885 1886 if (D->getPropertyImplementation() == ObjCPropertyDecl::Required) 1887 OS << " required"; 1888 else if (D->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1889 OS << " optional"; 1890 1891 ObjCPropertyDecl::PropertyAttributeKind Attrs = D->getPropertyAttributes(); 1892 if (Attrs != ObjCPropertyDecl::OBJC_PR_noattr) { 1893 if (Attrs & ObjCPropertyDecl::OBJC_PR_readonly) 1894 OS << " readonly"; 1895 if (Attrs & ObjCPropertyDecl::OBJC_PR_assign) 1896 OS << " assign"; 1897 if (Attrs & ObjCPropertyDecl::OBJC_PR_readwrite) 1898 OS << " readwrite"; 1899 if (Attrs & ObjCPropertyDecl::OBJC_PR_retain) 1900 OS << " retain"; 1901 if (Attrs & ObjCPropertyDecl::OBJC_PR_copy) 1902 OS << " copy"; 1903 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) 1904 OS << " nonatomic"; 1905 if (Attrs & ObjCPropertyDecl::OBJC_PR_atomic) 1906 OS << " atomic"; 1907 if (Attrs & ObjCPropertyDecl::OBJC_PR_weak) 1908 OS << " weak"; 1909 if (Attrs & ObjCPropertyDecl::OBJC_PR_strong) 1910 OS << " strong"; 1911 if (Attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) 1912 OS << " unsafe_unretained"; 1913 if (Attrs & ObjCPropertyDecl::OBJC_PR_class) 1914 OS << " class"; 1915 if (Attrs & ObjCPropertyDecl::OBJC_PR_getter) 1916 dumpDeclRef(D->getGetterMethodDecl(), "getter"); 1917 if (Attrs & ObjCPropertyDecl::OBJC_PR_setter) 1918 dumpDeclRef(D->getSetterMethodDecl(), "setter"); 1919 } 1920 } 1921 1922 void TextNodeDumper::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 1923 dumpName(D->getPropertyDecl()); 1924 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) 1925 OS << " synthesize"; 1926 else 1927 OS << " dynamic"; 1928 dumpDeclRef(D->getPropertyDecl()); 1929 dumpDeclRef(D->getPropertyIvarDecl()); 1930 } 1931 1932 void TextNodeDumper::VisitBlockDecl(const BlockDecl *D) { 1933 if (D->isVariadic()) 1934 OS << " variadic"; 1935 1936 if (D->capturesCXXThis()) 1937 OS << " captures_this"; 1938 } 1939 1940 void TextNodeDumper::VisitConceptDecl(const ConceptDecl *D) { 1941 dumpName(D); 1942 } 1943