From: Jakub Dupak Date: Mon, 11 Dec 2023 22:11:34 +0000 (+0100) Subject: gccrs: TyTy: SubstitutionRef cast specialization X-Git-Tag: basepoints/gcc-15~1538 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=75e6d0cf415c939b020cef2e2693b652848ae45d;p=thirdparty%2Fgcc.git gccrs: TyTy: SubstitutionRef cast specialization Allows skipping parent check when casting. gcc/rust/ChangeLog: * typecheck/rust-tyty.h (BaseType::is): Cast API. (SubstitutionRef>): Cast API. (BaseType::as): Cast API. (BaseType::try_as): Cast API. Signed-off-by: Jakub Dupak --- diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index 6ce760df66e6..2ed407ee169b 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -1575,6 +1575,84 @@ BaseType::is () const return this->is (); } +template <> +WARN_UNUSED_RESULT inline bool +BaseType::is () const +{ + auto kind = this->get_kind (); + return kind == FNPTR || kind == FNDEF || kind == CLOSURE || kind == ADT + || kind == PROJECTION; +} + +template <> +WARN_UNUSED_RESULT inline bool +BaseType::is () const +{ + return this->is (); +} + +template <> +WARN_UNUSED_RESULT inline SubstitutionRef * +BaseType::as () +{ + auto kind = this->get_kind (); + switch (kind) + { + case FNDEF: + return static_cast (this); + case CLOSURE: + return static_cast (this); + case ADT: + return static_cast (this); + case PROJECTION: + return static_cast (this); + default: + rust_unreachable (); + } +} + +template <> +WARN_UNUSED_RESULT inline const SubstitutionRef * +BaseType::as () const +{ + auto kind = this->get_kind (); + switch (kind) + { + case FNDEF: + return static_cast (this); + case CLOSURE: + return static_cast (this); + case ADT: + return static_cast (this); + case PROJECTION: + return static_cast (this); + default: + rust_unreachable (); + } +} + +template <> +WARN_UNUSED_RESULT inline SubstitutionRef * +BaseType::try_as () +{ + if (this->is ()) + { + return this->as (); + } + return nullptr; +} + +template <> +WARN_UNUSED_RESULT inline const SubstitutionRef * +BaseType::try_as () const +{ + if (this->is ()) + { + return this->as (); + } + return nullptr; +} + } // namespace TyTy } // namespace Rust