clang 24.0.0git
EvaluationResult.h
Go to the documentation of this file.
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 "DeclOrExpr.h"
13#include "clang/AST/APValue.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/Expr.h"
16
17namespace clang {
18namespace interp {
19class EvalEmitter;
20class Context;
21class Pointer;
22class SourceInfo;
23class InterpState;
24
25/// Defines the result of an evaluation.
26///
27/// The Kind defined if the evaluation was invalid, valid (but empty, e.g. for
28/// void expressions) or if we have a valid evaluation result.
29///
30/// We use this class to inspect and diagnose the result, as well as
31/// convert it to the requested form.
32class EvaluationResult final {
33public:
35 Empty, // Initial state.
36 Invalid, // Result is invalid.
37 Valid, // Result is valid and empty.
38 };
39
40private:
41#ifndef NDEBUG
42 const Context *Ctx = nullptr;
43#endif
45 ResultKind Kind = Empty;
46 DeclOrExpr Source = nullptr;
47
48 void setSource(DeclOrExpr D) { Source = D; }
49
50 void takeValue(APValue &&V) {
51 assert(empty());
52 Value = std::move(V);
53 Kind = Valid;
54 assert(!empty());
55 }
56 void setInvalid() {
57 // We are NOT asserting empty() here, since setting it to invalid
58 // is allowed even if there is already a result.
59 Kind = Invalid;
60 }
61 void setValid() {
62 assert(empty());
63 Kind = Valid;
64 }
65
66public:
67#ifndef NDEBUG
68 EvaluationResult(const Context *Ctx) : Ctx(Ctx) {}
69#else
70 EvaluationResult(const Context *Ctx) {}
71#endif
72
73 bool empty() const { return Kind == Empty; }
74 bool isInvalid() const { return Kind == Invalid; }
75
76 /// Moves the APValue containing the evaluation result to the caller.
77 APValue stealAPValue() { return std::move(Value); }
78
79 /// Check that all subobjects of the given pointer have been initialized.
80 bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const;
81 /// Check that none of the blocks the given pointer (transitively) points
82 /// to are dynamically allocated.
84 SourceInfo Info);
85
87 if (const auto *D = Source.asValueDecl())
88 return D->getType();
89 if (const auto *E = Source.asExpr())
90 return E->getType();
91 return QualType();
92 }
93
94 /// Dump to stderr.
95 void dump() const;
96
97 friend class EvalEmitter;
98 friend class InterpState;
99};
100
101} // namespace interp
102} // namespace clang
103
104#endif
#define V(N, I)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
A (possibly-)qualified type.
Definition TypeBase.h:938
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:47
An emitter which evaluates opcodes as they are emitted.
Definition EvalEmitter.h:31
bool checkDynamicAllocations(InterpState &S, const Pointer &Ptr, SourceInfo Info)
Check that none of the blocks the given pointer (transitively) points to are dynamically allocated.
APValue stealAPValue()
Moves the APValue containing the evaluation result to the caller.
bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const
Check that all subobjects of the given pointer have been initialized.
EvaluationResult(const Context *Ctx)
void dump() const
Dump to stderr.
Definition Disasm.cpp:647
Interpreter context.
Definition InterpState.h:43
A pointer to a memory block, live or dead.
Definition Pointer.h:414
Describes the statement/declaration an opcode was generated from.
Definition Source.h:77
Top level wrappers for InstallAPI frontend operations.