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/rust-compile-context.h | |
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/rust-compile-context.h')
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 49 |
1 files changed, 34 insertions, 15 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; |