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