sway_core/language/ty/
module.rs

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::sync::Arc;

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

use crate::{
    decl_engine::{DeclEngine, DeclEngineGet, DeclId, DeclRef, DeclRefFunction},
    language::{ty::*, HasModule, HasSubmodules, ModName},
    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,
}

impl TyModule {
    /// Iter on all constants in this module, which means, globals constants and
    /// local constants, but it does not enter into submodules.
    pub fn iter_constants(&self, de: &DeclEngine) -> Vec<ConstantDecl> {
        fn inside_code_block(de: &DeclEngine, block: &TyCodeBlock) -> Vec<ConstantDecl> {
            block
                .contents
                .iter()
                .flat_map(|node| inside_ast_node(de, node))
                .collect::<Vec<_>>()
        }

        fn inside_ast_node(de: &DeclEngine, node: &TyAstNode) -> Vec<ConstantDecl> {
            match &node.content {
                TyAstNodeContent::Declaration(decl) => match decl {
                    TyDecl::ConstantDecl(decl) => {
                        vec![decl.clone()]
                    }
                    TyDecl::FunctionDecl(decl) => {
                        let decl = de.get(&decl.decl_id);
                        inside_code_block(de, &decl.body)
                    }
                    TyDecl::ImplSelfOrTrait(decl) => {
                        let decl = de.get(&decl.decl_id);
                        decl.items
                            .iter()
                            .flat_map(|item| match item {
                                TyTraitItem::Fn(decl) => {
                                    let decl = de.get(decl.id());
                                    inside_code_block(de, &decl.body)
                                }
                                TyTraitItem::Constant(decl) => {
                                    vec![ConstantDecl {
                                        decl_id: *decl.id(),
                                    }]
                                }
                                _ => vec![],
                            })
                            .collect()
                    }
                    _ => vec![],
                },
                _ => vec![],
            }
        }

        self.all_nodes
            .iter()
            .flat_map(|node| inside_ast_node(de, node))
            .collect::<Vec<_>>()
    }
}

#[derive(Clone, Debug)]
pub struct TySubmodule {
    pub module: Arc<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 })) =
                &node.content
            {
                let fn_decl = decl_engine.get_function(decl_id);
                let name = fn_decl.name.clone();
                let span = fn_decl.span.clone();
                if fn_decl.is_test() {
                    return Some((fn_decl, DeclRef::new(name, *decl_id, span)));
                }
            }
            None
        })
    }

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

    /// All contract supertrait functions within this module.
    pub fn contract_supertrait_fns<'a: 'b, 'b>(
        &'b self,
        engines: &'a Engines,
    ) -> impl '_ + Iterator<Item = DeclId<TyFunctionDecl>> {
        self.all_nodes
            .iter()
            .flat_map(move |node| node.contract_supertrait_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
    }
}