mirror of
https://gcc.gnu.org/git/gcc.git
synced 2026-02-22 20:01:22 -05:00
gccrs: Add AST::AltPattern class
gcc/rust/ChangeLog: * ast/rust-ast-dump.cc (Dump::visit): Add AltPattern visitor. * ast/rust-ast-dump.h: (Dump::visit): Add AltPattern visitor. * ast/rust-ast-full-decls.h (class AltPattern): Add declaration. * ast/rust-ast-visitor.h: (ASTVisitor::visit): Add AltPattern visitor. * ast/rust-ast.cc (AltPattern::as_string): Add definition. (AltPattern::accept_vis): Add definition. * ast/rust-pattern.h (class AltPattern): Add declaration. * checks/errors/rust-feature-gate.h: (FeatureGate::visit) Add AltPattern visitor * expand/rust-attribute-visitor.cc (AttrVisitor::visit): Add AltPattern visitor. * expand/rust-attribute-visitor.h: (AttrVisitor::visit): Add AltPattern visitor. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Add AltPattern visitor. * hir/rust-ast-lower-base.h: (ASTLoweringBase::visit): Add AltPattern visitor. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Add AltPattern visitor. * resolve/rust-ast-resolve-base.h: (ResolverBase::visit): Add AltPattern visitor. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Add AltPattern visitor. * resolve/rust-early-name-resolver.h: (EarlyNameResolver::visit): Add AltPattern visitor. * util/rust-attributes.cc (AttributeChecker::visit): Add AltPattern visitor. * util/rust-attributes.h: (AttributeChecker::visit): Add AltPattern visitor. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
This commit is contained in:
@@ -1678,6 +1678,10 @@ void
|
||||
Dump::visit (SlicePattern &)
|
||||
{}
|
||||
|
||||
void
|
||||
Dump::visit (AltPattern &)
|
||||
{}
|
||||
|
||||
// rust-stmt.h
|
||||
void
|
||||
Dump::visit (EmptyStmt &)
|
||||
|
||||
@@ -293,6 +293,7 @@ private:
|
||||
void visit (TuplePattern &pattern);
|
||||
void visit (GroupedPattern &pattern);
|
||||
void visit (SlicePattern &pattern);
|
||||
void visit (AltPattern &pattern);
|
||||
|
||||
// rust-stmt.h
|
||||
void visit (EmptyStmt &stmt);
|
||||
|
||||
@@ -249,6 +249,7 @@ class TuplePatternItemsRanged;
|
||||
class TuplePattern;
|
||||
class GroupedPattern;
|
||||
class SlicePattern;
|
||||
class AltPattern;
|
||||
|
||||
// rust-type.h
|
||||
class TraitBound;
|
||||
|
||||
@@ -203,6 +203,7 @@ public:
|
||||
virtual void visit (TuplePattern &pattern) = 0;
|
||||
virtual void visit (GroupedPattern &pattern) = 0;
|
||||
virtual void visit (SlicePattern &pattern) = 0;
|
||||
virtual void visit (AltPattern &pattern) = 0;
|
||||
|
||||
// rust-stmt.h
|
||||
virtual void visit (EmptyStmt &stmt) = 0;
|
||||
|
||||
@@ -2704,6 +2704,17 @@ SlicePattern::as_string () const
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string
|
||||
AltPattern::as_string () const
|
||||
{
|
||||
std::string str ("AltPattern: ");
|
||||
|
||||
for (const auto &pattern : alts)
|
||||
str += "\n " + pattern->as_string ();
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string
|
||||
TuplePatternItemsMultiple::as_string () const
|
||||
{
|
||||
@@ -5649,6 +5660,12 @@ SlicePattern::accept_vis (ASTVisitor &vis)
|
||||
vis.visit (*this);
|
||||
}
|
||||
|
||||
void
|
||||
AltPattern::accept_vis (ASTVisitor &vis)
|
||||
{
|
||||
vis.visit (*this);
|
||||
}
|
||||
|
||||
void
|
||||
EmptyStmt::accept_vis (ASTVisitor &vis)
|
||||
{
|
||||
|
||||
@@ -1563,6 +1563,72 @@ protected:
|
||||
}
|
||||
};
|
||||
|
||||
// AST node for alternate patterns
|
||||
// joins together what are technically 'PatternNoTopAlt's
|
||||
class AltPattern : public Pattern
|
||||
{
|
||||
std::vector<std::unique_ptr<Pattern>> alts;
|
||||
Location locus;
|
||||
NodeId node_id;
|
||||
|
||||
public:
|
||||
std::string as_string () const override;
|
||||
|
||||
AltPattern (std::vector<std::unique_ptr<Pattern>> alts, Location locus)
|
||||
: alts (std::move (alts)), locus (locus),
|
||||
node_id (Analysis::Mappings::get ()->get_next_node_id ())
|
||||
{}
|
||||
|
||||
// Copy constructor with vector clone
|
||||
AltPattern (AltPattern const &other) : locus (other.locus)
|
||||
{
|
||||
node_id = other.node_id;
|
||||
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;
|
||||
node_id = other.node_id;
|
||||
|
||||
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 final { return locus; }
|
||||
|
||||
void accept_vis (ASTVisitor &vis) override;
|
||||
|
||||
// TODO: seems kinda dodgy. Think of better way.
|
||||
std::vector<std::unique_ptr<Pattern>> &get_alts () { return alts; }
|
||||
const std::vector<std::unique_ptr<Pattern>> &get_alts () const
|
||||
{
|
||||
return alts;
|
||||
}
|
||||
|
||||
NodeId get_node_id () const { return node_id; }
|
||||
|
||||
NodeId get_pattern_node_id () const override final { return node_id; }
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -169,6 +169,7 @@ public:
|
||||
void visit (AST::TuplePattern &pattern) override {}
|
||||
void visit (AST::GroupedPattern &pattern) override {}
|
||||
void visit (AST::SlicePattern &pattern) override {}
|
||||
void visit (AST::AltPattern &pattern) override {}
|
||||
void visit (AST::EmptyStmt &stmt) override {}
|
||||
void visit (AST::LetStmt &stmt) override {}
|
||||
void visit (AST::ExprStmtWithoutBlock &stmt) override {}
|
||||
|
||||
@@ -3152,6 +3152,20 @@ AttrVisitor::visit (AST::SlicePattern &pattern)
|
||||
// TODO: quit stripping now? or keep going?
|
||||
}
|
||||
}
|
||||
void
|
||||
AttrVisitor::visit (AST::AltPattern &pattern)
|
||||
{
|
||||
// can't strip individual patterns, only sub-patterns
|
||||
for (auto &alt : pattern.get_alts ())
|
||||
{
|
||||
alt->accept_vis (*this);
|
||||
|
||||
if (alt->is_marked_for_strip ())
|
||||
rust_error_at (alt->get_locus (),
|
||||
"cannot strip pattern in this position");
|
||||
// TODO: quit stripping now? or keep going?
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
AttrVisitor::visit (AST::EmptyStmt &)
|
||||
|
||||
@@ -258,6 +258,7 @@ public:
|
||||
void visit (AST::TuplePattern &pattern) override;
|
||||
void visit (AST::GroupedPattern &pattern) override;
|
||||
void visit (AST::SlicePattern &pattern) override;
|
||||
void visit (AST::AltPattern &pattern) override;
|
||||
|
||||
void visit (AST::EmptyStmt &) override;
|
||||
void visit (AST::LetStmt &stmt) override;
|
||||
|
||||
@@ -461,6 +461,9 @@ ASTLoweringBase::visit (AST::GroupedPattern &)
|
||||
void
|
||||
ASTLoweringBase::visit (AST::SlicePattern &)
|
||||
{}
|
||||
void
|
||||
ASTLoweringBase::visit (AST::AltPattern &)
|
||||
{}
|
||||
|
||||
// rust-stmt.h
|
||||
void
|
||||
|
||||
@@ -231,6 +231,7 @@ public:
|
||||
virtual void visit (AST::TuplePattern &pattern);
|
||||
virtual void visit (AST::GroupedPattern &pattern);
|
||||
virtual void visit (AST::SlicePattern &pattern);
|
||||
virtual void visit (AST::AltPattern &pattern);
|
||||
|
||||
// rust-stmt.h
|
||||
virtual void visit (AST::EmptyStmt &stmt);
|
||||
|
||||
@@ -582,6 +582,10 @@ void
|
||||
ResolverBase::visit (AST::SlicePattern &)
|
||||
{}
|
||||
|
||||
void
|
||||
ResolverBase::visit (AST::AltPattern &)
|
||||
{}
|
||||
|
||||
void
|
||||
ResolverBase::visit (AST::EmptyStmt &)
|
||||
{}
|
||||
|
||||
@@ -178,6 +178,7 @@ public:
|
||||
void visit (AST::TuplePattern &);
|
||||
void visit (AST::GroupedPattern &);
|
||||
void visit (AST::SlicePattern &);
|
||||
void visit (AST::AltPattern &);
|
||||
|
||||
void visit (AST::EmptyStmt &);
|
||||
void visit (AST::LetStmt &);
|
||||
|
||||
@@ -1052,6 +1052,13 @@ EarlyNameResolver::visit (AST::SlicePattern &pattern)
|
||||
item->accept_vis (*this);
|
||||
}
|
||||
|
||||
void
|
||||
EarlyNameResolver::visit (AST::AltPattern &pattern)
|
||||
{
|
||||
for (auto &alt : pattern.get_alts ())
|
||||
alt->accept_vis (*this);
|
||||
}
|
||||
|
||||
void
|
||||
EarlyNameResolver::visit (AST::EmptyStmt &)
|
||||
{}
|
||||
|
||||
@@ -249,6 +249,7 @@ private:
|
||||
virtual void visit (AST::TuplePattern &pattern);
|
||||
virtual void visit (AST::GroupedPattern &pattern);
|
||||
virtual void visit (AST::SlicePattern &pattern);
|
||||
virtual void visit (AST::AltPattern &pattern);
|
||||
virtual void visit (AST::EmptyStmt &stmt);
|
||||
virtual void visit (AST::LetStmt &stmt);
|
||||
virtual void visit (AST::ExprStmtWithoutBlock &stmt);
|
||||
|
||||
@@ -767,6 +767,10 @@ void
|
||||
AttributeChecker::visit (AST::SlicePattern &)
|
||||
{}
|
||||
|
||||
void
|
||||
AttributeChecker::visit (AST::AltPattern &)
|
||||
{}
|
||||
|
||||
// rust-stmt.h
|
||||
void
|
||||
AttributeChecker::visit (AST::EmptyStmt &)
|
||||
|
||||
@@ -242,6 +242,7 @@ private:
|
||||
void visit (AST::TuplePattern &pattern);
|
||||
void visit (AST::GroupedPattern &pattern);
|
||||
void visit (AST::SlicePattern &pattern);
|
||||
void visit (AST::AltPattern &pattern);
|
||||
|
||||
// rust-stmt.h
|
||||
void visit (AST::EmptyStmt &stmt);
|
||||
|
||||
Reference in New Issue
Block a user