Skip to content

Commit

Permalink
FIXUP: Move to_string update_func tasks into is_stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Dec 6, 2020
1 parent ddf8357 commit 1ef3cbe
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions src/analysis/special_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,51 +62,42 @@ pub type Infos = BTreeMap<Type, Info>;

/// Returns true on functions that take an instance as single argument and
/// return a string as result.
fn is_stringify(func: &FuncInfo) -> bool {
fn is_stringify(func: &mut FuncInfo, parent_type: &LibType, obj: &GObject) -> bool {
if func.parameters.c_parameters.len() != 1 {
return false;
}
if !func.parameters.c_parameters[0].instance_parameter {
return false;
}

func.ret
.parameter
.as_ref()
.map_or(false, |p| p.typ == TypeId::tid_utf8())
}

fn update_func(func: &mut FuncInfo, type_: Type, parent_type: &LibType, obj: &GObject) -> bool {
if func.visibility != Visibility::Comment {
func.visibility = visibility(type_);
}
if let Some(ret) = func.ret.parameter.as_mut() {
if ret.typ != TypeId::tid_utf8() {
return false;
}

if matches!(type_, Type::Display | Type::Stringify) {
if func.name == "to_string" {
// Rename to to_str to make sure it doesn't clash with ToString::to_string
func.name = "to_str".to_owned();

// As to not change old code behaviour, assume non-nullability outside
// enums and flags only. Function inside enums and flags have been
// appropriately marked in Gir.
// enums and flags only, and exclusively for to_string. Function inside
// enums and flags have been appropriately marked in Gir.
if !obj.trust_return_value_nullability
&& !matches!(parent_type, LibType::Enumeration(_) | LibType::Bitfield(_))
{
if let Some(par) = func.ret.parameter.as_mut() {
*par.nullable = false;
}
*ret.nullable = false;
}
}

// Cannot generate Display implementation for Option<>
if func
.ret
.parameter
.as_ref()
.map_or(false, |ret| *ret.nullable)
{
return false;
}
!*ret.nullable
} else {
false
}
}

fn update_func(func: &mut FuncInfo, type_: Type) -> bool {
if func.visibility != Visibility::Comment {
func.visibility = visibility(type_);
}
true
}
Expand All @@ -121,9 +112,9 @@ pub fn extract(functions: &mut Vec<FuncInfo>, parent_type: &LibType, obj: &GObje
let type_ = Type::extract(&func.name);

let type_ = match type_ {
Some(Type::Display) if is_stringify(func) => type_,
Some(Type::Display) if is_stringify(func, parent_type, obj) => type_,
Some(Type::Display) => None,
None if is_stringify(func) => Some(Type::Stringify),
None if is_stringify(func, parent_type, obj) => Some(Type::Stringify),
t => t,
};

Expand All @@ -132,7 +123,7 @@ pub fn extract(functions: &mut Vec<FuncInfo>, parent_type: &LibType, obj: &GObje
destroy = Some((func.glib_name.clone(), pos));
continue;
}
if !update_func(func, type_, parent_type, obj) {
if !update_func(func, type_) {
continue;
}
if func.name == "copy" {
Expand Down Expand Up @@ -170,7 +161,7 @@ pub fn extract(functions: &mut Vec<FuncInfo>, parent_type: &LibType, obj: &GObje
if let Some((glib_name, pos)) = destroy {
let ty_ = Type::from_str("destroy").unwrap();
let func = &mut functions[pos];
update_func(func, ty_, parent_type, obj);
update_func(func, ty_);
specials.insert(
ty_,
Info {
Expand Down

0 comments on commit 1ef3cbe

Please sign in to comment.