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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::sync::Arc;

use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::Span;

use crate::{
    decl_engine::{DeclEngine, DeclRef, DeclRefFunction},
    language::ModName,
    language::{ty::*, HasModule, HasSubmodules},
    semantic_analysis::namespace,
    transform::{self, AllowDeprecatedState},
    Engines,
};

#[derive(Clone, Debug)]
pub struct TyModule {
    pub span: Span,
    pub submodules: Vec<(ModName, TySubmodule)>,
    pub namespace: namespace::Namespace,
    pub all_nodes: Vec<TyAstNode>,
    pub attributes: transform::AttributesMap,
}

#[derive(Clone, Debug)]
pub struct TySubmodule {
    pub module: TyModule,
    pub mod_name_span: Span,
}

/// Iterator type for iterating over submodules.
///
/// Used rather than `impl Iterator` to enable recursive submodule iteration.
pub struct SubmodulesRecursive<'module> {
    submods: std::slice::Iter<'module, (ModName, TySubmodule)>,
    current: Option<(
        &'module (ModName, TySubmodule),
        Box<SubmodulesRecursive<'module>>,
    )>,
}

impl TyModule {
    /// An iterator yielding all submodules recursively, depth-first.
    pub fn submodules_recursive(&self) -> SubmodulesRecursive {
        SubmodulesRecursive {
            submods: self.submodules.iter(),
            current: None,
        }
    }

    /// All test functions within this module.
    pub fn test_fns<'a: 'b, 'b>(
        &'b self,
        decl_engine: &'a DeclEngine,
    ) -> impl '_ + Iterator<Item = (Arc<TyFunctionDecl>, DeclRefFunction)> {
        self.all_nodes.iter().filter_map(|node| {
            if let TyAstNodeContent::Declaration(TyDecl::FunctionDecl(FunctionDecl {
                decl_id,
                subst_list: _,
                name,
                decl_span,
            })) = &node.content
            {
                let fn_decl = decl_engine.get_function(decl_id);
                if fn_decl.is_test() {
                    return Some((
                        fn_decl,
                        DeclRef::new(name.clone(), *decl_id, decl_span.clone()),
                    ));
                }
            }
            None
        })
    }

    /// All contract functions within this module.
    pub fn contract_fns<'a: 'b, 'b>(
        &'b self,
        engines: &'a Engines,
    ) -> impl '_ + Iterator<Item = DeclRefFunction> {
        self.all_nodes
            .iter()
            .flat_map(move |node| node.contract_fns(engines))
    }

    pub(crate) fn check_deprecated(
        &self,
        engines: &Engines,
        handler: &Handler,
        allow_deprecated: &mut AllowDeprecatedState,
    ) {
        for (_, submodule) in self.submodules.iter() {
            submodule
                .module
                .check_deprecated(engines, handler, allow_deprecated);
        }

        for node in self.all_nodes.iter() {
            node.check_deprecated(engines, handler, allow_deprecated);
        }
    }

    pub(crate) fn check_recursive(
        &self,
        engines: &Engines,
        handler: &Handler,
    ) -> Result<(), ErrorEmitted> {
        handler.scope(|handler| {
            for (_, submodule) in self.submodules.iter() {
                let _ = submodule.module.check_recursive(engines, handler);
            }

            for node in self.all_nodes.iter() {
                let _ = node.check_recursive(engines, handler);
            }

            Ok(())
        })
    }
}

impl<'module> Iterator for SubmodulesRecursive<'module> {
    type Item = &'module (ModName, TySubmodule);
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            self.current = match self.current.take() {
                None => match self.submods.next() {
                    None => return None,
                    Some(submod) => {
                        Some((submod, Box::new(submod.1.module.submodules_recursive())))
                    }
                },
                Some((submod, mut submods)) => match submods.next() {
                    Some(next) => {
                        self.current = Some((submod, submods));
                        return Some(next);
                    }
                    None => return Some(submod),
                },
            }
        }
    }
}

impl HasModule<TyModule> for TySubmodule {
    fn module(&self) -> &TyModule {
        &self.module
    }
}

impl HasSubmodules<TySubmodule> for TyModule {
    fn submodules(&self) -> &[(ModName, TySubmodule)] {
        &self.submodules
    }
}