aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-02-20 15:52:09 +0100
committerPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-03-30 13:52:31 +0200
commit453a246bb4d4bd13e1a5d245b9f38923abbfc94e (patch)
tree853b7c6c6c775615e7ce17cec85c1cd2725f976f
parent541c0f009c6c21223942ca7f3157976701f7db8c (diff)
downloadgcc-453a246bb4d4bd13e1a5d245b9f38923abbfc94e.zip
gcc-453a246bb4d4bd13e1a5d245b9f38923abbfc94e.tar.gz
gcc-453a246bb4d4bd13e1a5d245b9f38923abbfc94e.tar.bz2
libproc_macro: Implement Display for Literal
Implement the Display trait on rust internal Literal structure. ChangeLog: * librust/proc_macro/rust/bridge/literal.rs: Implement Display. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r--librust/proc_macro/rust/bridge/literal.rs117
1 files changed, 116 insertions, 1 deletions
diff --git a/librust/proc_macro/rust/bridge/literal.rs b/librust/proc_macro/rust/bridge/literal.rs
index f44fdbc..6f7bb51 100644
--- a/librust/proc_macro/rust/bridge/literal.rs
+++ b/librust/proc_macro/rust/bridge/literal.rs
@@ -1,6 +1,7 @@
use bridge::span::Span;
-use std::convert::TryInto;
+use std::convert::{TryFrom, TryInto};
use std::ffi::c_uchar;
+use std::fmt;
extern "C" {
fn Literal__drop(literal: *const Literal);
@@ -210,3 +211,117 @@ impl Drop for Literal {
}
}
}
+
+impl fmt::Display for Literal {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Literal::String { data, size } => {
+ let slice =
+ unsafe { std::slice::from_raw_parts(*data, (*size).try_into().unwrap()) };
+ f.write_str("\"")?;
+ f.write_str(std::str::from_utf8(slice).unwrap())?;
+ f.write_str("\"")?;
+ }
+ Literal::ByteString { data, size } => {
+ f.write_str("b\"")?;
+ let slice =
+ unsafe { std::slice::from_raw_parts(*data, (*size).try_into().unwrap()) };
+ for &byte in slice {
+ if byte != b'"' && byte >= b' ' && byte <= b'z' {
+ char::try_from(byte).unwrap().fmt(f)?;
+ } else {
+ write!(f, "\\x{byte:02x}")?;
+ }
+ }
+ f.write_str("b\"")?;
+ }
+ Literal::Char(val) => {
+ let ch: char = (*val).try_into().unwrap();
+ match ch {
+ '\'' => f.write_str("'\\''")?,
+ '\0' => f.write_str("'\\0'")?,
+ '\n' => f.write_str("'\\n'")?,
+ ' '..='z' => write!(f, "'{ch}'")?,
+ _ => write!(f, "'\\u{val:x}'")?,
+ }
+ }
+ Literal::Unsigned(val, suffixed) => match val {
+ Unsigned::Unsigned8(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u8")?;
+ }
+ }
+ Unsigned::Unsigned16(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u16")?;
+ }
+ }
+ Unsigned::Unsigned32(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u32")?;
+ }
+ }
+ Unsigned::Unsigned64(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("u64")?;
+ }
+ }
+ },
+ Literal::Signed(val, suffixed) => match val {
+ Signed::Signed8(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i8")?;
+ }
+ }
+ Signed::Signed16(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i16")?;
+ }
+ }
+ Signed::Signed32(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i32")?;
+ }
+ }
+ Signed::Signed64(val) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("i64")?;
+ }
+ }
+ },
+ Literal::Usize(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("usize")?;
+ }
+ }
+ Literal::ISize(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("isize")?;
+ }
+ }
+ Literal::Float32(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("f32")?;
+ }
+ }
+ Literal::Float64(val, suffixed) => {
+ val.fmt(f)?;
+ if *suffixed {
+ f.write_str("f64")?;
+ }
+ }
+ }
+ Ok(())
+ }
+}