From f6d33adc6656839aebb4dca02df8efc8be6aedd2 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Tue, 19 Jan 2021 12:03:01 +0000 Subject: 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. --- gcc/rust/backend/rust-compile-context.h | 80 ++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) (limited to 'gcc/rust/backend/rust-compile-context.h') 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 parameters; + std::vector 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 -- cgit v1.1