Skip to content

Commit 4132a2b

Browse files
Extract former CXXMappings into TypeNames struct
This is the first stage of the Naming phase (#835) refactor. All associations between types and their fully qualified names are now stored in one central database, the TypeNames struct. The final goal for this struct is to include all fully qualified names for each nameable type in the bridge. Therefore the generator can use this to lookup any references from e.g. arguments. This is not quite done yet, as this isn't yet used everywhere, but it's a good first step
1 parent 6d61f03 commit 4132a2b

20 files changed

+741
-732
lines changed

crates/cxx-qt-gen/src/generator/cpp/constructor.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use super::qobject::GeneratedCppQObjectBlocks;
77
use crate::{
88
generator::{cpp::GeneratedCppQObject, utils::cpp::syn_type_to_cpp_type},
9-
parser::{constructor::Constructor, mappings::ParsedCxxMappings},
9+
parser::{constructor::Constructor, naming::TypeNames},
1010
CppFragment,
1111
};
1212

@@ -48,11 +48,11 @@ fn argument_names(arguments: &[Type]) -> Vec<String> {
4848
.collect()
4949
}
5050

51-
fn expand_arguments(arguments: &[Type], cxx_mappings: &ParsedCxxMappings) -> Result<String> {
51+
fn expand_arguments(arguments: &[Type], type_names: &TypeNames) -> Result<String> {
5252
Ok(arguments
5353
.iter()
5454
.zip(argument_names(arguments).into_iter())
55-
.map(|(ty, name)| syn_type_to_cpp_type(ty, cxx_mappings).map(|ty| format!("{ty} {name}")))
55+
.map(|(ty, name)| syn_type_to_cpp_type(ty, type_names).map(|ty| format!("{ty} {name}")))
5656
.collect::<Result<Vec<_>>>()?
5757
.join(", "))
5858
}
@@ -62,7 +62,7 @@ pub fn generate(
6262
constructors: &[Constructor],
6363
base_class: String,
6464
class_initializers: &[String],
65-
cxx_mappings: &ParsedCxxMappings,
65+
type_names: &TypeNames,
6666
) -> Result<GeneratedCppQObjectBlocks> {
6767
let initializers = class_initializers
6868
.iter()
@@ -80,7 +80,7 @@ pub fn generate(
8080
let rust_obj = qobject.rust_ident.as_str();
8181
let namespace_internals = &qobject.namespace_internals;
8282
for (index, constructor) in constructors.iter().enumerate() {
83-
let argument_list = expand_arguments(&constructor.arguments, cxx_mappings)?;
83+
let argument_list = expand_arguments(&constructor.arguments, type_names)?;
8484
let constructor_argument_names = argument_names(&constructor.arguments);
8585

8686
generated.methods.push(CppFragment::Pair {
@@ -171,7 +171,7 @@ mod tests {
171171
&[],
172172
"BaseClass".to_owned(),
173173
&["member1(1)".to_string(), "member2{ 2 }".to_string()],
174-
&ParsedCxxMappings::default(),
174+
&TypeNames::default(),
175175
)
176176
.unwrap();
177177

@@ -201,7 +201,7 @@ mod tests {
201201
&[],
202202
"BaseClass".to_owned(),
203203
&[],
204-
&ParsedCxxMappings::default(),
204+
&TypeNames::default(),
205205
)
206206
.unwrap();
207207

@@ -233,7 +233,7 @@ mod tests {
233233
}],
234234
"BaseClass".to_owned(),
235235
&[],
236-
&ParsedCxxMappings::default(),
236+
&TypeNames::default(),
237237
)
238238
.unwrap();
239239

@@ -283,7 +283,7 @@ mod tests {
283283
}],
284284
"BaseClass".to_owned(),
285285
&["initializer".to_string()],
286-
&ParsedCxxMappings::default(),
286+
&TypeNames::default(),
287287
)
288288
.unwrap();
289289

@@ -337,7 +337,7 @@ mod tests {
337337
],
338338
"BaseClass".to_owned(),
339339
&["initializer".to_string()],
340-
&ParsedCxxMappings::default(),
340+
&TypeNames::default(),
341341
)
342342
.unwrap();
343343

crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use crate::{
77
generator::cpp::signal::generate_cpp_signal,
8-
parser::{externcxxqt::ParsedExternCxxQt, mappings::ParsedCxxMappings},
8+
parser::{externcxxqt::ParsedExternCxxQt, naming::TypeNames},
99
CppFragment,
1010
};
1111
use std::collections::BTreeSet;
@@ -23,14 +23,14 @@ pub struct GeneratedCppExternCxxQtBlocks {
2323

2424
pub fn generate(
2525
blocks: &[ParsedExternCxxQt],
26-
cxx_mappings: &ParsedCxxMappings,
26+
type_names: &TypeNames,
2727
) -> Result<Vec<GeneratedCppExternCxxQtBlocks>> {
2828
let mut out = vec![];
2929

3030
for block in blocks {
3131
for signal in &block.signals {
3232
let mut block = GeneratedCppExternCxxQtBlocks::default();
33-
let data = generate_cpp_signal(signal, &signal.qobject_ident, cxx_mappings)?;
33+
let data = generate_cpp_signal(signal, &signal.qobject_ident, type_names)?;
3434
block.includes = data.includes;
3535
// Ensure that we include MaybeLockGuard<T> that is used in multiple places
3636
block
@@ -66,7 +66,7 @@ mod tests {
6666
}
6767
})
6868
.unwrap()];
69-
let generated = generate(&blocks, &ParsedCxxMappings::default()).unwrap();
69+
let generated = generate(&blocks, &TypeNames::default()).unwrap();
7070
assert_eq!(generated.len(), 2);
7171
}
7272

@@ -83,15 +83,15 @@ mod tests {
8383
}
8484
})
8585
.unwrap()];
86-
let mut cxx_mappings = ParsedCxxMappings::default();
87-
cxx_mappings
86+
let mut type_names = TypeNames::default();
87+
type_names
8888
.cxx_names
8989
.insert("ObjRust".to_owned(), "ObjCpp".to_owned());
90-
cxx_mappings
90+
type_names
9191
.namespaces
9292
.insert("ObjRust".to_owned(), "mynamespace".to_owned());
9393

