clang  13.0.0git
EvaluatedExprVisitor.h
Go to the documentation of this file.
1 //===--- EvaluatedExprVisitor.h - Evaluated expression visitor --*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the EvaluatedExprVisitor class template, which visits
10 // the potentially-evaluated subexpressions of a potentially-evaluated
11 // expression.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
15 #define LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
16 
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"
22 
23 namespace clang {
24 
25 class ASTContext;
26 
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> {
31 protected:
33 
34 public:
35 #define PTR(CLASS) typename Ptr<CLASS>::type
36 
38 
39  // Expressions that have no potentially-evaluated subexpressions (but may have
40  // other sub-expressions).
48 
50  // Only the base matters.
51  return this->Visit(E->getBase());
52  }
53 
55  // Don't visit either child expression if the condition is dependent.
56  if (E->getCond()->isValueDependent())
57  return;
58  // Only the selected subexpression matters; the other one is not evaluated.
59  return this->Visit(E->getChosenSubExpr());
60  }
61 
63  // The controlling expression of a generic selection is not evaluated.
64 
65  // Don't visit either child expression if the condition is type-dependent.
66  if (E->isResultDependent())
67  return;
68  // Only the selected subexpression matters; the other subexpressions and the
69  // controlling expression are not evaluated.
70  return this->Visit(E->getResultExpr());
71  }
72 
74  // Only the actual initializer matters; the designators are all constant
75  // expressions.
76  return this->Visit(E->getInit());
77  }
78 
80  if (E->isPotentiallyEvaluated())
81  return this->Visit(E->getExprOperand());
82  }
83 
86  return static_cast<ImplClass*>(this)->VisitExpr(CE);
87  }
88 
90  // Only visit the capture initializers, and not the body.
91  for (LambdaExpr::const_capture_init_iterator I = LE->capture_init_begin(),
92  E = LE->capture_init_end();
93  I != E; ++I)
94  if (*I)
95  this->Visit(*I);
96  }
97 
98  /// The basis case walks all of the children of the statement or
99  /// expression, assuming they are all potentially evaluated.
100  void VisitStmt(PTR(Stmt) S) {
101  for (auto *SubStmt : S->children())
102  if (SubStmt)
103  this->Visit(SubStmt);
104  }
105 
106 #undef PTR
107 };
108 
109 /// EvaluatedExprVisitor - This class visits 'Expr *'s
110 template <typename ImplClass>
112  : public EvaluatedExprVisitorBase<std::add_pointer, ImplClass> {
113 public:
115  : EvaluatedExprVisitorBase<std::add_pointer, ImplClass>(Context) {}
116 };
117 
118 /// ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
119 template <typename ImplClass>
121  : public EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass> {
122 public:
124  : EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass>(Context) {}
125 };
126 }
127 
128 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
clang::ExpressionTraitExpr
An expression trait intrinsic.
Definition: ExprCXX.h:2823
llvm
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion.
Definition: Dominators.h:30
clang::OffsetOfExpr
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition: Expr.h:2374
clang::EvaluatedExprVisitorBase::VisitExpressionTraitExpr
void VisitExpressionTraitExpr(PTR(ExpressionTraitExpr) E)
Definition: EvaluatedExprVisitor.h:44
clang::DesignatedInitExpr
Represents a C99 designated initializer expression.
Definition: Expr.h:4948
clang::CXXTypeidExpr::isPotentiallyEvaluated
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr....
Definition: ExprCXX.cpp:134
clang::EvaluatedExprVisitorBase::VisitCXXNoexceptExpr
void VisitCXXNoexceptExpr(PTR(CXXNoexceptExpr) E)
Definition: EvaluatedExprVisitor.h:47
clang::EvaluatedExprVisitorBase::VisitDeclRefExpr
void VisitDeclRefExpr(PTR(DeclRefExpr) E)
Definition: EvaluatedExprVisitor.h:41
clang::EvaluatedExprVisitorBase::VisitLambdaExpr
void VisitLambdaExpr(PTR(LambdaExpr) LE)
Definition: EvaluatedExprVisitor.h:89
clang::CXXTypeidExpr
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:839
DeclCXX.h
clang::EvaluatedExprVisitorBase
Given a potentially-evaluated expression, this visitor visits all of its potentially-evaluated subexp...
Definition: EvaluatedExprVisitor.h:30
clang::EvaluatedExprVisitorBase::VisitChooseExpr
void VisitChooseExpr(PTR(ChooseExpr) E)
Definition: EvaluatedExprVisitor.h:54
clang::CXXTypeidExpr::getExprOperand
Expr * getExprOperand() const
Definition: ExprCXX.h:886
clang::EvaluatedExprVisitor::EvaluatedExprVisitor
EvaluatedExprVisitor(const ASTContext &Context)
Definition: EvaluatedExprVisitor.h:114
clang::EvaluatedExprVisitorBase::VisitGenericSelectionExpr
void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E)
Definition: EvaluatedExprVisitor.h:62
clang::EvaluatedExprVisitorBase::VisitCXXUuidofExpr
void VisitCXXUuidofExpr(PTR(CXXUuidofExpr) E)
Definition: EvaluatedExprVisitor.h:46
clang::UnaryExprOrTypeTraitExpr
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition: Expr.h:2479
clang::ConstEvaluatedExprVisitor::ConstEvaluatedExprVisitor
ConstEvaluatedExprVisitor(const ASTContext &Context)
Definition: EvaluatedExprVisitor.h:123
clang::LambdaExpr
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1865
clang::interp::LE
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:237
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
PTR
#define PTR(CLASS)
Definition: EvaluatedExprVisitor.h:35
clang::EvaluatedExprVisitorBase::EvaluatedExprVisitorBase
EvaluatedExprVisitorBase(const ASTContext &Context)
Definition: EvaluatedExprVisitor.h:37
clang::StmtVisitorBase< Ptr, ImplClass, void >::Visit
void Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:43
clang::LambdaExpr::const_capture_init_iterator
Expr *const * const_capture_init_iterator
Const iterator that walks over the capture initialization arguments.
Definition: ExprCXX.h:1978
Expr.h
clang::StmtVisitorBase
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:36
ExprCXX.h
clang::EvaluatedExprVisitorBase::VisitCXXTypeidExpr
void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E)
Definition: EvaluatedExprVisitor.h:79
clang::ChooseExpr
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4444
clang::GenericSelectionExpr::isResultDependent
bool isResultDependent() const
Whether this generic selection is result-dependent.
Definition: Expr.h:5725
clang::MemberExpr::getBase
Expr * getBase() const
Definition: Expr.h:3163
clang::EvaluatedExprVisitorBase::VisitCallExpr
void VisitCallExpr(PTR(CallExpr) CE)
Definition: EvaluatedExprVisitor.h:84
clang::ChooseExpr::getChosenSubExpr
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition: Expr.h:4480
clang::BlockExpr
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5889
clang::CXXUuidofExpr
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:1056
StmtVisitor.h
clang::ChooseExpr::getCond
Expr * getCond() const
Definition: Expr.h:4484
clang::EvaluatedExprVisitorBase::VisitDesignatedInitExpr
void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E)
Definition: EvaluatedExprVisitor.h:73
clang::CXXNoexceptExpr
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition: ExprCXX.h:3997
clang::Expr::isValueDependent
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition: Expr.h:168
std
Definition: Format.h:3549
clang::EvaluatedExprVisitorBase::VisitUnaryExprOrTypeTraitExpr
void VisitUnaryExprOrTypeTraitExpr(PTR(UnaryExprOrTypeTraitExpr) E)
Definition: EvaluatedExprVisitor.h:43
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:68
clang::GenericSelectionExpr::getResultExpr
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition: Expr.h:5737
clang::GenericSelectionExpr
Represents a C11 generic selection.
Definition: Expr.h:5547
clang::ConstEvaluatedExprVisitor
ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
Definition: EvaluatedExprVisitor.h:120
clang::EvaluatedExprVisitorBase::VisitBlockExpr
void VisitBlockExpr(PTR(BlockExpr) E)
Definition: EvaluatedExprVisitor.h:45
clang::DesignatedInitExpr::getInit
Expr * getInit() const
Retrieve the initializer value.
Definition: Expr.h:5189
clang::MemberExpr
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3086
clang::EvaluatedExprVisitorBase::VisitStmt
void VisitStmt(PTR(Stmt) S)
The basis case walks all of the children of the statement or expression, assuming they are all potent...
Definition: EvaluatedExprVisitor.h:100
clang::EvaluatedExprVisitorBase::VisitMemberExpr
void VisitMemberExpr(PTR(MemberExpr) E)
Definition: EvaluatedExprVisitor.h:49
clang::EvaluatedExprVisitor
EvaluatedExprVisitor - This class visits 'Expr *'s.
Definition: EvaluatedExprVisitor.h:111
clang::EvaluatedExprVisitorBase::VisitOffsetOfExpr
void VisitOffsetOfExpr(PTR(OffsetOfExpr) E)
Definition: EvaluatedExprVisitor.h:42
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1213
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2730
clang::CallExpr::isUnevaluatedBuiltinCall
bool isUnevaluatedBuiltinCall(const ASTContext &Ctx) const
Returns true if this is a call to a builtin which does not evaluate side-effects within its arguments...
Definition: Expr.cpp:1377
clang::EvaluatedExprVisitorBase::Context
const ASTContext & Context
Definition: EvaluatedExprVisitor.h:32