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