-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copypaste enum analysis and function generation to bitfield
- Loading branch information
Showing
3 changed files
with
246 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use super::{function_parameters::TransformationType, imports::Imports, *}; | ||
use crate::{ | ||
config::gobjects::GObject, | ||
env::Env, | ||
library::{self, Transfer}, | ||
nameutil::*, | ||
traits::*, | ||
}; | ||
|
||
use log::info; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Info { | ||
pub full_name: String, | ||
pub type_id: library::TypeId, | ||
pub name: String, | ||
pub functions: Vec<functions::Info>, | ||
pub specials: special_functions::Infos, | ||
} | ||
|
||
impl Info { | ||
//TODO: add test in tests/ for panic | ||
pub fn type_<'a>(&self, library: &'a library::Library) -> &'a library::Bitfield { | ||
let type_ = library | ||
.type_(self.type_id) | ||
.maybe_ref() | ||
.unwrap_or_else(|| panic!("{} is not an flags.", self.full_name)); | ||
type_ | ||
} | ||
} | ||
|
||
pub fn new(env: &Env, obj: &GObject, imports: &mut Imports) -> Option<Info> { | ||
info!("Analyzing flags {}", obj.name); | ||
|
||
if !obj.status.need_generate() { | ||
return None; | ||
} | ||
|
||
if !obj | ||
.type_id | ||
.map_or(false, |tid| tid.ns_id == namespaces::MAIN) | ||
{ | ||
return None; | ||
} | ||
|
||
let flags_tid = env.library.find_type(0, &obj.name)?; | ||
let type_ = env.type_(flags_tid); | ||
let flags: &library::Bitfield = type_.maybe_ref()?; | ||
|
||
let name = split_namespace_name(&obj.name).1; | ||
|
||
// Mark the type as available within the bitfield namespace: | ||
imports.add_defined(name); | ||
|
||
let has_get_type = flags.glib_get_type.is_some(); | ||
if has_get_type { | ||
imports.add("glib::Type"); | ||
imports.add("glib::StaticType"); | ||
imports.add("glib::value::Value"); | ||
imports.add("glib::value::SetValue"); | ||
imports.add("glib::value::FromValue"); | ||
imports.add("glib::value::FromValueOptional"); | ||
imports.add("gobject_sys"); | ||
} | ||
|
||
if obj.generate_display_trait { | ||
imports.add("std::fmt"); | ||
} | ||
|
||
let mut functions = functions::analyze( | ||
env, | ||
&flags.functions, | ||
flags_tid, | ||
false, | ||
false, | ||
obj, | ||
imports, | ||
None, | ||
None, | ||
); | ||
|
||
// Gir does not currently mark the first parameter of associated bitfield functions - | ||
// that are identical to its bitfield type - as instance parameter since most languages | ||
// do not support this. | ||
for f in &mut functions { | ||
if f.parameters.c_parameters.is_empty() { | ||
continue; | ||
} | ||
|
||
let first_param = &mut f.parameters.c_parameters[0]; | ||
|
||
if first_param.typ == flags_tid { | ||
first_param.instance_parameter = true; | ||
|
||
let t = f | ||
.parameters | ||
.transformations | ||
.iter_mut() | ||
.find(|t| t.ind_c == 0) | ||
.unwrap(); | ||
|
||
if let TransformationType::ToGlibScalar { name, .. } = &mut t.transformation_type { | ||
*name = "self".to_owned(); | ||
} else { | ||
panic!( | ||
"Enum function instance param must be passed as scalar, not {:?}", | ||
t.transformation_type | ||
); | ||
} | ||
} | ||
} | ||
|
||
// Flags to_string functions provide static strings and need special error handling. | ||
if let Some(to_string) = functions.iter_mut().find(|f| f.name == "to_string") { | ||
if let Some(ret_param) = &to_string.ret.parameter { | ||
if ret_param.transfer == Transfer::None && to_string.status.need_generate() { | ||
to_string.name = "to_str".to_owned(); | ||
imports.add("std::ffi::CStr"); | ||
} | ||
} | ||
} | ||
|
||
let specials = special_functions::extract(&mut functions); | ||
|
||
special_functions::analyze_imports(&specials, imports); | ||
|
||
let info = Info { | ||
full_name: obj.name.clone(), | ||
type_id: flags_tid, | ||
name: name.to_owned(), | ||
functions, | ||
specials, | ||
}; | ||
|
||
Some(info) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters