aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
blob: 55330487fd7a7b47f3718bd87b46903b576b3c98 (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
// Copyright (C) 2020-2025 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 "rust-early-name-resolver-2.0.h"
#include "rust-ast-full.h"
#include "rust-diagnostics.h"
#include "rust-toplevel-name-resolver-2.0.h"
#include "rust-attributes.h"
#include "rust-finalize-imports-2.0.h"

namespace Rust {
namespace Resolver2_0 {

Early::Early (NameResolutionContext &ctx) : DefaultResolver (ctx), dirty (false)
{}

void
Early::insert_once (AST::MacroInvocation &invocation, NodeId resolved)
{
  // TODO: Should we use `ctx.mark_resolved()`?
  auto definition = ctx.mappings.lookup_macro_def (resolved);

  if (!ctx.mappings.lookup_macro_invocation (invocation))
    ctx.mappings.insert_macro_invocation (invocation, definition.value ());
}

void
Early::insert_once (AST::MacroRulesDefinition &def)
{
  // TODO: Should we use `ctx.mark_resolved()`?
  if (!ctx.mappings.lookup_macro_def (def.get_node_id ()))
    ctx.mappings.insert_macro_def (&def);
}

void
Early::go (AST::Crate &crate)
{
  // First we go through TopLevel resolution to get all our declared items
  auto toplevel = TopLevel (ctx);
  toplevel.go (crate);

  // We start with resolving the list of imports that `TopLevel` has built for
  // us
  for (auto &&import : toplevel.get_imports_to_resolve ())
    build_import_mapping (std::move (import));

  // Once this is done, we finalize their resolution
  FinalizeImports (std::move (import_mappings), toplevel, ctx).go (crate);

  dirty = toplevel.is_dirty ();
  // We now proceed with resolving macros, which can be nested in almost any
  // items
  textual_scope.push ();
  for (auto &item : crate.items)
    item->accept_vis (*this);
  textual_scope.pop ();
}

bool
Early::resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&glob)
{
  auto resolved = ctx.types.resolve_path (glob.to_resolve.get_segments ());
  if (!resolved.has_value ())
    return false;

  auto result
    = Analysis::Mappings::get ().lookup_ast_module (resolved->get_node_id ());
  if (!result)
    return false;

  // here, we insert the module's NodeId into the import_mappings and will look
  // up the module proper in `FinalizeImports`
  // The namespace does not matter here since we are dealing with a glob
  // TODO: Ugly
  import_mappings.insert (use_dec_id,
			  ImportPair (std::move (glob),
				      ImportData::Glob (*resolved)));

  return true;
}

bool
Early::resolve_simple_import (NodeId use_dec_id, TopLevel::ImportKind &&import)
{
  auto definitions = resolve_path_in_all_ns (import.to_resolve);

  // if we've found at least one definition, then we're good
  if (definitions.empty ())
    return false;

  auto &imports = import_mappings.new_or_access (use_dec_id);

  imports.emplace_back (
    ImportPair (std::move (import),
		ImportData::Simple (std::move (definitions))));

  return true;
}

bool
Early::resolve_rebind_import (NodeId use_dec_id,
			      TopLevel::ImportKind &&rebind_import)
{
  auto definitions = resolve_path_in_all_ns (rebind_import.to_resolve);

  // if we've found at least one definition, then we're good
  if (definitions.empty ())
    return false;

  auto &imports = import_mappings.new_or_access (use_dec_id);

  imports.emplace_back (
    ImportPair (std::move (rebind_import),
		ImportData::Rebind (std::move (definitions))));

  return true;
}

void
Early::build_import_mapping (
  std::pair<NodeId, std::vector<TopLevel::ImportKind>> &&use_import)
{
  auto found = false;
  auto use_dec_id = use_import.first;

  for (auto &&import : use_import.second)
    {
      // We create a copy of the path in case of errors, since the `import` will
      // be moved into the newly created import mappings
      auto path = import.to_resolve;

      switch (import.kind)
	{
	case TopLevel::ImportKind::Kind::Glob:
	  found = resolve_glob_import (use_dec_id, std::move (import));
	  break;
	case TopLevel::ImportKind::Kind::Simple:
	  found = resolve_simple_import (use_dec_id, std::move (import));
	  break;
	case TopLevel::ImportKind::Kind::Rebind:
	  found = resolve_rebind_import (use_dec_id, std::move (import));
	  break;
	}

      if (!found)
	collect_error (Error (path.get_final_segment ().get_locus (),
			      ErrorCode::E0433, "unresolved import %qs",
			      path.as_string ().c_str ()));
    }
}

void
Early::TextualScope::push ()
{
  // push a new empty scope
  scopes.emplace_back ();
}

void
Early::TextualScope::pop ()
{
  rust_assert (!scopes.empty ());

  scopes.pop_back ();
}

void
Early::TextualScope::insert (std::string name, NodeId id)
{
  rust_assert (!scopes.empty ());

  // we can ignore the return value as we always want the latest defined macro
  // to shadow a previous one - so if two macros have the same name and get
  // inserted with the same key, it's not a bug
  scopes.back ().insert ({name, id});
}

tl::optional<NodeId>
Early::TextualScope::get (const std::string &name)
{
  for (auto iterator = scopes.rbegin (); iterator != scopes.rend (); iterator++)
    {
      auto scope = *iterator;
      auto found = scope.find (name);
      if (found != scope.end ())
	return found->second;
    }

  return tl::nullopt;
}

void
Early::visit (AST::MacroRulesDefinition &def)
{
  DefaultResolver::visit (def);

  textual_scope.insert (def.get_rule_name ().as_string (), def.get_node_id ());
  insert_once (def);
}

void
Early::visit (AST::BlockExpr &block)
{
  textual_scope.push ();

  DefaultResolver::visit (block);

  textual_scope.pop ();
}

void
Early::visit (AST::Module &module)
{
  textual_scope.push ();

  DefaultResolver::visit (module);

  textual_scope.pop ();
}

void
Early::visit (AST::MacroInvocation &invoc)
{
  auto path = invoc.get_invoc_data ().get_path ();

  // When a macro is invoked by an unqualified identifier (not part of a
  // multi-part path), it is first looked up in textual scoping. If this does
  // not yield any results, then it is looked up in path-based scoping. If the
  // macro's name is qualified with a path, then it is only looked up in
  // path-based scoping.

  // https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope

  tl::optional<Rib::Definition> definition = tl::nullopt;
  if (path.get_segments ().size () == 1)
    definition
      = textual_scope.get (path.get_final_segment ().as_string ())
	  .map ([] (NodeId id) { return Rib::Definition::NonShadowable (id); });

  // we won't have changed `definition` from `nullopt` if there are more
  // than one segments in our path
  if (!definition.has_value ())
    definition = ctx.macros.resolve_path (path.get_segments ());

  // if the definition still does not have a value, then it's an error
  if (!definition.has_value ())
    {
      collect_error (Error (invoc.get_locus (), ErrorCode::E0433,
			    "could not resolve macro invocation"));
      return;
    }

  insert_once (invoc, definition->get_node_id ());

  // now do we need to keep mappings or something? or insert "uses" into our
  // ForeverStack? can we do that? are mappings simpler?
  auto &mappings = Analysis::Mappings::get ();
  auto rules_def = mappings.lookup_macro_def (definition->get_node_id ());

  // Macro definition not found, maybe it is not expanded yet.
  if (!rules_def)
    return;

  if (mappings.lookup_macro_invocation (invoc))
    return;

  mappings.insert_macro_invocation (invoc, rules_def.value ());
}

void
Early::visit_attributes (std::vector<AST::Attribute> &attrs)
{
  auto &mappings = Analysis::Mappings::get ();

  for (auto &attr : attrs)
    {
      auto name = attr.get_path ().get_segments ().at (0).get_segment_name ();

      if (attr.is_derive ())
	{
	  auto traits = attr.get_traits_to_derive ();
	  for (auto &trait : traits)
	    {
	      auto definition
		= ctx.macros.resolve_path (trait.get ().get_segments ());
	      if (!definition.has_value ())
		{
		  // FIXME: Change to proper error message
		  collect_error (Error (trait.get ().get_locus (),
					"could not resolve trait %qs",
					trait.get ().as_string ().c_str ()));
		  continue;
		}

	      auto pm_def = mappings.lookup_derive_proc_macro_def (
		definition->get_node_id ());

	      rust_assert (pm_def.has_value ());

	      mappings.insert_derive_proc_macro_invocation (trait,
							    pm_def.value ());
	    }
	}
      else if (Analysis::BuiltinAttributeMappings::get ()
		 ->lookup_builtin (name)
		 .is_error ()) // Do not resolve builtins
	{
	  auto definition
	    = ctx.macros.resolve_path (attr.get_path ().get_segments ());
	  if (!definition.has_value ())
	    {
	      // FIXME: Change to proper error message
	      collect_error (
		Error (attr.get_locus (),
		       "could not resolve attribute macro invocation"));
	      return;
	    }
	  auto pm_def = mappings.lookup_attribute_proc_macro_def (
	    definition->get_node_id ());

	  rust_assert (pm_def.has_value ());

	  mappings.insert_attribute_proc_macro_invocation (attr.get_path (),
							   pm_def.value ());
	}
    }
}

void
Early::visit (AST::Function &fn)
{
  visit_attributes (fn.get_outer_attrs ());
  DefaultResolver::visit (fn);
}

void
Early::visit (AST::StructStruct &s)
{
  visit_attributes (s.get_outer_attrs ());
  DefaultResolver::visit (s);
}

} // namespace Resolver2_0
} // namespace Rust