]> gcc.gnu.org Git - gcc.git/commitdiff
gccrs: libproc_macro: Add named constructor
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Tue, 11 Apr 2023 13:07:03 +0000 (15:07 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:34:11 +0000 (18:34 +0100)
Add named constructor directly in Ident struct.

libgrust/ChangeLog:

* libproc_macro/ident.cc (Ident__new): Use named
constructor.
(Ident__new_raw): Use named constructor.
(Ident__clone): Use clone member function.
(Ident::clone): Make clone const.
(Ident::make_ident): Add named construcot.
* libproc_macro/ident.h (struct Ident): Add named
constructor prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
libgrust/libproc_macro/ident.cc
libgrust/libproc_macro/ident.h

index 6ea70dc0823fb44c32fd5d39106920fb0f5435f8..6ad9e7807f10abc7fa515e7e24000dbfb216d974 100644 (file)
@@ -27,17 +27,13 @@ extern "C" {
 Ident
 Ident__new (unsigned char *str, std::uint64_t len)
 {
-  unsigned char *val = new unsigned char[len];
-  std::memcpy (val, str, len);
-  return {false, val, len};
+  return Ident::make_ident (str, len);
 }
 
 Ident
 Ident__new_raw (unsigned char *str, std::uint64_t len)
 {
-  unsigned char *val = new unsigned char[len];
-  std::memcpy (val, str, len);
-  return {true, val, len};
+  return Ident::make_ident (str, len, true);
 }
 
 void
@@ -49,8 +45,30 @@ Ident__drop (Ident *ident)
 Ident
 Ident__clone (const Ident *ident)
 {
-  unsigned char *val = new unsigned char[ident->len];
-  std::memcpy (val, ident->val, ident->len);
-  return {ident->is_raw, val, ident->len};
+  return ident->clone ();
+}
+}
+
+Ident
+Ident::clone () const
+{
+  unsigned char *val = new unsigned char[this->len];
+  std::memcpy (val, this->val, this->len);
+  return {this->is_raw, val, this->len};
 }
+
+Ident
+Ident::make_ident (std::string str, bool raw)
+{
+  return Ident::make_ident (reinterpret_cast<const unsigned char *> (
+                             str.c_str ()),
+                           str.length (), raw);
+}
+
+Ident
+Ident::make_ident (const unsigned char *str, std::uint64_t len, bool raw)
+{
+  unsigned char *val = new unsigned char[len];
+  std::memcpy (val, str, len);
+  return {raw, val, len};
 }
index 0a4b4f88bad9bee610ddc41d5e790548a61c074e..8ab7e6dc3242b2c7380a8b462e27fa538e3c5526 100644 (file)
@@ -33,6 +33,12 @@ struct Ident
   unsigned char *val;
   // Length in bytes
   std::uint64_t len;
+
+public:
+  Ident clone () const;
+  static Ident make_ident (std::string str, bool raw = false);
+  static Ident make_ident (const unsigned char *str, std::uint64_t len,
+                          bool raw = false);
 };
 
 extern "C" {
This page took 0.059919 seconds and 5 git commands to generate.