Skip to main content

i_slint_compiler/
load_builtins.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/*!
5    Parse the contents of builtins.slint and fill the builtin type registry
6*/
7
8use smol_str::{SmolStr, ToSmolStr};
9use std::cell::RefCell;
10use std::collections::HashMap;
11use std::rc::Rc;
12
13use crate::expression_tree::Expression;
14use crate::langtype::{
15    BuiltinElement, BuiltinPrivateStruct, BuiltinPropertyDefault, BuiltinPropertyInfo,
16    DefaultSizeBinding, ElementType, Function, NativeClass, Type,
17};
18use crate::object_tree::{self, *};
19use crate::parser::{SyntaxKind, SyntaxNode, identifier_text, syntax_nodes};
20use crate::typeregister::TypeRegister;
21
22/// Parse the contents of builtins.slint and fill the builtin type registry
23/// `register` is the register to fill with the builtin types.
24/// At this point, it really should already contain the basic Types (string, int, ...)
25pub(crate) fn load_builtins(register: &mut TypeRegister) {
26    let mut diag = crate::diagnostics::BuildDiagnostics::default();
27    let node = crate::parser::parse(include_str!("builtins.slint").into(), None, &mut diag);
28    if !diag.is_empty() {
29        let vec = diag.to_string_vec();
30        #[cfg(feature = "display-diagnostics")]
31        diag.print();
32        panic!("Error parsing the builtin elements: {vec:?}");
33    }
34
35    assert_eq!(node.kind(), crate::parser::SyntaxKind::Document);
36    let doc: syntax_nodes::Document = node.into();
37
38    let mut natives = HashMap::<SmolStr, Rc<BuiltinElement>>::new();
39
40    let exports = doc
41        .ExportsList()
42        .flat_map(|e| {
43            e.Component()
44                .map(|x| {
45                    let x = identifier_text(&x.DeclaredIdentifier()).unwrap();
46                    (x.clone(), x)
47                })
48                .into_iter()
49                .chain(e.ExportSpecifier().map(|e| {
50                    (
51                        identifier_text(&e.ExportIdentifier()).unwrap(),
52                        identifier_text(&e.ExportName().unwrap()).unwrap(),
53                    )
54                }))
55        })
56        .collect::<HashMap<_, _>>();
57
58    for c in doc.Component().chain(doc.ExportsList().filter_map(|e| e.Component())) {
59        let id = identifier_text(&c.DeclaredIdentifier()).unwrap();
60        let e = c.Element();
61        let diag = RefCell::new(&mut diag);
62        let mut n = NativeClass::new_with_properties(
63            &id,
64            e.PropertyDeclaration()
65                .filter(|p| p.TwoWayBinding().is_none()) // aliases are handled further down
66                .map(|p| {
67                    let prop_name = identifier_text(&p.DeclaredIdentifier()).unwrap();
68
69                    let mut info = BuiltinPropertyInfo::new(object_tree::type_from_node(
70                        p.Type().unwrap(),
71                        *diag.borrow_mut(),
72                        register,
73                    ));
74
75                    info.property_visibility = PropertyVisibility::Private;
76
77                    for token in p.children_with_tokens() {
78                        if token.kind() != SyntaxKind::Identifier {
79                            continue;
80                        }
81                        match (token.as_token().unwrap().text(), info.property_visibility) {
82                            ("in", PropertyVisibility::Private) => {
83                                info.property_visibility = PropertyVisibility::Input
84                            }
85                            ("out", PropertyVisibility::Private) => {
86                                info.property_visibility = PropertyVisibility::Output
87                            }
88                            ("in-out", PropertyVisibility::Private) => {
89                                info.property_visibility = PropertyVisibility::InOut
90                            }
91                            ("property", _) => (),
92                            _ => unreachable!("invalid property keyword when parsing builtin file for property {id}::{prop_name}"),
93                        }
94                    }
95
96                    if let Some(e) = p.BindingExpression() {
97                        let ty = info.ty.clone();
98                        info.default_value = BuiltinPropertyDefault::Expr(compiled(e, register, ty));
99                    }
100
101                    (prop_name, info)
102                })
103                .chain(e.CallbackDeclaration().map(|s| {
104                    (
105                        identifier_text(&s.DeclaredIdentifier()).unwrap(),
106                        BuiltinPropertyInfo::new(Type::Callback(Rc::new(Function{
107                            args: s
108                                .CallbackDeclarationParameter()
109                                .map(|a| {
110                                    object_tree::type_from_node(a.Type(), *diag.borrow_mut(), register)
111                                })
112                                .collect(),
113                            return_type: s.ReturnType().map(|a| {
114                                object_tree::type_from_node(
115                                    a.Type(),
116                                    *diag.borrow_mut(),
117                                    register,
118                                )
119                            }).unwrap_or(Type::Void),
120                            arg_names: s
121                                .CallbackDeclarationParameter()
122                                .map(|a| a.DeclaredIdentifier().and_then(|x| identifier_text(&x)).unwrap_or_default())
123                                .collect()
124                        }))),
125                    )
126                }))
127        );
128        n.deprecated_aliases = e
129            .PropertyDeclaration()
130            .flat_map(|p| {
131                if let Some(twb) = p.TwoWayBinding() {
132                    let alias_name = identifier_text(&p.DeclaredIdentifier()).unwrap();
133                    let alias_target = identifier_text(&twb.Expression().QualifiedName().expect(
134                        "internal error: built-in aliases can only be declared within the type",
135                    ))
136                    .unwrap();
137                    Some((alias_name, alias_target))
138                } else {
139                    None
140                }
141            })
142            .collect();
143        n.builtin_struct = parse_annotation("builtin_struct", &e)
144            .map(|x| x.unwrap().parse::<BuiltinPrivateStruct>().unwrap());
145        enum Base {
146            None,
147            Global,
148            NativeParent(Rc<BuiltinElement>),
149        }
150        let base = if c.child_text(SyntaxKind::Identifier).is_some_and(|t| t == "global") {
151            Base::Global
152        } else if let Some(base) = e.QualifiedName() {
153            let base = QualifiedTypeName::from_node(base).to_smolstr();
154            let base = natives.get(&base).unwrap().clone();
155            // because they are not taken from if we inherit from it
156            assert!(
157                base.additional_accepted_child_types.is_empty() && !base.additional_accept_self
158            );
159            n.parent = Some(base.native_class.clone());
160            Base::NativeParent(base)
161        } else {
162            Base::None
163        };
164
165        n.properties.extend(e.Function().map(|f| {
166            let name = identifier_text(&f.DeclaredIdentifier()).unwrap();
167            let return_type = f.ReturnType().map_or(Type::Void, |p| {
168                object_tree::type_from_node(p.Type(), *diag.borrow_mut(), register)
169            });
170            let mut args = Vec::new();
171            let mut arg_names = Vec::new();
172            for a in f.ArgumentDeclaration() {
173                args.push(object_tree::type_from_node(a.Type(), *diag.borrow_mut(), register));
174                arg_names.push(identifier_text(&a.DeclaredIdentifier()).unwrap_or_default());
175            }
176            (
177                name,
178                BuiltinPropertyInfo::new(Type::Function(
179                    Function { return_type, args, arg_names }.into(),
180                )),
181            )
182        }));
183
184        let mut builtin = BuiltinElement::new(Rc::new(n));
185        builtin.is_global = matches!(base, Base::Global);
186        let properties = &mut builtin.properties;
187        if let Base::NativeParent(parent) = &base {
188            properties.extend(parent.properties.iter().map(|(k, v)| (k.clone(), v.clone())));
189        }
190        properties
191            .extend(builtin.native_class.properties.iter().map(|(k, v)| (k.clone(), v.clone())));
192
193        builtin.disallow_global_types_as_child_elements =
194            parse_annotation("disallow_global_types_as_child_elements", &e).is_some();
195        builtin.is_non_item_type = parse_annotation("is_non_item_type", &e).is_some();
196        builtin.is_internal = parse_annotation("is_internal", &e).is_some();
197        builtin.accepts_focus = parse_annotation("accepts_focus", &e).is_some();
198        builtin.default_size_binding = parse_annotation("default_size_binding", &e)
199            .map(|size_type| match size_type.as_deref() {
200                Some("expands_to_parent_geometry") => DefaultSizeBinding::ExpandsToParentGeometry,
201                Some("implicit_size") => DefaultSizeBinding::ImplicitSize,
202                other => panic!("invalid default size binding {other:?}"),
203            })
204            .unwrap_or(DefaultSizeBinding::None);
205        builtin.additional_accepted_child_types = e
206            .SubElement()
207            .filter_map(|s| {
208                let a = identifier_text(&s.Element().QualifiedName().unwrap()).unwrap();
209                if a == builtin.native_class.class_name {
210                    builtin.additional_accept_self = true;
211                    None
212                } else {
213                    let t = natives[&a].clone();
214                    Some((a, t))
215                }
216            })
217            .collect();
218        if let Some(builtin_name) = exports.get(&id) {
219            if !matches!(&base, Base::Global) {
220                builtin.name.clone_from(builtin_name);
221                register.add_builtin(Rc::new(builtin));
222            } else {
223                let glob = Rc::new(Component {
224                    id: builtin_name.clone(),
225                    root_element: Rc::new(RefCell::new(Element {
226                        base_type: ElementType::Builtin(Rc::new(builtin)),
227                        ..Default::default()
228                    })),
229                    ..Default::default()
230                });
231                glob.root_element.borrow_mut().enclosing_component = Rc::downgrade(&glob);
232                register.add(glob);
233            }
234        } else {
235            natives.insert(id, Rc::new(builtin));
236        }
237    }
238
239    register.property_animation_type =
240        ElementType::Builtin(natives.remove("PropertyAnimation").unwrap());
241
242    register.empty_type = ElementType::Builtin(natives.remove("Empty").unwrap());
243
244    if !diag.is_empty() {
245        let vec = diag.to_string_vec();
246        #[cfg(feature = "display-diagnostics")]
247        diag.print();
248        panic!("Error loading the builtin elements: {vec:?}");
249    }
250}
251
252/// Compile an expression, knowing that the expression is basic (does not have lookup to other things)
253fn compiled(
254    node: syntax_nodes::BindingExpression,
255    type_register: &TypeRegister,
256    ty: Type,
257) -> Expression {
258    let mut diag = crate::diagnostics::BuildDiagnostics::default();
259    let mut ctx = crate::lookup::LookupCtx::empty_context(type_register, &mut diag);
260    ctx.property_type = ty.clone();
261    let e = Expression::from_binding_expression_node(node.clone().into(), &mut ctx)
262        .maybe_convert_to(ty, &node, &mut diag);
263    if diag.has_errors() {
264        let vec = diag.to_string_vec();
265        #[cfg(feature = "display-diagnostics")]
266        diag.print();
267        panic!("Error parsing the builtin elements: {vec:?}");
268    }
269    e
270}
271
272/// Find out if there are comments that starts with `//-key` and returns `None`
273/// if no annotation with this key is found, or `Some(None)` if it is found without a value
274/// or `Some(Some(value))` if there is a `//-key:value`  match
275fn parse_annotation(key: &str, node: &SyntaxNode) -> Option<Option<SmolStr>> {
276    for x in node.children_with_tokens() {
277        if x.kind() == SyntaxKind::Comment
278            && let Some(comment) = x
279                .as_token()
280                .unwrap()
281                .text()
282                .strip_prefix("//-")
283                .and_then(|x| x.trim_end().strip_prefix(key))
284        {
285            if comment.is_empty() {
286                return Some(None);
287            }
288            if let Some(comment) = comment.strip_prefix(':') {
289                return Some(Some(comment.into()));
290            }
291        }
292    }
293    None
294}