public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Refactor method call type checking
@ 2022-10-22 10:48 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-10-22 10:48 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:662a7a90305f3c5df24765ec6de6c4e56187ae1a
commit 662a7a90305f3c5df24765ec6de6c4e56187ae1a
Author: Philip Herron <philip.herron@embecosm.com>
Date: Wed Oct 19 10:59:18 2022 +0100
Refactor method call type checking
We used a visitor interface to handle type checking argument passing on
method call expressions. This was completely unnessecary as all method
calls should _always_ be to TyTy::FnType's. The benifit here is that we
need to reuse this interface to handle method resolution type checking
for FnTrait calls as closure calls dont have an HIR::MethodExpr so we
need a more abstract interface so that we can specify the types directly
with relevant location info.
Diff:
---
gcc/rust/typecheck/rust-hir-type-check-expr.cc | 3 +-
gcc/rust/typecheck/rust-tyty-call.cc | 98 ++++++++++++++++++--------
gcc/rust/typecheck/rust-tyty-call.h | 79 ++++++++++-----------
3 files changed, 106 insertions(+), 74 deletions(-)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index df4ddfab1dc..aa044ddb9c8 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -1162,7 +1162,8 @@ TypeCheckExpr::visit (HIR::MethodCallExpr &expr)
rust_debug ("type-checking method_call: {%s}", lookup->debug_str ().c_str ());
TyTy::BaseType *function_ret_tyty
- = TyTy::TypeCheckMethodCallExpr::go (lookup, expr, adjusted_self, context);
+ = TyTy::TypeCheckMethodCallExpr::go (static_cast<TyTy::FnType *> (lookup),
+ expr, adjusted_self, context);
if (function_ret_tyty == nullptr
|| function_ret_tyty->get_kind () == TyTy::TypeKind::ERROR)
{
diff --git a/gcc/rust/typecheck/rust-tyty-call.cc b/gcc/rust/typecheck/rust-tyty-call.cc
index 49aa2ef406e..18a34be09bf 100644
--- a/gcc/rust/typecheck/rust-tyty-call.cc
+++ b/gcc/rust/typecheck/rust-tyty-call.cc
@@ -216,29 +216,77 @@ TypeCheckCallExpr::visit (FnPtr &type)
// method call checker
-void
-TypeCheckMethodCallExpr::visit (FnType &type)
+TypeCheckMethodCallExpr::TypeCheckMethodCallExpr (
+ Analysis::NodeMapping call_mappings, std::vector<Argument> &args,
+ Location call_locus, Location receiver_locus, TyTy::BaseType *adjusted_self,
+ Resolver::TypeCheckContext *context)
+ : call_mappings (call_mappings), arguments (args), call_locus (call_locus),
+ receiver_locus (receiver_locus), adjusted_self (adjusted_self),
+ context (context), mappings (Analysis::Mappings::get ())
+{}
+
+BaseType *
+TypeCheckMethodCallExpr::go (FnType *ref, HIR::MethodCallExpr &call,
+ TyTy::BaseType *adjusted_self,
+ Resolver::TypeCheckContext *context)
+{
+ std::vector<Argument> args;
+ for (auto &arg : call.get_arguments ())
+ {
+ BaseType *argument_expr_tyty
+ = Resolver::TypeCheckExpr::Resolve (arg.get ());
+ if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
+ {
+ rust_error_at (arg->get_locus (),
+ "failed to resolve type for argument");
+ return new ErrorType (ref->get_ref ());
+ }
+
+ Argument a (arg->get_mappings (), argument_expr_tyty, arg->get_locus ());
+ args.push_back (std::move (a));
+ }
+
+ TypeCheckMethodCallExpr checker (call.get_mappings (), args,
+ call.get_locus (),
+ call.get_receiver ()->get_locus (),
+ adjusted_self, context);
+ return checker.check (*ref);
+}
+
+BaseType *
+TypeCheckMethodCallExpr::go (FnType *ref, Analysis::NodeMapping call_mappings,
+ std::vector<Argument> &args, Location call_locus,
+ Location receiver_locus,
+ TyTy::BaseType *adjusted_self,
+ Resolver::TypeCheckContext *context)
+{
+ TypeCheckMethodCallExpr checker (call_mappings, args, call_locus,
+ receiver_locus, adjusted_self, context);
+ return checker.check (*ref);
+}
+
+BaseType *
+TypeCheckMethodCallExpr::check (FnType &type)
{
Resolver::TypeCheckBase::unify_site (
- call.get_mappings ().get_hirid (), TyWithLocation (type.get_self_type ()),
- TyWithLocation (adjusted_self, call.get_receiver ()->get_locus ()),
- call.get_locus ());
+ call_mappings.get_hirid (), TyWithLocation (type.get_self_type ()),
+ TyWithLocation (adjusted_self, receiver_locus), call_locus);
// +1 for the receiver self
- size_t num_args_to_call = call.num_params () + 1;
+ size_t num_args_to_call = arguments.size () + 1;
if (num_args_to_call != type.num_params ())
{
- rust_error_at (call.get_locus (),
+ rust_error_at (call_locus,
"unexpected number of arguments %lu expected %lu",
- (unsigned long) call.num_params (),
+ (unsigned long) num_args_to_call,
(unsigned long) type.num_params ());
- return;
+ return new ErrorType (type.get_ref ());
}
size_t i = 1;
- for (auto &argument : call.get_arguments ())
+ for (auto &argument : arguments)
{
- Location arg_locus = argument->get_locus ();
+ Location arg_locus = argument.get_locus ();
auto fnparam = type.param_at (i);
HIR::Pattern *fn_param_pattern = fnparam.first;
@@ -248,25 +296,15 @@ TypeCheckMethodCallExpr::visit (FnType &type)
? mappings->lookup_location (param_ty->get_ref ())
: fn_param_pattern->get_locus ();
- auto argument_expr_tyty
- = Resolver::TypeCheckExpr::Resolve (argument.get ());
- if (argument_expr_tyty->get_kind () == TyTy::TypeKind::ERROR)
- {
- rust_error_at (
- argument->get_locus (),
- "failed to resolve type for argument expr in CallExpr");
- return;
- }
-
- HirId coercion_side_id = argument->get_mappings ().get_hirid ();
+ auto argument_expr_tyty = argument.get_argument_type ();
+ HirId coercion_side_id = argument.get_mappings ().get_hirid ();
auto resolved_argument_type = Resolver::TypeCheckBase::coercion_site (
coercion_side_id, TyWithLocation (param_ty, param_locus),
- TyWithLocation (argument_expr_tyty, arg_locus), argument->get_locus ());
+ TyWithLocation (argument_expr_tyty, arg_locus), arg_locus);
if (resolved_argument_type->get_kind () == TyTy::TypeKind::ERROR)
{
- rust_error_at (argument->get_locus (),
- "Type Resolution failure on parameter");
- return;
+ rust_error_at (arg_locus, "Type Resolution failure on parameter");
+ return new ErrorType (type.get_ref ());
}
i++;
@@ -274,15 +312,15 @@ TypeCheckMethodCallExpr::visit (FnType &type)
if (i != num_args_to_call)
{
- rust_error_at (call.get_locus (),
+ rust_error_at (call_locus,
"unexpected number of arguments %lu expected %lu",
- (unsigned long) i, (unsigned long) call.num_params ());
- return;
+ (unsigned long) i, (unsigned long) arguments.size ());
+ return new ErrorType (type.get_ref ());
}
type.monomorphize ();
- resolved = type.get_return_type ()->monomorphized_clone ();
+ return type.get_return_type ()->monomorphized_clone ();
}
} // namespace TyTy
diff --git a/gcc/rust/typecheck/rust-tyty-call.h b/gcc/rust/typecheck/rust-tyty-call.h
index 51817e6446d..eef567ac419 100644
--- a/gcc/rust/typecheck/rust-tyty-call.h
+++ b/gcc/rust/typecheck/rust-tyty-call.h
@@ -84,58 +84,51 @@ private:
Analysis::Mappings *mappings;
};
-class TypeCheckMethodCallExpr : private TyVisitor
+class Argument
{
public:
- // Resolve the Method parameters and return back the return type
- static BaseType *go (BaseType *ref, HIR::MethodCallExpr &call,
- TyTy::BaseType *adjusted_self,
- Resolver::TypeCheckContext *context)
- {
- TypeCheckMethodCallExpr checker (call, adjusted_self, context);
- ref->accept_vis (checker);
- return checker.resolved;
- }
+ Argument (Analysis::NodeMapping mapping, BaseType *argument_type,
+ Location locus)
+ : mapping (mapping), argument_type (argument_type), locus (locus)
+ {}
- void visit (InferType &) override { gcc_unreachable (); }
- void visit (TupleType &) override { gcc_unreachable (); }
- void visit (ArrayType &) override { gcc_unreachable (); }
- void visit (SliceType &) override { gcc_unreachable (); }
- void visit (BoolType &) override { gcc_unreachable (); }
- void visit (IntType &) override { gcc_unreachable (); }
- void visit (UintType &) override { gcc_unreachable (); }
- void visit (FloatType &) override { gcc_unreachable (); }
- void visit (USizeType &) override { gcc_unreachable (); }
- void visit (ISizeType &) override { gcc_unreachable (); }
- void visit (ErrorType &) override { gcc_unreachable (); }
- void visit (ADTType &) override { gcc_unreachable (); };
- void visit (CharType &) override { gcc_unreachable (); }
- void visit (ReferenceType &) override { gcc_unreachable (); }
- void visit (PointerType &) override { gcc_unreachable (); }
- void visit (ParamType &) override { gcc_unreachable (); }
- void visit (StrType &) override { gcc_unreachable (); }
- void visit (NeverType &) override { gcc_unreachable (); }
- void visit (PlaceholderType &) override { gcc_unreachable (); }
- void visit (ProjectionType &) override { gcc_unreachable (); }
- void visit (DynamicObjectType &) override { gcc_unreachable (); }
+ Location get_locus () const { return locus; }
- // FIXME
- void visit (FnPtr &type) override { gcc_unreachable (); }
+ BaseType *get_argument_type () { return argument_type; }
- // call fns
- void visit (FnType &type) override;
- void visit (ClosureType &type) override { gcc_unreachable (); }
+ Analysis::NodeMapping get_mappings () const { return mapping; }
private:
- TypeCheckMethodCallExpr (HIR::MethodCallExpr &c,
+ Analysis::NodeMapping mapping;
+ BaseType *argument_type;
+ Location locus;
+};
+
+class TypeCheckMethodCallExpr
+{
+public:
+ static BaseType *go (FnType *ref, HIR::MethodCallExpr &call,
+ TyTy::BaseType *adjusted_self,
+ Resolver::TypeCheckContext *context);
+
+ static BaseType *go (FnType *ref, Analysis::NodeMapping call_mappings,
+ std::vector<Argument> &args, Location call_locus,
+ Location receiver_locus, TyTy::BaseType *adjusted_self,
+ Resolver::TypeCheckContext *context);
+
+protected:
+ BaseType *check (FnType &type);
+
+ TypeCheckMethodCallExpr (Analysis::NodeMapping call_mappings,
+ std::vector<Argument> &args, Location call_locus,
+ Location receiver_locus,
TyTy::BaseType *adjusted_self,
- Resolver::TypeCheckContext *context)
- : resolved (nullptr), call (c), adjusted_self (adjusted_self),
- context (context), mappings (Analysis::Mappings::get ())
- {}
+ Resolver::TypeCheckContext *context);
- BaseType *resolved;
- HIR::MethodCallExpr &call;
+ Analysis::NodeMapping call_mappings;
+ std::vector<Argument> &arguments;
+ Location call_locus;
+ Location receiver_locus;
TyTy::BaseType *adjusted_self;
Resolver::TypeCheckContext *context;
Analysis::Mappings *mappings;
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-10-22 10:48 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 10:48 [gcc/devel/rust/master] Refactor method call type checking Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).