Debian Codegraph @

libgrust/libproc_macro/bridge/literal.rs

raw
1 // Copyright (C) 2023-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU Proc Macro Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
17
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 // <http://www.gnu.org/licenses/>.
22
23 use bridge::{ffistring::FFIString, span::Span};
24 use std::fmt;
25 use std::str::FromStr;
26 use LexError;
27
28 extern "C" {
29 fn Literal__from_string(str: FFIString, lit: *mut Literal) -> bool;
30 }
31
32 #[repr(C)]
33 #[derive(Debug, Clone, Copy)]
34 pub enum LitKind {
35 Byte,
36 Char,
37 Integer,
38 Float,
39 Str,
40 StrRaw(u8),
41 ByteStr,
42 ByteStrRaw(u8),
43 Err,
44 }
45
46 #[repr(C)]
47 #[derive(Debug, Clone)]
48 pub struct Literal {
49 kind: LitKind,
50 text: FFIString,
51 suffix: FFIString,
52 span: Span,
53 }
54
55 macro_rules! suffixed_int_literals {
56 ($($name: ident => $kind: ident,)*) => ($(
57 pub fn $name(n : $kind) -> Literal {
58 Literal {
59 kind : LitKind::Integer,
60 text: FFIString::from(&n.to_string()),
61 suffix: FFIString::from(stringify!($kind)),
62 span: Span::default(),
63 }
64 }
65 )*)
66 }
67
68 macro_rules! unsuffixed_int_literals {
69 ($($name: ident => $kind: ident,)*) => ($(
70 pub fn $name(n : $kind) -> Literal {
71 Literal {
72 kind : LitKind::Integer,
73 text: FFIString::from(&n.to_string()),
74 suffix: FFIString::from(""),
75 span: Span::default(),
76 }
77 }
78 )*)
79 }
80
81 impl Literal {
82 suffixed_int_literals! {
83 u8_suffixed => u8,
84 u16_suffixed => u16,
85 u32_suffixed => u32,
86 u64_suffixed => u64,
87 u128_suffixed => u128,
88 usize_suffixed => usize,
89 i8_suffixed => i8,
90 i16_suffixed => i16,
91 i32_suffixed => i32,
92 i64_suffixed => i64,
93 i128_suffixed => i128,
94 isize_suffixed => isize,
95 }
96
97 unsuffixed_int_literals! {
98 u8_unsuffixed => u8,
99 u16_unsuffixed => u16,
100 u32_unsuffixed => u32,
101 u64_unsuffixed => u64,
102 u128_unsuffixed => u128,
103 usize_unsuffixed => usize,
104 i8_unsuffixed => i8,
105 i16_unsuffixed => i16,
106 i32_unsuffixed => i32,
107 i64_unsuffixed => i64,
108 i128_unsuffixed => i128,
109 isize_unsuffixed => isize,
110 }
111
112 pub fn f32_unsuffixed(n: f32) -> Self {
113 let mut repr = n.to_string();
114 if !repr.contains('.') {
115 repr.push_str(".0");
116 }
117
118 Literal {
119 kind: LitKind::Float,
120 text: FFIString::from(&repr),
121 suffix: FFIString::from(""),
122 span: Span::default(),
123 }
124 }
125
126 pub fn f32_suffixed(n: f32) -> Self {
127 Literal {
128 kind: LitKind::Float,
129 text: FFIString::from(&n.to_string()),
130 suffix: FFIString::from("f32"),
131 span: Span::default(),
132 }
133 }
134
135 pub fn f64_unsuffixed(n: f64) -> Self {
136 let mut repr = n.to_string();
137 if !repr.contains('.') {
138 repr.push_str(".0");
139 }
140
141 Literal {
142 kind: LitKind::Float,
143 text: FFIString::from(&repr),
144 suffix: FFIString::from(""),
145 span: Span::default(),
146 }
147 }
148
149 pub fn f64_suffixed(n: f64) -> Self {
150 Literal {
151 kind: LitKind::Float,
152 text: FFIString::from(&n.to_string()),
153 suffix: FFIString::from("f64"),
154 span: Span::default(),
155 }
156 }
157
158 pub fn string(string: &str) -> Self {
159 Literal {
160 kind: LitKind::Str,
161 text: FFIString::from(string),
162 suffix: FFIString::from(""),
163 span: Span::default(),
164 }
165 }
166
167 pub fn character(c: char) -> Self {
168 Literal {
169 kind: LitKind::Char,
170 text: FFIString::from(&c.to_string()),
171 suffix: FFIString::from(""),
172 span: Span::default(),
173 }
174 }
175
176 pub fn byte_string(bytes: &[u8]) -> Self {
177 Literal {
178 kind: LitKind::ByteStr,
179 text: FFIString::from(&bytes.escape_ascii().to_string()),
180 suffix: FFIString::from(""),
181 span: Span::default(),
182 }
183 }
184
185 pub fn span(&self) -> Span {
186 self.span
187 }
188
189 pub fn set_span(&mut self, span: Span) {
190 self.span = span;
191 }
192 }
193
194 impl fmt::Display for Literal {
195 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
196 let text = &self.text.to_string();
197 match self.kind {
198 LitKind::Byte => {
199 f.write_str("b'")?;
200 f.write_str(text)?;
201 f.write_str("'")?;
202 }
203 LitKind::Char => {
204 f.write_str("'")?;
205 f.write_str(text)?;
206 f.write_str("'")?;
207 }
208 LitKind::Str => {
209 f.write_str("\"")?;
210 f.write_str(text)?;
211 f.write_str("\"")?;
212 }
213 LitKind::StrRaw(n) => {
214 f.write_str("r")?;
215 for _ in 0..n {
216 f.write_str("#")?;
217 }
218 f.write_str("\"")?;
219 f.write_str(text)?;
220 f.write_str("\"")?;
221 }
222 LitKind::ByteStr => {
223 f.write_str("b\"")?;
224 f.write_str(text)?;
225 f.write_str("\"")?;
226 }
227 LitKind::ByteStrRaw(n) => {
228 f.write_str("br")?;
229 for _ in 0..n {
230 f.write_str("#")?;
231 }
232 f.write_str("\"")?;
233 f.write_str(text)?;
234 f.write_str("\"")?;
235 }
236 _ => f.write_str(text)?,
237 }
238
239 f.write_str(&self.suffix.to_string())?;
240 Ok(())
241 }
242 }
243
244 impl FromStr for Literal {
245 type Err = LexError;
246
247 fn from_str(string: &str) -> Result<Self, LexError> {
248 // Structure that will be filled in by the cpp
249 let mut lit = Literal {
250 kind: LitKind::Err,
251 text: FFIString::from(""),
252 suffix: FFIString::from(""),
253 span: Span::default(),
254 };
255 // TODO: We might want to pass a LexError by reference to retrieve
256 // error information
257 if unsafe { Literal__from_string(string.into(), &mut lit as *mut Literal) } {
258 Err(LexError)
259 } else {
260 Ok(lit)
261 }
262 }
263 }