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 711 : ASTLoweringBase::visit (AST::UseDeclaration &)
349 711 : {}
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 9928 : ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
590 : bool default_to_static_lifetime)
591 : {
592 9928 : auto lifetime_type = lifetime.get_lifetime_type ();
593 9928 : 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 9928 : auto crate_num = mappings.get_current_crate ();
600 9928 : Analysis::NodeMapping mapping (crate_num, lifetime.get_node_id (),
601 9928 : mappings.get_next_hir_id (crate_num),
602 9928 : UNKNOWN_LOCAL_DEFID);
603 9928 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
604 :
605 19856 : return HIR::Lifetime (mapping, lifetime_type, lifetime.get_lifetime_name (),
606 19856 : lifetime.get_locus ());
607 : }
608 :
609 : HIR::LoopLabel
610 54 : ASTLoweringBase::lower_loop_label (AST::LoopLabel &loop_label)
611 : {
612 54 : HIR::Lifetime life = lower_lifetime (loop_label.get_lifetime ());
613 :
614 54 : auto crate_num = mappings.get_current_crate ();
615 54 : Analysis::NodeMapping mapping (crate_num, loop_label.get_node_id (),
616 54 : mappings.get_next_hir_id (crate_num),
617 54 : UNKNOWN_LOCAL_DEFID);
618 54 : mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
619 :
620 54 : return HIR::LoopLabel (mapping, std::move (life), loop_label.get_locus ());
621 54 : }
622 :
623 : std::vector<std::unique_ptr<HIR::GenericParam>>
624 4857 : ASTLoweringBase::lower_generic_params (
625 : std::vector<std::unique_ptr<AST::GenericParam>> ¶ms)
626 : {
627 4857 : std::vector<std::unique_ptr<HIR::GenericParam>> lowered;
628 4857 : lowered.reserve (params.size ());
629 :
630 10171 : for (auto &ast_param : params)
631 5314 : lowered.emplace_back (ASTLowerGenericParam::translate (*ast_param));
632 :
633 4857 : return lowered;
634 : }
635 :
636 : HIR::PathExprSegment
637 41654 : ASTLoweringBase::lower_path_expr_seg (AST::PathExprSegment &s)
638 : {
639 41654 : auto crate_num = mappings.get_current_crate ();
640 41654 : Analysis::NodeMapping mapping (crate_num, s.get_node_id (),
641 41654 : mappings.get_next_hir_id (crate_num),
642 41654 : UNKNOWN_LOCAL_DEFID);
643 :
644 41654 : return HIR::PathExprSegment (
645 : std::move (mapping),
646 124962 : HIR::PathIdentSegment (s.get_ident_segment ().as_string ()), s.get_locus (),
647 83308 : s.has_generic_args () ? lower_generic_args (s.get_generic_args ())
648 83308 : : HIR::GenericArgs::create_empty ());
649 : }
650 :
651 : HIR::GenericArgsBinding
652 93 : ASTLoweringBase::lower_binding (AST::GenericArgsBinding &binding)
653 : {
654 93 : auto impl_trait_allowed
655 93 : = binding.get_kind () == AST::GenericArgsBinding::Kind::Constraint
656 93 : ? ASTLoweringType::ImplTrait::Allow
657 93 : : ASTLoweringType::ImplTrait::Forbid;
658 93 : HIR::Type *lowered_type
659 93 : = ASTLoweringType::translate (binding.get_type (), false,
660 : impl_trait_allowed);
661 93 : auto kind = binding.get_kind () == AST::GenericArgsBinding::Kind::Constraint
662 93 : ? HIR::GenericArgsBinding::Kind::Constraint
663 85 : : HIR::GenericArgsBinding::Kind::Equality;
664 93 : return HIR::GenericArgsBinding (binding.get_identifier (),
665 186 : std::unique_ptr<HIR::Type> (lowered_type),
666 93 : binding.get_locus (), kind);
667 : }
668 :
669 : HIR::GenericArgs
670 4099 : ASTLoweringBase::lower_generic_args (AST::GenericArgs &args)
671 : {
672 4099 : std::vector<HIR::GenericArgsBinding> binding_args;
673 4099 : binding_args.reserve (args.get_binding_args ().size ());
674 :
675 4192 : for (auto &binding : args.get_binding_args ())
676 186 : binding_args.emplace_back (lower_binding (binding));
677 :
678 4099 : std::vector<HIR::Lifetime> lifetime_args;
679 4099 : lifetime_args.reserve (args.get_lifetime_args ().size ());
680 :
681 4130 : for (auto &lifetime : args.get_lifetime_args ())
682 31 : lifetime_args.emplace_back (lower_lifetime (lifetime));
683 :
684 4099 : std::vector<std::unique_ptr<HIR::Type>> type_args;
685 4099 : std::vector<HIR::ConstGenericArg> const_args;
686 :
687 8390 : for (auto &arg : args.get_generic_args ())
688 : {
689 4291 : switch (arg.get_kind ())
690 : {
691 4169 : case AST::GenericArg::Kind::Type:
692 4169 : {
693 8338 : type_args.emplace_back (
694 4169 : ASTLoweringType::translate (arg.get_type ()));
695 4169 : break;
696 : }
697 122 : case AST::GenericArg::Kind::Const:
698 122 : {
699 122 : auto expr = ASTLoweringExpr::translate (arg.get_expression ());
700 122 : const_args.emplace_back (std::unique_ptr<HIR::Expr> (expr),
701 122 : expr->get_locus ());
702 122 : break;
703 : }
704 0 : default:
705 0 : rust_unreachable ();
706 : }
707 : }
708 :
709 4099 : return HIR::GenericArgs (std::move (lifetime_args), std::move (type_args),
710 : std::move (binding_args), std::move (const_args),
711 4099 : args.get_locus ());
712 4099 : }
713 :
714 : HIR::SelfParam
715 9037 : ASTLoweringBase::lower_self (AST::Param ¶m)
716 : {
717 9037 : rust_assert (param.is_self ());
718 :
719 9037 : auto self = static_cast<AST::SelfParam &> (param);
720 9037 : auto crate_num = mappings.get_current_crate ();
721 9037 : Analysis::NodeMapping mapping (crate_num, self.get_node_id (),
722 9037 : mappings.get_next_hir_id (crate_num),
723 9037 : mappings.get_next_localdef_id (crate_num));
724 9037 : mappings.insert_location (mapping.get_hirid (), param.get_locus ());
725 :
726 9037 : if (self.has_type ())
727 : {
728 3 : HIR::Type *type = ASTLoweringType::translate (self.get_type ());
729 3 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (type),
730 3 : self.get_is_mut (), self.get_locus ());
731 : }
732 9034 : else if (!self.get_has_ref ())
733 : {
734 3665 : return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (nullptr),
735 3665 : self.get_is_mut (), self.get_locus ());
736 : }
737 :
738 5369 : tl::optional<HIR::Lifetime> lifetime = tl::nullopt;
739 :
740 5369 : if (self.has_lifetime ())
741 5104 : lifetime = lower_lifetime (self.get_lifetime ());
742 :
743 5369 : return HIR::SelfParam (mapping, lifetime, self.get_is_mut (),
744 10473 : self.get_locus ());
745 9037 : }
746 :
747 : HIR::Type *
748 5690 : ASTLoweringBase::lower_type_no_bounds (AST::TypeNoBounds &type)
749 : {
750 5690 : return ASTLoweringType::translate (type);
751 : }
752 :
753 : HIR::TypeParamBound *
754 1645 : ASTLoweringBase::lower_bound (AST::TypeParamBound &bound)
755 : {
756 1645 : return ASTLoweringTypeBounds::translate (bound);
757 : }
758 :
759 : /* Checks whether the name of a field already exists. Returns true
760 : and produces an error if so. */
761 : bool
762 2455 : struct_field_name_exists (std::vector<HIR::StructField> &fields,
763 : HIR::StructField &new_field)
764 : {
765 6544 : for (auto &field : fields)
766 : {
767 8194 : if (field.get_field_name ().as_string ().compare (
768 4097 : new_field.get_field_name ().as_string ())
769 4097 : == 0)
770 : {
771 8 : rich_location r (line_table, new_field.get_locus ());
772 8 : r.add_range (field.get_locus ());
773 8 : rust_error_at (r, ErrorCode::E0124, "field %qs is already declared",
774 8 : field.get_field_name ().as_string ().c_str ());
775 8 : return true;
776 8 : }
777 : }
778 : return false;
779 : }
780 :
781 : HIR::FunctionQualifiers
782 17691 : ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers)
783 : {
784 17691 : Unsafety unsafety
785 17691 : = qualifiers.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
786 17691 : bool has_extern = qualifiers.is_extern ();
787 17691 : ABI abi = has_extern ? ABI::C : ABI::RUST;
788 :
789 17691 : if (qualifiers.has_abi ())
790 : {
791 81 : const std::string &extern_abi = qualifiers.get_extern_abi ();
792 81 : abi = get_abi_from_string (extern_abi);
793 81 : if (has_extern && abi == ABI::UNKNOWN)
794 1 : rust_error_at (qualifiers.get_locus (), ErrorCode::E0703,
795 : "invalid ABI: found %qs", extern_abi.c_str ());
796 81 : }
797 :
798 17691 : return HIR::FunctionQualifiers (qualifiers.get_async_status (),
799 : qualifiers.get_const_status (), unsafety,
800 17691 : has_extern, abi);
801 : }
802 :
803 : void
804 37518 : ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
805 : {
806 52813 : for (const auto &attr : item.get_outer_attrs ())
807 : {
808 15295 : const auto &str_path = attr.get_path ().as_string ();
809 15295 : auto known_check = Analysis::Attributes::is_known (str_path);
810 15295 : if (known_check == Analysis::Attributes::AttributeKnowledge::Unknown)
811 : {
812 0 : rust_error_at (attr.get_locus (), "unknown attribute: %qs",
813 : str_path.c_str ());
814 0 : continue;
815 : }
816 :
817 : // If it is a tool attribute, the compiler can ignore it and let the tool
818 : // handle it
819 15295 : if (known_check == Analysis::Attributes::AttributeKnowledge::Tool)
820 0 : return;
821 :
822 15295 : bool is_lang_item = str_path == Values::Attributes::LANG
823 3671 : && attr.has_attr_input ()
824 18966 : && attr.get_attr_input ().get_attr_input_type ()
825 15295 : == AST::AttrInput::AttrInputType::LITERAL;
826 :
827 15295 : bool is_doc_item = str_path == Values::Attributes::DOC;
828 :
829 15295 : if (is_doc_item)
830 8466 : handle_doc_item_attribute (item, attr);
831 6829 : else if (is_lang_item)
832 3671 : handle_lang_item_attribute (item, attr);
833 3158 : else if (!attribute_handled_in_another_pass (str_path))
834 : {
835 0 : rust_error_at (attr.get_locus (), "unhandled attribute: [%s]",
836 0 : attr.get_path ().as_string ().c_str ());
837 : }
838 15295 : }
839 : }
840 :
841 : void
842 8466 : ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &,
843 : const AST::Attribute &attr)
844 : {
845 8466 : rust_assert (attr.has_attr_input ());
846 :
847 8466 : auto simple_doc_comment = attr.get_attr_input ().get_attr_input_type ()
848 8466 : == AST::AttrInput::AttrInputType::LITERAL;
849 :
850 8466 : if (simple_doc_comment)
851 : return;
852 :
853 764 : const AST::AttrInput &input = attr.get_attr_input ();
854 764 : bool is_token_tree
855 764 : = input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
856 764 : rust_assert (is_token_tree);
857 764 : const auto &option = static_cast<const AST::DelimTokenTree &> (input);
858 764 : AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
859 :
860 : // TODO: add actual and complete checks for the doc attributes
861 : //
862 : // FIXME: Move this to the AttributeChecker visitor
863 764 : rust_assert (meta_item);
864 : }
865 :
866 : static void
867 3671 : warn_if_stub_lang_item (location_t locus, LangItem::Kind kind)
868 : {
869 3671 : switch (kind)
870 : {
871 7 : case LangItem::Kind::FUTURE_TRAIT:
872 7 : case LangItem::Kind::POLL:
873 7 : case LangItem::Kind::READY:
874 7 : case LangItem::Kind::PENDING:
875 7 : case LangItem::Kind::GENERATOR:
876 7 : case LangItem::Kind::GENERATOR_STATE:
877 7 : case LangItem::Kind::MAYBE_UNINIT:
878 7 : case LangItem::Kind::BOX_FREE:
879 7 : case LangItem::Kind::DROP_IN_PLACE:
880 7 : rust_warning_at (locus, 0, "%qs is not implemented and has no effect",
881 7 : LangItem::PrettyString (kind).c_str ());
882 7 : break;
883 : default:
884 : break;
885 : }
886 3671 : }
887 :
888 : void
889 3671 : ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
890 : const AST::Attribute &attr)
891 : {
892 3671 : auto lang_item_type_str = Analysis::Attributes::extract_string_literal (attr);
893 3671 : rust_assert (lang_item_type_str.has_value ());
894 :
895 3671 : auto lang_item_type = LangItem::Parse (*lang_item_type_str);
896 :
897 3671 : if (lang_item_type)
898 : {
899 3671 : mappings.insert_lang_item (*lang_item_type,
900 3671 : item.get_mappings ().get_defid ());
901 3671 : warn_if_stub_lang_item (attr.get_locus (), *lang_item_type);
902 : }
903 : else
904 0 : rust_error_at (attr.get_locus (), "unknown lang item");
905 3671 : }
906 :
907 : bool
908 3158 : ASTLoweringBase::attribute_handled_in_another_pass (
909 : const std::string &attribute_path) const
910 : {
911 3158 : const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
912 3158 : if (lookup.is_error ())
913 : return false;
914 :
915 3158 : if (lookup.handler == Analysis::CompilerPass::UNKNOWN)
916 : return false;
917 :
918 3158 : return lookup.handler != Analysis::CompilerPass::HIR_LOWERING;
919 : }
920 :
921 : std::unique_ptr<HIR::TuplePatternItems>
922 512 : ASTLoweringBase::lower_tuple_pattern_multiple (
923 : AST::TuplePatternItemsNoRest &pattern)
924 : {
925 512 : std::vector<std::unique_ptr<HIR::Pattern>> patterns;
926 512 : patterns.reserve (pattern.get_patterns ().size ());
927 :
928 1542 : for (auto &p : pattern.get_patterns ())
929 1030 : patterns.emplace_back (ASTLoweringPattern::translate (*p));
930 :
931 512 : return std::unique_ptr<HIR::TuplePatternItems> (
932 512 : new HIR::TuplePatternItemsNoRest (std::move (patterns)));
933 512 : }
934 :
935 : std::unique_ptr<TuplePatternItems>
936 38 : ASTLoweringBase::lower_tuple_pattern_ranged (
937 : AST::TuplePatternItemsHasRest &pattern)
938 : {
939 38 : std::vector<std::unique_ptr<HIR::Pattern>> lower_patterns;
940 38 : lower_patterns.reserve (pattern.get_lower_patterns ().size ());
941 38 : std::vector<std::unique_ptr<HIR::Pattern>> upper_patterns;
942 38 : upper_patterns.reserve (pattern.get_upper_patterns ().size ());
943 :
944 71 : for (auto &p : pattern.get_lower_patterns ())
945 33 : lower_patterns.emplace_back (ASTLoweringPattern::translate (*p));
946 :
947 74 : for (auto &p : pattern.get_upper_patterns ())
948 36 : upper_patterns.emplace_back (ASTLoweringPattern::translate (*p));
949 :
950 38 : return std::unique_ptr<HIR::TuplePatternItems> (
951 : new HIR::TuplePatternItemsHasRest (std::move (lower_patterns),
952 38 : std::move (upper_patterns)));
953 38 : }
954 :
955 : std::unique_ptr<HIR::RangePatternBound>
956 92 : ASTLoweringBase::lower_range_pattern_bound (AST::RangePatternBound &bound)
957 : {
958 92 : std::unique_ptr<HIR::RangePatternBound> hir_bound = nullptr;
959 92 : switch (bound.get_bound_type ())
960 : {
961 71 : case AST::RangePatternBound::RangePatternBoundType::LITERAL:
962 71 : {
963 71 : AST::RangePatternBoundLiteral &ref
964 : = static_cast<AST::RangePatternBoundLiteral &> (bound);
965 :
966 71 : HIR::Literal literal = lower_literal (ref.get_literal ());
967 :
968 71 : hir_bound = std::unique_ptr<HIR::RangePatternBound> (
969 : new HIR::RangePatternBoundLiteral (literal, ref.get_locus (),
970 142 : ref.get_has_minus ()));
971 71 : }
972 71 : 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 92 : return hir_bound;
998 : }
999 :
1000 : HIR::Literal
1001 21898 : ASTLoweringBase::lower_literal (const AST::Literal &literal)
1002 : {
1003 21898 : HIR::Literal::LitType type = HIR::Literal::LitType::CHAR;
1004 21898 : 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 2465 : 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 2465 : type = HIR::Literal::LitType::STRING;
1020 : break;
1021 15 : case AST::Literal::LitType::C_STRING:
1022 15 : type = HIR::Literal::LitType::C_STRING;
1023 15 : break;
1024 17108 : case AST::Literal::LitType::INT:
1025 17108 : type = HIR::Literal::LitType::INT;
1026 17108 : break;
1027 348 : case AST::Literal::LitType::FLOAT:
1028 348 : type = HIR::Literal::LitType::FLOAT;
1029 348 : break;
1030 1333 : case AST::Literal::LitType::BOOL:
1031 1333 : type = HIR::Literal::LitType::BOOL;
1032 1333 : break;
1033 0 : case AST::Literal::LitType::ERROR:
1034 0 : rust_unreachable ();
1035 21898 : break;
1036 : }
1037 :
1038 43796 : return HIR::Literal (literal.as_string (), type, literal.get_type_hint ());
1039 : }
1040 :
1041 : HIR::ExternBlock *
1042 1714 : ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block)
1043 : {
1044 1714 : HIR::Visibility vis = translate_visibility (extern_block.get_visibility ());
1045 :
1046 1714 : auto crate_num = mappings.get_current_crate ();
1047 3428 : Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (),
1048 1714 : mappings.get_next_hir_id (crate_num),
1049 1714 : mappings.get_next_localdef_id (crate_num));
1050 :
1051 1714 : std::vector<std::unique_ptr<HIR::ExternalItem>> extern_items;
1052 1714 : extern_items.reserve (extern_block.get_extern_items ().size ());
1053 :
1054 4404 : for (auto &item : extern_block.get_extern_items ())
1055 : {
1056 2690 : if (item->is_marked_for_strip ())
1057 0 : continue;
1058 :
1059 5380 : extern_items.emplace_back (
1060 2690 : ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ()));
1061 : }
1062 :
1063 1714 : ABI abi = ABI::C;
1064 1714 : if (extern_block.has_abi ())
1065 : {
1066 1713 : const std::string &extern_abi = extern_block.get_abi ();
1067 1713 : abi = get_abi_from_string (extern_abi);
1068 1713 : if (abi == ABI::UNKNOWN)
1069 1 : rust_error_at (extern_block.get_locus (), ErrorCode::E0703,
1070 : "invalid ABI: found %qs", extern_abi.c_str ());
1071 1713 : }
1072 :
1073 1714 : HIR::ExternBlock *hir_extern_block
1074 : = new HIR::ExternBlock (mapping, abi, extern_block.has_abi (),
1075 : std::move (extern_items), std::move (vis),
1076 1714 : extern_block.get_inner_attrs (),
1077 1714 : extern_block.get_outer_attrs (),
1078 1714 : extern_block.get_locus ());
1079 :
1080 1714 : mappings.insert_hir_extern_block (hir_extern_block);
1081 :
1082 3428 : return hir_extern_block;
1083 1714 : }
1084 :
1085 : void
1086 994 : ASTLoweringBase::lower_macro_definition (AST::MacroRulesDefinition &def)
1087 : {
1088 994 : auto is_export = false;
1089 1134 : for (const auto &attr : def.get_outer_attrs ())
1090 140 : if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
1091 3 : is_export = true;
1092 :
1093 994 : if (is_export)
1094 : {
1095 3 : mappings.insert_exported_macro (def);
1096 3 : mappings.insert_ast_item (&def);
1097 3 : mappings.insert_location (def.get_node_id (), def.get_locus ());
1098 : }
1099 994 : }
1100 :
1101 : } // namespace HIR
1102 : } // namespace Rust
|