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