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