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