]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Make ConstantItem use Identifier
authorOwen Avery <powerboat9.gamer@gmail.com>
Wed, 4 Jun 2025 05:06:06 +0000 (01:06 -0400)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:44 +0000 (16:36 +0200)
The change to ASTLoweringExternItem is necessary, since with this patch
Identifier can be implicitly converted to std::string.

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Handle
changed type of ConstantItem::identifier.
* ast/rust-ast.cc (ConstantItem::as_string): Likewise.
* ast/rust-ast.h (operator const std::string &): New member
function.
* ast/rust-item.h (ConstantItem::identifier): Change type from
std::string to Identifier.
(ConstantItem::ConstantItem): Handle changed type of identifier
field.
(ConstantItem::is_unnamed): Likewise.
(ConstantItem::get_identifier): Likewise.
* hir/rust-ast-lower-extern.h (ASTLoweringExternItem::visit):
Avoid discarding location of wildcard patterns.
* lex/rust-token.cc: Include "rust-ast.h".
(Token::make_identifier): Add overload accepting an Identifier
instance.
* lex/rust-token.h (class Identifier): Add forward declaration
in order to...
(Token::make_identifier): ...declare an overload for this static
member function.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
gcc/rust/ast/rust-ast-collector.cc
gcc/rust/ast/rust-ast.cc
gcc/rust/ast/rust-ast.h
gcc/rust/ast/rust-item.h
gcc/rust/hir/rust-ast-lower-extern.h
gcc/rust/lex/rust-token.cc
gcc/rust/lex/rust-token.h

index d6b6a495d110cea147919ae2e605bdab39b4341f..c6ec0e7bb9be942b63f5da1c7cc640f4dd31122a 100644 (file)
@@ -2077,8 +2077,7 @@ TokenCollector::visit (ConstantItem &item)
     }
   else
     {
-      auto id = item.get_identifier ();
-      push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
+      push (Rust::Token::make_identifier (item.get_identifier ()));
     }
   push (Rust::Token::make (COLON, UNDEF_LOCATION));
   visit (item.get_type ());
index fdd86679ba74bb9c2fa89b4138b6c38cc9db4a5b..916829fe95c05a9d9538303690cdae1aaf1f6f98 100644 (file)
@@ -624,7 +624,7 @@ ConstantItem::as_string () const
 {
   std::string str = VisItem::as_string ();
 
-  str += "const " + identifier;
+  str += "const " + identifier.as_string ();
 
   // DEBUG: null pointer check
   if (type == nullptr)
index 90d2104eb85b5972ee0f993cf7641b4a9db12323..cd586c6aa7d3907df6ae8570ce3c50b5c482dc13 100644 (file)
@@ -62,6 +62,8 @@ public:
     return ident == other.ident;
   }
 
+  operator const std::string & () const { return ident; }
+
 private:
   std::string ident;
   location_t loc;
index 247a65f39c2ec106976abac2ae91f9120b996c38..d11eed7687b5914cd0359196292386db4bbfe26d 100644 (file)
@@ -2450,7 +2450,7 @@ class ConstantItem : public VisItem, public AssociatedItem
   // either has an identifier or "_" - maybe handle in identifier?
   // bool identifier_is_underscore;
   // if no identifier declared, identifier will be "_"
-  std::string identifier;
+  Identifier identifier;
 
   std::unique_ptr<Type> type;
   std::unique_ptr<Expr> const_expr;
@@ -2460,7 +2460,7 @@ class ConstantItem : public VisItem, public AssociatedItem
 public:
   std::string as_string () const override;
 
-  ConstantItem (std::string ident, Visibility vis, std::unique_ptr<Type> type,
+  ConstantItem (Identifier ident, Visibility vis, std::unique_ptr<Type> type,
                std::unique_ptr<Expr> const_expr,
                std::vector<Attribute> outer_attrs, location_t locus)
     : VisItem (std::move (vis), std::move (outer_attrs)),
@@ -2468,7 +2468,7 @@ public:
       const_expr (std::move (const_expr)), locus (locus)
   {}
 
-  ConstantItem (std::string ident, Visibility vis, std::unique_ptr<Type> type,
+  ConstantItem (Identifier ident, Visibility vis, std::unique_ptr<Type> type,
                std::vector<Attribute> outer_attrs, location_t locus)
     : VisItem (std::move (vis), std::move (outer_attrs)),
       identifier (std::move (ident)), type (std::move (type)),
@@ -2511,7 +2511,7 @@ public:
 
   /* Returns whether constant item is an "unnamed" (wildcard underscore used
    * as identifier) constant. */
-  bool is_unnamed () const { return identifier == "_"; }
+  bool is_unnamed () const { return identifier.as_string () == "_"; }
 
   location_t get_locus () const override final { return locus; }
 
@@ -2556,7 +2556,7 @@ public:
     return type;
   }
 
-  std::string get_identifier () const { return identifier; }
+  const Identifier &get_identifier () const { return identifier; }
 
   Item::Kind get_item_kind () const override
   {
index 0105e3840284cf7a1f853555e2c7a693093d3b84..3dca1b62666f95b7bc27a15f71eddb3ce61aef88 100644 (file)
@@ -99,7 +99,7 @@ public:
          = static_cast<AST::IdentifierPattern &> (param.get_pattern ());
        Identifier param_name = param_kind == AST::Pattern::Kind::Identifier
                                  ? param_ident.get_ident ()
-                                 : std::string ("_");
+                                 : Identifier ("_", param.get_locus ());
 
        HIR::Type *param_type = ASTLoweringType::translate (param.get_type ());
 
index 783638b4171758f14cbe7e270228e878c003c9a1..c396e100dd8dadfaf3a4f6b723ecfc9fdb3e2e7d 100644 (file)
@@ -20,6 +20,7 @@
 #include "rust-token.h"
 #include "rust-diagnostics.h"
 #include "rust-unicode.h"
+#include "rust-ast.h"
 
 namespace Rust {
 // Hackily defined way to get token description for enum value using x-macros
@@ -235,6 +236,13 @@ escape_special_chars (const std::string &source, Context ctx)
 
 } // namespace
 
+TokenPtr
+Token::make_identifier (const Identifier &ident)
+{
+  std::string str = ident;
+  return make_identifier (ident.get_locus (), std::move (str));
+}
+
 std::string
 Token::as_string () const
 {
index 2abdf27f6c3bea05d7332d232559ab5fff17cd64..2021aec4e4ca6b34d2b750aa031c4a3e7ba29368 100644 (file)
 #include "rust-unicode.h"
 
 namespace Rust {
+
+// used by Rust::Token::make_identifier
+class Identifier;
+
 // "Primitive core types" in Rust - the different int and float types, as well
 // as some others
 enum PrimitiveCoreType
@@ -324,6 +328,8 @@ public:
     return TokenPtr (new Token (IDENTIFIER, locus, std::move (str)));
   }
 
+  static TokenPtr make_identifier (const Identifier &ident);
+
   // Makes and returns a new TokenPtr of type INT_LITERAL.
   static TokenPtr make_int (location_t locus, std::string &&str,
                            PrimitiveCoreType type_hint = CORETYPE_UNKNOWN)