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 <pierre-emmanuel.patry@embecosm.com>
This commit is contained in:
Pierre-Emmanuel Patry
2023-04-11 16:24:09 +02:00
committed by Arthur Cohen
parent a79920c2d6
commit aae0830fbf
2 changed files with 243 additions and 13 deletions

View File

@@ -25,7 +25,6 @@
#include <cstdlib>
namespace Literal {
extern "C" {
void
@@ -55,23 +54,13 @@ Literal__drop (Literal *lit)
Literal
Literal__string (const unsigned char *str, std::uint64_t len)
{
unsigned char *data = new unsigned char[len];
StringPayload str_payload = {data, len};
std::memcpy (data, str, len);
LiteralPayload payload;
payload.string_payload = str_payload;
return {STRING, payload};
return Literal::make_string (str, len);
}
Literal
Literal__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};
std::memcpy (data, bytes, len);
LiteralPayload payload;
payload.byte_string_payload = bstr_payload;
return {BYTE_STRING, payload};
return Literal::make_byte_string (bytes, len);
}
bool
@@ -82,4 +71,214 @@ Literal__from_string (const unsigned char *str, std::uint64_t len, Literal *lit)
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};
std::memcpy (data, str, len);
LiteralPayload payload;
payload.string_payload = str_payload;
return {STRING, payload};
}
Literal
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};
std::memcpy (data, bytes, len);
LiteralPayload payload;
payload.byte_string_payload = bstr_payload;
return {BYTE_STRING, payload};
}
Literal
Literal::make_f32 (float value, bool suffixed)
{
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

View File

@@ -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" {