|
| 1 | +// Copyright (C) 2026 Free Software Foundation, Inc. |
| 2 | + |
| 3 | +// This file is part of GCC. |
| 4 | + |
| 5 | +// GCC is free software; you can redistribute it and/or modify it under |
| 6 | +// the terms of the GNU General Public License as published by the Free |
| 7 | +// Software Foundation; either version 3, or (at your option) any later |
| 8 | +// version. |
| 9 | + |
| 10 | +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +// for more details. |
| 14 | + |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with GCC; see the file COPYING3. If not see |
| 17 | +// <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +#include "rust-system.h" |
| 20 | +#include "rust-identifier-path.h" |
| 21 | +#include "rust-pattern.h" |
| 22 | + |
| 23 | +namespace Rust { |
| 24 | +namespace Resolver2_0 { |
| 25 | + |
| 26 | +IdentifierPathPass::IdentifierPathPass ( |
| 27 | + NameResolutionContext &ctx, std::map<Usage, Definition> expected_mappings) |
| 28 | + : ctx (&ctx), expected_mappings (std::move (expected_mappings)) |
| 29 | +{} |
| 30 | + |
| 31 | +void |
| 32 | +IdentifierPathPass::go (AST::Crate &crate, NameResolutionContext &ctx, |
| 33 | + std::map<Usage, Definition> expected_mappings) |
| 34 | +{ |
| 35 | + IdentifierPathPass pass (ctx, std::move (expected_mappings)); |
| 36 | + pass.visit (crate); |
| 37 | +} |
| 38 | + |
| 39 | +void |
| 40 | +IdentifierPathPass::reseat (std::unique_ptr<AST::Pattern> &ptr) |
| 41 | +{ |
| 42 | + AST::IdentifierPattern *ident_pat; |
| 43 | + if (ptr->get_pattern_kind () == AST::Pattern::Kind::Identifier) |
| 44 | + ident_pat = static_cast<AST::IdentifierPattern *> (ptr.get ()); |
| 45 | + else |
| 46 | + return; |
| 47 | + |
| 48 | + auto expected_it = expected_mappings.find (Usage (ident_pat->get_node_id ())); |
| 49 | + |
| 50 | + if (expected_it != expected_mappings.end ()) |
| 51 | + { |
| 52 | + auto def = expected_it->second; |
| 53 | + |
| 54 | + std::vector<AST::PathExprSegment> segments; |
| 55 | + segments.emplace_back (ident_pat->get_ident ().as_string (), |
| 56 | + ident_pat->get_locus ()); |
| 57 | + ctx->map_usage (Usage (segments.back ().get_node_id ()), def); |
| 58 | + ptr = std::make_unique<AST::PathInExpression> ( |
| 59 | + std::move (segments), std::vector<AST::Attribute> (), |
| 60 | + ident_pat->get_locus ()); |
| 61 | + ctx->map_usage (Usage (ptr->get_node_id ()), def); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace Resolver2_0 |
| 66 | +} // namespace Rust |
0 commit comments