aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-01-19 12:03:01 +0000
committerPhilip Herron <herron.philip@googlemail.com>2021-01-20 09:59:22 +0000
commitf6d33adc6656839aebb4dca02df8efc8be6aedd2 (patch)
tree9d4bc5c4da1a45c82bd2bb9e73d1c000071e9b4d /gcc/rust/backend
parentcb44a8feb815ee31946b33e713c62ac2d333d7be (diff)
downloadgcc-f6d33adc6656839aebb4dca02df8efc8be6aedd2.zip
gcc-f6d33adc6656839aebb4dca02df8efc8be6aedd2.tar.gz
gcc-f6d33adc6656839aebb4dca02df8efc8be6aedd2.tar.bz2
Fix bug using ADT types as return types to functions
When we use anything other than builtin types for returns or parameters the type resolver was missing the NodeId mappings meaning it would alawys fail to resolve them. Then in gimple conversion we need to be able to reference the already created RECORD types instead of always creating new instances.
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-context.h80
-rw-r--r--gcc/rust/backend/rust-compile-item.h15
-rw-r--r--gcc/rust/backend/rust-compile-tyty.h48
3 files changed, 85 insertions, 58 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index 5288e51..6516c71 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -225,12 +225,43 @@ public:
void visit (TyTy::InferType &type) override { gcc_unreachable (); }
- void visit (TyTy::FnType &type) override { gcc_unreachable (); }
-
void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
+ void visit (TyTy::FnType &type) override
+ {
+ Backend::Btyped_identifier receiver;
+ std::vector<Backend::Btyped_identifier> parameters;
+ std::vector<Backend::Btyped_identifier> results;
+
+ if (!type.get_return_type ()->is_unit ())
+ {
+ auto hir_type = type.get_return_type ();
+ auto ret = TyTyResolveCompile::compile (ctx, hir_type);
+ results.push_back (Backend::Btyped_identifier (
+ "_", ret,
+ ctx->get_mappings ()->lookup_location (hir_type->get_ref ())));
+ }
+
+ for (size_t i = 0; i < type.num_params (); i++)
+ {
+ auto param_tyty = type.param_at (i);
+ auto compiled_param_type
+ = TyTyResolveCompile::compile (ctx, param_tyty->get_base_type ());
+
+ auto compiled_param = Backend::Btyped_identifier (
+ param_tyty->get_identifier (), compiled_param_type,
+ ctx->get_mappings ()->lookup_location (param_tyty->get_ref ()));
+
+ parameters.push_back (compiled_param);
+ }
+
+ translated = ctx->get_backend ()->function_type (
+ receiver, parameters, results, NULL,
+ ctx->get_mappings ()->lookup_location (type.get_ref ()));
+ }
+
void visit (TyTy::UnitType &type) override
{
translated = ctx->get_backend ()->void_type ();
@@ -320,6 +351,51 @@ private:
::Btype *translated;
};
+class TyTyCompileParam : public TyTy::TyVisitor
+{
+public:
+ static ::Bvariable *compile (Context *ctx, Bfunction *fndecl,
+ TyTy::TyBase *ty)
+ {
+ TyTyCompileParam compiler (ctx, fndecl);
+ ty->accept_vis (compiler);
+ rust_assert (compiler.translated != nullptr);
+ return compiler.translated;
+ }
+
+ ~TyTyCompileParam () {}
+
+ void visit (TyTy::UnitType &type) override { gcc_unreachable (); }
+ void visit (TyTy::InferType &type) override { gcc_unreachable (); }
+ void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
+ void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
+ void visit (TyTy::FnType &type) override { gcc_unreachable (); }
+ void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
+ void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
+ void visit (TyTy::IntType &type) override { gcc_unreachable (); }
+ void visit (TyTy::UintType &type) override { gcc_unreachable (); }
+ void visit (TyTy::FloatType &type) override { gcc_unreachable (); }
+ void visit (TyTy::ErrorType &type) override { gcc_unreachable (); }
+
+ void visit (TyTy::ParamType &type) override
+ {
+ auto btype = TyTyResolveCompile::compile (ctx, type.get_base_type ());
+ bool tree_addressable = false;
+ translated = ctx->get_backend ()->parameter_variable (
+ fndecl, type.get_identifier (), btype, tree_addressable,
+ ctx->get_mappings ()->lookup_location (type.get_ref ()));
+ }
+
+private:
+ TyTyCompileParam (Context *ctx, ::Bfunction *fndecl)
+ : ctx (ctx), fndecl (fndecl), translated (nullptr)
+ {}
+
+ Context *ctx;
+ ::Bfunction *fndecl;
+ ::Bvariable *translated;
+};
+
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h
index c8cffc7..82dbc9b 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -127,16 +127,16 @@ public:
return;
}
- TyTy::TyBase *fnType;
+ TyTy::TyBase *fntype;
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
- &fnType))
+ &fntype))
{
rust_fatal_error (function.locus, "failed to lookup function type");
return;
}
// convert to the actual function type
- auto compiled_fn_type = TyTyCompile::compile (ctx->get_backend (), fnType);
+ ::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
unsigned int flags = 0;
bool is_main_fn = function.function_name.compare ("main") == 0;
@@ -159,15 +159,14 @@ public:
ctx->insert_function_decl (function.get_mappings ().get_hirid (), fndecl);
// setup the params
- TyTy::TyBase *tyret = TyTyExtractRetFromFnType::compile (fnType);
+ TyTy::TyBase *tyret = TyTyExtractRetFromFnType::compile (fntype);
std::vector<TyTy::ParamType *> typarams
- = TyTyExtractParamsFromFnType::compile (fnType);
+ = TyTyExtractParamsFromFnType::compile (fntype);
std::vector<Bvariable *> param_vars;
for (auto &it : typarams)
{
- auto compiled_param
- = TyTyCompileParam::compile (ctx->get_backend (), fndecl, it);
+ auto compiled_param = TyTyCompileParam::compile (ctx, fndecl, it);
param_vars.push_back (compiled_param);
ctx->insert_var_decl (it->get_ref (), compiled_param);
@@ -226,7 +225,7 @@ public:
Bvariable *return_address = nullptr;
if (function.has_function_return_type ())
{
- Btype *return_type = TyTyCompile::compile (ctx->get_backend (), tyret);
+ Btype *return_type = TyTyResolveCompile::compile (ctx, tyret);
bool address_is_taken = false;
Bstatement *ret_var_stmt = nullptr;
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h
index cd220e0..3eb2ca5 100644
--- a/gcc/rust/backend/rust-compile-tyty.h
+++ b/gcc/rust/backend/rust-compile-tyty.h
@@ -278,54 +278,6 @@ private:
TyTy::TyBase *translated;
};
-class TyTyCompileParam : public TyTy::TyVisitor
-{
-public:
- static ::Bvariable *compile (::Backend *backend, Bfunction *fndecl,
- TyTy::TyBase *ty)
- {
- TyTyCompileParam compiler (backend, fndecl);
- ty->accept_vis (compiler);
- rust_assert (compiler.translated != nullptr);
- return compiler.translated;
- }
-
- ~TyTyCompileParam () {}
-
- void visit (TyTy::UnitType &type) override { gcc_unreachable (); }
- void visit (TyTy::InferType &type) override { gcc_unreachable (); }
- void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
- void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
- void visit (TyTy::FnType &type) override { gcc_unreachable (); }
- void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
- void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
- void visit (TyTy::IntType &type) override { gcc_unreachable (); }
- void visit (TyTy::UintType &type) override { gcc_unreachable (); }
- void visit (TyTy::FloatType &type) override { gcc_unreachable (); }
- void visit (TyTy::ErrorType &type) override { gcc_unreachable (); }
-
- void visit (TyTy::ParamType &type) override
- {
- auto btype = TyTyCompile::compile (backend, type.get_base_type ());
- bool tree_addressable = false;
- translated = backend->parameter_variable (fndecl, type.get_identifier (),
- btype, tree_addressable,
- mappings->lookup_location (
- type.get_ref ()));
- }
-
-private:
- TyTyCompileParam (::Backend *backend, ::Bfunction *fndecl)
- : backend (backend), translated (nullptr), fndecl (fndecl),
- mappings (Analysis::Mappings::get ())
- {}
-
- ::Backend *backend;
- ::Bvariable *translated;
- ::Bfunction *fndecl;
- Analysis::Mappings *mappings;
-};
-
} // namespace Compile
} // namespace Rust