Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 Free Software Foundation, Inc.
2 : :
3 : : // This file is part of GCC.
4 : :
5 : : // GCC is free software; you can redistribute it and/or modify it under
6 : : // the terms of the GNU General Public License as published by the Free
7 : : // Software Foundation; either version 3, or (at your option) any later
8 : : // version.
9 : :
10 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : : // for more details.
14 : :
15 : : // You should have received a copy of the GNU General Public License
16 : : // along with GCC; see the file COPYING3. If not see
17 : : // <http://www.gnu.org/licenses/>.
18 : :
19 : : #ifndef RUST_TOPLEVEL_NAME_RESOLVER_2_0_H
20 : : #define RUST_TOPLEVEL_NAME_RESOLVER_2_0_H
21 : :
22 : : #include "rust-ast-visitor.h"
23 : : #include "rust-name-resolution-context.h"
24 : : #include "rust-default-resolver.h"
25 : :
26 : : namespace Rust {
27 : : namespace Resolver2_0 {
28 : :
29 : : class GlobbingVisitor : public AST::DefaultASTVisitor
30 : : {
31 : : using AST::DefaultASTVisitor::visit;
32 : :
33 : : public:
34 : 3 : GlobbingVisitor (NameResolutionContext &ctx) : ctx (ctx) {}
35 : :
36 : : void go (AST::Module *module);
37 : : void visit (AST::Module &module) override;
38 : : void visit (AST::MacroRulesDefinition ¯o) override;
39 : : void visit (AST::Function &function) override;
40 : : void visit (AST::StaticItem &static_item) override;
41 : : void visit (AST::StructStruct &struct_item) override;
42 : : void visit (AST::TupleStruct &tuple_struct) override;
43 : : void visit (AST::Enum &enum_item) override;
44 : : void visit (AST::Union &union_item) override;
45 : : void visit (AST::ConstantItem &const_item) override;
46 : : void visit (AST::ExternCrate &crate) override;
47 : : void visit (AST::UseDeclaration &use) override;
48 : :
49 : : private:
50 : : NameResolutionContext &ctx;
51 : : };
52 : : /**
53 : : * The `TopLevel` visitor takes care of collecting all the definitions in a
54 : : * crate, and inserting them into the proper namespaces. These definitions can
55 : : * then be accessed by subsequent resolvers, such as `Early` or `Late`.
56 : : */
57 : : class TopLevel : public DefaultResolver
58 : : {
59 : : using DefaultResolver::visit;
60 : :
61 : : public:
62 : : TopLevel (NameResolutionContext &resolver);
63 : :
64 : : void go (AST::Crate &crate);
65 : :
66 : : private:
67 : : /**
68 : : * Insert a new definition or error out if a definition with the same name was
69 : : * already present in the same namespace in the same scope.
70 : : *
71 : : * @param identifier The identifier of the definition to add.
72 : : * @param node A reference to the node, so we can get its `NodeId` and
73 : : * location.
74 : : * @param ns The namespace in which to add the definition.
75 : : */
76 : : template <typename T>
77 : : void insert_or_error_out (const Identifier &identifier, const T &node,
78 : : Namespace ns);
79 : : void insert_or_error_out (const Identifier &identifier,
80 : : const location_t &locus, const NodeId &id,
81 : : Namespace ns);
82 : :
83 : : // FIXME: Do we move these to our mappings?
84 : : std::unordered_map<NodeId, location_t> node_locations;
85 : :
86 : : // Store node forwarding for use declaration, the link between a
87 : : // "new" local name and its definition.
88 : : std::unordered_map<NodeId, NodeId> node_forwarding;
89 : :
90 : : void visit (AST::Module &module) override;
91 : : void visit (AST::Trait &trait) override;
92 : : void visit (AST::MacroRulesDefinition ¯o) override;
93 : : void visit (AST::Function &function) override;
94 : : void visit (AST::BlockExpr &expr) override;
95 : : void visit (AST::StaticItem &static_item) override;
96 : : void visit (AST::StructStruct &struct_item) override;
97 : : void visit (AST::TupleStruct &tuple_struct) override;
98 : : void visit (AST::EnumItem &variant) override;
99 : : void visit (AST::EnumItemTuple &variant) override;
100 : : void visit (AST::EnumItemStruct &variant) override;
101 : : void visit (AST::EnumItemDiscriminant &variant) override;
102 : : void visit (AST::Enum &enum_item) override;
103 : : void visit (AST::Union &union_item) override;
104 : : void visit (AST::ConstantItem &const_item) override;
105 : : void visit (AST::ExternCrate &crate) override;
106 : :
107 : : // FIXME: Documentation
108 : : // Call this on all the paths of a UseDec - so each flattened path in a
109 : : // UseTreeList for example
110 : : // FIXME: Should that return `found`?
111 : : bool handle_use_dec (AST::SimplePath &path);
112 : : bool handle_use_glob (AST::SimplePath &glob);
113 : : bool handle_rebind (std::pair<AST::SimplePath, AST::UseTreeRebind> &pair);
114 : :
115 : : void visit (AST::UseDeclaration &use) override;
116 : : };
117 : :
118 : : } // namespace Resolver2_0
119 : : } // namespace Rust
120 : :
121 : : #endif // !RUST_TOPLEVEL_NAME_RESOLVER_2_0_H
|