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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation,
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

/// The module consists of methods and structures for C++
/// and SWIG code generation.
///
use super::{
    create_enum_class,
    enums_cpp_forward_declarations,
    externs::create_extern_imports,
    function_translator::*,
    function_virtual_translator::*,
    templates::*,
    translate_enums,
};
use crate::binding_types::{ExternFunction, RustWrapperType, WrapperType};
use crate::cpp::wasm_generator::create_wasm_module;
use crate::extern_module_translator::{Exceptions, ExternModuleTranslator};

fn create_classes_declarations(extern_module_translator: &ExternModuleTranslator) -> String {
    extern_module_translator
        .user_custom_types
        .iter()
        .map(|(wrapper_type, vec_of_functions)| {
            let class_name = wrapper_type.wrapper_name.to_string();
            let functions_declaration: String = vec_of_functions
                .iter()
                .map(|f| FunctionTranslator::from_class_method(f, &class_name))
                .map(FunctionTranslator::generate_declaration)
                .collect();
            custom_class_definition(&class_name, functions_declaration)
        })
        .collect::<String>()
}

fn create_class_methods_definitions(extern_module_translator: &ExternModuleTranslator) -> String {
    extern_module_translator
        .user_custom_types
        .iter()
        .map(|(wrapper_type, vec_of_functions)| {
            let class_name = wrapper_type.wrapper_name.to_string();
            vec_of_functions
                .iter()
                .map(|f| FunctionTranslator::from_class_method(f, &class_name))
                .map(FunctionTranslator::generate_definition)
                .collect::<String>()
        })
        .collect::<String>()
}

fn create_abstract_classes_declarations(
    extern_module_translator: &ExternModuleTranslator,
) -> String {
    extern_module_translator
        .user_traits
        .iter()
        .map(|(wrapper_type, vec_of_functions)| {
            let class_name = wrapper_type.wrapper_name.to_string();
            let functions_declaration: String = vec_of_functions
                .iter()
                .map(|f| FunctionVirtualTranslator::from_virtual_function(f, &class_name))
                .map(FunctionVirtualTranslator::generate_virtual_declaration)
                .collect();
            abstract_class_declaration(&class_name, &functions_declaration)
        })
        .collect::<String>()
}

fn create_virtual_method_calls(extern_module_translator: &ExternModuleTranslator) -> String {
    extern_module_translator
        .user_traits
        .iter()
        .map(|(wrapper_type, vec_of_functions)| {
            let class_name = wrapper_type.wrapper_name.to_string();
            vec_of_functions
                .iter()
                .map(|f| FunctionVirtualTranslator::from_virtual_function(f, &class_name))
                .map(FunctionVirtualTranslator::generate_virtual_definition)
                .collect::<String>()
        })
        .collect::<String>()
}

fn create_rust_types_wrappers(extern_module_translator: &ExternModuleTranslator) -> String {
    extern_module_translator
        .rust_types_wrappers
        .ordered_iter()
        .filter_map(|wrapper| match wrapper {
            WrapperType {
                rust_type: RustWrapperType::Vector(inner_type),
                ..
            } => {
                let inner_type_name = inner_type.get_name();
                let vec_base_impl = vector_impl(&inner_type_name, &inner_type.wrapper_name);
                Some(vec_base_impl + &begin_end_impl(&inner_type_name, &inner_type.rust_type))
            }
            WrapperType {
                rust_type: RustWrapperType::Option(inner_type),
                ..
            } => {
                let inner_type_name = inner_type.get_name();
                Some(option_class(&inner_type_name, &inner_type.wrapper_name))
            }
            WrapperType {
                rust_type: RustWrapperType::Exceptions(Exceptions::NonPrimitive(idents)),
                wrapper_name,
                ..
            } => Some(
                idents
                    .iter()
                    .map(|exception| {
                        create_non_primitive_exception_class(
                            &exception.to_string(),
                            wrapper_name,
                            extern_module_translator.exception_trait_methods.iter(),
                        )
                    })
                    .collect::<String>(),
            ),
            WrapperType {
                rust_type: RustWrapperType::Exceptions(Exceptions::Primitive(idents)),
                wrapper_name,
                ..
            } => Some(
                idents
                    .iter()
                    .map(|exception| {
                        create_primitive_exception_class(
                            &exception.to_string(),
                            wrapper_name,
                            extern_module_translator.exception_trait_methods.iter(),
                        )
                    })
                    .collect::<String>(),
            ),
            _ => None,
        })
        .collect()
}

