aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ByteCode/EvaluationResult.h
blob: c296cc98ca375d81710ca90fc0e56bf80ed6f1fe (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
//===------ 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 "clang/AST/APValue.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"

namespace clang {
namespace interp {
class EvalEmitter;
class Context;
class Pointer;
class SourceInfo;
class InterpState;

/// Defines the result of an evaluation.
///
/// The Kind defined if the evaluation was invalid, valid (but empty, e.g. for
/// void expressions) or if we have a valid evaluation result.
///
/// 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.
    Invalid, // Result is invalid.
    Valid,   // Result is valid and empty.
  };

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

private:
#ifndef NDEBUG
  const Context *Ctx = nullptr;
#endif
  APValue Value;
  ResultKind Kind = Empty;
  DeclTy Source = nullptr;

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

  void takeValue(APValue &&V) {
    assert(empty());
    Value = std::move(V);
  }
  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:
#ifndef NDEBUG
  EvaluationResult(const Context *Ctx) : Ctx(Ctx) {}
#else
  EvaluationResult(const Context *Ctx) {}
#endif

  bool empty() const { return Kind == Empty; }
  bool isInvalid() const { return Kind == Invalid; }

  /// Returns an APValue for the evaluation result.
  APValue toAPValue() const {
    assert(!empty());
    assert(!isInvalid());
    return Value;
  }

  APValue stealAPValue() { return std::move(Value); }

  /// 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();
    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