diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 3 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.h | 15 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-type.h | 10 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty-rules.h | 16 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.cc | 76 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.h | 45 |
6 files changed, 88 insertions, 77 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index a4c32d8..c2ed0bb 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -391,7 +391,8 @@ public: Bexpression *length = ctx->get_backend ()->integer_constant_expression (capacity_type, ival); - Btype *element_type = TyTyResolveCompile::compile (ctx, type.get_type ()); + Btype *element_type + = TyTyResolveCompile::compile (ctx, type.get_element_type ()); translated = ctx->get_backend ()->array_type (element_type, length); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index 3cc0da8..7e94b981 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -127,11 +127,11 @@ public: return; } - std::vector<HirId> fields; + std::vector<TyTy::TyCtx> fields; for (auto &elem : expr.get_tuple_elems ()) { auto field_ty = TypeCheckExpr::Resolve (elem.get (), false); - fields.push_back (field_ty->get_ref ()); + fields.push_back (TyTy::TyCtx (field_ty->get_ref ())); } infered = new TyTy::TupleType (expr.get_mappings ().get_hirid (), fields); } @@ -460,7 +460,7 @@ public: rust_assert (ok); infered = new TyTy::ReferenceType (expr.get_mappings ().get_hirid (), - base->get_ref ()); + TyTy::TyCtx (base->get_ref ())); } break; @@ -649,7 +649,7 @@ public: } TyTy::ArrayType *array_type = (TyTy::ArrayType *) infered; - infered = array_type->get_type ()->clone (); + infered = array_type->get_element_type ()->clone (); } void visit (HIR::ArrayExpr &expr) @@ -660,8 +660,9 @@ public: elements->accept_vis (*this); rust_assert (infered_array_elems != nullptr); - infered = new TyTy::ArrayType (expr.get_mappings ().get_hirid (), num_elems, - infered_array_elems); + infered + = new TyTy::ArrayType (expr.get_mappings ().get_hirid (), num_elems, + TyTy::TyCtx (infered_array_elems->get_ref ())); } void visit (HIR::ArrayElemsValues &elems) @@ -891,7 +892,7 @@ public: // FIXME double_reference infered = new TyTy::ReferenceType (expr.get_mappings ().get_hirid (), - resolved_base->get_ref ()); + TyTy::TyCtx (resolved_base->get_ref ())); } void visit (HIR::DereferenceExpr &expr) diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.h b/gcc/rust/typecheck/rust-hir-type-check-type.h index 1dbcd37..af58d60 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.h +++ b/gcc/rust/typecheck/rust-hir-type-check-type.h @@ -142,11 +142,11 @@ public: return; } - std::vector<HirId> fields; + std::vector<TyTy::TyCtx> fields; for (auto &elem : tuple.get_elems ()) { auto field_ty = TypeCheckType::Resolve (elem.get ()); - fields.push_back (field_ty->get_ref ()); + fields.push_back (TyTy::TyCtx (field_ty->get_ref ())); } translated @@ -247,8 +247,8 @@ public: } TyTy::BaseType *base = TypeCheckType::Resolve (type.get_element_type ()); - translated - = new TyTy::ArrayType (type.get_mappings ().get_hirid (), capacity, base); + translated = new TyTy::ArrayType (type.get_mappings ().get_hirid (), + capacity, TyTy::TyCtx (base->get_ref ())); } void visit (HIR::ReferenceType &type) @@ -256,7 +256,7 @@ public: TyTy::BaseType *base = TypeCheckType::Resolve (type.get_base_type ().get ()); translated = new TyTy::ReferenceType (type.get_mappings ().get_hirid (), - base->get_ref ()); + TyTy::TyCtx (base->get_ref ())); } void visit (HIR::InferredType &type) diff --git a/gcc/rust/typecheck/rust-tyty-rules.h b/gcc/rust/typecheck/rust-tyty-rules.h index 758ec58..0ea7769 100644 --- a/gcc/rust/typecheck/rust-tyty-rules.h +++ b/gcc/rust/typecheck/rust-tyty-rules.h @@ -566,7 +566,8 @@ public: void visit (ArrayType &type) override { // check base type - auto base_resolved = base->get_type ()->unify (type.get_type ()); + auto base_resolved + = base->get_element_type ()->unify (type.get_element_type ()); if (base_resolved == nullptr) { BaseRules::visit (type); @@ -582,8 +583,9 @@ public: return; } - resolved = new ArrayType (type.get_ref (), type.get_ty_ref (), - type.get_capacity (), base_resolved); + resolved + = new ArrayType (type.get_ref (), type.get_ty_ref (), + type.get_capacity (), TyCtx (base_resolved->get_ref ())); } private: @@ -758,7 +760,7 @@ class TupleRules : public BaseRules public: TupleRules (TupleType *base) : BaseRules (base), base (base) {} - void visit (TupleType &type) + void visit (TupleType &type) override { if (base->num_fields () != type.num_fields ()) { @@ -766,7 +768,7 @@ public: return; } - std::vector<HirId> fields; + std::vector<TyCtx> fields; for (size_t i = 0; i < base->num_fields (); i++) { BaseType *bo = base->get_field (i); @@ -779,7 +781,7 @@ public: return; } - fields.push_back (unified_ty->get_ref ()); + fields.push_back (TyCtx (unified_ty->get_ref ())); } resolved @@ -888,7 +890,7 @@ public: } resolved = new ReferenceType (base->get_ref (), base->get_ty_ref (), - base_resolved->get_ref ()); + TyCtx (base_resolved->get_ref ())); } private: diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index 4894f06..8202679 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -27,6 +27,25 @@ namespace Rust { namespace TyTy { +TyCtx::TyCtx (HirId ref) : ref (ref) +{ + // ensure this reference is defined within the context + auto context = Resolver::TypeCheckContext::get (); + BaseType *lookup = nullptr; + bool ok = context->lookup_type (ref, &lookup); + rust_assert (ok); +} + +BaseType * +TyCtx::get_tyty () const +{ + auto context = Resolver::TypeCheckContext::get (); + BaseType *lookup = nullptr; + bool ok = context->lookup_type (ref, &lookup); + rust_assert (ok); + return lookup; +} + void UnitType::accept_vis (TyVisitor &vis) { @@ -314,11 +333,7 @@ TupleType::as_string () const BaseType * TupleType::get_field (size_t index) const { - auto context = Resolver::TypeCheckContext::get (); - BaseType *lookup = nullptr; - bool ok = context->lookup_type (fields.at (index), &lookup); - rust_assert (ok); - return lookup; + return fields.at (index).get_tyty (); } BaseType * @@ -435,8 +450,8 @@ ArrayType::accept_vis (TyVisitor &vis) std::string ArrayType::as_string () const { - return "[" + get_type ()->as_string () + ":" + std::to_string (capacity) - + "]"; + return "[" + get_element_type ()->as_string () + ":" + + std::to_string (capacity) + "]"; } BaseType * @@ -450,32 +465,29 @@ bool ArrayType::is_equal (const BaseType &other) const { if (get_kind () != other.get_kind ()) - { - return false; - } - else - { - auto other2 = static_cast<const ArrayType &> (other); - return get_type () == other2.get_type () - && get_capacity () == other2.get_capacity (); - } + return false; + + auto other2 = static_cast<const ArrayType &> (other); + if (get_capacity () != other2.get_capacity ()) + return false; + + auto this_element_type = get_element_type (); + auto other_element_type = other2.get_element_type (); + + return this_element_type->is_equal (*other_element_type); } BaseType * -ArrayType::get_type () const +ArrayType::get_element_type () const { - auto context = Resolver::TypeCheckContext::get (); - BaseType *lookup = nullptr; - bool ok = context->lookup_type (element_type_id, &lookup); - rust_assert (ok); - return lookup; + return element_type.get_tyty (); } BaseType * ArrayType::clone () { return new ArrayType (get_ref (), get_ty_ref (), get_capacity (), - get_type ()->clone (), get_combined_refs ()); + element_type, get_combined_refs ()); } void @@ -721,24 +733,10 @@ ReferenceType::is_equal (const BaseType &other) const return get_base ()->is_equal (*other2.get_base ()); } -const BaseType * -ReferenceType::get_base () const -{ - auto context = Resolver::TypeCheckContext::get (); - BaseType *lookup = nullptr; - bool ok = context->lookup_type (base, &lookup); - rust_assert (ok); - return lookup; -} - BaseType * -ReferenceType::get_base () +ReferenceType::get_base () const { - auto context = Resolver::TypeCheckContext::get (); - BaseType *lookup = nullptr; - bool ok = context->lookup_type (base, &lookup); - rust_assert (ok); - return lookup; + return base.get_tyty (); } BaseType * diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 4812932..372cb7d 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -115,6 +115,20 @@ protected: Analysis::Mappings *mappings; }; +// this is a placeholder for types that can change like inference variables +class TyCtx +{ +public: + explicit TyCtx (HirId ref); + + HirId get_ref () const { return ref; } + + BaseType *get_tyty () const; + +private: + HirId ref; +}; + class InferType : public BaseType { public: @@ -229,13 +243,12 @@ private: class TupleType : public BaseType { public: - TupleType (HirId ref, std::vector<HirId> fields, - + TupleType (HirId ref, std::vector<TyCtx> fields, std::set<HirId> refs = std::set<HirId> ()) : BaseType (ref, ref, TypeKind::TUPLE, refs), fields (fields) {} - TupleType (HirId ref, HirId ty_ref, std::vector<HirId> fields, + TupleType (HirId ref, HirId ty_ref, std::vector<TyCtx> fields, std::set<HirId> refs = std::set<HirId> ()) : BaseType (ref, ty_ref, TypeKind::TUPLE, refs), fields (fields) {} @@ -268,7 +281,7 @@ public: std::string get_name () const override final { return as_string (); } private: - std::vector<HirId> fields; + std::vector<TyCtx> fields; }; class ParamType : public BaseType @@ -551,16 +564,16 @@ private: class ArrayType : public BaseType { public: - ArrayType (HirId ref, size_t capacity, BaseType *type, + ArrayType (HirId ref, size_t capacity, TyCtx base, std::set<HirId> refs = std::set<HirId> ()) : BaseType (ref, ref, TypeKind::ARRAY, refs), capacity (capacity), - element_type_id (type->get_ref ()) + element_type (base) {} - ArrayType (HirId ref, HirId ty_ref, size_t capacity, BaseType *type, + ArrayType (HirId ref, HirId ty_ref, size_t capacity, TyCtx base, std::set<HirId> refs = std::set<HirId> ()) : BaseType (ref, ty_ref, TypeKind::ARRAY, refs), capacity (capacity), - element_type_id (type->get_ref ()) + element_type (base) {} void accept_vis (TyVisitor &vis) override; @@ -575,15 +588,13 @@ public: size_t get_capacity () const { return capacity; } - HirId element_type_ref () const { return element_type_id; } - - BaseType *get_type () const; + BaseType *get_element_type () const; BaseType *clone () final override; private: size_t capacity; - HirId element_type_id; + TyCtx element_type; }; class BoolType : public BaseType @@ -787,19 +798,17 @@ public: class ReferenceType : public BaseType { public: - ReferenceType (HirId ref, HirId base, + ReferenceType (HirId ref, TyCtx base, std::set<HirId> refs = std::set<HirId> ()) : BaseType (ref, ref, TypeKind::REF), base (base) {} - ReferenceType (HirId ref, HirId ty_ref, HirId base, + ReferenceType (HirId ref, HirId ty_ref, TyCtx base, std::set<HirId> refs = std::set<HirId> ()) : BaseType (ref, ty_ref, TypeKind::REF), base (base) {} - const TyTy::BaseType *get_base () const; - - TyTy::BaseType *get_base (); + BaseType *get_base () const; void accept_vis (TyVisitor &vis) override; @@ -814,7 +823,7 @@ public: BaseType *clone () final override; private: - HirId base; + TyCtx base; }; class StrType : public BaseType |