Skip to content

Commit

Permalink
cxx-qt-gen: pass ParsedCxxMappings to signals
Browse files Browse the repository at this point in the history
This allows us later to query for more than just the qualified
mappings, eg the namespace.
  • Loading branch information
ahayzen-kdab committed Sep 21, 2023
1 parent 98720c9 commit f71c81a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/generator/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl GeneratedRustBlocks {
.map(|qobject| {
GeneratedRustQObject::from(
qobject,
&parser.cxx_qt_data.cxx_mappings.qualified,
&parser.cxx_qt_data.cxx_mappings,
&parser.passthrough_module.ident,
)
})
Expand Down
32 changes: 19 additions & 13 deletions crates/cxx-qt-gen/src/generator/rust/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ use crate::{
naming::{property::QPropertyName, qobject::QObjectName},
rust::qobject::GeneratedRustQObject,
},
parser::property::ParsedQProperty,
parser::{mappings::ParsedCxxMappings, property::ParsedQProperty},
};
use std::collections::BTreeMap;
use syn::{Ident, Path, Result};
use syn::Result;

use super::signals::generate_rust_signals;

pub fn generate_rust_properties(
properties: &Vec<ParsedQProperty>,
qobject_idents: &QObjectName,
qualified_mappings: &BTreeMap<Ident, Path>,
cxx_mappings: &ParsedCxxMappings,
) -> Result<GeneratedRustQObject> {
let mut generated = GeneratedRustQObject::default();
let mut signals = vec![];
Expand All @@ -31,7 +30,12 @@ pub fn generate_rust_properties(
let idents = QPropertyName::from(property);

// Getters
let getter = getter::generate(&idents, qobject_idents, &property.ty, qualified_mappings);
let getter = getter::generate(
&idents,
qobject_idents,
&property.ty,
&cxx_mappings.qualified,
);
generated
.cxx_mod_contents
.append(&mut getter.cxx_bridge_as_items()?);
Expand All @@ -40,7 +44,12 @@ pub fn generate_rust_properties(
.append(&mut getter.implementation_as_items()?);

// Setters
let setter = setter::generate(&idents, qobject_idents, &property.ty, qualified_mappings);
let setter = setter::generate(
&idents,
qobject_idents,
&property.ty,
&cxx_mappings.qualified,
);
generated
.cxx_mod_contents
.append(&mut setter.cxx_bridge_as_items()?);
Expand All @@ -55,7 +64,7 @@ pub fn generate_rust_properties(
generated.append(&mut generate_rust_signals(
&signals,
qobject_idents,
qualified_mappings,
cxx_mappings,
)?);

Ok(generated)
Expand Down Expand Up @@ -87,12 +96,9 @@ mod tests {
];
let qobject_idents = create_qobjectname();

let generated = generate_rust_properties(
&properties,
&qobject_idents,
&BTreeMap::<Ident, Path>::default(),
)
.unwrap();
let generated =
generate_rust_properties(&properties, &qobject_idents, &ParsedCxxMappings::default())
.unwrap();

// Check that we have the expected number of blocks
assert_eq!(generated.cxx_mod_contents.len(), 12);
Expand Down
22 changes: 10 additions & 12 deletions crates/cxx-qt-gen/src/generator/rust/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::collections::BTreeMap;

use crate::{
generator::{
naming::{namespace::NamespaceName, qobject::QObjectName},
Expand All @@ -15,10 +13,10 @@ use crate::{
},
utils::rust::syn_ident_cxx_bridge_to_qualified_impl,
},
parser::qobject::ParsedQObject,
parser::{mappings::ParsedCxxMappings, qobject::ParsedQObject},
};
use quote::quote;
use syn::{Ident, Item, Path, Result};
use syn::{Ident, Item, Result};

use super::qenum;

Expand All @@ -39,7 +37,7 @@ impl GeneratedRustQObject {

pub fn from(
qobject: &ParsedQObject,
qualified_mappings: &BTreeMap<Ident, Path>,
cxx_mappings: &ParsedCxxMappings,
module_ident: &Ident,
) -> Result<GeneratedRustQObject> {
// Create the base object
Expand All @@ -56,7 +54,7 @@ impl GeneratedRustQObject {
generated.append(&mut generate_rust_properties(
&qobject.properties,
&qobject_idents,
qualified_mappings,
cxx_mappings,
)?);
generated.append(&mut generate_rust_methods(
&qobject.methods,
Expand All @@ -69,7 +67,7 @@ impl GeneratedRustQObject {
generated.append(&mut generate_rust_signals(
&qobject.signals,
&qobject_idents,
qualified_mappings,
cxx_mappings,
)?);
generated.append(&mut qenum::generate(&qobject.qenums));

Expand All @@ -95,7 +93,7 @@ impl GeneratedRustQObject {
generated.append(&mut threading::generate(
&qobject_idents,
&namespace_idents,
qualified_mappings,
&cxx_mappings.qualified,
module_ident,
)?);
}
Expand All @@ -107,7 +105,7 @@ impl GeneratedRustQObject {
if qobject.locking {
let qualified_impl = syn_ident_cxx_bridge_to_qualified_impl(
&qobject_idents.cpp_class.rust,
qualified_mappings,
&cxx_mappings.qualified,
);
generated.cxx_qt_mod_contents.push(syn::parse_quote! {
impl cxx_qt::Locking for #qualified_impl {}
Expand All @@ -118,13 +116,13 @@ impl GeneratedRustQObject {
&qobject.constructors,
&qobject_idents,
&namespace_idents,
qualified_mappings,
&cxx_mappings.qualified,
module_ident,
)?);

generated.append(&mut cxxqttype::generate(
&qobject_idents,
qualified_mappings,
&cxx_mappings.qualified,
)?);

Ok(generated)
Expand Down Expand Up @@ -202,7 +200,7 @@ mod tests {

let rust = GeneratedRustQObject::from(
parser.cxx_qt_data.qobjects.values().next().unwrap(),
&BTreeMap::<Ident, Path>::default(),
&ParsedCxxMappings::default(),
&format_ident!("ffi"),
)
.unwrap();
Expand Down
20 changes: 9 additions & 11 deletions crates/cxx-qt-gen/src/generator/rust/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::collections::BTreeMap;

use crate::{
generator::{
naming::{
Expand All @@ -20,7 +18,7 @@ use crate::{
parser::{mappings::ParsedCxxMappings, signals::ParsedSignal},
};
use quote::{format_ident, quote};
use syn::{parse_quote, FnArg, Ident, Path, Result};
use syn::{parse_quote, FnArg, Ident, Result};

pub fn generate_rust_free_signal(
signal: &ParsedSignal,
Expand Down Expand Up @@ -151,7 +149,7 @@ pub fn generate_rust_free_signal(
pub fn generate_rust_signals(
signals: &Vec<ParsedSignal>,
qobject_idents: &QObjectName,
qualified_mappings: &BTreeMap<Ident, Path>,
cxx_mappings: &ParsedCxxMappings,
) -> Result<GeneratedRustQObject> {
let mut generated = GeneratedRustQObject::default();
let qobject_name = &qobject_idents.cpp_class.rust;
Expand Down Expand Up @@ -183,7 +181,7 @@ pub fn generate_rust_signals(
.map(|mut parameter| {
if let FnArg::Typed(pat_type) = &mut parameter {
*pat_type.ty =
syn_type_cxx_bridge_to_qualified(&pat_type.ty, qualified_mappings);
syn_type_cxx_bridge_to_qualified(&pat_type.ty, &cxx_mappings.qualified);
}
parameter
})
Expand All @@ -195,9 +193,9 @@ pub fn generate_rust_signals(
parse_quote! { &#qobject_name }
};
let self_type_qualified =
syn_type_cxx_bridge_to_qualified(&self_type_cxx, qualified_mappings);
syn_type_cxx_bridge_to_qualified(&self_type_cxx, &cxx_mappings.qualified);
let qualified_impl =
syn_ident_cxx_bridge_to_qualified_impl(qobject_name, qualified_mappings);
syn_ident_cxx_bridge_to_qualified_impl(qobject_name, &cxx_mappings.qualified);

let mut unsafe_block = None;
let mut unsafe_call = Some(quote! { unsafe });
Expand Down Expand Up @@ -286,7 +284,7 @@ mod tests {
let generated = generate_rust_signals(
&vec![qsignal],
&qobject_idents,
&BTreeMap::<Ident, Path>::default(),
&ParsedCxxMappings::default(),
)
.unwrap();

Expand Down Expand Up @@ -366,7 +364,7 @@ mod tests {
let generated = generate_rust_signals(
&vec![qsignal],
&qobject_idents,
&BTreeMap::<Ident, Path>::default(),
&ParsedCxxMappings::default(),
)
.unwrap();

Expand Down Expand Up @@ -440,7 +438,7 @@ mod tests {
let generated = generate_rust_signals(
&vec![qsignal],
&qobject_idents,
&BTreeMap::<Ident, Path>::default(),
&ParsedCxxMappings::default(),
)
.unwrap();

Expand Down Expand Up @@ -511,7 +509,7 @@ mod tests {
let generated = generate_rust_signals(
&vec![qsignal],
&qobject_idents,
&BTreeMap::<Ident, Path>::default(),
&ParsedCxxMappings::default(),
)
.unwrap();

Expand Down

0 comments on commit f71c81a

Please sign in to comment.