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