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