@@ -2,10 +2,12 @@ use crate::hir::def_id::DefId;
2
2
use crate :: util:: nodemap:: { NodeMap , DefIdMap } ;
3
3
use syntax:: ast;
4
4
use syntax:: ext:: base:: MacroKind ;
5
+ use syntax:: ast:: NodeId ;
5
6
use syntax_pos:: Span ;
6
7
use rustc_macros:: HashStable ;
7
8
use crate :: hir;
8
9
use crate :: ty;
10
+ use std:: fmt:: Debug ;
9
11
10
12
use self :: Namespace :: * ;
11
13
@@ -43,7 +45,7 @@ pub enum NonMacroAttrKind {
43
45
}
44
46
45
47
#[ derive( Clone , Copy , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , HashStable ) ]
46
- pub enum Def {
48
+ pub enum Def < Id = hir :: HirId > {
47
49
// Type namespace
48
50
Mod ( DefId ) ,
49
51
/// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists.
@@ -78,8 +80,8 @@ pub enum Def {
78
80
Method ( DefId ) ,
79
81
AssociatedConst ( DefId ) ,
80
82
81
- Local ( ast :: NodeId ) ,
82
- Upvar ( ast :: NodeId , // `NodeId ` of closed over local
83
+ Local ( Id ) ,
84
+ Upvar ( Id , // `HirId ` of closed over local
83
85
usize , // index in the `freevars` list of the closure
84
86
ast:: NodeId ) , // expr node that creates the closure
85
87
Label ( ast:: NodeId ) ,
@@ -108,22 +110,22 @@ pub enum Def {
108
110
/// ```
109
111
#[ derive( Copy , Clone , Debug ) ]
110
112
pub struct PathResolution {
111
- base_def : Def ,
113
+ base_def : Def < NodeId > ,
112
114
unresolved_segments : usize ,
113
115
}
114
116
115
117
impl PathResolution {
116
- pub fn new ( def : Def ) -> Self {
118
+ pub fn new ( def : Def < NodeId > ) -> Self {
117
119
PathResolution { base_def : def, unresolved_segments : 0 }
118
120
}
119
121
120
- pub fn with_unresolved_segments ( def : Def , mut unresolved_segments : usize ) -> Self {
122
+ pub fn with_unresolved_segments ( def : Def < NodeId > , mut unresolved_segments : usize ) -> Self {
121
123
if def == Def :: Err { unresolved_segments = 0 }
122
124
PathResolution { base_def : def, unresolved_segments : unresolved_segments }
123
125
}
124
126
125
127
#[ inline]
126
- pub fn base_def ( & self ) -> Def {
128
+ pub fn base_def ( & self ) -> Def < NodeId > {
127
129
self . base_def
128
130
}
129
131
@@ -215,25 +217,36 @@ pub type DefMap = NodeMap<PathResolution>;
215
217
216
218
/// This is the replacement export map. It maps a module to all of the exports
217
219
/// within.
218
- pub type ExportMap = DefIdMap < Vec < Export > > ;
220
+ pub type ExportMap < Id > = DefIdMap < Vec < Export < Id > > > ;
219
221
220
222
/// Map used to track the `use` statements within a scope, matching it with all the items in every
221
223
/// namespace.
222
224
pub type ImportMap = NodeMap < PerNS < Option < PathResolution > > > ;
223
225
224
226
#[ derive( Copy , Clone , Debug , RustcEncodable , RustcDecodable , HashStable ) ]
225
- pub struct Export {
227
+ pub struct Export < Id > {
226
228
/// The name of the target.
227
229
pub ident : ast:: Ident ,
228
230
/// The definition of the target.
229
- pub def : Def ,
231
+ pub def : Def < Id > ,
230
232
/// The span of the target definition.
231
233
pub span : Span ,
232
234
/// The visibility of the export.
233
235
/// We include non-`pub` exports for hygienic macros that get used from extern crates.
234
236
pub vis : ty:: Visibility ,
235
237
}
236
238
239
+ impl < Id > Export < Id > {
240
+ pub fn map_id < R > ( self , map : impl FnMut ( Id ) -> R ) -> Export < R > {
241
+ Export {
242
+ ident : self . ident ,
243
+ def : self . def . map_id ( map) ,
244
+ span : self . span ,
245
+ vis : self . vis ,
246
+ }
247
+ }
248
+ }
249
+
237
250
impl CtorKind {
238
251
pub fn from_ast ( vdata : & ast:: VariantData ) -> CtorKind {
239
252
match * vdata {
@@ -264,9 +277,12 @@ impl NonMacroAttrKind {
264
277
}
265
278
}
266
279
267
- impl Def {
280
+ impl < Id > Def < Id > {
268
281
/// Return the `DefId` of this `Def` if it has an id, else panic.
269
- pub fn def_id ( & self ) -> DefId {
282
+ pub fn def_id ( & self ) -> DefId
283
+ where
284
+ Id : Debug ,
285
+ {
270
286
self . opt_def_id ( ) . unwrap_or_else ( || {
271
287
bug ! ( "attempted .def_id() on invalid def: {:?}" , self )
272
288
} )
@@ -358,4 +374,43 @@ impl Def {
358
374
_ => "a" ,
359
375
}
360
376
}
377
+
378
+ pub fn map_id < R > ( self , mut map : impl FnMut ( Id ) -> R ) -> Def < R > {
379
+ match self {
380
+ Def :: Fn ( id) => Def :: Fn ( id) ,
381
+ Def :: Mod ( id) => Def :: Mod ( id) ,
382
+ Def :: Static ( id, is_mutbl) => Def :: Static ( id, is_mutbl) ,
383
+ Def :: Enum ( id) => Def :: Enum ( id) ,
384
+ Def :: Variant ( id) => Def :: Variant ( id) ,
385
+ Def :: Ctor ( a, b, c) => Def :: Ctor ( a, b, c) ,
386
+ Def :: Struct ( id) => Def :: Struct ( id) ,
387
+ Def :: Existential ( id) => Def :: Existential ( id) ,
388
+ Def :: TyAlias ( id) => Def :: TyAlias ( id) ,
389
+ Def :: TraitAlias ( id) => Def :: TraitAlias ( id) ,
390
+ Def :: AssociatedTy ( id) => Def :: AssociatedTy ( id) ,
391
+ Def :: AssociatedExistential ( id) => Def :: AssociatedExistential ( id) ,
392
+ Def :: SelfCtor ( id) => Def :: SelfCtor ( id) ,
393
+ Def :: Union ( id) => Def :: Union ( id) ,
394
+ Def :: Trait ( id) => Def :: Trait ( id) ,
395
+ Def :: ForeignTy ( id) => Def :: ForeignTy ( id) ,
396
+ Def :: Method ( id) => Def :: Method ( id) ,
397
+ Def :: Const ( id) => Def :: Const ( id) ,
398
+ Def :: AssociatedConst ( id) => Def :: AssociatedConst ( id) ,
399
+ Def :: TyParam ( id) => Def :: TyParam ( id) ,
400
+ Def :: ConstParam ( id) => Def :: ConstParam ( id) ,
401
+ Def :: PrimTy ( id) => Def :: PrimTy ( id) ,
402
+ Def :: Local ( id) => Def :: Local ( map ( id) ) ,
403
+ Def :: Upvar ( id, index, closure) => Def :: Upvar (
404
+ map ( id) ,
405
+ index,
406
+ closure
407
+ ) ,
408
+ Def :: Label ( id) => Def :: Label ( id) ,
409
+ Def :: SelfTy ( a, b) => Def :: SelfTy ( a, b) ,
410
+ Def :: Macro ( id, macro_kind) => Def :: Macro ( id, macro_kind) ,
411
+ Def :: ToolMod => Def :: ToolMod ,
412
+ Def :: NonMacroAttr ( attr_kind) => Def :: NonMacroAttr ( attr_kind) ,
413
+ Def :: Err => Def :: Err ,
414
+ }
415
+ }
361
416
}
0 commit comments