aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-forever-stack.cc
blob: 725ae0ea01882d2ee90beed13b28e6754008cb81 (plain)
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
// Copyright (C) 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 "optional.h"

namespace Rust {
namespace Resolver2_0 {

bool
ForeverStackStore::Node::is_root () const
{
  return !parent.has_value ();
}

bool
ForeverStackStore::Node::is_leaf () const
{
  return children.empty ();
}

NodeId
ForeverStackStore::Node::get_id () const
{
  return id;
}

ForeverStackStore::Node &
ForeverStackStore::Node::insert_child (NodeId id, tl::optional<Identifier> path,
				       Rib::Kind kind)
{
  auto res = children.insert ({Link (id, path), Node (kind, id, *this)});

  rust_debug ("inserting link: Link(%d [%s]): existed? %s", id,
	      path.has_value () ? path.value ().as_string ().c_str ()
				: "<anon>",
	      !res.second ? "yes" : "no");

  // sanity check on rib kind
  // pick the value rib, since all ribs should have the same kind anyways
  rust_assert (res.second || res.first->second.value_rib.kind == kind);

  // verify, if we're using an existing node, our paths don't contradict
  if (!res.second && path.has_value ())
    {
      auto other_path = res.first->first.path;
      rust_assert (!other_path.has_value ()
		   || other_path.value ().as_string ()
			== path.value ().as_string ());
    }

  return res.first->second;
}

tl::optional<ForeverStackStore::Node &>
ForeverStackStore::Node::get_child (const Identifier &path)
{
  for (auto &ent : children)
    {
      if (ent.first.path.has_value ()
	  && ent.first.path->as_string () == path.as_string ())
	return ent.second;
    }
  return tl::nullopt;
}

tl::optional<const ForeverStackStore::Node &>
ForeverStackStore::Node::get_child (const Identifier &path) const
{
  for (auto &ent : children)
    {
      if (ent.first.path.has_value ()
	  && ent.first.path->as_string () == path.as_string ())
	return ent.second;
    }
  return tl::nullopt;
}

tl::optional<ForeverStackStore::Node &>
ForeverStackStore::Node::get_parent ()
{
  return parent;
}

tl::optional<const ForeverStackStore::Node &>
ForeverStackStore::Node::get_parent () const
{
  if (parent)
    return *parent;
  return tl::nullopt;
}

tl::optional<const Identifier &>
ForeverStackStore::Node::get_parent_path () const
{
  if (parent.has_value ())
    for (auto &ent : parent->children)
      if (ent.first.id == id && ent.first.path.has_value ())
	return ent.first.path.value ();
  return tl::nullopt;
}

Rib &
ForeverStackStore::Node::get_rib (Namespace ns)
{
  switch (ns)
    {
    case Namespace::Values:
      return value_rib;
    case Namespace::Types:
      return type_rib;
    case Namespace::Labels:
      return label_rib;
    case Namespace::Macros:
      return macro_rib;
    default:
      rust_unreachable ();
    }
}

const Rib &
ForeverStackStore::Node::get_rib (Namespace ns) const
{
  switch (ns)
    {
    case Namespace::Values:
      return value_rib;
    case Namespace::Types:
      return type_rib;
    case Namespace::Labels:
      return label_rib;
    case Namespace::Macros:
      return macro_rib;
    default:
      rust_unreachable ();
    }
}

tl::expected<NodeId, DuplicateNameError>
ForeverStackStore::Node::insert (const Identifier &name, NodeId node,
				 Namespace ns)
{
  // 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 get_rib (ns).insert (name.as_string (),
			      Rib::Definition::NonShadowable (node));
}

tl::expected<NodeId, DuplicateNameError>
ForeverStackStore::Node::insert_shadowable (const Identifier &name, NodeId node,
					    Namespace ns)
{
  return get_rib (ns).insert (name.as_string (),
			      Rib::Definition::Shadowable (node));
}

tl::expected<NodeId, DuplicateNameError>
ForeverStackStore::Node::insert_globbed (const Identifier &name, NodeId node,
					 Namespace ns)
{
  return get_rib (ns).insert (name.as_string (),
			      Rib::Definition::Globbed (node));
}

void
ForeverStackStore::Node::reverse_iter (std::function<KeepGoing (Node &)> lambda)
{
  for (Node *tmp = this; lambda (*tmp) == KeepGoing::Yes && !tmp->is_root ();
       tmp = &tmp->parent.value ())
    ;
}

void
ForeverStackStore::Node::reverse_iter (
  std::function<KeepGoing (const Node &)> lambda) const
{
  for (const Node *tmp = this;
       lambda (*tmp) == KeepGoing::Yes && !tmp->is_root ();
       tmp = &tmp->parent.value ())
    ;
}

void
ForeverStackStore::Node::child_iter (
  std::function<KeepGoing (NodeId, tl::optional<const Identifier &>, Node &)>
    lambda)
{
  for (auto &ent : children)
    {
      tl::optional<const Identifier &> path;
      if (ent.first.path.has_value ())
	path = ent.first.path.value ();
      auto keep_going = lambda (ent.first.id, path, ent.second);
      if (keep_going == KeepGoing::No)
	return;
    }
}

void
ForeverStackStore::Node::child_iter (
  std::function<KeepGoing (NodeId, tl::optional<const Identifier &>,
			   const Node &)>
    lambda) const
{
  for (auto &ent : children)
    {
      tl::optional<const Identifier &> path;
      if (ent.first.path.has_value ())
	path = ent.first.path.value ();
      auto keep_going = lambda (ent.first.id, path, ent.second);
      if (keep_going == KeepGoing::No)
	return;
    }
}

ForeverStackStore::Node &
ForeverStackStore::Node::find_closest_module ()
{
  // get kind of value_rib
  // but all ribs should share the same kind anyways
  if (value_rib.kind == Rib::Kind::Module || !parent.has_value ())
    return *this;
  else
    return parent->find_closest_module ();
}

const ForeverStackStore::Node &
ForeverStackStore::Node::find_closest_module () const
{
  // get kind of value_rib
  // but all ribs should share the same kind anyways
  if (value_rib.kind != Rib::Kind::Module || !parent.has_value ())
    return *this;
  else
    return parent->find_closest_module ();
}

tl::optional<ForeverStackStore::Node &>
ForeverStackStore::Node::dfs_node (NodeId to_find)
{
  if (id == to_find)
    return *this;

  for (auto &child : children)
    {
      auto candidate = child.second.dfs_node (to_find);

      if (candidate.has_value ())
	return candidate;
    }

  return tl::nullopt;
}

tl::optional<const ForeverStackStore::Node &>
ForeverStackStore::Node::dfs_node (NodeId to_find) const
{
  if (id == to_find)
    return *this;

  for (auto &child : children)
    {
      auto candidate = child.second.dfs_node (to_find);

      if (candidate.has_value ())
	return candidate;
    }

  return tl::nullopt;
}

ForeverStackStore::Node &
ForeverStackStore::get_root ()
{
  return root;
}

const ForeverStackStore::Node &
ForeverStackStore::get_root () const
{
  return root;
}

tl::optional<ForeverStackStore::Node &>
ForeverStackStore::get_node (NodeId node_id)
{
  return root.dfs_node (node_id);
}

tl::optional<const ForeverStackStore::Node &>
ForeverStackStore::get_node (NodeId node_id) const
{
  return root.dfs_node (node_id);
}

} // namespace Resolver2_0
} // namespace Rust