Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: generate attributes on trait fns #1462

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/codegen/child_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,18 @@ fn generate_func(

doc_hidden(w, prop.doc_hidden, comment_prefix, indent)?;
let decl = declaration(env, prop, is_get);
if !in_trait || only_declaration {
let add_doc_alias = if is_get {
prop.name != prop.getter_name && prop.name != prop.prop_name
} else {
prop.name != prop.prop_name
};
if add_doc_alias {
doc_alias(
w,
&format!("{}.{}", &prop.child_name, &prop.name),
comment_prefix,
indent,
)?;
}
let add_doc_alias = if is_get {
prop.name != prop.getter_name && prop.name != prop.prop_name
} else {
prop.name != prop.prop_name
};
if add_doc_alias {
doc_alias(
w,
&format!("{}.{}", &prop.child_name, &prop.name),
comment_prefix,
indent,
)?;
}
writeln!(
w,
Expand Down
29 changes: 7 additions & 22 deletions src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ use crate::{
pub fn get_must_use_if_needed(
parent_type_id: Option<TypeId>,
analysis: &analysis::functions::Info,
in_trait: bool,
only_declaration: bool,
comment_prefix: &str,
) -> Option<String> {
// If there is no parent, it means it's not a (trait) method so we're not
// interested.
if let Some(parent_type_id) = parent_type_id {
// Check it's a trait declaration or a method declaration (outside of a trait
// implementation).
if analysis.kind == library::FunctionKind::Method && (!in_trait || only_declaration) {
if analysis.kind == library::FunctionKind::Method {
// We now get the list of the returned types.
let outs = out_parameter_types(analysis);
// If there is only one type returned, we check if it's the same type as `self`
Expand Down Expand Up @@ -97,20 +95,16 @@ pub fn generate(
let suffix = if only_declaration { ";" } else { " {" };

writeln!(w)?;
if !in_trait || only_declaration {
cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;
}
cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;
cfg_condition(w, analysis.cfg_condition.as_ref(), commented, indent)?;
let version = Version::if_stricter_than(analysis.version, scope_version);
version_condition(w, env, None, version, commented, indent)?;
not_version_condition(w, analysis.not_version, commented, indent)?;
doc_hidden(w, analysis.doc_hidden, comment_prefix, indent)?;
allow_deprecated(w, analysis.deprecated_version, commented, indent)?;
if !in_trait || only_declaration {
doc_alias(w, &analysis.glib_name, comment_prefix, indent)?;
if analysis.codegen_name() != analysis.func_name {
doc_alias(w, &analysis.func_name, comment_prefix, indent)?;
}
doc_alias(w, &analysis.glib_name, comment_prefix, indent)?;
if analysis.codegen_name() != analysis.func_name {
doc_alias(w, &analysis.func_name, comment_prefix, indent)?;
}
// Don't add a guard for public or copy/equal functions
let dead_code_cfg = if !analysis.visibility.is_public() && !analysis.is_special() {
Expand All @@ -134,14 +128,7 @@ pub fn generate(
"{}{}{}{}{}{}{}{}{}",
allow_should_implement_trait,
dead_code_cfg,
get_must_use_if_needed(
parent_type_id,
analysis,
in_trait,
only_declaration,
comment_prefix
)
.unwrap_or_default(),
get_must_use_if_needed(parent_type_id, analysis, comment_prefix).unwrap_or_default(),
tabs(indent),
comment_prefix,
pub_prefix,
Expand All @@ -162,9 +149,7 @@ pub fn generate(
let suffix = if only_declaration { ";" } else { " {" };

writeln!(w)?;
if !in_trait || only_declaration {
cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;
}
cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;

writeln!(w, "{}{}", tabs(indent), comment_prefix)?;
cfg_condition(w, analysis.cfg_condition.as_ref(), commented, indent)?;
Expand Down
34 changes: 15 additions & 19 deletions src/codegen/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,22 @@ fn generate_prop_func(
writeln!(w)?;

let decl = declaration(env, prop);
if !in_trait || only_declaration {
cfg_deprecated(
w,
env,
Some(prop.typ),
prop.deprecated_version,
commented,
indent,
)?;
}
cfg_deprecated(
w,
env,
Some(prop.typ),
prop.deprecated_version,
commented,
indent,
)?;
version_condition(w, env, None, prop.version, commented, indent)?;
if !in_trait || only_declaration {
let add_doc_alias = if let Some(func_name_alias) = prop.func_name_alias.as_ref() {
&prop.name != func_name_alias && prop.name != prop.var_name
} else {
prop.name != prop.var_name
};
if add_doc_alias {
doc_alias(w, &prop.name, comment_prefix, indent)?;
}
let add_doc_alias = if let Some(func_name_alias) = prop.func_name_alias.as_ref() {
&prop.name != func_name_alias && prop.name != prop.var_name
} else {
prop.name != prop.var_name
};
if add_doc_alias {
doc_alias(w, &prop.name, comment_prefix, indent)?;
}
writeln!(
w,
Expand Down
18 changes: 7 additions & 11 deletions src/codegen/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,17 @@ pub fn generate(
let suffix = if only_declaration { ";" } else { " {" };

writeln!(w)?;
if !in_trait || only_declaration {
cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;
}
cfg_deprecated(w, env, None, analysis.deprecated_version, commented, indent)?;
version_condition(w, env, None, analysis.version, commented, indent)?;
doc_hidden(w, analysis.doc_hidden, comment_prefix, indent)?;
// Strip the "prefix" from "prefix::prop-name", if any.
// Ex.: "notify::is-locked".
if !in_trait || only_declaration {
doc_alias(
w,
analysis.signal_name.splitn(2, "::").last().unwrap(),
comment_prefix,
indent,
)?;
}
doc_alias(
w,
analysis.signal_name.splitn(2, "::").last().unwrap(),
comment_prefix,
indent,
)?;
writeln!(
w,
"{}{}{}{}{}",
Expand Down