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
use derive_more::From;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "size-of")]
use size_of::SizeOf;

use kodept_core::structure::rlt;
use kodept_core::structure::span::CodeHolder;

use crate::{Body, node, Parameter, Type, TypedParameter, wrapper};
use crate::graph::{GenericASTNode, NodeId};
use crate::graph::{Identity, SyntaxTreeBuilder};
use crate::traits::Linker;
use crate::traits::PopulateTree;

wrapper! {
    #[derive(Debug, PartialEq, From)]
    #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
    pub wrapper FunctionDeclaration {
        bodied(BodiedFunctionDeclaration) = GenericASTNode::BodiedFunction(x) => Some(x),
        abstract(AbstractFunctionDeclaration) = GenericASTNode::AbstractFunction(x) => Some(x),
    }
}

node! {
    #[derive(Debug, PartialEq)]
    #[cfg_attr(feature = "size-of", derive(SizeOf))]
    #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
    pub struct BodiedFunctionDeclaration {
        pub name: String,;
        pub parameters: Vec<Parameter>,
        pub return_type: Option<Type>,
        pub body: Identity<Body>,
    }
}

node! {
    #[derive(Debug, PartialEq)]
    #[cfg_attr(feature = "size-of", derive(SizeOf))]
    #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
    pub struct AbstractFunctionDeclaration {
        pub name: String,;
        pub parameters: Vec<TypedParameter>,
        pub return_type: Option<Type>,
    }
}

impl PopulateTree for rlt::BodiedFunction {
    type Output = BodiedFunctionDeclaration;

    fn convert<'a>(
        &'a self,
        builder: &mut SyntaxTreeBuilder,
        context: &mut (impl Linker<'a> + CodeHolder),
    ) -> NodeId<Self::Output> {
        builder
            .add_node(BodiedFunctionDeclaration {
                id: Default::default(),
                name: context.get_chunk_located(&self.id).to_string(),
            })
            .with_children_from(self.return_type.as_ref().map(|x| &x.1), context)
            .with_children_from(self.params.iter().flat_map(|x| x.inner.as_ref()), context)
            .with_children_from([self.body.as_ref()], context)
            .with_rlt(context, self)
            .id()
    }
}