diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-02-15 16:58:58 +0100 |
---|---|---|
committer | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-03-30 13:52:30 +0200 |
commit | 95016480c62e0c26d0456282344f48a55ce74f3c (patch) | |
tree | 21dabc116def43ecfc18719dcf5928231d507677 | |
parent | ddd0c450bc64e8d44a4c481f3cea0e238d95e08c (diff) | |
download | gcc-95016480c62e0c26d0456282344f48a55ce74f3c.zip gcc-95016480c62e0c26d0456282344f48a55ce74f3c.tar.gz gcc-95016480c62e0c26d0456282344f48a55ce74f3c.tar.bz2 |
libproc_macro: Add Ident type implementation
Add the Ident rust type internal implementation.
ChangeLog:
* librust/proc_macro/rust/bridge.rs: Add ident internal module.
* librust/proc_macro/rust/ident.rs: Add ident internals.
* librust/proc_macro/rust/span.rs: Make internal field public.
* librust/proc_macro/rust/bridge/ident.rs: Add internal Ident
type.
Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
-rw-r--r-- | librust/proc_macro/rust/bridge.rs | 1 | ||||
-rw-r--r-- | librust/proc_macro/rust/bridge/ident.rs | 33 | ||||
-rw-r--r-- | librust/proc_macro/rust/ident.rs | 37 | ||||
-rw-r--r-- | librust/proc_macro/rust/span.rs | 2 |
4 files changed, 58 insertions, 15 deletions
diff --git a/librust/proc_macro/rust/bridge.rs b/librust/proc_macro/rust/bridge.rs index e066cd4..33d0914 100644 --- a/librust/proc_macro/rust/bridge.rs +++ b/librust/proc_macro/rust/bridge.rs @@ -1 +1,2 @@ +pub mod ident; pub mod span; diff --git a/librust/proc_macro/rust/bridge/ident.rs b/librust/proc_macro/rust/bridge/ident.rs new file mode 100644 index 0000000..b6e4719 --- /dev/null +++ b/librust/proc_macro/rust/bridge/ident.rs @@ -0,0 +1,33 @@ +use bridge::span::Span; +use std::ffi::CString; + +#[repr(C)] +#[derive(Clone, Debug)] +pub struct Ident { + pub(crate) is_raw: bool, + pub(crate) val: CString, +} + +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"), + } + } + + pub fn new_raw(string: &str, _span: Span) -> Self { + Ident { + is_raw: true, + val: CString::new(string).expect("Cannot create CString from rust String"), + } + } + + pub fn span(&self) -> Span { + Span {} + } + + pub fn set_span(&mut self, span: Span) { + let _ = span; + } +} diff --git a/librust/proc_macro/rust/ident.rs b/librust/proc_macro/rust/ident.rs index 5d21cbd..cc85ea5 100644 --- a/librust/proc_macro/rust/ident.rs +++ b/librust/proc_macro/rust/ident.rs @@ -1,11 +1,10 @@ +use bridge; use std::fmt; use Span; /// An identifier. #[derive(Clone)] -pub struct Ident { - // Internal implementation details -} +pub struct Ident(pub(crate) bridge::ident::Ident); impl Ident { /// Creates a new identifier. @@ -19,8 +18,8 @@ impl Ident { /// /// The `string` argument must be a valid identifier permitted by the /// language, otherwise the function will panic. - pub fn new(_string: &str, _span: Span) -> Self { - todo!("Implement this function") + pub fn new(string: &str, span: Span) -> Self { + Ident(bridge::ident::Ident::new(string, span.0)) } /// Creates a new raw identifier. @@ -35,13 +34,13 @@ impl Ident { /// The `string` argument must be a valid identifier permitted by the /// language. Furthermore, it should not be a keyword used in path /// segments, otherwise this function will panic. - pub fn new_raw(_string: &str, _span: Span) -> Self { - todo!("Implement this function") + pub fn new_raw(string: &str, span: Span) -> Self { + Ident(bridge::ident::Ident::new_raw(string, span.0)) } /// Return the span of the identifier pub fn span(&self) -> Span { - todo!("Implement this function") + Span(self.0.span()) } /// Change the span of the identifier. @@ -49,20 +48,30 @@ impl Ident { /// # Arguments /// /// * `span` - The new span value. - pub fn set_span(&mut self, _span: Span) { - todo!("Implement this function") + pub fn set_span(&mut self, span: Span) { + self.0.set_span(span.0); } } impl fmt::Display for Ident { /// Display as lossless converted string. - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!("Implement this function") + 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, + ) } } impl fmt::Debug for Ident { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!("Implement this function") + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) } } diff --git a/librust/proc_macro/rust/span.rs b/librust/proc_macro/rust/span.rs index 0ea60eca..b5d573c 100644 --- a/librust/proc_macro/rust/span.rs +++ b/librust/proc_macro/rust/span.rs @@ -3,7 +3,7 @@ use std::fmt; /// A region of source code along with macro expansion information. #[derive(Copy, Clone)] -pub struct Span(bridge::span::Span); +pub struct Span(pub(crate) bridge::span::Span); impl Span { // TODO: Add experimental API functions for this type |