]> gcc.gnu.org Git - gcc.git/commitdiff
gccrs: ast: Refactor and add some Path node visitors
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 22 Mar 2023 12:20:31 +0000 (13:20 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:40 +0000 (18:28 +0100)
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>
gcc/rust/ast/rust-ast-tokenstream.cc
gcc/rust/ast/rust-ast-tokenstream.h
gcc/rust/ast/rust-ast.h
gcc/rust/ast/rust-expr.h

index 46ccf248a576cd14c1a1f1c815ee28133145de9c..92737fcece4b4ed8f894f665e9e68c2037e62944 100644 (file)
@@ -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)
index 6432ac40a1f8cdafcd8e3019ee2f4c7dba1391b8..70ef24637ff8761bc6eef86dc82925620801d6cb 100644 (file)
@@ -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);
 
index 70da859134081b8366072c5372f3ffb34387a9ac..b1406483cd7c8256f8844bcc053da1a23cf5d0cc 100644 (file)
@@ -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; }
 
index f5461848009040a07c437ea1c937c6f05dd2ba72..1b3a85fea411de7d30c35a628264b69f85108ba1 100644 (file)
@@ -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 ();
This page took 0.070084 seconds and 5 git commands to generate.