clang  12.0.0git
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 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 //===----------------------------------------------------------------------===//
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"
23 namespace clang {
25 class ASTContext;
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:
34 public:
35 #define PTR(CLASS) typename Ptr<CLASS>::type
37  explicit EvaluatedExprVisitorBase(const ASTContext &Context) : Context(Context) { }
39  // Expressions that have no potentially-evaluated subexpressions (but may have
40  // other sub-expressions).
50  // Only the base matters.
51  return this->Visit(E->getBase());
52  }
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  }
63  // The controlling expression of a generic selection is not evaluated.
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  }
74  // Only the actual initializer matters; the designators are all constant
75  // expressions.
76  return this->Visit(E->getInit());
77  }
80  if (E->isPotentiallyEvaluated())
81  return this->Visit(E->getExprOperand());
82  }
85  if (!CE->isUnevaluatedBuiltinCall(Context))
86  return static_cast<ImplClass*>(this)->VisitExpr(CE);
87  }
90  // Only visit the capture initializers, and not the body.
92  E = LE->capture_init_end();
93  I != E; ++I)
94  if (*I)
95  this->Visit(*I);
96  }
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  }
106 #undef PTR
107 };
109 /// EvaluatedExprVisitor - This class visits 'Expr *'s
110 template <typename ImplClass>
112  : public EvaluatedExprVisitorBase<std::add_pointer, ImplClass> {
113 public:
114  explicit EvaluatedExprVisitor(const ASTContext &Context)
115  : EvaluatedExprVisitorBase<std::add_pointer, ImplClass>(Context) {}
116 };
118 /// ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
119 template <typename ImplClass>
121  : public EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass> {
122 public:
123  explicit ConstEvaluatedExprVisitor(const ASTContext &Context)
124  : EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass>(Context) {}
125 };
126 }
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition: Expr.h:4397
EvaluatedExprVisitor - This class visits &#39;Expr *&#39;s.
Expr * getCond() const
Definition: Expr.h:4401
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition: Expr.h:5644
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
Stmt - This represents one statement.
Definition: Stmt.h:68
Expr * getBase() const
Definition: Expr.h:3139
#define PTR(CLASS)
Expr * getExprOperand() const
Definition: ExprCXX.h:852
Defines the clang::Expr interface and subclasses for C++ expressions.
void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E)
Represents a C99 designated initializer expression.
Definition: Expr.h:4858
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:174
Expr * getInit() const
Retrieve the initializer value.
Definition: Expr.h:5096
Definition: Format.h:2679
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:809
void VisitBlockExpr(PTR(BlockExpr) E)
capture_init_iterator capture_init_begin()
Retrieve the first initialization argument for this lambda expression (which initializes the first ca...
Definition: ExprCXX.h:1949
child_range children()
Definition: Stmt.cpp:224
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1822
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:1435
EvaluatedExprVisitor(const ASTContext &Context)
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand...
Definition: Expr.h:2517
EvaluatedExprVisitorBase(const ASTContext &Context)
void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E)
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5796
ConstEvaluatedExprVisitor(const ASTContext &Context)
void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E)
void VisitCallExpr(PTR(CallExpr) CE)
void VisitDeclRefExpr(PTR(DeclRefExpr) E)
void VisitExpressionTraitExpr(PTR(ExpressionTraitExpr) E)
void VisitLambdaExpr(PTR(LambdaExpr) LE)
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition: Expr.h:169
void VisitCXXNoexceptExpr(PTR(CXXNoexceptExpr) E)
An expression trait intrinsic.
Definition: ExprCXX.h:2777
Represents a C11 generic selection.
Definition: Expr.h:5454
Dataflow Directional Tag Classes.
void VisitUnaryExprOrTypeTraitExpr(PTR(UnaryExprOrTypeTraitExpr) E)
bool isResultDependent() const
Whether this generic selection is result-dependent.
Definition: Expr.h:5632
ConstEvaluatedExprVisitor - This class visits &#39;const Expr *&#39;s.
void VisitCXXUuidofExpr(PTR(CXXUuidofExpr) E)
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition: ExprCXX.h:3950
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:36
Given a potentially-evaluated expression, this visitor visits all of its potentially-evaluated subexp...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
MemberExpr - [C99] Structure and Union Members.
Definition: Expr.h:3062
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4361
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:237
void VisitMemberExpr(PTR(MemberExpr) E)
CallExpr - Represents a function call (C99, C++ []).
Definition: Expr.h:2768
void Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:43
void VisitChooseExpr(PTR(ChooseExpr) E)
capture_init_iterator capture_init_end()
Retrieve the iterator pointing one past the last initialization argument for this lambda expression...
Definition: ExprCXX.h:1961
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1190
Expr *const * const_capture_init_iterator
Const iterator that walks over the capture initialization arguments.
Definition: ExprCXX.h:1935
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type, member-designator).
Definition: Expr.h:2412
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:1022
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr...
Definition: ExprCXX.cpp:134
void VisitOffsetOfExpr(PTR(OffsetOfExpr) E)
void VisitStmt(PTR(Stmt) S)
The basis case walks all of the children of the statement or expression, assuming they are all potent...