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