diff options
author | Philip Herron <herron.philip@googlemail.com> | 2023-04-18 12:36:29 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 18:34:13 +0100 |
commit | b18533466c665d1beda7b2564cef515cf3e57aed (patch) | |
tree | 47a7f4dc53395bf3f52cac2e9b32140835a93769 | |
parent | 0c6338ddb64b8a3fc24470b3040e09ade2f772d8 (diff) | |
download | gcc-b18533466c665d1beda7b2564cef515cf3e57aed.zip gcc-b18533466c665d1beda7b2564cef515cf3e57aed.tar.gz gcc-b18533466c665d1beda7b2564cef515cf3e57aed.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>
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check.h | 10 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-typecheck-context.cc | 72 |
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 6fa75ec..3e478f2 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 5d05021..51ea87c 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; |