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