From 1c0f8d6d2a22ce498ef3f1bfd60a3867a90c8130 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Sat, 5 Jun 2021 16:37:29 +0100 Subject: This is the initial building blocks for Traits We can compile TraitImpls, since before we can actually start checking trait obligations we need to be able to implement the trait first. More desugaring is needed in HIR to make TraitImpls contain normal impl items to avoid seperation in how impl blocks are handled. Fixes #395 #472 --- gcc/rust/backend/rust-compile-implitem.h | 8 +++++ gcc/rust/backend/rust-compile-item.h | 16 +++++++++ gcc/rust/typecheck/rust-hir-type-check-implitem.h | 14 ++++++++ gcc/rust/typecheck/rust-hir-type-check-item.h | 15 +++++++++ gcc/rust/typecheck/rust-hir-type-check-toplevel.h | 41 +++++++++++++++++++++++ 5 files changed, 94 insertions(+) (limited to 'gcc/rust') diff --git a/gcc/rust/backend/rust-compile-implitem.h b/gcc/rust/backend/rust-compile-implitem.h index a5ca13b..a4fb6d1 100644 --- a/gcc/rust/backend/rust-compile-implitem.h +++ b/gcc/rust/backend/rust-compile-implitem.h @@ -42,6 +42,14 @@ public: item->accept_vis (compiler); } + static void Compile (TyTy::BaseType *self, HIR::TraitImplItem *item, + Context *ctx, bool compile_fns, + TyTy::BaseType *concrete = nullptr) + { + CompileInherentImplItem compiler (self, ctx, compile_fns, concrete); + item->accept_vis (compiler); + } + void visit (HIR::ConstantItem &constant) override { TyTy::BaseType *resolved_type = nullptr; diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index e3b6d0f..e681652 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -290,6 +290,22 @@ public: compile_fns); } + void visit (HIR::TraitImpl &impl_block) override + { + TyTy::BaseType *self_lookup = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + impl_block.get_type ()->get_mappings ().get_hirid (), &self_lookup)) + { + rust_error_at (impl_block.get_locus (), + "failed to resolve type of impl"); + return; + } + + for (auto &impl_item : impl_block.get_impl_items ()) + CompileInherentImplItem::Compile (self_lookup, impl_item.get (), ctx, + compile_fns); + } + private: CompileItem (Context *ctx, bool compile_fns, TyTy::BaseType *concrete) : HIRCompileBase (ctx), compile_fns (compile_fns), concrete (concrete) diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.h b/gcc/rust/typecheck/rust-hir-type-check-implitem.h index 2f54d0c..bed89b8 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.h @@ -41,6 +41,14 @@ public: item->accept_vis (resolver); } + static void + Resolve (HIR::TraitImplItem *item, TyTy::BaseType *self, + std::vector substitutions) + { + TypeCheckTopLevelImplItem resolver (self, substitutions); + item->accept_vis (resolver); + } + void visit (HIR::ConstantItem &constant) override { TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ()); @@ -219,6 +227,12 @@ public: item->accept_vis (resolver); } + static void Resolve (HIR::TraitImplItem *item, TyTy::BaseType *self) + { + TypeCheckImplItem resolver (self); + item->accept_vis (resolver); + } + void visit (HIR::Function &function) override { TyTy::BaseType *lookup; diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.h b/gcc/rust/typecheck/rust-hir-type-check-item.h index 1205dce..47d9a8a 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.h +++ b/gcc/rust/typecheck/rust-hir-type-check-item.h @@ -55,6 +55,21 @@ public: TypeCheckImplItem::Resolve (impl_item.get (), self); } + void visit (HIR::TraitImpl &impl_block) override + { + TyTy::BaseType *self = nullptr; + if (!context->lookup_type ( + impl_block.get_type ()->get_mappings ().get_hirid (), &self)) + { + rust_error_at (impl_block.get_locus (), + "failed to resolve Self for TraitImpl"); + return; + } + + for (auto &impl_item : impl_block.get_impl_items ()) + TypeCheckImplItem::Resolve (impl_item.get (), self); + } + void visit (HIR::Function &function) override { TyTy::BaseType *lookup; diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h index e01b46f..3ec231f 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h +++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h @@ -273,6 +273,47 @@ public: substitutions); } + void visit (HIR::TraitImpl &impl_block) override + { + std::vector substitutions; + if (impl_block.has_generics ()) + { + for (auto &generic_param : impl_block.get_generic_params ()) + { + switch (generic_param.get ()->get_kind ()) + { + case HIR::GenericParam::GenericKind::LIFETIME: + // Skipping Lifetime completely until better handling. + break; + + case HIR::GenericParam::GenericKind::TYPE: { + auto param_type + = TypeResolveGenericParam::Resolve (generic_param.get ()); + context->insert_type (generic_param->get_mappings (), + param_type); + + substitutions.push_back (TyTy::SubstitutionParamMapping ( + static_cast (*generic_param), + param_type)); + } + break; + } + } + } + + // TODO + // resolve the trait and check all items implemented + + auto self + = TypeCheckType::Resolve (impl_block.get_type ().get (), &substitutions); + if (self == nullptr || self->get_kind () == TyTy::TypeKind::ERROR) + return; + + for (auto &impl_item : impl_block.get_impl_items ()) + TypeCheckTopLevelImplItem::Resolve (impl_item.get (), self, + substitutions); + } + private: TypeCheckTopLevel () : TypeCheckBase () {} }; -- cgit v1.1