clang 22.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"
18
19namespace clang {
21
22/// Kinds of access we can perform on an object, for diagnostics. Note that
23/// we consider a member function call to be a kind of access, even though
24/// it is not formally an access of the object, because it has (largely) the
25/// same set of semantic restrictions.
40
41/// The order of this enum is important for diagnostics.
52
53enum class EvaluationMode {
54 /// Evaluate as a constant expression. Stop if we find that the expression
55 /// is not a constant expression.
57
58 /// Evaluate as a constant expression. Stop if we find that the expression
59 /// is not a constant expression. Some expressions can be retried in the
60 /// optimizer if we don't constant fold them here, but in an unevaluated
61 /// context we try to fold them immediately since the optimizer never
62 /// gets a chance to look at it.
64
65 /// Fold the expression to a constant. Stop if we hit a side-effect that
66 /// we can't model.
68
69 /// Evaluate in any way we know how. Don't worry about side-effects that
70 /// can't be modeled.
72};
73
74namespace interp {
75class Frame;
76class SourceInfo;
77
78/// Interface for the VM to interact with the AST walker's context.
79class State {
80public:
81 virtual ~State();
82
83 virtual bool noteUndefinedBehavior() = 0;
84 virtual bool keepEvaluatingAfterFailure() const = 0;
85 virtual bool keepEvaluatingAfterSideEffect() const = 0;
86 virtual Frame *getCurrentFrame() = 0;
87 virtual const Frame *getBottomFrame() const = 0;
88 virtual bool hasActiveDiagnostic() = 0;
89 virtual void setActiveDiagnostic(bool Flag) = 0;
90 virtual void setFoldFailureDiagnostic(bool Flag) = 0;
91 virtual Expr::EvalStatus &getEvalStatus() const = 0;
92 virtual ASTContext &getASTContext() const = 0;
93 virtual bool hasPriorDiagnostic() = 0;
94 virtual unsigned getCallStackDepth() = 0;
95 virtual bool noteSideEffect() = 0;
96
97 /// Are we checking whether the expression is a potential constant
98 /// expression?
102 /// Are we checking an expression for overflow?
106
107public:
108 State() = default;
109 /// Diagnose that the evaluation could not be folded (FF => FoldFailure)
112 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
113 unsigned ExtraNotes = 0);
114
116 FFDiag(const Expr *E,
117 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
118 unsigned ExtraNotes = 0);
119
121 FFDiag(const SourceInfo &SI,
122 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
123 unsigned ExtraNotes = 0);
124
125 /// Diagnose that the evaluation does not produce a C++11 core constant
126 /// expression.
127 ///
128 /// FIXME: Stop evaluating if we're in EM_ConstantExpression or
129 /// EM_PotentialConstantExpression mode and we produce one of these.
132 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
133 unsigned ExtraNotes = 0);
134
136 CCEDiag(const Expr *E,
137 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
138 unsigned ExtraNotes = 0);
139
141 CCEDiag(const SourceInfo &SI,
142 diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr,
143 unsigned ExtraNotes = 0);
144
145 /// Add a note to a prior diagnostic.
147
148 /// Add a stack of notes to a prior diagnostic.
150
151 /// Directly reports a diagnostic message.
153
154 const LangOptions &getLangOpts() const;
155
156 /// Whether or not we're in a context where the front end requires a
157 /// constant value.
158 bool InConstantContext = false;
159
160 /// Whether we're checking that an expression is a potential constant
161 /// expression. If so, do not fail on constructs that could become constant
162 /// later on (such as a use of an undefined global).
164
165 /// Whether we're checking for an expression that has undefined behavior.
166 /// If so, we will produce warnings if we encounter an operation that is
167 /// always undefined.
168 ///
169 /// Note that we still need to evaluate the expression normally when this
170 /// is set; this is used when evaluating ICEs in C.
172
174
175private:
176 void addCallStack(unsigned Limit);
177
178 PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId);
179
181 unsigned ExtraNotes, bool IsCCEDiag);
182};
183
184} // namespace interp
185} // namespace clang
186
187#endif
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:188
A little helper class used to produce diagnostics.
This represents one expression.
Definition Expr.h:112
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
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:73
bool checkingForUndefinedBehavior() const
Are we checking an expression for overflow?
Definition State.h:103
virtual bool hasActiveDiagnostic()=0
virtual void setActiveDiagnostic(bool Flag)=0
EvaluationMode EvalMode
Definition State.h:173
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:163
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:171
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
const LangOptions & getLangOpts() const
Definition State.cpp:115
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:99
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:158
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:42
@ CSK_ArrayToPointer
Definition State.h:46
@ CSK_Derived
Definition State.h:44
@ CSK_Base
Definition State.h:43
@ CSK_Real
Definition State.h:48
@ CSK_ArrayIndex
Definition State.h:47
@ CSK_Imag
Definition State.h:49
@ CSK_VectorElement
Definition State.h:50
@ CSK_Field
Definition State.h:45
AccessKinds
Kinds of access we can perform on an object, for diagnostics.
Definition State.h:26
@ AK_TypeId
Definition State.h:34
@ AK_Construct
Definition State.h:35
@ AK_Increment
Definition State.h:30
@ AK_DynamicCast
Definition State.h:33
@ AK_Read
Definition State.h:27
@ AK_Assign
Definition State.h:29
@ AK_IsWithinLifetime
Definition State.h:37
@ AK_MemberCall
Definition State.h:32
@ AK_ReadObjectRepresentation
Definition State.h:28
@ AK_Dereference
Definition State.h:38
@ AK_Destroy
Definition State.h:36
@ AK_Decrement
Definition State.h:31
EvaluationMode
Definition State.h:53
@ ConstantFold
Fold the expression to a constant.
Definition State.h:67
@ ConstantExpressionUnevaluated
Evaluate as a constant expression.
Definition State.h:63
@ ConstantExpression
Evaluate as a constant expression.
Definition State.h:56
@ IgnoreSideEffects
Evaluate in any way we know how.
Definition State.h:71
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition Expr.h:609