1 //===---- StmtProfile.cpp - Profile implementation for Stmt 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 Stmt::Profile method, which builds a unique bit 11 // representation that identifies a statement/expression. 12 // 13 //===----------------------------------------------------------------------===// 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/DeclCXX.h" 16 #include "clang/AST/DeclObjC.h" 17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/AST/Expr.h" 19 #include "clang/AST/ExprCXX.h" 20 #include "clang/AST/ExprObjC.h" 21 #include "clang/AST/StmtVisitor.h" 22 #include "llvm/ADT/FoldingSet.h" 23 using namespace clang; 24 25 namespace { 26 class StmtProfiler : public ConstStmtVisitor<StmtProfiler> { 27 llvm::FoldingSetNodeID &ID; 28 const ASTContext &Context; 29 bool Canonical; 30 31 public: 32 StmtProfiler(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 33 bool Canonical) 34 : ID(ID), Context(Context), Canonical(Canonical) { } 35 36 void VisitStmt(const Stmt *S); 37 38 #define STMT(Node, Base) void Visit##Node(const Node *S); 39 #include "clang/AST/StmtNodes.inc" 40 41 /// \brief Visit a declaration that is referenced within an expression 42 /// or statement. 43 void VisitDecl(const Decl *D); 44 45 /// \brief Visit a type that is referenced within an expression or 46 /// statement. 47 void VisitType(QualType T); 48 49 /// \brief Visit a name that occurs within an expression or statement. 50 void VisitName(DeclarationName Name); 51 52 /// \brief Visit a nested-name-specifier that occurs within an expression 53 /// or statement. 54 void VisitNestedNameSpecifier(NestedNameSpecifier *NNS); 55 56 /// \brief Visit a template name that occurs within an expression or 57 /// statement. 58 void VisitTemplateName(TemplateName Name); 59 60 /// \brief Visit template arguments that occur within an expression or 61 /// statement. 62 void VisitTemplateArguments(const TemplateArgumentLoc *Args, 63 unsigned NumArgs); 64 65 /// \brief Visit a single template argument. 66 void VisitTemplateArgument(const TemplateArgument &Arg); 67 }; 68 } 69 70 void StmtProfiler::VisitStmt(const Stmt *S) { 71 ID.AddInteger(S->getStmtClass()); 72 for (Stmt::const_child_range C = S->children(); C; ++C) { 73 if (*C) 74 Visit(*C); 75 else 76 ID.AddInteger(0); 77 } 78 } 79 80 void StmtProfiler::VisitDeclStmt(const DeclStmt *S) { 81 VisitStmt(S); 82 for (DeclStmt::const_decl_iterator D = S->decl_begin(), DEnd = S->decl_end(); 83 D != DEnd; ++D) 84 VisitDecl(*D); 85 } 86 87 void StmtProfiler::VisitNullStmt(const NullStmt *S) { 88 VisitStmt(S); 89 } 90 91 void StmtProfiler::VisitCompoundStmt(const CompoundStmt *S) { 92 VisitStmt(S); 93 } 94 95 void StmtProfiler::VisitSwitchCase(const SwitchCase *S) { 96 VisitStmt(S); 97 } 98 99 void StmtProfiler::VisitCaseStmt(const CaseStmt *S) { 100 VisitStmt(S); 101 } 102 103 void StmtProfiler::VisitDefaultStmt(const DefaultStmt *S) { 104 VisitStmt(S); 105 } 106 107 void StmtProfiler::VisitLabelStmt(const LabelStmt *S) { 108 VisitStmt(S); 109 VisitDecl(S->getDecl()); 110 } 111 112 void StmtProfiler::VisitAttributedStmt(const AttributedStmt *S) { 113 VisitStmt(S); 114 // TODO: maybe visit attributes? 115 } 116 117 void StmtProfiler::VisitIfStmt(const IfStmt *S) { 118 VisitStmt(S); 119 VisitDecl(S->getConditionVariable()); 120 } 121 122 void StmtProfiler::VisitSwitchStmt(const SwitchStmt *S) { 123 VisitStmt(S); 124 VisitDecl(S->getConditionVariable()); 125 } 126 127 void StmtProfiler::VisitWhileStmt(const WhileStmt *S) { 128 VisitStmt(S); 129 VisitDecl(S->getConditionVariable()); 130 } 131 132 void StmtProfiler::VisitDoStmt(const DoStmt *S) { 133 VisitStmt(S); 134 } 135 136 void StmtProfiler::VisitForStmt(const ForStmt *S) { 137 VisitStmt(S); 138 } 139 140 void StmtProfiler::VisitGotoStmt(const GotoStmt *S) { 141 VisitStmt(S); 142 VisitDecl(S->getLabel()); 143 } 144 145 void StmtProfiler::VisitIndirectGotoStmt(const IndirectGotoStmt *S) { 146 VisitStmt(S); 147 } 148 149 void StmtProfiler::VisitContinueStmt(const ContinueStmt *S) { 150 VisitStmt(S); 151 } 152 153 void StmtProfiler::VisitBreakStmt(const BreakStmt *S) { 154 VisitStmt(S); 155 } 156 157 void StmtProfiler::VisitReturnStmt(const ReturnStmt *S) { 158 VisitStmt(S); 159 } 160 161 void StmtProfiler::VisitGCCAsmStmt(const GCCAsmStmt *S) { 162 VisitStmt(S); 163 ID.AddBoolean(S->isVolatile()); 164 ID.AddBoolean(S->isSimple()); 165 VisitStringLiteral(S->getAsmString()); 166 ID.AddInteger(S->getNumOutputs()); 167 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) { 168 ID.AddString(S->getOutputName(I)); 169 VisitStringLiteral(S->getOutputConstraintLiteral(I)); 170 } 171 ID.AddInteger(S->getNumInputs()); 172 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) { 173 ID.AddString(S->getInputName(I)); 174 VisitStringLiteral(S->getInputConstraintLiteral(I)); 175 } 176 ID.AddInteger(S->getNumClobbers()); 177 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I) 178 VisitStringLiteral(S->getClobberStringLiteral(I)); 179 } 180 181 void StmtProfiler::VisitMSAsmStmt(const MSAsmStmt *S) { 182 // FIXME: Implement MS style inline asm statement profiler. 183 VisitStmt(S); 184 } 185 186 void StmtProfiler::VisitCXXCatchStmt(const CXXCatchStmt *S) { 187 VisitStmt(S); 188 VisitType(S->getCaughtType()); 189 } 190 191 void StmtProfiler::VisitCXXTryStmt(const CXXTryStmt *S) { 192 VisitStmt(S); 193 } 194 195 void StmtProfiler::VisitCXXForRangeStmt(const CXXForRangeStmt *S) { 196 VisitStmt(S); 197 } 198 199 void StmtProfiler::VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) { 200 VisitStmt(S); 201 ID.AddBoolean(S->isIfExists()); 202 VisitNestedNameSpecifier(S->getQualifierLoc().getNestedNameSpecifier()); 203 VisitName(S->getNameInfo().getName()); 204 } 205 206 void StmtProfiler::VisitSEHTryStmt(const SEHTryStmt *S) { 207 VisitStmt(S); 208 } 209 210 void StmtProfiler::VisitSEHFinallyStmt(const SEHFinallyStmt *S) { 211 VisitStmt(S); 212 } 213 214 void StmtProfiler::VisitSEHExceptStmt(const SEHExceptStmt *S) { 215 VisitStmt(S); 216 } 217 218 void StmtProfiler::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { 219 VisitStmt(S); 220 } 221 222 void StmtProfiler::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *S) { 223 VisitStmt(S); 224 ID.AddBoolean(S->hasEllipsis()); 225 if (S->getCatchParamDecl()) 226 VisitType(S->getCatchParamDecl()->getType()); 227 } 228 229 void StmtProfiler::VisitObjCAtFinallyStmt(const ObjCAtFinallyStmt *S) { 230 VisitStmt(S); 231 } 232 233 void StmtProfiler::VisitObjCAtTryStmt(const ObjCAtTryStmt *S) { 234 VisitStmt(S); 235 } 236 237 void 238 StmtProfiler::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S) { 239 VisitStmt(S); 240 } 241 242 void StmtProfiler::VisitObjCAtThrowStmt(const ObjCAtThrowStmt *S) { 243 VisitStmt(S); 244 } 245 246 void 247 StmtProfiler::VisitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt *S) { 248 VisitStmt(S); 249 } 250 251 void StmtProfiler::VisitExpr(const Expr *S) { 252 VisitStmt(S); 253 } 254 255 void StmtProfiler::VisitDeclRefExpr(const DeclRefExpr *S) { 256 VisitExpr(S); 257 if (!Canonical) 258 VisitNestedNameSpecifier(S->getQualifier()); 259 VisitDecl(S->getDecl()); 260 if (!Canonical) 261 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 262 } 263 264 void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) { 265 VisitExpr(S); 266 ID.AddInteger(S->getIdentType()); 267 } 268 269 void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) { 270 VisitExpr(S); 271 S->getValue().Profile(ID); 272 } 273 274 void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) { 275 VisitExpr(S); 276 ID.AddInteger(S->getKind()); 277 ID.AddInteger(S->getValue()); 278 } 279 280 void StmtProfiler::VisitFloatingLiteral(const FloatingLiteral *S) { 281 VisitExpr(S); 282 S->getValue().Profile(ID); 283 ID.AddBoolean(S->isExact()); 284 } 285 286 void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) { 287 VisitExpr(S); 288 } 289 290 void StmtProfiler::VisitStringLiteral(const StringLiteral *S) { 291 VisitExpr(S); 292 ID.AddString(S->getBytes()); 293 ID.AddInteger(S->getKind()); 294 } 295 296 void StmtProfiler::VisitParenExpr(const ParenExpr *S) { 297 VisitExpr(S); 298 } 299 300 void StmtProfiler::VisitParenListExpr(const ParenListExpr *S) { 301 VisitExpr(S); 302 } 303 304 void StmtProfiler::VisitUnaryOperator(const UnaryOperator *S) { 305 VisitExpr(S); 306 ID.AddInteger(S->getOpcode()); 307 } 308 309 void StmtProfiler::VisitOffsetOfExpr(const OffsetOfExpr *S) { 310 VisitType(S->getTypeSourceInfo()->getType()); 311 unsigned n = S->getNumComponents(); 312 for (unsigned i = 0; i < n; ++i) { 313 const OffsetOfExpr::OffsetOfNode& ON = S->getComponent(i); 314 ID.AddInteger(ON.getKind()); 315 switch (ON.getKind()) { 316 case OffsetOfExpr::OffsetOfNode::Array: 317 // Expressions handled below. 318 break; 319 320 case OffsetOfExpr::OffsetOfNode::Field: 321 VisitDecl(ON.getField()); 322 break; 323 324 case OffsetOfExpr::OffsetOfNode::Identifier: 325 ID.AddPointer(ON.getFieldName()); 326 break; 327 328 case OffsetOfExpr::OffsetOfNode::Base: 329 // These nodes are implicit, and therefore don't need profiling. 330 break; 331 } 332 } 333 334 VisitExpr(S); 335 } 336 337 void 338 StmtProfiler::VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *S) { 339 VisitExpr(S); 340 ID.AddInteger(S->getKind()); 341 if (S->isArgumentType()) 342 VisitType(S->getArgumentType()); 343 } 344 345 void StmtProfiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *S) { 346 VisitExpr(S); 347 } 348 349 void StmtProfiler::VisitCallExpr(const CallExpr *S) { 350 VisitExpr(S); 351 } 352 353 void StmtProfiler::VisitMemberExpr(const MemberExpr *S) { 354 VisitExpr(S); 355 VisitDecl(S->getMemberDecl()); 356 if (!Canonical) 357 VisitNestedNameSpecifier(S->getQualifier()); 358 ID.AddBoolean(S->isArrow()); 359 } 360 361 void StmtProfiler::VisitCompoundLiteralExpr(const CompoundLiteralExpr *S) { 362 VisitExpr(S); 363 ID.AddBoolean(S->isFileScope()); 364 } 365 366 void StmtProfiler::VisitCastExpr(const CastExpr *S) { 367 VisitExpr(S); 368 } 369 370 void StmtProfiler::VisitImplicitCastExpr(const ImplicitCastExpr *S) { 371 VisitCastExpr(S); 372 ID.AddInteger(S->getValueKind()); 373 } 374 375 void StmtProfiler::VisitExplicitCastExpr(const ExplicitCastExpr *S) { 376 VisitCastExpr(S); 377 VisitType(S->getTypeAsWritten()); 378 } 379 380 void StmtProfiler::VisitCStyleCastExpr(const CStyleCastExpr *S) { 381 VisitExplicitCastExpr(S); 382 } 383 384 void StmtProfiler::VisitBinaryOperator(const BinaryOperator *S) { 385 VisitExpr(S); 386 ID.AddInteger(S->getOpcode()); 387 } 388 389 void 390 StmtProfiler::VisitCompoundAssignOperator(const CompoundAssignOperator *S) { 391 VisitBinaryOperator(S); 392 } 393 394 void StmtProfiler::VisitConditionalOperator(const ConditionalOperator *S) { 395 VisitExpr(S); 396 } 397 398 void StmtProfiler::VisitBinaryConditionalOperator( 399 const BinaryConditionalOperator *S) { 400 VisitExpr(S); 401 } 402 403 void StmtProfiler::VisitAddrLabelExpr(const AddrLabelExpr *S) { 404 VisitExpr(S); 405 VisitDecl(S->getLabel()); 406 } 407 408 void StmtProfiler::VisitStmtExpr(const StmtExpr *S) { 409 VisitExpr(S); 410 } 411 412 void StmtProfiler::VisitShuffleVectorExpr(const ShuffleVectorExpr *S) { 413 VisitExpr(S); 414 } 415 416 void StmtProfiler::VisitChooseExpr(const ChooseExpr *S) { 417 VisitExpr(S); 418 } 419 420 void StmtProfiler::VisitGNUNullExpr(const GNUNullExpr *S) { 421 VisitExpr(S); 422 } 423 424 void StmtProfiler::VisitVAArgExpr(const VAArgExpr *S) { 425 VisitExpr(S); 426 } 427 428 void StmtProfiler::VisitInitListExpr(const InitListExpr *S) { 429 if (S->getSyntacticForm()) { 430 VisitInitListExpr(S->getSyntacticForm()); 431 return; 432 } 433 434 VisitExpr(S); 435 } 436 437 void StmtProfiler::VisitDesignatedInitExpr(const DesignatedInitExpr *S) { 438 VisitExpr(S); 439 ID.AddBoolean(S->usesGNUSyntax()); 440 for (DesignatedInitExpr::const_designators_iterator D = 441 S->designators_begin(), DEnd = S->designators_end(); 442 D != DEnd; ++D) { 443 if (D->isFieldDesignator()) { 444 ID.AddInteger(0); 445 VisitName(D->getFieldName()); 446 continue; 447 } 448 449 if (D->isArrayDesignator()) { 450 ID.AddInteger(1); 451 } else { 452 assert(D->isArrayRangeDesignator()); 453 ID.AddInteger(2); 454 } 455 ID.AddInteger(D->getFirstExprIndex()); 456 } 457 } 458 459 void StmtProfiler::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *S) { 460 VisitExpr(S); 461 } 462 463 void StmtProfiler::VisitExtVectorElementExpr(const ExtVectorElementExpr *S) { 464 VisitExpr(S); 465 VisitName(&S->getAccessor()); 466 } 467 468 void StmtProfiler::VisitBlockExpr(const BlockExpr *S) { 469 VisitExpr(S); 470 VisitDecl(S->getBlockDecl()); 471 } 472 473 void StmtProfiler::VisitGenericSelectionExpr(const GenericSelectionExpr *S) { 474 VisitExpr(S); 475 for (unsigned i = 0; i != S->getNumAssocs(); ++i) { 476 QualType T = S->getAssocType(i); 477 if (T.isNull()) 478 ID.AddPointer(0); 479 else 480 VisitType(T); 481 VisitExpr(S->getAssocExpr(i)); 482 } 483 } 484 485 void StmtProfiler::VisitPseudoObjectExpr(const PseudoObjectExpr *S) { 486 VisitExpr(S); 487 for (PseudoObjectExpr::const_semantics_iterator 488 i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) 489 // Normally, we would not profile the source expressions of OVEs. 490 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(*i)) 491 Visit(OVE->getSourceExpr()); 492 } 493 494 void StmtProfiler::VisitAtomicExpr(const AtomicExpr *S) { 495 VisitExpr(S); 496 ID.AddInteger(S->getOp()); 497 } 498 499 static Stmt::StmtClass DecodeOperatorCall(const CXXOperatorCallExpr *S, 500 UnaryOperatorKind &UnaryOp, 501 BinaryOperatorKind &BinaryOp) { 502 switch (S->getOperator()) { 503 case OO_None: 504 case OO_New: 505 case OO_Delete: 506 case OO_Array_New: 507 case OO_Array_Delete: 508 case OO_Arrow: 509 case OO_Call: 510 case OO_Conditional: 511 case NUM_OVERLOADED_OPERATORS: 512 llvm_unreachable("Invalid operator call kind"); 513 514 case OO_Plus: 515 if (S->getNumArgs() == 1) { 516 UnaryOp = UO_Plus; 517 return Stmt::UnaryOperatorClass; 518 } 519 520 BinaryOp = BO_Add; 521 return Stmt::BinaryOperatorClass; 522 523 case OO_Minus: 524 if (S->getNumArgs() == 1) { 525 UnaryOp = UO_Minus; 526 return Stmt::UnaryOperatorClass; 527 } 528 529 BinaryOp = BO_Sub; 530 return Stmt::BinaryOperatorClass; 531 532 case OO_Star: 533 if (S->getNumArgs() == 1) { 534 UnaryOp = UO_Minus; 535 return Stmt::UnaryOperatorClass; 536 } 537 538 BinaryOp = BO_Sub; 539 return Stmt::BinaryOperatorClass; 540 541 case OO_Slash: 542 BinaryOp = BO_Div; 543 return Stmt::BinaryOperatorClass; 544 545 case OO_Percent: 546 BinaryOp = BO_Rem; 547 return Stmt::BinaryOperatorClass; 548 549 case OO_Caret: 550 BinaryOp = BO_Xor; 551 return Stmt::BinaryOperatorClass; 552 553 case OO_Amp: 554 if (S->getNumArgs() == 1) { 555 UnaryOp = UO_AddrOf; 556 return Stmt::UnaryOperatorClass; 557 } 558 559 BinaryOp = BO_And; 560 return Stmt::BinaryOperatorClass; 561 562 case OO_Pipe: 563 BinaryOp = BO_Or; 564 return Stmt::BinaryOperatorClass; 565 566 case OO_Tilde: 567 UnaryOp = UO_Not; 568 return Stmt::UnaryOperatorClass; 569 570 case OO_Exclaim: 571 UnaryOp = UO_LNot; 572 return Stmt::UnaryOperatorClass; 573 574 case OO_Equal: 575 BinaryOp = BO_Assign; 576 return Stmt::BinaryOperatorClass; 577 578 case OO_Less: 579 BinaryOp = BO_LT; 580 return Stmt::BinaryOperatorClass; 581 582 case OO_Greater: 583 BinaryOp = BO_GT; 584 return Stmt::BinaryOperatorClass; 585 586 case OO_PlusEqual: 587 BinaryOp = BO_AddAssign; 588 return Stmt::CompoundAssignOperatorClass; 589 590 case OO_MinusEqual: 591 BinaryOp = BO_SubAssign; 592 return Stmt::CompoundAssignOperatorClass; 593 594 case OO_StarEqual: 595 BinaryOp = BO_MulAssign; 596 return Stmt::CompoundAssignOperatorClass; 597 598 case OO_SlashEqual: 599 BinaryOp = BO_DivAssign; 600 return Stmt::CompoundAssignOperatorClass; 601 602 case OO_PercentEqual: 603 BinaryOp = BO_RemAssign; 604 return Stmt::CompoundAssignOperatorClass; 605 606 case OO_CaretEqual: 607 BinaryOp = BO_XorAssign; 608 return Stmt::CompoundAssignOperatorClass; 609 610 case OO_AmpEqual: 611 BinaryOp = BO_AndAssign; 612 return Stmt::CompoundAssignOperatorClass; 613 614 case OO_PipeEqual: 615 BinaryOp = BO_OrAssign; 616 return Stmt::CompoundAssignOperatorClass; 617 618 case OO_LessLess: 619 BinaryOp = BO_Shl; 620 return Stmt::BinaryOperatorClass; 621 622 case OO_GreaterGreater: 623 BinaryOp = BO_Shr; 624 return Stmt::BinaryOperatorClass; 625 626 case OO_LessLessEqual: 627 BinaryOp = BO_ShlAssign; 628 return Stmt::CompoundAssignOperatorClass; 629 630 case OO_GreaterGreaterEqual: 631 BinaryOp = BO_ShrAssign; 632 return Stmt::CompoundAssignOperatorClass; 633 634 case OO_EqualEqual: 635 BinaryOp = BO_EQ; 636 return Stmt::BinaryOperatorClass; 637 638 case OO_ExclaimEqual: 639 BinaryOp = BO_NE; 640 return Stmt::BinaryOperatorClass; 641 642 case OO_LessEqual: 643 BinaryOp = BO_LE; 644 return Stmt::BinaryOperatorClass; 645 646 case OO_GreaterEqual: 647 BinaryOp = BO_GE; 648 return Stmt::BinaryOperatorClass; 649 650 case OO_AmpAmp: 651 BinaryOp = BO_LAnd; 652 return Stmt::BinaryOperatorClass; 653 654 case OO_PipePipe: 655 BinaryOp = BO_LOr; 656 return Stmt::BinaryOperatorClass; 657 658 case OO_PlusPlus: 659 UnaryOp = S->getNumArgs() == 1? UO_PreInc 660 : UO_PostInc; 661 return Stmt::UnaryOperatorClass; 662 663 case OO_MinusMinus: 664 UnaryOp = S->getNumArgs() == 1? UO_PreDec 665 : UO_PostDec; 666 return Stmt::UnaryOperatorClass; 667 668 case OO_Comma: 669 BinaryOp = BO_Comma; 670 return Stmt::BinaryOperatorClass; 671 672 673 case OO_ArrowStar: 674 BinaryOp = BO_PtrMemI; 675 return Stmt::BinaryOperatorClass; 676 677 case OO_Subscript: 678 return Stmt::ArraySubscriptExprClass; 679 } 680 681 llvm_unreachable("Invalid overloaded operator expression"); 682 } 683 684 685 void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) { 686 if (S->isTypeDependent()) { 687 // Type-dependent operator calls are profiled like their underlying 688 // syntactic operator. 689 UnaryOperatorKind UnaryOp = UO_Extension; 690 BinaryOperatorKind BinaryOp = BO_Comma; 691 Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp); 692 693 ID.AddInteger(SC); 694 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 695 Visit(S->getArg(I)); 696 if (SC == Stmt::UnaryOperatorClass) 697 ID.AddInteger(UnaryOp); 698 else if (SC == Stmt::BinaryOperatorClass || 699 SC == Stmt::CompoundAssignOperatorClass) 700 ID.AddInteger(BinaryOp); 701 else 702 assert(SC == Stmt::ArraySubscriptExprClass); 703 704 return; 705 } 706 707 VisitCallExpr(S); 708 ID.AddInteger(S->getOperator()); 709 } 710 711 void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) { 712 VisitCallExpr(S); 713 } 714 715 void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) { 716 VisitCallExpr(S); 717 } 718 719 void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) { 720 VisitExpr(S); 721 } 722 723 void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) { 724 VisitExplicitCastExpr(S); 725 } 726 727 void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) { 728 VisitCXXNamedCastExpr(S); 729 } 730 731 void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) { 732 VisitCXXNamedCastExpr(S); 733 } 734 735 void 736 StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) { 737 VisitCXXNamedCastExpr(S); 738 } 739 740 void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) { 741 VisitCXXNamedCastExpr(S); 742 } 743 744 void StmtProfiler::VisitUserDefinedLiteral(const UserDefinedLiteral *S) { 745 VisitCallExpr(S); 746 } 747 748 void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) { 749 VisitExpr(S); 750 ID.AddBoolean(S->getValue()); 751 } 752 753 void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) { 754 VisitExpr(S); 755 } 756 757 void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) { 758 VisitExpr(S); 759 if (S->isTypeOperand()) 760 VisitType(S->getTypeOperand()); 761 } 762 763 void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) { 764 VisitExpr(S); 765 if (S->isTypeOperand()) 766 VisitType(S->getTypeOperand()); 767 } 768 769 void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) { 770 VisitExpr(S); 771 ID.AddBoolean(S->isImplicit()); 772 } 773 774 void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) { 775 VisitExpr(S); 776 } 777 778 void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) { 779 VisitExpr(S); 780 VisitDecl(S->getParam()); 781 } 782 783 void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) { 784 VisitExpr(S); 785 VisitDecl( 786 const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor())); 787 } 788 789 void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) { 790 VisitExpr(S); 791 VisitDecl(S->getConstructor()); 792 ID.AddBoolean(S->isElidable()); 793 } 794 795 void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) { 796 VisitExplicitCastExpr(S); 797 } 798 799 void 800 StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) { 801 VisitCXXConstructExpr(S); 802 } 803 804 void 805 StmtProfiler::VisitLambdaExpr(const LambdaExpr *S) { 806 VisitExpr(S); 807 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(), 808 CEnd = S->explicit_capture_end(); 809 C != CEnd; ++C) { 810 ID.AddInteger(C->getCaptureKind()); 811 if (C->capturesVariable()) { 812 VisitDecl(C->getCapturedVar()); 813 ID.AddBoolean(C->isPackExpansion()); 814 } 815 } 816 // Note: If we actually needed to be able to match lambda 817 // expressions, we would have to consider parameters and return type 818 // here, among other things. 819 VisitStmt(S->getBody()); 820 } 821 822 void 823 StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) { 824 VisitExpr(S); 825 } 826 827 void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) { 828 VisitExpr(S); 829 ID.AddBoolean(S->isGlobalDelete()); 830 ID.AddBoolean(S->isArrayForm()); 831 VisitDecl(S->getOperatorDelete()); 832 } 833 834 835 void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) { 836 VisitExpr(S); 837 VisitType(S->getAllocatedType()); 838 VisitDecl(S->getOperatorNew()); 839 VisitDecl(S->getOperatorDelete()); 840 ID.AddBoolean(S->isArray()); 841 ID.AddInteger(S->getNumPlacementArgs()); 842 ID.AddBoolean(S->isGlobalNew()); 843 ID.AddBoolean(S->isParenTypeId()); 844 ID.AddInteger(S->getInitializationStyle()); 845 } 846 847 void 848 StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) { 849 VisitExpr(S); 850 ID.AddBoolean(S->isArrow()); 851 VisitNestedNameSpecifier(S->getQualifier()); 852 VisitType(S->getDestroyedType()); 853 } 854 855 void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) { 856 VisitExpr(S); 857 VisitNestedNameSpecifier(S->getQualifier()); 858 VisitName(S->getName()); 859 ID.AddBoolean(S->hasExplicitTemplateArgs()); 860 if (S->hasExplicitTemplateArgs()) 861 VisitTemplateArguments(S->getExplicitTemplateArgs().getTemplateArgs(), 862 S->getExplicitTemplateArgs().NumTemplateArgs); 863 } 864 865 void 866 StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) { 867 VisitOverloadExpr(S); 868 } 869 870 void StmtProfiler::VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *S) { 871 VisitExpr(S); 872 ID.AddInteger(S->getTrait()); 873 VisitType(S->getQueriedType()); 874 } 875 876 void StmtProfiler::VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *S) { 877 VisitExpr(S); 878 ID.AddInteger(S->getTrait()); 879 VisitType(S->getLhsType()); 880 VisitType(S->getRhsType()); 881 } 882 883 void StmtProfiler::VisitTypeTraitExpr(const TypeTraitExpr *S) { 884 VisitExpr(S); 885 ID.AddInteger(S->getTrait()); 886 ID.AddInteger(S->getNumArgs()); 887 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 888 VisitType(S->getArg(I)->getType()); 889 } 890 891 void StmtProfiler::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *S) { 892 VisitExpr(S); 893 ID.AddInteger(S->getTrait()); 894 VisitType(S->getQueriedType()); 895 } 896 897 void StmtProfiler::VisitExpressionTraitExpr(const ExpressionTraitExpr *S) { 898 VisitExpr(S); 899 ID.AddInteger(S->getTrait()); 900 VisitExpr(S->getQueriedExpression()); 901 } 902 903 void StmtProfiler::VisitDependentScopeDeclRefExpr( 904 const DependentScopeDeclRefExpr *S) { 905 VisitExpr(S); 906 VisitName(S->getDeclName()); 907 VisitNestedNameSpecifier(S->getQualifier()); 908 ID.AddBoolean(S->hasExplicitTemplateArgs()); 909 if (S->hasExplicitTemplateArgs()) 910 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 911 } 912 913 void StmtProfiler::VisitExprWithCleanups(const ExprWithCleanups *S) { 914 VisitExpr(S); 915 } 916 917 void StmtProfiler::VisitCXXUnresolvedConstructExpr( 918 const CXXUnresolvedConstructExpr *S) { 919 VisitExpr(S); 920 VisitType(S->getTypeAsWritten()); 921 } 922 923 void StmtProfiler::VisitCXXDependentScopeMemberExpr( 924 const CXXDependentScopeMemberExpr *S) { 925 ID.AddBoolean(S->isImplicitAccess()); 926 if (!S->isImplicitAccess()) { 927 VisitExpr(S); 928 ID.AddBoolean(S->isArrow()); 929 } 930 VisitNestedNameSpecifier(S->getQualifier()); 931 VisitName(S->getMember()); 932 ID.AddBoolean(S->hasExplicitTemplateArgs()); 933 if (S->hasExplicitTemplateArgs()) 934 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 935 } 936 937 void StmtProfiler::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *S) { 938 ID.AddBoolean(S->isImplicitAccess()); 939 if (!S->isImplicitAccess()) { 940 VisitExpr(S); 941 ID.AddBoolean(S->isArrow()); 942 } 943 VisitNestedNameSpecifier(S->getQualifier()); 944 VisitName(S->getMemberName()); 945 ID.AddBoolean(S->hasExplicitTemplateArgs()); 946 if (S->hasExplicitTemplateArgs()) 947 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 948 } 949 950 void StmtProfiler::VisitCXXNoexceptExpr(const CXXNoexceptExpr *S) { 951 VisitExpr(S); 952 } 953 954 void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) { 955 VisitExpr(S); 956 } 957 958 void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) { 959 VisitExpr(S); 960 VisitDecl(S->getPack()); 961 } 962 963 void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr( 964 const SubstNonTypeTemplateParmPackExpr *S) { 965 VisitExpr(S); 966 VisitDecl(S->getParameterPack()); 967 VisitTemplateArgument(S->getArgumentPack()); 968 } 969 970 void StmtProfiler::VisitSubstNonTypeTemplateParmExpr( 971 const SubstNonTypeTemplateParmExpr *E) { 972 // Profile exactly as the replacement expression. 973 Visit(E->getReplacement()); 974 } 975 976 void StmtProfiler::VisitFunctionParmPackExpr(const FunctionParmPackExpr *S) { 977 VisitExpr(S); 978 VisitDecl(S->getParameterPack()); 979 ID.AddInteger(S->getNumExpansions()); 980 for (FunctionParmPackExpr::iterator I = S->begin(), E = S->end(); I != E; ++I) 981 VisitDecl(*I); 982 } 983 984 void StmtProfiler::VisitMaterializeTemporaryExpr( 985 const MaterializeTemporaryExpr *S) { 986 VisitExpr(S); 987 } 988 989 void StmtProfiler::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { 990 VisitExpr(E); 991 } 992 993 void StmtProfiler::VisitObjCStringLiteral(const ObjCStringLiteral *S) { 994 VisitExpr(S); 995 } 996 997 void StmtProfiler::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { 998 VisitExpr(E); 999 } 1000 1001 void StmtProfiler::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) { 1002 VisitExpr(E); 1003 } 1004 1005 void StmtProfiler::VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) { 1006 VisitExpr(E); 1007 } 1008 1009 void StmtProfiler::VisitObjCEncodeExpr(const ObjCEncodeExpr *S) { 1010 VisitExpr(S); 1011 VisitType(S->getEncodedType()); 1012 } 1013 1014 void StmtProfiler::VisitObjCSelectorExpr(const ObjCSelectorExpr *S) { 1015 VisitExpr(S); 1016 VisitName(S->getSelector()); 1017 } 1018 1019 void StmtProfiler::VisitObjCProtocolExpr(const ObjCProtocolExpr *S) { 1020 VisitExpr(S); 1021 VisitDecl(S->getProtocol()); 1022 } 1023 1024 void StmtProfiler::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *S) { 1025 VisitExpr(S); 1026 VisitDecl(S->getDecl()); 1027 ID.AddBoolean(S->isArrow()); 1028 ID.AddBoolean(S->isFreeIvar()); 1029 } 1030 1031 void StmtProfiler::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *S) { 1032 VisitExpr(S); 1033 if (S->isImplicitProperty()) { 1034 VisitDecl(S->getImplicitPropertyGetter()); 1035 VisitDecl(S->getImplicitPropertySetter()); 1036 } else { 1037 VisitDecl(S->getExplicitProperty()); 1038 } 1039 if (S->isSuperReceiver()) { 1040 ID.AddBoolean(S->isSuperReceiver()); 1041 VisitType(S->getSuperReceiverType()); 1042 } 1043 } 1044 1045 void StmtProfiler::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *S) { 1046 VisitExpr(S); 1047 VisitDecl(S->getAtIndexMethodDecl()); 1048 VisitDecl(S->setAtIndexMethodDecl()); 1049 } 1050 1051 void StmtProfiler::VisitObjCMessageExpr(const ObjCMessageExpr *S) { 1052 VisitExpr(S); 1053 VisitName(S->getSelector()); 1054 VisitDecl(S->getMethodDecl()); 1055 } 1056 1057 void StmtProfiler::VisitObjCIsaExpr(const ObjCIsaExpr *S) { 1058 VisitExpr(S); 1059 ID.AddBoolean(S->isArrow()); 1060 } 1061 1062 void StmtProfiler::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *S) { 1063 VisitExpr(S); 1064 ID.AddBoolean(S->getValue()); 1065 } 1066 1067 void StmtProfiler::VisitObjCIndirectCopyRestoreExpr( 1068 const ObjCIndirectCopyRestoreExpr *S) { 1069 VisitExpr(S); 1070 ID.AddBoolean(S->shouldCopy()); 1071 } 1072 1073 void StmtProfiler::VisitObjCBridgedCastExpr(const ObjCBridgedCastExpr *S) { 1074 VisitExplicitCastExpr(S); 1075 ID.AddBoolean(S->getBridgeKind()); 1076 } 1077 1078 void StmtProfiler::VisitDecl(const Decl *D) { 1079 ID.AddInteger(D? D->getKind() : 0); 1080 1081 if (Canonical && D) { 1082 if (const NonTypeTemplateParmDecl *NTTP = 1083 dyn_cast<NonTypeTemplateParmDecl>(D)) { 1084 ID.AddInteger(NTTP->getDepth()); 1085 ID.AddInteger(NTTP->getIndex()); 1086 ID.AddBoolean(NTTP->isParameterPack()); 1087 VisitType(NTTP->getType()); 1088 return; 1089 } 1090 1091 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) { 1092 // The Itanium C++ ABI uses the type, scope depth, and scope 1093 // index of a parameter when mangling expressions that involve 1094 // function parameters, so we will use the parameter's type for 1095 // establishing function parameter identity. That way, our 1096 // definition of "equivalent" (per C++ [temp.over.link]) is at 1097 // least as strong as the definition of "equivalent" used for 1098 // name mangling. 1099 VisitType(Parm->getType()); 1100 ID.AddInteger(Parm->getFunctionScopeDepth()); 1101 ID.AddInteger(Parm->getFunctionScopeIndex()); 1102 return; 1103 } 1104 1105 if (const TemplateTypeParmDecl *TTP = 1106 dyn_cast<TemplateTypeParmDecl>(D)) { 1107 ID.AddInteger(TTP->getDepth()); 1108 ID.AddInteger(TTP->getIndex()); 1109 ID.AddBoolean(TTP->isParameterPack()); 1110 return; 1111 } 1112 1113 if (const TemplateTemplateParmDecl *TTP = 1114 dyn_cast<TemplateTemplateParmDecl>(D)) { 1115 ID.AddInteger(TTP->getDepth()); 1116 ID.AddInteger(TTP->getIndex()); 1117 ID.AddBoolean(TTP->isParameterPack()); 1118 return; 1119 } 1120 } 1121 1122 ID.AddPointer(D? D->getCanonicalDecl() : 0); 1123 } 1124 1125 void StmtProfiler::VisitType(QualType T) { 1126 if (Canonical) 1127 T = Context.getCanonicalType(T); 1128 1129 ID.AddPointer(T.getAsOpaquePtr()); 1130 } 1131 1132 void StmtProfiler::VisitName(DeclarationName Name) { 1133 ID.AddPointer(Name.getAsOpaquePtr()); 1134 } 1135 1136 void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) { 1137 if (Canonical) 1138 NNS = Context.getCanonicalNestedNameSpecifier(NNS); 1139 ID.AddPointer(NNS); 1140 } 1141 1142 void StmtProfiler::VisitTemplateName(TemplateName Name) { 1143 if (Canonical) 1144 Name = Context.getCanonicalTemplateName(Name); 1145 1146 Name.Profile(ID); 1147 } 1148 1149 void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args, 1150 unsigned NumArgs) { 1151 ID.AddInteger(NumArgs); 1152 for (unsigned I = 0; I != NumArgs; ++I) 1153 VisitTemplateArgument(Args[I].getArgument()); 1154 } 1155 1156 void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) { 1157 // Mostly repetitive with TemplateArgument::Profile! 1158 ID.AddInteger(Arg.getKind()); 1159 switch (Arg.getKind()) { 1160 case TemplateArgument::Null: 1161 break; 1162 1163 case TemplateArgument::Type: 1164 VisitType(Arg.getAsType()); 1165 break; 1166 1167 case TemplateArgument::Template: 1168 case TemplateArgument::TemplateExpansion: 1169 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern()); 1170 break; 1171 1172 case TemplateArgument::Declaration: 1173 VisitDecl(Arg.getAsDecl()); 1174 break; 1175 1176 case TemplateArgument::NullPtr: 1177 VisitType(Arg.getNullPtrType()); 1178 break; 1179 1180 case TemplateArgument::Integral: 1181 Arg.getAsIntegral().Profile(ID); 1182 VisitType(Arg.getIntegralType()); 1183 break; 1184 1185 case TemplateArgument::Expression: 1186 Visit(Arg.getAsExpr()); 1187 break; 1188 1189 case TemplateArgument::Pack: 1190 const TemplateArgument *Pack = Arg.pack_begin(); 1191 for (unsigned i = 0, e = Arg.pack_size(); i != e; ++i) 1192 VisitTemplateArgument(Pack[i]); 1193 break; 1194 } 1195 } 1196 1197 void Stmt::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 1198 bool Canonical) const { 1199 StmtProfiler Profiler(ID, Context, Canonical); 1200 Profiler.Visit(this); 1201 } 1202