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

Draft: codgen: Allow renaming objects #1374

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions book/src/config_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Sometimes Gir understands the object definition incorrectly or the `.gir` file c
[[object]]
# object's fullname
name = "Gtk.SomeClass"
# An optional different name of the object
rename = "Gtk.SomeNewName"
# can be also "manual" and "ignore" but it's simpler to just put the object in the same array
status = "generate"
# replace the parameter name for the child in child properties (instead "child")
Expand Down
7 changes: 1 addition & 6 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,12 +933,7 @@ pub fn is_carray_with_direct_elements(env: &Env, typ: library::TypeId) -> bool {
match *env.library.type_(typ) {
Type::CArray(inner_tid) => {
use super::conversion_type::ConversionType;
match env.library.type_(inner_tid) {
Type::Basic(..) if ConversionType::of(env, inner_tid) == ConversionType::Direct => {
true
}
_ => false,
}
matches!(env.library.type_(inner_tid), Type::Basic(..) if ConversionType::of(env, inner_tid) == ConversionType::Direct)
}
_ => false,
}
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ where
.iter()
.enumerate()
.filter_map(|(indice, param)| {
(!indices_to_ignore.contains(&(indice as u32))).then(|| param)
(!indices_to_ignore.contains(&(indice as u32))).then_some(param)
})
.filter(|param| !param.instance_parameter)
.collect();
Expand Down
26 changes: 15 additions & 11 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
mod_rs.push(format!(
"{} use self::enums::{};",
enum_analysis.visibility.export_visibility(),
enum_.name
config.rename.as_ref().unwrap_or(&enum_.name)
));

generate_enum(env, w, enum_, config, enum_analysis)?;
Expand Down Expand Up @@ -132,8 +132,12 @@ fn generate_enum(
writeln!(w, "#[derive(Clone, Copy)]")?;
writeln!(w, "#[non_exhaustive]")?;
doc_alias(w, &enum_.c_type, "", 0)?;
if config.rename.is_some() {
doc_alias(w, &enum_.name, "", 0)?;
}

writeln!(w, "{} enum {} {{", analysis.visibility, enum_.name)?;
let enum_name = config.rename.as_ref().unwrap_or(&enum_.name);
writeln!(w, "{} enum {} {{", analysis.visibility, enum_name)?;
for member in &members {
cfg_deprecated(
w,
Expand Down Expand Up @@ -208,7 +212,7 @@ fn generate_enum(
"impl fmt::Display for {0} {{\n\
\tfn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{\n\
\t\twrite!(f, \"{0}::{{}}\", match *self {{",
enum_.name
enum_name
)?;
for member in &members {
version_condition_no_doc(w, env, None, member.version, false, 3)?;
Expand Down Expand Up @@ -236,7 +240,7 @@ impl IntoGlib for {name} {{
fn into_glib(self) -> {sys_crate_name}::{ffi_name} {{
match self {{",
sys_crate_name = sys_crate_name,
name = enum_.name,
name = enum_name,
ffi_name = enum_.c_type
)?;
for member in &members {
Expand Down Expand Up @@ -274,7 +278,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
unsafe fn from_glib(value: {sys_crate_name}::{ffi_name}) -> Self {{
{assert}match value {{",
sys_crate_name = sys_crate_name,
name = enum_.name,
name = enum_name,
ffi_name = enum_.c_type,
assert = assert
)?;
Expand Down Expand Up @@ -308,7 +312,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
"impl ErrorDomain for {name} {{
fn domain() -> Quark {{
{assert}",
name = enum_.name,
name = enum_name,
assert = assert
)?;

Expand Down Expand Up @@ -390,7 +394,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
}}
}}",
sys_crate_name = sys_crate_name,
name = enum_.name,
name = enum_name,
get_type = get_type
)?;
writeln!(w)?;
Expand All @@ -402,7 +406,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
"impl {valuetype} for {name} {{
type Type = Self;
}}",
name = enum_.name,
name = enum_name,
valuetype = use_glib_type(env, "value::ValueType"),
)?;
writeln!(w)?;
Expand All @@ -418,7 +422,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
{assert}from_glib({glib}(value.to_glib_none().0))
}}
}}",
name = enum_.name,
name = enum_name,
glib = use_glib_type(env, "gobject_ffi::g_value_get_enum"),
gvalue = use_glib_type(env, "Value"),
genericwrongvaluetypechecker = use_glib_type(env, "value::GenericValueTypeChecker"),
Expand All @@ -443,7 +447,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
Self::static_type()
}}
}}",
name = enum_.name,
name = enum_name,
glib = use_glib_type(env, "gobject_ffi::g_value_set_enum"),
gvalue = use_glib_type(env, "Value"),
gtype = use_glib_type(env, "Type"),
Expand All @@ -455,7 +459,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
w,
env,
config,
&enum_.name,
enum_name,
enum_.version,
enum_.members.iter(),
|member| {
Expand Down
25 changes: 15 additions & 10 deletions src/codegen/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
mod_rs.push(format!(
"{} use self::flags::{};",
flags_analysis.visibility.export_visibility(),
flags.name
config.rename.as_ref().unwrap_or(&flags.name),
));
generate_flags(env, w, flags, config, flags_analysis)?;
}
Expand Down Expand Up @@ -92,11 +92,16 @@ fn generate_flags(
derives(w, d, 1)?;
}

