clang  19.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  // Return whether this visitor should recurse into discarded statements for a
36  // 'constexpr-if'.
37  bool shouldVisitDiscardedStmt() const { return true; }
38 #define PTR(CLASS) typename Ptr<CLASS>::type
39 
41 
42  // Expressions that have no potentially-evaluated subexpressions (but may have
43  // other sub-expressions).
51 
53  // Only the base matters.
54  return this->Visit(E->getBase());
55  }
56 
58  // Don't visit either child expression if the condition is dependent.
59  if (E->getCond()->isValueDependent())
60  return;
61  // Only the selected subexpression matters; the other one is not evaluated.
62  return this->Visit(E->getChosenSubExpr());
63  }
64 
66  // The controlling expression of a generic selection is not evaluated.
67 
68  // Don't visit either child expression if the condition is type-dependent.
69  if (E->isResultDependent())
70  return;
71  // Only the selected subexpression matters; the other subexpressions and the
72  // controlling expression are not evaluated.
73  return this->Visit(E->getResultExpr());
74  }
75 
77  // Only the actual initializer matters; the designators are all constant
78  // expressions.
79  return this->Visit(E->getInit());
80  }
81 
83  if (E->isPotentiallyEvaluated())
84  return this->Visit(E->getExprOperand());
85  }
86 
89  return getDerived().VisitExpr(CE);
90  }
91 
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();
96  I != E; ++I)
97  if (*I)
98  this->Visit(*I);
99  }
100 
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())
105  if (SubStmt)
106  this->Visit(SubStmt);
107  }
108 
109  void VisitIfStmt(PTR(IfStmt) If) {
111  if (auto SubStmt = If->getNondiscardedCase(Context)) {
112  if (*SubStmt)
113  this->Visit(*SubStmt);
114  return;
115  }
116  }
117 
118  getDerived().VisitStmt(If);
119  }
120 
121  ImplClass &getDerived() { return *static_cast<ImplClass *>(this); }
122 
123 #undef PTR
124 };
125 
126 /// EvaluatedExprVisitor - This class visits 'Expr *'s
127 template <typename ImplClass>
129  : public EvaluatedExprVisitorBase<std::add_pointer, ImplClass> {
130 public:
132  : EvaluatedExprVisitorBase<std::add_pointer, ImplClass>(Context) {}
133 };
134 
135 /// ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
136 template <typename ImplClass>
138  : public EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass> {
139 public:
141  : EvaluatedExprVisitorBase<llvm::make_const_ptr, ImplClass>(Context) {}
142 };
143 }
144 
145 #endif // LLVM_CLANG_AST_EVALUATEDEXPRVISITOR_H
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
#define PTR(CLASS)
Defines the clang::Expr interface and subclasses for C++ expressions.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6214
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition: ExprCXX.h:4119
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition: ExprCXX.h:845
Expr * getExprOperand() const
Definition: ExprCXX.h:892
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr....
Definition: ExprCXX.cpp:135
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:1062
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2872
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:1639
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4610
Expr * getCond() const
Definition: Expr.h:4650
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition: Expr.h:4646
ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
ConstEvaluatedExprVisitor(const ASTContext &Context)
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
Represents a C99 designated initializer expression.
Definition: Expr.h:5133
Expr * getInit() const
Retrieve the initializer value.
Definition: Expr.h:5401
Given a potentially-evaluated expression, this visitor visits all of its potentially-evaluated subexp...
void VisitBlockExpr(PTR(BlockExpr) E)
EvaluatedExprVisitorBase(const ASTContext &Context)
void VisitStmt(PTR(Stmt) S)
The basis case walks all of the children of the statement or expression, assuming they are all potent...
void VisitExpressionTraitExpr(PTR(ExpressionTraitExpr) E)
void VisitCXXNoexceptExpr(PTR(CXXNoexceptExpr) E)
void VisitMemberExpr(PTR(MemberExpr) E)
void VisitCallExpr(PTR(CallExpr) CE)
void VisitCXXUuidofExpr(PTR(CXXUuidofExpr) E)
void VisitCXXTypeidExpr(PTR(CXXTypeidExpr) E)
void VisitGenericSelectionExpr(PTR(GenericSelectionExpr) E)
void VisitDeclRefExpr(PTR(DeclRefExpr) E)
void VisitOffsetOfExpr(PTR(OffsetOfExpr) E)
void VisitDesignatedInitExpr(PTR(DesignatedInitExpr) E)
void VisitUnaryExprOrTypeTraitExpr(PTR(UnaryExprOrTypeTraitExpr) E)
void VisitChooseExpr(PTR(ChooseExpr) E)
void VisitLambdaExpr(PTR(LambdaExpr) LE)
EvaluatedExprVisitor - This class visits 'Expr *'s.
EvaluatedExprVisitor(const ASTContext &Context)
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition: Expr.h:175
An expression trait intrinsic.
Definition: ExprCXX.h:2919
Represents a C11 generic selection.
Definition: Expr.h:5766
bool isResultDependent() const
Whether this generic selection is result-dependent.
Definition: Expr.h:6018
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition: Expr.h:6050
IfStmt - This represents an if/then/else.
Definition: Stmt.h:2138
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1950
Expr *const * const_capture_init_iterator
Const iterator that walks over the capture initialization arguments.
Definition: ExprCXX.h:2062
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3224
Expr * getBase() const
Definition: Expr.h:3301
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition: Expr.h:2517
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:37
void Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:44
Stmt - This represents one statement.
Definition: Stmt.h:84
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition: Expr.h:2620
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:883
The JSON file list parser is used to communicate input to InstallAPI.
@ If
'if' clause, allowed on all the Compute Constructs, Data Constructs, Executable Constructs,...
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
Definition: Format.h:5433