Skip to content

Commit

Permalink
refactor: Separate trait parsing & structuring from QObject (#1052)
Browse files Browse the repository at this point in the history
* refactor: Separate trait parsing from QObject

Move the association between QObject and trait into the structuring
phase.
This should be one of the last things left in the structuring refactor.

Fix #938

* Remove pre-processing of QObjects during parsing.

Now that we've moved everything into the structuring phase, this is no
longer necessary and simplifies parsing a lot.

* Change qobjects to be a Vec instead of BTreeMap, including writing a quick lookup function

---------

Co-authored-by: Ben Ford <ben.ford@kdab.com>
  • Loading branch information
LeonMatthesKDAB and BenFordTytherington authored Aug 30, 2024
1 parent bfba2d0 commit 4e48578
Show file tree
Hide file tree
Showing 16 changed files with 673 additions and 648 deletions.
10 changes: 5 additions & 5 deletions crates/cxx-qt-gen/src/generator/cpp/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn expand_arguments(arguments: &[Type], type_names: &TypeNames) -> Result<String

pub fn generate(
qobject: &GeneratedCppQObject,
constructors: &[Constructor],
constructors: &[&Constructor],
base_class: String,
class_initializers: &[String],
type_names: &TypeNames,
Expand Down Expand Up @@ -287,7 +287,7 @@ mod tests {
fn constructor_without_base_arguments() {
let blocks = generate(
&qobject_for_testing(),
&[Constructor {
&[&Constructor {
arguments: vec![parse_quote! { i32 }, parse_quote! { *mut QObject }],
..mock_constructor()
}],
Expand Down Expand Up @@ -333,7 +333,7 @@ mod tests {
fn constructor_with_all_arguments() {
let blocks = generate(
&qobject_for_testing(),
&[Constructor {
&[&Constructor {
arguments: vec![parse_quote! { i8 }, parse_quote! { i16 }],
new_arguments: vec![parse_quote! { i16}, parse_quote! { i32 }],
initialize_arguments: vec![parse_quote! { i32 }, parse_quote! { i64 }],
Expand Down Expand Up @@ -385,11 +385,11 @@ mod tests {
let blocks = generate(
&qobject_for_testing(),
&[
Constructor {
&Constructor {
arguments: vec![],
..mock_constructor()
},
Constructor {
&Constructor {
arguments: vec![parse_quote! { *mut QObject }],
base_arguments: vec![parse_quote! { *mut QObject }],
..mock_constructor()
Expand Down
8 changes: 4 additions & 4 deletions crates/cxx-qt-gen/src/generator/cpp/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,23 @@ impl GeneratedCppQObject {
// If this type has threading enabled then add generation
//
// Note that threading also includes locking C++ generation
if qobject.threading {
if structured_qobject.threading {
// The parser phase should check that this is true
debug_assert!(qobject.locking);
debug_assert!(structured_qobject.locking);

let (initializer, mut blocks) = threading::generate(&qobject_idents)?;
generated.blocks.append(&mut blocks);
class_initializers.push(initializer);
// If this type has locking enabled then add generation
} else if qobject.locking {
} else if structured_qobject.locking {
let (initializer, mut blocks) = locking::generate()?;
generated.blocks.append(&mut blocks);
class_initializers.push(initializer);
}

generated.blocks.append(&mut constructor::generate(
&generated,
&qobject.constructors,
&structured_qobject.constructors,
base_class,
&class_initializers,
type_names,
Expand Down
10 changes: 5 additions & 5 deletions crates/cxx-qt-gen/src/generator/rust/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn unsafe_if(condition: bool) -> Option<TokenStream> {
}

pub fn generate(
constructors: &[Constructor],
constructors: &[&Constructor],
qobject_idents: &QObjectNames,
namespace: &NamespaceName,
type_names: &TypeNames,
Expand Down Expand Up @@ -463,7 +463,7 @@ mod tests {
NamespaceName::from_namespace_and_ident("qobject", &format_ident!("MyObject"))
}

fn generate_mocked(constructors: &[Constructor]) -> GeneratedRustFragment {
fn generate_mocked(constructors: &[&Constructor]) -> GeneratedRustFragment {
let mut type_names = TypeNames::mock();

type_names.mock_insert("QString", None, None, None);
Expand Down Expand Up @@ -768,8 +768,8 @@ mod tests {
#[test]
fn multiple_constructors() {
let blocks = generate_mocked(&[
mock_constructor(),
Constructor {
&mock_constructor(),
&Constructor {
arguments: vec![parse_quote! { *const QObject }],
new_arguments: vec![parse_quote! { i16 }],
initialize_arguments: vec![
Expand Down Expand Up @@ -801,7 +801,7 @@ mod tests {
#[test]
fn constructor_impl_with_unused_lifetime() {
let result = super::generate(
&[Constructor {
&[&Constructor {
lifetime: Some(parse_quote! { 'a }),
..mock_constructor()
}],
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/src/generator/rust/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl GeneratedRustFragment {
}

// If this type has threading enabled then add generation
if qobject.threading {
if structured_qobject.threading {
generated.append(&mut threading::generate(
&qobject_idents,
&namespace_idents,
Expand All @@ -94,7 +94,7 @@ 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 {
if structured_qobject.locking {
let qualified_impl =
type_names.rust_qualified(qobject_idents.name.rust_unqualified())?;
generated.cxx_qt_mod_contents.push(syn::parse_quote! {
Expand All @@ -103,7 +103,7 @@ impl GeneratedRustFragment {
}

generated.append(&mut constructor::generate(
&qobject.constructors,
&structured_qobject.constructors,
&qobject_idents,
&namespace_idents,
type_names,
Expand Down
Loading

0 comments on commit 4e48578

Please sign in to comment.