public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Add difference between attributes
@ 2023-04-11  8:45 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-11  8:45 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e44f127cdb12a28536fe21983dfad20570bceda0

commit e44f127cdb12a28536fe21983dfad20570bceda0
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Wed Apr 5 12:36:41 2023 +0200

    ast: Add difference between attributes
    
    Add a boolean to tell inner and outer attributes ast nodes appart. This
    meant refactoring a bit their parsing function.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast.h: Add boolean for differenciation.
            * parse/rust-parse-impl.h (Parser::parse_doc_comment): Change
            function interface to make code cleaner. It should return a body
            instead of the whole attribute.
            (Parser::parse_inner_attribute): Specify the inner status of the
            node.
            (Parser::parse_attribute_body): Change function interface to
            make the code cleaner much like parse_doc_comment.
            (Parser::parse_outer_attribute): Specify outer status of the
            node.
            * parse/rust-parse.h: Update functions prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-ast.h          |  9 +++++++--
 gcc/rust/parse/rust-parse-impl.h | 41 +++++++++++++++++++++++++++++++---------
 gcc/rust/parse/rust-parse.h      |  6 ++++--
 3 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 09eef4eec29..be9c7224ab2 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -456,6 +456,8 @@ private:
 
   Location locus;
 
+  bool inner_attribute;
+
   // TODO: maybe a variable storing whether attr input is parsed or not
 
 public:
@@ -464,8 +466,9 @@ public:
 
   // Constructor has pointer AttrInput for polymorphism reasons
   Attribute (SimplePath path, std::unique_ptr<AttrInput> input,
-	     Location locus = Location ())
-    : path (std::move (path)), attr_input (std::move (input)), locus (locus)
+	     Location locus = Location (), bool inner_attribute = false)
+    : path (std::move (path)), attr_input (std::move (input)), locus (locus),
+      inner_attribute (inner_attribute)
   {}
 
   // default destructor
@@ -554,6 +557,8 @@ public:
 
   std::string as_string () const;
 
+  bool is_inner_attribute () const { return inner_attribute; }
+
   // no visitor pattern as not currently polymorphic
 
   const SimplePath &get_path () const { return path; }
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 7c554e8bd0f..d49aa6f3d5c 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -484,7 +484,7 @@ Parser<ManagedTokenSource>::parse_inner_attributes ()
 
 // Parse a inner or outer doc comment into an doc attribute
 template <typename ManagedTokenSource>
-AST::Attribute
+std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, Location>
 Parser<ManagedTokenSource>::parse_doc_comment ()
 {
   const_TokenPtr token = lexer.peek_token ();
@@ -498,7 +498,7 @@ Parser<ManagedTokenSource>::parse_doc_comment ()
   std::unique_ptr<AST::AttrInput> attr_input (
     new AST::AttrInputLiteral (std::move (lit_expr)));
   lexer.skip_token ();
-  return AST::Attribute (std::move (attr_path), std::move (attr_input), locus);
+  return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
 }
 
 // Parse a single inner attribute.
@@ -507,7 +507,13 @@ AST::Attribute
 Parser<ManagedTokenSource>::parse_inner_attribute ()
 {
   if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
-    return parse_doc_comment ();
+    {
+      auto values = parse_doc_comment ();
+      auto path = std::move (std::get<0> (values));
+      auto input = std::move (std::get<1> (values));
+      auto loc = std::get<2> (values);
+      return AST::Attribute (std::move (path), std::move (input), loc, true);
+    }
 
   if (lexer.peek_token ()->get_id () != HASH)
     {
@@ -533,7 +539,13 @@ Parser<ManagedTokenSource>::parse_inner_attribute ()
   if (!skip_token (LEFT_SQUARE))
     return AST::Attribute::create_empty ();
 
-  AST::Attribute actual_attribute = parse_attribute_body ();
+  auto values = parse_attribute_body ();
+
+  auto path = std::move (std::get<0> (values));
+  auto input = std::move (std::get<1> (values));
+  auto loc = std::get<2> (values);
+  auto actual_attribute
+    = AST::Attribute (std::move (path), std::move (input), loc, true);
 
   if (!skip_token (RIGHT_SQUARE))
     return AST::Attribute::create_empty ();
@@ -543,7 +555,7 @@ Parser<ManagedTokenSource>::parse_inner_attribute ()
 
 // Parses the body of an attribute (inner or outer).
 template <typename ManagedTokenSource>
-AST::Attribute
+std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, Location>
 Parser<ManagedTokenSource>::parse_attribute_body ()
 {
   Location locus = lexer.peek_token ()->get_locus ();
@@ -558,13 +570,13 @@ Parser<ManagedTokenSource>::parse_attribute_body ()
 
       // Skip past potential further info in attribute (i.e. attr_input)
       skip_after_end_attribute ();
-      return AST::Attribute::create_empty ();
+      return std::make_tuple (std::move (attr_path), nullptr, Location ());
     }
 
   std::unique_ptr<AST::AttrInput> attr_input = parse_attr_input ();
   // AttrInput is allowed to be null, so no checks here
 
-  return AST::Attribute (std::move (attr_path), std::move (attr_input), locus);
+  return std::make_tuple (std::move (attr_path), std::move (attr_input), locus);
 }
 
 /* Determines whether token is a valid simple path segment. This does not
@@ -1161,7 +1173,13 @@ AST::Attribute
 Parser<ManagedTokenSource>::parse_outer_attribute ()
 {
   if (lexer.peek_token ()->get_id () == OUTER_DOC_COMMENT)
-    return parse_doc_comment ();
+    {
+      auto values = parse_doc_comment ();
+      auto path = std::move (std::get<0> (values));
+      auto input = std::move (std::get<1> (values));
+      auto loc = std::get<2> (values);
+      return AST::Attribute (std::move (path), std::move (input), loc, false);
+    }
 
   if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)
     {
@@ -1199,7 +1217,12 @@ Parser<ManagedTokenSource>::parse_outer_attribute ()
 
   lexer.skip_token ();
 
-  AST::Attribute actual_attribute = parse_attribute_body ();
+  auto values = parse_attribute_body ();
+  auto path = std::move (std::get<0> (values));
+  auto input = std::move (std::get<1> (values));
+  auto loc = std::get<2> (values);
+  auto actual_attribute
+    = AST::Attribute (std::move (path), std::move (input), loc, true);
 
   if (lexer.peek_token ()->get_id () != RIGHT_SQUARE)
     return AST::Attribute::create_empty ();
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index 42168e961e3..6957b66a2bb 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -149,7 +149,8 @@ public:
   AST::Visibility parse_visibility ();
   std::unique_ptr<AST::IdentifierPattern> parse_identifier_pattern ();
   std::unique_ptr<AST::TokenTree> parse_token_tree ();
-  AST::Attribute parse_attribute_body ();
+  std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, Location>
+  parse_attribute_body ();
   AST::AttrVec parse_inner_attributes ();
   std::unique_ptr<AST::MacroInvocation>
   parse_macro_invocation (AST::AttrVec outer_attrs);
@@ -172,7 +173,8 @@ private:
   AST::AttrVec parse_outer_attributes ();
   AST::Attribute parse_outer_attribute ();
   std::unique_ptr<AST::AttrInput> parse_attr_input ();
-  AST::Attribute parse_doc_comment ();
+  std::tuple<AST::SimplePath, std::unique_ptr<AST::AttrInput>, Location>
+  parse_doc_comment ();
 
   // Path-related
   AST::SimplePath parse_simple_path ();

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

only message in thread, other threads:[~2023-04-11  8:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-11  8:45 [gcc/devel/rust/master] ast: Add difference between attributes 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).