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 (const Stmt *SubStmt : S->children()) {
73     if (SubStmt)
74       Visit(SubStmt);
75     else
76       ID.AddInteger(0);
77   }
78 }
79 
80 void StmtProfiler::VisitDeclStmt(const DeclStmt *S) {
81   VisitStmt(S);
82   for (const auto *D : S->decls())
83     VisitDecl(D);
84 }
85 
86 void StmtProfiler::VisitNullStmt(const NullStmt *S) {
87   VisitStmt(S);
88 }
89 
90 void StmtProfiler::VisitCompoundStmt(const CompoundStmt *S) {
91   VisitStmt(S);
92 }
93 
94 void StmtProfiler::VisitSwitchCase(const SwitchCase *S) {
95   VisitStmt(S);
96 }
97 
98 void StmtProfiler::VisitCaseStmt(const CaseStmt *S) {
99   VisitStmt(S);
100 }
101 
102 void StmtProfiler::VisitDefaultStmt(const DefaultStmt *S) {
103   VisitStmt(S);
104 }
105 
106 void StmtProfiler::VisitLabelStmt(const LabelStmt *S) {
107   VisitStmt(S);
108   VisitDecl(S->getDecl());
109 }
110 
111 void StmtProfiler::VisitAttributedStmt(const AttributedStmt *S) {
112   VisitStmt(S);
113   // TODO: maybe visit attributes?
114 }
115 
116 void StmtProfiler::VisitIfStmt(const IfStmt *S) {
117   VisitStmt(S);
118   VisitDecl(S->getConditionVariable());
119 }
120 
121 void StmtProfiler::VisitSwitchStmt(const SwitchStmt *S) {
122   VisitStmt(S);
123   VisitDecl(S->getConditionVariable());
124 }
125 
126 void StmtProfiler::VisitWhileStmt(const WhileStmt *S) {
127   VisitStmt(S);
128   VisitDecl(S->getConditionVariable());
129 }
130 
131 void StmtProfiler::VisitDoStmt(const DoStmt *S) {
132   VisitStmt(S);
133 }
134 
135 void StmtProfiler::VisitForStmt(const ForStmt *S) {
136   VisitStmt(S);
137 }
138 
139 void StmtProfiler::VisitGotoStmt(const GotoStmt *S) {
140   VisitStmt(S);
141   VisitDecl(S->getLabel());
142 }
143 
144 void StmtProfiler::VisitIndirectGotoStmt(const IndirectGotoStmt *S) {
145   VisitStmt(S);
146 }
147 
148 void StmtProfiler::VisitContinueStmt(const ContinueStmt *S) {
149   VisitStmt(S);
150 }
151 
152 void StmtProfiler::VisitBreakStmt(const BreakStmt *S) {
153   VisitStmt(S);
154 }
155 
156 void StmtProfiler::VisitReturnStmt(const ReturnStmt *S) {
157   VisitStmt(S);
158 }
159 
160 void StmtProfiler::VisitGCCAsmStmt(const GCCAsmStmt *S) {
161   VisitStmt(S);
162   ID.AddBoolean(S->isVolatile());
163   ID.AddBoolean(S->isSimple());
164   VisitStringLiteral(S->getAsmString());
165   ID.AddInteger(S->getNumOutputs());
166   for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
167     ID.AddString(S->getOutputName(I));
168     VisitStringLiteral(S->getOutputConstraintLiteral(I));
169   }
170   ID.AddInteger(S->getNumInputs());
171   for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
172     ID.AddString(S->getInputName(I));
173     VisitStringLiteral(S->getInputConstraintLiteral(I));
174   }
175   ID.AddInteger(S->getNumClobbers());
176   for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
177     VisitStringLiteral(S->getClobberStringLiteral(I));
178 }
179 
180 void StmtProfiler::VisitMSAsmStmt(const MSAsmStmt *S) {
181   // FIXME: Implement MS style inline asm statement profiler.
182   VisitStmt(S);
183 }
184 
185 void StmtProfiler::VisitCXXCatchStmt(const CXXCatchStmt *S) {
186   VisitStmt(S);
187   VisitType(S->getCaughtType());
188 }
189 
190 void StmtProfiler::VisitCXXTryStmt(const CXXTryStmt *S) {
191   VisitStmt(S);
192 }
193 
194 void StmtProfiler::VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
195   VisitStmt(S);
196 }
197 
198 void StmtProfiler::VisitMSDependentExistsStmt(const MSDependentExistsStmt *S) {
199   VisitStmt(S);
200   ID.AddBoolean(S->isIfExists());
201   VisitNestedNameSpecifier(S->getQualifierLoc().getNestedNameSpecifier());
202   VisitName(S->getNameInfo().getName());
203 }
204 
205 void StmtProfiler::VisitSEHTryStmt(const SEHTryStmt *S) {
206   VisitStmt(S);
207 }
208 
209 void StmtProfiler::VisitSEHFinallyStmt(const SEHFinallyStmt *S) {
210   VisitStmt(S);
211 }
212 
213 void StmtProfiler::VisitSEHExceptStmt(const SEHExceptStmt *S) {
214   VisitStmt(S);
215 }
216 
217 void StmtProfiler::VisitSEHLeaveStmt(const SEHLeaveStmt *S) {
218   VisitStmt(S);
219 }
220 
221 void StmtProfiler::VisitCapturedStmt(const CapturedStmt *S) {
222   VisitStmt(S);
223 }
224 
225 void StmtProfiler::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
226   VisitStmt(S);
227 }
228 
229 void StmtProfiler::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *S) {
230   VisitStmt(S);
231   ID.AddBoolean(S->hasEllipsis());
232   if (S->getCatchParamDecl())
233     VisitType(S->getCatchParamDecl()->getType());
234 }
235 
236 void StmtProfiler::VisitObjCAtFinallyStmt(const ObjCAtFinallyStmt *S) {
237   VisitStmt(S);
238 }
239 
240 void StmtProfiler::VisitObjCAtTryStmt(const ObjCAtTryStmt *S) {
241   VisitStmt(S);
242 }
243 
244 void
245 StmtProfiler::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S) {
246   VisitStmt(S);
247 }
248 
249 void StmtProfiler::VisitObjCAtThrowStmt(const ObjCAtThrowStmt *S) {
250   VisitStmt(S);
251 }
252 
253 void
254 StmtProfiler::VisitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt *S) {
255   VisitStmt(S);
256 }
257 
258 namespace {
259 class OMPClauseProfiler : public ConstOMPClauseVisitor<OMPClauseProfiler> {
260   StmtProfiler *Profiler;
261   /// \brief Process clauses with list of variables.
262   template <typename T>
263   void VisitOMPClauseList(T *Node);
264 public:
265   OMPClauseProfiler(StmtProfiler *P) : Profiler(P) { }
266 #define OPENMP_CLAUSE(Name, Class)                                             \
267   void Visit##Class(const Class *C);
268 #include "clang/Basic/OpenMPKinds.def"
269 };
270 
271 void OMPClauseProfiler::VisitOMPIfClause(const OMPIfClause *C) {
272   if (C->getCondition())
273     Profiler->VisitStmt(C->getCondition());
274 }
275 
276 void OMPClauseProfiler::VisitOMPFinalClause(const OMPFinalClause *C) {
277   if (C->getCondition())
278     Profiler->VisitStmt(C->getCondition());
279 }
280 
281 void OMPClauseProfiler::VisitOMPNumThreadsClause(const OMPNumThreadsClause *C) {
282   if (C->getNumThreads())
283     Profiler->VisitStmt(C->getNumThreads());
284 }
285 
286 void OMPClauseProfiler::VisitOMPSafelenClause(const OMPSafelenClause *C) {
287   if (C->getSafelen())
288     Profiler->VisitStmt(C->getSafelen());
289 }
290 
291 void OMPClauseProfiler::VisitOMPCollapseClause(const OMPCollapseClause *C) {
292   if (C->getNumForLoops())
293     Profiler->VisitStmt(C->getNumForLoops());
294 }
295 
296 void OMPClauseProfiler::VisitOMPDefaultClause(const OMPDefaultClause *C) { }
297 
298 void OMPClauseProfiler::VisitOMPProcBindClause(const OMPProcBindClause *C) { }
299 
300 void OMPClauseProfiler::VisitOMPScheduleClause(const OMPScheduleClause *C) {
301   if (C->getChunkSize()) {
302     Profiler->VisitStmt(C->getChunkSize());
303     if (C->getHelperChunkSize()) {
304       Profiler->VisitStmt(C->getChunkSize());
305     }
306   }
307 }
308 
309 void OMPClauseProfiler::VisitOMPOrderedClause(const OMPOrderedClause *) {}
310 
311 void OMPClauseProfiler::VisitOMPNowaitClause(const OMPNowaitClause *) {}
312 
313 void OMPClauseProfiler::VisitOMPUntiedClause(const OMPUntiedClause *) {}
314 
315 void OMPClauseProfiler::VisitOMPMergeableClause(const OMPMergeableClause *) {}
316 
317 void OMPClauseProfiler::VisitOMPReadClause(const OMPReadClause *) {}
318 
319 void OMPClauseProfiler::VisitOMPWriteClause(const OMPWriteClause *) {}
320 
321 void OMPClauseProfiler::VisitOMPUpdateClause(const OMPUpdateClause *) {}
322 
323 void OMPClauseProfiler::VisitOMPCaptureClause(const OMPCaptureClause *) {}
324 
325 void OMPClauseProfiler::VisitOMPSeqCstClause(const OMPSeqCstClause *) {}
326 
327 template<typename T>
328 void OMPClauseProfiler::VisitOMPClauseList(T *Node) {
329   for (auto *E : Node->varlists()) {
330     Profiler->VisitStmt(E);
331   }
332 }
333 
334 void OMPClauseProfiler::VisitOMPPrivateClause(const OMPPrivateClause *C) {
335   VisitOMPClauseList(C);
336   for (auto *E : C->private_copies()) {
337     Profiler->VisitStmt(E);
338   }
339 }
340 void
341 OMPClauseProfiler::VisitOMPFirstprivateClause(const OMPFirstprivateClause *C) {
342   VisitOMPClauseList(C);
343   for (auto *E : C->private_copies()) {
344     Profiler->VisitStmt(E);
345   }
346   for (auto *E : C->inits()) {
347     Profiler->VisitStmt(E);
348   }
349 }
350 void
351 OMPClauseProfiler::VisitOMPLastprivateClause(const OMPLastprivateClause *C) {
352   VisitOMPClauseList(C);
353   for (auto *E : C->source_exprs()) {
354     Profiler->VisitStmt(E);
355   }
356   for (auto *E : C->destination_exprs()) {
357     Profiler->VisitStmt(E);
358   }
359   for (auto *E : C->assignment_ops()) {
360     Profiler->VisitStmt(E);
361   }
362 }
363 void OMPClauseProfiler::VisitOMPSharedClause(const OMPSharedClause *C) {
364   VisitOMPClauseList(C);
365 }
366 void OMPClauseProfiler::VisitOMPReductionClause(
367                                          const OMPReductionClause *C) {
368   Profiler->VisitNestedNameSpecifier(
369       C->getQualifierLoc().getNestedNameSpecifier());
370   Profiler->VisitName(C->getNameInfo().getName());
371   VisitOMPClauseList(C);
372   for (auto *E : C->lhs_exprs()) {
373     Profiler->VisitStmt(E);
374   }
375   for (auto *E : C->rhs_exprs()) {
376     Profiler->VisitStmt(E);
377   }
378   for (auto *E : C->reduction_ops()) {
379     Profiler->VisitStmt(E);
380   }
381 }
382 void OMPClauseProfiler::VisitOMPLinearClause(const OMPLinearClause *C) {
383   VisitOMPClauseList(C);
384   for (auto *E : C->inits()) {
385     Profiler->VisitStmt(E);
386   }
387   for (auto *E : C->updates()) {
388     Profiler->VisitStmt(E);
389   }
390   for (auto *E : C->finals()) {
391     Profiler->VisitStmt(E);
392   }
393   Profiler->VisitStmt(C->getStep());
394   Profiler->VisitStmt(C->getCalcStep());
395 }
396 void OMPClauseProfiler::VisitOMPAlignedClause(const OMPAlignedClause *C) {
397   VisitOMPClauseList(C);
398   Profiler->VisitStmt(C->getAlignment());
399 }
400 void OMPClauseProfiler::VisitOMPCopyinClause(const OMPCopyinClause *C) {
401   VisitOMPClauseList(C);
402   for (auto *E : C->source_exprs()) {
403     Profiler->VisitStmt(E);
404   }
405   for (auto *E : C->destination_exprs()) {
406     Profiler->VisitStmt(E);
407   }
408   for (auto *E : C->assignment_ops()) {
409     Profiler->VisitStmt(E);
410   }
411 }
412 void
413 OMPClauseProfiler::VisitOMPCopyprivateClause(const OMPCopyprivateClause *C) {
414   VisitOMPClauseList(C);
415   for (auto *E : C->source_exprs()) {
416     Profiler->VisitStmt(E);
417   }
418   for (auto *E : C->destination_exprs()) {
419     Profiler->VisitStmt(E);
420   }
421   for (auto *E : C->assignment_ops()) {
422     Profiler->VisitStmt(E);
423   }
424 }
425 void OMPClauseProfiler::VisitOMPFlushClause(const OMPFlushClause *C) {
426   VisitOMPClauseList(C);
427 }
428 void OMPClauseProfiler::VisitOMPDependClause(const OMPDependClause *C) {
429   VisitOMPClauseList(C);
430 }
431 }
432 
433 void
434 StmtProfiler::VisitOMPExecutableDirective(const OMPExecutableDirective *S) {
435   VisitStmt(S);
436   OMPClauseProfiler P(this);
437   ArrayRef<OMPClause *> Clauses = S->clauses();
438   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
439        I != E; ++I)
440     if (*I)
441       P.Visit(*I);
442 }
443 
444 void StmtProfiler::VisitOMPLoopDirective(const OMPLoopDirective *S) {
445   VisitOMPExecutableDirective(S);
446 }
447 
448 void StmtProfiler::VisitOMPParallelDirective(const OMPParallelDirective *S) {
449   VisitOMPExecutableDirective(S);
450 }
451 
452 void StmtProfiler::VisitOMPSimdDirective(const OMPSimdDirective *S) {
453   VisitOMPLoopDirective(S);
454 }
455 
456 void StmtProfiler::VisitOMPForDirective(const OMPForDirective *S) {
457   VisitOMPLoopDirective(S);
458 }
459 
460 void StmtProfiler::VisitOMPForSimdDirective(const OMPForSimdDirective *S) {
461   VisitOMPLoopDirective(S);
462 }
463 
464 void StmtProfiler::VisitOMPSectionsDirective(const OMPSectionsDirective *S) {
465   VisitOMPExecutableDirective(S);
466 }
467 
468 void StmtProfiler::VisitOMPSectionDirective(const OMPSectionDirective *S) {
469   VisitOMPExecutableDirective(S);
470 }
471 
472 void StmtProfiler::VisitOMPSingleDirective(const OMPSingleDirective *S) {
473   VisitOMPExecutableDirective(S);
474 }
475 
476 void StmtProfiler::VisitOMPMasterDirective(const OMPMasterDirective *S) {
477   VisitOMPExecutableDirective(S);
478 }
479 
480 void StmtProfiler::VisitOMPCriticalDirective(const OMPCriticalDirective *S) {
481   VisitOMPExecutableDirective(S);
482   VisitName(S->getDirectiveName().getName());
483 }
484 
485 void
486 StmtProfiler::VisitOMPParallelForDirective(const OMPParallelForDirective *S) {
487   VisitOMPLoopDirective(S);
488 }
489 
490 void StmtProfiler::VisitOMPParallelForSimdDirective(
491     const OMPParallelForSimdDirective *S) {
492   VisitOMPLoopDirective(S);
493 }
494 
495 void StmtProfiler::VisitOMPParallelSectionsDirective(
496     const OMPParallelSectionsDirective *S) {
497   VisitOMPExecutableDirective(S);
498 }
499 
500 void StmtProfiler::VisitOMPTaskDirective(const OMPTaskDirective *S) {
501   VisitOMPExecutableDirective(S);
502 }
503 
504 void StmtProfiler::VisitOMPTaskyieldDirective(const OMPTaskyieldDirective *S) {
505   VisitOMPExecutableDirective(S);
506 }
507 
508 void StmtProfiler::VisitOMPBarrierDirective(const OMPBarrierDirective *S) {
509   VisitOMPExecutableDirective(S);
510 }
511 
512 void StmtProfiler::VisitOMPTaskwaitDirective(const OMPTaskwaitDirective *S) {
513   VisitOMPExecutableDirective(S);
514 }
515 
516 void StmtProfiler::VisitOMPTaskgroupDirective(const OMPTaskgroupDirective *S) {
517   VisitOMPExecutableDirective(S);
518 }
519 
520 void StmtProfiler::VisitOMPFlushDirective(const OMPFlushDirective *S) {
521   VisitOMPExecutableDirective(S);
522 }
523 
524 void StmtProfiler::VisitOMPOrderedDirective(const OMPOrderedDirective *S) {
525   VisitOMPExecutableDirective(S);
526 }
527 
528 void StmtProfiler::VisitOMPAtomicDirective(const OMPAtomicDirective *S) {
529   VisitOMPExecutableDirective(S);
530 }
531 
532 void StmtProfiler::VisitOMPTargetDirective(const OMPTargetDirective *S) {
533   VisitOMPExecutableDirective(S);
534 }
535 
536 void StmtProfiler::VisitOMPTargetDataDirective(const OMPTargetDataDirective *S) {
537   VisitOMPExecutableDirective(S);
538 }
539 
540 void StmtProfiler::VisitOMPTeamsDirective(const OMPTeamsDirective *S) {
541   VisitOMPExecutableDirective(S);
542 }
543 
544 void StmtProfiler::VisitOMPCancellationPointDirective(
545     const OMPCancellationPointDirective *S) {
546   VisitOMPExecutableDirective(S);
547 }
548 
549 void StmtProfiler::VisitOMPCancelDirective(const OMPCancelDirective *S) {
550   VisitOMPExecutableDirective(S);
551 }
552 
553 void StmtProfiler::VisitExpr(const Expr *S) {
554   VisitStmt(S);
555 }
556 
557 void StmtProfiler::VisitDeclRefExpr(const DeclRefExpr *S) {
558   VisitExpr(S);
559   if (!Canonical)
560     VisitNestedNameSpecifier(S->getQualifier());
561   VisitDecl(S->getDecl());
562   if (!Canonical)
563     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
564 }
565 
566 void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
567   VisitExpr(S);
568   ID.AddInteger(S->getIdentType());
569 }
570 
571 void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
572   VisitExpr(S);
573   S->getValue().Profile(ID);
574   ID.AddInteger(S->getType()->castAs<BuiltinType>()->getKind());
575 }
576 
577 void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) {
578   VisitExpr(S);
579   ID.AddInteger(S->getKind());
580   ID.AddInteger(S->getValue());
581 }
582 
583 void StmtProfiler::VisitFloatingLiteral(const FloatingLiteral *S) {
584   VisitExpr(S);
585   S->getValue().Profile(ID);
586   ID.AddBoolean(S->isExact());
587   ID.AddInteger(S->getType()->castAs<BuiltinType>()->getKind());
588 }
589 
590 void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) {
591   VisitExpr(S);
592 }
593 
594 void StmtProfiler::VisitStringLiteral(const StringLiteral *S) {
595   VisitExpr(S);
596   ID.AddString(S->getBytes());
597   ID.AddInteger(S->getKind());
598 }
599 
600 void StmtProfiler::VisitParenExpr(const ParenExpr *S) {
601   VisitExpr(S);
602 }
603 
604 void StmtProfiler::VisitParenListExpr(const ParenListExpr *S) {
605   VisitExpr(S);
606 }
607 
608 void StmtProfiler::VisitUnaryOperator(const UnaryOperator *S) {
609   VisitExpr(S);
610   ID.AddInteger(S->getOpcode());
611 }
612 
613 void StmtProfiler::VisitOffsetOfExpr(const OffsetOfExpr *S) {
614   VisitType(S->getTypeSourceInfo()->getType());
615   unsigned n = S->getNumComponents();
616   for (unsigned i = 0; i < n; ++i) {
617     const OffsetOfExpr::OffsetOfNode& ON = S->getComponent(i);
618     ID.AddInteger(ON.getKind());
619     switch (ON.getKind()) {
620     case OffsetOfExpr::OffsetOfNode::Array:
621       // Expressions handled below.
622       break;
623 
624     case OffsetOfExpr::OffsetOfNode::Field:
625       VisitDecl(ON.getField());
626       break;
627 
628     case OffsetOfExpr::OffsetOfNode::Identifier:
629       ID.AddPointer(ON.getFieldName());
630       break;
631 
632     case OffsetOfExpr::OffsetOfNode::Base:
633       // These nodes are implicit, and therefore don't need profiling.
634       break;
635     }
636   }
637 
638   VisitExpr(S);
639 }
640 
641 void
642 StmtProfiler::VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *S) {
643   VisitExpr(S);
644   ID.AddInteger(S->getKind());
645   if (S->isArgumentType())
646     VisitType(S->getArgumentType());
647 }
648 
649 void StmtProfiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *S) {
650   VisitExpr(S);
651 }
652 
653 void StmtProfiler::VisitCallExpr(const CallExpr *S) {
654   VisitExpr(S);
655 }
656 
657 void StmtProfiler::VisitMemberExpr(const MemberExpr *S) {
658   VisitExpr(S);
659   VisitDecl(S->getMemberDecl());
660   if (!Canonical)
661     VisitNestedNameSpecifier(S->getQualifier());
662   ID.AddBoolean(S->isArrow());
663 }
664 
665 void StmtProfiler::VisitCompoundLiteralExpr(const CompoundLiteralExpr *S) {
666   VisitExpr(S);
667   ID.AddBoolean(S->isFileScope());
668 }
669 
670 void StmtProfiler::VisitCastExpr(const CastExpr *S) {
671   VisitExpr(S);
672 }
673 
674 void StmtProfiler::VisitImplicitCastExpr(const ImplicitCastExpr *S) {
675   VisitCastExpr(S);
676   ID.AddInteger(S->getValueKind());
677 }
678 
679 void StmtProfiler::VisitExplicitCastExpr(const ExplicitCastExpr *S) {
680   VisitCastExpr(S);
681   VisitType(S->getTypeAsWritten());
682 }
683 
684 void StmtProfiler::VisitCStyleCastExpr(const CStyleCastExpr *S) {
685   VisitExplicitCastExpr(S);
686 }
687 
688 void StmtProfiler::VisitBinaryOperator(const BinaryOperator *S) {
689   VisitExpr(S);
690   ID.AddInteger(S->getOpcode());
691 }
692 
693 void
694 StmtProfiler::VisitCompoundAssignOperator(const CompoundAssignOperator *S) {
695   VisitBinaryOperator(S);
696 }
697 
698 void StmtProfiler::VisitConditionalOperator(const ConditionalOperator *S) {
699   VisitExpr(S);
700 }
701 
702 void StmtProfiler::VisitBinaryConditionalOperator(
703     const BinaryConditionalOperator *S) {
704   VisitExpr(S);
705 }
706 
707 void StmtProfiler::VisitAddrLabelExpr(const AddrLabelExpr *S) {
708   VisitExpr(S);
709   VisitDecl(S->getLabel());
710 }
711 
712 void StmtProfiler::VisitStmtExpr(const StmtExpr *S) {
713   VisitExpr(S);
714 }
715 
716 void StmtProfiler::VisitShuffleVectorExpr(const ShuffleVectorExpr *S) {
717   VisitExpr(S);
718 }
719 
720 void StmtProfiler::VisitConvertVectorExpr(const ConvertVectorExpr *S) {
721   VisitExpr(S);
722 }
723 
724 void StmtProfiler::VisitChooseExpr(const ChooseExpr *S) {
725   VisitExpr(S);
726 }
727 
728 void StmtProfiler::VisitGNUNullExpr(const GNUNullExpr *S) {
729   VisitExpr(S);
730 }
731 
732 void StmtProfiler::VisitVAArgExpr(const VAArgExpr *S) {
733   VisitExpr(S);
734 }
735 
736 void StmtProfiler::VisitInitListExpr(const InitListExpr *S) {
737   if (S->getSyntacticForm()) {
738     VisitInitListExpr(S->getSyntacticForm());
739     return;
740   }
741 
742   VisitExpr(S);
743 }
744 
745 void StmtProfiler::VisitDesignatedInitExpr(const DesignatedInitExpr *S) {
746   VisitExpr(S);
747   ID.AddBoolean(S->usesGNUSyntax());
748   for (DesignatedInitExpr::const_designators_iterator D =
749          S->designators_begin(), DEnd = S->designators_end();
750        D != DEnd; ++D) {
751     if (D->isFieldDesignator()) {
752       ID.AddInteger(0);
753       VisitName(D->getFieldName());
754       continue;
755     }
756 
757     if (D->isArrayDesignator()) {
758       ID.AddInteger(1);
759     } else {
760       assert(D->isArrayRangeDesignator());
761       ID.AddInteger(2);
762     }
763     ID.AddInteger(D->getFirstExprIndex());
764   }
765 }
766 
767 // Seems that if VisitInitListExpr() only works on the syntactic form of an
768 // InitListExpr, then a DesignatedInitUpdateExpr is not encountered.
769 void StmtProfiler::VisitDesignatedInitUpdateExpr(
770     const DesignatedInitUpdateExpr *S) {
771   llvm_unreachable("Unexpected DesignatedInitUpdateExpr in syntactic form of "
772                    "initializer");
773 }
774 
775 void StmtProfiler::VisitNoInitExpr(const NoInitExpr *S) {
776   llvm_unreachable("Unexpected NoInitExpr in syntactic form of initializer");
777 }
778 
779 void StmtProfiler::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *S) {
780   VisitExpr(S);
781 }
782 
783 void StmtProfiler::VisitExtVectorElementExpr(const ExtVectorElementExpr *S) {
784   VisitExpr(S);
785   VisitName(&S->getAccessor());
786 }
787 
788 void StmtProfiler::VisitBlockExpr(const BlockExpr *S) {
789   VisitExpr(S);
790   VisitDecl(S->getBlockDecl());
791 }
792 
793 void StmtProfiler::VisitGenericSelectionExpr(const GenericSelectionExpr *S) {
794   VisitExpr(S);
795   for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
796     QualType T = S->getAssocType(i);
797     if (T.isNull())
798       ID.AddPointer(nullptr);
799     else
800       VisitType(T);
801     VisitExpr(S->getAssocExpr(i));
802   }
803 }
804 
805 void StmtProfiler::VisitPseudoObjectExpr(const PseudoObjectExpr *S) {
806   VisitExpr(S);
807   for (PseudoObjectExpr::const_semantics_iterator
808          i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i)
809     // Normally, we would not profile the source expressions of OVEs.
810     if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(*i))
811       Visit(OVE->getSourceExpr());
812 }
813 
814 void StmtProfiler::VisitAtomicExpr(const AtomicExpr *S) {
815   VisitExpr(S);
816   ID.AddInteger(S->getOp());
817 }
818 
819 static Stmt::StmtClass DecodeOperatorCall(const CXXOperatorCallExpr *S,
820                                           UnaryOperatorKind &UnaryOp,
821                                           BinaryOperatorKind &BinaryOp) {
822   switch (S->getOperator()) {
823   case OO_None:
824   case OO_New:
825   case OO_Delete:
826   case OO_Array_New:
827   case OO_Array_Delete:
828   case OO_Arrow:
829   case OO_Call:
830   case OO_Conditional:
831   case NUM_OVERLOADED_OPERATORS:
832     llvm_unreachable("Invalid operator call kind");
833 
834   case OO_Plus:
835     if (S->getNumArgs() == 1) {
836       UnaryOp = UO_Plus;
837       return Stmt::UnaryOperatorClass;
838     }
839 
840     BinaryOp = BO_Add;
841     return Stmt::BinaryOperatorClass;
842 
843   case OO_Minus:
844     if (S->getNumArgs() == 1) {
845       UnaryOp = UO_Minus;
846       return Stmt::UnaryOperatorClass;
847     }
848 
849     BinaryOp = BO_Sub;
850     return Stmt::BinaryOperatorClass;
851 
852   case OO_Star:
853     if (S->getNumArgs() == 1) {
854       UnaryOp = UO_Deref;
855       return Stmt::UnaryOperatorClass;
856     }
857 
858     BinaryOp = BO_Mul;
859     return Stmt::BinaryOperatorClass;
860 
861   case OO_Slash:
862     BinaryOp = BO_Div;
863     return Stmt::BinaryOperatorClass;
864 
865   case OO_Percent:
866     BinaryOp = BO_Rem;
867     return Stmt::BinaryOperatorClass;
868 
869   case OO_Caret:
870     BinaryOp = BO_Xor;
871     return Stmt::BinaryOperatorClass;
872 
873   case OO_Amp:
874     if (S->getNumArgs() == 1) {
875       UnaryOp = UO_AddrOf;
876       return Stmt::UnaryOperatorClass;
877     }
878 
879     BinaryOp = BO_And;
880     return Stmt::BinaryOperatorClass;
881 
882   case OO_Pipe:
883     BinaryOp = BO_Or;
884     return Stmt::BinaryOperatorClass;
885 
886   case OO_Tilde:
887     UnaryOp = UO_Not;
888     return Stmt::UnaryOperatorClass;
889 
890   case OO_Exclaim:
891     UnaryOp = UO_LNot;
892     return Stmt::UnaryOperatorClass;
893 
894   case OO_Equal:
895     BinaryOp = BO_Assign;
896     return Stmt::BinaryOperatorClass;
897 
898   case OO_Less:
899     BinaryOp = BO_LT;
900     return Stmt::BinaryOperatorClass;
901 
902   case OO_Greater:
903     BinaryOp = BO_GT;
904     return Stmt::BinaryOperatorClass;
905 
906   case OO_PlusEqual:
907     BinaryOp = BO_AddAssign;
908     return Stmt::CompoundAssignOperatorClass;
909 
910   case OO_MinusEqual:
911     BinaryOp = BO_SubAssign;
912     return Stmt::CompoundAssignOperatorClass;
913 
914   case OO_StarEqual:
915     BinaryOp = BO_MulAssign;
916     return Stmt::CompoundAssignOperatorClass;
917 
918   case OO_SlashEqual:
919     BinaryOp = BO_DivAssign;
920     return Stmt::CompoundAssignOperatorClass;
921 
922   case OO_PercentEqual:
923     BinaryOp = BO_RemAssign;
924     return Stmt::CompoundAssignOperatorClass;
925 
926   case OO_CaretEqual:
927     BinaryOp = BO_XorAssign;
928     return Stmt::CompoundAssignOperatorClass;
929 
930   case OO_AmpEqual:
931     BinaryOp = BO_AndAssign;
932     return Stmt::CompoundAssignOperatorClass;
933 
934   case OO_PipeEqual:
935     BinaryOp = BO_OrAssign;
936     return Stmt::CompoundAssignOperatorClass;
937 
938   case OO_LessLess:
939     BinaryOp = BO_Shl;
940     return Stmt::BinaryOperatorClass;
941 
942   case OO_GreaterGreater:
943     BinaryOp = BO_Shr;
944     return Stmt::BinaryOperatorClass;
945 
946   case OO_LessLessEqual:
947     BinaryOp = BO_ShlAssign;
948     return Stmt::CompoundAssignOperatorClass;
949 
950   case OO_GreaterGreaterEqual:
951     BinaryOp = BO_ShrAssign;
952     return Stmt::CompoundAssignOperatorClass;
953 
954   case OO_EqualEqual:
955     BinaryOp = BO_EQ;
956     return Stmt::BinaryOperatorClass;
957 
958   case OO_ExclaimEqual:
959     BinaryOp = BO_NE;
960     return Stmt::BinaryOperatorClass;
961 
962   case OO_LessEqual:
963     BinaryOp = BO_LE;
964     return Stmt::BinaryOperatorClass;
965 
966   case OO_GreaterEqual:
967     BinaryOp = BO_GE;
968     return Stmt::BinaryOperatorClass;
969 
970   case OO_AmpAmp:
971     BinaryOp = BO_LAnd;
972     return Stmt::BinaryOperatorClass;
973 
974   case OO_PipePipe:
975     BinaryOp = BO_LOr;
976     return Stmt::BinaryOperatorClass;
977 
978   case OO_PlusPlus:
979     UnaryOp = S->getNumArgs() == 1? UO_PreInc
980                                   : UO_PostInc;
981     return Stmt::UnaryOperatorClass;
982 
983   case OO_MinusMinus:
984     UnaryOp = S->getNumArgs() == 1? UO_PreDec
985                                   : UO_PostDec;
986     return Stmt::UnaryOperatorClass;
987 
988   case OO_Comma:
989     BinaryOp = BO_Comma;
990     return Stmt::BinaryOperatorClass;
991 
992 
993   case OO_ArrowStar:
994     BinaryOp = BO_PtrMemI;
995     return Stmt::BinaryOperatorClass;
996 
997   case OO_Subscript:
998     return Stmt::ArraySubscriptExprClass;
999   }
1000 
1001   llvm_unreachable("Invalid overloaded operator expression");
1002 }
1003 
1004 
1005 void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) {
1006   if (S->isTypeDependent()) {
1007     // Type-dependent operator calls are profiled like their underlying
1008     // syntactic operator.
1009     UnaryOperatorKind UnaryOp = UO_Extension;
1010     BinaryOperatorKind BinaryOp = BO_Comma;
1011     Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp);
1012 
1013     ID.AddInteger(SC);
1014     for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
1015       Visit(S->getArg(I));
1016     if (SC == Stmt::UnaryOperatorClass)
1017       ID.AddInteger(UnaryOp);
1018     else if (SC == Stmt::BinaryOperatorClass ||
1019              SC == Stmt::CompoundAssignOperatorClass)
1020       ID.AddInteger(BinaryOp);
1021     else
1022       assert(SC == Stmt::ArraySubscriptExprClass);
1023 
1024     return;
1025   }
1026 
1027   VisitCallExpr(S);
1028   ID.AddInteger(S->getOperator());
1029 }
1030 
1031 void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) {
1032   VisitCallExpr(S);
1033 }
1034 
1035 void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) {
1036   VisitCallExpr(S);
1037 }
1038 
1039 void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) {
1040   VisitExpr(S);
1041 }
1042 
1043 void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) {
1044   VisitExplicitCastExpr(S);
1045 }
1046 
1047 void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
1048   VisitCXXNamedCastExpr(S);
1049 }
1050 
1051 void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) {
1052   VisitCXXNamedCastExpr(S);
1053 }
1054 
1055 void
1056 StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) {
1057   VisitCXXNamedCastExpr(S);
1058 }
1059 
1060 void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) {
1061   VisitCXXNamedCastExpr(S);
1062 }
1063 
1064 void StmtProfiler::VisitUserDefinedLiteral(const UserDefinedLiteral *S) {
1065   VisitCallExpr(S);
1066 }
1067 
1068 void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) {
1069   VisitExpr(S);
1070   ID.AddBoolean(S->getValue());
1071 }
1072 
1073 void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
1074   VisitExpr(S);
1075 }
1076 
1077 void StmtProfiler::VisitCXXStdInitializerListExpr(
1078     const CXXStdInitializerListExpr *S) {
1079   VisitExpr(S);
1080 }
1081 
1082 void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) {
1083   VisitExpr(S);
1084   if (S->isTypeOperand())
1085     VisitType(S->getTypeOperandSourceInfo()->getType());
1086 }
1087 
1088 void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) {
1089   VisitExpr(S);
1090   if (S->isTypeOperand())
1091     VisitType(S->getTypeOperandSourceInfo()->getType());
1092 }
1093 
1094 void StmtProfiler::VisitMSPropertyRefExpr(const MSPropertyRefExpr *S) {
1095   VisitExpr(S);
1096   VisitDecl(S->getPropertyDecl());
1097 }
1098 
1099 void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) {
1100   VisitExpr(S);
1101   ID.AddBoolean(S->isImplicit());
1102 }
1103 
1104 void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) {
1105   VisitExpr(S);
1106 }
1107 
1108 void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) {
1109   VisitExpr(S);
1110   VisitDecl(S->getParam());
1111 }
1112 
1113 void StmtProfiler::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *S) {
1114   VisitExpr(S);
1115   VisitDecl(S->getField());
1116 }
1117 
1118 void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) {
1119   VisitExpr(S);
1120   VisitDecl(
1121          const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
1122 }
1123 
1124 void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) {
1125   VisitExpr(S);
1126   VisitDecl(S->getConstructor());
1127   ID.AddBoolean(S->isElidable());
1128 }
1129 
1130 void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
1131   VisitExplicitCastExpr(S);
1132 }
1133 
1134 void
1135 StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) {
1136   VisitCXXConstructExpr(S);
1137 }
1138 
1139 void
1140 StmtProfiler::VisitLambdaExpr(const LambdaExpr *S) {
1141   VisitExpr(S);
1142   for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
1143                                  CEnd = S->explicit_capture_end();
1144        C != CEnd; ++C) {
1145     ID.AddInteger(C->getCaptureKind());
1146     switch (C->getCaptureKind()) {
1147     case LCK_This:
1148       break;
1149     case LCK_ByRef:
1150     case LCK_ByCopy:
1151       VisitDecl(C->getCapturedVar());
1152       ID.AddBoolean(C->isPackExpansion());
1153       break;
1154     case LCK_VLAType:
1155       llvm_unreachable("VLA type in explicit captures.");
1156     }
1157   }
1158   // Note: If we actually needed to be able to match lambda
1159   // expressions, we would have to consider parameters and return type
1160   // here, among other things.
1161   VisitStmt(S->getBody());
1162 }
1163 
1164 void
1165 StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) {
1166   VisitExpr(S);
1167 }
1168 
1169 void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
1170   VisitExpr(S);
1171   ID.AddBoolean(S->isGlobalDelete());
1172   ID.AddBoolean(S->isArrayForm());
1173   VisitDecl(S->getOperatorDelete());
1174 }
1175 
1176 
1177 void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) {
1178   VisitExpr(S);
1179   VisitType(S->getAllocatedType());
1180   VisitDecl(S->getOperatorNew());
1181   VisitDecl(S->getOperatorDelete());
1182   ID.AddBoolean(S->isArray());
1183   ID.AddInteger(S->getNumPlacementArgs());
1184   ID.AddBoolean(S->isGlobalNew());
1185   ID.AddBoolean(S->isParenTypeId());
1186   ID.AddInteger(S->getInitializationStyle());
1187 }
1188 
1189 void
1190 StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
1191   VisitExpr(S);
1192   ID.AddBoolean(S->isArrow());
1193   VisitNestedNameSpecifier(S->getQualifier());
1194   ID.AddBoolean(S->getScopeTypeInfo() != nullptr);
1195   if (S->getScopeTypeInfo())
1196     VisitType(S->getScopeTypeInfo()->getType());
1197   ID.AddBoolean(S->getDestroyedTypeInfo() != nullptr);
1198   if (S->getDestroyedTypeInfo())
1199     VisitType(S->getDestroyedType());
1200   else
1201     ID.AddPointer(S->getDestroyedTypeIdentifier());
1202 }
1203 
1204 void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) {
1205   VisitExpr(S);
1206   VisitNestedNameSpecifier(S->getQualifier());
1207   VisitName(S->getName());
1208   ID.AddBoolean(S->hasExplicitTemplateArgs());
1209   if (S->hasExplicitTemplateArgs())
1210     VisitTemplateArguments(S->getExplicitTemplateArgs().getTemplateArgs(),
1211                            S->getExplicitTemplateArgs().NumTemplateArgs);
1212 }
1213 
1214 void
1215 StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) {
1216   VisitOverloadExpr(S);
1217 }
1218 
1219 void StmtProfiler::VisitTypeTraitExpr(const TypeTraitExpr *S) {
1220   VisitExpr(S);
1221   ID.AddInteger(S->getTrait());
1222   ID.AddInteger(S->getNumArgs());
1223   for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
1224     VisitType(S->getArg(I)->getType());
1225 }
1226 
1227 void StmtProfiler::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *S) {
1228   VisitExpr(S);
1229   ID.AddInteger(S->getTrait());
1230   VisitType(S->getQueriedType());
1231 }
1232 
1233 void StmtProfiler::VisitExpressionTraitExpr(const ExpressionTraitExpr *S) {
1234   VisitExpr(S);
1235   ID.AddInteger(S->getTrait());
1236   VisitExpr(S->getQueriedExpression());
1237 }
1238 
1239 void StmtProfiler::VisitDependentScopeDeclRefExpr(
1240     const DependentScopeDeclRefExpr *S) {
1241   VisitExpr(S);
1242   VisitName(S->getDeclName());
1243   VisitNestedNameSpecifier(S->getQualifier());
1244   ID.AddBoolean(S->hasExplicitTemplateArgs());
1245   if (S->hasExplicitTemplateArgs())
1246     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
1247 }
1248 
1249 void StmtProfiler::VisitExprWithCleanups(const ExprWithCleanups *S) {
1250   VisitExpr(S);
1251 }
1252 
1253 void StmtProfiler::VisitCXXUnresolvedConstructExpr(
1254     const CXXUnresolvedConstructExpr *S) {
1255   VisitExpr(S);
1256   VisitType(S->getTypeAsWritten());
1257 }
1258 
1259 void StmtProfiler::VisitCXXDependentScopeMemberExpr(
1260     const CXXDependentScopeMemberExpr *S) {
1261   ID.AddBoolean(S->isImplicitAccess());
1262   if (!S->isImplicitAccess()) {
1263     VisitExpr(S);
1264     ID.AddBoolean(S->isArrow());
1265   }
1266   VisitNestedNameSpecifier(S->getQualifier());
1267   VisitName(S->getMember());
1268   ID.AddBoolean(S->hasExplicitTemplateArgs());
1269   if (S->hasExplicitTemplateArgs())
1270     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
1271 }
1272 
1273 void StmtProfiler::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *S) {
1274   ID.AddBoolean(S->isImplicitAccess());
1275   if (!S->isImplicitAccess()) {
1276     VisitExpr(S);
1277     ID.AddBoolean(S->isArrow());
1278   }
1279   VisitNestedNameSpecifier(S->getQualifier());
1280   VisitName(S->getMemberName());
1281   ID.AddBoolean(S->hasExplicitTemplateArgs());
1282   if (S->hasExplicitTemplateArgs())
1283     VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
1284 }
1285 
1286 void StmtProfiler::VisitCXXNoexceptExpr(const CXXNoexceptExpr *S) {
1287   VisitExpr(S);
1288 }
1289 
1290 void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) {
1291   VisitExpr(S);
1292 }
1293 
1294 void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
1295   VisitExpr(S);
1296   VisitDecl(S->getPack());
1297 }
1298 
1299 void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr(
1300     const SubstNonTypeTemplateParmPackExpr *S) {
1301   VisitExpr(S);
1302   VisitDecl(S->getParameterPack());
1303   VisitTemplateArgument(S->getArgumentPack());
1304 }
1305 
1306 void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
1307     const SubstNonTypeTemplateParmExpr *E) {
1308   // Profile exactly as the replacement expression.
1309   Visit(E->getReplacement());
1310 }
1311 
1312 void StmtProfiler::VisitFunctionParmPackExpr(const FunctionParmPackExpr *S) {
1313   VisitExpr(S);
1314   VisitDecl(S->getParameterPack());
1315   ID.AddInteger(S->getNumExpansions());
1316   for (FunctionParmPackExpr::iterator I = S->begin(), E = S->end(); I != E; ++I)
1317     VisitDecl(*I);
1318 }
1319 
1320 void StmtProfiler::VisitMaterializeTemporaryExpr(
1321                                            const MaterializeTemporaryExpr *S) {
1322   VisitExpr(S);
1323 }
1324 
1325 void StmtProfiler::VisitCXXFoldExpr(const CXXFoldExpr *S) {
1326   VisitExpr(S);
1327   ID.AddInteger(S->getOperator());
1328 }
1329 
1330 void StmtProfiler::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
1331   VisitExpr(E);
1332 }
1333 
1334 void StmtProfiler::VisitTypoExpr(const TypoExpr *E) {
1335   VisitExpr(E);
1336 }
1337 
1338 void StmtProfiler::VisitObjCStringLiteral(const ObjCStringLiteral *S) {
1339   VisitExpr(S);
1340 }
1341 
1342 void StmtProfiler::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
1343   VisitExpr(E);
1344 }
1345 
1346 void StmtProfiler::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
1347   VisitExpr(E);
1348 }
1349 
1350 void StmtProfiler::VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
1351   VisitExpr(E);
1352 }
1353 
1354 void StmtProfiler::VisitObjCEncodeExpr(const ObjCEncodeExpr *S) {
1355   VisitExpr(S);
1356   VisitType(S->getEncodedType());
1357 }
1358 
1359 void StmtProfiler::VisitObjCSelectorExpr(const ObjCSelectorExpr *S) {
1360   VisitExpr(S);
1361   VisitName(S->getSelector());
1362 }
1363 
1364 void StmtProfiler::VisitObjCProtocolExpr(const ObjCProtocolExpr *S) {
1365   VisitExpr(S);
1366   VisitDecl(S->getProtocol());
1367 }
1368 
1369 void StmtProfiler::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *S) {
1370   VisitExpr(S);
1371   VisitDecl(S->getDecl());
1372   ID.AddBoolean(S->isArrow());
1373   ID.AddBoolean(S->isFreeIvar());
1374 }
1375 
1376 void StmtProfiler::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *S) {
1377   VisitExpr(S);
1378   if (S->isImplicitProperty()) {
1379     VisitDecl(S->getImplicitPropertyGetter());
1380     VisitDecl(S->getImplicitPropertySetter());
1381   } else {
1382     VisitDecl(S->getExplicitProperty());
1383   }
1384   if (S->isSuperReceiver()) {
1385     ID.AddBoolean(S->isSuperReceiver());
1386     VisitType(S->getSuperReceiverType());
1387   }
1388 }
1389 
1390 void StmtProfiler::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *S) {
1391   VisitExpr(S);
1392   VisitDecl(S->getAtIndexMethodDecl());
1393   VisitDecl(S->setAtIndexMethodDecl());
1394 }
1395 
1396 void StmtProfiler::VisitObjCMessageExpr(const ObjCMessageExpr *S) {
1397   VisitExpr(S);
1398   VisitName(S->getSelector());
1399   VisitDecl(S->getMethodDecl());
1400 }
1401 
1402 void StmtProfiler::VisitObjCIsaExpr(const ObjCIsaExpr *S) {
1403   VisitExpr(S);
1404   ID.AddBoolean(S->isArrow());
1405 }
1406 
1407 void StmtProfiler::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *S) {
1408   VisitExpr(S);
1409   ID.AddBoolean(S->getValue());
1410 }
1411 
1412 void StmtProfiler::VisitObjCIndirectCopyRestoreExpr(
1413     const ObjCIndirectCopyRestoreExpr *S) {
1414   VisitExpr(S);
1415   ID.AddBoolean(S->shouldCopy());
1416 }
1417 
1418 void StmtProfiler::VisitObjCBridgedCastExpr(const ObjCBridgedCastExpr *S) {
1419   VisitExplicitCastExpr(S);
1420   ID.AddBoolean(S->getBridgeKind());
1421 }
1422 
1423 void StmtProfiler::VisitDecl(const Decl *D) {
1424   ID.AddInteger(D? D->getKind() : 0);
1425 
1426   if (Canonical && D) {
1427     if (const NonTypeTemplateParmDecl *NTTP =
1428           dyn_cast<NonTypeTemplateParmDecl>(D)) {
1429       ID.AddInteger(NTTP->getDepth());
1430       ID.AddInteger(NTTP->getIndex());
1431       ID.AddBoolean(NTTP->isParameterPack());
1432       VisitType(NTTP->getType());
1433       return;
1434     }
1435 
1436     if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
1437       // The Itanium C++ ABI uses the type, scope depth, and scope
1438       // index of a parameter when mangling expressions that involve
1439       // function parameters, so we will use the parameter's type for
1440       // establishing function parameter identity. That way, our
1441       // definition of "equivalent" (per C++ [temp.over.link]) is at
1442       // least as strong as the definition of "equivalent" used for
1443       // name mangling.
1444       VisitType(Parm->getType());
1445       ID.AddInteger(Parm->getFunctionScopeDepth());
1446       ID.AddInteger(Parm->getFunctionScopeIndex());
1447       return;
1448     }
1449 
1450     if (const TemplateTypeParmDecl *TTP =
1451           dyn_cast<TemplateTypeParmDecl>(D)) {
1452       ID.AddInteger(TTP->getDepth());
1453       ID.AddInteger(TTP->getIndex());
1454       ID.AddBoolean(TTP->isParameterPack());
1455       return;
1456     }
1457 
1458     if (const TemplateTemplateParmDecl *TTP =
1459           dyn_cast<TemplateTemplateParmDecl>(D)) {
1460       ID.AddInteger(TTP->getDepth());
1461       ID.AddInteger(TTP->getIndex());
1462       ID.AddBoolean(TTP->isParameterPack());
1463       return;
1464     }
1465   }
1466 
1467   ID.AddPointer(D? D->getCanonicalDecl() : nullptr);
1468 }
1469 
1470 void StmtProfiler::VisitType(QualType T) {
1471   if (Canonical)
1472     T = Context.getCanonicalType(T);
1473 
1474   ID.AddPointer(T.getAsOpaquePtr());
1475 }
1476 
1477 void StmtProfiler::VisitName(DeclarationName Name) {
1478   ID.AddPointer(Name.getAsOpaquePtr());
1479 }
1480 
1481 void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) {
1482   if (Canonical)
1483     NNS = Context.getCanonicalNestedNameSpecifier(NNS);
1484   ID.AddPointer(NNS);
1485 }
1486 
1487 void StmtProfiler::VisitTemplateName(TemplateName Name) {
1488   if (Canonical)
1489     Name = Context.getCanonicalTemplateName(Name);
1490 
1491   Name.Profile(ID);
1492 }
1493 
1494 void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args,
1495                                           unsigned NumArgs) {
1496   ID.AddInteger(NumArgs);
1497   for (unsigned I = 0; I != NumArgs; ++I)
1498     VisitTemplateArgument(Args[I].getArgument());
1499 }
1500 
1501 void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) {
1502   // Mostly repetitive with TemplateArgument::Profile!
1503   ID.AddInteger(Arg.getKind());
1504   switch (Arg.getKind()) {
1505   case TemplateArgument::Null:
1506     break;
1507 
1508   case TemplateArgument::Type:
1509     VisitType(Arg.getAsType());
1510     break;
1511 
1512   case TemplateArgument::Template:
1513   case TemplateArgument::TemplateExpansion:
1514     VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
1515     break;
1516 
1517   case TemplateArgument::Declaration:
1518     VisitDecl(Arg.getAsDecl());
1519     break;
1520 
1521   case TemplateArgument::NullPtr:
1522     VisitType(Arg.getNullPtrType());
1523     break;
1524 
1525   case TemplateArgument::Integral:
1526     Arg.getAsIntegral().Profile(ID);
1527     VisitType(Arg.getIntegralType());
1528     break;
1529 
1530   case TemplateArgument::Expression:
1531     Visit(Arg.getAsExpr());
1532     break;
1533 
1534   case TemplateArgument::Pack:
1535     for (const auto &P : Arg.pack_elements())
1536       VisitTemplateArgument(P);
1537     break;
1538   }
1539 }
1540 
1541 void Stmt::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1542                    bool Canonical) const {
1543   StmtProfiler Profiler(ID, Context, Canonical);
1544   Profiler.Visit(this);
1545 }
1546