diff --git a/crates/cxx-qt-gen/src/generator/cpp/cxxqttype.rs b/crates/cxx-qt-gen/src/generator/cpp/cxxqttype.rs index c9706849c..e8a1c584b 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/cxxqttype.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/cxxqttype.rs @@ -9,7 +9,7 @@ use syn::Result; pub fn generate(qobject_idents: &QObjectName) -> Result { let mut result = GeneratedCppQObjectBlocks::default(); - let rust_ident = qobject_idents.rust_struct.cpp.to_string(); + let rust_struct = qobject_idents.rust_struct.cxx_qualified(); result .includes @@ -17,7 +17,7 @@ pub fn generate(qobject_idents: &QObjectName) -> Result")); + .push(format!("::rust::cxxqt1::CxxQtType<{rust_struct}>")); Ok(result) } diff --git a/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs b/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs index 5274ad7ea..9e30e1169 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs @@ -29,7 +29,8 @@ pub fn generate( for block in blocks { for signal in &block.signals { let mut block = GeneratedCppExternCxxQtBlocks::default(); - let data = generate_cpp_signal(signal, &signal.qobject_ident, type_names)?; + let qobject_name = type_names.lookup(&signal.qobject_ident)?; + let data = generate_cpp_signal(signal, qobject_name, type_names)?; block.includes = data.includes; // Ensure that we include MaybeLockGuard that is used in multiple places block diff --git a/crates/cxx-qt-gen/src/generator/cpp/method.rs b/crates/cxx-qt-gen/src/generator/cpp/method.rs index b148f14cd..78b228887 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/method.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/method.rs @@ -26,7 +26,7 @@ pub fn generate_cpp_methods( type_names: &TypeNames, ) -> Result { let mut generated = GeneratedCppQObjectBlocks::default(); - let qobject_ident = qobject_idents.cpp_class.cpp.to_string(); + let qobject_ident = qobject_idents.name.cxx_unqualified(); for invokable in invokables { let idents = QMethodName::from(invokable); let return_cxx_ty = syn_type_to_cpp_return_type(&invokable.method.sig.output, type_names)?; diff --git a/crates/cxx-qt-gen/src/generator/cpp/property/mod.rs b/crates/cxx-qt-gen/src/generator/cpp/property/mod.rs index 9ca4138c5..896a60e97 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/property/mod.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/property/mod.rs @@ -24,7 +24,7 @@ pub fn generate_cpp_properties( ) -> Result { let mut generated = GeneratedCppQObjectBlocks::default(); let mut signals = vec![]; - let qobject_ident = qobject_idents.cpp_class.cpp.to_string(); + let qobject_ident = qobject_idents.name.cxx_unqualified(); for property in properties { // Cache the idents as they are used in multiple places diff --git a/crates/cxx-qt-gen/src/generator/cpp/property/signal.rs b/crates/cxx-qt-gen/src/generator/cpp/property/signal.rs index b601efbbe..67e0b1603 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/property/signal.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/property/signal.rs @@ -13,7 +13,7 @@ use crate::{ pub fn generate(idents: &QPropertyName, qobject_idents: &QObjectName) -> ParsedSignal { // We build our signal in the generation phase as we need to use the naming // structs to build the signal name - let cpp_class_rust = &qobject_idents.cpp_class.rust; + let cpp_class_rust = &qobject_idents.name.rust_unqualified(); let notify_cpp = &idents.notify.cpp; let notify_rust_str = idents.notify.rust.to_string(); let method: ForeignItemFn = syn::parse_quote! { @@ -24,6 +24,6 @@ pub fn generate(idents: &QPropertyName, qobject_idents: &QObjectName) -> ParsedS ParsedSignal::from_property_method( method, idents.notify.clone(), - qobject_idents.cpp_class.rust.clone(), + qobject_idents.name.rust_unqualified().clone(), ) } diff --git a/crates/cxx-qt-gen/src/generator/cpp/qobject.rs b/crates/cxx-qt-gen/src/generator/cpp/qobject.rs index 3c0027fa6..0ed577b1d 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/qobject.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/qobject.rs @@ -104,7 +104,7 @@ impl GeneratedCppQObject { let qobject = structured_qobject.declaration; // Create the base object - let qobject_idents = QObjectName::from(qobject); + let qobject_idents = QObjectName::from_qobject(qobject, type_names)?; let namespace_idents = NamespaceName::from(qobject); let mut generated = GeneratedCppQObject { name: qobject.name.clone(), diff --git a/crates/cxx-qt-gen/src/generator/cpp/signal.rs b/crates/cxx-qt-gen/src/generator/cpp/signal.rs index 91c87a7de..e083ebaf0 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/signal.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/signal.rs @@ -11,13 +11,12 @@ use crate::{ signals::{QSignalHelperName, QSignalName}, }, }, - naming::cpp::syn_type_to_cpp_type, - naming::TypeNames, + naming::{cpp::syn_type_to_cpp_type, Name, TypeNames}, parser::{parameter::ParsedFunctionParameter, signals::ParsedSignal}, }; use indoc::formatdoc; use std::collections::BTreeSet; -use syn::{Ident, Result}; +use syn::Result; #[derive(Default)] pub struct CppSignalFragment { @@ -49,7 +48,7 @@ struct Parameters { fn parameter_types_and_values( parameters: &[ParsedFunctionParameter], type_names: &TypeNames, - self_ty: &Ident, + self_ty: &Name, ) -> Result { let mut parameter_named_types_with_self = vec![]; let mut parameter_types_with_self = vec![]; @@ -66,7 +65,7 @@ fn parameter_types_and_values( let parameter_named_types = parameter_named_types_with_self.join(", "); // Insert the extra argument into the closure - let self_ty = type_names.cxx_qualified(self_ty)?; + let self_ty = self_ty.cxx_qualified(); parameter_named_types_with_self.insert(0, format!("{self_ty}& self")); parameter_types_with_self.insert(0, format!("{self_ty}&")); parameter_values_with_self.insert(0, "self".to_owned()); @@ -81,7 +80,7 @@ fn parameter_types_and_values( pub fn generate_cpp_signal( signal: &ParsedSignal, - qobject_ident: &Ident, + qobject_name: &Name, type_names: &TypeNames, ) -> Result { let mut generated = CppSignalFragment::default(); @@ -92,17 +91,17 @@ pub fn generate_cpp_signal( .insert("#include ".to_owned()); // Build a namespace that includes any namespace for the T - let qobject_ident_namespaced = type_names.cxx_qualified(qobject_ident)?; + let qobject_ident_namespaced = qobject_name.cxx_qualified(); // Prepare the idents let idents = QSignalName::from(signal); - let idents_helper = QSignalHelperName::new(&idents, qobject_ident, type_names)?; + let idents_helper = QSignalHelperName::new(&idents, qobject_name)?; let signal_ident = idents.name.cpp; let free_connect_ident_cpp = idents_helper.connect_name.cpp; // Retrieve the parameters for the signal - let parameters = parameter_types_and_values(&signal.parameters, type_names, qobject_ident)?; + let parameters = parameter_types_and_values(&signal.parameters, type_names, qobject_name)?; let parameters_named_types = parameters.named_types; let parameters_named_types_with_self = parameters.named_types_with_self; let parameter_types_with_self = parameters.types_with_self; @@ -196,11 +195,10 @@ pub fn generate_cpp_signals( type_names: &TypeNames, ) -> Result { let mut generated = GeneratedCppQObjectBlocks::default(); - let qobject_ident = &qobject_idents.cpp_class.cpp; for signal in signals { let mut block = GeneratedCppQObjectBlocks::default(); - let data = generate_cpp_signal(signal, qobject_ident, type_names)?; + let data = generate_cpp_signal(signal, &qobject_idents.name, type_names)?; block.includes = data.includes; block.forward_declares_namespaced = data.forward_declares; block.fragments = data.fragments; @@ -533,7 +531,8 @@ mod tests { let mut type_names = TypeNames::default(); type_names.mock_insert("ObjRust", None, None, None); - let generated = generate_cpp_signal(&signal, &signal.qobject_ident, &type_names).unwrap(); + let qobject_name = type_names.lookup(&signal.qobject_ident).unwrap(); + let generated = generate_cpp_signal(&signal, &qobject_name, &type_names).unwrap(); assert_eq!(generated.methods.len(), 0); @@ -623,8 +622,8 @@ mod tests { let mut type_names = TypeNames::default(); type_names.mock_insert("ObjRust", None, Some("ObjCpp"), Some("mynamespace")); - - let generated = generate_cpp_signal(&signal, &signal.qobject_ident, &type_names).unwrap(); + let qobject_name = type_names.lookup(&signal.qobject_ident).unwrap(); + let generated = generate_cpp_signal(&signal, qobject_name, &type_names).unwrap(); assert_eq!(generated.methods.len(), 0); diff --git a/crates/cxx-qt-gen/src/generator/cpp/threading.rs b/crates/cxx-qt-gen/src/generator/cpp/threading.rs index 3d6d66aab..170949e6b 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/threading.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/threading.rs @@ -13,7 +13,7 @@ use syn::Result; pub fn generate(qobject_idents: &QObjectName) -> Result<(String, GeneratedCppQObjectBlocks)> { let mut result = GeneratedCppQObjectBlocks::default(); - let cpp_class = &qobject_idents.cpp_class.cpp; + let cpp_class = &qobject_idents.name.cxx_unqualified(); let cxx_qt_thread_ident = &qobject_idents.cxx_qt_thread_class; result.forward_declares.push(format!( diff --git a/crates/cxx-qt-gen/src/generator/naming/qobject.rs b/crates/cxx-qt-gen/src/generator/naming/qobject.rs index cf616ab6b..4b40d456f 100644 --- a/crates/cxx-qt-gen/src/generator/naming/qobject.rs +++ b/crates/cxx-qt-gen/src/generator/naming/qobject.rs @@ -2,56 +2,54 @@ // SPDX-FileContributor: Andrew Hayzen // // SPDX-License-Identifier: MIT OR Apache-2.0 -use crate::{generator::naming::CombinedIdent, naming::Name, parser::qobject::ParsedQObject}; +use crate::{ + naming::{Name, TypeNames}, + parser::qobject::ParsedQObject, +}; use convert_case::{Case, Casing}; use quote::format_ident; -use syn::Ident; +use syn::{Ident, Result}; /// Names for parts of a Q_OBJECT pub struct QObjectName { - // Store the ident so that cxx_qt_thread_method can use it later - ident: Ident, - /// The name of the C++ class - pub cpp_class: CombinedIdent, - /// The name of the Rust struct - pub rust_struct: CombinedIdent, + /// The name of the QObject itself. + pub name: Name, + /// The name of the inner Rust struct + pub rust_struct: Name, /// The name of the CxxQtThread pub cxx_qt_thread_class: Ident, /// The name of the Rust closure wrapper to be passed in to CxxQtThread pub cxx_qt_thread_queued_fn_struct: Ident, } -impl From<&ParsedQObject> for QObjectName { - fn from(qobject: &ParsedQObject) -> Self { - Self::from_name_and_ident(&qobject.name, qobject.rust_type.clone()) +impl QObjectName { + pub fn from_qobject(qobject: &ParsedQObject, type_names: &TypeNames) -> Result { + Self::from_name_and_ident(&qobject.name, &qobject.rust_type, type_names) } -} -impl QObjectName { - pub fn from_name_and_ident(qobject_name: &Name, ident_right: Ident) -> Self { - Self { - cpp_class: CombinedIdent { - cpp: format_ident!("{}", qobject_name.cxx_unqualified()), - rust: qobject_name.rust_unqualified().clone(), - }, - rust_struct: CombinedIdent::from_ident(ident_right), + pub fn from_name_and_ident( + qobject_name: &Name, + ident_right: &Ident, + type_names: &TypeNames, + ) -> Result { + Ok(Self { + name: qobject_name.clone(), + rust_struct: type_names.lookup(ident_right)?.clone(), cxx_qt_thread_class: cxx_qt_thread_class_from_ident(qobject_name.rust_unqualified()), cxx_qt_thread_queued_fn_struct: cxx_qt_thread_queued_fn_struct_from_ident( qobject_name.rust_unqualified(), ), - ident: qobject_name.rust_unqualified().clone(), - } + }) } // Only for mocking in tests #[cfg(test)] pub fn from_idents(ident_left: Ident, ident_right: Ident) -> Self { Self { - cpp_class: CombinedIdent::from_ident(ident_left.clone()), - rust_struct: CombinedIdent::from_ident(ident_right), + name: Name::mock(&ident_left.to_string()), + rust_struct: Name::mock(&ident_right.to_string()), cxx_qt_thread_class: cxx_qt_thread_class_from_ident(&ident_left), cxx_qt_thread_queued_fn_struct: cxx_qt_thread_queued_fn_struct_from_ident(&ident_left), - ident: ident_left, } } @@ -59,7 +57,7 @@ impl QObjectName { pub fn cxx_qt_thread_method(&self, suffix: &str) -> Ident { format_ident!( "cxx_qt_ffi_{ident}_{suffix}", - ident = self.ident.to_string().to_case(Case::Snake) + ident = self.name.cxx_unqualified().to_case(Case::Snake) ) } } @@ -74,16 +72,6 @@ fn cxx_qt_thread_queued_fn_struct_from_ident(ident: &Ident) -> Ident { format_ident!("{ident}CxxQtThreadQueuedFn") } -impl CombinedIdent { - /// For a given ident generate the Rust and C++ names - fn from_ident(ident: Ident) -> Self { - Self { - cpp: ident.clone(), - rust: ident, - } - } -} - #[cfg(test)] pub mod tests { use super::*; @@ -91,16 +79,20 @@ pub mod tests { use crate::parser::qobject::tests::create_parsed_qobject; pub fn create_qobjectname() -> QObjectName { - QObjectName::from(&create_parsed_qobject()) + QObjectName::from_qobject(&create_parsed_qobject(), &TypeNames::mock()).unwrap() } #[test] fn test_parsed_property() { - let names = QObjectName::from(&create_parsed_qobject()); - assert_eq!(names.cpp_class.cpp, format_ident!("MyObject")); - assert_eq!(names.cpp_class.rust, format_ident!("MyObject")); - assert_eq!(names.rust_struct.cpp, format_ident!("MyObjectRust")); - assert_eq!(names.rust_struct.rust, format_ident!("MyObjectRust")); + let names = + QObjectName::from_qobject(&create_parsed_qobject(), &TypeNames::mock()).unwrap(); + assert_eq!(names.name.cxx_unqualified(), "MyObject"); + assert_eq!(names.name.rust_unqualified(), &format_ident!("MyObject")); + assert_eq!(names.rust_struct.cxx_unqualified(), "MyObjectRust"); + assert_eq!( + names.rust_struct.rust_unqualified(), + &format_ident!("MyObjectRust") + ); assert_eq!( names.cxx_qt_thread_class, format_ident!("MyObjectCxxQtThread") diff --git a/crates/cxx-qt-gen/src/generator/naming/signals.rs b/crates/cxx-qt-gen/src/generator/naming/signals.rs index eb459f4d9..521ba294d 100644 --- a/crates/cxx-qt-gen/src/generator/naming/signals.rs +++ b/crates/cxx-qt-gen/src/generator/naming/signals.rs @@ -3,7 +3,7 @@ // // SPDX-License-Identifier: MIT OR Apache-2.0 use crate::parser::signals::ParsedSignal; -use crate::{generator::naming::CombinedIdent, naming::TypeNames}; +use crate::{generator::naming::CombinedIdent, naming::Name}; use convert_case::{Case, Casing}; use quote::format_ident; use syn::{Ident, Result}; @@ -52,12 +52,9 @@ pub struct QSignalHelperName { } impl QSignalHelperName { - pub fn new( - idents: &QSignalName, - qobject_ident: &Ident, - type_names: &TypeNames, - ) -> Result { + pub fn new(idents: &QSignalName, qobject_name: &Name) -> Result { let signal_ident = &idents.name.cpp; + let qobject_ident = qobject_name.rust_unqualified().to_string(); let handler_alias = format_ident!("{qobject_ident}CxxQtSignalHandler{signal_ident}"); let namespace = { // This namespace will take the form of: @@ -71,10 +68,10 @@ impl QSignalHelperName { // // See the comment on TypeNames::cxx_qualified for why fully qualifying is // unfortunately not possible. - let qobject_namespace = type_names.namespace(qobject_ident)?; + let qobject_namespace = qobject_name.namespace(); let namespace: Vec<_> = qobject_namespace .into_iter() - .chain(vec!["rust::cxxqtgen1".to_owned()]) + .chain(vec!["rust::cxxqtgen1"]) .collect(); namespace.join("::") diff --git a/crates/cxx-qt-gen/src/generator/rust/constructor.rs b/crates/cxx-qt-gen/src/generator/rust/constructor.rs index 300dd2f41..1fc2d3d12 100644 --- a/crates/cxx-qt-gen/src/generator/rust/constructor.rs +++ b/crates/cxx-qt-gen/src/generator/rust/constructor.rs @@ -63,7 +63,7 @@ fn generate_default_constructor( qobject_idents: &QObjectName, namespace: &NamespaceName, ) -> GeneratedRustFragment { - let rust_struct_ident = &qobject_idents.rust_struct.rust; + let rust_struct_ident = qobject_idents.rust_struct.rust_unqualified(); let create_rs_ident = format_ident!( "create_rs_{object_name}", @@ -189,12 +189,12 @@ pub fn generate( let mut result = GeneratedRustFragment::default(); let namespace_internals = &namespace.internal; - let qobject_name = &qobject_idents.cpp_class.cpp; - let qobject_name_rust = &qobject_idents.cpp_class.rust; + let qobject_name = qobject_idents.name.cxx_unqualified(); + let qobject_name_rust = qobject_idents.name.rust_unqualified(); let qobject_name_rust_qualified = type_names.rust_qualified(qobject_name_rust)?; let qobject_name_snake = qobject_name.to_string().to_case(Case::Snake); - let rust_struct_name_rust = &qobject_idents.rust_struct.rust; + let rust_struct_name_rust = qobject_idents.rust_struct.rust_unqualified(); for (index, constructor) in constructors.iter().enumerate() { let lifetime = constructor.lifetime.as_ref().map(|lifetime| { diff --git a/crates/cxx-qt-gen/src/generator/rust/cxxqttype.rs b/crates/cxx-qt-gen/src/generator/rust/cxxqttype.rs index 6cf9a483c..a3b6a2e3a 100644 --- a/crates/cxx-qt-gen/src/generator/rust/cxxqttype.rs +++ b/crates/cxx-qt-gen/src/generator/rust/cxxqttype.rs @@ -18,8 +18,8 @@ pub fn generate( ) -> Result { let mut blocks = GeneratedRustFragment::default(); - let cpp_struct_ident = &qobject_ident.cpp_class.rust; - let rust_struct_ident = &qobject_ident.rust_struct.rust; + let cpp_struct_ident = &qobject_ident.name.rust_unqualified(); + let rust_struct_ident = &qobject_ident.rust_struct.rust_unqualified(); let qualified_impl = type_names.rust_qualified(cpp_struct_ident)?; let fragment = RustFragmentPair { @@ -86,7 +86,7 @@ mod tests { #[test] fn test_generate_rust_cxxqttype() { let qobject = create_parsed_qobject(); - let qobject_idents = QObjectName::from(&qobject); + let qobject_idents = QObjectName::from_qobject(&qobject, &TypeNames::mock()).unwrap(); let generated = generate(&qobject_idents, &TypeNames::mock()).unwrap(); diff --git a/crates/cxx-qt-gen/src/generator/rust/externcxxqt.rs b/crates/cxx-qt-gen/src/generator/rust/externcxxqt.rs index 679e12867..decc5e313 100644 --- a/crates/cxx-qt-gen/src/generator/rust/externcxxqt.rs +++ b/crates/cxx-qt-gen/src/generator/rust/externcxxqt.rs @@ -41,7 +41,7 @@ impl GeneratedRustFragment { // Build the signals for signal in &extern_cxxqt_block.signals { - let qobject_name = &signal.qobject_ident; + let qobject_name = type_names.lookup(&signal.qobject_ident)?; generated.append(&mut generate_rust_signal( signal, diff --git a/crates/cxx-qt-gen/src/generator/rust/inherit.rs b/crates/cxx-qt-gen/src/generator/rust/inherit.rs index 35063735c..d1c700db8 100644 --- a/crates/cxx-qt-gen/src/generator/rust/inherit.rs +++ b/crates/cxx-qt-gen/src/generator/rust/inherit.rs @@ -16,7 +16,7 @@ pub fn generate( methods: &[ParsedInheritedMethod], ) -> Result { let mut blocks = GeneratedRustFragment::default(); - let qobject_name = &qobject_ident.cpp_class.rust; + let qobject_name = qobject_ident.name.rust_unqualified(); let mut bridges = methods .iter() diff --git a/crates/cxx-qt-gen/src/generator/rust/method.rs b/crates/cxx-qt-gen/src/generator/rust/method.rs index 66cdd6d80..d3649306b 100644 --- a/crates/cxx-qt-gen/src/generator/rust/method.rs +++ b/crates/cxx-qt-gen/src/generator/rust/method.rs @@ -19,7 +19,7 @@ pub fn generate_rust_methods( qobject_idents: &QObjectName, ) -> Result { let mut generated = GeneratedRustFragment::default(); - let cpp_class_name_rust = &qobject_idents.cpp_class.rust; + let cpp_class_name_rust = &qobject_idents.name.rust_unqualified(); for invokable in invokables { let idents = QMethodName::from(invokable); diff --git a/crates/cxx-qt-gen/src/generator/rust/property/getter.rs b/crates/cxx-qt-gen/src/generator/rust/property/getter.rs index d4174b51c..40b9df4cb 100644 --- a/crates/cxx-qt-gen/src/generator/rust/property/getter.rs +++ b/crates/cxx-qt-gen/src/generator/rust/property/getter.rs @@ -20,7 +20,7 @@ pub fn generate( cxx_ty: &Type, type_names: &TypeNames, ) -> Result { - let cpp_class_name_rust = &qobject_idents.cpp_class.rust; + let cpp_class_name_rust = &qobject_idents.name.rust_unqualified(); let getter_wrapper_cpp = idents.getter_wrapper.cpp.to_string(); let getter_rust = &idents.getter.rust; let ident = &idents.name.rust; diff --git a/crates/cxx-qt-gen/src/generator/rust/property/setter.rs b/crates/cxx-qt-gen/src/generator/rust/property/setter.rs index 109e242db..6fc9e46d2 100644 --- a/crates/cxx-qt-gen/src/generator/rust/property/setter.rs +++ b/crates/cxx-qt-gen/src/generator/rust/property/setter.rs @@ -20,7 +20,7 @@ pub fn generate( cxx_ty: &Type, type_names: &TypeNames, ) -> Result { - let cpp_class_name_rust = &qobject_idents.cpp_class.rust; + let cpp_class_name_rust = &qobject_idents.name.rust_unqualified(); let setter_wrapper_cpp = idents.setter_wrapper.cpp.to_string(); let setter_rust = &idents.setter.rust; let ident = &idents.name.rust; diff --git a/crates/cxx-qt-gen/src/generator/rust/property/signal.rs b/crates/cxx-qt-gen/src/generator/rust/property/signal.rs index 12ed1ae76..a0c5e15fa 100644 --- a/crates/cxx-qt-gen/src/generator/rust/property/signal.rs +++ b/crates/cxx-qt-gen/src/generator/rust/property/signal.rs @@ -13,7 +13,7 @@ use crate::{ pub fn generate(idents: &QPropertyName, qobject_idents: &QObjectName) -> ParsedSignal { // We build our signal in the generation phase as we need to use the naming // structs to build the signal name - let cpp_class_rust = &qobject_idents.cpp_class.rust; + let cpp_class_rust = &qobject_idents.name.rust_unqualified(); let notify_rust = &idents.notify.rust; let notify_cpp_str = &idents.notify.cpp.to_string(); let method: ForeignItemFn = syn::parse_quote! { @@ -24,6 +24,6 @@ pub fn generate(idents: &QPropertyName, qobject_idents: &QObjectName) -> ParsedS ParsedSignal::from_property_method( method, idents.notify.clone(), - qobject_idents.cpp_class.rust.clone(), + qobject_idents.name.rust_unqualified().clone(), ) } diff --git a/crates/cxx-qt-gen/src/generator/rust/qobject.rs b/crates/cxx-qt-gen/src/generator/rust/qobject.rs index 1866bb048..63ac8c823 100644 --- a/crates/cxx-qt-gen/src/generator/rust/qobject.rs +++ b/crates/cxx-qt-gen/src/generator/rust/qobject.rs @@ -29,7 +29,7 @@ impl GeneratedRustFragment { module_ident: &Ident, ) -> Result { // Create the base object - let qobject_idents = QObjectName::from(qobject); + let qobject_idents = QObjectName::from_qobject(qobject, type_names)?; let namespace_idents = NamespaceName::from(qobject); let mut generated = Self::default(); @@ -92,7 +92,8 @@ impl GeneratedRustFragment { // This could be implemented using an auto trait in the future once stable // https://doc.rust-lang.org/beta/unstable-book/language-features/auto-traits.html if qobject.locking { - let qualified_impl = type_names.rust_qualified(&qobject_idents.cpp_class.rust)?; + let qualified_impl = + type_names.rust_qualified(qobject_idents.name.rust_unqualified())?; generated.cxx_qt_mod_contents.push(syn::parse_quote! { impl cxx_qt::Locking for #qualified_impl {} }); @@ -118,17 +119,17 @@ fn generate_qobject_definitions( namespace: &str, ) -> Result { let mut generated = GeneratedRustFragment::default(); - let cpp_class_name_rust = &qobject_idents.cpp_class.rust; - let cpp_class_name_cpp = &qobject_idents.cpp_class.cpp; + let cpp_class_name_rust = &qobject_idents.name.rust_unqualified(); + let cpp_class_name_cpp = &qobject_idents.name.cxx_unqualified(); - let rust_struct_name_rust = &qobject_idents.rust_struct.rust; + let rust_struct_name_rust = &qobject_idents.rust_struct.rust_unqualified(); let rust_struct_name_rust_str = rust_struct_name_rust.to_string(); let namespace = if !namespace.is_empty() { Some(quote! { #[namespace=#namespace] }) } else { None }; - let cxx_name = if cpp_class_name_rust == cpp_class_name_cpp { + let cxx_name = if cpp_class_name_rust.to_string() == *cpp_class_name_cpp { quote! {} } else { let cpp_class_name_cpp = cpp_class_name_cpp.to_string(); diff --git a/crates/cxx-qt-gen/src/generator/rust/signals.rs b/crates/cxx-qt-gen/src/generator/rust/signals.rs index 3fa59a729..8581df77d 100644 --- a/crates/cxx-qt-gen/src/generator/rust/signals.rs +++ b/crates/cxx-qt-gen/src/generator/rust/signals.rs @@ -11,8 +11,7 @@ use crate::{ }, rust::fragment::{GeneratedRustFragment, RustFragmentPair}, }, - naming::rust::syn_type_cxx_bridge_to_qualified, - naming::TypeNames, + naming::{rust::syn_type_cxx_bridge_to_qualified, Name, TypeNames}, parser::signals::ParsedSignal, syntax::attribute::attribute_find_path, }; @@ -21,12 +20,14 @@ use syn::{parse_quote, FnArg, Ident, Result, Type}; pub fn generate_rust_signal( signal: &ParsedSignal, - qobject_name: &Ident, + qobject_name: &Name, type_names: &TypeNames, module_ident: &Ident, ) -> Result { let idents = QSignalName::from(signal); - let idents_helper = QSignalHelperName::new(&idents, qobject_name, type_names)?; + let idents_helper = QSignalHelperName::new(&idents, qobject_name)?; + + let qobject_name_rust = qobject_name.rust_unqualified(); let signal_name_cpp = idents.name.cpp; let signal_name_cpp_str = signal_name_cpp.to_string(); @@ -72,12 +73,12 @@ pub fn generate_rust_signal( .collect::>()?; let self_type_cxx = if signal.mutable { - parse_quote! { Pin<&mut #qobject_name> } + parse_quote! { Pin<&mut #qobject_name_rust> } } else { - parse_quote! { &#qobject_name } + parse_quote! { &#qobject_name_rust } }; let self_type_qualified = syn_type_cxx_bridge_to_qualified(&self_type_cxx, type_names)?; - let qualified_impl = type_names.rust_qualified(qobject_name)?; + let qualified_impl = qobject_name.rust_qualified(); let mut unsafe_block = None; let mut unsafe_call = Some(quote! { unsafe }); @@ -211,7 +212,6 @@ pub fn generate_rust_signals( module_ident: &Ident, ) -> Result { let mut generated = GeneratedRustFragment::default(); - let qobject_name = &qobject_idents.cpp_class.rust; // Create the methods for the other signals for signal in signals { @@ -235,7 +235,7 @@ pub fn generate_rust_signals( }; generated.append(&mut generate_rust_signal( &signal, - qobject_name, + &qobject_idents.name, type_names, module_ident, )?); @@ -907,9 +907,13 @@ mod tests { private: false, }; + let qobject_name = TypeNames::mock() + .lookup(&qsignal.qobject_ident) + .unwrap() + .clone(); let generated = generate_rust_signal( &qsignal, - &qsignal.qobject_ident, + &qobject_name, &TypeNames::mock(), &format_ident!("ffi"), ) @@ -1058,9 +1062,13 @@ mod tests { private: true, }; + let qobject_name = TypeNames::mock() + .lookup(&qsignal.qobject_ident) + .unwrap() + .clone(); let generated = generate_rust_signal( &qsignal, - &qsignal.qobject_ident, + &qobject_name, &TypeNames::mock(), &format_ident!("ffi"), ) diff --git a/crates/cxx-qt-gen/src/generator/rust/threading.rs b/crates/cxx-qt-gen/src/generator/rust/threading.rs index afb587f65..ee989f435 100644 --- a/crates/cxx-qt-gen/src/generator/rust/threading.rs +++ b/crates/cxx-qt-gen/src/generator/rust/threading.rs @@ -26,7 +26,7 @@ pub fn generate( ) -> Result { let mut blocks = GeneratedRustFragment::default(); - let cpp_struct_ident = &qobject_ident.cpp_class.rust; + let cpp_struct_ident = qobject_ident.name.rust_unqualified(); let cxx_qt_thread_ident = &qobject_ident.cxx_qt_thread_class; let cxx_qt_thread_queued_fn_ident = &qobject_ident.cxx_qt_thread_queued_fn_struct; let cxx_qt_thread_queue_fn = qobject_ident.cxx_qt_thread_method("queue_boxed_fn"); @@ -166,7 +166,7 @@ mod tests { #[test] fn test_generate_rust_threading() { let qobject = create_parsed_qobject(); - let qobject_idents = QObjectName::from(&qobject); + let qobject_idents = QObjectName::from_qobject(&qobject, &TypeNames::mock()).unwrap(); let namespace_ident = NamespaceName::from(&qobject); let generated = generate( diff --git a/crates/cxx-qt-gen/src/naming/name.rs b/crates/cxx-qt-gen/src/naming/name.rs index 5b48cbcef..bc93c7ac3 100644 --- a/crates/cxx-qt-gen/src/naming/name.rs +++ b/crates/cxx-qt-gen/src/naming/name.rs @@ -109,6 +109,17 @@ impl Name { &self.rust } + /// Get the qualified name of this type in Rust, including its source module. + pub fn rust_qualified(&self) -> Path { + if let Some(module) = &self.module { + let mut qualified_ident = module.clone(); + qualified_ident.segments.push(self.rust.clone().into()); + qualified_ident + } else { + Path::from(self.rust.clone()) + } + } + /// Set the namespace to the given value. /// /// Returns the previous value of the namespace. diff --git a/crates/cxx-qt-gen/src/naming/type_names.rs b/crates/cxx-qt-gen/src/naming/type_names.rs index 5bca89dd9..deb50fffa 100644 --- a/crates/cxx-qt-gen/src/naming/type_names.rs +++ b/crates/cxx-qt-gen/src/naming/type_names.rs @@ -322,15 +322,7 @@ impl TypeNames { /// Note that this only handles types that are declared inside this bridge. /// E.g. UniquePtr -> cxx::UniquePtr isn't handled here. pub fn rust_qualified(&self, ident: &Ident) -> Result { - self.lookup(ident).map(|name| { - if let Some(module) = &name.module { - let mut qualified_ident = module.clone(); - qualified_ident.segments.push(name.rust.clone().into()); - qualified_ident - } else { - Path::from(name.rust.clone()) - } - }) + self.lookup(ident).map(Name::rust_qualified) } fn duplicate_type(&self, ident: &Ident) -> Error {