From 18db2ddd531e8cffd1a9424392fc1f2545368fe9 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Fri, 6 Aug 2021 10:30:57 +0800 Subject: [PATCH] change the type TypeEnum::TObj {object_id} to DefinitionId as with top_level change TopLevelDef::Class {object_id} to DefinitionId --- nac3core/src/codegen/expr.rs | 4 +-- nac3core/src/top_level.rs | 5 ++-- nac3core/src/typecheck/magic_methods.rs | 2 +- .../src/typecheck/type_inferencer/test.rs | 26 +++++++++---------- nac3core/src/typecheck/typedef/mod.rs | 7 ++--- nac3core/src/typecheck/typedef/test.rs | 10 +++---- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index e0f72ba8..8e629f9b 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -13,7 +13,7 @@ impl<'ctx> CodeGenContext<'ctx> { // we cannot have other types, virtual type should be handled by function calls _ => unreachable!(), }; - let def = &self.top_level.definitions.read()[obj_id]; + let def = &self.top_level.definitions.read()[obj_id.0]; let index = if let TopLevelDef::Class { fields, .. } = &*def.read() { fields.iter().find_position(|x| x.0 == attr).unwrap().0 } else { @@ -137,7 +137,7 @@ impl<'ctx> CodeGenContext<'ctx> { let primitives = &self.top_level.primitives; match &expr.node { ExprKind::Constant { value, .. } => { - let ty = expr.custom.clone().unwrap(); + let ty = expr.custom.unwrap(); self.gen_const(value, ty) } ExprKind::Name { id, .. } => { diff --git a/nac3core/src/top_level.rs b/nac3core/src/top_level.rs index 87d82363..a861e791 100644 --- a/nac3core/src/top_level.rs +++ b/nac3core/src/top_level.rs @@ -7,12 +7,13 @@ use inkwell::{builder::Builder, context::Context, module::Module, values::Pointe use parking_lot::RwLock; use rustpython_parser::ast::Stmt; -pub struct DefinitionId(usize); +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +pub struct DefinitionId(pub usize); pub enum TopLevelDef { Class { // object ID used for TypeEnum - object_id: usize, + object_id: DefinitionId, // type variables bounded to the class. type_vars: Vec, // class fields and method signature. diff --git a/nac3core/src/typecheck/magic_methods.rs b/nac3core/src/typecheck/magic_methods.rs index 4c648699..ff28da5e 100644 --- a/nac3core/src/typecheck/magic_methods.rs +++ b/nac3core/src/typecheck/magic_methods.rs @@ -61,7 +61,7 @@ pub fn comparison_name(op: &Cmpop) -> Option<&'static str> { } } -pub fn impl_binop(unifier: &mut Unifier, store: &PrimitiveStore, ty: Type, other_ty: &[Type], ret_ty: Type, ops: &[ast::Operator]) { +pub fn impl_binop(unifier: &mut Unifier, _store: &PrimitiveStore, ty: Type, other_ty: &[Type], ret_ty: Type, ops: &[ast::Operator]) { if let TypeEnum::TObj {fields, ..} = unifier.get_ty(ty).borrow() { for op in ops { fields.borrow_mut().insert( diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index 4592ec6e..d88fe6d4 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -55,27 +55,27 @@ impl TestEnvironment { let mut unifier = Unifier::new(); let int32 = unifier.add_ty(TypeEnum::TObj { - obj_id: 0, + obj_id: DefinitionId(0), fields: HashMap::new().into(), params: HashMap::new(), }); let int64 = unifier.add_ty(TypeEnum::TObj { - obj_id: 1, + obj_id: DefinitionId(1), fields: HashMap::new().into(), params: HashMap::new(), }); let float = unifier.add_ty(TypeEnum::TObj { - obj_id: 2, + obj_id: DefinitionId(2), fields: HashMap::new().into(), params: HashMap::new(), }); let bool = unifier.add_ty(TypeEnum::TObj { - obj_id: 3, + obj_id: DefinitionId(3), fields: HashMap::new().into(), params: HashMap::new(), }); let none = unifier.add_ty(TypeEnum::TObj { - obj_id: 4, + obj_id: DefinitionId(4), fields: HashMap::new().into(), params: HashMap::new(), }); @@ -120,27 +120,27 @@ impl TestEnvironment { let mut unifier = Unifier::new(); let mut identifier_mapping = HashMap::new(); let int32 = unifier.add_ty(TypeEnum::TObj { - obj_id: 0, + obj_id: DefinitionId(0), fields: HashMap::new().into(), params: HashMap::new(), }); let int64 = unifier.add_ty(TypeEnum::TObj { - obj_id: 1, + obj_id: DefinitionId(1), fields: HashMap::new().into(), params: HashMap::new(), }); let float = unifier.add_ty(TypeEnum::TObj { - obj_id: 2, + obj_id: DefinitionId(2), fields: HashMap::new().into(), params: HashMap::new(), }); let bool = unifier.add_ty(TypeEnum::TObj { - obj_id: 3, + obj_id: DefinitionId(3), fields: HashMap::new().into(), params: HashMap::new(), }); let none = unifier.add_ty(TypeEnum::TObj { - obj_id: 4, + obj_id: DefinitionId(4), fields: HashMap::new().into(), params: HashMap::new(), }); @@ -151,7 +151,7 @@ impl TestEnvironment { let (v0, id) = unifier.get_fresh_var(); let foo_ty = unifier.add_ty(TypeEnum::TObj { - obj_id: 5, + obj_id: DefinitionId(5), fields: [("a".into(), v0)].iter().cloned().collect::>().into(), params: [(id, v0)].iter().cloned().collect(), }); @@ -171,7 +171,7 @@ impl TestEnvironment { vars: Default::default(), })); let bar = unifier.add_ty(TypeEnum::TObj { - obj_id: 6, + obj_id: DefinitionId(6), fields: [("a".into(), int32), ("b".into(), fun)] .iter() .cloned() @@ -189,7 +189,7 @@ impl TestEnvironment { ); let bar2 = unifier.add_ty(TypeEnum::TObj { - obj_id: 7, + obj_id: DefinitionId(7), fields: [("a".into(), bool), ("b".into(), fun)] .iter() .cloned() diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index 93d90bf0..daf7fcfe 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -6,6 +6,7 @@ use std::iter::once; use std::rc::Rc; use std::sync::{Arc, Mutex}; +use crate::top_level::DefinitionId; use super::unification_table::{UnificationKey, UnificationTable}; #[cfg(test)] @@ -64,7 +65,7 @@ pub enum TypeEnum { ty: Type, }, TObj { - obj_id: usize, + obj_id: DefinitionId, fields: RefCell>, params: VarMap, }, @@ -433,7 +434,7 @@ impl Unifier { TObj { obj_id: id2, params: params2, .. }, ) => { if id1 != id2 { - return Err(format!("Cannot unify objects with ID {} and {}", id1, id2)); + return Err(format!("Cannot unify objects with ID {} and {}", id1.0, id2.0)); } for (x, y) in zip(params1.values(), params2.values()) { self.unify(*x, *y)?; @@ -570,7 +571,7 @@ impl Unifier { format!("virtual[{}]", self.stringify(*ty, obj_to_name, var_to_name)) } TypeEnum::TObj { obj_id, params, .. } => { - let name = obj_to_name(*obj_id); + let name = obj_to_name(obj_id.0); if !params.is_empty() { let mut params = params.values().map(|v| self.stringify(*v, obj_to_name, var_to_name)); diff --git a/nac3core/src/typecheck/typedef/test.rs b/nac3core/src/typecheck/typedef/test.rs index f200dc18..0e4a32f8 100644 --- a/nac3core/src/typecheck/typedef/test.rs +++ b/nac3core/src/typecheck/typedef/test.rs @@ -78,7 +78,7 @@ impl TestEnvironment { type_mapping.insert( "int".into(), unifier.add_ty(TypeEnum::TObj { - obj_id: 0, + obj_id: DefinitionId(0), fields: HashMap::new().into(), params: HashMap::new(), }), @@ -86,7 +86,7 @@ impl TestEnvironment { type_mapping.insert( "float".into(), unifier.add_ty(TypeEnum::TObj { - obj_id: 1, + obj_id: DefinitionId(1), fields: HashMap::new().into(), params: HashMap::new(), }), @@ -94,7 +94,7 @@ impl TestEnvironment { type_mapping.insert( "bool".into(), unifier.add_ty(TypeEnum::TObj { - obj_id: 2, + obj_id: DefinitionId(2), fields: HashMap::new().into(), params: HashMap::new(), }), @@ -103,7 +103,7 @@ impl TestEnvironment { type_mapping.insert( "Foo".into(), unifier.add_ty(TypeEnum::TObj { - obj_id: 3, + obj_id: DefinitionId(3), fields: [("a".into(), v0)].iter().cloned().collect::>().into(), params: [(id, v0)].iter().cloned().collect(), }), @@ -334,7 +334,7 @@ fn test_virtual() { vars: HashMap::new(), })); let bar = env.unifier.add_ty(TypeEnum::TObj { - obj_id: 5, + obj_id: DefinitionId(5), fields: [("f".to_string(), fun), ("a".to_string(), int)] .iter() .cloned()