https://gcc.gnu.org/g:6fef5bc497572798b817ad7afdcf5e5fb00c7910

commit r15-8151-g6fef5bc497572798b817ad7afdcf5e5fb00c7910
Author: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
Date:   Mon May 20 12:00:11 2024 +0200

    gccrs: Allow multiple outer attributes on generic params
    
    Previously generic params only allowed one outer attribute in front of
    them.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast-collector.cc (TokenCollector::visit): Visit outer
            attributes.
            * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Change outer
            attribute visit, we need to visit all of them.
            * ast/rust-ast.cc (LifetimeParam::as_string): Change as_string
            implementation to allow multiple outer attributes.
            (TypeParam::as_string): Likewise.
            * ast/rust-ast.h (class LifetimeParam): Allow multiple outer
            attributes.
            * ast/rust-item.h (class TypeParam): Likewise.
            * ast/rust-path.h: Likewise.
            * parse/rust-parse-impl.h (Parser::parse_generic_param): Change call
            to outer attribute parsing to collect several attributes.
            (Parser::parse_lifetime_param): Likewise.
            (Parser::parse_type_param): Likewise.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-ast-collector.cc |  3 +++
 gcc/rust/ast/rust-ast-visitor.cc   |  6 +++---
 gcc/rust/ast/rust-ast.cc           | 12 ++++++------
 gcc/rust/ast/rust-ast.h            | 13 ++++++-------
 gcc/rust/ast/rust-item.h           | 14 +++++++-------
 gcc/rust/ast/rust-path.h           | 10 +++++-----
 gcc/rust/parse/rust-parse-impl.h   | 14 +++++++-------
 7 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index a7f6ed57623a..a75722587f55 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -476,6 +476,7 @@ TokenCollector::visit (LifetimeParam &lifetime_param)
 
   // TODO what to do with outer attr? They are not mentioned in the reference.
 
+  visit_items_as_lines (lifetime_param.get_outer_attrs ());
   auto lifetime = lifetime_param.get_lifetime ();
   visit (lifetime);
 
@@ -495,6 +496,7 @@ TokenCollector::visit (ConstGenericParam &param)
   // Syntax:
   // const IDENTIFIER : Type ( = Block | IDENTIFIER | -?LITERAL )?
 
+  visit_items_as_lines (param.get_outer_attrs ());
   push (Rust::Token::make (CONST, param.get_locus ()));
   auto id = param.get_name ().as_string ();
   push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
@@ -1509,6 +1511,7 @@ TokenCollector::visit (TypeParam &param)
   // TypeParamBounds :
   //    TypeParamBound ( + TypeParamBound )* +?
 