94-
let generated = generate(&blocks, &cxx_mappings).unwrap();
94+
let generated = generate(&blocks, &type_names).unwrap();
9595
assert_eq!(generated.len(), 1);
9696
}
9797
}

crates/cxx-qt-gen/src/generator/cpp/inherit.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ use crate::{
99
cpp::{fragment::CppFragment, qobject::GeneratedCppQObjectBlocks},
1010
utils::cpp::syn_type_to_cpp_return_type,
1111
},
12-
parser::{inherit::ParsedInheritedMethod, mappings::ParsedCxxMappings},
12+
parser::{inherit::ParsedInheritedMethod, naming::TypeNames},
1313
};
1414

1515
use syn::Result;
1616

1717
pub fn generate(
1818
inherited_methods: &[ParsedInheritedMethod],
1919
base_class: &Option<String>,
20-
cxx_mappings: &ParsedCxxMappings,
20+
type_names: &TypeNames,
2121
) -> Result<GeneratedCppQObjectBlocks> {
2222
let mut result = GeneratedCppQObjectBlocks::default();
2323

2424
for method in inherited_methods {
25-
let return_type = syn_type_to_cpp_return_type(&method.method.sig.output, cxx_mappings)?;
25+
let return_type = syn_type_to_cpp_return_type(&method.method.sig.output, type_names)?;
2626
let base_class = base_class.as_deref().unwrap_or("QObject");
2727

2828
result.methods.push(CppFragment::Header(formatdoc! {
@@ -58,11 +58,7 @@ mod tests {
5858
) -> Result<GeneratedCppQObjectBlocks> {
5959
let inherited_methods = vec![ParsedInheritedMethod::parse(method, Safety::Safe).unwrap()];
6060
let base_class = base_class.map(|s| s.to_owned());
61-
generate(
62-
&inherited_methods,
63-
&base_class,
64-
&ParsedCxxMappings::default(),
65-
)
61+
generate(&inherited_methods, &base_class, &TypeNames::default())
6662
}
6763

6864
fn assert_generated_eq(expected: &str, generated: &GeneratedCppQObjectBlocks) {

crates/cxx-qt-gen/src/generator/cpp/method.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ use crate::{
1515
},
1616
},
1717
parser::{
18-
mappings::ParsedCxxMappings,
1918
method::{ParsedMethod, ParsedQInvokableSpecifiers},
19+
naming::TypeNames,
2020
},
2121
};
2222
use indoc::formatdoc;
@@ -25,14 +25,13 @@ use syn::{spanned::Spanned, Error, FnArg, Pat, PatIdent, PatType, Result};
2525
pub fn generate_cpp_methods(
2626
invokables: &Vec<ParsedMethod>,
2727
qobject_idents: &QObjectName,
28-
cxx_mappings: &ParsedCxxMappings,
28+
type_names: &TypeNames,
2929
) -> Result<GeneratedCppQObjectBlocks> {
3030
let mut generated = GeneratedCppQObjectBlocks::default();
3131
let qobject_ident = qobject_idents.cpp_class.cpp.to_string();
3232
for invokable in invokables {
3333
let idents = QMethodName::from(invokable);
34-
let return_cxx_ty =
35-
syn_type_to_cpp_return_type(&invokable.method.sig.output, cxx_mappings)?;
34+
let return_cxx_ty = syn_type_to_cpp_return_type(&invokable.method.sig.output, type_names)?;
3635

3736
let parameters: Vec<CppNamedType> = invokable
3837
.method
@@ -54,7 +53,7 @@ pub fn generate_cpp_methods(
5453
} else {
5554
Ok(Some(CppNamedType {
5655
ident: ident.to_string(),
57-
ty: syn_type_to_cpp_type(ty, cxx_mappings)?,
56+
ty: syn_type_to_cpp_type(ty, type_names)?,
5857
}))
5958
}
6059
} else {
@@ -232,8 +231,7 @@ mod tests {
232231
let qobject_idents = create_qobjectname();
233232

234233
let generated =
235-
generate_cpp_methods(&invokables, &qobject_idents, &ParsedCxxMappings::default())
236-
.unwrap();
234+
generate_cpp_methods(&invokables, &qobject_idents, &TypeNames::default()).unwrap();
237235

238236
// methods
239237
assert_eq!(generated.methods.len(), 5);
@@ -401,15 +399,11 @@ mod tests {
401399
}];
402400
let qobject_idents = create_qobjectname();
403401

404-
let mut cxx_mappings = ParsedCxxMappings::default();
405-
cxx_mappings
406-
.cxx_names
407-
.insert("A".to_owned(), "A1".to_owned());
408-
cxx_mappings
409-
.cxx_names
410-
.insert("B".to_owned(), "B2".to_owned());
402+
let mut type_names = TypeNames::default();
403+
type_names.cxx_names.insert("A".to_owned(), "A1".to_owned());
404+
type_names.cxx_names.insert("B".to_owned(), "B2".to_owned());
411405

412-
let generated = generate_cpp_methods(&invokables, &qobject_idents, &cxx_mappings).unwrap();
406+
let generated = generate_cpp_methods(&invokables, &qobject_idents, &type_names).unwrap();
413407

414408
// methods
415409
assert_eq!(generated.methods.len(), 1);

crates/cxx-qt-gen/src/generator/cpp/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ impl GeneratedCppBlocks {
6363
.cxx_qt_data
6464
.qobjects
6565
.values()
66-
.map(|qobject| GeneratedCppQObject::from(qobject, &parser.cxx_qt_data.cxx_mappings))
66+
.map(|qobject| GeneratedCppQObject::from(qobject, &parser.type_names))
6767
.collect::<Result<Vec<GeneratedCppQObject>>>()?,
6868
extern_cxx_qt: externcxxqt::generate(
6969
&parser.cxx_qt_data.extern_cxxqt_blocks,
70-
&parser.cxx_qt_data.cxx_mappings,
70+
&parser.type_names,
7171
)?,
7272
})
7373
}

crates/cxx-qt-gen/src/generator/cpp/property/mod.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::generator::{
88
naming::{property::QPropertyName, qobject::QObjectName},
99
utils::cpp::syn_type_to_cpp_type,
1010
};
11-
use crate::parser::{mappings::ParsedCxxMappings, property::ParsedQProperty};
11+
use crate::parser::{naming::TypeNames, property::ParsedQProperty};
1212
use syn::Result;
1313

1414
mod getter;
@@ -19,7 +19,7 @@ mod signal;
1919
pub fn generate_cpp_properties(
2020
properties: &Vec<ParsedQProperty>,
2121
qobject_idents: &QObjectName,
22-
cxx_mappings: &ParsedCxxMappings,
22+
type_names: &TypeNames,
2323
) -> Result<GeneratedCppQObjectBlocks> {
2424
let mut generated = GeneratedCppQObjectBlocks::default();
2525
let mut signals = vec![];
@@ -28,7 +28,7 @@ pub fn generate_cpp_properties(
2828
for property in properties {
2929
// Cache the idents as they are used in multiple places
3030
let idents = QPropertyName::from(property);
31-
let cxx_ty = syn_type_to_cpp_type(&property.ty, cxx_mappings)?;
31+
let cxx_ty = syn_type_to_cpp_type(&property.ty, type_names)?;
3232

3333
generated.metaobjects.push(meta::generate(&idents, &cxx_ty));
3434
generated
@@ -49,7 +49,7 @@ pub fn generate_cpp_properties(
4949
generated.append(&mut generate_cpp_signals(
5050
&signals,
5151
qobject_idents,
52-
cxx_mappings,
52+
type_names,
5353
)?);
5454

5555
Ok(generated)
@@ -81,8 +81,7 @@ mod tests {
8181
let qobject_idents = create_qobjectname();
8282

8383
let generated =
84-
generate_cpp_properties(&properties, &qobject_idents, &ParsedCxxMappings::default())
85-
.unwrap();
84+
generate_cpp_properties(&properties, &qobject_idents, &TypeNames::default()).unwrap();
8685

8786
// metaobjects
8887
assert_eq!(generated.metaobjects.len(), 2);
@@ -360,13 +359,10 @@ mod tests {
360359
}];
361360
let qobject_idents = create_qobjectname();
362361

363-
let mut cxx_mapping = ParsedCxxMappings::default();
364-
cxx_mapping
365-
.cxx_names
366-
.insert("A".to_owned(), "A1".to_owned());
362+
let mut type_names = TypeNames::default();
363+
type_names.cxx_names.insert("A".to_owned(), "A1".to_owned());
367364

368-
let generated =
369-
generate_cpp_properties(&properties, &qobject_idents, &cxx_mapping).unwrap();
365+
let generated = generate_cpp_properties(&properties, &qobject_idents, &type_names).unwrap();
370366

371367
// metaobjects
372368
assert_eq!(generated.metaobjects.len(), 1);

crates/cxx-qt-gen/src/generator/cpp/qenum.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syn::Result;
1010

1111
use crate::{
1212
generator::utils::cpp::Indent,
13-
parser::{mappings::ParsedCxxMappings, qenum::ParsedQEnum},
13+
parser::{naming::TypeNames, qenum::ParsedQEnum},
1414
writer::cpp::namespaced,
1515
};
1616

@@ -52,15 +52,15 @@ pub fn generate_declaration(qenum: &ParsedQEnum, includes: &mut BTreeSet<String>
5252

5353
pub fn generate(
5454
qenums: &[ParsedQEnum],
55-
cxx_mappings: &ParsedCxxMappings,
55+
type_names: &TypeNames,
5656
) -> Result<GeneratedCppQObjectBlocks> {
5757
let mut generated = GeneratedCppQObjectBlocks::default();
5858

5959
for qenum in qenums {
6060
let enum_name = &qenum.ident.to_string();
6161

62-
let mut qualified_name = cxx_mappings.cxx(enum_name);
63-
// TODO: this is a workaround for cxx_mappings.cxx not always returning a fully-qualified
62+
let mut qualified_name = type_names.cxx(enum_name);
63+
// TODO: this is a workaround for type_names.cxx not always returning a fully-qualified
6464
// identifier.
6565
// Once https://github.com/KDAB/cxx-qt/issues/619 is fixed, this can be removed.
6666
if !qualified_name.starts_with("::") {
@@ -102,7 +102,7 @@ mod tests {
102102
})
103103
.unwrap()];
104104

105-
let generated = generate(&qenums, &ParsedCxxMappings::default()).unwrap();
105+
let generated = generate(&qenums, &TypeNames::default()).unwrap();
106106
assert_eq!(generated.includes.len(), 1);
107107
assert!(generated.includes.contains("#include <cstdint>"));
108108
assert_eq!(generated.metaobjects.len(), 1);

0 commit comments

Comments
 (0)