public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] libproc_macro: Add member functions to Literal
@ 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:4f7461b1fdbc0325f1ad17d71d678f6f27d15d16

commit 4f7461b1fdbc0325f1ad17d71d678f6f27d15d16
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Tue Apr 11 16:24:09 2023 +0200

    libproc_macro: Add member functions to Literal
    
    Add some member functions to the Literal structure as well as named
    constructors.
    
    ChangeLog:
    
            * libgrust/libproc_macro/literal.cc (Literal__string): Add call
            to named constructor.
            (Literal__byte_string): Likewise.
            (Literal::make_unsigned): Add function.
            (Literal::make_signed): Add function.
            (Literal::clone): Likewise.
            (Literal::make_u8): Likewise.
            (Literal::make_u16): Likewise.
            (Literal::make_u32): Likewise.
            (Literal::make_u64): Likewise.
            (Literal::make_i8): Likewise.
            (Literal::make_i16): Likewise.
            (Literal::make_i32): Likewise.
            (Literal::make_i64): Likewise.
            (Literal::make_string): Likewise.
            (Literal::make_byte_string): Likewise.
            (Literal::make_f32): Likewise.
            (Literal::make_f64): Likewise.
            (make_char): Likewise.
            (Literal::make_char): Likewise.
            (make_usize): Likewise.
            (Literal::make_usize): Likewise.
            (make_isize): Likewise.
            (Literal::make_isize): Likewise.
            * libgrust/libproc_macro/literal.h: Add prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 libgrust/libproc_macro/literal.cc | 213 ++++++++++++++++++++++++++++++++++++--
 libgrust/libproc_macro/literal.h  |  31 ++++++
 2 files changed, 237 insertions(+), 7 deletions(-)

diff --git a/libgrust/libproc_macro/literal.cc b/libgrust/libproc_macro/literal.cc
index 57709d59b37..aad7090d7ab 100644
--- a/libgrust/libproc_macro/literal.cc
+++ b/libgrust/libproc_macro/literal.cc
@@ -25,7 +25,6 @@
 #include <cstdlib>
 
 namespace Literal {
-
 extern "C" {
 
 void
@@ -54,6 +53,164 @@ Literal__drop (Literal *lit)
 
 Literal
 Literal__string (const unsigned char *str, std::uint64_t len)
+{
+  return Literal::make_string (str, len);
+}
+
+Literal
+Literal__byte_string (const std::uint8_t *bytes, std::uint64_t len)
+{
+  return Literal::make_byte_string (bytes, len);
+}
+
+bool
+Literal__from_string (const unsigned char *str, std::uint64_t len, Literal *lit)
+{
+  // FIXME: implement this function with parser
+  std::abort ();
+  return false;
+}
+}
+
+Literal
+Literal::make_unsigned (UnsignedSuffixPayload p)
+{
+  LiteralPayload payload;
+  payload.unsigned_payload = p;
+  return {UNSIGNED, payload};
+}
+
+Literal
+Literal::make_signed (SignedSuffixPayload p)
+{
+  LiteralPayload payload;
+  payload.signed_payload = p;
+  return {SIGNED, payload};
+}
+
+Literal
+Literal::clone () const
+{
+  Literal lit = *this;
+  switch (this->tag)
+    {
+    case STRING:
+      lit.payload.string_payload.data
+	= new unsigned char[lit.payload.string_payload.len];
+      std::memcpy (lit.payload.string_payload.data,
+		   this->payload.string_payload.data,
+		   lit.payload.string_payload.len);
+      break;
+    case BYTE_STRING:
+      lit.payload.byte_string_payload.data
+	= new uint8_t[lit.payload.byte_string_payload.size];
+      std::memcpy (lit.payload.byte_string_payload.data,
+		   this->payload.byte_string_payload.data,
+		   lit.payload.byte_string_payload.size);
+      break;
+    default:
+      break;
+    }
+  return lit;
+}
+
+Literal
+Literal::make_u8 (std::uint8_t value, bool suffixed)
+{
+  UnsignedPayload unsigned_payload;
+  unsigned_payload.unsigned8 = value;
+  Unsigned val{UNSIGNED_8, unsigned_payload};
+  UnsignedSuffixPayload payload{val, suffixed};
+
+  return make_unsigned (payload);
+}
+
+Literal
+Literal::make_u16 (std::uint16_t value, bool suffixed)
+{
+  UnsignedPayload unsigned_payload;
+  unsigned_payload.unsigned16 = value;
+  Unsigned val{UNSIGNED_16, unsigned_payload};
+  UnsignedSuffixPayload payload{val, suffixed};
+
+  return make_unsigned (payload);
+}
+
+Literal
+Literal::make_u32 (std::uint32_t value, bool suffixed)
+{
+  UnsignedPayload unsigned_payload;
+  unsigned_payload.unsigned32 = value;
+  Unsigned val{UNSIGNED_32, unsigned_payload};
+  UnsignedSuffixPayload payload{val, suffixed};
+
+  return make_unsigned (payload);
+}
+
+Literal
+Literal::make_u64 (std::uint64_t value, bool suffixed)
+{
+  UnsignedPayload unsigned_payload;
+  unsigned_payload.unsigned64 = value;
+  Unsigned val{UNSIGNED_64, unsigned_payload};
+  UnsignedSuffixPayload payload{val, suffixed};
+
+  return make_unsigned (payload);
+}
+
+Literal
+Literal::make_i8 (std::int8_t value, bool suffixed)
+{
+  SignedPayload signed_payload;
+  signed_payload.signed8 = value;
+  Signed val{SIGNED_8, signed_payload};
+  SignedSuffixPayload payload{val, suffixed};
+
+  return make_signed (payload);
+}
+
+Literal
+Literal::make_i16 (std::int16_t value, bool suffixed)
+{
+  SignedPayload signed_payload;
+  signed_payload.signed16 = value;
+  Signed val{SIGNED_16, signed_payload};
+  SignedSuffixPayload payload{val, suffixed};
+
+  return make_signed (payload);
+}
+
+Literal
+Literal::make_i32 (std::int32_t value, bool suffixed)
+{
+  SignedPayload signed_payload;
+  signed_payload.signed32 = value;
+  Signed val{SIGNED_32, signed_payload};
+  SignedSuffixPayload payload = {val, suffixed};
+
+  return make_signed (payload);
+}
+
+Literal
+Literal::make_i64 (std::int64_t value, bool suffixed)
+{
+  SignedPayload signed_payload;
+  signed_payload.signed64 = value;
+  Signed val{SIGNED_64, signed_payload};
+  SignedSuffixPayload payload{val, suffixed};
+
+  return make_signed (payload);
+}
+
+Literal
+Literal::make_string (const std::string &str)
+{
+  return make_string (reinterpret_cast<const unsigned char *> (str.c_str ()),
+		      str.length ());
+}
+
+Literal
+Literal::make_string (const unsigned char *str, std::uint64_t len)
 {
   unsigned char *data = new unsigned char[len];
   StringPayload str_payload = {data, len};
@@ -64,7 +221,13 @@ Literal__string (const unsigned char *str, std::uint64_t len)
 }
 
 Literal
-Literal__byte_string (const std::uint8_t *bytes, std::uint64_t len)
+Literal::make_byte_string (const std::vector<std::uint8_t> &vec)
+{
+  return make_byte_string (vec.data (), vec.size ());
+}
+
+Literal
+Literal::make_byte_string (const std::uint8_t *bytes, std::uint64_t len)
 {
   std::uint8_t *data = new std::uint8_t[len];
   ByteStringPayload bstr_payload = {data, len};
@@ -74,12 +237,48 @@ Literal__byte_string (const std::uint8_t *bytes, std::uint64_t len)
   return {BYTE_STRING, payload};
 }
 
-bool
-Literal__from_string (const unsigned char *str, std::uint64_t len, Literal *lit)
+Literal
+Literal::make_f32 (float value, bool suffixed)
 {
-  // FIXME: implement this function with parser
-  std::abort ();
-  return false;
+  Float32Payload f{value, suffixed};
+  LiteralPayload payload;
+  payload.float32_payload = f;
+  return {FLOAT32, payload};
 }
+
+Literal
+Literal::make_f64 (double value, bool suffixed)
+{
+  Float64Payload f{value, suffixed};
+  LiteralPayload payload;
+  payload.float64_payload = f;
+  return {FLOAT64, payload};
 }
+
+Literal
+Literal::make_char (std::uint32_t ch)
+{
+  LiteralPayload payload;
+  payload.char_payload = ch;
+  return {CHAR, payload};
+}
+
+Literal
+Literal::make_usize (std::uint64_t value, bool suffixed)
+{
+  UsizePayload p{value, suffixed};
+  LiteralPayload payload;
+  payload.usize_payload = p;
+  return {USIZE, payload};
+}
+
+Literal
+Literal::make_isize (std::int64_t value, bool suffixed)
+{
+  IsizePayload p{value, suffixed};
+  LiteralPayload payload;
+  payload.isize_payload = p;
+  return {ISIZE, payload};
+}
+
 } // namespace Literal
