@@ -2,10 +2,12 @@ use crate::hir::def_id::DefId;
22use crate :: util:: nodemap:: { NodeMap , DefIdMap } ;
33use syntax:: ast;
44use syntax:: ext:: base:: MacroKind ;
5+ use syntax:: ast:: NodeId ;
56use syntax_pos:: Span ;
67use rustc_macros:: HashStable ;
78use crate :: hir;
89use crate :: ty;
10+ use std:: fmt:: Debug ;
911
1012use self :: Namespace :: * ;
1113
@@ -43,7 +45,7 @@ pub enum NonMacroAttrKind {
4345}
4446
4547#[ derive( Clone , Copy , PartialEq , Eq , RustcEncodable , RustcDecodable , Hash , Debug , HashStable ) ]
46- pub enum Def {
48+ pub enum Def < Id = hir :: HirId > {
4749 // Type namespace
4850 Mod ( DefId ) ,
4951 /// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists.
@@ -78,8 +80,8 @@ pub enum Def {
7880 Method ( DefId ) ,
7981 AssociatedConst ( DefId ) ,
8082
81- Local ( ast :: NodeId ) ,
82- Upvar ( ast :: NodeId , // `NodeId ` of closed over local
83+ Local ( Id ) ,
84+ Upvar ( Id , // `HirId ` of closed over local
8385 usize , // index in the `freevars` list of the closure
8486 ast:: NodeId ) , // expr node that creates the closure
8587 Label ( ast:: NodeId ) ,
@@ -108,22 +110,22 @@ pub enum Def {
108110/// ```
109111#[ derive( Copy , Clone , Debug ) ]
110112pub struct PathResolution {
111- base_def : Def ,
113+ base_def : Def < NodeId > ,
112114 unresolved_segments : usize ,
113115}
114116
115117impl PathResolution {
116- pub fn new ( def : Def ) -> Self {
118+ pub fn new ( def : Def < NodeId > ) -> Self {
117119 PathResolution { base_def : def, unresolved_segments : 0 }
118120 }
119121
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 {
121123 if def == Def :: Err { unresolved_segments = 0 }
122124 PathResolution { base_def : def, unresolved_segments : unresolved_segments }
123125 }
124126
125127 #[ inline]
126- pub fn base_def ( & self ) -> Def {
128+ pub fn base_def ( & self ) -> Def < NodeId > {
127129 self . base_def
128130 }
129131
@@ -215,25 +217,36 @@ pub type DefMap = NodeMap<PathResolution>;
215217
216218/// This is the replacement export map. It maps a module to all of the exports
217219/// within.
218- pub type ExportMap = DefIdMap < Vec < Export > > ;
220+ pub type ExportMap < Id > = DefIdMap < Vec < Export < Id > > > ;
219221
220222/// Map used to track the `use` statements within a scope, matching it with all the items in every
221223/// namespace.
222224pub type ImportMap = NodeMap < PerNS < Option < PathResolution > > > ;
223225
224226#[ derive( Copy , Clone , Debug , RustcEncodable , RustcDecodable , HashStable ) ]
225- pub struct Export {
227+ pub struct Export < Id > {
226228 /// The name of the target.
227229 pub ident : ast:: Ident ,
228230 /// The definition of the target.
229- pub def : Def ,
231+ pub def : Def < Id > ,
230232 /// The span of the target definition.
231233 pub span : Span ,
232234 /// The visibility of the export.
233235 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
234236 pub vis : ty:: Visibility ,
235237}
236238
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+
237250impl CtorKind {
238251 pub fn from_ast ( vdata : & ast:: VariantData ) -> CtorKind {
239252 match * vdata {
@@ -264,9 +277,12 @@ impl NonMacroAttrKind {
264277 }
265278}
266279
267- impl Def {
280+ impl < Id > Def < Id > {
268281 /// 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+ {
270286 self . opt_def_id ( ) . unwrap_or_else ( || {
271287 bug ! ( "attempted .def_id() on invalid def: {:?}" , self )
272288 } )
@@ -358,4 +374,43 @@ impl Def {
358374 _ => "a" ,
359375 }
360376 }
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+ }
361416}
0 commit comments