| 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 | use std::rc::Rc; |
| 5 | |
| 6 | use crate::diagnostics::BuildDiagnostics; |
| 7 | use crate::expression_tree::{BuiltinFunction, Callable, Expression}; |
| 8 | use 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 |
| 13 | pub fn check_expressions(doc: &crate::object_tree::Document, diag: &mut BuildDiagnostics) { |
| 14 | for component: &Rc in &doc.inner_components { |
| 15 | visit_all_expressions(component, |e: &mut Expression, _| check_expression(component, e, diag)); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | fn check_expression(component: &Rc<Component>, e: &Expression, diag: &mut BuildDiagnostics) { |
| 20 | if let Expression::FunctionCall { function: Callable::Builtin(b: &BuiltinFunction), source_location: &Option, .. } = e { |
| 21 | match b { |
| 22 | BuiltinFunction::GetWindowScaleFactor => { |
| 23 | if component.is_global() { |
| 24 | diag.push_error(message:"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(message:"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: &Expression| check_expression(component, e, diag)) |
| 36 | } |
| 37 | |