clang 23.0.0git
State.h
Go to the documentation of this file.
1//===--- State.h - State chain for the VM and AST Walker --------*- 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// Defines the base class of the interpreter and evaluator state.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_STATE_H
14#define LLVM_CLANG_AST_INTERP_STATE_H
15
17#include "clang/AST/Expr.h"
19
20namespace clang {
22
23/// Kinds of access we can perform on an object, for diagnostics. Note that
24/// we consider a member function call to be a kind of access, even though
25/// it is not formally an access of the object, because it has (largely) the
26/// same set of semantic restrictions.
41
42/// The order of this enum is important for diagnostics.
53
54enum class EvaluationMode {
55 /// Evaluate as a constant expression. Stop if we find that the expression
56 /// is not a constant expression.
58
59 /// Evaluate as a constant expression. Stop if we find that the expression
60 /// is not a constant expression. Some expressions can be retried in the
61 /// optimizer if we don't constant fold them here, but in an unevaluated
62 /// context we try to fold them immediately since the optimizer never
63 /// gets a chance to look at it.
65
66 /// Fold the expression to a constant. Stop if we hit a side-effect that
67 /// we can't model.
69
70 /// Evaluate in any way we know how. Don't worry about side-effects that
71 /// can't be modeled.
73};
74
75namespace interp {
76class Frame;
77class SourceInfo;
78
79/// Interface for the VM to interact with the AST walker's context.
80class State {
81public:
82 virtual ~State();
83
84 virtual bool noteUndefinedBehavior() = 0;
85 virtual bool keepEvaluatingAfterFailure() const = 0;
86 virtual bool keepEvaluatingAfterSideEffect() const = 0;
87 virtual Frame *getCurrentFrame() = 0;
88 virtual const Frame *getBottomFrame() const = 0;
89 virtual bool hasActiveDiagnostic() = 0;
90 virtual void setActiveDiagnostic(bool Flag) = 0;
91 virtual void setFoldFailureDiagnostic(bool Flag) = 0;
92 virtual Expr::EvalStatus &getEvalStatus() const = 0;
93 virtual ASTContext &getASTContext() const = 0;
94 virtual bool hasPriorDiagnostic() = 0;
95 virtual unsigned getCallStackDepth() = 0;
96 virtual bool noteSideEffect() = 0;
97
98 /// Are we checking whether the expression is a potential constant
99 /// expression?
103 /// Are we checking an expression for overflow?
107
108public:
109 State() = default;
110 /// Diagnose that the evaluation could not be folded (FF => FoldFailure)
113 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
114 unsigned ExtraNotes = 0);
115
117 FFDiag(const Expr *E,
118 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
119 unsigned ExtraNotes = 0);
120
122 FFDiag(const SourceInfo &SI,
123 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
124 unsigned ExtraNotes = 0);
125
126 /// Diagnose that the evaluation does not produce a C++11 core constant
127 /// expression.
128 ///
129 /// FIXME: Stop evaluating if we're in EM_ConstantExpression or
130 /// EM_PotentialConstantExpression mode and we produce one of these.
133 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
134 unsigned ExtraNotes = 0);
135
137 CCEDiag(const Expr *E,
138 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
139 unsigned ExtraNotes = 0);
140
142 CCEDiag(const SourceInfo &SI,
143 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
144 unsigned ExtraNotes = 0);
145
146 /// Add a note to a prior diagnostic.
148
149 /// Add a stack of notes to a prior diagnostic.
151
152 /// Directly reports a diagnostic message.
154
155 /// Whether or not we're in a context where the front end requires a
156 /// constant value.
157 bool InConstantContext = false;
158
159 /// Whether we're checking that an expression is a potential constant
160 /// expression. If so, do not fail on constructs that could become constant
161 /// later on (such as a use of an undefined global).
163
164 /// Whether we're checking for an expression that has undefined behavior.
165 /// If so, we will produce warnings if we encounter an operation that is
166 /// always undefined.
167 ///
168 /// Note that we still need to evaluate the expression normally when this
169 /// is set; this is used when evaluating ICEs in C.
171
173
174private:
175 void addCallStack(unsigned Limit);
176
177 PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId);
178
180 unsigned ExtraNotes, bool IsCCEDiag);
181};
182
183} // namespace interp
184} // namespace clang
185
186#endif
Implements a partial diagnostic which may not be emitted.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
A little helper class used to produce diagnostics.
This represents one expression.
Definition Expr.h:112
A partial diagnostic which we might know in advance that we are not going to emit.
Encodes a location in the source.
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Describes the statement/declaration an opcode was generated from.
Definition Source.h:74
bool checkingForUndefinedBehavior() const
Are we checking an expression for overflow?
Definition State.h:104
virtual bool hasActiveDiagnostic()=0
virtual void setActiveDiagnostic(bool Flag)=0
EvaluationMode EvalMode
Definition State.h:172
DiagnosticBuilder report(SourceLocation Loc, diag::kind DiagId)
Directly reports a diagnostic message.
Definition State.cpp:74
virtual Frame * getCurrentFrame()=0
virtual bool noteUndefinedBehavior()=0
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:21
bool CheckingPotentialConstantExpression
Whether we're checking that an expression is a potential constant expression.
Definition State.h:162
virtual Expr::EvalStatus & getEvalStatus() const =0
virtual unsigned getCallStackDepth()=0
bool CheckingForUndefinedBehavior
Whether we're checking for an expression that has undefined behavior.
Definition State.h:170
virtual bool keepEvaluatingAfterFailure() const =0
void addNotes(ArrayRef< PartialDiagnosticAt > Diags)
Add a stack of notes to a prior diagnostic.
Definition State.cpp:69
virtual const Frame * getBottomFrame() const =0
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition State.cpp:42
virtual void setFoldFailureDiagnostic(bool Flag)=0
virtual bool noteSideEffect()=0
virtual bool hasPriorDiagnostic()=0
bool checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:100
virtual bool keepEvaluatingAfterSideEffect() const =0
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition State.h:157
virtual ASTContext & getASTContext() const =0
virtual ~State()
Definition State.cpp:19
unsigned kind
All of the diagnostics that can be emitted by the frontend.
The JSON file list parser is used to communicate input to InstallAPI.
CheckSubobjectKind
The order of this enum is important for diagnostics.
Definition State.h:43
@ CSK_ArrayToPointer
Definition State.h:47
@ CSK_Derived
Definition State.h:45
@ CSK_Base
Definition State.h:44
@ CSK_Real
Definition State.h:49
@ CSK_ArrayIndex
Definition State.h:48
@ CSK_Imag
Definition State.h:50
@ CSK_VectorElement
Definition State.h:51
@ CSK_Field
Definition State.h:46
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:27
@ AK_TypeId
Definition State.h:35
@ AK_Construct
Definition State.h:36
@ AK_Increment
Definition State.h:31
@ AK_DynamicCast
Definition State.h:34
@ AK_Read
Definition State.h:28
@ AK_Assign
Definition State.h:30
@ AK_IsWithinLifetime
Definition State.h:38
@ AK_MemberCall
Definition State.h:33
@ AK_ReadObjectRepresentation
Definition State.h:29
@ AK_Dereference
Definition State.h:39
@ AK_Destroy
Definition State.h:37
@ AK_Decrement
Definition State.h:32
EvaluationMode
Definition State.h:54
@ ConstantFold
Fold the expression to a constant.
Definition State.h:68
@ ConstantExpressionUnevaluated
Evaluate as a constant expression.
Definition State.h:64
@ ConstantExpression
Evaluate as a constant expression.
Definition State.h:57
@ IgnoreSideEffects
Evaluate in any way we know how.
Definition State.h:72
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition Expr.h:609