Debian Codegraph @

gcc/rust/resolve/rust-early-name-resolver.h

raw
1 // Copyright (C) 2020-2025 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_H
20 #define RUST_EARLY_NAME_RESOLVER_H
21
22 #include "rust-name-resolver.h"
23 #include "rust-system.h"
24 #include "rust-ast.h"
25 #include "rust-ast-visitor.h"
26
27 namespace Rust {
28 namespace Resolver {
29
30 class EarlyNameResolver : public AST::DefaultASTVisitor
31 {
32 public:
33 EarlyNameResolver ();
34
35 void go (AST::Crate &crate);
36
37 private:
38 using AST::DefaultASTVisitor::visit;
39
40 /**
41 * Execute a lambda within a scope. This is equivalent to calling
42 * `enter_scope` before your code and `exit_scope` after. This ensures
43 * no errors can be committed
44 */
45 void scoped (NodeId scope_id, std::function<void ()> fn)
46 {
47 auto old_scope = current_scope;
48 current_scope = scope_id;
49 resolver.get_macro_scope ().push (scope_id);
50 resolver.push_new_macro_rib (resolver.get_macro_scope ().peek ());
51
52 fn ();
53
54 resolver.get_macro_scope ().pop ();
55 current_scope = old_scope;
56 }
57
58 /**
59 * Accumulate all of the nested macros which escape their module through the
60 * use of the #[macro_use] attribute.
61 *
62 * This function recursively accumulates macros in all of the nested modules
63 * of an item container (an AST::Crate or an AST::Module) and returns this new
64 * list of items. You can then use the `take_items` and `set_items` functions
65 * on these containers to replace their list of items.
66 */
67 std::vector<std::unique_ptr<AST::Item>>
68 accumulate_escaped_macros (AST::Module &module);
69
70 /**
71 * The "scope" we are currently in.
72 *
73 * This involves lexical scopes:
74 *
75 * ```rust
76 * // current_scope = crate_id;
77 * macro_rules! foo { () => {} )
78 *
79 * {
80 * // current_scope = current_block_id;
81 * macro_rules! foo { () => { something!(); } }
82 * }
83 * // current_scope = crate_id;
84 * ```
85 *
86 * as well as any sort of scope-like structure that might impact import name
87 * resolution or macro name resolution:
88 *
89 * ```rust
90 * macro_rules! foo {
91 * () => { fn empty() {} }
92 * }
93 *
94 *
95 * trait Foo {
96 * fn foo() {
97 * fn inner_foo() {
98 * macro_rules! foo { () => {} )
99 *
100 * foo!();
101 * }
102 *
103 * foo!();
104 * }
105 *
106 * foo!();
107 * }
108 *
109 * foo!();
110 * ```
111 */
112 NodeId current_scope;
113
114 /* The crate's scope */
115 NodeId crate_scope;
116
117 Resolver &resolver;
118 Analysis::Mappings &mappings;
119
120 /**
121 * Early name-resolve generic args, which can be macro invocations
122 */
123 void resolve_generic_args (AST::GenericArgs &generic_args);
124
125 /**
126 * Early name-resolve a qualified path type, which can contain macro
127 * invocations
128 */
129 void resolve_qualified_path_type (AST::QualifiedPathType &path);
130
131 virtual void visit (AST::Crate &crate);
132 virtual void visit (AST::DelimTokenTree &delim_tok_tree);
133 virtual void visit (AST::AttrInputMetaItemContainer &input);
134 virtual void visit (AST::IdentifierExpr &ident_expr);
135 virtual void visit (AST::LifetimeParam &lifetime_param);
136 virtual void visit (AST::ConstGenericParam &const_param);
137 virtual void visit (AST::PathInExpression &path);
138 virtual void visit (AST::TypePathSegmentGeneric &segment);
139 virtual void visit (AST::QualifiedPathInExpression &path);
140 virtual void visit (AST::QualifiedPathInType &path);
141 virtual void visit (AST::LiteralExpr &expr);
142 virtual void visit (AST::AttrInputLiteral &attr_input);
143 virtual void visit (AST::AttrInputMacro &attr_input);
144 virtual void visit (AST::MetaItemLitExpr &meta_item);
145 virtual void visit (AST::MetaItemPathLit &meta_item);
146 virtual void visit (AST::StructExprStruct &expr);
147 virtual void visit (AST::StructExprFieldIdentifier &field);
148 virtual void visit (AST::StructExprStructBase &expr);
149 virtual void visit (AST::BlockExpr &expr);
150 virtual void visit (AST::ContinueExpr &expr);
151 virtual void visit (AST::RangeFullExpr &expr);
152 virtual void visit (AST::ForLoopExpr &expr);
153 virtual void visit (AST::IfLetExpr &expr);
154 virtual void visit (AST::MatchExpr &expr);
155 virtual void visit (AST::LifetimeWhereClauseItem &item);
156 virtual void visit (AST::Module &module);
157 virtual void visit (AST::ExternCrate &crate);
158 virtual void visit (AST::UseTreeGlob &use_tree);
159 virtual void visit (AST::UseTreeList &use_tree);
160 virtual void visit (AST::UseTreeRebind &use_tree);
161 virtual void visit (AST::UseDeclaration &use_decl);
162 virtual void visit (AST::EnumItem &item);
163 virtual void visit (AST::Union &union_item);
164 virtual void visit (AST::TraitItemType &item);
165 virtual void visit (AST::Trait &trait);
166 virtual void visit (AST::InherentImpl &impl);
167 virtual void visit (AST::TraitImpl &impl);
168 virtual void visit (AST::ExternalTypeItem &item);
169 virtual void visit (AST::ExternBlock &block);
170 virtual void visit (AST::MacroMatchRepetition &match);
171 virtual void visit (AST::MacroMatcher &matcher);
172 virtual void visit (AST::MacroRulesDefinition &rules_def);
173 virtual void visit (AST::MacroInvocation &macro_invoc);
174 virtual void visit (AST::MetaItemPath &meta_item);
175 virtual void visit (AST::MetaItemSeq &meta_item);
176 virtual void visit (AST::MetaNameValueStr &meta_item);
177 virtual void visit (AST::MetaListPaths &meta_item);
178 virtual void visit (AST::MetaListNameValueStr &meta_item);
179 virtual void visit (AST::RangePatternBoundLiteral &bound);
180 virtual void visit (AST::RangePatternBoundPath &bound);
181 virtual void visit (AST::RangePatternBoundQualPath &bound);
182 virtual void visit (AST::StructPatternFieldIdent &field);
183 virtual void visit (AST::StructPattern &pattern);
184 virtual void visit (AST::TupleStructPattern &pattern);
185 virtual void visit (AST::TupleType &type);
186 virtual void visit (AST::RawPointerType &type);
187 virtual void visit (AST::ReferenceType &type);
188 virtual void visit (AST::ArrayType &type);
189 virtual void visit (AST::SliceType &type);
190 virtual void visit (AST::InferredType &type);
191 };
192
193 } // namespace Resolver
194 } // namespace Rust
195
196 #endif // RUST_EARLY_NAME_RESOLVER_H