1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
3
4use crate::Cli;
5use i_slint_compiler::langtype::Type;
6
7use i_slint_compiler::parser::{syntax_nodes, NodeOrToken, SyntaxKind, SyntaxNode};
8use std::io::Write;
9
10pub(crate) fn fold_node(
11 node: &SyntaxNode,
12 file: &mut impl Write,
13 state: &mut crate::State,
14 _args: &Cli,
15) -> std::io::Result<bool> {
16 if let Some(s) = syntax_nodes::CallbackDeclaration::new(node.clone()) {
17 if state.current_elem.as_ref().map_or(false, |e| e.borrow().is_legacy_syntax)
18 && s.child_text(SyntaxKind::Identifier).as_deref() != Some("pure")
19 {
20 if s.ReturnType().is_some() {
21 write!(file, "pure ")?;
22 } else if let Some(twb) = s.TwoWayBinding() {
23 let nr = super::lookup_changes::with_lookup_ctx(state, |lookup_ctx| {
24 lookup_ctx.property_type = Type::InferredCallback;
25 let r = i_slint_compiler::passes::resolving::resolve_two_way_binding(
26 twb, lookup_ctx,
27 );
28 r
29 })
30 .flatten();
31
32 if let Some(nr) = nr {
33 let lk = nr.element().borrow().lookup_property(nr.name());
34 if lk.declared_pure == Some(true) {
35 write!(file, "pure ")?;
36 } else if let Type::Callback { return_type, .. } = lk.property_type {
37 if return_type.is_some() {
38 write!(file, "pure ")?;
39 }
40 }
41 }
42 }
43 }
44 } else if let Some(s) = syntax_nodes::Function::new(node.clone()) {
45 if state.current_elem.as_ref().map_or(false, |e| e.borrow().is_legacy_syntax)
46 && s.ReturnType().is_some()
47 {
48 let (mut pure, mut public) = (false, false);
49 for t in s
50 .children_with_tokens()
51 .filter_map(NodeOrToken::into_token)
52 .filter(|t| t.kind() == SyntaxKind::Identifier)
53 {
54 match t.text() {
55 "pure" => pure = true,
56 "public" => public = true,
57 _ => (),
58 }
59 }
60 if !pure && public {
61 write!(file, "pure ")?;
62 }
63 }
64 }
65 Ok(false)
66}
67