Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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-ast-lower-base.h"
20 : : #include "rust-ast-lower-type.h"
21 : : #include "rust-ast-lower-pattern.h"
22 : : #include "rust-ast-lower-extern.h"
23 : : #include "rust-ast.h"
24 : : #include "rust-attribute-values.h"
25 : : #include "rust-diagnostics.h"
26 : : #include "rust-expr.h"
27 : : #include "rust-item.h"
28 : : #include "rust-system.h"
29 : : #include "rust-attributes.h"
30 : :
31 : : namespace Rust {
32 : : namespace HIR {
33 : :
34 : : void
35 : 0 : ASTLoweringBase::visit (AST::MacroInvocation &invoc)
36 : : {
37 : 0 : rust_fatal_error (invoc.get_locus (), "rogue macro detected during lowering");
38 : : rust_unreachable ();
39 : : }
40 : :
41 : : void
42 : 0 : ASTLoweringBase::visit (AST::ErrorPropagationExpr &expr)
43 : : {
44 : 0 : rust_fatal_error (expr.get_locus (),
45 : : "missing desugar for question mark operator");
46 : : rust_unreachable ();
47 : : }
48 : :
49 : : void
50 : 0 : ASTLoweringBase::visit (AST::TryExpr &expr)
51 : : {
52 : 0 : rust_fatal_error (expr.get_locus (), "missing desugar for try-blocks");
53 : : rust_unreachable ();
54 : : }
55 : :
56 : : void
57 : 0 : ASTLoweringBase::visit (AST::ForLoopExpr &expr)
58 : : {
59 : 0 : rust_fatal_error (expr.get_locus (), "missing desugar for for-loops");
60 : : rust_unreachable ();
61 : : }
62 : :
63 : : void
64 : 0 : ASTLoweringBase::visit (AST::WhileLetLoopExpr &expr)
65 : : {
66 : 0 : rust_fatal_error (expr.get_locus (), "missing desugar for while-let loops");
67 : : rust_unreachable ();
68 : : }
69 : :
70 : : void
71 : 0 : ASTLoweringBase::visit (AST::Token &)
72 : 0 : {}
73 : : void
74 : 0 : ASTLoweringBase::visit (AST::DelimTokenTree &)
75 : 0 : {}
76 : : void
77 : 0 : ASTLoweringBase::visit (AST::AttrInputMetaItemContainer &)
78 : 0 : {}
79 : : // void ASTLoweringBase::visit(MetaItemmeta_item) {}
80 : : // void vsit(Stmtstmt) {}
81 : : // void ASTLoweringBase::visit(Exprexpr) {}
82 : : void
83 : 0 : ASTLoweringBase::visit (AST::IdentifierExpr &)
84 : 0 : {}
85 : : // void ASTLoweringBase::visit(Patternpattern) {}
86 : : // void ASTLoweringBase::visit(Typetype) {}
87 : : // void ASTLoweringBase::visit(TypeParamBoundtype_param_bound) {}
88 : : void
89 : 0 : ASTLoweringBase::visit (AST::Lifetime &)
90 : 0 : {}
91 : : // void ASTLoweringBase::visit(GenericParamgeneric_param) {}
92 : : void
93 : 0 : ASTLoweringBase::visit (AST::LifetimeParam &)
94 : 0 : {}
95 : : void
96 : 0 : ASTLoweringBase::visit (AST::ConstGenericParam &)
97 : 0 : {}
98 : : // void ASTLoweringBase::visit(TraitItemtrait_item) {}
99 : : // void ASTLoweringBase::visit(InherentImplIteminherent_impl_item) {}
100 : : // void ASTLoweringBase::visit(TraitImplItemtrait_impl_item) {}
101 : :
102 : : // rust-path.h
103 : : void
104 : 0 : ASTLoweringBase::visit (AST::PathInExpression &)
105 : 0 : {}
106 : : void
107 : 0 : ASTLoweringBase::visit (AST::TypePathSegment &)
108 : 0 : {}
109 : : void
110 : 0 : ASTLoweringBase::visit (AST::TypePathSegmentGeneric &)
111 : 0 : {}
112 : : void
113 : 0 : ASTLoweringBase::visit (AST::TypePathSegmentFunction &)
114 : 0 : {}
115 : : void
116 : 0 : ASTLoweringBase::visit (AST::TypePath &)
117 : 0 : {}
118 : : void
119 : 0 : ASTLoweringBase::visit (AST::QualifiedPathInExpression &)
120 : 0 : {}
121 : : void
122 : 0 : ASTLoweringBase::visit (AST::QualifiedPathInType &)
123 : 0 : {}
124 : :
125 : : // rust-expr.h
126 : : void
127 : 0 : ASTLoweringBase::visit (AST::LiteralExpr &)
128 : 0 : {}
129 : : void
130 : 0 : ASTLoweringBase::visit (AST::AttrInputLiteral &)
131 : 0 : {}
132 : : void
133 : 0 : ASTLoweringBase::visit (AST::AttrInputMacro &)
134 : 0 : {}
135 : : void
136 : 0 : ASTLoweringBase::visit (AST::MetaItemLitExpr &)
137 : 0 : {}
138 : : void
139 : 0 : ASTLoweringBase::visit (AST::MetaItemPathExpr &)
140 : 0 : {}
141 : : void
142 : 0 : ASTLoweringBase::visit (AST::BorrowExpr &)
143 : 0 : {}
144 : : void
145 : 0 : ASTLoweringBase::visit (AST::DereferenceExpr &)
146 : 0 : {}
147 : : void
148 : 0 : ASTLoweringBase::visit (AST::NegationExpr &)
149 : 0 : {}
150 : : void
151 : 0 : ASTLoweringBase::visit (AST::ArithmeticOrLogicalExpr &)
152 : 0 : {}
153 : : void
154 : 0 : ASTLoweringBase::visit (AST::ComparisonExpr &)
155 : 0 : {}
156 : : void
157 : 0 : ASTLoweringBase::visit (AST::LazyBooleanExpr &)
158 : 0 : {}
159 : : void
160 : 0 : ASTLoweringBase::visit (AST::TypeCastExpr &)
161 : 0 : {}
162 : : void
163 : 0 : ASTLoweringBase::visit (AST::AssignmentExpr &)
164 : 0 : {}
165 : : void
166 : 0 : ASTLoweringBase::visit (AST::CompoundAssignmentExpr &)
167 : 0 : {}
168 : : void
169 : 0 : ASTLoweringBase::visit (AST::GroupedExpr &)
170 : 0 : {}
171 : : // void ASTLoweringBase::visit(ArrayElemselems) {}
172 : : void
173 : 0 : ASTLoweringBase::visit (AST::ArrayElemsValues &)
174 : 0 : {}
175 : : void
176 : 0 : ASTLoweringBase::visit (AST::ArrayElemsCopied &)
177 : 0 : {}
178 : : void
179 : 0 : ASTLoweringBase::visit (AST::ArrayExpr &)
180 : 0 : {}
181 : : void
182 : 0 : ASTLoweringBase::visit (AST::ArrayIndexExpr &)
183 : 0 : {}
184 : : void
185 : 0 : ASTLoweringBase::visit (AST::TupleExpr &)
186 : 0 : {}
187 : : void
188 : 0 : ASTLoweringBase::visit (AST::TupleIndexExpr &)
189 : 0 : {}
190 : : void
191 : 0 : ASTLoweringBase::visit (AST::StructExprStruct &)
192 : 0 : {}
193 : : // void ASTLoweringBase::visit(StructExprFieldfield) {}
194 : : void
195 : 0 : ASTLoweringBase::visit (AST::StructExprFieldIdentifier &)
196 : 0 : {}
197 : : void
198 : 0 : ASTLoweringBase::visit (AST::StructExprFieldIdentifierValue &)
199 : 0 : {}
200 : : void
201 : 0 : ASTLoweringBase::visit (AST::StructExprFieldIndexValue &)
202 : 0 : {}
203 : : void
204 : 0 : ASTLoweringBase::visit (AST::StructExprStructFields &)
205 : 0 : {}
206 : : void
207 : 0 : ASTLoweringBase::visit (AST::StructExprStructBase &)
208 : 0 : {}
209 : : void
210 : 0 : ASTLoweringBase::visit (AST::CallExpr &)
211 : 0 : {}
212 : : void
213 : 0 : ASTLoweringBase::visit (AST::MethodCallExpr &)
214 : 0 : {}
215 : : void
216 : 0 : ASTLoweringBase::visit (AST::FieldAccessExpr &)
217 : 0 : {}
218 : : void
219 : 0 : ASTLoweringBase::visit (AST::ClosureExprInner &)
220 : 0 : {}
221 : : void
222 : 0 : ASTLoweringBase::visit (AST::BlockExpr &)
223 : 0 : {}
224 : : void
225 : 0 : ASTLoweringBase::visit (AST::AnonConst &)
226 : 0 : {}
227 : : void
228 : 0 : ASTLoweringBase::visit (AST::ConstBlock &)
229 : 0 : {}
230 : : void
231 : 0 : ASTLoweringBase::visit (AST::ClosureExprInnerTyped &)
232 : 0 : {}
233 : : void
234 : 0 : ASTLoweringBase::visit (AST::ContinueExpr &)
235 : 0 : {}
236 : : void
237 : 0 : ASTLoweringBase::visit (AST::BreakExpr &)
238 : 0 : {}
239 : : void
240 : 0 : ASTLoweringBase::visit (AST::RangeFromToExpr &)
241 : 0 : {}
242 : : void
243 : 0 : ASTLoweringBase::visit (AST::RangeFromExpr &)
244 : 0 : {}
245 : : void
246 : 0 : ASTLoweringBase::visit (AST::RangeToExpr &)
247 : 0 : {}
248 : : void
249 : 0 : ASTLoweringBase::visit (AST::RangeFullExpr &)
250 : 0 : {}
251 : : void
252 : 0 : ASTLoweringBase::visit (AST::RangeFromToInclExpr &)
253 : 0 : {}
254 : : void
255 : 0 : ASTLoweringBase::visit (AST::RangeToInclExpr &)
256 : 0 : {}
257 : :
258 : : void
259 : 0 : ASTLoweringBase::visit (AST::BoxExpr &)
260 : 0 : {}
261 : :
262 : : void
263 : 0 : ASTLoweringBase::visit (AST::ReturnExpr &)
264 : 0 : {}
265 : : void
266 : 0 : ASTLoweringBase::visit (AST::UnsafeBlockExpr &)
267 : 0 : {}
268 : : void
269 : 0 : ASTLoweringBase::visit (AST::LoopExpr &)
270 : 0 : {}
271 : : void
272 : 0 : ASTLoweringBase::visit (AST::WhileLoopExpr &)
273 : 0 : {}
274 : : void
275 : 0 : ASTLoweringBase::visit (AST::IfExpr &)
276 : 0 : {}
277 : : void
278 : 0 : ASTLoweringBase::visit (AST::IfExprConseqElse &)
279 : 0 : {}
280 : : void
281 : 0 : ASTLoweringBase::visit (AST::IfLetExpr &)
282 : 0 : {}
283 : : void
284 : 0 : ASTLoweringBase::visit (AST::IfLetExprConseqElse &)
285 : 0 : {}
286 : :
287 : : void
288 : 0 : ASTLoweringBase::visit (AST::InlineAsm &)
289 : 0 : {}
290 : :
291 : : void
292 : 0 : ASTLoweringBase::visit (AST::LlvmInlineAsm &)
293 : 0 : {}
294 : :
295 : : // void ASTLoweringBase::visit(MatchCasematch_case) {}
296 : : // void ASTLoweringBase:: (AST::MatchCaseBlockExpr &) {}
297 : : // void ASTLoweringBase:: (AST::MatchCaseExpr &) {}
298 : : void
299 : 0 : ASTLoweringBase::visit (AST::MatchExpr &)
300 : 0 : {}
301 : : void
302 : 0 : ASTLoweringBase::visit (AST::AwaitExpr &)
303 : 0 : {}
304 : : void
305 : 0 : ASTLoweringBase::visit (AST::AsyncBlockExpr &)
306 : 0 : {}
307 : :
308 : : // rust-item.h
309 : : void
310 : 0 : ASTLoweringBase::visit (AST::TypeParam &)
311 : 0 : {}
312 : : // void ASTLoweringBase::visit(WhereClauseItemitem) {}
313 : : void
314 : 0 : ASTLoweringBase::visit (AST::LifetimeWhereClauseItem &)
315 : 0 : {}
316 : : void
317 : 0 : ASTLoweringBase::visit (AST::TypeBoundWhereClauseItem &)
318 : 0 : {}
319 : : void
320 : 0 : ASTLoweringBase::visit (AST::Module &)
321 : 0 : {}
322 : : void
323 : 0 : ASTLoweringBase::visit (AST::ExternCrate &)
324 : 0 : {}
325 : : // void ASTLoweringBase::visit(UseTreeuse_tree) {}
326 : : void
327 : 0 : ASTLoweringBase::visit (AST::UseTreeGlob &)
328 : 0 : {}
329 : : void
330 : 0 : ASTLoweringBase::visit (AST::UseTreeList &)
331 : 0 : {}
332 : : void
333 : 0 : ASTLoweringBase::visit (AST::UseTreeRebind &)
334 : 0 : {}
335 : : void
336 : 635 : ASTLoweringBase::visit (AST::UseDeclaration &)
337 : 635 : {}
338 : : void
339 : 0 : ASTLoweringBase::visit (AST::Function &)
340 : 0 : {}
341 : : void
342 : 1 : ASTLoweringBase::visit (AST::TypeAlias &)
343 : 1 : {}
344 : : void
345 : 0 : ASTLoweringBase::visit (AST::StructStruct &)
346 : 0 : {}
347 : : void
348 : 0 : ASTLoweringBase::visit (AST::TupleStruct &)
349 : 0 : {}
350 : : void
351 : 0 : ASTLoweringBase::visit (AST::EnumItem &)
352 : 0 : {}
353 : : void
354 : 0 : ASTLoweringBase::visit (AST::EnumItemTuple &)
355 : 0 : {}
356 : : void
357 : 0 : ASTLoweringBase::visit (AST::EnumItemStruct &)
358 : 0 : {}
359 : : void
360 : 0 : ASTLoweringBase::visit (AST::EnumItemDiscriminant &)
361 : 0 : {}
362 : : void
363 : 0 : ASTLoweringBase::visit (AST::Enum &)
364 : 0 : {}
365 : : void
366 : 0 : ASTLoweringBase::visit (AST::Union &)
367 : 0 : {}
368 : : void
369 : 0 : ASTLoweringBase::visit (AST::ConstantItem &)
370 : 0 : {}
371 : : void
372 : 0 : ASTLoweringBase::visit (AST::StaticItem &)
373 : 0 : {}
374 : : void
375 : 0 : ASTLoweringBase::visit (AST::TraitItemType &)
376 : 0 : {}
377 : : void
378 : 0 : ASTLoweringBase::visit (AST::Trait &)
379 : 0 : {}
380 : : void
381 : 0 : ASTLoweringBase::visit (AST::InherentImpl &)
382 : 0 : {}
383 : : void
384 : 0 : ASTLoweringBase::visit (AST::TraitImpl &)
385 : 0 : {}
386 : : // void ASTLoweringBase::visit(ExternalItemitem) {}
387 : : void
388 : 0 : ASTLoweringBase::visit (AST::ExternalTypeItem &)
389 : 0 : {}
390 : : void
391 : 0 : ASTLoweringBase::visit (AST::ExternalStaticItem &)
392 : 0 : {}
393 : : void
394 : 0 : ASTLoweringBase::visit (AST::ExternBlock &)
395 : 0 : {}
396 : :
397 : : // rust-macro.h
398 : : void
399 : 0 : ASTLoweringBase::visit (AST::MacroMatchFragment &)
400 : 0 : {}
401 : : void
402 : 0 : ASTLoweringBase::visit (AST::MacroMatchRepetition &)
403 : 0 : {}
404 : : void
405 : 0 : ASTLoweringBase::visit (AST::MacroMatcher &)
406 : 0 : {}
407 : : void
408 : 0 : ASTLoweringBase::visit (AST::MacroRulesDefinition &)
409 : 0 : {}
410 : : void
411 : 0 : ASTLoweringBase::visit (AST::MetaItemPath &)
412 : 0 : {}
413 : : void
414 : 0 : ASTLoweringBase::visit (AST::MetaItemSeq &)
415 : 0 : {}
416 : : void
417 : 0 : ASTLoweringBase::visit (AST::MetaWord &)
418 : 0 : {}
419 : : void
420 : 0 : ASTLoweringBase::visit (AST::MetaNameValueStr &)
421 : 0 : {}
422 : : void
423 : 0 : ASTLoweringBase::visit (AST::MetaListPaths &)
424 : 0 : {}
425 : : void
426 : 0 : ASTLoweringBase::visit (AST::MetaListNameValueStr &)
427 : 0 : {}
428 : :
429 : : // rust-pattern.h
430 : : void
431 : 0 : ASTLoweringBase::visit (AST::LiteralPattern &)
432 : 0 : {}
433 : : void
434 : 0 : ASTLoweringBase::visit (AST::IdentifierPattern &)
435 : 0 : {}
436 : : void
437 : 0 : ASTLoweringBase::visit (AST::WildcardPattern &)
438 : 0 : {}
439 : : void
440 : 0 : ASTLoweringBase::visit (AST::RestPattern &)
441 : 0 : {}
442 : : // void ASTLoweringBase::visit(RangePatternBoundbound) {}
443 : : void
444 : 0 : ASTLoweringBase::visit (AST::RangePatternBoundLiteral &)
445 : 0 : {}
446 : : void
447 : 0 : ASTLoweringBase::visit (AST::RangePatternBoundPath &)
448 : 0 : {}
449 : : void
450 : 0 : ASTLoweringBase::visit (AST::RangePatternBoundQualPath &)
451 : 0 : {}
452 : : void
453 : 0 : ASTLoweringBase::visit (AST::RangePattern &)
454 : 0 : {}
455 : : void
456 : 0 : ASTLoweringBase::visit (AST::ReferencePattern &)
457 : 0 : {}
458 : : // void ASTLoweringBase::visit(StructPatternFieldfield) {}
459 : : void
460 : 0 : ASTLoweringBase::visit (AST::StructPatternFieldTuplePat &)
461 : 0 : {}
462 : : void
463 : 0 : ASTLoweringBase::visit (AST::StructPatternFieldIdentPat &)
464 : 0 : {}
465 : : void
466 : 0 : ASTLoweringBase::visit (AST::StructPatternFieldIdent &)
467 : 0 : {}
468 : : void
469 : 0 : ASTLoweringBase::visit (AST::StructPattern &)
470 : 0 : {}
471 : : // void ASTLoweringBase::visit(TupleStructItemstuple_items) {}
472 : : void
473 : 0 : ASTLoweringBase::visit (AST::TupleStructItemsNoRest &)
474 : 0 : {}
475 : : void
476 : 0 : ASTLoweringBase::visit (AST::TupleStructItemsHasRest &)
477 : 0 : {}
478 : : void
479 : 0 : ASTLoweringBase::visit (AST::TupleStructPattern &)
480 : 0 : {}
481 : : // void ASTLoweringBase::visit(TuplePatternItemstuple_items) {}
482 : : void
483 : 0 : ASTLoweringBase::visit (AST::TuplePatternItemsNoRest &)
484 : 0 : {}
485 : : void
486 : 0 : ASTLoweringBase::visit (AST::TuplePatternItemsHasRest &)
487 : 0 : {}
488 : : void
489 : 0 : ASTLoweringBase::visit (AST::TuplePattern &)
490 : 0 : {}
491 : : void
492 : 0 : ASTLoweringBase::visit (AST::GroupedPattern &)
493 : 0 : {}
494 : : void
495 : 0 : ASTLoweringBase::visit (AST::SlicePatternItemsNoRest &)
496 : 0 : {}
497 : : void
498 : 0 : ASTLoweringBase::visit (AST::SlicePatternItemsHasRest &)
499 : 0 : {}
500 : : void
501 : 0 : ASTLoweringBase::visit (AST::SlicePattern &)
502 : 0 : {}
503 : : void
504 : 0 : ASTLoweringBase::visit (AST::AltPattern &)
505 : 0 : {}
506 : :
507 : : // rust-stmt.h
508 : : void
509 : 0 : ASTLoweringBase::visit (AST::EmptyStmt &)
510 : 0 : {}
511 : : void
512 : 0 : ASTLoweringBase::visit (AST::LetStmt &)
513 : 0 : {}
514 : : void
515 : 0 : ASTLoweringBase::visit (AST::ExprStmt &)
516 : 0 : {}
517 : :
518 : : // rust-type.h
519 : : void
520 : 0 : ASTLoweringBase::visit (AST::TraitBound &)
521 : 0 : {}
522 : : void
523 : 0 : ASTLoweringBase::visit (AST::ImplTraitType &)
524 : 0 : {}
525 : : void
526 : 0 : ASTLoweringBase::visit (AST::TraitObjectType &)
527 : 0 : {}
528 : : void
529 : 0 : ASTLoweringBase::visit (AST::ParenthesisedType &)
530 : 0 : {}
531 : : void
532 : 0 : ASTLoweringBase::visit (AST::ImplTraitTypeOneBound &)
533 : 0 : {}
534 : : void
535 : 0 : ASTLoweringBase::visit (AST::TraitObjectTypeOneBound &)
536 : 0 : {}
537 : : void
538 : 0 : ASTLoweringBase::visit (AST::TupleType &)
539 : 0 : {}
540 : : void
541 : 0 : ASTLoweringBase::visit (AST::NeverType &)
542 : 0 : {}
543 : : void
544 : 0 : ASTLoweringBase::visit (AST::RawPointerType &)
545 : 0 : {}
546 : : void
547 : 0 : ASTLoweringBase::visit (AST::ReferenceType &)
548 : 0 : {}
549 : : void
550 : 0 : ASTLoweringBase::visit (AST::ArrayType &)
551 : 0 : {}
552 : : void
553 : 0 : ASTLoweringBase::visit (AST::SliceType &)
554 : 0 : {}
555 : : void
556 : 0 : ASTLoweringBase::visit (AST::InferredType &)
557 : 0 : {}
558 : : void
559 : 0 : ASTLoweringBase::visit (AST::BareFunctionType &)
560 : 0 : {}
561 : :
562 : : void
563 : 0 : ASTLoweringBase::visit (AST::FunctionParam ¶m)
564 : 0 : {}
565 : :
566 : : void
567 : 0 : ASTLoweringBase::visit (AST::VariadicParam ¶m)
568 : 0 : {}
569 : :
570 : : void
571 : 0 : ASTLoweringBase::visit (AST::SelfParam ¶m)
572 : 0 : {}
573 : :
574 : : void
575 : 0 : ASTLoweringBase::visit (AST::FormatArgs &fmt)
576 : 0 : {}
577 : :
578 : : void
579 : 0 : ASTLoweringBase::visit (AST::OffsetOf &offset_of)
580 : 0 : {}
581 : :
582 : : HIR::Lifetime
583 : 9074 : ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
584 : : bool default_to_static_lifetime)
585 : : {
586 : 9074 : auto lifetime_type = lifetime.get_lifetime_type ();
587 : 9074 : if (lifetime_type == AST::Lifetime::WILDCARD && default_to_static_lifetime)
588 : : {
589 : : // If compiling in a static context.
590 : 11 : lifetime_type = AST::Lifetime::STATIC;
591 : : }
592 : :
593 : 9074 : auto crate_num = mappings.get_current_crate ();
594 : 9074 : Analysis::NodeMapping mapping (crate_num, lifetime.get_node_id (),
595 : 9074 : mappings.get_next_hir_id (crate_num),
596 : 9074 : UNKNOWN_LOCAL_DEFID);
597 : 9074 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
598 : :
599 : 18148 : return HIR::Lifetime (mapping, lifetime_type, lifetime.get_lifetime_name (),
600 : 18148 : lifetime.get_locus ());
601 : : }
602 : :
603 : : HIR::LoopLabel
604 : 30 : ASTLoweringBase::lower_loop_label (AST::LoopLabel &loop_label)
605 : : {
606 : 30 : HIR::Lifetime life = lower_lifetime (loop_label.get_lifetime ());
607 : :
608 : 30 : auto crate_num = mappings.get_current_crate ();
609 : 30 : Analysis::NodeMapping mapping (crate_num, loop_label.get_node_id (),
610 : 30 : mappings.get_next_hir_id (crate_num),
611 : 30 : UNKNOWN_LOCAL_DEFID);
612 : 30 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
613 : :
614 : 30 : return HIR::LoopLabel (mapping, std::move (life), loop_label.get_locus ());
615 : 30 : }
616 : :
617 : : std::vector<std::unique_ptr<HIR::GenericParam>>
618 : 4218 : ASTLoweringBase::lower_generic_params (
619 : : std::vector<std::unique_ptr<AST::GenericParam>> ¶ms)
620 : : {
621 : 4218 : std::vector<std::unique_ptr<HIR::GenericParam>> lowered;
622 : 4218 : lowered.reserve (params.size ());
623 : :
624 : 8843 : for (auto &ast_param : params)
625 : 4625 : lowered.emplace_back (ASTLowerGenericParam::translate (*ast_param));
626 : :
627 : 4218 : return lowered;
628 : : }
629 : :
630 : : HIR::PathExprSegment
631 : 36074 : ASTLoweringBase::lower_path_expr_seg (AST::PathExprSegment &s)
632 : : {
633 : 36074 : auto crate_num = mappings.get_current_crate ();
634 : 36074 : Analysis::NodeMapping mapping (crate_num, s.get_node_id (),
635 : 36074 : mappings.get_next_hir_id (crate_num),
636 : 36074 : UNKNOWN_LOCAL_DEFID);
637 : :
638 : 36074 : return HIR::PathExprSegment (
639 : : std::move (mapping),
640 : 108222 : HIR::PathIdentSegment (s.get_ident_segment ().as_string ()), s.get_locus (),
641 : 72148 : s.has_generic_args () ? lower_generic_args (s.get_generic_args ())
642 : 72148 : : HIR::GenericArgs::create_empty ());
643 : : }
644 : :
645 : : HIR::GenericArgsBinding
646 : 47 : ASTLoweringBase::lower_binding (AST::GenericArgsBinding &binding)
647 : : {
648 : 47 : HIR::Type *lowered_type = ASTLoweringType::translate (binding.get_type ());
649 : 47 : return HIR::GenericArgsBinding (binding.get_identifier (),
650 : 94 : std::unique_ptr<HIR::Type> (lowered_type),
651 : 47 : binding.get_locus ());
652 : : }
653 : :
654 : : HIR::GenericArgs
655 : 3599 : ASTLoweringBase::lower_generic_args (AST::GenericArgs &args)
656 : : {
657 : 3599 : std::vector<HIR::GenericArgsBinding> binding_args;
658 : 3599 : binding_args.reserve (args.get_binding_args ().size ());
659 : :
660 : 3646 : for (auto &binding : args.get_binding_args ())
661 : 94 : binding_args.emplace_back (lower_binding (binding));
662 : :
663 : 3599 : std::vector<HIR::Lifetime> lifetime_args;
664 : 3599 : lifetime_args.reserve (args.get_lifetime_args ().size ());
665 : :
666 : 3630 : for (auto &lifetime : args.get_lifetime_args ())
667 : 31 : lifetime_args.emplace_back (lower_lifetime (lifetime));
668 : :
669 : 3599 : std::vector<std::unique_ptr<HIR::Type>> type_args;
670 : 3599 : std::vector<HIR::ConstGenericArg> const_args;
671 : :
672 : 7409 : for (auto &arg : args.get_generic_args ())
673 : : {
674 : 3810 : switch (arg.get_kind ())
675 : : {
676 : 3692 : case AST::GenericArg::Kind::Type:
677 : 3692 : {
678 : 7384 : type_args.emplace_back (
679 : 3692 : ASTLoweringType::translate (arg.get_type ()));
680 : 3692 : break;
681 : : }
682 : 118 : case AST::GenericArg::Kind::Const:
683 : 118 : {
684 : 118 : auto expr = ASTLoweringExpr::translate (arg.get_expression ());
685 : 118 : const_args.emplace_back (std::unique_ptr<HIR::Expr> (expr),
686 : 118 : expr->get_locus ());
687 : 118 : break;
688 : : }
689 : 0 : default:
690 : 0 : rust_unreachable ();
691 : : }
692 : : }
693 : :
694 : 3599 : return HIR::GenericArgs (std::move (lifetime_args), std::move (type_args),
695 : : std::move (binding_args), std::move (const_args),
696 : 3599 : args.get_locus ());
697 : 3599 : }
698 : :
699 : : HIR::SelfParam
700 : 7955 : ASTLoweringBase::lower_self (AST::Param ¶m)
701 : : {
702 : 7955 : rust_assert (param.is_self ());
703 : :
704 : 7955 : auto self = static_cast<AST::SelfParam &> (param);
705 : 7955 : auto crate_num = mappings.get_current_crate ();
706 : 7955 : Analysis::NodeMapping mapping (crate_num, self.get_node_id (),
707 : 7955 : mappings.get_next_hir_id (crate_num),
708 : 7955 : mappings.get_next_localdef_id (crate_num));
709 : 7955 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
710 : :
711 : 7955 : if (self.has_type ())
712 : : {
713 : 1 : HIR::Type *type = ASTLoweringType::translate (self.get_type ());
714 : 1 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (type),
715 : 1 : self.get_is_mut (), self.get_locus ());
716 : : }
717 : 7954 : else if (!self.get_has_ref ())
718 : : {
719 : 3088 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (nullptr),
720 : 3088 : self.get_is_mut (), self.get_locus ());
721 : : }
722 : :
723 : 4866 : tl::optional<HIR::Lifetime> lifetime = tl::nullopt;
724 : :
725 : 4866 : if (self.has_lifetime ())
726 : 4606 : lifetime = lower_lifetime (self.get_lifetime ());
727 : :
728 : 4866 : return HIR::SelfParam (mapping, lifetime, self.get_is_mut (),
729 : 9472 : self.get_locus ());
730 : 7955 : }
731 : :
732 : : HIR::Type *
733 : 5101 : ASTLoweringBase::lower_type_no_bounds (AST::TypeNoBounds &type)
734 : : {
735 : 5101 : return ASTLoweringType::translate (type);
736 : : }
737 : :
738 : : HIR::TypeParamBound *
739 : 1431 : ASTLoweringBase::lower_bound (AST::TypeParamBound &bound)
740 : : {
741 : 1431 : return ASTLoweringTypeBounds::translate (bound);
742 : : }
743 : :
744 : : /* Checks whether the name of a field already exists. Returns true
745 : : and produces an error if so. */
746 : : bool
747 : 2271 : struct_field_name_exists (std::vector<HIR::StructField> &fields,
748 : : HIR::StructField &new_field)
749 : : {
750 : 6298 : for (auto &field : fields)
751 : : {
752 : 8070 : if (field.get_field_name ().as_string ().compare (
753 : 4035 : new_field.get_field_name ().as_string ())
754 : 4035 : == 0)
755 : : {
756 : 8 : rich_location r (line_table, new_field.get_locus ());
757 : 8 : r.add_range (field.get_locus ());
758 : 8 : rust_error_at (r, ErrorCode::E0124, "field %qs is already declared",
759 : 8 : field.get_field_name ().as_string ().c_str ());
760 : 8 : return true;
761 : 8 : }
762 : : }
763 : : return false;
764 : : }
765 : :
766 : : HIR::FunctionQualifiers
767 : 15740 : ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers)
768 : : {
769 : 15740 : Unsafety unsafety
770 : 15740 : = qualifiers.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
771 : 15740 : bool has_extern = qualifiers.is_extern ();
772 : 15740 : ABI abi = has_extern ? ABI::C : ABI::RUST;
773 : :
774 : 15740 : if (qualifiers.has_abi ())
775 : : {
776 : 80 : const std::string &extern_abi = qualifiers.get_extern_abi ();
777 : 80 : abi = get_abi_from_string (extern_abi);
778 : 80 : if (has_extern && abi == ABI::UNKNOWN)
779 : 1 : rust_error_at (qualifiers.get_locus (), ErrorCode::E0703,
780 : : "invalid ABI: found %qs", extern_abi.c_str ());
781 : 80 : }
782 : :
783 : 15740 : return HIR::FunctionQualifiers (qualifiers.get_async_status (),
784 : : qualifiers.get_const_status (), unsafety,
785 : 15740 : has_extern, abi);
786 : : }
787 : :
788 : : void
789 : 33220 : ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
790 : : {
791 : 47687 : for (const auto &attr : item.get_outer_attrs ())
792 : : {
793 : 14467 : const auto &str_path = attr.get_path ().as_string ();
794 : 14467 : if (!Analysis::Attributes::is_known (str_path))
795 : : {
796 : 0 : rust_error_at (attr.get_locus (), "unknown attribute");
797 : 0 : continue;
798 : : }
799 : :
800 : 14467 : bool is_lang_item = str_path == Values::Attributes::LANG
801 : 3182 : && attr.has_attr_input ()
802 : 17649 : && attr.get_attr_input ().get_attr_input_type ()
803 : 14467 : == AST::AttrInput::AttrInputType::LITERAL;
804 : :
805 : 14467 : bool is_doc_item = str_path == Values::Attributes::DOC;
806 : :
807 : 14467 : if (is_doc_item)
808 : 8368 : handle_doc_item_attribute (item, attr);
809 : 6099 : else if (is_lang_item)
810 : 3182 : handle_lang_item_attribute (item, attr);
811 : 2917 : else if (!attribute_handled_in_another_pass (str_path))
812 : : {
813 : 0 : rust_error_at (attr.get_locus (), "unhandled attribute: [%s]",
814 : 0 : attr.get_path ().as_string ().c_str ());
815 : : }
816 : 14467 : }
817 : 33220 : }
818 : :
819 : : void
820 : 8368 : ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &,
821 : : const AST::Attribute &attr)
822 : : {
823 : 8368 : auto simple_doc_comment = attr.has_attr_input ()
824 : 8368 : && attr.get_attr_input ().get_attr_input_type ()
825 : 9132 : == AST::AttrInput::AttrInputType::LITERAL;
826 : 764 : if (simple_doc_comment)
827 : : return;
828 : :
829 : 764 : const AST::AttrInput &input = attr.get_attr_input ();
830 : 764 : bool is_token_tree
831 : 764 : = input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
832 : 764 : rust_assert (is_token_tree);
833 : 764 : const auto &option = static_cast<const AST::DelimTokenTree &> (input);
834 : 764 : AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
835 : :
836 : : // TODO: add actual and complete checks for the doc attributes
837 : : //
838 : : // FIXME: Move this to the AttributeChecker visitor
839 : 764 : rust_assert (meta_item);
840 : : }
841 : :
842 : : void
843 : 3182 : ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
844 : : const AST::Attribute &attr)
845 : : {
846 : 3182 : auto lang_item_type_str = Analysis::Attributes::extract_string_literal (attr);
847 : 3182 : rust_assert (lang_item_type_str.has_value ());
848 : :
849 : 3182 : auto lang_item_type = LangItem::Parse (*lang_item_type_str);
850 : :
851 : 3182 : if (lang_item_type)
852 : 3182 : mappings.insert_lang_item (*lang_item_type,
853 : 3182 : item.get_mappings ().get_defid ());
854 : : else
855 : 0 : rust_error_at (attr.get_locus (), "unknown lang item");
856 : 3182 : }
857 : :
858 : : bool
859 : 2917 : ASTLoweringBase::attribute_handled_in_another_pass (
860 : : const std::string &attribute_path) const
861 : : {
862 : 2917 : const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
863 : 2917 : if (lookup.is_error ())
864 : : return false;
865 : :
866 : 2917 : if (lookup.handler == Analysis::CompilerPass::UNKNOWN)
867 : : return false;
868 : :
869 : 2917 : return lookup.handler != Analysis::CompilerPass::HIR_LOWERING;
870 : : }
871 : :
872 : : std::unique_ptr<HIR::TuplePatternItems>
873 : 393 : ASTLoweringBase::lower_tuple_pattern_multiple (
874 : : AST::TuplePatternItemsNoRest &pattern)
875 : : {
876 : 393 : std::vector<std::unique_ptr<HIR::Pattern>> patterns;
877 : 393 : patterns.reserve (pattern.get_patterns ().size ());
878 : :
879 : 1193 : for (auto &p : pattern.get_patterns ())
880 : 800 : patterns.emplace_back (ASTLoweringPattern::translate (*p));
881 : :
882 : 393 : return std::unique_ptr<HIR::TuplePatternItems> (
883 : 393 : new HIR::TuplePatternItemsNoRest (std::move (patterns)));
884 : 393 : }
885 : :
886 : : std::unique_ptr<TuplePatternItems>
887 : 27 : ASTLoweringBase::lower_tuple_pattern_ranged (
888 : : AST::TuplePatternItemsHasRest &pattern)
889 : : {
890 : 27 : std::vector<std::unique_ptr<HIR::Pattern>> lower_patterns;
891 : 27 : lower_patterns.reserve (pattern.get_lower_patterns ().size ());
892 : 27 : std::vector<std::unique_ptr<HIR::Pattern>> upper_patterns;
893 : 27 : upper_patterns.reserve (pattern.get_upper_patterns ().size ());
894 : :
895 : 55 : for (auto &p : pattern.get_lower_patterns ())
896 : 28 : lower_patterns.emplace_back (ASTLoweringPattern::translate (*p));
897 : :
898 : 59 : for (auto &p : pattern.get_upper_patterns ())
899 : 32 : upper_patterns.emplace_back (ASTLoweringPattern::translate (*p));
900 : :
901 : 27 : return std::unique_ptr<HIR::TuplePatternItems> (
902 : : new HIR::TuplePatternItemsHasRest (std::move (lower_patterns),
903 : 27 : std::move (upper_patterns)));
904 : 27 : }
905 : :
906 : : std::unique_ptr<HIR::SlicePatternItems>
907 : 32 : ASTLoweringBase::lower_slice_pattern_no_rest (
908 : : AST::SlicePatternItemsNoRest &pattern)
909 : : {
910 : 32 : std::vector<std::unique_ptr<HIR::Pattern>> patterns;
911 : 32 : patterns.reserve (pattern.get_patterns ().size ());
912 : 96 : for (auto &p : pattern.get_patterns ())
913 : 64 : patterns.emplace_back (ASTLoweringPattern::translate (*p));
914 : :
915 : 32 : return std::unique_ptr<HIR::SlicePatternItems> (
916 : 32 : new HIR::SlicePatternItemsNoRest (std::move (patterns)));
917 : 32 : }
918 : :
919 : : std::unique_ptr<HIR::SlicePatternItems>
920 : 44 : ASTLoweringBase::lower_slice_pattern_has_rest (
921 : : AST::SlicePatternItemsHasRest &pattern)
922 : : {
923 : 44 : std::vector<std::unique_ptr<HIR::Pattern>> lower_patterns;
924 : 44 : lower_patterns.reserve (pattern.get_lower_patterns ().size ());
925 : 44 : std::vector<std::unique_ptr<HIR::Pattern>> upper_patterns;
926 : 44 : upper_patterns.reserve (pattern.get_upper_patterns ().size ());
927 : :
928 : 87 : for (auto &p : pattern.get_lower_patterns ())
929 : 43 : lower_patterns.emplace_back (
930 : 43 : std::unique_ptr<HIR::Pattern> (ASTLoweringPattern::translate (*p)));
931 : :
932 : 87 : for (auto &p : pattern.get_upper_patterns ())
933 : 43 : upper_patterns.emplace_back (
934 : 43 : std::unique_ptr<HIR::Pattern> (ASTLoweringPattern::translate (*p)));
935 : :
936 : 44 : return std::unique_ptr<HIR::SlicePatternItems> (
937 : : new HIR::SlicePatternItemsHasRest (std::move (lower_patterns),
938 : 44 : std::move (upper_patterns)));
939 : 44 : }
940 : :
941 : : std::unique_ptr<HIR::RangePatternBound>
942 : 76 : ASTLoweringBase::lower_range_pattern_bound (AST::RangePatternBound &bound)
943 : : {
944 : 76 : std::unique_ptr<HIR::RangePatternBound> hir_bound = nullptr;
945 : 76 : switch (bound.get_bound_type ())
946 : : {
947 : 55 : case AST::RangePatternBound::RangePatternBoundType::LITERAL:
948 : 55 : {
949 : 55 : AST::RangePatternBoundLiteral &ref
950 : : = static_cast<AST::RangePatternBoundLiteral &> (bound);
951 : :
952 : 55 : HIR::Literal literal = lower_literal (ref.get_literal ());
953 : :
954 : 55 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
955 : : new HIR::RangePatternBoundLiteral (literal, ref.get_locus (),
956 : 110 : ref.get_has_minus ()));
957 : 55 : }
958 : 55 : break;
959 : 21 : case AST::RangePatternBound::RangePatternBoundType::PATH:
960 : 21 : {
961 : 21 : auto &ref = static_cast<AST::RangePatternBoundPath &> (bound);
962 : :
963 : 21 : HIR::PathInExpression *path
964 : 21 : = ASTLowerPathInExpression::translate (ref.get_path ());
965 : :
966 : 63 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
967 : 21 : new HIR::RangePatternBoundPath (*path));
968 : : }
969 : 21 : break;
970 : 0 : case AST::RangePatternBound::RangePatternBoundType::QUALPATH:
971 : 0 : {
972 : 0 : auto &ref = static_cast<AST::RangePatternBoundQualPath &> (bound);
973 : :
974 : 0 : HIR::QualifiedPathInExpression *qualpath
975 : 0 : = ASTLowerQualPathInExpression::translate (ref.get_qualified_path ());
976 : :
977 : 0 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
978 : 0 : new HIR::RangePatternBoundQualPath (*qualpath));
979 : : }
980 : 0 : break;
981 : : }
982 : :
983 : 76 : return hir_bound;
984 : : }
985 : :
986 : : HIR::Literal
987 : 18932 : ASTLoweringBase::lower_literal (const AST::Literal &literal)
988 : : {
989 : 18932 : HIR::Literal::LitType type = HIR::Literal::LitType::CHAR;
990 : 18932 : switch (literal.get_lit_type ())
991 : : {
992 : : case AST::Literal::LitType::CHAR:
993 : : type = HIR::Literal::LitType::CHAR;
994 : : break;
995 : : case AST::Literal::LitType::STRING:
996 : 2377 : type = HIR::Literal::LitType::STRING;
997 : : break;
998 : 408 : case AST::Literal::LitType::BYTE:
999 : 408 : type = HIR::Literal::LitType::BYTE;
1000 : 408 : break;
1001 : 35 : case AST::Literal::LitType::BYTE_STRING:
1002 : 35 : type = HIR::Literal::LitType::BYTE_STRING;
1003 : 35 : break;
1004 : : case AST::Literal::LitType::RAW_STRING:
1005 : 2377 : type = HIR::Literal::LitType::STRING;
1006 : : break;
1007 : 14303 : case AST::Literal::LitType::INT:
1008 : 14303 : type = HIR::Literal::LitType::INT;
1009 : 14303 : break;
1010 : 326 : case AST::Literal::LitType::FLOAT:
1011 : 326 : type = HIR::Literal::LitType::FLOAT;
1012 : 326 : break;
1013 : 1297 : case AST::Literal::LitType::BOOL:
1014 : 1297 : type = HIR::Literal::LitType::BOOL;
1015 : 1297 : break;
1016 : 0 : case AST::Literal::LitType::ERROR:
1017 : 0 : rust_unreachable ();
1018 : 18932 : break;
1019 : : }
1020 : :
1021 : 37864 : return HIR::Literal (literal.as_string (), type, literal.get_type_hint ());
1022 : : }
1023 : :
1024 : : HIR::ExternBlock *
1025 : 1467 : ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block)
1026 : : {
1027 : 1467 : HIR::Visibility vis = translate_visibility (extern_block.get_visibility ());
1028 : :
1029 : 1467 : auto crate_num = mappings.get_current_crate ();
1030 : 2934 : Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (),
1031 : 1467 : mappings.get_next_hir_id (crate_num),
1032 : 1467 : mappings.get_next_localdef_id (crate_num));
1033 : :
1034 : 1467 : std::vector<std::unique_ptr<HIR::ExternalItem>> extern_items;
1035 : 1467 : extern_items.reserve (extern_block.get_extern_items ().size ());
1036 : :
1037 : 3679 : for (auto &item : extern_block.get_extern_items ())
1038 : : {
1039 : 2212 : if (item->is_marked_for_strip ())
1040 : 0 : continue;
1041 : :
1042 : 4424 : extern_items.emplace_back (
1043 : 2212 : ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ()));
1044 : : }
1045 : :
1046 : 1467 : ABI abi = ABI::C;
1047 : 1467 : if (extern_block.has_abi ())
1048 : : {
1049 : 1467 : const std::string &extern_abi = extern_block.get_abi ();
1050 : 1467 : abi = get_abi_from_string (extern_abi);
1051 : 1467 : if (abi == ABI::UNKNOWN)
1052 : 1 : rust_error_at (extern_block.get_locus (), ErrorCode::E0703,
1053 : : "invalid ABI: found %qs", extern_abi.c_str ());
1054 : 1467 : }
1055 : :
1056 : 1467 : HIR::ExternBlock *hir_extern_block
1057 : : = new HIR::ExternBlock (mapping, abi, std::move (extern_items),
1058 : 1467 : std::move (vis), extern_block.get_inner_attrs (),
1059 : 1467 : extern_block.get_outer_attrs (),
1060 : 1467 : extern_block.get_locus ());
1061 : :
1062 : 1467 : mappings.insert_hir_extern_block (hir_extern_block);
1063 : :
1064 : 2934 : return hir_extern_block;
1065 : 1467 : }
1066 : :
1067 : : void
1068 : 903 : ASTLoweringBase::lower_macro_definition (AST::MacroRulesDefinition &def)
1069 : : {
1070 : 903 : auto is_export = false;
1071 : 1041 : for (const auto &attr : def.get_outer_attrs ())
1072 : 138 : if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
1073 : 2 : is_export = true;
1074 : :
1075 : 903 : if (is_export)
1076 : : {
1077 : 2 : mappings.insert_exported_macro (def);
1078 : 2 : mappings.insert_ast_item (&def);
1079 : 2 : mappings.insert_location (def.get_node_id (), def.get_locus ());
1080 : : }
1081 : 903 : }
1082 : :
1083 : : } // namespace HIR
1084 : : } // namespace Rust
|