From: Owen Avery <powerboat9.ga...@gmail.com>

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.ga...@gmail.com>
---
 gcc/rust/ast/rust-ast-collector.cc   |  3 +--
 gcc/rust/ast/rust-ast.cc             |  2 +-
 gcc/rust/ast/rust-ast.h              |  2 ++
 gcc/rust/ast/rust-item.h             | 10 +++++-----
 gcc/rust/hir/rust-ast-lower-extern.h |  2 +-
 gcc/rust/lex/rust-token.cc           |  8 ++++++++
 gcc/rust/lex/rust-token.h            |  6 ++++++
 7 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index d6b6a495d11..c6ec0e7bb9b 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -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 ());
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index fdd86679ba7..916829fe95c 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -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)
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 90d2104eb85..cd586c6aa7d 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -62,6 +62,8 @@ public:
     return ident == other.ident;
   }
 
+  operator const std::string & () const { return ident; }
+
 private:
   std::string ident;
   location_t loc;
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 247a65f39c2..d11eed7687b 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -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
   {
diff --git a/gcc/rust/hir/rust-ast-lower-extern.h 
b/gcc/rust/hir/rust-ast-lower-extern.h
index 0105e384028..3dca1b62666 100644
--- a/gcc/rust/hir/rust-ast-lower-extern.h
+++ b/gcc/rust/hir/rust-ast-lower-extern.h
@@ -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 ());
 
diff --git a/gcc/rust/lex/rust-token.cc b/gcc/rust/lex/rust-token.cc
index 783638b4171..c396e100dd8 100644
--- a/gcc/rust/lex/rust-token.cc
+++ b/gcc/rust/lex/rust-token.cc
@@ -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
 {
diff --git a/gcc/rust/lex/rust-token.h b/gcc/rust/lex/rust-token.h
index 2abdf27f6c3..2021aec4e4c 100644
--- a/gcc/rust/lex/rust-token.h
+++ b/gcc/rust/lex/rust-token.h
@@ -24,6 +24,10 @@
 #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)
-- 
2.49.0

Reply via email to