i_slint_compiler/passes/
check_expressions.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
4use std::rc::Rc;
5
6use crate::diagnostics::BuildDiagnostics;
7use crate::expression_tree::{BuiltinFunction, Callable, Expression};
8use crate::object_tree::{visit_all_expressions, Component};
9
10/// Check the validity of expressions
11///
12/// - Check that the GetWindowScaleFactor and GetWindowDefaultFontSize are not called in a global
13pub fn check_expressions(doc: &crate::object_tree::Document, diag: &mut BuildDiagnostics) {
14    for component in &doc.inner_components {
15        visit_all_expressions(component, |e, _| check_expression(component, e, diag));
16    }
17}
18
19fn check_expression(component: &Rc<Component>, e: &Expression, diag: &mut BuildDiagnostics) {
20    if let Expression::FunctionCall { function: Callable::Builtin(b), source_location, .. } = e {
21        match b {
22            BuiltinFunction::GetWindowScaleFactor => {
23                if component.is_global() {
24                    diag.push_error("Cannot convert between logical and physical length in a global component, because the scale factor is not known".into(), source_location);
25                }
26            }
27            BuiltinFunction::GetWindowDefaultFontSize => {
28                if component.is_global() {
29                    diag.push_error("Cannot convert between rem and logical length in a global component, because the default font size is not known".into(), source_location);
30                }
31            }
32            _ => {}
33        }
34    }
35    e.visit(|e| check_expression(component, e, diag))
36}