+  visit_items_as_lines (param.get_outer_attrs ());
   auto id = param.get_type_representation ().as_string ();
   push (Rust::Token::make_identifier (param.get_locus (), std::move (id)));
   if (param.has_type_param_bounds ())
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index ba3eb67cbe7a..2c1674e21ea1 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -69,7 +69,7 @@ DefaultASTVisitor::visit (AST::Lifetime &lifetime)
 void
 DefaultASTVisitor::visit (AST::LifetimeParam &lifetime_param)
 {
-  visit (lifetime_param.get_outer_attribute ());
+  visit_outer_attrs (lifetime_param);
   visit (lifetime_param.get_lifetime ());
   for (auto &lifetime_bound : lifetime_param.get_lifetime_bounds ())
     visit (lifetime_bound);
@@ -78,7 +78,7 @@ DefaultASTVisitor::visit (AST::LifetimeParam &lifetime_param)
 void
 DefaultASTVisitor::visit (AST::ConstGenericParam &const_param)
 {
-  visit (const_param.get_outer_attribute ());
+  visit_outer_attrs (const_param);
   if (const_param.has_type ())
     visit (const_param.get_type ());
   if (const_param.has_default_value ())
@@ -665,7 +665,7 @@ DefaultASTVisitor::visit (AST::AsyncBlockExpr &expr)
 void
 DefaultASTVisitor::visit (AST::TypeParam &param)
 {
-  visit (param.get_outer_attribute ());
+  visit_outer_attrs (param);
   for (auto &bound : param.get_type_param_bounds ())
     visit (bound);
   if (param.has_type ())
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index bc896928ce6e..8045a6885105 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -2399,11 +2399,11 @@ LifetimeParam::as_string () const
 {
   std::string str ("LifetimeParam: ");
 
-  str += "\n Outer attribute: ";
+  str += "\n Outer attribute:";
   if (!has_outer_attribute ())
     str += "none";
-  else
-    str += outer_attr.as_string ();
+  for (auto &attr : outer_attrs)
+    str += " " + attr.as_string ();
 
   str += "\n Lifetime: " + lifetime.as_string ();
 
@@ -2495,11 +2495,11 @@ TypeParam::as_string () const
 {
   std::string str ("TypeParam: ");
 
-  str += "\n Outer attribute: ";
+  str += "\n Outer attribute:";
   if (!has_outer_attribute ())
     str += "none";
-  else
-    str += outer_attr.as_string ();
+  for (auto &attr : outer_attrs)
+    str += " " + attr.as_string ();
 
   str += "\n Identifier: " + type_representation.as_string ();
 
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 0693b10bd2ee..ee1c58c8fd66 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1602,7 +1602,7 @@ class LifetimeParam : public GenericParam
 {
   Lifetime lifetime;
   std::vector<Lifetime> lifetime_bounds;
-  Attribute outer_attr;
+  AST::AttrVec outer_attrs;
   location_t locus;
 
 public:
@@ -1610,7 +1610,7 @@ public:
 
   Lifetime &get_lifetime () { return lifetime; }
 
-  Attribute &get_outer_attribute () { return outer_attr; }
+  AST::AttrVec &get_outer_attrs () { return outer_attrs; }
 
   // Returns whether the lifetime param has any lifetime bounds.
   bool has_lifetime_bounds () const { return !lifetime_bounds.empty (); }
@@ -1618,13 +1618,12 @@ public:
   std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
 
   // Returns whether the lifetime param has an outer attribute.
-  bool has_outer_attribute () const { return !outer_attr.is_empty (); }
+  bool has_outer_attribute () const { return !outer_attrs.empty (); }
 
   // Creates an error state lifetime param.
   static LifetimeParam create_error ()
   {
-    return LifetimeParam (Lifetime::error (), {}, Attribute::create_empty (),
-                         UNDEF_LOCATION);
+    return LifetimeParam (Lifetime::error (), {}, {}, UNDEF_LOCATION);
   }
 
   // Returns whether the lifetime param is in an error state.
@@ -1632,10 +1631,10 @@ public:
 
   // Constructor
   LifetimeParam (Lifetime lifetime, std::vector<Lifetime> lifetime_bounds,
-                Attribute outer_attr, location_t locus)
+                AST::AttrVec outer_attrs, location_t locus)
     : lifetime (std::move (lifetime)),
       lifetime_bounds (std::move (lifetime_bounds)),
-      outer_attr (std::move (outer_attr)), locus (locus)
+      outer_attrs (std::move (outer_attrs)), locus (locus)
   {}
 
   std::string as_string () const override;
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index d11835a147cd..bd9113ffa237 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -53,7 +53,7 @@ class TypeParam : public GenericParam
 {
   // bool has_outer_attribute;
   // std::unique_ptr<Attribute> outer_attr;
-  Attribute outer_attr;
+  AST::AttrVec outer_attrs;
 
   Identifier type_representation;
 
@@ -77,17 +77,17 @@ public:
   bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
 
   // Returns whether the type param has an outer attribute.
-  bool has_outer_attribute () const { return !outer_attr.is_empty (); }
+  bool has_outer_attribute () const { return !outer_attrs.empty (); }
 
-  Attribute &get_outer_attribute () { return outer_attr; }
+  AST::AttrVec &get_outer_attrs () { return outer_attrs; }
 
   TypeParam (Identifier type_representation, location_t locus = UNDEF_LOCATION,
             std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
             = std::vector<std::unique_ptr<TypeParamBound>> (),
             std::unique_ptr<Type> type = nullptr,
-            Attribute outer_attr = Attribute::create_empty ())
+            AST::AttrVec outer_attrs = {})
     : GenericParam (Analysis::Mappings::get ().get_next_node_id ()),
-      outer_attr (std::move (outer_attr)),
+      outer_attrs (std::move (outer_attrs)),
       type_representation (std::move (type_representation)),
       type_param_bounds (std::move (type_param_bounds)),
       type (std::move (type)), locus (locus)
@@ -95,7 +95,7 @@ public:
 
   // Copy constructor uses clone
   TypeParam (TypeParam const &other)
-    : GenericParam (other.node_id), outer_attr (other.outer_attr),
+    : GenericParam (other.node_id), outer_attrs (other.outer_attrs),
       type_representation (other.type_representation), locus (other.locus)
   {
     // guard to prevent null pointer dereference
@@ -111,7 +111,7 @@ public:
   TypeParam &operator= (TypeParam const &other)
   {
     type_representation = other.type_representation;
-    outer_attr = other.outer_attr;
+    outer_attrs = other.outer_attrs;
     locus = other.locus;
     node_id = other.node_id;
 
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index 60eec59b72e7..b20f31cc4adf 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -347,21 +347,21 @@ class ConstGenericParam : public GenericParam
    */
   GenericArg default_value;
 
-  Attribute outer_attr;
+  AST::AttrVec outer_attrs;
   location_t locus;
 
 public:
   ConstGenericParam (Identifier name, std::unique_ptr<AST::Type> type,
-                    GenericArg default_value, Attribute outer_attr,
+                    GenericArg default_value, AST::AttrVec outer_attrs,
                     location_t locus)
     : name (name), type (std::move (type)),
-      default_value (std::move (default_value)), outer_attr (outer_attr),
+      default_value (std::move (default_value)), outer_attrs (outer_attrs),
       locus (locus)
   {}
 
   ConstGenericParam (const ConstGenericParam &other)
     : GenericParam (), name (other.name), type (other.type->clone_type ()),
-      default_value (other.default_value), outer_attr (other.outer_attr),
+      default_value (other.default_value), outer_attrs (other.outer_attrs),
       locus (other.locus)
   {}
 
@@ -370,7 +370,7 @@ public:
 
   const Identifier &get_name () const { return name; }
 
-  Attribute &get_outer_attribute () { return outer_attr; }
+  AST::AttrVec &get_outer_attrs () { return outer_attrs; }
 
   AST::Type &get_type ()
   {
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 2c6b3d80cb25..6eb195519805 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -3093,7 +3093,7 @@ template <typename EndTokenPred>
 std::unique_ptr<AST::GenericParam>
 Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
 {
-  auto outer_attrs = parse_outer_attribute ();
+  auto outer_attrs = parse_outer_attributes ();
   std::unique_ptr<AST::GenericParam> param;
   auto token = lexer.peek_token ();
 
@@ -3460,8 +3460,8 @@ template <typename ManagedTokenSource>
 AST::LifetimeParam
 Parser<ManagedTokenSource>::parse_lifetime_param ()
 {
-  // parse outer attribute, which is optional and may not exist
-  AST::Attribute outer_attr = parse_outer_attribute ();
+  // parse outer attributes, which are optional and may not exist
+  auto outer_attrs = parse_outer_attributes ();
 
   // save lifetime token - required
   const_TokenPtr lifetime_tok = lexer.peek_token ();
@@ -3484,7 +3484,7 @@ Parser<ManagedTokenSource>::parse_lifetime_param ()
     }
 
   return AST::LifetimeParam (std::move (lifetime), std::move (lifetime_bounds),
-                            std::move (outer_attr),
+                            std::move (outer_attrs),
                             lifetime_tok->get_locus ());
 }
 
@@ -3561,8 +3561,8 @@ template <typename ManagedTokenSource>
 std::unique_ptr<AST::TypeParam>
 Parser<ManagedTokenSource>::parse_type_param ()
 {
-  // parse outer attribute, which is optional and may not exist
-  AST::Attribute outer_attr = parse_outer_attribute ();
+  // parse outer attributes, which are optional and may not exist
+  auto outer_attrs = parse_outer_attributes ();
 
   const_TokenPtr identifier_tok = lexer.peek_token ();
   if (identifier_tok->get_id () != IDENTIFIER)
@@ -3605,7 +3605,7 @@ Parser<ManagedTokenSource>::parse_type_param ()
   return std::unique_ptr<AST::TypeParam> (
     new AST::TypeParam (std::move (ident), identifier_tok->get_locus (),
                        std::move (type_param_bounds), std::move (type),
-                       std::move (outer_attr)));
+                       std::move (outer_attrs)));
 }
 
 /* Parses regular (i.e. non-generic) parameters in functions or methods. Also

Reply via email to