aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
blob: 095037042658b52732a8bdaef4eedc6d7f0b838f (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
// 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/>.

#ifndef RUST_TOPLEVEL_NAME_RESOLVER_2_0_H
#define RUST_TOPLEVEL_NAME_RESOLVER_2_0_H

#include "rust-ast-visitor.h"
#include "rust-name-resolution-context.h"
#include "rust-default-resolver.h"

namespace Rust {
namespace Resolver2_0 {

class GlobbingVisitor : public AST::DefaultASTVisitor
{
  using AST::DefaultASTVisitor::visit;

public:
  GlobbingVisitor (NameResolutionContext &ctx) : ctx (ctx) {}

  void go (AST::Module *module);
  void visit (AST::Module &module) override;
  void visit (AST::MacroRulesDefinition &macro) override;
  void visit (AST::Function &function) override;
  void visit (AST::StaticItem &static_item) override;
  void visit (AST::StructStruct &struct_item) override;
  void visit (AST::TupleStruct &tuple_struct) override;
  void visit (AST::Enum &enum_item) override;
  void visit (AST::Union &union_item) override;
  void visit (AST::ConstantItem &const_item) override;
  void visit (AST::ExternCrate &crate) override;
  void visit (AST::UseDeclaration &use) override;

private:
  NameResolutionContext &ctx;
};
/**
 * The `TopLevel` visitor takes care of collecting all the definitions in a
 * crate, and inserting them into the proper namespaces. These definitions can
 * then be accessed by subsequent resolvers, such as `Early` or `Late`.
 */
class TopLevel : public DefaultResolver
{
  using DefaultResolver::visit;

public:
  TopLevel (NameResolutionContext &resolver);

  void go (AST::Crate &crate);

private:
  /**
   * Insert a new definition or error out if a definition with the same name was
   * already present in the same namespace in the same scope.
   *
   * @param identifier The identifier of the definition to add.
   * @param node A reference to the node, so we can get its `NodeId` and
   * location.
   * @param ns The namespace in which to add the definition.
   */
  template <typename T>
  void insert_or_error_out (const Identifier &identifier, const T &node,
			    Namespace ns);
  void insert_or_error_out (const Identifier &identifier,
			    const location_t &locus, const NodeId &id,
			    Namespace ns);

  // FIXME: Do we move these to our mappings?
  std::unordered_map<NodeId, location_t> node_locations;

  // Store node forwarding for use declaration, the link between a
  // "new" local name and its definition.
  std::unordered_map<NodeId, NodeId> node_forwarding;

  void visit (AST::Module &module) override;
  void visit (AST::Trait &trait) override;
  void visit (AST::MacroRulesDefinition &macro) override;
  void visit (AST::Function &function) override;
  void visit (AST::BlockExpr &expr) override;
  void visit (AST::StaticItem &static_item) override;
  void visit (AST::StructStruct &struct_item) override;
  void visit (AST::TupleStruct &tuple_struct) override;
  void visit (AST::EnumItem &variant) override;
  void visit (AST::EnumItemTuple &variant) override;
  void visit (AST::EnumItemStruct &variant) override;
  void visit (AST::EnumItemDiscriminant &variant) override;
  void visit (AST::Enum &enum_item) override;
  void visit (AST::Union &union_item) override;
  void visit (AST::ConstantItem &const_item) override;
  void visit (AST::ExternCrate &crate) override;

  // FIXME: Documentation
  // Call this on all the paths of a UseDec - so each flattened path in a
  // UseTreeList for example
  // FIXME: Should that return `found`?
  bool handle_use_dec (AST::SimplePath &path);
  bool handle_use_glob (AST::SimplePath &glob);
  bool handle_rebind (std::pair<AST::SimplePath, AST::UseTreeRebind> &pair);

  void visit (AST::UseDeclaration &use) override;
};

} // namespace Resolver2_0
} // namespace Rust

#endif // !RUST_TOPLEVEL_NAME_RESOLVER_2_0_H