sixtyfps-compilerlib 0.1.2

Internal SixtyFPS compiler library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
/* LICENSE BEGIN
    This file is part of the SixtyFPS Project -- https://sixtyfps.io
    Copyright (c) 2021 Olivier Goffart <olivier.goffart@sixtyfps.io>
    Copyright (c) 2021 Simon Hausmann <simon.hausmann@sixtyfps.io>

    SPDX-License-Identifier: GPL-3.0-only
    This file is also available under commercial licensing terms.
    Please contact info@sixtyfps.io for more information.
LICENSE END */
/*! module for the Rust code generator

Some convention used in the generated code:
 - `_self` is of type `Pin<&ComponentType>`  where ComponentType is the type of the generated component,
    this is existing for any evaluation of a binding
 - `self_rc` is of type `VRc<ComponentVTable, ComponentType>` or Rc<ComponentType> for globals
    this is usually a local variable to the init code that shouldn't rbe relied upon by the binding code.
*/

use crate::diagnostics::{BuildDiagnostics, Spanned};
use crate::expression_tree::{
    BindingExpression, BuiltinFunction, EasingCurve, Expression, NamedReference, OperatorClass,
    Path,
};
use crate::langtype::Type;
use crate::layout::{Layout, LayoutGeometry, LayoutRect, Orientation};
use crate::object_tree::{Component, Document, ElementRc};
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use std::{collections::BTreeMap, rc::Rc};

fn ident(ident: &str) -> proc_macro2::Ident {
    if ident.contains('-') {
        format_ident!("r#{}", ident.replace('-', "_"))
    } else {
        format_ident!("r#{}", ident)
    }
}

impl quote::ToTokens for Orientation {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let tks = match self {
            Orientation::Horizontal => quote!(sixtyfps::re_exports::Orientation::Horizontal),
            Orientation::Vertical => quote!(sixtyfps::re_exports::Orientation::Vertical),
        };
        tokens.extend(tks);
    }
}

