aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-03-22 13:20:31 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2023-03-30 16:48:26 +0200
commit028f85abf70e231f9517a9f693fb6f3111d3a213 (patch)
treefed477ae98fc5c86c7b7959243cf82743b3066a2 /gcc
parentcf6c77494c09e23aa2108eb238efcae35627e810 (diff)
downloadgcc-028f85abf70e231f9517a9f693fb6f3111d3a213.zip
gcc-028f85abf70e231f9517a9f693fb6f3111d3a213.tar.gz
gcc-028f85abf70e231f9517a9f693fb6f3111d3a213.tar.bz2
ast: Refactor and add some Path node visitors
Implement some functions for Path nodes and refactor existing ones by merging some common code. gcc/rust/ChangeLog: * ast/rust-ast-tokenstream.cc (TokenStream::visit): Implement visitors. * ast/rust-ast-tokenstream.h: Add function prototypes. * ast/rust-ast.h: Add missing getters. * ast/rust-expr.h: Likewise. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast-tokenstream.cc240
-rw-r--r--gcc/rust/ast/rust-ast-tokenstream.h4
-rw-r--r--gcc/rust/ast/rust-ast.h5
-rw-r--r--gcc/rust/ast/rust-expr.h6
4 files changed, 131 insertions, 124 deletions
diff --git a/gcc/rust/ast/rust-ast-tokenstream.cc b/gcc/rust/ast/rust-ast-tokenstream.cc
index 46ccf24..92737fc 100644
--- a/gcc/rust/ast/rust-ast-tokenstream.cc
+++ b/gcc/rust/ast/rust-ast-tokenstream.cc
@@ -105,8 +105,7 @@ TokenStream::visit (Attribute &attrib)
{
tokens.push_back (Rust::Token::make (HASH, attrib.get_locus ()));
tokens.push_back (Rust::Token::make (LEFT_SQUARE, Location ()));
- visit_items_joined_by_separator (attrib.get_path ().get_segments (),
- SCOPE_RESOLUTION);
+ visit (attrib.get_path ());
if (attrib.has_attr_input ())
{
@@ -141,6 +140,17 @@ TokenStream::visit (Attribute &attrib)
}
void
+TokenStream::visit (SimplePath &path)
+{
+ if (path.get_has_opening_scope_resolution ())
+ {
+ tokens.push_back (
+ Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
+ }
+ visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
+}
+
+void
TokenStream::visit (SimplePathSegment &segment)
{
auto name = segment.get_segment_name ();
@@ -197,8 +207,7 @@ TokenStream::visit (Visibility &vis)
tokens.push_back (Rust::Token::make (PUB, vis.get_locus ()));
tokens.push_back (Rust::Token::make (LEFT_PAREN, Location ()));
tokens.push_back (Rust::Token::make_identifier (Location (), "in"));
- visit_items_joined_by_separator (vis.get_path ().get_segments (),
- SCOPE_RESOLUTION);
+ visit (vis.get_path ());
tokens.push_back (Rust::Token::make (RIGHT_PAREN, Location ()));
break;
case Visibility::PRIV:
@@ -401,80 +410,71 @@ TokenStream::visit (LifetimeParam &lifetime_param)
}
void
-TokenStream::visit (ConstGenericParam &)
-{}
+TokenStream::visit (ConstGenericParam &param)
+{
+ // Syntax:
+ // const IDENTIFIER : Type ( = Block | IDENTIFIER | -?LITERAL )?
+
+ tokens.push_back (Rust::Token::make (CONST, param.get_locus ()));
+ auto id = param.get_name ();
+ tokens.push_back (Rust::Token::make_identifier (Location (), std::move (id)));
+ tokens.push_back (Rust::Token::make (COLON, Location ()));
+ visit (param.get_type ());
+ if (param.has_default_value ())
+ {
+ tokens.push_back (Rust::Token::make (EQUAL, Location ()));
+ visit (param.get_type ());
+ }
+}
void
-TokenStream::visit (PathInExpression &path)
+TokenStream::visit (PathExprSegment &segment)
{
- if (path.opening_scope_resolution ())
+ visit (segment.get_ident_segment ());
+ if (segment.has_generic_args ())
{
+ auto generics = segment.get_generic_args ();
tokens.push_back (
- Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
- }
+ Rust::Token::make (SCOPE_RESOLUTION, segment.get_locus ()));
+ tokens.push_back (Rust::Token::make (LEFT_ANGLE, generics.get_locus ()));
- for (auto &segment : path.get_segments ())
- {
- auto ident_segment = segment.get_ident_segment ();
- // TODO: Add visitor pattern to PathIdentSegment ?
- if (ident_segment.is_super_segment ())
- {
- tokens.push_back (
- Rust::Token::make (SUPER, ident_segment.get_locus ()));
- }
- else if (ident_segment.is_crate_segment ())
- {
- tokens.push_back (
- Rust::Token::make (CRATE, ident_segment.get_locus ()));
- }
- else if (ident_segment.is_lower_self ())
- {
- tokens.push_back (
- Rust::Token::make (SELF, ident_segment.get_locus ()));
- }
- else if (ident_segment.is_big_self ())
- {
- tokens.push_back (
- Rust::Token::make (SELF_ALIAS, ident_segment.get_locus ()));
- }
- else
+ auto &lifetime_args = generics.get_lifetime_args ();
+ auto &generic_args = generics.get_generic_args ();
+ auto &binding_args = generics.get_binding_args ();
+
+ visit_items_joined_by_separator (generic_args, COMMA);
+
+ if (!lifetime_args.empty ()
+ && (!generic_args.empty () || !binding_args.empty ()))
{
- auto id = ident_segment.as_string ();
- tokens.push_back (
- Rust::Token::make_identifier (ident_segment.get_locus (),
- std::move (id)));
+ tokens.push_back (Rust::Token::make (COMMA, Location ()));
}
- if (segment.has_generic_args ())
- {
- auto generics = segment.get_generic_args ();
- tokens.push_back (
- Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
- tokens.push_back (
- Rust::Token::make (LEFT_ANGLE, generics.get_locus ()));
-
- auto &lifetime_args = generics.get_lifetime_args ();
- auto &generic_args = generics.get_generic_args ();
- auto &binding_args = generics.get_binding_args ();
- visit_items_joined_by_separator (generic_args, COMMA);
+ visit_items_joined_by_separator (binding_args, COMMA);
- if (!lifetime_args.empty ()
- && (!generic_args.empty () || !binding_args.empty ()))
- {
- tokens.push_back (Rust::Token::make (COMMA, Location ()));
- }
+ if (!generic_args.empty () && !binding_args.empty ())
+ {
+ tokens.push_back (Rust::Token::make (COMMA, Location ()));
+ }
- visit_items_joined_by_separator (binding_args, COMMA);
+ visit_items_joined_by_separator (lifetime_args, COMMA);
- if (!generic_args.empty () && !binding_args.empty ())
- {
- tokens.push_back (Rust::Token::make (COMMA, Location ()));
- }
+ tokens.push_back (Rust::Token::make (RIGHT_ANGLE, Location ()));
+ }
+}
- visit_items_joined_by_separator (lifetime_args, COMMA);
+void
+TokenStream::visit (PathInExpression &path)
+{
+ if (path.opening_scope_resolution ())
+ {
+ tokens.push_back (
+ Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
+ }
- tokens.push_back (Rust::Token::make (RIGHT_ANGLE, Location ()));
- }
+ for (auto &segment : path.get_segments ())
+ {
+ visit (segment);
}
}
@@ -613,75 +613,67 @@ TokenStream::visit (TypePath &path)
}
void
+TokenStream::visit (PathIdentSegment &segment)
+{
+ if (segment.is_super_segment ())
+ {
+ tokens.push_back (Rust::Token::make (SUPER, segment.get_locus ()));
+ }
+ else if (segment.is_crate_segment ())
+ {
+ tokens.push_back (Rust::Token::make (CRATE, segment.get_locus ()));
+ }
+ else if (segment.is_lower_self ())
+ {
+ tokens.push_back (Rust::Token::make (SELF, segment.get_locus ()));
+ }
+ else if (segment.is_big_self ())
+ {
+ tokens.push_back (Rust::Token::make (SELF_ALIAS, segment.get_locus ()));
+ }
+ else
+ {
+ auto id = segment.as_string ();
+ tokens.push_back (
+ Rust::Token::make_identifier (segment.get_locus (), std::move (id)));
+ }
+}
+
+void
TokenStream::visit (QualifiedPathInExpression &path)
{
for (auto &segment : path.get_segments ())
{
- auto ident_segment = segment.get_ident_segment ();
- if (ident_segment.is_super_segment ())
- {
- tokens.push_back (
- Rust::Token::make (SUPER, ident_segment.get_locus ()));
- }
- else if (ident_segment.is_crate_segment ())
- {
- tokens.push_back (
- Rust::Token::make (CRATE, ident_segment.get_locus ()));
- }
- else if (ident_segment.is_lower_self ())
- {
- tokens.push_back (
- Rust::Token::make (SELF, ident_segment.get_locus ()));
- }
- else if (ident_segment.is_big_self ())
- {
- tokens.push_back (
- Rust::Token::make (SELF_ALIAS, ident_segment.get_locus ()));
- }
- else
- {
- auto id = ident_segment.as_string ();
- tokens.push_back (
- Rust::Token::make_identifier (ident_segment.get_locus (),
- std::move (id)));
- }
- if (segment.has_generic_args ())
- {
- auto generics = segment.get_generic_args ();
- tokens.push_back (
- Rust::Token::make (SCOPE_RESOLUTION, path.get_locus ()));
- tokens.push_back (
- Rust::Token::make (LEFT_ANGLE, generics.get_locus ()));
-
- auto &lifetime_args = generics.get_lifetime_args ();
- auto &generic_args = generics.get_generic_args ();
- auto &binding_args = generics.get_binding_args ();
-
- visit_items_joined_by_separator (generic_args, COMMA);
-
- if (!lifetime_args.empty ()
- && (!generic_args.empty () || !binding_args.empty ()))
- {
- tokens.push_back (Rust::Token::make (COMMA, Location ()));
- }
-
- visit_items_joined_by_separator (binding_args, COMMA);
-
- if (!generic_args.empty () && !binding_args.empty ())
- {
- tokens.push_back (Rust::Token::make (COMMA, Location ()));
- }
-
- visit_items_joined_by_separator (lifetime_args, COMMA);
+ visit (segment);
+ }
+}
- tokens.push_back (Rust::Token::make (RIGHT_ANGLE, Location ()));
- }
+void
+TokenStream::visit (QualifiedPathType &path)
+{
+ tokens.push_back (Rust::Token::make (LEFT_ANGLE, path.get_locus ()));
+ visit (path.get_type ());
+ if (path.has_as_clause ())
+ {
+ tokens.push_back (Rust::Token::make (AS, Location ()));
+ visit (path.get_as_type_path ());
}
+ tokens.push_back (Rust::Token::make (RIGHT_ANGLE, Location ()));
}
void
-TokenStream::visit (QualifiedPathInType &)
-{}
+TokenStream::visit (QualifiedPathInType &path)
+{
+ visit (path.get_qualified_path_type ());
+
+ tokens.push_back (Rust::Token::make (COLON, Location ()));
+ visit (path.get_associated_segment ());
+ for (auto &segment : path.get_segments ())
+ {
+ tokens.push_back (Rust::Token::make (COLON, Location ()));
+ visit (segment);
+ }
+}
void
TokenStream::visit (Literal &lit, Location locus)
diff --git a/gcc/rust/ast/rust-ast-tokenstream.h b/gcc/rust/ast/rust-ast-tokenstream.h
index 6432ac4..70ef246 100644
--- a/gcc/rust/ast/rust-ast-tokenstream.h
+++ b/gcc/rust/ast/rust-ast-tokenstream.h
@@ -121,11 +121,15 @@ private:
void visit (ConstGenericParam &const_param);
// rust-path.h
+ void visit (SimplePath &path);
+ void visit (PathExprSegment &segment);
+ void visit (PathIdentSegment &segment);
void visit (PathInExpression &path);
void visit (TypePathSegment &segment);
void visit (TypePathSegmentGeneric &segment);
void visit (TypePathSegmentFunction &segment);
void visit (TypePath &path);
+ void visit (QualifiedPathType &path);
void visit (QualifiedPathInExpression &path);
void visit (QualifiedPathInType &path);
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 90a5773..a4da389 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -393,6 +393,11 @@ public:
std::string as_string () const;
+ bool get_has_opening_scope_resolution () const
+ {
+ return has_opening_scope_resolution;
+ }
+
Location get_locus () const { return locus; }
NodeId get_node_id () const { return node_id; }
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index f546184..1b3a85f 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -152,6 +152,8 @@ public:
Location get_locus () const override { return lit_expr.get_locus (); }
+ LiteralExpr get_literal () const { return lit_expr; }
+
void accept_vis (ASTVisitor &vis) override;
bool check_cfg_predicate (const Session &session) const override;
@@ -175,6 +177,10 @@ public:
: path (std::move (path)), lit (std::move (lit_expr))
{}
+ SimplePath get_path () const { return path; }
+
+ LiteralExpr get_literal () const { return lit; }
+
std::string as_string () const override
{
return path.as_string () + " = " + lit.as_string ();