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     Visit(*C);
74 }
75 
76 void StmtProfiler::VisitDeclStmt(const DeclStmt *S) {
77   VisitStmt(S);
78   for (DeclStmt::const_decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
79        D != DEnd; ++D)
80     VisitDecl(*D);
81 }
82 
83 void StmtProfiler::VisitNullStmt(const NullStmt *S) {
84   VisitStmt(S);
85 }
86 
87 void StmtProfiler::VisitCompoundStmt(const CompoundStmt *S) {
88   VisitStmt(S);
89 }
90 
91 void StmtProfiler::VisitSwitchCase(const SwitchCase *S) {
92   VisitStmt(S);
93 }
94 
95 void StmtProfiler::VisitCaseStmt(const CaseStmt *S) {
96   VisitStmt(S);
97 }
98 
99 void StmtProfiler::VisitDefaultStmt(const DefaultStmt *S) {
100   VisitStmt(S);
101 }
102 
103 void StmtProfiler::VisitLabelStmt(const LabelStmt *S) {
104   VisitStmt(S);
105   VisitDecl(S->getDecl());
106 }
107 
108 void StmtProfiler::VisitIfStmt(const IfStmt *S) {
109   VisitStmt(S);
110   VisitDecl(S->getConditionVariable());
111 }
112 
113 void StmtProfiler::VisitSwitchStmt(const SwitchStmt *S) {
114   VisitStmt(S);
115   VisitDecl(S->getConditionVariable());
116 }
117 
118 void StmtProfiler::VisitWhileStmt(const WhileStmt *S) {
119   VisitStmt(S);
120   VisitDecl(S->getConditionVariable());
121 }
122 
123 void StmtProfiler::VisitDoStmt(const DoStmt *S) {
124   VisitStmt(S);
125 }
126 
127 void StmtProfiler::VisitForStmt(const ForStmt *S) {
128   VisitStmt(S);
129 }
130 
131 void StmtProfiler::VisitGotoStmt(const GotoStmt *S) {
132   VisitStmt(S);
133   VisitDecl(S->getLabel());
134 }
135 
136 void StmtProfiler::VisitIndirectGotoStmt(const IndirectGotoStmt *S) {
137   VisitStmt(S);
138 }
139 
140 void StmtProfiler::VisitContinueStmt(const ContinueStmt *S) {
141   VisitStmt(S);
142 }
143 
144 void StmtProfiler::VisitBreakStmt(const BreakStmt *S) {
145   VisitStmt(S);
146 }
147 
148 void StmtProfiler::VisitReturnStmt(const ReturnStmt *S) {
149   VisitStmt(S);
150 }
151 
152 void StmtProfiler::VisitAsmStmt(const AsmStmt *S) {
153   VisitStmt(S);
154   ID.AddBoolean(S->isVolatile());
155   ID.AddBoolean(S->isSimple());
156   VisitStringLiteral(S->getAsmString());
157   ID.AddInteger(S->getNumOutputs());
158   for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
159     ID.AddString(S->getOutputName(I));
160     VisitStringLiteral(S->getOutputConstraintLiteral(I));
161   }
162   ID.AddInteger(S->getNumInputs());
163   for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
164     ID.AddString(S->getInputName(I));
165     VisitStringLiteral(S->getInputConstraintLiteral(I));
166   }
167   ID.AddInteger(S->getNumClobbers());
168   for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
169     VisitStringLiteral(S->getClobber(I));
170 }
171 
172 void StmtProfiler::VisitCXXCatchStmt(const CXXCatchStmt *S) {
173   VisitStmt(S);
174   VisitType(S->getCaughtType());
175 }
176 
177 void StmtProfiler::VisitCXXTryStmt(const CXXTryStmt *S) {
178   VisitStmt(S);
179 }
180 
181 void StmtProfiler::VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
182   VisitStmt(S);
183 }
184 
185 void StmtProfiler::VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) {
186   VisitStmt(S);
187   ID.AddBoolean(S->isIfExists());
188   VisitNestedNameSpecifier(S->getQualifierLoc().getNestedNameSpecifier());
189   VisitName(S->getNameInfo().getName());
190 }
191 
192 void StmtProfiler::VisitSEHTryStmt(const SEHTryStmt *S) {
193   VisitStmt(S);
194 }
195 
196 void StmtProfiler::VisitSEHFinallyStmt(const SEHFinallyStmt *S) {
197   VisitStmt(S);
198 }
199 
200 void StmtProfiler::VisitSEHExceptStmt(const SEHExceptStmt *S) {
201   VisitStmt(S);
202 }
203 
204 void StmtProfiler::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
205   VisitStmt(S);
206 }
207 
208 void StmtProfiler::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *S) {
209   VisitStmt(S);
210   ID.AddBoolean(S->hasEllipsis());
211   if (S->getCatchParamDecl())
212     VisitType(S->getCatchParamDecl()->getType());
213 }
214 
215 void StmtProfiler::VisitObjCAtFinallyStmt(const ObjCAtFinallyStmt *S) {
216   VisitStmt(S);
217 }
218 
219 void StmtProfiler::VisitObjCAtTryStmt(const ObjCAtTryStmt *S) {
220   VisitStmt(S);
221 }
222 
223 void
224 StmtProfiler::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S) {
225   VisitStmt(S);
226 }
227 
228 void StmtProfiler::VisitObjCAtThrowStmt(const ObjCAtThrowStmt *S) {
229   VisitStmt(S);
230 }
231 
232 void
233 StmtProfiler::VisitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt *S) {
234   VisitStmt(S);
235 }
236 
237 void StmtProfiler::VisitExpr(const Expr *S) {
238   VisitStmt(S);
239 }
240 
241 void StmtProfiler::VisitDeclRefExpr(const DeclRefExpr *S) {
242   VisitExpr(S);
243   if (!Canonical)
244     VisitNestedNameSpecifier(S->getQualifier());
245   VisitDecl(S->getDecl());
246   if (!Canonical)
247     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
248 }
249 
250 void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
251   VisitExpr(S);
252   ID.AddInteger(S->getIdentType());
253 }
254 
255 void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
256   VisitExpr(S);
257   S->getValue().Profile(ID);
258 }
259 
260 void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) {
261   VisitExpr(S);
262   ID.AddInteger(S->getKind());
263   ID.AddInteger(S->getValue());
264 }
265 
266 void StmtProfiler::VisitFloatingLiteral(const FloatingLiteral *S) {
267   VisitExpr(S);
268   S->getValue().Profile(ID);
269   ID.AddBoolean(S->isExact());
270 }
271 
272 void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) {
273   VisitExpr(S);
274 }
275 
276 void StmtProfiler::VisitStringLiteral(const StringLiteral *S) {
277   VisitExpr(S);
278   ID.AddString(S->getBytes());
279   ID.AddInteger(S->getKind());
280 }
281 
282 void StmtProfiler::VisitParenExpr(const ParenExpr *S) {
283   VisitExpr(S);
284 }
285 
286 void StmtProfiler::VisitParenListExpr(const ParenListExpr *S) {
287   VisitExpr(S);
288 }
289 
290 void StmtProfiler::VisitUnaryOperator(const UnaryOperator *S) {
291   VisitExpr(S);
292   ID.AddInteger(S->getOpcode());
293 }
294 
295 void StmtProfiler::VisitOffsetOfExpr(const OffsetOfExpr *S) {
296   VisitType(S->getTypeSourceInfo()->getType());
297   unsigned n = S->getNumComponents();
298   for (unsigned i = 0; i < n; ++i) {
299     const OffsetOfExpr::OffsetOfNode& ON = S->getComponent(i);
300     ID.AddInteger(ON.getKind());
301     switch (ON.getKind()) {
302     case OffsetOfExpr::OffsetOfNode::Array:
303       // Expressions handled below.
304       break;
305 
306     case OffsetOfExpr::OffsetOfNode::Field:
307       VisitDecl(ON.getField());
308       break;
309 
310     case OffsetOfExpr::OffsetOfNode::Identifier:
311       ID.AddPointer(ON.getFieldName());
312       break;
313 
314     case OffsetOfExpr::OffsetOfNode::Base:
315       // These nodes are implicit, and therefore don't need profiling.
316       break;
317     }
318   }
319 
320   VisitExpr(S);
321 }
322 
323 void
324 StmtProfiler::VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *S) {
325   VisitExpr(S);
326   ID.AddInteger(S->getKind());
327   if (S->isArgumentType())
328     VisitType(S->getArgumentType());
329 }
330 
331 void StmtProfiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *S) {
332   VisitExpr(S);
333 }
334 
335 void StmtProfiler::VisitCallExpr(const CallExpr *S) {
336   VisitExpr(S);
337 }
338 
339 void StmtProfiler::VisitMemberExpr(const MemberExpr *S) {
340   VisitExpr(S);
341   VisitDecl(S->getMemberDecl());
342   if (!Canonical)
343     VisitNestedNameSpecifier(S->getQualifier());
344   ID.AddBoolean(S->isArrow());
345 }
346 
347 void StmtProfiler::VisitCompoundLiteralExpr(const CompoundLiteralExpr *S) {
348   VisitExpr(S);
349   ID.AddBoolean(S->isFileScope());
350 }
351 
352 void StmtProfiler::VisitCastExpr(const CastExpr *S) {
353   VisitExpr(S);
354 }
355 
356 void StmtProfiler::VisitImplicitCastExpr(const ImplicitCastExpr *S) {
357   VisitCastExpr(S);
358   ID.AddInteger(S->getValueKind());
359 }
360 
361 void StmtProfiler::VisitExplicitCastExpr(const ExplicitCastExpr *S) {
362   VisitCastExpr(S);
363   VisitType(S->getTypeAsWritten());
364 }
365 
366 void StmtProfiler::VisitCStyleCastExpr(const CStyleCastExpr *S) {
367   VisitExplicitCastExpr(S);
368 }
369 
370 void StmtProfiler::VisitBinaryOperator(const BinaryOperator *S) {
371   VisitExpr(S);
372   ID.AddInteger(S->getOpcode());
373 }
374 
375 void
376 StmtProfiler::VisitCompoundAssignOperator(const CompoundAssignOperator *S) {
377   VisitBinaryOperator(S);
378 }
379 
380 void StmtProfiler::VisitConditionalOperator(const ConditionalOperator *S) {
381   VisitExpr(S);
382 }
383 
384 void StmtProfiler::VisitBinaryConditionalOperator(
385     const BinaryConditionalOperator *S) {
386   VisitExpr(S);
387 }
388 
389 void StmtProfiler::VisitAddrLabelExpr(const AddrLabelExpr *S) {
390   VisitExpr(S);
391   VisitDecl(S->getLabel());
392 }
393 
394 void StmtProfiler::VisitStmtExpr(const StmtExpr *S) {
395   VisitExpr(S);
396 }
397 
398 void StmtProfiler::VisitShuffleVectorExpr(const ShuffleVectorExpr *S) {
399   VisitExpr(S);
400 }
401 
402 void StmtProfiler::VisitChooseExpr(const ChooseExpr *S) {
403   VisitExpr(S);
404 }
405 
406 void StmtProfiler::VisitGNUNullExpr(const GNUNullExpr *S) {
407   VisitExpr(S);
408 }
409 
410 void StmtProfiler::VisitVAArgExpr(const VAArgExpr *S) {
411   VisitExpr(S);
412 }
413 
414 void StmtProfiler::VisitInitListExpr(const InitListExpr *S) {
415   if (S->getSyntacticForm()) {
416     VisitInitListExpr(S->getSyntacticForm());
417     return;
418   }
419 
420   VisitExpr(S);
421 }
422 
423 void StmtProfiler::VisitDesignatedInitExpr(const DesignatedInitExpr *S) {
424   VisitExpr(S);
425   ID.AddBoolean(S->usesGNUSyntax());
426   for (DesignatedInitExpr::const_designators_iterator D =
427          S->designators_begin(), DEnd = S->designators_end();
428        D != DEnd; ++D) {
429     if (D->isFieldDesignator()) {
430       ID.AddInteger(0);
431       VisitName(D->getFieldName());
432       continue;
433     }
434 
435     if (D->isArrayDesignator()) {
436       ID.AddInteger(1);
437     } else {
438       assert(D->isArrayRangeDesignator());
439       ID.AddInteger(2);
440     }
441     ID.AddInteger(D->getFirstExprIndex());
442   }
443 }
444 
445 void StmtProfiler::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *S) {
446   VisitExpr(S);
447 }
448 
449 void StmtProfiler::VisitExtVectorElementExpr(const ExtVectorElementExpr *S) {
450   VisitExpr(S);
451   VisitName(&S->getAccessor());
452 }
453 
454 void StmtProfiler::VisitBlockExpr(const BlockExpr *S) {
455   VisitExpr(S);
456   VisitDecl(S->getBlockDecl());
457 }
458 
459 void StmtProfiler::VisitBlockDeclRefExpr(const BlockDeclRefExpr *S) {
460   VisitExpr(S);
461   VisitDecl(S->getDecl());
462   ID.AddBoolean(S->isByRef());
463   ID.AddBoolean(S->isConstQualAdded());
464 }
465 
466 void StmtProfiler::VisitGenericSelectionExpr(const GenericSelectionExpr *S) {
467   VisitExpr(S);
468   for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
469     QualType T = S->getAssocType(i);
470     if (T.isNull())
471       ID.AddPointer(0);
472     else
473       VisitType(T);
474     VisitExpr(S->getAssocExpr(i));
475   }
476 }
477 
478 void StmtProfiler::VisitPseudoObjectExpr(const PseudoObjectExpr *S) {
479   VisitExpr(S);
480   for (PseudoObjectExpr::const_semantics_iterator
481          i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i)
482     // Normally, we would not profile the source expressions of OVEs.
483     if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(*i))
484       Visit(OVE->getSourceExpr());
485 }
486 
487 void StmtProfiler::VisitAtomicExpr(const AtomicExpr *S) {
488   VisitExpr(S);
489   ID.AddInteger(S->getOp());
490 }
491 
492 static Stmt::StmtClass DecodeOperatorCall(const CXXOperatorCallExpr *S,
493                                           UnaryOperatorKind &UnaryOp,
494                                           BinaryOperatorKind &BinaryOp) {
495   switch (S->getOperator()) {
496   case OO_None:
497   case OO_New:
498   case OO_Delete:
499   case OO_Array_New:
500   case OO_Array_Delete:
501   case OO_Arrow:
502   case OO_Call:
503   case OO_Conditional:
504   case NUM_OVERLOADED_OPERATORS:
505     llvm_unreachable("Invalid operator call kind");
506     return Stmt::ArraySubscriptExprClass;
507 
508   case OO_Plus:
509     if (S->getNumArgs() == 1) {
510       UnaryOp = UO_Plus;
511       return Stmt::UnaryOperatorClass;
512     }
513 
514     BinaryOp = BO_Add;
515     return Stmt::BinaryOperatorClass;
516 
517   case OO_Minus:
518     if (S->getNumArgs() == 1) {
519       UnaryOp = UO_Minus;
520       return Stmt::UnaryOperatorClass;
521     }
522 
523     BinaryOp = BO_Sub;
524     return Stmt::BinaryOperatorClass;
525 
526   case OO_Star:
527     if (S->getNumArgs() == 1) {
528       UnaryOp = UO_Minus;
529       return Stmt::UnaryOperatorClass;
530     }
531 
532     BinaryOp = BO_Sub;
533     return Stmt::BinaryOperatorClass;
534 
535   case OO_Slash:
536     BinaryOp = BO_Div;
537     return Stmt::BinaryOperatorClass;
538 
539   case OO_Percent:
540     BinaryOp = BO_Rem;
541     return Stmt::BinaryOperatorClass;
542 
543   case OO_Caret:
544     BinaryOp = BO_Xor;
545     return Stmt::BinaryOperatorClass;
546 
547   case OO_Amp:
548     if (S->getNumArgs() == 1) {
549       UnaryOp = UO_AddrOf;
550       return Stmt::UnaryOperatorClass;
551     }
552 
553     BinaryOp = BO_And;
554     return Stmt::BinaryOperatorClass;
555 
556   case OO_Pipe:
557     BinaryOp = BO_Or;
558     return Stmt::BinaryOperatorClass;
559 
560   case OO_Tilde:
561     UnaryOp = UO_Not;
562     return Stmt::UnaryOperatorClass;
563 
564   case OO_Exclaim:
565     UnaryOp = UO_LNot;
566     return Stmt::UnaryOperatorClass;
567 
568   case OO_Equal:
569     BinaryOp = BO_Assign;
570     return Stmt::BinaryOperatorClass;
571 
572   case OO_Less:
573     BinaryOp = BO_LT;
574     return Stmt::BinaryOperatorClass;
575 
576   case OO_Greater:
577     BinaryOp = BO_GT;
578     return Stmt::BinaryOperatorClass;
579 
580   case OO_PlusEqual:
581     BinaryOp = BO_AddAssign;
582     return Stmt::CompoundAssignOperatorClass;
583 
584   case OO_MinusEqual:
585     BinaryOp = BO_SubAssign;
586     return Stmt::CompoundAssignOperatorClass;
587 
588   case OO_StarEqual:
589     BinaryOp = BO_MulAssign;
590     return Stmt::CompoundAssignOperatorClass;
591 
592   case OO_SlashEqual:
593     BinaryOp = BO_DivAssign;
594     return Stmt::CompoundAssignOperatorClass;
595 
596   case OO_PercentEqual:
597     BinaryOp = BO_RemAssign;
598     return Stmt::CompoundAssignOperatorClass;
599 
600   case OO_CaretEqual:
601     BinaryOp = BO_XorAssign;
602     return Stmt::CompoundAssignOperatorClass;
603 
604   case OO_AmpEqual:
605     BinaryOp = BO_AndAssign;
606     return Stmt::CompoundAssignOperatorClass;
607 
608   case OO_PipeEqual:
609     BinaryOp = BO_OrAssign;
610     return Stmt::CompoundAssignOperatorClass;
611 
612   case OO_LessLess:
613     BinaryOp = BO_Shl;
614     return Stmt::BinaryOperatorClass;
615 
616   case OO_GreaterGreater:
617     BinaryOp = BO_Shr;
618     return Stmt::BinaryOperatorClass;
619 
620   case OO_LessLessEqual:
621     BinaryOp = BO_ShlAssign;
622     return Stmt::CompoundAssignOperatorClass;
623 
624   case OO_GreaterGreaterEqual:
625     BinaryOp = BO_ShrAssign;
626     return Stmt::CompoundAssignOperatorClass;
627 
628   case OO_EqualEqual:
629     BinaryOp = BO_EQ;
630     return Stmt::BinaryOperatorClass;
631 
632   case OO_ExclaimEqual:
633     BinaryOp = BO_NE;
634     return Stmt::BinaryOperatorClass;
635 
636   case OO_LessEqual:
637     BinaryOp = BO_LE;
638     return Stmt::BinaryOperatorClass;
639 
640   case OO_GreaterEqual:
641     BinaryOp = BO_GE;
642     return Stmt::BinaryOperatorClass;
643 
644   case OO_AmpAmp:
645     BinaryOp = BO_LAnd;
646     return Stmt::BinaryOperatorClass;
647 
648   case OO_PipePipe:
649     BinaryOp = BO_LOr;
650     return Stmt::BinaryOperatorClass;
651 
652   case OO_PlusPlus:
653     UnaryOp = S->getNumArgs() == 1? UO_PreInc
654                                   : UO_PostInc;
655     return Stmt::UnaryOperatorClass;
656 
657   case OO_MinusMinus:
658     UnaryOp = S->getNumArgs() == 1? UO_PreDec
659                                   : UO_PostDec;
660     return Stmt::UnaryOperatorClass;
661 
662   case OO_Comma:
663     BinaryOp = BO_Comma;
664     return Stmt::BinaryOperatorClass;
665 
666 
667   case OO_ArrowStar:
668     BinaryOp = BO_PtrMemI;
669     return Stmt::BinaryOperatorClass;
670 
671   case OO_Subscript:
672     return Stmt::ArraySubscriptExprClass;
673   }
674 
675   llvm_unreachable("Invalid overloaded operator expression");
676 }
677 
678 
679 void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) {
680   if (S->isTypeDependent()) {
681     // Type-dependent operator calls are profiled like their underlying
682     // syntactic operator.
683     UnaryOperatorKind UnaryOp = UO_Extension;
684     BinaryOperatorKind BinaryOp = BO_Comma;
685     Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp);
686 
687     ID.AddInteger(SC);
688     for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
689       Visit(S->getArg(I));
690     if (SC == Stmt::UnaryOperatorClass)
691       ID.AddInteger(UnaryOp);
692     else if (SC == Stmt::BinaryOperatorClass ||
693              SC == Stmt::CompoundAssignOperatorClass)
694       ID.AddInteger(BinaryOp);
695     else
696       assert(SC == Stmt::ArraySubscriptExprClass);
697 
698     return;
699   }
700 
701   VisitCallExpr(S);
702   ID.AddInteger(S->getOperator());
703 }
704 
705 void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) {
706   VisitCallExpr(S);
707 }
708 
709 void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) {
710   VisitCallExpr(S);
711 }
712 
713 void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) {
714   VisitExpr(S);
715 }
716 
717 void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) {
718   VisitExplicitCastExpr(S);
719 }
720 
721 void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
722   VisitCXXNamedCastExpr(S);
723 }
724 
725 void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) {
726   VisitCXXNamedCastExpr(S);
727 }
728 
729 void
730 StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) {
731   VisitCXXNamedCastExpr(S);
732 }
733 
734 void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) {
735   VisitCXXNamedCastExpr(S);
736 }
737 
738 void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) {
739   VisitExpr(S);
740   ID.AddBoolean(S->getValue());
741 }
742 
743 void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
744   VisitExpr(S);
745 }
746 
747 void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) {
748   VisitExpr(S);
749   if (S->isTypeOperand())
750     VisitType(S->getTypeOperand());
751 }
752 
753 void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) {
754   VisitExpr(S);
755   if (S->isTypeOperand())
756     VisitType(S->getTypeOperand());
757 }
758 
759 void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) {
760   VisitExpr(S);
761 }
762 
763 void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) {
764   VisitExpr(S);
765 }
766 
767 void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) {
768   VisitExpr(S);
769   VisitDecl(S->getParam());
770 }
771 
772 void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) {
773   VisitExpr(S);
774   VisitDecl(
775          const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
776 }
777 
778 void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) {
779   VisitExpr(S);
780   VisitDecl(S->getConstructor());
781   ID.AddBoolean(S->isElidable());
782 }
783 
784 void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
785   VisitExplicitCastExpr(S);
786 }
787 
788 void
789 StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) {
790   VisitCXXConstructExpr(S);
791 }
792 
793 void
794 StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) {
795   VisitExpr(S);
796 }
797 
798 void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
799   VisitExpr(S);
800   ID.AddBoolean(S->isGlobalDelete());
801   ID.AddBoolean(S->isArrayForm());
802   VisitDecl(S->getOperatorDelete());
803 }
804 
805 
806 void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) {
807   VisitExpr(S);
808   VisitType(S->getAllocatedType());
809   VisitDecl(S->getOperatorNew());
810   VisitDecl(S->getOperatorDelete());
811   VisitDecl(S->getConstructor());
812   ID.AddBoolean(S->isArray());
813   ID.AddInteger(S->getNumPlacementArgs());
814   ID.AddBoolean(S->isGlobalNew());
815   ID.AddBoolean(S->isParenTypeId());
816   ID.AddBoolean(S->hasInitializer());
817   ID.AddInteger(S->getNumConstructorArgs());
818 }
819 
820 void
821 StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
822   VisitExpr(S);
823   ID.AddBoolean(S->isArrow());
824   VisitNestedNameSpecifier(S->getQualifier());
825   VisitType(S->getDestroyedType());
826 }
827 
828 void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) {
829   VisitExpr(S);
830   VisitNestedNameSpecifier(S->getQualifier());
831   VisitName(S->getName());
832   ID.AddBoolean(S->hasExplicitTemplateArgs());
833   if (S->hasExplicitTemplateArgs())
834     VisitTemplateArguments(S->getExplicitTemplateArgs().getTemplateArgs(),
835                            S->getExplicitTemplateArgs().NumTemplateArgs);
836 }
837 
838 void
839 StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) {
840   VisitOverloadExpr(S);
841 }
842 
843 void StmtProfiler::VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *S) {
844   VisitExpr(S);
845   ID.AddInteger(S->getTrait());
846   VisitType(S->getQueriedType());
847 }
848 
849 void StmtProfiler::VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *S) {
850   VisitExpr(S);
851   ID.AddInteger(S->getTrait());
852   VisitType(S->getLhsType());
853   VisitType(S->getRhsType());
854 }
855 
856 void StmtProfiler::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *S) {
857   VisitExpr(S);
858   ID.AddInteger(S->getTrait());
859   VisitType(S->getQueriedType());
860 }
861 
862 void StmtProfiler::VisitExpressionTraitExpr(const ExpressionTraitExpr *S) {
863   VisitExpr(S);
864   ID.AddInteger(S->getTrait());
865   VisitExpr(S->getQueriedExpression());
866 }
867 
868 void StmtProfiler::VisitDependentScopeDeclRefExpr(
869     const DependentScopeDeclRefExpr *S) {
870   VisitExpr(S);
871   VisitName(S->getDeclName());
872   VisitNestedNameSpecifier(S->getQualifier());
873   ID.AddBoolean(S->hasExplicitTemplateArgs());
874   if (S->hasExplicitTemplateArgs())
875     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
876 }
877 
878 void StmtProfiler::VisitExprWithCleanups(const ExprWithCleanups *S) {
879   VisitExpr(S);
880 }
881 
882 void StmtProfiler::VisitCXXUnresolvedConstructExpr(
883     const CXXUnresolvedConstructExpr *S) {
884   VisitExpr(S);
885   VisitType(S->getTypeAsWritten());
886 }
887 
888 void StmtProfiler::VisitCXXDependentScopeMemberExpr(
889     const CXXDependentScopeMemberExpr *S) {
890   ID.AddBoolean(S->isImplicitAccess());
891   if (!S->isImplicitAccess()) {
892     VisitExpr(S);
893     ID.AddBoolean(S->isArrow());
894   }
895   VisitNestedNameSpecifier(S->getQualifier());
896   VisitName(S->getMember());
897   ID.AddBoolean(S->hasExplicitTemplateArgs());
898   if (S->hasExplicitTemplateArgs())
899     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
900 }
901 
902 void StmtProfiler::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *S) {
903   ID.AddBoolean(S->isImplicitAccess());
904   if (!S->isImplicitAccess()) {
905     VisitExpr(S);
906     ID.AddBoolean(S->isArrow());
907   }
908   VisitNestedNameSpecifier(S->getQualifier());
909   VisitName(S->getMemberName());
910   ID.AddBoolean(S->hasExplicitTemplateArgs());
911   if (S->hasExplicitTemplateArgs())
912     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
913 }
914 
915 void StmtProfiler::VisitCXXNoexceptExpr(const CXXNoexceptExpr *S) {
916   VisitExpr(S);
917 }
918 
919 void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) {
920   VisitExpr(S);
921 }
922 
923 void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
924   VisitExpr(S);
925   VisitDecl(S->getPack());
926 }
927 
928 void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr(
929     const SubstNonTypeTemplateParmPackExpr *S) {
930   VisitExpr(S);
931   VisitDecl(S->getParameterPack());
932   VisitTemplateArgument(S->getArgumentPack());
933 }
934 
935 void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
936     const SubstNonTypeTemplateParmExpr *E) {
937   // Profile exactly as the replacement expression.
938   Visit(E->getReplacement());
939 }
940 
941 void StmtProfiler::VisitMaterializeTemporaryExpr(
942                                            const MaterializeTemporaryExpr *S) {
943   VisitExpr(S);
944 }
945 
946 void StmtProfiler::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
947   VisitExpr(E);
948 }
949 
950 void StmtProfiler::VisitObjCStringLiteral(const ObjCStringLiteral *S) {
951   VisitExpr(S);
952 }
953 
954 void StmtProfiler::VisitObjCEncodeExpr(const ObjCEncodeExpr *S) {
955   VisitExpr(S);
956   VisitType(S->getEncodedType());
957 }
958 
959 void StmtProfiler::VisitObjCSelectorExpr(const ObjCSelectorExpr *S) {
960   VisitExpr(S);
961   VisitName(S->getSelector());
962 }
963 
964 void StmtProfiler::VisitObjCProtocolExpr(const ObjCProtocolExpr *S) {
965   VisitExpr(S);
966   VisitDecl(S->getProtocol());
967 }
968 
969 void StmtProfiler::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *S) {
970   VisitExpr(S);
971   VisitDecl(S->getDecl());
972   ID.AddBoolean(S->isArrow());
973   ID.AddBoolean(S->isFreeIvar());
974 }
975 
976 void StmtProfiler::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *S) {
977   VisitExpr(S);
978   if (S->isImplicitProperty()) {
979     VisitDecl(S->getImplicitPropertyGetter());
980     VisitDecl(S->getImplicitPropertySetter());
981   } else {
982     VisitDecl(S->getExplicitProperty());
983   }
984   if (S->isSuperReceiver()) {
985     ID.AddBoolean(S->isSuperReceiver());
986     VisitType(S->getSuperReceiverType());
987   }
988 }
989 
990 void StmtProfiler::VisitObjCMessageExpr(const ObjCMessageExpr *S) {
991   VisitExpr(S);
992   VisitName(S->getSelector());
993   VisitDecl(S->getMethodDecl());
994 }
995 
996 void StmtProfiler::VisitObjCIsaExpr(const ObjCIsaExpr *S) {
997   VisitExpr(S);
998   ID.AddBoolean(S->isArrow());
999 }
1000 
1001 void StmtProfiler::VisitObjCIndirectCopyRestoreExpr(
1002     const ObjCIndirectCopyRestoreExpr *S) {
1003   VisitExpr(S);
1004   ID.AddBoolean(S->shouldCopy());
1005 }
1006 
1007 void StmtProfiler::VisitObjCBridgedCastExpr(const ObjCBridgedCastExpr *S) {
1008   VisitExplicitCastExpr(S);
1009   ID.AddBoolean(S->getBridgeKind());
1010 }
1011 
1012 void StmtProfiler::VisitDecl(const Decl *D) {
1013   ID.AddInteger(D? D->getKind() : 0);
1014 
1015   if (Canonical && D) {
1016     if (const NonTypeTemplateParmDecl *NTTP =
1017           dyn_cast<NonTypeTemplateParmDecl>(D)) {
1018       ID.AddInteger(NTTP->getDepth());
1019       ID.AddInteger(NTTP->getIndex());
1020       ID.AddBoolean(NTTP->isParameterPack());
1021       VisitType(NTTP->getType());
1022       return;
1023     }
1024 
1025     if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
1026       // The Itanium C++ ABI uses the type, scope depth, and scope
1027       // index of a parameter when mangling expressions that involve
1028       // function parameters, so we will use the parameter's type for
1029       // establishing function parameter identity. That way, our
1030       // definition of "equivalent" (per C++ [temp.over.link]) is at
1031       // least as strong as the definition of "equivalent" used for
1032       // name mangling.
1033       VisitType(Parm->getType());
1034       ID.AddInteger(Parm->getFunctionScopeDepth());
1035       ID.AddInteger(Parm->getFunctionScopeIndex());
1036       return;
1037     }
1038 
1039     if (const TemplateTemplateParmDecl *TTP =
1040           dyn_cast<TemplateTemplateParmDecl>(D)) {
1041       ID.AddInteger(TTP->getDepth());
1042       ID.AddInteger(TTP->getIndex());
1043       ID.AddBoolean(TTP->isParameterPack());
1044       return;
1045     }
1046   }
1047 
1048   ID.AddPointer(D? D->getCanonicalDecl() : 0);
1049 }
1050 
1051 void StmtProfiler::VisitType(QualType T) {
1052   if (Canonical)
1053     T = Context.getCanonicalType(T);
1054 
1055   ID.AddPointer(T.getAsOpaquePtr());
1056 }
1057 
1058 void StmtProfiler::VisitName(DeclarationName Name) {
1059   ID.AddPointer(Name.getAsOpaquePtr());
1060 }
1061 
1062 void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) {
1063   if (Canonical)
1064     NNS = Context.getCanonicalNestedNameSpecifier(NNS);
1065   ID.AddPointer(NNS);
1066 }
1067 
1068 void StmtProfiler::VisitTemplateName(TemplateName Name) {
1069   if (Canonical)
1070     Name = Context.getCanonicalTemplateName(Name);
1071 
1072   Name.Profile(ID);
1073 }
1074 
1075 void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args,
1076                                           unsigned NumArgs) {
1077   ID.AddInteger(NumArgs);
1078   for (unsigned I = 0; I != NumArgs; ++I)
1079     VisitTemplateArgument(Args[I].getArgument());
1080 }
1081 
1082 void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) {
1083   // Mostly repetitive with TemplateArgument::Profile!
1084   ID.AddInteger(Arg.getKind());
1085   switch (Arg.getKind()) {
1086   case TemplateArgument::Null:
1087     break;
1088 
1089   case TemplateArgument::Type:
1090     VisitType(Arg.getAsType());
1091     break;
1092 
1093   case TemplateArgument::Template:
1094   case TemplateArgument::TemplateExpansion:
1095     VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
1096     break;
1097 
1098   case TemplateArgument::Declaration:
1099     VisitDecl(Arg.getAsDecl());
1100     break;
1101 
1102   case TemplateArgument::Integral:
1103     Arg.getAsIntegral()->Profile(ID);
1104     VisitType(Arg.getIntegralType());
1105     break;
1106 
1107   case TemplateArgument::Expression:
1108     Visit(Arg.getAsExpr());
1109     break;
1110 
1111   case TemplateArgument::Pack:
1112     const TemplateArgument *Pack = Arg.pack_begin();
1113     for (unsigned i = 0, e = Arg.pack_size(); i != e; ++i)
1114       VisitTemplateArgument(Pack[i]);
1115     break;
1116   }
1117 }
1118 
1119 void Stmt::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1120                    bool Canonical) const {
1121   StmtProfiler Profiler(ID, Context, Canonical);
1122   Profiler.Visit(this);
1123 }
1124