aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-02-24 12:47:57 +0000
committerPhilip Herron <philip.herron@embecosm.com>2022-02-24 12:47:57 +0000
commit833c439a501a02c6c3c5b34f8e4c84706e8b62f2 (patch)
treea6f896c42117eb55ac7f14454170b94d811f9a7e /gcc/rust
parenta6dd242845303f44f38035189fd9360c7f572dfc (diff)
downloadgcc-833c439a501a02c6c3c5b34f8e4c84706e8b62f2.zip
gcc-833c439a501a02c6c3c5b34f8e4c84706e8b62f2.tar.gz
gcc-833c439a501a02c6c3c5b34f8e4c84706e8b62f2.tar.bz2
Add boilerplate for the new SliceType
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/backend/rust-compile-type.cc7
-rw-r--r--gcc/rust/backend/rust-compile-type.h1
-rw-r--r--gcc/rust/backend/rust-compile-tyty.h2
-rw-r--r--gcc/rust/typecheck/rust-substitution-mapper.h4
-rw-r--r--gcc/rust/typecheck/rust-tyty-call.h2
-rw-r--r--gcc/rust/typecheck/rust-tyty-cast.h53
-rw-r--r--gcc/rust/typecheck/rust-tyty-cmp.h61
-rw-r--r--gcc/rust/typecheck/rust-tyty-coercion.h54
-rw-r--r--gcc/rust/typecheck/rust-tyty-rules.h53
-rw-r--r--gcc/rust/typecheck/rust-tyty-visitor.h2
-rw-r--r--gcc/rust/typecheck/rust-tyty.cc76
-rw-r--r--gcc/rust/typecheck/rust-tyty.h45
12 files changed, 360 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile-type.cc b/gcc/rust/backend/rust-compile-type.cc
index 6de063b..21da9af 100644
--- a/gcc/rust/backend/rust-compile-type.cc
+++ b/gcc/rust/backend/rust-compile-type.cc
@@ -345,6 +345,13 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
}
void
+TyTyResolveCompile::visit (const TyTy::SliceType &type)
+{
+ // TODO
+ gcc_unreachable ();
+}
+
+void
TyTyResolveCompile::visit (const TyTy::BoolType &type)
{
tree compiled_type = nullptr;
diff --git a/gcc/rust/backend/rust-compile-type.h b/gcc/rust/backend/rust-compile-type.h
index 50598de..4f9c403 100644
--- a/gcc/rust/backend/rust-compile-type.h
+++ b/gcc/rust/backend/rust-compile-type.h
@@ -43,6 +43,7 @@ public:
void visit (const TyTy::FnType &) override;
void visit (const TyTy::FnPtr &) override;
void visit (const TyTy::ArrayType &) override;
+ void visit (const TyTy::SliceType &) override;
void visit (const TyTy::BoolType &) override;
void visit (const TyTy::IntType &) override;
void visit (const TyTy::UintType &) override;
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h
index 2e5a769..a3720f8 100644
--- a/gcc/rust/backend/rust-compile-tyty.h
+++ b/gcc/rust/backend/rust-compile-tyty.h
@@ -62,6 +62,8 @@ public:
void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
+ void visit (TyTy::SliceType &) override { gcc_unreachable (); }
+
void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
void visit (TyTy::PointerType &) override { gcc_unreachable (); }
diff --git a/gcc/rust/typecheck/rust-substitution-mapper.h b/gcc/rust/typecheck/rust-substitution-mapper.h
index 4ea4762..0d48bd2 100644
--- a/gcc/rust/typecheck/rust-substitution-mapper.h
+++ b/gcc/rust/typecheck/rust-substitution-mapper.h
@@ -124,6 +124,7 @@ public:
void visit (TyTy::TupleType &) override { gcc_unreachable (); }
void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
+ void visit (TyTy::SliceType &) override { gcc_unreachable (); }
void visit (TyTy::BoolType &) override { gcc_unreachable (); }
void visit (TyTy::IntType &) override { gcc_unreachable (); }
void visit (TyTy::UintType &) override { gcc_unreachable (); }
@@ -224,6 +225,7 @@ public:
void visit (TyTy::InferType &) override { gcc_unreachable (); }
void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
+ void visit (TyTy::SliceType &) override { gcc_unreachable (); }
void visit (TyTy::BoolType &) override { gcc_unreachable (); }
void visit (TyTy::IntType &) override { gcc_unreachable (); }
void visit (TyTy::UintType &) override { gcc_unreachable (); }
@@ -286,6 +288,7 @@ public:
void visit (TyTy::TupleType &) override { gcc_unreachable (); }
void visit (TyTy::FnPtr &) override { gcc_unreachable (); }
void visit (TyTy::ArrayType &) override { gcc_unreachable (); }
+ void visit (TyTy::SliceType &) override { gcc_unreachable (); }
void visit (TyTy::BoolType &) override { gcc_unreachable (); }
void visit (TyTy::IntType &) override { gcc_unreachable (); }
void visit (TyTy::UintType &) override { gcc_unreachable (); }
@@ -343,6 +346,7 @@ public:
void visit (const TyTy::TupleType &) override {}
void visit (const TyTy::FnPtr &) override {}
void visit (const TyTy::ArrayType &) override {}
+ void visit (const TyTy::SliceType &) override {}
void visit (const TyTy::BoolType &) override {}
void visit (const TyTy::IntType &) override {}
void visit (const TyTy::UintType &) override {}
diff --git a/gcc/rust/typecheck/rust-tyty-call.h b/gcc/rust/typecheck/rust-tyty-call.h
index b238d476f..51817e6 100644
--- a/gcc/rust/typecheck/rust-tyty-call.h
+++ b/gcc/rust/typecheck/rust-tyty-call.h
@@ -43,6 +43,7 @@ public:
void visit (InferType &) override { gcc_unreachable (); }
void visit (TupleType &) override { gcc_unreachable (); }
void visit (ArrayType &) override { gcc_unreachable (); }
+ void visit (SliceType &) override { gcc_unreachable (); }
void visit (BoolType &) override { gcc_unreachable (); }
void visit (IntType &) override { gcc_unreachable (); }
void visit (UintType &) override { gcc_unreachable (); }
@@ -99,6 +100,7 @@ public:
void visit (InferType &) override { gcc_unreachable (); }
void visit (TupleType &) override { gcc_unreachable (); }
void visit (ArrayType &) override { gcc_unreachable (); }
+ void visit (SliceType &) override { gcc_unreachable (); }
void visit (BoolType &) override { gcc_unreachable (); }
void visit (IntType &) override { gcc_unreachable (); }
void visit (UintType &) override { gcc_unreachable (); }
diff --git a/gcc/rust/typecheck/rust-tyty-cast.h b/gcc/rust/typecheck/rust-tyty-cast.h
index b9f4e14..0e0e7b0 100644
--- a/gcc/rust/typecheck/rust-tyty-cast.h
+++ b/gcc/rust/typecheck/rust-tyty-cast.h
@@ -153,6 +153,17 @@ public:
type.as_string ().c_str ());
}
+ virtual void visit (SliceType &type) override
+ {
+ Location ref_locus = mappings->lookup_location (type.get_ref ());
+ Location base_locus = mappings->lookup_location (get_base ()->get_ref ());
+ RichLocation r (ref_locus);
+ r.add_range (base_locus);
+ rust_error_at (r, "invalid cast [%s] to [%s]",
+ get_base ()->as_string ().c_str (),
+ type.as_string ().c_str ());
+ }
+
virtual void visit (BoolType &type) override
{
Location ref_locus = mappings->lookup_location (type.get_ref ());
@@ -468,6 +479,19 @@ public:
BaseCastRules::visit (type);
}
+ void visit (SliceType &type) override
+ {
+ bool is_valid
+ = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
+ if (is_valid)
+ {
+ resolved = type.clone ();
+ return;
+ }
+
+ BaseCastRules::visit (type);
+ }
+
void visit (ADTType &type) override
{
bool is_valid
@@ -818,6 +842,35 @@ private:
ArrayType *base;
};
+class SliceCastRules : public BaseCastRules
+{
+ using Rust::TyTy::BaseCastRules::visit;
+
+public:
+ SliceCastRules (SliceType *base) : BaseCastRules (base), base (base) {}
+
+ void visit (SliceType &type) override
+ {
+ // check base type
+ auto base_resolved
+ = base->get_element_type ()->unify (type.get_element_type ());
+ if (base_resolved == nullptr)
+ {
+ BaseCastRules::visit (type);
+ return;
+ }
+
+ resolved = new SliceType (type.get_ref (), type.get_ty_ref (),
+ type.get_ident ().locus,
+ TyVar (base_resolved->get_ref ()));
+ }
+
+private:
+ BaseType *get_base () override { return base; }
+
+ SliceType *base;
+};
+
class BoolCastRules : public BaseCastRules
{
using Rust::TyTy::BaseCastRules::visit;
diff --git a/gcc/rust/typecheck/rust-tyty-cmp.h b/gcc/rust/typecheck/rust-tyty-cmp.h
index bd568be..436bde9 100644
--- a/gcc/rust/typecheck/rust-tyty-cmp.h
+++ b/gcc/rust/typecheck/rust-tyty-cmp.h
@@ -155,6 +155,22 @@ public:
}
}
+ virtual void visit (const SliceType &type) override
+ {
+ ok = false;
+ if (emit_error_flag)
+ {
+ Location ref_locus = mappings->lookup_location (type.get_ref ());
+ Location base_locus
+ = mappings->lookup_location (get_base ()->get_ref ());
+ RichLocation r (ref_locus);
+ r.add_range (base_locus);
+ rust_error_at (r, "expected [%s] got [%s]",
+ get_base ()->as_string ().c_str (),
+ type.as_string ().c_str ());
+ }
+ }
+
virtual void visit (const BoolType &type) override
{
ok = false;
@@ -544,6 +560,19 @@ public:
BaseCmp::visit (type);
}
+ void visit (const SliceType &type) override
+ {
+ bool is_valid
+ = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
+ if (is_valid)
+ {
+ ok = true;
+ return;
+ }
+
+ BaseCmp::visit (type);
+ }
+
void visit (const ADTType &type) override
{
bool is_valid
@@ -864,6 +893,36 @@ private:
const ArrayType *base;
};
+class SliceCmp : public BaseCmp
+{
+ using Rust::TyTy::BaseCmp::visit;
+
+public:
+ SliceCmp (const SliceType *base, bool emit_errors)
+ : BaseCmp (base, emit_errors), base (base)
+ {}
+
+ void visit (const SliceType &type) override
+ {
+ // 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))
+ {
+ BaseCmp::visit (type);
+ return;
+ }
+
+ ok = true;
+ }
+
+ void visit (const ParamType &type) override { ok = true; }
+
+private:
+ const BaseType *get_base () const override { return base; }
+ const SliceType *base;
+};
+
class BoolCmp : public BaseCmp
{
using Rust::TyTy::BaseCmp::visit;
@@ -1258,6 +1317,8 @@ public:
void visit (const ArrayType &) override { ok = true; }
+ void visit (const SliceType &) override { ok = true; }
+
void visit (const BoolType &) override { ok = true; }
void visit (const IntType &) override { ok = true; }
diff --git a/gcc/rust/typecheck/rust-tyty-coercion.h b/gcc/rust/typecheck/rust-tyty-coercion.h
index 75913f0..4deed55 100644
--- a/gcc/rust/typecheck/rust-tyty-coercion.h
+++ b/gcc/rust/typecheck/rust-tyty-coercion.h
@@ -167,6 +167,17 @@ public:
type.as_string ().c_str ());
}
+ virtual void visit (SliceType &type) override
+ {
+ Location ref_locus = mappings->lookup_location (type.get_ref ());
+ Location base_locus = mappings->lookup_location (get_base ()->get_ref ());
+ RichLocation r (ref_locus);
+ r.add_range (base_locus);
+ rust_error_at (r, "expected [%s] got [%s]",
+ get_base ()->as_string ().c_str (),
+ type.as_string ().c_str ());
+ }
+
virtual void visit (BoolType &type) override
{
Location ref_locus = mappings->lookup_location (type.get_ref ());
@@ -483,6 +494,19 @@ public:
BaseCoercionRules::visit (type);
}
+ void visit (SliceType &type) override
+ {
+ bool is_valid
+ = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
+ if (is_valid)
+ {
+ resolved = type.clone ();
+ return;
+ }
+
+ BaseCoercionRules::visit (type);
+ }
+
void visit (ADTType &type) override
{
bool is_valid
@@ -836,6 +860,36 @@ private:
ArrayType *base;
};
+class SliceCoercionRules : public BaseCoercionRules
+{
+ using Rust::TyTy::BaseCoercionRules::visit;
+
+public:
+ SliceCoercionRules (SliceType *base) : BaseCoercionRules (base), base (base)
+ {}
+
+ void visit (SliceType &type) override
+ {
+ // check base type
+ auto base_resolved
+ = base->get_element_type ()->unify (type.get_element_type ());
+ if (base_resolved == nullptr)
+ {
+ BaseCoercionRules::visit (type);
+ return;
+ }
+
+ resolved = new SliceType (type.get_ref (), type.get_ty_ref (),
+ type.get_ident ().locus,
+ TyVar (base_resolved->get_ref ()));
+ }
+
+private:
+ BaseType *get_base () override { return base; }
+
+ SliceType *base;
+};
+
class BoolCoercionRules : public BaseCoercionRules
{
using Rust::TyTy::BaseCoercionRules::visit;
diff --git a/gcc/rust/typecheck/rust-tyty-rules.h b/gcc/rust/typecheck/rust-tyty-rules.h
index 906e33d..c1fc2cd 100644
--- a/gcc/rust/typecheck/rust-tyty-rules.h
+++ b/gcc/rust/typecheck/rust-tyty-rules.h
@@ -185,6 +185,17 @@ public:
type.as_string ().c_str ());
}
+ virtual void visit (SliceType &type) override
+ {
+ Location ref_locus = mappings->lookup_location (type.get_ref ());
+ Location base_locus = mappings->lookup_location (get_base ()->get_ref ());
+ RichLocation r (ref_locus);
+ r.add_range (base_locus);
+ rust_error_at (r, "expected [%s] got [%s]",
+ get_base ()->as_string ().c_str (),
+ type.as_string ().c_str ());
+ }
+
virtual void visit (BoolType &type) override
{
Location ref_locus = mappings->lookup_location (type.get_ref ());
@@ -500,6 +511,19 @@ public:
BaseRules::visit (type);
}
+ void visit (SliceType &type) override
+ {
+ bool is_valid
+ = (base->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL);
+ if (is_valid)
+ {
+ resolved = type.clone ();
+ return;
+ }
+
+ BaseRules::visit (type);
+ }
+
void visit (ADTType &type) override
{
bool is_valid
@@ -850,6 +874,35 @@ private:
ArrayType *base;
};
+class SliceRules : public BaseRules
+{
+ using Rust::TyTy::BaseRules::visit;
+
+public:
+ SliceRules (SliceType *base) : BaseRules (base), base (base) {}
+
+ void visit (SliceType &type) override
+ {
+ // check base type
+ auto base_resolved
+ = base->get_element_type ()->unify (type.get_element_type ());
+ if (base_resolved == nullptr)
+ {
+ BaseRules::visit (type);
+ return;
+ }
+
+ resolved = new SliceType (type.get_ref (), type.get_ty_ref (),
+ type.get_ident ().locus,
+ TyVar (base_resolved->get_ref ()));
+ }
+
+private:
+ BaseType *get_base () override { return base; }
+
+ SliceType *base;
+};
+
class BoolRules : public BaseRules
{
using Rust::TyTy::BaseRules::visit;
diff --git a/gcc/rust/typecheck/rust-tyty-visitor.h b/gcc/rust/typecheck/rust-tyty-visitor.h
index fa01a71..464e70d 100644
--- a/gcc/rust/typecheck/rust-tyty-visitor.h
+++ b/gcc/rust/typecheck/rust-tyty-visitor.h
@@ -33,6 +33,7 @@ public:
virtual void visit (FnType &type) = 0;
virtual void visit (FnPtr &type) = 0;
virtual void visit (ArrayType &type) = 0;
+ virtual void visit (SliceType &type) = 0;
virtual void visit (BoolType &type) = 0;
virtual void visit (IntType &type) = 0;
virtual void visit (UintType &type) = 0;
@@ -61,6 +62,7 @@ public:
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 SliceType &type) = 0;
virtual void visit (const BoolType &type) = 0;
virtual void visit (const IntType &type) = 0;
virtual void visit (const UintType &type) = 0;
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index a58e1cd..fa5dcfb 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -59,6 +59,9 @@ TypeKindFormat::to_string (TypeKind kind)
case TypeKind::ARRAY:
return "ARRAY";
+ case TypeKind::SLICE:
+ return "SLICE";
+
case TypeKind::FNDEF:
return "FnDef";
@@ -1503,6 +1506,79 @@ ArrayType::clone () const
}
void
+SliceType::accept_vis (TyVisitor &vis)
+{
+ vis.visit (*this);
+}
+
+void
+SliceType::accept_vis (TyConstVisitor &vis) const
+{
+ vis.visit (*this);
+}
+
+std::string
+SliceType::as_string () const
+{
+ return "[" + get_element_type ()->as_string () + "]";
+}
+
+BaseType *
+SliceType::unify (BaseType *other)
+{
+ SliceRules r (this);
+ return r.unify (other);
+}
+
+BaseType *
+SliceType::coerce (BaseType *other)
+{
+ SliceCoercionRules r (this);
+ return r.coerce (other);
+}
+
+BaseType *
+SliceType::cast (BaseType *other)
+{
+ SliceCastRules r (this);
+ return r.cast (other);
+}
+
+bool
+SliceType::can_eq (const BaseType *other, bool emit_errors) const
+{
+ SliceCmp r (this, emit_errors);
+ return r.can_eq (other);
+}
+
+bool
+SliceType::is_equal (const BaseType &other) const
+{
+ if (get_kind () != other.get_kind ())
+ return false;
+
+ auto other2 = static_cast<const SliceType &> (other);
+
+ 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 *
+SliceType::get_element_type () const
+{
+ return element_type.get_tyty ();
+}
+
+BaseType *
+SliceType::clone () const
+{
+ return new SliceType (get_ref (), get_ty_ref (), ident.locus, element_type,
+ get_combined_refs ());
+}
+
+void
BoolType::accept_vis (TyVisitor &vis)
{
vis.visit (*this);
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index 036d772..85948b2 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -47,6 +47,7 @@ enum TypeKind
POINTER,
PARAM,
ARRAY,
+ SLICE,
FNDEF,
FNPTR,
TUPLE,
@@ -1666,6 +1667,50 @@ private:
HIR::Expr &capacity_expr;
};
+class SliceType : public BaseType
+{
+public:
+ SliceType (HirId ref, Location locus, TyVar base,
+ std::set<HirId> refs = std::set<HirId> ())
+ : BaseType (ref, ref, TypeKind::SLICE,
+ {Resolver::CanonicalPath::create_empty (), locus}, refs),
+ element_type (base)
+ {}
+
+ SliceType (HirId ref, HirId ty_ref, Location locus, TyVar base,
+ std::set<HirId> refs = std::set<HirId> ())
+ : BaseType (ref, ty_ref, TypeKind::SLICE,
+ {Resolver::CanonicalPath::create_empty (), locus}, refs),
+ element_type (base)
+ {}
+
+ 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 (const BaseType *other, bool emit_errors) const override final;
+ BaseType *coerce (BaseType *other) override;
+ BaseType *cast (BaseType *other) override;
+
+ bool is_equal (const BaseType &other) const override;
+
+ BaseType *get_element_type () const;
+
+ BaseType *clone () const final override;
+
+ bool is_concrete () const final override
+ {
+ return get_element_type ()->is_concrete ();
+ }
+
+private:
+ TyVar element_type;
+};
+
class BoolType : public BaseType
{
public: