Line data Source code
1 : // Copyright (C) 2025-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 : #ifndef RUST_PARSE_ERROR_H
20 : #define RUST_PARSE_ERROR_H
21 :
22 : #include "expected.h"
23 : #include "rust-ast.h"
24 : #include "rust-parse-utils.h"
25 :
26 : namespace Rust {
27 : namespace Parse {
28 : namespace Error {
29 :
30 45 : struct EnumVariant
31 : {
32 : enum class Kind
33 : {
34 : CHILD_ERROR,
35 : NO_IDENTIFIER,
36 : UNFINISHED_TUPLE_VARIANT,
37 : } kind;
38 :
39 : static tl::expected<std::unique_ptr<AST::EnumItem>, EnumVariant>
40 15 : make_not_identifier (const_TokenPtr unexpected_token)
41 : {
42 30 : return tl::unexpected<EnumVariant> (EnumVariant (unexpected_token));
43 : }
44 :
45 : static tl::expected<std::unique_ptr<AST::EnumItem>, EnumVariant>
46 0 : make_child_error ()
47 : {
48 0 : return tl::unexpected<EnumVariant> (EnumVariant (Kind::CHILD_ERROR));
49 : }
50 :
51 : static tl::expected<std::unique_ptr<AST::EnumItem>, EnumVariant>
52 0 : make_unfinished_tuple_variant ()
53 : {
54 0 : return tl::unexpected<EnumVariant> (
55 0 : EnumVariant (Kind::UNFINISHED_TUPLE_VARIANT));
56 : }
57 :
58 : private:
59 15 : EnumVariant (const_TokenPtr unexpected_token)
60 15 : : kind (Kind::NO_IDENTIFIER), unexpected_token (unexpected_token)
61 15 : {}
62 :
63 0 : EnumVariant (Kind kind) : kind (kind), unexpected_token (nullptr)
64 : {
65 0 : rust_assert (kind != Kind::NO_IDENTIFIER);
66 : }
67 :
68 : // Only valid for missing identifier
69 : const_TokenPtr unexpected_token;
70 : };
71 :
72 : struct Attribute
73 : {
74 0 : static tl::expected<AST::Attribute, Attribute> make_malformed ()
75 : {
76 0 : return tl::unexpected<Attribute> (Attribute (Kind::MALFORMED));
77 : }
78 :
79 7 : static tl::expected<AST::Attribute, Attribute> make_malformed_body ()
80 : {
81 7 : return tl::unexpected<Attribute> (Attribute (Kind::MALFORMED_BODY));
82 : }
83 :
84 2 : static tl::expected<AST::Attribute, Attribute> make_unexpected_inner ()
85 : {
86 2 : return tl::unexpected<Attribute> (Attribute (Kind::UNEXPECTED_INNER));
87 : }
88 :
89 : enum class Kind
90 : {
91 : MALFORMED,
92 : MALFORMED_BODY,
93 : UNEXPECTED_INNER,
94 : } kind;
95 :
96 : private:
97 9 : Attribute (Kind kind) : kind (kind) {}
98 : };
99 :
100 : struct AttributeBody
101 : {
102 0 : static tl::expected<Parse::AttributeBody, AttributeBody> make_invalid_path ()
103 : {
104 0 : return tl::unexpected<AttributeBody> (AttributeBody (Kind::INVALID_PATH));
105 : }
106 :
107 : static tl::expected<Parse::AttributeBody, AttributeBody>
108 7 : make_invalid_attrinput ()
109 : {
110 7 : return tl::unexpected<AttributeBody> (
111 7 : AttributeBody (Kind::INVALID_ATTRINPUT));
112 : }
113 :
114 : enum class Kind
115 : {
116 : INVALID_PATH,
117 : INVALID_ATTRINPUT,
118 : } kind;
119 :
120 : private:
121 7 : AttributeBody (Kind kind) : kind (kind) {}
122 : };
123 :
124 : struct SimplePathSegment
125 : {
126 : static tl::expected<AST::SimplePathSegment, SimplePathSegment>
127 222 : make_invalid_token_or_path_end ()
128 : {
129 222 : return tl::unexpected<SimplePathSegment> (
130 222 : SimplePathSegment (Kind::INVALID_SIMPLE_PATH_TOKEN));
131 : }
132 :
133 : enum class Kind
134 : {
135 : /* Invalid token found whilst parsing a simple path segment, could be an
136 : error or the end of the path */
137 : INVALID_SIMPLE_PATH_TOKEN,
138 : } kind;
139 :
140 : private:
141 222 : SimplePathSegment (Kind kind) : kind (kind) {}
142 : };
143 :
144 : struct PathIdentSegment
145 : {
146 : static tl::expected<AST::PathIdentSegment, PathIdentSegment>
147 211 : make_invalid_token ()
148 : {
149 211 : return tl::unexpected<PathIdentSegment> (
150 211 : PathIdentSegment (Kind::INVALID_PATH_IDENT_TOKEN));
151 : }
152 :
153 : enum class Kind
154 : {
155 : INVALID_PATH_IDENT_TOKEN,
156 : } kind;
157 :
158 : private:
159 211 : PathIdentSegment (Kind kind) : kind (kind) {}
160 : };
161 :
162 : struct AttrInput
163 : {
164 : static tl::expected<std::unique_ptr<AST::AttrInput>, AttrInput>
165 0 : make_malformed ()
166 : {
167 0 : return tl::unexpected<AttrInput> (AttrInput (Kind::MALFORMED));
168 : }
169 :
170 : static tl::expected<std::unique_ptr<AST::AttrInput>, AttrInput>
171 0 : make_bad_macro_invocation ()
172 : {
173 0 : return tl::unexpected<AttrInput> (AttrInput (Kind::BAD_MACRO_INVOCATION));
174 : }
175 :
176 : static tl::expected<std::unique_ptr<AST::AttrInput>, AttrInput>
177 6390 : make_missing_attrinput ()
178 : {
179 6390 : return tl::unexpected<AttrInput> (AttrInput (Kind::MISSING));
180 : }
181 :
182 : static tl::expected<std::unique_ptr<AST::AttrInput>, AttrInput>
183 7 : make_bad_token_tree ()
184 : {
185 7 : return tl::unexpected<AttrInput> (AttrInput (Kind::BAD_TOKEN_TREE));
186 : }
187 :
188 : enum class Kind
189 : {
190 : MALFORMED,
191 : BAD_MACRO_INVOCATION,
192 : BAD_TOKEN_TREE,
193 : // Not an hard error in some context
194 : MISSING,
195 : } kind;
196 :
197 : private:
198 6397 : AttrInput (Kind kind) : kind (kind) {}
199 : };
200 :
201 : struct Item
202 : {
203 7 : static tl::expected<std::unique_ptr<AST::Item>, Item> make_end_of_file ()
204 : {
205 7 : return tl::unexpected<Item> (Item (Kind::END_OF_FILE));
206 : }
207 :
208 73 : static tl::expected<std::unique_ptr<AST::Item>, Item> make_malformed ()
209 : {
210 73 : return tl::unexpected<Item> (Item (Kind::MALFORMED));
211 : }
212 :
213 : enum class Kind
214 : {
215 : END_OF_FILE,
216 : MALFORMED,
217 : } kind;
218 :
219 : private:
220 80 : Item (Kind kind) : kind (kind) {}
221 : };
222 :
223 158 : struct Items
224 : {
225 : static tl::expected<std::vector<std::unique_ptr<AST::Item>>, Items>
226 79 : make_malformed (std::vector<std::unique_ptr<AST::Item>> items)
227 : {
228 79 : return tl::unexpected<Items> (Items (Kind::MALFORMED, std::move (items)));
229 : }
230 :
231 : enum class Kind
232 : {
233 : MALFORMED,
234 : } kind;
235 :
236 : Items (Items const &) = delete;
237 : Items &operator= (Items const &) = delete;
238 :
239 79 : Items (Items &&items) = default;
240 : Items &operator= (Items &&) = default;
241 :
242 : // Should we do anything with valid items ?
243 : std::vector<std::unique_ptr<AST::Item>> items;
244 :
245 : private:
246 79 : Items (Kind kind, std::vector<std::unique_ptr<AST::Item>> items)
247 79 : : kind (kind), items (std::move (items))
248 : {}
249 : };
250 :
251 : struct Visibility
252 : {
253 0 : static tl::expected<AST::Visibility, Visibility> make_malformed ()
254 : {
255 0 : return tl::unexpected<Visibility> (Visibility (Kind::MALFORMED));
256 : }
257 :
258 0 : static tl::expected<AST::Visibility, Visibility> make_missing_path ()
259 : {
260 0 : return tl::unexpected<Visibility> (Visibility (Kind::MISSING_PATH));
261 : }
262 :
263 : enum class Kind
264 : {
265 : MISSING_PATH,
266 : MALFORMED,
267 : } kind;
268 :
269 : private:
270 0 : Visibility (Kind kind) : kind (kind) {}
271 : };
272 :
273 : struct LifetimeParam
274 : {
275 : static tl::expected<AST::LifetimeParam, LifetimeParam>
276 2 : make_not_a_lifetime_param ()
277 : {
278 2 : return tl::unexpected<LifetimeParam> (
279 2 : LifetimeParam (Kind::NOT_A_LIFETIME_PARAM));
280 : }
281 :
282 : enum class Kind
283 : {
284 : NOT_A_LIFETIME_PARAM,
285 : } kind;
286 :
287 : private:
288 2 : LifetimeParam (Kind kind) : kind (kind) {}
289 : };
290 :
291 : class Lifetime
292 : {
293 : };
294 :
295 : struct LoopLabel
296 : {
297 0 : static tl::expected<AST::LoopLabel, LoopLabel> make_not_loop_label ()
298 : {
299 0 : return tl::unexpected<LoopLabel> (LoopLabel (Kind::NOT_LOOP_LABEL));
300 : }
301 :
302 40 : static tl::expected<AST::LoopLabel, LoopLabel> make_missing_colon ()
303 : {
304 40 : return tl::unexpected<LoopLabel> (LoopLabel (Kind::MISSING_COLON));
305 : }
306 :
307 : enum class Kind
308 : {
309 : // Not an hard error
310 : NOT_LOOP_LABEL,
311 : // Hard error
312 : MISSING_COLON,
313 : } kind;
314 :
315 : private:
316 40 : LoopLabel (Kind kind) : kind (kind) {}
317 : };
318 :
319 : struct Self
320 : {
321 : static tl::expected<std::unique_ptr<AST::Param>, Self>
322 3 : make_self_raw_pointer ()
323 : {
324 3 : return tl::unexpected<Self> (Self (Kind::SELF_RAW_PTR));
325 : }
326 :
327 10355 : static tl::expected<std::unique_ptr<AST::Param>, Self> make_not_self ()
328 : {
329 10355 : return tl::unexpected<Self> (Self (Kind::NOT_SELF));
330 : }
331 :
332 0 : static tl::expected<std::unique_ptr<AST::Param>, Self> make_parsing_error ()
333 : {
334 0 : return tl::unexpected<Self> (Self (Kind::PARSING));
335 : }
336 :
337 : enum class Kind
338 : {
339 : SELF_RAW_PTR,
340 : PARSING,
341 : NOT_SELF,
342 : } kind;
343 :
344 : private:
345 10358 : Self (Kind kind) : kind (kind) {}
346 : };
347 :
348 : enum class Expr
349 : {
350 : MALFORMED,
351 : CHILD_ERROR,
352 : NULL_EXPR,
353 : NULL_DENOTATION,
354 : LEFT_DENOTATION,
355 : };
356 :
357 : enum class StructExprField
358 : {
359 : MALFORMED,
360 : CHILD_ERROR,
361 : // Not a hard error
362 : STRUCT_BASE,
363 : };
364 :
365 : // Generic intermediate AST node error used when the errors need no special
366 : // handling
367 : enum class Node
368 : {
369 : // Unexpected or missing token whilst parsing the node
370 : MALFORMED,
371 : // Error whilst parsing a child construct for the current node
372 : CHILD_ERROR,
373 : };
374 :
375 : } // namespace Error
376 : } // namespace Parse
377 : } // namespace Rust
378 :
379 : #endif /* !RUST_PARSE_ERROR_H */
|