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: Compile range patterns Date: Wed, 8 Jun 2022 12:47:21 +0000 (GMT) [thread overview] Message-ID: <20220608124721.E9C33380E19E@sourceware.org> (raw) https://gcc.gnu.org/g:b1eb3e036c6fb5acc28c714375e6309fb5aa9ec8 commit b1eb3e036c6fb5acc28c714375e6309fb5aa9ec8 Author: David Faust <david.faust@oracle.com> Date: Thu May 12 15:48:18 2022 -0700 backend: Compile range patterns Diff: --- gcc/rust/backend/rust-compile-pattern.cc | 58 ++++++++++++++++++++++ gcc/rust/backend/rust-compile-pattern.h | 2 +- gcc/testsuite/rust/execute/torture/match_range1.rs | 34 +++++++++++++ gcc/testsuite/rust/execute/torture/match_range2.rs | 40 +++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index 9c1a35a5b91..aefa4eb08bb 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -105,6 +105,64 @@ CompilePatternCaseLabelExpr::visit (HIR::LiteralPattern &pattern) case_label_expr = build_case_label (lit, NULL_TREE, associated_case_label); } +static tree +compile_range_pattern_bound (HIR::RangePatternBound *bound, + Analysis::NodeMapping mappings, Location locus, + Context *ctx) +{ + tree result = NULL_TREE; + switch (bound->get_bound_type ()) + { + case HIR::RangePatternBound::RangePatternBoundType::LITERAL: { + HIR::RangePatternBoundLiteral &ref + = *static_cast<HIR::RangePatternBoundLiteral *> (bound); + + HIR::LiteralExpr *litexpr + = new HIR::LiteralExpr (mappings, ref.get_literal (), locus, + std::vector<AST::Attribute> ()); + + result = CompileExpr::Compile (litexpr, ctx); + } + break; + + case HIR::RangePatternBound::RangePatternBoundType::PATH: { + HIR::RangePatternBoundPath &ref + = *static_cast<HIR::RangePatternBoundPath *> (bound); + + result = ResolvePathRef::Compile (ref.get_path (), ctx); + + // If the path resolves to a const expression, fold it. + result = ConstCtx::fold (result); + } + break; + + case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: { + HIR::RangePatternBoundQualPath &ref + = *static_cast<HIR::RangePatternBoundQualPath *> (bound); + + result = ResolvePathRef::Compile (ref.get_qualified_path (), ctx); + + // If the path resolves to a const expression, fold it. + result = ConstCtx::fold (result); + } + } + + return result; +} + +void +CompilePatternCaseLabelExpr::visit (HIR::RangePattern &pattern) +{ + tree upper = compile_range_pattern_bound (pattern.get_upper_bound ().get (), + pattern.get_pattern_mappings (), + pattern.get_locus (), ctx); + tree lower = compile_range_pattern_bound (pattern.get_lower_bound ().get (), + pattern.get_pattern_mappings (), + pattern.get_locus (), ctx); + + case_label_expr = build_case_label (lower, upper, associated_case_label); +} + // setup the bindings void diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 1b6e80fb88d..0eb5d61249b 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -37,13 +37,13 @@ public: void visit (HIR::StructPattern &pattern) override; void visit (HIR::TupleStructPattern &pattern) override; void visit (HIR::WildcardPattern &pattern) override; + void visit (HIR::RangePattern &pattern) override; // Empty visit for unused Pattern HIR nodes. void visit (HIR::GroupedPattern &) override {} void visit (HIR::IdentifierPattern &) override {} void visit (HIR::LiteralPattern &) override; void visit (HIR::QualifiedPathInExpression &) override {} - void visit (HIR::RangePattern &) override {} void visit (HIR::ReferencePattern &) override {} void visit (HIR::SlicePattern &) override {} void visit (HIR::TuplePattern &) override {} diff --git a/gcc/testsuite/rust/execute/torture/match_range1.rs b/gcc/testsuite/rust/execute/torture/match_range1.rs new file mode 100644 index 00000000000..8fe8f4c766f --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/match_range1.rs @@ -0,0 +1,34 @@ +// { dg-output "zero to END_RANGE\nzero to END_RANGE\nelse\n" } + +extern "C" { + fn printf(s: *const i8, ...); +} + +const END_RANGE: i32 = 15; + +fn foo (x: i32) { + match x { + 0..=END_RANGE => { + let a = "zero to END_RANGE\n\0"; + let b = a as *const str; + let c = b as *const i8; + printf (c); + } + + _ => { + let a = "else\n\0"; + let b = a as *const str; + let c = b as *const i8; + printf (c); + } + } +} + +fn main () -> i32 { + + foo (11); + foo (15); + foo (21); + + 0 +} diff --git a/gcc/testsuite/rust/execute/torture/match_range2.rs b/gcc/testsuite/rust/execute/torture/match_range2.rs new file mode 100644 index 00000000000..82980c2f0c5 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/match_range2.rs @@ -0,0 +1,40 @@ +// { dg-output "lowercase\nuppercase\nother\n" } + +extern "C" { + fn printf(s: *const i8, ...); +} + +const BIG_A: char = 'A'; +const BIG_Z: char = 'Z'; + +fn bar (x: char) { + match x { + + 'a'..='z' => { + let a = "lowercase\n\0"; + let b = a as *const str; + let c = b as *const i8; + printf (c); + } + BIG_A..=BIG_Z => { + let a = "uppercase\n\0"; + let b = a as *const str; + let c = b as *const i8; + printf (c); + } + _ => { + let a = "other\n\0"; + let b = a as *const str; + let c = b as *const i8; + printf (c); + } + } +} + +fn main () -> i32 { + bar ('b'); + bar ('X'); + bar ('!'); + + 0 +}
reply other threads:[~2022-06-08 12:47 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=20220608124721.E9C33380E19E@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).