Debian Codegraph @

libgrust/libproc_macro_internal/literal.cc

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 #include "literal.h"
24 #include <cstring>
25 #include <cstdlib>
26
27 #include "registration.h"
28
29 namespace ProcMacro {
30
31 extern "C" {
32 bool
33 Literal__from_string (FFIString str, Literal *lit)
34 {
35 bool result;
36 auto source = str.to_string ();
37
38 *lit = Literal::make_literal (source, result);
39 return result;
40 }
41 }
42
43 void
44 Literal::drop (Literal *lit)
45 {
46 FFIString::drop (&lit->text);
47 FFIString::drop (&lit->suffix);
48 }
49
50 Literal
51 Literal::clone () const
52 {
53 return {this->kind, this->text.clone (), this->suffix.clone (), this->span};
54 }
55
56 Literal
57 Literal::make_literal (const std::string &text, bool &has_error)
58 {
59 return __gccrs_proc_macro_lit_from_str_ (text, has_error);
60 }
61
62 Literal
63 Literal::make_literal (LitKind kind, Span span, const std::string &text,
64 const std::string &suffix)
65 {
66 auto ffi_text = FFIString::make_ffistring (text);
67 auto ffi_suffix = FFIString::make_ffistring (suffix);
68 return {kind, ffi_text, ffi_suffix, span};
69 }
70
71 Literal
72 Literal::make_u8 (std::uint8_t value, bool suffixed)
73 {
74 auto text = FFIString::make_ffistring (std::to_string (value));
75 auto suffix = FFIString::make_ffistring (suffixed ? "u8" : "");
76 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
77 }
78
79 Literal
80 Literal::make_u16 (std::uint16_t value, bool suffixed)
81 {
82 auto text = FFIString::make_ffistring (std::to_string (value));
83 auto suffix = FFIString::make_ffistring (suffixed ? "u16" : "");
84 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
85 }
86
87 Literal
88 Literal::make_u32 (std::uint32_t value, bool suffixed)
89 {
90 auto text = FFIString::make_ffistring (std::to_string (value));
91 auto suffix = FFIString::make_ffistring (suffixed ? "u32" : "");
92 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
93 }
94
95 Literal
96 Literal::make_u64 (std::uint64_t value, bool suffixed)
97 {
98 auto text = FFIString::make_ffistring (std::to_string (value));
99 auto suffix = FFIString::make_ffistring (suffixed ? "u64" : "");
100 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
101 }
102
103 Literal
104 Literal::make_i8 (std::int8_t value, bool suffixed)
105 {
106 auto text = FFIString::make_ffistring (std::to_string (value));
107 auto suffix = FFIString::make_ffistring (suffixed ? "i8" : "");
108 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
109 }
110
111 Literal
112 Literal::make_i16 (std::int16_t value, bool suffixed)
113 {
114 auto text = FFIString::make_ffistring (std::to_string (value));
115 auto suffix = FFIString::make_ffistring (suffixed ? "i16" : "");
116 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
117 }
118
119 Literal
120 Literal::make_i32 (std::int32_t value, bool suffixed)
121 {
122 auto text = FFIString::make_ffistring (std::to_string (value));
123 auto suffix = FFIString::make_ffistring (suffixed ? "i32" : "");
124 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
125 }
126
127 Literal
128 Literal::make_i64 (std::int64_t value, bool suffixed)
129 {
130 auto text = FFIString::make_ffistring (std::to_string (value));
131 auto suffix = FFIString::make_ffistring (suffixed ? "i64" : "");
132 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
133 }
134
135 Literal
136 Literal::make_string (const std::string &str)
137 {
138 auto text = FFIString::make_ffistring (str);
139 auto suffix = FFIString::make_ffistring ("");
140 return {LitKind::make_str (), text, suffix, Span::make_unknown ()};
141 }
142
143 Literal
144 Literal::make_byte_string (const std::vector<std::uint8_t> &vec)
145 {
146 auto text
147 = FFIString::make_ffistring (std::string (vec.cbegin (), vec.cend ()));
148 auto suffix = FFIString::make_ffistring ("");
149 return {LitKind::make_byte_str (), text, suffix, Span::make_unknown ()};
150 }
151
152 Literal
153 Literal::make_f32 (float value, bool suffixed)
154 {
155 auto text = FFIString::make_ffistring (std::to_string (value));
156 auto suffix = FFIString::make_ffistring (suffixed ? "f32" : "");
157 return {LitKind::make_float (), text, suffix, Span::make_unknown ()};
158 }
159
160 Literal
161 Literal::make_f64 (double value, bool suffixed)
162 {
163 auto text = FFIString::make_ffistring (std::to_string (value));
164 auto suffix = FFIString::make_ffistring (suffixed ? "f64" : "");
165 return {LitKind::make_float (), text, suffix, Span::make_unknown ()};
166 }
167
168 Literal
169 Literal::make_char (std::uint32_t ch)
170 {
171 auto text = FFIString::make_ffistring (std::to_string ((char) ch));
172 auto suffix = FFIString::make_ffistring ("");
173 return {LitKind::make_char (), text, suffix, Span::make_unknown ()};
174 }
175
176 Literal
177 Literal::make_usize (std::uint64_t value, bool suffixed)
178 {
179 auto text = FFIString::make_ffistring (std::to_string (value));
180 auto suffix = FFIString::make_ffistring (suffixed ? "usize" : "");
181 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
182 }
183
184 Literal
185 Literal::make_isize (std::int64_t value, bool suffixed)
186 {
187 auto text = FFIString::make_ffistring (std::to_string (value));
188 auto suffix = FFIString::make_ffistring (suffixed ? "isize" : "");
189 return {LitKind::make_integer (), text, suffix, Span::make_unknown ()};
190 }
191
192 LitKind
193 LitKind::make_byte ()
194 {
195 LitKindPayload payload;
196 return {BYTE, payload};
197 }
198
199 LitKind
200 LitKind::make_char ()
201 {
202 LitKindPayload payload;
203 return {CHAR, payload};
204 }
205
206 LitKind
207 LitKind::make_integer ()
208 {
209 LitKindPayload payload;
210 return {INTEGER, payload};
211 }
212
213 LitKind
214 LitKind::make_float ()
215 {
216 LitKindPayload payload;
217 return {FLOAT, payload};
218 }
219
220 LitKind
221 LitKind::make_str ()
222 {
223 LitKindPayload payload;
224 return {STR, payload};
225 }
226
227 LitKind
228 LitKind::make_str_raw (std::uint8_t val)
229 {
230 LitKindPayload payload;
231 payload.str_raw = val;
232 return {STR_RAW, payload};
233 }
234
235 LitKind
236 LitKind::make_byte_str ()
237 {
238 LitKindPayload payload;
239 return {BYTE_STR, payload};
240 }
241
242 LitKind
243 LitKind::make_byte_str_raw (std::uint8_t val)
244 {
245 LitKindPayload payload;
246 payload.byte_str_raw = val;
247 return {BYTE_STR_RAW, payload};
248 }
249
250 } // namespace ProcMacro