fn rust_type(ty: &Type) -> Option<proc_macro2::TokenStream> {
    match ty {
        Type::Int32 => Some(quote!(i32)),
        Type::Float32 => Some(quote!(f32)),
        Type::String => Some(quote!(sixtyfps::re_exports::SharedString)),
        Type::Color => Some(quote!(sixtyfps::re_exports::Color)),
        Type::Duration => Some(quote!(i64)),
        Type::Angle => Some(quote!(f32)),
        Type::PhysicalLength => Some(quote!(f32)),
        Type::LogicalLength => Some(quote!(f32)),
        Type::Percent => Some(quote!(f32)),
        Type::Bool => Some(quote!(bool)),
        Type::Image => Some(quote!(sixtyfps::re_exports::Image)),
        Type::Struct { fields, name: None, .. } => {
            let elem = fields.values().map(|v| rust_type(v)).collect::<Option<Vec<_>>>()?;
            // This will produce a tuple
            Some(quote!((#(#elem,)*)))
        }
        Type::Struct { name: Some(name), .. } => Some(struct_name_to_tokens(name)),
        Type::Array(o) => {
            let inner = rust_type(o)?;
            Some(quote!(sixtyfps::re_exports::ModelHandle<#inner>))
        }
        Type::Enumeration(e) => {
            let e = ident(&e.name);
            Some(quote!(sixtyfps::re_exports::#e))
        }
        Type::Brush => Some(quote!(sixtyfps::Brush)),
        Type::LayoutCache => Some(quote!(SharedVector<f32>)),
        _ => None,
    }
}

fn get_rust_type(
    ty: &Type,
    type_node: &dyn crate::diagnostics::Spanned,
    diag: &mut BuildDiagnostics,
) -> proc_macro2::TokenStream {
    rust_type(ty).unwrap_or_else(|| {
        diag.push_error(format!("Cannot map property type {} to Rust", ty), type_node);
        quote!(_)
    })
}

/// Generate the rust code for the given component.
///
/// Fill the diagnostic in case of error.
pub fn generate(doc: &Document, diag: &mut BuildDiagnostics) -> Option<TokenStream> {
    if matches!(doc.root_component.root_element.borrow().base_type, Type::Invalid | Type::Void) {
        // empty document, nothing to generate
        return None;
    }

    let (structs_ids, structs): (Vec<_>, Vec<_>) = doc
        .root_component
        .used_types
        .borrow()
        .structs
        .iter()
        .filter_map(|ty| {
            if let Type::Struct { fields, name: Some(name), node: Some(_) } = ty {
                Some((ident(name), generate_struct(name, fields, diag)))
            } else {
                None
            }
        })
        .unzip();
    let compo = generate_component(&doc.root_component, diag)?;
    let compo_id = public_component_id(&doc.root_component);
    let compo_module = format_ident!("sixtyfps_generated_{}", compo_id);
    let version_check = format_ident!(
        "VersionCheck_{}_{}_{}",
        env!("CARGO_PKG_VERSION_MAJOR"),
        env!("CARGO_PKG_VERSION_MINOR"),
        env!("CARGO_PKG_VERSION_PATCH"),
    );
    let used_types = doc.root_component.used_types.borrow();
    let globals = used_types
        .globals
        .iter()
        .filter_map(|glob| glob.requires_code_generation().then(|| generate_component(glob, diag)))
        .collect::<Vec<_>>();
    let globals_ids = used_types
        .globals
        .iter()
        .filter_map(|glob| {
            (glob.visible_in_public_api() && glob.requires_code_generation()).then(|| {
                glob.exported_global_names
                    .borrow()
                    .iter()
                    .map(|name| ident(&name))
                    .collect::<Vec<_>>() // Would prefer not to collect here, but borrow() requires
            })
        })
        .flatten()
        .collect::<Vec<_>>();

    Some(quote! {
        #[allow(non_snake_case)]
        #[allow(clippy::style)]
        #[allow(clippy::complexity)]
        mod #compo_module {
            use sixtyfps::re_exports::*;
            #(#structs)*
            #(#globals)*
            #compo
            const _THE_SAME_VERSION_MUST_BE_USED_FOR_THE_COMPILER_AND_THE_RUNTIME : sixtyfps::#version_check = sixtyfps::#version_check;
        }
        pub use #compo_module::{#compo_id #(,#structs_ids)* #(,#globals_ids)* };
        pub use sixtyfps::{ComponentHandle, Global};
    })
}

fn generate_struct(
    name: &str,
    fields: &BTreeMap<String, Type>,
    diag: &mut BuildDiagnostics,
) -> TokenStream {
    let component_id = struct_name_to_tokens(name);
    let (declared_property_vars, declared_property_types): (Vec<_>, Vec<_>) = fields
        .iter()
        .map(|(name, ty)| {
            (ident(name), get_rust_type(ty, &crate::diagnostics::SourceLocation::default(), diag))
        })
        .unzip();

    quote! {
        #[derive(Default, PartialEq, Debug, Clone)]
        pub struct #component_id {
            #(pub #declared_property_vars : #declared_property_types),*
        }
    }
}

fn handle_property_binding(
    component: &Rc<Component>,
    item_rc: &ElementRc,
    prop_name: &str,
    binding_expression: &BindingExpression,
    init: &mut Vec<TokenStream>,
) {
    let rust_property = access_member(item_rc, prop_name, component, quote!(_self), false);
    let prop_type = item_rc.borrow().lookup_property(prop_name).property_type;

    let init_self_pin_ref = if item_rc.borrow().enclosing_component.upgrade().unwrap().is_global() {
        quote!(
            let _self = self_rc.as_ref();
        )
    } else {
        quote!(
            let _self = self_rc.as_pin_ref();
        )
    };

    if matches!(prop_type, Type::Callback { .. }) {
        if matches!(binding_expression.expression, Expression::Invalid) {
            return;
        }
        let tokens_for_expression = compile_expression(binding_expression, component);
        init.push(quote!({
            sixtyfps::internal::set_callback_handler(#rust_property, &self_rc, {
                move |self_rc, args| {
                    #init_self_pin_ref
                    (#tokens_for_expression) as _
                }
            });
        }));
    } else {
        for nr in &binding_expression.two_way_bindings {
            let p2 = access_member(&nr.element(), nr.name(), component, quote!(_self), false);
            init.push(quote!(
                Property::link_two_way(#rust_property, #p2);
            ));
        }
        if matches!(binding_expression.expression, Expression::Invalid) {
            return;
        }

        let tokens_for_expression = compile_expression(binding_expression, component);
        let is_constant =
            binding_expression.analysis.borrow().as_ref().map_or(false, |a| a.is_const);
        init.push(if is_constant {
            let t = rust_type(&prop_type).unwrap_or(quote!(_));

            // When there is a `return` statement, we must use a lambda expression in the generated code so that the
            // generated code can have an actual return in it. We only want to do that if necessary because otherwise
            // this would slow down the rust compilation
            let mut uses_return = false;
            binding_expression.visit_recursive(&mut |e| {
                if matches!(e, Expression::ReturnStatement(..)) {
                    uses_return = true;
                }
            });

            if uses_return {
                quote! { #rust_property.set((||-> #t { (#tokens_for_expression) as #t })()); }
            } else {
                quote! { #rust_property.set({ (#tokens_for_expression) as #t }); }
            }
        } else {
            let binding_tokens = quote!({
                move |self_rc| {
                    #init_self_pin_ref
                    (#tokens_for_expression) as _
                }
            });

            let is_state_info = matches!(prop_type, Type::Struct { name: Some(name), .. } if name.ends_with("::StateInfo"));
            if is_state_info {
                quote! { {
                    sixtyfps::internal::set_property_state_binding(#rust_property, &self_rc, #binding_tokens);
                } }
            } else {
                match &binding_expression.animation {
                    Some(crate::object_tree::PropertyAnimation::Static(anim)) => {
                        let anim = property_animation_tokens(component, anim);
                        quote! { {
                            #init_self_pin_ref
                            sixtyfps::internal::set_animated_property_binding(#rust_property, &self_rc, #binding_tokens, #anim);
                        } }
                    }
                    Some(crate::object_tree::PropertyAnimation::Transition {
                        state_ref,
                        animations,
                    }) => {
                        let state_tokens = compile_expression(state_ref, component);
                        let anim_expr = animations.iter().map(|a| {
                            let cond = compile_expression(
                                &a.condition(Expression::ReadLocalVariable {
                                    name: "state".into(),
                                    ty: state_ref.ty(),
                                }),
                                component,
                            );
                            let a_tokens = property_animation_tokens(component, &a.animation);
                            quote!(if #cond { #a_tokens })
                        });
                        quote! {
                            sixtyfps::internal::set_animated_property_binding_for_transition(#rust_property, &self_rc, #binding_tokens, move |self_rc| {
                                #init_self_pin_ref
                                let state = #state_tokens;
                                ({ #(#anim_expr else)* { sixtyfps::re_exports::PropertyAnimation::default() }  }, state.change_time)
                            });
                        }
                    }
                    None => {
                        quote! { {
                            sixtyfps::internal::set_property_binding(#rust_property, &self_rc, #binding_tokens);
                        } }
                    }
                }
            }
        });
    }
}

/// Generate the rust code for the given component.
///
/// Fill the diagnostic in case of error.
fn generate_component(
    component: &Rc<Component>,
    diag: &mut BuildDiagnostics,
) -> Option<TokenStream> {
    let inner_component_id = inner_component_id(component);

    let mut extra_components = component
        .popup_windows
        .borrow()
        .iter()
        .filter_map(|c| generate_component(&c.component, diag))
        .collect::<Vec<_>>();

    let self_init = if !component.is_global() {
        quote!(let _self = vtable::VRc::as_pin_ref(&self.0);)
    } else {
        quote!(let _self = self.0.as_ref();)
    };

    let mut declared_property_vars = vec![];
    let mut declared_property_types = vec![];
    let mut declared_callbacks = vec![];
    let mut declared_callbacks_types = vec![];
    let mut declared_callbacks_ret = vec![];
    let mut property_and_callback_accessors: Vec<TokenStream> = vec![];
    for (prop_name, property_decl) in component.root_element.borrow().property_declarations.iter() {
        let prop_ident = ident(prop_name);
        let prop = if let Some(alias) = &property_decl.is_alias {
            access_named_reference(alias, component, quote!(_self))
        } else {
            let field = access_component_field_offset(&inner_component_id, &prop_ident);
            quote!(#field.apply_pin(_self))
        };

        if let Type::Callback { args, return_type } = &property_decl.property_type {
            let callback_args = args
                .iter()
                .map(|a| get_rust_type(a, &property_decl.type_node(), diag))
                .collect::<Vec<_>>();
            let return_type = return_type
                .as_ref()
                .map_or(quote!(()), |a| get_rust_type(a, &property_decl.type_node(), diag));

            if property_decl.expose_in_public_api {
                let args_name = (0..callback_args.len())
                    .map(|i| format_ident!("arg_{}", i))
                    .collect::<Vec<_>>();
                let caller_ident = format_ident!("invoke_{}", prop_ident);
                property_and_callback_accessors.push(quote!(
                    #[allow(dead_code)]
                    pub fn #caller_ident(&self, #(#args_name : #callback_args,)*) -> #return_type {
                        #self_init
                        #prop.call(&(#(#args_name,)*))
                    }
                ));

                let on_ident = format_ident!("on_{}", prop_ident);
                let args_index =
                    (0..callback_args.len()).map(proc_macro2::Literal::usize_unsuffixed);
                property_and_callback_accessors.push(
                    quote!(
                        #[allow(dead_code)]
                        pub fn #on_ident(&self, f: impl Fn(#(#callback_args),*) -> #return_type + 'static) {
                            #self_init
                            #[allow(unused)]
                            #prop.set_handler(
                                // FIXME: why do i need to clone here?
                                move |args| f(#(args.#args_index.clone()),*)
                            )
                        }
                    )
                    ,
                );
            }
            if property_decl.is_alias.is_none() {
                declared_callbacks.push(prop_ident.clone());
                declared_callbacks_types.push(callback_args);
                declared_callbacks_ret.push(return_type);
            }
        } else {
            let rust_property_type =
                get_rust_type(&property_decl.property_type, &property_decl.type_node(), diag);
            if property_decl.expose_in_public_api {
                let getter_ident = format_ident!("get_{}", prop_ident);
                let setter_ident = format_ident!("set_{}", prop_ident);

                property_and_callback_accessors.push(quote!(
                    #[allow(dead_code)]
                    pub fn #getter_ident(&self) -> #rust_property_type {
                        #[allow(unused_imports)]
                        use sixtyfps::re_exports::*;
                        #self_init
                        #prop.get()
                    }
                ));

                let set_value = if let Some(alias) = &property_decl.is_alias {
                    property_set_value_tokens(
                        component,
                        &alias.element(),
                        alias.name(),
                        quote!(value),
                    )
                } else {
                    property_set_value_tokens(
                        component,
                        &component.root_element,
                        prop_name,
                        quote!(value),
                    )
                };
                property_and_callback_accessors.push(quote!(
                    #[allow(dead_code)]
                    pub fn #setter_ident(&self, value: #rust_property_type) {
                        #[allow(unused_imports)]
                        use sixtyfps::re_exports::*;
                        #self_init
                        #prop.#set_value
                    }
                ));
            }

            if property_decl.is_alias.is_none() {
                declared_property_vars.push(prop_ident.clone());
                declared_property_types.push(rust_property_type.clone());
            }
        }
    }

    if diag.has_error() {
        return None;
    }

    let mut item_tree_array = Vec::new();
    let mut item_names = Vec::new();
    let mut item_types = Vec::new();
    let mut repeated_element_names = Vec::new();
    let mut repeated_element_layouts = Vec::new();
    let mut repeated_element_components = Vec::new();
    let mut repeated_visit_branch = Vec::new();
    let mut repeated_input_branch = Vec::new();
    let mut init = Vec::new();
    let mut window_field_init = None;
    let mut window_parent_param = None;
    super::build_array_helper(component, |item_rc, children_index, parent_index| {
        let parent_index = parent_index as u32;
        let item = item_rc.borrow();
        if item.base_type == Type::Void {
            assert!(component.is_global());
        } else if let Some(repeated) = &item.repeated {
            let base_component = item.base_type.as_component();
            let repeater_index = repeated_element_names.len();
            let repeater_id = format_ident!("repeater_{}", ident(&item.id));
            let rep_inner_component_id = self::inner_component_id(&*base_component);

            extra_components.push(generate_component(&*base_component, diag).unwrap_or_else(
                || {
                    assert!(diag.has_error());
                    Default::default()
                },
            ));

            let extra_fn = if repeated.is_listview.is_some() {
                let am = |prop| {
                    access_member(
                        &base_component.root_element,
                        prop,
                        base_component,
                        quote!(self),
                        false,
                    )
                };
                let p_y = am("y");
                let p_height = am("height");
                let p_width = am("width");
                quote! {
                    fn listview_layout(
                        self: core::pin::Pin<&Self>,
                        offset_y: &mut f32,
                        viewport_width: core::pin::Pin<&sixtyfps::re_exports::Property<f32>>,
                    ) {
                        use sixtyfps::re_exports::*;
                        let vp_w = viewport_width.get();
                        #p_y.set(*offset_y);
                        *offset_y += #p_height.get();
                        let w = #p_width.get();
                        if vp_w < w {
                            viewport_width.set(w);
                        }
                    }
                }
            } else {
                // TODO: we could generate this code only if we know that this component is in a box layout
                quote! {
                    fn box_layout_data(self: ::core::pin::Pin<&Self>, o: sixtyfps::re_exports::Orientation)
                        -> sixtyfps::re_exports::BoxLayoutCellData
                    {
                        use sixtyfps::re_exports::*;
                        BoxLayoutCellData { constraint: self.as_ref().layout_info(o) }
                    }
                }
            };

            extra_components.push(if repeated.is_conditional_element {
                quote! {
                    impl sixtyfps::re_exports::RepeatedComponent for #rep_inner_component_id {
                        type Data = ();
                        fn update(&self, _: usize, _: Self::Data) { }
                        #extra_fn
                    }
                }
            } else {
                let data_type = get_rust_type(
                    &Expression::RepeaterModelReference { element: Rc::downgrade(item_rc) }.ty(),
                    &item.node.as_ref().map(|x| x.to_source_location()),
                    diag,
                );

                quote! {
                    impl sixtyfps::re_exports::RepeatedComponent for #rep_inner_component_id {
                        type Data = #data_type;
                        fn update(&self, index: usize, data: Self::Data) {
                            self.index.set(index);
                            self.model_data.set(data);
                        }
                        #extra_fn
                    }
                }
            });

            let mut model = compile_expression(&repeated.model, component);
            if repeated.is_conditional_element {
                model =
                    quote!(sixtyfps::re_exports::ModelHandle::new(std::rc::Rc::<bool>::new(#model)))
            }

            // FIXME: there could be an optimization if `repeated.model.is_constant()`, we don't need a binding
            init.push(quote! {
                _self.#repeater_id.set_model_binding({
                    let self_weak = sixtyfps::re_exports::VRc::downgrade(&self_rc);
                    move || {
                        let self_rc = self_weak.upgrade().unwrap();
                        let _self = self_rc.as_pin_ref();
                        (#model) as _
                    }
                });
            });

            if let Some(listview) = &repeated.is_listview {
                let vp_y = access_named_reference(&listview.viewport_y, component, quote!(_self));
                let vp_h =
                    access_named_reference(&listview.viewport_height, component, quote!(_self));
                let lv_h =
                    access_named_reference(&listview.listview_height, component, quote!(_self));
                let vp_w =
                    access_named_reference(&listview.viewport_width, component, quote!(_self));
                let lv_w =
                    access_named_reference(&listview.listview_width, component, quote!(_self));

                let ensure_updated = quote! {
                    #inner_component_id::FIELD_OFFSETS.#repeater_id.apply_pin(_self).ensure_updated_listview(
                        || { #rep_inner_component_id::new(_self.self_weak.get().unwrap().clone(), &_self.window.window_handle()).into() },
                        #vp_w, #vp_h, #vp_y, #lv_w.get(), #lv_h
                    );
                };

                repeated_visit_branch.push(quote!(
                    #repeater_index => {
                        #ensure_updated
                        _self.#repeater_id.visit(order, visitor)
                    }
                ));

                repeated_element_layouts.push(quote!(
                    #ensure_updated
                ));
            } else {
                let ensure_updated = quote! {
                    #inner_component_id::FIELD_OFFSETS.#repeater_id.apply_pin(_self).ensure_updated(
                        || { #rep_inner_component_id::new(_self.self_weak.get().unwrap().clone(), &_self.window.window_handle()).into() }
                    );
                };

                repeated_visit_branch.push(quote!(
                    #repeater_index => {
                        #ensure_updated
                        _self.#repeater_id.visit(order, visitor)
                    }
                ));

                repeated_element_layouts.push(quote!(
                    #ensure_updated
                    _self.#repeater_id.compute_layout();
                ));
            }

            repeated_input_branch.push(quote!(
                #repeater_index => self.#repeater_id.input_event(rep_index, event, window),
            ));

            item_tree_array.push(quote!(
                sixtyfps::re_exports::ItemTreeNode::DynamicTree {
                    index: #repeater_index,
                    parent_index: #parent_index,
                }
            ));

            repeated_element_names.push(repeater_id);
            repeated_element_components.push(rep_inner_component_id);
        } else if item.is_flickable_viewport {
            let field_name =
                ident(&crate::object_tree::find_parent_element(item_rc).unwrap().borrow().id);
            let children_count = item.children.len() as u32;
            let field = access_component_field_offset(&inner_component_id, &field_name);

            item_tree_array.push(quote!(
                sixtyfps::re_exports::ItemTreeNode::Item{
                    item: VOffset::new(#field + sixtyfps::re_exports::Flickable::FIELD_OFFSETS.viewport),
                    children_count: #children_count,
                    children_index: #children_index,
                    parent_index: #parent_index
                }
            ));
        } else {
            let field_name = ident(&item.id);
            let children_count = item.children.len() as u32;
            let field = access_component_field_offset(&inner_component_id, &field_name);

            item_tree_array.push(quote!(
                sixtyfps::re_exports::ItemTreeNode::Item{
                    item: VOffset::new(#field),
                    children_count: #children_count,
                    children_index: #children_index,
                    parent_index: #parent_index,
                }
            ));
            item_names.push(field_name);
            item_types.push(ident(&item.base_type.as_native().class_name));
        }
    });

    super::handle_property_bindings_init(component, |elem, prop, binding| {
        handle_property_binding(component, elem, prop, binding, &mut init)
    });

    let resource_symbols: Vec<proc_macro2::TokenStream> = component
        .embedded_file_resources
        .borrow()
        .iter()
        .map(|(path, id)| {
            let symbol = format_ident!("SFPS_EMBEDDED_RESOURCE_{}", id);
            quote!(const #symbol: &'static [u8] = ::core::include_bytes!(#path);)
        })
        .collect();

    let layouts = compute_layout(component);
    let mut visibility = if component.visible_in_public_api() { Some(quote!(pub)) } else { None };
    let mut parent_component_type = None;
    let mut has_window_impl = None;
    let mut window_field = Some(quote!(window: sixtyfps::Window));
    if let Some(parent_element) = component.parent_element.upgrade() {
        visibility = None;
        if parent_element.borrow().repeated.as_ref().map_or(false, |r| !r.is_conditional_element) {
            declared_property_vars.push(format_ident!("index"));
            declared_property_types.push(quote!(usize));
            declared_property_vars.push(format_ident!("model_data"));
            declared_property_types.push(get_rust_type(
                &Expression::RepeaterModelReference { element: component.parent_element.clone() }
                    .ty(),
                &parent_element.borrow().node.as_ref().map(|x| x.to_source_location()),
                diag,
            ));
        }

        parent_component_type = Some(self::inner_component_id(
            &parent_element.borrow().enclosing_component.upgrade().unwrap(),
        ));
        window_field_init = Some(quote!(window: parent_window.clone().into()));
        window_parent_param = Some(quote!(, parent_window: &sixtyfps::re_exports::WindowRc))
    } else if !component.is_global() {
        // FIXME: This field is public for testing.
        window_field = Some(quote!(pub window: sixtyfps::Window));
        window_field_init = Some(quote!(window: sixtyfps::create_window().into()));

        init.push(quote!(_self.window.window_handle().set_component(&VRc::into_dyn(_self.as_ref().self_weak.get().unwrap().upgrade().unwrap()));));

        has_window_impl = Some(quote!(
            impl sixtyfps::re_exports::WindowHandleAccess for #inner_component_id {
                fn window_handle(&self) -> &std::rc::Rc<sixtyfps::re_exports::Window> {
                    self.window.window_handle()
                }
            }
        ))
    } else {
        window_field = None;
    };

    // Trick so we can use `#()` as a `if let Some` in `quote!`
    let parent_component_type = parent_component_type.iter().collect::<Vec<_>>();

    if diag.has_error() {
        return None;
    }

    let (drop_impl, pin) = if component.is_global() {
        (None, quote!(#[pin]))
    } else {
        let item_fields = item_names
            .iter()
            .map(|field_name| access_component_field_offset(&format_ident!("Self"), field_name));
        (
            Some(quote!(impl sixtyfps::re_exports::PinnedDrop for #inner_component_id {
                fn drop(self: core::pin::Pin<&mut #inner_component_id>) {
                    use sixtyfps::re_exports::*;
                    let items = [
                        #(VRef::new_pin(#item_fields.apply_pin(self.as_ref())),)*
                    ];
                    self.window.window_handle().free_graphics_resources(&Slice::from_slice(&items));
                }
            })),
            quote!(#[pin_drop]),
        )
    };

    for extra_init_code in component.setup_code.borrow().iter() {
        init.push(compile_expression(extra_init_code, component));
    }

    let (item_tree_impl, component_impl) = if component.is_global() {
        (None, None)
    } else {
        let item_tree_array_len = item_tree_array.len();
        let parent_item_index =
            component.parent_element.upgrade().and_then(|e| e.borrow().item_index.get().copied());
        let parent_item_index = parent_item_index.iter();
        init.insert(0, quote!(sixtyfps::re_exports::init_component_items(_self, Self::item_tree(), &_self.window.window_handle());));
        (
            Some(quote! {
                fn item_tree() -> &'static [sixtyfps::re_exports::ItemTreeNode<Self>] {
                    use sixtyfps::re_exports::*;
                    ComponentVTable_static!(static VT for #inner_component_id);
                    // FIXME: ideally this should be a const
                    static ITEM_TREE : Lazy<[sixtyfps::re_exports::ItemTreeNode<#inner_component_id>; #item_tree_array_len]>  =
                        Lazy::new(|| [#(#item_tree_array),*]);
                    &*ITEM_TREE
                }
            }),
            Some(quote! {
                impl sixtyfps::re_exports::Component for #inner_component_id {
                    fn visit_children_item(self: ::core::pin::Pin<&Self>, index: isize, order: sixtyfps::re_exports::TraversalOrder, visitor: sixtyfps::re_exports::ItemVisitorRefMut)
                        -> sixtyfps::re_exports::VisitChildrenResult
                    {
                        use sixtyfps::re_exports::*;
                        return sixtyfps::re_exports::visit_item_tree(self, &VRc::into_dyn(self.as_ref().self_weak.get().unwrap().upgrade().unwrap()), Self::item_tree(), index, order, visitor, visit_dynamic);
                        #[allow(unused)]
                        fn visit_dynamic(_self: ::core::pin::Pin<&#inner_component_id>, order: sixtyfps::re_exports::TraversalOrder, visitor: ItemVisitorRefMut, dyn_index: usize) -> VisitChildrenResult  {
                            match dyn_index {
                                #(#repeated_visit_branch)*
                                _ => panic!("invalid dyn_index {}", dyn_index),
                            }
                        }
                    }


                    #layouts

                    fn get_item_ref(self: ::core::pin::Pin<&Self>, index: usize) -> ::core::pin::Pin<ItemRef> {
                        match &Self::item_tree()[index] {
                            ItemTreeNode::Item { item, .. } => item.apply_pin(self),
                            ItemTreeNode::DynamicTree { .. } => panic!("get_item_ref called on dynamic tree"),

                        }
                    }

                    fn parent_item(self: ::core::pin::Pin<&Self>, index: usize, result: &mut sixtyfps::re_exports::ItemWeak) {
                        if index == 0 {
                            #(
                                if let Some(parent) = self.parent.clone().into_dyn().upgrade() {
                                    *result = sixtyfps::re_exports::ItemRc::new(parent, #parent_item_index).parent_item();
                                }
                            )*
                            return;
                        }
                        let parent_index = match &Self::item_tree()[index] {
                            ItemTreeNode::Item { parent_index, .. } => *parent_index,
                            ItemTreeNode::DynamicTree { parent_index, .. } => *parent_index,
                        };
                        let self_rc = self.self_weak.get().unwrap().clone().into_dyn().upgrade().unwrap();
                        *result = ItemRc::new(self_rc, parent_index as _).downgrade()
                    }
                }
            }),
        )
    };

    let (global_name, global_type): (Vec<_>, Vec<_>) = component
        .used_types
        .borrow()
        .globals
        .iter()
        .map(|g| (format_ident!("global_{}", public_component_id(g)), self::inner_component_id(g)))
        .unzip();

    let new_code = if !component.is_global() {
        quote! {
            let self_rc = VRc::new(self_);
            self_rc.self_weak.set(VRc::downgrade(&self_rc)).map_err(|_|())
                .expect("Can only be pinned once");
            let _self = self_rc.as_pin_ref();
        }
    } else {
        quote! {
            let self_rc = ::std::rc::Rc::pin(self_);
            let _self = self_rc.as_ref();
        }
    };
    let self_weak = if !component.is_global() { Some(quote!(self_weak)) } else { None };
    let self_weak = self_weak.into_iter().collect::<Vec<_>>();
    let component_handle = if !component.is_global() {
        quote!(vtable::VRc<sixtyfps::re_exports::ComponentVTable, Self>)
    } else {
        quote!(::core::pin::Pin<::std::rc::Rc<Self>>)
    };

    let public_component_id = public_component_id(component);
    let public_interface = if !component.is_global() && component.visible_in_public_api() {
        let parent_name =
            if !parent_component_type.is_empty() { Some(quote!(parent)) } else { None };
        let window_parent_name = window_parent_param.as_ref().map(|_| quote!(, parent_window));

        let component_handle_impl = if component.parent_element.upgrade().is_none() {
            Some(quote!(
                impl sixtyfps::ComponentHandle for #public_component_id {
                    type Inner = #inner_component_id;
                    fn as_weak(&self) -> sixtyfps::Weak<Self> {
                        sixtyfps::Weak::new(&self.0)
                    }

                    fn clone_strong(&self) -> Self {
                        Self(self.0.clone())
                    }

                    fn from_inner(inner: vtable::VRc<sixtyfps::re_exports::ComponentVTable, #inner_component_id>) -> Self {
                        Self(inner)
                    }

                    fn run(&self) {
                        self.show();
                        sixtyfps::run_event_loop();
                        self.hide();
                    }

                    fn show(&self) {
                        self.window().show();
                    }

                    fn hide(&self) {
                        self.window().hide()
                    }

                    fn window(&self) -> &sixtyfps::Window {
                        &vtable::VRc::as_pin_ref(&self.0).get_ref().window
                    }

                    fn global<'a, T: sixtyfps::Global<'a, Self>>(&'a self) -> T {
                        T::get(&self)
                    }
                }
            ))
        } else {
            None
        };

        let global_accessor_impl = global_name
            .iter()
            .zip(component.used_types.borrow().globals.iter())
            .filter_map(|(global_name, global)| {
                global.visible_in_public_api().then(|| {
                    let global_type = self::public_component_id(global);
                    quote!(
                        impl<'a> sixtyfps::Global<'a, #public_component_id> for #global_type<'a> {
                            fn get(component: &'a #public_component_id) -> Self {
                                Self(&component.0.#global_name)
                            }
                        }
                    )
                })
            })
            .collect::<Vec<_>>();

        Some(quote!(
            #visibility struct #public_component_id(vtable::VRc<sixtyfps::re_exports::ComponentVTable, #inner_component_id>);

            impl #public_component_id {
                pub fn new(#(parent: sixtyfps::re_exports::VWeak::<sixtyfps::re_exports::ComponentVTable, #parent_component_type>)* #window_parent_param) -> Self {
                    Self(#inner_component_id::new(#parent_name #window_parent_name))
                }
                #(#property_and_callback_accessors)*
            }

            #component_handle_impl

            #(#global_accessor_impl)*

            impl From<#public_component_id> for vtable::VRc<sixtyfps::re_exports::ComponentVTable, #inner_component_id> {
                fn from(value: #public_component_id) -> Self {
                    value.0
                }
            }
        ))
    } else if component.is_global() && component.visible_in_public_api() {
        let aliases =
            component.global_aliases().into_iter().map(|name| ident(&name)).collect::<Vec<_>>();

        Some(quote!(
            #visibility struct #public_component_id<'a>(&'a ::core::pin::Pin<::std::rc::Rc<#inner_component_id>>);

            impl<'a> #public_component_id<'a> {
                #(#property_and_callback_accessors)*
            }

            #(#visibility type #aliases<'a> = #public_component_id<'a>;)*
        ))
    } else {
        None
    };

    Some(quote!(
        #(#resource_symbols)*

        #[derive(sixtyfps::re_exports::FieldOffsets)]
        #[const_field_offset(sixtyfps::re_exports::const_field_offset)]
        #[repr(C)]
        #pin
        #visibility struct #inner_component_id {
            #(#item_names : sixtyfps::re_exports::#item_types,)*
            #(#declared_property_vars : sixtyfps::re_exports::Property<#declared_property_types>,)*
            #(#declared_callbacks : sixtyfps::re_exports::Callback<(#(#declared_callbacks_types,)*), #declared_callbacks_ret>,)*
            #(#repeated_element_names : sixtyfps::re_exports::Repeater<#repeated_element_components>,)*
            #(#self_weak : sixtyfps::re_exports::OnceCell<sixtyfps::re_exports::VWeak<sixtyfps::re_exports::ComponentVTable, #inner_component_id>>,)*
            #(parent : sixtyfps::re_exports::VWeak<sixtyfps::re_exports::ComponentVTable, #parent_component_type>,)*
            #(#global_name : ::core::pin::Pin<::std::rc::Rc<#global_type>>,)*
            #window_field
        }

        #component_impl

        impl #inner_component_id{
            pub fn new(#(parent: sixtyfps::re_exports::VWeak::<sixtyfps::re_exports::ComponentVTable, #parent_component_type>)* #window_parent_param)
                -> #component_handle
            {
                #![allow(unused)]
                use sixtyfps::re_exports::*;
                let mut self_ = Self {
                    #(#item_names : ::core::default::Default::default(),)*
                    #(#declared_property_vars : ::core::default::Default::default(),)*
                    #(#declared_callbacks : ::core::default::Default::default(),)*
                    #(#repeated_element_names : ::core::default::Default::default(),)*
                    #(#self_weak : ::core::default::Default::default(),)*
                    #(parent : parent as sixtyfps::re_exports::VWeak::<sixtyfps::re_exports::ComponentVTable, #parent_component_type>,)*
                    #(#global_name : #global_type::new(),)*
                    #window_field_init
                };
                #new_code
                #(#init)*
                self_rc
            }
            #item_tree_impl

        }

        #public_interface

        #drop_impl

        #has_window_impl

        #(#extra_components)*
    ))
}

/// Return an identifier suitable for this component for internal use
fn inner_component_id(component: &Component) -> proc_macro2::Ident {
    if component.is_global()
        && matches!(&component.root_element.borrow().base_type, Type::Builtin(_))
    {
        public_component_id(component)
    } else {
        format_ident!("Inner{}", public_component_id(component))
    }
}

/// Return an identifier suitable for this component for the developer facing API
fn public_component_id(component: &Component) -> proc_macro2::Ident {
    if component.is_global() {
        ident(&component.root_element.borrow().id)
    } else if component.id.is_empty() {
        let s = &component.root_element.borrow().id;
        // Capitalize first letter:
        let mut it = s.chars();
        let id =
            it.next().map(|c| c.to_ascii_uppercase()).into_iter().chain(it).collect::<String>();
        ident(&id)
    } else {
        ident(&component.id)
    }
}

fn property_animation_tokens(
    component: &Rc<Component>,
    animation: &ElementRc,
) -> Option<TokenStream> {
    let animation = animation.borrow();
    let bindings = animation.bindings.iter().map(|(prop, initializer)| {
        let prop_ident = ident(prop);
        let initializer = compile_expression(initializer, component);
        quote!(#prop_ident: #initializer as _)
    });

    Some(quote!(sixtyfps::re_exports::PropertyAnimation{
        #(#bindings, )*
        ..::core::default::Default::default()
    }))
}

fn property_set_value_tokens(
    component: &Rc<Component>,
    element: &ElementRc,
    property_name: &str,
    value_tokens: TokenStream,
) -> TokenStream {
    match element.borrow().bindings.get(property_name).and_then(|b| b.animation.as_ref()) {
        Some(crate::object_tree::PropertyAnimation::Static(animation)) => {
            let animation_tokens = property_animation_tokens(component, animation);
            quote!(set_animated_value(#value_tokens, #animation_tokens))
        }
        _ => quote!(set(#value_tokens)),
    }
}

/// Returns the code that can access the given property or callback (but without the set or get)
///
/// to be used like:
/// ```ignore
/// let access = access_member(...)
/// quote!(#access.get())
/// ```
fn access_member(
    element: &ElementRc,
    name: &str,
    component: &Rc<Component>,
    component_rust: TokenStream,
    is_special: bool,
) -> TokenStream {
    let e = element.borrow();

    let enclosing_component = e.enclosing_component.upgrade().unwrap();
    if Rc::ptr_eq(component, &enclosing_component) {
        let inner_component_id = inner_component_id(&enclosing_component);
        let name_ident = ident(name);
        if e.property_declarations.contains_key(name) || is_special || component.is_global() {
            let field = access_component_field_offset(&inner_component_id, &name_ident);
            quote!(#field.apply_pin(#component_rust))
        } else if e.is_flickable_viewport {
            let elem_ident =
                ident(&crate::object_tree::find_parent_element(element).unwrap().borrow().id);
            let element_field = access_component_field_offset(&inner_component_id, &elem_ident);

            quote!((#element_field
                + sixtyfps::re_exports::Flickable::FIELD_OFFSETS.viewport
                + sixtyfps::re_exports::Rectangle::FIELD_OFFSETS.#name_ident)
                    .apply_pin(#component_rust)
            )
        } else {
            let elem_ident = ident(&e.id);
            let elem_ty = ident(&e.base_type.as_native().class_name);
            let element_field = access_component_field_offset(&inner_component_id, &elem_ident);

            quote!((#element_field + #elem_ty::FIELD_OFFSETS.#name_ident)
                .apply_pin(#component_rust)
            )
        }
    } else if enclosing_component.is_global() {
        let mut root_component = component.clone();
        let mut component_rust = component_rust;
        while let Some(p) = root_component.parent_element.upgrade() {
            root_component = p.borrow().enclosing_component.upgrade().unwrap();
            component_rust = quote!(#component_rust.parent.upgrade().unwrap().as_pin_ref());
        }
        let global_id = format_ident!("global_{}", public_component_id(&enclosing_component));
        let global_comp = quote!(#component_rust.#global_id.as_ref());
        access_member(element, name, &enclosing_component, global_comp, is_special)
    } else {
        access_member(
            element,
            name,
            &component
                .parent_element
                .upgrade()
                .unwrap()
                .borrow()
                .enclosing_component
                .upgrade()
                .unwrap(),
            quote!(#component_rust.parent.upgrade().unwrap().as_pin_ref()),
            is_special,
        )
    }
}

/// Call access_member  for a NamedReference
fn access_named_reference(
    nr: &NamedReference,
    component: &Rc<Component>,
    component_rust: TokenStream,
) -> TokenStream {
    access_member(&nr.element(), nr.name(), component, component_rust, false)
}

/// Returns the code that can access the component of the given element
fn access_element_component(
    element: &ElementRc,
    component: &Rc<Component>,
    component_rust: TokenStream,
) -> TokenStream {
    let e = element.borrow();

    let enclosing_component = e.enclosing_component.upgrade().unwrap();
    if Rc::ptr_eq(component, &enclosing_component) {
        return component_rust;
    } else {
        access_element_component(
            element,
            &component
                .parent_element
                .upgrade()
                .unwrap()
                .borrow()
                .enclosing_component
                .upgrade()
                .unwrap(),
            quote!(#component_rust.parent.upgrade().unwrap().as_pin_ref()),
        )
    }
}

fn compile_expression(expr: &Expression, component: &Rc<Component>) -> TokenStream {
    match expr {
        Expression::StringLiteral(s) => quote!(sixtyfps::re_exports::SharedString::from(#s)),
        Expression::NumberLiteral(n, unit) => {
            let n = unit.normalize(*n);
            quote!(#n)
        }
        Expression::BoolLiteral(b) => quote!(#b),
        Expression::Cast { from, to } => {
            let f = compile_expression(&*from, component);
            match (from.ty(), to) {
                (Type::Float32, Type::String) | (Type::Int32, Type::String) => {
                    quote!(sixtyfps::re_exports::SharedString::from(format!("{}", #f).as_str()))
                }
                (Type::Float32, Type::Model) | (Type::Int32, Type::Model) => {
                    quote!(sixtyfps::re_exports::ModelHandle::new(std::rc::Rc::<usize>::new(#f as usize)))
                }
                (Type::Float32, Type::Color) => {
                    quote!(sixtyfps::re_exports::Color::from_argb_encoded(#f as u32))
                }
                (Type::Color, Type::Brush) => {
                    quote!(sixtyfps::Brush::SolidColor(#f))
                }
                (Type::Brush, Type::Color) => {
                    quote!(#f.color())
                }
                (Type::Struct { ref fields, .. }, Type::Component(c)) => {
                    let fields = fields.iter().enumerate().map(|(index, (name, _))| {
                        let index = proc_macro2::Literal::usize_unsuffixed(index);
                        let name = ident(name);
                        quote!(#name: obj.#index as _)
                    });
                    let id : TokenStream = c.id.parse().unwrap();
                    quote!({ let obj = #f; #id { #(#fields),*} })
                }
                (Type::Struct { ref fields, .. }, Type::Struct{  name: Some(n), .. }) => {
                    let fields = fields.iter().enumerate().map(|(index, (name, _))| {
                        let index = proc_macro2::Literal::usize_unsuffixed(index);
                        let name = ident(name);
                        quote!(#name: obj.#index as _)
                    });
                    let id = struct_name_to_tokens(n);
                    quote!({ let obj = #f; #id { #(#fields),*} })
                }
                _ => f,
            }
        }
        Expression::PropertyReference(nr) => {
            let access = access_named_reference(nr, component, quote!(_self));
            quote!(#access.get())
        }
        Expression::BuiltinFunctionReference(funcref, _) => match funcref {
            BuiltinFunction::GetWindowScaleFactor => {
                quote!(_self.window.window_handle().scale_factor)
            }
            BuiltinFunction::Debug => quote!((|x| println!("{:?}", x))),
            BuiltinFunction::Mod => quote!((|a1, a2| (a1 as i32) % (a2 as i32))),
            BuiltinFunction::Round => quote!((|a| (a as f64).round())),
            BuiltinFunction::Ceil => quote!((|a| (a as f64).ceil())),
            BuiltinFunction::Floor => quote!((|a| (a as f64).floor())),
            BuiltinFunction::Sqrt => quote!((|a| (a as f64).sqrt())),
            BuiltinFunction::Abs => quote!((|a| (a as f64).abs())),
            BuiltinFunction::Sin => quote!((|a| (a as f64).to_radians().sin())),
            BuiltinFunction::Cos => quote!((|a| (a as f64).to_radians().cos())),
            BuiltinFunction::Tan => quote!((|a| (a as f64).to_radians().tan())),
            BuiltinFunction::ASin => quote!((|a| (a as f64).asin().to_degrees())),
            BuiltinFunction::ACos => quote!((|a| (a as f64).acos().to_degrees())),
            BuiltinFunction::ATan => quote!((|a| (a as f64).atan().to_degrees())),
            BuiltinFunction::SetFocusItem | BuiltinFunction::ShowPopupWindow | BuiltinFunction::ImplicitLayoutInfo(_) => {
                panic!("internal error: should be handled directly in CallFunction")
            }
            BuiltinFunction::StringToFloat => {
                quote!((|x: SharedString| -> f64 { ::core::str::FromStr::from_str(x.as_str()).unwrap_or_default() } ))
            }
            BuiltinFunction::StringIsFloat => {
                quote!((|x: SharedString| { <f64 as ::core::str::FromStr>::from_str(x.as_str()).is_ok() } ))
            }
            BuiltinFunction::ColorBrighter => {
                quote!((|x: Color, factor| -> Color { x.brighter(factor as f32) }))
            }
            BuiltinFunction::ColorDarker => {
                quote!((|x: Color, factor| -> Color { x.darker(factor as f32) }))
            }
            BuiltinFunction::ImageSize => {
                quote!((|x: Image| -> Size { x.size() }))
            }

            BuiltinFunction::Rgb => {
                quote!((|r: i32, g: i32, b: i32, a: f32| {
                    let r: u8 = r.max(0).min(255) as u8;
                    let g: u8 = g.max(0).min(255) as u8;
                    let b: u8 = b.max(0).min(255) as u8;
                    let a: u8 = (255. * a).max(0.).min(255.) as u8;
                    sixtyfps::re_exports::Color::from_argb_u8(a, r, g, b)
                }))
            }
            BuiltinFunction::RegisterCustomFontByPath => {
                panic!("internal error: BuiltinFunction::RegisterCustomFontByPath can only be compiled as part of a FunctionCall expression")
            }
            BuiltinFunction::RegisterCustomFontByMemory => {
                panic!("internal error: BuiltinFunction::RegisterCustomFontByMemory can only be compiled as part of a FunctionCall expression")
            }
        },
        Expression::ElementReference(_) => todo!("Element references are only supported in the context of built-in function calls at the moment"),
        Expression::MemberFunction{ .. } => panic!("member function expressions must not appear in the code generator anymore"),
        Expression::BuiltinMacroReference { .. } => panic!("macro expressions must not appear in the code generator anymore"),
        Expression::RepeaterIndexReference { element } => {
            let access = access_member(
                &element.upgrade().unwrap().borrow().base_type.as_component().root_element,
                "index",
                component,
                quote!(_self),
                true,
            );
            quote!(#access.get())
        }
        Expression::RepeaterModelReference { element } => {
            let access = access_member(
                &element.upgrade().unwrap().borrow().base_type.as_component().root_element,
                "model_data",
                component,
                quote!(_self),
                true,
            );
            quote!(#access.get())
        }
        Expression::FunctionParameterReference { index, .. } => {
            let i = proc_macro2::Literal::usize_unsuffixed(*index);
            quote! {args.#i.clone()}
        }
        Expression::StructFieldAccess { base, name } => match base.ty() {
            Type::Struct { fields, name: None, .. } => {
                let index = fields
                    .keys()
                    .position(|k| k == name)
                    .expect("Expression::ObjectAccess: Cannot find a key in an object");
                let index = proc_macro2::Literal::usize_unsuffixed(index);
                let base_e = compile_expression(base, component);
                quote!((#base_e).#index )
            }
            Type::Struct { .. } => {
                let name = ident(name);
                let base_e = compile_expression(base, component);
                quote!((#base_e).#name)
            }
            _ => panic!("Expression::ObjectAccess's base expression is not an Object type"),
        },
        Expression::CodeBlock(sub) => {
            let map = sub.iter().map(|e| compile_expression(e, component));
            quote!({ #(#map);* })
        }
        Expression::CallbackReference(nr) => access_named_reference(
            nr,
            component,
            quote!(_self),
        ),
        Expression::FunctionCall { function, arguments,  source_location: _ } => {
            match &**function {
                Expression::BuiltinFunctionReference(BuiltinFunction::SetFocusItem, _) => {
                    if arguments.len() != 1 {
                        panic!("internal error: incorrect argument count to SetFocusItem call");
                    }
                    if let Expression::ElementReference(focus_item) = &arguments[0] {
                        let focus_item = focus_item.upgrade().unwrap();
                        let component_ref = access_element_component(&focus_item, component, quote!(_self));
                        let focus_item = focus_item.borrow();
                        let item_index = focus_item.item_index.get().unwrap();
                        quote!(
                            _self.window.window_handle().clone().set_focus_item(&ItemRc::new(VRc::into_dyn(#component_ref.self_weak.get().unwrap().upgrade().unwrap()), #item_index));
                        )
                    } else {
                        panic!("internal error: argument to SetFocusItem must be an element")
                    }
                }
                Expression::BuiltinFunctionReference(BuiltinFunction::ShowPopupWindow, _) => {
                    if arguments.len() != 1 {
                        panic!("internal error: incorrect argument count to ShowPopupWindow call");
                    }
                    if let Expression::ElementReference(popup_window) = &arguments[0] {
                        let popup_window = popup_window.upgrade().unwrap();
                        let pop_comp = popup_window.borrow().enclosing_component.upgrade().unwrap();
                        let popup_window_id = inner_component_id(&pop_comp);
                        let parent_component = pop_comp.parent_element.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
                        let popup_list = parent_component.popup_windows.borrow();
                        let popup = popup_list.iter().find(|p| Rc::ptr_eq(&p.component, &pop_comp)).unwrap();
                        let x = access_named_reference(&popup.x, component, quote!(_self));
                        let y = access_named_reference(&popup.y, component, quote!(_self));
                        quote!(
                            _self.window.window_handle().show_popup(
                                &VRc::into_dyn(#popup_window_id::new(_self.self_weak.get().unwrap().clone(), &_self.window.window_handle()).into()),
                                Point::new(#x.get(), #y.get())
                            );
                        )
                    } else {
                        panic!("internal error: argument to SetFocusItem must be an element")
                    }
                }
                Expression::BuiltinFunctionReference(BuiltinFunction::ImplicitLayoutInfo(orient), _) => {
                    if arguments.len() != 1 {
                        panic!("internal error: incorrect argument count to ImplicitLayoutInfo call");
                    }
                    if let Expression::ElementReference(item) = &arguments[0] {
                        let item = item.upgrade().unwrap();
                        let item = item.borrow();
                        let item_id = ident(&item.id);
                        let item_field = access_component_field_offset(&format_ident!("Self"), &item_id);
                        quote!(
                            #item_field.apply_pin(_self).layouting_info(#orient, &_self.window.window_handle())
                        )
                    } else {
                        panic!("internal error: argument to ImplicitLayoutInfo must be an element")
                    }
                }
                Expression::BuiltinFunctionReference(BuiltinFunction::RegisterCustomFontByPath, _) => {
                    if arguments.len() != 1 {
                        panic!("internal error: incorrect argument count to RegisterCustomFontByPath call");
                    }
                    if let Expression::StringLiteral(path) = &arguments[0] {
                        quote!(sixtyfps::register_font_from_path(&std::path::PathBuf::from(#path));)
                    } else {
                        panic!("internal error: argument to RegisterCustomFontByPath must be a string literal")
                    }
                }
                Expression::BuiltinFunctionReference(BuiltinFunction::RegisterCustomFontByMemory, _) => {
                    if arguments.len() != 1 {
                        panic!("internal error: incorrect argument count to RegisterCustomFontByMemory call");
                    }
                    if let Expression::NumberLiteral(resource_id, _) = &arguments[0] {
                        let resource_id: usize = *resource_id as _;
                        let symbol = format_ident!("SFPS_EMBEDDED_RESOURCE_{}", resource_id);
                        quote!(sixtyfps::register_font_from_memory(#symbol.into());)
                    } else {
                        panic!("internal error: argument to RegisterCustomFontByMemory must be a number")
                    }
                }
                _ => {
                    let f = compile_expression(function, component);
                    let a = arguments.iter().map(|a| compile_expression(a, component));
                    let function_type = function.ty();
                    match function_type {
                         Type::Callback { args, .. } => {
                            let cast = args.iter().map(|ty| match ty {
                                Type::Bool => quote!(as bool),
                                Type::Int32 => quote!(as i32),
                                Type::Float32 => quote!(as f32),
                                _ => quote!(.clone()),
                            });
                            quote! { #f.call(&(#((#a)#cast,)*).into())}
                        }
                        Type::Function {args, .. } => {
                            let cast = args.iter().map(|ty| match ty {
                                Type::Bool => quote!(as bool),
                                Type::Int32 => quote!(as i32),
                                Type::Float32 => quote!(as f32),
                                _ => quote!(.clone()),
                            });
                            quote! { #f(#((#a) #cast),*)}
                        }
                        _ => panic!("not calling a function")
                    }
                }
            }

        }
        Expression::SelfAssignment { lhs, rhs, op } => {
            let rhs = compile_expression(&*rhs, component);
            compile_assignment(lhs, *op, rhs, component)
        }
        Expression::BinaryExpression { lhs, rhs, op } => {
            let (conv1, conv2) = match crate::expression_tree::operator_class(*op) {
                OperatorClass::ArithmeticOp => match lhs.ty() {
                    Type::String => (None, Some(quote!(.as_str()))),
                    Type::Struct{..} => (None, None),
                    _ => (Some(quote!(as f64)), Some(quote!(as f64))),
                },
                OperatorClass::ComparisonOp
                    if matches!(
                        lhs.ty(),
                        Type::Int32
                            | Type::Float32
                            | Type::Duration
                            | Type::PhysicalLength
                            | Type::LogicalLength
                            | Type::Angle
                    ) =>
                {
                    (Some(quote!(as f64)), Some(quote!(as f64)))
                }
                _ => (None, None),
            };
            let lhs = compile_expression(&*lhs, component);
            let rhs = compile_expression(&*rhs, component);

            let op = match op {
                '=' => quote!(==),
                '!' => quote!(!=),
                '≤' => quote!(<=),
                '≥' => quote!(>=),
                '&' => quote!(&&),
                '|' => quote!(||),
                _ => proc_macro2::TokenTree::Punct(proc_macro2::Punct::new(
                    *op,
                    proc_macro2::Spacing::Alone,
                ))
                .into(),
            };
            quote!( ((#lhs #conv1 ) #op (#rhs #conv2)) )
        }
        Expression::UnaryOp { sub, op } => {
            let sub = compile_expression(&*sub, component);
            if *op == '+' {
                // there is no unary '+' in rust
                return sub;
            }
            let op = proc_macro2::Punct::new(*op, proc_macro2::Spacing::Alone);
            quote!( #op #sub )
        }
        Expression::ImageReference(resource_ref) => {
            match resource_ref {
                crate::expression_tree::ImageReference::None => {
                    quote!(sixtyfps::re_exports::Image::default())
                }
                crate::expression_tree::ImageReference::AbsolutePath(path) => {
                     quote!(sixtyfps::re_exports::Image::load_from_path(::std::path::Path::new(#path)).unwrap())
                },
                crate::expression_tree::ImageReference::EmbeddedData { resource_id, extension } => {
                    let symbol = format_ident!("SFPS_EMBEDDED_RESOURCE_{}", resource_id);
                    let format = proc_macro2::Literal::byte_string(extension.as_bytes());
                    quote!(
                        sixtyfps::re_exports::Image::from(
                            sixtyfps::re_exports::ImageInner::EmbeddedData{ data: #symbol.into(), format: Slice::from_slice(#format) }
                        )
                    )
                }
            }
        }
        Expression::Condition { condition, true_expr, false_expr } => {
            let condition_code = compile_expression(&*condition, component);
            let true_code = compile_expression(&*true_expr, component);
            let false_code = compile_expression(&*false_expr, component);
            quote!(
                if #condition_code {
                    #true_code
                } else {
                    (#false_code) as _
                }
            )
        }
        Expression::Invalid | Expression::Uncompiled(_)  => {
            let error = format!("unsupported expression {:?}", expr);
            quote!(compile_error! {#error})
        }
        Expression::Array { values, element_ty } => {
            let rust_element_ty = rust_type(element_ty).unwrap();
            let val = values.iter().map(|e| compile_expression(e, component));
            quote!(sixtyfps::re_exports::ModelHandle::new(
                std::rc::Rc::new(sixtyfps::re_exports::VecModel::<#rust_element_ty>::from(vec![#(#val as _),*]))
            ))
        }
        Expression::Struct { ty, values } => {
            if let Type::Struct { fields, name, .. } = ty {
                let elem = fields.iter().map(|(k, t)| {
                    values.get(k).map(|e| {
                        let ce = compile_expression(e, component);
                        let t = rust_type(t).unwrap_or_default();
                        quote!(#ce as #t)
                    })
                });
                if let Some(name) = name {
                    let name : TokenStream = struct_name_to_tokens(name.as_str());
                    let keys = fields.keys().map(|k| ident(k));
                    quote!(#name { #(#keys: #elem,)* })
                } else {
                    // This will produce a tuple
                    quote!((#(#elem,)*))
                }
            } else {
                panic!("Expression::Object is not a Type::Object")
            }
        }
        Expression::PathElements { elements } => compile_path(elements, component),
        Expression::StoreLocalVariable { name, value } => {
            let value = compile_expression(value, component);
            let name = ident(name);
            quote!(let #name = #value;)
        }
        Expression::ReadLocalVariable { name, .. } => {
            let name = ident(name);
            quote!(#name)
        }
        Expression::EasingCurve(EasingCurve::Linear) => {
            quote!(sixtyfps::re_exports::EasingCurve::Linear)
        }
        Expression::EasingCurve(EasingCurve::CubicBezier(a, b, c, d)) => {
            quote!(sixtyfps::re_exports::EasingCurve::CubicBezier([#a, #b, #c, #d]))
        }
        Expression::LinearGradient{angle, stops} => {
            let angle = compile_expression(angle, component);
            let stops = stops.iter().map(|(color, stop)| {
                let color = compile_expression(color, component);
                let position = compile_expression(stop, component);
                quote!(sixtyfps::re_exports::GradientStop{ color: #color, position: #position as _ })
            });
            quote!(sixtyfps::Brush::LinearGradient(
                sixtyfps::re_exports::LinearGradientBrush::new(#angle as _, [#(#stops),*].iter().cloned())
            ))
        }
        Expression::EnumerationValue(value) => {
            let base_ident = ident(&value.enumeration.name);
            let value_ident = ident(&value.to_string());
            quote!(sixtyfps::re_exports::#base_ident::#value_ident)
        }
        Expression::ReturnStatement(expr) => {
            let return_expr = expr.as_ref().map(|expr| compile_expression(expr, component));
            quote!(return (#return_expr) as _;)
        },
        Expression::LayoutCacheAccess { layout_cache_prop, index, repeater_index } => {
            let cache = access_named_reference(layout_cache_prop, component, quote!(_self));
            if let Some(ri) = repeater_index {
                let offset = compile_expression(ri, component);
                quote!({
                    let cache = #cache.get();
                    *cache.get((cache[#index] as usize) + #offset as usize * 2).unwrap_or(&0.)
                })
            } else {
                quote!(#cache.get()[#index])
            }
        }
        Expression::ComputeLayoutInfo(Layout::GridLayout(layout), o) => {
            let (padding, spacing) = generate_layout_padding_and_spacing(&layout.geometry, *o, component);
            let cells = grid_layout_cell_data(layout, *o, component);
            quote!(grid_layout_info(Slice::from_slice(&#cells), #spacing, #padding))
        }
        Expression::ComputeLayoutInfo(Layout::BoxLayout(layout), o) => {
            let (padding, spacing) = generate_layout_padding_and_spacing(&layout.geometry,*o, component);
            let (cells, alignment) = box_layout_data(layout, *o, component, None);
            if *o == layout.orientation {
                quote!(box_layout_info(Slice::from_slice(&#cells), #spacing, #padding, #alignment))
            } else {
                quote!(box_layout_info_ortho(Slice::from_slice(&#cells), #padding))
            }
        }
        Expression::ComputeLayoutInfo(Layout::PathLayout(_), _) => unimplemented!(),
        Expression::SolveLayout(Layout::GridLayout(layout), o) => {
            let (padding, spacing) = generate_layout_padding_and_spacing(&layout.geometry, *o, component);
            let cells = grid_layout_cell_data(layout, *o, component);
            let size = layout_geometry_size(&layout.geometry.rect, *o, component);
            quote!(solve_grid_layout(&GridLayoutData{
                size: #size,
                spacing: #spacing,
                padding: #padding,
                cells: Slice::from_slice(&#cells),
            }))
        }
        Expression::SolveLayout(Layout::BoxLayout(layout), o) => {
            let (padding, spacing) = generate_layout_padding_and_spacing(&layout.geometry, *o, component);
            let mut repeated_indices = Default::default();
            let mut repeated_indices_init = Default::default();
            let (cells, alignment) = box_layout_data(layout, *o, component, Some((&mut repeated_indices, &mut repeated_indices_init)));
            let size = layout_geometry_size(&layout.geometry.rect, *o, component);
            quote!({
                #repeated_indices_init
                solve_box_layout(
                    &BoxLayoutData {
                        size: #size,
                        spacing: #spacing,
                        padding: #padding,
                        alignment: #alignment,
                        cells: Slice::from_slice(&#cells),
                    },
                    Slice::from_slice(&#repeated_indices),
                )
            })
        }
        Expression::SolveLayout(Layout::PathLayout(layout), _) => {
            let width = layout_geometry_size(&layout.rect,  Orientation::Horizontal, component);
            let height = layout_geometry_size(&layout.rect,  Orientation::Vertical, component);
            let elements = compile_path(&layout.path, component);
            let get_prop = |nr: &Option<NamedReference>| {
                nr.as_ref().map_or_else(
                    || quote!(::core::default::Default::default()),
                    |nr| {
                        let p = access_named_reference(nr, component, quote!(_self));
                        quote!(#p.get())
                    },
                )
            };
            let offset = get_prop(&layout.offset_reference);
            let count = layout.elements.len(); // FIXME! repeater
            quote!(
                solve_path_layout(
                    &PathLayoutData {
                        width: #width,
                        height: #height,
                        x: 0.,
                        y: 0.,
                        elements: &#elements,
                        offset: #offset,
                        item_count: #count as _,
                    },
                    Slice::from_slice(&[]),
                )
            )
        }
    }
}

/// Return a TokenStream for a name (as in [`Type::Struct::name`])
fn struct_name_to_tokens(name: &str) -> TokenStream {
    // the name match the C++ signature so we need to change that to the rust namespace
    let mut name = name.replace("::private_api::", "::re_exports::").replace('-', "_");
    if !name.contains("::") {
        name.insert_str(0, "r#")
    }
    name.parse().unwrap()
}

fn compile_assignment(
    lhs: &Expression,
    op: char,
    rhs: TokenStream,
    component: &Rc<Component>,
) -> TokenStream {
    match lhs {
        Expression::PropertyReference(nr) => {
            let lhs_ = access_named_reference(nr, component, quote!(_self));
            if op == '=' {
                quote!( #lhs_.set((#rhs) as _) )
            } else {
                let op = proc_macro2::Punct::new(op, proc_macro2::Spacing::Alone);
                if lhs.ty() == Type::String {
                    quote!( #lhs_.set(#lhs_.get() #op #rhs.as_str()) )
                } else {
                    quote!( #lhs_.set(((#lhs_.get() as f64) #op (#rhs as f64)) as _) )
                }
            }
        }
        Expression::StructFieldAccess { base, name } => {
            let tmpobj = quote!(tmpobj);
            let get_obj = compile_expression(base, component);
            let ty = base.ty();
            let (member, member_ty) = match &ty {
                Type::Struct { fields, name: None, .. } => {
                    let index = fields
                        .keys()
                        .position(|k| k == name)
                        .expect("Expression::ObjectAccess: Cannot find a key in an object");
                    let index = proc_macro2::Literal::usize_unsuffixed(index);
                    (quote!(#index), fields[name].clone())
                }
                Type::Struct { fields, name: Some(_), .. } => {
                    let n = ident(name);
                    (quote!(#n), fields[name].clone())
                }
                _ => panic!("Expression::ObjectAccess's base expression is not an Object type"),
            };

            let conv = if member_ty == Type::String {
                if op == '=' {
                    quote!()
                } else {
                    quote!(.as_str())
                }
            } else {
                let member_ty = rust_type(&member_ty).unwrap_or_default();
                quote!(as #member_ty)
            };

            let op = match op {
                '+' => quote!(+=),
                '*' => quote!(*=),
                '-' => quote!(-=),
                '/' => quote!(/=),
                '=' => quote!(=),
                _ => panic!("Unknown assignment op {:?}", op),
            };

            let new_value = quote!({
               let mut #tmpobj = #get_obj;
               #tmpobj.#member #op (#rhs #conv);
               #tmpobj
            });
            compile_assignment(base, '=', new_value, component)
        }
        Expression::RepeaterModelReference { element } => {
            let element = element.upgrade().unwrap();
            let parent_component = element.borrow().base_type.as_component().clone();
            let repeater_access = access_member(
                &parent_component
                    .parent_element
                    .upgrade()
                    .unwrap()
                    .borrow()
                    .enclosing_component
                    .upgrade()
                    .unwrap()
                    .root_element,
                &format!("repeater_{}", element.borrow().id),
                component,
                quote!(_self),
                true,
            );
            let index_access = access_member(
                &parent_component.root_element,
                "index",
                component,
                quote!(_self),
                true,
            );
            if op == '=' {
                quote!(#repeater_access.model_set_row_data(#index_access.get(), #rhs as _))
            } else {
                let op = proc_macro2::Punct::new(op, proc_macro2::Spacing::Alone);
                let old_data = compile_expression(lhs, component);
                if lhs.ty() == Type::String {
                    quote!(#repeater_access.model_set_row_data(#index_access.get(), #old_data #op &#rhs))
                } else {
                    quote!(#repeater_access.model_set_row_data(#index_access.get(), ((#old_data as f64) #op (#rhs as f64)) as _))
                }
            }
        }
        _ => panic!("typechecking should make sure this was a PropertyReference"),
    }
}

fn grid_layout_cell_data(
    layout: &crate::layout::GridLayout,
    orientation: Orientation,
    component: &Rc<Component>,
) -> TokenStream {
    let cells = layout.elems.iter().map(|c| {
        let (col_or_row, span) = c.col_or_row_and_span(orientation);
        let layout_info =
            get_layout_info(&c.item.element, component, &c.item.constraints, orientation);
        quote!(GridLayoutCellData {
            col_or_row: #col_or_row,
            span: #span,
            constraint: #layout_info,
        })
    });
    quote!([ #(#cells),* ])
}

/// Returns `(cells, alignment)`.
/// The repeated_indices initialize the repeated_indices (var, init_code)
fn box_layout_data(
    layout: &crate::layout::BoxLayout,
    orientation: Orientation,
    component: &Rc<Component>,
    mut repeated_indices: Option<(&mut TokenStream, &mut TokenStream)>,
) -> (TokenStream, TokenStream) {
    let alignment = if let Some(expr) = &layout.geometry.alignment {
        let p = access_named_reference(expr, component, quote!(_self));
        quote!(#p.get())
    } else {
        quote!(::core::default::Default::default())
    };

    let repeater_count =
        layout.elems.iter().filter(|i| i.element.borrow().repeated.is_some()).count();

    if repeater_count == 0 {
        let cells = layout.elems.iter().map(|li| {
            let layout_info = get_layout_info(&li.element, component, &li.constraints, orientation);
            quote!(BoxLayoutCellData { constraint: #layout_info })
        });
        if let Some((ri, _)) = &mut repeated_indices {
            **ri = quote!([]);
        }
        (quote!([ #(#cells),* ]), alignment)
    } else {
        let mut fixed_count = 0usize;
        let mut repeated_count = quote!();
        let mut push_code = quote!();
        let inner_component_id = inner_component_id(component);
        if let Some((ri, init)) = &mut repeated_indices {
            **ri = quote!(repeater_indices);
            **init = quote!( let mut #ri = [ 0u32; #repeater_count * 2]; );
        }
        let mut repeater_idx = 0usize;
        for item in &layout.elems {
            if item.element.borrow().repeated.is_some() {
                let repeater_id = format_ident!("repeater_{}", ident(&item.element.borrow().id));
                let rep_inner_component_id =
                    self::inner_component_id(item.element.borrow().base_type.as_component());
                repeated_count = quote!(#repeated_count + _self.#repeater_id.len());
                let ri = repeated_indices.as_ref().map(|(ri, _)| {
                    quote!(
                        #ri[#repeater_idx * 2] = items_vec.len() as u32;
                        #ri[#repeater_idx * 2 + 1] = internal_vec.len() as u32;
                    )
                });
                repeater_idx += 1;
                push_code = quote! {
                    #push_code
                    #inner_component_id::FIELD_OFFSETS.#repeater_id.apply_pin(_self).ensure_updated(
                        || { #rep_inner_component_id::new(_self.self_weak.get().unwrap().clone(), &_self.window.window_handle()).into() }
                    );
                    let internal_vec = _self.#repeater_id.components_vec();
                    #ri
                    for sub_comp in &internal_vec {
                        items_vec.push(sub_comp.as_pin_ref().box_layout_data(#orientation))
                    }
                }
            } else {
                let layout_info =
                    get_layout_info(&item.element, component, &item.constraints, orientation);
                fixed_count += 1;
                push_code = quote! {
                    #push_code
                    items_vec.push(BoxLayoutCellData { constraint: #layout_info });
                }
            }
        }
        (
            quote! { {
                let mut items_vec = Vec::with_capacity(#fixed_count #repeated_count);
                #push_code
                items_vec
            } },
            alignment,
        )
    }
}

fn generate_layout_padding_and_spacing(
    layout_geometry: &LayoutGeometry,
    orientation: Orientation,
    component: &Rc<Component>,
) -> (TokenStream, TokenStream) {
    let padding_prop = |expr| {
        if let Some(expr) = expr {
            let p = access_named_reference(expr, component, quote!(_self));
            quote!(#p.get())
        } else {
            quote!(0.)
        }
    };
    let spacing = padding_prop(layout_geometry.spacing.as_ref());
    let (begin, end) = layout_geometry.padding.begin_end(orientation);
    let (begin, end) = (padding_prop(begin), padding_prop(end));
    let padding = quote!(&sixtyfps::re_exports::Padding { begin: #begin, end: #end });

    (padding, spacing)
}

fn layout_geometry_size(
    rect: &LayoutRect,
    orientation: Orientation,
    component: &Rc<Component>,
) -> TokenStream {
    let nr = rect.size_reference(orientation);
    nr.map_or_else(
        || quote!(::core::default::Default::default()),
        |nr| {
            let p = access_named_reference(nr, component, quote!(_self));
            quote!(#p.get())
        },
    )
}

fn compute_layout(component: &Rc<Component>) -> TokenStream {
    let elem = &component.root_element;
    let constraints = component.root_constraints.borrow();
    let layout_info_h = get_layout_info(elem, component, &constraints, Orientation::Horizontal);
    let layout_info_v = get_layout_info(elem, component, &constraints, Orientation::Vertical);
    quote! {
        fn layout_info(self: ::core::pin::Pin<&Self>, orientation: sixtyfps::re_exports::Orientation) -> sixtyfps::re_exports::LayoutInfo {
            #![allow(unused)]
            use sixtyfps::re_exports::*;
            let _self = self;
            match orientation {
                sixtyfps::re_exports::Orientation::Horizontal => #layout_info_h,
                sixtyfps::re_exports::Orientation::Vertical => #layout_info_v,
            }
        }
    }
}

fn get_layout_info(
    elem: &ElementRc,
    component: &Rc<Component>,
    constraints: &crate::layout::LayoutConstraints,
    orientation: Orientation,
) -> TokenStream {
    let layout_info = if let Some(layout_info_prop) = &elem.borrow().layout_info_prop(orientation) {
        let li = access_named_reference(layout_info_prop, component, quote!(_self));
        quote! {#li.get()}
    } else {
        let elem_id = ident(&elem.borrow().id);
        let inner_component_id = inner_component_id(component);
        quote!(#inner_component_id::FIELD_OFFSETS.#elem_id.apply_pin(_self).layouting_info(#orientation, &_self.window.window_handle()))
    };

    if constraints.has_explicit_restrictions() {
        let (name, expr): (Vec<_>, Vec<_>) = constraints
            .for_each_restrictions(orientation)
            .map(|(e, s)| (ident(s), access_named_reference(e, component, quote!(_self))))
            .unzip();
        quote!({
            let mut layout_info = #layout_info;
                #(layout_info.#name = #expr.get();)*
            layout_info
        })
    } else {
        layout_info
    }
}

fn compile_path_events(events: &[crate::expression_tree::PathEvent]) -> TokenStream {
    use lyon_path::Event;

    let mut coordinates = Vec::new();

    let converted_events: Vec<proc_macro2::TokenStream> = events
        .iter()
        .map(|event| match event {
            Event::Begin { at } => {
                coordinates.push(at);
                quote!(sixtyfps::re_exports::PathEvent::Begin)
            }
            Event::Line { from, to } => {
                coordinates.push(from);
                coordinates.push(to);
                quote!(sixtyfps::re_exports::PathEvent::Line)
            }
            Event::Quadratic { from, ctrl, to } => {
                coordinates.push(from);
                coordinates.push(ctrl);
                coordinates.push(to);
                quote!(sixtyfps::re_exports::PathEvent::Quadratic)
            }
            Event::Cubic { from, ctrl1, ctrl2, to } => {
                coordinates.push(from);
                coordinates.push(ctrl1);
                coordinates.push(ctrl2);
                coordinates.push(to);
                quote!(sixtyfps::re_exports::PathEvent::Cubic)
            }
            Event::End { close, .. } => {
                if *close {
                    quote!(sixtyfps::re_exports::PathEvent::EndClosed)
                } else {
                    quote!(sixtyfps::re_exports::PathEvent::EndOpen)
                }
            }
        })
        .collect();

    let coordinates: Vec<TokenStream> = coordinates
        .into_iter()
        .map(|pt| {
            let x = pt.x;
            let y = pt.y;
            quote!(sixtyfps::re_exports::Point::new(#x, #y))
        })
        .collect();

    quote!(sixtyfps::re_exports::SharedVector::<sixtyfps::re_exports::PathEvent>::from_slice(&[#(#converted_events),*]),
           sixtyfps::re_exports::SharedVector::<sixtyfps::re_exports::Point>::from_slice(&[#(#coordinates),*]))
}

fn compile_path(path: &Path, component: &Rc<Component>) -> TokenStream {
    match path {
        Path::Elements(elements) => {
            let converted_elements: Vec<TokenStream> = elements
                .iter()
                .map(|element| {
                    let mut bindings = element
                        .bindings
                        .iter()
                        .map(|(property, expr)| {
                            let prop_ident = ident(property);
                            let binding_expr = compile_expression(expr, component);

                            quote!(#prop_ident: #binding_expr as _).to_string()
                        })
                        .collect::<Vec<String>>();

                    if bindings.len() < element.element_type.properties.len() {
                        bindings.push("..Default::default()".into())
                    }

                    let bindings = bindings.join(",");

                    let ctor_format_string = element
                        .element_type
                        .native_class.rust_type_constructor
                        .as_ref()
                        .expect(
                        "Unexpected error in type registry: path element is lacking rust type name",
                    );

                    ctor_format_string
                        .replace("{}", &bindings)
                        .parse()
                        .expect("Error parsing rust path element constructor")
                })
                .collect();

            quote!(sixtyfps::re_exports::PathData::Elements(
                sixtyfps::re_exports::SharedVector::<sixtyfps::re_exports::PathElement>::from_slice(&[#(#converted_elements),*])
            ))
        }
        Path::Events(events) => {
            let events = compile_path_events(events);
            quote!(sixtyfps::re_exports::PathData::Events(#events))
        }
    }
}

// In Rust debug builds, accessing the member of the FIELD_OFFSETS ends up copying the
// entire FIELD_OFFSETS into a new stack allocation, which with large property
// binding initialization functions isn't re-used and with large generated inner
// components ends up large amounts of stack space (see issue #133)
fn access_component_field_offset(component_id: &Ident, field: &Ident) -> TokenStream {
    quote!({ *&#component_id::FIELD_OFFSETS.#field })
}