aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-name-resolver.h
blob: 23d3c1c26c00f93ad9959940c67d8f7f7282a6ef (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
// 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/>.

#ifndef RUST_NAME_RESOLVER_H
#define RUST_NAME_RESOLVER_H

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

namespace Rust {
namespace Resolver {

class Rib
{
public:
  enum ItemType
  {
    Var,
    Param,
    Function,
    Type,
    Module,
    Static,
    Const,
    Trait,
    Impl,
    TraitImpl,
    ExternCrate,
    MacroDecl,
    Label,
    Unknown
  };

  // FIXME
  // Rust uses local_def_ids assigned by def_collector on the AST. Consider
  // moving to a local-def-id
  Rib (CrateNum crateNum, NodeId node_id);

  // this takes the relative paths of items within a compilation unit for lookup
  void insert_name (
    const CanonicalPath &path, NodeId id, location_t locus, bool shadow,
    ItemType type,
    std::function<void (const CanonicalPath &, NodeId, location_t)> dup_cb);

  bool lookup_canonical_path (const NodeId &id, CanonicalPath *ident);
  bool lookup_name (const CanonicalPath &ident, NodeId *id);
  void clear_name (const CanonicalPath &ident, NodeId id);
  void append_reference_for_def (NodeId def, NodeId ref);
  bool have_references_for_node (NodeId def) const;
  bool decl_was_declared_here (NodeId def) const;
  bool lookup_decl_type (NodeId def, ItemType *type) const;
  void debug () const;
  std::string debug_str () const;

  CrateNum get_crate_num () const { return crate_num; }
  NodeId get_node_id () const { return node_id; }
  std::map<NodeId, location_t> &get_declarations () { return decls_within_rib; }

private:
  CrateNum crate_num;
  NodeId node_id;
  std::map<CanonicalPath, NodeId> path_mappings;
  std::map<NodeId, CanonicalPath> reverse_path_mappings;
  std::map<NodeId, location_t> decls_within_rib;
  std::map<NodeId, std::set<NodeId>> references;
  std::map<NodeId, ItemType> decl_type_mappings;
};

class Scope
{
public:
  Scope (CrateNum crate_num);

  void insert (
    const CanonicalPath &ident, NodeId id, location_t locus, bool shadow,
    Rib::ItemType type,
    std::function<void (const CanonicalPath &, NodeId, location_t)> dup_cb);

  void insert (const CanonicalPath &ident, NodeId id, location_t locus,
	       Rib::ItemType type = Rib::ItemType::Unknown);
  bool lookup (const CanonicalPath &ident, NodeId *id);
  bool lookup_decl_type (NodeId id, Rib::ItemType *type);
  bool lookup_rib_for_decl (NodeId id, const Rib **rib);

  void iterate (std::function<bool (Rib *)> cb);
  void iterate (std::function<bool (const Rib *)> cb) const;

  Rib *peek ();
  void push (NodeId id);
  Rib *pop ();

  bool decl_was_declared_here (NodeId def) const;
  void append_reference_for_def (NodeId refId, NodeId defId);

  CrateNum get_crate_num () const { return crate_num; }

  const std::vector<Rib *> &get_context () const { return stack; };

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

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);
  void push_new_macro_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);
  bool find_macro_rib (NodeId id, Rib **rib);

  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);

  void insert_resolved_macro (NodeId refId, NodeId defId);
  bool lookup_resolved_macro (NodeId refId, NodeId *defId);

  void insert_resolved_misc (NodeId refId, NodeId defId);
  bool lookup_resolved_misc (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; }
  Scope &get_macro_scope () { return macro_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 push_new_module_scope (NodeId module_id)
  {
    current_module_stack.push_back (module_id);
  }

  void pop_module_scope ()
  {
    rust_assert (!current_module_stack.empty ());
    current_module_stack.pop_back ();
  }

  NodeId peek_current_module_scope () const
  {
    rust_assert (!current_module_stack.empty ());
    return current_module_stack.back ();
  }

  NodeId peek_crate_module_scope () const
  {
    rust_assert (!current_module_stack.empty ());
    return current_module_stack.front ();
  }

  NodeId peek_parent_module_scope () const
  {
    rust_assert (current_module_stack.size () > 1);
    return current_module_stack.at (current_module_stack.size () - 2);
  }

  void push_closure_context (NodeId closure_expr_id);
  void pop_closure_context ();
  void insert_captured_item (NodeId id);
  const std::set<NodeId> &get_captures (NodeId id) const;

protected:
  bool decl_needs_capture (NodeId decl_rib_node_id, NodeId closure_rib_node_id,
			   const Scope &scope);

private:
  Resolver ();

  void generate_builtins ();
  void setup_builtin (const std::string &name, TyTy::BaseType *tyty);

  Analysis::Mappings *mappings;
  TypeCheckContext *tyctx;

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

  Scope name_scope;
  Scope type_scope;
  Scope label_scope;
  Scope macro_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;
  std::map<NodeId, Rib *> macro_ribs;

  // 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;
  std::map<NodeId, NodeId> resolved_macros;

  // misc
  std::map<NodeId, NodeId> misc_resolved_items;

  // keep track of the current module scope ids
  std::vector<NodeId> current_module_stack;

  // captured variables mappings
  std::vector<NodeId> closure_context;
  std::map<NodeId, std::set<NodeId>> closures_capture_mappings;
};

} // namespace Resolver
} // namespace Rust

#endif // RUST_NAME_RESOLVER_H