sway_core/language/parsed/declaration/
function.rs1use crate::{
2 ast_elements::type_argument::GenericTypeArgument,
3 engine_threading::*,
4 language::{parsed::*, *},
5 transform::{self, AttributeKind},
6 type_system::*,
7};
8use sway_types::{ident::Ident, span::Span, Named, Spanned};
9
10#[derive(Debug, Clone)]
11pub enum FunctionDeclarationKind {
12 Default,
13 Entry,
14 Main,
15 Test,
16}
17
18#[derive(Debug, Clone)]
19pub struct FunctionDeclaration {
20 pub purity: Purity,
21 pub attributes: transform::Attributes,
22 pub name: Ident,
23 pub visibility: Visibility,
24 pub body: CodeBlock,
25 pub parameters: Vec<FunctionParameter>,
26 pub span: Span,
27 pub return_type: GenericTypeArgument,
28 pub type_parameters: Vec<TypeParameter>,
29 pub where_clause: Vec<(Ident, Vec<TraitConstraint>)>,
30 pub kind: FunctionDeclarationKind,
31 pub implementing_type: Option<Declaration>,
32}
33
34impl EqWithEngines for FunctionDeclaration {}
35impl PartialEqWithEngines for FunctionDeclaration {
36 fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
37 self.purity == other.purity
38 && self.attributes == other.attributes
39 && self.name == other.name
40 && self.visibility == other.visibility
41 && self.body.eq(&other.body, ctx)
42 && self.parameters.eq(&other.parameters, ctx)
43 && self.return_type.eq(&other.return_type, ctx)
44 && self.type_parameters.eq(&other.type_parameters, ctx)
45 }
46}
47
48impl DebugWithEngines for FunctionDeclaration {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &Engines) -> std::fmt::Result {
50 f.write_fmt(format_args!("{}", self.name))
51 }
52}
53
54impl Named for FunctionDeclaration {
55 fn name(&self) -> &sway_types::BaseIdent {
56 &self.name
57 }
58}
59
60impl Spanned for FunctionDeclaration {
61 fn span(&self) -> sway_types::Span {
62 self.span.clone()
63 }
64}
65
66#[derive(Debug, Clone)]
67pub struct FunctionParameter {
68 pub name: Ident,
69 pub is_reference: bool,
70 pub is_mutable: bool,
71 pub mutability_span: Span,
72 pub type_argument: GenericTypeArgument,
73}
74
75impl EqWithEngines for FunctionParameter {}
76impl PartialEqWithEngines for FunctionParameter {
77 fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
78 self.name == other.name
79 && self.is_reference == other.is_reference
80 && self.is_mutable == other.is_mutable
81 && self.mutability_span == other.mutability_span
82 && self.type_argument.eq(&other.type_argument, ctx)
83 }
84}
85
86impl FunctionDeclaration {
87 pub(crate) fn is_test(&self) -> bool {
89 self.attributes.has_any_of_kind(AttributeKind::Test)
90 }
91}