1//===---- StmtProfile.cpp - Profile implementation for Stmt ASTs ----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt::Profile method, which builds a unique bit
11// representation that identifies a statement/expression.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/AST/ExprObjC.h"
21#include "clang/AST/StmtVisitor.h"
22#include "llvm/ADT/FoldingSet.h"
23using namespace clang;
24
25namespace {
26  class StmtProfiler : public ConstStmtVisitor<StmtProfiler> {
27    llvm::FoldingSetNodeID &ID;
28    const ASTContext &Context;
29    bool Canonical;
30
31  public:
32    StmtProfiler(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
33                 bool Canonical)
34      : ID(ID), Context(Context), Canonical(Canonical) { }
35
36    void VisitStmt(const Stmt *S);
37
38#define STMT(Node, Base) void Visit##Node(const Node *S);
39#include "clang/AST/StmtNodes.inc"
40
41    /// \brief Visit a declaration that is referenced within an expression
42    /// or statement.
43    void VisitDecl(const Decl *D);
44
45    /// \brief Visit a type that is referenced within an expression or
46    /// statement.
47    void VisitType(QualType T);
48
49    /// \brief Visit a name that occurs within an expression or statement.
50    void VisitName(DeclarationName Name);
51
52    /// \brief Visit a nested-name-specifier that occurs within an expression
53    /// or statement.
54    void VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
55
56    /// \brief Visit a template name that occurs within an expression or
57    /// statement.
58    void VisitTemplateName(TemplateName Name);
59
60    /// \brief Visit template arguments that occur within an expression or
61    /// statement.
62    void VisitTemplateArguments(const TemplateArgumentLoc *Args,
63                                unsigned NumArgs);
64
65    /// \brief Visit a single template argument.
66    void VisitTemplateArgument(const TemplateArgument &Arg);
67  };
68}
69
70void StmtProfiler::VisitStmt(const Stmt *S) {
71  ID.AddInteger(S->getStmtClass());
72  for (Stmt::const_child_range C = S->children(); C; ++C) {
73    if (*C)
74      Visit(*C);
75    else
76      ID.AddInteger(0);
77  }
78}
79
80void StmtProfiler::VisitDeclStmt(const DeclStmt *S) {
81  VisitStmt(S);
82  for (DeclStmt::const_decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
83       D != DEnd; ++D)
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::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
219  VisitStmt(S);
220}
221
222void StmtProfiler::VisitObjCAtCatchStmt(const ObjCAtCatchStmt *S) {
223  VisitStmt(S);
224  ID.AddBoolean(S->hasEllipsis());
225  if (S->getCatchParamDecl())
226    VisitType(S->getCatchParamDecl()->getType());
227}
228
229void StmtProfiler::VisitObjCAtFinallyStmt(const ObjCAtFinallyStmt *S) {
230  VisitStmt(S);
231}
232
233void StmtProfiler::VisitObjCAtTryStmt(const ObjCAtTryStmt *S) {
234  VisitStmt(S);
235}
236
237void
238StmtProfiler::VisitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt *S) {
239  VisitStmt(S);
240}
241
242void StmtProfiler::VisitObjCAtThrowStmt(const ObjCAtThrowStmt *S) {
243  VisitStmt(S);
244}
245
246void
247StmtProfiler::VisitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt *S) {
248  VisitStmt(S);
249}
250
251void StmtProfiler::VisitExpr(const Expr *S) {
252  VisitStmt(S);
253}
254
255void StmtProfiler::VisitDeclRefExpr(const DeclRefExpr *S) {
256  VisitExpr(S);
257  if (!Canonical)
258    VisitNestedNameSpecifier(S->getQualifier());
259  VisitDecl(S->getDecl());
260  if (!Canonical)
261    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
262}
263
264void StmtProfiler::VisitPredefinedExpr(const PredefinedExpr *S) {
265  VisitExpr(S);
266  ID.AddInteger(S->getIdentType());
267}
268
269void StmtProfiler::VisitIntegerLiteral(const IntegerLiteral *S) {
270  VisitExpr(S);
271  S->getValue().Profile(ID);
272}
273
274void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) {
275  VisitExpr(S);
276  ID.AddInteger(S->getKind());
277  ID.AddInteger(S->getValue());
278}
279
280void StmtProfiler::VisitFloatingLiteral(const FloatingLiteral *S) {
281  VisitExpr(S);
282  S->getValue().Profile(ID);
283  ID.AddBoolean(S->isExact());
284}
285
286void StmtProfiler::VisitImaginaryLiteral(const ImaginaryLiteral *S) {
287  VisitExpr(S);
288}
289
290void StmtProfiler::VisitStringLiteral(const StringLiteral *S) {
291  VisitExpr(S);
292  ID.AddString(S->getBytes());
293  ID.AddInteger(S->getKind());
294}
295
296void StmtProfiler::VisitParenExpr(const ParenExpr *S) {
297  VisitExpr(S);
298}
299
300void StmtProfiler::VisitParenListExpr(const ParenListExpr *S) {
301  VisitExpr(S);
302}
303
304void StmtProfiler::VisitUnaryOperator(const UnaryOperator *S) {
305  VisitExpr(S);
306  ID.AddInteger(S->getOpcode());
307}
308
309void StmtProfiler::VisitOffsetOfExpr(const OffsetOfExpr *S) {
310  VisitType(S->getTypeSourceInfo()->getType());
311  unsigned n = S->getNumComponents();
312  for (unsigned i = 0; i < n; ++i) {
313    const OffsetOfExpr::OffsetOfNode& ON = S->getComponent(i);
314    ID.AddInteger(ON.getKind());
315    switch (ON.getKind()) {
316    case OffsetOfExpr::OffsetOfNode::Array:
317      // Expressions handled below.
318      break;
319
320    case OffsetOfExpr::OffsetOfNode::Field:
321      VisitDecl(ON.getField());
322      break;
323
324    case OffsetOfExpr::OffsetOfNode::Identifier:
325      ID.AddPointer(ON.getFieldName());
326      break;
327
328    case OffsetOfExpr::OffsetOfNode::Base:
329      // These nodes are implicit, and therefore don't need profiling.
330      break;
331    }
332  }
333
334  VisitExpr(S);
335}
336
337void
338StmtProfiler::VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *S) {
339  VisitExpr(S);
340  ID.AddInteger(S->getKind());
341  if (S->isArgumentType())
342    VisitType(S->getArgumentType());
343}
344
345void StmtProfiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *S) {
346  VisitExpr(S);
347}
348
349void StmtProfiler::VisitCallExpr(const CallExpr *S) {
350  VisitExpr(S);
351}
352
353void StmtProfiler::VisitMemberExpr(const MemberExpr *S) {
354  VisitExpr(S);
355  VisitDecl(S->getMemberDecl());
356  if (!Canonical)
357    VisitNestedNameSpecifier(S->getQualifier());
358  ID.AddBoolean(S->isArrow());
359}
360
361void StmtProfiler::VisitCompoundLiteralExpr(const CompoundLiteralExpr *S) {
362  VisitExpr(S);
363  ID.AddBoolean(S->isFileScope());
364}
365
366void StmtProfiler::VisitCastExpr(const CastExpr *S) {
367  VisitExpr(S);
368}
369
370void StmtProfiler::VisitImplicitCastExpr(const ImplicitCastExpr *S) {
371  VisitCastExpr(S);
372  ID.AddInteger(S->getValueKind());
373}
374
375void StmtProfiler::VisitExplicitCastExpr(const ExplicitCastExpr *S) {
376  VisitCastExpr(S);
377  VisitType(S->getTypeAsWritten());
378}
379
380void StmtProfiler::VisitCStyleCastExpr(const CStyleCastExpr *S) {
381  VisitExplicitCastExpr(S);
382}
383
384void StmtProfiler::VisitBinaryOperator(const BinaryOperator *S) {
385  VisitExpr(S);
386  ID.AddInteger(S->getOpcode());
387}
388
389void
390StmtProfiler::VisitCompoundAssignOperator(const CompoundAssignOperator *S) {
391  VisitBinaryOperator(S);
392}
393
394void StmtProfiler::VisitConditionalOperator(const ConditionalOperator *S) {
395  VisitExpr(S);
396}
397
398void StmtProfiler::VisitBinaryConditionalOperator(
399    const BinaryConditionalOperator *S) {
400  VisitExpr(S);
401}
402
403void StmtProfiler::VisitAddrLabelExpr(const AddrLabelExpr *S) {
404  VisitExpr(S);
405  VisitDecl(S->getLabel());
406}
407
408void StmtProfiler::VisitStmtExpr(const StmtExpr *S) {
409  VisitExpr(S);
410}
411
412void StmtProfiler::VisitShuffleVectorExpr(const ShuffleVectorExpr *S) {
413  VisitExpr(S);
414}
415
416void StmtProfiler::VisitChooseExpr(const ChooseExpr *S) {
417  VisitExpr(S);
418}
419
420void StmtProfiler::VisitGNUNullExpr(const GNUNullExpr *S) {
421  VisitExpr(S);
422}
423
424void StmtProfiler::VisitVAArgExpr(const VAArgExpr *S) {
425  VisitExpr(S);
426}
427
428void StmtProfiler::VisitInitListExpr(const InitListExpr *S) {
429  if (S->getSyntacticForm()) {
430    VisitInitListExpr(S->getSyntacticForm());
431    return;
432  }
433
434  VisitExpr(S);
435}
436
437void StmtProfiler::VisitDesignatedInitExpr(const DesignatedInitExpr *S) {
438  VisitExpr(S);
439  ID.AddBoolean(S->usesGNUSyntax());
440  for (DesignatedInitExpr::const_designators_iterator D =
441         S->designators_begin(), DEnd = S->designators_end();
442       D != DEnd; ++D) {
443    if (D->isFieldDesignator()) {
444      ID.AddInteger(0);
445      VisitName(D->getFieldName());
446      continue;
447    }
448
449    if (D->isArrayDesignator()) {
450      ID.AddInteger(1);
451    } else {
452      assert(D->isArrayRangeDesignator());
453      ID.AddInteger(2);
454    }
455    ID.AddInteger(D->getFirstExprIndex());
456  }
457}
458
459void StmtProfiler::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *S) {
460  VisitExpr(S);
461}
462
463void StmtProfiler::VisitExtVectorElementExpr(const ExtVectorElementExpr *S) {
464  VisitExpr(S);
465  VisitName(&S->getAccessor());
466}
467
468void StmtProfiler::VisitBlockExpr(const BlockExpr *S) {
469  VisitExpr(S);
470  VisitDecl(S->getBlockDecl());
471}
472
473void StmtProfiler::VisitGenericSelectionExpr(const GenericSelectionExpr *S) {
474  VisitExpr(S);
475  for (unsigned i = 0; i != S->getNumAssocs(); ++i) {
476    QualType T = S->getAssocType(i);
477    if (T.isNull())
478      ID.AddPointer(0);
479    else
480      VisitType(T);
481    VisitExpr(S->getAssocExpr(i));
482  }
483}
484
485void StmtProfiler::VisitPseudoObjectExpr(const PseudoObjectExpr *S) {
486  VisitExpr(S);
487  for (PseudoObjectExpr::const_semantics_iterator
488         i = S->semantics_begin(), e = S->semantics_end(); i != e; ++i)
489    // Normally, we would not profile the source expressions of OVEs.
490    if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(*i))
491      Visit(OVE->getSourceExpr());
492}
493
494void StmtProfiler::VisitAtomicExpr(const AtomicExpr *S) {
495  VisitExpr(S);
496  ID.AddInteger(S->getOp());
497}
498
499static Stmt::StmtClass DecodeOperatorCall(const CXXOperatorCallExpr *S,
500                                          UnaryOperatorKind &UnaryOp,
501                                          BinaryOperatorKind &BinaryOp) {
502  switch (S->getOperator()) {
503  case OO_None:
504  case OO_New:
505  case OO_Delete:
506  case OO_Array_New:
507  case OO_Array_Delete:
508  case OO_Arrow:
509  case OO_Call:
510  case OO_Conditional:
511  case NUM_OVERLOADED_OPERATORS:
512    llvm_unreachable("Invalid operator call kind");
513
514  case OO_Plus:
515    if (S->getNumArgs() == 1) {
516      UnaryOp = UO_Plus;
517      return Stmt::UnaryOperatorClass;
518    }
519
520    BinaryOp = BO_Add;
521    return Stmt::BinaryOperatorClass;
522
523  case OO_Minus:
524    if (S->getNumArgs() == 1) {
525      UnaryOp = UO_Minus;
526      return Stmt::UnaryOperatorClass;
527    }
528
529    BinaryOp = BO_Sub;
530    return Stmt::BinaryOperatorClass;
531
532  case OO_Star:
533    if (S->getNumArgs() == 1) {
534      UnaryOp = UO_Minus;
535      return Stmt::UnaryOperatorClass;
536    }
537
538    BinaryOp = BO_Sub;
539    return Stmt::BinaryOperatorClass;
540
541  case OO_Slash:
542    BinaryOp = BO_Div;
543    return Stmt::BinaryOperatorClass;
544
545  case OO_Percent:
546    BinaryOp = BO_Rem;
547    return Stmt::BinaryOperatorClass;
548
549  case OO_Caret:
550    BinaryOp = BO_Xor;
551    return Stmt::BinaryOperatorClass;
552
553  case OO_Amp:
554    if (S->getNumArgs() == 1) {
555      UnaryOp = UO_AddrOf;
556      return Stmt::UnaryOperatorClass;
557    }
558
559    BinaryOp = BO_And;
560    return Stmt::BinaryOperatorClass;
561
562  case OO_Pipe:
563    BinaryOp = BO_Or;
564    return Stmt::BinaryOperatorClass;
565
566  case OO_Tilde:
567    UnaryOp = UO_Not;
568    return Stmt::UnaryOperatorClass;
569
570  case OO_Exclaim:
571    UnaryOp = UO_LNot;
572    return Stmt::UnaryOperatorClass;
573
574  case OO_Equal:
575    BinaryOp = BO_Assign;
576    return Stmt::BinaryOperatorClass;
577
578  case OO_Less:
579    BinaryOp = BO_LT;
580    return Stmt::BinaryOperatorClass;
581
582  case OO_Greater:
583    BinaryOp = BO_GT;
584    return Stmt::BinaryOperatorClass;
585
586  case OO_PlusEqual:
587    BinaryOp = BO_AddAssign;
588    return Stmt::CompoundAssignOperatorClass;
589
590  case OO_MinusEqual:
591    BinaryOp = BO_SubAssign;
592    return Stmt::CompoundAssignOperatorClass;
593
594  case OO_StarEqual:
595    BinaryOp = BO_MulAssign;
596    return Stmt::CompoundAssignOperatorClass;
597
598  case OO_SlashEqual:
599    BinaryOp = BO_DivAssign;
600    return Stmt::CompoundAssignOperatorClass;
601
602  case OO_PercentEqual:
603    BinaryOp = BO_RemAssign;
604    return Stmt::CompoundAssignOperatorClass;
605
606  case OO_CaretEqual:
607    BinaryOp = BO_XorAssign;
608    return Stmt::CompoundAssignOperatorClass;
609
610  case OO_AmpEqual:
611    BinaryOp = BO_AndAssign;
612    return Stmt::CompoundAssignOperatorClass;
613
614  case OO_PipeEqual:
615    BinaryOp = BO_OrAssign;
616    return Stmt::CompoundAssignOperatorClass;
617
618  case OO_LessLess:
619    BinaryOp = BO_Shl;
620    return Stmt::BinaryOperatorClass;
621
622  case OO_GreaterGreater:
623    BinaryOp = BO_Shr;
624    return Stmt::BinaryOperatorClass;
625
626  case OO_LessLessEqual:
627    BinaryOp = BO_ShlAssign;
628    return Stmt::CompoundAssignOperatorClass;
629
630  case OO_GreaterGreaterEqual:
631    BinaryOp = BO_ShrAssign;
632    return Stmt::CompoundAssignOperatorClass;
633
634  case OO_EqualEqual:
635    BinaryOp = BO_EQ;
636    return Stmt::BinaryOperatorClass;
637
638  case OO_ExclaimEqual:
639    BinaryOp = BO_NE;
640    return Stmt::BinaryOperatorClass;
641
642  case OO_LessEqual:
643    BinaryOp = BO_LE;
644    return Stmt::BinaryOperatorClass;
645
646  case OO_GreaterEqual:
647    BinaryOp = BO_GE;
648    return Stmt::BinaryOperatorClass;
649
650  case OO_AmpAmp:
651    BinaryOp = BO_LAnd;
652    return Stmt::BinaryOperatorClass;
653
654  case OO_PipePipe:
655    BinaryOp = BO_LOr;
656    return Stmt::BinaryOperatorClass;
657
658  case OO_PlusPlus:
659    UnaryOp = S->getNumArgs() == 1? UO_PreInc
660                                  : UO_PostInc;
661    return Stmt::UnaryOperatorClass;
662
663  case OO_MinusMinus:
664    UnaryOp = S->getNumArgs() == 1? UO_PreDec
665                                  : UO_PostDec;
666    return Stmt::UnaryOperatorClass;
667
668  case OO_Comma:
669    BinaryOp = BO_Comma;
670    return Stmt::BinaryOperatorClass;
671
672
673  case OO_ArrowStar:
674    BinaryOp = BO_PtrMemI;
675    return Stmt::BinaryOperatorClass;
676
677  case OO_Subscript:
678    return Stmt::ArraySubscriptExprClass;
679  }
680
681  llvm_unreachable("Invalid overloaded operator expression");
682}
683
684
685void StmtProfiler::VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *S) {
686  if (S->isTypeDependent()) {
687    // Type-dependent operator calls are profiled like their underlying
688    // syntactic operator.
689    UnaryOperatorKind UnaryOp = UO_Extension;
690    BinaryOperatorKind BinaryOp = BO_Comma;
691    Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp);
692
693    ID.AddInteger(SC);
694    for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
695      Visit(S->getArg(I));
696    if (SC == Stmt::UnaryOperatorClass)
697      ID.AddInteger(UnaryOp);
698    else if (SC == Stmt::BinaryOperatorClass ||
699             SC == Stmt::CompoundAssignOperatorClass)
700      ID.AddInteger(BinaryOp);
701    else
702      assert(SC == Stmt::ArraySubscriptExprClass);
703
704    return;
705  }
706
707  VisitCallExpr(S);
708  ID.AddInteger(S->getOperator());
709}
710
711void StmtProfiler::VisitCXXMemberCallExpr(const CXXMemberCallExpr *S) {
712  VisitCallExpr(S);
713}
714
715void StmtProfiler::VisitCUDAKernelCallExpr(const CUDAKernelCallExpr *S) {
716  VisitCallExpr(S);
717}
718
719void StmtProfiler::VisitAsTypeExpr(const AsTypeExpr *S) {
720  VisitExpr(S);
721}
722
723void StmtProfiler::VisitCXXNamedCastExpr(const CXXNamedCastExpr *S) {
724  VisitExplicitCastExpr(S);
725}
726
727void StmtProfiler::VisitCXXStaticCastExpr(const CXXStaticCastExpr *S) {
728  VisitCXXNamedCastExpr(S);
729}
730
731void StmtProfiler::VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *S) {
732  VisitCXXNamedCastExpr(S);
733}
734
735void
736StmtProfiler::VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *S) {
737  VisitCXXNamedCastExpr(S);
738}
739
740void StmtProfiler::VisitCXXConstCastExpr(const CXXConstCastExpr *S) {
741  VisitCXXNamedCastExpr(S);
742}
743
744void StmtProfiler::VisitUserDefinedLiteral(const UserDefinedLiteral *S) {
745  VisitCallExpr(S);
746}
747
748void StmtProfiler::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *S) {
749  VisitExpr(S);
750  ID.AddBoolean(S->getValue());
751}
752
753void StmtProfiler::VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *S) {
754  VisitExpr(S);
755}
756
757void StmtProfiler::VisitCXXTypeidExpr(const CXXTypeidExpr *S) {
758  VisitExpr(S);
759  if (S->isTypeOperand())
760    VisitType(S->getTypeOperand());
761}
762
763void StmtProfiler::VisitCXXUuidofExpr(const CXXUuidofExpr *S) {
764  VisitExpr(S);
765  if (S->isTypeOperand())
766    VisitType(S->getTypeOperand());
767}
768
769void StmtProfiler::VisitCXXThisExpr(const CXXThisExpr *S) {
770  VisitExpr(S);
771  ID.AddBoolean(S->isImplicit());
772}
773
774void StmtProfiler::VisitCXXThrowExpr(const CXXThrowExpr *S) {
775  VisitExpr(S);
776}
777
778void StmtProfiler::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) {
779  VisitExpr(S);
780  VisitDecl(S->getParam());
781}
782
783void StmtProfiler::VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *S) {
784  VisitExpr(S);
785  VisitDecl(
786         const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
787}
788
789void StmtProfiler::VisitCXXConstructExpr(const CXXConstructExpr *S) {
790  VisitExpr(S);
791  VisitDecl(S->getConstructor());
792  ID.AddBoolean(S->isElidable());
793}
794
795void StmtProfiler::VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *S) {
796  VisitExplicitCastExpr(S);
797}
798
799void
800StmtProfiler::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *S) {
801  VisitCXXConstructExpr(S);
802}
803
804void
805StmtProfiler::VisitLambdaExpr(const LambdaExpr *S) {
806  VisitExpr(S);
807  for (LambdaExpr::capture_iterator C = S->explicit_capture_begin(),
808                                 CEnd = S->explicit_capture_end();
809       C != CEnd; ++C) {
810    ID.AddInteger(C->getCaptureKind());
811    if (C->capturesVariable()) {
812      VisitDecl(C->getCapturedVar());
813      ID.AddBoolean(C->isPackExpansion());
814    }
815  }
816  // Note: If we actually needed to be able to match lambda
817  // expressions, we would have to consider parameters and return type
818  // here, among other things.
819  VisitStmt(S->getBody());
820}
821
822void
823StmtProfiler::VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *S) {
824  VisitExpr(S);
825}
826
827void StmtProfiler::VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
828  VisitExpr(S);
829  ID.AddBoolean(S->isGlobalDelete());
830  ID.AddBoolean(S->isArrayForm());
831  VisitDecl(S->getOperatorDelete());
832}
833
834
835void StmtProfiler::VisitCXXNewExpr(const CXXNewExpr *S) {
836  VisitExpr(S);
837  VisitType(S->getAllocatedType());
838  VisitDecl(S->getOperatorNew());
839  VisitDecl(S->getOperatorDelete());
840  ID.AddBoolean(S->isArray());
841  ID.AddInteger(S->getNumPlacementArgs());
842  ID.AddBoolean(S->isGlobalNew());
843  ID.AddBoolean(S->isParenTypeId());
844  ID.AddInteger(S->getInitializationStyle());
845}
846
847void
848StmtProfiler::VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
849  VisitExpr(S);
850  ID.AddBoolean(S->isArrow());
851  VisitNestedNameSpecifier(S->getQualifier());
852  VisitType(S->getDestroyedType());
853}
854
855void StmtProfiler::VisitOverloadExpr(const OverloadExpr *S) {
856  VisitExpr(S);
857  VisitNestedNameSpecifier(S->getQualifier());
858  VisitName(S->getName());
859  ID.AddBoolean(S->hasExplicitTemplateArgs());
860  if (S->hasExplicitTemplateArgs())
861    VisitTemplateArguments(S->getExplicitTemplateArgs().getTemplateArgs(),
862                           S->getExplicitTemplateArgs().NumTemplateArgs);
863}
864
865void
866StmtProfiler::VisitUnresolvedLookupExpr(const UnresolvedLookupExpr *S) {
867  VisitOverloadExpr(S);
868}
869
870void StmtProfiler::VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *S) {
871  VisitExpr(S);
872  ID.AddInteger(S->getTrait());
873  VisitType(S->getQueriedType());
874}
875
876void StmtProfiler::VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *S) {
877  VisitExpr(S);
878  ID.AddInteger(S->getTrait());
879  VisitType(S->getLhsType());
880  VisitType(S->getRhsType());
881}
882
883void StmtProfiler::VisitTypeTraitExpr(const TypeTraitExpr *S) {
884  VisitExpr(S);
885  ID.AddInteger(S->getTrait());
886  ID.AddInteger(S->getNumArgs());
887  for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
888    VisitType(S->getArg(I)->getType());
889}
890
891void StmtProfiler::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *S) {
892  VisitExpr(S);
893  ID.AddInteger(S->getTrait());
894  VisitType(S->getQueriedType());
895}
896
897void StmtProfiler::VisitExpressionTraitExpr(const ExpressionTraitExpr *S) {
898  VisitExpr(S);
899  ID.AddInteger(S->getTrait());
900  VisitExpr(S->getQueriedExpression());
901}
902
903void StmtProfiler::VisitDependentScopeDeclRefExpr(
904    const DependentScopeDeclRefExpr *S) {
905  VisitExpr(S);
906  VisitName(S->getDeclName());
907  VisitNestedNameSpecifier(S->getQualifier());
908  ID.AddBoolean(S->hasExplicitTemplateArgs());
909  if (S->hasExplicitTemplateArgs())
910    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
911}
912
913void StmtProfiler::VisitExprWithCleanups(const ExprWithCleanups *S) {
914  VisitExpr(S);
915}
916
917void StmtProfiler::VisitCXXUnresolvedConstructExpr(
918    const CXXUnresolvedConstructExpr *S) {
919  VisitExpr(S);
920  VisitType(S->getTypeAsWritten());
921}
922
923void StmtProfiler::VisitCXXDependentScopeMemberExpr(
924    const CXXDependentScopeMemberExpr *S) {
925  ID.AddBoolean(S->isImplicitAccess());
926  if (!S->isImplicitAccess()) {
927    VisitExpr(S);
928    ID.AddBoolean(S->isArrow());
929  }
930  VisitNestedNameSpecifier(S->getQualifier());
931  VisitName(S->getMember());
932  ID.AddBoolean(S->hasExplicitTemplateArgs());
933  if (S->hasExplicitTemplateArgs())
934    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
935}
936
937void StmtProfiler::VisitUnresolvedMemberExpr(const UnresolvedMemberExpr *S) {
938  ID.AddBoolean(S->isImplicitAccess());
939  if (!S->isImplicitAccess()) {
940    VisitExpr(S);
941    ID.AddBoolean(S->isArrow());
942  }
943  VisitNestedNameSpecifier(S->getQualifier());
944  VisitName(S->getMemberName());
945  ID.AddBoolean(S->hasExplicitTemplateArgs());
946  if (S->hasExplicitTemplateArgs())
947    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
948}
949
950void StmtProfiler::VisitCXXNoexceptExpr(const CXXNoexceptExpr *S) {
951  VisitExpr(S);
952}
953
954void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) {
955  VisitExpr(S);
956}
957
958void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
959  VisitExpr(S);
960  VisitDecl(S->getPack());
961}
962
963void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr(
964    const SubstNonTypeTemplateParmPackExpr *S) {
965  VisitExpr(S);
966  VisitDecl(S->getParameterPack());
967  VisitTemplateArgument(S->getArgumentPack());
968}
969
970void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
971    const SubstNonTypeTemplateParmExpr *E) {
972  // Profile exactly as the replacement expression.
973  Visit(E->getReplacement());
974}
975
976void StmtProfiler::VisitMaterializeTemporaryExpr(
977                                           const MaterializeTemporaryExpr *S) {
978  VisitExpr(S);
979}
980
981void StmtProfiler::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
982  VisitExpr(E);
983}
984
985void StmtProfiler::VisitObjCStringLiteral(const ObjCStringLiteral *S) {
986  VisitExpr(S);
987}
988
989void StmtProfiler::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
990  VisitExpr(E);
991}
992
993void StmtProfiler::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
994  VisitExpr(E);
995}
996
997void StmtProfiler::VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
998  VisitExpr(E);
999}
1000
1001void StmtProfiler::VisitObjCEncodeExpr(const ObjCEncodeExpr *S) {
1002  VisitExpr(S);
1003  VisitType(S->getEncodedType());
1004}
1005
1006void StmtProfiler::VisitObjCSelectorExpr(const ObjCSelectorExpr *S) {
1007  VisitExpr(S);
1008  VisitName(S->getSelector());
1009}
1010
1011void StmtProfiler::VisitObjCProtocolExpr(const ObjCProtocolExpr *S) {
1012  VisitExpr(S);
1013  VisitDecl(S->getProtocol());
1014}
1015
1016void StmtProfiler::VisitObjCIvarRefExpr(const ObjCIvarRefExpr *S) {
1017  VisitExpr(S);
1018  VisitDecl(S->getDecl());
1019  ID.AddBoolean(S->isArrow());
1020  ID.AddBoolean(S->isFreeIvar());
1021}
1022
1023void StmtProfiler::VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *S) {
1024  VisitExpr(S);
1025  if (S->isImplicitProperty()) {
1026    VisitDecl(S->getImplicitPropertyGetter());
1027    VisitDecl(S->getImplicitPropertySetter());
1028  } else {
1029    VisitDecl(S->getExplicitProperty());
1030  }
1031  if (S->isSuperReceiver()) {
1032    ID.AddBoolean(S->isSuperReceiver());
1033    VisitType(S->getSuperReceiverType());
1034  }
1035}
1036
1037void StmtProfiler::VisitObjCSubscriptRefExpr(const ObjCSubscriptRefExpr *S) {
1038  VisitExpr(S);
1039  VisitDecl(S->getAtIndexMethodDecl());
1040  VisitDecl(S->setAtIndexMethodDecl());
1041}
1042
1043void StmtProfiler::VisitObjCMessageExpr(const ObjCMessageExpr *S) {
1044  VisitExpr(S);
1045  VisitName(S->getSelector());
1046  VisitDecl(S->getMethodDecl());
1047}
1048
1049void StmtProfiler::VisitObjCIsaExpr(const ObjCIsaExpr *S) {
1050  VisitExpr(S);
1051  ID.AddBoolean(S->isArrow());
1052}
1053
1054void StmtProfiler::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *S) {
1055  VisitExpr(S);
1056  ID.AddBoolean(S->getValue());
1057}
1058
1059void StmtProfiler::VisitObjCIndirectCopyRestoreExpr(
1060    const ObjCIndirectCopyRestoreExpr *S) {
1061  VisitExpr(S);
1062  ID.AddBoolean(S->shouldCopy());
1063}
1064
1065void StmtProfiler::VisitObjCBridgedCastExpr(const ObjCBridgedCastExpr *S) {
1066  VisitExplicitCastExpr(S);
1067  ID.AddBoolean(S->getBridgeKind());
1068}
1069
1070void StmtProfiler::VisitDecl(const Decl *D) {
1071  ID.AddInteger(D? D->getKind() : 0);
1072
1073  if (Canonical && D) {
1074    if (const NonTypeTemplateParmDecl *NTTP =
1075          dyn_cast<NonTypeTemplateParmDecl>(D)) {
1076      ID.AddInteger(NTTP->getDepth());
1077      ID.AddInteger(NTTP->getIndex());
1078      ID.AddBoolean(NTTP->isParameterPack());
1079      VisitType(NTTP->getType());
1080      return;
1081    }
1082
1083    if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
1084      // The Itanium C++ ABI uses the type, scope depth, and scope
1085      // index of a parameter when mangling expressions that involve
1086      // function parameters, so we will use the parameter's type for
1087      // establishing function parameter identity. That way, our
1088      // definition of "equivalent" (per C++ [temp.over.link]) is at
1089      // least as strong as the definition of "equivalent" used for
1090      // name mangling.
1091      VisitType(Parm->getType());
1092      ID.AddInteger(Parm->getFunctionScopeDepth());
1093      ID.AddInteger(Parm->getFunctionScopeIndex());
1094      return;
1095    }
1096
1097    if (const TemplateTypeParmDecl *TTP =
1098          dyn_cast<TemplateTypeParmDecl>(D)) {
1099      ID.AddInteger(TTP->getDepth());
1100      ID.AddInteger(TTP->getIndex());
1101      ID.AddBoolean(TTP->isParameterPack());
1102      return;
1103    }
1104
1105    if (const TemplateTemplateParmDecl *TTP =
1106          dyn_cast<TemplateTemplateParmDecl>(D)) {
1107      ID.AddInteger(TTP->getDepth());
1108      ID.AddInteger(TTP->getIndex());
1109      ID.AddBoolean(TTP->isParameterPack());
1110      return;
1111    }
1112  }
1113
1114  ID.AddPointer(D? D->getCanonicalDecl() : 0);
1115}
1116
1117void StmtProfiler::VisitType(QualType T) {
1118  if (Canonical)
1119    T = Context.getCanonicalType(T);
1120
1121  ID.AddPointer(T.getAsOpaquePtr());
1122}
1123
1124void StmtProfiler::VisitName(DeclarationName Name) {
1125  ID.AddPointer(Name.getAsOpaquePtr());
1126}
1127
1128void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) {
1129  if (Canonical)
1130    NNS = Context.getCanonicalNestedNameSpecifier(NNS);
1131  ID.AddPointer(NNS);
1132}
1133
1134void StmtProfiler::VisitTemplateName(TemplateName Name) {
1135  if (Canonical)
1136    Name = Context.getCanonicalTemplateName(Name);
1137
1138  Name.Profile(ID);
1139}
1140
1141void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args,
1142                                          unsigned NumArgs) {
1143  ID.AddInteger(NumArgs);
1144  for (unsigned I = 0; I != NumArgs; ++I)
1145    VisitTemplateArgument(Args[I].getArgument());
1146}
1147
1148void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) {
1149  // Mostly repetitive with TemplateArgument::Profile!
1150  ID.AddInteger(Arg.getKind());
1151  switch (Arg.getKind()) {
1152  case TemplateArgument::Null:
1153    break;
1154
1155  case TemplateArgument::Type:
1156    VisitType(Arg.getAsType());
1157    break;
1158
1159  case TemplateArgument::Template:
1160  case TemplateArgument::TemplateExpansion:
1161    VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
1162    break;
1163
1164  case TemplateArgument::Declaration:
1165    VisitDecl(Arg.getAsDecl());
1166    break;
1167
1168  case TemplateArgument::Integral:
1169    Arg.getAsIntegral().Profile(ID);
1170    VisitType(Arg.getIntegralType());
1171    break;
1172
1173  case TemplateArgument::Expression:
1174    Visit(Arg.getAsExpr());
1175    break;
1176
1177  case TemplateArgument::Pack:
1178    const TemplateArgument *Pack = Arg.pack_begin();
1179    for (unsigned i = 0, e = Arg.pack_size(); i != e; ++i)
1180      VisitTemplateArgument(Pack[i]);
1181    break;
1182  }
1183}
1184
1185void Stmt::Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1186                   bool Canonical) const {
1187  StmtProfiler Profiler(ID, Context, Canonical);
1188  Profiler.Visit(this);
1189}
1190