public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] libproc_macro: Add named constructor
@ 2023-05-02 7:08 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-05-02 7:08 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:3fb63250eafe750e6af31cbdea66902c5f7e9adb
commit 3fb63250eafe750e6af31cbdea66902c5f7e9adb
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date: Tue Apr 11 15:07:03 2023 +0200
libproc_macro: Add named constructor
Add named constructor directly in Ident struct.
ChangeLog:
* libgrust/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.
* libgrust/libproc_macro/ident.h (struct Ident): Add named
constructor prototypes.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diff:
---
libgrust/libproc_macro/ident.cc | 36 +++++++++++++++++++++++++++---------
libgrust/libproc_macro/ident.h | 6 ++++++
2 files changed, 33 insertions(+), 9 deletions(-)
diff --git a/libgrust/libproc_macro/ident.cc b/libgrust/libproc_macro/ident.cc
index 6ea70dc0823..6ad9e7807f1 100644
--- a/libgrust/libproc_macro/ident.cc
+++ b/libgrust/libproc_macro/ident.cc
@@ -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};
}
diff --git a/libgrust/libproc_macro/ident.h b/libgrust/libproc_macro/ident.h
index 0a4b4f88bad..8ab7e6dc324 100644
--- a/libgrust/libproc_macro/ident.h
+++ b/libgrust/libproc_macro/ident.h
@@ -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" {
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-05-02 7:08 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-02 7:08 [gcc/devel/rust/master] libproc_macro: Add named constructor Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).