public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Refactor and add some Path node visitors
@ 2023-04-06 21:33 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:33 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:028f85abf70e231f9517a9f693fb6f3111d3a213

commit 028f85abf70e231f9517a9f693fb6f3111d3a213
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Wed Mar 22 13:20:31 2023 +0100

    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>

Diff:
---
 gcc/rust/ast/rust-ast-tokenstream.cc | 240 +++++++++++++++++------------------
 gcc/rust/ast/rust-ast-tokenstream.h  |   4 +
 gcc/rust/ast/rust-ast.h              |   5 +
 gcc/rust/ast/rust-expr.h             |   6 +
 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 46ccf248a57..92737fcece4 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 ())
     {
@@ -140,6 +139,17 @@ TokenStream::visit (Attribute &attrib)
   tokens.push_back (Rust::Token::make (RIGHT_SQUARE, Location ()));
 }
 
+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)
 {
@@ -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);
     }
 }
 
@@ -612,76 +612,68 @@ TokenStream::visit (TypePath &path)
   visit_items_joined_by_separator (path.get_segments (), SCOPE_RESOLUTION);
 }
 
+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 6432ac40a1f..70ef24637ff 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 90a57731423..a4da389615f 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 f5461848009..1b3a85fea41 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 ();

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:33 [gcc/devel/rust/master] ast: Refactor and add some Path node visitors 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).