6
6
7
7
use std:: fmt:: { self , Display } ;
8
8
9
+ use rustc_ast:: visit:: AssocCtxt ;
10
+ use rustc_ast:: { AssocItemKind , ForeignItemKind , ast} ;
11
+ use rustc_macros:: HashStable_Generic ;
12
+
9
13
use crate :: def:: DefKind ;
10
14
use crate :: { Item , ItemKind , TraitItem , TraitItemKind , hir} ;
11
15
12
- #[ derive( Copy , Clone , PartialEq , Debug ) ]
16
+ #[ derive( Copy , Clone , PartialEq , Debug , Eq , HashStable_Generic ) ]
13
17
pub enum GenericParamKind {
14
18
Type ,
15
19
Lifetime ,
16
20
Const ,
17
21
}
18
22
19
- #[ derive( Copy , Clone , PartialEq , Debug ) ]
23
+ #[ derive( Copy , Clone , PartialEq , Debug , Eq , HashStable_Generic ) ]
20
24
pub 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
22
33
Inherent ,
23
34
}
24
35
25
- #[ derive( Copy , Clone , PartialEq , Debug ) ]
36
+ #[ derive( Copy , Clone , PartialEq , Debug , Eq , HashStable_Generic ) ]
26
37
pub enum Target {
27
38
ExternCrate ,
28
39
Use ,
@@ -57,6 +68,9 @@ pub enum Target {
57
68
PatField ,
58
69
ExprField ,
59
70
WherePredicate ,
71
+ MacroCall ,
72
+ Crate ,
73
+ Delegation { mac : bool } ,
60
74
}
61
75
62
76
impl Display for Target {
@@ -98,7 +112,10 @@ impl Target {
98
112
| Target :: Param
99
113
| Target :: PatField
100
114
| Target :: ExprField
101
- | Target :: WherePredicate => false ,
115
+ | Target :: MacroCall
116
+ | Target :: Crate
117
+ | Target :: WherePredicate
118
+ | Target :: Delegation { .. } => false ,
102
119
}
103
120
}
104
121
@@ -146,6 +163,39 @@ impl Target {
146
163
}
147
164
}
148
165
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
+
149
199
pub fn from_trait_item ( trait_item : & TraitItem < ' _ > ) -> Target {
150
200
match trait_item. kind {
151
201
TraitItemKind :: Const ( ..) => Target :: AssocConst ,
@@ -183,12 +233,40 @@ impl Target {
183
233
}
184
234
}
185
235
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
+
186
264
pub fn name ( self ) -> & ' static str {
187
265
match self {
188
266
Target :: ExternCrate => "extern crate" ,
189
267
Target :: Use => "use" ,
190
- Target :: Static => "static item " ,
191
- Target :: Const => "constant item " ,
268
+ Target :: Static => "static" ,
269
+ Target :: Const => "constant" ,
192
270
Target :: Fn => "function" ,
193
271
Target :: Closure => "closure" ,
194
272
Target :: Mod => "module" ,
@@ -202,8 +280,7 @@ impl Target {
202
280
Target :: Union => "union" ,
203
281
Target :: Trait => "trait" ,
204
282
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" ,
207
284
Target :: Expression => "expression" ,
208
285
Target :: Statement => "statement" ,
209
286
Target :: Arm => "match arm" ,
@@ -212,12 +289,13 @@ impl Target {
212
289
MethodKind :: Inherent => "inherent method" ,
213
290
MethodKind :: Trait { body : false } => "required trait method" ,
214
291
MethodKind :: Trait { body : true } => "provided trait method" ,
292
+ MethodKind :: TraitImpl => "trait method in an impl block" ,
215
293
} ,
216
294
Target :: AssocTy => "associated type" ,
217
295
Target :: ForeignFn => "foreign function" ,
218
296
Target :: ForeignStatic => "foreign static item" ,
219
297
Target :: ForeignTy => "foreign type" ,
220
- Target :: GenericParam { kind, has_default : _ } => match kind {
298
+ Target :: GenericParam { kind, .. } => match kind {
221
299
GenericParamKind :: Type => "type parameter" ,
222
300
GenericParamKind :: Lifetime => "lifetime parameter" ,
223
301
GenericParamKind :: Const => "const parameter" ,
@@ -227,6 +305,60 @@ impl Target {
227
305
Target :: PatField => "pattern field" ,
228
306
Target :: ExprField => "struct field" ,
229
307
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" ,
230
362
}
231
363
}
232
364
}
0 commit comments