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::VisitCapturedStmt(const CapturedStmt *S) { 219 VisitStmt(S); 220 } 221 222 void StmtProfiler::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) { 223 VisitStmt(S); 224 } 225 226 void StmtProfiler::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *S) { 227 VisitStmt(S); 228 ID.AddBoolean(S->hasEllipsis()); 229 if (S->getCatchParamDecl()) 230 VisitType(S->getCatchParamDecl()->getType()); 231 } 232 233 void StmtProfiler::VisitObjCAtFinallyStmt(const ObjCAtFinallyStmt *S) { 234 VisitStmt(S); 235 } 236 237 void StmtProfiler::VisitObjCAtTryStmt(const ObjCAtTryStmt *S) { 238 VisitStmt(S); 239 } 240 241 void 242 StmtProfiler::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S) { 243 VisitStmt(S); 244 } 245 246 void StmtProfiler::VisitObjCAtThrowStmt(const ObjCAtThrowStmt *S) { 247 VisitStmt(S); 248 } 249 250 void 251 StmtProfiler::VisitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt *S) { 252 VisitStmt(S); 253 } 254 255 namespace { 256 class OMPClauseProfiler : public ConstOMPClauseVisitor<OMPClauseProfiler> { 257 StmtProfiler *Profiler; 258 /// \brief Process clauses with list of variables. 259 template <typename T> 260 void VisitOMPClauseList(T *Node); 261 public: 262 OMPClauseProfiler(StmtProfiler *P) : Profiler(P) { } 263 #define OPENMP_CLAUSE(Name, Class) \ 264 void Visit##Class(const Class *C); 265 #include "clang/Basic/OpenMPKinds.def" 266 }; 267 268 void OMPClauseProfiler::VisitOMPDefaultClause(const OMPDefaultClause *C) { } 269 270 template<typename T> 271 void OMPClauseProfiler::VisitOMPClauseList(T *Node) { 272 for (typename T::varlist_const_iterator I = Node->varlist_begin(), 273 E = Node->varlist_end(); 274 I != E; ++I) 275 Profiler->VisitStmt(*I); 276 } 277 278 void OMPClauseProfiler::VisitOMPPrivateClause(const OMPPrivateClause *C) { 279 VisitOMPClauseList(C); 280 } 281 void OMPClauseProfiler::VisitOMPFirstprivateClause( 282 const OMPFirstprivateClause *C) { 283 VisitOMPClauseList(C); 284 } 285 void OMPClauseProfiler::VisitOMPSharedClause(const OMPSharedClause *C) { 286 VisitOMPClauseList(C); 287 } 288 } 289 290 void 291 StmtProfiler::VisitOMPParallelDirective(const OMPParallelDirective *S) { 292 VisitStmt(S); 293 OMPClauseProfiler P(this); 294 ArrayRef<OMPClause *> Clauses = S->clauses(); 295 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end(); 296 I != E; ++I) 297 if (*I) 298 P.Visit(*I); 299 } 300 301 void StmtProfiler::VisitExpr(const Expr *S) { 302 VisitStmt(S); 303 } 304 305 void StmtProfiler::VisitDeclRefExpr(const DeclRefExpr *S) { 306 VisitExpr(S); 307 if (!Canonical) 308 VisitNestedNameSpecifier(S->getQualifier()); 309 VisitDecl(S->getDecl()); 310 if (!Canonical) 311 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 312 } 313 314 void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) { 315 VisitExpr(S); 316 ID.AddInteger(S->getIdentType()); 317 } 318 319 void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) { 320 VisitExpr(S); 321 S->getValue().Profile(ID); 322 } 323 324 void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) { 325 VisitExpr(S); 326 ID.AddInteger(S->getKind()); 327 ID.AddInteger(S->getValue()); 328 } 329 330 void StmtProfiler::VisitFloatingLiteral(const FloatingLiteral *S) { 331 VisitExpr(S); 332 S->getValue().Profile(ID); 333 ID.AddBoolean(S->isExact()); 334 } 335 336 void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) { 337 VisitExpr(S); 338 } 339 340 void StmtProfiler::VisitStringLiteral(const StringLiteral *S) { 341 VisitExpr(S); 342 ID.AddString(S->getBytes()); 343 ID.AddInteger(S->getKind()); 344 } 345 346 void StmtProfiler::VisitParenExpr(const ParenExpr *S) { 347 VisitExpr(S); 348 } 349 350 void StmtProfiler::VisitParenListExpr(const ParenListExpr *S) { 351 VisitExpr(S); 352 } 353 354 void StmtProfiler::VisitUnaryOperator(const UnaryOperator *S) { 355 VisitExpr(S); 356 ID.AddInteger(S->getOpcode()); 357 } 358 359 void StmtProfiler::VisitOffsetOfExpr(const OffsetOfExpr *S) { 360 VisitType(S->getTypeSourceInfo()->getType()); 361 unsigned n = S->getNumComponents(); 362 for (unsigned i = 0; i < n; ++i) { 363 const OffsetOfExpr::OffsetOfNode& ON = S->getComponent(i); 364 ID.AddInteger(ON.getKind()); 365 switch (ON.getKind()) { 366 case OffsetOfExpr::OffsetOfNode::Array: 367 // Expressions handled below. 368 break; 369 370 case OffsetOfExpr::OffsetOfNode::Field: 371 VisitDecl(ON.getField()); 372 break; 373 374 case OffsetOfExpr::OffsetOfNode::Identifier: 375 ID.AddPointer(ON.getFieldName()); 376 break; 377 378 case OffsetOfExpr::OffsetOfNode::Base: 379 // These nodes are implicit, and therefore don't need profiling. 380 break; 381 } 382 } 383 384 VisitExpr(S); 385 } 386 387 void 388 StmtProfiler::VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *S) { 389 VisitExpr(S); 390 ID.AddInteger(S->getKind()); 391 if (S->isArgumentType()) 392 VisitType(S->getArgumentType()); 393 } 394 395 void StmtProfiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *S) { 396 VisitExpr(S); 397 } 398 399 void StmtProfiler::VisitCallExpr(const CallExpr *S) { 400 VisitExpr(S); 401 } 402 403 void StmtProfiler::VisitMemberExpr(const MemberExpr *S) { 404 VisitExpr(S); 405 VisitDecl(S->getMemberDecl()); 406 if (!Canonical) 407 VisitNestedNameSpecifier(S->getQualifier()); 408 ID.AddBoolean(S->isArrow()); 409 } 410 411 void StmtProfiler::VisitCompoundLiteralExpr(const CompoundLiteralExpr *S) { 412 VisitExpr(S); 413 ID.AddBoolean(S->isFileScope()); 414 } 415 416 void StmtProfiler::VisitCastExpr(const CastExpr *S) { 417 VisitExpr(S); 418 } 419 420 void StmtProfiler::VisitImplicitCastExpr(const ImplicitCastExpr *S) { 421 VisitCastExpr(S); 422 ID.AddInteger(S->getValueKind()); 423 } 424 425 void StmtProfiler::VisitExplicitCastExpr(const ExplicitCastExpr *S) { 426 VisitCastExpr(S); 427 VisitType(S->getTypeAsWritten()); 428 } 429 430 void StmtProfiler::VisitCStyleCastExpr(const CStyleCastExpr *S) { 431 VisitExplicitCastExpr(S); 432 } 433 434 void StmtProfiler::VisitBinaryOperator(const BinaryOperator *S) { 435 VisitExpr(S); 436 ID.AddInteger(S->getOpcode()); 437 } 438 439 void 440 StmtProfiler::VisitCompoundAssignOperator(const CompoundAssignOperator *S) { 441 VisitBinaryOperator(S); 442 } 443 444 void StmtProfiler::VisitConditionalOperator(const ConditionalOperator *S) { 445 VisitExpr(S); 446 } 447 448 void StmtProfiler::VisitBinaryConditionalOperator( 449 const BinaryConditionalOperator *S) { 450 VisitExpr(S); 451 } 452 453 void StmtProfiler::VisitAddrLabelExpr(const AddrLabelExpr *S) { 454 VisitExpr(S); 455 VisitDecl(S->getLabel()); 456 } 457 458 void StmtProfiler::VisitStmtExpr(const StmtExpr *S) { 459 VisitExpr(S); 460 } 461 462 void StmtProfiler::VisitShuffleVectorExpr(const ShuffleVectorExpr *S) { 463 VisitExpr(S); 464 } 465 466 void StmtProfiler::VisitConvertVectorExpr(const ConvertVectorExpr *S) { 467 VisitExpr(S); 468 } 469 470 void StmtProfiler::VisitChooseExpr(const ChooseExpr *S) { 471 VisitExpr(S); 472 } 473 474 void StmtProfiler::VisitGNUNullExpr(const GNUNullExpr *S) { 475 VisitExpr(S); 476 } 477 478 void StmtProfiler::VisitVAArgExpr(const VAArgExpr *S) { 479 VisitExpr(S); 480 } 481 482 void StmtProfiler::VisitInitListExpr(const InitListExpr *S) { 483 if (S->getSyntacticForm()) { 484 VisitInitListExpr(S->getSyntacticForm()); 485 return; 486 } 487 488 VisitExpr(S); 489 } 490 491 void StmtProfiler::VisitDesignatedInitExpr(const DesignatedInitExpr *S) { 492 VisitExpr(S); 493 ID.AddBoolean(S->usesGNUSyntax()); 494 for (DesignatedInitExpr::const_designators_iterator D = 495 S->designators_begin(), DEnd = S->designators_end(); 496 D != DEnd; ++D) { 497 if (D->isFieldDesignator()) { 498 ID.AddInteger(0); 499 VisitName(D->getFieldName()); 500 continue; 501 } 502 503 if (D->isArrayDesignator()) { 504 ID.AddInteger(1); 505 } else { 506 assert(D->isArrayRangeDesignator()); 507 ID.AddInteger(2); 508 } 509 ID.AddInteger(D->getFirstExprIndex()); 510 } 511 } 512 513 void StmtProfiler::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *S) { 514 VisitExpr(S); 515 } 516 517 void StmtProfiler::VisitExtVectorElementExpr(const ExtVectorElementExpr *S) { 518 VisitExpr(S); 519 VisitName(&S->getAccessor()); 520 } 521 522 void StmtProfiler::VisitBlockExpr(const BlockExpr *S) { 523 VisitExpr(S); 524 VisitDecl(S->getBlockDecl()); 525 } 526 527 void StmtProfiler::VisitGenericSelectionExpr(const GenericSelectionExpr *S) { 528 VisitExpr(S); 529 for (unsigned i = 0; i != S->getNumAssocs(); ++i) { 530 QualType T = S->getAssocType(i); 531 if (T.isNull()) 532 ID.AddPointer(0); 533 else 534 VisitType(T); 535 VisitExpr(S->getAssocExpr(i)); 536 } 537 } 538 539 void StmtProfiler::VisitPseudoObjectExpr(const PseudoObjectExpr *S) { 540 VisitExpr(S); 541 for (PseudoObjectExpr::const_semantics_iterator 542 i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i) 543 // Normally, we would not profile the source expressions of OVEs. 544 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(*i)) 545 Visit(OVE->getSourceExpr()); 546 } 547 548 void StmtProfiler::VisitAtomicExpr(const AtomicExpr *S) { 549 VisitExpr(S); 550 ID.AddInteger(S->getOp()); 551 } 552 553 static Stmt::StmtClass DecodeOperatorCall(const CXXOperatorCallExpr *S, 554 UnaryOperatorKind &UnaryOp, 555 BinaryOperatorKind &BinaryOp) { 556 switch (S->getOperator()) { 557 case OO_None: 558 case OO_New: 559 case OO_Delete: 560 case OO_Array_New: 561 case OO_Array_Delete: 562 case OO_Arrow: 563 case OO_Call: 564 case OO_Conditional: 565 case NUM_OVERLOADED_OPERATORS: 566 llvm_unreachable("Invalid operator call kind"); 567 568 case OO_Plus: 569 if (S->getNumArgs() == 1) { 570 UnaryOp = UO_Plus; 571 return Stmt::UnaryOperatorClass; 572 } 573 574 BinaryOp = BO_Add; 575 return Stmt::BinaryOperatorClass; 576 577 case OO_Minus: 578 if (S->getNumArgs() == 1) { 579 UnaryOp = UO_Minus; 580 return Stmt::UnaryOperatorClass; 581 } 582 583 BinaryOp = BO_Sub; 584 return Stmt::BinaryOperatorClass; 585 586 case OO_Star: 587 if (S->getNumArgs() == 1) { 588 UnaryOp = UO_Minus; 589 return Stmt::UnaryOperatorClass; 590 } 591 592 BinaryOp = BO_Sub; 593 return Stmt::BinaryOperatorClass; 594 595 case OO_Slash: 596 BinaryOp = BO_Div; 597 return Stmt::BinaryOperatorClass; 598 599 case OO_Percent: 600 BinaryOp = BO_Rem; 601 return Stmt::BinaryOperatorClass; 602 603 case OO_Caret: 604 BinaryOp = BO_Xor; 605 return Stmt::BinaryOperatorClass; 606 607 case OO_Amp: 608 if (S->getNumArgs() == 1) { 609 UnaryOp = UO_AddrOf; 610 return Stmt::UnaryOperatorClass; 611 } 612 613 BinaryOp = BO_And; 614 return Stmt::BinaryOperatorClass; 615 616 case OO_Pipe: 617 BinaryOp = BO_Or; 618 return Stmt::BinaryOperatorClass; 619 620 case OO_Tilde: 621 UnaryOp = UO_Not; 622 return Stmt::UnaryOperatorClass; 623 624 case OO_Exclaim: 625 UnaryOp = UO_LNot; 626 return Stmt::UnaryOperatorClass; 627 628 case OO_Equal: 629 BinaryOp = BO_Assign; 630 return Stmt::BinaryOperatorClass; 631 632 case OO_Less: 633 BinaryOp = BO_LT; 634 return Stmt::BinaryOperatorClass; 635 636 case OO_Greater: 637 BinaryOp = BO_GT; 638 return Stmt::BinaryOperatorClass; 639 640 case OO_PlusEqual: 641 BinaryOp = BO_AddAssign; 642 return Stmt::CompoundAssignOperatorClass; 643 644 case OO_MinusEqual: 645 BinaryOp = BO_SubAssign; 646 return Stmt::CompoundAssignOperatorClass; 647 648 case OO_StarEqual: 649 BinaryOp = BO_MulAssign; 650 return Stmt::CompoundAssignOperatorClass; 651 652 case OO_SlashEqual: 653 BinaryOp = BO_DivAssign; 654 return Stmt::CompoundAssignOperatorClass; 655 656 case OO_PercentEqual: 657 BinaryOp = BO_RemAssign; 658 return Stmt::CompoundAssignOperatorClass; 659 660 case OO_CaretEqual: 661 BinaryOp = BO_XorAssign; 662 return Stmt::CompoundAssignOperatorClass; 663 664 case OO_AmpEqual: 665 BinaryOp = BO_AndAssign; 666 return Stmt::CompoundAssignOperatorClass; 667 668 case OO_PipeEqual: 669 BinaryOp = BO_OrAssign; 670 return Stmt::CompoundAssignOperatorClass; 671 672 case OO_LessLess: 673 BinaryOp = BO_Shl; 674 return Stmt::BinaryOperatorClass; 675 676 case OO_GreaterGreater: 677 BinaryOp = BO_Shr; 678 return Stmt::BinaryOperatorClass; 679 680 case OO_LessLessEqual: 681 BinaryOp = BO_ShlAssign; 682 return Stmt::CompoundAssignOperatorClass; 683 684 case OO_GreaterGreaterEqual: 685 BinaryOp = BO_ShrAssign; 686 return Stmt::CompoundAssignOperatorClass; 687 688 case OO_EqualEqual: 689 BinaryOp = BO_EQ; 690 return Stmt::BinaryOperatorClass; 691 692 case OO_ExclaimEqual: 693 BinaryOp = BO_NE; 694 return Stmt::BinaryOperatorClass; 695 696 case OO_LessEqual: 697 BinaryOp = BO_LE; 698 return Stmt::BinaryOperatorClass; 699 700 case OO_GreaterEqual: 701 BinaryOp = BO_GE; 702 return Stmt::BinaryOperatorClass; 703 704 case OO_AmpAmp: 705 BinaryOp = BO_LAnd; 706 return Stmt::BinaryOperatorClass; 707 708 case OO_PipePipe: 709 BinaryOp = BO_LOr; 710 return Stmt::BinaryOperatorClass; 711 712 case OO_PlusPlus: 713 UnaryOp = S->getNumArgs() == 1? UO_PreInc 714 : UO_PostInc; 715 return Stmt::UnaryOperatorClass; 716 717 case OO_MinusMinus: 718 UnaryOp = S->getNumArgs() == 1? UO_PreDec 719 : UO_PostDec; 720 return Stmt::UnaryOperatorClass; 721 722 case OO_Comma: 723 BinaryOp = BO_Comma; 724 return Stmt::BinaryOperatorClass; 725 726 727 case OO_ArrowStar: 728 BinaryOp = BO_PtrMemI; 729 return Stmt::BinaryOperatorClass; 730 731 case OO_Subscript: 732 return Stmt::ArraySubscriptExprClass; 733 } 734 735 llvm_unreachable("Invalid overloaded operator expression"); 736 } 737 738 739 void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) { 740 if (S->isTypeDependent()) { 741 // Type-dependent operator calls are profiled like their underlying 742 // syntactic operator. 743 UnaryOperatorKind UnaryOp = UO_Extension; 744 BinaryOperatorKind BinaryOp = BO_Comma; 745 Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp); 746 747 ID.AddInteger(SC); 748 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 749 Visit(S->getArg(I)); 750 if (SC == Stmt::UnaryOperatorClass) 751 ID.AddInteger(UnaryOp); 752 else if (SC == Stmt::BinaryOperatorClass || 753 SC == Stmt::CompoundAssignOperatorClass) 754 ID.AddInteger(BinaryOp); 755 else 756 assert(SC == Stmt::ArraySubscriptExprClass); 757 758 return; 759 } 760 761 VisitCallExpr(S); 762 ID.AddInteger(S->getOperator()); 763 } 764 765 void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) { 766 VisitCallExpr(S); 767 } 768 769 void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) { 770 VisitCallExpr(S); 771 } 772 773 void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) { 774 VisitExpr(S); 775 } 776 777 void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) { 778 VisitExplicitCastExpr(S); 779 } 780 781 void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) { 782 VisitCXXNamedCastExpr(S); 783 } 784 785 void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) { 786 VisitCXXNamedCastExpr(S); 787 } 788 789 void 790 StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) { 791 VisitCXXNamedCastExpr(S); 792 } 793 794 void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) { 795 VisitCXXNamedCastExpr(S); 796 } 797 798 void StmtProfiler::VisitUserDefinedLiteral(const UserDefinedLiteral *S) { 799 VisitCallExpr(S); 800 } 801 802 void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) { 803 VisitExpr(S); 804 ID.AddBoolean(S->getValue()); 805 } 806 807 void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) { 808 VisitExpr(S); 809 } 810 811 void StmtProfiler::VisitCXXStdInitializerListExpr( 812 const CXXStdInitializerListExpr *S) { 813 VisitExpr(S); 814 } 815 816 void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) { 817 VisitExpr(S); 818 if (S->isTypeOperand()) 819 VisitType(S->getTypeOperandSourceInfo()->getType()); 820 } 821 822 void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) { 823 VisitExpr(S); 824 if (S->isTypeOperand()) 825 VisitType(S->getTypeOperandSourceInfo()->getType()); 826 } 827 828 void StmtProfiler::VisitMSPropertyRefExpr(const MSPropertyRefExpr *S) { 829 VisitExpr(S); 830 VisitDecl(S->getPropertyDecl()); 831 } 832 833 void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) { 834 VisitExpr(S); 835 ID.AddBoolean(S->isImplicit()); 836 } 837 838 void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) { 839 VisitExpr(S); 840 } 841 842 void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) { 843 VisitExpr(S); 844 VisitDecl(S->getParam()); 845 } 846 847 void StmtProfiler::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *S) { 848 VisitExpr(S); 849 VisitDecl(S->getField()); 850 } 851 852 void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) { 853 VisitExpr(S); 854 VisitDecl( 855 const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor())); 856 } 857 858 void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) { 859 VisitExpr(S); 860 VisitDecl(S->getConstructor()); 861 ID.AddBoolean(S->isElidable()); 862 } 863 864 void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) { 865 VisitExplicitCastExpr(S); 866 } 867 868 void 869 StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) { 870 VisitCXXConstructExpr(S); 871 } 872 873 void 874 StmtProfiler::VisitLambdaExpr(const LambdaExpr *S) { 875 VisitExpr(S); 876 for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(), 877 CEnd = S->explicit_capture_end(); 878 C != CEnd; ++C) { 879 ID.AddInteger(C->getCaptureKind()); 880 switch (C->getCaptureKind()) { 881 case LCK_This: 882 break; 883 case LCK_ByRef: 884 case LCK_ByCopy: 885 VisitDecl(C->getCapturedVar()); 886 ID.AddBoolean(C->isPackExpansion()); 887 break; 888 } 889 } 890 // Note: If we actually needed to be able to match lambda 891 // expressions, we would have to consider parameters and return type 892 // here, among other things. 893 VisitStmt(S->getBody()); 894 } 895 896 void 897 StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) { 898 VisitExpr(S); 899 } 900 901 void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) { 902 VisitExpr(S); 903 ID.AddBoolean(S->isGlobalDelete()); 904 ID.AddBoolean(S->isArrayForm()); 905 VisitDecl(S->getOperatorDelete()); 906 } 907 908 909 void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) { 910 VisitExpr(S); 911 VisitType(S->getAllocatedType()); 912 VisitDecl(S->getOperatorNew()); 913 VisitDecl(S->getOperatorDelete()); 914 ID.AddBoolean(S->isArray()); 915 ID.AddInteger(S->getNumPlacementArgs()); 916 ID.AddBoolean(S->isGlobalNew()); 917 ID.AddBoolean(S->isParenTypeId()); 918 ID.AddInteger(S->getInitializationStyle()); 919 } 920 921 void 922 StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) { 923 VisitExpr(S); 924 ID.AddBoolean(S->isArrow()); 925 VisitNestedNameSpecifier(S->getQualifier()); 926 ID.AddBoolean(S->getScopeTypeInfo() != 0); 927 if (S->getScopeTypeInfo()) 928 VisitType(S->getScopeTypeInfo()->getType()); 929 ID.AddBoolean(S->getDestroyedTypeInfo() != 0); 930 if (S->getDestroyedTypeInfo()) 931 VisitType(S->getDestroyedType()); 932 else 933 ID.AddPointer(S->getDestroyedTypeIdentifier()); 934 } 935 936 void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) { 937 VisitExpr(S); 938 VisitNestedNameSpecifier(S->getQualifier()); 939 VisitName(S->getName()); 940 ID.AddBoolean(S->hasExplicitTemplateArgs()); 941 if (S->hasExplicitTemplateArgs()) 942 VisitTemplateArguments(S->getExplicitTemplateArgs().getTemplateArgs(), 943 S->getExplicitTemplateArgs().NumTemplateArgs); 944 } 945 946 void 947 StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) { 948 VisitOverloadExpr(S); 949 } 950 951 void StmtProfiler::VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *S) { 952 VisitExpr(S); 953 ID.AddInteger(S->getTrait()); 954 VisitType(S->getQueriedType()); 955 } 956 957 void StmtProfiler::VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *S) { 958 VisitExpr(S); 959 ID.AddInteger(S->getTrait()); 960 VisitType(S->getLhsType()); 961 VisitType(S->getRhsType()); 962 } 963 964 void StmtProfiler::VisitTypeTraitExpr(const TypeTraitExpr *S) { 965 VisitExpr(S); 966 ID.AddInteger(S->getTrait()); 967 ID.AddInteger(S->getNumArgs()); 968 for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I) 969 VisitType(S->getArg(I)->getType()); 970 } 971 972 void StmtProfiler::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *S) { 973 VisitExpr(S); 974 ID.AddInteger(S->getTrait()); 975 VisitType(S->getQueriedType()); 976 } 977 978 void StmtProfiler::VisitExpressionTraitExpr(const ExpressionTraitExpr *S) { 979 VisitExpr(S); 980 ID.AddInteger(S->getTrait()); 981 VisitExpr(S->getQueriedExpression()); 982 } 983 984 void StmtProfiler::VisitDependentScopeDeclRefExpr( 985 const DependentScopeDeclRefExpr *S) { 986 VisitExpr(S); 987 VisitName(S->getDeclName()); 988 VisitNestedNameSpecifier(S->getQualifier()); 989 ID.AddBoolean(S->hasExplicitTemplateArgs()); 990 if (S->hasExplicitTemplateArgs()) 991 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 992 } 993 994 void StmtProfiler::VisitExprWithCleanups(const ExprWithCleanups *S) { 995 VisitExpr(S); 996 } 997 998 void StmtProfiler::VisitCXXUnresolvedConstructExpr( 999 const CXXUnresolvedConstructExpr *S) { 1000 VisitExpr(S); 1001 VisitType(S->getTypeAsWritten()); 1002 } 1003 1004 void StmtProfiler::VisitCXXDependentScopeMemberExpr( 1005 const CXXDependentScopeMemberExpr *S) { 1006 ID.AddBoolean(S->isImplicitAccess()); 1007 if (!S->isImplicitAccess()) { 1008 VisitExpr(S); 1009 ID.AddBoolean(S->isArrow()); 1010 } 1011 VisitNestedNameSpecifier(S->getQualifier()); 1012 VisitName(S->getMember()); 1013 ID.AddBoolean(S->hasExplicitTemplateArgs()); 1014 if (S->hasExplicitTemplateArgs()) 1015 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 1016 } 1017 1018 void StmtProfiler::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *S) { 1019 ID.AddBoolean(S->isImplicitAccess()); 1020 if (!S->isImplicitAccess()) { 1021 VisitExpr(S); 1022 ID.AddBoolean(S->isArrow()); 1023 } 1024 VisitNestedNameSpecifier(S->getQualifier()); 1025 VisitName(S->getMemberName()); 1026 ID.AddBoolean(S->hasExplicitTemplateArgs()); 1027 if (S->hasExplicitTemplateArgs()) 1028 VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs()); 1029 } 1030 1031 void StmtProfiler::VisitCXXNoexceptExpr(const CXXNoexceptExpr *S) { 1032 VisitExpr(S); 1033 } 1034 1035 void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) { 1036 VisitExpr(S); 1037 } 1038 1039 void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) { 1040 VisitExpr(S); 1041 VisitDecl(S->getPack()); 1042 } 1043 1044 void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr( 1045 const SubstNonTypeTemplateParmPackExpr *S) { 1046 VisitExpr(S); 1047 VisitDecl(S->getParameterPack()); 1048 VisitTemplateArgument(S->getArgumentPack()); 1049 } 1050 1051 void StmtProfiler::VisitSubstNonTypeTemplateParmExpr( 1052 const SubstNonTypeTemplateParmExpr *E) { 1053 // Profile exactly as the replacement expression. 1054 Visit(E->getReplacement()); 1055 } 1056 1057 void StmtProfiler::VisitFunctionParmPackExpr(const FunctionParmPackExpr *S) { 1058 VisitExpr(S); 1059 VisitDecl(S->getParameterPack()); 1060 ID.AddInteger(S->getNumExpansions()); 1061 for (FunctionParmPackExpr::iterator I = S->begin(), E = S->end(); I != E; ++I) 1062 VisitDecl(*I); 1063 } 1064 1065 void StmtProfiler::VisitMaterializeTemporaryExpr( 1066 const MaterializeTemporaryExpr *S) { 1067 VisitExpr(S); 1068 } 1069 1070 void StmtProfiler::VisitOpaqueValueExpr(const OpaqueValueExpr *E) { 1071 VisitExpr(E); 1072 } 1073 1074 void StmtProfiler::VisitObjCStringLiteral(const ObjCStringLiteral *S) { 1075 VisitExpr(S); 1076 } 1077 1078 void StmtProfiler::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { 1079 VisitExpr(E); 1080 } 1081 1082 void StmtProfiler::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) { 1083 VisitExpr(E); 1084 } 1085 1086 void StmtProfiler::VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) { 1087 VisitExpr(E); 1088 } 1089 1090 void StmtProfiler::VisitObjCEncodeExpr(const ObjCEncodeExpr *S) { 1091 VisitExpr(S); 1092 VisitType(S->getEncodedType()); 1093 } 1094 1095 void StmtProfiler::VisitObjCSelectorExpr(const ObjCSelectorExpr *S) { 1096 VisitExpr(S); 1097 VisitName(S->getSelector()); 1098 } 1099 1100 void StmtProfiler::VisitObjCProtocolExpr(const ObjCProtocolExpr *S) { 1101 VisitExpr(S); 1102 VisitDecl(S->getProtocol()); 1103 } 1104 1105 void StmtProfiler::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *S) { 1106 VisitExpr(S); 1107 VisitDecl(S->getDecl()); 1108 ID.AddBoolean(S->isArrow()); 1109 ID.AddBoolean(S->isFreeIvar()); 1110 } 1111 1112 void StmtProfiler::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *S) { 1113 VisitExpr(S); 1114 if (S->isImplicitProperty()) { 1115 VisitDecl(S->getImplicitPropertyGetter()); 1116 VisitDecl(S->getImplicitPropertySetter()); 1117 } else { 1118 VisitDecl(S->getExplicitProperty()); 1119 } 1120 if (S->isSuperReceiver()) { 1121 ID.AddBoolean(S->isSuperReceiver()); 1122 VisitType(S->getSuperReceiverType()); 1123 } 1124 } 1125 1126 void StmtProfiler::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *S) { 1127 VisitExpr(S); 1128 VisitDecl(S->getAtIndexMethodDecl()); 1129 VisitDecl(S->setAtIndexMethodDecl()); 1130 } 1131 1132 void StmtProfiler::VisitObjCMessageExpr(const ObjCMessageExpr *S) { 1133 VisitExpr(S); 1134 VisitName(S->getSelector()); 1135 VisitDecl(S->getMethodDecl()); 1136 } 1137 1138 void StmtProfiler::VisitObjCIsaExpr(const ObjCIsaExpr *S) { 1139 VisitExpr(S); 1140 ID.AddBoolean(S->isArrow()); 1141 } 1142 1143 void StmtProfiler::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *S) { 1144 VisitExpr(S); 1145 ID.AddBoolean(S->getValue()); 1146 } 1147 1148 void StmtProfiler::VisitObjCIndirectCopyRestoreExpr( 1149 const ObjCIndirectCopyRestoreExpr *S) { 1150 VisitExpr(S); 1151 ID.AddBoolean(S->shouldCopy()); 1152 } 1153 1154 void StmtProfiler::VisitObjCBridgedCastExpr(const ObjCBridgedCastExpr *S) { 1155 VisitExplicitCastExpr(S); 1156 ID.AddBoolean(S->getBridgeKind()); 1157 } 1158 1159 void StmtProfiler::VisitDecl(const Decl *D) { 1160 ID.AddInteger(D? D->getKind() : 0); 1161 1162 if (Canonical && D) { 1163 if (const NonTypeTemplateParmDecl *NTTP = 1164 dyn_cast<NonTypeTemplateParmDecl>(D)) { 1165 ID.AddInteger(NTTP->getDepth()); 1166 ID.AddInteger(NTTP->getIndex()); 1167 ID.AddBoolean(NTTP->isParameterPack()); 1168 VisitType(NTTP->getType()); 1169 return; 1170 } 1171 1172 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) { 1173 // The Itanium C++ ABI uses the type, scope depth, and scope 1174 // index of a parameter when mangling expressions that involve 1175 // function parameters, so we will use the parameter's type for 1176 // establishing function parameter identity. That way, our 1177 // definition of "equivalent" (per C++ [temp.over.link]) is at 1178 // least as strong as the definition of "equivalent" used for 1179 // name mangling. 1180 VisitType(Parm->getType()); 1181 ID.AddInteger(Parm->getFunctionScopeDepth()); 1182 ID.AddInteger(Parm->getFunctionScopeIndex()); 1183 return; 1184 } 1185 1186 if (const TemplateTypeParmDecl *TTP = 1187 dyn_cast<TemplateTypeParmDecl>(D)) { 1188 ID.AddInteger(TTP->getDepth()); 1189 ID.AddInteger(TTP->getIndex()); 1190 ID.AddBoolean(TTP->isParameterPack()); 1191 return; 1192 } 1193 1194 if (const TemplateTemplateParmDecl *TTP = 1195 dyn_cast<TemplateTemplateParmDecl>(D)) { 1196 ID.AddInteger(TTP->getDepth()); 1197 ID.AddInteger(TTP->getIndex()); 1198 ID.AddBoolean(TTP->isParameterPack()); 1199 return; 1200 } 1201 } 1202 1203 ID.AddPointer(D? D->getCanonicalDecl() : 0); 1204 } 1205 1206 void StmtProfiler::VisitType(QualType T) { 1207 if (Canonical) 1208 T = Context.getCanonicalType(T); 1209 1210 ID.AddPointer(T.getAsOpaquePtr()); 1211 } 1212 1213 void StmtProfiler::VisitName(DeclarationName Name) { 1214 ID.AddPointer(Name.getAsOpaquePtr()); 1215 } 1216 1217 void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) { 1218 if (Canonical) 1219 NNS = Context.getCanonicalNestedNameSpecifier(NNS); 1220 ID.AddPointer(NNS); 1221 } 1222 1223 void StmtProfiler::VisitTemplateName(TemplateName Name) { 1224 if (Canonical) 1225 Name = Context.getCanonicalTemplateName(Name); 1226 1227 Name.Profile(ID); 1228 } 1229 1230 void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args, 1231 unsigned NumArgs) { 1232 ID.AddInteger(NumArgs); 1233 for (unsigned I = 0; I != NumArgs; ++I) 1234 VisitTemplateArgument(Args[I].getArgument()); 1235 } 1236 1237 void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) { 1238 // Mostly repetitive with TemplateArgument::Profile! 1239 ID.AddInteger(Arg.getKind()); 1240 switch (Arg.getKind()) { 1241 case TemplateArgument::Null: 1242 break; 1243 1244 case TemplateArgument::Type: 1245 VisitType(Arg.getAsType()); 1246 break; 1247 1248 case TemplateArgument::Template: 1249 case TemplateArgument::TemplateExpansion: 1250 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern()); 1251 break; 1252 1253 case TemplateArgument::Declaration: 1254 VisitDecl(Arg.getAsDecl()); 1255 break; 1256 1257 case TemplateArgument::NullPtr: 1258 VisitType(Arg.getNullPtrType()); 1259 break; 1260 1261 case TemplateArgument::Integral: 1262 Arg.getAsIntegral().Profile(ID); 1263 VisitType(Arg.getIntegralType()); 1264 break; 1265 1266 case TemplateArgument::Expression: 1267 Visit(Arg.getAsExpr()); 1268 break; 1269 1270 case TemplateArgument::Pack: 1271 const TemplateArgument *Pack = Arg.pack_begin(); 1272 for (unsigned i = 0, e = Arg.pack_size(); i != e; ++i) 1273 VisitTemplateArgument(Pack[i]); 1274 break; 1275 } 1276 } 1277 1278 void Stmt::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 1279 bool Canonical) const { 1280 StmtProfiler Profiler(ID, Context, Canonical); 1281 Profiler.Visit(this); 1282 } 1283