libgir/analysis/
enums.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::Enumeration {
18        (library
19            .type_(self.type_id)
20            .maybe_ref()
21            .unwrap_or_else(|| panic!("{} is not an enumeration.", self.full_name))) as _
22    }
23}
24
25pub fn new(env: &Env, obj: &GObject, imports: &mut Imports) -> Option<Info> {
26    info!("Analyzing enumeration {}", obj.name);
27
28    if obj.status.ignored() {
29        return None;
30    }
31
32    let enumeration_tid = env.library.find_type(0, &obj.name)?;
33    let type_ = env.type_(enumeration_tid);
34    let enumeration: &library::Enumeration = type_.maybe_ref()?;
35
36    let name = split_namespace_name(&obj.name).1;
37
38    if obj.status.need_generate() {
39        // Mark the type as available within the enum namespace:
40        imports.add_defined(&format!("crate::{name}"));
41        imports.add("crate::ffi");
42
43        let imports = &mut imports.with_defaults(enumeration.version, &None);
44        imports.add("glib::translate::*");
45
46        let has_get_quark = enumeration.error_domain.is_some();
47        if has_get_quark {
48            imports.add("glib::prelude::*");
49        }
50
51        let has_get_type = enumeration.glib_get_type.is_some();
52        if has_get_type {
53            imports.add("glib::prelude::*");
54        }
55    }
56
57    let mut functions = functions::analyze(
58        env,
59        &enumeration.functions,
60        Some(enumeration_tid),
61        false,
62        false,
63        obj,
64        imports,
65        None,
66        None,
67    );
68
69    // Gir does not currently mark the first parameter of associated enum functions
70    // - that are identical to its enum type - as instance parameter since most
71    // languages do not support this.
72    for f in &mut functions {
73        if f.parameters.c_parameters.is_empty() {
74            continue;
75        }
76
77        let first_param = &mut f.parameters.c_parameters[0];
78
79        if first_param.typ == enumeration_tid {
80            first_param.instance_parameter = true;
81
82            let t = f
83                .parameters
84                .transformations
85                .iter_mut()
86                .find(|t| t.ind_c == 0)
87                .unwrap();
88
89            if let TransformationType::ToGlibScalar { name, .. } = &mut t.transformation_type {
90                *name = "self".to_owned();
91            } else {
92                panic!(
93                    "Enumeration function instance param must be passed as scalar, not {:?}",
94                    t.transformation_type
95                );
96            }
97        }
98    }
99
100    let specials = special_functions::extract(&mut functions, type_, obj);
101
102    if obj.status.need_generate() {
103        special_functions::analyze_imports(&specials, imports);
104    }
105
106    let info = Info {
107        full_name: obj.name.clone(),
108        type_id: enumeration_tid,
109        name: name.to_owned(),
110        functions,
111        specials,
112        visibility: obj.visibility,
113    };
114
115    Some(info)
116}