aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ByteCode/EvaluationResult.h
blob: 3b6c65eff1ef869b2016989429b59a7e99944f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//===------ EvaluationResult.h - Result class  for the VM -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_AST_INTERP_EVALUATION_RESULT_H
#define LLVM_CLANG_AST_INTERP_EVALUATION_RESULT_H

#include "FunctionPointer.h"
#include "Pointer.h"
#include "clang/AST/APValue.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include <optional>
#include <variant>

namespace clang {
namespace interp {
class EvalEmitter;
class Context;

/// Defines the result of an evaluation.
///
/// The result might be in different forms--one of the pointer types,
/// an APValue, or nothing.
///
/// We use this class to inspect and diagnose the result, as well as
/// convert it to the requested form.
class EvaluationResult final {
public:
  enum ResultKind {
    Empty,   // Initial state.
    LValue,  // Result is an lvalue/pointer.
    RValue,  // Result is an rvalue.
    Invalid, // Result is invalid.
    Valid,   // Result is valid and empty.
  };

  using DeclTy = llvm::PointerUnion<const Decl *, const Expr *>;

private:
  const Context *Ctx = nullptr;
  std::variant<std::monostate, Pointer, FunctionPointer, APValue> Value;
  ResultKind Kind = Empty;
  DeclTy Source = nullptr; // Currently only needed for dump().

  EvaluationResult(ResultKind Kind) : Kind(Kind) {
    // Leave everything empty. Can be used as an
    // error marker or for void return values.
    assert(Kind == Valid || Kind == Invalid);
  }

  void setSource(DeclTy D) { Source = D; }

  void setValue(const APValue &V) {
    // V could still be an LValue.
    assert(empty());
    Value = std::move(V);
    Kind = RValue;
  }
  void setFunctionPointer(const FunctionPointer &P) {
    assert(empty());
    Value = P;
    Kind = LValue;
  }
  void setInvalid() {
    // We are NOT asserting empty() here, since setting it to invalid
    // is allowed even if there is already a result.
    Kind = Invalid;
  }
  void setValid() {
    assert(empty());
    Kind = Valid;
  }

public:
  EvaluationResult(const Context *Ctx) : Ctx(Ctx) {}

  bool empty() const { return Kind == Empty; }
  bool isInvalid() const { return Kind == Invalid; }
  bool isLValue() const { return Kind == LValue; }
  bool isRValue() const { return Kind == RValue; }
  bool isPointer() const { return std::holds_alternative<Pointer>(Value); }

  /// Returns an APValue for the evaluation result. The returned
  /// APValue might be an LValue or RValue.
  APValue toAPValue() const;

  /// If the result is an LValue, convert that to an RValue
  /// and return it. This may fail, e.g. if the result is an
  /// LValue and we can't read from it.
  std::optional<APValue> toRValue() const;

  /// Check that all subobjects of the given pointer have been initialized.
  bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const;
  /// Check that none of the blocks the given pointer (transitively) points
  /// to are dynamically allocated.
  bool checkReturnValue(InterpState &S, const Context &Ctx, const Pointer &Ptr,
                        const SourceInfo &Info);

  QualType getSourceType() const {
    if (const auto *D =
            dyn_cast_if_present<ValueDecl>(Source.dyn_cast<const Decl *>()))
      return D->getType();
    else if (const auto *E = Source.dyn_cast<const Expr *>())
      return E->getType();
    return QualType();
  }

  /// Dump to stderr.
  void dump() const;

  friend class EvalEmitter;
  friend class InterpState;
};

} // namespace interp
} // namespace clang

#endif