teo_parser/ast/
function_declaration.rs

1use crate::ast::argument_list_declaration::ArgumentListDeclaration;
2
3use crate::ast::callable_variant::CallableVariant;
4use crate::ast::doc_comment::DocComment;
5use crate::ast::generics::{GenericsConstraint, GenericsDeclaration};
6use crate::ast::identifier::Identifier;
7use crate::ast::type_expr::TypeExpr;
8use crate::ast::struct_declaration::StructDeclaration;
9use crate::{declare_container_node, impl_container_node_defaults, node_child_fn, node_optional_child_fn};
10use crate::format::Writer;
11use crate::traits::info_provider::InfoProvider;
12use crate::traits::write::Write;
13
14declare_container_node!(FunctionDeclaration, named, availability,
15    pub r#static: bool,
16    pub inside_struct: bool,
17    pub(crate) comment: Option<usize>,
18    pub(crate) identifier: usize,
19    pub(crate) generics_declaration: Option<usize>,
20    pub(crate) argument_list_declaration: usize,
21    pub(crate) generics_constraint: Option<usize>,
22    pub(crate) return_type: usize,
23);
24
25impl_container_node_defaults!(FunctionDeclaration, named, availability);
26
27impl FunctionDeclaration {
28
29    node_optional_child_fn!(comment, DocComment);
30
31    node_child_fn!(identifier, Identifier);
32
33    node_optional_child_fn!(generics_declaration, GenericsDeclaration);
34
35    node_child_fn!(argument_list_declaration, ArgumentListDeclaration);
36
37    node_optional_child_fn!(generics_constraint, GenericsConstraint);
38
39    node_child_fn!(return_type, TypeExpr);
40
41    pub fn callable_variants<'a>(&'a self, struct_declaration: &'a StructDeclaration) -> Vec<CallableVariant<'a>> {
42        let mut generics_declaration = vec![];
43        let mut generics_constraint = vec![];
44        if let Some(d) = struct_declaration.generics_declaration() {
45            generics_declaration.push(d);
46        }
47        if let Some(d) = struct_declaration.generics_constraint() {
48            generics_constraint.push(d);
49        }
50        if let Some(d) = self.generics_declaration() {
51            generics_declaration.push(d);
52        }
53        if let Some(d) = self.generics_constraint() {
54            generics_constraint.push(d);
55        }
56        vec![CallableVariant {
57            generics_declarations: generics_declaration,
58            argument_list_declaration: Some(self.argument_list_declaration()),
59            generics_constraints: generics_constraint,
60            pipeline_input: None,
61            pipeline_output: None,
62        }]
63    }
64}
65
66impl InfoProvider for FunctionDeclaration {
67
68    fn namespace_skip(&self) -> usize {
69        if self.inside_struct {
70            2
71        } else {
72            1
73        }
74    }
75}
76
77impl Write for FunctionDeclaration {
78    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
79        writer.write_children(self, self.children.values())
80    }
81    fn is_block_level_element(&self) -> bool {
82        true
83    }
84}