Skip to content

Commit dcdf61c

Browse files
committed
Fix issues and add ValueToString helper
1 parent 456b66b commit dcdf61c

File tree

4 files changed

+251
-318
lines changed

4 files changed

+251
-318
lines changed

clang/include/clang/Interpreter/Interpreter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Interpreter {
224224

225225
std::string ValueDataToString(const Value &V) const;
226226

227-
llvm::Expected<Expr *> convertExprToValue(Expr *E, bool IsOOP = true);
227+
llvm::Expected<Expr *> convertExprToValue(Expr *E, bool IsOOP = false);
228228

229229
// When we deallocate clang::Value we need to run the destructor of the type.
230230
// This function forces emission of the needed dtor.

clang/include/clang/Interpreter/Value.h

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,68 @@ template <> inline void *Value::as() const {
212212

213213
class ValueBuffer {
214214
public:
215+
enum Kind { K_Builtin, K_Array, K_Pointer, K_Unknown };
215216
QualType Ty;
217+
Kind K;
218+
ValueBuffer(Kind K = K_Unknown) : K(K) {}
216219
virtual ~ValueBuffer() = default;
217-
virtual std::string toString() const = 0;
218-
virtual bool isValid() const = 0;
220+
bool isUnknown() const { return K == K_Unknown; }
221+
bool isBuiltin() const { return K == K_Builtin; }
222+
bool isArray() const { return K == K_Array; }
223+
bool isPointer() const { return K == K_Pointer; }
224+
static bool classof(const ValueBuffer *) { return true; }
225+
};
226+
227+
class BuiltinValueBuffer;
228+
class ArrayValueBuffer;
229+
class PointerValueBuffer;
230+
231+
class BuiltinValueBuffer : public ValueBuffer {
232+
public:
233+
std::vector<uint8_t> raw;
234+
BuiltinValueBuffer(QualType _Ty) : ValueBuffer(K_Builtin) { Ty = _Ty; }
235+
template <typename T> T as() const {
236+
T v{};
237+
// assert(raw.size() >= sizeof(T) && "Buffer too small for type!");
238+
memcpy(&v, raw.data(), sizeof(T));
239+
return v;
240+
}
241+
static bool classof(const ValueBuffer *B) { return B->isBuiltin(); }
242+
};
243+
244+
class ArrayValueBuffer : public ValueBuffer {
245+
public:
246+
std::vector<std::unique_ptr<ValueBuffer>> Elements;
247+
ArrayValueBuffer(QualType EleTy) : ValueBuffer(K_Array) { Ty = EleTy; }
248+
249+
static bool classof(const ValueBuffer *B) { return B->isArray(); }
250+
};
251+
252+
class PointerValueBuffer : public ValueBuffer {
253+
public:
254+
uint64_t Address = 0;
255+
std::unique_ptr<ValueBuffer> Pointee; // optional, used only for char*
256+
257+
PointerValueBuffer(QualType _Ty, uint64_t Addr = 0)
258+
: ValueBuffer(K_Pointer), Address(Addr) {
259+
Ty = _Ty;
260+
}
261+
262+
static bool classof(const ValueBuffer *B) { return B->isPointer(); }
263+
};
264+
265+
class ValueToString {
266+
private:
267+
ASTContext &Ctx;
268+
269+
public:
270+
ValueToString(ASTContext &Ctx) : Ctx(Ctx) {}
271+
std::string toString(const ValueBuffer *);
272+
273+
private:
274+
std::string BuiltinToString(const BuiltinValueBuffer &B);
275+
std::string PointerToString(const PointerValueBuffer &P);
276+
std::string ArrayToString(const ArrayValueBuffer &A);
219277
};
220278

221279
class ValueResultManager {
@@ -226,7 +284,7 @@ class ValueResultManager {
226284
explicit ValueResultManager(ASTContext &Ctx, llvm::orc::MemoryAccess &MemAcc);
227285

228286
static std::unique_ptr<ValueResultManager>
229-
Create(llvm::orc::LLJIT &EE, ASTContext &Ctx, bool IsOutOfProcess = true);
287+
Create(llvm::orc::LLJIT &EE, ASTContext &Ctx, bool IsOutOfProcess = false);
230288

231289
ValueId registerPendingResult(QualType QT) {
232290
ValueId NewID = NextID.fetch_add(1, std::memory_order_relaxed);

0 commit comments

Comments
 (0)