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 671 : ASTLoweringBase::visit (AST::UseDeclaration &)
349 671 : {}
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::SlicePattern &)
508 0 : {}
509 : void
510 0 : ASTLoweringBase::visit (AST::AltPattern &)
511 0 : {}
512 :
513 : // rust-stmt.h
514 : void
515 0 : ASTLoweringBase::visit (AST::EmptyStmt &)
516 0 : {}
517 : void
518 0 : ASTLoweringBase::visit (AST::LetStmt &)
519 0 : {}
520 : void
521 0 : ASTLoweringBase::visit (AST::ExprStmt &)
522 0 : {}
523 :
524 : // rust-type.h
525 : void
526 0 : ASTLoweringBase::visit (AST::TraitBound &)
527 0 : {}
528 : void
529 0 : ASTLoweringBase::visit (AST::ImplTraitType &)
530 0 : {}
531 : void
532 0 : ASTLoweringBase::visit (AST::TraitObjectType &)
533 0 : {}
534 : void
535 0 : ASTLoweringBase::visit (AST::ParenthesisedType &)
536 0 : {}
537 : void
538 0 : ASTLoweringBase::visit (AST::ImplTraitTypeOneBound &)
539 0 : {}
540 : void
541 0 : ASTLoweringBase::visit (AST::TraitObjectTypeOneBound &)
542 0 : {}
543 : void
544 0 : ASTLoweringBase::visit (AST::TupleType &)
545 0 : {}
546 : void
547 0 : ASTLoweringBase::visit (AST::NeverType &)
548 0 : {}
549 : void
550 0 : ASTLoweringBase::visit (AST::RawPointerType &)
551 0 : {}
552 : void
553 0 : ASTLoweringBase::visit (AST::ReferenceType &)
554 0 : {}
555 : void
556 0 : ASTLoweringBase::visit (AST::ArrayType &)
557 0 : {}
558 : void
559 0 : ASTLoweringBase::visit (AST::SliceType &)
560 0 : {}
561 : void
562 0 : ASTLoweringBase::visit (AST::InferredType &)
563 0 : {}
564 : void
565 0 : ASTLoweringBase::visit (AST::BareFunctionType &)
566 0 : {}
567 :
568 : void
569 0 : ASTLoweringBase::visit (AST::FunctionParam ¶m)
570 0 : {}
571 :
572 : void
573 0 : ASTLoweringBase::visit (AST::VariadicParam ¶m)
574 0 : {}
575 :
576 : void
577 0 : ASTLoweringBase::visit (AST::SelfParam ¶m)
578 0 : {}
579 :
580 : void
581 0 : ASTLoweringBase::visit (AST::FormatArgs &fmt)
582 0 : {}
583 :
584 : void
585 0 : ASTLoweringBase::visit (AST::OffsetOf &offset_of)
586 0 : {}
587 :
588 : HIR::Lifetime
589 9297 : ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
590 : bool default_to_static_lifetime)
591 : {
592 9297 : auto lifetime_type = lifetime.get_lifetime_type ();
593 9297 : if (lifetime_type == AST::Lifetime::WILDCARD && default_to_static_lifetime)
594 : {
595 : // If compiling in a static context.
596 11 : lifetime_type = AST::Lifetime::STATIC;
597 : }
598 :
599 9297 : auto crate_num = mappings.get_current_crate ();
600 9297 : Analysis::NodeMapping mapping (crate_num, lifetime.get_node_id (),
601 9297 : mappings.get_next_hir_id (crate_num),
602 9297 : UNKNOWN_LOCAL_DEFID);
603 9297 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
604 :
605 18594 : return HIR::Lifetime (mapping, lifetime_type, lifetime.get_lifetime_name (),
606 18594 : lifetime.get_locus ());
607 : }
608 :
609 : HIR::LoopLabel
610 53 : ASTLoweringBase::lower_loop_label (AST::LoopLabel &loop_label)
611 : {
612 53 : HIR::Lifetime life = lower_lifetime (loop_label.get_lifetime ());
613 :
614 53 : auto crate_num = mappings.get_current_crate ();
615 53 : Analysis::NodeMapping mapping (crate_num, loop_label.get_node_id (),
616 53 : mappings.get_next_hir_id (crate_num),
617 53 : UNKNOWN_LOCAL_DEFID);
618 53 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
619 :
620 53 : return HIR::LoopLabel (mapping, std::move (life), loop_label.get_locus ());
621 53 : }
622 :
623 : std::vector<std::unique_ptr<HIR::GenericParam>>
624 4470 : ASTLoweringBase::lower_generic_params (
625 : std::vector<std::unique_ptr<AST::GenericParam>> ¶ms)
626 : {
627 4470 : std::vector<std::unique_ptr<HIR::GenericParam>> lowered;
628 4470 : lowered.reserve (params.size ());
629 :
630 9353 : for (auto &ast_param : params)
631 4883 : lowered.emplace_back (ASTLowerGenericParam::translate (*ast_param));
632 :
633 4470 : return lowered;
634 : }
635 :
636 : HIR::PathExprSegment
637 38274 : ASTLoweringBase::lower_path_expr_seg (AST::PathExprSegment &s)
638 : {
639 38274 : auto crate_num = mappings.get_current_crate ();
640 38274 : Analysis::NodeMapping mapping (crate_num, s.get_node_id (),
641 38274 : mappings.get_next_hir_id (crate_num),
642 38274 : UNKNOWN_LOCAL_DEFID);
643 :
644 38274 : return HIR::PathExprSegment (
645 : std::move (mapping),
646 114822 : HIR::PathIdentSegment (s.get_ident_segment ().as_string ()), s.get_locus (),
647 76548 : s.has_generic_args () ? lower_generic_args (s.get_generic_args ())
648 76548 : : HIR::GenericArgs::create_empty ());
649 : }
650 :
651 : HIR::GenericArgsBinding
652 72 : ASTLoweringBase::lower_binding (AST::GenericArgsBinding &binding)
653 : {
654 72 : HIR::Type *lowered_type = ASTLoweringType::translate (binding.get_type ());
655 72 : return HIR::GenericArgsBinding (binding.get_identifier (),
656 144 : std::unique_ptr<HIR::Type> (lowered_type),
657 72 : binding.get_locus ());
658 : }
659 :
660 : HIR::GenericArgs
661 3706 : ASTLoweringBase::lower_generic_args (AST::GenericArgs &args)
662 : {
663 3706 : std::vector<HIR::GenericArgsBinding> binding_args;
664 3706 : binding_args.reserve (args.get_binding_args ().size ());
665 :
666 3778 : for (auto &binding : args.get_binding_args ())
667 144 : binding_args.emplace_back (lower_binding (binding));
668 :
669 3706 : std::vector<HIR::Lifetime> lifetime_args;
670 3706 : lifetime_args.reserve (args.get_lifetime_args ().size ());
671 :
672 3737 : for (auto &lifetime : args.get_lifetime_args ())
673 31 : lifetime_args.emplace_back (lower_lifetime (lifetime));
674 :
675 3706 : std::vector<std::unique_ptr<HIR::Type>> type_args;
676 3706 : std::vector<HIR::ConstGenericArg> const_args;
677 :
678 7603 : for (auto &arg : args.get_generic_args ())
679 : {
680 3897 : switch (arg.get_kind ())
681 : {
682 3775 : case AST::GenericArg::Kind::Type:
683 3775 : {
684 7550 : type_args.emplace_back (
685 3775 : ASTLoweringType::translate (arg.get_type ()));
686 3775 : break;
687 : }
688 122 : case AST::GenericArg::Kind::Const:
689 122 : {
690 122 : auto expr = ASTLoweringExpr::translate (arg.get_expression ());
691 122 : const_args.emplace_back (std::unique_ptr<HIR::Expr> (expr),
692 122 : expr->get_locus ());
693 122 : break;
694 : }
695 0 : default:
696 0 : rust_unreachable ();
697 : }
698 : }
699 :
700 3706 : return HIR::GenericArgs (std::move (lifetime_args), std::move (type_args),
701 : std::move (binding_args), std::move (const_args),
702 3706 : args.get_locus ());
703 3706 : }
704 :
705 : HIR::SelfParam
706 8106 : ASTLoweringBase::lower_self (AST::Param ¶m)
707 : {
708 8106 : rust_assert (param.is_self ());
709 :
710 8106 : auto self = static_cast<AST::SelfParam &> (param);
711 8106 : auto crate_num = mappings.get_current_crate ();
712 8106 : Analysis::NodeMapping mapping (crate_num, self.get_node_id (),
713 8106 : mappings.get_next_hir_id (crate_num),
714 8106 : mappings.get_next_localdef_id (crate_num));
715 8106 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
716 :
717 8106 : if (self.has_type ())
718 : {
719 1 : HIR::Type *type = ASTLoweringType::translate (self.get_type ());
720 1 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (type),
721 1 : self.get_is_mut (), self.get_locus ());
722 : }
723 8105 : else if (!self.get_has_ref ())
724 : {
725 3129 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (nullptr),
726 3129 : self.get_is_mut (), self.get_locus ());
727 : }
728 :
729 4976 : tl::optional<HIR::Lifetime> lifetime = tl::nullopt;
730 :
731 4976 : if (self.has_lifetime ())
732 4715 : lifetime = lower_lifetime (self.get_lifetime ());
733 :
734 4976 : return HIR::SelfParam (mapping, lifetime, self.get_is_mut (),
735 9691 : self.get_locus ());
736 8106 : }
737 :
738 : HIR::Type *
739 5293 : ASTLoweringBase::lower_type_no_bounds (AST::TypeNoBounds &type)
740 : {
741 5293 : return ASTLoweringType::translate (type);
742 : }
743 :
744 : HIR::TypeParamBound *
745 1489 : ASTLoweringBase::lower_bound (AST::TypeParamBound &bound)
746 : {
747 1489 : return ASTLoweringTypeBounds::translate (bound);
748 : }
749 :
750 : /* Checks whether the name of a field already exists. Returns true
751 : and produces an error if so. */
752 : bool
753 2366 : struct_field_name_exists (std::vector<HIR::StructField> &fields,
754 : HIR::StructField &new_field)
755 : {
756 6427 : for (auto &field : fields)
757 : {
758 8138 : if (field.get_field_name ().as_string ().compare (
759 4069 : new_field.get_field_name ().as_string ())
760 4069 : == 0)
761 : {
762 8 : rich_location r (line_table, new_field.get_locus ());
763 8 : r.add_range (field.get_locus ());
764 8 : rust_error_at (r, ErrorCode::E0124, "field %qs is already declared",
765 8 : field.get_field_name ().as_string ().c_str ());
766 8 : return true;
767 8 : }
768 : }
769 : return false;
770 : }
771 :
772 : HIR::FunctionQualifiers
773 16364 : ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers)
774 : {
775 16364 : Unsafety unsafety
776 16364 : = qualifiers.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
777 16364 : bool has_extern = qualifiers.is_extern ();
778 16364 : ABI abi = has_extern ? ABI::C : ABI::RUST;
779 :
780 16364 : if (qualifiers.has_abi ())
781 : {
782 80 : const std::string &extern_abi = qualifiers.get_extern_abi ();
783 80 : abi = get_abi_from_string (extern_abi);
784 80 : if (has_extern && abi == ABI::UNKNOWN)
785 1 : rust_error_at (qualifiers.get_locus (), ErrorCode::E0703,
786 : "invalid ABI: found %qs", extern_abi.c_str ());
787 80 : }
788 :
789 16364 : return HIR::FunctionQualifiers (qualifiers.get_async_status (),
790 : qualifiers.get_const_status (), unsafety,
791 16364 : has_extern, abi);
792 : }
793 :
794 : void
795 34681 : ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
796 : {
797 49468 : for (const auto &attr : item.get_outer_attrs ())
798 : {
799 14787 : const auto &str_path = attr.get_path ().as_string ();
800 14787 : if (!Analysis::Attributes::is_known (str_path))
801 : {
802 0 : rust_error_at (attr.get_locus (), "unknown attribute");
803 0 : continue;
804 : }
805 :
806 14787 : bool is_lang_item = str_path == Values::Attributes::LANG
807 3468 : && attr.has_attr_input ()
808 18255 : && attr.get_attr_input ().get_attr_input_type ()
809 14787 : == AST::AttrInput::AttrInputType::LITERAL;
810 :
811 14787 : bool is_doc_item = str_path == Values::Attributes::DOC;
812 :
813 14787 : if (is_doc_item)
814 8368 : handle_doc_item_attribute (item, attr);
815 6419 : else if (is_lang_item)
816 3468 : handle_lang_item_attribute (item, attr);
817 2951 : else if (!attribute_handled_in_another_pass (str_path))
818 : {
819 0 : rust_error_at (attr.get_locus (), "unhandled attribute: [%s]",
820 0 : attr.get_path ().as_string ().c_str ());
821 : }
822 14787 : }
823 34681 : }
824 :
825 : void
826 8368 : ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &,
827 : const AST::Attribute &attr)
828 : {
829 8368 : rust_assert (attr.has_attr_input ());
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 : static void
851 3468 : warn_if_stub_lang_item (location_t locus, LangItem::Kind kind)
852 : {
853 3468 : switch (kind)
854 : {
855 7 : case LangItem::Kind::FUTURE_TRAIT:
856 7 : case LangItem::Kind::POLL:
857 7 : case LangItem::Kind::READY:
858 7 : case LangItem::Kind::PENDING:
859 7 : case LangItem::Kind::GENERATOR:
860 7 : case LangItem::Kind::GENERATOR_STATE:
861 7 : case LangItem::Kind::MAYBE_UNINIT:
862 7 : case LangItem::Kind::BOX_FREE:
863 7 : case LangItem::Kind::DROP_IN_PLACE:
864 7 : rust_warning_at (locus, 0, "%qs is not implemented and has no effect",
865 7 : LangItem::PrettyString (kind).c_str ());
866 7 : break;
867 : default:
868 : break;
869 : }
870 3468 : }
871 :
872 : void
873 3468 : ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
874 : const AST::Attribute &attr)
875 : {
876 3468 : auto lang_item_type_str = Analysis::Attributes::extract_string_literal (attr);
877 3468 : rust_assert (lang_item_type_str.has_value ());
878 :
879 3468 : auto lang_item_type = LangItem::Parse (*lang_item_type_str);
880 :
881 3468 : if (lang_item_type)
882 : {
883 3468 : mappings.insert_lang_item (*lang_item_type,
884 3468 : item.get_mappings ().get_defid ());
885 3468 : warn_if_stub_lang_item (attr.get_locus (), *lang_item_type);
886 : }
887 : else
888 0 : rust_error_at (attr.get_locus (), "unknown lang item");
889 3468 : }
890 :
891 : bool
892 2951 : ASTLoweringBase::attribute_handled_in_another_pass (
893 : const std::string &attribute_path) const
894 : {
895 2951 : const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
896 2951 : if (lookup.is_error ())
897 : return false;
898 :
899 2951 : if (lookup.handler == Analysis::CompilerPass::UNKNOWN)
900 : return false;
901 :
902 2951 : return lookup.handler != Analysis::CompilerPass::HIR_LOWERING;
903 : }
904 :
905 : std::unique_ptr<HIR::TuplePatternItems>
906 424 : ASTLoweringBase::lower_tuple_pattern_multiple (
907 : AST::TuplePatternItemsNoRest &pattern)
908 : {
909 424 : std::vector<std::unique_ptr<HIR::Pattern>> patterns;
910 424 : patterns.reserve (pattern.get_patterns ().size ());
911 :
912 1278 : for (auto &p : pattern.get_patterns ())
913 854 : patterns.emplace_back (ASTLoweringPattern::translate (*p));
914 :
915 424 : return std::unique_ptr<HIR::TuplePatternItems> (
916 424 : new HIR::TuplePatternItemsNoRest (std::move (patterns)));
917 424 : }
918 :
919 : std::unique_ptr<TuplePatternItems>
920 38 : ASTLoweringBase::lower_tuple_pattern_ranged (
921 : AST::TuplePatternItemsHasRest &pattern)
922 : {
923 38 : std::vector<std::unique_ptr<HIR::Pattern>> lower_patterns;
924 38 : lower_patterns.reserve (pattern.get_lower_patterns ().size ());
925 38 : std::vector<std::unique_ptr<HIR::Pattern>> upper_patterns;
926 38 : upper_patterns.reserve (pattern.get_upper_patterns ().size ());
927 :
928 71 : for (auto &p : pattern.get_lower_patterns ())
929 33 : lower_patterns.emplace_back (ASTLoweringPattern::translate (*p));
930 :
931 74 : for (auto &p : pattern.get_upper_patterns ())
932 36 : upper_patterns.emplace_back (ASTLoweringPattern::translate (*p));
933 :
934 38 : return std::unique_ptr<HIR::TuplePatternItems> (
935 : new HIR::TuplePatternItemsHasRest (std::move (lower_patterns),
936 38 : std::move (upper_patterns)));
937 38 : }
938 :
939 : std::unique_ptr<HIR::RangePatternBound>
940 92 : ASTLoweringBase::lower_range_pattern_bound (AST::RangePatternBound &bound)
941 : {
942 92 : std::unique_ptr<HIR::RangePatternBound> hir_bound = nullptr;
943 92 : switch (bound.get_bound_type ())
944 : {
945 71 : case AST::RangePatternBound::RangePatternBoundType::LITERAL:
946 71 : {
947 71 : AST::RangePatternBoundLiteral &ref
948 : = static_cast<AST::RangePatternBoundLiteral &> (bound);
949 :
950 71 : HIR::Literal literal = lower_literal (ref.get_literal ());
951 :
952 71 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
953 : new HIR::RangePatternBoundLiteral (literal, ref.get_locus (),
954 142 : ref.get_has_minus ()));
955 71 : }
956 71 : break;
957 21 : case AST::RangePatternBound::RangePatternBoundType::PATH:
958 21 : {
959 21 : auto &ref = static_cast<AST::RangePatternBoundPath &> (bound);
960 :
961 21 : HIR::PathInExpression *path
962 21 : = ASTLowerPathInExpression::translate (ref.get_path ());
963 :
964 63 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
965 21 : new HIR::RangePatternBoundPath (*path));
966 : }
967 21 : break;
968 0 : case AST::RangePatternBound::RangePatternBoundType::QUALPATH:
969 0 : {
970 0 : auto &ref = static_cast<AST::RangePatternBoundQualPath &> (bound);
971 :
972 0 : HIR::QualifiedPathInExpression *qualpath
973 0 : = ASTLowerQualPathInExpression::translate (ref.get_qualified_path ());
974 :
975 0 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
976 0 : new HIR::RangePatternBoundQualPath (*qualpath));
977 : }
978 0 : break;
979 : }
980 :
981 92 : return hir_bound;
982 : }
983 :
984 : HIR::Literal
985 21395 : ASTLoweringBase::lower_literal (const AST::Literal &literal)
986 : {
987 21395 : HIR::Literal::LitType type = HIR::Literal::LitType::CHAR;
988 21395 : switch (literal.get_lit_type ())
989 : {
990 : case AST::Literal::LitType::CHAR:
991 : type = HIR::Literal::LitType::CHAR;
992 : break;
993 : case AST::Literal::LitType::STRING:
994 2430 : type = HIR::Literal::LitType::STRING;
995 : break;
996 408 : case AST::Literal::LitType::BYTE:
997 408 : type = HIR::Literal::LitType::BYTE;
998 408 : break;
999 35 : case AST::Literal::LitType::BYTE_STRING:
1000 35 : type = HIR::Literal::LitType::BYTE_STRING;
1001 35 : break;
1002 : case AST::Literal::LitType::RAW_STRING:
1003 2430 : type = HIR::Literal::LitType::STRING;
1004 : break;
1005 15 : case AST::Literal::LitType::C_STRING:
1006 15 : type = HIR::Literal::LitType::C_STRING;
1007 15 : break;
1008 16655 : case AST::Literal::LitType::INT:
1009 16655 : type = HIR::Literal::LitType::INT;
1010 16655 : break;
1011 347 : case AST::Literal::LitType::FLOAT:
1012 347 : type = HIR::Literal::LitType::FLOAT;
1013 347 : break;
1014 1319 : case AST::Literal::LitType::BOOL:
1015 1319 : type = HIR::Literal::LitType::BOOL;
1016 1319 : break;
1017 0 : case AST::Literal::LitType::ERROR:
1018 0 : rust_unreachable ();
1019 21395 : break;
1020 : }
1021 :
1022 42790 : return HIR::Literal (literal.as_string (), type, literal.get_type_hint ());
1023 : }
1024 :
1025 : HIR::ExternBlock *
1026 1674 : ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block)
1027 : {
1028 1674 : HIR::Visibility vis = translate_visibility (extern_block.get_visibility ());
1029 :
1030 1674 : auto crate_num = mappings.get_current_crate ();
1031 3348 : Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (),
1032 1674 : mappings.get_next_hir_id (crate_num),
1033 1674 : mappings.get_next_localdef_id (crate_num));
1034 :
1035 1674 : std::vector<std::unique_ptr<HIR::ExternalItem>> extern_items;
1036 1674 : extern_items.reserve (extern_block.get_extern_items ().size ());
1037 :
1038 4240 : for (auto &item : extern_block.get_extern_items ())
1039 : {
1040 2566 : if (item->is_marked_for_strip ())
1041 0 : continue;
1042 :
1043 5132 : extern_items.emplace_back (
1044 2566 : ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ()));
1045 : }
1046 :
1047 1674 : ABI abi = ABI::C;
1048 1674 : if (extern_block.has_abi ())
1049 : {
1050 1673 : const std::string &extern_abi = extern_block.get_abi ();
1051 1673 : abi = get_abi_from_string (extern_abi);
1052 1673 : if (abi == ABI::UNKNOWN)
1053 1 : rust_error_at (extern_block.get_locus (), ErrorCode::E0703,
1054 : "invalid ABI: found %qs", extern_abi.c_str ());
1055 1673 : }
1056 :
1057 1674 : HIR::ExternBlock *hir_extern_block
1058 : = new HIR::ExternBlock (mapping, abi, extern_block.has_abi (),
1059 : std::move (extern_items), std::move (vis),
1060 1674 : extern_block.get_inner_attrs (),
1061 1674 : extern_block.get_outer_attrs (),
1062 1674 : extern_block.get_locus ());
1063 :
1064 1674 : mappings.insert_hir_extern_block (hir_extern_block);
1065 :
1066 3348 : return hir_extern_block;
1067 1674 : }
1068 :
1069 : void
1070 906 : ASTLoweringBase::lower_macro_definition (AST::MacroRulesDefinition &def)
1071 : {
1072 906 : auto is_export = false;
1073 1046 : for (const auto &attr : def.get_outer_attrs ())
1074 140 : if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
1075 3 : is_export = true;
1076 :
1077 906 : if (is_export)
1078 : {
1079 3 : mappings.insert_exported_macro (def);
1080 3 : mappings.insert_ast_item (&def);
1081 3 : mappings.insert_location (def.get_node_id (), def.get_locus ());
1082 : }
1083 906 : }
1084 :
1085 : } // namespace HIR
1086 : } // namespace Rust
|