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