forked from M-Labs/nac3
1
0
Fork 0

core: remove code dup with `make_exception_fields`

This commit is contained in:
lyken 2024-06-17 10:47:49 +08:00
parent 676412fe6d
commit 2abe75d1f4
2 changed files with 39 additions and 51 deletions

View File

@ -1,6 +1,6 @@
use std::iter::once; use std::iter::once;
use helper::{debug_assert_prim_is_allowed, PrimDefDetails}; use helper::{debug_assert_prim_is_allowed, make_exception_fields, PrimDefDetails};
use indexmap::IndexMap; use indexmap::IndexMap;
use inkwell::{ use inkwell::{
attributes::{Attribute, AttributeLoc}, attributes::{Attribute, AttributeLoc},
@ -42,17 +42,7 @@ pub fn get_exn_constructor(
let int32 = primitives.int32; let int32 = primitives.int32;
let int64 = primitives.int64; let int64 = primitives.int64;
let string = primitives.str; let string = primitives.str;
let exception_fields = vec![ let exception_fields = make_exception_fields(int32, int64, string);
("__name__".into(), int32, true),
("__file__".into(), string, true),
("__line__".into(), int32, true),
("__col__".into(), int32, true),
("__func__".into(), string, true),
("__message__".into(), string, true),
("__param0__".into(), int64, true),
("__param1__".into(), int64, true),
("__param2__".into(), int64, true),
];
let exn_cons_args = vec![ let exn_cons_args = vec![
FuncArg { FuncArg {
name: "msg".into(), name: "msg".into(),
@ -65,7 +55,11 @@ pub fn get_exn_constructor(
]; ];
let exn_type = unifier.add_ty(TypeEnum::TObj { let exn_type = unifier.add_ty(TypeEnum::TObj {
obj_id: DefinitionId(class_id), obj_id: DefinitionId(class_id),
fields: exception_fields.iter().map(|(a, b, c)| (*a, (*b, *c))).collect(), fields: exception_fields
.clone()
.into_iter()
.map(|(name, ty, mutable)| (name, (ty, mutable)))
.collect(),
params: VarMap::default(), params: VarMap::default(),
}); });
let signature = unifier.add_ty(TypeEnum::TFunc(FunSignature { let signature = unifier.add_ty(TypeEnum::TFunc(FunSignature {
@ -596,31 +590,17 @@ impl<'a> BuiltinBuilder<'a> {
let PrimitiveStore { int32, int64, str, .. } = *self.primitives; let PrimitiveStore { int32, int64, str, .. } = *self.primitives;
match prim { match prim {
PrimDef::Exception => { PrimDef::Exception => TopLevelDef::Class {
let exception_fields: Vec<(StrRef, Type, bool)> = vec![
("__name__".into(), int32, true),
("__file__".into(), str, true),
("__line__".into(), int32, true),
("__col__".into(), int32, true),
("__func__".into(), str, true),
("__message__".into(), str, true),
("__param0__".into(), int64, true),
("__param1__".into(), int64, true),
("__param2__".into(), int64, true),
];
TopLevelDef::Class {
name: prim.name().into(), name: prim.name().into(),
object_id: prim.id(), object_id: prim.id(),
type_vars: Vec::default(), type_vars: Vec::default(),
fields: exception_fields, fields: make_exception_fields(int32, int64, str),
methods: Vec::default(), methods: Vec::default(),
ancestors: vec![], ancestors: vec![],
constructor: None, constructor: None,
resolver: None, resolver: None,
loc: None, loc: None,
} },
}
_ => unreachable!(), _ => unreachable!(),
} }
} }

View File

@ -269,6 +269,23 @@ pub fn debug_assert_prim_is_allowed(prim: PrimDef, allowlist: &[PrimDef]) {
} }
} }
/// Construct the fields of class `Exception`
/// See [`TypeEnum::TObj::fields`] and [`TopLevelDef::Class::fields`]
#[must_use]
pub fn make_exception_fields(int32: Type, int64: Type, str: Type) -> Vec<(StrRef, Type, bool)> {
vec![
("__name__".into(), int32, true),
("__file__".into(), str, true),
("__line__".into(), int32, true),
("__col__".into(), int32, true),
("__func__".into(), str, true),
("__message__".into(), str, true),
("__param0__".into(), int64, true),
("__param1__".into(), int64, true),
("__param2__".into(), int64, true),
]
}
impl TopLevelDef { impl TopLevelDef {
pub fn to_string(&self, unifier: &mut Unifier) -> String { pub fn to_string(&self, unifier: &mut Unifier) -> String {
match self { match self {
@ -347,19 +364,10 @@ impl TopLevelComposer {
}); });
let exception = unifier.add_ty(TypeEnum::TObj { let exception = unifier.add_ty(TypeEnum::TObj {
obj_id: PrimDef::Exception.id(), obj_id: PrimDef::Exception.id(),
fields: vec![ fields: make_exception_fields(int32, int64, str)
("__name__".into(), (int32, true)),
("__file__".into(), (str, true)),
("__line__".into(), (int32, true)),
("__col__".into(), (int32, true)),
("__func__".into(), (str, true)),
("__message__".into(), (str, true)),
("__param0__".into(), (int64, true)),
("__param1__".into(), (int64, true)),
("__param2__".into(), (int64, true)),
]
.into_iter() .into_iter()
.collect::<HashMap<_, _>>(), .map(|(name, ty, mutable)| (name, (ty, mutable)))
.collect(),
params: VarMap::new(), params: VarMap::new(),
}); });
let uint32 = unifier.add_ty(TypeEnum::TObj { let uint32 = unifier.add_ty(TypeEnum::TObj {