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