Skip to content

Commit 3b0c031

Browse files
committed
Remove fallback for identify mechanism, always perform checks.
1 parent 7008f3b commit 3b0c031

File tree

6 files changed

+34
-36
lines changed

6 files changed

+34
-36
lines changed

src/core/instructions.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ identify(::Type{Instruction}, ref::API.LLVMValueRef) =
1010
identify(Instruction, Val{API.LLVMGetInstructionOpcode(ref)}())
1111
identify(::Type{Instruction}, ::Val{K}) where {K} = error("Unknown instruction kind $K")
1212

13-
@inline function check(::Type{T}, ref::API.LLVMValueRef) where T<:Instruction
13+
@inline function refcheck(::Type{T}, ref::API.LLVMValueRef) where T<:Instruction
1414
ref==C_NULL && throw(UndefRefError())
15-
if Base.JLOptions().debug_level >= 2
16-
T′ = identify(Instruction, ref)
17-
if T != T′
18-
error("invalid conversion of $T′ instruction reference to $T")
19-
end
15+
T′ = identify(Instruction, ref)
16+
if T != T′
17+
error("invalid conversion of $T′ instruction reference to $T")
2018
end
2119
end
2220

src/core/type.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ identify(::Type{LLVMType}, ref::API.LLVMTypeRef) =
99
identify(LLVMType, Val{API.LLVMGetTypeKind(ref)}())
1010
identify(::Type{LLVMType}, ::Val{K}) where {K} = error("Unknown type kind $K")
1111

12-
@inline function check(::Type{T}, ref::API.LLVMTypeRef) where T<:LLVMType
12+
@inline function refcheck(::Type{T}, ref::API.LLVMTypeRef) where T<:LLVMType
1313
ref==C_NULL && throw(UndefRefError())
14-
if Base.JLOptions().debug_level >= 2
15-
T′ = identify(LLVMType, ref)
16-
if T != T′
17-
error("invalid conversion of $T′ type reference to $T")
18-
end
14+
T′ = identify(LLVMType, ref)
15+
if T != T′
16+
error("invalid conversion of $T′ type reference to $T")
1917
end
2018
end
2119

src/core/value.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ identify(::Type{Value}, ref::API.LLVMValueRef) =
1212
identify(Value, Val{API.LLVMGetValueKind(ref)}())
1313
identify(::Type{Value}, ::Val{K}) where {K} = error("Unknown value kind $K")
1414

15-
@inline function check(::Type{T}, ref::API.LLVMValueRef) where T<:Value
15+
@inline function refcheck(::Type{T}, ref::API.LLVMValueRef) where T<:Value
1616
ref==C_NULL && throw(UndefRefError())
17-
if Base.JLOptions().debug_level >= 2
18-
T′ = identify(Value, ref)
19-
if T != T′
20-
error("invalid conversion of $T′ value reference to $T")
21-
end
17+
T′ = identify(Value, ref)
18+
if T != T′
19+
error("invalid conversion of $T′ value reference to $T")
2220
end
2321
end
2422

src/pass.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export ModulePass
2222
return runner(mod)::Core.Bool
2323
end
2424

25-
return new(API.LLVMCreateModulePass(name, callback), callback)
25+
ref = API.LLVMCreateModulePass(name, callback)
26+
refcheck(ModulePass, ref)
27+
28+
return new(ref, callback)
2629
end
2730
end
2831

@@ -43,7 +46,10 @@ export FunctionPass
4346
return runner(fn)::Core.Bool
4447
end
4548

46-
return new(API.LLVMCreateFunctionPass(name, callback), callback)
49+
ref = API.LLVMCreateFunctionPass(name, callback)
50+
refcheck(FunctionPass, ref)
51+
52+
return new(ref, callback)
4753
end
4854
end
4955

@@ -66,7 +72,10 @@ export BasicBlockPass
6672
return runner(bb)::Core.Bool
6773
end
6874

69-
return new(API.LLVMCreateBasicBlockPass(name, callback), callback)
75+
ref = API.LLVMCreateBasicBlockPass(name, callback)
76+
refcheck(BasicBlockPass, ref)
77+
78+
return new(ref, callback)
7079
end
7180
end
7281

src/util.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ end
1111

1212
# llvm.org/docs/doxygen/html/group__LLVMCSupportTypes.html
1313

14-
# abstract implementations
15-
identify(::Type{T}, ref) where {T} = T(ref)
16-
@inline function check(::Type, ref::Ptr)
17-
ref==C_NULL && throw(UndefRefError())
18-
end
19-
2014
# macro that adds an inner constructor to a type definition,
21-
# calling `check` on the ref field argument
15+
# calling `refcheck` on the ref field argument
2216
macro checked(typedef)
2317
# decode structure definition
2418
if Meta.isexpr(typedef, :macrocall)
@@ -61,12 +55,17 @@ macro checked(typedef)
6155

6256
# insert checked constructor
6357
push!(body.args, :(
64-
$typename($(field_defs...)) = (check($typename, ref); new($(field_names...)))
58+
$typename($(field_defs...)) = (refcheck($typename, ref); new($(field_names...)))
6559
))
6660

6761
return esc(typedef)
6862
end
6963

64+
# the most basic check is asserting that we don't use a null pointer
65+
@inline function refcheck(::Type, ref::Ptr)
66+
ref==C_NULL && throw(UndefRefError())
67+
end
68+
7069

7170
## runtime ccall wrapper
7271

test/core.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ Context() do ctx
2424
@test typeof(typ.ref) == LLVM.API.LLVMTypeRef # untyped
2525

2626
@test typeof(LLVM.IntegerType(typ.ref)) == LLVM.IntegerType # type reconstructed
27-
if Base.JLOptions().debug_level >= 2
28-
@test_throws ErrorException LLVM.FunctionType(typ.ref) # wrong type
29-
end
27+
@test_throws ErrorException LLVM.FunctionType(typ.ref) # wrong type
3028
@test_throws UndefRefError LLVM.FunctionType(LLVM.API.LLVMTypeRef(C_NULL))
3129

3230
@test typeof(typ.ref) == LLVM.API.LLVMTypeRef
@@ -203,12 +201,10 @@ LLVM.Module("SomeModule", ctx) do mod
203201
@test typeof(val.ref) == LLVM.API.LLVMValueRef # untyped
204202

205203
@test typeof(LLVM.Instruction(val.ref)) == LLVM.AllocaInst # type reconstructed
206-
if Base.JLOptions().debug_level >= 2
207-
@test_throws ErrorException LLVM.Function(val.ref) # wrong
208-
end
204+
@test_throws ErrorException LLVM.Function(val.ref) # wrong
209205
@test_throws UndefRefError LLVM.Function(LLVM.API.LLVMValueRef(C_NULL))
210206

211-
@test typeof(Value(val.ref)) == LLVM.AllocaInst # type reconstructed
207+
@test typeof(Value(val.ref)) == LLVM.AllocaInst # type reconstructed
212208
@test_throws UndefRefError Value(LLVM.API.LLVMValueRef(C_NULL))
213209

214210
show(devnull, val)

0 commit comments

Comments
 (0)