aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-pattern.cc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-05-13 15:45:24 +0000
committerGitHub <noreply@github.com>2022-05-13 15:45:24 +0000
commite4213b9568ae8cb8a4e31326e0e78c79db0a99cc (patch)
tree95fa5b04afbe6b66a3faece7399782c8305dab11 /gcc/rust/backend/rust-compile-pattern.cc
parentd4434b511a4e650e95c7a1de6810d8748c5d8a70 (diff)
parentb1eb3e036c6fb5acc28c714375e6309fb5aa9ec8 (diff)
downloadgcc-e4213b9568ae8cb8a4e31326e0e78c79db0a99cc.zip
gcc-e4213b9568ae8cb8a4e31326e0e78c79db0a99cc.tar.gz
gcc-e4213b9568ae8cb8a4e31326e0e78c79db0a99cc.tar.bz2
Merge #1248
1248: Support RangePatterns in matches r=dafaust a=dafaust Add name resolution, lowering, type checking and compilation for `RangePattern`s in matches. Co-authored-by: David Faust <david.faust@oracle.com>
Diffstat (limited to 'gcc/rust/backend/rust-compile-pattern.cc')
-rw-r--r--gcc/rust/backend/rust-compile-pattern.cc58
1 files changed, 58 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc
index 9c1a35a..aefa4eb 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