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