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_EARLY_NAME_RESOLVER_2_0_H
20 : : #define RUST_EARLY_NAME_RESOLVER_2_0_H
21 : :
22 : : #include "optional.h"
23 : : #include "rust-ast.h"
24 : : #include "rust-ast-visitor.h"
25 : : #include "rust-name-resolution-context.h"
26 : : #include "rust-default-resolver.h"
27 : :
28 : : namespace Rust {
29 : : namespace Resolver2_0 {
30 : :
31 : : class Early : public DefaultResolver
32 : : {
33 : : using DefaultResolver::visit;
34 : :
35 : : public:
36 : : Early (NameResolutionContext &ctx);
37 : :
38 : : void go (AST::Crate &crate);
39 : :
40 : 35 : const std::vector<Error> &get_macro_resolve_errors () const
41 : : {
42 : 35 : return macro_resolve_errors;
43 : : }
44 : :
45 : : // we need to handle definitions for textual scoping
46 : : void visit (AST::MacroRulesDefinition &) override;
47 : :
48 : : // as well as lexical scopes
49 : : void visit (AST::BlockExpr &) override;
50 : : void visit (AST::Module &) override;
51 : :
52 : : void visit (AST::MacroInvocation &) override;
53 : :
54 : : void visit (AST::Function &) override;
55 : : void visit (AST::StructStruct &) override;
56 : :
57 : : private:
58 : : void visit_attributes (std::vector<AST::Attribute> &attrs);
59 : :
60 : : /**
61 : : * Insert a resolved macro invocation into the mappings once, meaning that we
62 : : * can call this function each time the early name resolution pass is underway
63 : : * and it will not trigger assertions for already resolved invocations.
64 : : */
65 : : // TODO: Rename
66 : : void insert_once (AST::MacroInvocation &invocation, NodeId resolved);
67 : : // TODO: Rename
68 : : void insert_once (AST::MacroRulesDefinition &definition);
69 : :
70 : : /**
71 : : * Macros can either be resolved through textual scoping or regular path
72 : : * scoping - which this class represents. Textual scoping works similarly to a
73 : : * "simple" name resolution algorith, with the addition of "shadowing". Each
74 : : * time a new lexical scope is entered, we push a new map onto the stack, in
75 : : * which newly defined macros are added. The latest defined macro is the one
76 : : * that takes precedence. When resolving a macro invocation to its definition,
77 : : * we walk up the stack and look for a definition in each of the map until we
78 : : * find one. Otherwise, the macro invocation is unresolved, and goes through
79 : : * regular path resolution.
80 : : */
81 : 35 : class TextualScope
82 : : {
83 : : public:
84 : : void push ();
85 : : void pop ();
86 : :
87 : : void insert (std::string name, NodeId id);
88 : : tl::optional<NodeId> get (const std::string &name);
89 : :
90 : : private:
91 : : std::vector<std::unordered_map<std::string, NodeId>> scopes;
92 : : };
93 : :
94 : : TextualScope textual_scope;
95 : : std::vector<Error> macro_resolve_errors;
96 : :
97 : 6 : void collect_error (Error e) { macro_resolve_errors.push_back (e); }
98 : : };
99 : :
100 : : } // namespace Resolver2_0
101 : : } // namespace Rust
102 : :
103 : : #endif // ! RUST_EARLY_NAME_RESOLVER_2_0_H
|