public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] backend: factor out check_match_scrutinee Date: Fri, 8 Jul 2022 07:31:23 +0000 (GMT) [thread overview] Message-ID: <20220708073123.84E8B3857350@sourceware.org> (raw) https://gcc.gnu.org/g:7784232540214b0fa500fa5682326f8841f170d5 commit 7784232540214b0fa500fa5682326f8841f170d5 Author: David Faust <david.faust@oracle.com> Date: Thu Jul 7 11:25:52 2022 -0700 backend: factor out check_match_scrutinee This commit pulls some of the validation of the match expression scrutinee out into a helper, since it will be needed to be reused when compiling matches on tuples. Diff: --- gcc/rust/backend/rust-compile-expr.cc | 75 +++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index d12dc78cf41..02015b1a7c2 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -181,6 +181,54 @@ CompileExpr::visit (HIR::DereferenceExpr &expr) known_valid, expr.get_locus ()); } + +// Helper for CompileExpr::visit (HIR::MatchExpr). +// Check that the scrutinee of EXPR is a valid kind of expression to match on. +// Return the TypeKind of the scrutinee if it is valid, or TyTy::TypeKind::ERROR +// if not. +static TyTy::TypeKind +check_match_scrutinee (HIR::MatchExpr &expr, Context *ctx) +{ + TyTy::BaseType *scrutinee_expr_tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + expr.get_scrutinee_expr ()->get_mappings ().get_hirid (), + &scrutinee_expr_tyty)) + { + return TyTy::TypeKind::ERROR; + } + + TyTy::TypeKind scrutinee_kind = scrutinee_expr_tyty->get_kind (); + rust_assert ((TyTy::is_primitive_type_kind (scrutinee_kind) + && scrutinee_kind != TyTy::TypeKind::NEVER) + || scrutinee_kind == TyTy::TypeKind::ADT + || scrutinee_kind == TyTy::TypeKind::TUPLE); + + if (scrutinee_kind == TyTy::TypeKind::ADT) + { + // this will need to change but for now the first pass implementation, + // lets assert this is the case + TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_expr_tyty); + rust_assert (adt->is_enum ()); + rust_assert (adt->number_of_variants () > 0); + } + else if (scrutinee_kind == TyTy::TypeKind::FLOAT) + { + // FIXME: CASE_LABEL_EXPR does not support floating point types. + // Find another way to compile these. + rust_sorry_at (expr.get_locus (), + "match on floating-point types is not yet supported"); + } + + TyTy::BaseType *expr_tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), + &expr_tyty)) + { + return TyTy::TypeKind::ERROR; + } + + return scrutinee_kind; +} + void CompileExpr::visit (HIR::MatchExpr &expr) { @@ -213,36 +261,13 @@ CompileExpr::visit (HIR::MatchExpr &expr) the control flow graph. */ // DEFTREECODE (CASE_LABEL_EXPR, "case_label_expr", tcc_statement, 4) - TyTy::BaseType *scrutinee_expr_tyty = nullptr; - if (!ctx->get_tyctx ()->lookup_type ( - expr.get_scrutinee_expr ()->get_mappings ().get_hirid (), - &scrutinee_expr_tyty)) + TyTy::TypeKind scrutinee_kind = check_match_scrutinee (expr, ctx); + if (scrutinee_kind == TyTy::TypeKind::ERROR) { translated = error_mark_node; return; } - TyTy::TypeKind scrutinee_kind = scrutinee_expr_tyty->get_kind (); - rust_assert ((TyTy::is_primitive_type_kind (scrutinee_kind) - && scrutinee_kind != TyTy::TypeKind::NEVER) - || scrutinee_kind == TyTy::TypeKind::ADT); - - if (scrutinee_kind == TyTy::TypeKind::ADT) - { - // this will need to change but for now the first pass implementation, - // lets assert this is the case - TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_expr_tyty); - rust_assert (adt->is_enum ()); - rust_assert (adt->number_of_variants () > 0); - } - else if (scrutinee_kind == TyTy::TypeKind::FLOAT) - { - // FIXME: CASE_LABEL_EXPR does not support floating point types. - // Find another way to compile these. - rust_sorry_at (expr.get_locus (), - "match on floating-point types is not yet supported"); - } - TyTy::BaseType *expr_tyty = nullptr; if (!ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &expr_tyty))
reply other threads:[~2022-07-08 7:31 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220708073123.84E8B3857350@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).