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 
507   case OO_Plus:
508     if (S->getNumArgs() == 1) {
509       UnaryOp = UO_Plus;
510       return Stmt::UnaryOperatorClass;
511     }
512 
513     BinaryOp = BO_Add;
514     return Stmt::BinaryOperatorClass;
515 
516   case OO_Minus:
517     if (S->getNumArgs() == 1) {
518       UnaryOp = UO_Minus;
519       return Stmt::UnaryOperatorClass;
520     }
521 
522     BinaryOp = BO_Sub;
523     return Stmt::BinaryOperatorClass;
524 
525   case OO_Star:
526     if (S->getNumArgs() == 1) {
527       UnaryOp = UO_Minus;
528       return Stmt::UnaryOperatorClass;
529     }
530 
531     BinaryOp = BO_Sub;
532     return Stmt::BinaryOperatorClass;
533 
534   case OO_Slash:
535     BinaryOp = BO_Div;
536     return Stmt::BinaryOperatorClass;
537 
538   case OO_Percent:
539     BinaryOp = BO_Rem;
540     return Stmt::BinaryOperatorClass;
541 
542   case OO_Caret:
543     BinaryOp = BO_Xor;
544     return Stmt::BinaryOperatorClass;
545 
546   case OO_Amp:
547     if (S->getNumArgs() == 1) {
548       UnaryOp = UO_AddrOf;
549       return Stmt::UnaryOperatorClass;
550     }
551 
552     BinaryOp = BO_And;
553     return Stmt::BinaryOperatorClass;
554 
555   case OO_Pipe:
556     BinaryOp = BO_Or;
557     return Stmt::BinaryOperatorClass;
558 
559   case OO_Tilde:
560     UnaryOp = UO_Not;
561     return Stmt::UnaryOperatorClass;
562 
563   case OO_Exclaim:
564     UnaryOp = UO_LNot;
565     return Stmt::UnaryOperatorClass;
566 
567   case OO_Equal:
568     BinaryOp = BO_Assign;
569     return Stmt::BinaryOperatorClass;
570 
571   case OO_Less:
572     BinaryOp = BO_LT;
573     return Stmt::BinaryOperatorClass;
574 
575   case OO_Greater:
576     BinaryOp = BO_GT;
577     return Stmt::BinaryOperatorClass;
578 
579   case OO_PlusEqual:
580     BinaryOp = BO_AddAssign;
581     return Stmt::CompoundAssignOperatorClass;
582 
583   case OO_MinusEqual:
584     BinaryOp = BO_SubAssign;
585     return Stmt::CompoundAssignOperatorClass;
586 
587   case OO_StarEqual:
588     BinaryOp = BO_MulAssign;
589     return Stmt::CompoundAssignOperatorClass;
590 
591   case OO_SlashEqual:
592     BinaryOp = BO_DivAssign;
593     return Stmt::CompoundAssignOperatorClass;
594 
595   case OO_PercentEqual:
596     BinaryOp = BO_RemAssign;
597     return Stmt::CompoundAssignOperatorClass;
598 
599   case OO_CaretEqual:
600     BinaryOp = BO_XorAssign;
601     return Stmt::CompoundAssignOperatorClass;
602 
603   case OO_AmpEqual:
604     BinaryOp = BO_AndAssign;
605     return Stmt::CompoundAssignOperatorClass;
606 
607   case OO_PipeEqual:
608     BinaryOp = BO_OrAssign;
609     return Stmt::CompoundAssignOperatorClass;
610 
611   case OO_LessLess:
612     BinaryOp = BO_Shl;
613     return Stmt::BinaryOperatorClass;
614 
615   case OO_GreaterGreater:
616     BinaryOp = BO_Shr;
617     return Stmt::BinaryOperatorClass;
618 
619   case OO_LessLessEqual:
620     BinaryOp = BO_ShlAssign;
621     return Stmt::CompoundAssignOperatorClass;
622 
623   case OO_GreaterGreaterEqual:
624     BinaryOp = BO_ShrAssign;
625     return Stmt::CompoundAssignOperatorClass;
626 
627   case OO_EqualEqual:
628     BinaryOp = BO_EQ;
629     return Stmt::BinaryOperatorClass;
630 
631   case OO_ExclaimEqual:
632     BinaryOp = BO_NE;
633     return Stmt::BinaryOperatorClass;
634 
635   case OO_LessEqual:
636     BinaryOp = BO_LE;
637     return Stmt::BinaryOperatorClass;
638 
639   case OO_GreaterEqual:
640     BinaryOp = BO_GE;
641     return Stmt::BinaryOperatorClass;
642 
643   case OO_AmpAmp:
644     BinaryOp = BO_LAnd;
645     return Stmt::BinaryOperatorClass;
646 
647   case OO_PipePipe:
648     BinaryOp = BO_LOr;
649     return Stmt::BinaryOperatorClass;
650 
651   case OO_PlusPlus:
652     UnaryOp = S->getNumArgs() == 1? UO_PreInc
653                                   : UO_PostInc;
654     return Stmt::UnaryOperatorClass;
655 
656   case OO_MinusMinus:
657     UnaryOp = S->getNumArgs() == 1? UO_PreDec
658                                   : UO_PostDec;
659     return Stmt::UnaryOperatorClass;
660 
661   case OO_Comma:
662     BinaryOp = BO_Comma;
663     return Stmt::BinaryOperatorClass;
664 
665 
666   case OO_ArrowStar:
667     BinaryOp = BO_PtrMemI;
668     return Stmt::BinaryOperatorClass;
669 
670   case OO_Subscript:
671     return Stmt::ArraySubscriptExprClass;
672   }
673 
674   llvm_unreachable("Invalid overloaded operator expression");
675 }
676 
677 
678 void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) {
679   if (S->isTypeDependent()) {
680     // Type-dependent operator calls are profiled like their underlying
681     // syntactic operator.
682     UnaryOperatorKind UnaryOp = UO_Extension;
683     BinaryOperatorKind BinaryOp = BO_Comma;
684     Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp);
685 
686     ID.AddInteger(SC);
687     for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
688       Visit(S->getArg(I));
689     if (SC == Stmt::UnaryOperatorClass)
690       ID.AddInteger(UnaryOp);
691     else if (SC == Stmt::BinaryOperatorClass ||
692              SC == Stmt::CompoundAssignOperatorClass)
693       ID.AddInteger(BinaryOp);
694     else
695       assert(SC == Stmt::ArraySubscriptExprClass);
696 
697     return;
698   }
699 
700   VisitCallExpr(S);
701   ID.AddInteger(S->getOperator());
702 }
703 
704 void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) {
705   VisitCallExpr(S);
706 }
707 
708 void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) {
709   VisitCallExpr(S);
710 }
711 
712 void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) {
713   VisitExpr(S);
714 }
715 
716 void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) {
717   VisitExplicitCastExpr(S);
718 }
719 
720 void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
721   VisitCXXNamedCastExpr(S);
722 }
723 
724 void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) {
725   VisitCXXNamedCastExpr(S);
726 }
727 
728 void
729 StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) {
730   VisitCXXNamedCastExpr(S);
731 }
732 
733 void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) {
734   VisitCXXNamedCastExpr(S);
735 }
736 
737 void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) {
738   VisitExpr(S);
739   ID.AddBoolean(S->getValue());
740 }
741 
742 void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
743   VisitExpr(S);
744 }
745 
746 void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) {
747   VisitExpr(S);
748   if (S->isTypeOperand())
749     VisitType(S->getTypeOperand());
750 }
751 
752 void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) {
753   VisitExpr(S);
754   if (S->isTypeOperand())
755     VisitType(S->getTypeOperand());
756 }
757 
758 void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) {
759   VisitExpr(S);
760 }
761 
762 void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) {
763   VisitExpr(S);
764 }
765 
766 void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) {
767   VisitExpr(S);
768   VisitDecl(S->getParam());
769 }
770 
771 void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) {
772   VisitExpr(S);
773   VisitDecl(
774          const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
775 }
776 
777 void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) {
778   VisitExpr(S);
779   VisitDecl(S->getConstructor());
780   ID.AddBoolean(S->isElidable());
781 }
782 
783 void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
784   VisitExplicitCastExpr(S);
785 }
786 
787 void
788 StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) {
789   VisitCXXConstructExpr(S);
790 }
791 
792 void
793 StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) {
794   VisitExpr(S);
795 }
796 
797 void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
798   VisitExpr(S);
799   ID.AddBoolean(S->isGlobalDelete());
800   ID.AddBoolean(S->isArrayForm());
801   VisitDecl(S->getOperatorDelete());
802 }
803 
804 
805 void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) {
806   VisitExpr(S);
807   VisitType(S->getAllocatedType());
808   VisitDecl(S->getOperatorNew());
809   VisitDecl(S->getOperatorDelete());
810   VisitDecl(S->getConstructor());
811   ID.AddBoolean(S->isArray());
812   ID.AddInteger(S->getNumPlacementArgs());
813   ID.AddBoolean(S->isGlobalNew());
814   ID.AddBoolean(S->isParenTypeId());
815   ID.AddBoolean(S->hasInitializer());
816   ID.AddInteger(S->getNumConstructorArgs());
817 }
818 
819 void
820 StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
821   VisitExpr(S);
822   ID.AddBoolean(S->isArrow());
823   VisitNestedNameSpecifier(S->getQualifier());
824   VisitType(S->getDestroyedType());
825 }
826 
827 void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) {
828   VisitExpr(S);
829   VisitNestedNameSpecifier(S->getQualifier());
830   VisitName(S->getName());
831   ID.AddBoolean(S->hasExplicitTemplateArgs());
832   if (S->hasExplicitTemplateArgs())
833     VisitTemplateArguments(S->getExplicitTemplateArgs().getTemplateArgs(),
834                            S->getExplicitTemplateArgs().NumTemplateArgs);
835 }
836 
837 void
838 StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) {
839   VisitOverloadExpr(S);
840 }
841 
842 void StmtProfiler::VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *S) {
843   VisitExpr(S);
844   ID.AddInteger(S->getTrait());
845   VisitType(S->getQueriedType());
846 }
847 
848 void StmtProfiler::VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *S) {
849   VisitExpr(S);
850   ID.AddInteger(S->getTrait());
851   VisitType(S->getLhsType());
852   VisitType(S->getRhsType());
853 }
854 
855 void StmtProfiler::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *S) {
856   VisitExpr(S);
857   ID.AddInteger(S->getTrait());
858   VisitType(S->getQueriedType());
859 }
860 
861 void StmtProfiler::VisitExpressionTraitExpr(const ExpressionTraitExpr *S) {
862   VisitExpr(S);
863   ID.AddInteger(S->getTrait());
864   VisitExpr(S->getQueriedExpression());
865 }
866 
867 void StmtProfiler::VisitDependentScopeDeclRefExpr(
868     const DependentScopeDeclRefExpr *S) {
869   VisitExpr(S);
870   VisitName(S->getDeclName());
871   VisitNestedNameSpecifier(S->getQualifier());
872   ID.AddBoolean(S->hasExplicitTemplateArgs());
873   if (S->hasExplicitTemplateArgs())
874     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
875 }
876 
877 void StmtProfiler::VisitExprWithCleanups(const ExprWithCleanups *S) {
878   VisitExpr(S);
879 }
880 
881 void StmtProfiler::VisitCXXUnresolvedConstructExpr(
882     const CXXUnresolvedConstructExpr *S) {
883   VisitExpr(S);
884   VisitType(S->getTypeAsWritten());
885 }
886 
887 void StmtProfiler::VisitCXXDependentScopeMemberExpr(
888     const CXXDependentScopeMemberExpr *S) {
889   ID.AddBoolean(S->isImplicitAccess());
890   if (!S->isImplicitAccess()) {
891     VisitExpr(S);
892     ID.AddBoolean(S->isArrow());
893   }
894   VisitNestedNameSpecifier(S->getQualifier());
895   VisitName(S->getMember());
896   ID.AddBoolean(S->hasExplicitTemplateArgs());
897   if (S->hasExplicitTemplateArgs())
898     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
899 }
900 
901 void StmtProfiler::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *S) {
902   ID.AddBoolean(S->isImplicitAccess());
903   if (!S->isImplicitAccess()) {
904     VisitExpr(S);
905     ID.AddBoolean(S->isArrow());
906   }
907   VisitNestedNameSpecifier(S->getQualifier());
908   VisitName(S->getMemberName());
909   ID.AddBoolean(S->hasExplicitTemplateArgs());
910   if (S->hasExplicitTemplateArgs())
911     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
912 }
913 
914 void StmtProfiler::VisitCXXNoexceptExpr(const CXXNoexceptExpr *S) {
915   VisitExpr(S);
916 }
917 
918 void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) {
919   VisitExpr(S);
920 }
921 
922 void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
923   VisitExpr(S);
924   VisitDecl(S->getPack());
925 }
926 
927 void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr(
928     const SubstNonTypeTemplateParmPackExpr *S) {
929   VisitExpr(S);
930   VisitDecl(S->getParameterPack());
931   VisitTemplateArgument(S->getArgumentPack());
932 }
933 
934 void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
935     const SubstNonTypeTemplateParmExpr *E) {
936   // Profile exactly as the replacement expression.
937   Visit(E->getReplacement());
938 }
939 
940 void StmtProfiler::VisitMaterializeTemporaryExpr(
941                                            const MaterializeTemporaryExpr *S) {
942   VisitExpr(S);
943 }
944 
945 void StmtProfiler::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
946   VisitExpr(E);
947 }
948 
949 void StmtProfiler::VisitObjCStringLiteral(const ObjCStringLiteral *S) {
950   VisitExpr(S);
951 }
952 
953 void StmtProfiler::VisitObjCEncodeExpr(const ObjCEncodeExpr *S) {
954   VisitExpr(S);
955   VisitType(S->getEncodedType());
956 }
957 
958 void StmtProfiler::VisitObjCSelectorExpr(const ObjCSelectorExpr *S) {
959   VisitExpr(S);
960   VisitName(S->getSelector());
961 }
962 
963 void StmtProfiler::VisitObjCProtocolExpr(const ObjCProtocolExpr *S) {
964   VisitExpr(S);
965   VisitDecl(S->getProtocol());
966 }
967 
968 void StmtProfiler::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *S) {
969   VisitExpr(S);
970   VisitDecl(S->getDecl());
971   ID.AddBoolean(S->isArrow());
972   ID.AddBoolean(S->isFreeIvar());
973 }
974 
975 void StmtProfiler::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *S) {
976   VisitExpr(S);
977   if (S->isImplicitProperty()) {
978     VisitDecl(S->getImplicitPropertyGetter());
979     VisitDecl(S->getImplicitPropertySetter());
980   } else {
981     VisitDecl(S->getExplicitProperty());
982   }
983   if (S->isSuperReceiver()) {
984     ID.AddBoolean(S->isSuperReceiver());
985     VisitType(S->getSuperReceiverType());
986   }
987 }
988 
989 void StmtProfiler::VisitObjCMessageExpr(const ObjCMessageExpr *S) {
990   VisitExpr(S);
991   VisitName(S->getSelector());
992   VisitDecl(S->getMethodDecl());
993 }
994 
995 void StmtProfiler::VisitObjCIsaExpr(const ObjCIsaExpr *S) {
996   VisitExpr(S);
997   ID.AddBoolean(S->isArrow());
998 }
999 
1000 void StmtProfiler::VisitObjCIndirectCopyRestoreExpr(
1001     const ObjCIndirectCopyRestoreExpr *S) {
1002   VisitExpr(S);
1003   ID.AddBoolean(S->shouldCopy());
1004 }
1005 
1006 void StmtProfiler::VisitObjCBridgedCastExpr(const ObjCBridgedCastExpr *S) {
1007   VisitExplicitCastExpr(S);
1008   ID.AddBoolean(S->getBridgeKind());
1009 }
1010 
1011 void StmtProfiler::VisitDecl(const Decl *D) {
1012   ID.AddInteger(D? D->getKind() : 0);
1013 
1014   if (Canonical && D) {
1015     if (const NonTypeTemplateParmDecl *NTTP =
1016           dyn_cast<NonTypeTemplateParmDecl>(D)) {
1017       ID.AddInteger(NTTP->getDepth());
1018       ID.AddInteger(NTTP->getIndex());
1019       ID.AddBoolean(NTTP->isParameterPack());
1020       VisitType(NTTP->getType());
1021       return;
1022     }
1023 
1024     if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
1025       // The Itanium C++ ABI uses the type, scope depth, and scope
1026       // index of a parameter when mangling expressions that involve
1027       // function parameters, so we will use the parameter's type for
1028       // establishing function parameter identity. That way, our
1029       // definition of "equivalent" (per C++ [temp.over.link]) is at
1030       // least as strong as the definition of "equivalent" used for
1031       // name mangling.
1032       VisitType(Parm->getType());
1033       ID.AddInteger(Parm->getFunctionScopeDepth());
1034       ID.AddInteger(Parm->getFunctionScopeIndex());
1035       return;
1036     }
1037 
1038     if (const TemplateTemplateParmDecl *TTP =
1039           dyn_cast<TemplateTemplateParmDecl>(D)) {
1040       ID.AddInteger(TTP->getDepth());
1041       ID.AddInteger(TTP->getIndex());
1042       ID.AddBoolean(TTP->isParameterPack());
1043       return;
1044     }
1045   }
1046 
1047   ID.AddPointer(D? D->getCanonicalDecl() : 0);
1048 }
1049 
1050 void StmtProfiler::VisitType(QualType T) {
1051   if (Canonical)
1052     T = Context.getCanonicalType(T);
1053 
1054   ID.AddPointer(T.getAsOpaquePtr());
1055 }
1056 
1057 void StmtProfiler::VisitName(DeclarationName Name) {
1058   ID.AddPointer(Name.getAsOpaquePtr());
1059 }
1060 
1061 void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) {
1062   if (Canonical)
1063     NNS = Context.getCanonicalNestedNameSpecifier(NNS);
1064   ID.AddPointer(NNS);
1065 }
1066 
1067 void StmtProfiler::VisitTemplateName(TemplateName Name) {
1068   if (Canonical)
1069     Name = Context.getCanonicalTemplateName(Name);
1070 
1071   Name.Profile(ID);
1072 }
1073 
1074 void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args,
1075                                           unsigned NumArgs) {
1076   ID.AddInteger(NumArgs);
1077   for (unsigned I = 0; I != NumArgs; ++I)
1078     VisitTemplateArgument(Args[I].getArgument());
1079 }
1080 
1081 void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) {
1082   // Mostly repetitive with TemplateArgument::Profile!
1083   ID.AddInteger(Arg.getKind());
1084   switch (Arg.getKind()) {
1085   case TemplateArgument::Null:
1086     break;
1087 
1088   case TemplateArgument::Type:
1089     VisitType(Arg.getAsType());
1090     break;
1091 
1092   case TemplateArgument::Template:
1093   case TemplateArgument::TemplateExpansion:
1094     VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
1095     break;
1096 
1097   case TemplateArgument::Declaration:
1098     VisitDecl(Arg.getAsDecl());
1099     break;
1100 
1101   case TemplateArgument::Integral:
1102     Arg.getAsIntegral()->Profile(ID);
1103     VisitType(Arg.getIntegralType());
1104     break;
1105 
1106   case TemplateArgument::Expression:
1107     Visit(Arg.getAsExpr());
1108     break;
1109 
1110   case TemplateArgument::Pack:
1111     const TemplateArgument *Pack = Arg.pack_begin();
1112     for (unsigned i = 0, e = Arg.pack_size(); i != e; ++i)
1113       VisitTemplateArgument(Pack[i]);
1114     break;
1115   }
1116 }
1117 
1118 void Stmt::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1119                    bool Canonical) const {
1120   StmtProfiler Profiler(ID, Context, Canonical);
1121   Profiler.Visit(this);
1122 }
1123