1 //===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===//
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 defines the EvaluatedExprVisitor class template, which visits
11 //  the potentially-evaluated subexpressions of a potentially-evaluated
12 //  expression.
13 //
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
16 #define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
17 
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/StmtVisitor.h"
22 
23 namespace clang {
24 
25 class ASTContext;
26 
27 /// \brief Given a potentially-evaluated expression, this visitor visits all
28 /// of its potentially-evaluated subexpressions, recursively.
29 template<typename ImplClass>
30 class EvaluatedExprVisitor : public StmtVisitor<ImplClass> {
31   ASTContext &Context;
32 
33 public:
EvaluatedExprVisitor(ASTContext & Context)34   explicit EvaluatedExprVisitor(ASTContext &Context) : Context(Context) { }
35 
36   // Expressions that have no potentially-evaluated subexpressions (but may have
37   // other sub-expressions).
VisitDeclRefExpr(DeclRefExpr * E)38   void VisitDeclRefExpr(DeclRefExpr *E) { }
VisitOffsetOfExpr(OffsetOfExpr * E)39   void VisitOffsetOfExpr(OffsetOfExpr *E) { }
VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr * E)40   void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { }
VisitExpressionTraitExpr(ExpressionTraitExpr * E)41   void VisitExpressionTraitExpr(ExpressionTraitExpr *E) { }
VisitBlockExpr(BlockExpr * E)42   void VisitBlockExpr(BlockExpr *E) { }
VisitCXXUuidofExpr(CXXUuidofExpr * E)43   void VisitCXXUuidofExpr(CXXUuidofExpr *E) { }
VisitCXXNoexceptExpr(CXXNoexceptExpr * E)44   void VisitCXXNoexceptExpr(CXXNoexceptExpr *E) { }
45 
VisitMemberExpr(MemberExpr * E)46   void VisitMemberExpr(MemberExpr *E) {
47     // Only the base matters.
48     return this->Visit(E->getBase());
49   }
50 
VisitChooseExpr(ChooseExpr * E)51   void VisitChooseExpr(ChooseExpr *E) {
52     // Don't visit either child expression if the condition is dependent.
53     if (E->getCond()->isValueDependent())
54       return;
55     // Only the selected subexpression matters; the other one is not evaluated.
56     return this->Visit(E->getChosenSubExpr());
57   }
58 
VisitGenericSelectionExpr(GenericSelectionExpr * E)59   void VisitGenericSelectionExpr(GenericSelectionExpr *E) {
60     // The controlling expression of a generic selection is not evaluated.
61 
62     // Don't visit either child expression if the condition is type-dependent.
63     if (E->isResultDependent())
64       return;
65     // Only the selected subexpression matters; the other subexpressions and the
66     // controlling expression are not evaluated.
67     return this->Visit(E->getResultExpr());
68   }
69 
VisitDesignatedInitExpr(DesignatedInitExpr * E)70   void VisitDesignatedInitExpr(DesignatedInitExpr *E) {
71     // Only the actual initializer matters; the designators are all constant
72     // expressions.
73     return this->Visit(E->getInit());
74   }
75 
VisitCXXTypeidExpr(CXXTypeidExpr * E)76   void VisitCXXTypeidExpr(CXXTypeidExpr *E) {
77     if (E->isPotentiallyEvaluated())
78       return this->Visit(E->getExprOperand());
79   }
80 
VisitCallExpr(CallExpr * CE)81   void VisitCallExpr(CallExpr *CE) {
82     if (!CE->isUnevaluatedBuiltinCall(Context))
83       return static_cast<ImplClass*>(this)->VisitExpr(CE);
84   }
85 
VisitLambdaExpr(LambdaExpr * LE)86   void VisitLambdaExpr(LambdaExpr *LE) {
87     // Only visit the capture initializers, and not the body.
88     for (LambdaExpr::capture_init_iterator I = LE->capture_init_begin(),
89                                            E = LE->capture_init_end();
90          I != E; ++I)
91       if (*I)
92         this->Visit(*I);
93   }
94 
95   /// \brief The basis case walks all of the children of the statement or
96   /// expression, assuming they are all potentially evaluated.
VisitStmt(Stmt * S)97   void VisitStmt(Stmt *S) {
98     for (Stmt::child_range C = S->children(); C; ++C)
99       if (*C)
100         this->Visit(*C);
101   }
102 };
103 
104 }
105 
106 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
107