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