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: prim.name().into(),
("__name__".into(), int32, true), object_id: prim.id(),
("__file__".into(), str, true), type_vars: Vec::default(),
("__line__".into(), int32, true), fields: make_exception_fields(int32, int64, str),
("__col__".into(), int32, true), methods: Vec::default(),
("__func__".into(), str, true), ancestors: vec![],
("__message__".into(), str, true), constructor: None,
("__param0__".into(), int64, true), resolver: None,
("__param1__".into(), int64, true), loc: None,
("__param2__".into(), int64, true), },
];
TopLevelDef::Class {
name: prim.name().into(),
object_id: prim.id(),
type_vars: Vec::default(),
fields: exception_fields,
methods: Vec::default(),
ancestors: vec![],
constructor: None,
resolver: 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)), .into_iter()
("__file__".into(), (str, true)), .map(|(name, ty, mutable)| (name, (ty, mutable)))
("__line__".into(), (int32, true)), .collect(),
("__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()
.collect::<HashMap<_, _>>(),
params: VarMap::new(), params: VarMap::new(),
}); });
let uint32 = unifier.add_ty(TypeEnum::TObj { let uint32 = unifier.add_ty(TypeEnum::TObj {