aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-07-01 16:22:57 +0000
committerGitHub <noreply@github.com>2021-07-01 16:22:57 +0000
commit458f7a5459a8907fa55a84248ba137281ac675c4 (patch)
treee3df7cf10a523c2f9320d152b0d799c0693131e7 /gcc/rust/backend
parent0163ca4b6f2248103d416405b6758f7abb5e18ab (diff)
parent5e4505e162d069ca58ce43d62770b81cb36137c0 (diff)
downloadgcc-458f7a5459a8907fa55a84248ba137281ac675c4.zip
gcc-458f7a5459a8907fa55a84248ba137281ac675c4.tar.gz
gcc-458f7a5459a8907fa55a84248ba137281ac675c4.tar.bz2
Merge #540
540: Add trait-impl support for methods r=philberty a=philberty This PR strips HIR::Method and reuses the HIR::Function with a HIR::SelfParam. It reduces complexity in type checking and GENERIC generation. Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-base.h1
-rw-r--r--gcc/rust/backend/rust-compile-implitem.h240
2 files changed, 38 insertions, 203 deletions
diff --git a/gcc/rust/backend/rust-compile-base.h b/gcc/rust/backend/rust-compile-base.h
index c346af5..6a05c1c 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -121,7 +121,6 @@ public:
// virtual void visit(WhereClauseItem& item) {}
virtual void visit (HIR::LifetimeWhereClauseItem &item) {}
virtual void visit (HIR::TypeBoundWhereClauseItem &item) {}
- virtual void visit (HIR::Method &method) {}
virtual void visit (HIR::ModuleBodied &module) {}
virtual void visit (HIR::ModuleNoBody &module) {}
virtual void visit (HIR::ExternCrate &crate) {}
diff --git a/gcc/rust/backend/rust-compile-implitem.h b/gcc/rust/backend/rust-compile-implitem.h
index 70f76b7..83af5de 100644
--- a/gcc/rust/backend/rust-compile-implitem.h
+++ b/gcc/rust/backend/rust-compile-implitem.h
@@ -135,212 +135,48 @@ public:
TyTy::BaseType *tyret = fntype->get_return_type ();
std::vector<Bvariable *> param_vars;
- size_t i = 0;
- for (auto &it : fntype->get_params ())
+ if (function.is_method ())
{
- HIR::FunctionParam &referenced_param
- = function.get_function_params ().at (i);
- auto param_tyty = it.second;
- auto compiled_param_type
- = TyTyResolveCompile::compile (ctx, param_tyty);
-
- Location param_locus
- = ctx->get_mappings ()->lookup_location (param_tyty->get_ref ());
- Bvariable *compiled_param_var
- = CompileFnParam::compile (ctx, fndecl, &referenced_param,
- compiled_param_type, param_locus);
- if (compiled_param_var == nullptr)
+ // insert self
+ TyTy::BaseType *self_tyty_lookup = nullptr;
+ if (!ctx->get_tyctx ()->lookup_type (
+ function.get_self_param ().get_mappings ().get_hirid (),
+ &self_tyty_lookup))
{
- rust_error_at (param_locus, "failed to compile parameter variable");
+ rust_error_at (function.get_self_param ().get_locus (),
+ "failed to lookup self param type");
return;
}
- param_vars.push_back (compiled_param_var);
-
- ctx->insert_var_decl (referenced_param.get_mappings ().get_hirid (),
- compiled_param_var);
- i++;
- }
-
- if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
- {
- rust_fatal_error (function.get_locus (),
- "failed to setup parameter variables");
- return;
- }
-
- // lookup locals
- auto block_expr = function.get_definition ().get ();
- auto body_mappings = block_expr->get_mappings ();
-
- Resolver::Rib *rib = nullptr;
- if (!ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
- &rib))
- {
- rust_fatal_error (function.get_locus (),
- "failed to setup locals per block");
- return;
- }
-
- std::vector<Bvariable *> locals;
- bool ok = compile_locals_for_block (*rib, fndecl, locals);
- rust_assert (ok);
-
- Bblock *enclosing_scope = NULL;
- HIR::BlockExpr *function_body = function.get_definition ().get ();
- Location start_location = function_body->get_locus ();
- Location end_location = function_body->get_closing_locus ();
-
- Bblock *code_block
- = ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
- start_location, end_location);
- ctx->push_block (code_block);
-
- Bvariable *return_address = nullptr;
- if (function.has_function_return_type ())
- {
- Btype *return_type = TyTyResolveCompile::compile (ctx, tyret);
-
- bool address_is_taken = false;
- Bstatement *ret_var_stmt = nullptr;
-
- return_address = ctx->get_backend ()->temporary_variable (
- fndecl, code_block, return_type, NULL, address_is_taken,
- function.get_locus (), &ret_var_stmt);
-
- ctx->add_statement (ret_var_stmt);
- }
-
- ctx->push_fn (fndecl, return_address);
-
- compile_function_body (fndecl, function.get_definition (),
- function.has_function_return_type ());
-
- ctx->pop_block ();
- auto body = ctx->get_backend ()->block_statement (code_block);
- if (!ctx->get_backend ()->function_set_body (fndecl, body))
- {
- rust_error_at (function.get_locus (), "failed to set body to function");
- return;
- }
-
- ctx->pop_fn ();
-
- ctx->push_function (fndecl);
- }
-
- void visit (HIR::Method &method) override
- {
- if (!compile_fns)
- return;
-
- TyTy::BaseType *fntype_tyty;
- if (!ctx->get_tyctx ()->lookup_type (method.get_mappings ().get_hirid (),
- &fntype_tyty))
- {
- rust_fatal_error (method.get_locus (),
- "failed to lookup function type");
- return;
- }
-
- rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF);
- TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty);
- if (fntype->has_subsititions_defined ())
- {
- // we cant do anything for this only when it is used and a concrete type
- // is given
- if (concrete == nullptr)
- return;
- else
+ Btype *self_type = TyTyResolveCompile::compile (ctx, self_tyty_lookup);
+ if (self_type == nullptr)
{
- rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF);
- fntype = static_cast<TyTy::FnType *> (concrete);
+ rust_error_at (function.get_self_param ().get_locus (),
+ "failed to compile self param type");
+ return;
}
- }
- // items can be forward compiled which means we may not need to invoke this
- // code. We might also have already compiled this generic function as well.
- Bfunction *lookup = nullptr;
- if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup, fntype))
- {
- // has this been added to the list then it must be finished
- if (ctx->function_completed (lookup))
+ Bvariable *compiled_self_param
+ = CompileSelfParam::compile (ctx, fndecl, function.get_self_param (),
+ self_type,
+ function.get_self_param ().get_locus ());
+ if (compiled_self_param == nullptr)
{
- Bfunction *dummy = nullptr;
- if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &dummy))
- ctx->insert_function_decl (fntype->get_ty_ref (), lookup, fntype);
-
+ rust_error_at (function.get_self_param ().get_locus (),
+ "failed to compile self param variable");
return;
}
- }
-
- if (fntype->has_subsititions_defined ())
- {
- // override the Hir Lookups for the substituions in this context
- fntype->override_context ();
- }
-
- // convert to the actual function type
- ::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
-
- unsigned int flags = 0;
-
- // if its the main fn or pub visibility mark its as DECL_PUBLIC
- // please see https://github.com/Rust-GCC/gccrs/pull/137
- if (method.has_visibility ())
- flags |= Backend::function_is_visible;
-
- std::string fn_identifier
- = self->get_name () + "_" + method.get_method_name ();
- std::string asm_name
- = ctx->mangle_impl_item (self, fntype, method.get_method_name ());
-
- Bfunction *fndecl
- = ctx->get_backend ()->function (compiled_fn_type, fn_identifier,
- asm_name, flags, method.get_locus ());
- ctx->insert_function_decl (fntype->get_ty_ref (), fndecl, fntype);
-
- // setup the params
- TyTy::BaseType *tyret = fntype->get_return_type ();
- std::vector<Bvariable *> param_vars;
- // insert self
- TyTy::BaseType *self_tyty_lookup = nullptr;
- if (!ctx->get_tyctx ()->lookup_type (
- method.get_self_param ().get_mappings ().get_hirid (),
- &self_tyty_lookup))
- {
- rust_error_at (method.get_self_param ().get_locus (),
- "failed to lookup self param type");
- return;
- }
-
- Btype *self_type = TyTyResolveCompile::compile (ctx, self_tyty_lookup);
- if (self_type == nullptr)
- {
- rust_error_at (method.get_self_param ().get_locus (),
- "failed to compile self param type");
- return;
- }
-
- Bvariable *compiled_self_param
- = CompileSelfParam::compile (ctx, fndecl, method.get_self_param (),
- self_type,
- method.get_self_param ().get_locus ());
- if (compiled_self_param == nullptr)
- {
- rust_error_at (method.get_self_param ().get_locus (),
- "failed to compile self param variable");
- return;
+ param_vars.push_back (compiled_self_param);
+ ctx->insert_var_decl (
+ function.get_self_param ().get_mappings ().get_hirid (),
+ compiled_self_param);
}
- param_vars.push_back (compiled_self_param);
- ctx->insert_var_decl (method.get_self_param ().get_mappings ().get_hirid (),
- compiled_self_param);
-
- // offset from + 1 for the TyTy::FnType being used
- size_t i = 1;
- for (auto referenced_param : method.get_function_params ())
+ // offset from + 1 for the TyTy::FnType being used when this is a method to
+ // skip over Self on the FnType
+ size_t i = function.is_method () ? 1 : 0;
+ for (auto referenced_param : function.get_function_params ())
{
auto tyty_param = fntype->param_at (i);
auto param_tyty = tyty_param.second;
@@ -361,7 +197,7 @@ public:
compiled_param_type, param_locus);
if (compiled_param_var == nullptr)
{
- rust_error_at (param_locus, "failed to compile parameter variable");
+ rust_error_at (param_locus, "Failed to compile parameter variable");
return;
}
@@ -374,20 +210,20 @@ public:
if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
{
- rust_fatal_error (method.get_locus (),
+ rust_fatal_error (function.get_locus (),
"failed to setup parameter variables");
return;
}
// lookup locals
- auto block_expr = method.get_function_body ().get ();
+ auto block_expr = function.get_definition ().get ();
auto body_mappings = block_expr->get_mappings ();
Resolver::Rib *rib = nullptr;
if (!ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (),
&rib))
{
- rust_fatal_error (method.get_locus (),
+ rust_fatal_error (function.get_locus (),
"failed to setup locals per block");
return;
}
@@ -397,7 +233,7 @@ public:
rust_assert (ok);
Bblock *enclosing_scope = NULL;
- HIR::BlockExpr *function_body = method.get_function_body ().get ();
+ HIR::BlockExpr *function_body = function.get_definition ().get ();
Location start_location = function_body->get_locus ();
Location end_location = function_body->get_closing_locus ();
@@ -407,7 +243,7 @@ public:
ctx->push_block (code_block);
Bvariable *return_address = nullptr;
- if (method.has_function_return_type ())
+ if (function.has_function_return_type ())
{
Btype *return_type = TyTyResolveCompile::compile (ctx, tyret);
@@ -416,21 +252,21 @@ public:
return_address = ctx->get_backend ()->temporary_variable (
fndecl, code_block, return_type, NULL, address_is_taken,
- method.get_locus (), &ret_var_stmt);
+ function.get_locus (), &ret_var_stmt);
ctx->add_statement (ret_var_stmt);
}
ctx->push_fn (fndecl, return_address);
- compile_function_body (fndecl, method.get_function_body (),
- method.has_function_return_type ());
+ compile_function_body (fndecl, function.get_definition (),
+ function.has_function_return_type ());
ctx->pop_block ();
auto body = ctx->get_backend ()->block_statement (code_block);
if (!ctx->get_backend ()->function_set_body (fndecl, body))
{
- rust_error_at (method.get_locus (), "failed to set body to function");
+ rust_error_at (function.get_locus (), "failed to set body to function");
return;
}