aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-10-19 10:59:18 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2023-02-21 12:36:39 +0100
commit4d021d9e019dd2a7b5442653a20e126f37fd8f39 (patch)
tree20712a4b87e65c15e98505097c4ff247aeaaaf5f
parent977e0e5227d467fcda43edfae72c5251c283d871 (diff)
downloadgcc-4d021d9e019dd2a7b5442653a20e126f37fd8f39.zip
gcc-4d021d9e019dd2a7b5442653a20e126f37fd8f39.tar.gz
gcc-4d021d9e019dd2a7b5442653a20e126f37fd8f39.tar.bz2
gccrs: Refactor method call type checking
gcc/rust/ChangeLog: * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Simplify method call type checking by removing visitor and instead using one static cast. Use the new interface. * typecheck/rust-tyty-call.cc (TypeCheckMethodCallExpr::visit): Likewise. (TypeCheckMethodCallExpr::go): Likewise. (TypeCheckMethodCallExpr::check): Likewise. * typecheck/rust-tyty-call.h (class TypeCheckMethodCallExpr): Likewise. (class Argument): Likewise.
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-expr.cc3
-rw-r--r--gcc/rust/typecheck/rust-tyty-call.cc98
-rw-r--r--gcc/rust/typecheck/rust-tyty-call.h79
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 6b8c63b..9140b8b 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 96c6b93..0c9e9f4 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 aa47328..4d1c811 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;