1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use sha2::{Digest, Sha256};
use sway_types::{Ident, JsonABIFunction, JsonTypeApplication, JsonTypeDeclaration, Span, Spanned};
use crate::{
declaration_engine::*,
error::*,
language::{parsed, ty::*, Inline, Purity, Visibility},
transform,
type_system::*,
};
use sway_types::constants::{INLINE_ALWAYS_NAME, INLINE_NEVER_NAME};
#[derive(Clone, Debug, Eq)]
pub struct TyFunctionDeclaration {
pub name: Ident,
pub body: TyCodeBlock,
pub parameters: Vec<TyFunctionParameter>,
pub span: Span,
pub attributes: transform::AttributesMap,
pub return_type: TypeId,
pub initial_return_type: TypeId,
pub type_parameters: Vec<TypeParameter>,
pub return_type_span: Span,
pub visibility: Visibility,
pub is_contract_call: bool,
pub purity: Purity,
}
impl From<&TyFunctionDeclaration> for TyAstNode {
fn from(o: &TyFunctionDeclaration) -> Self {
let span = o.span.clone();
TyAstNode {
content: TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration(
de_insert_function(o.clone()),
)),
span,
}
}
}
impl PartialEq for TyFunctionDeclaration {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.body == other.body
&& self.parameters == other.parameters
&& look_up_type_id(self.return_type) == look_up_type_id(other.return_type)
&& self.type_parameters == other.type_parameters
&& self.visibility == other.visibility
&& self.is_contract_call == other.is_contract_call
&& self.purity == other.purity
}
}
impl CopyTypes for TyFunctionDeclaration {
fn copy_types_inner(&mut self, type_mapping: &TypeMapping) {
self.type_parameters
.iter_mut()
.for_each(|x| x.copy_types(type_mapping));
self.parameters
.iter_mut()
.for_each(|x| x.copy_types(type_mapping));
self.return_type.copy_types(type_mapping);
self.body.copy_types(type_mapping);
}
}
impl ReplaceSelfType for TyFunctionDeclaration {
fn replace_self_type(&mut self, self_type: TypeId) {
self.type_parameters
.iter_mut()
.for_each(|x| x.replace_self_type(self_type));
self.parameters
.iter_mut()
.for_each(|x| x.replace_self_type(self_type));
self.return_type.replace_self_type(self_type);
self.body.replace_self_type(self_type);
}
}
impl ReplaceDecls for TyFunctionDeclaration {
fn replace_decls_inner(&mut self, decl_mapping: &DeclMapping) {
self.body.replace_decls(decl_mapping);
}
}
impl Spanned for TyFunctionDeclaration {
fn span(&self) -> Span {
self.span.clone()
}
}
impl MonomorphizeHelper for TyFunctionDeclaration {
fn type_parameters(&self) -> &[TypeParameter] {
&self.type_parameters
}
fn name(&self) -> &Ident {
&self.name
}
}
impl UnconstrainedTypeParameters for TyFunctionDeclaration {
fn type_parameter_is_unconstrained(&self, type_parameter: &TypeParameter) -> bool {
let type_parameter_info = look_up_type_id(type_parameter.type_id);
if self
.type_parameters
.iter()
.map(|type_param| look_up_type_id(type_param.type_id))
.any(|x| x == type_parameter_info)
{
return false;
}
if self
.parameters
.iter()
.map(|param| look_up_type_id(param.type_id))
.any(|x| x == type_parameter_info)
{
return true;
}
if look_up_type_id(self.return_type) == type_parameter_info {
return true;
}
false
}
}
impl TyFunctionDeclaration {
pub(crate) fn error(decl: parsed::FunctionDeclaration) -> TyFunctionDeclaration {
let parsed::FunctionDeclaration {
name,
return_type,
span,
return_type_span,
visibility,
purity,
..
} = decl;
let initial_return_type = insert_type(return_type);
TyFunctionDeclaration {
purity,
name,
body: TyCodeBlock {
contents: Default::default(),
},
span,
attributes: Default::default(),
is_contract_call: false,
return_type_span,
parameters: Default::default(),
visibility,
return_type: initial_return_type,
initial_return_type,
type_parameters: Default::default(),
}
}
pub(crate) fn parameters_span(&self) -> Span {
if !self.parameters.is_empty() {
self.parameters.iter().fold(
self.parameters[0].name.span(),
|acc, TyFunctionParameter { type_span, .. }| Span::join(acc, type_span.clone()),
)
} else {
self.name.span()
}
}
pub fn to_fn_selector_value_untruncated(&self) -> CompileResult<Vec<u8>> {
let mut errors = vec![];
let mut warnings = vec![];
let mut hasher = Sha256::new();
let data = check!(
self.to_selector_name(),
return err(warnings, errors),
warnings,
errors
);
hasher.update(data);
let hash = hasher.finalize();
ok(hash.to_vec(), warnings, errors)
}
pub fn to_fn_selector_value(&self) -> CompileResult<[u8; 4]> {
let mut errors = vec![];
let mut warnings = vec![];
let hash = check!(
self.to_fn_selector_value_untruncated(),
return err(warnings, errors),
warnings,
errors
);
let mut buf = [0u8; 4];
buf.copy_from_slice(&hash[..4]);
ok(buf, warnings, errors)
}
pub fn to_selector_name(&self) -> CompileResult<String> {
let mut errors = vec![];
let mut warnings = vec![];
let named_params = self
.parameters
.iter()
.map(
|TyFunctionParameter {
type_id, type_span, ..
}| {
to_typeinfo(*type_id, type_span)
.expect("unreachable I think?")
.to_selector_name(type_span)
},
)
.filter_map(|name| name.ok(&mut warnings, &mut errors))
.collect::<Vec<String>>();
ok(
format!("{}({})", self.name.as_str(), named_params.join(","),),
warnings,
errors,
)
}
pub(crate) fn generate_json_abi_function(
&self,
types: &mut Vec<JsonTypeDeclaration>,
) -> JsonABIFunction {
let input_types = self
.parameters
.iter()
.map(|x| JsonTypeDeclaration {
type_id: *x.initial_type_id,
type_field: x.initial_type_id.get_json_type_str(x.type_id),
components: x.initial_type_id.get_json_type_components(types, x.type_id),
type_parameters: x.type_id.get_json_type_parameters(types, x.type_id),
})
.collect::<Vec<_>>();
let output_type = JsonTypeDeclaration {
type_id: *self.initial_return_type,
type_field: self.initial_return_type.get_json_type_str(self.return_type),
components: self
.return_type
.get_json_type_components(types, self.return_type),
type_parameters: self
.return_type
.get_json_type_parameters(types, self.return_type),
};
types.extend(input_types);
types.push(output_type);
JsonABIFunction {
name: self.name.as_str().to_string(),
inputs: self
.parameters
.iter()
.map(|x| JsonTypeApplication {
name: x.name.to_string(),
type_id: *x.initial_type_id,
type_arguments: x.initial_type_id.get_json_type_arguments(types, x.type_id),
})
.collect(),
output: JsonTypeApplication {
name: "".to_string(),
type_id: *self.initial_return_type,
type_arguments: self
.initial_return_type
.get_json_type_arguments(types, self.return_type),
},
}
}
pub fn is_main_entry(&self) -> bool {
self.name.as_str() == sway_types::constants::DEFAULT_ENTRY_POINT_FN_NAME
}
pub fn is_test(&self) -> bool {
self.attributes
.contains_key(&transform::AttributeKind::Test)
}
pub fn inline(&self) -> Option<Inline> {
match self
.attributes
.get(&transform::AttributeKind::Inline)?
.last()?
.args
.first()?
.as_str()
{
INLINE_NEVER_NAME => Some(Inline::Never),
INLINE_ALWAYS_NAME => Some(Inline::Always),
_ => None,
}
}
pub fn is_entry(&self) -> bool {
self.is_main_entry() || self.is_test()
}
}
#[derive(Debug, Clone, Eq)]
pub struct TyFunctionParameter {
pub name: Ident,
pub is_reference: bool,
pub is_mutable: bool,
pub mutability_span: Span,
pub type_id: TypeId,
pub initial_type_id: TypeId,
pub type_span: Span,
}
impl PartialEq for TyFunctionParameter {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& look_up_type_id(self.type_id) == look_up_type_id(other.type_id)
&& self.is_mutable == other.is_mutable
}
}
impl CopyTypes for TyFunctionParameter {
fn copy_types_inner(&mut self, type_mapping: &TypeMapping) {
self.type_id.copy_types(type_mapping);
}
}
impl ReplaceSelfType for TyFunctionParameter {
fn replace_self_type(&mut self, self_type: TypeId) {
self.type_id.replace_self_type(self_type);
}
}
impl TyFunctionParameter {
pub fn is_self(&self) -> bool {
self.name.as_str() == "self"
}
}