Skip to content

Commit

Permalink
Add get_pattern_kind to Pattern
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* ast/rust-ast.h: Add Kind Enum to
	Pattern.
	* ast/rust-macro.h: Add get_pattern_kind().
	* ast/rust-path.h: Likewise.
	* ast/rust-pattern.h: Likewise.

Signed-off-by: 0xn4utilus <[email protected]>
  • Loading branch information
0xn4utilus authored and P-E-P committed Mar 5, 2024
1 parent 48add29 commit 1c467c0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1360,12 +1360,32 @@ class IdentifierExpr : public ExprWithoutBlock
class Pattern : public Visitable
{
public:
enum class Kind
{
Literal,
Identifier,
Wildcard,
Rest,
Range,
Reference,
Struct,
TupleStruct,
Tuple,
Grouped,
Slice,
Alt,
Path,
MacroInvocation,
};

// Unique pointer custom clone function
std::unique_ptr<Pattern> clone_pattern () const
{
return std::unique_ptr<Pattern> (clone_pattern_impl ());
}

virtual Kind get_pattern_kind () = 0;

// possible virtual methods: is_refutable()

virtual ~Pattern () {}
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/ast/rust-macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,11 @@ class MacroInvocation : public TypeNoBounds,

std::string as_string () const override;

Pattern::Kind get_pattern_kind () override
{
return Pattern::Kind::MacroInvocation;
}

/**
* The default constructor you should use. Whenever we parse a macro call, we
* cannot possibly know whether or not this call refers to a builtin macro or
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ class PathPattern : public Pattern
// TODO: this seems kinda dodgy
std::vector<PathExprSegment> &get_segments () { return segments; }
const std::vector<PathExprSegment> &get_segments () const { return segments; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Path; }
};

/* AST node representing a path-in-expression pattern (path that allows
Expand Down
33 changes: 33 additions & 0 deletions gcc/rust/ast/rust-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class LiteralPattern : public Pattern

const Literal &get_literal () const { return lit; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Literal; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -149,6 +151,11 @@ class IdentifierPattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override
{
return Pattern::Kind::Identifier;
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -177,6 +184,8 @@ class WildcardPattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Wildcard; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -204,6 +213,8 @@ class RestPattern : public Pattern

NodeId get_node_id () const override final { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Rest; }

protected:
RestPattern *clone_pattern_impl () const override
{
Expand Down Expand Up @@ -431,6 +442,8 @@ class RangePattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Range; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -499,6 +512,11 @@ class ReferencePattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override
{
return Pattern::Kind::Reference;
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -934,6 +952,8 @@ class StructPattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Struct; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -1174,6 +1194,11 @@ class TupleStructPattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override
{
return Pattern::Kind::TupleStruct;
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -1411,6 +1436,8 @@ class TuplePattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Tuple; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -1471,6 +1498,8 @@ class GroupedPattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Grouped; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -1535,6 +1564,8 @@ class SlicePattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Slice; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -1600,6 +1631,8 @@ class AltPattern : public Pattern

NodeId get_node_id () const override { return node_id; }

Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Alt; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down

0 comments on commit 1c467c0

Please sign in to comment.