| 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 "expected.h" |
| 20 |
#include "rust-ast.h" |
| 21 |
#include "rust-diagnostics.h" |
| 22 |
#include "rust-forever-stack.h" |
| 23 |
#include "rust-edition.h" |
| 24 |
#include "rust-rib.h" |
| 25 |
#include "rust-unwrap-segment.h" |
| 26 |
#include "optional.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace Resolver2_0 { |
| 30 |
|
| 31 |
template <Namespace N> |
| 32 |
bool |
| 33 |
ForeverStack<N>::Node::is_root () const |
| 34 |
{ |
| 35 |
return !parent.has_value (); |
| 36 |
} |
| 37 |
|
| 38 |
template <Namespace N> |
| 39 |
bool |
| 40 |
ForeverStack<N>::Node::is_prelude () const |
| 41 |
{ |
| 42 |
return rib.kind == Rib::Kind::Prelude; |
| 43 |
} |
| 44 |
|
| 45 |
template <Namespace N> |
| 46 |
bool |
| 47 |
ForeverStack<N>::Node::is_leaf () const |
| 48 |
{ |
| 49 |
return children.empty (); |
| 50 |
} |
| 51 |
|
| 52 |
template <Namespace N> |
| 53 |
void |
| 54 |
ForeverStack<N>::Node::insert_child (Link link, Node child) |
| 55 |
{ |
| 56 |
auto res = children.insert ({link, child}); |
| 57 |
|
| 58 |
// Do we want to error if the child already exists? Probably not, right? |
| 59 |
// That's kinda the point, isn't it. So this method always succeeds, right? |
| 60 |
} |
| 61 |
|
| 62 |
template <Namespace N> |
| 63 |
void |
| 64 |
ForeverStack<N>::push (Rib::Kind rib_kind, NodeId id, |
| 65 |
tl::optional<Identifier> path) |
| 66 |
{ |
| 67 |
push_inner (rib_kind, Link (id, path)); |
| 68 |
} |
| 69 |
|
| 70 |
template <Namespace N> |
| 71 |
void |
| 72 |
ForeverStack<N>::push_inner (Rib rib, Link link) |
| 73 |
{ |
| 74 |
if (rib.kind == Rib::Kind::Prelude) |
| 75 |
{ |
| 76 |
// If you push_inner into the prelude from outside the root, you will pop |
| 77 |
// back into the root, which could screw up a traversal. |
| 78 |
rust_assert (&cursor_reference.get () == &root); |
| 79 |
// Prelude doesn't have an access path |
| 80 |
rust_assert (!link.path); |
| 81 |
update_cursor (this->lang_prelude); |
| 82 |
return; |
| 83 |
} |
| 84 |
// If the link does not exist, we create it and emplace a new `Node` with the |
| 85 |
// current node as its parent. `unordered_map::emplace` returns a pair with |
| 86 |
// the iterator and a boolean. If the value already exists, the iterator |
| 87 |
// points to it. Otherwise, it points to the newly emplaced value, so we can |
| 88 |
// just update our cursor(). |
| 89 |
auto emplace = cursor ().children.emplace ( |
| 90 |
std::make_pair (link, Node (rib, link.id, cursor ()))); |
| 91 |
|
| 92 |
auto it = emplace.first; |
| 93 |
auto existed = !emplace.second; |
| 94 |
|
| 95 |
rust_debug ("inserting link: Link(%d [%s]): existed? %s", link.id, |
| 96 |
link.path.has_value () ? link.path.value ().as_string ().c_str () |
| 97 |
: "<anon>", |
| 98 |
existed ? "yes" : "no"); |
| 99 |
|
| 100 |
// We update the cursor |
| 101 |
update_cursor (it->second); |
| 102 |
} |
| 103 |
|
| 104 |
template <Namespace N> |
| 105 |
void |
| 106 |
ForeverStack<N>::pop () |
| 107 |
{ |
| 108 |
rust_assert (!cursor ().is_root ()); |
| 109 |
|
| 110 |
rust_debug ("popping link"); |
| 111 |
|
| 112 |
for (const auto &kv : cursor ().rib.get_values ()) |
| 113 |
rust_debug ("current_rib: k: %s, v: %s", kv.first.c_str (), |
| 114 |
kv.second.to_string ().c_str ()); |
| 115 |
|
| 116 |
if (cursor ().parent.has_value ()) |
| 117 |
for (const auto &kv : cursor ().parent.value ().rib.get_values ()) |
| 118 |
rust_debug ("new cursor: k: %s, v: %s", kv.first.c_str (), |
| 119 |
kv.second.to_string ().c_str ()); |
| 120 |
|
| 121 |
update_cursor (cursor ().parent.value ()); |
| 122 |
} |
| 123 |
|
| 124 |
static tl::expected<NodeId, DuplicateNameError> |
| 125 |
insert_inner (Rib &rib, std::string name, Rib::Definition definition) |
| 126 |
{ |
| 127 |
return rib.insert (name, definition); |
| 128 |
} |
| 129 |
|
| 130 |
template <Namespace N> |
| 131 |
tl::expected<NodeId, DuplicateNameError> |
| 132 |
ForeverStack<N>::insert (Identifier name, NodeId node) |
| 133 |
{ |
| 134 |
auto &innermost_rib = peek (); |
| 135 |
|
| 136 |
// So what do we do here - if the Rib has already been pushed in an earlier |
| 137 |
// pass, we might end up in a situation where it is okay to re-add new names. |
| 138 |
// Do we just ignore that here? Do we keep track of if the Rib is new or not? |
| 139 |
// should our cursor have info on the current node like "is it newly pushed"? |
| 140 |
return insert_inner (innermost_rib, name.as_string (), |
| 141 |
Rib::Definition::NonShadowable (node)); |
| 142 |
} |
| 143 |
|
| 144 |
template <Namespace N> |
| 145 |
tl::expected<NodeId, DuplicateNameError> |
| 146 |
ForeverStack<N>::insert_shadowable (Identifier name, NodeId node) |
| 147 |
{ |
| 148 |
auto &innermost_rib = peek (); |
| 149 |
|
| 150 |
return insert_inner (innermost_rib, name.as_string (), |
| 151 |
Rib::Definition::Shadowable (node)); |
| 152 |
} |
| 153 |
|
| 154 |
template <Namespace N> |
| 155 |
tl::expected<NodeId, DuplicateNameError> |
| 156 |
ForeverStack<N>::insert_globbed (Identifier name, NodeId node) |
| 157 |
{ |
| 158 |
auto &innermost_rib = peek (); |
| 159 |
|
| 160 |
return insert_inner (innermost_rib, name.as_string (), |
| 161 |
Rib::Definition::Globbed (node)); |
| 162 |
} |
| 163 |
|
| 164 |
template <Namespace N> |
| 165 |
tl::expected<NodeId, DuplicateNameError> |
| 166 |
ForeverStack<N>::insert_at_root (Identifier name, NodeId node) |
| 167 |
{ |
| 168 |
auto &root_rib = root.rib; |
| 169 |
|
| 170 |
// inserting in the root of the crate is never a shadowing operation, even for |
| 171 |
// macros |
| 172 |
return insert_inner (root_rib, name.as_string (), |
| 173 |
Rib::Definition::NonShadowable (node)); |
| 174 |
} |
| 175 |
|
| 176 |
// Specialization for Macros and Labels - where we are allowed to shadow |
| 177 |
// existing definitions |
| 178 |
template <> |
| 179 |
inline tl::expected<NodeId, DuplicateNameError> |
| 180 |
ForeverStack<Namespace::Macros>::insert (Identifier name, NodeId node) |
| 181 |
{ |
| 182 |
return insert_inner (peek (), name.as_string (), |
| 183 |
Rib::Definition::Shadowable (node)); |
| 184 |
} |
| 185 |
|
| 186 |
template <> |
| 187 |
inline tl::expected<NodeId, DuplicateNameError> |
| 188 |
ForeverStack<Namespace::Labels>::insert (Identifier name, NodeId node) |
| 189 |
{ |
| 190 |
return insert_inner (peek (), name.as_string (), |
| 191 |
Rib::Definition::Shadowable (node)); |
| 192 |
} |
| 193 |
|
| 194 |
template <> |
| 195 |
inline tl::expected<NodeId, DuplicateNameError> |
| 196 |
ForeverStack<Namespace::Types>::insert_variant (Identifier name, NodeId node) |
| 197 |
{ |
| 198 |
return insert_inner (peek (), name.as_string (), |
| 199 |
Rib::Definition::NonShadowable (node, true)); |
| 200 |
} |
| 201 |
|
| 202 |
template <Namespace N> |
| 203 |
Rib & |
| 204 |
ForeverStack<N>::peek () |
| 205 |
{ |
| 206 |
return cursor ().rib; |
| 207 |
} |
| 208 |
|
| 209 |
template <Namespace N> |
| 210 |
const Rib & |
| 211 |
ForeverStack<N>::peek () const |
| 212 |
{ |
| 213 |
return cursor ().rib; |
| 214 |
} |
| 215 |
|
| 216 |
template <Namespace N> |
| 217 |
void |
| 218 |
ForeverStack<N>::reverse_iter (std::function<KeepGoing (Node &)> lambda) |
| 219 |
{ |
| 220 |
return reverse_iter (cursor (), lambda); |
| 221 |
} |
| 222 |
|
| 223 |
template <Namespace N> |
| 224 |
void |
| 225 |
ForeverStack<N>::reverse_iter ( |
| 226 |
std::function<KeepGoing (const Node &)> lambda) const |
| 227 |
{ |
| 228 |
return reverse_iter (cursor (), lambda); |
| 229 |
} |
| 230 |
|
| 231 |
template <Namespace N> |
| 232 |
void |
| 233 |
ForeverStack<N>::reverse_iter (Node &start, |
| 234 |
std::function<KeepGoing (Node &)> lambda) |
| 235 |
{ |
| 236 |
auto *tmp = &start; |
| 237 |
|
| 238 |
while (true) |
| 239 |
{ |
| 240 |
auto keep_going = lambda (*tmp); |
| 241 |
if (keep_going == KeepGoing::No) |
| 242 |
return; |
| 243 |
|
| 244 |
if (tmp->is_root ()) |
| 245 |
return; |
| 246 |
|
| 247 |
tmp = &tmp->parent.value (); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
template <Namespace N> |
| 252 |
void |
| 253 |
ForeverStack<N>::reverse_iter ( |
| 254 |
const Node &start, std::function<KeepGoing (const Node &)> lambda) const |
| 255 |
{ |
| 256 |
auto *tmp = &start; |
| 257 |
|
| 258 |
while (true) |
| 259 |
{ |
| 260 |
auto keep_going = lambda (*tmp); |
| 261 |
if (keep_going == KeepGoing::No) |
| 262 |
return; |
| 263 |
|
| 264 |
if (tmp->is_root ()) |
| 265 |
return; |
| 266 |
|
| 267 |
tmp = &tmp->parent.value (); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
template <Namespace N> |
| 272 |
typename ForeverStack<N>::Node & |
| 273 |
ForeverStack<N>::cursor () |
| 274 |
{ |
| 275 |
return cursor_reference; |
| 276 |
} |
| 277 |
|
| 278 |
template <Namespace N> |
| 279 |
const typename ForeverStack<N>::Node & |
| 280 |
ForeverStack<N>::cursor () const |
| 281 |
{ |
| 282 |
return cursor_reference; |
| 283 |
} |
| 284 |
|
| 285 |
template <Namespace N> |
| 286 |
void |
| 287 |
ForeverStack<N>::update_cursor (Node &new_cursor) |
| 288 |
{ |
| 289 |
cursor_reference = new_cursor; |
| 290 |
} |
| 291 |
|
| 292 |
template <Namespace N> |
| 293 |
tl::optional<Rib::Definition> |
| 294 |
ForeverStack<N>::get (const Identifier &name) |
| 295 |
{ |
| 296 |
tl::optional<Rib::Definition> resolved_definition = tl::nullopt; |
| 297 |
|
| 298 |
// TODO: Can we improve the API? have `reverse_iter` return an optional? |
| 299 |
reverse_iter ([&resolved_definition, &name] (Node ¤t) { |
| 300 |
auto candidate = current.rib.get (name.as_string ()); |
| 301 |
|
| 302 |
return candidate.map_or ( |
| 303 |
[&resolved_definition] (Rib::Definition found) { |
| 304 |
if (found.is_variant ()) |
| 305 |
return KeepGoing::Yes; |
| 306 |
// for most namespaces, we do not need to care about various ribs - |
| 307 |
// they are available from all contexts if defined in the current |
| 308 |
// scope, or an outermore one. so if we do have a candidate, we can |
| 309 |
// return it directly and stop iterating |
| 310 |
resolved_definition = found; |
| 311 |
|
| 312 |
return KeepGoing::No; |
| 313 |
}, |
| 314 |
// if there was no candidate, we keep iterating |
| 315 |
KeepGoing::Yes); |
| 316 |
}); |
| 317 |
|
| 318 |
return resolved_definition; |
| 319 |
} |
| 320 |
|
| 321 |
template <Namespace N> |
| 322 |
tl::optional<Rib::Definition> |
| 323 |
ForeverStack<N>::get_lang_prelude (const Identifier &name) |
| 324 |
{ |
| 325 |
return lang_prelude.rib.get (name.as_string ()); |
| 326 |
} |
| 327 |
|
| 328 |
template <Namespace N> |
| 329 |
tl::optional<Rib::Definition> |
| 330 |
ForeverStack<N>::get_lang_prelude (const std::string &name) |
| 331 |
{ |
| 332 |
return lang_prelude.rib.get (name); |
| 333 |
} |
| 334 |
|
| 335 |
template <> |
| 336 |
tl::optional<Rib::Definition> inline ForeverStack<Namespace::Labels>::get ( |
| 337 |
const Identifier &name) |
| 338 |
{ |
| 339 |
tl::optional<Rib::Definition> resolved_definition = tl::nullopt; |
| 340 |
|
| 341 |
reverse_iter ([&resolved_definition, &name] (Node ¤t) { |
| 342 |
// looking up for labels cannot go through function ribs |
| 343 |
// TODO: What other ribs? |
| 344 |
if (current.rib.kind == Rib::Kind::Function) |
| 345 |
return KeepGoing::No; |
| 346 |
|
| 347 |
auto candidate = current.rib.get (name.as_string ()); |
| 348 |
|
| 349 |
// FIXME: Factor this in a function with the generic `get` |
| 350 |
return candidate.map_or ( |
| 351 |
[&resolved_definition] (Rib::Definition found) { |
| 352 |
resolved_definition = found; |
| 353 |
|
| 354 |
return KeepGoing::No; |
| 355 |
}, |
| 356 |
KeepGoing::Yes); |
| 357 |
}); |
| 358 |
|
| 359 |
return resolved_definition; |
| 360 |
} |
| 361 |
|
| 362 |
/* Check if an iterator points to the last element */ |
| 363 |
template <typename I, typename C> |
| 364 |
static bool |
| 365 |
is_last (const I &iterator, const C &collection) |
| 366 |
{ |
| 367 |
return iterator + 1 == collection.end (); |
| 368 |
} |
| 369 |
|
| 370 |
/* Check if an iterator points to the start of the collection */ |
| 371 |
template <typename I, typename C> |
| 372 |
static bool |
| 373 |
is_start (const I &iterator, const C &collection) |
| 374 |
{ |
| 375 |
return iterator == collection.begin (); |
| 376 |
} |
| 377 |
|
| 378 |
template <Namespace N> |
| 379 |
typename ForeverStack<N>::Node & |
| 380 |
ForeverStack<N>::find_closest_module (Node &starting_point) |
| 381 |
{ |
| 382 |
auto *closest_module = &starting_point; |
| 383 |
|
| 384 |
reverse_iter (starting_point, [&closest_module] (Node ¤t) { |
| 385 |
if (current.rib.kind == Rib::Kind::Module || current.is_root ()) |
| 386 |
{ |
| 387 |
closest_module = ¤t; |
| 388 |
return KeepGoing::No; |
| 389 |
} |
| 390 |
|
| 391 |
return KeepGoing::Yes; |
| 392 |
}); |
| 393 |
|
| 394 |
return *closest_module; |
| 395 |
} |
| 396 |
|
| 397 |
/* If a the given condition is met, emit an error about misused leading path |
| 398 |
* segments */ |
| 399 |
template <typename S> |
| 400 |
static inline bool |
| 401 |
check_leading_kw_at_start (const S &segment, bool condition) |
| 402 |
{ |
| 403 |
if (condition) |
| 404 |
rust_error_at ( |
| 405 |
segment.get_locus (), ErrorCode::E0433, |
| 406 |
"leading path segment %qs can only be used at the beginning of a path", |
| 407 |
segment.as_string ().c_str ()); |
| 408 |
|
| 409 |
return condition; |
| 410 |
} |
| 411 |
|
| 412 |
// we first need to handle the "starting" segments - `super`, `self` or |
| 413 |
// `crate`. we don't need to do anything for `self` and can just skip it. for |
| 414 |
// `crate`, we need to go back to the root of the current stack. for each |
| 415 |
// `super` segment, we go back to the cursor's parent until we reach the |
| 416 |
// correct one or the root. |
| 417 |
template <Namespace N> |
| 418 |
template <typename S> |
| 419 |
tl::optional<typename std::vector<S>::const_iterator> |
| 420 |
ForeverStack<N>::find_starting_point ( |
| 421 |
const std::vector<S> &segments, std::reference_wrapper<Node> &starting_point, |
| 422 |
std::function<void (const S &, NodeId)> insert_segment_resolution) |
| 423 |
{ |
| 424 |
auto iterator = segments.begin (); |
| 425 |
|
| 426 |
for (; !is_last (iterator, segments); iterator++) |
| 427 |
{ |
| 428 |
auto &outer_seg = *iterator; |
| 429 |
|
| 430 |
if (unwrap_segment_get_lang_item (outer_seg).has_value ()) |
| 431 |
break; |
| 432 |
|
| 433 |
auto &seg = unwrap_type_segment (outer_seg); |
| 434 |
bool is_self_or_crate |
| 435 |
= seg.is_crate_path_seg () || seg.is_lower_self_seg (); |
| 436 |
|
| 437 |
// if we're after the first path segment and meet `self` or `crate`, it's |
| 438 |
// an error - we should only be seeing `super` keywords at this point |
| 439 |
if (check_leading_kw_at_start (seg, !is_start (iterator, segments) |
| 440 |
&& is_self_or_crate)) |
| 441 |
return tl::nullopt; |
| 442 |
|
| 443 |
if (seg.is_crate_path_seg ()) |
| 444 |
{ |
| 445 |
starting_point = root; |
| 446 |
insert_segment_resolution (outer_seg, starting_point.get ().id); |
| 447 |
iterator++; |
| 448 |
break; |
| 449 |
} |
| 450 |
if (seg.is_lower_self_seg ()) |
| 451 |
{ |
| 452 |
// insert segment resolution and exit |
| 453 |
starting_point = find_closest_module (starting_point); |
| 454 |
insert_segment_resolution (outer_seg, starting_point.get ().id); |
| 455 |
iterator++; |
| 456 |
break; |
| 457 |
} |
| 458 |
if (seg.is_super_path_seg ()) |
| 459 |
{ |
| 460 |
starting_point = find_closest_module (starting_point); |
| 461 |
if (starting_point.get ().is_root ()) |
| 462 |
{ |
| 463 |
rust_error_at (seg.get_locus (), ErrorCode::E0433, |
| 464 |
"too many leading %<super%> keywords"); |
| 465 |
return tl::nullopt; |
| 466 |
} |
| 467 |
|
| 468 |
starting_point |
| 469 |
= find_closest_module (starting_point.get ().parent.value ()); |
| 470 |
|
| 471 |
insert_segment_resolution (outer_seg, starting_point.get ().id); |
| 472 |
continue; |
| 473 |
} |
| 474 |
|
| 475 |
// now we've gone through the allowed `crate`, `self` or leading `super` |
| 476 |
// segments. we can start resolving each segment itself. |
| 477 |
// if we do see another leading segment, then we can error out. |
| 478 |
break; |
| 479 |
} |
| 480 |
|
| 481 |
return iterator; |
| 482 |
} |
| 483 |
|
| 484 |
template <Namespace N> |
| 485 |
template <typename S> |
| 486 |
tl::optional<typename ForeverStack<N>::Node &> |
| 487 |
ForeverStack<N>::resolve_segments ( |
| 488 |
Node &starting_point, const std::vector<S> &segments, |
| 489 |
typename std::vector<S>::const_iterator iterator, |
| 490 |
std::function<void (const S &, NodeId)> insert_segment_resolution) |
| 491 |
{ |
| 492 |
Node *current_node = &starting_point; |
| 493 |
for (; !is_last (iterator, segments); iterator++) |
| 494 |
{ |
| 495 |
auto &outer_seg = *iterator; |
| 496 |
|
| 497 |
if (auto lang_item = unwrap_segment_get_lang_item (outer_seg)) |
| 498 |
{ |
| 499 |
NodeId seg_id = Analysis::Mappings::get ().get_lang_item_node ( |
| 500 |
lang_item.value ()); |
| 501 |
current_node = &dfs_node (root, seg_id).value (); |
| 502 |
|
| 503 |
insert_segment_resolution (outer_seg, seg_id); |
| 504 |
continue; |
| 505 |
} |
| 506 |
|
| 507 |
auto &seg = unwrap_type_segment (outer_seg); |
| 508 |
std::string str = seg.as_string (); |
| 509 |
rust_debug ("[ARTHUR]: resolving segment part: %s", str.c_str ()); |
| 510 |
|
| 511 |
// check that we don't encounter *any* leading keywords afterwards |
| 512 |
if (check_leading_kw_at_start (seg, seg.is_crate_path_seg () |
| 513 |
|| seg.is_super_path_seg () |
| 514 |
|| seg.is_lower_self_seg ())) |
| 515 |
return tl::nullopt; |
| 516 |
|
| 517 |
tl::optional<typename ForeverStack<N>::Node &> child = tl::nullopt; |
| 518 |
|
| 519 |
/* |
| 520 |
* On every iteration this loop either |
| 521 |
* |
| 522 |
* 1. terminates |
| 523 |
* |
| 524 |
* 2. decreases the depth of the node pointed to by current_node until |
| 525 |
* current_node reaches the root |
| 526 |
* |
| 527 |
* 3. If the root node is reached, and we were not able to resolve the |
| 528 |
* segment, we search the prelude rib for the segment, by setting |
| 529 |
* current_node to point to the prelude, and toggling the |
| 530 |
* searched_prelude boolean to true. If current_node is the prelude |
| 531 |
* rib, and searched_prelude is true, we will exit. |
| 532 |
* |
| 533 |
* This ensures termination. |
| 534 |
* |
| 535 |
*/ |
| 536 |
bool searched_prelude = false; |
| 537 |
while (true) |
| 538 |
{ |
| 539 |
// may set the value of child |
| 540 |
for (auto &kv : current_node->children) |
| 541 |
{ |
| 542 |
auto &link = kv.first; |
| 543 |
|
| 544 |
if (link.path.map_or ( |
| 545 |
[&str] (Identifier path) { |
| 546 |
auto &path_str = path.as_string (); |
| 547 |
return str == path_str; |
| 548 |
}, |
| 549 |
false)) |
| 550 |
{ |
| 551 |
child = kv.second; |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
if (child.has_value ()) |
| 557 |
{ |
| 558 |
break; |
| 559 |
} |
| 560 |
|
| 561 |
if (N == Namespace::Types) |
| 562 |
{ |
| 563 |
auto rib_lookup = current_node->rib.get (seg.as_string ()); |
| 564 |
if (rib_lookup && !rib_lookup->is_ambiguous ()) |
| 565 |
{ |
| 566 |
insert_segment_resolution (outer_seg, |
| 567 |
rib_lookup->get_node_id ()); |
| 568 |
return tl::nullopt; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
if (current_node->is_root () && !searched_prelude) |
| 573 |
{ |
| 574 |
searched_prelude = true; |
| 575 |
current_node = &lang_prelude; |
| 576 |
continue; |
| 577 |
} |
| 578 |
|
| 579 |
if (!is_start (iterator, segments) |
| 580 |
|| current_node->rib.kind == Rib::Kind::Module |
| 581 |
|| current_node->is_prelude ()) |
| 582 |
{ |
| 583 |
return tl::nullopt; |
| 584 |
} |
| 585 |
|
| 586 |
current_node = ¤t_node->parent.value (); |
| 587 |
} |
| 588 |
|
| 589 |
// if child didn't contain a value |
| 590 |
// the while loop above should have return'd or kept looping |
| 591 |
current_node = &child.value (); |
| 592 |
insert_segment_resolution (outer_seg, current_node->id); |
| 593 |
} |
| 594 |
|
| 595 |
return *current_node; |
| 596 |
} |
| 597 |
|
| 598 |
template <> |
| 599 |
inline tl::optional<Rib::Definition> |
| 600 |
ForeverStack<Namespace::Types>::resolve_final_segment (Node &final_node, |
| 601 |
std::string &seg_name, |
| 602 |
bool is_lower_self) |
| 603 |
{ |
| 604 |
if (is_lower_self) |
| 605 |
return Rib::Definition::NonShadowable (final_node.id); |
| 606 |
else |
| 607 |
return final_node.rib.get (seg_name); |
| 608 |
} |
| 609 |
|
| 610 |
template <Namespace N> |
| 611 |
tl::optional<Rib::Definition> |
| 612 |
ForeverStack<N>::resolve_final_segment (Node &final_node, std::string &seg_name, |
| 613 |
bool is_lower_self) |
| 614 |
{ |
| 615 |
return final_node.rib.get (seg_name); |
| 616 |
} |
| 617 |
|
| 618 |
template <Namespace N> |
| 619 |
template <typename S> |
| 620 |
tl::optional<Rib::Definition> |
| 621 |
ForeverStack<N>::resolve_path ( |
| 622 |
const std::vector<S> &segments, bool has_opening_scope_resolution, |
| 623 |
std::function<void (const S &, NodeId)> insert_segment_resolution) |
| 624 |
{ |
| 625 |
// TODO: What to do if segments.empty() ? |
| 626 |
|
| 627 |
// handle paths with opening scopes |
| 628 |
std::function<void (void)> cleanup_current = [] () {}; |
| 629 |
if (has_opening_scope_resolution) |
| 630 |
{ |
| 631 |
Node *last_current = &cursor_reference.get (); |
| 632 |
if (get_rust_edition () == Edition::E2015) |
| 633 |
cursor_reference = root; |
| 634 |
else |
| 635 |
cursor_reference = extern_prelude; |
| 636 |
cleanup_current |
| 637 |
= [this, last_current] () { cursor_reference = *last_current; }; |
| 638 |
} |
| 639 |
|
| 640 |
// if there's only one segment, we just use `get` |
| 641 |
if (segments.size () == 1) |
| 642 |
{ |
| 643 |
auto &seg = segments.front (); |
| 644 |
if (auto lang_item = unwrap_segment_get_lang_item (seg)) |
| 645 |
{ |
| 646 |
NodeId seg_id = Analysis::Mappings::get ().get_lang_item_node ( |
| 647 |
lang_item.value ()); |
| 648 |
|
| 649 |
insert_segment_resolution (seg, seg_id); |
| 650 |
cleanup_current (); |
| 651 |
// TODO: does NonShadowable matter? |
| 652 |
return Rib::Definition::NonShadowable (seg_id); |
| 653 |
} |
| 654 |
|
| 655 |
tl::optional<Rib::Definition> res |
| 656 |
= get (unwrap_type_segment (segments.back ()).as_string ()); |
| 657 |
|
| 658 |
if (!res) |
| 659 |
res = get_lang_prelude ( |
| 660 |
unwrap_type_segment (segments.back ()).as_string ()); |
| 661 |
|
| 662 |
if (res && !res->is_ambiguous ()) |
| 663 |
insert_segment_resolution (segments.back (), res->get_node_id ()); |
| 664 |
cleanup_current (); |
| 665 |
return res; |
| 666 |
} |
| 667 |
|
| 668 |
std::reference_wrapper<Node> starting_point = cursor (); |
| 669 |
|
| 670 |
auto res |
| 671 |
= find_starting_point (segments, starting_point, insert_segment_resolution) |
| 672 |
.and_then ( |
| 673 |
[this, &segments, &starting_point, &insert_segment_resolution] ( |
| 674 |
typename std::vector<S>::const_iterator iterator) { |
| 675 |
return resolve_segments (starting_point.get (), segments, iterator, |
| 676 |
insert_segment_resolution); |
| 677 |
}) |
| 678 |
.and_then ([this, &segments, &insert_segment_resolution] ( |
| 679 |
Node final_node) -> tl::optional<Rib::Definition> { |
| 680 |
// leave resolution within impl blocks to type checker |
| 681 |
if (final_node.rib.kind == Rib::Kind::TraitOrImpl) |
| 682 |
return tl::nullopt; |
| 683 |
|
| 684 |
auto &seg = unwrap_type_segment (segments.back ()); |
| 685 |
std::string seg_name = seg.as_string (); |
| 686 |
|
| 687 |
// assuming this can't be a lang item segment |
| 688 |
tl::optional<Rib::Definition> res |
| 689 |
= resolve_final_segment (final_node, seg_name, |
| 690 |
seg.is_lower_self_seg ()); |
| 691 |
// Ok we didn't find it in the rib, Lets try the prelude... |
| 692 |
if (!res) |
| 693 |
res = get_lang_prelude (seg_name); |
| 694 |
|
| 695 |
if (res && !res->is_ambiguous ()) |
| 696 |
insert_segment_resolution (segments.back (), res->get_node_id ()); |
| 697 |
|
| 698 |
return res; |
| 699 |
}); |
| 700 |
cleanup_current (); |
| 701 |
return res; |
| 702 |
} |
| 703 |
|
| 704 |
template <Namespace N> |
| 705 |
tl::optional<typename ForeverStack<N>::DfsResult> |
| 706 |
ForeverStack<N>::dfs (ForeverStack<N>::Node &starting_point, NodeId to_find) |
| 707 |
{ |
| 708 |
auto values = starting_point.rib.get_values (); |
| 709 |
|
| 710 |
for (auto &kv : values) |
| 711 |
{ |
| 712 |
for (auto id : kv.second.ids_shadowable) |
| 713 |
if (id == to_find) |
| 714 |
return {{starting_point, kv.first}}; |
| 715 |
for (auto id : kv.second.ids_non_shadowable) |
| 716 |
if (id == to_find) |
| 717 |
return {{starting_point, kv.first}}; |
| 718 |
for (auto id : kv.second.ids_globbed) |
| 719 |
if (id == to_find) |
| 720 |
return {{starting_point, kv.first}}; |
| 721 |
} |
| 722 |
|
| 723 |
for (auto &child : starting_point.children) |
| 724 |
{ |
| 725 |
auto candidate = dfs (child.second, to_find); |
| 726 |
|
| 727 |
if (candidate.has_value ()) |
| 728 |
return candidate; |
| 729 |
} |
| 730 |
|
| 731 |
return tl::nullopt; |
| 732 |
} |
| 733 |
|
| 734 |
template <Namespace N> |
| 735 |
tl::optional<typename ForeverStack<N>::ConstDfsResult> |
| 736 |
ForeverStack<N>::dfs (const ForeverStack<N>::Node &starting_point, |
| 737 |
NodeId to_find) const |
| 738 |
{ |
| 739 |
auto values = starting_point.rib.get_values (); |
| 740 |
|
| 741 |
for (auto &kv : values) |
| 742 |
{ |
| 743 |
for (auto id : kv.second.ids_shadowable) |
| 744 |
if (id == to_find) |
| 745 |
return {{starting_point, kv.first}}; |
| 746 |
for (auto id : kv.second.ids_non_shadowable) |
| 747 |
if (id == to_find) |
| 748 |
return {{starting_point, kv.first}}; |
| 749 |
for (auto id : kv.second.ids_globbed) |
| 750 |
if (id == to_find) |
| 751 |
return {{starting_point, kv.first}}; |
| 752 |
} |
| 753 |
|
| 754 |
for (auto &child : starting_point.children) |
| 755 |
{ |
| 756 |
auto candidate = dfs (child.second, to_find); |
| 757 |
|
| 758 |
if (candidate.has_value ()) |
| 759 |
return candidate; |
| 760 |
} |
| 761 |
|
| 762 |
return tl::nullopt; |
| 763 |
} |
| 764 |
|
| 765 |
template <Namespace N> |
| 766 |
tl::optional<Resolver::CanonicalPath> |
| 767 |
ForeverStack<N>::to_canonical_path (NodeId id) const |
| 768 |
{ |
| 769 |
// find the id in the current forever stack, starting from the root, |
| 770 |
// performing either a BFS or DFS once the Node containing the ID is found, go |
| 771 |
// back up to the root (parent().parent().parent()...) accumulate link |
| 772 |
// segments reverse them that's your canonical path |
| 773 |
|
| 774 |
return dfs (root, id).map ([this, id] (ConstDfsResult tuple) { |
| 775 |
auto containing_node = tuple.first; |
| 776 |
auto name = tuple.second; |
| 777 |
|
| 778 |
auto segments = std::vector<Resolver::CanonicalPath> (); |
| 779 |
|
| 780 |
reverse_iter (containing_node, [&segments] (const Node ¤t) { |
| 781 |
if (current.is_root ()) |
| 782 |
return KeepGoing::No; |
| 783 |
|
| 784 |
auto children = current.parent.value ().children; |
| 785 |
const Link *outer_link = nullptr; |
| 786 |
|
| 787 |
for (auto &kv : children) |
| 788 |
{ |
| 789 |
auto &link = kv.first; |
| 790 |
auto &child = kv.second; |
| 791 |
|
| 792 |
if (current.id == child.id) |
| 793 |
{ |
| 794 |
outer_link = &link; |
| 795 |
break; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
rust_assert (outer_link); |
| 800 |
|
| 801 |
outer_link->path.map ([&segments, outer_link] (Identifier path) { |
| 802 |
segments.emplace (segments.begin (), |
| 803 |
Resolver::CanonicalPath::new_seg (outer_link->id, |
| 804 |
path.as_string ())); |
| 805 |
}); |
| 806 |
|
| 807 |
return KeepGoing::Yes; |
| 808 |
}); |
| 809 |
|
| 810 |
auto &mappings = Analysis::Mappings::get (); |
| 811 |
CrateNum crate_num = mappings.lookup_crate_num (root.id).value (); |
| 812 |
auto path = Resolver::CanonicalPath::new_seg ( |
| 813 |
root.id, mappings.get_crate_name (crate_num).value ()); |
| 814 |
path.set_crate_num (crate_num); |
| 815 |
|
| 816 |
for (const auto &segment : segments) |
| 817 |
path = path.append (segment); |
| 818 |
|
| 819 |
// Finally, append the name |
| 820 |
path = path.append (Resolver::CanonicalPath::new_seg (id, name)); |
| 821 |
|
| 822 |
return path; |
| 823 |
}); |
| 824 |
} |
| 825 |
|
| 826 |
template <Namespace N> |
| 827 |
tl::optional<Rib &> |
| 828 |
ForeverStack<N>::dfs_rib (ForeverStack<N>::Node &starting_point, NodeId to_find) |
| 829 |
{ |
| 830 |
return dfs_node (starting_point, to_find).map ([] (Node &x) -> Rib & { |
| 831 |
return x.rib; |
| 832 |
}); |
| 833 |
} |
| 834 |
|
| 835 |
template <Namespace N> |
| 836 |
tl::optional<const Rib &> |
| 837 |
ForeverStack<N>::dfs_rib (const ForeverStack<N>::Node &starting_point, |
| 838 |
NodeId to_find) const |
| 839 |
{ |
| 840 |
return dfs_node (starting_point, to_find) |
| 841 |
.map ([] (const Node &x) -> const Rib & { return x.rib; }); |
| 842 |
} |
| 843 |
|
| 844 |
template <Namespace N> |
| 845 |
tl::optional<typename ForeverStack<N>::Node &> |
| 846 |
ForeverStack<N>::dfs_node (ForeverStack<N>::Node &starting_point, |
| 847 |
NodeId to_find) |
| 848 |
{ |
| 849 |
if (starting_point.id == to_find) |
| 850 |
return starting_point; |
| 851 |
|
| 852 |
for (auto &child : starting_point.children) |
| 853 |
{ |
| 854 |
auto candidate = dfs_node (child.second, to_find); |
| 855 |
|
| 856 |
if (candidate.has_value ()) |
| 857 |
return candidate; |
| 858 |
} |
| 859 |
|
| 860 |
return tl::nullopt; |
| 861 |
} |
| 862 |
|
| 863 |
template <Namespace N> |
| 864 |
tl::optional<const typename ForeverStack<N>::Node &> |
| 865 |
ForeverStack<N>::dfs_node (const ForeverStack<N>::Node &starting_point, |
| 866 |
NodeId to_find) const |
| 867 |
{ |
| 868 |
if (starting_point.id == to_find) |
| 869 |
return starting_point; |
| 870 |
|
| 871 |
for (auto &child : starting_point.children) |
| 872 |
{ |
| 873 |
auto candidate = dfs_node (child.second, to_find); |
| 874 |
|
| 875 |
if (candidate.has_value ()) |
| 876 |
return candidate; |
| 877 |
} |
| 878 |
|
| 879 |
return tl::nullopt; |
| 880 |
} |
| 881 |
|
| 882 |
template <Namespace N> |
| 883 |
tl::optional<Rib &> |
| 884 |
ForeverStack<N>::to_rib (NodeId rib_id) |
| 885 |
{ |
| 886 |
return dfs_rib (root, rib_id); |
| 887 |
} |
| 888 |
|
| 889 |
template <Namespace N> |
| 890 |
tl::optional<const Rib &> |
| 891 |
ForeverStack<N>::to_rib (NodeId rib_id) const |
| 892 |
{ |
| 893 |
return dfs_rib (root, rib_id); |
| 894 |
} |
| 895 |
|
| 896 |
template <Namespace N> |
| 897 |
void |
| 898 |
ForeverStack<N>::stream_rib (std::stringstream &stream, const Rib &rib, |
| 899 |
const std::string &next, |
| 900 |
const std::string &next_next) const |
| 901 |
{ |
| 902 |
std::string rib_kind = Rib::kind_to_string (rib.kind); |
| 903 |
stream << next << "rib [" << rib_kind << "]: {"; |
| 904 |
if (rib.get_values ().empty ()) |
| 905 |
{ |
| 906 |
stream << "}\n"; |
| 907 |
return; |
| 908 |
} |
| 909 |
else |
| 910 |
{ |
| 911 |
stream << "\n"; |
| 912 |
} |
| 913 |
|
| 914 |
for (const auto &kv : rib.get_values ()) |
| 915 |
stream << next_next << kv.first << ": " << kv.second.to_string () << "\n"; |
| 916 |
|
| 917 |
stream << next << "},\n"; |
| 918 |
} |
| 919 |
|
| 920 |
template <Namespace N> |
| 921 |
void |
| 922 |
ForeverStack<N>::stream_node (std::stringstream &stream, unsigned indentation, |
| 923 |
const ForeverStack<N>::Node &node) const |
| 924 |
{ |
| 925 |
auto indent = std::string (indentation, ' '); |
| 926 |
auto next = std::string (indentation + 4, ' '); |
| 927 |
auto next_next = std::string (indentation + 8, ' '); |
| 928 |
|
| 929 |
stream << indent << "Node {\n" |
| 930 |
<< next << "is_root: " << (node.is_root () ? "true" : "false") << ",\n" |
| 931 |
<< next << "is_leaf: " << (node.is_leaf () ? "true" : "false") |
| 932 |
<< ",\n"; |
| 933 |
|
| 934 |
stream_rib (stream, node.rib, next, next_next); |
| 935 |
|
| 936 |
stream << indent << "}\n"; |
| 937 |
|
| 938 |
for (auto &kv : node.children) |
| 939 |
{ |
| 940 |
auto link = kv.first; |
| 941 |
auto child = kv.second; |
| 942 |
stream << indent << "Link (" << link.id << ", " |
| 943 |
<< (link.path.has_value () ? link.path.value ().as_string () |
| 944 |
: "<anon>") |
| 945 |
<< "):\n"; |
| 946 |
|
| 947 |
stream_node (stream, indentation + 4, child); |
| 948 |
|
| 949 |
stream << '\n'; |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
template <Namespace N> |
| 954 |
std::string |
| 955 |
ForeverStack<N>::as_debug_string () const |
| 956 |
{ |
| 957 |
std::stringstream stream; |
| 958 |
|
| 959 |
stream_node (stream, 0, root); |
| 960 |
|
| 961 |
return stream.str (); |
| 962 |
} |
| 963 |
|
| 964 |
template <Namespace N> |
| 965 |
bool |
| 966 |
ForeverStack<N>::is_module_descendant (NodeId parent, NodeId child) const |
| 967 |
{ |
| 968 |
return dfs_node (dfs_node (root, parent).value (), child).has_value (); |
| 969 |
} |
| 970 |
|
| 971 |
// FIXME: Can we add selftests? |
| 972 |
|
| 973 |
} // namespace Resolver2_0 |
| 974 |
} // namespace Rust |