clang 8.0.0

EvaluatedExprVisitor.h
Go to the documentation of this file.
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 #include "llvm/ADT/STLExtras.h"
23
24 namespace clang {
25
26 class ASTContext;
27
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> {
32 protected:
33 const ASTContext &Context;
34
35 public:
36 #define PTR(CLASS) typename Ptr<CLASS>::type
37
38 explicit EvaluatedExprVisitorBase(const ASTContext &Context) : Context(Context) { }
39
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) { }
49
50 void VisitMemberExpr(PTR(MemberExpr) E) {
51 // Only the base matters.
52 return this->Visit(E->getBase());
53 }
54
55 void VisitChooseExpr(PTR(ChooseExpr) E) {
56 // Don't visit either child expression if the condition is dependent.
57 if (E->getCond()->isValueDependent())
58 return;
59 // Only the selected subexpression matters; the other one is not evaluated.
60 return this->Visit(E->getChosenSubExpr());
61 }
62
63 void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E) {
64 // The controlling expression of a generic selection is not evaluated.
65
66 // Don't visit either child expression if the condition is type-dependent.
67 if (E->isResultDependent())
68 return;
69 // Only the selected subexpression matters; the other subexpressions and the
70 // controlling expression are not evaluated.
71 return this->Visit(E->getResultExpr());
72 }
73
74 void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E) {
75 // Only the actual initializer matters; the designators are all constant
76 // expressions.
77 return this->Visit(E->getInit());
78 }
79
80 void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E) {
81 if (E->isPotentiallyEvaluated())
82 return this->Visit(E->getExprOperand());
83 }
84
85 void VisitCallExpr(PTR(CallExpr) CE) {
86 if (!CE->isUnevaluatedBuiltinCall(Context))
87 return static_cast<ImplClass*>(this)->VisitExpr(CE);
88 }
89
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();
94 I != E; ++I)
95 if (*I)
96 this->Visit(*I);
97 }
98
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())
103 if (SubStmt)
104 this->Visit(SubStmt);
105 }
106
107 #undef PTR
108 };
109
110 /// EvaluatedExprVisitor - This class visits 'Expr *'s
111 template <typename ImplClass>
112 class EvaluatedExprVisitor
113 : public EvaluatedExprVisitorBase<std::add_pointer, ImplClass> {
114 public:
115 explicit EvaluatedExprVisitor(const ASTContext &Context)
116 : EvaluatedExprVisitorBase<std::add_pointer, ImplClass>(Context) {}
117 };
118
119 /// ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
120 template <typename ImplClass>
121 class ConstEvaluatedExprVisitor
122 : public EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass> {
123 public:
124 explicit ConstEvaluatedExprVisitor(const ASTContext &Context)
125 : EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass>(Context) {}
126 };
127 }
128
129 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
clang::EvaluatedExprVisitorBase::Context
const ASTContext & Context
Definition: EvaluatedExprVisitor.h:33
clang::ChooseExpr::getChosenSubExpr
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition: Expr.h:4018
clang::EvaluatedExprVisitor
EvaluatedExprVisitor - This class visits &#39;Expr *&#39;s.
Definition: EvaluatedExprVisitor.h:112
clang::ChooseExpr::getCond
Expr * getCond() const
Definition: Expr.h:4022
llvm
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::MemberExpr::getBase
Expr * getBase() const
Definition: Expr.h:2767
PTR
#define PTR(CLASS)
Definition: EvaluatedExprVisitor.h:36
clang::GenericSelectionExpr::getResultExpr
const Expr * getResultExpr() const
The generic selection&#39;s result expression.
Definition: Expr.h:5086
clang::CXXTypeidExpr::getExprOperand
Expr * getExprOperand() const
Definition: ExprCXX.h:726
ExprCXX.h
Defines the clang::Expr interface and subclasses for C++ expressions.
clang::EvaluatedExprVisitorBase::VisitDesignatedInitExpr
void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E)
Definition: EvaluatedExprVisitor.h:74
clang::DesignatedInitExpr
Represents a C99 designated initializer expression.
Definition: Expr.h:4419
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
clang::DesignatedInitExpr::getInit
Expr * getInit() const
Retrieve the initializer value.
Definition: Expr.h:4653
std
Definition: Format.h:2072
clang::CXXTypeidExpr
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:669
clang::EvaluatedExprVisitorBase::VisitBlockExpr
void VisitBlockExpr(PTR(BlockExpr) E)
Definition: EvaluatedExprVisitor.h:46
clang::LambdaExpr::capture_init_begin
capture_init_iterator capture_init_begin()
Retrieve the first initialization argument for this lambda expression (which initializes the first ca...
Definition: ExprCXX.h:1794
clang::Stmt::children
child_range children()
Definition: Stmt.cpp:237
clang::LambdaExpr
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1649
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:1390
clang::EvaluatedExprVisitor::EvaluatedExprVisitor
EvaluatedExprVisitor(const ASTContext &Context)
Definition: EvaluatedExprVisitor.h:115
clang::UnaryExprOrTypeTraitExpr
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand...
Definition: Expr.h:2222
clang::EvaluatedExprVisitorBase::EvaluatedExprVisitorBase
EvaluatedExprVisitorBase(const ASTContext &Context)
Definition: EvaluatedExprVisitor.h:38
clang::EvaluatedExprVisitorBase::VisitGenericSelectionExpr
void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E)
Definition: EvaluatedExprVisitor.h:63
clang::BlockExpr
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5177
clang::ConstEvaluatedExprVisitor::ConstEvaluatedExprVisitor
ConstEvaluatedExprVisitor(const ASTContext &Context)
Definition: EvaluatedExprVisitor.h:124
StmtVisitor.h
clang::EvaluatedExprVisitorBase::VisitCXXTypeidExpr
void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E)
Definition: EvaluatedExprVisitor.h:80
clang::EvaluatedExprVisitorBase::VisitCallExpr
void VisitCallExpr(PTR(CallExpr) CE)
Definition: EvaluatedExprVisitor.h:85
clang::EvaluatedExprVisitorBase::VisitDeclRefExpr
void VisitDeclRefExpr(PTR(DeclRefExpr) E)
Definition: EvaluatedExprVisitor.h:42
clang::EvaluatedExprVisitorBase::VisitExpressionTraitExpr
void VisitExpressionTraitExpr(PTR(ExpressionTraitExpr) E)
Definition: EvaluatedExprVisitor.h:45
clang::EvaluatedExprVisitorBase::VisitLambdaExpr
void VisitLambdaExpr(PTR(LambdaExpr) LE)
Definition: EvaluatedExprVisitor.h:90
clang::Expr::isValueDependent
bool isValueDependent() const
isValueDependent - Determines whether this expression is value-dependent (C++ [temp.dep.constexpr]).
Definition: Expr.h:149
clang::EvaluatedExprVisitorBase::VisitCXXNoexceptExpr
void VisitCXXNoexceptExpr(PTR(CXXNoexceptExpr) E)
Definition: EvaluatedExprVisitor.h:48
clang::ExpressionTraitExpr
An expression trait intrinsic.
Definition: ExprCXX.h:2580
clang::GenericSelectionExpr
Represents a C11 generic selection.
Definition: Expr.h:5010
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::EvaluatedExprVisitorBase::VisitUnaryExprOrTypeTraitExpr
void VisitUnaryExprOrTypeTraitExpr(PTR(UnaryExprOrTypeTraitExpr) E)
Definition: EvaluatedExprVisitor.h:44
clang::GenericSelectionExpr::isResultDependent
bool isResultDependent() const
Whether this generic selection is result-dependent.
Definition: Expr.h:5074
clang::ConstEvaluatedExprVisitor
ConstEvaluatedExprVisitor - This class visits &#39;const Expr *&#39;s.
Definition: EvaluatedExprVisitor.h:121
clang::EvaluatedExprVisitorBase::VisitCXXUuidofExpr
void VisitCXXUuidofExpr(PTR(CXXUuidofExpr) E)
Definition: EvaluatedExprVisitor.h:47
clang::CXXNoexceptExpr
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition: ExprCXX.h:3719
clang::StmtVisitorBase
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:36
clang::EvaluatedExprVisitorBase
Given a potentially-evaluated expression, this visitor visits all of its potentially-evaluated subexp...
Definition: EvaluatedExprVisitor.h:31
DeclCXX.h
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
clang::MemberExpr
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:2682
clang::ChooseExpr
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:3977
clang::EvaluatedExprVisitorBase::VisitMemberExpr
void VisitMemberExpr(PTR(MemberExpr) E)
Definition: EvaluatedExprVisitor.h:50
Expr.h
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2396
clang::StmtVisitorBase< Ptr, ImplClass, void >::Visit
void Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:43
clang::EvaluatedExprVisitorBase::VisitChooseExpr
void VisitChooseExpr(PTR(ChooseExpr) E)
Definition: EvaluatedExprVisitor.h:55
clang::LambdaExpr::capture_init_end
capture_init_iterator capture_init_end()
Retrieve the iterator pointing one past the last initialization argument for this lambda expression...
Definition: ExprCXX.h:1806
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1041
clang::LambdaExpr::const_capture_init_iterator
Expr *const * const_capture_init_iterator
Const iterator that walks over the capture initialization arguments.
Definition: ExprCXX.h:1780
clang::OffsetOfExpr
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type, member-designator).
Definition: Expr.h:2117
clang::CXXUuidofExpr
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:887
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:62
clang::EvaluatedExprVisitorBase::VisitOffsetOfExpr
void VisitOffsetOfExpr(PTR(OffsetOfExpr) E)
Definition: EvaluatedExprVisitor.h:43
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:101