aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPhilip Herron <herron.philip@googlemail.com>2023-04-18 12:36:29 +0100
committerPhilip Herron <philip.herron@embecosm.com>2023-04-26 15:02:40 +0000
commit198f98ec04c9f09f956fee36cfb93957e06fbe7d (patch)
treed8505260d926896c5615f064a28a3d843edb082c /gcc
parentc630493edcd2ccbef51a5893796f5deecd9d8a0b (diff)
downloadgcc-198f98ec04c9f09f956fee36cfb93957e06fbe7d.zip
gcc-198f98ec04c9f09f956fee36cfb93957e06fbe7d.tar.gz
gcc-198f98ec04c9f09f956fee36cfb93957e06fbe7d.tar.bz2
gccrs: add error state to TypeCheckContextItem and missing copy ctor's
When checking current context we might be in the const or static context which does not have a current function or impl or trait context associated with it. So this allows us to represent the an error state for that case. gcc/rust/ChangeLog: * typecheck/rust-hir-type-check.h: New error state and missing copy implementations * typecheck/rust-typecheck-context.cc (TypeCheckContextItem::TypeCheckContextItem): missing copy ctor (TypeCheckContextItem::operator=): missing copy implementation (TypeCheckContextItem::get_error): new static function (TypeCheckContextItem::is_error): new method (TypeCheckContextItem::get_context_type): handle error state (TypeCheckContextItem::get_defid): handle error state Signed-off-by: Philip Herron <herron.philip@googlemail.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check.h10
-rw-r--r--gcc/rust/typecheck/rust-typecheck-context.cc72
2 files changed, 82 insertions, 0 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h b/gcc/rust/typecheck/rust-hir-type-check.h
index 74ab6c3..4f8364d 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -35,11 +35,19 @@ public:
ITEM,
IMPL_ITEM,
TRAIT_ITEM,
+ ERROR
};
TypeCheckContextItem (HIR::Function *item);
TypeCheckContextItem (HIR::ImplBlock *impl_block, HIR::Function *item);
TypeCheckContextItem (HIR::TraitItemFunc *trait_item);
+ TypeCheckContextItem (const TypeCheckContextItem &other);
+
+ TypeCheckContextItem &operator= (const TypeCheckContextItem &other);
+
+ static TypeCheckContextItem get_error ();
+
+ bool is_error () const;
ItemType get_type () const;
@@ -54,6 +62,8 @@ public:
DefId get_defid () const;
private:
+ TypeCheckContextItem ();
+
union Item
{
HIR::Function *item;
diff --git a/gcc/rust/typecheck/rust-typecheck-context.cc b/gcc/rust/typecheck/rust-typecheck-context.cc
index ffa49dc..2e3a628 100644
--- a/gcc/rust/typecheck/rust-typecheck-context.cc
+++ b/gcc/rust/typecheck/rust-typecheck-context.cc
@@ -525,6 +525,71 @@ TypeCheckContextItem::TypeCheckContextItem (HIR::TraitItemFunc *trait_item)
: type (ItemType::TRAIT_ITEM), item (trait_item)
{}
+TypeCheckContextItem::TypeCheckContextItem (const TypeCheckContextItem &other)
+ : type (other.type), item (other.item)
+{
+ switch (other.type)
+ {
+ case ITEM:
+ item.item = other.item.item;
+ break;
+
+ case IMPL_ITEM:
+ item.impl_item = other.item.impl_item;
+ break;
+
+ case TRAIT_ITEM:
+ item.trait_item = other.item.trait_item;
+ break;
+
+ case ERROR:
+ item.item = nullptr;
+ break;
+ }
+}
+
+TypeCheckContextItem::TypeCheckContextItem ()
+ : type (ItemType::ERROR), item (static_cast<HIR::Function *> (nullptr))
+{}
+
+TypeCheckContextItem &
+TypeCheckContextItem::operator= (const TypeCheckContextItem &other)
+{
+ type = other.type;
+ switch (other.type)
+ {
+ case ITEM:
+ item.item = other.item.item;
+ break;
+
+ case IMPL_ITEM:
+ item.impl_item = other.item.impl_item;
+ break;
+
+ case TRAIT_ITEM:
+ item.trait_item = other.item.trait_item;
+ break;
+
+ case ERROR:
+ item.item = nullptr;
+ break;
+ }
+
+ return *this;
+}
+
+TypeCheckContextItem
+TypeCheckContextItem::get_error ()
+{
+ return TypeCheckContextItem ();
+}
+
+bool
+TypeCheckContextItem::is_error () const
+{
+ return type == ERROR;
+}
+
HIR::Function *
TypeCheckContextItem::get_item ()
{
@@ -571,6 +636,10 @@ TypeCheckContextItem::get_context_type ()
case TRAIT_ITEM:
reference = get_trait_item ()->get_mappings ().get_hirid ();
break;
+
+ case ERROR:
+ gcc_unreachable ();
+ return nullptr;
}
rust_assert (reference != UNKNOWN_HIRID);
@@ -595,6 +664,9 @@ TypeCheckContextItem::get_defid () const
case TRAIT_ITEM:
return item.trait_item->get_mappings ().get_defid ();
+
+ case ERROR:
+ return UNKNOWN_DEFID;
}
return UNKNOWN_DEFID;