StmtProfile.cpp revision 200583
180709Sjake//===---- StmtProfile.cpp - Profile implementation for Stmt ASTs ----------===//
280709Sjake//
380709Sjake//                     The LLVM Compiler Infrastructure
480709Sjake//
580709Sjake// This file is distributed under the University of Illinois Open Source
680709Sjake// License. See LICENSE.TXT for details.
780709Sjake//
880709Sjake//===----------------------------------------------------------------------===//
980709Sjake//
1080709Sjake// This file implements the Stmt::Profile method, which builds a unique bit
1180709Sjake// representation that identifies a statement/expression.
1280709Sjake//
1380709Sjake//===----------------------------------------------------------------------===//
1481334Sobrien#include "clang/AST/ASTContext.h"
1580709Sjake#include "clang/AST/DeclCXX.h"
1680709Sjake#include "clang/AST/DeclObjC.h"
1781334Sobrien#include "clang/AST/DeclTemplate.h"
1880709Sjake#include "clang/AST/Expr.h"
1980709Sjake#include "clang/AST/ExprCXX.h"
2080709Sjake#include "clang/AST/ExprObjC.h"
2180709Sjake#include "clang/AST/StmtVisitor.h"
2280709Sjake#include "llvm/ADT/FoldingSet.h"
2380709Sjakeusing namespace clang;
2480709Sjake
2580709Sjakenamespace {
2680709Sjake  class StmtProfiler : public StmtVisitor<StmtProfiler> {
2780709Sjake    llvm::FoldingSetNodeID &ID;
2880709Sjake    ASTContext &Context;
2980709Sjake    bool Canonical;
3080709Sjake
3180709Sjake  public:
3280709Sjake    StmtProfiler(llvm::FoldingSetNodeID &ID, ASTContext &Context,
3380709Sjake                 bool Canonical)
3491224Sjake      : ID(ID), Context(Context), Canonical(Canonical) { }
3580709Sjake
3680709Sjake    void VisitStmt(Stmt *S);
3780709Sjake
3880709Sjake#define STMT(Node, Base) void Visit##Node(Node *S);
3980709Sjake#include "clang/AST/StmtNodes.def"
4091224Sjake
4191224Sjake    /// \brief Visit a declaration that is referenced within an expression
4280709Sjake    /// or statement.
4391224Sjake    void VisitDecl(Decl *D);
4491224Sjake
4591224Sjake    /// \brief Visit a type that is referenced within an expression or
4680709Sjake    /// statement.
4780709Sjake    void VisitType(QualType T);
4880709Sjake
4980709Sjake    /// \brief Visit a name that occurs within an expression or statement.
5080709Sjake    void VisitName(DeclarationName Name);
5180709Sjake
5280709Sjake    /// \brief Visit a nested-name-specifier that occurs within an expression
5380709Sjake    /// or statement.
5480709Sjake    void VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
5580709Sjake
5680709Sjake    /// \brief Visit a template name that occurs within an expression or
5780709Sjake    /// statement.
5880709Sjake    void VisitTemplateName(TemplateName Name);
5980709Sjake
6080709Sjake    /// \brief Visit template arguments that occur within an expression or
6180709Sjake    /// statement.
6280709Sjake    void VisitTemplateArguments(const TemplateArgumentLoc *Args, unsigned NumArgs);
6380709Sjake
6480709Sjake    /// \brief Visit a single template argument.
6580709Sjake    void VisitTemplateArgument(const TemplateArgument &Arg);
6680709Sjake  };
6791613Sjake}
6891613Sjake
6980709Sjakevoid StmtProfiler::VisitStmt(Stmt *S) {
7080709Sjake  ID.AddInteger(S->getStmtClass());
7180709Sjake  for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
7280709Sjake       C != CEnd; ++C)
7381176Sjake    Visit(*C);
7481176Sjake}
7581176Sjake
7681176Sjakevoid StmtProfiler::VisitDeclStmt(DeclStmt *S) {
7781176Sjake  VisitStmt(S);
7881176Sjake  for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
7981176Sjake       D != DEnd; ++D)
8081176Sjake    VisitDecl(*D);
8181176Sjake}
8281176Sjake
8381176Sjakevoid StmtProfiler::VisitNullStmt(NullStmt *S) {
8481176Sjake  VisitStmt(S);
8581176Sjake}
8681176Sjake
8781176Sjakevoid StmtProfiler::VisitCompoundStmt(CompoundStmt *S) {
8891616Sjake  VisitStmt(S);
8991616Sjake}
9091616Sjake
9188629Sjakevoid StmtProfiler::VisitSwitchCase(SwitchCase *S) {
9288629Sjake  VisitStmt(S);
9388629Sjake}
9488629Sjake
9588629Sjakevoid StmtProfiler::VisitCaseStmt(CaseStmt *S) {
9688629Sjake  VisitStmt(S);
9788629Sjake}
9888629Sjake
9980709Sjakevoid StmtProfiler::VisitDefaultStmt(DefaultStmt *S) {
10088629Sjake  VisitStmt(S);
10188629Sjake}
10288629Sjake
10388629Sjakevoid StmtProfiler::VisitLabelStmt(LabelStmt *S) {
10488629Sjake  VisitStmt(S);
10588629Sjake  VisitName(S->getID());
10688629Sjake}
10791782Sjake
10880709Sjakevoid StmtProfiler::VisitIfStmt(IfStmt *S) {
10991782Sjake  VisitStmt(S);
11091782Sjake  VisitDecl(S->getConditionVariable());
11191782Sjake}
11280709Sjake
11380709Sjakevoid StmtProfiler::VisitSwitchStmt(SwitchStmt *S) {
11480709Sjake  VisitStmt(S);
11580709Sjake  VisitDecl(S->getConditionVariable());
11691613Sjake}
11791613Sjake
11881377Sjakevoid StmtProfiler::VisitWhileStmt(WhileStmt *S) {
11988629Sjake  VisitStmt(S);
12081377Sjake  VisitDecl(S->getConditionVariable());
12180709Sjake}
12280709Sjake
12380709Sjakevoid StmtProfiler::VisitDoStmt(DoStmt *S) {
12481377Sjake  VisitStmt(S);
12580709Sjake}
12688629Sjake
12788629Sjakevoid StmtProfiler::VisitForStmt(ForStmt *S) {
12891170Sjake  VisitStmt(S);
12981176Sjake}
13081377Sjake
13180709Sjakevoid StmtProfiler::VisitGotoStmt(GotoStmt *S) {
13280709Sjake  VisitStmt(S);
13391170Sjake  VisitName(S->getLabel()->getID());
13480709Sjake}
13580709Sjake
13680709Sjakevoid StmtProfiler::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
13781377Sjake  VisitStmt(S);
13880709Sjake}
13988629Sjake
14088629Sjakevoid StmtProfiler::VisitContinueStmt(ContinueStmt *S) {
14191170Sjake  VisitStmt(S);
14281377Sjake}
14380709Sjake
14480709Sjakevoid StmtProfiler::VisitBreakStmt(BreakStmt *S) {
14591170Sjake  VisitStmt(S);
14680709Sjake}
14780709Sjake
14880709Sjakevoid StmtProfiler::VisitReturnStmt(ReturnStmt *S) {
14988629Sjake  VisitStmt(S);
15088629Sjake}
15188629Sjake
15288629Sjakevoid StmtProfiler::VisitAsmStmt(AsmStmt *S) {
15388629Sjake  VisitStmt(S);
15488629Sjake  ID.AddBoolean(S->isVolatile());
15588629Sjake  ID.AddBoolean(S->isSimple());
15691782Sjake  VisitStringLiteral(S->getAsmString());
15780709Sjake  ID.AddInteger(S->getNumOutputs());
15891782Sjake  for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
15991782Sjake    ID.AddString(S->getOutputName(I));
16091782Sjake    VisitStringLiteral(S->getOutputConstraintLiteral(I));
16180709Sjake  }
16280709Sjake  ID.AddInteger(S->getNumInputs());
16380709Sjake  for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
16480709Sjake    ID.AddString(S->getInputName(I));
16591613Sjake    VisitStringLiteral(S->getInputConstraintLiteral(I));
16691613Sjake  }
16781377Sjake  ID.AddInteger(S->getNumClobbers());
16888629Sjake  for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
16981377Sjake    VisitStringLiteral(S->getClobber(I));
17080709Sjake}
17180709Sjake
17280709Sjakevoid StmtProfiler::VisitCXXCatchStmt(CXXCatchStmt *S) {
17381377Sjake  VisitStmt(S);
17480709Sjake  VisitType(S->getCaughtType());
17588629Sjake}
17688629Sjake
17791170Sjakevoid StmtProfiler::VisitCXXTryStmt(CXXTryStmt *S) {
17881377Sjake  VisitStmt(S);
17981377Sjake}
18081377Sjake
18181377Sjakevoid StmtProfiler::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
18281377Sjake  VisitStmt(S);
18381377Sjake}
18481377Sjake
18581377Sjakevoid StmtProfiler::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
18681377Sjake  VisitStmt(S);
18781377Sjake  ID.AddBoolean(S->hasEllipsis());
18881377Sjake  if (S->getCatchParamDecl())
18991170Sjake    VisitType(S->getCatchParamDecl()->getType());
19080709Sjake}
19180709Sjake
19280709Sjakevoid StmtProfiler::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
19391782Sjake  VisitStmt(S);
19488629Sjake}
19591782Sjake
19691782Sjakevoid StmtProfiler::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
19791782Sjake  VisitStmt(S);
19891613Sjake}
19991613Sjake
20091613Sjakevoid StmtProfiler::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
20191613Sjake  VisitStmt(S);
20288629Sjake}
20388629Sjake
20488629Sjakevoid StmtProfiler::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
20581377Sjake  VisitStmt(S);
20680709Sjake}
20788629Sjake
20888629Sjakevoid StmtProfiler::VisitExpr(Expr *S) {
20991170Sjake  VisitStmt(S);
21081377Sjake}
21180709Sjake
21280709Sjakevoid StmtProfiler::VisitDeclRefExpr(DeclRefExpr *S) {
21391170Sjake  VisitExpr(S);
21480709Sjake  VisitNestedNameSpecifier(S->getQualifier());
21580709Sjake  VisitDecl(S->getDecl());
21680709Sjake  VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
21791782Sjake}
21880709Sjake
21980709Sjakevoid StmtProfiler::VisitPredefinedExpr(PredefinedExpr *S) {
22091782Sjake  VisitExpr(S);
22180709Sjake  ID.AddInteger(S->getIdentType());
22291782Sjake}
22380709Sjake
22480709Sjakevoid StmtProfiler::VisitIntegerLiteral(IntegerLiteral *S) {
22580709Sjake  VisitExpr(S);
22691782Sjake  S->getValue().Profile(ID);
22791172Sjake}
22891172Sjake
22991782Sjakevoid StmtProfiler::VisitCharacterLiteral(CharacterLiteral *S) {
23091172Sjake  VisitExpr(S);
23191172Sjake  ID.AddBoolean(S->isWide());
23291172Sjake  ID.AddInteger(S->getValue());
23391782Sjake}
23491172Sjake
23591782Sjakevoid StmtProfiler::VisitFloatingLiteral(FloatingLiteral *S) {
23691172Sjake  VisitExpr(S);
23791172Sjake  S->getValue().Profile(ID);
23891172Sjake  ID.AddBoolean(S->isExact());
23981377Sjake}
24080709Sjake
24191613Sjakevoid StmtProfiler::VisitImaginaryLiteral(ImaginaryLiteral *S) {
24280709Sjake  VisitExpr(S);
24381377Sjake}
24480709Sjake
24581377Sjakevoid StmtProfiler::VisitStringLiteral(StringLiteral *S) {
24680709Sjake  VisitExpr(S);
24780709Sjake  ID.AddString(S->getString());
24880709Sjake  ID.AddBoolean(S->isWide());
24981377Sjake}
25080709Sjake
25191613Sjakevoid StmtProfiler::VisitParenExpr(ParenExpr *S) {
25280709Sjake  VisitExpr(S);
25381377Sjake}
25480709Sjake
25581377Sjakevoid StmtProfiler::VisitParenListExpr(ParenListExpr *S) {
25680709Sjake  VisitExpr(S);
25780709Sjake}
25880709Sjake
259void StmtProfiler::VisitUnaryOperator(UnaryOperator *S) {
260  VisitExpr(S);
261  ID.AddInteger(S->getOpcode());
262}
263
264void StmtProfiler::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *S) {
265  VisitExpr(S);
266  ID.AddBoolean(S->isSizeOf());
267  if (S->isArgumentType())
268    VisitType(S->getArgumentType());
269}
270
271void StmtProfiler::VisitArraySubscriptExpr(ArraySubscriptExpr *S) {
272  VisitExpr(S);
273}
274
275void StmtProfiler::VisitCallExpr(CallExpr *S) {
276  VisitExpr(S);
277}
278
279void StmtProfiler::VisitMemberExpr(MemberExpr *S) {
280  VisitExpr(S);
281  VisitDecl(S->getMemberDecl());
282  VisitNestedNameSpecifier(S->getQualifier());
283  ID.AddBoolean(S->isArrow());
284}
285
286void StmtProfiler::VisitCompoundLiteralExpr(CompoundLiteralExpr *S) {
287  VisitExpr(S);
288  ID.AddBoolean(S->isFileScope());
289}
290
291void StmtProfiler::VisitCastExpr(CastExpr *S) {
292  VisitExpr(S);
293}
294
295void StmtProfiler::VisitImplicitCastExpr(ImplicitCastExpr *S) {
296  VisitCastExpr(S);
297  ID.AddBoolean(S->isLvalueCast());
298}
299
300void StmtProfiler::VisitExplicitCastExpr(ExplicitCastExpr *S) {
301  VisitCastExpr(S);
302  VisitType(S->getTypeAsWritten());
303}
304
305void StmtProfiler::VisitCStyleCastExpr(CStyleCastExpr *S) {
306  VisitExplicitCastExpr(S);
307}
308
309void StmtProfiler::VisitBinaryOperator(BinaryOperator *S) {
310  VisitExpr(S);
311  ID.AddInteger(S->getOpcode());
312}
313
314void StmtProfiler::VisitCompoundAssignOperator(CompoundAssignOperator *S) {
315  VisitBinaryOperator(S);
316}
317
318void StmtProfiler::VisitConditionalOperator(ConditionalOperator *S) {
319  VisitExpr(S);
320}
321
322void StmtProfiler::VisitAddrLabelExpr(AddrLabelExpr *S) {
323  VisitExpr(S);
324  VisitName(S->getLabel()->getID());
325}
326
327void StmtProfiler::VisitStmtExpr(StmtExpr *S) {
328  VisitExpr(S);
329}
330
331void StmtProfiler::VisitTypesCompatibleExpr(TypesCompatibleExpr *S) {
332  VisitExpr(S);
333  VisitType(S->getArgType1());
334  VisitType(S->getArgType2());
335}
336
337void StmtProfiler::VisitShuffleVectorExpr(ShuffleVectorExpr *S) {
338  VisitExpr(S);
339}
340
341void StmtProfiler::VisitChooseExpr(ChooseExpr *S) {
342  VisitExpr(S);
343}
344
345void StmtProfiler::VisitGNUNullExpr(GNUNullExpr *S) {
346  VisitExpr(S);
347}
348
349void StmtProfiler::VisitVAArgExpr(VAArgExpr *S) {
350  VisitExpr(S);
351}
352
353void StmtProfiler::VisitInitListExpr(InitListExpr *S) {
354  if (S->getSyntacticForm()) {
355    VisitInitListExpr(S->getSyntacticForm());
356    return;
357  }
358
359  VisitExpr(S);
360}
361
362void StmtProfiler::VisitDesignatedInitExpr(DesignatedInitExpr *S) {
363  VisitExpr(S);
364  ID.AddBoolean(S->usesGNUSyntax());
365  for (DesignatedInitExpr::designators_iterator D = S->designators_begin(),
366                                             DEnd = S->designators_end();
367       D != DEnd; ++D) {
368    if (D->isFieldDesignator()) {
369      ID.AddInteger(0);
370      VisitName(D->getFieldName());
371      continue;
372    }
373
374    if (D->isArrayDesignator()) {
375      ID.AddInteger(1);
376    } else {
377      assert(D->isArrayRangeDesignator());
378      ID.AddInteger(2);
379    }
380    ID.AddInteger(D->getFirstExprIndex());
381  }
382}
383
384void StmtProfiler::VisitImplicitValueInitExpr(ImplicitValueInitExpr *S) {
385  VisitExpr(S);
386}
387
388void StmtProfiler::VisitExtVectorElementExpr(ExtVectorElementExpr *S) {
389  VisitExpr(S);
390  VisitName(&S->getAccessor());
391}
392
393void StmtProfiler::VisitBlockExpr(BlockExpr *S) {
394  VisitExpr(S);
395  VisitDecl(S->getBlockDecl());
396}
397
398void StmtProfiler::VisitBlockDeclRefExpr(BlockDeclRefExpr *S) {
399  VisitExpr(S);
400  VisitDecl(S->getDecl());
401  ID.AddBoolean(S->isByRef());
402  ID.AddBoolean(S->isConstQualAdded());
403}
404
405void StmtProfiler::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *S) {
406  VisitCallExpr(S);
407  ID.AddInteger(S->getOperator());
408}
409
410void StmtProfiler::VisitCXXMemberCallExpr(CXXMemberCallExpr *S) {
411  VisitCallExpr(S);
412}
413
414void StmtProfiler::VisitCXXNamedCastExpr(CXXNamedCastExpr *S) {
415  VisitExplicitCastExpr(S);
416}
417
418void StmtProfiler::VisitCXXStaticCastExpr(CXXStaticCastExpr *S) {
419  VisitCXXNamedCastExpr(S);
420}
421
422void StmtProfiler::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *S) {
423  VisitCXXNamedCastExpr(S);
424}
425
426void StmtProfiler::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *S) {
427  VisitCXXNamedCastExpr(S);
428}
429
430void StmtProfiler::VisitCXXConstCastExpr(CXXConstCastExpr *S) {
431  VisitCXXNamedCastExpr(S);
432}
433
434void StmtProfiler::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *S) {
435  VisitExpr(S);
436  ID.AddBoolean(S->getValue());
437}
438
439void StmtProfiler::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *S) {
440  VisitExpr(S);
441}
442
443void StmtProfiler::VisitCXXTypeidExpr(CXXTypeidExpr *S) {
444  VisitExpr(S);
445  if (S->isTypeOperand())
446    VisitType(S->getTypeOperand());
447}
448
449void StmtProfiler::VisitCXXThisExpr(CXXThisExpr *S) {
450  VisitExpr(S);
451}
452
453void StmtProfiler::VisitCXXThrowExpr(CXXThrowExpr *S) {
454  VisitExpr(S);
455}
456
457void StmtProfiler::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *S) {
458  VisitExpr(S);
459  VisitDecl(S->getParam());
460}
461
462void StmtProfiler::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *S) {
463  VisitExpr(S);
464  VisitDecl(
465         const_cast<CXXDestructorDecl *>(S->getTemporary()->getDestructor()));
466}
467
468void StmtProfiler::VisitCXXConstructExpr(CXXConstructExpr *S) {
469  VisitExpr(S);
470  VisitDecl(S->getConstructor());
471  ID.AddBoolean(S->isElidable());
472}
473
474void StmtProfiler::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *S) {
475  VisitExplicitCastExpr(S);
476}
477
478void StmtProfiler::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *S) {
479  VisitCXXConstructExpr(S);
480}
481
482void StmtProfiler::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *S) {
483  VisitExpr(S);
484}
485
486void StmtProfiler::VisitCXXDeleteExpr(CXXDeleteExpr *S) {
487  VisitExpr(S);
488  ID.AddBoolean(S->isGlobalDelete());
489  ID.AddBoolean(S->isArrayForm());
490  VisitDecl(S->getOperatorDelete());
491}
492
493
494void StmtProfiler::VisitCXXNewExpr(CXXNewExpr *S) {
495  VisitExpr(S);
496  VisitType(S->getAllocatedType());
497  VisitDecl(S->getOperatorNew());
498  VisitDecl(S->getOperatorDelete());
499  VisitDecl(S->getConstructor());
500  ID.AddBoolean(S->isArray());
501  ID.AddInteger(S->getNumPlacementArgs());
502  ID.AddBoolean(S->isGlobalNew());
503  ID.AddBoolean(S->isParenTypeId());
504  ID.AddBoolean(S->hasInitializer());
505  ID.AddInteger(S->getNumConstructorArgs());
506}
507
508void StmtProfiler::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *S) {
509  VisitExpr(S);
510  ID.AddBoolean(S->isArrow());
511  VisitNestedNameSpecifier(S->getQualifier());
512  VisitType(S->getDestroyedType());
513}
514
515void
516StmtProfiler::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *S) {
517  VisitExpr(S);
518  VisitNestedNameSpecifier(S->getQualifier());
519  VisitName(S->getName());
520  ID.AddBoolean(S->hasExplicitTemplateArgs());
521  if (S->hasExplicitTemplateArgs())
522    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
523}
524
525void StmtProfiler::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *S) {
526  VisitExpr(S);
527  ID.AddInteger(S->getTrait());
528  VisitType(S->getQueriedType());
529}
530
531void
532StmtProfiler::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *S) {
533  VisitExpr(S);
534  VisitName(S->getDeclName());
535  VisitNestedNameSpecifier(S->getQualifier());
536  ID.AddBoolean(S->hasExplicitTemplateArgs());
537  if (S->hasExplicitTemplateArgs())
538    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
539}
540
541void StmtProfiler::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *S) {
542  VisitExpr(S);
543  ID.AddBoolean(S->shouldDestroyTemporaries());
544  for (unsigned I = 0, N = S->getNumTemporaries(); I != N; ++I)
545    VisitDecl(
546      const_cast<CXXDestructorDecl *>(S->getTemporary(I)->getDestructor()));
547}
548
549void
550StmtProfiler::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *S) {
551  VisitExpr(S);
552  VisitType(S->getTypeAsWritten());
553}
554
555void
556StmtProfiler::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *S) {
557  ID.AddBoolean(S->isImplicitAccess());
558  if (!S->isImplicitAccess()) {
559    VisitExpr(S);
560    ID.AddBoolean(S->isArrow());
561  }
562  VisitNestedNameSpecifier(S->getQualifier());
563  VisitName(S->getMember());
564  ID.AddBoolean(S->hasExplicitTemplateArgs());
565  if (S->hasExplicitTemplateArgs())
566    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
567}
568
569void StmtProfiler::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *S) {
570  ID.AddBoolean(S->isImplicitAccess());
571  if (!S->isImplicitAccess()) {
572    VisitExpr(S);
573    ID.AddBoolean(S->isArrow());
574  }
575  VisitNestedNameSpecifier(S->getQualifier());
576  VisitName(S->getMemberName());
577  ID.AddBoolean(S->hasExplicitTemplateArgs());
578  if (S->hasExplicitTemplateArgs())
579    VisitTemplateArguments(S->getTemplateArgs(), S->getNumTemplateArgs());
580}
581
582void StmtProfiler::VisitObjCStringLiteral(ObjCStringLiteral *S) {
583  VisitExpr(S);
584}
585
586void StmtProfiler::VisitObjCEncodeExpr(ObjCEncodeExpr *S) {
587  VisitExpr(S);
588  VisitType(S->getEncodedType());
589}
590
591void StmtProfiler::VisitObjCSelectorExpr(ObjCSelectorExpr *S) {
592  VisitExpr(S);
593  VisitName(S->getSelector());
594}
595
596void StmtProfiler::VisitObjCProtocolExpr(ObjCProtocolExpr *S) {
597  VisitExpr(S);
598  VisitDecl(S->getProtocol());
599}
600
601void StmtProfiler::VisitObjCIvarRefExpr(ObjCIvarRefExpr *S) {
602  VisitExpr(S);
603  VisitDecl(S->getDecl());
604  ID.AddBoolean(S->isArrow());
605  ID.AddBoolean(S->isFreeIvar());
606}
607
608void StmtProfiler::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *S) {
609  VisitExpr(S);
610  VisitDecl(S->getProperty());
611}
612
613void StmtProfiler::VisitObjCImplicitSetterGetterRefExpr(
614                                  ObjCImplicitSetterGetterRefExpr *S) {
615  VisitExpr(S);
616  VisitDecl(S->getGetterMethod());
617  VisitDecl(S->getSetterMethod());
618  VisitDecl(S->getInterfaceDecl());
619}
620
621void StmtProfiler::VisitObjCMessageExpr(ObjCMessageExpr *S) {
622  VisitExpr(S);
623  VisitName(S->getSelector());
624  VisitDecl(S->getMethodDecl());
625}
626
627void StmtProfiler::VisitObjCSuperExpr(ObjCSuperExpr *S) {
628  VisitExpr(S);
629}
630
631void StmtProfiler::VisitObjCIsaExpr(ObjCIsaExpr *S) {
632  VisitExpr(S);
633  ID.AddBoolean(S->isArrow());
634}
635
636void StmtProfiler::VisitDecl(Decl *D) {
637  ID.AddInteger(D? D->getKind() : 0);
638
639  if (Canonical && D) {
640    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
641      ID.AddInteger(NTTP->getDepth());
642      ID.AddInteger(NTTP->getIndex());
643      VisitType(NTTP->getType());
644      return;
645    }
646
647    if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
648      // The Itanium C++ ABI uses the type of a parameter when mangling
649      // expressions that involve function parameters, so we will use the
650      // parameter's type for establishing function parameter identity. That
651      // way, our definition of "equivalent" (per C++ [temp.over.link])
652      // matches the definition of "equivalent" used for name mangling.
653      VisitType(Parm->getType());
654      return;
655    }
656
657    if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
658      ID.AddInteger(TTP->getDepth());
659      ID.AddInteger(TTP->getIndex());
660      return;
661    }
662  }
663
664  ID.AddPointer(D? D->getCanonicalDecl() : 0);
665}
666
667void StmtProfiler::VisitType(QualType T) {
668  if (Canonical)
669    T = Context.getCanonicalType(T);
670
671  ID.AddPointer(T.getAsOpaquePtr());
672}
673
674void StmtProfiler::VisitName(DeclarationName Name) {
675  ID.AddPointer(Name.getAsOpaquePtr());
676}
677
678void StmtProfiler::VisitNestedNameSpecifier(NestedNameSpecifier *NNS) {
679  if (Canonical)
680    NNS = Context.getCanonicalNestedNameSpecifier(NNS);
681  ID.AddPointer(NNS);
682}
683
684void StmtProfiler::VisitTemplateName(TemplateName Name) {
685  if (Canonical)
686    Name = Context.getCanonicalTemplateName(Name);
687
688  Name.Profile(ID);
689}
690
691void StmtProfiler::VisitTemplateArguments(const TemplateArgumentLoc *Args,
692                                          unsigned NumArgs) {
693  ID.AddInteger(NumArgs);
694  for (unsigned I = 0; I != NumArgs; ++I)
695    VisitTemplateArgument(Args[I].getArgument());
696}
697
698void StmtProfiler::VisitTemplateArgument(const TemplateArgument &Arg) {
699  // Mostly repetitive with TemplateArgument::Profile!
700  ID.AddInteger(Arg.getKind());
701  switch (Arg.getKind()) {
702  case TemplateArgument::Null:
703    break;
704
705  case TemplateArgument::Type:
706    VisitType(Arg.getAsType());
707    break;
708
709  case TemplateArgument::Template:
710    VisitTemplateName(Arg.getAsTemplate());
711    break;
712
713  case TemplateArgument::Declaration:
714    VisitDecl(Arg.getAsDecl());
715    break;
716
717  case TemplateArgument::Integral:
718    Arg.getAsIntegral()->Profile(ID);
719    VisitType(Arg.getIntegralType());
720    break;
721
722  case TemplateArgument::Expression:
723    Visit(Arg.getAsExpr());
724    break;
725
726  case TemplateArgument::Pack:
727    const TemplateArgument *Pack = Arg.pack_begin();
728    for (unsigned i = 0, e = Arg.pack_size(); i != e; ++i)
729      VisitTemplateArgument(Pack[i]);
730    break;
731  }
732}
733
734void Stmt::Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context,
735                   bool Canonical) {
736  StmtProfiler Profiler(ID, Context, Canonical);
737  Profiler.Visit(this);
738}
739