1use sway_error::handler::ErrorEmitted;
2
3use crate::priv_prelude::*;
4
5pub mod item_abi;
6pub mod item_configurable;
7pub mod item_const;
8pub mod item_enum;
9pub mod item_fn;
10pub mod item_impl;
11pub mod item_storage;
12pub mod item_struct;
13pub mod item_trait;
14pub mod item_type_alias;
15pub mod item_use;
16
17pub type Item = Annotated<ItemKind>;
18
19impl Spanned for Item {
20 fn span(&self) -> Span {
21 match self.attributes.first() {
22 Some(attr0) => Span::join(attr0.span(), &self.value.span()),
23 None => self.value.span(),
24 }
25 }
26}
27
28#[allow(clippy::large_enum_variant)]
29#[derive(Clone, Debug, Serialize)]
30pub enum ItemKind {
31 Submodule(Submodule),
32 Use(ItemUse),
33 Struct(ItemStruct),
34 Enum(ItemEnum),
35 Fn(ItemFn),
36 Trait(ItemTrait),
37 Impl(ItemImpl),
38 Abi(ItemAbi),
39 Const(ItemConst),
40 Storage(ItemStorage),
41 Configurable(ItemConfigurable),
42 TypeAlias(ItemTypeAlias),
43 Error(Box<[Span]>, #[serde(skip_serializing)] ErrorEmitted),
45}
46
47impl ItemKind {
48 pub fn friendly_name(&self) -> &'static str {
55 use ItemKind::*;
56 match self {
57 Submodule(_) => "submodule (`mod`)",
58 Use(_) => "import (`use`)",
59 Struct(_) => "struct declaration",
60 Enum(_) => "enum declaration",
61 Fn(_) => "function declaration",
62 Trait(_) => "trait declaration",
63 Impl(item_impl) => match item_impl.trait_opt {
64 Some(_) => "ABI or trait implementation",
65 None => "inherent implementation",
66 },
67 Abi(_) => "abi declaration",
68 Const(_) => "constant declaration",
69 Storage(_) => "contract storage declaration",
70 Configurable(_) => "configurable declaration",
71 TypeAlias(_) => "type alias declaration",
72 Error(..) => "error",
73 }
74 }
75
76 pub fn friendly_name_with_acronym(&self) -> &'static str {
77 match self.friendly_name() {
78 "abi declaration" => "ABI declaration",
79 friendly_name => friendly_name,
80 }
81 }
82}
83
84impl Spanned for ItemKind {
85 fn span(&self) -> Span {
86 match self {
87 ItemKind::Submodule(item_mod) => item_mod.span(),
88 ItemKind::Use(item_use) => item_use.span(),
89 ItemKind::Struct(item_struct) => item_struct.span(),
90 ItemKind::Enum(item_enum) => item_enum.span(),
91 ItemKind::Fn(item_fn) => item_fn.span(),
92 ItemKind::Trait(item_trait) => item_trait.span(),
93 ItemKind::Impl(item_impl) => item_impl.span(),
94 ItemKind::Abi(item_abi) => item_abi.span(),
95 ItemKind::Const(item_const) => item_const.span(),
96 ItemKind::Storage(item_storage) => item_storage.span(),
97 ItemKind::Configurable(item_configurable) => item_configurable.span(),
98 ItemKind::TypeAlias(item_type_alias) => item_type_alias.span(),
99 ItemKind::Error(spans, _) => Span::join_all(spans.iter().cloned()),
100 }
101 }
102}
103
104#[derive(Clone, Debug, Serialize)]
105pub struct TypeField {
106 pub visibility: Option<PubToken>,
107 pub name: Ident,
108 pub colon_token: ColonToken,
109 pub ty: Ty,
110}
111
112impl Spanned for TypeField {
113 fn span(&self) -> Span {
114 let start = match &self.visibility {
115 Some(pub_token) => pub_token.span(),
116 None => self.name.span(),
117 };
118 let end = self.ty.span();
119 Span::join(start, &end)
120 }
121}
122
123#[derive(Clone, Debug, Serialize)]
124pub enum FnArgs {
125 Static(Punctuated<FnArg, CommaToken>),
126 NonStatic {
127 self_token: SelfToken,
128 ref_self: Option<RefToken>,
129 mutable_self: Option<MutToken>,
130 args_opt: Option<(CommaToken, Punctuated<FnArg, CommaToken>)>,
131 },
132}
133
134#[derive(Clone, Debug, Serialize)]
135pub struct FnArg {
136 pub pattern: Pattern,
137 pub colon_token: ColonToken,
138 pub ty: Ty,
139}
140
141impl Spanned for FnArg {
142 fn span(&self) -> Span {
143 Span::join(self.pattern.span(), &self.ty.span())
144 }
145}
146
147#[derive(Clone, Debug, Serialize)]
148pub struct FnSignature {
149 pub visibility: Option<PubToken>,
150 pub fn_token: FnToken,
151 pub name: Ident,
152 pub generics: Option<GenericParams>,
153 pub arguments: Parens<FnArgs>,
154 pub return_type_opt: Option<(RightArrowToken, Ty)>,
155 pub where_clause_opt: Option<WhereClause>,
156}
157
158impl Spanned for FnSignature {
159 fn span(&self) -> Span {
160 let start = match &self.visibility {
161 Some(pub_token) => pub_token.span(),
162 None => self.fn_token.span(),
163 };
164 let end = match &self.where_clause_opt {
165 Some(where_clause) => where_clause.span(),
166 None => match &self.return_type_opt {
167 Some((_right_arrow, ty)) => ty.span(),
168 None => self.arguments.span(),
169 },
170 };
171 Span::join(start, &end)
172 }
173}
174
175#[derive(Clone, Debug, Serialize)]
176pub struct TraitType {
177 pub name: Ident,
178 pub type_token: TypeToken,
179 pub eq_token_opt: Option<EqToken>,
180 pub ty_opt: Option<Ty>,
181 pub semicolon_token: SemicolonToken,
182}
183
184impl Spanned for TraitType {
185 fn span(&self) -> Span {
186 let start = self.type_token.span();
187 let end = match &self.ty_opt {
188 Some(ty_opt) => ty_opt.span(),
189 None => self.name.span(),
190 };
191 Span::join(start, &end)
192 }
193}