public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add AltPattern HIR node
@ 2023-03-08 9:56 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-03-08 9:56 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:94cbaa29a8afe6ea1dfe5ab04dad0d87a05ce89e
commit 94cbaa29a8afe6ea1dfe5ab04dad0d87a05ce89e
Author: Owen Avery <powerboat9.gamer@gmail.com>
Date: Mon Feb 20 14:46:03 2023 -0500
Add AltPattern HIR node
Example:
match x {
0 | 1 => true,
_ => false
}
gcc/rust/ChangeLog:
* backend/rust-compile-fnparam.h:
(CompileFnParam::visit): Add AltPattern visitor.
* backend/rust-compile-pattern.h:
(CompilePatternCaseLabelExpr::visit): Add AltPattern visitor.
(CompilePatternBindings::visit): Add AltPattern visitor.
(CompilePatternLet::visit): Add AltPattern visitor.
* backend/rust-compile-resolve-path.h:
(ResolvePathRef::visit): Add AltPattern visitor.
* backend/rust-compile-var-decl.h:
(CompileVarDecl::visit): Add AltPattern visitor.
* checks/errors/rust-const-checker.cc
(ConstChecker::visit): Add AltPattern visitor.
* checks/errors/rust-const-checker.h:
(ConstChecker::visit): Add AltPattern visitor.
* checks/errors/rust-unsafe-checker.cc
(UnsafeChecker::visit): Add AltPattern visitor.
* checks/errors/rust-unsafe-checker.h:
(UnsafeChecker::visit): Add AltPattern visitor.
* hir/rust-hir-dump.cc
(Dump::visit): Add AltPattern visitor.
* hir/rust-hir-dump.h:
(Dump::visit): Add AltPattern visitor.
* hir/tree/rust-hir-full-decls.h
(class AltPattern): Add forward declaration.
* hir/tree/rust-hir-pattern.h
(class AltPattern): New class.
* hir/tree/rust-hir-visitor.h:
(HIRFullVisitor::visit): Add AltPattern visitor.
(HIRFullVisitorBase::visit): Add AltPattern visitor.
(HIRPatternVisitor::visit): Add AltPattern visitor.
* hir/tree/rust-hir.h:
(Pattern::PatternType::ALT): New enumerator.
* typecheck/rust-hir-type-check-pattern.cc
(TypeCheckPattern::visit): Add AltPattern visitor.
* typecheck/rust-hir-type-check-pattern.h:
(TypeCheckPattern::visit): Add AltPattern visitor.
Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diff:
---
gcc/rust/backend/rust-compile-fnparam.h | 1 +
gcc/rust/backend/rust-compile-pattern.h | 20 +++++++
gcc/rust/backend/rust-compile-resolve-path.h | 1 +
gcc/rust/backend/rust-compile-var-decl.h | 1 +
gcc/rust/checks/errors/rust-const-checker.cc | 4 ++
gcc/rust/checks/errors/rust-const-checker.h | 1 +
gcc/rust/checks/errors/rust-unsafe-checker.cc | 4 ++
gcc/rust/checks/errors/rust-unsafe-checker.h | 1 +
gcc/rust/hir/rust-hir-dump.cc | 3 +
gcc/rust/hir/rust-hir-dump.h | 1 +
gcc/rust/hir/tree/rust-hir-full-decls.h | 1 +
gcc/rust/hir/tree/rust-hir-pattern.h | 67 +++++++++++++++++++++++
gcc/rust/hir/tree/rust-hir-visitor.h | 3 +
gcc/rust/hir/tree/rust-hir.h | 1 +
gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 7 +++
gcc/rust/typecheck/rust-hir-type-check-pattern.h | 1 +
16 files changed, 117 insertions(+)
diff --git a/gcc/rust/backend/rust-compile-fnparam.h b/gcc/rust/backend/rust-compile-fnparam.h
index 10e8b67c140..c5b08e3a019 100644
--- a/gcc/rust/backend/rust-compile-fnparam.h
+++ b/gcc/rust/backend/rust-compile-fnparam.h
@@ -39,6 +39,7 @@ public:
void visit (HIR::TupleStructPattern &) override;
// Empty visit for unused Pattern HIR nodes.
+ void visit (HIR::AltPattern &) override {}
void visit (HIR::LiteralPattern &) override {}
void visit (HIR::PathInExpression &) override {}
void visit (HIR::QualifiedPathInExpression &) override {}
diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h
index 8f44b7b5dbb..93faeeae8d1 100644
--- a/gcc/rust/backend/rust-compile-pattern.h
+++ b/gcc/rust/backend/rust-compile-pattern.h
@@ -39,6 +39,13 @@ public:
void visit (HIR::WildcardPattern &pattern) override;
void visit (HIR::RangePattern &pattern) override;
+ // unsupported
+ void visit (HIR::AltPattern &pattern) override
+ {
+ rust_sorry_at (pattern.get_locus (),
+ "alternate pattern case labels not supported");
+ }
+
// Empty visit for unused Pattern HIR nodes.
void visit (HIR::IdentifierPattern &) override {}
void visit (HIR::LiteralPattern &) override;
@@ -70,6 +77,13 @@ public:
void visit (HIR::StructPattern &pattern) override;
void visit (HIR::TupleStructPattern &pattern) override;
+ // unsupported
+ void visit (HIR::AltPattern &pattern) override
+ {
+ rust_sorry_at (pattern.get_locus (),
+ "alternate pattern bindings not supported");
+ }
+
// Empty visit for unused Pattern HIR nodes.
void visit (HIR::IdentifierPattern &) override {}
void visit (HIR::LiteralPattern &) override {}
@@ -104,6 +118,12 @@ public:
void visit (HIR::TuplePattern &) override;
// check for unimplemented Pattern HIR nodes.
+ void visit (HIR::AltPattern &pattern) override
+ {
+ rust_sorry_at (pattern.get_locus (),
+ "alternate pattern let statements not supported");
+ }
+
void visit (HIR::LiteralPattern &pattern) override
{
rust_sorry_at (pattern.get_locus (),
diff --git a/gcc/rust/backend/rust-compile-resolve-path.h b/gcc/rust/backend/rust-compile-resolve-path.h
index 6aec7be472f..ad7a67fe911 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.h
+++ b/gcc/rust/backend/rust-compile-resolve-path.h
@@ -50,6 +50,7 @@ public:
void visit (HIR::RangePattern &) override {}
void visit (HIR::ReferencePattern &) override {}
void visit (HIR::SlicePattern &) override {}
+ void visit (HIR::AltPattern &) override {}
void visit (HIR::StructPattern &) override {}
void visit (HIR::TuplePattern &) override {}
void visit (HIR::TupleStructPattern &) override {}
diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h
index 13b429d41e1..bc838167b2b 100644
--- a/gcc/rust/backend/rust-compile-var-decl.h
+++ b/gcc/rust/backend/rust-compile-var-decl.h
@@ -54,6 +54,7 @@ public:
}
// Empty visit for unused Pattern HIR nodes.
+ void visit (HIR::AltPattern &) override {}
void visit (HIR::LiteralPattern &) override {}
void visit (HIR::PathInExpression &) override {}
void visit (HIR::QualifiedPathInExpression &) override {}
diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc
index 3752cf36716..d0ca5cf1c8a 100644
--- a/gcc/rust/checks/errors/rust-const-checker.cc
+++ b/gcc/rust/checks/errors/rust-const-checker.cc
@@ -838,6 +838,10 @@ void
ConstChecker::visit (SlicePattern &)
{}
+void
+ConstChecker::visit (AltPattern &)
+{}
+
void
ConstChecker::visit (EmptyStmt &)
{}
diff --git a/gcc/rust/checks/errors/rust-const-checker.h b/gcc/rust/checks/errors/rust-const-checker.h
index e0402361367..2183992b7d3 100644
--- a/gcc/rust/checks/errors/rust-const-checker.h
+++ b/gcc/rust/checks/errors/rust-const-checker.h
@@ -187,6 +187,7 @@ private:
virtual void visit (TuplePatternItemsRanged &tuple_items) override;
virtual void visit (TuplePattern &pattern) override;
virtual void visit (SlicePattern &pattern) override;
+ virtual void visit (AltPattern &pattern) override;
virtual void visit (EmptyStmt &stmt) override;
virtual void visit (LetStmt &stmt) override;
virtual void visit (ExprStmtWithoutBlock &stmt) override;
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index ff7a0b3f85a..be85c90c7ad 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -905,6 +905,10 @@ void
UnsafeChecker::visit (SlicePattern &)
{}
+void
+UnsafeChecker::visit (AltPattern &)
+{}
+
void
UnsafeChecker::visit (EmptyStmt &)
{}
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h b/gcc/rust/checks/errors/rust-unsafe-checker.h
index ce270c0e7ef..2e6719a5d6e 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.h
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.h
@@ -169,6 +169,7 @@ private:
virtual void visit (TuplePatternItemsRanged &tuple_items) override;
virtual void visit (TuplePattern &pattern) override;
virtual void visit (SlicePattern &pattern) override;
+ virtual void visit (AltPattern &pattern) override;
virtual void visit (EmptyStmt &stmt) override;
virtual void visit (LetStmt &stmt) override;
virtual void visit (ExprStmtWithoutBlock &stmt) override;
diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc
index d6b458b72d3..9975a1e66b1 100644
--- a/gcc/rust/hir/rust-hir-dump.cc
+++ b/gcc/rust/hir/rust-hir-dump.cc
@@ -580,6 +580,9 @@ Dump::visit (TuplePattern &)
void
Dump::visit (SlicePattern &)
{}
+void
+Dump::visit (AltPattern &)
+{}
void
Dump::visit (EmptyStmt &)
diff --git a/gcc/rust/hir/rust-hir-dump.h b/gcc/rust/hir/rust-hir-dump.h
index b66e6b13f7c..c1ef9849b46 100644
--- a/gcc/rust/hir/rust-hir-dump.h
+++ b/gcc/rust/hir/rust-hir-dump.h
@@ -164,6 +164,7 @@ private:
virtual void visit (TuplePatternItemsRanged &) override;
virtual void visit (TuplePattern &) override;
virtual void visit (SlicePattern &) override;
+ virtual void visit (AltPattern &) override;
virtual void visit (EmptyStmt &) override;
virtual void visit (LetStmt &) override;
diff --git a/gcc/rust/hir/tree/rust-hir-full-decls.h b/gcc/rust/hir/tree/rust-hir-full-decls.h
index 30adda6a1f3..d486d918186 100644
--- a/gcc/rust/hir/tree/rust-hir-full-decls.h
+++ b/gcc/rust/hir/tree/rust-hir-full-decls.h
@@ -207,6 +207,7 @@ class TuplePatternItemsMultiple;
class TuplePatternItemsRanged;
class TuplePattern;
class SlicePattern;
+class AltPattern;
// rust-type.h
class TraitBound;
diff --git a/gcc/rust/hir/tree/rust-hir-pattern.h b/gcc/rust/hir/tree/rust-hir-pattern.h
index 47a38cc075c..69faed1643e 100644
--- a/gcc/rust/hir/tree/rust-hir-pattern.h
+++ b/gcc/rust/hir/tree/rust-hir-pattern.h
@@ -1300,6 +1300,73 @@ protected:
}
};
+// HIR node for alternative patterns
+class AltPattern : public Pattern
+{
+ std::vector<std::unique_ptr<Pattern>> alts;
+ Location locus;
+ Analysis::NodeMapping mappings;
+
+public:
+public:
+ std::string as_string () const override;
+
+ AltPattern (Analysis::NodeMapping mappings,
+ std::vector<std::unique_ptr<Pattern>> alts, Location locus)
+ : alts (std::move (alts)), locus (locus), mappings (mappings)
+ {}
+
+ // Copy constructor with vector clone
+ AltPattern (AltPattern const &other)
+ : locus (other.locus), mappings (other.mappings)
+ {
+ alts.reserve (other.alts.size ());
+ for (const auto &e : other.alts)
+ alts.push_back (e->clone_pattern ());
+ }
+
+ // Overloaded assignment operator to vector clone
+ AltPattern &operator= (AltPattern const &other)
+ {
+ locus = other.locus;
+ mappings = other.mappings;
+
+ alts.clear ();
+ alts.reserve (other.alts.size ());
+ for (const auto &e : other.alts)
+ alts.push_back (e->clone_pattern ());
+
+ return *this;
+ }
+
+ // move constructors
+ AltPattern (AltPattern &&other) = default;
+ AltPattern &operator= (AltPattern &&other) = default;
+
+ Location get_locus () const override { return locus; }
+
+ void accept_vis (HIRFullVisitor &vis) override;
+ void accept_vis (HIRPatternVisitor &vis) override;
+
+ Analysis::NodeMapping get_pattern_mappings () const override final
+ {
+ return mappings;
+ }
+
+ PatternType get_pattern_type () const override final
+ {
+ return PatternType::ALT;
+ }
+
+protected:
+ /* Use covariance to implement clone function as returning this object rather
+ * than base */
+ AltPattern *clone_pattern_impl () const override
+ {
+ return new AltPattern (*this);
+ }
+};
+
// Moved definition to rust-path.h
class PathPattern;
diff --git a/gcc/rust/hir/tree/rust-hir-visitor.h b/gcc/rust/hir/tree/rust-hir-visitor.h
index a635f9079f1..fa6f5923f2f 100644
--- a/gcc/rust/hir/tree/rust-hir-visitor.h
+++ b/gcc/rust/hir/tree/rust-hir-visitor.h
@@ -139,6 +139,7 @@ public:
virtual void visit (TuplePatternItemsRanged &tuple_items) = 0;
virtual void visit (TuplePattern &pattern) = 0;
virtual void visit (SlicePattern &pattern) = 0;
+ virtual void visit (AltPattern &pattern) = 0;
virtual void visit (EmptyStmt &stmt) = 0;
virtual void visit (LetStmt &stmt) = 0;
virtual void visit (ExprStmtWithoutBlock &stmt) = 0;
@@ -290,6 +291,7 @@ public:
virtual void visit (TuplePatternItemsRanged &) override {}
virtual void visit (TuplePattern &) override {}
virtual void visit (SlicePattern &) override {}
+ virtual void visit (AltPattern &) override {}
virtual void visit (EmptyStmt &) override {}
virtual void visit (LetStmt &) override {}
@@ -475,6 +477,7 @@ public:
virtual void visit (RangePattern &) = 0;
virtual void visit (ReferencePattern &) = 0;
virtual void visit (SlicePattern &) = 0;
+ virtual void visit (AltPattern &) = 0;
virtual void visit (StructPattern &) = 0;
virtual void visit (TuplePattern &) = 0;
virtual void visit (TupleStructPattern &) = 0;
diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h
index 28f1f21a6bb..6ed74428356 100644
--- a/gcc/rust/hir/tree/rust-hir.h
+++ b/gcc/rust/hir/tree/rust-hir.h
@@ -384,6 +384,7 @@ public:
TUPLE,
GROUPED,
SLICE,
+ ALT
};
BaseKind get_hir_kind () override final { return PATTERN; }
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
index b7412bc405d..3dca07baf9c 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
@@ -437,5 +437,12 @@ TypeCheckPattern::emit_pattern_size_error (const HIR::Pattern &pattern,
got_field_count == 1 ? "element" : "elements");
}
+void
+TypeCheckPattern::visit (HIR::AltPattern &pattern)
+{
+ rust_sorry_at (pattern.get_locus (),
+ "type checking alternate patterns not supported");
+}
+
} // namespace Resolver
} // namespace Rust
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.h b/gcc/rust/typecheck/rust-hir-type-check-pattern.h
index a4fbf888213..3b392b552d5 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.h
@@ -42,6 +42,7 @@ public:
void visit (HIR::QualifiedPathInExpression &pattern) override;
void visit (HIR::ReferencePattern &pattern) override;
void visit (HIR::SlicePattern &pattern) override;
+ void visit (HIR::AltPattern &pattern) override;
private:
TypeCheckPattern (TyTy::BaseType *parent);
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-03-08 9:56 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 9:56 [gcc/devel/rust/master] Add AltPattern HIR node Thomas Schwinge
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).