Debian Codegraph @

gcc/rust/hir/rust-ast-lower-base.cc

raw
1 // Copyright (C) 2020-2025 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 ASTLoweringBase::visit (AST::MacroInvocation &invoc)
36 {
37 rust_fatal_error (invoc.get_locus (), "rogue macro detected during lowering");
38 rust_unreachable ();
39 }
40
41 void
42 ASTLoweringBase::visit (AST::ErrorPropagationExpr &expr)
43 {
44 rust_fatal_error (expr.get_locus (),
45 "missing desugar for question mark operator");
46 rust_unreachable ();
47 }
48
49 void
50 ASTLoweringBase::visit (AST::Token &)
51 {}
52 void
53 ASTLoweringBase::visit (AST::DelimTokenTree &)
54 {}
55 void
56 ASTLoweringBase::visit (AST::AttrInputMetaItemContainer &)
57 {}
58 // void ASTLoweringBase::visit(MetaItemmeta_item) {}
59 // void vsit(Stmtstmt) {}
60 // void ASTLoweringBase::visit(Exprexpr) {}
61 void
62 ASTLoweringBase::visit (AST::IdentifierExpr &)
63 {}
64 // void ASTLoweringBase::visit(Patternpattern) {}
65 // void ASTLoweringBase::visit(Typetype) {}
66 // void ASTLoweringBase::visit(TypeParamBoundtype_param_bound) {}
67 void
68 ASTLoweringBase::visit (AST::Lifetime &)
69 {}
70 // void ASTLoweringBase::visit(GenericParamgeneric_param) {}
71 void
72 ASTLoweringBase::visit (AST::LifetimeParam &)
73 {}
74 void
75 ASTLoweringBase::visit (AST::ConstGenericParam &)
76 {}
77 // void ASTLoweringBase::visit(TraitItemtrait_item) {}
78 // void ASTLoweringBase::visit(InherentImplIteminherent_impl_item) {}
79 // void ASTLoweringBase::visit(TraitImplItemtrait_impl_item) {}
80
81 // rust-path.h
82 void
83 ASTLoweringBase::visit (AST::PathInExpression &)
84 {}
85 void
86 ASTLoweringBase::visit (AST::TypePathSegment &)
87 {}
88 void
89 ASTLoweringBase::visit (AST::TypePathSegmentGeneric &)
90 {}
91 void
92 ASTLoweringBase::visit (AST::TypePathSegmentFunction &)
93 {}
94 void
95 ASTLoweringBase::visit (AST::TypePath &)
96 {}
97 void
98 ASTLoweringBase::visit (AST::QualifiedPathInExpression &)
99 {}
100 void
101 ASTLoweringBase::visit (AST::QualifiedPathInType &)
102 {}
103
104 // rust-expr.h
105 void
106 ASTLoweringBase::visit (AST::LiteralExpr &)
107 {}
108 void
109 ASTLoweringBase::visit (AST::AttrInputLiteral &)
110 {}
111 void
112 ASTLoweringBase::visit (AST::AttrInputMacro &)
113 {}
114 void
115 ASTLoweringBase::visit (AST::MetaItemLitExpr &)
116 {}
117 void
118 ASTLoweringBase::visit (AST::MetaItemPathLit &)
119 {}
120 void
121 ASTLoweringBase::visit (AST::BorrowExpr &)
122 {}
123 void
124 ASTLoweringBase::visit (AST::DereferenceExpr &)
125 {}
126 void
127 ASTLoweringBase::visit (AST::NegationExpr &)
128 {}
129 void
130 ASTLoweringBase::visit (AST::ArithmeticOrLogicalExpr &)
131 {}
132 void
133 ASTLoweringBase::visit (AST::ComparisonExpr &)
134 {}
135 void
136 ASTLoweringBase::visit (AST::LazyBooleanExpr &)
137 {}
138 void
139 ASTLoweringBase::visit (AST::TypeCastExpr &)
140 {}
141 void
142 ASTLoweringBase::visit (AST::AssignmentExpr &)
143 {}
144 void
145 ASTLoweringBase::visit (AST::CompoundAssignmentExpr &)
146 {}
147 void
148 ASTLoweringBase::visit (AST::GroupedExpr &)
149 {}
150 // void ASTLoweringBase::visit(ArrayElemselems) {}
151 void
152 ASTLoweringBase::visit (AST::ArrayElemsValues &)
153 {}
154 void
155 ASTLoweringBase::visit (AST::ArrayElemsCopied &)
156 {}
157 void
158 ASTLoweringBase::visit (AST::ArrayExpr &)
159 {}
160 void
161 ASTLoweringBase::visit (AST::ArrayIndexExpr &)
162 {}
163 void
164 ASTLoweringBase::visit (AST::TupleExpr &)
165 {}
166 void
167 ASTLoweringBase::visit (AST::TupleIndexExpr &)
168 {}
169 void
170 ASTLoweringBase::visit (AST::StructExprStruct &)
171 {}
172 // void ASTLoweringBase::visit(StructExprFieldfield) {}
173 void
174 ASTLoweringBase::visit (AST::StructExprFieldIdentifier &)
175 {}
176 void
177 ASTLoweringBase::visit (AST::StructExprFieldIdentifierValue &)
178 {}
179 void
180 ASTLoweringBase::visit (AST::StructExprFieldIndexValue &)
181 {}
182 void
183 ASTLoweringBase::visit (AST::StructExprStructFields &)
184 {}
185 void
186 ASTLoweringBase::visit (AST::StructExprStructBase &)
187 {}
188 void
189 ASTLoweringBase::visit (AST::CallExpr &)
190 {}
191 void
192 ASTLoweringBase::visit (AST::MethodCallExpr &)
193 {}
194 void
195 ASTLoweringBase::visit (AST::FieldAccessExpr &)
196 {}
197 void
198 ASTLoweringBase::visit (AST::ClosureExprInner &)
199 {}
200 void
201 ASTLoweringBase::visit (AST::BlockExpr &)
202 {}
203 void
204 ASTLoweringBase::visit (AST::ClosureExprInnerTyped &)
205 {}
206 void
207 ASTLoweringBase::visit (AST::ContinueExpr &)
208 {}
209 void
210 ASTLoweringBase::visit (AST::BreakExpr &)
211 {}
212 void
213 ASTLoweringBase::visit (AST::RangeFromToExpr &)
214 {}
215 void
216 ASTLoweringBase::visit (AST::RangeFromExpr &)
217 {}
218 void
219 ASTLoweringBase::visit (AST::RangeToExpr &)
220 {}
221 void
222 ASTLoweringBase::visit (AST::RangeFullExpr &)
223 {}
224 void
225 ASTLoweringBase::visit (AST::RangeFromToInclExpr &)
226 {}
227 void
228 ASTLoweringBase::visit (AST::RangeToInclExpr &)
229 {}
230
231 void
232 ASTLoweringBase::visit (AST::BoxExpr &)
233 {}
234
235 void
236 ASTLoweringBase::visit (AST::ReturnExpr &)
237 {}
238 void
239 ASTLoweringBase::visit (AST::UnsafeBlockExpr &)
240 {}
241 void
242 ASTLoweringBase::visit (AST::LoopExpr &)
243 {}
244 void
245 ASTLoweringBase::visit (AST::WhileLoopExpr &)
246 {}
247 void
248 ASTLoweringBase::visit (AST::WhileLetLoopExpr &)
249 {}
250 void
251 ASTLoweringBase::visit (AST::ForLoopExpr &)
252 {}
253 void
254 ASTLoweringBase::visit (AST::IfExpr &)
255 {}
256 void
257 ASTLoweringBase::visit (AST::IfExprConseqElse &)
258 {}
259 void
260 ASTLoweringBase::visit (AST::IfLetExpr &)
261 {}
262 void
263 ASTLoweringBase::visit (AST::IfLetExprConseqElse &)
264 {}
265
266 void
267 ASTLoweringBase::visit (AST::InlineAsm &)
268 {}
269
270 // void ASTLoweringBase::visit(MatchCasematch_case) {}
271 // void ASTLoweringBase:: (AST::MatchCaseBlockExpr &) {}
272 // void ASTLoweringBase:: (AST::MatchCaseExpr &) {}
273 void
274 ASTLoweringBase::visit (AST::MatchExpr &)
275 {}
276 void
277 ASTLoweringBase::visit (AST::AwaitExpr &)
278 {}
279 void
280 ASTLoweringBase::visit (AST::AsyncBlockExpr &)
281 {}
282
283 // rust-item.h
284 void
285 ASTLoweringBase::visit (AST::TypeParam &)
286 {}
287 // void ASTLoweringBase::visit(WhereClauseItemitem) {}
288 void
289 ASTLoweringBase::visit (AST::LifetimeWhereClauseItem &)
290 {}
291 void
292 ASTLoweringBase::visit (AST::TypeBoundWhereClauseItem &)
293 {}
294 void
295 ASTLoweringBase::visit (AST::Module &)
296 {}
297 void
298 ASTLoweringBase::visit (AST::ExternCrate &)
299 {}
300 // void ASTLoweringBase::visit(UseTreeuse_tree) {}
301 void
302 ASTLoweringBase::visit (AST::UseTreeGlob &)
303 {}
304 void
305 ASTLoweringBase::visit (AST::UseTreeList &)
306 {}
307 void
308 ASTLoweringBase::visit (AST::UseTreeRebind &)
309 {}
310 void
311 ASTLoweringBase::visit (AST::UseDeclaration &)
312 {}
313 void
314 ASTLoweringBase::visit (AST::Function &)
315 {}
316 void
317 ASTLoweringBase::visit (AST::TypeAlias &)
318 {}
319 void
320 ASTLoweringBase::visit (AST::StructStruct &)
321 {}
322 void
323 ASTLoweringBase::visit (AST::TupleStruct &)
324 {}
325 void
326 ASTLoweringBase::visit (AST::EnumItem &)
327 {}
328 void
329 ASTLoweringBase::visit (AST::EnumItemTuple &)
330 {}
331 void
332 ASTLoweringBase::visit (AST::EnumItemStruct &)
333 {}
334 void
335 ASTLoweringBase::visit (AST::EnumItemDiscriminant &)
336 {}
337 void
338 ASTLoweringBase::visit (AST::Enum &)
339 {}
340 void
341 ASTLoweringBase::visit (AST::Union &)
342 {}
343 void
344 ASTLoweringBase::visit (AST::ConstantItem &)
345 {}
346 void
347 ASTLoweringBase::visit (AST::StaticItem &)
348 {}
349 void
350 ASTLoweringBase::visit (AST::TraitItemConst &)
351 {}
352 void
353 ASTLoweringBase::visit (AST::TraitItemType &)
354 {}
355 void
356 ASTLoweringBase::visit (AST::Trait &)
357 {}
358 void
359 ASTLoweringBase::visit (AST::InherentImpl &)
360 {}
361 void
362 ASTLoweringBase::visit (AST::TraitImpl &)
363 {}
364 // void ASTLoweringBase::visit(ExternalItemitem) {}
365 void
366 ASTLoweringBase::visit (AST::ExternalTypeItem &)
367 {}
368 void
369 ASTLoweringBase::visit (AST::ExternalStaticItem &)
370 {}
371 void
372 ASTLoweringBase::visit (AST::ExternBlock &)
373 {}
374
375 // rust-macro.h
376 void
377 ASTLoweringBase::visit (AST::MacroMatchFragment &)
378 {}
379 void
380 ASTLoweringBase::visit (AST::MacroMatchRepetition &)
381 {}
382 void
383 ASTLoweringBase::visit (AST::MacroMatcher &)
384 {}
385 void
386 ASTLoweringBase::visit (AST::MacroRulesDefinition &)
387 {}
388 void
389 ASTLoweringBase::visit (AST::MetaItemPath &)
390 {}
391 void
392 ASTLoweringBase::visit (AST::MetaItemSeq &)
393 {}
394 void
395 ASTLoweringBase::visit (AST::MetaWord &)
396 {}
397 void
398 ASTLoweringBase::visit (AST::MetaNameValueStr &)
399 {}
400 void
401 ASTLoweringBase::visit (AST::MetaListPaths &)
402 {}
403 void
404 ASTLoweringBase::visit (AST::MetaListNameValueStr &)
405 {}
406
407 // rust-pattern.h
408 void
409 ASTLoweringBase::visit (AST::LiteralPattern &)
410 {}
411 void
412 ASTLoweringBase::visit (AST::IdentifierPattern &)
413 {}
414 void
415 ASTLoweringBase::visit (AST::WildcardPattern &)
416 {}
417 void
418 ASTLoweringBase::visit (AST::RestPattern &)
419 {}
420 // void ASTLoweringBase::visit(RangePatternBoundbound) {}
421 void
422 ASTLoweringBase::visit (AST::RangePatternBoundLiteral &)
423 {}
424 void
425 ASTLoweringBase::visit (AST::RangePatternBoundPath &)
426 {}
427 void
428 ASTLoweringBase::visit (AST::RangePatternBoundQualPath &)
429 {}
430 void
431 ASTLoweringBase::visit (AST::RangePattern &)
432 {}
433 void
434 ASTLoweringBase::visit (AST::ReferencePattern &)
435 {}
436 // void ASTLoweringBase::visit(StructPatternFieldfield) {}
437 void
438 ASTLoweringBase::visit (AST::StructPatternFieldTuplePat &)
439 {}
440 void
441 ASTLoweringBase::visit (AST::StructPatternFieldIdentPat &)
442 {}
443 void
444 ASTLoweringBase::visit (AST::StructPatternFieldIdent &)
445 {}
446 void
447 ASTLoweringBase::visit (AST::StructPattern &)
448 {}
449 // void ASTLoweringBase::visit(TupleStructItemstuple_items) {}
450 void
451 ASTLoweringBase::visit (AST::TupleStructItemsNoRange &)
452 {}
453 void
454 ASTLoweringBase::visit (AST::TupleStructItemsRange &)
455 {}
456 void
457 ASTLoweringBase::visit (AST::TupleStructPattern &)
458 {}
459 // void ASTLoweringBase::visit(TuplePatternItemstuple_items) {}
460 void
461 ASTLoweringBase::visit (AST::TuplePatternItemsMultiple &)
462 {}
463 void
464 ASTLoweringBase::visit (AST::TuplePatternItemsRanged &)
465 {}
466 void
467 ASTLoweringBase::visit (AST::TuplePattern &)
468 {}
469 void
470 ASTLoweringBase::visit (AST::GroupedPattern &)
471 {}
472 void
473 ASTLoweringBase::visit (AST::SlicePattern &)
474 {}
475 void
476 ASTLoweringBase::visit (AST::AltPattern &)
477 {}
478
479 // rust-stmt.h
480 void
481 ASTLoweringBase::visit (AST::EmptyStmt &)
482 {}
483 void
484 ASTLoweringBase::visit (AST::LetStmt &)
485 {}
486 void
487 ASTLoweringBase::visit (AST::ExprStmt &)
488 {}
489
490 // rust-type.h
491 void
492 ASTLoweringBase::visit (AST::TraitBound &)
493 {}
494 void
495 ASTLoweringBase::visit (AST::ImplTraitType &)
496 {}
497 void
498 ASTLoweringBase::visit (AST::TraitObjectType &)
499 {}
500 void
501 ASTLoweringBase::visit (AST::ParenthesisedType &)
502 {}
503 void
504 ASTLoweringBase::visit (AST::ImplTraitTypeOneBound &)
505 {}
506 void
507 ASTLoweringBase::visit (AST::TraitObjectTypeOneBound &)
508 {}
509 void
510 ASTLoweringBase::visit (AST::TupleType &)
511 {}
512 void
513 ASTLoweringBase::visit (AST::NeverType &)
514 {}
515 void
516 ASTLoweringBase::visit (AST::RawPointerType &)
517 {}
518 void
519 ASTLoweringBase::visit (AST::ReferenceType &)
520 {}
521 void
522 ASTLoweringBase::visit (AST::ArrayType &)
523 {}
524 void
525 ASTLoweringBase::visit (AST::SliceType &)
526 {}
527 void
528 ASTLoweringBase::visit (AST::InferredType &)
529 {}
530 void
531 ASTLoweringBase::visit (AST::BareFunctionType &)
532 {}
533
534 void
535 ASTLoweringBase::visit (AST::FunctionParam &param)
536 {}
537
538 void
539 ASTLoweringBase::visit (AST::VariadicParam &param)
540 {}
541
542 void
543 ASTLoweringBase::visit (AST::SelfParam &param)
544 {}
545
546 void
547 ASTLoweringBase::visit (AST::FormatArgs &fmt)
548 {}
549
550 HIR::Lifetime
551 ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
552 bool default_to_static_lifetime)
553 {
554 auto lifetime_type = lifetime.get_lifetime_type ();
555 if (lifetime_type == AST::Lifetime::WILDCARD && default_to_static_lifetime)
556 {
557 // If compiling in a static context.
558 lifetime_type = AST::Lifetime::STATIC;
559 }
560
561 auto crate_num = mappings.get_current_crate ();
562 Analysis::NodeMapping mapping (crate_num, lifetime.get_node_id (),
563 mappings.get_next_hir_id (crate_num),
564 UNKNOWN_LOCAL_DEFID);
565 mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
566
567 return HIR::Lifetime (mapping, lifetime_type, lifetime.get_lifetime_name (),
568 lifetime.get_locus ());
569 }
570
571 HIR::LoopLabel
572 ASTLoweringBase::lower_loop_label (AST::LoopLabel &loop_label)
573 {
574 HIR::Lifetime life = lower_lifetime (loop_label.get_lifetime ());
575
576 auto crate_num = mappings.get_current_crate ();
577 Analysis::NodeMapping mapping (crate_num, loop_label.get_node_id (),
578 mappings.get_next_hir_id (crate_num),
579 UNKNOWN_LOCAL_DEFID);
580 mappings.insert_node_to_hir (mapping.get_nodeid (), mapping.get_hirid ());
581
582 return HIR::LoopLabel (mapping, std::move (life), loop_label.get_locus ());
583 }
584
585 std::vector<std::unique_ptr<HIR::GenericParam>>
586 ASTLoweringBase::lower_generic_params (
587 std::vector<std::unique_ptr<AST::GenericParam>> &params)
588 {
589 std::vector<std::unique_ptr<HIR::GenericParam>> lowered;
590 for (auto &ast_param : params)
591 {
592 auto hir_param = ASTLowerGenericParam::translate (*ast_param);
593 lowered.push_back (std::unique_ptr<HIR::GenericParam> (hir_param));
594 }
595
596 return lowered;
597 }
598
599 HIR::PathExprSegment
600 ASTLoweringBase::lower_path_expr_seg (AST::PathExprSegment &s)
601 {
602 auto crate_num = mappings.get_current_crate ();
603 Analysis::NodeMapping mapping (crate_num, s.get_node_id (),
604 mappings.get_next_hir_id (crate_num),
605 UNKNOWN_LOCAL_DEFID);
606
607 return HIR::PathExprSegment (
608 std::move (mapping),
609 HIR::PathIdentSegment (s.get_ident_segment ().as_string ()), s.get_locus (),
610 s.has_generic_args () ? lower_generic_args (s.get_generic_args ())
611 : HIR::GenericArgs::create_empty ());
612 }
613
614 HIR::GenericArgsBinding
615 ASTLoweringBase::lower_binding (AST::GenericArgsBinding &binding)
616 {
617 HIR::Type *lowered_type = ASTLoweringType::translate (binding.get_type ());
618 return HIR::GenericArgsBinding (binding.get_identifier (),
619 std::unique_ptr<HIR::Type> (lowered_type),
620 binding.get_locus ());
621 }
622
623 HIR::GenericArgs
624 ASTLoweringBase::lower_generic_args (AST::GenericArgs &args)
625 {
626 std::vector<HIR::GenericArgsBinding> binding_args;
627 for (auto &binding : args.get_binding_args ())
628 {
629 HIR::GenericArgsBinding b = lower_binding (binding);
630 binding_args.push_back (std::move (b));
631 }
632
633 std::vector<HIR::Lifetime> lifetime_args;
634 for (auto &lifetime : args.get_lifetime_args ())
635 {
636 HIR::Lifetime l = lower_lifetime (lifetime);
637 lifetime_args.push_back (std::move (l));
638 }
639
640 std::vector<std::unique_ptr<HIR::Type>> type_args;
641 std::vector<HIR::ConstGenericArg> const_args;
642
643 for (auto &arg : args.get_generic_args ())
644 {
645 switch (arg.get_kind ())
646 {
647 case AST::GenericArg::Kind::Type: {
648 auto type = ASTLoweringType::translate (arg.get_type ());
649 type_args.emplace_back (std::unique_ptr<HIR::Type> (type));
650 break;
651 }
652 case AST::GenericArg::Kind::Const: {
653 auto expr = ASTLoweringExpr::translate (arg.get_expression ());
654 const_args.emplace_back (
655 HIR::ConstGenericArg (std::unique_ptr<HIR::Expr> (expr),
656 expr->get_locus ()));
657 break;
658 }
659 default:
660 rust_unreachable ();
661 }
662 }
663
664 return HIR::GenericArgs (std::move (lifetime_args), std::move (type_args),
665 std::move (binding_args), std::move (const_args),
666 args.get_locus ());
667 }
668
669 HIR::SelfParam
670 ASTLoweringBase::lower_self (AST::Param &param)
671 {
672 rust_assert (param.is_self ());
673
674 auto self = static_cast<AST::SelfParam &> (param);
675 auto crate_num = mappings.get_current_crate ();
676 Analysis::NodeMapping mapping (crate_num, self.get_node_id (),
677 mappings.get_next_hir_id (crate_num),
678 mappings.get_next_localdef_id (crate_num));
679 mappings.insert_location (mapping.get_hirid (), param.get_locus ());
680
681 if (self.has_type ())
682 {
683 HIR::Type *type = ASTLoweringType::translate (self.get_type ());
684 return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (type),
685 self.get_is_mut (), self.get_locus ());
686 }
687 else if (!self.get_has_ref ())
688 {
689 return HIR::SelfParam (mapping, std::unique_ptr<HIR::Type> (nullptr),
690 self.get_is_mut (), self.get_locus ());
691 }
692
693 tl::optional<HIR::Lifetime> lifetime = tl::nullopt;
694
695 if (self.has_lifetime ())
696 lifetime = lower_lifetime (self.get_lifetime ());
697
698 return HIR::SelfParam (mapping, lifetime, self.get_is_mut (),
699 self.get_locus ());
700 }
701
702 HIR::Type *
703 ASTLoweringBase::lower_type_no_bounds (AST::TypeNoBounds &type)
704 {
705 return ASTLoweringType::translate (type);
706 }
707
708 HIR::TypeParamBound *
709 ASTLoweringBase::lower_bound (AST::TypeParamBound &bound)
710 {
711 return ASTLoweringTypeBounds::translate (bound);
712 }
713
714 /* Checks whether the name of a field already exists. Returns true
715 and produces an error if so. */
716 bool
717 struct_field_name_exists (std::vector<HIR::StructField> &fields,
718 HIR::StructField &new_field)
719 {
720 for (auto &field : fields)
721 {
722 if (field.get_field_name ().as_string ().compare (
723 new_field.get_field_name ().as_string ())
724 == 0)
725 {
726 rich_location r (line_table, new_field.get_locus ());
727 r.add_range (field.get_locus ());
728 rust_error_at (r, ErrorCode::E0124, "field %qs is already declared",
729 field.get_field_name ().as_string ().c_str ());
730 return true;
731 }
732 }
733 return false;
734 }
735
736 HIR::FunctionQualifiers
737 ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers)
738 {
739 Unsafety unsafety
740 = qualifiers.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
741 bool has_extern = qualifiers.is_extern ();
742 ABI abi = has_extern ? ABI::C : ABI::RUST;
743
744 if (qualifiers.has_abi ())
745 {
746 const std::string &extern_abi = qualifiers.get_extern_abi ();
747 abi = get_abi_from_string (extern_abi);
748 if (has_extern && abi == ABI::UNKNOWN)
749 rust_error_at (qualifiers.get_locus (), ErrorCode::E0703,
750 "invalid ABI: found %qs", extern_abi.c_str ());
751 }
752
753 return HIR::FunctionQualifiers (qualifiers.get_async_status (),
754 qualifiers.get_const_status (), unsafety,
755 has_extern, abi);
756 }
757
758 void
759 ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
760 {
761 for (const auto &attr : item.get_outer_attrs ())
762 {
763 const auto &str_path = attr.get_path ().as_string ();
764 if (!Analysis::Attributes::is_known (str_path))
765 {
766 rust_error_at (attr.get_locus (), "unknown attribute");
767 continue;
768 }
769
770 bool is_lang_item = str_path == Values::Attributes::LANG
771 && attr.has_attr_input ()
772 && attr.get_attr_input ().get_attr_input_type ()
773 == AST::AttrInput::AttrInputType::LITERAL;
774
775 bool is_doc_item = str_path == Values::Attributes::DOC;
776
777 if (is_doc_item)
778 handle_doc_item_attribute (item, attr);
779 else if (is_lang_item)
780 handle_lang_item_attribute (item, attr);
781 else if (!attribute_handled_in_another_pass (str_path))
782 {
783 rust_error_at (attr.get_locus (), "unhandled attribute: [%s]",
784 attr.get_path ().as_string ().c_str ());
785 }
786 }
787 }
788
789 void
790 ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &,
791 const AST::Attribute &attr)
792 {
793 auto simple_doc_comment = attr.has_attr_input ()
794 && attr.get_attr_input ().get_attr_input_type ()
795 == AST::AttrInput::AttrInputType::LITERAL;
796 if (simple_doc_comment)
797 return;
798
799 const AST::AttrInput &input = attr.get_attr_input ();
800 bool is_token_tree
801 = input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
802 rust_assert (is_token_tree);
803 const auto &option = static_cast<const AST::DelimTokenTree &> (input);
804 AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
805
806 // TODO: add actual and complete checks for the doc attributes
807 //
808 // FIXME: Move this to the AttributeChecker visitor
809 rust_assert (meta_item);
810 }
811
812 void
813 ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
814 const AST::Attribute &attr)
815 {
816 auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
817 const auto &lang_item_type_str = literal.get_literal ().as_string ();
818 auto lang_item_type = LangItem::Parse (lang_item_type_str);
819
820 if (lang_item_type)
821 mappings.insert_lang_item (*lang_item_type,
822 item.get_mappings ().get_defid ());
823 else
824 rust_error_at (attr.get_locus (), "unknown lang item");
825 }
826
827 bool
828 ASTLoweringBase::attribute_handled_in_another_pass (
829 const std::string &attribute_path) const
830 {
831 const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
832 if (lookup.is_error ())
833 return false;
834
835 if (lookup.handler == Analysis::CompilerPass::UNKNOWN)
836 return false;
837
838 return lookup.handler != Analysis::CompilerPass::HIR_LOWERING;
839 }
840
841 std::unique_ptr<HIR::TuplePatternItems>
842 ASTLoweringBase::lower_tuple_pattern_multiple (
843 AST::TuplePatternItemsMultiple &pattern)
844 {
845 std::vector<std::unique_ptr<HIR::Pattern>> patterns;
846 for (auto &p : pattern.get_patterns ())
847 {
848 HIR::Pattern *translated = ASTLoweringPattern::translate (*p);
849 patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
850 }
851
852 return std::unique_ptr<HIR::TuplePatternItems> (
853 new HIR::TuplePatternItemsMultiple (std::move (patterns)));
854 }
855
856 std::unique_ptr<TuplePatternItems>
857 ASTLoweringBase::lower_tuple_pattern_ranged (
858 AST::TuplePatternItemsRanged &pattern)
859 {
860 std::vector<std::unique_ptr<HIR::Pattern>> lower_patterns;
861 std::vector<std::unique_ptr<HIR::Pattern>> upper_patterns;
862
863 for (auto &p : pattern.get_lower_patterns ())
864 {
865 HIR::Pattern *translated = ASTLoweringPattern::translate (*p);
866 lower_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
867 }
868
869 for (auto &p : pattern.get_upper_patterns ())
870 {
871 HIR::Pattern *translated = ASTLoweringPattern::translate (*p);
872 upper_patterns.push_back (std::unique_ptr<HIR::Pattern> (translated));
873 }
874
875 return std::unique_ptr<HIR::TuplePatternItems> (
876 new HIR::TuplePatternItemsRanged (std::move (lower_patterns),
877 std::move (upper_patterns)));
878 }
879
880 std::unique_ptr<HIR::RangePatternBound>
881 ASTLoweringBase::lower_range_pattern_bound (AST::RangePatternBound &bound)
882 {
883 std::unique_ptr<HIR::RangePatternBound> hir_bound = nullptr;
884 switch (bound.get_bound_type ())
885 {
886 case AST::RangePatternBound::RangePatternBoundType::LITERAL: {
887 AST::RangePatternBoundLiteral &ref
888 = static_cast<AST::RangePatternBoundLiteral &> (bound);
889
890 HIR::Literal literal = lower_literal (ref.get_literal ());
891
892 hir_bound = std::unique_ptr<HIR::RangePatternBound> (
893 new HIR::RangePatternBoundLiteral (literal, ref.get_locus (),
894 ref.get_has_minus ()));
895 }
896 break;
897 case AST::RangePatternBound::RangePatternBoundType::PATH: {
898 auto &ref = static_cast<AST::RangePatternBoundPath &> (bound);
899
900 HIR::PathInExpression *path
901 = ASTLowerPathInExpression::translate (ref.get_path ());
902
903 hir_bound = std::unique_ptr<HIR::RangePatternBound> (
904 new HIR::RangePatternBoundPath (*path));
905 }
906 break;
907 case AST::RangePatternBound::RangePatternBoundType::QUALPATH: {
908 auto &ref = static_cast<AST::RangePatternBoundQualPath &> (bound);
909
910 HIR::QualifiedPathInExpression *qualpath
911 = ASTLowerQualPathInExpression::translate (ref.get_qualified_path ());
912
913 hir_bound = std::unique_ptr<HIR::RangePatternBound> (
914 new HIR::RangePatternBoundQualPath (*qualpath));
915 }
916 break;
917 }
918
919 return hir_bound;
920 }
921
922 HIR::Literal
923 ASTLoweringBase::lower_literal (const AST::Literal &literal)
924 {
925 HIR::Literal::LitType type = HIR::Literal::LitType::CHAR;
926 switch (literal.get_lit_type ())
927 {
928 case AST::Literal::LitType::CHAR:
929 type = HIR::Literal::LitType::CHAR;
930 break;
931 case AST::Literal::LitType::STRING:
932 type = HIR::Literal::LitType::STRING;
933 break;
934 case AST::Literal::LitType::BYTE:
935 type = HIR::Literal::LitType::BYTE;
936 break;
937 case AST::Literal::LitType::BYTE_STRING:
938 type = HIR::Literal::LitType::BYTE_STRING;
939 break;
940 case AST::Literal::LitType::RAW_STRING:
941 type = HIR::Literal::LitType::STRING;
942 break;
943 case AST::Literal::LitType::INT:
944 type = HIR::Literal::LitType::INT;
945 break;
946 case AST::Literal::LitType::FLOAT:
947 type = HIR::Literal::LitType::FLOAT;
948 break;
949 case AST::Literal::LitType::BOOL:
950 type = HIR::Literal::LitType::BOOL;
951 break;
952 case AST::Literal::LitType::ERROR:
953 rust_unreachable ();
954 break;
955 }
956
957 return HIR::Literal (literal.as_string (), type, literal.get_type_hint ());
958 }
959
960 HIR::ExternBlock *
961 ASTLoweringBase::lower_extern_block (AST::ExternBlock &extern_block)
962 {
963 HIR::Visibility vis = translate_visibility (extern_block.get_visibility ());
964
965 auto crate_num = mappings.get_current_crate ();
966 Analysis::NodeMapping mapping (crate_num, extern_block.get_node_id (),
967 mappings.get_next_hir_id (crate_num),
968 mappings.get_next_localdef_id (crate_num));
969
970 std::vector<std::unique_ptr<HIR::ExternalItem>> extern_items;
971 for (auto &item : extern_block.get_extern_items ())
972 {
973 if (item->is_marked_for_strip ())
974 continue;
975
976 HIR::ExternalItem *lowered
977 = ASTLoweringExternItem::translate (item.get (), mapping.get_hirid ());
978 extern_items.push_back (std::unique_ptr<HIR::ExternalItem> (lowered));
979 }
980
981 ABI abi = ABI::C;
982 if (extern_block.has_abi ())
983 {
984 const std::string &extern_abi = extern_block.get_abi ();
985 abi = get_abi_from_string (extern_abi);
986 if (abi == ABI::UNKNOWN)
987 rust_error_at (extern_block.get_locus (), ErrorCode::E0703,
988 "invalid ABI: found %qs", extern_abi.c_str ());
989 }
990
991 HIR::ExternBlock *hir_extern_block
992 = new HIR::ExternBlock (mapping, abi, std::move (extern_items),
993 std::move (vis), extern_block.get_inner_attrs (),
994 extern_block.get_outer_attrs (),
995 extern_block.get_locus ());
996
997 mappings.insert_hir_extern_block (hir_extern_block);
998
999 return hir_extern_block;
1000 }
1001
1002 void
1003 ASTLoweringBase::lower_macro_definition (AST::MacroRulesDefinition &def)
1004 {
1005 auto is_export = false;
1006 for (const auto &attr : def.get_outer_attrs ())
1007 if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
1008 is_export = true;
1009
1010 if (is_export)
1011 {
1012 mappings.insert_exported_macro (def);
1013 mappings.insert_ast_item (&def);
1014 mappings.insert_location (def.get_node_id (), def.get_locus ());
1015 }
1016 }
1017
1018 } // namespace HIR
1019 } // namespace Rust