clang 19.0.0git
Value.cpp
Go to the documentation of this file.
1//===------------ Value.cpp - Definition of interpreter value -------------===//
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// This file defines the class that used to represent a value in incremental
10// C++.
11//
12//===----------------------------------------------------------------------===//
13
16#include "clang/AST/Type.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/raw_os_ostream.h"
21#include <cassert>
22#include <cstdint>
23#include <utility>
24
25namespace {
26
27// This is internal buffer maintained by Value, used to hold temporaries.
28class ValueStorage {
29public:
30 using DtorFunc = void (*)(void *);
31
32 static unsigned char *CreatePayload(void *DtorF, size_t AllocSize,
33 size_t ElementsSize) {
34 if (AllocSize < sizeof(Canary))
35 AllocSize = sizeof(Canary);
36 unsigned char *Buf =
37 new unsigned char[ValueStorage::getPayloadOffset() + AllocSize];
38 ValueStorage *VS = new (Buf) ValueStorage(DtorF, AllocSize, ElementsSize);
39 std::memcpy(VS->getPayload(), Canary, sizeof(Canary));
40 return VS->getPayload();
41 }
42
43 unsigned char *getPayload() { return Storage; }
44 const unsigned char *getPayload() const { return Storage; }
45
46 static unsigned getPayloadOffset() {
47 static ValueStorage Dummy(nullptr, 0, 0);
48 return Dummy.getPayload() - reinterpret_cast<unsigned char *>(&Dummy);
49 }
50
51 static ValueStorage *getFromPayload(void *Payload) {
52 ValueStorage *R = reinterpret_cast<ValueStorage *>(
53 (unsigned char *)Payload - getPayloadOffset());
54 return R;
55 }
56
57 void Retain() { ++RefCnt; }
58
59 void Release() {
60 assert(RefCnt > 0 && "Can't release if reference count is already zero");
61 if (--RefCnt == 0) {
62 // We have a non-trivial dtor.
63 if (Dtor && IsAlive()) {
64 assert(Elements && "We at least should have 1 element in Value");
65 size_t Stride = AllocSize / Elements;
66 for (size_t Idx = 0; Idx < Elements; ++Idx)
67 (*Dtor)(getPayload() + Idx * Stride);
68 }
69 delete[] reinterpret_cast<unsigned char *>(this);
70 }
71 }
72
73 // Check whether the storage is valid by validating the canary bits.
74 // If someone accidentally write some invalid bits in the storage, the canary
75 // will be changed first, and `IsAlive` will return false then.
76 bool IsAlive() const {
77 return std::memcmp(getPayload(), Canary, sizeof(Canary)) != 0;
78 }
79
80private:
81 ValueStorage(void *DtorF, size_t AllocSize, size_t ElementsNum)
82 : RefCnt(1), Dtor(reinterpret_cast<DtorFunc>(DtorF)),
83 AllocSize(AllocSize), Elements(ElementsNum) {}
84
85 mutable unsigned RefCnt;
86 DtorFunc Dtor = nullptr;
87 size_t AllocSize = 0;
88 size_t Elements = 0;
89 unsigned char Storage[1];
90
91 // These are some canary bits that are used for protecting the storage been
92 // damaged.
93 static constexpr unsigned char Canary[8] = {0x4c, 0x37, 0xad, 0x8f,
94 0x2d, 0x23, 0x95, 0x91};
95};
96} // namespace
97
98namespace clang {
99
101 if (Ctx.hasSameType(QT, Ctx.VoidTy))
102 return Value::K_Void;
103
104 if (const auto *ET = QT->getAs<EnumType>())
105 QT = ET->getDecl()->getIntegerType();
106
107 const auto *BT = QT->getAs<BuiltinType>();
108 if (!BT || BT->isNullPtrType())
109 return Value::K_PtrOrObj;
110
111 switch (QT->castAs<BuiltinType>()->getKind()) {
112 default:
113 assert(false && "Type not supported");
115#define X(type, name) \
116 case BuiltinType::name: \
117 return Value::K_##name;
119#undef X
120 }
121}
122
123Value::Value(Interpreter *In, void *Ty) : Interp(In), OpaqueType(Ty) {
125 if (ValueKind == K_PtrOrObj) {
127 if ((Canon->isPointerType() || Canon->isObjectType() ||
128 Canon->isReferenceType()) &&
129 (Canon->isRecordType() || Canon->isConstantArrayType() ||
130 Canon->isMemberPointerType())) {
131 IsManuallyAlloc = true;
132 // Compile dtor function.
134 void *DtorF = nullptr;
135 size_t ElementsSize = 1;
136 QualType DtorTy = getType();
137
138 if (const auto *ArrTy =
139 llvm::dyn_cast<ConstantArrayType>(DtorTy.getTypePtr())) {
140 DtorTy = ArrTy->getElementType();
141 llvm::APInt ArrSize(sizeof(size_t) * 8, 1);
142 do {
143 ArrSize *= ArrTy->getSize();
144 ArrTy = llvm::dyn_cast<ConstantArrayType>(
145 ArrTy->getElementType().getTypePtr());
146 } while (ArrTy);
147 ElementsSize = static_cast<size_t>(ArrSize.getZExtValue());
148 }
149 if (const auto *RT = DtorTy->getAs<RecordType>()) {
150 if (CXXRecordDecl *CXXRD =
151 llvm::dyn_cast<CXXRecordDecl>(RT->getDecl())) {
153 Interp.CompileDtorCall(CXXRD))
154 DtorF = reinterpret_cast<void *>(Addr->getValue());
155 else
156 llvm::logAllUnhandledErrors(Addr.takeError(), llvm::errs());
157 }
158 }
159
160 size_t AllocSize =
162 unsigned char *Payload =
163 ValueStorage::CreatePayload(DtorF, AllocSize, ElementsSize);
164 setPtr((void *)Payload);
165 }
166 }
167}
168
170 : Interp(RHS.Interp), OpaqueType(RHS.OpaqueType), Data(RHS.Data),
171 ValueKind(RHS.ValueKind), IsManuallyAlloc(RHS.IsManuallyAlloc) {
172 if (IsManuallyAlloc)
173 ValueStorage::getFromPayload(getPtr())->Retain();
174}
175
176Value::Value(Value &&RHS) noexcept {
177 Interp = std::exchange(RHS.Interp, nullptr);
178 OpaqueType = std::exchange(RHS.OpaqueType, nullptr);
179 Data = RHS.Data;
180 ValueKind = std::exchange(RHS.ValueKind, K_Unspecified);
181 IsManuallyAlloc = std::exchange(RHS.IsManuallyAlloc, false);
182
183 if (IsManuallyAlloc)
184 ValueStorage::getFromPayload(getPtr())->Release();
185}
186
188 if (IsManuallyAlloc)
189 ValueStorage::getFromPayload(getPtr())->Release();
190
191 Interp = RHS.Interp;
193 Data = RHS.Data;
194 ValueKind = RHS.ValueKind;
196
197 if (IsManuallyAlloc)
198 ValueStorage::getFromPayload(getPtr())->Retain();
199
200 return *this;
201}
202
203Value &Value::operator=(Value &&RHS) noexcept {
204 if (this != &RHS) {
205 if (IsManuallyAlloc)
206 ValueStorage::getFromPayload(getPtr())->Release();
207
208 Interp = std::exchange(RHS.Interp, nullptr);
209 OpaqueType = std::exchange(RHS.OpaqueType, nullptr);
210 ValueKind = std::exchange(RHS.ValueKind, K_Unspecified);
211 IsManuallyAlloc = std::exchange(RHS.IsManuallyAlloc, false);
212
213 Data = RHS.Data;
214 }
215 return *this;
216}
217
219 if (IsManuallyAlloc)
220 ValueStorage::getFromPayload(getPtr())->Release();
222 OpaqueType = nullptr;
223 Interp = nullptr;
224 IsManuallyAlloc = false;
225}
226
228
229void *Value::getPtr() const {
230 assert(ValueKind == K_PtrOrObj);
231 return Data.m_Ptr;
232}
233
236}
237
239 assert(Interp != nullptr &&
240 "Can't get interpreter from a default constructed value");
241 return *Interp;
242}
243
245 assert(Interp != nullptr &&
246 "Can't get interpreter from a default constructed value");
247 return *Interp;
248}
249
251
253 return getInterpreter().getASTContext();
254}
255
256void Value::dump() const { print(llvm::outs()); }
257
258void Value::printType(llvm::raw_ostream &Out) const {
259 Out << "Not implement yet.\n";
260}
261void Value::printData(llvm::raw_ostream &Out) const {
262 Out << "Not implement yet.\n";
263}
264void Value::print(llvm::raw_ostream &Out) const {
265 assert(OpaqueType != nullptr && "Can't print default Value");
266 Out << "Not implement yet.\n";
267}
268
269} // namespace clang
Defines the clang::ASTContext interface.
#define REPL_BUILTIN_TYPES
Definition: Value.h:75
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
bool hasSameType(QualType T1, QualType T2) const
Determine whether the given types T1 and T2 are equivalent.
Definition: ASTContext.h:2579
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType VoidTy
Definition: ASTContext.h:1091
This class is used for builtin types like 'int'.
Definition: Type.h:2771
Kind getKind() const
Definition: Type.h:2813
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:5365
Provides top-level interfaces for incremental compilation and execution.
Definition: Interpreter.h:91
llvm::Expected< llvm::orc::ExecutorAddr > CompileDtorCall(CXXRecordDecl *CXXRD)
const ASTContext & getASTContext() const
A (possibly-)qualified type.
Definition: Type.h:738
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7149
static QualType getFromOpaquePtr(const void *Ptr)
Definition: Type.h:787
QualType getCanonicalType() const
Definition: Type.h:7201
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5339
bool isConstantArrayType() const
Definition: Type.h:7472
bool isPointerType() const
Definition: Type.h:7402
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7980
bool isReferenceType() const
Definition: Type.h:7414
bool isMemberPointerType() const
Definition: Type.h:7450
bool isObjectType() const
Determine whether this type is an object type.
Definition: Type.h:2195
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7913
bool isRecordType() const
Definition: Type.h:7496
QualType getType() const
Definition: Value.cpp:234
void setKind(Kind K)
Definition: Value.h:137
void print(llvm::raw_ostream &Out) const
Definition: Value.cpp:264
ASTContext & getASTContext()
Definition: Value.cpp:250
Interpreter & getInterpreter()
Definition: Value.cpp:238
void printType(llvm::raw_ostream &Out) const
Definition: Value.cpp:258
void * OpaqueType
Definition: Value.h:196
Kind ValueKind
Definition: Value.h:198
void dump() const
Definition: Value.cpp:256
Interpreter * Interp
Definition: Value.h:195
Value & operator=(const Value &RHS)
Definition: Value.cpp:187
void clear()
Definition: Value.cpp:218
void printData(llvm::raw_ostream &Out) const
Definition: Value.cpp:261
Value()=default
void * getPtr() const
Definition: Value.cpp:229
bool IsManuallyAlloc
Definition: Value.h:199
void setPtr(void *Ptr)
Definition: Value.h:141
@ K_PtrOrObj
Definition: Value.h:108
@ K_Unspecified
Definition: Value.h:109
Storage Data
Definition: Value.h:197
The JSON file list parser is used to communicate input to InstallAPI.
static Value::Kind ConvertQualTypeToKind(const ASTContext &Ctx, QualType QT)
Definition: Value.cpp:100