let flags_name = config.rename.as_ref().unwrap_or(&flags.name);

doc_alias(w, &flags.c_type, "", 1)?;
if config.rename.is_some() {
doc_alias(w, &flags.name, "", 0)?;
}
writeln!(
w,
" {} struct {}: u32 {{",
analysis.visibility, flags.name
analysis.visibility, flags_name
)?;
for member in &flags.members {
let member_config = config.members.matched(&member.name);
Expand Down Expand Up @@ -184,14 +189,14 @@ fn generate_flags(
\t\t<Self as fmt::Debug>::fmt(self, f)\n\
\t}}\n\
}}\n",
flags.name
flags_name
)?;
}
generate_default_impl(
w,
env,
config,
&flags.name,
flags_name,
flags.version,
flags.members.iter(),
|member| {
Expand Down Expand Up @@ -222,7 +227,7 @@ impl IntoGlib for {name} {{
}}
",
sys_crate_name = sys_crate_name,
name = flags.name,
name = flags_name,
ffi_name = flags.c_type
)?;

Expand All @@ -244,7 +249,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
}}
",
sys_crate_name = sys_crate_name,
name = flags.name,
name = flags_name,
ffi_name = flags.c_type,
assert = assert
)?;
Expand All @@ -266,7 +271,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
}}
}}",
sys_crate_name = sys_crate_name,
name = flags.name,
name = flags_name,
get_type = get_type
)?;
writeln!(w)?;
Expand All @@ -278,7 +283,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
"impl {valuetype} for {name} {{
type Type = Self;
}}",
name = flags.name,
name = flags_name,
valuetype = use_glib_type(env, "value::ValueType"),
)?;
writeln!(w)?;
Expand All @@ -294,7 +299,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
{assert}from_glib({glib}(value.to_glib_none().0))
}}
}}",
name = flags.name,
name = flags_name,
glib = use_glib_type(env, "gobject_ffi::g_value_get_flags"),
gvalue = use_glib_type(env, "Value"),
genericwrongvaluetypechecker = use_glib_type(env, "value::GenericValueTypeChecker"),
Expand All @@ -319,7 +324,7 @@ impl FromGlib<{sys_crate_name}::{ffi_name}> for {name} {{
Self::static_type()
}}
}}",
name = flags.name,
name = flags_name,
glib = use_glib_type(env, "gobject_ffi::g_value_set_flags"),
gvalue = use_glib_type(env, "Value"),
gtype = use_glib_type(env, "Type"),
Expand Down
Loading