aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-context.h
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/rust-compile-context.h
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/rust-compile-context.h')
-rw-r--r--gcc/rust/backend/rust-compile-context.h80
1 files changed, 78 insertions, 2 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