From 573120551a0bc6813f32ed371df65311724e96dd Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Thu, 25 Mar 2021 16:24:59 +0000 Subject: Add generics for impl blocks Generics paramters to impl blocks allows each impl-item to inherit these and get handled in a similar way as to normal functions. Fixes #237 --- gcc/rust/backend/rust-compile-context.h | 3 +- gcc/rust/backend/rust-compile-implitem.h | 28 +++++++++++++--- gcc/rust/backend/rust-compile-resolve-path.cc | 47 ++++++++++++++++++++++----- gcc/rust/backend/rust-compile.cc | 2 +- 4 files changed, 64 insertions(+), 16 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 6347464..8866575 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -297,8 +297,7 @@ public: void visit (TyTy::ParamType ¶m) override { - TyTy::TyVar var (param.get_ty_ref ()); - var.get_tyty ()->accept_vis (*this); + param.resolve ()->accept_vis (*this); } void visit (TyTy::FnType &type) override diff --git a/gcc/rust/backend/rust-compile-implitem.h b/gcc/rust/backend/rust-compile-implitem.h index 0817424..15aba9b 100644 --- a/gcc/rust/backend/rust-compile-implitem.h +++ b/gcc/rust/backend/rust-compile-implitem.h @@ -35,9 +35,10 @@ class CompileInherentImplItem : public HIRCompileBase public: static void Compile (TyTy::BaseType *self, HIR::InherentImplItem *item, - Context *ctx, bool compile_fns) + Context *ctx, bool compile_fns, + TyTy::BaseType *concrete = nullptr) { - CompileInherentImplItem compiler (self, ctx, compile_fns); + CompileInherentImplItem compiler (self, ctx, compile_fns, concrete); item->accept_vis (compiler); } @@ -92,6 +93,21 @@ public: } TyTy::FnType *fntype = static_cast (fntype_tyty); + if (fntype->has_subsititions_defined ()) + { + // we cant do anything for this only when it is used + if (concrete == nullptr) + return; + else + { + rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF); + fntype = static_cast (concrete); + + // 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); @@ -111,7 +127,6 @@ public: ctx->insert_function_decl (fntype->get_ty_ref (), fndecl); // setup the params - TyTy::BaseType *tyret = fntype->get_return_type (); std::vector param_vars; @@ -431,12 +446,15 @@ public: } private: - CompileInherentImplItem (TyTy::BaseType *self, Context *ctx, bool compile_fns) - : HIRCompileBase (ctx), self (self), compile_fns (compile_fns) + CompileInherentImplItem (TyTy::BaseType *self, Context *ctx, bool compile_fns, + TyTy::BaseType *concrete) + : HIRCompileBase (ctx), self (self), compile_fns (compile_fns), + concrete (concrete) {} TyTy::BaseType *self; bool compile_fns; + TyTy::BaseType *concrete; }; } // namespace Compile diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index 4fbaae3..90c4eeb 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -78,19 +78,50 @@ ResolvePathRef::visit (HIR::PathInExpression &expr) Bfunction *fn = nullptr; if (!ctx->lookup_function_decl (lookup->get_ty_ref (), &fn)) { - // it must resolve to some kind of HIR::Item + // it must resolve to some kind of HIR::Item or HIR::InheritImplItem HIR::Item *resolved_item = ctx->get_mappings ()->lookup_hir_item ( expr.get_mappings ().get_crate_num (), ref); - if (resolved_item == nullptr) + if (resolved_item != nullptr) { - rust_error_at (expr.get_locus (), "failed to lookup definition decl"); - return; + if (!lookup->has_subsititions_defined ()) + CompileItem::compile (resolved_item, ctx); + else + CompileItem::compile (resolved_item, ctx, true, lookup); } - - if (!lookup->has_subsititions_defined ()) - CompileItem::compile (resolved_item, ctx); else - CompileItem::compile (resolved_item, ctx, true, lookup); + { + HirId parent_impl_id = UNKNOWN_HIRID; + HIR::InherentImplItem *resolved_item + = ctx->get_mappings ()->lookup_hir_implitem ( + expr.get_mappings ().get_crate_num (), ref, &parent_impl_id); + if (resolved_item != nullptr) + { + rust_assert (parent_impl_id != UNKNOWN_HIRID); + HIR::Item *impl_ref = ctx->get_mappings ()->lookup_hir_item ( + expr.get_mappings ().get_crate_num (), parent_impl_id); + rust_assert (impl_ref != nullptr); + HIR::InherentImpl *impl + = static_cast (impl_ref); + + TyTy::BaseType *self = nullptr; + bool ok = ctx->get_tyctx ()->lookup_type ( + impl->get_type ()->get_mappings ().get_hirid (), &self); + rust_assert (ok); + + if (!lookup->has_subsititions_defined ()) + CompileInherentImplItem::Compile (self, resolved_item, ctx, + true); + else + CompileInherentImplItem::Compile (self, resolved_item, ctx, + true, lookup); + } + else + { + rust_error_at (expr.get_locus (), + "failed to lookup definition decl"); + return; + } + } if (!ctx->lookup_function_decl (lookup->get_ty_ref (), &fn)) { diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 204cce7..7b1b2ff 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -130,7 +130,7 @@ CompileExpr::visit (HIR::MethodCallExpr &expr) // resolve it now HIR::InherentImplItem *resolved_item = ctx->get_mappings ()->lookup_hir_implitem ( - expr.get_mappings ().get_crate_num (), ref); + expr.get_mappings ().get_crate_num (), ref, nullptr); if (resolved_item == nullptr) { rust_error_at (expr.get_locus (), "failed to lookup forward decl"); -- cgit v1.1