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