Skip to content

Commit eadb5be

Browse files
committed
Maintain context after translating LLVMTypeRef into IRType
1 parent 98580c4 commit eadb5be

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Sources/LLVM/IRType.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@ public extension IRType {
4242
}
4343

4444
internal func convertType(_ type: LLVMTypeRef) -> IRType {
45+
let context = Context(llvm: LLVMGetTypeContext(type))
4546
switch LLVMGetTypeKind(type) {
46-
case LLVMVoidTypeKind:
47-
return VoidType()
48-
case LLVMHalfTypeKind:
49-
return FloatType.half
50-
case LLVMFloatTypeKind: return FloatType.float
51-
case LLVMDoubleTypeKind: return FloatType.double
52-
case LLVMX86_FP80TypeKind: return FloatType.x86FP80
53-
case LLVMFP128TypeKind: return FloatType.fp128
54-
case LLVMPPC_FP128TypeKind: return FloatType.fp128
55-
case LLVMLabelTypeKind: return LabelType()
47+
case LLVMVoidTypeKind: return VoidType(in: context)
48+
case LLVMFloatTypeKind: return FloatType(kind: .float, in: context)
49+
case LLVMHalfTypeKind: return FloatType(kind: .half, in: context)
50+
case LLVMDoubleTypeKind: return FloatType(kind: .double, in: context)
51+
case LLVMX86_FP80TypeKind: return FloatType(kind: .x86FP80, in: context)
52+
case LLVMFP128TypeKind: return FloatType(kind: .fp128, in: context)
53+
case LLVMPPC_FP128TypeKind: return FloatType(kind: .fp128, in: context)
54+
case LLVMLabelTypeKind: return LabelType(in: context)
5655
case LLVMIntegerTypeKind:
5756
let width = LLVMGetIntTypeWidth(type)
58-
return IntType(width: Int(width))
57+
return IntType(width: Int(width), in: context)
5958
case LLVMFunctionTypeKind:
6059
var params = [IRType]()
6160
let count = Int(LLVMCountParamTypes(type))
@@ -86,7 +85,7 @@ internal func convertType(_ type: LLVMTypeRef) -> IRType {
8685
case LLVMMetadataTypeKind:
8786
return MetadataType(llvm: type)
8887
case LLVMX86_MMXTypeKind:
89-
return X86MMXType()
88+
return X86MMXType(in: context)
9089
case LLVMTokenTypeKind:
9190
return TokenType(llvm: type)
9291
default: fatalError("unknown type kind for type \(type)")

Sources/LLVM/LabelType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct LabelType: IRType {
1212
///
1313
/// - parameter context: The context to create this type in
1414
/// - SeeAlso: http://llvm.org/docs/ProgrammersManual.html#achieving-isolation-with-llvmcontext
15-
public init(context: Context? = nil) {
15+
public init(in context: Context? = nil) {
1616
self.context = context
1717
}
1818

Sources/LLVM/X86MMXType.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@ import cllvm
99
/// represented as intrinsic or asm calls with arguments and/or results of this
1010
/// type. There are no arrays, vectors or constants of this type.
1111
public struct X86MMXType: IRType {
12+
13+
/// Returns the context associated with this module.
14+
public let context: Context?
15+
1216
/// Creates an `X86MMXType`.
13-
public init() {}
17+
///
18+
/// - parameter context: The context to create this type in
19+
/// - SeeAlso: http://llvm.org/docs/ProgrammersManual.html#achieving-isolation-with-llvmcontext
20+
public init(in context: Context? = nil) {
21+
self.context = context
22+
}
1423

1524
/// Retrieves the underlying LLVM type object.
1625
public func asLLVM() -> LLVMTypeRef {
26+
if let context = context {
27+
return LLVMX86MMXTypeInContext(context.llvm)
28+
}
1729
return LLVMX86MMXType()
1830
}
1931
}

0 commit comments

Comments
 (0)