diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2024-05-20 12:00:11 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-03-17 16:35:31 +0100 |
commit | 6fef5bc497572798b817ad7afdcf5e5fb00c7910 (patch) | |
tree | 4a452e7f1fb4d191b360c9c3582b62747572e7b3 /gcc/rust/ast | |
parent | f092edae96511e92b9d458184b23848f95138ab2 (diff) | |
download | gcc-6fef5bc497572798b817ad7afdcf5e5fb00c7910.zip gcc-6fef5bc497572798b817ad7afdcf5e5fb00c7910.tar.gz gcc-6fef5bc497572798b817ad7afdcf5e5fb00c7910.tar.bz2 |
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.patry@embecosm.com>
Diffstat (limited to 'gcc/rust/ast')
-rw-r--r-- | gcc/rust/ast/rust-ast-collector.cc | 3 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-visitor.cc | 6 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast.cc | 12 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast.h | 13 | ||||
-rw-r--r-- | gcc/rust/ast/rust-item.h | 14 | ||||
-rw-r--r-- | gcc/rust/ast/rust-path.h | 10 |
6 files changed, 30 insertions, 28 deletions
diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index a7f6ed5..a757225 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 ¶m) // 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 ¶m) // 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 ba3eb67..2c1674e 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 ¶m) { - 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 bc89692..8045a68 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 0693b10..ee1c58c 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 d11835a..bd9113f 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 60eec59..b20f31c 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 () { |