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