1 //===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the EvaluatedExprVisitor class template, which visits
10 // the potentially-evaluated subexpressions of a potentially-evaluated
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
15 #define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "llvm/ADT/STLExtras.h"
27 /// Given a potentially-evaluated expression, this visitor visits all
28 /// of its potentially-evaluated subexpressions, recursively.
29 template<template <typename> class Ptr, typename ImplClass>
30 class EvaluatedExprVisitorBase : public StmtVisitorBase<Ptr, ImplClass, void> {
32 const ASTContext &Context;
35 // Return whether this visitor should recurse into discarded statements for a
37 bool shouldVisitDiscardedStmt() const { return true; }
38 #define PTR(CLASS) typename Ptr<CLASS>::type
40 explicit EvaluatedExprVisitorBase(const ASTContext &Context) : Context(Context) { }
42 // Expressions that have no potentially-evaluated subexpressions (but may have
43 // other sub-expressions).
44 void VisitDeclRefExpr(PTR(DeclRefExpr) E) { }
45 void VisitOffsetOfExpr(PTR(OffsetOfExpr) E) { }
46 void VisitUnaryExprOrTypeTraitExpr(PTR(UnaryExprOrTypeTraitExpr) E) { }
47 void VisitExpressionTraitExpr(PTR(ExpressionTraitExpr) E) { }
48 void VisitBlockExpr(PTR(BlockExpr) E) { }
49 void VisitCXXUuidofExpr(PTR(CXXUuidofExpr) E) { }
50 void VisitCXXNoexceptExpr(PTR(CXXNoexceptExpr) E) { }
52 void VisitMemberExpr(PTR(MemberExpr) E) {
53 // Only the base matters.
54 return this->Visit(E->getBase());
57 void VisitChooseExpr(PTR(ChooseExpr) E) {
58 // Don't visit either child expression if the condition is dependent.
59 if (E->getCond()->isValueDependent())
61 // Only the selected subexpression matters; the other one is not evaluated.
62 return this->Visit(E->getChosenSubExpr());
65 void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E) {
66 // The controlling expression of a generic selection is not evaluated.
68 // Don't visit either child expression if the condition is type-dependent.
69 if (E->isResultDependent())
71 // Only the selected subexpression matters; the other subexpressions and the
72 // controlling expression are not evaluated.
73 return this->Visit(E->getResultExpr());
76 void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E) {
77 // Only the actual initializer matters; the designators are all constant
79 return this->Visit(E->getInit());
82 void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E) {
83 if (E->isPotentiallyEvaluated())
84 return this->Visit(E->getExprOperand());
87 void VisitCallExpr(PTR(CallExpr) CE) {
88 if (!CE->isUnevaluatedBuiltinCall(Context))
89 return getDerived().VisitExpr(CE);
92 void VisitLambdaExpr(PTR(LambdaExpr) LE) {
93 // Only visit the capture initializers, and not the body.
94 for (LambdaExpr::const_capture_init_iterator I = LE->capture_init_begin(),
95 E = LE->capture_init_end();
101 /// The basis case walks all of the children of the statement or
102 /// expression, assuming they are all potentially evaluated.
103 void VisitStmt(PTR(Stmt) S) {
104 for (auto *SubStmt : S->children())
106 this->Visit(SubStmt);
109 void VisitIfStmt(PTR(IfStmt) If) {
110 if (!getDerived().shouldVisitDiscardedStmt()) {
111 if (auto SubStmt = If->getNondiscardedCase(Context)) {
113 this->Visit(*SubStmt);
118 getDerived().VisitStmt(If);
121 ImplClass &getDerived() { return *static_cast<ImplClass *>(this); }
126 /// EvaluatedExprVisitor - This class visits 'Expr *'s
127 template <typename ImplClass>
128 class EvaluatedExprVisitor
129 : public EvaluatedExprVisitorBase<std::add_pointer, ImplClass> {
131 explicit EvaluatedExprVisitor(const ASTContext &Context)
132 : EvaluatedExprVisitorBase<std::add_pointer, ImplClass>(Context) {}
135 /// ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
136 template <typename ImplClass>
137 class ConstEvaluatedExprVisitor
138 : public EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass> {
140 explicit ConstEvaluatedExprVisitor(const ASTContext &Context)
141 : EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass>(Context) {}
145 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H