Skip to content

Commit aa577bf

Browse files
committed
Check for pre-existing types in the context.
1 parent dc4028f commit aa577bf

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

src/core/module.jl

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,12 @@ set_used!(mod::Module, values::GlobalVariable...) = nothing
7777
set_compiler_used!(mod::Module, values::GlobalVariable...) = nothing
7878
end
7979

80+
8081
## type iteration
8182

8283
export types
8384

84-
struct ModuleTypeDict <: AbstractDict{String,LLVMType}
85-
mod::Module
86-
end
87-
88-
types(mod::Module) = ModuleTypeDict(mod)
89-
90-
function Base.haskey(iter::ModuleTypeDict, name::String)
91-
return API.LLVMGetTypeByName(iter.mod, name) != C_NULL
92-
end
93-
94-
function Base.getindex(iter::ModuleTypeDict, name::String)
95-
objref = API.LLVMGetTypeByName(iter.mod, name)
96-
objref == C_NULL && throw(KeyError(name))
97-
return LLVMType(objref)
98-
end
85+
@deprecate types(mod::Module) types(context(mod))
9986

10087

10188
## metadata iteration

src/core/type.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,31 @@ identify(::Type{LLVMType}, ::Val{API.LLVMTokenTypeKind}) = TokenType
272272

273273
TokenType(ctx::Context) =
274274
TokenType(API.LLVMTokenTypeInContext(ctx))
275+
276+
277+
## type iteration
278+
279+
export types
280+
281+
struct ContextTypeDict <: AbstractDict{String,LLVMType}
282+
ctx::Context
283+
end
284+
285+
# FIXME: remove on LLVM 12
286+
function LLVMGetTypeByName2(ctx::Context, name)
287+
Module("dummy", ctx) do mod
288+
API.LLVMGetTypeByName(mod, name)
289+
end
290+
end
291+
292+
types(ctx::Context) = ContextTypeDict(ctx)
293+
294+
function Base.haskey(iter::ContextTypeDict, name::String)
295+
return LLVMGetTypeByName2(iter.ctx, name) != C_NULL
296+
end
297+
298+
function Base.getindex(iter::ContextTypeDict, name::String)
299+
objref = LLVMGetTypeByName2(iter.ctx, name)
300+
objref == C_NULL && throw(KeyError(name))
301+
return LLVMType(objref)
302+
end

src/core/value/constant.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ ConstantStruct(typ::StructType, values::Vector{<:Constant}) =
206206
ConstantStruct(API.LLVMConstNamedStruct(typ, values, length(values)))
207207

208208
# create a ConstantStruct from a Julia object
209-
function ConstantStruct(value::T, ctx::Context=GlobalContext();
209+
function ConstantStruct(value::T, ctx::Context=GlobalContext(); name=String(nameof(T)),
210210
anonymous::Core.Bool=false, packed::Core.Bool=false) where {T}
211211
isbitstype(T) || throw(ArgumentError("Can only create a ConstantStruct from an isbits struct"))
212212
constants = Vector{Constant}()
@@ -228,7 +228,9 @@ function ConstantStruct(value::T, ctx::Context=GlobalContext();
228228
if anonymous
229229
ConstantStruct(constants, ctx; packed=packed)
230230
else
231-
typ = StructType(String(nameof(T)), ctx)
231+
# check if this name already exists in the context
232+
haskey(types(ctx), name) && throw(ArgumentError("Type name '$name' is already used in this context. Use a different context, name, or request an anonymous struct."))
233+
typ = StructType(name, ctx)
232234
ConstantStruct(typ, constants)
233235
end
234236
end

0 commit comments

Comments
 (0)