fn create_result_wrappers(extern_module_translator: &ExternModuleTranslator) -> String {
    extern_module_translator
        .rust_types_wrappers
        .ordered_iter()
        .filter_map(|wrapper| match wrapper {
            WrapperType {
                rust_type: RustWrapperType::Result(ok_type, exceptions_type),
                ..
            } => {
                let ok_type = ok_type.get_name();
                let error_enum_name = &exceptions_type.wrapper_name;
                Some(result_class(
                    &wrapper.wrapper_name,
                    &ok_type,
                    error_enum_name,
                ))
            }
            _ => None,
        })
        .collect()
}

fn create_global_functions_definitions(
    extern_module_translator: &ExternModuleTranslator,
) -> String {
    extern_module_translator
        .global_functions
        .iter()
        .map(FunctionTranslator::from_global_function)
        .map(FunctionTranslator::generate_definition)
        .collect()
}

pub fn create_classes_forward_declarations(
    extern_module_translator: &ExternModuleTranslator,
) -> String {
    extern_module_translator
        .rust_types_wrappers
        .unordered_iter()
        .map(|wrapper| match wrapper {
            WrapperType {
                wrapper_name,
                rust_type:
                    RustWrapperType::Custom
                    | RustWrapperType::Trait
                    | RustWrapperType::Result(_, _)
                    | RustWrapperType::ArcMutex,
                ..
            } => {
                format!("struct {wrapper_name};\n")
            }
            _ => "".to_owned(),
        })
        .collect()
}

fn create_exception_class_enum(extern_module_translator: &ExternModuleTranslator) -> String {
    create_enum_class(
        "ExceptionClass",
        extern_module_translator
            .exception_names
            .iter()
            .chain(std::iter::once(&RUST_EXCEPTION_BASE_CLASS_NAME.to_string())),
    )
}

/// Results of this function can be written in the `*.hpp` and `*.i` files.
/// The generated C++ header file can be used in C++ application. The header
/// file and `*.i` file can be included to the SWIG interface file and used
/// to generate glue code for Java, C# etc.
///
pub fn generate_cpp_file(
    extern_module_translator: &ExternModuleTranslator,
    extern_functions: &[ExternFunction],
) -> String {
    let externs = create_extern_imports(extern_functions);
    let classes_forward_declarations =
        create_classes_forward_declarations(extern_module_translator);
    let enums_cpp_forward_declarations = enums_cpp_forward_declarations(extern_module_translator);
    let enum_classes_definitions = translate_enums(extern_module_translator);
    let classes_declaration = create_classes_declarations(extern_module_translator);
    let classes_definition = create_class_methods_definitions(extern_module_translator);
    let abstract_classes_declaration =
        create_abstract_classes_declarations(extern_module_translator);
    let virtual_methods_calls = create_virtual_method_calls(extern_module_translator);
    let rust_types_wrappers = create_rust_types_wrappers(extern_module_translator);
    let result_wrapper = create_result_wrappers(extern_module_translator);
    let global_functions_definition: String =
        create_global_functions_definitions(extern_module_translator);
    let wasm_module = create_wasm_module(extern_module_translator);
    let exception_class_enum = create_exception_class_enum(extern_module_translator);
    let base_exception_class = base_exception_class(extern_module_translator);

    format!(
        "{IMPORTS}
{exception_class_enum}
{TYPEDEFS}
{enums_cpp_forward_declarations}
{classes_forward_declarations}
extern \"C\" {{
{externs}
}}
{PREDEFINED}
{base_exception_class}
{abstract_classes_declaration}
{classes_declaration}
{enum_classes_definitions}
{rust_types_wrappers}
{result_wrapper}
{global_functions_definition}
{classes_definition}
{virtual_methods_calls}
{wasm_module}"
    )
}