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
use crate::ast::argument_list_declaration::ArgumentListDeclaration;

use crate::ast::callable_variant::CallableVariant;
use crate::ast::doc_comment::DocComment;
use crate::ast::generics::{GenericsConstraint, GenericsDeclaration};
use crate::ast::identifier::Identifier;
use crate::ast::type_expr::TypeExpr;
use crate::ast::struct_declaration::StructDeclaration;
use crate::{declare_container_node, impl_container_node_defaults, node_child_fn, node_optional_child_fn};
use crate::format::Writer;
use crate::traits::info_provider::InfoProvider;
use crate::traits::write::Write;

declare_container_node!(FunctionDeclaration, named, availability,
    pub r#static: bool,
    pub inside_struct: bool,
    pub(crate) comment: Option<usize>,
    pub(crate) identifier: usize,
    pub(crate) generics_declaration: Option<usize>,
    pub(crate) argument_list_declaration: usize,
    pub(crate) generics_constraint: Option<usize>,
    pub(crate) return_type: usize,
);

impl_container_node_defaults!(FunctionDeclaration, named, availability);

impl FunctionDeclaration {

    node_optional_child_fn!(comment, DocComment);

    node_child_fn!(identifier, Identifier);

    node_optional_child_fn!(generics_declaration, GenericsDeclaration);

    node_child_fn!(argument_list_declaration, ArgumentListDeclaration);

    node_optional_child_fn!(generics_constraint, GenericsConstraint);

    node_child_fn!(return_type, TypeExpr);

    pub fn callable_variants<'a>(&'a self, struct_declaration: &'a StructDeclaration) -> Vec<CallableVariant<'a>> {
        let mut generics_declaration = vec![];
        let mut generics_constraint = vec![];
        if let Some(d) = struct_declaration.generics_declaration() {
            generics_declaration.push(d);
        }
        if let Some(d) = struct_declaration.generics_constraint() {
            generics_constraint.push(d);
        }
        if let Some(d) = self.generics_declaration() {
            generics_declaration.push(d);
        }
        if let Some(d) = self.generics_constraint() {
            generics_constraint.push(d);
        }
        vec![CallableVariant {
            generics_declarations: generics_declaration,
            argument_list_declaration: Some(self.argument_list_declaration()),
            generics_constraints: generics_constraint,
            pipeline_input: None,
            pipeline_output: None,
        }]
    }
}

impl InfoProvider for FunctionDeclaration {

    fn namespace_skip(&self) -> usize {
        if self.inside_struct {
            2
        } else {
            1
        }
    }
}

impl Write for FunctionDeclaration {
    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
        writer.write_children(self, self.children.values())
    }
    fn is_block_level_element(&self) -> bool {
        true
    }
}