66
77use std:: fmt:: { self , Display } ;
88
9+ use rustc_ast:: visit:: AssocCtxt ;
10+ use rustc_ast:: { AssocItemKind , ForeignItemKind , ast} ;
11+ use rustc_macros:: HashStable_Generic ;
12+
913use crate :: def:: DefKind ;
1014use crate :: { Item , ItemKind , TraitItem , TraitItemKind , hir} ;
1115
12- #[ derive( Copy , Clone , PartialEq , Debug ) ]
16+ #[ derive( Copy , Clone , PartialEq , Debug , Eq , HashStable_Generic ) ]
1317pub enum GenericParamKind {
1418 Type ,
1519 Lifetime ,
1620 Const ,
1721}
1822
19- #[ derive( Copy , Clone , PartialEq , Debug ) ]
23+ #[ derive( Copy , Clone , PartialEq , Debug , Eq , HashStable_Generic ) ]
2024pub enum MethodKind {
21- Trait { body : bool } ,
25+ /// Method in a `trait Trait` block
26+ Trait {
27+ /// Whether a default is provided for this method
28+ body : bool ,
29+ } ,
30+ /// Method in a `impl Trait for Type` block
31+ TraitImpl ,
32+ /// Method in a `impl Type` block
2233 Inherent ,
2334}
2435
25- #[ derive( Copy , Clone , PartialEq , Debug ) ]
36+ #[ derive( Copy , Clone , PartialEq , Debug , Eq , HashStable_Generic ) ]
2637pub enum Target {
2738 ExternCrate ,
2839 Use ,
@@ -57,6 +68,9 @@ pub enum Target {
5768 PatField ,
5869 ExprField ,
5970 WherePredicate ,
71+ MacroCall ,
72+ Crate ,
73+ Delegation { mac : bool } ,
6074}
6175
6276impl Display for Target {
@@ -98,7 +112,10 @@ impl Target {
98112 | Target :: Param
99113 | Target :: PatField
100114 | Target :: ExprField
101- | Target :: WherePredicate => false ,
115+ | Target :: MacroCall
116+ | Target :: Crate
117+ | Target :: WherePredicate
118+ | Target :: Delegation { .. } => false ,
102119 }
103120 }
104121
@@ -146,6 +163,39 @@ impl Target {
146163 }
147164 }
148165
166+ pub fn from_ast_item ( item : & ast:: Item ) -> Target {
167+ match item. kind {
168+ ast:: ItemKind :: ExternCrate ( ..) => Target :: ExternCrate ,
169+ ast:: ItemKind :: Use ( ..) => Target :: Use ,
170+ ast:: ItemKind :: Static { .. } => Target :: Static ,
171+ ast:: ItemKind :: Const ( ..) => Target :: Const ,
172+ ast:: ItemKind :: Fn { .. } => Target :: Fn ,
173+ ast:: ItemKind :: Mod ( ..) => Target :: Mod ,
174+ ast:: ItemKind :: ForeignMod { .. } => Target :: ForeignMod ,
175+ ast:: ItemKind :: GlobalAsm { .. } => Target :: GlobalAsm ,
176+ ast:: ItemKind :: TyAlias ( ..) => Target :: TyAlias ,
177+ ast:: ItemKind :: Enum ( ..) => Target :: Enum ,
178+ ast:: ItemKind :: Struct ( ..) => Target :: Struct ,
179+ ast:: ItemKind :: Union ( ..) => Target :: Union ,
180+ ast:: ItemKind :: Trait ( ..) => Target :: Trait ,
181+ ast:: ItemKind :: TraitAlias ( ..) => Target :: TraitAlias ,
182+ ast:: ItemKind :: Impl ( ref i) => Target :: Impl { of_trait : i. of_trait . is_some ( ) } ,
183+ ast:: ItemKind :: MacCall ( ..) => Target :: MacroCall ,
184+ ast:: ItemKind :: MacroDef ( ..) => Target :: MacroDef ,
185+ ast:: ItemKind :: Delegation ( ..) => Target :: Delegation { mac : false } ,
186+ ast:: ItemKind :: DelegationMac ( ..) => Target :: Delegation { mac : true } ,
187+ }
188+ }
189+
190+ pub fn from_foreign_item_kind ( kind : & ast:: ForeignItemKind ) -> Target {
191+ match kind {
192+ ForeignItemKind :: Static ( _) => Target :: ForeignStatic ,
193+ ForeignItemKind :: Fn ( _) => Target :: ForeignFn ,
194+ ForeignItemKind :: TyAlias ( _) => Target :: ForeignTy ,
195+ ForeignItemKind :: MacCall ( _) => Target :: MacroCall ,
196+ }
197+ }
198+
149199 pub fn from_trait_item ( trait_item : & TraitItem < ' _ > ) -> Target {
150200 match trait_item. kind {
151201 TraitItemKind :: Const ( ..) => Target :: AssocConst ,
@@ -183,12 +233,40 @@ impl Target {
183233 }
184234 }
185235
236+ pub fn from_assoc_item_kind ( kind : & ast:: AssocItemKind , assoc_ctxt : AssocCtxt ) -> Target {
237+ match kind {
238+ AssocItemKind :: Const ( _) => Target :: AssocConst ,
239+ AssocItemKind :: Fn ( f) => Target :: Method ( match assoc_ctxt {
240+ AssocCtxt :: Trait => MethodKind :: Trait { body : f. body . is_some ( ) } ,
241+ AssocCtxt :: Impl { of_trait } => {
242+ if of_trait {
243+ MethodKind :: TraitImpl
244+ } else {
245+ MethodKind :: Inherent
246+ }
247+ }
248+ } ) ,
249+ AssocItemKind :: Type ( _) => Target :: AssocTy ,
250+ AssocItemKind :: Delegation ( _) => Target :: Delegation { mac : false } ,
251+ AssocItemKind :: DelegationMac ( _) => Target :: Delegation { mac : true } ,
252+ AssocItemKind :: MacCall ( _) => Target :: MacroCall ,
253+ }
254+ }
255+
256+ pub fn from_expr ( expr : & ast:: Expr ) -> Self {
257+ match & expr. kind {
258+ ast:: ExprKind :: Closure ( ..) | ast:: ExprKind :: Gen ( ..) => Self :: Closure ,
259+ ast:: ExprKind :: Paren ( e) => Self :: from_expr ( & e) ,
260+ _ => Self :: Expression ,
261+ }
262+ }
263+
186264 pub fn name ( self ) -> & ' static str {
187265 match self {
188266 Target :: ExternCrate => "extern crate" ,
189267 Target :: Use => "use" ,
190- Target :: Static => "static item " ,
191- Target :: Const => "constant item " ,
268+ Target :: Static => "static" ,
269+ Target :: Const => "constant" ,
192270 Target :: Fn => "function" ,
193271 Target :: Closure => "closure" ,
194272 Target :: Mod => "module" ,
@@ -202,8 +280,7 @@ impl Target {
202280 Target :: Union => "union" ,
203281 Target :: Trait => "trait" ,
204282 Target :: TraitAlias => "trait alias" ,
205- Target :: Impl { of_trait : false } => "inherent implementation block" ,
206- Target :: Impl { of_trait : true } => "trait implementation block" ,
283+ Target :: Impl { .. } => "implementation block" ,
207284 Target :: Expression => "expression" ,
208285 Target :: Statement => "statement" ,
209286 Target :: Arm => "match arm" ,
@@ -212,12 +289,13 @@ impl Target {
212289 MethodKind :: Inherent => "inherent method" ,
213290 MethodKind :: Trait { body : false } => "required trait method" ,
214291 MethodKind :: Trait { body : true } => "provided trait method" ,
292+ MethodKind :: TraitImpl => "trait method in an impl block" ,
215293 } ,
216294 Target :: AssocTy => "associated type" ,
217295 Target :: ForeignFn => "foreign function" ,
218296 Target :: ForeignStatic => "foreign static item" ,
219297 Target :: ForeignTy => "foreign type" ,
220- Target :: GenericParam { kind, has_default : _ } => match kind {
298+ Target :: GenericParam { kind, .. } => match kind {
221299 GenericParamKind :: Type => "type parameter" ,
222300 GenericParamKind :: Lifetime => "lifetime parameter" ,
223301 GenericParamKind :: Const => "const parameter" ,
@@ -227,6 +305,60 @@ impl Target {
227305 Target :: PatField => "pattern field" ,
228306 Target :: ExprField => "struct field" ,
229307 Target :: WherePredicate => "where predicate" ,
308+ Target :: MacroCall => "macro call" ,
309+ Target :: Crate => "crate" ,
310+ Target :: Delegation { .. } => "delegation" ,
311+ }
312+ }
313+
314+ pub fn plural_name ( self ) -> & ' static str {
315+ match self {
316+ Target :: ExternCrate => "extern crates" ,
317+ Target :: Use => "use statements" ,
318+ Target :: Static => "statics" ,
319+ Target :: Const => "constants" ,
320+ Target :: Fn => "functions" ,
321+ Target :: Closure => "closures" ,
322+ Target :: Mod => "modules" ,
323+ Target :: ForeignMod => "foreign modules" ,
324+ Target :: GlobalAsm => "global asms" ,
325+ Target :: TyAlias => "type aliases" ,
326+ Target :: Enum => "enums" ,
327+ Target :: Variant => "enum variants" ,
328+ Target :: Struct => "structs" ,
329+ Target :: Field => "struct fields" ,
330+ Target :: Union => "unions" ,
331+ Target :: Trait => "traits" ,
332+ Target :: TraitAlias => "trait aliases" ,
333+ Target :: Impl { of_trait : false } => "inherent impl blocks" ,
334+ Target :: Impl { of_trait : true } => "trait impl blocks" ,
335+ Target :: Expression => "expressions" ,
336+ Target :: Statement => "statements" ,
337+ Target :: Arm => "match arms" ,
338+ Target :: AssocConst => "associated consts" ,
339+ Target :: Method ( kind) => match kind {
340+ MethodKind :: Inherent => "inherent methods" ,
341+ MethodKind :: Trait { body : false } => "required trait methods" ,
342+ MethodKind :: Trait { body : true } => "provided trait methods" ,
343+ MethodKind :: TraitImpl => "trait methods in impl blocks" ,
344+ } ,
345+ Target :: AssocTy => "associated types" ,
346+ Target :: ForeignFn => "foreign functions" ,
347+ Target :: ForeignStatic => "foreign statics" ,
348+ Target :: ForeignTy => "foreign types" ,
349+ Target :: GenericParam { kind, has_default : _ } => match kind {
350+ GenericParamKind :: Type => "type parameters" ,
351+ GenericParamKind :: Lifetime => "lifetime parameters" ,
352+ GenericParamKind :: Const => "const parameters" ,
353+ } ,
354+ Target :: MacroDef => "macro defs" ,
355+ Target :: Param => "function params" ,
356+ Target :: PatField => "pattern fields" ,
357+ Target :: ExprField => "struct fields" ,
358+ Target :: WherePredicate => "where predicates" ,
359+ Target :: MacroCall => "macro calls" ,
360+ Target :: Crate => "crates" ,
361+ Target :: Delegation { .. } => "delegations" ,
230362 }
231363 }
232364}
0 commit comments