|
3 | 3 | //
|
4 | 4 | // SPDX-License-Identifier: MIT OR Apache-2.0
|
5 | 5 |
|
6 |
| -use crate::parser::method::MethodFields; |
| 6 | +use crate::parser::cxxqtdata::ParsedCxxQtData; |
| 7 | +use crate::parser::inherit::ParsedInheritedMethod; |
| 8 | +use crate::parser::method::{MethodFields, ParsedMethod}; |
| 9 | +use crate::parser::qobject::ParsedQObject; |
| 10 | +use crate::parser::signals::ParsedSignal; |
| 11 | +use crate::Parser; |
7 | 12 | use proc_macro2::Ident;
|
| 13 | +use quote::format_ident; |
8 | 14 | use std::ops::DerefMut;
|
9 | 15 | use syn::spanned::Spanned;
|
10 |
| -use syn::Error; |
| 16 | +use syn::{Error, Result}; |
11 | 17 |
|
12 | 18 | /// Inline any `Self` types in the methods signatures with the Ident of a qobject passed in
|
13 | 19 | ///
|
@@ -41,86 +47,133 @@ pub fn try_inline_self_invokables(
|
41 | 47 | Ok(())
|
42 | 48 | }
|
43 | 49 |
|
| 50 | +/// A collection of items found in an extern block when parsing |
| 51 | +type BlockComponents<'a> = ( |
| 52 | + &'a mut Vec<ParsedQObject>, |
| 53 | + &'a mut Vec<ParsedMethod>, |
| 54 | + &'a mut Vec<ParsedInheritedMethod>, |
| 55 | + &'a mut Vec<ParsedSignal>, |
| 56 | +); |
| 57 | + |
| 58 | +// Separates the parsed data by block and returns tuples of the components |
| 59 | +fn separate_blocks( |
| 60 | + data: &mut ParsedCxxQtData, |
| 61 | +) -> Vec<BlockComponents> { |
| 62 | + data.qobjects |
| 63 | + .iter_mut() |
| 64 | + .zip(data.methods.iter_mut()) |
| 65 | + .zip(data.inherited_methods.iter_mut()) |
| 66 | + .zip(data.signals.iter_mut()) |
| 67 | + .map(|(((qobject, method), inherited_method), signal)| { |
| 68 | + (qobject, method, inherited_method, signal) |
| 69 | + }) |
| 70 | + .collect() |
| 71 | +} |
| 72 | + |
| 73 | +/// For a given parser, attempt to inline the `Self` type used in any of the blocks with that blocks unique QObject |
| 74 | +pub fn qualify_self_types(parser: &mut Parser) -> Result<()> { |
| 75 | + for (qobjects, methods, inherited, signals) in separate_blocks(&mut parser.cxx_qt_data) { |
| 76 | + let inline_self = qobjects.len() == 1; |
| 77 | + let inline_ident = qobjects |
| 78 | + .last() |
| 79 | + .map(|obj| format_ident!("{}", obj.declaration.ident_left)); |
| 80 | + |
| 81 | + try_inline_self_invokables(inline_self, &inline_ident, methods)?; |
| 82 | + try_inline_self_invokables(inline_self, &inline_ident, signals)?; |
| 83 | + try_inline_self_invokables(inline_self, &inline_ident, inherited)?; |
| 84 | + } |
| 85 | + |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
44 | 89 | #[cfg(test)]
|
45 | 90 | mod tests {
|
46 | 91 | use super::*;
|
47 |
| - use crate::parser::cxxqtdata::ParsedCxxQtData; |
48 | 92 | use crate::parser::method::ParsedMethod;
|
49 | 93 | use crate::tests::assert_parse_errors;
|
50 |
| - use quote::format_ident; |
51 |
| - use syn::{parse_quote, Item}; |
| 94 | + use syn::parse_quote; |
52 | 95 |
|
53 | 96 | #[test]
|
54 | 97 | fn test_self_inlining_ref() {
|
55 |
| - let mut parsed_cxxqtdata = ParsedCxxQtData::new(format_ident!("ffi"), None); |
56 |
| - let extern_rust_qt: Item = parse_quote! { |
57 |
| - unsafe extern "RustQt" { |
58 |
| - #[qobject] |
59 |
| - type MyObject = super::T; |
| 98 | + let module = parse_quote! { |
| 99 | + #[cxx_qt::bridge] |
| 100 | + mod ffi { |
| 101 | + unsafe extern "RustQt" { |
| 102 | + #[qobject] |
| 103 | + type MyObject = super::T; |
60 | 104 |
|
61 |
| - fn my_method(&self); |
| 105 | + fn my_method(&self); |
62 | 106 |
|
63 |
| - #[inherit] |
64 |
| - fn my_inherited_method(&self); |
| 107 | + #[inherit] |
| 108 | + fn my_inherited_method(&self); |
| 109 | + } |
65 | 110 | }
|
66 | 111 | };
|
67 |
| - |
68 |
| - parsed_cxxqtdata.parse_cxx_qt_item(extern_rust_qt).unwrap(); |
| 112 | + let mut parser = Parser::from(module).unwrap(); |
| 113 | + assert!(qualify_self_types(&mut parser).is_ok()); |
69 | 114 | }
|
70 | 115 |
|
71 | 116 | #[test]
|
72 | 117 | fn test_self_inlining_pin() {
|
73 |
| - let mut parsed_cxxqtdata = ParsedCxxQtData::new(format_ident!("ffi"), None); |
74 |
| - let extern_rust_qt: Item = parse_quote! { |
75 |
| - unsafe extern "RustQt" { |
76 |
| - #[qobject] |
77 |
| - type MyObject = super::T; |
78 |
| - |
79 |
| - #[qsignal] |
80 |
| - fn my_signal(self: Pin<&mut Self>); |
81 |
| - } |
82 |
| - }; |
| 118 | + let module = parse_quote! { |
| 119 | + #[cxx_qt::bridge] |
| 120 | + mod ffi { |
| 121 | + unsafe extern "RustQt" { |
| 122 | + #[qobject] |
| 123 | + type MyObject = super::T; |
83 | 124 |
|
84 |
| - let extern_cpp_qt: Item = parse_quote! { |
85 |
| - unsafe extern "C++Qt" { |
86 |
| - #[qobject] |
87 |
| - type MyObject; |
| 125 | + #[qsignal] |
| 126 | + fn my_signal(self: Pin<&mut Self>); |
| 127 | + } |
88 | 128 |
|
89 |
| - #[qsignal] |
90 |
| - fn my_signal(self: Pin<&mut Self>); |
| 129 | + unsafe extern "C++Qt" { |
| 130 | + #[qobject] |
| 131 | + type MyOtherObject; |
| 132 | + |
| 133 | + #[qsignal] |
| 134 | + fn my_signal(self: Pin<&mut Self>); |
| 135 | + } |
91 | 136 | }
|
92 | 137 | };
|
93 |
| - |
94 |
| - parsed_cxxqtdata.parse_cxx_qt_item(extern_rust_qt).unwrap(); |
95 |
| - parsed_cxxqtdata.parse_cxx_qt_item(extern_cpp_qt).unwrap(); |
| 138 | + let mut parser = Parser::from(module).unwrap(); |
| 139 | + assert!(qualify_self_types(&mut parser).is_ok()); |
96 | 140 | }
|
97 | 141 |
|
98 | 142 | #[test]
|
99 | 143 | fn test_self_inlining_methods_invalid() {
|
100 | 144 | assert_parse_errors! {
|
101 |
| - |item| ParsedCxxQtData::new(format_ident!("ffi"), None).parse_cxx_qt_item(item) => |
| 145 | + |item| qualify_self_types(&mut Parser::from(item)?) => |
102 | 146 | // No QObject in block
|
103 | 147 | {
|
104 |
| - extern "RustQt" { |
105 |
| - fn my_method(&self); |
| 148 | + #[cxx_qt::bridge] |
| 149 | + mod ffi { |
| 150 | + extern "RustQt" { |
| 151 | + fn my_method(&self); |
| 152 | + } |
106 | 153 | }
|
107 | 154 | }
|
108 | 155 |
|
109 | 156 | {
|
110 |
| - extern "RustQt" { |
111 |
| - fn my_method(self: Pin<&mut Self>); |
| 157 | + #[cxx_qt::bridge] |
| 158 | + mod ffi { |
| 159 | + extern "RustQt" { |
| 160 | + fn my_method(self: Pin<&mut Self>); |
| 161 | + } |
112 | 162 | }
|
113 | 163 | }
|
114 | 164 | // More than 1 QObjects in block
|
115 | 165 | {
|
116 |
| - extern "RustQt" { |
117 |
| - #[qobject] |
118 |
| - type MyObject = super::T; |
| 166 | + #[cxx_qt::bridge] |
| 167 | + mod ffi { |
| 168 | + extern "RustQt" { |
| 169 | + #[qobject] |
| 170 | + type MyObject = super::T; |
119 | 171 |
|
120 |
| - #[qobject] |
121 |
| - type MyOtherObject = super::S; |
| 172 | + #[qobject] |
| 173 | + type MyOtherObject = super::S; |
122 | 174 |
|
123 |
| - fn my_method(&self); |
| 175 | + fn my_method(&self); |
| 176 | + } |
124 | 177 | }
|
125 | 178 | }
|
126 | 179 | }
|
|
0 commit comments