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