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 : 627 : ASTLoweringBase::visit (AST::UseDeclaration &)
337 : 627 : {}
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::TraitItemConst &)
376 : 0 : {}
377 : : void
378 : 0 : ASTLoweringBase::visit (AST::TraitItemType &)
379 : 0 : {}
380 : : void
381 : 0 : ASTLoweringBase::visit (AST::Trait &)
382 : 0 : {}
383 : : void
384 : 0 : ASTLoweringBase::visit (AST::InherentImpl &)
385 : 0 : {}
386 : : void
387 : 0 : ASTLoweringBase::visit (AST::TraitImpl &)
388 : 0 : {}
389 : : // void ASTLoweringBase::visit(ExternalItemitem) {}
390 : : void
391 : 0 : ASTLoweringBase::visit (AST::ExternalTypeItem &)
392 : 0 : {}
393 : : void
394 : 0 : ASTLoweringBase::visit (AST::ExternalStaticItem &)
395 : 0 : {}
396 : : void
397 : 0 : ASTLoweringBase::visit (AST::ExternBlock &)
398 : 0 : {}
399 : :
400 : : // rust-macro.h
401 : : void
402 : 0 : ASTLoweringBase::visit (AST::MacroMatchFragment &)
403 : 0 : {}
404 : : void
405 : 0 : ASTLoweringBase::visit (AST::MacroMatchRepetition &)
406 : 0 : {}
407 : : void
408 : 0 : ASTLoweringBase::visit (AST::MacroMatcher &)
409 : 0 : {}
410 : : void
411 : 0 : ASTLoweringBase::visit (AST::MacroRulesDefinition &)
412 : 0 : {}
413 : : void
414 : 0 : ASTLoweringBase::visit (AST::MetaItemPath &)
415 : 0 : {}
416 : : void
417 : 0 : ASTLoweringBase::visit (AST::MetaItemSeq &)
418 : 0 : {}
419 : : void
420 : 0 : ASTLoweringBase::visit (AST::MetaWord &)
421 : 0 : {}
422 : : void
423 : 0 : ASTLoweringBase::visit (AST::MetaNameValueStr &)
424 : 0 : {}
425 : : void
426 : 0 : ASTLoweringBase::visit (AST::MetaListPaths &)
427 : 0 : {}
428 : : void
429 : 0 : ASTLoweringBase::visit (AST::MetaListNameValueStr &)
430 : 0 : {}
431 : :
432 : : // rust-pattern.h
433 : : void
434 : 0 : ASTLoweringBase::visit (AST::LiteralPattern &)
435 : 0 : {}
436 : : void
437 : 0 : ASTLoweringBase::visit (AST::IdentifierPattern &)
438 : 0 : {}
439 : : void
440 : 0 : ASTLoweringBase::visit (AST::WildcardPattern &)
441 : 0 : {}
442 : : void
443 : 0 : ASTLoweringBase::visit (AST::RestPattern &)
444 : 0 : {}
445 : : // void ASTLoweringBase::visit(RangePatternBoundbound) {}
446 : : void
447 : 0 : ASTLoweringBase::visit (AST::RangePatternBoundLiteral &)
448 : 0 : {}
449 : : void
450 : 0 : ASTLoweringBase::visit (AST::RangePatternBoundPath &)
451 : 0 : {}
452 : : void
453 : 0 : ASTLoweringBase::visit (AST::RangePatternBoundQualPath &)
454 : 0 : {}
455 : : void
456 : 0 : ASTLoweringBase::visit (AST::RangePattern &)
457 : 0 : {}
458 : : void
459 : 0 : ASTLoweringBase::visit (AST::ReferencePattern &)
460 : 0 : {}
461 : : // void ASTLoweringBase::visit(StructPatternFieldfield) {}
462 : : void
463 : 0 : ASTLoweringBase::visit (AST::StructPatternFieldTuplePat &)
464 : 0 : {}
465 : : void
466 : 0 : ASTLoweringBase::visit (AST::StructPatternFieldIdentPat &)
467 : 0 : {}
468 : : void
469 : 0 : ASTLoweringBase::visit (AST::StructPatternFieldIdent &)
470 : 0 : {}
471 : : void
472 : 0 : ASTLoweringBase::visit (AST::StructPattern &)
473 : 0 : {}
474 : : // void ASTLoweringBase::visit(TupleStructItemstuple_items) {}
475 : : void
476 : 0 : ASTLoweringBase::visit (AST::TupleStructItemsNoRange &)
477 : 0 : {}
478 : : void
479 : 0 : ASTLoweringBase::visit (AST::TupleStructItemsRange &)
480 : 0 : {}
481 : : void
482 : 0 : ASTLoweringBase::visit (AST::TupleStructPattern &)
483 : 0 : {}
484 : : // void ASTLoweringBase::visit(TuplePatternItemstuple_items) {}
485 : : void
486 : 0 : ASTLoweringBase::visit (AST::TuplePatternItemsMultiple &)
487 : 0 : {}
488 : : void
489 : 0 : ASTLoweringBase::visit (AST::TuplePatternItemsRanged &)
490 : 0 : {}
491 : : void
492 : 0 : ASTLoweringBase::visit (AST::TuplePattern &)
493 : 0 : {}
494 : : void
495 : 0 : ASTLoweringBase::visit (AST::GroupedPattern &)
496 : 0 : {}
497 : : void
498 : 0 : ASTLoweringBase::visit (AST::SlicePatternItemsNoRest &)
499 : 0 : {}
500 : : void
501 : 0 : ASTLoweringBase::visit (AST::SlicePatternItemsHasRest &)
502 : 0 : {}
503 : : void
504 : 0 : ASTLoweringBase::visit (AST::SlicePattern &)
505 : 0 : {}
506 : : void
507 : 0 : ASTLoweringBase::visit (AST::AltPattern &)
508 : 0 : {}
509 : :
510 : : // rust-stmt.h
511 : : void
512 : 0 : ASTLoweringBase::visit (AST::EmptyStmt &)
513 : 0 : {}
514 : : void
515 : 0 : ASTLoweringBase::visit (AST::LetStmt &)
516 : 0 : {}
517 : : void
518 : 0 : ASTLoweringBase::visit (AST::ExprStmt &)
519 : 0 : {}
520 : :
521 : : // rust-type.h
522 : : void
523 : 0 : ASTLoweringBase::visit (AST::TraitBound &)
524 : 0 : {}
525 : : void
526 : 0 : ASTLoweringBase::visit (AST::ImplTraitType &)
527 : 0 : {}
528 : : void
529 : 0 : ASTLoweringBase::visit (AST::TraitObjectType &)
530 : 0 : {}
531 : : void
532 : 0 : ASTLoweringBase::visit (AST::ParenthesisedType &)
533 : 0 : {}
534 : : void
535 : 0 : ASTLoweringBase::visit (AST::ImplTraitTypeOneBound &)
536 : 0 : {}
537 : : void
538 : 0 : ASTLoweringBase::visit (AST::TraitObjectTypeOneBound &)
539 : 0 : {}
540 : : void
541 : 0 : ASTLoweringBase::visit (AST::TupleType &)
542 : 0 : {}
543 : : void
544 : 0 : ASTLoweringBase::visit (AST::NeverType &)
545 : 0 : {}
546 : : void
547 : 0 : ASTLoweringBase::visit (AST::RawPointerType &)
548 : 0 : {}
549 : : void
550 : 0 : ASTLoweringBase::visit (AST::ReferenceType &)
551 : 0 : {}
552 : : void
553 : 0 : ASTLoweringBase::visit (AST::ArrayType &)
554 : 0 : {}
555 : : void
556 : 0 : ASTLoweringBase::visit (AST::SliceType &)
557 : 0 : {}
558 : : void
559 : 0 : ASTLoweringBase::visit (AST::InferredType &)
560 : 0 : {}
561 : : void
562 : 0 : ASTLoweringBase::visit (AST::BareFunctionType &)
563 : 0 : {}
564 : :
565 : : void
566 : 0 : ASTLoweringBase::visit (AST::FunctionParam ¶m)
567 : 0 : {}
568 : :
569 : : void
570 : 0 : ASTLoweringBase::visit (AST::VariadicParam ¶m)
571 : 0 : {}
572 : :
573 : : void
574 : 0 : ASTLoweringBase::visit (AST::SelfParam ¶m)
575 : 0 : {}
576 : :
577 : : void
578 : 0 : ASTLoweringBase::visit (AST::FormatArgs &fmt)
579 : 0 : {}
580 : :
581 : : void
582 : 0 : ASTLoweringBase::visit (AST::OffsetOf &offset_of)
583 : 0 : {}
584 : :
585 : : HIR::Lifetime
586 : 9007 : ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
587 : : bool default_to_static_lifetime)
588 : : {
589 : 9007 : auto lifetime_type = lifetime.get_lifetime_type ();
590 : 9007 : if (lifetime_type == AST::Lifetime::WILDCARD && default_to_static_lifetime)
591 : : {
592 : : // If compiling in a static context.
593 : 10 : lifetime_type = AST::Lifetime::STATIC;
594 : : }
595 : :
596 : 9007 : auto crate_num = mappings.get_current_crate ();
597 : 9007 : Analysis::NodeMapping mapping (crate_num, lifetime.get_node_id (),
598 : 9007 : mappings.get_next_hir_id (crate_num),
599 : 9007 : UNKNOWN_LOCAL_DEFID);
600 : 9007 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
601 : :
602 : 18014 : return HIR::Lifetime (mapping, lifetime_type, lifetime.get_lifetime_name (),
603 : 18014 : lifetime.get_locus ());
604 : : }
605 : :
606 : : HIR::LoopLabel
607 : 30 : ASTLoweringBase::lower_loop_label (AST::LoopLabel &loop_label)
608 : : {
609 : 30 : HIR::Lifetime life = lower_lifetime (loop_label.get_lifetime ());
610 : :
611 : 30 : auto crate_num = mappings.get_current_crate ();
612 : 30 : Analysis::NodeMapping mapping (crate_num, loop_label.get_node_id (),
613 : 30 : mappings.get_next_hir_id (crate_num),
614 : 30 : UNKNOWN_LOCAL_DEFID);
615 : 30 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
616 : :
617 : 30 : return HIR::LoopLabel (mapping, std::move (life), loop_label.get_locus ());
618 : 30 : }
619 : :
620 : : std::vector<std::unique_ptr<HIR::GenericParam>>
621 : 4119 : ASTLoweringBase::lower_generic_params (
622 : : std::vector<std::unique_ptr<AST::GenericParam>> ¶ms)
623 : : {
624 : 4119 : std::vector<std::unique_ptr<HIR::GenericParam>> lowered;
625 : 8644 : for (auto &ast_param : params)
626 : : {
627 : 4525 : auto hir_param = ASTLowerGenericParam::translate (*ast_param);
628 : 4525 : lowered.push_back (std::unique_ptr<HIR::GenericParam> (hir_param));
629 : : }
630 : :
631 : 4119 : return lowered;
632 : : }
633 : :
634 : : HIR::PathExprSegment
635 : 35724 : ASTLoweringBase::lower_path_expr_seg (AST::PathExprSegment &s)
636 : : {
637 : 35724 : auto crate_num = mappings.get_current_crate ();
638 : 35724 : Analysis::NodeMapping mapping (crate_num, s.get_node_id (),
639 : 35724 : mappings.get_next_hir_id (crate_num),
640 : 35724 : UNKNOWN_LOCAL_DEFID);
641 : :
642 : 35724 : return HIR::PathExprSegment (
643 : : std::move (mapping),
644 : 107172 : HIR::PathIdentSegment (s.get_ident_segment ().as_string ()), s.get_locus (),
645 : 71448 : s.has_generic_args () ? lower_generic_args (s.get_generic_args ())
646 : 71448 : : HIR::GenericArgs::create_empty ());
647 : : }
648 : :
649 : : HIR::GenericArgsBinding
650 : 47 : ASTLoweringBase::lower_binding (AST::GenericArgsBinding &binding)
651 : : {
652 : 47 : HIR::Type *lowered_type = ASTLoweringType::translate (binding.get_type ());
653 : 47 : return HIR::GenericArgsBinding (binding.get_identifier (),
654 : 94 : std::unique_ptr<HIR::Type> (lowered_type),
655 : 47 : binding.get_locus ());
656 : : }
657 : :
658 : : HIR::GenericArgs
659 : 3514 : ASTLoweringBase::lower_generic_args (AST::GenericArgs &args)
660 : : {
661 : 3514 : std::vector<HIR::GenericArgsBinding> binding_args;
662 : 3561 : for (auto &binding : args.get_binding_args ())
663 : : {
664 : 47 : HIR::GenericArgsBinding b = lower_binding (binding);
665 : 47 : binding_args.push_back (std::move (b));
666 : 47 : }
667 : :
668 : 3514 : std::vector<HIR::Lifetime> lifetime_args;
669 : 3541 : for (auto &lifetime : args.get_lifetime_args ())
670 : : {
671 : 27 : HIR::Lifetime l = lower_lifetime (lifetime);
672 : 27 : lifetime_args.push_back (std::move (l));
673 : 27 : }
674 : :
675 : 3514 : std::vector<std::unique_ptr<HIR::Type>> type_args;
676 : 3514 : std::vector<HIR::ConstGenericArg> const_args;
677 : :
678 : 7243 : for (auto &arg : args.get_generic_args ())
679 : : {
680 : 3729 : switch (arg.get_kind ())
681 : : {
682 : 3664 : case AST::GenericArg::Kind::Type:
683 : 3664 : {
684 : 3664 : auto type = ASTLoweringType::translate (arg.get_type ());
685 : 3664 : type_args.emplace_back (std::unique_ptr<HIR::Type> (type));
686 : 3664 : break;
687 : : }
688 : 65 : case AST::GenericArg::Kind::Const:
689 : 65 : {
690 : 65 : auto expr = ASTLoweringExpr::translate (arg.get_expression ());
691 : 65 : const_args.emplace_back (
692 : 130 : HIR::ConstGenericArg (std::unique_ptr<HIR::Expr> (expr),
693 : 65 : expr->get_locus ()));
694 : 65 : break;
695 : : }
696 : 0 : default:
697 : 0 : rust_unreachable ();
698 : : }
699 : : }
700 : :
701 : 3514 : return HIR::GenericArgs (std::move (lifetime_args), std::move (type_args),
702 : : std::move (binding_args), std::move (const_args),
703 : 3514 : args.get_locus ());
704 : 3514 : }
705 : :
706 : : HIR::SelfParam
707 : 7916 : ASTLoweringBase::lower_self (AST::Param ¶m)
708 : : {
709 : 7916 : rust_assert (param.is_self ());
710 : :
711 : 7916 : auto self = static_cast<AST::SelfParam &> (param);
712 : 7916 : auto crate_num = mappings.get_current_crate ();
713 : 7916 : Analysis::NodeMapping mapping (crate_num, self.get_node_id (),
714 : 7916 : mappings.get_next_hir_id (crate_num),
715 : 7916 : mappings.get_next_localdef_id (crate_num));
716 : 7916 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
717 : :
718 : 7916 : if (self.has_type ())
719 : : {
720 : 1 : HIR::Type *type = ASTLoweringType::translate (self.get_type ());
721 : 1 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (type),
722 : 1 : self.get_is_mut (), self.get_locus ());
723 : : }
724 : 7915 : else if (!self.get_has_ref ())
725 : : {
726 : 3085 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (nullptr),
727 : 3085 : self.get_is_mut (), self.get_locus ());
728 : : }
729 : :
730 : 4830 : tl::optional<HIR::Lifetime> lifetime = tl::nullopt;
731 : :
732 : 4830 : if (self.has_lifetime ())
733 : 4574 : lifetime = lower_lifetime (self.get_lifetime ());
734 : :
735 : 4830 : return HIR::SelfParam (mapping, lifetime, self.get_is_mut (),
736 : 9404 : self.get_locus ());
737 : 7916 : }
738 : :
739 : : HIR::Type *
740 : 4876 : ASTLoweringBase::lower_type_no_bounds (AST::TypeNoBounds &type)
741 : : {
742 : 4876 : return ASTLoweringType::translate (type);
743 : : }
744 : :
745 : : HIR::TypeParamBound *
746 : 1420 : ASTLoweringBase::lower_bound (AST::TypeParamBound &bound)
747 : : {
748 : 1420 : return ASTLoweringTypeBounds::translate (bound);
749 : : }
750 : :
751 : : /* Checks whether the name of a field already exists. Returns true
752 : : and produces an error if so. */
753 : : bool
754 : 2261 : struct_field_name_exists (std::vector<HIR::StructField> &fields,
755 : : HIR::StructField &new_field)
756 : : {
757 : 6284 : for (auto &field : fields)
758 : : {
759 : 8062 : if (field.get_field_name ().as_string ().compare (
760 : 4031 : new_field.get_field_name ().as_string ())
761 : 4031 : == 0)
762 : : {
763 : 8 : rich_location r (line_table, new_field.get_locus ());
764 : 8 : r.add_range (field.get_locus ());
765 : 8 : rust_error_at (r, ErrorCode::E0124, "field %qs is already declared",
766 : 8 : field.get_field_name ().as_string ().c_str ());
767 : 8 : return true;
768 : 8 : }
769 : : }
770 : : return false;
771 : : }
772 : :
773 : : HIR::FunctionQualifiers
774 : 15527 : ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers)
775 : : {
776 : 15527 : Unsafety unsafety
777 : 15527 : = qualifiers.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
778 : 15527 : bool has_extern = qualifiers.is_extern ();
779 : 15527 : ABI abi = has_extern ? ABI::C : ABI::RUST;
780 : :
781 : 15527 : if (qualifiers.has_abi ())
782 : : {
783 : 64 : const std::string &extern_abi = qualifiers.get_extern_abi ();
784 : 64 : abi = get_abi_from_string (extern_abi);
785 : 64 : if (has_extern && abi == ABI::UNKNOWN)
786 : 1 : rust_error_at (qualifiers.get_locus (), ErrorCode::E0703,
787 : : "invalid ABI: found %qs", extern_abi.c_str ());
788 : 64 : }
789 : :
790 : 15527 : return HIR::FunctionQualifiers (qualifiers.get_async_status (),
791 : : qualifiers.get_const_status (), unsafety,
792 : 15527 : has_extern, abi);
793 : : }
794 : :
795 : : void
796 : 32722 : ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
797 : : {
798 : 47098 : for (const auto &attr : item.get_outer_attrs ())
799 : : {
800 : 14376 : const auto &str_path = attr.get_path ().as_string ();
801 : 14376 : if (!Analysis::Attributes::is_known (str_path))
802 : : {
803 : 0 : rust_error_at (attr.get_locus (), "unknown attribute");
804 : 0 : continue;
805 : : }
806 : :
807 : 14376 : bool is_lang_item = str_path == Values::Attributes::LANG
808 : 3116 : && attr.has_attr_input ()
809 : 17492 : && attr.get_attr_input ().get_attr_input_type ()
810 : 14376 : == AST::AttrInput::AttrInputType::LITERAL;
811 : :
812 : 14376 : bool is_doc_item = str_path == Values::Attributes::DOC;
813 : :
814 : 14376 : if (is_doc_item)
815 : 8366 : handle_doc_item_attribute (item, attr);
816 : 6010 : else if (is_lang_item)
817 : 3116 : handle_lang_item_attribute (item, attr);
818 : 2894 : else if (!attribute_handled_in_another_pass (str_path))
819 : : {
820 : 0 : rust_error_at (attr.get_locus (), "unhandled attribute: [%s]",
821 : 0 : attr.get_path ().as_string ().c_str ());
822 : : }
823 : 14376 : }
824 : 32722 : }
825 : :
826 : : void
827 : 8366 : ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &,
828 : : const AST::Attribute &attr)
829 : : {
830 : 8366 : auto simple_doc_comment = attr.has_attr_input ()
831 : 8366 : && attr.get_attr_input ().get_attr_input_type ()
832 : 9130 : == AST::AttrInput::AttrInputType::LITERAL;
833 : 764 : if (simple_doc_comment)
834 : : return;
835 : :
836 : 764 : const AST::AttrInput &input = attr.get_attr_input ();
837 : 764 : bool is_token_tree
838 : 764 : = input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
839 : 764 : rust_assert (is_token_tree);
840 : 764 : const auto &option = static_cast<const AST::DelimTokenTree &> (input);
841 : 764 : AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
842 : :
843 : : // TODO: add actual and complete checks for the doc attributes
844 : : //
845 : : // FIXME: Move this to the AttributeChecker visitor
846 : 764 : rust_assert (meta_item);
847 : : }
848 : :
849 : : void
850 : 3116 : ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
851 : : const AST::Attribute &attr)
852 : : {
853 : 3116 : auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
854 : 3116 : const auto &lang_item_type_str = literal.get_literal ().as_string ();
855 : 3116 : auto lang_item_type = LangItem::Parse (lang_item_type_str);
856 : :
857 : 3116 : if (lang_item_type)
858 : 3116 : mappings.insert_lang_item (*lang_item_type,
859 : 3116 : item.get_mappings ().get_defid ());
860 : : else
861 : 0 : rust_error_at (attr.get_locus (), "unknown lang item");
862 : 3116 : }
863 : :
864 : : bool
865 : 2894 : ASTLoweringBase::attribute_handled_in_another_pass (
866 : : const std::string &attribute_path) const
867 : : {
868 : 2894 : const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
869 : 2894 : if (lookup.is_error ())
870 : : return false;
871 : :
872 : 2894 : if (lookup.handler == Analysis::CompilerPass::UNKNOWN)
873 : : return false;
874 : :
875 : 2894 : return lookup.handler != Analysis::CompilerPass::HIR_LOWERING;
876 : : }
877 : :
878 : : std::unique_ptr<HIR::TuplePatternItems>
879 : 377 : ASTLoweringBase::lower_tuple_pattern_multiple (
880 : : AST::TuplePatternItemsMultiple &pattern)
881 : : {
882 : 377 : std::vector<std::unique_ptr<HIR::Pattern>> patterns;
883 : 1132 : for (auto &p : pattern.get_patterns ())
884 : : {
885 : 755 : HIR::Pattern *translated = ASTLoweringPattern::translate (*p);
886 : 755 : patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
887 : : }
888 : :
889 : 377 : return std::unique_ptr<HIR::TuplePatternItems> (
890 : 377 : new HIR::TuplePatternItemsMultiple (std::move (patterns)));
891 : 377 : }
892 : :
893 : : std::unique_ptr<TuplePatternItems>
894 : 24 : ASTLoweringBase::lower_tuple_pattern_ranged (
895 : : AST::TuplePatternItemsRanged &pattern)
896 : : {
897 : 24 : std::vector<std::unique_ptr<HIR::Pattern>> lower_patterns;
898 : 24 : std::vector<std::unique_ptr<HIR::Pattern>> upper_patterns;
899 : :
900 : 48 : for (auto &p : pattern.get_lower_patterns ())
901 : : {
902 : 24 : HIR::Pattern *translated = ASTLoweringPattern::translate (*p);
903 : 24 : lower_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
904 : : }
905 : :
906 : 51 : for (auto &p : pattern.get_upper_patterns ())
907 : : {
908 : 27 : HIR::Pattern *translated = ASTLoweringPattern::translate (*p);
909 : 27 : upper_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
910 : : }
911 : :
912 : 24 : return std::unique_ptr<HIR::TuplePatternItems> (
913 : : new HIR::TuplePatternItemsRanged (std::move (lower_patterns),
914 : 24 : std::move (upper_patterns)));
915 : 24 : }
916 : :
917 : : std::unique_ptr<HIR::RangePatternBound>
918 : 42 : ASTLoweringBase::lower_range_pattern_bound (AST::RangePatternBound &bound)
919 : : {
920 : 42 : std::unique_ptr<HIR::RangePatternBound> hir_bound = nullptr;
921 : 42 : switch (bound.get_bound_type ())
922 : : {
923 : 21 : case AST::RangePatternBound::RangePatternBoundType::LITERAL:
924 : 21 : {
925 : 21 : AST::RangePatternBoundLiteral &ref
926 : : = static_cast<AST::RangePatternBoundLiteral &> (bound);
927 : :
928 : 21 : HIR::Literal literal = lower_literal (ref.get_literal ());
929 : :
930 : 21 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
931 : : new HIR::RangePatternBoundLiteral (literal, ref.get_locus (),
932 : 42 : ref.get_has_minus ()));
933 : 21 : }
934 : 21 : break;
935 : 21 : case AST::RangePatternBound::RangePatternBoundType::PATH:
936 : 21 : {
937 : 21 : auto &ref = static_cast<AST::RangePatternBoundPath &> (bound);
938 : :
939 : 21 : HIR::PathInExpression *path
940 : 21 : = ASTLowerPathInExpression::translate (ref.get_path ());
941 : :
942 : 63 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
943 : 21 : new HIR::RangePatternBoundPath (*path));
944 : : }
945 : 21 : break;
946 : 0 : case AST::RangePatternBound::RangePatternBoundType::QUALPATH:
947 : 0 : {
948 : 0 : auto &ref = static_cast<AST::RangePatternBoundQualPath &> (bound);
949 : :
950 : 0 : HIR::QualifiedPathInExpression *qualpath
951 : 0 : = ASTLowerQualPathInExpression::translate (ref.get_qualified_path ());
952 : :
953 : 0 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
954 : 0 : new HIR::RangePatternBoundQualPath (*qualpath));
955 : : }
956 : 0 : break;
957 : : }
958 : :
959 : 42 : return hir_bound;
960 : : }
961 : :
962 : : HIR::Literal
963 : 18061 : ASTLoweringBase::lower_literal (const AST::Literal &literal)
964 : : {
965 : 18061 : HIR::Literal::LitType type = HIR::Literal::LitType::CHAR;
966 : 18061 : switch (literal.get_lit_type ())
967 : : {
968 : : case AST::Literal::LitType::CHAR:
969 : : type = HIR::Literal::LitType::CHAR;
970 : : break;
971 : : case AST::Literal::LitType::STRING:
972 : 2284 : type = HIR::Literal::LitType::STRING;
973 : : break;
974 : 408 : case AST::Literal::LitType::BYTE:
975 : 408 : type = HIR::Literal::LitType::BYTE;
976 : 408 : break;
977 : 35 : case AST::Literal::LitType::BYTE_STRING:
978 : 35 : type = HIR::Literal::LitType::BYTE_STRING;
979 : 35 : break;
980 : : case AST::Literal::LitType::RAW_STRING:
981 : 2284 : type = HIR::Literal::LitType::STRING;
982 : : break;
983 : 13530 : case AST::Literal::LitType::INT:
984 : 13530 : type = HIR::Literal::LitType::INT;
985 : 13530 : break;
986 : 325 : case AST::Literal::LitType::FLOAT:
987 : 325 : type = HIR::Literal::LitType::FLOAT;
988 : 325 : break;
989 : 1293 : case AST::Literal::LitType::BOOL:
990 : 1293 : type = HIR::Literal::LitType::BOOL;
991 : 1293 : break;
992 : 0 : case AST::Literal::LitType::ERROR:
993 : 0 : rust_unreachable ();
994 : 18061 : break;
995 : : }
996 : :
997 : 36122 : return HIR::Literal (literal.as_string (), type, literal.get_type_hint ());
998 : : }
999 : :
1000 : : HIR::ExternBlock *
1001 : 1425 : ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block)
1002 : : {
1003 : 1425 : HIR::Visibility vis = translate_visibility (extern_block.get_visibility ());
1004 : :
1005 : 1425 : auto crate_num = mappings.get_current_crate ();
1006 : 1425 : Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (),
1007 : 1425 : mappings.get_next_hir_id (crate_num),
1008 : 1425 : mappings.get_next_localdef_id (crate_num));
1009 : :
1010 : 1425 : std::vector<std::unique_ptr<HIR::ExternalItem>> extern_items;
1011 : 3595 : for (auto &item : extern_block.get_extern_items ())
1012 : : {
1013 : 2170 : if (item->is_marked_for_strip ())
1014 : 0 : continue;
1015 : :
1016 : 2170 : HIR::ExternalItem *lowered
1017 : 2170 : = ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ());
1018 : 2170 : extern_items.push_back (std::unique_ptr<HIR::ExternalItem> (lowered));
1019 : : }
1020 : :
1021 : 1425 : ABI abi = ABI::C;
1022 : 1425 : if (extern_block.has_abi ())
1023 : : {
1024 : 1425 : const std::string &extern_abi = extern_block.get_abi ();
1025 : 1425 : abi = get_abi_from_string (extern_abi);
1026 : 1425 : if (abi == ABI::UNKNOWN)
1027 : 1 : rust_error_at (extern_block.get_locus (), ErrorCode::E0703,
1028 : : "invalid ABI: found %qs", extern_abi.c_str ());
1029 : 1425 : }
1030 : :
1031 : 1425 : HIR::ExternBlock *hir_extern_block
1032 : : = new HIR::ExternBlock (mapping, abi, std::move (extern_items),
1033 : 1425 : std::move (vis), extern_block.get_inner_attrs (),
1034 : 1425 : extern_block.get_outer_attrs (),
1035 : 1425 : extern_block.get_locus ());
1036 : :
1037 : 1425 : mappings.insert_hir_extern_block (hir_extern_block);
1038 : :
1039 : 2850 : return hir_extern_block;
1040 : 1425 : }
1041 : :
1042 : : void
1043 : 887 : ASTLoweringBase::lower_macro_definition (AST::MacroRulesDefinition &def)
1044 : : {
1045 : 887 : auto is_export = false;
1046 : 1014 : for (const auto &attr : def.get_outer_attrs ())
1047 : 127 : if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
1048 : 1 : is_export = true;
1049 : :
1050 : 887 : if (is_export)
1051 : : {
1052 : 1 : mappings.insert_exported_macro (def);
1053 : 1 : mappings.insert_ast_item (&def);
1054 : 1 : mappings.insert_location (def.get_node_id (), def.get_locus ());
1055 : : }
1056 : 887 : }
1057 : :
1058 : : } // namespace HIR
1059 : : } // namespace Rust
|