| 1 | //===------ EvaluationResult.h - Result class for the VM -------*- 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 | #ifndef LLVM_CLANG_AST_INTERP_EVALUATION_RESULT_H |
| 10 | #define LLVM_CLANG_AST_INTERP_EVALUATION_RESULT_H |
| 11 | |
| 12 | #include "clang/AST/APValue.h" |
| 13 | #include "clang/AST/Decl.h" |
| 14 | #include "clang/AST/Expr.h" |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace interp { |
| 18 | class EvalEmitter; |
| 19 | class Context; |
| 20 | class Pointer; |
| 21 | class SourceInfo; |
| 22 | class InterpState; |
| 23 | |
| 24 | /// Defines the result of an evaluation. |
| 25 | /// |
| 26 | /// The Kind defined if the evaluation was invalid, valid (but empty, e.g. for |
| 27 | /// void expressions) or if we have a valid evaluation result. |
| 28 | /// |
| 29 | /// We use this class to inspect and diagnose the result, as well as |
| 30 | /// convert it to the requested form. |
| 31 | class EvaluationResult final { |
| 32 | public: |
| 33 | enum ResultKind { |
| 34 | Empty, // Initial state. |
| 35 | Invalid, // Result is invalid. |
| 36 | Valid, // Result is valid and empty. |
| 37 | }; |
| 38 | |
| 39 | using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>; |
| 40 | |
| 41 | private: |
| 42 | #ifndef NDEBUG |
| 43 | const Context *Ctx = nullptr; |
| 44 | #endif |
| 45 | APValue Value; |
| 46 | ResultKind Kind = Empty; |
| 47 | DeclTy Source = nullptr; |
| 48 | |
| 49 | void setSource(DeclTy D) { Source = D; } |
| 50 | |
| 51 | void takeValue(APValue &&V) { |
| 52 | assert(empty()); |
| 53 | Value = std::move(V); |
| 54 | Kind = Valid; |
| 55 | assert(!empty()); |
| 56 | } |
| 57 | void setInvalid() { |
| 58 | // We are NOT asserting empty() here, since setting it to invalid |
| 59 | // is allowed even if there is already a result. |
| 60 | Kind = Invalid; |
| 61 | } |
| 62 | void setValid() { |
| 63 | assert(empty()); |
| 64 | Kind = Valid; |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | #ifndef NDEBUG |
| 69 | EvaluationResult(const Context *Ctx) : Ctx(Ctx) {} |
| 70 | #else |
| 71 | EvaluationResult(const Context *Ctx) {} |
| 72 | #endif |
| 73 | |
| 74 | bool empty() const { return Kind == Empty; } |
| 75 | bool isInvalid() const { return Kind == Invalid; } |
| 76 | |
| 77 | /// Moves the APValue containing the evaluation result to the caller. |
| 78 | APValue stealAPValue() { return std::move(Value); } |
| 79 | |
| 80 | /// Check that all subobjects of the given pointer have been initialized. |
| 81 | bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const; |
| 82 | /// Check that none of the blocks the given pointer (transitively) points |
| 83 | /// to are dynamically allocated. |
| 84 | bool checkDynamicAllocations(InterpState &S, const Context &Ctx, |
| 85 | const Pointer &Ptr, SourceInfo Info); |
| 86 | |
| 87 | QualType getSourceType() const { |
| 88 | if (const auto *D = |
| 89 | dyn_cast_if_present<ValueDecl>(Val: Source.dyn_cast<const Decl *>())) |
| 90 | return D->getType(); |
| 91 | if (const auto *E = Source.dyn_cast<const Expr *>()) |
| 92 | return E->getType(); |
| 93 | return QualType(); |
| 94 | } |
| 95 | |
| 96 | /// Dump to stderr. |
| 97 | void dump() const; |
| 98 | |
| 99 | friend class EvalEmitter; |
| 100 | friend class InterpState; |
| 101 | }; |
| 102 | |
| 103 | } // namespace interp |
| 104 | } // namespace clang |
| 105 | |
| 106 | #endif |
| 107 | |