diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-02-16 12:59:23 +0100 |
---|---|---|
committer | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-03-30 13:52:30 +0200 |
commit | e41c961955d9faf8de28aa5096a592573d01ca3b (patch) | |
tree | 45e1c2a2161c533ac2b02ac786045e7d7526b8bd | |
parent | 905cf7298dea48f6b354ec98810b91f9b16c0541 (diff) | |
download | gcc-e41c961955d9faf8de28aa5096a592573d01ca3b.zip gcc-e41c961955d9faf8de28aa5096a592573d01ca3b.tar.gz gcc-e41c961955d9faf8de28aa5096a592573d01ca3b.tar.bz2 |
libproc_macro: Make Ident ffi-safe
Make the Ident rust type ffi-safe. This involved a rework of the
structure construction/destroy which will be delagated to the cpp part.
ChangeLog:
* librust/proc_macro/rust/bridge/ident.rs: Make Ident ffi-safe.
* librust/proc_macro/rust/ident.rs: Refactor display trait.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r-- | librust/proc_macro/rust/bridge/ident.rs | 46 | ||||
-rw-r--r-- | librust/proc_macro/rust/ident.rs | 12 |
2 files changed, 37 insertions, 21 deletions
diff --git a/librust/proc_macro/rust/bridge/ident.rs b/librust/proc_macro/rust/bridge/ident.rs index b6e4719..880cf77 100644 --- a/librust/proc_macro/rust/bridge/ident.rs +++ b/librust/proc_macro/rust/bridge/ident.rs @@ -1,26 +1,29 @@ use bridge::span::Span; -use std::ffi::CString; +use std::ffi::{c_char, CStr, CString}; +use std::fmt; + +extern "C" { + fn Ident__new(string: *const c_char) -> Ident; + fn Ident__new_raw(string: *const c_char) -> Ident; + fn Ident__drop(ident: *const Ident); +} #[repr(C)] #[derive(Clone, Debug)] pub struct Ident { pub(crate) is_raw: bool, - pub(crate) val: CString, + pub(crate) val: *const c_char, } impl Ident { pub fn new(string: &str, _span: Span) -> Self { - Ident { - is_raw: false, - val: CString::new(string).expect("Cannot create CString from rust String"), - } + let string = CString::new(string).expect("Cannot convert to CString"); + unsafe { Ident__new(string.as_ptr()) } } pub fn new_raw(string: &str, _span: Span) -> Self { - Ident { - is_raw: true, - val: CString::new(string).expect("Cannot create CString from rust String"), - } + let string = CString::new(string).expect("Cannot convert to CString"); + unsafe { Ident__new_raw(string.as_ptr()) } } pub fn span(&self) -> Span { @@ -31,3 +34,26 @@ impl Ident { let _ = span; } } + +impl Drop for Ident { + fn drop(&mut self) { + unsafe { Ident__drop(self as *const Ident) } + } +} + +impl fmt::Display for Ident { + /// Display as lossless converted string. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_raw { + f.write_str("r#")?; + } + fmt::Display::fmt( + unsafe { + CStr::from_ptr(self.val) + .to_str() + .expect("Cannot convert back to rust string") + }, + f, + ) + } +} diff --git a/librust/proc_macro/rust/ident.rs b/librust/proc_macro/rust/ident.rs index cc85ea5..809c993 100644 --- a/librust/proc_macro/rust/ident.rs +++ b/librust/proc_macro/rust/ident.rs @@ -56,17 +56,7 @@ impl Ident { impl fmt::Display for Ident { /// Display as lossless converted string. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.0.is_raw { - f.write_str("r#")?; - } - fmt::Display::fmt( - &self - .0 - .val - .to_str() - .expect("Cannot convert back to rust string"), - f, - ) + self.0.fmt(f) } } |