[core] codegen: Normalize RangeType factory functions

Better matches factory functions of other ProxyTypes.
This commit is contained in:
David Mak 2025-01-23 14:55:49 +08:00
parent 13aa590429
commit c36aac323d
3 changed files with 34 additions and 9 deletions

View File

@ -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) {

View File

@ -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());
}

View File

@ -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: CodeGenerator + ?Sized>(_: &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`].