From 5ca2dbeec83bc9089de4c5efca32417d67751c5b Mon Sep 17 00:00:00 2001 From: David Mak Date: Wed, 27 Mar 2024 10:36:02 +0800 Subject: [PATCH] core/typedef: Add Type::obj_id to replace get_obj_id --- nac3artiq/src/codegen.rs | 2 +- nac3artiq/src/symbol_resolver.rs | 4 ++-- nac3core/src/codegen/expr.rs | 6 +++--- nac3core/src/symbol_resolver.rs | 4 ++-- nac3core/src/toplevel/helper.rs | 2 +- nac3core/src/typecheck/typedef/mod.rs | 13 +++++++------ 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index 1d8f6626..bcf023cc 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -624,7 +624,7 @@ pub fn attributes_writeback( let ty = ty.unwrap(); match &*ctx.unifier.get_ty(ty) { 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 // for non-primitive attributes, they should be in another global diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index 3fb9cbbf..4021a560 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -710,7 +710,7 @@ impl InnerResolver { // 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 (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 { unreachable!("cannot be None") @@ -993,7 +993,7 @@ impl InnerResolver { } else if ty_id == self.primitive_ids.option { let option_val_ty = match ctx.unifier.get_ty_immutable(expected_ty).as_ref() { 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 } diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 56e6e0b9..e96878f7 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -154,7 +154,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { SymbolValue::OptionSome(v) => { let ty = match self.unifier.get_ty_immutable(ty).as_ref() { 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 } @@ -168,7 +168,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { SymbolValue::OptionNone => { let ty = match self.unifier.get_ty_immutable(ty).as_ref() { 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 } @@ -1924,7 +1924,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>( // directly generate code for option.unwrap // since it needs to return static value to optimize for kernel invariant if attr == &"unwrap".into() - && id == ctx.primitives.option.get_obj_id(&ctx.unifier) + && id == ctx.primitives.option.obj_id(&ctx.unifier).unwrap() { match val { ValueEnum::Static(v) => return match v.get_field("_nac3_option".into(), ctx) { diff --git a/nac3core/src/symbol_resolver.rs b/nac3core/src/symbol_resolver.rs index de41ac1a..26960754 100644 --- a/nac3core/src/symbol_resolver.rs +++ b/nac3core/src/symbol_resolver.rs @@ -185,13 +185,13 @@ impl SymbolValue { TypeAnnotation::Tuple(vs_tys) } SymbolValue::OptionNone => TypeAnnotation::CustomClass { - id: primitives.option.get_obj_id(unifier), + id: primitives.option.obj_id(unifier).unwrap(), params: Vec::default(), }, SymbolValue::OptionSome(v) => { let ty = v.get_type_annotation(primitives, unifier); TypeAnnotation::CustomClass { - id: primitives.option.get_obj_id(unifier), + id: primitives.option.obj_id(unifier).unwrap(), params: vec![ty], } } diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 8a3908e9..2180d16e 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -549,7 +549,7 @@ impl TopLevelComposer { TypeAnnotation::CustomClass { id: e_id, params: e_param }, ) => { *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.len() == 1 && e_param.len() == 1 diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index 4bfddce5..3ad2b398 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -61,13 +61,14 @@ pub enum RecordKey { } impl Type { - // a wrapper function for cleaner code so that we don't need to - // write this long pattern matching just to get the field `obj_id` - pub fn get_obj_id(self, unifier: &Unifier) -> DefinitionId { - if let TypeEnum::TObj { obj_id, .. } = unifier.get_ty_immutable(self).as_ref() { - *obj_id + /// Wrapper function for cleaner code so that we don't need to write this long pattern matching + /// just to get the field `obj_id`. + #[must_use] + pub fn obj_id(self, unifier: &Unifier) -> Option { + if let TypeEnum::TObj { obj_id, .. } = &*unifier.get_ty_immutable(self) { + Some(*obj_id) } else { - unreachable!("expect a object type") + None } } }