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