|
4 | 4 | //! conflicts between multiple such attributes attached to the same
|
5 | 5 | //! item.
|
6 | 6 |
|
| 7 | +use rustc_ast as ast; |
| 8 | +use rustc_ast::visit as ast_visit; |
| 9 | + |
7 | 10 | use crate::hir;
|
8 | 11 | use crate::{Item, ItemKind, TraitItem, TraitItemKind};
|
9 | 12 |
|
@@ -198,4 +201,41 @@ impl Target {
|
198 | 201 | Target::ExprField => "struct field",
|
199 | 202 | }
|
200 | 203 | }
|
| 204 | + |
| 205 | + pub fn from_ast_item(item: &ast::Item) -> Target { |
| 206 | + match item.kind { |
| 207 | + ast::ItemKind::ExternCrate(_) => Target::ExternCrate, |
| 208 | + ast::ItemKind::Use(_) => Target::Use, |
| 209 | + ast::ItemKind::Static(..) => Target::Static, |
| 210 | + ast::ItemKind::Const(..) => Target::Const, |
| 211 | + ast::ItemKind::Fn(_) => Target::Fn, |
| 212 | + ast::ItemKind::Mod(..) => Target::Mod, |
| 213 | + ast::ItemKind::ForeignMod(_) => Target::ForeignMod, |
| 214 | + ast::ItemKind::GlobalAsm(_) => Target::GlobalAsm, |
| 215 | + ast::ItemKind::TyAlias(_) => Target::TyAlias, |
| 216 | + ast::ItemKind::Enum(..) => Target::Enum, |
| 217 | + ast::ItemKind::Struct(..) => Target::Struct, |
| 218 | + ast::ItemKind::Union(..) => Target::Union, |
| 219 | + ast::ItemKind::Trait(_) => Target::Trait, |
| 220 | + ast::ItemKind::TraitAlias(..) => Target::TraitAlias, |
| 221 | + ast::ItemKind::Impl(_) => Target::Impl, |
| 222 | + ast::ItemKind::MacroDef(_) => Target::MacroDef, |
| 223 | + ast::ItemKind::MacCall(_) => panic!("unexpected MacCall"), |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + pub fn from_ast_assoc_item(kind: &ast::AssocItemKind, ctxt: ast_visit::AssocCtxt) -> Target { |
| 228 | + match kind { |
| 229 | + ast::AssocItemKind::Const(..) => Target::AssocConst, |
| 230 | + ast::AssocItemKind::Fn(f) => { |
| 231 | + let kind = match ctxt { |
| 232 | + ast_visit::AssocCtxt::Impl => MethodKind::Inherent, |
| 233 | + ast_visit::AssocCtxt::Trait => MethodKind::Trait { body: f.body.is_some() }, |
| 234 | + }; |
| 235 | + Target::Method(kind) |
| 236 | + } |
| 237 | + ast::AssocItemKind::Type(_) => Target::AssocTy, |
| 238 | + ast::AssocItemKind::MacCall(_) => panic!("unexpected MacCall"), |
| 239 | + } |
| 240 | + } |
201 | 241 | }
|
0 commit comments