forked from M-Labs/nac3
1
0
Fork 0

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
1 changed files with 20 additions and 3 deletions

View File

@ -1,3 +1,5 @@
use inkwell::types::BasicType;
use crate::{ use crate::{
codegen::{model::*, CodeGenContext, CodeGenerator}, codegen::{model::*, CodeGenContext, CodeGenerator},
typecheck::typedef::{iter_type_vars, Type, TypeEnum}, 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. /// A NAC3 Python List object.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct ListObject<'ctx> { 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 item_type: Type,
pub instance: Instance<'ctx, Ptr<Struct<List<Any<'ctx>>>>>, 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 // Create object
let value = plist.check_value(generator, ctx.ctx, object.value).unwrap(); let value = plist.check_value(generator, ctx.ctx, object.value).unwrap();
@ -121,7 +132,13 @@ impl<'ctx> ListObject<'ctx> {
item_type: Type, item_type: Type,
len: Instance<'ctx, Int<SizeT>>, len: Instance<'ctx, Int<SizeT>>,
) -> Self { ) -> 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 items = Any(item_type_llvm).array_alloca(generator, ctx, len.value);
let instance = Struct(List { item: Any(item_type_llvm) }).alloca(generator, ctx); let instance = Struct(List { item: Any(item_type_llvm) }).alloca(generator, ctx);