public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] libproc_macro: Add Ident type implementation
@ 2023-04-06 21:30 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:30 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:95016480c62e0c26d0456282344f48a55ce74f3c

commit 95016480c62e0c26d0456282344f48a55ce74f3c
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Wed Feb 15 16:58:58 2023 +0100

    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>

Diff:
---
 librust/proc_macro/rust/bridge.rs       |  1 +
 librust/proc_macro/rust/bridge/ident.rs | 33 +++++++++++++++++++++++++++++
 librust/proc_macro/rust/ident.rs        | 37 ++++++++++++++++++++-------------
 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 e066cd483cd..33d09141fbb 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 00000000000..b6e471962d2
--- /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 5d21cbd498a..cc85ea5b9d3 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 0ea60eca0ca..b5d573cc5af 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:30 [gcc/devel/rust/master] libproc_macro: Add Ident type implementation Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).