sway_core/language/parsed/declaration/
function.rs

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