1
0
forked from M-Labs/nac3

allow ListObject to have TVar item_type

This commit is contained in:
lyken 2024-08-24 12:37:25 +08:00
parent 2941e8e865
commit c845924c20
No known key found for this signature in database
GPG Key ID: 3BD5FC6AC8325DD8

View File

@ -1,3 +1,5 @@
use inkwell::types::BasicType;
use crate::{
codegen::{model::*, CodeGenContext, CodeGenerator},
typecheck::typedef::{iter_type_vars, Type, TypeEnum},
@ -34,7 +36,8 @@ impl<'ctx, Item: Model<'ctx>> StructKind<'ctx> for List<Item> {
/// A NAC3 Python List object.
#[derive(Debug, Clone, Copy)]
pub struct ListObject<'ctx> {
/// Typechecker type of the list items
/// Typechecker type of the list items. Could be [`TypeEnum::TVar`] if unresolved (like
/// in the case of empty lists and the typechecker does not have enough hints).
pub item_type: Type,
pub instance: Instance<'ctx, Ptr<Struct<List<Any<'ctx>>>>>,
}
@ -63,7 +66,15 @@ impl<'ctx> ListObject<'ctx> {
}
};
let plist = Ptr(Struct(List { item: Any(ctx.get_llvm_type(generator, item_type)) }));
// If `item_type` is unresolved, the list's ptr will default to `size_t*`
// as a placeholder because there are no opaque pointers in LLVM 14.
let item_type_llvm = if let TypeEnum::TVar { .. } = &*ctx.unifier.get_ty(item_type) {
generator.get_size_type(ctx.ctx).as_basic_type_enum()
} else {
ctx.get_llvm_type(generator, item_type)
};
let plist = Ptr(Struct(List { item: Any(item_type_llvm) }));
// Create object
let value = plist.check_value(generator, ctx.ctx, object.value).unwrap();
@ -121,7 +132,13 @@ impl<'ctx> ListObject<'ctx> {
item_type: Type,
len: Instance<'ctx, Int<SizeT>>,
) -> Self {
let item_type_llvm = ctx.get_llvm_type(generator, item_type);
// If `item_type` is unresolved, the list's ptr will default to `size_t*`
// as a placeholder because there are no opaque pointers in LLVM 14.
let item_type_llvm = if let TypeEnum::TVar { .. } = &*ctx.unifier.get_ty(item_type) {
generator.get_size_type(ctx.ctx).as_basic_type_enum()
} else {
ctx.get_llvm_type(generator, item_type)
};
let items = Any(item_type_llvm).array_alloca(generator, ctx, len.value);
let instance = Struct(List { item: Any(item_type_llvm) }).alloca(generator, ctx);