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