aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-02-16 16:41:13 +0100
committerPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-03-30 13:52:30 +0200
commit20578ae77461ad57abfa29823213e29bfa51e670 (patch)
treece9ca943978c9cee5d2df10c5bdc7992972a3704
parent6a12e6ea0e6a8575e9c21a4e68d9b636ed157a2f (diff)
downloadgcc-20578ae77461ad57abfa29823213e29bfa51e670.zip
gcc-20578ae77461ad57abfa29823213e29bfa51e670.tar.gz
gcc-20578ae77461ad57abfa29823213e29bfa51e670.tar.bz2
libproc_macro: Add literal internal implementation
Add internal implementation for the rust Literal type in libproc_macro rust interface. ChangeLog: * librust/proc_macro/rust/bridge.rs: Add internal literal module. * librust/proc_macro/rust/literal.rs: Add link to internal implementation. * librust/proc_macro/rust/bridge/literal.rs: Add internal implementation. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r--librust/proc_macro/rust/bridge.rs1
-rw-r--r--librust/proc_macro/rust/bridge/literal.rs212
-rw-r--r--librust/proc_macro/rust/literal.rs136
3 files changed, 281 insertions, 68 deletions
diff --git a/librust/proc_macro/rust/bridge.rs b/librust/proc_macro/rust/bridge.rs
index b3b28c0..eb49071 100644
--- a/librust/proc_macro/rust/bridge.rs
+++ b/librust/proc_macro/rust/bridge.rs
@@ -1,3 +1,4 @@
pub mod ident;
+pub mod literal;
pub mod punct;
pub mod span;
diff --git a/librust/proc_macro/rust/bridge/literal.rs b/librust/proc_macro/rust/bridge/literal.rs
new file mode 100644
index 0000000..f44fdbc
--- /dev/null
+++ b/librust/proc_macro/rust/bridge/literal.rs
@@ -0,0 +1,212 @@
+use bridge::span::Span;
+use std::convert::TryInto;
+use std::ffi::c_uchar;
+
+extern "C" {
+ fn Literal__drop(literal: *const Literal);
+ fn Literal__string(str: *const c_uchar) -> Literal;
+ fn Literal__byte_string(bytes: *const u8) -> Literal;
+}
+
+#[repr(C)]
+#[derive(Clone)]
+pub enum Unsigned {
+ Unsigned8(u8),
+ Unsigned16(u16),
+ Unsigned32(u32),
+ Unsigned64(u64),
+ // FIXME: 128 bits ffi is not safe for now
+ // https://github.com/rust-lang/rust/issues/54341
+ //
+ // Unsigned128(u128),
+}
+
+#[repr(C)]
+#[derive(Clone)]
+pub enum Signed {
+ Signed8(i8),
+ Signed16(i16),
+ Signed32(i32),
+ Signed64(i64),
+ // FIXME: 128 bits ffi is not safe for now
+ // https://github.com/rust-lang/rust/issues/54341
+ //
+ // Signed128(i128),
+}
+
+#[repr(C)]
+#[derive(Clone)]
+pub enum Literal {
+ /// String literal internal representation
+ ///
+ /// # Note
+ /// This variant is constructed through FFI
+ #[allow(dead_code)]
+ String {
+ data: *const c_uchar,
+ size: u64,
+ },
+ /// Bytestring literal internal representation
+ ///
+ /// # Note
+ /// This variant is constructed through FFI
+ #[allow(dead_code)]
+ ByteString {
+ data: *const u8,
+ size: u64,
+ },
+ Char(u32),
+ Unsigned(Unsigned, bool),
+ Signed(Signed, bool),
+ Usize(u64, bool),
+ ISize(i64, bool),
+ Float32(f32, bool),
+ Float64(f64, bool),
+}
+
+impl Literal {
+ pub fn u8_suffixed(n: u8) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned8(n), true)
+ }
+
+ pub fn u16_suffixed(n: u16) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned16(n), true)
+ }
+
+ pub fn u32_suffixed(n: u32) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned32(n), true)
+ }
+
+ pub fn u64_suffixed(n: u64) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned64(n), true)
+ }
+
+ pub fn u128_suffixed(_n: u128) -> Self {
+ todo!("Implement this function")
+ }
+
+ pub fn usize_suffixed(n: usize) -> Self {
+ Literal::Usize(n.try_into().expect("Cannot convert usize to u64"), true)
+ }
+
+ pub fn i8_suffixed(n: i8) -> Self {
+ Literal::Signed(Signed::Signed8(n), true)
+ }
+
+ pub fn i16_suffixed(n: i16) -> Self {
+ Literal::Signed(Signed::Signed16(n), true)
+ }
+
+ pub fn i32_suffixed(n: i32) -> Self {
+ Literal::Signed(Signed::Signed32(n), true)
+ }
+
+ pub fn i64_suffixed(n: i64) -> Self {
+ Literal::Signed(Signed::Signed64(n), true)
+ }
+
+ pub fn i128_suffixed(_n: i128) -> Self {
+ todo!("Implement this function")
+ }
+
+ pub fn isize_suffixed(n: isize) -> Self {
+ Literal::ISize(n.try_into().expect("Cannot convert isize to i64"), true)
+ }
+
+ // Unsuffixed
+
+ pub fn u8_unsuffixed(n: u8) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned8(n), false)
+ }
+
+ pub fn u16_unsuffixed(n: u16) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned16(n), false)
+ }
+
+ pub fn u32_unsuffixed(n: u32) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned32(n), false)
+ }
+
+ pub fn u64_unsuffixed(n: u64) -> Self {
+ Literal::Unsigned(Unsigned::Unsigned64(n), false)
+ }
+
+ pub fn u128_unsuffixed(_n: u128) -> Self {
+ todo!("Implement this function")
+ }
+
+ pub fn usize_unsuffixed(n: usize) -> Self {
+ Literal::Usize(n.try_into().expect("Cannot convert usize to u64"), false)
+ }
+
+ pub fn i8_unsuffixed(n: i8) -> Self {
+ Literal::Signed(Signed::Signed8(n), false)
+ }
+
+ pub fn i16_unsuffixed(n: i16) -> Self {
+ Literal::Signed(Signed::Signed16(n), false)
+ }
+
+ pub fn i32_unsuffixed(n: i32) -> Self {
+ Literal::Signed(Signed::Signed32(n), false)
+ }
+
+ pub fn i64_unsuffixed(n: i64) -> Self {
+ Literal::Signed(Signed::Signed64(n), false)
+ }
+
+ pub fn i128_unsuffixed(_n: i128) -> Self {
+ todo!("Implement this function")
+ }
+
+ pub fn isize_unsuffixed(n: isize) -> Self {
+ Literal::ISize(n.try_into().expect("Cannot convert isize to i64"), false)
+ }
+
+ pub fn f32_unsuffixed(n: f32) -> Self {
+ Literal::Float32(n, false)
+ }
+
+ pub fn f32_suffixed(n: f32) -> Self {
+ Literal::Float32(n, true)
+ }
+
+ pub fn f64_unsuffixed(n: f64) -> Self {
+ Literal::Float64(n, false)
+ }
+
+ pub fn f64_suffixed(n: f64) -> Self {
+ Literal::Float64(n, true)
+ }
+
+ pub fn string(string: &str) -> Self {
+ unsafe { Literal__string(string.as_ptr()) }
+ }
+
+ pub fn character(c: char) -> Self {
+ Literal::Char(c.into())
+ }
+
+ pub fn byte_string(bytes: &[u8]) -> Self {
+ unsafe { Literal__byte_string(bytes.as_ptr()) }
+ }
+
+ pub fn span(&self) -> Span {
+ Span {}
+ }
+
+ pub fn set_span(&mut self, span: Span) {
+ let _ = span;
+ }
+}
+
+impl Drop for Literal {
+ fn drop(&mut self) {
+ match self {
+ Literal::String { .. } | Literal::ByteString { .. } => unsafe {
+ Literal__drop(self as *const Literal)
+ },
+ _ => (),
+ }
+ }
+}
diff --git a/librust/proc_macro/rust/literal.rs b/librust/proc_macro/rust/literal.rs
index 93d9b76..2820da6 100644
--- a/librust/proc_macro/rust/literal.rs
+++ b/librust/proc_macro/rust/literal.rs
@@ -1,3 +1,4 @@
+use bridge;
use std::fmt;
use std::str::FromStr;
use LexError;
@@ -20,142 +21,141 @@ use Span;
/// Boolean literals like `true` and `false` are `Ident`s and do not belong
/// here.
#[derive(Clone)]
-pub struct Literal {
- // Internal implementation details
-}
+pub struct Literal(bridge::literal::Literal);
impl Literal {
// TODO: Add experimental API functions for this type
+ // TODO: Generate those constructor with 1/2 macros instead
- pub fn u8_suffixed(_n: u8) -> Self {
- todo!("Implement this function")
+ pub fn u8_suffixed(n: u8) -> Self {
+ Literal(bridge::literal::Literal::u8_suffixed(n))
}
- pub fn u16_suffixed(_n: u16) -> Self {
- todo!("Implement this function")
+ pub fn u16_suffixed(n: u16) -> Self {
+ Literal(bridge::literal::Literal::u16_suffixed(n))
}
- pub fn u32_suffixed(_n: u32) -> Self {
- todo!("Implement this function")
+ pub fn u32_suffixed(n: u32) -> Self {
+ Literal(bridge::literal::Literal::u32_suffixed(n))
}
- pub fn u64_suffixed(_n: u64) -> Self {
- todo!("Implement this function")
+ pub fn u64_suffixed(n: u64) -> Self {
+ Literal(bridge::literal::Literal::u64_suffixed(n))
}
- pub fn u128_suffixed(_n: u128) -> Self {
- todo!("Implement this function")
+ pub fn u128_suffixed(n: u128) -> Self {
+ Literal(bridge::literal::Literal::u128_suffixed(n))
}
- pub fn usize_suffixed(_n: usize) -> Self {
- todo!("Implement this function")
+ pub fn usize_suffixed(n: usize) -> Self {
+ Literal(bridge::literal::Literal::usize_suffixed(n))
}
- pub fn i8_suffixed(_n: i8) -> Self {
- todo!("Implement this function")
+ pub fn i8_suffixed(n: i8) -> Self {
+ Literal(bridge::literal::Literal::i8_suffixed(n))
}
- pub fn i16_suffixed(_n: i16) -> Self {
- todo!("Implement this function")
+ pub fn i16_suffixed(n: i16) -> Self {
+ Literal(bridge::literal::Literal::i16_suffixed(n))
}
- pub fn i32_suffixed(_n: i32) -> Self {
- todo!("Implement this function")
+ pub fn i32_suffixed(n: i32) -> Self {
+ Literal(bridge::literal::Literal::i32_suffixed(n))
}
- pub fn i64_suffixed(_n: i64) -> Self {
- todo!("Implement this function")
+ pub fn i64_suffixed(n: i64) -> Self {
+ Literal(bridge::literal::Literal::i64_suffixed(n))
}
- pub fn i128_suffixed(_n: i128) -> Self {
- todo!("Implement this function")
+ pub fn i128_suffixed(n: i128) -> Self {
+ Literal(bridge::literal::Literal::i128_suffixed(n))
}
- pub fn isize_suffixed(_n: isize) -> Self {
- todo!("Implement this function")
+ pub fn isize_suffixed(n: isize) -> Self {
+ Literal(bridge::literal::Literal::isize_suffixed(n))
}
// Unsuffixed
- pub fn u8_unsuffixed(_n: u8) -> Self {
- todo!("Implement this function")
+ pub fn u8_unsuffixed(n: u8) -> Self {
+ Literal(bridge::literal::Literal::u8_unsuffixed(n))
}
- pub fn u16_unsuffixed(_n: u16) -> Self {
- todo!("Implement this function")
+ pub fn u16_unsuffixed(n: u16) -> Self {
+ Literal(bridge::literal::Literal::u16_unsuffixed(n))
}
- pub fn u32_unsuffixed(_n: u32) -> Self {
- todo!("Implement this function")
+ pub fn u32_unsuffixed(n: u32) -> Self {
+ Literal(bridge::literal::Literal::u32_unsuffixed(n))
}
- pub fn u64_unsuffixed(_n: u64) -> Self {
- todo!("Implement this function")
+ pub fn u64_unsuffixed(n: u64) -> Self {
+ Literal(bridge::literal::Literal::u64_unsuffixed(n))
}
- pub fn u128_unsuffixed(_n: u128) -> Self {
- todo!("Implement this function")
+ pub fn u128_unsuffixed(n: u128) -> Self {
+ Literal(bridge::literal::Literal::u128_unsuffixed(n))
}
- pub fn usize_unsuffixed(_n: usize) -> Self {
- todo!("Implement this function")
+ pub fn usize_unsuffixed(n: usize) -> Self {
+ Literal(bridge::literal::Literal::usize_unsuffixed(n))
}
- pub fn i8_unsuffixed(_n: i8) -> Self {
- todo!("Implement this function")
+ pub fn i8_unsuffixed(n: i8) -> Self {
+ Literal(bridge::literal::Literal::i8_unsuffixed(n))
}
- pub fn i16_unsuffixed(_n: i16) -> Self {
- todo!("Implement this function")
+ pub fn i16_unsuffixed(n: i16) -> Self {
+ Literal(bridge::literal::Literal::i16_unsuffixed(n))
}
- pub fn i32_unsuffixed(_n: i32) -> Self {
- todo!("Implement this function")
+ pub fn i32_unsuffixed(n: i32) -> Self {
+ Literal(bridge::literal::Literal::i32_unsuffixed(n))
}
- pub fn i64_unsuffixed(_n: i64) -> Self {
- todo!("Implement this function")
+ pub fn i64_unsuffixed(n: i64) -> Self {
+ Literal(bridge::literal::Literal::i64_unsuffixed(n))
}
- pub fn i128_unsuffixed(_n: i128) -> Self {
- todo!("Implement this function")
+ pub fn i128_unsuffixed(n: i128) -> Self {
+ Literal(bridge::literal::Literal::i128_unsuffixed(n))
}
- pub fn isize_unsuffixed(_n: isize) -> Self {
- todo!("Implement this function")
+ pub fn isize_unsuffixed(n: isize) -> Self {
+ Literal(bridge::literal::Literal::isize_unsuffixed(n))
}
- pub fn f32_unsuffixed(_n: f32) -> Self {
- todo!("Implement this function")
+ pub fn f32_unsuffixed(n: f32) -> Self {
+ Literal(bridge::literal::Literal::f32_unsuffixed(n))
}
- pub fn f32_suffixed(_n: f32) -> Self {
- todo!("Implement this function")
+ pub fn f32_suffixed(n: f32) -> Self {
+ Literal(bridge::literal::Literal::f32_suffixed(n))
}
- pub fn f64_unsuffixed(_n: f64) -> Self {
- todo!("Implement this function")
+ pub fn f64_unsuffixed(n: f64) -> Self {
+ Literal(bridge::literal::Literal::f64_unsuffixed(n))
}
- pub fn f64_suffixed(_n: f64) -> Self {
- todo!("Implement this function")
+ pub fn f64_suffixed(n: f64) -> Self {
+ Literal(bridge::literal::Literal::f64_suffixed(n))
}
- pub fn string(_string: &str) -> Self {
- todo!("Implement this function")
+ pub fn string(string: &str) -> Self {
+ Literal(bridge::literal::Literal::string(string))
}
- pub fn character(_c: char) -> Self {
- todo!("Implement this function")
+ pub fn character(c: char) -> Self {
+ Literal(bridge::literal::Literal::character(c))
}
- pub fn byte_string(_bytes: &[u8]) -> Self {
- todo!("Implement this function")
+ pub fn byte_string(bytes: &[u8]) -> Self {
+ Literal(bridge::literal::Literal::byte_string(bytes))
}
/// Get the [`Span`] for this literal.
pub fn span(&self) -> Span {
- todo!("Get the span of a literal")
+ Span(self.0.span())
}
/// Set the span for this literal.
@@ -163,8 +163,8 @@ impl Literal {
/// # Arguments
///
/// * `span` - The new span value.
- pub fn set_span(&mut self, _span: Span) {
- todo!("Set the span of a literal")
+ pub fn set_span(&mut self, span: Span) {
+ self.0.set_span(span.0);
}
}