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
+
9
12
use crate :: def:: DefKind ;
10
13
use crate :: { Item , ItemKind , TraitItem , TraitItemKind , hir} ;
11
14
12
- #[ derive( Copy , Clone , PartialEq , Debug ) ]
15
+ #[ derive( Copy , Clone , PartialEq , Debug , Eq ) ]
13
16
pub enum GenericParamKind {
14
17
Type ,
15
18
Lifetime ,
16
19
Const ,
17
20
}
18
21
19
- #[ derive( Copy , Clone , PartialEq , Debug ) ]
22
+ #[ derive( Copy , Clone , PartialEq , Debug , Eq ) ]
20
23
pub enum MethodKind {
21
- Trait { body : bool } ,
24
+ /// Method in a `trait Trait` block
25
+ Trait {
26
+ /// Whether a default is provided for this method
27
+ body : bool ,
28
+ } ,
29
+ /// Method in a `impl Trait for Type` block
30
+ TraitImpl ,
31
+ /// Method in a `impl Type` block
22
32
Inherent ,
23
33
}
24
34
25
- #[ derive( Copy , Clone , PartialEq , Debug ) ]
35
+ #[ derive( Copy , Clone , PartialEq , Debug , Eq ) ]
26
36
pub enum Target {
27
37
ExternCrate ,
28
38
Use ,
@@ -57,6 +67,9 @@ pub enum Target {
57
67
PatField ,
58
68
ExprField ,
59
69
WherePredicate ,
70
+ MacroCall ,
71
+ Crate ,
72
+ Delegation ,
60
73
}
61
74
62
75
impl Display for Target {
@@ -98,7 +111,10 @@ impl Target {
98
111
| Target :: Param
99
112
| Target :: PatField
100
113
| Target :: ExprField
101
- | Target :: WherePredicate => false ,
114
+ | Target :: MacroCall
115
+ | Target :: Crate
116
+ | Target :: WherePredicate
117
+ | Target :: Delegation => false ,
102
118
}
103
119
}
104
120
@@ -146,6 +162,39 @@ impl Target {
146
162
}
147
163
}
148
164
165
+ pub fn from_ast_item ( item : & ast:: Item ) -> Target {
166
+ match item. kind {
167
+ ast:: ItemKind :: ExternCrate ( ..) => Target :: ExternCrate ,
168
+ ast:: ItemKind :: Use ( ..) => Target :: Use ,
169
+ ast:: ItemKind :: Static { .. } => Target :: Static ,
170
+ ast:: ItemKind :: Const ( ..) => Target :: Const ,
171
+ ast:: ItemKind :: Fn { .. } => Target :: Fn ,
172
+ ast:: ItemKind :: Mod ( ..) => Target :: Mod ,
173
+ ast:: ItemKind :: ForeignMod { .. } => Target :: ForeignMod ,
174
+ ast:: ItemKind :: GlobalAsm { .. } => Target :: GlobalAsm ,
175
+ ast:: ItemKind :: TyAlias ( ..) => Target :: TyAlias ,
176
+ ast:: ItemKind :: Enum ( ..) => Target :: Enum ,
177
+ ast:: ItemKind :: Struct ( ..) => Target :: Struct ,
178
+ ast:: ItemKind :: Union ( ..) => Target :: Union ,
179
+ ast:: ItemKind :: Trait ( ..) => Target :: Trait ,
180
+ ast:: ItemKind :: TraitAlias ( ..) => Target :: TraitAlias ,
181
+ ast:: ItemKind :: Impl ( ref i) => Target :: Impl { of_trait : i. of_trait . is_some ( ) } ,
182
+ ast:: ItemKind :: MacCall ( ..) => Target :: MacroCall ,
183
+ ast:: ItemKind :: MacroDef ( ..) => Target :: MacroDef ,
184
+ ast:: ItemKind :: Delegation ( ..) => Target :: Fn ,
185
+ ast:: ItemKind :: DelegationMac ( ..) => panic ! ( "macros should be expanded" ) ,
186
+ }
187
+ }
188
+
189
+ pub fn from_foreign_item_kind ( kind : & ast:: ForeignItemKind ) -> Target {
190
+ match kind {
191
+ ForeignItemKind :: Static ( _) => Target :: ForeignStatic ,
192
+ ForeignItemKind :: Fn ( _) => Target :: ForeignFn ,
193
+ ForeignItemKind :: TyAlias ( _) => Target :: ForeignTy ,
194
+ ForeignItemKind :: MacCall ( _) => panic ! ( "macros should be expanded" ) ,
195
+ }
196
+ }
197
+
149
198
pub fn from_trait_item ( trait_item : & TraitItem < ' _ > ) -> Target {
150
199
match trait_item. kind {
151
200
TraitItemKind :: Const ( ..) => Target :: AssocConst ,
@@ -183,12 +232,39 @@ impl Target {
183
232
}
184
233
}
185
234
235
+ pub fn from_assoc_item_kind ( kind : & ast:: AssocItemKind , assoc_ctxt : AssocCtxt ) -> Target {
236
+ match kind {
237
+ AssocItemKind :: Const ( _) => Target :: AssocConst ,
238
+ AssocItemKind :: Fn ( f) => Target :: Method ( match assoc_ctxt {
239
+ AssocCtxt :: Trait => MethodKind :: Trait { body : f. body . is_some ( ) } ,
240
+ AssocCtxt :: Impl { of_trait } => {
241
+ if of_trait {
242
+ MethodKind :: TraitImpl
243
+ } else {
244
+ MethodKind :: Inherent
245
+ }
246
+ }
247
+ } ) ,
248
+ AssocItemKind :: Type ( _) => Target :: AssocTy ,
249
+ AssocItemKind :: Delegation ( _) => Target :: Delegation ,
250
+ _ => unreachable ! ( ) ,
251
+ }
252
+ }
253
+
254
+ pub fn from_expr ( expr : & ast:: Expr ) -> Self {
255
+ match & expr. kind {
256
+ ast:: ExprKind :: Closure ( ..) | ast:: ExprKind :: Gen ( ..) => Self :: Closure ,
257
+ ast:: ExprKind :: Paren ( e) => Self :: from_expr ( & e) ,
258
+ _ => Self :: Expression ,
259
+ }
260
+ }
261
+
186
262
pub fn name ( self ) -> & ' static str {
187
263
match self {
188
264
Target :: ExternCrate => "extern crate" ,
189
265
Target :: Use => "use" ,
190
- Target :: Static => "static item " ,
191
- Target :: Const => "constant item " ,
266
+ Target :: Static => "static" ,
267
+ Target :: Const => "constant" ,
192
268
Target :: Fn => "function" ,
193
269
Target :: Closure => "closure" ,
194
270
Target :: Mod => "module" ,
@@ -202,8 +278,7 @@ impl Target {
202
278
Target :: Union => "union" ,
203
279
Target :: Trait => "trait" ,
204
280
Target :: TraitAlias => "trait alias" ,
205
- Target :: Impl { of_trait : false } => "inherent implementation block" ,
206
- Target :: Impl { of_trait : true } => "trait implementation block" ,
281
+ Target :: Impl { .. } => "implementation block" ,
207
282
Target :: Expression => "expression" ,
208
283
Target :: Statement => "statement" ,
209
284
Target :: Arm => "match arm" ,
@@ -212,12 +287,13 @@ impl Target {
212
287
MethodKind :: Inherent => "inherent method" ,
213
288
MethodKind :: Trait { body : false } => "required trait method" ,
214
289
MethodKind :: Trait { body : true } => "provided trait method" ,
290
+ MethodKind :: TraitImpl => "trait method in an impl block" ,
215
291
} ,
216
292
Target :: AssocTy => "associated type" ,
217
293
Target :: ForeignFn => "foreign function" ,
218
294
Target :: ForeignStatic => "foreign static item" ,
219
295
Target :: ForeignTy => "foreign type" ,
220
- Target :: GenericParam { kind, has_default : _ } => match kind {
296
+ Target :: GenericParam { kind, .. } => match kind {
221
297
GenericParamKind :: Type => "type parameter" ,
222
298
GenericParamKind :: Lifetime => "lifetime parameter" ,
223
299
GenericParamKind :: Const => "const parameter" ,
@@ -227,6 +303,60 @@ impl Target {
227
303
Target :: PatField => "pattern field" ,
228
304
Target :: ExprField => "struct field" ,
229
305
Target :: WherePredicate => "where predicate" ,
306
+ Target :: MacroCall => "macro call" ,
307
+ Target :: Crate => "crate" ,
308
+ Target :: Delegation => "delegation" ,
309
+ }
310
+ }
311
+
312
+ pub fn plural_name ( self ) -> & ' static str {
313
+ match self {
314
+ Target :: ExternCrate => "extern crates" ,
315
+ Target :: Use => "use statements" ,
316
+ Target :: Static => "statics" ,
317
+ Target :: Const => "constants" ,
318
+ Target :: Fn => "functions" ,
319
+ Target :: Closure => "closures" ,
320
+ Target :: Mod => "modules" ,
321
+ Target :: ForeignMod => "foreign modules" ,
322
+ Target :: GlobalAsm => "global asms" ,
323
+ Target :: TyAlias => "type aliases" ,
324
+ Target :: Enum => "enums" ,
325
+ Target :: Variant => "enum variants" ,
326
+ Target :: Struct => "structs" ,
327
+ Target :: Field => "struct fields" ,
328
+ Target :: Union => "unions" ,
329
+ Target :: Trait => "traits" ,
330
+ Target :: TraitAlias => "trait aliases" ,
331
+ Target :: Impl { of_trait : false } => "inherent impl blocks" ,
332
+ Target :: Impl { of_trait : true } => "trait impl blocks" ,
333
+ Target :: Expression => "expressions" ,
334
+ Target :: Statement => "statements" ,
335
+ Target :: Arm => "match arms" ,
336
+ Target :: AssocConst => "associated consts" ,
337
+ Target :: Method ( kind) => match kind {
338
+ MethodKind :: Inherent => "inherent methods" ,
339
+ MethodKind :: Trait { body : false } => "required trait methods" ,
340
+ MethodKind :: Trait { body : true } => "provided trait methods" ,
341
+ MethodKind :: TraitImpl => "trait methods in impl blocks" ,
342
+ } ,
343
+ Target :: AssocTy => "associated types" ,
344
+ Target :: ForeignFn => "foreign functions" ,
345
+ Target :: ForeignStatic => "foreign statics" ,
346
+ Target :: ForeignTy => "foreign types" ,
347
+ Target :: GenericParam { kind, has_default : _ } => match kind {
348
+ GenericParamKind :: Type => "type parameters" ,
349
+ GenericParamKind :: Lifetime => "lifetime parameters" ,
350
+ GenericParamKind :: Const => "const parameters" ,
351
+ } ,
352
+ Target :: MacroDef => "macro defs" ,
353
+ Target :: Param => "function params" ,
354
+ Target :: PatField => "pattern fields" ,
355
+ Target :: ExprField => "struct fields" ,
356
+ Target :: WherePredicate => "where predicates" ,
357
+ Target :: MacroCall => "macro calls" ,
358
+ Target :: Crate => "crates" ,
359
+ Target :: Delegation => "delegations" ,
230
360
}
231
361
}
232
362
}
0 commit comments