core/typedef: Add Type::obj_id to replace get_obj_id

This commit is contained in:
David Mak 2024-03-27 10:36:02 +08:00
parent 9a98cde595
commit 5ca2dbeec8
6 changed files with 16 additions and 15 deletions

View File

@ -624,7 +624,7 @@ pub fn attributes_writeback(
let ty = ty.unwrap(); let ty = ty.unwrap();
match &*ctx.unifier.get_ty(ty) { match &*ctx.unifier.get_ty(ty) {
TypeEnum::TObj { fields, obj_id, .. } TypeEnum::TObj { fields, obj_id, .. }
if *obj_id != ctx.primitives.option.get_obj_id(&ctx.unifier) => if *obj_id != ctx.primitives.option.obj_id(&ctx.unifier).unwrap() =>
{ {
// we only care about primitive attributes // we only care about primitive attributes
// for non-primitive attributes, they should be in another global // for non-primitive attributes, they should be in another global

View File

@ -710,7 +710,7 @@ impl InnerResolver {
// special handling for option type since its class member layout in python side // special handling for option type since its class member layout in python side
// is special and cannot be mapped directly to a nac3 type as below // is special and cannot be mapped directly to a nac3 type as below
(TypeEnum::TObj { obj_id, params, .. }, false) (TypeEnum::TObj { obj_id, params, .. }, false)
if *obj_id == primitives.option.get_obj_id(unifier) => if *obj_id == primitives.option.obj_id(unifier).unwrap() =>
{ {
let Ok(field_data) = obj.getattr("_nac3_option") else { let Ok(field_data) = obj.getattr("_nac3_option") else {
unreachable!("cannot be None") unreachable!("cannot be None")
@ -993,7 +993,7 @@ impl InnerResolver {
} else if ty_id == self.primitive_ids.option { } else if ty_id == self.primitive_ids.option {
let option_val_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() { let option_val_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() {
TypeEnum::TObj { obj_id, params, .. } TypeEnum::TObj { obj_id, params, .. }
if *obj_id == ctx.primitives.option.get_obj_id(&ctx.unifier) => if *obj_id == ctx.primitives.option.obj_id(&ctx.unifier).unwrap() =>
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} }

View File

@ -154,7 +154,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
SymbolValue::OptionSome(v) => { SymbolValue::OptionSome(v) => {
let ty = match self.unifier.get_ty_immutable(ty).as_ref() { let ty = match self.unifier.get_ty_immutable(ty).as_ref() {
TypeEnum::TObj { obj_id, params, .. } TypeEnum::TObj { obj_id, params, .. }
if *obj_id == self.primitives.option.get_obj_id(&self.unifier) => if *obj_id == self.primitives.option.obj_id(&self.unifier).unwrap() =>
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} }
@ -168,7 +168,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
SymbolValue::OptionNone => { SymbolValue::OptionNone => {
let ty = match self.unifier.get_ty_immutable(ty).as_ref() { let ty = match self.unifier.get_ty_immutable(ty).as_ref() {
TypeEnum::TObj { obj_id, params, .. } TypeEnum::TObj { obj_id, params, .. }
if *obj_id == self.primitives.option.get_obj_id(&self.unifier) => if *obj_id == self.primitives.option.obj_id(&self.unifier).unwrap() =>
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} }
@ -1924,7 +1924,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
// directly generate code for option.unwrap // directly generate code for option.unwrap
// since it needs to return static value to optimize for kernel invariant // since it needs to return static value to optimize for kernel invariant
if attr == &"unwrap".into() if attr == &"unwrap".into()
&& id == ctx.primitives.option.get_obj_id(&ctx.unifier) && id == ctx.primitives.option.obj_id(&ctx.unifier).unwrap()
{ {
match val { match val {
ValueEnum::Static(v) => return match v.get_field("_nac3_option".into(), ctx) { ValueEnum::Static(v) => return match v.get_field("_nac3_option".into(), ctx) {

View File

@ -185,13 +185,13 @@ impl SymbolValue {
TypeAnnotation::Tuple(vs_tys) TypeAnnotation::Tuple(vs_tys)
} }
SymbolValue::OptionNone => TypeAnnotation::CustomClass { SymbolValue::OptionNone => TypeAnnotation::CustomClass {
id: primitives.option.get_obj_id(unifier), id: primitives.option.obj_id(unifier).unwrap(),
params: Vec::default(), params: Vec::default(),
}, },
SymbolValue::OptionSome(v) => { SymbolValue::OptionSome(v) => {
let ty = v.get_type_annotation(primitives, unifier); let ty = v.get_type_annotation(primitives, unifier);
TypeAnnotation::CustomClass { TypeAnnotation::CustomClass {
id: primitives.option.get_obj_id(unifier), id: primitives.option.obj_id(unifier).unwrap(),
params: vec![ty], params: vec![ty],
} }
} }

View File

@ -549,7 +549,7 @@ impl TopLevelComposer {
TypeAnnotation::CustomClass { id: e_id, params: e_param }, TypeAnnotation::CustomClass { id: e_id, params: e_param },
) => { ) => {
*f_id == *e_id *f_id == *e_id
&& *f_id == primitive.option.get_obj_id(unifier) && *f_id == primitive.option.obj_id(unifier).unwrap()
&& (f_param.is_empty() && (f_param.is_empty()
|| (f_param.len() == 1 || (f_param.len() == 1
&& e_param.len() == 1 && e_param.len() == 1

View File

@ -61,13 +61,14 @@ pub enum RecordKey {
} }
impl Type { impl Type {
// a wrapper function for cleaner code so that we don't need to /// Wrapper function for cleaner code so that we don't need to write this long pattern matching
// write this long pattern matching just to get the field `obj_id` /// just to get the field `obj_id`.
pub fn get_obj_id(self, unifier: &Unifier) -> DefinitionId { #[must_use]
if let TypeEnum::TObj { obj_id, .. } = unifier.get_ty_immutable(self).as_ref() { pub fn obj_id(self, unifier: &Unifier) -> Option<DefinitionId> {
*obj_id if let TypeEnum::TObj { obj_id, .. } = &*unifier.get_ty_immutable(self) {
Some(*obj_id)
} else { } else {
unreachable!("expect a object type") None
} }
} }
} }