]> git.proxmox.com Git - rustc.git/blob - src/librustc_hir/target.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / librustc_hir / target.rs
1 //! This module implements some validity checks for attributes.
2 //! In particular it verifies that `#[inline]` and `#[repr]` attributes are
3 //! attached to items that actually support them and if there are
4 //! conflicts between multiple such attributes attached to the same
5 //! item.
6
7 use crate::hir;
8 use crate::{Item, ItemKind, TraitItem, TraitItemKind};
9
10 use std::fmt::{self, Display};
11
12 #[derive(Copy, Clone, PartialEq)]
13 pub enum MethodKind {
14 Trait { body: bool },
15 Inherent,
16 }
17
18 #[derive(Copy, Clone, PartialEq)]
19 pub enum Target {
20 ExternCrate,
21 Use,
22 Static,
23 Const,
24 Fn,
25 Closure,
26 Mod,
27 ForeignMod,
28 GlobalAsm,
29 TyAlias,
30 OpaqueTy,
31 Enum,
32 Struct,
33 Union,
34 Trait,
35 TraitAlias,
36 Impl,
37 Expression,
38 Statement,
39 AssocConst,
40 Method(MethodKind),
41 AssocTy,
42 ForeignFn,
43 ForeignStatic,
44 ForeignTy,
45 }
46
47 impl Display for Target {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 write!(
50 f,
51 "{}",
52 match *self {
53 Target::ExternCrate => "extern crate",
54 Target::Use => "use",
55 Target::Static => "static item",
56 Target::Const => "constant item",
57 Target::Fn => "function",
58 Target::Closure => "closure",
59 Target::Mod => "module",
60 Target::ForeignMod => "foreign module",
61 Target::GlobalAsm => "global asm",
62 Target::TyAlias => "type alias",
63 Target::OpaqueTy => "opaque type",
64 Target::Enum => "enum",
65 Target::Struct => "struct",
66 Target::Union => "union",
67 Target::Trait => "trait",
68 Target::TraitAlias => "trait alias",
69 Target::Impl => "item",
70 Target::Expression => "expression",
71 Target::Statement => "statement",
72 Target::AssocConst => "associated const",
73 Target::Method(_) => "method",
74 Target::AssocTy => "associated type",
75 Target::ForeignFn => "foreign function",
76 Target::ForeignStatic => "foreign static item",
77 Target::ForeignTy => "foreign type",
78 }
79 )
80 }
81 }
82
83 impl Target {
84 pub fn from_item(item: &Item<'_>) -> Target {
85 match item.kind {
86 ItemKind::ExternCrate(..) => Target::ExternCrate,
87 ItemKind::Use(..) => Target::Use,
88 ItemKind::Static(..) => Target::Static,
89 ItemKind::Const(..) => Target::Const,
90 ItemKind::Fn(..) => Target::Fn,
91 ItemKind::Mod(..) => Target::Mod,
92 ItemKind::ForeignMod(..) => Target::ForeignMod,
93 ItemKind::GlobalAsm(..) => Target::GlobalAsm,
94 ItemKind::TyAlias(..) => Target::TyAlias,
95 ItemKind::OpaqueTy(..) => Target::OpaqueTy,
96 ItemKind::Enum(..) => Target::Enum,
97 ItemKind::Struct(..) => Target::Struct,
98 ItemKind::Union(..) => Target::Union,
99 ItemKind::Trait(..) => Target::Trait,
100 ItemKind::TraitAlias(..) => Target::TraitAlias,
101 ItemKind::Impl { .. } => Target::Impl,
102 }
103 }
104
105 pub fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
106 match trait_item.kind {
107 TraitItemKind::Const(..) => Target::AssocConst,
108 TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
109 Target::Method(MethodKind::Trait { body: false })
110 }
111 TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => {
112 Target::Method(MethodKind::Trait { body: true })
113 }
114 TraitItemKind::Type(..) => Target::AssocTy,
115 }
116 }
117
118 pub fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
119 match foreign_item.kind {
120 hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
121 hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
122 hir::ForeignItemKind::Type => Target::ForeignTy,
123 }
124 }
125 }