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
// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

mod constructor;
pub mod cxxqttype;
pub mod externcxxqt;
pub mod fragment;
pub mod inherit;
pub mod locking;
pub mod method;
pub mod property;
pub mod qenum;
pub mod qnamespace;
pub mod qobject;
pub mod signal;
pub mod threading;

use std::collections::BTreeSet;

use crate::parser::Parser;
use externcxxqt::GeneratedCppExternCxxQtBlocks;
use qobject::GeneratedCppQObject;
use syn::Result;

/// Representation of the generated C++ code for a group of QObjects
pub struct GeneratedCppBlocks {
    /// Forward declarations that aren't associated with any QObjects (e.g. "free" qenums).
    pub forward_declares: Vec<String>,
    /// Additional includes for the CXX bridge
    pub includes: BTreeSet<String>,
    /// Stem of the CXX header to include
    pub cxx_file_stem: String,
    /// Generated QObjects
    pub qobjects: Vec<GeneratedCppQObject>,
    /// Generated extern C++Qt blocks
    pub extern_cxx_qt: Vec<GeneratedCppExternCxxQtBlocks>,
}

impl GeneratedCppBlocks {
    pub fn from(parser: &Parser) -> Result<GeneratedCppBlocks> {
        let mut includes = BTreeSet::new();

        let mut forward_declares: Vec<_> = parser
            .cxx_qt_data
            .qnamespaces
            .iter()
            .map(|parsed_qnamespace| qnamespace::generate(parsed_qnamespace, &mut includes))
            .collect();
        forward_declares.extend(
            parser
                .cxx_qt_data
                .qenums
                .iter()
                .map(|parsed_qenum| qenum::generate_declaration(parsed_qenum, &mut includes)),
        );
        Ok(GeneratedCppBlocks {
            forward_declares,
            includes,
            cxx_file_stem: parser.cxx_file_stem.clone(),
            qobjects: parser
                .cxx_qt_data
                .qobjects
                .values()
                .map(|qobject| GeneratedCppQObject::from(qobject, &parser.cxx_qt_data.cxx_mappings))
                .collect::<Result<Vec<GeneratedCppQObject>>>()?,
            extern_cxx_qt: externcxxqt::generate(
                &parser.cxx_qt_data.extern_cxxqt_blocks,
                &parser.cxx_qt_data.cxx_mappings,
            )?,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::parser::Parser;
    use syn::{parse_quote, ItemMod};

    #[test]
    fn test_generated_cpp_blocks() {
        let module: ItemMod = parse_quote! {
            #[cxx_qt::bridge]
            mod ffi {
                extern "RustQt" {
                    #[qobject]
                    type MyObject = super::MyObjectRust;
                }
            }
        };
        let parser = Parser::from(module).unwrap();

        let cpp = GeneratedCppBlocks::from(&parser).unwrap();
        assert_eq!(cpp.cxx_file_stem, "ffi");
        assert_eq!(cpp.qobjects.len(), 1);
        assert_eq!(cpp.qobjects[0].namespace, "");
    }

    #[test]
    fn test_generated_cpp_blocks_cxx_file_stem() {
        let module: ItemMod = parse_quote! {
            #[cxx_qt::bridge(cxx_file_stem = "my_object")]
            mod ffi {
                extern "RustQt" {
                    #[qobject]
                    type MyObject = super::MyObjectRust;
                }
            }
        };
        let parser = Parser::from(module).unwrap();

        let cpp = GeneratedCppBlocks::from(&parser).unwrap();
        assert_eq!(cpp.cxx_file_stem, "my_object");
        assert_eq!(cpp.qobjects.len(), 1);
        assert_eq!(&cpp.qobjects[0].namespace, "");
    }

    #[test]
    fn test_generated_cpp_blocks_namespace() {
        let module: ItemMod = parse_quote! {
            #[cxx_qt::bridge(namespace = "cxx_qt")]
            mod ffi {
                extern "RustQt" {
                    #[qobject]
                    type MyObject = super::MyObjectRust;
                }
            }
        };
        let parser = Parser::from(module).unwrap();

        let cpp = GeneratedCppBlocks::from(&parser).unwrap();
        assert_eq!(cpp.qobjects[0].namespace, "cxx_qt");
    }
}