@@ -12,63 +12,41 @@ use {Module, Resolver};
12
12
use build_reduced_graph:: BuildReducedGraphVisitor ;
13
13
use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , DefIndex } ;
14
14
use rustc:: hir:: map:: DefCollector ;
15
- use rustc:: middle:: cstore:: LoadedMacro ;
16
- use rustc:: util:: nodemap:: FnvHashMap ;
17
- use std:: cell:: RefCell ;
18
- use std:: mem;
19
15
use std:: rc:: Rc ;
20
- use syntax:: ast:: { self , Name } ;
16
+ use syntax:: ast;
21
17
use syntax:: errors:: DiagnosticBuilder ;
22
18
use syntax:: ext:: base:: { self , MultiModifier , MultiDecorator , MultiItemModifier } ;
23
- use syntax:: ext:: base:: { NormalTT , Resolver as SyntaxResolver , SyntaxExtension } ;
19
+ use syntax:: ext:: base:: { NormalTT , SyntaxExtension } ;
24
20
use syntax:: ext:: expand:: { Expansion , Invocation , InvocationKind } ;
25
21
use syntax:: ext:: hygiene:: Mark ;
26
22
use syntax:: ext:: tt:: macro_rules;
27
- use syntax:: feature_gate:: { self , emit_feature_err} ;
28
- use syntax:: parse:: token:: { self , intern} ;
23
+ use syntax:: parse:: token:: intern;
29
24
use syntax:: util:: lev_distance:: find_best_match_for_name;
30
- use syntax:: visit:: { self , Visitor } ;
31
- use syntax_pos:: Span ;
32
25
33
26
#[ derive( Clone ) ]
34
27
pub struct ExpansionData < ' a > {
35
- module : Rc < ModuleData > ,
28
+ pub module : Module < ' a > ,
36
29
def_index : DefIndex ,
37
- pub module2 : Module < ' a > ,
38
30
}
39
31
40
32
impl < ' a > ExpansionData < ' a > {
41
33
pub fn root ( graph_root : Module < ' a > ) -> Self {
42
34
ExpansionData {
43
- module : Default :: default ( ) ,
35
+ module : graph_root ,
44
36
def_index : CRATE_DEF_INDEX ,
45
- module2 : graph_root,
46
37
}
47
38
}
48
39
}
49
40
50
- // FIXME(jseyfried): merge with `::ModuleS`.
51
- #[ derive( Default ) ]
52
- struct ModuleData {
53
- parent : Option < Rc < ModuleData > > ,
54
- macros : RefCell < FnvHashMap < Name , Rc < SyntaxExtension > > > ,
55
- macros_escape : bool ,
56
- }
57
-
58
41
impl < ' a > base:: Resolver for Resolver < ' a > {
59
42
fn next_node_id ( & mut self ) -> ast:: NodeId {
60
43
self . session . next_node_id ( )
61
44
}
62
45
63
46
fn visit_expansion ( & mut self , mark : Mark , expansion : & Expansion ) {
64
- let expansion_data = self . expansion_data [ & mark. as_u32 ( ) ] . clone ( ) ;
65
- self . current_module = expansion_data. module2 ;
66
- let mut visitor =
67
- ExpansionVisitor { current_module : expansion_data. module , resolver : self } ;
68
-
69
- visitor. collect_def_ids ( mark, expansion) ;
70
- expansion. visit_with ( & mut visitor) ;
71
- expansion. visit_with ( & mut BuildReducedGraphVisitor { resolver : visitor. resolver } ) ;
47
+ self . collect_def_ids ( mark, expansion) ;
48
+ self . current_module = self . expansion_data [ & mark. as_u32 ( ) ] . module ;
49
+ expansion. visit_with ( & mut BuildReducedGraphVisitor { resolver : self } ) ;
72
50
}
73
51
74
52
fn add_macro ( & mut self , scope : Mark , mut def : ast:: MacroDef ) {
@@ -90,9 +68,9 @@ impl<'a> base::Resolver for Resolver<'a> {
90
68
self . macro_names . insert ( ident. name ) ;
91
69
}
92
70
93
- let mut module = self . expansion_data [ & scope. as_u32 ( ) ] . module . clone ( ) ;
71
+ let mut module = self . expansion_data [ & scope. as_u32 ( ) ] . module ;
94
72
while module. macros_escape {
95
- module = module. parent . clone ( ) . unwrap ( ) ;
73
+ module = module. parent . unwrap ( ) ;
96
74
}
97
75
module. macros . borrow_mut ( ) . insert ( ident. name , ext) ;
98
76
}
@@ -132,12 +110,12 @@ impl<'a> base::Resolver for Resolver<'a> {
132
110
InvocationKind :: Attr { ref attr, .. } => ( intern ( & * attr. name ( ) ) , attr. span ) ,
133
111
} ;
134
112
135
- let mut module = self . expansion_data [ & scope. as_u32 ( ) ] . module . clone ( ) ;
113
+ let mut module = self . expansion_data [ & scope. as_u32 ( ) ] . module ;
136
114
loop {
137
115
if let Some ( ext) = module. macros . borrow ( ) . get ( & name) {
138
116
return Some ( ext. clone ( ) ) ;
139
117
}
140
- match module. parent . clone ( ) {
118
+ match module. parent {
141
119
Some ( parent) => module = parent,
142
120
None => break ,
143
121
}
@@ -166,133 +144,19 @@ impl<'a> Resolver<'a> {
166
144
}
167
145
}
168
146
169
- fn insert_custom_derive ( & mut self , name : & str , ext : Rc < MultiItemModifier > , sp : Span ) {
170
- if !self . session . features . borrow ( ) . rustc_macro {
171
- let sess = & self . session . parse_sess ;
172
- let msg = "loading custom derive macro crates is experimentally supported" ;
173
- emit_feature_err ( sess, "rustc_macro" , sp, feature_gate:: GateIssue :: Language , msg) ;
174
- }
175
- if self . derive_modes . insert ( token:: intern ( name) , ext) . is_some ( ) {
176
- self . session . span_err ( sp, & format ! ( "cannot shadow existing derive mode `{}`" , name) ) ;
177
- }
178
- }
179
- }
180
-
181
- struct ExpansionVisitor < ' b , ' a : ' b > {
182
- resolver : & ' b mut Resolver < ' a > ,
183
- current_module : Rc < ModuleData > ,
184
- }
185
-
186
- impl < ' a , ' b > ExpansionVisitor < ' a , ' b > {
187
- fn visit_invoc ( & mut self , id : ast:: NodeId ) {
188
- self . resolver . expansion_data . get_mut ( & id. as_u32 ( ) ) . unwrap ( ) . module =
189
- self . current_module . clone ( ) ;
190
- }
191
-
192
- // does this attribute list contain "macro_use"?
193
- fn contains_macro_use ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
194
- for attr in attrs {
195
- if attr. check_name ( "macro_escape" ) {
196
- let msg = "macro_escape is a deprecated synonym for macro_use" ;
197
- let mut err = self . resolver . session . struct_span_warn ( attr. span , msg) ;
198
- if let ast:: AttrStyle :: Inner = attr. node . style {
199
- err. help ( "consider an outer attribute, #[macro_use] mod ..." ) . emit ( ) ;
200
- } else {
201
- err. emit ( ) ;
202
- }
203
- } else if !attr. check_name ( "macro_use" ) {
204
- continue ;
205
- }
206
-
207
- if !attr. is_word ( ) {
208
- self . resolver . session . span_err ( attr. span ,
209
- "arguments to macro_use are not allowed here" ) ;
210
- }
211
- return true ;
212
- }
213
-
214
- false
215
- }
216
-
217
147
fn collect_def_ids ( & mut self , mark : Mark , expansion : & Expansion ) {
218
- let expansion_data = & mut self . resolver . expansion_data ;
219
- let module = & self . current_module ;
220
- let module2 = self . resolver . current_module ;
148
+ let expansion_data = & mut self . expansion_data ;
149
+ let module = self . current_module ;
221
150
let def_index = expansion_data[ & mark. as_u32 ( ) ] . def_index ;
222
151
let visit_macro_invoc = & mut |id : ast:: NodeId , def_index| {
223
152
expansion_data. insert ( id. as_u32 ( ) , ExpansionData {
224
153
def_index : def_index,
225
- module : module. clone ( ) ,
226
- module2 : module2,
154
+ module : module,
227
155
} ) ;
228
156
} ;
229
157
230
- let mut def_collector = DefCollector :: new ( & mut self . resolver . definitions ) ;
158
+ let mut def_collector = DefCollector :: new ( & mut self . definitions ) ;
231
159
def_collector. visit_macro_invoc = Some ( visit_macro_invoc) ;
232
160
def_collector. with_parent ( def_index, |def_collector| expansion. visit_with ( def_collector) ) ;
233
161
}
234
162
}
235
-
236
- macro_rules! method {
237
- ( $visit: ident: $ty: ty, $invoc: path, $walk: ident) => {
238
- fn $visit( & mut self , node: & $ty) {
239
- match node. node {
240
- $invoc( ..) => self . visit_invoc( node. id) ,
241
- _ => visit:: $walk( self , node) ,
242
- }
243
- }
244
- }
245
- }
246
-
247
- impl < ' a , ' b > Visitor for ExpansionVisitor < ' a , ' b > {
248
- method ! ( visit_trait_item: ast:: TraitItem , ast:: TraitItemKind :: Macro , walk_trait_item) ;
249
- method ! ( visit_impl_item: ast:: ImplItem , ast:: ImplItemKind :: Macro , walk_impl_item) ;
250
- method ! ( visit_stmt: ast:: Stmt , ast:: StmtKind :: Mac , walk_stmt) ;
251
- method ! ( visit_expr: ast:: Expr , ast:: ExprKind :: Mac , walk_expr) ;
252
- method ! ( visit_pat: ast:: Pat , ast:: PatKind :: Mac , walk_pat) ;
253
- method ! ( visit_ty: ast:: Ty , ast:: TyKind :: Mac , walk_ty) ;
254
-
255
- fn visit_item ( & mut self , item : & ast:: Item ) {
256
- match item. node {
257
- ast:: ItemKind :: Mac ( ..) if item. id == ast:: DUMMY_NODE_ID => { } // Scope placeholder
258
- ast:: ItemKind :: Mac ( ..) => self . visit_invoc ( item. id ) ,
259
- ast:: ItemKind :: Mod ( ..) => {
260
- let module_data = ModuleData {
261
- parent : Some ( self . current_module . clone ( ) ) ,
262
- macros : RefCell :: new ( FnvHashMap ( ) ) ,
263
- macros_escape : self . contains_macro_use ( & item. attrs ) ,
264
- } ;
265
- let orig_module = mem:: replace ( & mut self . current_module , Rc :: new ( module_data) ) ;
266
- visit:: walk_item ( self , item) ;
267
- self . current_module = orig_module;
268
- }
269
- ast:: ItemKind :: ExternCrate ( ..) => {
270
- // We need to error on `#[macro_use] extern crate` when it isn't at the
271
- // crate root, because `$crate` won't work properly.
272
- // FIXME(jseyfried): This will be nicer once `ModuleData` is merged with `ModuleS`.
273
- let is_crate_root = self . current_module . parent . as_ref ( ) . unwrap ( ) . parent . is_none ( ) ;
274
- for def in self . resolver . crate_loader . load_macros ( item, is_crate_root) {
275
- match def {
276
- LoadedMacro :: Def ( def) => self . resolver . add_macro ( Mark :: root ( ) , def) ,
277
- LoadedMacro :: CustomDerive ( name, ext) => {
278
- self . resolver . insert_custom_derive ( & name, ext, item. span ) ;
279
- }
280
- }
281
- }
282
- visit:: walk_item ( self , item) ;
283
- }
284
- _ => visit:: walk_item ( self , item) ,
285
- }
286
- }
287
-
288
- fn visit_block ( & mut self , block : & ast:: Block ) {
289
- let module_data = ModuleData {
290
- parent : Some ( self . current_module . clone ( ) ) ,
291
- macros : RefCell :: new ( FnvHashMap ( ) ) ,
292
- macros_escape : false ,
293
- } ;
294
- let orig_module = mem:: replace ( & mut self . current_module , Rc :: new ( module_data) ) ;
295
- visit:: walk_block ( self , block) ;
296
- self . current_module = orig_module;
297
- }
298
- }
0 commit comments