diff --git a/libgrust/libproc_macro/literal.h b/libgrust/libproc_macro/literal.h
index 6ae707b5b5e..b7894222105 100644
--- a/libgrust/libproc_macro/literal.h
+++ b/libgrust/libproc_macro/literal.h
@@ -24,6 +24,8 @@
 #define LITERAL_H
 
 #include <cstdint>
+#include <string>
+#include <vector>
 
 namespace Literal {
 enum UnsignedTag
@@ -157,6 +159,35 @@ struct Literal
 {
   LiteralTag tag;
   LiteralPayload payload;
+
+public:
+  Literal clone () const;
+
+  static Literal make_u8 (std::uint8_t value, bool suffixed = false);
+  static Literal make_u16 (std::uint16_t value, bool suffixed = false);
+  static Literal make_u32 (std::uint32_t value, bool suffixed = false);
+  static Literal make_u64 (std::uint64_t value, bool suffixed = false);
+
+  static Literal make_i8 (std::int8_t value, bool suffixed = false);
+  static Literal make_i16 (std::int16_t value, bool suffixed = false);
+  static Literal make_i32 (std::int32_t value, bool suffixed = false);
+  static Literal make_i64 (std::int64_t value, bool suffixed = false);
+
+  static Literal make_string (const std::string &str);
+  static Literal make_string (const unsigned char *str, std::uint64_t len);
+  static Literal make_byte_string (const std::vector<std::uint8_t> &vec);
+  static Literal make_byte_string (const std::uint8_t *bytes,
+				   std::uint64_t len);
+
+  static Literal make_f32 (float value, bool suffixed = false);
+  static Literal make_f64 (double value, bool suffixed = false);
+
+  static Literal make_char (std::uint32_t ch);
+  static Literal make_usize (std::uint64_t value, bool suffixed = false);
+  static Literal make_isize (std::int64_t value, bool suffixed = false);
+
+  static Literal make_unsigned (UnsignedSuffixPayload p);
+  static Literal make_signed (SignedSuffixPayload p);
 };
 
 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 member functions to Literal 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).