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