forked from KDAB/cxx-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinherit.rs
151 lines (134 loc) · 4.55 KB
/
inherit.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
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// SPDX-FileContributor: Leon Matthes <[email protected]>
// SPDX-License-Identifier: MIT OR Apache-2.0
use crate::{
generator::{naming::qobject::QObjectName, rust::qobject::GeneratedRustQObjectBlocks},
parser::inherit::ParsedInheritedMethod,
};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Item, Result};
pub fn generate(
qobject_ident: &QObjectName,
methods: &[ParsedInheritedMethod],
) -> Result<GeneratedRustQObjectBlocks> {
let mut blocks = GeneratedRustQObjectBlocks::default();
let qobject_name = &qobject_ident.cpp_class.rust;
let mut bridges = methods
.iter()
.map(|method| {
let parameters = method
.parameters
.iter()
.map(|parameter| {
let ident = ¶meter.ident;
let ty = ¶meter.ty;
quote! { #ident: #ty }
})
.collect::<Vec<TokenStream>>();
let ident = &method.method.sig.ident;
let cxx_name_string = &method.wrapper_ident().to_string();
let self_param = if method.mutable {
quote! { self: Pin<&mut #qobject_name> }
} else {
quote! { self: &#qobject_name }
};
let return_type = &method.method.sig.output;
let mut unsafe_block = None;
let mut unsafe_call = Some(quote! { unsafe });
if method.safe {
std::mem::swap(&mut unsafe_call, &mut unsafe_block);
}
let attrs = &method.method.attrs;
syn::parse2(quote! {
#unsafe_block extern "C++" {
#(#attrs)*
#[cxx_name = #cxx_name_string]
#unsafe_call fn #ident(#self_param, #(#parameters),*) #return_type;
}
})
})
.collect::<Result<Vec<Item>>>()?;
blocks.cxx_mod_contents.append(&mut bridges);
Ok(blocks)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
generator::naming::qobject::tests::create_qobjectname, syntax::safety::Safety,
tests::assert_tokens_eq,
};
use syn::{parse_quote, ForeignItemFn};
fn generate_from_foreign(
method: ForeignItemFn,
safety: Safety,
) -> Result<GeneratedRustQObjectBlocks> {
let inherited_methods = vec![ParsedInheritedMethod::parse(method, safety).unwrap()];
generate(&create_qobjectname(), &inherited_methods)
}
#[test]
fn test_mutable() {
let generated = generate_from_foreign(
parse_quote! {
fn test(self: Pin<&mut qobject::MyObject>, a: B, b: C);
},
Safety::Safe,
)
.unwrap();
assert_eq!(generated.cxx_mod_contents.len(), 1);
assert_eq!(generated.cxx_qt_mod_contents.len(), 0);
assert_tokens_eq(
&generated.cxx_mod_contents[0],
quote! {
unsafe extern "C++" {
#[cxx_name = "testCxxQtInherit"]
fn test(self: Pin<&mut MyObject>, a: B, b: C);
}
},
);
}
#[test]
fn test_immutable() {
let generated = generate_from_foreign(
parse_quote! {
fn test(self: &qobject::MyObject, a: B, b: C);
},
Safety::Safe,
)
.unwrap();
assert_eq!(generated.cxx_mod_contents.len(), 1);
assert_eq!(generated.cxx_qt_mod_contents.len(), 0);
assert_tokens_eq(
&generated.cxx_mod_contents[0],
quote! {
unsafe extern "C++" {
#[cxx_name = "testCxxQtInherit"]
fn test(self: &MyObject, a: B, b: C);
}
},
);
}
#[test]
fn test_unsafe() {
let generated = generate_from_foreign(
parse_quote! {
unsafe fn test(self: &qobject::MyObject);
},
Safety::Unsafe,
)
.unwrap();
assert_eq!(generated.cxx_mod_contents.len(), 1);
assert_eq!(generated.cxx_qt_mod_contents.len(), 0);
assert_tokens_eq(
&generated.cxx_mod_contents[0],
// TODO: Maybe remove the trailing comma after self?
quote! {
extern "C++" {
#[cxx_name = "testCxxQtInherit"]
unsafe fn test(self: &MyObject,);
}
},
);
}
}