StmtProfile.cpp revision 8f43d52b46b600b2eb4e62550f7a6d7a78fd001b
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 StmtVisitor<StmtProfiler> {
27    llvm::FoldingSetNodeID &ID;
28    ASTContext &Context;
29    bool Canonical;
30
31  public:
32    StmtProfiler(llvm::FoldingSetNodeID &ID, ASTContext &Context,
33                 bool Canonical)
34      : ID(ID), Context(Context), Canonical(Canonical) { }
35
36    void VisitStmt(Stmt *S);
37
38#define STMT(Node, Base) void Visit##Node(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(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, unsigned NumArgs);
63
64    /// \brief Visit a single template argument.
65    void VisitTemplateArgument(const TemplateArgument &Arg);
66  };
67}
68
69void StmtProfiler::VisitStmt(Stmt *S) {
70  ID.AddInteger(S->getStmtClass());
71  for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
72       C != CEnd; ++C)
73    Visit(*C);
74}
75
76void StmtProfiler::VisitDeclStmt(DeclStmt *S) {
77  VisitStmt(S);
78  for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
79       D != DEnd; ++D)
80    VisitDecl(*D);
81}
82
83void StmtProfiler::VisitNullStmt(NullStmt *S) {
84  VisitStmt(S);
85}
86
87void StmtProfiler::VisitCompoundStmt(CompoundStmt *S) {
88  VisitStmt(S);
89}
90
91void StmtProfiler::VisitSwitchCase(SwitchCase *S) {
92  VisitStmt(S);
93}
94
95void StmtProfiler::VisitCaseStmt(CaseStmt *S) {
96  VisitStmt(S);
97}
98
99void StmtProfiler::VisitDefaultStmt(DefaultStmt *S) {
100  VisitStmt(S);
101}
102
103void StmtProfiler::VisitLabelStmt(LabelStmt *S) {
104  VisitStmt(S);
105  VisitName(S->getID());
106}
107
108void StmtProfiler::VisitIfStmt(IfStmt *S) {
109  VisitStmt(S);
110  VisitDecl(S->getConditionVariable());
111}
112
113void StmtProfiler::VisitSwitchStmt(SwitchStmt *S) {
114  VisitStmt(S);
115  VisitDecl(S->getConditionVariable());
116}
117
118void StmtProfiler::VisitWhileStmt(WhileStmt *S) {
119  VisitStmt(S);
120  VisitDecl(S->getConditionVariable());
121}
122
123void StmtProfiler::VisitDoStmt(DoStmt *S) {
124  VisitStmt(S);
125}
126
127void StmtProfiler::VisitForStmt(ForStmt *S) {
128  VisitStmt(S);
129}
130
131void StmtProfiler::VisitGotoStmt(GotoStmt *S) {
132  VisitStmt(S);
133  VisitName(S->getLabel()->getID());
134}
135
136void StmtProfiler::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
137  VisitStmt(S);
138}
139
140void StmtProfiler::VisitContinueStmt(ContinueStmt *S) {
141  VisitStmt(S);
142}
143
144void StmtProfiler::VisitBreakStmt(BreakStmt *S) {
145  VisitStmt(S);
146}
147
148void StmtProfiler::VisitReturnStmt(ReturnStmt *S) {
149  VisitStmt(S);
150}
151
152void StmtProfiler::VisitAsmStmt(AsmStmt *S) {
153  VisitStmt(S);
154  ID.AddBoolean(S->isVolatile());
155  ID.AddBoolean(S->isSimple());
156  VisitStringLiteral(S->getAsmString());
157  ID.AddInteger(S->getNumOutputs());
158  for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
159    ID.AddString(S->getOutputName(I));
160    VisitStringLiteral(S->getOutputConstraintLiteral(I));
161  }
162  ID.AddInteger(S->getNumInputs());
163  for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
164    ID.AddString(S->getInputName(I));
165    VisitStringLiteral(S->getInputConstraintLiteral(I));
166  }
167  ID.AddInteger(S->getNumClobbers());
168  for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
169    VisitStringLiteral(S->getClobber(I));
170}
171
172void StmtProfiler::VisitCXXCatchStmt(CXXCatchStmt *S) {
173  VisitStmt(S);
174  VisitType(S->getCaughtType());
175}
176
177void StmtProfiler::VisitCXXTryStmt(CXXTryStmt *S) {
178  VisitStmt(S);
179}
180
181void StmtProfiler::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
182  VisitStmt(S);
183}
184
185void StmtProfiler::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
186  VisitStmt(S);
187  ID.AddBoolean(S->hasEllipsis());
188  if (S->getCatchParamDecl())
189    VisitType(S->getCatchParamDecl()->getType());
190}
191
192void StmtProfiler::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
193  VisitStmt(S);
194}
195
196void StmtProfiler::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
197  VisitStmt(S);
198}
199
200void StmtProfiler::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
201  VisitStmt(S);
202}
203
204void StmtProfiler::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
205  VisitStmt(S);
206}
207
208void StmtProfiler::VisitExpr(Expr *S) {
209  VisitStmt(S);
210}
211
212void StmtProfiler::VisitDeclRefExpr(DeclRefExpr *S) {
213  VisitExpr(S);
214  VisitNestedNameSpecifier(S->getQualifier());
215  VisitDecl(S->getDecl());
216  VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
217}
218
219void StmtProfiler::VisitPredefinedExpr(PredefinedExpr *S) {
220  VisitExpr(S);
221  ID.AddInteger(S->getIdentType());
222}
223
224void StmtProfiler::VisitIntegerLiteral(IntegerLiteral *S) {
225  VisitExpr(S);
226  S->getValue().Profile(ID);
227}
228
229void StmtProfiler::VisitCharacterLiteral(CharacterLiteral *S) {
230  VisitExpr(S);
231  ID.AddBoolean(S->isWide());
232  ID.AddInteger(S->getValue());
233}
234
235void StmtProfiler::VisitFloatingLiteral(FloatingLiteral *S) {
236  VisitExpr(S);
237  S->getValue().Profile(ID);
238  ID.AddBoolean(S->isExact());
239}
240
241void StmtProfiler::VisitImaginaryLiteral(ImaginaryLiteral *S) {
242  VisitExpr(S);
243}
244
245void StmtProfiler::VisitStringLiteral(StringLiteral *S) {
246  VisitExpr(S);
247  ID.AddString(S->getString());
248  ID.AddBoolean(S->isWide());
249}
250
251void StmtProfiler::VisitParenExpr(ParenExpr *S) {
252  VisitExpr(S);
253}
254
255void StmtProfiler::VisitParenListExpr(ParenListExpr *S) {
256  VisitExpr(S);
257}
258
259void StmtProfiler::VisitUnaryOperator(UnaryOperator *S) {
260  VisitExpr(S);
261  ID.AddInteger(S->getOpcode());
262}
263
264void StmtProfiler::VisitOffsetOfExpr(OffsetOfExpr *S) {
265  VisitType(S->getTypeSourceInfo()->getType());
266  unsigned n = S->getNumComponents();
267  for (unsigned i = 0; i < n; ++i) {
268    const OffsetOfExpr::OffsetOfNode& ON = S->getComponent(i);
269    ID.AddInteger(ON.getKind());
270    switch (ON.getKind()) {
271    case OffsetOfExpr::OffsetOfNode::Array:
272      // Expressions handled below.
273      break;
274
275    case OffsetOfExpr::OffsetOfNode::Field:
276      VisitDecl(ON.getField());
277      break;
278
279    case OffsetOfExpr::OffsetOfNode::Identifier:
280      ID.AddPointer(ON.getFieldName());
281      break;
282
283    case OffsetOfExpr::OffsetOfNode::Base:
284      // These nodes are implicit, and therefore don't need profiling.
285      break;
286    }
287  }
288
289  VisitExpr(S);
290}
291
292void StmtProfiler::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *S) {
293  VisitExpr(S);
294  ID.AddBoolean(S->isSizeOf());
295  if (S->isArgumentType())
296    VisitType(S->getArgumentType());
297}
298
299void StmtProfiler::VisitArraySubscriptExpr(ArraySubscriptExpr *S) {
300  VisitExpr(S);
301}
302
303void StmtProfiler::VisitCallExpr(CallExpr *S) {
304  VisitExpr(S);
305}
306
307void StmtProfiler::VisitMemberExpr(MemberExpr *S) {
308  VisitExpr(S);
309  VisitDecl(S->getMemberDecl());
310  VisitNestedNameSpecifier(S->getQualifier());
311  ID.AddBoolean(S->isArrow());
312}
313
314void StmtProfiler::VisitCompoundLiteralExpr(CompoundLiteralExpr *S) {
315  VisitExpr(S);
316  ID.AddBoolean(S->isFileScope());
317}
318
319void StmtProfiler::VisitCastExpr(CastExpr *S) {
320  VisitExpr(S);
321}
322
323void StmtProfiler::VisitImplicitCastExpr(ImplicitCastExpr *S) {
324  VisitCastExpr(S);
325  ID.AddBoolean(S->isLvalueCast());
326}
327
328void StmtProfiler::VisitExplicitCastExpr(ExplicitCastExpr *S) {
329  VisitCastExpr(S);
330  VisitType(S->getTypeAsWritten());
331}
332
333void StmtProfiler::VisitCStyleCastExpr(CStyleCastExpr *S) {
334  VisitExplicitCastExpr(S);
335}
336
337void StmtProfiler::VisitBinaryOperator(BinaryOperator *S) {
338  VisitExpr(S);
339  ID.AddInteger(S->getOpcode());
340}
341
342void StmtProfiler::VisitCompoundAssignOperator(CompoundAssignOperator *S) {
343  VisitBinaryOperator(S);
344}
345
346void StmtProfiler::VisitConditionalOperator(ConditionalOperator *S) {
347  VisitExpr(S);
348}
349
350void StmtProfiler::VisitAddrLabelExpr(AddrLabelExpr *S) {
351  VisitExpr(S);
352  VisitName(S->getLabel()->getID());
353}
354
355void StmtProfiler::VisitStmtExpr(StmtExpr *S) {
356  VisitExpr(S);
357}
358
359void StmtProfiler::VisitTypesCompatibleExpr(TypesCompatibleExpr *S) {
360  VisitExpr(S);
361  VisitType(S->getArgType1());
362  VisitType(S->getArgType2());
363}
364
365void StmtProfiler::VisitShuffleVectorExpr(ShuffleVectorExpr *S) {
366  VisitExpr(S);
367}
368
369void StmtProfiler::VisitChooseExpr(ChooseExpr *S) {
370  VisitExpr(S);
371}
372
373void StmtProfiler::VisitGNUNullExpr(GNUNullExpr *S) {
374  VisitExpr(S);
375}
376
377void StmtProfiler::VisitVAArgExpr(VAArgExpr *S) {
378  VisitExpr(S);
379}
380
381void StmtProfiler::VisitInitListExpr(InitListExpr *S) {
382  if (S->getSyntacticForm()) {
383    VisitInitListExpr(S->getSyntacticForm());
384    return;
385  }
386
387  VisitExpr(S);
388}
389
390void StmtProfiler::VisitDesignatedInitExpr(DesignatedInitExpr *S) {
391  VisitExpr(S);
392  ID.AddBoolean(S->usesGNUSyntax());
393  for (DesignatedInitExpr::designators_iterator D = S->designators_begin(),
394                                             DEnd = S->designators_end();
395       D != DEnd; ++D) {
396    if (D->isFieldDesignator()) {
397      ID.AddInteger(0);
398      VisitName(D->getFieldName());
399      continue;
400    }
401
402    if (D->isArrayDesignator()) {
403      ID.AddInteger(1);
404    } else {
405      assert(D->isArrayRangeDesignator());
406      ID.AddInteger(2);
407    }
408    ID.AddInteger(D->getFirstExprIndex());
409  }
410}
411
412void StmtProfiler::VisitImplicitValueInitExpr(ImplicitValueInitExpr *S) {
413  VisitExpr(S);
414}
415
416void StmtProfiler::VisitExtVectorElementExpr(ExtVectorElementExpr *S) {
417  VisitExpr(S);
418  VisitName(&S->getAccessor());
419}
420
421void StmtProfiler::VisitBlockExpr(BlockExpr *S) {
422  VisitExpr(S);
423  VisitDecl(S->getBlockDecl());
424}
425
426void StmtProfiler::VisitBlockDeclRefExpr(BlockDeclRefExpr *S) {
427  VisitExpr(S);
428  VisitDecl(S->getDecl());
429  ID.AddBoolean(S->isByRef());
430  ID.AddBoolean(S->isConstQualAdded());
431}
432
433static Stmt::StmtClass DecodeOperatorCall(CXXOperatorCallExpr *S,
434                                          UnaryOperator::Opcode &UnaryOp,
435                                          BinaryOperator::Opcode &BinaryOp) {
436  switch (S->getOperator()) {
437  case OO_None:
438  case OO_New:
439  case OO_Delete:
440  case OO_Array_New:
441  case OO_Array_Delete:
442  case OO_Arrow:
443  case OO_Call:
444  case OO_Conditional:
445  case NUM_OVERLOADED_OPERATORS:
446    llvm_unreachable("Invalid operator call kind");
447    return Stmt::ArraySubscriptExprClass;
448
449  case OO_Plus:
450    if (S->getNumArgs() == 1) {
451      UnaryOp = UnaryOperator::Plus;
452      return Stmt::UnaryOperatorClass;
453    }
454
455    BinaryOp = BinaryOperator::Add;
456    return Stmt::BinaryOperatorClass;
457
458  case OO_Minus:
459    if (S->getNumArgs() == 1) {
460      UnaryOp = UnaryOperator::Minus;
461      return Stmt::UnaryOperatorClass;
462    }
463
464    BinaryOp = BinaryOperator::Sub;
465    return Stmt::BinaryOperatorClass;
466
467  case OO_Star:
468    if (S->getNumArgs() == 1) {
469      UnaryOp = UnaryOperator::Minus;
470      return Stmt::UnaryOperatorClass;
471    }
472
473    BinaryOp = BinaryOperator::Sub;
474    return Stmt::BinaryOperatorClass;
475
476  case OO_Slash:
477    BinaryOp = BinaryOperator::Div;
478    return Stmt::BinaryOperatorClass;
479
480  case OO_Percent:
481    BinaryOp = BinaryOperator::Rem;
482    return Stmt::BinaryOperatorClass;
483
484  case OO_Caret:
485    BinaryOp = BinaryOperator::Xor;
486    return Stmt::BinaryOperatorClass;
487
488  case OO_Amp:
489    if (S->getNumArgs() == 1) {
490      UnaryOp = UnaryOperator::AddrOf;
491      return Stmt::UnaryOperatorClass;
492    }
493
494    BinaryOp = BinaryOperator::And;
495    return Stmt::BinaryOperatorClass;
496
497  case OO_Pipe:
498    BinaryOp = BinaryOperator::Or;
499    return Stmt::BinaryOperatorClass;
500
501  case OO_Tilde:
502    UnaryOp = UnaryOperator::Not;
503    return Stmt::UnaryOperatorClass;
504
505  case OO_Exclaim:
506    UnaryOp = UnaryOperator::LNot;
507    return Stmt::UnaryOperatorClass;
508
509  case OO_Equal:
510    BinaryOp = BinaryOperator::Assign;
511    return Stmt::BinaryOperatorClass;
512
513  case OO_Less:
514    BinaryOp = BinaryOperator::LT;
515    return Stmt::BinaryOperatorClass;
516
517  case OO_Greater:
518    BinaryOp = BinaryOperator::GT;
519    return Stmt::BinaryOperatorClass;
520
521  case OO_PlusEqual:
522    BinaryOp = BinaryOperator::AddAssign;
523    return Stmt::CompoundAssignOperatorClass;
524
525  case OO_MinusEqual:
526    BinaryOp = BinaryOperator::SubAssign;
527    return Stmt::CompoundAssignOperatorClass;
528
529  case OO_StarEqual:
530    BinaryOp = BinaryOperator::MulAssign;
531    return Stmt::CompoundAssignOperatorClass;
532
533  case OO_SlashEqual:
534    BinaryOp = BinaryOperator::DivAssign;
535    return Stmt::CompoundAssignOperatorClass;
536
537  case OO_PercentEqual:
538    BinaryOp = BinaryOperator::RemAssign;
539    return Stmt::CompoundAssignOperatorClass;
540
541  case OO_CaretEqual:
542    BinaryOp = BinaryOperator::XorAssign;
543    return Stmt::CompoundAssignOperatorClass;
544
545  case OO_AmpEqual:
546    BinaryOp = BinaryOperator::AndAssign;
547    return Stmt::CompoundAssignOperatorClass;
548
549  case OO_PipeEqual:
550    BinaryOp = BinaryOperator::OrAssign;
551    return Stmt::CompoundAssignOperatorClass;
552
553  case OO_LessLess:
554    BinaryOp = BinaryOperator::Shl;
555    return Stmt::BinaryOperatorClass;
556
557  case OO_GreaterGreater:
558    BinaryOp = BinaryOperator::Shr;
559    return Stmt::BinaryOperatorClass;
560
561  case OO_LessLessEqual:
562    BinaryOp = BinaryOperator::ShlAssign;
563    return Stmt::CompoundAssignOperatorClass;
564
565  case OO_GreaterGreaterEqual:
566    BinaryOp = BinaryOperator::ShrAssign;
567    return Stmt::CompoundAssignOperatorClass;
568
569  case OO_EqualEqual:
570    BinaryOp = BinaryOperator::EQ;
571    return Stmt::BinaryOperatorClass;
572
573  case OO_ExclaimEqual:
574    BinaryOp = BinaryOperator::NE;
575    return Stmt::BinaryOperatorClass;
576
577  case OO_LessEqual:
578    BinaryOp = BinaryOperator::LE;
579    return Stmt::BinaryOperatorClass;
580
581  case OO_GreaterEqual:
582    BinaryOp = BinaryOperator::GE;
583    return Stmt::BinaryOperatorClass;
584
585  case OO_AmpAmp:
586    BinaryOp = BinaryOperator::LAnd;
587    return Stmt::BinaryOperatorClass;
588
589  case OO_PipePipe:
590    BinaryOp = BinaryOperator::LOr;
591    return Stmt::BinaryOperatorClass;
592
593  case OO_PlusPlus:
594    UnaryOp = S->getNumArgs() == 1? UnaryOperator::PreInc
595                                  : UnaryOperator::PostInc;
596    return Stmt::UnaryOperatorClass;
597
598  case OO_MinusMinus:
599    UnaryOp = S->getNumArgs() == 1? UnaryOperator::PreDec
600                                  : UnaryOperator::PostDec;
601    return Stmt::UnaryOperatorClass;
602
603  case OO_Comma:
604    BinaryOp = BinaryOperator::Comma;
605    return Stmt::BinaryOperatorClass;
606
607
608  case OO_ArrowStar:
609    BinaryOp = BinaryOperator::PtrMemI;
610    return Stmt::BinaryOperatorClass;
611
612  case OO_Subscript:
613    return Stmt::ArraySubscriptExprClass;
614  }
615
616  llvm_unreachable("Invalid overloaded operator expression");
617}
618
619
620void StmtProfiler::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
621  if (S->isTypeDependent()) {
622    // Type-dependent operator calls are profiled like their underlying
623    // syntactic operator.
624    UnaryOperator::Opcode UnaryOp = UnaryOperator::Extension;
625    BinaryOperator::Opcode BinaryOp = BinaryOperator::Comma;
626    Stmt::StmtClass SC = DecodeOperatorCall(S, UnaryOp, BinaryOp);
627
628    ID.AddInteger(SC);
629    for (unsigned I = 0, N = S->getNumArgs(); I != N; ++I)
630      Visit(S->getArg(I));
631    if (SC == Stmt::UnaryOperatorClass)
632      ID.AddInteger(UnaryOp);
633    else if (SC == Stmt::BinaryOperatorClass ||
634             SC == Stmt::CompoundAssignOperatorClass)
635      ID.AddInteger(BinaryOp);
636    else
637      assert(SC == Stmt::ArraySubscriptExprClass);
638
639    return;
640  }
641
642  VisitCallExpr(S);
643  ID.AddInteger(S->getOperator());
644}
645
646void StmtProfiler::VisitCXXMemberCallExpr(CXXMemberCallExpr *S) {
647  VisitCallExpr(S);
648}
649
650void StmtProfiler::VisitCXXNamedCastExpr(CXXNamedCastExpr *S) {
651  VisitExplicitCastExpr(S);
652}
653
654void StmtProfiler::VisitCXXStaticCastExpr(CXXStaticCastExpr *S) {
655  VisitCXXNamedCastExpr(S);
656}
657
658void StmtProfiler::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *S) {
659  VisitCXXNamedCastExpr(S);
660}
661
662void StmtProfiler::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *S) {
663  VisitCXXNamedCastExpr(S);
664}
665
666void StmtProfiler::VisitCXXConstCastExpr(CXXConstCastExpr *S) {
667  VisitCXXNamedCastExpr(S);
668}
669
670void StmtProfiler::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *S) {
671  VisitExpr(S);
672  ID.AddBoolean(S->getValue());
673}
674
675void StmtProfiler::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) {
676  VisitExpr(S);
677}
678
679void StmtProfiler::VisitCXXTypeidExpr(CXXTypeidExpr *S) {
680  VisitExpr(S);
681  if (S->isTypeOperand())
682    VisitType(S->getTypeOperand());
683}
684
685void StmtProfiler::VisitCXXThisExpr(CXXThisExpr *S) {
686  VisitExpr(S);
687}
688
689void StmtProfiler::VisitCXXThrowExpr(CXXThrowExpr *S) {
690  VisitExpr(S);
691}
692
693void StmtProfiler::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *S) {
694  VisitExpr(S);
695  VisitDecl(S->getParam());
696}
697
698void StmtProfiler::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *S) {
699  VisitExpr(S);
700  VisitDecl(
701         const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
702}
703
704void StmtProfiler::VisitCXXBindReferenceExpr(CXXBindReferenceExpr *S) {
705  VisitExpr(S);
706}
707
708void StmtProfiler::VisitCXXConstructExpr(CXXConstructExpr *S) {
709  VisitExpr(S);
710  VisitDecl(S->getConstructor());
711  ID.AddBoolean(S->isElidable());
712}
713
714void StmtProfiler::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *S) {
715  VisitExplicitCastExpr(S);
716}
717
718void StmtProfiler::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *S) {
719  VisitCXXConstructExpr(S);
720}
721
722void StmtProfiler::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *S) {
723  VisitExpr(S);
724}
725
726void StmtProfiler::VisitCXXDeleteExpr(CXXDeleteExpr *S) {
727  VisitExpr(S);
728  ID.AddBoolean(S->isGlobalDelete());
729  ID.AddBoolean(S->isArrayForm());
730  VisitDecl(S->getOperatorDelete());
731}
732
733
734void StmtProfiler::VisitCXXNewExpr(CXXNewExpr *S) {
735  VisitExpr(S);
736  VisitType(S->getAllocatedType());
737  VisitDecl(S->getOperatorNew());
738  VisitDecl(S->getOperatorDelete());
739  VisitDecl(S->getConstructor());
740  ID.AddBoolean(S->isArray());
741  ID.AddInteger(S->getNumPlacementArgs());
742  ID.AddBoolean(S->isGlobalNew());
743  ID.AddBoolean(S->isParenTypeId());
744  ID.AddBoolean(S->hasInitializer());
745  ID.AddInteger(S->getNumConstructorArgs());
746}
747
748void StmtProfiler::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *S) {
749  VisitExpr(S);
750  ID.AddBoolean(S->isArrow());
751  VisitNestedNameSpecifier(S->getQualifier());
752  VisitType(S->getDestroyedType());
753}
754
755void
756StmtProfiler::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *S) {
757  VisitExpr(S);
758  VisitNestedNameSpecifier(S->getQualifier());
759  VisitName(S->getName());
760  ID.AddBoolean(S->hasExplicitTemplateArgs());
761  if (S->hasExplicitTemplateArgs())
762    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
763}
764
765void StmtProfiler::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *S) {
766  VisitExpr(S);
767  ID.AddInteger(S->getTrait());
768  VisitType(S->getQueriedType());
769}
770
771void
772StmtProfiler::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *S) {
773  VisitExpr(S);
774  VisitName(S->getDeclName());
775  VisitNestedNameSpecifier(S->getQualifier());
776  ID.AddBoolean(S->hasExplicitTemplateArgs());
777  if (S->hasExplicitTemplateArgs())
778    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
779}
780
781void StmtProfiler::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *S) {
782  VisitExpr(S);
783  for (unsigned I = 0, N = S->getNumTemporaries(); I != N; ++I)
784    VisitDecl(
785      const_cast<CXXDestructorDecl *>(S->getTemporary(I)->getDestructor()));
786}
787
788void
789StmtProfiler::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *S) {
790  VisitExpr(S);
791  VisitType(S->getTypeAsWritten());
792}
793
794void
795StmtProfiler::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *S) {
796  ID.AddBoolean(S->isImplicitAccess());
797  if (!S->isImplicitAccess()) {
798    VisitExpr(S);
799    ID.AddBoolean(S->isArrow());
800  }
801  VisitNestedNameSpecifier(S->getQualifier());
802  VisitName(S->getMember());
803  ID.AddBoolean(S->hasExplicitTemplateArgs());
804  if (S->hasExplicitTemplateArgs())
805    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
806}
807
808void StmtProfiler::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *S) {
809  ID.AddBoolean(S->isImplicitAccess());
810  if (!S->isImplicitAccess()) {
811    VisitExpr(S);
812    ID.AddBoolean(S->isArrow());
813  }
814  VisitNestedNameSpecifier(S->getQualifier());
815  VisitName(S->getMemberName());
816  ID.AddBoolean(S->hasExplicitTemplateArgs());
817  if (S->hasExplicitTemplateArgs())
818    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
819}
820
821void StmtProfiler::VisitObjCStringLiteral(ObjCStringLiteral *S) {
822  VisitExpr(S);
823}
824
825void StmtProfiler::VisitObjCEncodeExpr(ObjCEncodeExpr *S) {
826  VisitExpr(S);
827  VisitType(S->getEncodedType());
828}
829
830void StmtProfiler::VisitObjCSelectorExpr(ObjCSelectorExpr *S) {
831  VisitExpr(S);
832  VisitName(S->getSelector());
833}
834
835void StmtProfiler::VisitObjCProtocolExpr(ObjCProtocolExpr *S) {
836  VisitExpr(S);
837  VisitDecl(S->getProtocol());
838}
839
840void StmtProfiler::VisitObjCIvarRefExpr(ObjCIvarRefExpr *S) {
841  VisitExpr(S);
842  VisitDecl(S->getDecl());
843  ID.AddBoolean(S->isArrow());
844  ID.AddBoolean(S->isFreeIvar());
845}
846
847void StmtProfiler::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *S) {
848  VisitExpr(S);
849  VisitDecl(S->getProperty());
850}
851
852void StmtProfiler::VisitObjCImplicitSetterGetterRefExpr(
853                                  ObjCImplicitSetterGetterRefExpr *S) {
854  VisitExpr(S);
855  VisitDecl(S->getGetterMethod());
856  VisitDecl(S->getSetterMethod());
857  VisitDecl(S->getInterfaceDecl());
858}
859
860void StmtProfiler::VisitObjCMessageExpr(ObjCMessageExpr *S) {
861  VisitExpr(S);
862  VisitName(S->getSelector());
863  VisitDecl(S->getMethodDecl());
864}
865
866void StmtProfiler::VisitObjCSuperExpr(ObjCSuperExpr *S) {
867  VisitExpr(S);
868}
869
870void StmtProfiler::VisitObjCIsaExpr(ObjCIsaExpr *S) {
871  VisitExpr(S);
872  ID.AddBoolean(S->isArrow());
873}
874
875void StmtProfiler::VisitDecl(Decl *D) {
876  ID.AddInteger(D? D->getKind() : 0);
877
878  if (Canonical && D) {
879    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
880      ID.AddInteger(NTTP->getDepth());
881      ID.AddInteger(NTTP->getIndex());
882      VisitType(NTTP->getType());
883      return;
884    }
885
886    if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
887      // The Itanium C++ ABI uses the type of a parameter when mangling
888      // expressions that involve function parameters, so we will use the
889      // parameter's type for establishing function parameter identity. That
890      // way, our definition of "equivalent" (per C++ [temp.over.link])
891      // matches the definition of "equivalent" used for name mangling.
892      VisitType(Parm->getType());
893      return;
894    }
895
896    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
897      ID.AddInteger(TTP->getDepth());
898      ID.AddInteger(TTP->getIndex());
899      return;
900    }
901  }
902
903  ID.AddPointer(D? D->getCanonicalDecl() : 0);
904}
905
906void StmtProfiler::VisitType(QualType T) {
907  if (Canonical)
908    T = Context.getCanonicalType(T);
909
910  ID.AddPointer(T.getAsOpaquePtr());
911}
912
913void StmtProfiler::VisitName(DeclarationName Name) {
914  ID.AddPointer(Name.getAsOpaquePtr());
915}
916
917void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) {
918  if (Canonical)
919    NNS = Context.getCanonicalNestedNameSpecifier(NNS);
920  ID.AddPointer(NNS);
921}
922
923void StmtProfiler::VisitTemplateName(TemplateName Name) {
924  if (Canonical)
925    Name = Context.getCanonicalTemplateName(Name);
926
927  Name.Profile(ID);
928}
929
930void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args,
931                                          unsigned NumArgs) {
932  ID.AddInteger(NumArgs);
933  for (unsigned I = 0; I != NumArgs; ++I)
934    VisitTemplateArgument(Args[I].getArgument());
935}
936
937void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) {
938  // Mostly repetitive with TemplateArgument::Profile!
939  ID.AddInteger(Arg.getKind());
940  switch (Arg.getKind()) {
941  case TemplateArgument::Null:
942    break;
943
944  case TemplateArgument::Type:
945    VisitType(Arg.getAsType());
946    break;
947
948  case TemplateArgument::Template:
949    VisitTemplateName(Arg.getAsTemplate());
950    break;
951
952  case TemplateArgument::Declaration:
953    VisitDecl(Arg.getAsDecl());
954    break;
955
956  case TemplateArgument::Integral:
957    Arg.getAsIntegral()->Profile(ID);
958    VisitType(Arg.getIntegralType());
959    break;
960
961  case TemplateArgument::Expression:
962    Visit(Arg.getAsExpr());
963    break;
964
965  case TemplateArgument::Pack:
966    const TemplateArgument *Pack = Arg.pack_begin();
967    for (unsigned i = 0, e = Arg.pack_size(); i != e; ++i)
968      VisitTemplateArgument(Pack[i]);
969    break;
970  }
971}
972
973void Stmt::Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context,
974                   bool Canonical) {
975  StmtProfiler Profiler(ID, Context, Canonical);
976  Profiler.Visit(this);
977}
978