forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValue.cpp
269 lines (227 loc) · 8.36 KB
/
Value.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//===------------ Value.cpp - Definition of interpreter value -------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file defines the class that used to represent a value in incremental
// C++.
//
//===----------------------------------------------------------------------===//
#include "clang/Interpreter/Value.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Type.h"
#include "clang/Interpreter/Interpreter.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_os_ostream.h"
#include <cassert>
#include <cstdint>
#include <utility>
namespace {
// This is internal buffer maintained by Value, used to hold temporaries.
class ValueStorage {
public:
using DtorFunc = void (*)(void *);
static unsigned char *CreatePayload(void *DtorF, size_t AllocSize,
size_t ElementsSize) {
if (AllocSize < sizeof(Canary))
AllocSize = sizeof(Canary);
unsigned char *Buf =
new unsigned char[ValueStorage::getPayloadOffset() + AllocSize];
ValueStorage *VS = new (Buf) ValueStorage(DtorF, AllocSize, ElementsSize);
std::memcpy(VS->getPayload(), Canary, sizeof(Canary));
return VS->getPayload();
}
unsigned char *getPayload() { return Storage; }
const unsigned char *getPayload() const { return Storage; }
static unsigned getPayloadOffset() {
static ValueStorage Dummy(nullptr, 0, 0);
return Dummy.getPayload() - reinterpret_cast<unsigned char *>(&Dummy);
}
static ValueStorage *getFromPayload(void *Payload) {
ValueStorage *R = reinterpret_cast<ValueStorage *>(
(unsigned char *)Payload - getPayloadOffset());
return R;
}
void Retain() { ++RefCnt; }
void Release() {
assert(RefCnt > 0 && "Can't release if reference count is already zero");
if (--RefCnt == 0) {
// We have a non-trivial dtor.
if (Dtor && IsAlive()) {
assert(Elements && "We at least should have 1 element in Value");
size_t Stride = AllocSize / Elements;
for (size_t Idx = 0; Idx < Elements; ++Idx)
(*Dtor)(getPayload() + Idx * Stride);
}
delete[] reinterpret_cast<unsigned char *>(this);
}
}
// Check whether the storage is valid by validating the canary bits.
// If someone accidentally write some invalid bits in the storage, the canary
// will be changed first, and `IsAlive` will return false then.
bool IsAlive() const {
return std::memcmp(getPayload(), Canary, sizeof(Canary)) != 0;
}
private:
ValueStorage(void *DtorF, size_t AllocSize, size_t ElementsNum)
: RefCnt(1), Dtor(reinterpret_cast<DtorFunc>(DtorF)),
AllocSize(AllocSize), Elements(ElementsNum) {}
mutable unsigned RefCnt;
DtorFunc Dtor = nullptr;
size_t AllocSize = 0;
size_t Elements = 0;
unsigned char Storage[1];
// These are some canary bits that are used for protecting the storage been
// damaged.
static constexpr unsigned char Canary[8] = {0x4c, 0x37, 0xad, 0x8f,
0x2d, 0x23, 0x95, 0x91};
};
} // namespace
namespace clang {
static Value::Kind ConvertQualTypeToKind(const ASTContext &Ctx, QualType QT) {
if (Ctx.hasSameType(QT, Ctx.VoidTy))
return Value::K_Void;
if (const auto *ET = QT->getAs<EnumType>())
QT = ET->getDecl()->getIntegerType();
const auto *BT = QT->getAs<BuiltinType>();
if (!BT || BT->isNullPtrType())
return Value::K_PtrOrObj;
switch (QT->castAs<BuiltinType>()->getKind()) {
default:
assert(false && "Type not supported");
return Value::K_Unspecified;
#define X(type, name) \
case BuiltinType::name: \
return Value::K_##name;
REPL_BUILTIN_TYPES
#undef X
}
}
Value::Value(Interpreter *In, void *Ty) : Interp(In), OpaqueType(Ty) {
setKind(ConvertQualTypeToKind(getASTContext(), getType()));
if (ValueKind == K_PtrOrObj) {
QualType Canon = getType().getCanonicalType();
if ((Canon->isPointerType() || Canon->isObjectType() ||
Canon->isReferenceType()) &&
(Canon->isRecordType() || Canon->isConstantArrayType() ||
Canon->isMemberPointerType())) {
IsManuallyAlloc = true;
// Compile dtor function.
Interpreter &Interp = getInterpreter();
void *DtorF = nullptr;
size_t ElementsSize = 1;
QualType DtorTy = getType();
if (const auto *ArrTy =
llvm::dyn_cast<ConstantArrayType>(DtorTy.getTypePtr())) {
DtorTy = ArrTy->getElementType();
llvm::APInt ArrSize(sizeof(size_t) * 8, 1);
do {
ArrSize *= ArrTy->getSize();
ArrTy = llvm::dyn_cast<ConstantArrayType>(
ArrTy->getElementType().getTypePtr());
} while (ArrTy);
ElementsSize = static_cast<size_t>(ArrSize.getZExtValue());
}
if (const auto *RT = DtorTy->getAs<RecordType>()) {
if (CXXRecordDecl *CXXRD =
llvm::dyn_cast<CXXRecordDecl>(RT->getDecl())) {
if (llvm::Expected<llvm::orc::ExecutorAddr> Addr =
Interp.CompileDtorCall(CXXRD))
DtorF = reinterpret_cast<void *>(Addr->getValue());
else
llvm::logAllUnhandledErrors(Addr.takeError(), llvm::errs());
}
}
size_t AllocSize =
getASTContext().getTypeSizeInChars(getType()).getQuantity();
unsigned char *Payload =
ValueStorage::CreatePayload(DtorF, AllocSize, ElementsSize);
setPtr((void *)Payload);
}
}
}
Value::Value(const Value &RHS)
: Interp(RHS.Interp), OpaqueType(RHS.OpaqueType), Data(RHS.Data),
ValueKind(RHS.ValueKind), IsManuallyAlloc(RHS.IsManuallyAlloc) {
if (IsManuallyAlloc)
ValueStorage::getFromPayload(getPtr())->Retain();
}
Value::Value(Value &&RHS) noexcept {
Interp = std::exchange(RHS.Interp, nullptr);
OpaqueType = std::exchange(RHS.OpaqueType, nullptr);
Data = RHS.Data;
ValueKind = std::exchange(RHS.ValueKind, K_Unspecified);
IsManuallyAlloc = std::exchange(RHS.IsManuallyAlloc, false);
if (IsManuallyAlloc)
ValueStorage::getFromPayload(getPtr())->Release();
}
Value &Value::operator=(const Value &RHS) {
if (IsManuallyAlloc)
ValueStorage::getFromPayload(getPtr())->Release();
Interp = RHS.Interp;
OpaqueType = RHS.OpaqueType;
Data = RHS.Data;
ValueKind = RHS.ValueKind;
IsManuallyAlloc = RHS.IsManuallyAlloc;
if (IsManuallyAlloc)
ValueStorage::getFromPayload(getPtr())->Retain();
return *this;
}
Value &Value::operator=(Value &&RHS) noexcept {
if (this != &RHS) {
if (IsManuallyAlloc)
ValueStorage::getFromPayload(getPtr())->Release();
Interp = std::exchange(RHS.Interp, nullptr);
OpaqueType = std::exchange(RHS.OpaqueType, nullptr);
ValueKind = std::exchange(RHS.ValueKind, K_Unspecified);
IsManuallyAlloc = std::exchange(RHS.IsManuallyAlloc, false);
Data = RHS.Data;
}
return *this;
}
void Value::clear() {
if (IsManuallyAlloc)
ValueStorage::getFromPayload(getPtr())->Release();
ValueKind = K_Unspecified;
OpaqueType = nullptr;
Interp = nullptr;
IsManuallyAlloc = false;
}
Value::~Value() { clear(); }
void *Value::getPtr() const {
assert(ValueKind == K_PtrOrObj);
return Data.m_Ptr;
}
QualType Value::getType() const {
return QualType::getFromOpaquePtr(OpaqueType);
}
Interpreter &Value::getInterpreter() {
assert(Interp != nullptr &&
"Can't get interpreter from a default constructed value");
return *Interp;
}
const Interpreter &Value::getInterpreter() const {
assert(Interp != nullptr &&
"Can't get interpreter from a default constructed value");
return *Interp;
}
ASTContext &Value::getASTContext() { return getInterpreter().getASTContext(); }
const ASTContext &Value::getASTContext() const {
return getInterpreter().getASTContext();
}
void Value::dump() const { print(llvm::outs()); }
void Value::printType(llvm::raw_ostream &Out) const {
Out << "Not implement yet.\n";
}
void Value::printData(llvm::raw_ostream &Out) const {
Out << "Not implement yet.\n";
}
void Value::print(llvm::raw_ostream &Out) const {
assert(OpaqueType != nullptr && "Can't print default Value");
Out << "Not implement yet.\n";
}
} // namespace clang