From: Pierre-Emmanuel Patry Date: Tue, 11 Apr 2023 14:24:09 +0000 (+0200) Subject: gccrs: libproc_macro: Add member functions to Literal X-Git-Tag: basepoints/gcc-15~2647 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aae0830fbf1cfeb8f0014e15c2cd2cad54c31afe;p=thirdparty%2Fgcc.git gccrs: libproc_macro: Add member functions to Literal Add some member functions to the Literal structure as well as named constructors. libgrust/ChangeLog: * 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. * libproc_macro/literal.h: Add prototypes. Signed-off-by: Pierre-Emmanuel Patry --- diff --git a/libgrust/libproc_macro/literal.cc b/libgrust/libproc_macro/literal.cc index 57709d59b374..aad7090d7ab3 100644 --- a/libgrust/libproc_macro/literal.cc +++ b/libgrust/libproc_macro/literal.cc @@ -25,7 +25,6 @@ #include 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 (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 &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 6ae707b5b5ef..b78942221053 100644 --- a/libgrust/libproc_macro/literal.h +++ b/libgrust/libproc_macro/literal.h @@ -24,6 +24,8 @@ #define LITERAL_H #include +#include +#include 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 &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" {