1
0
forked from M-Labs/nac3

formatting

This commit is contained in:
pca006132 2021-08-12 16:36:23 +08:00
parent 8c7ccb626b
commit 1db8378f60
3 changed files with 53 additions and 35 deletions

View File

@ -62,44 +62,53 @@ fn get_llvm_type<'ctx>(
use TypeEnum::*; use TypeEnum::*;
// we assume the type cache should already contain primitive types, // we assume the type cache should already contain primitive types,
// and they should be passed by value instead of passing as pointer. // and they should be passed by value instead of passing as pointer.
type_cache.get(&unifier.get_representative(ty)).cloned().unwrap_or_else(|| match &*unifier.get_ty(ty) { type_cache.get(&unifier.get_representative(ty)).cloned().unwrap_or_else(|| {
TObj { obj_id, fields, .. } => { match &*unifier.get_ty(ty) {
// a struct with fields in the order of declaration TObj { obj_id, fields, .. } => {
let defs = top_level.definitions.read(); // a struct with fields in the order of declaration
let definition = defs.get(obj_id.0).unwrap(); let defs = top_level.definitions.read();
let ty = if let TopLevelDef::Class { fields: fields_list, .. } = &*definition.read() { let definition = defs.get(obj_id.0).unwrap();
let fields = fields.borrow(); let ty = if let TopLevelDef::Class { fields: fields_list, .. } = &*definition.read()
let fields = fields_list {
let fields = fields.borrow();
let fields = fields_list
.iter()
.map(|f| get_llvm_type(ctx, unifier, top_level, type_cache, fields[&f.0]))
.collect_vec();
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
} else {
unreachable!()
};
ty
}
TTuple { ty } => {
// a struct with fields in the order present in the tuple
let fields = ty
.iter() .iter()
.map(|f| get_llvm_type(ctx, unifier, top_level, type_cache, fields[&f.0])) .map(|ty| get_llvm_type(ctx, unifier, top_level, type_cache, *ty))
.collect_vec(); .collect_vec();
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into() ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
} else { }
unreachable!() TList { ty } => {
}; // a struct with an integer and a pointer to an array
ty let element_type = get_llvm_type(ctx, unifier, top_level, type_cache, *ty);
let fields =
[ctx.i32_type().into(), element_type.ptr_type(AddressSpace::Generic).into()];
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
}
TVirtual { .. } => unimplemented!(),
_ => unreachable!(),
} }
TTuple { ty } => {
// a struct with fields in the order present in the tuple
let fields = ty
.iter()
.map(|ty| get_llvm_type(ctx, unifier, top_level, type_cache, *ty))
.collect_vec();
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
}
TList { ty } => {
// a struct with an integer and a pointer to an array
let element_type = get_llvm_type(ctx, unifier, top_level, type_cache, *ty);
let fields =
[ctx.i32_type().into(), element_type.ptr_type(AddressSpace::Generic).into()];
ctx.struct_type(&fields, false).ptr_type(AddressSpace::Generic).into()
}
TVirtual { .. } => unimplemented!(),
_ => unreachable!(),
}) })
} }
pub fn gen_func<'ctx>(context: &'ctx Context, builder: Builder<'ctx>, module: Module<'ctx>, task: CodeGenTask, top_level_ctx: Arc<TopLevelContext>) -> Module<'ctx> { pub fn gen_func<'ctx>(
context: &'ctx Context,
builder: Builder<'ctx>,
module: Module<'ctx>,
task: CodeGenTask,
top_level_ctx: Arc<TopLevelContext>,
) -> Module<'ctx> {
// unwrap_or(0) is for unit tests without using rayon // unwrap_or(0) is for unit tests without using rayon
let (mut unifier, primitives) = { let (mut unifier, primitives) = {
let unifiers = top_level_ctx.unifiers.read(); let unifiers = top_level_ctx.unifiers.read();

View File

@ -95,15 +95,20 @@ pub fn parse_type_annotation<T>(
Subscript { value, slice, .. } => { Subscript { value, slice, .. } => {
if let Name { id, .. } = &value.node { if let Name { id, .. } = &value.node {
if id == "virtual" { if id == "virtual" {
let ty = parse_type_annotation(resolver, top_level, unifier, primitives, slice)?; let ty =
parse_type_annotation(resolver, top_level, unifier, primitives, slice)?;
Ok(unifier.add_ty(TypeEnum::TVirtual { ty })) Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
} else { } else {
let types = if let Tuple { elts, .. } = &slice.node { let types = if let Tuple { elts, .. } = &slice.node {
elts.iter() elts.iter()
.map(|v| parse_type_annotation(resolver, top_level, unifier, primitives, v)) .map(|v| {
parse_type_annotation(resolver, top_level, unifier, primitives, v)
})
.collect::<Result<Vec<_>, _>>()? .collect::<Result<Vec<_>, _>>()?
} else { } else {
vec![parse_type_annotation(resolver, top_level, unifier, primitives, slice)?] vec![parse_type_annotation(
resolver, top_level, unifier, primitives, slice,
)?]
}; };
let obj_id = resolver let obj_id = resolver

View File

@ -696,7 +696,11 @@ impl Unifier {
let fields = self let fields = self
.subst_map(&fields.borrow(), mapping) .subst_map(&fields.borrow(), mapping)
.unwrap_or_else(|| fields.borrow().clone()); .unwrap_or_else(|| fields.borrow().clone());
Some(self.add_ty(TypeEnum::TObj { obj_id, params: params.into(), fields: fields.into() })) Some(self.add_ty(TypeEnum::TObj {
obj_id,
params: params.into(),
fields: fields.into(),
}))
} else { } else {
None None
} }