aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-tyty.cc118
-rw-r--r--gcc/rust/typecheck/rust-tyty.h41
2 files changed, 154 insertions, 5 deletions
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index a3a2ffe..75fb827 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -130,6 +130,20 @@ StructFieldType::unify (BaseType *other)
return r.unify (other);
}
+bool
+StructFieldType::equals (const BaseType &other) const
+{
+ if (get_kind () != other.get_kind ())
+ {
+ return false;
+ }
+ else
+ {
+ auto other2 = static_cast<const StructFieldType &> (other);
+ return get_field_type () == other2.get_field_type ();
+ }
+}
+
BaseType *
StructFieldType::clone ()
{
@@ -165,6 +179,31 @@ ADTType::unify (BaseType *other)
return r.unify (other);
}
+bool
+ADTType::equals (const BaseType &other) const
+{
+ if (get_kind () != other.get_kind ())
+ {
+ return false;
+ }
+ else
+ {
+ auto other2 = static_cast<const ADTType &> (other);
+ if (num_fields () != other2.num_fields ())
+ {
+ return false;
+ }
+ for (int i = 0; i < num_fields (); i++)
+ {
+ if (!get_field (i)->equals (*other2.get_field (i)))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+}
+
BaseType *
ADTType::clone ()
{
@@ -211,6 +250,31 @@ TupleType::unify (BaseType *other)
return r.unify (other);
}
+bool
+TupleType::equals (const BaseType &other) const
+{
+ if (get_kind () != other.get_kind ())
+ {
+ return false;
+ }
+ else
+ {
+ auto other2 = static_cast<const TupleType &> (other);
+ if (num_fields () != other2.num_fields ())
+ {
+ return false;
+ }
+ for (int i = 0; i < num_fields (); i++)
+ {
+ if (!get_field (i)->equals (*other2.get_field (i)))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+}
+
BaseType *
TupleType::clone ()
{
@@ -247,6 +311,31 @@ FnType::unify (BaseType *other)
return r.unify (other);
}
+bool
+FnType::equals (const BaseType &other) const
+{
+ if (get_kind () != other.get_kind ())
+ {
+ return false;
+ }
+ else
+ {
+ auto other2 = static_cast<const FnType &> (other);
+ if (!get_return_type ()->equals (*other2.get_return_type ()))
+ return false;
+ if (num_params () != other2.num_params ())
+ return false;
+ for (int i = 0; i < num_params (); i++)
+ {
+ auto lhs = param_at (i).second;
+ auto rhs = other2.param_at (i).second;
+ if (!lhs->equals (*rhs))
+ return false;
+ }
+ return true;
+ }
+}
+
BaseType *
FnType::clone ()
{
@@ -279,6 +368,21 @@ ArrayType::unify (BaseType *other)
return r.unify (other);
}
+bool
+ArrayType::equals (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 ();
+ }
+}
+
BaseType *
ArrayType::get_type () const
{
@@ -529,6 +633,20 @@ ReferenceType::unify (BaseType *other)
return r.unify (other);
}
+bool
+ReferenceType::equals (const BaseType &other) const
+{
+ if (get_kind () != other.get_kind ())
+ {
+ return false;
+ }
+ else
+ {
+ auto other2 = static_cast<const ReferenceType &> (other);
+ return get_base () == other2.get_base ();
+ }
+}
+
const BaseType *
ReferenceType::get_base () const
{
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index 4b0ac86..2be807b 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -73,6 +73,15 @@ public:
releasing the memory of the returned ty. */
virtual BaseType *unify (BaseType *other) = 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
+ 1. (For ADTs, arrays, tuples, refs) have the same underlying ty
+ 2. (For functions) have the same signature */
+ virtual bool equals (const BaseType &other) const
+ {
+ return get_kind () == other.get_kind ();
+ }
+
virtual bool is_unit () const { return kind == TypeKind::UNIT; }
TypeKind get_kind () const { return kind; }
@@ -199,9 +208,11 @@ public:
BaseType *unify (BaseType *other) override;
+ virtual bool equals (const BaseType &other) const override;
+
std::string get_name () const { return name; }
- BaseType *get_field_type () { return ty; }
+ BaseType *get_field_type () const { return ty; }
BaseType *clone () final override;
@@ -231,6 +242,8 @@ public:
BaseType *unify (BaseType *other) override;
+ virtual bool equals (const BaseType &other) const override;
+
size_t num_fields () const { return fields.size (); }
BaseType *get_field (size_t index) const;
@@ -275,14 +288,16 @@ public:
BaseType *unify (BaseType *other) override;
+ virtual bool equals (const BaseType &other) const override;
+
size_t num_fields () const { return fields.size (); }
std::string get_name () const { return identifier; }
- StructFieldType *get_field (size_t index) { return fields.at (index); }
+ StructFieldType *get_field (size_t index) const { return fields.at (index); }
StructFieldType *get_field (const std::string &lookup,
- size_t *index = nullptr)
+ size_t *index = nullptr) const
{
size_t i = 0;
for (auto &field : fields)
@@ -341,6 +356,8 @@ public:
BaseType *unify (BaseType *other) override;
+ virtual bool equals (const BaseType &other) const override;
+
size_t num_params () const { return params.size (); }
std::vector<std::pair<HIR::Pattern *, BaseType *> > &get_params ()
@@ -348,12 +365,22 @@ public:
return params;
}
+ const std::vector<std::pair<HIR::Pattern *, BaseType *> > &get_params () const
+ {
+ return params;
+ }
+
std::pair<HIR::Pattern *, BaseType *> &param_at (size_t idx)
{
- return params[idx];
+ return params.at (idx);
+ }
+
+ const std::pair<HIR::Pattern *, BaseType *> &param_at (size_t idx) const
+ {
+ return params.at (idx);
}
- BaseType *get_return_type () { return type; }
+ BaseType *get_return_type () const { return type; }
BaseType *clone () final override;
@@ -383,6 +410,8 @@ public:
BaseType *unify (BaseType *other) override;
+ virtual bool equals (const BaseType &other) const override;
+
size_t get_capacity () const { return capacity; }
HirId element_type_ref () const { return element_type_id; }
@@ -603,6 +632,8 @@ public:
BaseType *unify (BaseType *other) override;
+ virtual bool equals (const BaseType &other) const override;
+
BaseType *clone () final override;
private: