Skip to content

Commit

Permalink
Move name into InvokableFields to further simplify parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFordTytherington committed Aug 30, 2024
1 parent 2a43203 commit b9fb0db
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
17 changes: 6 additions & 11 deletions crates/cxx-qt-gen/src/parser/inherit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::parser::{
use crate::{
naming::Name,
parser::parameter::ParsedFunctionParameter,
syntax::{attribute::attribute_take_path, expr::expr_to_string, safety::Safety},
syntax::{attribute::attribute_take_path, safety::Safety},
};
use quote::format_ident;
use syn::{Attribute, ForeignItemFn, Ident, Result};
Expand Down Expand Up @@ -45,25 +45,20 @@ impl ParsedInheritedMethod {
let docs = separate_docs(&mut method);
let invokable_fields = extract_common_fields(&method, docs)?;

let mut name =
Name::from_rust_ident_and_attrs(&method.sig.ident, &method.attrs, None, None)?;
// This block seems unnecessary but since attrs are passed through on generator/rust/inherit.rs a duplicate attr would occur without it
attribute_take_path(&mut method.attrs, &["cxx_name"]);

// This block seems unnecessary but removing it causes one cxx_name attr in test_outputs to lose the CxxQtInherit suffix
if let Some(attr) = attribute_take_path(&mut method.attrs, &["cxx_name"]) {
name = name.with_cxx_name(expr_to_string(&attr.meta.require_name_value()?.value)?);
}

Ok(Self::from_invokable_fields(invokable_fields, method, name))
Ok(Self::from_invokable_fields(invokable_fields, method))
}

fn from_invokable_fields(fields: InvokableFields, method: ForeignItemFn, name: Name) -> Self {
fn from_invokable_fields(fields: InvokableFields, method: ForeignItemFn) -> Self {
Self {
method,
qobject_ident: fields.qobject_ident,
mutable: fields.mutable,
safe: fields.safe,
parameters: fields.parameters,
name,
name: fields.name,
docs: fields.docs,
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/cxx-qt-gen/src/parser/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ impl ParsedMethod {
let docs = separate_docs(&mut method);
let invokable_fields = extract_common_fields(&method, docs)?;

let name = Name::from_rust_ident_and_attrs(&method.sig.ident, &method.attrs, None, None)?;

if name.namespace().is_some() {
if invokable_fields.name.namespace().is_some() {
return Err(Error::new_spanned(
method.sig.ident,
"Methods / QInvokables cannot have a namespace attribute",
Expand All @@ -90,14 +88,13 @@ impl ParsedMethod {
ParsedQInvokableSpecifiers::Virtual,
] {
if attribute_take_path(&mut method.attrs, specifier.as_str_slice()).is_some() {
specifiers.insert(specifier);
specifiers.insert(specifier); // Should a fn be able to be Override AND Virtual?
}
}

Ok(ParsedMethod::from_invokable_fields(
invokable_fields,
method,
name,
specifiers,
is_qinvokable,
))
Expand All @@ -106,7 +103,6 @@ impl ParsedMethod {
fn from_invokable_fields(
fields: InvokableFields,
method: ForeignItemFn,
name: Name,
specifiers: HashSet<ParsedQInvokableSpecifiers>,
is_qinvokable: bool,
) -> Self {
Expand All @@ -118,7 +114,7 @@ impl ParsedMethod {
parameters: fields.parameters,
specifiers,
is_qinvokable,
name,
name: fields.name,
docs: fields.docs,
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/cxx-qt-gen/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct InvokableFields {
mutable: bool,
parameters: Vec<ParsedFunctionParameter>,
safe: bool,
name: Name,
docs: Vec<Attribute>,
}

Expand All @@ -69,11 +70,14 @@ pub fn extract_common_fields(

let parameters = ParsedFunctionParameter::parse_all_ignoring_receiver(&method.sig)?;
let safe = method.sig.unsafety.is_none();
let name = Name::from_rust_ident_and_attrs(&method.sig.ident, &method.attrs, None, None)?;

Ok(InvokableFields {
qobject_ident,
mutable,
parameters,
safe,
name,
docs,
})
}
Expand Down
8 changes: 2 additions & 6 deletions crates/cxx-qt-gen/src/parser/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ impl ParsedSignal {
let docs = separate_docs(&mut method);
let invokable_fields = extract_common_fields(&method, docs)?;

let name = Name::from_rust_ident_and_attrs(&method.sig.ident, &method.attrs, None, None)?;

if name.namespace().is_some() {
if invokable_fields.name.namespace().is_some() {
return Err(Error::new_spanned(
method.sig.ident,
"Signals cannot have a namespace attribute",
Expand All @@ -115,7 +113,6 @@ impl ParsedSignal {
Ok(Self::from_invokable_fields(
invokable_fields,
method,
name,
inherit,
private,
))
Expand All @@ -124,7 +121,6 @@ impl ParsedSignal {
fn from_invokable_fields(
fields: InvokableFields,
method: ForeignItemFn,
name: Name,
inherit: bool,
private: bool,
) -> Self {
Expand All @@ -134,7 +130,7 @@ impl ParsedSignal {
mutable: fields.mutable,
safe: fields.safe,
parameters: fields.parameters,
name,
name: fields.name,
inherit,
private,
docs: fields.docs,
Expand Down

0 comments on commit b9fb0db

Please sign in to comment.