| 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 | |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/ASTDiagnostic.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/OptionalDiagnostic.h" |
| 20 | |
| 21 | namespace clang { |
| 22 | class OptionalDiagnostic; |
| 23 | |
| 24 | /// Kinds of access we can perform on an object, for diagnostics. Note that |
| 25 | /// we consider a member function call to be a kind of access, even though |
| 26 | /// it is not formally an access of the object, because it has (largely) the |
| 27 | /// same set of semantic restrictions. |
| 28 | enum AccessKinds { |
| 29 | AK_Read, |
| 30 | AK_ReadObjectRepresentation, |
| 31 | AK_Assign, |
| 32 | AK_Increment, |
| 33 | AK_Decrement, |
| 34 | AK_MemberCall, |
| 35 | AK_DynamicCast, |
| 36 | AK_TypeId, |
| 37 | AK_Construct, |
| 38 | AK_Destroy, |
| 39 | AK_IsWithinLifetime, |
| 40 | AK_Dereference |
| 41 | }; |
| 42 | |
| 43 | /// The order of this enum is important for diagnostics. |
| 44 | enum CheckSubobjectKind { |
| 45 | CSK_Base, |
| 46 | CSK_Derived, |
| 47 | CSK_Field, |
| 48 | CSK_ArrayToPointer, |
| 49 | CSK_ArrayIndex, |
| 50 | CSK_Real, |
| 51 | CSK_Imag, |
| 52 | CSK_VectorElement |
| 53 | }; |
| 54 | |
| 55 | enum class EvaluationMode { |
| 56 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 57 | /// is not a constant expression. |
| 58 | ConstantExpression, |
| 59 | |
| 60 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 61 | /// is not a constant expression. Some expressions can be retried in the |
| 62 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 63 | /// context we try to fold them immediately since the optimizer never |
| 64 | /// gets a chance to look at it. |
| 65 | ConstantExpressionUnevaluated, |
| 66 | |
| 67 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
| 68 | /// we can't model. |
| 69 | ConstantFold, |
| 70 | |
| 71 | /// Evaluate in any way we know how. Don't worry about side-effects that |
| 72 | /// can't be modeled. |
| 73 | IgnoreSideEffects, |
| 74 | }; |
| 75 | |
| 76 | namespace interp { |
| 77 | class Frame; |
| 78 | class SourceInfo; |
| 79 | |
| 80 | /// Interface for the VM to interact with the AST walker's context. |
| 81 | class State { |
| 82 | public: |
| 83 | State(ASTContext &ASTCtx, Expr::EvalStatus &EvalStatus) |
| 84 | : Ctx(ASTCtx), EvalStatus(EvalStatus) {} |
| 85 | virtual ~State(); |
| 86 | |
| 87 | virtual const Frame *getCurrentFrame() = 0; |
| 88 | virtual const Frame *getBottomFrame() const = 0; |
| 89 | virtual unsigned getCallStackDepth() = 0; |
| 90 | virtual bool stepsLeft() const = 0; |
| 91 | |
| 92 | Expr::EvalStatus &getEvalStatus() const { return EvalStatus; } |
| 93 | ASTContext &getASTContext() const { return Ctx; } |
| 94 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
| 95 | |
| 96 | /// Note that we have had a side-effect, and determine whether we should |
| 97 | /// keep evaluating. |
| 98 | bool noteSideEffect() const { |
| 99 | EvalStatus.HasSideEffects = true; |
| 100 | return keepEvaluatingAfterSideEffect(); |
| 101 | } |
| 102 | |
| 103 | /// Should we continue evaluation as much as possible after encountering a |
| 104 | /// construct which can't be reduced to a value? |
| 105 | bool keepEvaluatingAfterFailure() const; |
| 106 | /// Should we continue evaluation after encountering a side-effect that we |
| 107 | /// couldn't model? |
| 108 | bool keepEvaluatingAfterSideEffect() const; |
| 109 | |
| 110 | /// Note that we hit something that was technically undefined behavior, but |
| 111 | /// that we can evaluate past it (such as signed overflow or floating-point |
| 112 | /// division by zero.) |
| 113 | bool noteUndefinedBehavior() const { |
| 114 | EvalStatus.HasUndefinedBehavior = true; |
| 115 | return keepEvaluatingAfterUndefinedBehavior(); |
| 116 | } |
| 117 | |
| 118 | /// Are we checking whether the expression is a potential constant |
| 119 | /// expression? |
| 120 | bool checkingPotentialConstantExpression() const { |
| 121 | return CheckingPotentialConstantExpression; |
| 122 | } |
| 123 | /// Are we checking an expression for overflow? |
| 124 | bool checkingForUndefinedBehavior() const { |
| 125 | return CheckingForUndefinedBehavior; |
| 126 | } |
| 127 | |
| 128 | /// Diagnose that the evaluation could not be folded (FF => FoldFailure) |
| 129 | OptionalDiagnostic |
| 130 | FFDiag(SourceLocation Loc, |
| 131 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 132 | unsigned = 0); |
| 133 | |
| 134 | OptionalDiagnostic |
| 135 | FFDiag(const Expr *E, |
| 136 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 137 | unsigned = 0); |
| 138 | |
| 139 | OptionalDiagnostic |
| 140 | FFDiag(SourceInfo SI, |
| 141 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 142 | unsigned = 0); |
| 143 | |
| 144 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 145 | /// expression. |
| 146 | /// |
| 147 | /// FIXME: Stop evaluating if we're in EM_ConstantExpression or |
| 148 | /// EM_PotentialConstantExpression mode and we produce one of these. |
| 149 | OptionalDiagnostic |
| 150 | CCEDiag(SourceLocation Loc, |
| 151 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 152 | unsigned = 0); |
| 153 | |
| 154 | OptionalDiagnostic |
| 155 | CCEDiag(const Expr *E, |
| 156 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 157 | unsigned = 0); |
| 158 | |
| 159 | OptionalDiagnostic |
| 160 | CCEDiag(SourceInfo SI, |
| 161 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 162 | unsigned = 0); |
| 163 | |
| 164 | /// Add a note to a prior diagnostic. |
| 165 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId); |
| 166 | |
| 167 | /// Add a stack of notes to a prior diagnostic. |
| 168 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags); |
| 169 | |
| 170 | /// Directly reports a diagnostic message. |
| 171 | DiagnosticBuilder report(SourceLocation Loc, diag::kind DiagId); |
| 172 | |
| 173 | /// Whether or not we're in a context where the front end requires a |
| 174 | /// constant value. |
| 175 | bool InConstantContext = false; |
| 176 | |
| 177 | /// Whether we're checking that an expression is a potential constant |
| 178 | /// expression. If so, do not fail on constructs that could become constant |
| 179 | /// later on (such as a use of an undefined global). |
| 180 | bool CheckingPotentialConstantExpression = false; |
| 181 | |
| 182 | /// Whether we're checking for an expression that has undefined behavior. |
| 183 | /// If so, we will produce warnings if we encounter an operation that is |
| 184 | /// always undefined. |
| 185 | /// |
| 186 | /// Note that we still need to evaluate the expression normally when this |
| 187 | /// is set; this is used when evaluating ICEs in C. |
| 188 | bool CheckingForUndefinedBehavior = false; |
| 189 | |
| 190 | EvaluationMode EvalMode; |
| 191 | ASTContext &Ctx; |
| 192 | Expr::EvalStatus &EvalStatus; |
| 193 | |
| 194 | private: |
| 195 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 196 | /// notes attached to it will also be stored, otherwise they will not be. |
| 197 | bool HasActiveDiagnostic = false; |
| 198 | |
| 199 | /// Have we emitted a diagnostic explaining why we couldn't constant |
| 200 | /// fold (not just why it's not strictly a constant expression)? |
| 201 | bool HasFoldFailureDiagnostic = false; |
| 202 | |
| 203 | void addCallStack(unsigned Limit); |
| 204 | |
| 205 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId); |
| 206 | |
| 207 | OptionalDiagnostic diag(SourceLocation Loc, diag::kind DiagId, |
| 208 | unsigned , bool IsCCEDiag); |
| 209 | |
| 210 | /// Should we continue evaluation after encountering undefined behavior? |
| 211 | bool keepEvaluatingAfterUndefinedBehavior() const; |
| 212 | |
| 213 | // If we have a prior diagnostic, it will be noting that the expression |
| 214 | // isn't a constant expression. This diagnostic is more important, |
| 215 | // unless we require this evaluation to produce a constant expression. |
| 216 | // |
| 217 | // FIXME: We might want to show both diagnostics to the user in |
| 218 | // EvaluationMode::ConstantFold mode. |
| 219 | bool hasPriorDiagnostic(); |
| 220 | |
| 221 | void setFoldFailureDiagnostic(bool Flag) { HasFoldFailureDiagnostic = Flag; }; |
| 222 | void setActiveDiagnostic(bool Flag) { HasActiveDiagnostic = Flag; }; |
| 223 | bool hasActiveDiagnostic() const { return HasActiveDiagnostic; } |
| 224 | }; |
| 225 | |
| 226 | } // namespace interp |
| 227 | } // namespace clang |
| 228 | |
| 229 | #endif |
| 230 | |