1 //===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the EvaluatedExprVisitor class template, which visits
11 // the potentially-evaluated subexpressions of a potentially-evaluated
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
16 #define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
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 #include "llvm/ADT/STLExtras.h"
28 /// Given a potentially-evaluated expression, this visitor visits all
29 /// of its potentially-evaluated subexpressions, recursively.
30 template<template <typename> class Ptr, typename ImplClass>
31 class EvaluatedExprVisitorBase : public StmtVisitorBase<Ptr, ImplClass, void> {
33 const ASTContext &Context;
36 #define PTR(CLASS) typename Ptr<CLASS>::type
38 explicit EvaluatedExprVisitorBase(const ASTContext &Context) : Context(Context) { }
40 // Expressions that have no potentially-evaluated subexpressions (but may have
41 // other sub-expressions).
42 void VisitDeclRefExpr(PTR(DeclRefExpr) E) { }
43 void VisitOffsetOfExpr(PTR(OffsetOfExpr) E) { }
44 void VisitUnaryExprOrTypeTraitExpr(PTR(UnaryExprOrTypeTraitExpr) E) { }
45 void VisitExpressionTraitExpr(PTR(ExpressionTraitExpr) E) { }
46 void VisitBlockExpr(PTR(BlockExpr) E) { }
47 void VisitCXXUuidofExpr(PTR(CXXUuidofExpr) E) { }
48 void VisitCXXNoexceptExpr(PTR(CXXNoexceptExpr) E) { }
50 void VisitMemberExpr(PTR(MemberExpr) E) {
51 // Only the base matters.
52 return this->Visit(E->getBase());
55 void VisitChooseExpr(PTR(ChooseExpr) E) {
56 // Don't visit either child expression if the condition is dependent.
57 if (E->getCond()->isValueDependent())
59 // Only the selected subexpression matters; the other one is not evaluated.
60 return this->Visit(E->getChosenSubExpr());
63 void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E) {
64 // The controlling expression of a generic selection is not evaluated.
66 // Don't visit either child expression if the condition is type-dependent.
67 if (E->isResultDependent())
69 // Only the selected subexpression matters; the other subexpressions and the
70 // controlling expression are not evaluated.
71 return this->Visit(E->getResultExpr());
74 void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E) {
75 // Only the actual initializer matters; the designators are all constant
77 return this->Visit(E->getInit());
80 void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E) {
81 if (E->isPotentiallyEvaluated())
82 return this->Visit(E->getExprOperand());
85 void VisitCallExpr(PTR(CallExpr) CE) {
86 if (!CE->isUnevaluatedBuiltinCall(Context))
87 return static_cast<ImplClass*>(this)->VisitExpr(CE);
90 void VisitLambdaExpr(PTR(LambdaExpr) LE) {
91 // Only visit the capture initializers, and not the body.
92 for (LambdaExpr::const_capture_init_iterator I = LE->capture_init_begin(),
93 E = LE->capture_init_end();
99 /// The basis case walks all of the children of the statement or
100 /// expression, assuming they are all potentially evaluated.
101 void VisitStmt(PTR(Stmt) S) {
102 for (auto *SubStmt : S->children())
104 this->Visit(SubStmt);
110 /// EvaluatedExprVisitor - This class visits 'Expr *'s
111 template <typename ImplClass>
112 class EvaluatedExprVisitor
113 : public EvaluatedExprVisitorBase<std::add_pointer, ImplClass> {
115 explicit EvaluatedExprVisitor(const ASTContext &Context)
116 : EvaluatedExprVisitorBase<std::add_pointer, ImplClass>(Context) {}
119 /// ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
120 template <typename ImplClass>
121 class ConstEvaluatedExprVisitor
122 : public EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass> {
124 explicit ConstEvaluatedExprVisitor(const ASTContext &Context)
125 : EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass>(Context) {}
129 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H