From b4af06052310c3ef806bf60c89b0d8e1bb502f7a Mon Sep 17 00:00:00 2001 From: Teng Zhang Date: Sat, 6 Jul 2024 14:02:20 -0700 Subject: [PATCH] refactor vector --- .../move-model/src/builder/exp_builder.rs | 103 ++++++------------ 1 file changed, 31 insertions(+), 72 deletions(-) diff --git a/third_party/move/move-model/src/builder/exp_builder.rs b/third_party/move/move-model/src/builder/exp_builder.rs index 058e5fc3ec1409..3b78f152e1efa8 100644 --- a/third_party/move/move-model/src/builder/exp_builder.rs +++ b/third_party/move/move-model/src/builder/exp_builder.rs @@ -3326,12 +3326,19 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo } else { expected_type.clone() }; - let _vec_exp_e = self.translate_exp_in_context( - vec_exp, - &Type::Vector(Box::new(inner_ty.clone())), + let ref_vec_exp_e = &sp( + vec_exp.loc, + EA::Exp_::Borrow(mutable, Box::new(vec_exp.clone())), + ); + let vec_exp_e = self.translate_exp_in_context( + ref_vec_exp_e, + &Type::Reference( + ReferenceKind::from_is_mut(mutable), + Box::new(Type::Vector(Box::new(inner_ty.clone()))), + ), context, ); - let _idx_exp_e = + let idx_exp_e = self.translate_exp_in_context(idx_exp, &Type::Primitive(PrimitiveType::U64), context); if self.had_errors { return self.new_error_exp(); @@ -3342,78 +3349,30 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo "vector::borrow" }; // TODO: comment out because of #13888 - // for m in self.env().get_modules() { - // if m.is_std_vector() { - // let mid = m.get_id(); - // for f in m.get_functions() { - // if f.get_full_name_str() == target_str { - // let fid = f.get_id(); - // let instantiated_inner = self.subs.specialize(&inner_ty); - // let node_id = self.env().new_node( - // loc.clone(), - // Type::Reference( - // ReferenceKind::from_is_mut(mutable), - // Box::new(instantiated_inner.clone()), - // ), - // ); - // self.set_node_instantiation(node_id, vec![inner_ty.clone()]); - // let ref_type = Type::Reference( - // ReferenceKind::from_is_mut(mutable), - // Box::new(self.get_node_type(vec_exp_e.node_id())), - // ); - // let ref_id = self.env().new_node(loc.clone(), ref_type); - // let exp = ExpData::Call( - // ref_id, - // Operation::Borrow(ReferenceKind::from_is_mut(mutable)), - // vec![vec_exp_e.clone().into_exp()], - // ); - // let _call = - // ExpData::Call(node_id, Operation::MoveFunction(mid, fid), vec![ - // exp.into_exp(), - // idx_exp_e.clone().into_exp(), - // ]); - // // return call; - // } - // } - // } - // } - let mut symbol_opt = None; - let mut module_opt = None; - for (name, mid) in &self.parent.parent.module_table { - let name_str = name.name().display(self.symbol_pool()).to_string(); - if name_str == "vector" || name.addr().is_one() { - let module_env = self.parent.parent.env.get_module(*mid); - for fenv in module_env.get_functions() { - if fenv.get_full_name_str() == target_str { - let symbol = fenv.get_name(); - symbol_opt = Some(symbol); - module_opt = Some(name.clone()); - break; + for m in self.env().get_modules() { + if m.is_std_vector() { + let mid = m.get_id(); + for f in m.get_functions() { + if f.get_full_name_str() == target_str { + let fid = f.get_id(); + let instantiated_inner = self.subs.specialize(&inner_ty); + let node_id = self.env().new_node( + loc.clone(), + Type::Reference( + ReferenceKind::from_is_mut(mutable), + Box::new(instantiated_inner.clone()), + ), + ); + self.set_node_instantiation(node_id, vec![inner_ty.clone()]); + let call = ExpData::Call(node_id, Operation::MoveFunction(mid, fid), vec![ + vec_exp_e.into_exp(), + idx_exp_e.clone().into_exp(), + ]); + return call; } } - break; } } - if let (Some(symbol), Some(module_name)) = (symbol_opt, module_opt) { - let name_loc = &self.to_loc(&vec_exp.loc); - return self.translate_call( - loc, - name_loc, - CallKind::Regular, - &Some(module_name.clone()), - symbol, - &None, - &[ - &sp( - vec_exp.loc, - EA::Exp_::Borrow(mutable, Box::new(vec_exp.clone())), - ), - idx_exp, - ], - expected_type, - context, - ); - } ExpData::Invalid(self.env().new_node_id()) }