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