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