From 1b5ac3cd25ccb6b6b4603004702d964820d5dd9d Mon Sep 17 00:00:00 2001 From: pca006132 Date: Wed, 22 Sep 2021 15:56:53 +0800 Subject: [PATCH] nac3core: do not alloc call if it is monomorphic --- nac3core/src/codegen/expr.rs | 16 ++++++++--- nac3core/src/typecheck/type_inferencer/mod.rs | 27 ++++++------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 83547bfa..11df52a9 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -650,10 +650,18 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { ) }); params.extend(kw_iter); - let signature = self - .unifier - .get_call_signature(*self.calls.get(&expr.location.into()).unwrap()) - .unwrap(); + let call = self.calls.get(&expr.location.into()); + let signature = match call { + Some(call) => self.unifier.get_call_signature(*call).unwrap(), + None => { + let ty = func.custom.unwrap(); + if let TypeEnum::TFunc(sign) = &*self.unifier.get_ty(ty) { + sign.borrow().clone() + } else { + unreachable!() + } + } + }; match &func.as_ref().node { ExprKind::Name { id, .. } => { // TODO: handle primitive casts and function pointers diff --git a/nac3core/src/typecheck/type_inferencer/mod.rs b/nac3core/src/typecheck/type_inferencer/mod.rs index f6a61293..608129e4 100644 --- a/nac3core/src/typecheck/type_inferencer/mod.rs +++ b/nac3core/src/typecheck/type_inferencer/mod.rs @@ -180,7 +180,9 @@ impl<'a> fold::Fold<()> for Inferencer<'a> { Some(self.infer_attribute(value, attr)?) } ast::ExprKind::BoolOp { values, .. } => Some(self.infer_bool_ops(values)?), - ast::ExprKind::BinOp { left, op, right } => Some(self.infer_bin_ops(expr.location, left, op, right)?), + ast::ExprKind::BinOp { left, op, right } => { + Some(self.infer_bin_ops(expr.location, left, op, right)?) + } ast::ExprKind::UnaryOp { op, operand } => Some(self.infer_unary_ops(op, operand)?), ast::ExprKind::Compare { left, ops, comparators } => { Some(self.infer_compare(left, ops, comparators)?) @@ -246,17 +248,10 @@ impl<'a> Inferencer<'a> { if let TypeEnum::TFunc(sign) = &*self.unifier.get_ty(*ty) { let sign = sign.borrow(); if sign.vars.is_empty() { - let call = self.unifier.add_call(Call { - posargs: params, - kwargs: HashMap::new(), - fun: RefCell::new(Some(*ty)), - ret: sign.ret, - }); if let Some(ret) = ret { self.unifier.unify(sign.ret, ret).unwrap(); } - self.calls.insert(location.into(), call); - return Ok(sign.ret) + return Ok(sign.ret); } } } @@ -488,17 +483,11 @@ impl<'a> Inferencer<'a> { if let TypeEnum::TFunc(sign) = &*self.unifier.get_ty(func.custom.unwrap()) { let sign = sign.borrow(); if sign.vars.is_empty() { - let call = self.unifier.add_call(Call { - posargs: args.iter().map(|v| v.custom.unwrap()).collect(), - kwargs: keywords - .iter() - .map(|v| (v.node.arg.as_ref().unwrap().clone(), v.custom.unwrap())) - .collect(), - fun: RefCell::new(func.custom), - ret: sign.ret, + return Ok(Located { + location, + custom: Some(sign.ret), + node: ExprKind::Call { func, args, keywords }, }); - self.calls.insert(location.into(), call); - return Ok(Located { location, custom: Some(sign.ret), node: ExprKind::Call { func, args, keywords } }) } }