sway_core/language/parsed/declaration/
function.rs

1use crate::{
2    ast_elements::type_parameter::ConstGenericParameter,
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::AttributesMap,
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: TypeArgument,
28    pub type_parameters: Vec<TypeParameter>,
29    pub const_generic_parameters: Vec<ConstGenericParameter>,
30    pub where_clause: Vec<(Ident, Vec<TraitConstraint>)>,
31    pub kind: FunctionDeclarationKind,
32    pub implementing_type: Option<Declaration>,
33}
34
35impl EqWithEngines for FunctionDeclaration {}
36impl PartialEqWithEngines for FunctionDeclaration {
37    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
38        self.purity == other.purity
39            && self.attributes == other.attributes
40            && self.name == other.name
41            && self.visibility == other.visibility
42            && self.body.eq(&other.body, ctx)
43            && self.parameters.eq(&other.parameters, ctx)
44            && self.return_type.eq(&other.return_type, ctx)
45            && self.type_parameters.eq(&other.type_parameters, ctx)
46    }
47}
48
49impl DebugWithEngines for FunctionDeclaration {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>, _engines: &Engines) -> std::fmt::Result {
51        f.write_fmt(format_args!("{}", self.name))
52    }
53}
54
55impl Named for FunctionDeclaration {
56    fn name(&self) -> &sway_types::BaseIdent {
57        &self.name
58    }
59}
60
61impl Spanned for FunctionDeclaration {
62    fn span(&self) -> sway_types::Span {
63        self.span.clone()
64    }
65}
66
67#[derive(Debug, Clone)]
68pub struct FunctionParameter {
69    pub name: Ident,
70    pub is_reference: bool,
71    pub is_mutable: bool,
72    pub mutability_span: Span,
73    pub type_argument: TypeArgument,
74}
75
76impl EqWithEngines for FunctionParameter {}
77impl PartialEqWithEngines for FunctionParameter {
78    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
79        self.name == other.name
80            && self.is_reference == other.is_reference
81            && self.is_mutable == other.is_mutable
82            && self.mutability_span == other.mutability_span
83            && self.type_argument.eq(&other.type_argument, ctx)
84    }
85}
86
87impl FunctionDeclaration {
88    /// Checks if this `FunctionDeclaration` is a test.
89    pub(crate) fn is_test(&self) -> bool {
90        self.attributes
91            .keys()
92            .any(|k| matches!(k, AttributeKind::Test))
93    }
94}