Skip to content

Commit fdf11d2

Browse files
committed
Disallow sizeof queries on LLVM values and types.
1 parent b6b75b6 commit fdf11d2

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

src/core/type.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ export LLVMType, issized, context, show
44
abstract type LLVMType end
55

66
Base.eltype(typ::LLVMType) = Any
7+
Base.sizeof(typ::LLVMType) = error("LLVM types are not sized")
8+
# TODO: expose LLVMSizeOf/LLVMAlignOf, yielding run-time values?
9+
# XXX: can we query type sizes from the data layout or target?
710

811
Base.unsafe_convert(::Type{API.LLVMTypeRef}, typ::LLVMType) = typ.ref
912

src/core/value.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ function Value(ref::API.LLVMValueRef)
3737
end
3838

3939

40-
4140
## general APIs
4241

4342
export llvmtype, llvmeltype, name, name!, replace_uses!, replace_metadata_uses!, isconstant, isundef, ispoison, context
4443

4544
llvmtype(val::Value) = LLVMType(API.LLVMTypeOf(val))
4645
llvmeltype(val::Value) = eltype(llvmtype(val))
4746

47+
# defer size queries to the LLVM type (where we'll error)
48+
Base.sizeof(val::Value) = sizeof(llvmtype(val))
49+
4850
name(val::Value) = unsafe_string(API.LLVMGetValueName(val))
4951
name!(val::Value, name::String) = API.LLVMSetValueName(val, name)
5052

test/core.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ end
4848

4949
@test issized(LLVM.Int1Type(ctx))
5050
@test !issized(LLVM.VoidType(ctx))
51+
@test_throws ErrorException sizeof(typ)
5152
end
5253

5354
# integer
@@ -216,6 +217,7 @@ end
216217
show(devnull, val)
217218

218219
@test llvmtype(val) == LLVM.PointerType(typ)
220+
@test_throws ErrorException sizeof(val)
219221
@test name(val) == "foo"
220222
@test !isconstant(val)
221223
@test !isundef(val)

0 commit comments

Comments
 (0)