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

Generate Into<Option<_>> in argument position where applicable #1620

Open
wants to merge 4 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
581 changes: 359 additions & 222 deletions src/analysis/bounds.rs

Large diffs are not rendered by default.

69 changes: 27 additions & 42 deletions src/analysis/child_properties.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use log::error;

use crate::{
analysis::{
bounds::{Bound, Bounds},
imports::Imports,
ref_mode::RefMode,
rust_type::RustType,
},
analysis::{bounds::Bounds, imports::Imports, ref_mode::RefMode, rust_type::RustType},
config,
consts::TYPE_PARAMETERS_START,
env::Env,
Expand Down Expand Up @@ -118,45 +113,35 @@ fn analyze_property(
}
let nullable = library::Nullable(set_in_ref_mode.is_ref());

let mut bounds = Bounds::default();
let mut bounds_str = String::new();
let dir = ParameterDirection::In;
let set_params = if let Some(bound) = Bounds::type_for(env, typ) {
let r_type = RustType::builder(env, typ)
.ref_mode(RefMode::ByRefFake)
.try_build()
.into_string();

let _bound = Bound {
bound_type: bound,
parameter_name: TYPE_PARAMETERS_START.to_string(),
alias: Some(TYPE_PARAMETERS_START.to_owned()),
type_str: r_type,
callback_modified: false,
let set_params =
if bounds.add_for_property_setter(env, typ, &prop.name, set_in_ref_mode, nullable) {
// TODO: bounds_str push?!?!
bounds_str.push_str("TODO");
format!("{prop_name}: {TYPE_PARAMETERS_START}")
// let mut bounds = Bounds::default();
// bounds.add_parameter("P", &r_type, bound, false);
// let (s_bounds, _) = function::bounds(&bounds, &[], false);
// // Because the bounds won't necessarily be added into the final
// function, we // only keep the "inner" part to make
// the string computation easier. So // `<T: X>` becomes
// `T: X`. bounds_str.push_str(&s_bounds[1..s_bounds.
// len() - 1]); format!("{}: {}", prop_name,
// bounds.iter().last().unwrap().alias)
} else {
format!(
"{}: {}",
prop_name,
RustType::builder(env, typ)
.direction(dir)
.nullable(nullable)
.ref_mode(set_in_ref_mode)
.try_build_param()
.into_string()
)
};
// TODO: bounds_str push?!?!
bounds_str.push_str("TODO");
format!("{prop_name}: {TYPE_PARAMETERS_START}")
// let mut bounds = Bounds::default();
// bounds.add_parameter("P", &r_type, bound, false);
// let (s_bounds, _) = function::bounds(&bounds, &[], false);
// // Because the bounds won't necessarily be added into the final
// function, we // only keep the "inner" part to make
// the string computation easier. So // `<T: X>` becomes
// `T: X`. bounds_str.push_str(&s_bounds[1..s_bounds.
// len() - 1]); format!("{}: {}", prop_name,
// bounds.iter().last().unwrap().alias)
} else {
format!(
"{}: {}",
prop_name,
RustType::builder(env, typ)
.direction(dir)
.nullable(nullable)
.ref_mode(set_in_ref_mode)
.try_build_param()
.into_string()
)
};

Some(ChildProperty {
name,
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/class_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ fn analyze_property(
let (get_out_ref_mode, set_in_ref_mode, nullable) = get_property_ref_modes(env, prop);

let mut bounds = Bounds::default();
if let Some(bound) = Bounds::type_for(env, prop.typ) {
let set_has_bound =
bounds.add_for_property_setter(env, prop.typ, &prop.name, set_in_ref_mode, nullable);
if set_has_bound {
imports.add("glib::prelude::*");
bounds.add_parameter(&prop.name, &rust_type_res.into_string(), bound, false);
}

Some(Property {
Expand All @@ -139,7 +140,6 @@ fn analyze_property(
nullable,
get_out_ref_mode,
set_in_ref_mode,
set_bound: None,
bounds,
version: prop_version,
deprecated_version: prop.deprecated_version,
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/ffi_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
typ.get_name()
);
warn_main!(tid, "{}", msg);
return Err(TypeError::Mismatch(msg));
return Err(TypeError::mismatch(msg));
}
} else {
warn_main!(tid, "Type `{}` missing c_type", typ.get_name());
Expand Down Expand Up @@ -156,7 +156,7 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
env.library.type_(tid).get_name()
);
warn_main!(tid, "{}", msg);
Err(TypeError::Mismatch(msg))
Err(TypeError::mismatch(msg))
}
} else {
fix_name(env, tid, inner)
Expand All @@ -168,7 +168,7 @@ fn ffi_inner(env: &Env, tid: TypeId, inner: &str) -> Result {
inner
);
warn_main!(tid, "{}", msg);
Err(TypeError::Mismatch(msg))
Err(TypeError::mismatch(msg))
}
}
}
Expand Down Expand Up @@ -201,7 +201,7 @@ fn fix_name(env: &Env, type_id: TypeId, name: &str) -> Result {
.type_status_sys(&type_id.full_name(&env.library))
.ignored()
{
Err(TypeError::Ignored(name_with_prefix))
Err(TypeError::ignored(name_with_prefix))
} else {
Ok(name_with_prefix.into())
}
Expand Down
51 changes: 25 additions & 26 deletions src/analysis/function_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub enum TransformationType {
ToGlibScalar {
name: String,
nullable: library::Nullable,
needs_into: bool,
to_glib_extra: String,
},
ToGlibPointer {
name: String,
Expand Down Expand Up @@ -127,8 +127,12 @@ impl TransformationType {
}

pub fn set_to_glib_extra(&mut self, to_glib_extra_: &str) {
if let Self::ToGlibPointer { to_glib_extra, .. } = self {
*to_glib_extra = to_glib_extra_.to_owned();
match self {
Self::ToGlibScalar { to_glib_extra, .. }
| Self::ToGlibPointer { to_glib_extra, .. } => {
*to_glib_extra = to_glib_extra_.to_owned();
}
_ => (),
}
}
}
Expand Down Expand Up @@ -285,15 +289,16 @@ pub fn analyze(
}
if let Some(array_par) = array_par {
let mut array_name = nameutil::mangle_keywords(&array_par.name);
if let Some(bound_type) = Bounds::type_for(env, array_par.typ) {
array_name = (array_name.into_owned()
+ &Bounds::get_to_glib_extra(
&bound_type,
*array_par.nullable,
array_par.instance_parameter,
move_,
))
.into();
if let Some(to_glib_extra) = Bounds::get_to_glib_extra_for(
env,
array_par.typ,
if move_ { RefMode::None } else { RefMode::ByRef },
Nullable(false),
array_par.direction,
array_par.instance_parameter,
array_par.scope,
) {
array_name = (array_name.into_owned() + &to_glib_extra).into();
}

add_rust_parameter = false;
Expand Down Expand Up @@ -356,37 +361,31 @@ pub fn analyze(
TransformationType::ToGlibScalar {
name,
nullable,
needs_into: false,
to_glib_extra: String::new(),
}
}
}
ConversionType::Scalar => TransformationType::ToGlibScalar {
name,
nullable,
needs_into: false,
to_glib_extra: String::new(),
},
ConversionType::Option => {
let needs_into = match try_from_glib {
TryFromGlib::Option => par.direction == library::ParameterDirection::In,
TryFromGlib::OptionMandatory => false,
other => unreachable!("{:?} inconsistent / conversion type", other),
};
let needs_into = par.direction == library::ParameterDirection::In
&& matches!(try_from_glib, TryFromGlib::Option);
TransformationType::ToGlibScalar {
name,
nullable: Nullable(false),
needs_into,
to_glib_extra: if needs_into { ".into()" } else { "" }.to_string(),
}
}
ConversionType::Result { .. } => {
let needs_into = match try_from_glib {
TryFromGlib::Result { .. } => par.direction == library::ParameterDirection::In,
TryFromGlib::ResultInfallible { .. } => false,
other => unreachable!("{:?} inconsistent / conversion type", other),
};
let needs_into = par.direction == library::ParameterDirection::In
&& matches!(try_from_glib, TryFromGlib::Result { .. });
TransformationType::ToGlibScalar {
name,
nullable: Nullable(false),
needs_into,
to_glib_extra: if needs_into { ".into()" } else { "" }.to_string(),
}
}
ConversionType::Pointer => TransformationType::ToGlibPointer {
Expand Down
66 changes: 44 additions & 22 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ fn analyze_callbacks(
None => &func.name,
};
let mut destructors_to_update = Vec::new();
let mut ind_c = 0;
for pos in 0..parameters.c_parameters.len() {
// If it is a user data parameter, we ignore it.
if cross_user_data_check.values().any(|p| *p == pos) || user_data_indexes.contains(&pos)
Expand All @@ -370,30 +371,41 @@ fn analyze_callbacks(
if let Ok(rust_type) = RustType::builder(env, par.typ)
.direction(par.direction)
.try_from_glib(&par.try_from_glib)
.try_build()
.for_callback(true)
.try_build_param()
{
used_types.extend(rust_type.into_used_types());
}
let rust_type = env.library.type_(par.typ);
let callback_info = if !*par.nullable || !rust_type.is_function() {
let (to_glib_extra, callback_info) = bounds.add_for_parameter(
env,
func,
par,
false,
concurrency,
configured_functions,
);
if let Some(to_glib_extra) = to_glib_extra {
if par.c_type != "GDestroyNotify" {
to_glib_extras.insert(pos, to_glib_extra);
}
}
callback_info
let (to_glib_extra, callback_info) = if !*par.nullable || !rust_type.is_function() {
bounds.add_for_parameter(env, func, par, false, concurrency, configured_functions)
} else {
None
(
Bounds::get_to_glib_extra_for(
env,
par.typ,
if par.move_ {
RefMode::None
} else {
par.ref_mode
},
par.nullable,
par.direction,
par.instance_parameter,
par.scope,
),
None,
)
};

if let Some(to_glib_extra) = to_glib_extra {
if par.c_type != "GDestroyNotify" {
to_glib_extras.insert(ind_c, to_glib_extra);
}
}

ind_c += 1;

if rust_type.is_function() {
if par.c_type != "GDestroyNotify" {
let callback_parameters_config = configured_functions.iter().find_map(|f| {
Expand Down Expand Up @@ -535,6 +547,14 @@ fn analyze_callbacks(
false,
in_trait,
);

for transformation in &mut parameters.transformations {
if let Some(to_glib_extra) = to_glib_extras.get(&transformation.ind_c) {
transformation
.transformation_type
.set_to_glib_extra(to_glib_extra);
}
}
} else {
warn_main!(
type_tid,
Expand Down Expand Up @@ -752,7 +772,7 @@ fn analyze_function(
if let Ok(rust_type) = RustType::builder(env, par.typ)
.direction(par.direction)
.try_from_glib(&par.try_from_glib)
.try_build()
.try_build_param()
{
if !rust_type.as_str().ends_with("GString") || par.c_type == "gchar***" {
used_types.extend(rust_type.into_used_types());
Expand Down Expand Up @@ -787,6 +807,7 @@ fn analyze_function(
&& *env.library.type_(par.typ) == Type::Basic(library::Basic::Pointer))
&& RustType::builder(env, par.typ)
.direction(par.direction)
.ref_mode(par.ref_mode)
.scope(par.scope)
.try_from_glib(&par.try_from_glib)
.try_build_param()
Expand Down Expand Up @@ -862,15 +883,15 @@ fn analyze_function(
for out in &trampoline.output_params {
if let Ok(rust_type) = RustType::builder(env, out.lib_par.typ)
.direction(ParameterDirection::Out)
.try_build()
.try_build_param()
{
used_types.extend(rust_type.into_used_types());
}
}
if let Some(ref out) = trampoline.ffi_ret {
if let Ok(rust_type) = RustType::builder(env, out.lib_par.typ)
.direction(ParameterDirection::Return)
.try_build()
.try_build_param()
{
used_types.extend(rust_type.into_used_types());
}
Expand Down Expand Up @@ -1178,14 +1199,15 @@ fn analyze_callback(
.direction(p.direction)
.nullable(p.nullable)
.try_from_glib(&p.try_from_glib)
.try_build()
.for_callback(true)
.try_build_param()
{
imports_to_add.extend(rust_type.into_used_types());
}
}
if let Ok(rust_type) = RustType::builder(env, func.ret.typ)
.direction(ParameterDirection::Return)
.try_build()
.try_build_param()
{
if !rust_type.as_str().ends_with("GString")
&& !rust_type.as_str().ends_with("GAsyncResult")
Expand Down
Loading
Loading