diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 37e1bb33..73a28b7a 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -800,7 +800,7 @@ pub fn gen_func_impl< Some(t) => t.as_basic_type_enum(), } }), - (primitives.range, RangeType::new(context).as_base_type().into()), + (primitives.range, RangeType::new_with_generator(generator, context).as_base_type().into()), (primitives.exception, { let name = "Exception"; if let Some(t) = module.get_struct_type(name) { diff --git a/nac3core/src/codegen/test.rs b/nac3core/src/codegen/test.rs index a58a9847..01672c55 100644 --- a/nac3core/src/codegen/test.rs +++ b/nac3core/src/codegen/test.rs @@ -453,8 +453,9 @@ fn test_classes_list_type_new() { #[test] fn test_classes_range_type_new() { let ctx = inkwell::context::Context::create(); + let generator = DefaultCodeGenerator::new(String::new(), ctx.i64_type()); - let llvm_range = RangeType::new(&ctx); + let llvm_range = RangeType::new_with_generator(&generator, &ctx); assert!(RangeType::is_representable(llvm_range.as_base_type()).is_ok()); } diff --git a/nac3core/src/codegen/types/range.rs b/nac3core/src/codegen/types/range.rs index bdd4e79c..b92d7658 100644 --- a/nac3core/src/codegen/types/range.rs +++ b/nac3core/src/codegen/types/range.rs @@ -5,9 +5,12 @@ use inkwell::{ }; use super::ProxyType; -use crate::codegen::{ - values::{ProxyValue, RangeValue}, - {CodeGenContext, CodeGenerator}, +use crate::{ + codegen::{ + values::{ProxyValue, RangeValue}, + {CodeGenContext, CodeGenerator}, + }, + typecheck::typedef::{Type, TypeEnum}, }; /// Proxy type for a `range` type in LLVM. @@ -54,12 +57,33 @@ impl<'ctx> RangeType<'ctx> { llvm_i32.array_type(3).ptr_type(AddressSpace::default()) } - /// Creates an instance of [`RangeType`]. - #[must_use] - pub fn new(ctx: &'ctx Context) -> Self { + fn new_impl(ctx: &'ctx Context) -> Self { let llvm_range = Self::llvm_type(ctx); - RangeType::from_type(llvm_range) + RangeType { ty: llvm_range } + } + + /// Creates an instance of [`RangeType`]. + #[must_use] + pub fn new(ctx: &CodeGenContext<'ctx, '_>) -> Self { + RangeType::new_impl(ctx.ctx) + } + + /// Creates an instance of [`RangeType`]. + #[must_use] + pub fn new_with_generator(_: &G, ctx: &'ctx Context) -> Self { + Self::new_impl(ctx) + } + + /// Creates an [`RangeType`] from a [unifier type][Type]. + #[must_use] + pub fn from_unifier_type(ctx: &mut CodeGenContext<'ctx, '_>, ty: Type) -> Self { + // Check unifier type + assert!( + matches!(&*ctx.unifier.get_ty_immutable(ty), TypeEnum::TObj { obj_id, .. } if *obj_id == ctx.primitives.range.obj_id(&ctx.unifier).unwrap()) + ); + + Self::new(ctx) } /// Creates an [`RangeType`] from a [`PointerType`].