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 MemberPointer;
22class Pointer;
23class SourceInfo;
24class InterpState;
25
26/// Defines the result of an evaluation.
27///
28/// The Kind defined if the evaluation was invalid, valid (but empty, e.g. for
29/// void expressions) or if we have a valid evaluation result.
30///
31/// We use this class to inspect and diagnose the result, as well as
32/// convert it to the requested form.
33class EvaluationResult final {
34public:
36 Empty, // Initial state.
37 Invalid, // Result is invalid.
38 Valid, // Result is valid and empty.
39 };
40
41private:
42 const Context &Ctx;
44 ResultKind Kind = Empty;
45 DeclOrExpr Source = nullptr;
46
47 void setSource(DeclOrExpr D) { Source = D; }
48
49 void takeValue(APValue &&V) {
50 assert(empty());
51 Value = std::move(V);
52 Kind = Valid;
53 assert(!empty());
54 }
55 void setInvalid() {
56 // We are NOT asserting empty() here, since setting it to invalid
57 // is allowed even if there is already a result.
58 Kind = Invalid;
59 }
60 void setValid() {
61 assert(empty());
62 Kind = Valid;
63 }
64
65 QualType getStorageType() const;
66
67public:
68 EvaluationResult(const Context &Ctx) : Ctx(Ctx) {}
69
70 bool empty() const { return Kind == Empty; }
71 bool isInvalid() const { return Kind == Invalid; }
72
73 APValue stealAPValue() { return std::move(Value); }
74
75 /// Check that all subobjects of the given pointer have been initialized.
76 bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const;
77 /// Check that none of the blocks the given pointer (transitively) points
78 /// to are dynamically allocated.
80 SourceInfo Info) const;
81
82 /// Check the given pointer as an lvalue, i.e. make sure it's a global
83 /// lvalue and diagnose if it's not.
84 bool checkLValue(InterpState &S, const Pointer &Ptr, SourceInfo Info,
85 ConstantExprKind ConstexprKind) const;
86 /// Check all fields of the given pointer.
87 bool checkLValueFields(InterpState &S, const Pointer &Ptr, SourceInfo Info,
88 ConstantExprKind ConstexprKind) const;
89
90 /// Check if the given member pointer can be returned from an evaluation.
91 static bool checkMemberPointer(InterpState &S, const MemberPointer &MemberPtr,
92 SourceInfo Info,
93 ConstantExprKind ConstexprKind);
94 /// Check if the given function pointer can be returned from an evaluation.
95 bool checkFunctionPointer(InterpState &S, const Pointer &Ptr, SourceInfo Info,
96 ConstantExprKind ConstexprKind) const;
97
99 if (const auto *D = Source.asValueDecl())
100 return D->getType();
101 if (const auto *E = Source.asExpr())
102 return E->getType();
103 return QualType();
104 }
105
106 /// Dump to stderr.
107 void dump() const;
108
109 friend class EvalEmitter;
110 friend class InterpState;
111};
112
113} // namespace interp
114} // namespace clang
115
116#endif
#define V(N, I)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:124
A (possibly-)qualified type.
Definition TypeBase.h:938
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:49
An emitter which evaluates opcodes as they are emitted.
Definition EvalEmitter.h:32
static bool checkMemberPointer(InterpState &S, const MemberPointer &MemberPtr, SourceInfo Info, ConstantExprKind ConstexprKind)
Check if the given member pointer can be returned from an evaluation.
bool checkFunctionPointer(InterpState &S, const Pointer &Ptr, SourceInfo Info, ConstantExprKind ConstexprKind) const
Check if the given function pointer can be returned from an evaluation.
EvaluationResult(const Context &Ctx)
bool checkLValue(InterpState &S, const Pointer &Ptr, SourceInfo Info, ConstantExprKind ConstexprKind) const
Check the given pointer as an lvalue, i.e.
bool checkDynamicAllocations(InterpState &S, const Pointer &Ptr, SourceInfo Info) const
Check that none of the blocks the given pointer (transitively) points to are dynamically allocated.
bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const
Check that all subobjects of the given pointer have been initialized.
void dump() const
Dump to stderr.
Definition Disasm.cpp:639
bool checkLValueFields(InterpState &S, const Pointer &Ptr, SourceInfo Info, ConstantExprKind ConstexprKind) const
Check all fields of the given pointer.
Interpreter context.
Definition InterpState.h:47
A pointer to a memory block, live or dead.
Definition Pointer.h:546
Describes the statement/declaration an opcode was generated from.
Definition Source.h:77
Top level wrappers for InstallAPI frontend operations.
Expr::ConstantExprKind ConstantExprKind
Definition Expr.h:1062