diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-03-10 18:10:06 +0000 |
---|---|---|
committer | Philip Herron <herron.philip@googlemail.com> | 2021-03-22 20:01:30 +0000 |
commit | 280ac5bd99b4d66ea10d0aa8d4edba53bf460b10 (patch) | |
tree | e67968afbe4668e6a2679ed961d137cdb409166d /gcc/rust/backend | |
parent | eb33139efa7bbbd09ad26403c36a5dcf31e1b14e (diff) | |
download | gcc-280ac5bd99b4d66ea10d0aa8d4edba53bf460b10.zip gcc-280ac5bd99b4d66ea10d0aa8d4edba53bf460b10.tar.gz gcc-280ac5bd99b4d66ea10d0aa8d4edba53bf460b10.tar.bz2 |
Generics continued this adds more type resolution to ADT and Functions
Adds recursive generic argument handling for structs and functions. With a
new substitution mapper class to coerce the HIR::GenericArgs appropriately.
This is the building block to work on impl blocks with generics and
better Monomorphization support to handle duplicate functions etc.
Fixes: #236 #234 #235
Addresses: #237
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 49 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-implitem.h | 8 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-item.h | 49 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-resolve-path.cc | 27 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-tyty.h | 6 |
5 files changed, 97 insertions, 42 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 3f4a9ac..6f45e57 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -61,10 +61,26 @@ public: } } - ~Context () {} - - bool lookup_compiled_types (HirId id, ::Btype **type) + bool lookup_compiled_types (HirId id, ::Btype **type, + const TyTy::BaseType *ref = nullptr) { + if (ref != nullptr && ref->has_subsititions_defined ()) + { + for (auto it = mono.begin (); it != mono.end (); it++) + { + std::pair<HirId, ::Btype *> &val = it->second; + const TyTy::BaseType *r = it->first; + + if (ref->is_equal (*r)) + { + *type = val.second; + + return true; + } + } + return false; + } + auto it = compiled_type_map.find (id); if (it == compiled_type_map.end ()) return false; @@ -73,9 +89,15 @@ public: return true; } - void insert_compiled_type (HirId id, ::Btype *type) + void insert_compiled_type (HirId id, ::Btype *type, + const TyTy::BaseType *ref = nullptr) { compiled_type_map[id] = type; + if (ref != nullptr) + { + std::pair<HirId, ::Btype *> elem (id, type); + mono[ref] = std::move (elem); + } } ::Backend *get_backend () { return backend; } @@ -250,6 +272,7 @@ private: std::vector< ::Bblock *> scope_stack; std::vector< ::Bvariable *> loop_value_stack; std::vector< ::Blabel *> loop_begin_labels; + std::map<const TyTy::BaseType *, std::pair<HirId, ::Btype *> > mono; // To GCC middle-end std::vector< ::Btype *> type_decls; @@ -274,12 +297,8 @@ public: void visit (TyTy::ParamType ¶m) override { - rust_assert (param.get_ref () != param.get_ty_ref ()); - - TyTy::BaseType *lookup = nullptr; - bool ok = ctx->get_tyctx ()->lookup_type (param.get_ty_ref (), &lookup); - rust_assert (ok); - lookup->accept_vis (*this); + TyTy::TyVar var (param.get_ty_ref ()); + var.get_tyty ()->accept_vis (*this); } void visit (TyTy::FnType &type) override @@ -339,8 +358,7 @@ public: void visit (TyTy::ADTType &type) override { - bool ok = ctx->lookup_compiled_types (type.get_ty_ref (), &translated); - if (ok) + if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type)) return; // create implicit struct @@ -361,11 +379,12 @@ public: Btype *named_struct = ctx->get_backend ()->named_type (type.get_name (), struct_type_record, ctx->get_mappings ()->lookup_location ( - type.get_ref ())); + type.get_ty_ref ())); ctx->push_type (named_struct); - ctx->insert_compiled_type (type.get_ty_ref (), named_struct); translated = named_struct; + + ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type); } void visit (TyTy::TupleType &type) override @@ -485,7 +504,7 @@ public: } private: - TyTyResolveCompile (Context *ctx) : ctx (ctx) {} + TyTyResolveCompile (Context *ctx) : ctx (ctx), translated (nullptr) {} Context *ctx; ::Btype *translated; diff --git a/gcc/rust/backend/rust-compile-implitem.h b/gcc/rust/backend/rust-compile-implitem.h index 202b868..0817424 100644 --- a/gcc/rust/backend/rust-compile-implitem.h +++ b/gcc/rust/backend/rust-compile-implitem.h @@ -91,7 +91,7 @@ public: return; } - TyTy::FnType *fntype = (TyTy::FnType *) fntype_tyty; + TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty); // convert to the actual function type ::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype); @@ -108,7 +108,7 @@ public: Bfunction *fndecl = ctx->get_backend ()->function (compiled_fn_type, fn_identifier, asm_name, flags, function.get_locus ()); - ctx->insert_function_decl (function.get_mappings ().get_hirid (), fndecl); + ctx->insert_function_decl (fntype->get_ty_ref (), fndecl); // setup the params @@ -256,7 +256,7 @@ public: return; } - TyTy::FnType *fntype = (TyTy::FnType *) fntype_tyty; + TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty); // convert to the actual function type ::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype); @@ -273,7 +273,7 @@ public: Bfunction *fndecl = ctx->get_backend ()->function (compiled_fn_type, fn_identifier, asm_name, flags, method.get_locus ()); - ctx->insert_function_decl (method.get_mappings ().get_hirid (), fndecl); + ctx->insert_function_decl (fntype->get_ty_ref (), fndecl); // setup the params TyTy::BaseType *tyret = fntype->get_return_type (); diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index 1d4fcda..2bbfe4c 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -35,9 +35,10 @@ class CompileItem : public HIRCompileBase using Rust::Compile::HIRCompileBase::visit; public: - static void compile (HIR::Item *item, Context *ctx, bool compile_fns = true) + static void compile (HIR::Item *item, Context *ctx, bool compile_fns = true, + TyTy::BaseType *concrete = nullptr) { - CompileItem compiler (ctx, compile_fns); + CompileItem compiler (ctx, compile_fns, concrete); item->accept_vis (compiler); } @@ -118,8 +119,22 @@ public: return; } - TyTy::FnType *fntype = (TyTy::FnType *) fntype_tyty; - // convert to the actual function type + TyTy::FnType *fntype = static_cast<TyTy::FnType *> (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<TyTy::FnType *> (concrete); + + // override the Hir Lookups for the substituions in this context + fntype->override_context (); + } + } + ::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype); unsigned int flags = 0; @@ -130,21 +145,30 @@ public: if (is_main_fn || function.has_visibility ()) flags |= Backend::function_is_visible; + std::string ir_symbol_name = function.get_function_name (); std::string asm_name = function.get_function_name (); if (!is_main_fn) { // FIXME need name mangling - asm_name = "__" + function.get_function_name (); + if (concrete == nullptr) + asm_name = "__" + function.get_function_name (); + else + { + ir_symbol_name + = function.get_function_name () + fntype->subst_as_string (); + + asm_name = "__" + function.get_function_name (); + for (auto &sub : fntype->get_substs ()) + asm_name += "G" + sub.as_string (); + } } Bfunction *fndecl - = ctx->get_backend ()->function (compiled_fn_type, - function.get_function_name (), asm_name, - flags, function.get_locus ()); - ctx->insert_function_decl (function.get_mappings ().get_hirid (), fndecl); + = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name, + asm_name, flags, function.get_locus ()); + ctx->insert_function_decl (fntype->get_ty_ref (), fndecl); // setup the params - TyTy::BaseType *tyret = fntype->get_return_type (); std::vector<Bvariable *> param_vars; @@ -274,11 +298,12 @@ public: } private: - CompileItem (Context *ctx, bool compile_fns) - : HIRCompileBase (ctx), compile_fns (compile_fns) + CompileItem (Context *ctx, bool compile_fns, TyTy::BaseType *concrete) + : HIRCompileBase (ctx), compile_fns (compile_fns), concrete (concrete) {} 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 1a798ee..4fbaae3 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -67,24 +67,35 @@ ResolvePathRef::visit (HIR::PathInExpression &expr) return; } - // must be a function call + // must be a function call but it might be a generic function which needs to + // be compiled first + TyTy::BaseType *lookup = nullptr; + bool ok = ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), + &lookup); + rust_assert (ok); + rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF); + Bfunction *fn = nullptr; - if (!ctx->lookup_function_decl (ref, &fn)) + if (!ctx->lookup_function_decl (lookup->get_ty_ref (), &fn)) { - // this might fail because its a forward decl so we can attempt to - // resolve it now + // it must resolve to some kind of HIR::Item HIR::Item *resolved_item = ctx->get_mappings ()->lookup_hir_item ( expr.get_mappings ().get_crate_num (), ref); if (resolved_item == nullptr) { - rust_error_at (expr.get_locus (), "failed to lookup forward decl"); + rust_error_at (expr.get_locus (), "failed to lookup definition decl"); return; } - CompileItem::compile (resolved_item, ctx); - if (!ctx->lookup_function_decl (ref, &fn)) + if (!lookup->has_subsititions_defined ()) + CompileItem::compile (resolved_item, ctx); + else + CompileItem::compile (resolved_item, ctx, true, lookup); + + if (!ctx->lookup_function_decl (lookup->get_ty_ref (), &fn)) { - rust_error_at (expr.get_locus (), "forward decl was not compiled 1"); + rust_fatal_error (expr.get_locus (), + "forward decl was not compiled 1"); return; } } diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h index 815ebd5..774fd2e 100644 --- a/gcc/rust/backend/rust-compile-tyty.h +++ b/gcc/rust/backend/rust-compile-tyty.h @@ -102,7 +102,7 @@ public: void visit (TyTy::IntType &type) override { - switch (type.get_kind ()) + switch (type.get_int_kind ()) { case TyTy::IntType::I8: translated @@ -139,7 +139,7 @@ public: void visit (TyTy::UintType &type) override { - switch (type.get_kind ()) + switch (type.get_uint_kind ()) { case TyTy::UintType::U8: translated = backend->named_type ("u8", backend->integer_type (true, 8), @@ -175,7 +175,7 @@ public: void visit (TyTy::FloatType &type) override { - switch (type.get_kind ()) + switch (type.get_float_kind ()) { case TyTy::FloatType::F32: translated = backend->named_type ("f32", backend->float_type (32), |