| 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-name-resolution-context.h" |
| 20 |
#include "optional.h" |
| 21 |
#include "rust-mapping-common.h" |
| 22 |
|
| 23 |
namespace Rust { |
| 24 |
namespace Resolver2_0 { |
| 25 |
|
| 26 |
NameResolutionContext::NameResolutionContext () |
| 27 |
: mappings (Analysis::Mappings::get ()) |
| 28 |
{} |
| 29 |
|
| 30 |
tl::expected<NodeId, DuplicateNameError> |
| 31 |
NameResolutionContext::insert (Identifier name, NodeId id, Namespace ns) |
| 32 |
{ |
| 33 |
switch (ns) |
| 34 |
{ |
| 35 |
case Namespace::Values: |
| 36 |
return values.insert (name, id); |
| 37 |
case Namespace::Types: |
| 38 |
return types.insert (name, id); |
| 39 |
case Namespace::Macros: |
| 40 |
return macros.insert (name, id); |
| 41 |
case Namespace::Labels: |
| 42 |
default: |
| 43 |
// return labels.insert (name, id); |
| 44 |
rust_unreachable (); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
tl::expected<NodeId, DuplicateNameError> |
| 49 |
NameResolutionContext::insert_variant (Identifier name, NodeId id) |
| 50 |
{ |
| 51 |
return types.insert_variant (name, id); |
| 52 |
} |
| 53 |
|
| 54 |
tl::expected<NodeId, DuplicateNameError> |
| 55 |
NameResolutionContext::insert_shadowable (Identifier name, NodeId id, |
| 56 |
Namespace ns) |
| 57 |
{ |
| 58 |
switch (ns) |
| 59 |
{ |
| 60 |
case Namespace::Values: |
| 61 |
return values.insert_shadowable (name, id); |
| 62 |
case Namespace::Types: |
| 63 |
return types.insert_shadowable (name, id); |
| 64 |
case Namespace::Macros: |
| 65 |
return macros.insert_shadowable (name, id); |
| 66 |
case Namespace::Labels: |
| 67 |
default: |
| 68 |
// return labels.insert (name, id); |
| 69 |
rust_unreachable (); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
tl::expected<NodeId, DuplicateNameError> |
| 74 |
NameResolutionContext::insert_globbed (Identifier name, NodeId id, Namespace ns) |
| 75 |
{ |
| 76 |
switch (ns) |
| 77 |
{ |
| 78 |
case Namespace::Values: |
| 79 |
return values.insert_globbed (name, id); |
| 80 |
case Namespace::Types: |
| 81 |
return types.insert_globbed (name, id); |
| 82 |
case Namespace::Macros: |
| 83 |
return macros.insert_globbed (name, id); |
| 84 |
case Namespace::Labels: |
| 85 |
default: |
| 86 |
// return labels.insert (name, id); |
| 87 |
rust_unreachable (); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void |
| 92 |
NameResolutionContext::map_usage (Usage usage, Definition definition) |
| 93 |
{ |
| 94 |
auto inserted = resolved_nodes.emplace (usage, definition).second; |
| 95 |
|
| 96 |
// is that valid? |
| 97 |
rust_assert (inserted); |
| 98 |
} |
| 99 |
|
| 100 |
tl::optional<NodeId> |
| 101 |
NameResolutionContext::lookup (NodeId usage) const |
| 102 |
{ |
| 103 |
auto it = resolved_nodes.find (Usage (usage)); |
| 104 |
|
| 105 |
if (it == resolved_nodes.end ()) |
| 106 |
return tl::nullopt; |
| 107 |
|
| 108 |
return it->second.id; |
| 109 |
} |
| 110 |
|
| 111 |
void |
| 112 |
NameResolutionContext::scoped (Rib::Kind rib_kind, NodeId id, |
| 113 |
std::function<void (void)> lambda, |
| 114 |
tl::optional<Identifier> path) |
| 115 |
{ |
| 116 |
// NOTE: You must be at the root node when pushing the prelude rib. |
| 117 |
values.push (rib_kind, id, path); |
| 118 |
types.push (rib_kind, id, path); |
| 119 |
macros.push (rib_kind, id, path); |
| 120 |
// labels.push (rib, id); |
| 121 |
|
| 122 |
lambda (); |
| 123 |
|
| 124 |
values.pop (); |
| 125 |
types.pop (); |
| 126 |
macros.pop (); |
| 127 |
// labels.pop (rib); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
NameResolutionContext::scoped (Rib::Kind rib_kind, Namespace ns, |
| 132 |
NodeId scope_id, |
| 133 |
std::function<void (void)> lambda, |
| 134 |
tl::optional<Identifier> path) |
| 135 |
{ |
| 136 |
// This could work... I'm not sure why you would want to do this though. |
| 137 |
rust_assert (rib_kind != Rib::Kind::Prelude); |
| 138 |
|
| 139 |
switch (ns) |
| 140 |
{ |
| 141 |
case Namespace::Values: |
| 142 |
values.push (rib_kind, scope_id, path); |
| 143 |
break; |
| 144 |
case Namespace::Types: |
| 145 |
types.push (rib_kind, scope_id, path); |
| 146 |
break; |
| 147 |
case Namespace::Labels: |
| 148 |
case Namespace::Macros: |
| 149 |
gcc_unreachable (); |
| 150 |
} |
| 151 |
|
| 152 |
lambda (); |
| 153 |
|
| 154 |
switch (ns) |
| 155 |
{ |
| 156 |
case Namespace::Values: |
| 157 |
values.pop (); |
| 158 |
break; |
| 159 |
case Namespace::Types: |
| 160 |
types.pop (); |
| 161 |
break; |
| 162 |
case Namespace::Labels: |
| 163 |
case Namespace::Macros: |
| 164 |
gcc_unreachable (); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
} // namespace Resolver2_0 |
| 169 |
} // namespace Rust |