libgir/analysis/
flags.rs

1use log::info;
2
3use super::{function_parameters::TransformationType, imports::Imports, *};
4use crate::{codegen::Visibility, config::gobjects::GObject, env::Env, nameutil::*, traits::*};
5
6#[derive(Debug, Default)]
7pub struct Info {
8    pub full_name: String,
9    pub type_id: library::TypeId,
10    pub name: String,
11    pub functions: Vec<functions::Info>,
12    pub specials: special_functions::Infos,
13    pub visibility: Visibility,
14}
15
16impl Info {
17    pub fn type_<'a>(&self, library: &'a library::Library) -> &'a library::Bitfield {
18        let type_ = library
19            .type_(self.type_id)
20            .maybe_ref()
21            .unwrap_or_else(|| panic!("{} is not an flags.", self.full_name));
22        type_
23    }
24}
25
26pub fn new(env: &Env, obj: &GObject, imports: &mut Imports) -> Option<Info> {
27    info!("Analyzing flags {}", obj.name);
28
29    if obj.status.ignored() {
30        return None;
31    }
32
33    let flags_tid = env.library.find_type(0, &obj.name)?;
34    let type_ = env.type_(flags_tid);
35    let flags: &library::Bitfield = type_.maybe_ref()?;
36
37    let name = split_namespace_name(&obj.name).1;
38
39    if obj.status.need_generate() {
40        // Mark the type as available within the bitfield namespace:
41        imports.add_defined(&format!("crate::{name}"));
42        imports.add("crate::ffi");
43
44        let imports = &mut imports.with_defaults(flags.version, &None);
45        imports.add("glib::translate::*");
46        imports.add("glib::bitflags::bitflags");
47
48        let has_get_type = flags.glib_get_type.is_some();
49        if has_get_type {
50            imports.add("glib::prelude::*");
51        }
52    }
53
54    let mut functions = functions::analyze(
55        env,
56        &flags.functions,
57        Some(flags_tid),
58        false,
59        false,
60        obj,
61        imports,
62        None,
63        None,
64    );
65
66    // Gir does not currently mark the first parameter of associated bitfield
67    // functions - that are identical to its bitfield type - as instance
68    // parameter since most languages do not support this.
69    for f in &mut functions {
70        if f.parameters.c_parameters.is_empty() {
71            continue;
72        }
73
74        let first_param = &mut f.parameters.c_parameters[0];
75
76        if first_param.typ == flags_tid {
77            first_param.instance_parameter = true;
78
79            let t = f
80                .parameters
81                .transformations
82                .iter_mut()
83                .find(|t| t.ind_c == 0)
84                .unwrap();
85
86            if let TransformationType::ToGlibScalar { name, .. } = &mut t.transformation_type {
87                *name = "self".to_owned();
88            } else {
89                panic!(
90                    "Bitfield function instance param must be passed as scalar, not {:?}",
91                    t.transformation_type
92                );
93            }
94        }
95    }
96
97    let specials = special_functions::extract(&mut functions, type_, obj);
98
99    if obj.status.need_generate() {
100        special_functions::analyze_imports(&specials, imports);
101    }
102
103    let info = Info {
104        full_name: obj.name.clone(),
105        type_id: flags_tid,
106        name: name.to_owned(),
107        functions,
108        specials,
109        visibility: obj.visibility,
110    };
111
112    Some(info)
113}