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::VisitOMPIfClause(const OMPIfClause *C) {
269   if (C->getCondition())
270     Profiler->VisitStmt(C->getCondition());
271 }
272 
273 void OMPClauseProfiler::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
274 
275 template<typename T>
276 void OMPClauseProfiler::VisitOMPClauseList(T *Node) {
277   for (typename T::varlist_const_iterator I = Node->varlist_begin(),
278                                           E = Node->varlist_end();
279          I != E; ++I)
280     Profiler->VisitStmt(*I);
281 }
282 
283 void OMPClauseProfiler::VisitOMPPrivateClause(const OMPPrivateClause *C) {
284   VisitOMPClauseList(C);
285 }
286 void OMPClauseProfiler::VisitOMPFirstprivateClause(
287                                          const OMPFirstprivateClause *C) {
288   VisitOMPClauseList(C);
289 }
290 void OMPClauseProfiler::VisitOMPSharedClause(const OMPSharedClause *C) {
291   VisitOMPClauseList(C);
292 }
293 }
294 
295 void
296 StmtProfiler::VisitOMPExecutableDirective(const OMPExecutableDirective *S) {
297   VisitStmt(S);
298   OMPClauseProfiler P(this);
299   ArrayRef<OMPClause *> Clauses = S->clauses();
300   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
301        I != E; ++I)
302     if (*I)
303       P.Visit(*I);
304 }
305 
306 void StmtProfiler::VisitOMPParallelDirective(const OMPParallelDirective *S) {
307   VisitOMPExecutableDirective(S);
308 }
309 
310 void StmtProfiler::VisitOMPSimdDirective(const OMPSimdDirective *S) {
311   VisitOMPExecutableDirective(S);
312 }
313 
314 void StmtProfiler::VisitExpr(const Expr *S) {
315   VisitStmt(S);
316 }
317 
318 void StmtProfiler::VisitDeclRefExpr(const DeclRefExpr *S) {
319   VisitExpr(S);
320   if (!Canonical)
321     VisitNestedNameSpecifier(S->getQualifier());
322   VisitDecl(S->getDecl());
323   if (!Canonical)
324     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
325 }
326 
327 void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
328   VisitExpr(S);
329   ID.AddInteger(S->getIdentType());
330 }
331 
332 void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
333   VisitExpr(S);
334   S->getValue().Profile(ID);
335 }
336 
337 void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) {
338   VisitExpr(S);
339   ID.AddInteger(S->getKind());
340   ID.AddInteger(S->getValue());
341 }
342 
343 void StmtProfiler::VisitFloatingLiteral(const FloatingLiteral *S) {
344   VisitExpr(S);
345   S->getValue().Profile(ID);
346   ID.AddBoolean(S->isExact());
347 }
348 
349 void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) {
350   VisitExpr(S);
351 }
352 
353 void StmtProfiler::VisitStringLiteral(const StringLiteral *S) {
354   VisitExpr(S);
355   ID.AddString(S->getBytes());
356   ID.AddInteger(S->getKind());
357 }
358 
359 void StmtProfiler::VisitParenExpr(const ParenExpr *S) {
360   VisitExpr(S);
361 }
362 
363 void StmtProfiler::VisitParenListExpr(const ParenListExpr *S) {
364   VisitExpr(S);
365 }
366 
367 void StmtProfiler::VisitUnaryOperator(const UnaryOperator *S) {
368   VisitExpr(S);
369   ID.AddInteger(S->getOpcode());
370 }
371 
372 void StmtProfiler::VisitOffsetOfExpr(const OffsetOfExpr *S) {
373   VisitType(S->getTypeSourceInfo()->getType());
374   unsigned n = S->getNumComponents();
375   for (unsigned i = 0; i < n; ++i) {
376     const OffsetOfExpr::OffsetOfNode& ON = S->getComponent(i);
377     ID.AddInteger(ON.getKind());
378     switch (ON.getKind()) {
379     case OffsetOfExpr::OffsetOfNode::Array:
380       // Expressions handled below.
381       break;
382 
383     case OffsetOfExpr::OffsetOfNode::Field:
384       VisitDecl(ON.getField());
385       break;
386 
387     case OffsetOfExpr::OffsetOfNode::Identifier:
388       ID.AddPointer(ON.getFieldName());
389       break;
390 
391     case OffsetOfExpr::OffsetOfNode::Base:
392       // These nodes are implicit, and therefore don't need profiling.
393       break;
394     }
395   }
396 
397   VisitExpr(S);
398 }
399 
400 void
401 StmtProfiler::VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *S) {
402   VisitExpr(S);
403   ID.AddInteger(S->getKind());
404   if (S->isArgumentType())
405     VisitType(S->getArgumentType());
406 }
407 
408 void StmtProfiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *S) {
409   VisitExpr(S);
410 }
411 
412 void StmtProfiler::VisitCallExpr(const CallExpr *S) {
413   VisitExpr(S);
414 }
415 
416 void StmtProfiler::VisitMemberExpr(const MemberExpr *S) {
417   VisitExpr(S);
418   VisitDecl(S->getMemberDecl());
419   if (!Canonical)
420     VisitNestedNameSpecifier(S->getQualifier());
421   ID.AddBoolean(S->isArrow());
422 }
423 
424 void StmtProfiler::VisitCompoundLiteralExpr(const CompoundLiteralExpr *S) {
425   VisitExpr(S);
426   ID.AddBoolean(S->isFileScope());
427 }
428 
429 void StmtProfiler::VisitCastExpr(const CastExpr *S) {
430   VisitExpr(S);
431 }
432 
433 void StmtProfiler::VisitImplicitCastExpr(const ImplicitCastExpr *S) {
434   VisitCastExpr(S);
435   ID.AddInteger(S->getValueKind());
436 }
437 
438 void StmtProfiler::VisitExplicitCastExpr(const ExplicitCastExpr *S) {
439   VisitCastExpr(S);
440   VisitType(S->getTypeAsWritten());
441 }
442 
443 void StmtProfiler::VisitCStyleCastExpr(const CStyleCastExpr *S) {
444   VisitExplicitCastExpr(S);
445 }
446 
447 void StmtProfiler::VisitBinaryOperator(const BinaryOperator *S) {
448   VisitExpr(S);
449   ID.AddInteger(S->getOpcode());
450 }
451 
452 void
453 StmtProfiler::VisitCompoundAssignOperator(const CompoundAssignOperator *S) {
454   VisitBinaryOperator(S);
455 }
456 
457 void StmtProfiler::VisitConditionalOperator(const ConditionalOperator *S) {
458   VisitExpr(S);
459 }
460 
461 void StmtProfiler::VisitBinaryConditionalOperator(
462     const BinaryConditionalOperator *S) {
463   VisitExpr(S);
464 }
465 
466 void StmtProfiler::VisitAddrLabelExpr(const AddrLabelExpr *S) {
467   VisitExpr(S);
468   VisitDecl(S->getLabel());
469 }
470 
471 void StmtProfiler::VisitStmtExpr(const StmtExpr *S) {
472   VisitExpr(S);
473 }
474 
475 void StmtProfiler::VisitShuffleVectorExpr(const ShuffleVectorExpr *S) {
476   VisitExpr(S);
477 }
478 
479 void StmtProfiler::VisitConvertVectorExpr(const ConvertVectorExpr *S) {
480   VisitExpr(S);
481 }
482 
483 void StmtProfiler::VisitChooseExpr(const ChooseExpr *S) {
484   VisitExpr(S);
485 }
486 
487 void StmtProfiler::VisitGNUNullExpr(const GNUNullExpr *S) {
488   VisitExpr(S);
489 }
490 
491 void StmtProfiler::VisitVAArgExpr(const VAArgExpr *S) {
492   VisitExpr(S);
493 }
494 
495 void StmtProfiler::VisitInitListExpr(const InitListExpr *S) {
496   if (S->getSyntacticForm()) {
497     VisitInitListExpr(S->getSyntacticForm());
498     return;
499   }
500 
501   VisitExpr(S);
502 }
503 
504 void StmtProfiler::VisitDesignatedInitExpr(const DesignatedInitExpr *S) {
505   VisitExpr(S);
506   ID.AddBoolean(S->usesGNUSyntax());
507   for (DesignatedInitExpr::const_designators_iterator D =
508          S->designators_begin(), DEnd = S->designators_end();
509        D != DEnd; ++D) {
510     if (D->isFieldDesignator()) {
511       ID.AddInteger(0);
512       VisitName(D->getFieldName());
513       continue;
514     }
515 
516     if (D->isArrayDesignator()) {
517       ID.AddInteger(1);
518     } else {
519       assert(D->isArrayRangeDesignator());
520       ID.AddInteger(2);
521     }
522     ID.AddInteger(D->getFirstExprIndex());
523   }
524 }
525 
526 void StmtProfiler::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *S) {
527   VisitExpr(S);
528 }
529 
530 void StmtProfiler::VisitExtVectorElementExpr(const ExtVectorElementExpr *S) {
531   VisitExpr(S);
532   VisitName(&S->getAccessor());
533 }
534 
535 void StmtProfiler::VisitBlockExpr(const BlockExpr *S) {
536   VisitExpr(S);
537   VisitDecl(S->getBlockDecl());
538 }
539 
540 void StmtProfiler::VisitGenericSelectionExpr(const GenericSelectionExpr *S) {
541   VisitExpr(S);
542   for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
543     QualType T = S->getAssocType(i);
544     if (T.isNull())
545       ID.AddPointer(0);
546     else
547       VisitType(T);
548     VisitExpr(S->getAssocExpr(i));
549   }
550 }
551 
552 void StmtProfiler::VisitPseudoObjectExpr(const PseudoObjectExpr *S) {
553   VisitExpr(S);
554   for (PseudoObjectExpr::const_semantics_iterator
555          i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i)
556     // Normally, we would not profile the source expressions of OVEs.
557     if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(*i))
558       Visit(OVE->getSourceExpr());
559 }
560 
561 void StmtProfiler::VisitAtomicExpr(const AtomicExpr *S) {
562   VisitExpr(S);
563   ID.AddInteger(S->getOp());
564 }
565 
566 static Stmt::StmtClass DecodeOperatorCall(const CXXOperatorCallExpr *S,
567                                           UnaryOperatorKind &UnaryOp,
568                                           BinaryOperatorKind &BinaryOp) {
569   switch (S->getOperator()) {
570   case OO_None:
571   case OO_New:
572   case OO_Delete:
573   case OO_Array_New:
574   case OO_Array_Delete:
575   case OO_Arrow:
576   case OO_Call:
577   case OO_Conditional:
578   case NUM_OVERLOADED_OPERATORS:
579     llvm_unreachable("Invalid operator call kind");
580 
581   case OO_Plus:
582     if (S->getNumArgs() == 1) {
583       UnaryOp = UO_Plus;
584       return Stmt::UnaryOperatorClass;
585     }
586 
587     BinaryOp = BO_Add;
588     return Stmt::BinaryOperatorClass;
589 
590   case OO_Minus:
591     if (S->getNumArgs() == 1) {
592       UnaryOp = UO_Minus;
593       return Stmt::UnaryOperatorClass;
594     }
595 
596     BinaryOp = BO_Sub;
597     return Stmt::BinaryOperatorClass;
598 
599   case OO_Star:
600     if (S->getNumArgs() == 1) {
601       UnaryOp = UO_Minus;
602       return Stmt::UnaryOperatorClass;
603     }
604 
605     BinaryOp = BO_Sub;
606     return Stmt::BinaryOperatorClass;
607 
608   case OO_Slash:
609     BinaryOp = BO_Div;
610     return Stmt::BinaryOperatorClass;
611 
612   case OO_Percent:
613     BinaryOp = BO_Rem;
614     return Stmt::BinaryOperatorClass;
615 
616   case OO_Caret:
617     BinaryOp = BO_Xor;
618     return Stmt::BinaryOperatorClass;
619 
620   case OO_Amp:
621     if (S->getNumArgs() == 1) {
622       UnaryOp = UO_AddrOf;
623       return Stmt::UnaryOperatorClass;
624     }
625 
626     BinaryOp = BO_And;
627     return Stmt::BinaryOperatorClass;
628 
629   case OO_Pipe:
630     BinaryOp = BO_Or;
631     return Stmt::BinaryOperatorClass;
632 
633   case OO_Tilde:
634     UnaryOp = UO_Not;
635     return Stmt::UnaryOperatorClass;
636 
637   case OO_Exclaim:
638     UnaryOp = UO_LNot;
639     return Stmt::UnaryOperatorClass;
640 
641   case OO_Equal:
642     BinaryOp = BO_Assign;
643     return Stmt::BinaryOperatorClass;
644 
645   case OO_Less:
646     BinaryOp = BO_LT;
647     return Stmt::BinaryOperatorClass;
648 
649   case OO_Greater:
650     BinaryOp = BO_GT;
651     return Stmt::BinaryOperatorClass;
652 
653   case OO_PlusEqual:
654     BinaryOp = BO_AddAssign;
655     return Stmt::CompoundAssignOperatorClass;
656 
657   case OO_MinusEqual:
658     BinaryOp = BO_SubAssign;
659     return Stmt::CompoundAssignOperatorClass;
660 
661   case OO_StarEqual:
662     BinaryOp = BO_MulAssign;
663     return Stmt::CompoundAssignOperatorClass;
664 
665   case OO_SlashEqual:
666     BinaryOp = BO_DivAssign;
667     return Stmt::CompoundAssignOperatorClass;
668 
669   case OO_PercentEqual:
670     BinaryOp = BO_RemAssign;
671     return Stmt::CompoundAssignOperatorClass;
672 
673   case OO_CaretEqual:
674     BinaryOp = BO_XorAssign;
675     return Stmt::CompoundAssignOperatorClass;
676 
677   case OO_AmpEqual:
678     BinaryOp = BO_AndAssign;
679     return Stmt::CompoundAssignOperatorClass;
680 
681   case OO_PipeEqual:
682     BinaryOp = BO_OrAssign;
683     return Stmt::CompoundAssignOperatorClass;
684 
685   case OO_LessLess:
686     BinaryOp = BO_Shl;
687     return Stmt::BinaryOperatorClass;
688 
689   case OO_GreaterGreater:
690     BinaryOp = BO_Shr;
691     return Stmt::BinaryOperatorClass;
692 
693   case OO_LessLessEqual:
694     BinaryOp = BO_ShlAssign;
695     return Stmt::CompoundAssignOperatorClass;
696 
697   case OO_GreaterGreaterEqual:
698     BinaryOp = BO_ShrAssign;
699     return Stmt::CompoundAssignOperatorClass;
700 
701   case OO_EqualEqual:
702     BinaryOp = BO_EQ;
703     return Stmt::BinaryOperatorClass;
704 
705   case OO_ExclaimEqual:
706     BinaryOp = BO_NE;
707     return Stmt::BinaryOperatorClass;
708 
709   case OO_LessEqual:
710     BinaryOp = BO_LE;
711     return Stmt::BinaryOperatorClass;
712 
713   case OO_GreaterEqual:
714     BinaryOp = BO_GE;
715     return Stmt::BinaryOperatorClass;
716 
717   case OO_AmpAmp:
718     BinaryOp = BO_LAnd;
719     return Stmt::BinaryOperatorClass;
720 
721   case OO_PipePipe:
722     BinaryOp = BO_LOr;
723     return Stmt::BinaryOperatorClass;
724 
725   case OO_PlusPlus:
726     UnaryOp = S->getNumArgs() == 1? UO_PreInc
727                                   : UO_PostInc;
728     return Stmt::UnaryOperatorClass;
729 
730   case OO_MinusMinus:
731     UnaryOp = S->getNumArgs() == 1? UO_PreDec
732                                   : UO_PostDec;
733     return Stmt::UnaryOperatorClass;
734 
735   case OO_Comma:
736     BinaryOp = BO_Comma;
737     return Stmt::BinaryOperatorClass;
738 
739 
740   case OO_ArrowStar:
741     BinaryOp = BO_PtrMemI;
742     return Stmt::BinaryOperatorClass;
743 
744   case OO_Subscript:
745     return Stmt::ArraySubscriptExprClass;
746   }
747 
748   llvm_unreachable("Invalid overloaded operator expression");
749 }
750 
751 
752 void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) {
753   if (S->isTypeDependent()) {
754     // Type-dependent operator calls are profiled like their underlying
755     // syntactic operator.
756     UnaryOperatorKind UnaryOp = UO_Extension;
757     BinaryOperatorKind BinaryOp = BO_Comma;
758     Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp);
759 
760     ID.AddInteger(SC);
761     for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
762       Visit(S->getArg(I));
763     if (SC == Stmt::UnaryOperatorClass)
764       ID.AddInteger(UnaryOp);
765     else if (SC == Stmt::BinaryOperatorClass ||
766              SC == Stmt::CompoundAssignOperatorClass)
767       ID.AddInteger(BinaryOp);
768     else
769       assert(SC == Stmt::ArraySubscriptExprClass);
770 
771     return;
772   }
773 
774   VisitCallExpr(S);
775   ID.AddInteger(S->getOperator());
776 }
777 
778 void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) {
779   VisitCallExpr(S);
780 }
781 
782 void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) {
783   VisitCallExpr(S);
784 }
785 
786 void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) {
787   VisitExpr(S);
788 }
789 
790 void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) {
791   VisitExplicitCastExpr(S);
792 }
793 
794 void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
795   VisitCXXNamedCastExpr(S);
796 }
797 
798 void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) {
799   VisitCXXNamedCastExpr(S);
800 }
801 
802 void
803 StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) {
804   VisitCXXNamedCastExpr(S);
805 }
806 
807 void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) {
808   VisitCXXNamedCastExpr(S);
809 }
810 
811 void StmtProfiler::VisitUserDefinedLiteral(const UserDefinedLiteral *S) {
812   VisitCallExpr(S);
813 }
814 
815 void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) {
816   VisitExpr(S);
817   ID.AddBoolean(S->getValue());
818 }
819 
820 void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
821   VisitExpr(S);
822 }
823 
824 void StmtProfiler::VisitCXXStdInitializerListExpr(
825     const CXXStdInitializerListExpr *S) {
826   VisitExpr(S);
827 }
828 
829 void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) {
830   VisitExpr(S);
831   if (S->isTypeOperand())
832     VisitType(S->getTypeOperandSourceInfo()->getType());
833 }
834 
835 void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) {
836   VisitExpr(S);
837   if (S->isTypeOperand())
838     VisitType(S->getTypeOperandSourceInfo()->getType());
839 }
840 
841 void StmtProfiler::VisitMSPropertyRefExpr(const MSPropertyRefExpr *S) {
842   VisitExpr(S);
843   VisitDecl(S->getPropertyDecl());
844 }
845 
846 void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) {
847   VisitExpr(S);
848   ID.AddBoolean(S->isImplicit());
849 }
850 
851 void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) {
852   VisitExpr(S);
853 }
854 
855 void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) {
856   VisitExpr(S);
857   VisitDecl(S->getParam());
858 }
859 
860 void StmtProfiler::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *S) {
861   VisitExpr(S);
862   VisitDecl(S->getField());
863 }
864 
865 void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) {
866   VisitExpr(S);
867   VisitDecl(
868          const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
869 }
870 
871 void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) {
872   VisitExpr(S);
873   VisitDecl(S->getConstructor());
874   ID.AddBoolean(S->isElidable());
875 }
876 
877 void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
878   VisitExplicitCastExpr(S);
879 }
880 
881 void
882 StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) {
883   VisitCXXConstructExpr(S);
884 }
885 
886 void
887 StmtProfiler::VisitLambdaExpr(const LambdaExpr *S) {
888   VisitExpr(S);
889   for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
890                                  CEnd = S->explicit_capture_end();
891        C != CEnd; ++C) {
892     ID.AddInteger(C->getCaptureKind());
893     switch (C->getCaptureKind()) {
894     case LCK_This:
895       break;
896     case LCK_ByRef:
897     case LCK_ByCopy:
898       VisitDecl(C->getCapturedVar());
899       ID.AddBoolean(C->isPackExpansion());
900       break;
901     }
902   }
903   // Note: If we actually needed to be able to match lambda
904   // expressions, we would have to consider parameters and return type
905   // here, among other things.
906   VisitStmt(S->getBody());
907 }
908 
909 void
910 StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) {
911   VisitExpr(S);
912 }
913 
914 void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
915   VisitExpr(S);
916   ID.AddBoolean(S->isGlobalDelete());
917   ID.AddBoolean(S->isArrayForm());
918   VisitDecl(S->getOperatorDelete());
919 }
920 
921 
922 void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) {
923   VisitExpr(S);
924   VisitType(S->getAllocatedType());
925   VisitDecl(S->getOperatorNew());
926   VisitDecl(S->getOperatorDelete());
927   ID.AddBoolean(S->isArray());
928   ID.AddInteger(S->getNumPlacementArgs());
929   ID.AddBoolean(S->isGlobalNew());
930   ID.AddBoolean(S->isParenTypeId());
931   ID.AddInteger(S->getInitializationStyle());
932 }
933 
934 void
935 StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
936   VisitExpr(S);
937   ID.AddBoolean(S->isArrow());
938   VisitNestedNameSpecifier(S->getQualifier());
939   ID.AddBoolean(S->getScopeTypeInfo() != 0);
940   if (S->getScopeTypeInfo())
941     VisitType(S->getScopeTypeInfo()->getType());
942   ID.AddBoolean(S->getDestroyedTypeInfo() != 0);
943   if (S->getDestroyedTypeInfo())
944     VisitType(S->getDestroyedType());
945   else
946     ID.AddPointer(S->getDestroyedTypeIdentifier());
947 }
948 
949 void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) {
950   VisitExpr(S);
951   VisitNestedNameSpecifier(S->getQualifier());
952   VisitName(S->getName());
953   ID.AddBoolean(S->hasExplicitTemplateArgs());
954   if (S->hasExplicitTemplateArgs())
955     VisitTemplateArguments(S->getExplicitTemplateArgs().getTemplateArgs(),
956                            S->getExplicitTemplateArgs().NumTemplateArgs);
957 }
958 
959 void
960 StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) {
961   VisitOverloadExpr(S);
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