i_slint_compiler/passes/
collect_globals.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4//! Passes that fills the root component used_types.globals
5
6use by_address::ByAddress;
7
8use crate::diagnostics::BuildDiagnostics;
9use crate::expression_tree::NamedReference;
10use crate::object_tree::*;
11use std::collections::HashSet;
12use std::rc::Rc;
13
14/// Fill the root_component's used_types.globals
15pub fn collect_globals(doc: &Document, _diag: &mut BuildDiagnostics) {
16    doc.used_types.borrow_mut().globals.clear();
17    let mut set = HashSet::new();
18    let mut sorted_globals = vec![];
19    for (_, ty) in &*doc.exports {
20        if let Some(c) = ty.as_ref().left() {
21            if c.is_global() && set.insert(ByAddress(c.clone())) {
22                collect_in_component(c, &mut set, &mut sorted_globals);
23                sorted_globals.push(c.clone());
24            }
25        }
26    }
27    doc.visit_all_used_components(|component| {
28        collect_in_component(component, &mut set, &mut sorted_globals)
29    });
30
31    doc.used_types.borrow_mut().globals = sorted_globals;
32}
33
34fn collect_in_component(
35    component: &Rc<Component>,
36    global_set: &mut HashSet<ByAddress<Rc<Component>>>,
37    sorted_globals: &mut Vec<Rc<Component>>,
38) {
39    let mut maybe_collect_global = |nr: &mut NamedReference| {
40        let element = nr.element();
41        let global_component = element.borrow().enclosing_component.upgrade().unwrap();
42        if global_component.is_global() && global_set.insert(ByAddress(global_component.clone())) {
43            collect_in_component(&global_component, global_set, sorted_globals);
44            sorted_globals.push(global_component);
45        }
46    };
47    visit_all_named_references(component, &mut maybe_collect_global);
48}