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