diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-07-16 14:57:55 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-16 14:57:55 +0000 |
commit | eea221e39ead0f45e9ef91aaf1b5ed855a4a5d8b (patch) | |
tree | a0eb9997e6add34e1040f8454965cdb0f3db6410 | |
parent | d57f3b8af1498c7bb4b1eaea372270119e9845fd (diff) | |
parent | 40ab687d56597abc6d2ead6dc4e5a051923aca45 (diff) | |
download | gcc-eea221e39ead0f45e9ef91aaf1b5ed855a4a5d8b.zip gcc-eea221e39ead0f45e9ef91aaf1b5ed855a4a5d8b.tar.gz gcc-eea221e39ead0f45e9ef91aaf1b5ed855a4a5d8b.tar.bz2 |
Merge #572
572: Cleanup and add const modifiers to equlity interface r=philberty a=philberty
As part of an effort to cleanup some of the interfaces within the TyTy module
this PR adds a const visitor for accessing each type and updates the can_eq
interface to also be const as it should not require mutability.
These changes fell out of a branch for optional trait items support.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 4 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-path-probe.h | 27 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty-cmp.h | 313 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty-visitor.h | 24 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.cc | 152 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.h | 68 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.cc | 24 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.h | 5 |
8 files changed, 408 insertions, 209 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 68324c7..5ba5918 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -396,6 +396,8 @@ public: mappings->insert_defid_mapping (mapping.get_defid (), translated); mappings->insert_hir_item (mapping.get_crate_num (), mapping.get_hirid (), translated); + mappings->insert_hir_impl_block (mapping.get_crate_num (), + mapping.get_hirid (), hir_impl_block); mappings->insert_location (crate_num, mapping.get_hirid (), impl_block.get_locus ()); @@ -540,6 +542,8 @@ public: mappings->insert_defid_mapping (mapping.get_defid (), translated); mappings->insert_hir_item (mapping.get_crate_num (), mapping.get_hirid (), translated); + mappings->insert_hir_impl_block (mapping.get_crate_num (), + mapping.get_hirid (), hir_impl_block); mappings->insert_location (crate_num, mapping.get_hirid (), impl_block.get_locus ()); diff --git a/gcc/rust/typecheck/rust-hir-path-probe.h b/gcc/rust/typecheck/rust-hir-path-probe.h index a10a562..1b7aa4d 100644 --- a/gcc/rust/typecheck/rust-hir-path-probe.h +++ b/gcc/rust/typecheck/rust-hir-path-probe.h @@ -29,8 +29,17 @@ namespace Resolver { struct PathProbeCandidate { + enum CandidateType + { + IMPL_CONST, + IMPL_TYPE_ALIAS, + IMPL_FUNC, + }; + + CandidateType type; HIR::ImplItem *impl_item; TyTy::BaseType *ty; + HIR::ImplBlock *parent; }; class PathProbeType : public TypeCheckBase @@ -48,11 +57,13 @@ public: probe.process_candidate (id, item, impl); return true; }); + return probe.candidates; } void process_candidate (HirId id, HIR::ImplItem *item, HIR::ImplBlock *impl) { + current_impl = impl; HirId impl_ty_id = impl->get_type ()->get_mappings ().get_hirid (); TyTy::BaseType *impl_block_ty = nullptr; bool ok = context->lookup_type (impl_ty_id, &impl_block_ty); @@ -75,7 +86,9 @@ public: bool ok = context->lookup_type (tyid, &ty); rust_assert (ok); - PathProbeCandidate candidate{&alias, ty}; + PathProbeCandidate candidate{ + PathProbeCandidate::CandidateType::IMPL_TYPE_ALIAS, &alias, ty, + current_impl}; candidates.push_back (std::move (candidate)); } } @@ -90,7 +103,9 @@ public: bool ok = context->lookup_type (tyid, &ty); rust_assert (ok); - PathProbeCandidate candidate{&constant, ty}; + PathProbeCandidate candidate{ + PathProbeCandidate::CandidateType::IMPL_CONST, &constant, ty, + current_impl}; candidates.push_back (std::move (candidate)); } } @@ -105,19 +120,23 @@ public: bool ok = context->lookup_type (tyid, &ty); rust_assert (ok); - PathProbeCandidate candidate{&function, ty}; + PathProbeCandidate candidate{ + PathProbeCandidate::CandidateType::IMPL_FUNC, &function, ty, + current_impl}; candidates.push_back (std::move (candidate)); } } private: PathProbeType (TyTy::BaseType *receiver, const HIR::PathIdentSegment &query) - : TypeCheckBase (), receiver (receiver), search (query) + : TypeCheckBase (), receiver (receiver), search (query), + current_impl (nullptr) {} TyTy::BaseType *receiver; const HIR::PathIdentSegment &search; std::vector<PathProbeCandidate> candidates; + HIR::ImplBlock *current_impl; }; class ReportMultipleCandidateError : private TypeCheckBase diff --git a/gcc/rust/typecheck/rust-tyty-cmp.h b/gcc/rust/typecheck/rust-tyty-cmp.h index f01ef3b..e856c41 100644 --- a/gcc/rust/typecheck/rust-tyty-cmp.h +++ b/gcc/rust/typecheck/rust-tyty-cmp.h @@ -28,17 +28,19 @@ namespace Rust { namespace TyTy { -class BaseCmp : public TyVisitor +class BaseCmp : public TyConstVisitor { public: - virtual bool can_eq (BaseType *other) + virtual bool can_eq (const BaseType *other) { if (other->get_kind () == TypeKind::PARAM) { - ParamType *p = static_cast<ParamType *> (other); + const ParamType *p = static_cast<const ParamType *> (other); if (p->can_resolve ()) { - other = p->resolve (); + const BaseType *resolved = p->resolve (); + resolved->accept_vis (*this); + return ok; } } @@ -46,7 +48,7 @@ public: return ok; } - virtual void visit (TupleType &type) override + virtual void visit (const TupleType &type) override { ok = false; @@ -63,7 +65,7 @@ public: } } - virtual void visit (ADTType &type) override + virtual void visit (const ADTType &type) override { ok = false; if (emit_error_flag) @@ -79,7 +81,7 @@ public: } } - virtual void visit (InferType &type) override + virtual void visit (const InferType &type) override { ok = false; if (emit_error_flag) @@ -95,7 +97,7 @@ public: } } - virtual void visit (FnType &type) override + virtual void visit (const FnType &type) override { ok = false; if (emit_error_flag) @@ -111,7 +113,7 @@ public: } } - virtual void visit (FnPtr &type) override + virtual void visit (const FnPtr &type) override { ok = false; if (emit_error_flag) @@ -127,7 +129,7 @@ public: } } - virtual void visit (ArrayType &type) override + virtual void visit (const ArrayType &type) override { ok = false; if (emit_error_flag) @@ -143,7 +145,7 @@ public: } } - virtual void visit (BoolType &type) override + virtual void visit (const BoolType &type) override { ok = false; if (emit_error_flag) @@ -159,7 +161,7 @@ public: } } - virtual void visit (IntType &type) override + virtual void visit (const IntType &type) override { ok = false; if (emit_error_flag) @@ -175,7 +177,7 @@ public: } } - virtual void visit (UintType &type) override + virtual void visit (const UintType &type) override { ok = false; if (emit_error_flag) @@ -191,7 +193,7 @@ public: } } - virtual void visit (USizeType &type) override + virtual void visit (const USizeType &type) override { ok = false; if (emit_error_flag) @@ -207,7 +209,7 @@ public: } } - virtual void visit (ISizeType &type) override + virtual void visit (const ISizeType &type) override { ok = false; if (emit_error_flag) @@ -223,7 +225,7 @@ public: } } - virtual void visit (FloatType &type) override + virtual void visit (const FloatType &type) override { ok = false; if (emit_error_flag) @@ -239,7 +241,7 @@ public: } } - virtual void visit (ErrorType &type) override + virtual void visit (const ErrorType &type) override { ok = false; if (emit_error_flag) @@ -255,7 +257,7 @@ public: } } - virtual void visit (CharType &type) override + virtual void visit (const CharType &type) override { ok = false; if (emit_error_flag) @@ -271,7 +273,7 @@ public: } } - virtual void visit (ReferenceType &type) override + virtual void visit (const ReferenceType &type) override { ok = false; if (emit_error_flag) @@ -287,7 +289,7 @@ public: } } - virtual void visit (StrType &type) override + virtual void visit (const StrType &type) override { ok = false; if (emit_error_flag) @@ -303,7 +305,7 @@ public: } } - virtual void visit (NeverType &type) override + virtual void visit (const NeverType &type) override { ok = false; if (emit_error_flag) @@ -319,20 +321,20 @@ public: } } - virtual void visit (PlaceholderType &type) override + virtual void visit (const PlaceholderType &type) override { // it is ok for types to can eq to a placeholder ok = true; } - virtual void visit (ParamType &type) override + virtual void visit (const ParamType &type) override { // it is ok for types to can eq to a ParamType ok = true; } protected: - BaseCmp (BaseType *base, bool emit_errors) + BaseCmp (const BaseType *base, bool emit_errors) : mappings (Analysis::Mappings::get ()), context (Resolver::TypeCheckContext::get ()), ok (false), emit_error_flag (emit_errors) @@ -346,7 +348,7 @@ protected: private: /* Returns a pointer to the ty that created this rule. */ - virtual BaseType *get_base () = 0; + virtual const BaseType *get_base () const = 0; }; class InferCmp : public BaseCmp @@ -354,11 +356,11 @@ class InferCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - InferCmp (InferType *base, bool emit_errors) + InferCmp (const InferType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (BoolType &type) override + void visit (const BoolType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL); @@ -371,7 +373,7 @@ public: BaseCmp::visit (type); } - void visit (IntType &type) override + void visit (const IntType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL) @@ -386,7 +388,7 @@ public: BaseCmp::visit (type); } - void visit (UintType &type) override + void visit (const UintType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL) @@ -401,7 +403,7 @@ public: BaseCmp::visit (type); } - void visit (USizeType &type) override + void visit (const USizeType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL) @@ -416,7 +418,7 @@ public: BaseCmp::visit (type); } - void visit (ISizeType &type) override + void visit (const ISizeType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL) @@ -431,7 +433,7 @@ public: BaseCmp::visit (type); } - void visit (FloatType &type) override + void visit (const FloatType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL) @@ -445,7 +447,7 @@ public: BaseCmp::visit (type); } - void visit (ArrayType &type) override + void visit (const ArrayType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL); @@ -458,7 +460,7 @@ public: BaseCmp::visit (type); } - void visit (ADTType &type) override + void visit (const ADTType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL); @@ -471,7 +473,7 @@ public: BaseCmp::visit (type); } - void visit (TupleType &type) override + void visit (const TupleType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL); @@ -484,7 +486,7 @@ public: BaseCmp::visit (type); } - void visit (InferType &type) override + void visit (const InferType &type) override { switch (base->get_infer_kind ()) { @@ -524,7 +526,7 @@ public: BaseCmp::visit (type); } - void visit (CharType &type) override + void visit (const CharType &type) override { { bool is_valid @@ -539,7 +541,7 @@ public: } } - void visit (ReferenceType &type) override + void visit (const ReferenceType &type) override { bool is_valid @@ -553,7 +555,7 @@ public: BaseCmp::visit (type); } - void visit (ParamType &type) override + void visit (const ParamType &type) override { bool is_valid = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL); @@ -567,9 +569,8 @@ public: } private: - BaseType *get_base () override { return base; } - - InferType *base; + const BaseType *get_base () const override { return base; } + const InferType *base; }; class FnCmp : public BaseCmp @@ -577,16 +578,16 @@ class FnCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - FnCmp (FnType *base, bool emit_errors) + FnCmp (const FnType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL; } - void visit (FnType &type) override + void visit (const FnType &type) override { if (base->num_params () != type.num_params ()) { @@ -619,9 +620,8 @@ public: } private: - BaseType *get_base () override { return base; } - - FnType *base; + const BaseType *get_base () const override { return base; } + const FnType *base; }; class FnptrCmp : public BaseCmp @@ -629,11 +629,11 @@ class FnptrCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - FnptrCmp (FnPtr *base, bool emit_errors) + FnptrCmp (const FnPtr *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { if (type.get_infer_kind () != InferType::InferTypeKind::GENERAL) { @@ -644,7 +644,7 @@ public: ok = true; } - void visit (FnPtr &type) override + void visit (const FnPtr &type) override { if (base->num_params () != type.num_params ()) { @@ -674,7 +674,7 @@ public: ok = true; } - void visit (FnType &type) override + void visit (const FnType &type) override { if (base->num_params () != type.num_params ()) { @@ -705,9 +705,8 @@ public: } private: - BaseType *get_base () override { return base; } - - FnPtr *base; + const BaseType *get_base () const override { return base; } + const FnPtr *base; }; class ArrayCmp : public BaseCmp @@ -715,26 +714,24 @@ class ArrayCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - ArrayCmp (ArrayType *base, bool emit_errors) + ArrayCmp (const ArrayType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (ArrayType &type) override + void visit (const ArrayType &type) override { - // check base type - auto base_resolved - = base->get_element_type ()->unify (type.get_element_type ()); - if (base_resolved->get_kind () == TypeKind::ERROR) + // need to check the base types and capacity + if (type.get_capacity () != base->get_capacity ()) { BaseCmp::visit (type); return; } - // need to check the base types and capacity - if (type.get_capacity () != base->get_capacity ()) + // check base type + const BaseType *base_element = base->get_element_type (); + const BaseType *other_element = type.get_element_type (); + if (!base_element->can_eq (other_element, emit_error_flag)) { - Location locus = mappings->lookup_location (type.get_ref ()); - rust_error_at (locus, "mismatch in array capacity"); BaseCmp::visit (type); return; } @@ -743,9 +740,8 @@ public: } private: - BaseType *get_base () override { return base; } - - ArrayType *base; + const BaseType *get_base () const override { return base; } + const ArrayType *base; }; class BoolCmp : public BaseCmp @@ -753,21 +749,20 @@ class BoolCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - BoolCmp (BoolType *base, bool emit_errors) + BoolCmp (const BoolType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (BoolType &type) override { ok = true; } + void visit (const BoolType &type) override { ok = true; } - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL; } private: - BaseType *get_base () override { return base; } - - BoolType *base; + const BaseType *get_base () const override { return base; } + const BoolType *base; }; class IntCmp : public BaseCmp @@ -775,24 +770,23 @@ class IntCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - IntCmp (IntType *base, bool emit_errors) + IntCmp (const IntType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT; } - void visit (IntType &type) override + void visit (const IntType &type) override { ok = type.get_int_kind () == base->get_int_kind (); } private: - BaseType *get_base () override { return base; } - - IntType *base; + const BaseType *get_base () const override { return base; } + const IntType *base; }; class UintCmp : public BaseCmp @@ -800,24 +794,23 @@ class UintCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - UintCmp (UintType *base, bool emit_errors) + UintCmp (const UintType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT; } - void visit (UintType &type) override + void visit (const UintType &type) override { ok = type.get_uint_kind () == base->get_uint_kind (); } private: - BaseType *get_base () override { return base; } - - UintType *base; + const BaseType *get_base () const override { return base; } + const UintType *base; }; class FloatCmp : public BaseCmp @@ -825,24 +818,23 @@ class FloatCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - FloatCmp (FloatType *base, bool emit_errors) + FloatCmp (const FloatType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () != InferType::InferTypeKind::INTEGRAL; } - void visit (FloatType &type) override + void visit (const FloatType &type) override { ok = type.get_float_kind () == base->get_float_kind (); } private: - BaseType *get_base () override { return base; } - - FloatType *base; + const BaseType *get_base () const override { return base; } + const FloatType *base; }; class ADTCmp : public BaseCmp @@ -850,11 +842,11 @@ class ADTCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - ADTCmp (ADTType *base, bool emit_errors) + ADTCmp (const ADTType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (ADTType &type) override + void visit (const ADTType &type) override { if (base->get_identifier ().compare (type.get_identifier ()) != 0) { @@ -870,8 +862,8 @@ public: for (size_t i = 0; i < type.num_fields (); ++i) { - TyTy::StructFieldType *base_field = base->get_field (i); - TyTy::StructFieldType *other_field = type.get_field (i); + const TyTy::StructFieldType *base_field = base->get_imm_field (i); + const TyTy::StructFieldType *other_field = type.get_imm_field (i); TyTy::BaseType *this_field_ty = base_field->get_field_type (); TyTy::BaseType *other_field_ty = other_field->get_field_type (); @@ -887,9 +879,8 @@ public: } private: - BaseType *get_base () override { return base; } - - ADTType *base; + const BaseType *get_base () const override { return base; } + const ADTType *base; }; class TupleCmp : public BaseCmp @@ -897,11 +888,11 @@ class TupleCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - TupleCmp (TupleType *base, bool emit_errors) + TupleCmp (const TupleType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (TupleType &type) override + void visit (const TupleType &type) override { if (base->num_fields () != type.num_fields ()) { @@ -925,9 +916,8 @@ public: } private: - BaseType *get_base () override { return base; } - - TupleType *base; + const BaseType *get_base () const override { return base; } + const TupleType *base; }; class USizeCmp : public BaseCmp @@ -935,21 +925,20 @@ class USizeCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - USizeCmp (USizeType *base, bool emit_errors) + USizeCmp (const USizeType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT; } - void visit (USizeType &type) override { ok = true; } + void visit (const USizeType &type) override { ok = true; } private: - BaseType *get_base () override { return base; } - - USizeType *base; + const BaseType *get_base () const override { return base; } + const USizeType *base; }; class ISizeCmp : public BaseCmp @@ -957,21 +946,20 @@ class ISizeCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - ISizeCmp (ISizeType *base, bool emit_errors) + ISizeCmp (const ISizeType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () != InferType::InferTypeKind::FLOAT; } - void visit (ISizeType &type) override { ok = true; } + void visit (const ISizeType &type) override { ok = true; } private: - BaseType *get_base () override { return base; } - - ISizeType *base; + const BaseType *get_base () const override { return base; } + const ISizeType *base; }; class CharCmp : public BaseCmp @@ -979,21 +967,20 @@ class CharCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - CharCmp (CharType *base, bool emit_errors) + CharCmp (const CharType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (InferType &type) override + void visit (const InferType &type) override { ok = type.get_infer_kind () == InferType::InferTypeKind::GENERAL; } - void visit (CharType &type) override { ok = true; } + void visit (const CharType &type) override { ok = true; } private: - BaseType *get_base () override { return base; } - - CharType *base; + const BaseType *get_base () const override { return base; } + const CharType *base; }; class ReferenceCmp : public BaseCmp @@ -1001,11 +988,11 @@ class ReferenceCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - ReferenceCmp (ReferenceType *base, bool emit_errors) + ReferenceCmp (const ReferenceType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (ReferenceType &type) override + void visit (const ReferenceType &type) override { auto base_type = base->get_base (); auto other_base_type = type.get_base (); @@ -1014,9 +1001,8 @@ public: } private: - BaseType *get_base () override { return base; } - - ReferenceType *base; + const BaseType *get_base () const override { return base; } + const ReferenceType *base; }; class ParamCmp : public BaseCmp @@ -1024,7 +1010,7 @@ class ParamCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - ParamCmp (ParamType *base, bool emit_errors) + ParamCmp (const ParamType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} @@ -1037,7 +1023,7 @@ public: // // rust also allows for a = foo{a:123}; Where we can use an Inference Variable // to handle the typing of the struct - bool can_eq (BaseType *other) override final + bool can_eq (const BaseType *other) override { if (base->get_ref () == base->get_ty_ref ()) return BaseCmp::can_eq (other); @@ -1062,12 +1048,11 @@ public: // impl <X>Foo<X> { ... } // both of these types are compatible so we mostly care about the number of // generic arguments - void visit (ParamType &type) override { ok = true; } + void visit (const ParamType &type) override { ok = true; } private: - BaseType *get_base () override { return base; } - - ParamType *base; + const BaseType *get_base () const override { return base; } + const ParamType *base; }; class StrCmp : public BaseCmp @@ -1076,16 +1061,15 @@ class StrCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - StrCmp (StrType *base, bool emit_errors) + StrCmp (const StrType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (StrType &type) override { ok = true; } + void visit (const StrType &type) override { ok = true; } private: - BaseType *get_base () override { return base; } - - StrType *base; + const BaseType *get_base () const override { return base; } + const StrType *base; }; class NeverCmp : public BaseCmp @@ -1093,16 +1077,15 @@ class NeverCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - NeverCmp (NeverType *base, bool emit_errors) + NeverCmp (const NeverType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - void visit (NeverType &type) override { ok = true; } + void visit (const NeverType &type) override { ok = true; } private: - BaseType *get_base () override { return base; } - - NeverType *base; + const BaseType *get_base () const override { return base; } + const NeverType *base; }; class PlaceholderCmp : public BaseCmp @@ -1110,52 +1093,52 @@ class PlaceholderCmp : public BaseCmp using Rust::TyTy::BaseCmp::visit; public: - PlaceholderCmp (PlaceholderType *base, bool emit_errors) + PlaceholderCmp (const PlaceholderType *base, bool emit_errors) : BaseCmp (base, emit_errors), base (base) {} - virtual void visit (TupleType &) override { ok = true; } + virtual void visit (const TupleType &) override { ok = true; } - virtual void visit (ADTType &) override { ok = true; } + virtual void visit (const ADTType &) override { ok = true; } - virtual void visit (InferType &) override { ok = true; } + virtual void visit (const InferType &) override { ok = true; } - virtual void visit (FnType &) override { ok = true; } + virtual void visit (const FnType &) override { ok = true; } - virtual void visit (FnPtr &) override { ok = true; } + virtual void visit (const FnPtr &) override { ok = true; } - virtual void visit (ArrayType &) override { ok = true; } + virtual void visit (const ArrayType &) override { ok = true; } - virtual void visit (BoolType &) override { ok = true; } + virtual void visit (const BoolType &) override { ok = true; } - virtual void visit (IntType &) override { ok = true; } + virtual void visit (const IntType &) override { ok = true; } - virtual void visit (UintType &) override { ok = true; } + virtual void visit (const UintType &) override { ok = true; } - virtual void visit (USizeType &) override { ok = true; } + virtual void visit (const USizeType &) override { ok = true; } - virtual void visit (ISizeType &) override { ok = true; } + virtual void visit (const ISizeType &) override { ok = true; } - virtual void visit (FloatType &) override { ok = true; } + virtual void visit (const FloatType &) override { ok = true; } - virtual void visit (ErrorType &) override { ok = true; } + virtual void visit (const ErrorType &) override { ok = true; } - virtual void visit (CharType &) override { ok = true; } + virtual void visit (const CharType &) override { ok = true; } - virtual void visit (ReferenceType &) override { ok = true; } + virtual void visit (const ReferenceType &) override { ok = true; } - virtual void visit (ParamType &) override { ok = true; } + virtual void visit (const ParamType &) override { ok = true; } - virtual void visit (StrType &) override { ok = true; } + virtual void visit (const StrType &) override { ok = true; } - virtual void visit (NeverType &) override { ok = true; } + virtual void visit (const NeverType &) override { ok = true; } - virtual void visit (PlaceholderType &) override { ok = true; } + virtual void visit (const PlaceholderType &) override { ok = true; } private: - BaseType *get_base () override { return base; } + const BaseType *get_base () const override { return base; } - PlaceholderType *base; + const PlaceholderType *base; }; } // namespace TyTy diff --git a/gcc/rust/typecheck/rust-tyty-visitor.h b/gcc/rust/typecheck/rust-tyty-visitor.h index 2bac5fe..2dd97b8 100644 --- a/gcc/rust/typecheck/rust-tyty-visitor.h +++ b/gcc/rust/typecheck/rust-tyty-visitor.h @@ -48,6 +48,30 @@ public: virtual void visit (PlaceholderType &type) = 0; }; +class TyConstVisitor +{ +public: + virtual void visit (const InferType &type) = 0; + virtual void visit (const ADTType &type) = 0; + virtual void visit (const TupleType &type) = 0; + virtual void visit (const FnType &type) = 0; + virtual void visit (const FnPtr &type) = 0; + virtual void visit (const ArrayType &type) = 0; + virtual void visit (const BoolType &type) = 0; + virtual void visit (const IntType &type) = 0; + virtual void visit (const UintType &type) = 0; + virtual void visit (const FloatType &type) = 0; + virtual void visit (const USizeType &type) = 0; + virtual void visit (const ISizeType &type) = 0; + virtual void visit (const ErrorType &type) = 0; + virtual void visit (const CharType &type) = 0; + virtual void visit (const ReferenceType &type) = 0; + virtual void visit (const ParamType &type) = 0; + virtual void visit (const StrType &type) = 0; + virtual void visit (const NeverType &type) = 0; + virtual void visit (const PlaceholderType &type) = 0; +}; + } // namespace TyTy } // namespace Rust diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index ab47310..e2c5f71 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -75,6 +75,12 @@ InferType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +InferType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string InferType::as_string () const { @@ -98,7 +104,7 @@ InferType::unify (BaseType *other) } bool -InferType::can_eq (BaseType *other, bool emit_errors) +InferType::can_eq (const BaseType *other, bool emit_errors) const { InferCmp r (this, emit_errors); return r.can_eq (other); @@ -142,6 +148,12 @@ ErrorType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +ErrorType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string ErrorType::as_string () const { @@ -155,7 +167,7 @@ ErrorType::unify (BaseType *other) } bool -ErrorType::can_eq (BaseType *other, bool emit_errors) +ErrorType::can_eq (const BaseType *other, bool emit_errors) const { return get_kind () == other->get_kind (); } @@ -379,6 +391,12 @@ ADTType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +ADTType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string ADTType::as_string () const { @@ -421,7 +439,7 @@ ADTType::unify (BaseType *other) } bool -ADTType::can_eq (BaseType *other, bool emit_errors) +ADTType::can_eq (const BaseType *other, bool emit_errors) const { ADTCmp r (this, emit_errors); return r.can_eq (other); @@ -556,6 +574,12 @@ TupleType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +TupleType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string TupleType::as_string () const { @@ -582,7 +606,7 @@ TupleType::unify (BaseType *other) } bool -TupleType::can_eq (BaseType *other, bool emit_errors) +TupleType::can_eq (const BaseType *other, bool emit_errors) const { TupleCmp r (this, emit_errors); return r.can_eq (other); @@ -642,6 +666,12 @@ FnType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +FnType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string FnType::as_string () const { @@ -666,7 +696,7 @@ FnType::unify (BaseType *other) } bool -FnType::can_eq (BaseType *other, bool emit_errors) +FnType::can_eq (const BaseType *other, bool emit_errors) const { FnCmp r (this, emit_errors); return r.can_eq (other); @@ -842,6 +872,12 @@ FnPtr::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +FnPtr::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string FnPtr::as_string () const { @@ -861,7 +897,7 @@ FnPtr::unify (BaseType *other) } bool -FnPtr::can_eq (BaseType *other, bool emit_errors) +FnPtr::can_eq (const BaseType *other, bool emit_errors) const { FnptrCmp r (this, emit_errors); return r.can_eq (other); @@ -907,6 +943,12 @@ ArrayType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +ArrayType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string ArrayType::as_string () const { @@ -928,7 +970,7 @@ ArrayType::unify (BaseType *other) } bool -ArrayType::can_eq (BaseType *other, bool emit_errors) +ArrayType::can_eq (const BaseType *other, bool emit_errors) const { ArrayCmp r (this, emit_errors); return r.can_eq (other); @@ -969,6 +1011,12 @@ BoolType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +BoolType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string BoolType::as_string () const { @@ -983,7 +1031,7 @@ BoolType::unify (BaseType *other) } bool -BoolType::can_eq (BaseType *other, bool emit_errors) +BoolType::can_eq (const BaseType *other, bool emit_errors) const { BoolCmp r (this, emit_errors); return r.can_eq (other); @@ -1001,6 +1049,12 @@ IntType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +IntType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string IntType::as_string () const { @@ -1029,7 +1083,7 @@ IntType::unify (BaseType *other) } bool -IntType::can_eq (BaseType *other, bool emit_errors) +IntType::can_eq (const BaseType *other, bool emit_errors) const { IntCmp r (this, emit_errors); return r.can_eq (other); @@ -1058,6 +1112,12 @@ UintType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +UintType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string UintType::as_string () const { @@ -1086,7 +1146,7 @@ UintType::unify (BaseType *other) } bool -UintType::can_eq (BaseType *other, bool emit_errors) +UintType::can_eq (const BaseType *other, bool emit_errors) const { UintCmp r (this, emit_errors); return r.can_eq (other); @@ -1115,6 +1175,12 @@ FloatType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +FloatType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string FloatType::as_string () const { @@ -1137,7 +1203,7 @@ FloatType::unify (BaseType *other) } bool -FloatType::can_eq (BaseType *other, bool emit_errors) +FloatType::can_eq (const BaseType *other, bool emit_errors) const { FloatCmp r (this, emit_errors); return r.can_eq (other); @@ -1166,6 +1232,12 @@ USizeType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +USizeType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string USizeType::as_string () const { @@ -1180,7 +1252,7 @@ USizeType::unify (BaseType *other) } bool -USizeType::can_eq (BaseType *other, bool emit_errors) +USizeType::can_eq (const BaseType *other, bool emit_errors) const { USizeCmp r (this, emit_errors); return r.can_eq (other); @@ -1198,6 +1270,12 @@ ISizeType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +ISizeType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string ISizeType::as_string () const { @@ -1212,7 +1290,7 @@ ISizeType::unify (BaseType *other) } bool -ISizeType::can_eq (BaseType *other, bool emit_errors) +ISizeType::can_eq (const BaseType *other, bool emit_errors) const { ISizeCmp r (this, emit_errors); return r.can_eq (other); @@ -1230,6 +1308,12 @@ CharType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +CharType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string CharType::as_string () const { @@ -1244,7 +1328,7 @@ CharType::unify (BaseType *other) } bool -CharType::can_eq (BaseType *other, bool emit_errors) +CharType::can_eq (const BaseType *other, bool emit_errors) const { CharCmp r (this, emit_errors); return r.can_eq (other); @@ -1262,6 +1346,12 @@ ReferenceType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +ReferenceType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string ReferenceType::as_string () const { @@ -1276,7 +1366,7 @@ ReferenceType::unify (BaseType *other) } bool -ReferenceType::can_eq (BaseType *other, bool emit_errors) +ReferenceType::can_eq (const BaseType *other, bool emit_errors) const { ReferenceCmp r (this, emit_errors); return r.can_eq (other); @@ -1327,6 +1417,12 @@ ParamType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +ParamType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string ParamType::as_string () const { @@ -1351,7 +1447,7 @@ ParamType::unify (BaseType *other) } bool -ParamType::can_eq (BaseType *other, bool emit_errors) +ParamType::can_eq (const BaseType *other, bool emit_errors) const { ParamCmp r (this, emit_errors); return r.can_eq (other); @@ -1437,6 +1533,12 @@ StrType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +StrType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string StrType::as_string () const { @@ -1451,7 +1553,7 @@ StrType::unify (BaseType *other) } bool -StrType::can_eq (BaseType *other, bool emit_errors) +StrType::can_eq (const BaseType *other, bool emit_errors) const { StrCmp r (this, emit_errors); return r.can_eq (other); @@ -1469,6 +1571,12 @@ NeverType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +NeverType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string NeverType::as_string () const { @@ -1483,7 +1591,7 @@ NeverType::unify (BaseType *other) } bool -NeverType::can_eq (BaseType *other, bool emit_errors) +NeverType::can_eq (const BaseType *other, bool emit_errors) const { NeverCmp r (this, emit_errors); return r.can_eq (other); @@ -1501,6 +1609,12 @@ PlaceholderType::accept_vis (TyVisitor &vis) vis.visit (*this); } +void +PlaceholderType::accept_vis (TyConstVisitor &vis) const +{ + vis.visit (*this); +} + std::string PlaceholderType::as_string () const { @@ -1515,7 +1629,7 @@ PlaceholderType::unify (BaseType *other) } bool -PlaceholderType::can_eq (BaseType *other, bool emit_errors) +PlaceholderType::can_eq (const BaseType *other, bool emit_errors) const { PlaceholderCmp r (this, emit_errors); return r.can_eq (other); diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index e2f1595..935b943 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -122,6 +122,7 @@ public: }; class TyVisitor; +class TyConstVisitor; class BaseType { public: @@ -140,9 +141,10 @@ public: void set_ty_ref (HirId id) { ty_ref = id; } - /* Visitor pattern for double dispatch. BaseRules implements TyVisitor. */ virtual void accept_vis (TyVisitor &vis) = 0; + virtual void accept_vis (TyConstVisitor &vis) const = 0; + virtual std::string as_string () const = 0; virtual std::string get_name () const = 0; @@ -155,7 +157,7 @@ public: // similar to unify but does not actually perform type unification but // determines whether they are compatible - virtual bool can_eq (BaseType *other, bool emit_errors) = 0; + virtual bool can_eq (const BaseType *other, bool emit_errors) const = 0; // Check value equality between two ty. Type inference rules are ignored. Two // ty are considered equal if they're of the same kind, and @@ -269,12 +271,13 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; @@ -302,13 +305,14 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; bool is_unit () const override { return true; } std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; @@ -332,11 +336,12 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; @@ -415,13 +420,14 @@ public: static TupleType *get_unit_type (HirId ref) { return new TupleType (ref); } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; bool is_unit () const override { return this->fields.empty (); } std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; bool is_equal (const BaseType &other) const override; @@ -849,11 +855,12 @@ public: bool is_unit () const override { return this->fields.empty (); } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; bool is_equal (const BaseType &other) const override; @@ -874,6 +881,11 @@ public: StructFieldType *get_field (size_t index) { return fields.at (index); } + const StructFieldType *get_imm_field (size_t index) const + { + return fields.at (index); + } + StructFieldType *get_field (const std::string &lookup, size_t *index = nullptr) const { @@ -959,6 +971,7 @@ public: } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; @@ -967,7 +980,7 @@ public: std::string get_identifier () const { return identifier; } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; bool is_equal (const BaseType &other) const override; @@ -1062,11 +1075,12 @@ public: BaseType *param_at (size_t idx) const { return params.at (idx).get_tyty (); } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; bool is_equal (const BaseType &other) const override; @@ -1102,13 +1116,14 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; bool is_equal (const BaseType &other) const override; @@ -1141,13 +1156,14 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; }; @@ -1174,13 +1190,14 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; IntKind get_int_kind () const { return int_kind; } @@ -1214,13 +1231,14 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; UintKind get_uint_kind () const { return uint_kind; } @@ -1252,13 +1270,14 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; FloatKind get_float_kind () const { return float_kind; } @@ -1292,13 +1311,14 @@ public: } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; }; @@ -1325,13 +1345,14 @@ public: } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; }; @@ -1358,13 +1379,14 @@ public: } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; }; @@ -1395,13 +1417,14 @@ public: BaseType *get_base () const; void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; std::string get_name () const override final { return as_string (); } BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; bool is_equal (const BaseType &other) const override; @@ -1442,11 +1465,12 @@ public: std::string get_name () const override final { return as_string (); } void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; bool is_equal (const BaseType &other) const override; @@ -1475,11 +1499,12 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; @@ -1503,11 +1528,12 @@ public: {} void accept_vis (TyVisitor &vis) override; + void accept_vis (TyConstVisitor &vis) const override; std::string as_string () const override; BaseType *unify (BaseType *other) override; - bool can_eq (BaseType *other, bool emit_errors) override; + bool can_eq (const BaseType *other, bool emit_errors) const override final; BaseType *clone () final override; diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index fb8070a..33ba8d0 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -260,6 +260,30 @@ Mappings::lookup_hir_item (CrateNum crateNum, HirId id) } void +Mappings::insert_hir_impl_block (CrateNum crateNum, HirId id, + HIR::ImplBlock *item) +{ + rust_assert (lookup_hir_impl_block (crateNum, id) == nullptr); + + hirImplBlockMappings[crateNum][id] = item; + nodeIdToHirMappings[crateNum][item->get_mappings ().get_nodeid ()] = id; +} + +HIR::ImplBlock * +Mappings::lookup_hir_impl_block (CrateNum crateNum, HirId id) +{ + auto it = hirImplBlockMappings.find (crateNum); + if (it == hirImplBlockMappings.end ()) + return nullptr; + + auto iy = it->second.find (id); + if (iy == it->second.end ()) + return nullptr; + + return iy->second; +} + +void Mappings::insert_hir_implitem (CrateNum crateNum, HirId id, HirId parent_impl_id, HIR::ImplItem *item) { diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 9b67928..98c15a2 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -128,6 +128,10 @@ public: void insert_hir_item (CrateNum crateNum, HirId id, HIR::Item *item); HIR::Item *lookup_hir_item (CrateNum crateNum, HirId id); + void insert_hir_impl_block (CrateNum crateNum, HirId id, + HIR::ImplBlock *item); + HIR::ImplBlock *lookup_hir_impl_block (CrateNum crateNum, HirId id); + void insert_hir_implitem (CrateNum crateNum, HirId id, HirId parent_impl_id, HIR::ImplItem *item); HIR::ImplItem *lookup_hir_implitem (CrateNum crateNum, HirId id, @@ -227,6 +231,7 @@ private: hirImplItemMappings; std::map<CrateNum, std::map<HirId, HIR::SelfParam *> > hirSelfParamMappings; std::map<HirId, HIR::ImplBlock *> hirImplItemsToImplMappings; + std::map<CrateNum, std::map<HirId, HIR::ImplBlock *> > hirImplBlockMappings; // location info std::map<CrateNum, std::map<NodeId, Location> > locations; |