Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "rust-system.h"
20 : #include "rust-token.h"
21 : #include "rust-diagnostics.h"
22 : #include "rust-unicode.h"
23 : #include "rust-ast.h"
24 :
25 : namespace Rust {
26 : // Hackily defined way to get token description for enum value using x-macros
27 : const char *
28 170919 : get_token_description (TokenId id)
29 : {
30 170919 : switch (id)
31 : {
32 : #define RS_TOKEN(name, descr) \
33 : case name: \
34 : return descr;
35 : #define RS_TOKEN_KEYWORD_2015(x, y) RS_TOKEN (x, y)
36 : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
37 170919 : RS_TOKEN_LIST
38 : #undef RS_TOKEN_KEYWORD_2015
39 : #undef RS_TOKEN_KEYWORD_2018
40 : #undef RS_TOKEN
41 0 : default:
42 0 : rust_unreachable ();
43 : }
44 : }
45 :
46 : /* Hackily defined way to get token description as a string for enum value using
47 : * x-macros */
48 : const char *
49 1 : token_id_to_str (TokenId id)
50 : {
51 1 : switch (id)
52 : {
53 : #define RS_TOKEN(name, _) \
54 : case name: \
55 : return #name;
56 : #define RS_TOKEN_KEYWORD_2015(x, y) RS_TOKEN (x, y)
57 : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
58 1 : RS_TOKEN_LIST
59 : #undef RS_TOKEN_KEYWORD_2015
60 : #undef RS_TOKEN_KEYWORD_2018
61 : #undef RS_TOKEN
62 0 : default:
63 0 : rust_unreachable ();
64 : }
65 : }
66 :
67 : /* checks if a token is a keyword */
68 : bool
69 417306 : token_id_is_keyword (TokenId id)
70 : {
71 417306 : switch (id)
72 : {
73 : #define RS_TOKEN_KEYWORD_2015(name, _) case name:
74 : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
75 : #define RS_TOKEN(a, b)
76 : RS_TOKEN_LIST return true;
77 : #undef RS_TOKEN_KEYWORD_2015
78 : #undef RS_TOKEN_KEYWORD_2018
79 : #undef RS_TOKEN
80 417268 : default:
81 417268 : return false;
82 : }
83 : }
84 :
85 : /* gets the string associated with a keyword */
86 : const std::string &
87 24 : token_id_keyword_string (TokenId id)
88 : {
89 24 : switch (id)
90 : {
91 : #define RS_TOKEN_KEYWORD_2015(id, str_ptr) \
92 : case id: \
93 : { \
94 : static const std::string str (str_ptr); \
95 : return str; \
96 : } \
97 : rust_unreachable ();
98 : #define RS_TOKEN_KEYWORD_2018 RS_TOKEN_KEYWORD_2015
99 : #define RS_TOKEN(a, b)
100 24 : RS_TOKEN_LIST
101 : #undef RS_TOKEN_KEYWORD_2015
102 : #undef RS_TOKEN_KEYWORD_2018
103 : #undef RS_TOKEN
104 0 : default:
105 0 : rust_unreachable ();
106 : }
107 : }
108 :
109 : const char *
110 80 : get_type_hint_string (PrimitiveCoreType type)
111 : {
112 80 : switch (type)
113 : {
114 : case CORETYPE_BOOL:
115 : return "bool";
116 0 : case CORETYPE_CHAR:
117 0 : return "char";
118 0 : case CORETYPE_STR:
119 0 : return "str";
120 : // case CORETYPE_INT:
121 0 : case CORETYPE_ISIZE:
122 0 : return "isize";
123 : // case CORETYPE_UINT:
124 32 : case CORETYPE_USIZE:
125 32 : return "usize";
126 42 : case CORETYPE_F32:
127 42 : return "f32";
128 0 : case CORETYPE_F64:
129 0 : return "f64";
130 0 : case CORETYPE_I8:
131 0 : return "i8";
132 0 : case CORETYPE_I16:
133 0 : return "i16";
134 1 : case CORETYPE_I32:
135 1 : return "i32";
136 0 : case CORETYPE_I64:
137 0 : return "i64";
138 0 : case CORETYPE_I128:
139 0 : return "i128";
140 2 : case CORETYPE_U8:
141 2 : return "u8";
142 2 : case CORETYPE_U16:
143 2 : return "u16";
144 1 : case CORETYPE_U32:
145 1 : return "u32";
146 0 : case CORETYPE_U64:
147 0 : return "u64";
148 0 : case CORETYPE_U128:
149 0 : return "u128";
150 0 : case CORETYPE_PURE_DECIMAL:
151 0 : return "pure_decimal";
152 0 : case CORETYPE_UNKNOWN:
153 0 : default:
154 0 : return "unknown";
155 : }
156 : }
157 :
158 : const char *
159 80 : Token::get_type_hint_str () const
160 : {
161 80 : return get_type_hint_string (type_hint);
162 : }
163 :
164 : std::string
165 247300 : nfc_normalize_token_string (location_t loc, TokenId id, const std::string &str)
166 : {
167 247300 : if (id == IDENTIFIER || id == LIFETIME)
168 : {
169 206084 : tl::optional<Utf8String> ustring = Utf8String::make_utf8_string (str);
170 206084 : if (ustring.has_value ())
171 206084 : return ustring.value ().nfc_normalize ().as_string ();
172 : else
173 0 : rust_internal_error_at (loc,
174 : "identifier '%s' is not a valid UTF-8 string",
175 : str.c_str ());
176 206084 : }
177 : else
178 41216 : return str;
179 : }
180 :
181 : namespace {
182 : enum class Context
183 : {
184 : String,
185 : Char
186 : };
187 :
188 : const std::map<char, std::string> matches = {
189 : {'\t', "\\t"}, {'\n', "\\n"}, {'\r', "\\r"},
190 : {'\0', "\\0"}, {'\\', "\\\\"}, {'\v', "\\v"},
191 : };
192 :
193 : std::string
194 3088 : escape_special_chars (const std::string &source, Context ctx)
195 : {
196 3088 : std::stringstream stream;
197 3088 : decltype (matches)::const_iterator result;
198 25801 : for (char c : source)
199 : {
200 : // FIXME: #2411 Also replace escaped unicode values and \x digits
201 22713 : if ((result = matches.find (c)) != matches.end ())
202 116 : stream << result->second;
203 22597 : else if (c == '\'' && ctx == Context::Char)
204 0 : stream << "\\'";
205 22597 : else if (c == '"' && ctx == Context::String)
206 16 : stream << "\\\"";
207 : else
208 22581 : stream << c;
209 : }
210 :
211 3088 : return stream.str ();
212 3088 : }
213 :
214 : } // namespace
215 :
216 : TokenPtr
217 1 : Token::make_identifier (const Identifier &ident)
218 : {
219 1 : std::string str = ident;
220 1 : return make_identifier (ident.get_locus (), std::move (str));
221 1 : }
222 :
223 : std::string
224 73138 : Token::as_string () const
225 : {
226 73138 : if (should_have_str ())
227 : {
228 23188 : switch (get_id ())
229 : {
230 2997 : case STRING_LITERAL:
231 5994 : return "\"" + escape_special_chars (get_str (), Context::String)
232 2997 : + "\"";
233 14 : case BYTE_STRING_LITERAL:
234 28 : return "b\"" + escape_special_chars (get_str (), Context::String)
235 14 : + "\"";
236 0 : case RAW_STRING_LITERAL:
237 0 : return "r\"" + escape_special_chars (get_str (), Context::String)
238 0 : + "\"";
239 49 : case CHAR_LITERAL:
240 98 : return "'" + escape_special_chars (get_str (), Context::Char) + "'";
241 28 : case BYTE_CHAR_LITERAL:
242 56 : return "b'" + escape_special_chars (get_str (), Context::Char) + "'";
243 1097 : case LIFETIME:
244 1097 : return "'" + get_str ();
245 0 : case SCOPE_RESOLUTION:
246 0 : return "::";
247 1537 : case INT_LITERAL:
248 1537 : if (get_type_hint () == CORETYPE_UNKNOWN)
249 1457 : return get_str ();
250 : else
251 80 : return get_str () + get_type_hint_str ();
252 16 : case FLOAT_LITERAL:
253 16 : if (get_type_hint () == CORETYPE_UNKNOWN)
254 16 : return get_str ();
255 : else
256 0 : return get_str () + get_type_hint_str ();
257 17450 : default:
258 17450 : return get_str ();
259 : }
260 : }
261 : else
262 : {
263 49950 : return get_token_description ();
264 : }
265 : }
266 : } // namespace Rust
|