aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-name-resolver.h
blob: 9c01d47debade8193957bb287c1cc89b39c9b051 (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
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
// Copyright (C) 2020 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/>.

#ifndef RUST_NAME_RESOLVER_H
#define RUST_NAME_RESOLVER_H

#include "rust-system.h"
#include "rust-hir-map.h"
#include "rust-hir-type-check.h"

namespace Rust {
namespace Resolver {

class Rib
{
public:
  // Rusts uses local_def_ids assigned by def_collector on the AST
  // lets use NodeId instead
  Rib (CrateNum crateNum, NodeId node_id)
    : crate_num (crateNum), node_id (node_id)
  {}

  ~Rib () {}

  void insert_name (std::string ident, NodeId id, Location locus, bool shadow,
		    std::function<void (std::string, NodeId, Location)> dup_cb)
  {
    auto it = mappings.find (ident);
    bool already_exists = it != mappings.end ();
    if (already_exists && !shadow)
      {
	for (auto &decl : decls_within_rib)
	  {
	    if (decl.first == it->second)
	      {
		dup_cb (ident, it->second, decl.second);
		return;
	      }
	  }
	dup_cb (ident, it->second, locus);
	return;
      }

    mappings[ident] = id;
    decls_within_rib.insert (std::pair<NodeId, Location> (id, locus));
    references[id] = {};
  }

  bool lookup_name (std::string ident, NodeId *id)
  {
    auto it = mappings.find (ident);
    if (it == mappings.end ())
      return false;

    *id = it->second;
    return true;
  }

  void clear_name (std::string ident, NodeId id)
  {
    mappings.erase (ident);
    for (auto &it : decls_within_rib)
      {
	if (it.first == id)
	  {
	    decls_within_rib.erase (it);
	    break;
	  }
      }
  }

  CrateNum get_crate_num () const { return crate_num; }
  NodeId get_node_id () const { return node_id; }

  void iterate_decls (std::function<bool (NodeId, Location)> cb)
  {
    for (auto it : decls_within_rib)
      {
	if (!cb (it.first, it.second))
	  return;
      }
  }

  void iterate_references_for_def (NodeId def, std::function<bool (NodeId)> cb)
  {
    auto it = references.find (def);
    if (it == references.end ())
      return;

    for (auto ref : it->second)
      {
	if (!cb (ref))
	  return;
      }
  }

  void append_reference_for_def (NodeId def, NodeId ref)
  {
    references[def].insert (ref);
  }

  bool have_references_for_node (NodeId def) const
  {
    auto it = references.find (def);
    if (it == references.end ())
      return false;

    return !it->second.empty ();
  }

  bool decl_was_declared_here (NodeId def) const
  {
    for (auto &it : decls_within_rib)
      {
	if (it.first == def)
	  return true;
      }
    return false;
  }

private:
  CrateNum crate_num;
  NodeId node_id;
  std::map<std::string, NodeId> mappings;
  std::set<std::pair<NodeId, Location> > decls_within_rib;
  std::map<NodeId, std::set<NodeId> > references;
};

class Scope
{
public:
  Scope (CrateNum crate_num) : crate_num (crate_num) {}
  ~Scope () {}

  void insert (std::string ident, NodeId id, Location locus, bool shadow,
	       std::function<void (std::string, NodeId, Location)> dup_cb)
  {
    peek ()->insert_name (ident, id, locus, shadow, dup_cb);
  }

  void insert (std::string ident, NodeId id, Location locus)
  {
    peek ()->insert_name (ident, id, locus, true,
			  [] (std::string, NodeId, Location) -> void {});
  }

  bool lookup (std::string ident, NodeId *id)
  {
    NodeId lookup = UNKNOWN_NODEID;
    iterate ([&] (Rib *r) mutable -> bool {
      if (r->lookup_name (ident, &lookup))
	return false;
      return true;
    });

    *id = lookup;
    return lookup != UNKNOWN_NODEID;
  }

  void iterate (std::function<bool (Rib *)> cb)
  {
    for (auto it = stack.rbegin (); it != stack.rend (); ++it)
      {
	if (!cb (*it))
	  return;
      }
  }

  Rib *peek () { return stack.back (); }

  void push (NodeId id) { stack.push_back (new Rib (get_crate_num (), id)); }

  Rib *pop ()
  {
    Rib *r = peek ();
    stack.pop_back ();
    return r;
  }

  CrateNum get_crate_num () const { return crate_num; }

  void append_reference_for_def (NodeId refId, NodeId defId)
  {
    bool ok = false;
    iterate ([&] (Rib *r) mutable -> bool {
      if (r->decl_was_declared_here (defId))
	{
	  ok = true;
	  r->append_reference_for_def (defId, refId);
	  return false;
	}
      return true;
    });
    rust_assert (ok);
  }

private:
  CrateNum crate_num;
  std::vector<Rib *> stack;
};

// This can map simple NodeIds for names to their parent node
// for example:
//
// var x = y + 1;
//
// say y has node id=1 and the plus_expression has id=2
// then the Definition will have
// Definition { node=1, parent=2 }
// this will be used later to gather the ribs for the type inferences context
//
// if parent is UNKNOWN_NODEID then this is a root declaration
// say the var_decl hasa node_id=4;
// the parent could be a BLOCK_Expr node_id but lets make it UNKNOWN_NODE_ID
// so we know when it terminates
struct Definition
{
  NodeId node;
  NodeId parent;
  // add kind ?
};

class Resolver
{
public:
  static Resolver *get ();
  ~Resolver () {}

  // these builtin types
  void insert_builtin_types (Rib *r);

  // these will be required for type resolution passes to
  // map back to tyty nodes
  std::vector<AST::Type *> &get_builtin_types ();

  void push_new_name_rib (Rib *r);
  void push_new_type_rib (Rib *r);
  void push_new_label_rib (Rib *r);

  bool find_name_rib (NodeId id, Rib **rib);
  bool find_type_rib (NodeId id, Rib **rib);
  bool find_label_rib (NodeId id, Rib **rib);

  void insert_new_definition (NodeId id, Definition def);
  bool lookup_definition (NodeId id, Definition *def);

  void insert_resolved_name (NodeId refId, NodeId defId);
  bool lookup_resolved_name (NodeId refId, NodeId *defId);

  void insert_resolved_type (NodeId refId, NodeId defId);
  bool lookup_resolved_type (NodeId refId, NodeId *defId);

  void insert_resolved_label (NodeId refId, NodeId defId);
  bool lookup_resolved_label (NodeId refId, NodeId *defId);

  // proxy for scoping
  Scope &get_name_scope () { return name_scope; }
  Scope &get_type_scope () { return type_scope; }
  Scope &get_label_scope () { return label_scope; }

  NodeId get_global_type_node_id () { return global_type_node_id; }

  void set_unit_type_node_id (NodeId id) { unit_ty_node_id = id; }
  NodeId get_unit_type_node_id () { return unit_ty_node_id; }

  void mark_decl_mutability (NodeId id, bool mut)
  {
    rust_assert (decl_mutability.find (id) == decl_mutability.end ());
    decl_mutability[id] = mut;
  }

  bool decl_is_mutable (NodeId id) const
  {
    auto it = decl_mutability.find (id);
    rust_assert (it != decl_mutability.end ());
    return it->second;
  }

  void mark_assignment_to_decl (NodeId id, NodeId assignment)
  {
    auto it = assignment_to_decl.find (id);
    if (it == assignment_to_decl.end ())
      assignment_to_decl[id] = {};

    assignment_to_decl[id].insert (assignment);
  }

  size_t get_num_assignments_to_decl (NodeId id) const
  {
    auto it = assignment_to_decl.find (id);
    if (it == assignment_to_decl.end ())
      return 0;

    return it->second.size ();
  }

  void iterate_name_ribs (std::function<void (Rib *)> cb)
  {
    for (auto it = name_ribs.begin (); it != name_ribs.end (); it++)
      cb (it->second);
  }

  void iterate_type_ribs (std::function<void (Rib *)> cb)
  {
    for (auto it = type_ribs.begin (); it != type_ribs.end (); it++)
      {
	if (it->first == global_type_node_id)
	  continue;

	cb (it->second);
      }
  }

  void iterate_label_ribs (std::function<void (Rib *)> cb)
  {
    for (auto it = label_ribs.begin (); it != label_ribs.end (); it++)
      cb (it->second);
  }

private:
  Resolver ();

  void generate_builtins ();

  Analysis::Mappings *mappings;
  TypeCheckContext *tyctx;

  std::vector<AST::Type *> builtins;

  Scope name_scope;
  Scope type_scope;
  Scope label_scope;

  NodeId global_type_node_id;
  NodeId unit_ty_node_id;

  // map a AST Node to a Rib
  std::map<NodeId, Rib *> name_ribs;
  std::map<NodeId, Rib *> type_ribs;
  std::map<NodeId, Rib *> label_ribs;

  // map any Node to its Definition
  // ie any name or type usage
  std::map<NodeId, Definition> name_definitions;

  // Rust uses DefIds to namespace these under a crate_num
  // but then it uses the def_collector to assign local_defids
  // to each ast node as well. not sure if this is going to fit
  // with gcc very well to compile a full crate in one go but we will
  // see.

  // these are of the form ref->Def-NodeId
  // we need two namespaces one for names and ones for types
  std::map<NodeId, NodeId> resolved_names;
  std::map<NodeId, NodeId> resolved_types;
  std::map<NodeId, NodeId> resolved_labels;

  // map of resolved names mutability flag
  std::map<NodeId, bool> decl_mutability;
  // map of resolved names and set of assignments to the decl
  std::map<NodeId, std::set<NodeId> > assignment_to_decl;
};

} // namespace Resolver
} // namespace Rust

#endif // RUST_NAME_RESOLVER_H