summaryrefslogtreecommitdiff
path: root/docs/fpc_lang_ref.ipf
blob: abe951e95e8c72db352c0cd17051b7f664951688 (plain)
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
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
:userdoc.
:title.Free Pascal&colon. Language Reference guide
:docprof toc=123456.
.* ==============================================================
.* Custom symbols
.nameit symbol='fpc' text='Free Pascal'
.nameit symbol='delphi' text='Delphi'
.nameit symbol='tp' text='Turbo Pascal'
.nameit symbol='fpcversion' text='2&per.4'
.nameit symbol='date' text='December 2009'
.nameit symbol='progref' text='Programmer&apos.s Guide [ http://www.freepascal.org/docs.var ]'
.nameit symbol='ra' text='►'
.nameit symbol='la' text='◄'
.nameit symbol='dar' text='▼'
.nameit symbol='uar' text='^'
.nameit symbol='linux' text='Linux'
.* ==============================================================
:h1.Free Pascal&colon. Language Reference guide
:p.
:hp2.Language Reference guide for Free Pascal version &fpcversion. :ehp2.

:p.
Document version &fpcversion. (r617)
.br
&date.

:p.
:p.
:p.
:p.
Written by :hp1.Michael van Canneyt:ehp1.
.br
LaTeX to IPF conversion by :hp1.Graeme Geldenhuys:ehp1.
:p.
:p.
:p.
:note.:color fc=red.Please switch DocView to using the UTF-8 text encoding for this document.:color fc=default.

.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % About this guide
.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:h2.About this guide
:p.
This document serves as the reference for the Pascal langauge as implemented
by the &fpc. compiler. It describes all Pascal constructs supported by 
&fpc., and lists all supported data types. It does not, however, give a 
detailed explanation of the Pascal language: it is not a tutorial. 
The aim is to list which Pascal constructs are supported, and to show 
where the &fpc. implementation differs from the &tp. or &delphi.
implementations.

:p.
The &tp. and &delphi. Pascal compilers introduced various features in the 
Pascal language. The Free Pascal compiler emulates these compilers in the
appropriate mode of the compiler: certain features are available only
if the compiler is switched to the appropriate mode. When required for 
a certain feature, the use of the :hp1.-M:ehp1. command-line switch or 
:hp1.{$MODE}:ehp1. directive will be indicated in the text. More information
about the various modes can be found in the user's manual and the
programmer's manual.

:p.
Earlier versions of this document also contained the reference documentation
of the :hp1.system:ehp1. unit and :hp1.objpas:ehp1. unit. This has been moved to the 
RTL reference guide.

.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Notations
:h3.Notations
:p.
Throughout this document, we will refer to functions, types and variables 
with :font facename=Courier size=16x16.typewriter:font facename='System Proportional'. font.
Files are referred to with a sans font: :font facename=Sans size=16x16.filename:font facename='System Proportional'..


.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Syntax diagrams
:h3.Syntax diagrams
:p.
All elements of the Pascal language are explained in syntax diagrams. Syntax diagrams are like flow
charts. Reading a syntax diagram means getting from the left side to the right side, following the
arrows. When the right side of a syntax diagram is reached, and it ends with a single arrow, this
means the syntax diagram is continued on the next line. If the line ends on 2 arrows pointing to each
other, then the diagram is ended.
:p.
Syntactical elements are written like this:
:cgraphic.
&ra.&ra.─── syntactical elemements are like this ───────────────────────────────────&ra.&la.
:ecgraphic.

:p.
Keywords which must be typed exactly as in the diagram:
:cgraphic.
&ra.&ra.─── :hp2.keywords are like this:ehp2. ─────────────────────────────────────────────────&ra.&la.
:ecgraphic.

:p.
When something can be repeated, there is an arrow around it:
:cgraphic.
&ra.&ra.─────┬─ this can be repeated ─┬─────────────────────────────────────────────&ra.&la.
       ^────────────────────────┘
:ecgraphic.

:p.
When there are different possibilities, they are listed in rows:
:cgraphic.
&ra.&ra.─────┬─ First possibility ──┬───────────────────────────────────────────────&ra.&la.
       └─ Second possibility ─┘
:ecgraphic.

:p.
Note, that one of the possibilities can be empty:
:cgraphic.
&ra.&ra.─────┬──────────────────────┬───────────────────────────────────────────────&ra.&la.
       ├─ First possibility  ─┤
       └─ Second possibility ─┘
:ecgraphic.

:p.
This means that both the first or second possibility are optional. Of course, all these elements can be
combined and nested.


.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % About the Pascal Language
.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:h2.About the Pascal Language
:p.
The language Pascal was originally designed by Niklaus Wirth around 1970. It 
has evolved significantly since that day, with a lot of contributions by the
various compiler constructors (Notably: Borland). The basic elements have been
kept throughout the years:
:ul.
:li. Easy syntax, rather verbose, yet easy to read. Ideal for teaching.
:li. Strongly typed.
:li. Procedural.
:li. Case insensitive.
:li. Allows nested procedures.
:li. Easy input/output routines built-in.
:eul.

:p.
The &tp. and &delphi. Pascal compilers introduced various features in
the Pascal language, most notably easier string handling and object 
orientedness. The &fpc. compiler initially emulated most of &tp.
and later on &delphi.. It emulates these compilers in the appropriate mode
of the compiler: certain features are available only if the compiler is 
switched to the appropriate mode. When required for a certain feature, the use
of the -M command-line switch or {$MODE } directive will be indicated in the
text. More information about the various modes can be found in the User's 
Manual and the Programmer's Manual.


.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Pascal Tokens
.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:h2.Pascal Tokens
:p.
Tokens are the basic lexical building blocks of source code: they 
are the 'words' of the language: characters are combined into tokens according
to the rules of the programming language. There are five classes of tokens:

:parml tsize=20 break=none.
:pt.:hp2.reserved words:ehp2.
:pd. These are words which have a fixed meaning in the language. They cannot 
be changed or redefined.

:pt.:hp2.identifiers:ehp2.
:pd.These are names of symbols that the programmer defines. They can be changed
and re-used. They are subject to the scope rules of the language.

:pt.:hp2.operators:ehp2.
:pd.These are usually symbols for mathematical or other operations: +, -, * and
so on.

:pt.:hp2.separators:ehp2.
:pd.This is usually white-space.

:pt.:hp2.constants:ehp2.
:pd.Numerical or character constants are used to denote actual values in the
source code, such as 1 (integer constant) or 2.3 (float constant) or 
'String constant' (a string: a piece of text).
:eparml.

:p.
In this chapter we describe all the Pascal reserved words&comma. as well as the
various ways to denote strings&comma. numbers&comma. identifiers etc&per.


.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Symbols
:h3.Symbols
:p.
&fpc. allows all characters, digits and some special character
symbols in a Pascal source file.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Recognised symbols:ehp2.

&ra.&ra.─── letter ──┬─ A..Z ─┬─────────────────────────────────────────────────────&ra.&la.
               └─ a..z ─┘

&ra.&ra.─── digit ─ 0..9 ───────────────────────────────────────────────────────────&ra.&la.

&ra.&ra.─── hex digit ──┬─ 0..9 ─┬──────────────────────────────────────────────────&ra.&la.
                  ├─ A..Z ─┤
                  └─ a..z ─┘
└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
The following characters have a special meaning:
:xmp.
+ - * / = < > [ ] . , ( ) : ^ @ { } $ #
:exmp.

:p.
and the following character pairs too:
:xmp.
<= >= := += -= *= /= (* *) (. .) //
:exmp.

:p.
When used in a range specifier, the character pair (. is equivalent to the 
left square bracket [. Likewise, the character pair .) is equivalent to the 
right square bracket ]. When used for comment delimiters, the character pair
(* is equivalent to the left brace { and the character pair *) is equivalent
to the right brace }. These character pairs retain their normal meaning in 
string expressions.

.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Comments
:h3.Comments
:p.
Comments are pieces of the source code which are completely discarded by the
compiler. They exist only for the benefit of the programmer, so he can explain
certain pieces of code. For the compiler, it is as if the comments were not 
present.

:p.
The following piece of code demonstrates a comment:
:xmp.
(* My beautiful function returns an interesting result *)
Function Beautiful: Integer;
:exmp.

:p.
The use of (* and *) as comment delimiters dates from the very first days of
the Pascal language. It has been replaced mostly by the use of { and } as 
comment delimiters, as in the following example:
:xmp.
{ My beautiful function returns an interesting result }
Function Beautiful: Integer;
:exmp.

:p.
The comment can also span multiple lines:
:xmp.
{
   My beautiful function returns an interesting result,
   but only if the argument A is less than B.
}
Function Beautiful(A, B: Integer): Integer;
:exmp.

:p.
Single line comments can also be made with the // delimiter:
:xmp.
// My beautiful function returns an interesting result
Function Beautiful: Integer;
:exmp.

:p.
The comment extends from the // character till the end of the line. This kind
of comment was introduced by Borland in the &delphi. Pascal compiler.
:p.
&fpc. supports the use of nested comments. The following constructs are
valid comments:
:xmp.
(* This is an old style comment *)
{ This is a Turbo Pascal comment }
// This is a &delphi. comment. All is ignored till the end of the line.
:exmp.

:p.
The following are valid ways of nesting comments:
:xmp.
{ Comment 1 (* comment 2 *) }
(* Comment 1 { comment 2 } *)
{ comment 1 // Comment 2 }
(* comment 1 // Comment 2 *)
// comment 1 (* comment 2 *)
// comment 1 { comment 2 }
:exmp.

:p.
The last two comments must be on one line. The following two will give errors:
:xmp.
// Valid comment { No longer valid comment !!
   }
:exmp.

:p.
and
:xmp.
// Valid comment (* No longer valid comment !!
   *)
:exmp.

:p.
The compiler will react with a 'invalid character' error when it encounters
such constructs, regardless of the :hp1.-Mturbo:ehp1. switch.

:nt.
In :hp1.TP:ehp1. and :hp1.Delphi:ehp1. mode, nested comments are not allowed, 
for maximum compatibility with existing code for those compilers.
:ent.



.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Reserved words
:h3.Reserved words
:p.
Reserved words are part of the Pascal language, and as such, cannot be
redefined by the programmer. Throughout the syntax diagrams they will be 
denoted using a :hp2.bold:ehp2. typeface. Pascal is not case sensitive so the compiler
will accept any combination of upper or lower case letters for reserved words.

:p.
We make a distinction between &tp. and &delphi. reserved words. In 
:hp2.TP:ehp2. mode, only the &tp. reserved words are recognised, but
the &delphi. ones can be redefined. By default, &fpc. recognises the &delphi.
reserved words.

:h4.&tp. reserved words
:p.
The following keywords exist in &tp. mode:
.*              *                *                *
:xmp.
absolute        file             object           shr
and             for              of               string
array           function         on               then
asm             goto             operator         to
begin           if               or               type
case            implementation   packed           unit
const           in               procedure        until
constructor     inherited        program          uses
destructor      inline           record           var
div             interface        reintroduce      while
do              label            repeat           with
downto          mod              self             xor
else            nil              set
end             not              shl
:exmp.

:h4.&fpc. reserved words
:p.
On top of the &tp. reserved words, &fpc. also considers the 
following as reserved words:
.*              *                *                *
:xmp.
dispose         false            true
exit            new
:exmp.


:h4.Object Pascal reserved words
:p.
The reserved words of Object Pascal (used in :hp2.Delphi:ehp2. or :hp2.ObjPas:ehp2. mode) are the
same as the &tp. ones, with the following additional keywords:
.*              *                *                *
:xmp.
as              finalization     library          raise
class           finally          on               resourcestring
dispinterface   initialization   out              threadvar
except          inline           packed           try
exports         is               property

:exmp.


:h4.Modifiers
:p.
The following is a list of all modifiers. They are not exactly reserved words
in the sense that they can be used as identifiers, but in specific places, they
have a special meaning for the compiler, i.e., the compiler considers them as
part of the Pascal language.
.*              *                *                *
:xmp.
absolute        external         nostackframe     read
abstract        far              oldfpccall       register
alias           far16            override         reintroduce
assembler       forward          pascal           safecall
cdecl           index            private          softfloat
cppdecl         local            protected        stdcall
default         name             public           virtual
export          near             published        write
:exmp.

:p.
:nt.
Predefined types such as :hp1.Byte:ehp1., :hp1.Boolean:ehp1. and constants such
as :hp1.maxint:ehp1. are not reserved words. They are identifiers, declared in the system unit. This means
that these types can be redefined in other units. The programmer is however
:hp2.not:ehp2. encouraged to do this, as it will cause a lot of confusion.
:ent.

:h3.Identifiers
:p.
Identifiers denote programmer defined names for specific constants, types,
variables, procedures and functions, units, and programs. All programmer
defined names in the source code – excluding reserved words – are designated
as identifiers.

:p.
Identifiers consist of between 1 and 127 significant characters (letters,
digits and the underscore character), of which the first must be an alphanumeric
character, or an underscore (_). The following diagram gives the basic syntax
for identifiers.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Identifiers:ehp2.

&ra.&ra.─── identifier ──┬─ letter ─┬─┬──────────────┬──────────────────────────────&ra.&la.
                   └─── _ ────┘ ^─┬─ letter ─┬─┘
                                  ├─ digit ──┤
                                  └─── _ ────┘
└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
Like Pascal reserved words, identifiers are case insensitive, that is, both
:xmp.
myprocedure;
:exmp.

:p.
and
:xmp.
MyProcedure;
:exmp.

:p.
refer to the same procedure.

:nt.
As of version 2.5.1 it is possible to specify a reserved word as an identifier
by prepending it with an ampersand (&amp.). This means that the following is
possible:

:xmp.
program testdo;

procedure &amp.do;
begin
end;

begin
  &amp.do;
end.
:exmp.

:p.
The reserved word :hp1.do:ehp1. is used as an identifier for the declaration
as well as the invocation of the procedure 'do'.
:ent.


.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Hint directives
:h3.Hint directives
:p.
Most identifiers (constants, variables, functions or methods, properties) can
have a hint directive appended to their definition:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Hint directives:ehp2.

&ra.&ra.─── hint directive ──┬─────────────────┬────────────────────────────────────&ra.&la.
                       ├── Deprecated ───┤
                       ├─ Experimental ──┤
                       ├─── Platform ────┤
                       └─ Unimplemented ─┘
└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
Whenever an identifier marked with a hint directive is later encountered by the
compiler, then a warning will be displayed, corresponding to the specified hint.

:parml tsize=20 break=none.

:pt.:hp2.deprecated:ehp2.
:pd.The use of this identifier is deprecated, use an alternative instead.

:pt.:hp2.experimental:ehp2.
:pd.The use of this identifier is experimental: this can be used to flag new
features that should be used with caution.

:pt.:hp2.platform:ehp2.
:pd.This is a platform-dependent identifier: it may not be defined on all
platforms.

:pt.:hp2.unimplemented:ehp2.
:pd.This should be used on functions and procedures only. It should be used to
signal that a particular feature has not yet been implemented.

:eparml.

:p.
The following are examples:

:xmp.
const
  AConst = 12 deprecated;

var
  p: integer platform;

function Something: Integer; experimental;
begin
  Something := P + AConst;
end;

begin
  Something;
end.
:exmp.

:p.
This would result in the following output:

:xmp.
testhd.pp(11,15) Warning: Symbol "p" is not portable
testhd.pp(11,22) Warning: Symbol "AConst" is deprecated
testhd.pp(15,3) Warning: Symbol "Something" is experimental
:exmp.

:p.
Hint directives can follow all kinds of identifiers: units, constants, types,
variables, functions, procedures and methods.



.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Numbers
:h3.Numbers
:p.
Numbers are by default denoted in decimal notation.
Real (or decimal) numbers are written using engineering or scientific
notation (e.g. 0.314E1).

:p.
For integer type constants, &fpc. supports 4 formats:

:ol.
:li. Normal, decimal format (base 10). This is the standard format.

:li. Hexadecimal format (base 16), in the same way as &tp. does.
To specify a constant value in hexadecimal format, prepend it with a dollar
sign ($). Thus, the hexadecimal $FF equals 255 decimal.
Note that case is insignificant when using hexadecimal constants.

:li. As of version 1.0.7, Octal format (base 8) is also supported.
To specify a constant in octal format, prepend it with a ampersand (&amp.).
For instance 15 is specified in octal notation as &amp.17. 

:li. Binary notation (base 2). A binary number can be specified
by preceding it with a percent sign (%). Thus, 255 can be
specified in binary notation as %11111111.
:eol.

:p.
The following diagrams show the syntax for numbers.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Numbers:ehp2.

&ra.&ra.─── hex digit sequence ──┬── hex digit ──┬──────────────────────────────────&ra.&la.
                           &uar.───────────────┘

&ra.&ra.─── octal digit sequence ──┬── octal digit ──┬──────────────────────────────&ra.&la.
                             &uar.─────────────────┘

&ra.&ra.─── binary digit sequence ──┬┬─ 1 ─┬┬───────────────────────────────────────&ra.&la.
                              │└─ 0 ─┘│
                              &uar.───────┘

&ra.&ra.─── digit sequence ──┬── digit ──┬──────────────────────────────────────────&ra.&la.
                       ^───────────┘

&ra.&ra.─── unsigned integer ──┬────── digit sequence ───────┬──────────────────────&ra.&la.
                         ├─ $ ─ hex digit sequence ────┤
                         ├─ & ─ octal digit sequence ──┤
                         └─ % ─ binary digit sequence ─┘

&ra.&ra.─── hex digit sequence ──┬── hex digit ──┬──────────────────────────────────&ra.&la.
                           ^───────────────┘

&ra.&ra.─── sign ──┬── + ──┬────────────────────────────────────────────────────────&ra.&la.
             └── - ──┘

&ra.&ra.─── unsigned real ─ digit sequence ─┬────────────────────┬┬──────────────┬──&ra.&la.
                                      └ . ─ digit sequence ┘└ scale factor ┘

&ra.&ra.─── scale factor ─┬─ E ─┬┬──────┬─ digit sequence ──────────────────────────&ra.&la.
                    └─ e ─┘└ sign ┘

&ra.&ra.─── unsigned number ──┬─── unsigned real ──┬────────────────────────────────&ra.&la.
                        └─ unsigned integer ─┘

&ra.&ra.─── signed number ──┬────────┬─ unsigned number ────────────────────────────&ra.&la.
                      └─ sign ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:nt.
Octal and Binary notation are not supported in TP or Delphi compatibility mode.
:ent.



.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Labels
:h3.Labels
:p.
A label is a name for a location in the source code to which can be 
jumped to from another location with a :hp2.goto:ehp2. statement. A Label is a
standard identifier with the exception that it can start with a digit.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Label:ehp2.

&ra.&ra.─── label ──┬─ digit sequence ─┬────────────────────────────────────────────&ra.&la.
              └── identifier ────┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:nt.
The -Sg or -Mtp switches must be specified before labels can be used. By
default, &fpc. doesn't support :hp2.label:ehp2. and :hp2.goto:ehp2. statements.
The :hp2.{$GOTO ON}:ehp2. directive can also be used to allow use of labels and
the goto statement.
:ent.


.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Character strings
:h3.Character strings
:p.
A character string (or string for short) is a sequence of zero or more
characters (byte sized), enclosed in single quotes, and on a single 
line of the program source code: no literal carriage return or linefeed 
characters can appear in the string.

:p.
A character set with nothing between the quotes ('') is an empty string.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Character strings:ehp2.

&ra.&ra.─── character string ──┬┬─ quoted string ──┬┬───────────────────────────────&ra.&la.
                         │└─ control string ─┘│
                         ^────────────────────┘

&ra.&ra.─── quoted string ─ ' ──┬─ string character ─┬─ ' ──────────────────────────&ra.&la.
                          ^────────────────────┘

&ra.&ra.─── string character ──┬─ Any character except ' or CR ─┬───────────────────&ra.&la.
                         └────────────── " ───────────────┘ 

&ra.&ra.─── control string ──┬─ # ─ unsigned integer ─┬─────────────────────────────&ra.&la.
                       ^────────────────────────┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
The string consists of standard, 8-bit ASCII characters or Unicode (normally
UTF-8 encoded) characters. The :hp1.control string:ehp1. can be used to specify
characters which cannot be typed on a keyboard, such as :hp1.#27:ehp1. for
the escape character. 

:p.
The single quote character can be embedded in the string by typing it twice. 
The C construct of escaping characters in the string (using a backslash) 
is not supported in Pascal.

:p.
The following are valid string constants:
:xmp.
  'This is a pascal string'
  ''
  'a'
  'A tabulator character: '#9' is easy to embed'
:exmp.

:p.
The following is an invalid string:

:xmp.
  'the string starts here
   and continues here'
:exmp.

:p.
The above string must be typed as:

:xmp.
  'the string starts here'#13#10'   and continues here'
:exmp.

or
:xmp.
  'the string starts here'#10'   and continues here'
:exmp.

:p.
on unices (including Mac OS X), and as

:xmp.
  'the string starts here'#13'   and continues here'
:exmp.

on a classic Mac-like operating system.
 
It is possible to use other character sets in strings: in that case the 
codepage of the source file must be specified with the :hp1.{$CODEPAGE XXX}:ehp1.
directive or with the :hp2.-Fc:ehp2. command line option for the compiler. In that
case the characters in a string will be interpreted as characters from the
specified codepage.




.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Constants
.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:h2.Constants
:p.
Just as in &tp., &fpc. supports both ordinary and typed constants.

:ul.
:li.:link reftype=hd refid=constants_ordinary.Ordinary constants:elink.
:li.:link reftype=hd refid=constants_typed.Typed constants:elink.
:li.:link reftype=hd refid=constants_resource.Resource strings:elink.
:eul.

.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Ordinary constants
:h3 name=constants_ordinary.Ordinary constants
:p.
Ordinary constants declarations are constructed using an identifier name 
followed by an "=" token, and followed by an optional expression consisting 
of legal combinations of numbers, characters, boolean values or enumerated 
values as appropriate. The following syntax diagram shows how to construct 
a legal declaration of an ordinary constant.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Constant declaration:ehp2.

&ra.&ra.─── constant declaration ─┬─ identifier ─ = ─ expression ─ hint directives ─ ; ─┬───&ra.&la.
                            ^─────────────────────────────────────────────────────┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
The compiler must be able to evaluate the expression in a constant
declaration at compile time.  This means that most of the functions
in the Run-Time library cannot be used in a constant
declaration.

:p.
Operators such as +, -, *, /, not, and, or, div, mod, ord, chr,
sizeof, pi, int, trunc, round, frac, odd can be used, however. 
For more information on expressions, see the section :link reftype=hd refid=expressions.Expressions:elink.&per.


:p.
Only constants of the following types can be declared: :hp1.Ordinal types:ehp1.&comma.
:hp1.Real types:ehp1.&comma. :hp1.Char:ehp1.&comma. and :hp1.String:ehp1.&per.
The following are all valid constant declarations&colon.

:xmp.
Const
  e = 2.7182818;  { Real type constant. }
  a = 2;          { Ordinal (Integer) type constant. }
  c = '4';        { Character type constant. }
  s = 'This is a constant string'; {String type constant.}
  s = chr(32)
  ls = SizeOf(Longint);
:exmp.

:p.
Assigning a value to an ordinary constant is not permitted.
Thus, given the previous declaration, the following will result
in a compiler error:
:xmp.
  s := 'some other string';
:exmp.

:p.
For string constants, the type of the string is dependent on some compiler
switches. If a specific type is desired, a typed constant should be used, 
as explained in the following section.

:p.
Prior to version 1.9, &fpc. did not correctly support 64-bit constants. As
of version 1.9, 64-bit constants can be specified.



.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Typed constants
:h3 name=constants_typed.Typed constants
:p.
Sometimes it is necessary to specify the type of a constant, for instance
for constants of complex structures (defined later in the manual).
Their definition is quite simple.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Constant declaration:ehp2.

       typed
&ra.&ra.─── constant ───┬─ identifier ─ : ─ type ─ = ─ type constant ─ hint directives ─ ; ─┬───&ra.&la.
     declaration  ^───────────────────────────────────────────────────────────────────┘

&ra.&ra.─── typed constant ──┬────── constant ───────┬──────────────────────────────&ra.&la.
                       ├─   address constant  ─┤
                       ├─    array constant   ─┤
                       ├─   record constant   ─┤
                       └─ procedural constant ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
Contrary to ordinary constants, a value can be assigned to them at 
run-time. This is an old concept from &tp., which has been 
replaced with support for initialized variables: For a detailed 
description, see :link reftype=hd refid='variables_initializedvars'.Initialized variables:elink..

Support for assigning values to typed constants is controlled by the 
:hp2.{$J}:ehp2. directive: it can be switched off, but is on by default 
(for &tp. compatibility). Initialized variables are always allowed.

:nt.
It should be stressed that typed constants are automatically initialized at program start.
This is also true for :hp1.local:ehp1. typed constants and initialized variables. 
Local typed constants are also initialized at program start. If their value was 
changed during previous invocations of the function, they will retain their 
changed value, i.e. they are not initialized each time the function is invoked.
:ent.

.* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.* % Ordinary constants
:h3 name=constants_resource.Resource strings
:p.
A special kind of constant declaration block is the :hp1.resourcestring:ehp1.
block. Resourcestring declarations are much like constant string
declarations: resource strings act as constant strings, but they 
can be localized by means of a set of special routines in the 
:hp1.objpas:ehp1. unit. A resource string declaration block
is only allowed in the :hp1.Delphi:ehp1. or :hp1.ObjFPC:ehp1. modes.

:p.
The following is an example of a resourcestring definition:
:xmp.
resourcestring
  FileMenu = '&amp.File...';
  EditMenu = '&amp.Edit...';
:exmp.

:p.
All string constants defined in the resourcestring section are stored
in special tables. The strings in these tables can be manipulated
at runtime with some special mechanisms in the :hp1.objpas:ehp1. unit.

:p.
Semantically, the strings act like ordinary constants; It is not allowed
to assign values to them (except through the special mechanisms in the 
objpas unit). However, they can be used in assignments or expressions as 
ordinary string constants. The main use of the resourcestring section is 
to provide an easy means of internationalization.

:p.
More on the subject of resourcestrings can be found in the 
:link reftype=hd database='prog.inf' refid=0.&progref.:elink., and
in the :hp1.objpas:ehp1. unit reference.

:nt.
Note that a resource string which is given as an expression will not change if
the parts of the expression are changed:
:xmp.
resourcestring
  Part1 = 'First part of a long string.';
  Part2 = 'Second part of a long string.';
  Sentence = Part1 + ' ' + Part2;
:exmp.

:p.
If the localization routines translate :hp1.Part1:ehp1. and :hp1.Part2:ehp1., the
:hp1.Sentence:ehp1. constant will not be translated automatically: it has a
separate entry in the resource string tables, and must therefor be
translated separately. The above construct simply says that the 
initial value of :hp1.Sentence:ehp1. equals :hp1.Part1+' '+Part2:ehp1..
:ent.

:nt.
Likewise, when using resource strings in a constant array, only the initial
values of the resource strings will be used in the array: when the
individual constants are translated, the elements in the array will retain
their original value.
:xmp.
resourcestring
  Yes = 'Yes.';
  No = 'No.';

var
  YesNo: Array[Boolean] of string = (No, Yes);
  B: Boolean;

begin
  Writeln(YesNo[B]);
end.
:exmp.

:p.
This will print 'Yes.' or 'No.' depending on the value of B, even if the 
constants Yes and No have been localized by some localization mechanism.
:ent.


.* ==============================================================
:h2 name=types.Types
:p.
All variables have a type. &fpc. supports the same basic types as &tp., with
some extra types from &delphi..
The programmer can declare his own types, which is in essence defining an identifier
that can be used to denote this custom type when declaring variables further
in the source code.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Type declaration:ehp2.

&ra.&ra.─── type declaration ─── identifier ─ = ─ type ─ ; ─────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
There are 7 major type classes:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Types:ehp2.

&ra.&ra.─── type  ──┬─── simple type ────┬──────────────────────────────────────────&ra.&la.
              ├─   string type    ─┤
              ├─ structured type  ─┤
              ├─  pointer type    ─┤
              ├─ procedural type  ─┤
              ├─  generic type    ─┤
              ├─ specialized type ─┤
              └─ type identifier  ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
The last case, :hp1.type identifier:ehp1., is just a means to give another
name to a type. This presents a way to make types platform independent, by
only using these types, and then defining these types for each platform
individually. Any programmer who then uses these custom types doesn't have to worry
about the underlying type size: it is opaque to him. It also allows to use shortcut names 
for fully qualified type names. e.g. define :hp1.system.longint:ehp1. as
:hp1.Olongint:ehp1. and then redefine :hp1.longint:ehp1..


.* --------------------------------------------------------------
:h3 name=base_types.Base types
:p.
The base or simple types of &fpc. are the &delphi. types.
We will discuss each type separately.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Simple types:ehp2.

&ra.&ra.─── simple type  ──┬─ ordinal type ─┬───────────────────────────────────────&ra.&la.
                     └─  real type   ─┘

&ra.&ra.─── real type ─── real type identifier ─────────────────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.


.* ...................................................................
:h4.Ordinal types
:p.
With the exception of :hp1.int64:ehp1., :hp1.qword:ehp1. and Real types, 
all base types are ordinal types. Ordinal types have the following 
characteristics:
:ol.
:li. Ordinal types are countable and ordered, i.e. it is, in principle,
possible to start counting them one by one, in a specified order.
This property allows the operation of functions as :hp2.Inc:ehp2., :hp2.Ord:ehp2.,
:hp2.Dec:ehp2. on ordinal types to be defined.
:li. Ordinal values have a smallest possible value. Trying to apply the
:hp2.Pred:ehp2. function on the smallest possible value will generate a range
check error if range checking is enabled.
:li. Ordinal values have a largest possible value. Trying to apply the
:hp2.Succ:ehp2. function on the largest possible value will generate a range
check error if range checking is enabled.
:eol.


:h5.Integers
:p.
A list of pre-defined integer types are presented below.

:lm margin=10.
:cgraphic.
  :hp2.Name:ehp2.
────────────
  Integer
  Shortint
  SmallInt
  Longint
  Longword
  Int64
  Byte
  Word
  Cardinal
  QWord
  Boolean
  ByteBool
  WordBool
  LongBool
  Char
────────────
:ecgraphic.
:lm margin=1.

:p.
The integer types, and their ranges and sizes, that are predefined in
&fpc. are listed in in the table below. Please note that
the :hp1.qword:ehp1. and :hp1.int64:ehp1. types are not true ordinals, so
some Pascal constructs will not work with these two integer types.

:cgraphic.
 :hp2.Type                         Range                   Size in bytes:ehp2.
────────────────────────────────────────────────────────────────────
 Byte                         0 .. 255                            1
 Shortint                  -128 .. 127                            1
 Smallint                -32768 .. 32767                          2
 Word                         0 .. 65535                          2
 Integer              either smallint or longint        size 2 or 4
 Cardinal                    longword                             4
 Longint            -2147483648 .. 2147483647                     4
 Longword                     0 .. 4294967295                     4
 Int64     -9223372036854775808 .. 9223372036854775807            8
 QWord                        0 .. 18446744073709551615           8
────────────────────────────────────────────────────────────────────
:ecgraphic.

:p.
The :hp1.integer:ehp1. type maps to the smallint type in the default
&fpc. mode. It maps to either a longint in either Delphi or ObjFPC
mode. The :hp1.cardinal:ehp1. type is currently always mapped to the 
longword type.

:nt.
All decimal constants which do no fit within the -2147483648..2147483647 range 
are silently and automatically parsed as 64-bit integer constants as of version 
1.9.0. Earlier versions would convert it to a real-typed constant.
:ent.

&fpc. does automatic type conversion in expressions where different kinds of
integer types are used.


:h5.Boolean types
:p.
&fpc. supports the :hp1.Boolean:ehp1. type, with its two pre-defined possible
values :hp1.True:ehp1. and :hp1.False:ehp1.. These are the only two values that can be
assigned to a :hp1.Boolean:ehp1. type. Of course, any expression that resolves
to a boolean value, can also be assigned to a boolean type.

:cgraphic.
 :hp2.Name             Size        Ord(True):ehp2.
─────────────────────────────────────────────────
 Boolean           1          1
 ByteBool          1          Any nonzero value
 WordBool          2          Any nonzero value
 LongBool          4          Any nonzero value
─────────────────────────────────────────────────
:ecgraphic.

:p.
&fpc. also supports the :hp1.ByteBool:ehp1., :hp1.WordBool:ehp1. and :hp1.LongBool:ehp1. types.
These are of type :hp1.Byte:ehp1., :hp1.Word:ehp1. or :hp1.Longint:ehp1., but are
assignment compatible with a :hp1.Boolean:ehp1.: the value :hp1.False:ehp1. is 
equivalent to 0 (zero) and any nonzero value is considered :hp1.True:ehp1. when
converting to a boolean value. A boolean value of :hp1.True:ehp1. is converted
to -1 in case it is assigned to a variable of type :hp1.LongBool:ehp1..

:p.
Assuming :hp1.B:ehp1. to be of type :hp1.Boolean:ehp1., the following are valid
assignments:

:xmp.
 B := True;
 B := False;
 B := 1 <> 2;  { Results in B := True }
:exmp.

:p.
Boolean expressions are also used in conditions.

:nt.
In &fpc., boolean expressions are by default always evaluated in such a
way that when the result is known, the rest of the expression will no longer
be evaluated: this is called short-cut boolean evaluation.

:p.
In the following example, the function :hp1.Func:ehp1. will never be called, 
which may have strange side-effects.

:xmp.
 ...
 B := False;
 A := B and Func;
:exmp.

:p.
Here :hp1.Func:ehp1. is a function which returns a :hp1.Boolean:ehp1. type.

This behaviour is controllable by the :hp2.{$B}:ehp2. compiler directive.
:ent.


.* ...................................................................
:h5.Enumeration types
:p.
Enumeration types are supported in &fpc.. On top of the &tp.
implementation, &fpc. also allows a C-style extension of the
enumeration type, where a value is assigned to a particular element of
the enumeration list.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Enumeration types:ehp2.

&ra.&ra.─── enumerated type ─ ( ─┬┬── identifier list ───┬┬─ ) ─────────────────────&ra.&la.
                           │└─ assigned enum list ─┘│
                           ^────────── , ───────────┘

&ra.&ra.─── identifier list ──┬─ identifier ─┬──────────────────────────────────────&ra.&la.
                        ^────── , ─────┘

&ra.&ra.─── assigned enum list ──┬─ identifier ─ := ─ expression ─┬─────────────────&ra.&la.
                           ^─────────────── , ──────────────┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
(see :link reftype=hd refid=expressions.Expressions:elink. for how to use expressions)
When using assigned enumerated types, the assigned elements must be in
ascending numerical order in the list, or the compiler will complain.
The expressions used in assigned enumerated elements must be known at
compile time. So the following is a correct enumerated type declaration:
:xmp.
Type
  Direction = (North, East, South, West);
:exmp.

:p.
A C-style enumeration type looks as follows:

:xmp.
Type
  EnumType = (one, two, three, forty := 40, fortyone);
:exmp.

:p.
As a result, the ordinal number of :hp1.forty:ehp1. is 40, and not 3,
as it would be when the ':= 40' wasn't present.
The ordinal value of :hp1.fortyone:ehp1. is then 41, and not 4, as it
would be when the assignment wasn't present. After an assignment in an
enumerated definition the compiler adds 1 to the assigned value to assign to
the next enumerated value.
:p.
When specifying such an enumeration type, it is important to keep in mind
that the enumerated elements should be kept in ascending order. The
following will produce a compiler error:

:xmp.
Type
  EnumType = (one, two, three, forty := 40, thirty := 30);
:exmp.

:p.
It is necessary to keep :hp1.forty:ehp1. and :hp1.thirty:ehp1. in the correct order.
When using enumeration types it is important to keep the following points
in mind:

:ol.
:li. The :hp2.Pred:ehp2. and :hp2.Succ:ehp2. functions cannot be used on
this kind of enumeration types. Trying to do this anyhow will result in a
compiler error.
:li. Enumeration types are stored using a default, independent of the
actual number of values: the compiler does not try to optimize for space.
This behaviour can be changed with the :hp2.{$PACKENUM n}:ehp2. compiler 
directive, which tells the compiler the minimal number of bytes to be 
used for enumeration types. For instance:

:xmp.
type
{$PACKENUM 4}
  LargeEnum = ( BigOne, BigTwo, BigThree );
{$PACKENUM 1}
  SmallEnum = ( one, two, three );
var
  S: SmallEnum;
  L: LargeEnum;
begin
  WriteLn('Small enum : ', SizeOf(S));
  WriteLn('Large enum : ', SizeOf(L));
end.
:exmp.
:eol.

More information can be found in the &progref., in the Compiler Directives section.


.* ...................................................................
:h5.Subrange types
:p.
A subrange type is a range of values from an ordinal type (the host type). To define a subrange type,
one must specify its limiting values: the highest and lowest value of the type.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Subrange types:ehp2.

&ra.&ra.─── subrange type ─ constant ─ .. ─ constant ───────────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
Some of the predefined integer types are defined as subrange types:

:xmp.
type
  Longint  = $80000000..$7fffffff;
  Integer  = -32768..32767;
  shortint = -128..127;
  byte     = 0..255;
  Word     = 0..65535;
:exmp.

:p.
Subrange types of enumeration types can also be defined:

:xmp.
type
  Days = (monday, tuesday, wednesday, thursday, friday, saturday, sunday);
  WorkDays = monday..friday;
  WeekEnd  = saturday..sunday;
:exmp.


.* ...................................................................
:h4.Real types
:p.
&fpc. uses the math coprocessor (or emulation) for all its floating-point calculations. The Real
native type is processor dependent, but it is either Single or Double. Only the IEEE floating point
types are supported, and these depend on the target processor and emulation options. The true &tp.
compatible types are listed in the table below. The :hp1.Comp:ehp1. type is, in effect, a 64-bit integer and
is not available on all target platforms. To get more information on the supported types for each
platform, refer to the &progref..


:cgraphic.
 :hp2.Type                Range           Significant digits     Size in bytes:ehp2.
──────────────────────────────────────────────────────────────────────────
 Real          platform dependent           ???                  4 or 8
 Single        1.5E-45 .. 3.4E38            7-8                       4
 Double       5.0E-324 .. 1.7E308          15-16                      8
 Extended    1.9E-4932 .. 1.1E4932         19-20                     10
 Comp          -2E64+1 .. 2E63-1           19-20                      8
 Currency    -922337203685477.5808 ..      19-20                      8
              922337203685477.5807
──────────────────────────────────────────────────────────────────────────
:ecgraphic.

:p.
The currency type is a fixed-point real data type which is internally used as an 64-bit integer type
(automatically scaled with a factor 10000), this minimalizes rounding errors.



.* --------------------------------------------------------------
:h3 name=character_types.Character types
:p.
We have eight character types in &fpc..

:ul.
:li.:link reftype=hd refid=character_types_char.Char:elink.
:li.:link reftype=hd refid=character_types_strings.Strings:elink.
:li.:link reftype=hd refid=character_types_shortstrings.Short strings:elink.
:li.:link reftype=hd refid=character_types_ansistrings.AnsiStrings:elink.
:li.:link reftype=hd refid=character_types_widestrings.WideStrings:elink.
:li.:link reftype=hd refid=character_types_unicodestrings.UnicodeStrings:elink.
:li.:link reftype=hd refid=character_types_constantstrings.Constant strings:elink.
:li.:link reftype=hd refid=character_types_pcharstrings.PChar - Null terminated strings:elink.
:eul.


.* ...................................................................
:h4 name=character_types_char.Char
:p.
&fpc. supports the type :hp2.Char:ehp2.. A :hp2.Char:ehp2. is exactly 1 byte in
size, and contains one ASCII character.

:p.
A character constant can be specified by enclosing the character in single
quotes, as follows: 'a' or 'A' are both character constants.

:p.
A character can also be specified by its character
value (commonly an ASCII code), by preceding the ordinal value with the 
number symbol (#). For example specifying :hp1.#65:ehp1. would be the same as :hp1.'A':ehp1.

:p.
Also, the caret character (^) can be used in combination with a letter to
specify a character with ASCII value less than 27. Thus ^G equals
#7 - G is the seventh letter in the alphabet.

:p.
When the single quote character must be represented, it should be typed
two times successively, thus :hp1.'''':ehp1. represents the single quote character.


.* ...................................................................
:h4 name=character_types_strings.Strings
:p.
&fpc. supports the :hp2.String:ehp2. type as it is defined in &tp.:
a sequence of characters with an optional size specification.
It also supports AnsiStrings (with unlimited length) as in Delphi.

:p.
To declare a variable as a string, use the following type specification:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.String Type:ehp2.

&ra.&ra.─── string type ─ :hp2.string:ehp2. ──┬──────────────────────────────┬─────────────────&ra.&la.
                             └─ [ ── unsigned integer ── ] ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
If there is a size specifier, then its maximum value - indicating the maximum 
size of the string - is 255.
:p.
The meaning of a string declaration statement without size indicator is 
interpreted differently depending on the :hp2.{$H}:ehp2. switch. If no size
indication is present, the above declaration can declare an AnsiString or 
a short string.
:p.
Whatever the actual type, AnsiStrings and short strings can be used
interchangeably. The compiler always takes care of the necessary type
conversions. Note, however, that the result of an expression that contains
ansistrings and short strings will always be an AnsiString.


:h4 name=character_types_shortstrings.Short strings
:p.
A string declaration declares a short string in the following cases:
:ol.
:li. If the switch is off: {$H-}, the string declaration
will always be a short string declaration.
:li. If the switch is on {$H+}, and there is a maximum length (the
size) specifier, the declaration is a short string declaration.
:eol.

:p.
The predefined type :hp2.ShortString:ehp2. is defined as a string of size 255:

:xmp.
 ShortString = String[255];
:exmp.

:p.
If the size of the string is not specified, 255 is taken as a
default. The actual length of the string can be obtained with the
:hp1.Length():ehp1. standard runtime routine. For example in

:xmp.
{$H-}

type
  NameString = String[10];
  StreetString = String;
:exmp.

:p.
:hp1.NameString:ehp1. can contain a maximum of 10 characters. While
:hp1.StreetString:ehp1. can contain up to 255 characters.

:nt.
Short strings have a maximum length of 255 characters: when specifying a
maximum length, the maximum length may not exceed 255. If a length larger
than 255 is attempted, then the compiler will give an error message:

:xmp.
Error: string length must be a value from 1 to 255
:exmp.

:p.
For short strings, the length is stored in the character at index 0. Old
&tp. code relies on this, and it is implemented similarly in &fpc.. 
Despite this, to write portable code, it is best to set the length of a 
shortstring  with the :hp1.SetLength():ehp1. call, and to retrieve
it with the :hp1.Length():ehp1. call. These functions will always work, whatever
the internal representation of the short strings or other strings in use:
this allows easy switching between the various string types.
:ent.


.* ...................................................................
:h4 name=character_types_ansistrings.AnsiStrings
:p.
AnsiStrings are strings that have no length limit. They are reference
counted and are guaranteed to be null terminated. Internally, an ansistring is treated as 
a pointer: the actual content of the string is stored on the heap, as much
memory as needed to store the string content is allocated. 

This is all handled transparantly, i.e. they can be manipulated as a normal 
short string. Ansistrings can be defined using the predefined :hp2.AnsiString:ehp2. 
type. 

:nt.
The null-termination does not mean that null characters (char(0) or #0) 
cannot be used: the null-termination is not used internally, but is there for
convenience when dealing with external routines that expect a
null-terminated string (as most C routines do).
:ent.

:p.
If the {$H} switch is on, then a string definition using the
regular :hp1.String:ehp1. keyword and that doesn't contain a length specifier, 
will be regarded as an ansistring as well. If a length specifier is present,
a short string will be used, regardless of the {$H} setting.

:p.
If the string is empty (''), then the internal pointer representation
of the string pointer is :hp1.Nil:ehp1.. If the string is not empty, then the 
pointer points to a structure in heap memory.

:p.
The internal representation as a pointer, and the automatic null-termination
make it possible to typecast an ansistring to a pchar. If the string is empty 
(so the pointer is Nil) then the compiler makes sure that the typecasted 
pchar will point to a null byte.

:p.
Assigning one ansistring to another doesn't involve moving the actual
string. A statement

:xmp.
  S2 := S1;
:exmp.

:p.
results in the reference count of :hp1.S2:ehp1. being decreased with 1, 
the reference count of :hp1.S1:ehp1. is increased by 1, and finally :hp1.S1:ehp1.
(as a pointer) is copied to :hp1.S2:ehp1.. This is a significant speed-up in
the code.

:p.
If the reference count of a string reaches zero, then the memory occupied 
by the string is deallocated automatically, and the pointer is set to
:hp1.Nil:ehp1., so no memory leaks arise.

:p.
When an ansistring is declared, the &fpc. compiler initially
allocates just memory for a pointer, not more. This pointer is guaranteed
to be :hp1.Nil:ehp1., meaning that the string is initially empty. This is
true for local and global ansistrings or ansistrings that are part of a 
structure (arrays, records or objects).

:p.
This does introduce an overhead. For instance, declaring

:xmp.
var
  A: array[1..100000] of string;
:exmp.

:p.
will copy the value :hp1.Nil:ehp1. 100,000 times into :hp1.A:ehp1.. 
When :hp1.A:ehp1. goes out of scope, then the reference 
count of the 100,000 strings will be decreased by 1 for each
of these strings. All this happens invisible to the programmer, 
but when considering performance issues, this is important.

:p.
Memory for the string content will be allocated only when the string is 
assigned a value. If the string goes out of scope, then its reference 
count is automatically decreased by 1. If the reference count reaches 
zero, the memory reserved for the string is released.

:p.
If a value is assigned to a character of a string that has a reference count
greater than 1, such as in the following statements:

:xmp.
  S := T;  { reference count for S and T are now 2 }
  S[I] := '@';
:exmp.

:p.
then a copy of the string is created before the assignment. This is known
as :hp1.copy-on-write:ehp1. semantics. It is possible to force a string to have
a reference count equal to 1 with the :hp1.UniqueString():ehp1. call:

:xmp.
  S := T;
  R := T; // Reference count of T is at least 3
  UniqueString(T); // Reference count of T is quaranteed 1
:exmp.

:p.
It's recommended to do this e.g. when typecasting an ansistring to a PChar var
and passing it to a C routine that modifies the string.

:p.
The :hp1.Length():ehp1. function must be used to get the length of an
ansistring: the length is not stored at character 0 of the ansistring. 
The construct

:xmp.
 L := ord(S[0]);
:exmp.

:p.
which was valid for &tp. shortstrings, is no longer correct for
AnsiStrings. The compiler will warn if such a construct is encountered.

:p.
To set the length of an ansistring, the :hp1.SetLength():ehp1. function must be used.
Constant ansistrings have a reference count of -1 and are treated specially.
The same remark as for :hp1.Length():ehp1. must be given: The construct

:xmp.
  L := 12;
  S[0] := Char(L);
:exmp.

:p.
which was valid for &tp. shortstrings, is no longer correct for
AnsiStrings. The compiler will warn if such a construct is encountered.

:p.
AnsiStrings are converted to short strings by the compiler if needed,
this means that the use of ansistrings and short strings can be mixed
without problems.

:p.
AnsiStrings can be typecasted to :hp1.PChar:ehp1. or :hp1.Pointer:ehp1. types:

:xmp.
var
  P: Pointer;
  PC: PChar;
  S: AnsiString;
begin
  S  := 'This is an ansistring';
  PC := Pchar(S);
  P  := Pointer(S);
:exmp.

:p.
There is a difference between the two typecasts. When an empty
ansistring is typecasted to a pointer, the pointer wil be :hp1.Nil:ehp1.. If an
empty ansistring is typecasted to a :hp1.PChar:ehp1., then the result will be a pointer to a
zero byte (an empty string).

:p.
The result of such a typecast must be used with care. In general, it is best
to consider the result of such a typecast as read-only, i.e. only suitable for
passing to a procedure that needs a constant pchar argument.

:p.
It is therefore :hp2.not:ehp2. advisable to typecast one of the following:

:ol.
:li. Expressions.
:li. Strings that have a reference count larger than 1.
In this case you should call :hp1.UniqueString():ehp1. to ensure the 
string has a reference count 1.
:eol.



.* ...................................................................
:h4 name=character_types_widestrings.WideStrings
:p.
WideStrings (used to represent Unicode character strings) are implemented in much 
the same way as AnsiStrings: reference counted, null-terminated arrays, only they 
are implemented as arrays of :hp1.WideChars:ehp1. instead of regular :hp1.Chars:ehp1..
A :hp1.WideChar:ehp1. is a two-byte character (an element of a DBCS: Double Byte
Character Set). Mostly the same rules apply for WideStrings as for 
AnsiStrings. The compiler transparently converts WideStrings to
AnsiStrings and vice versa.

:p.
Similarly to the typecast of an Ansistring to a PChar null-terminated
array of characters, a WideString can be converted to a PWideChar
null-terminated array of characters. 
Note that the :hp1.PWideChar:ehp1. array is terminated by 2 null bytes instead of
1, so a typecast to a PChar is not automatic.

:p.
The compiler itself provides no support for any conversion from Unicode to
AnsiStrings or vice versa. The :hp2.system:ehp2. unit has a widestring manager
record, which can be initialized with some OS-specific Unicode handling
routines. For more information, see the :hp2.system:ehp2. unit reference.


.* ...................................................................
:h4 name=character_types_unicodestrings.UnicodeStrings
:p.
[text to be written]
:p.
Same as WideString, but reference counted on all platforms.



.* ...................................................................
:h4 name=character_types_constantstrings.Constant strings
:p.
To specify a constant string, it must be enclosed in single quotes, just
as a :hp1.Char:ehp1. type, only now more than one character is allowed.
Given that :hp1.S:ehp1. is of type :hp1.String:ehp1., the following are valid
assignments:

:xmp.
S := 'This is a string.';
S := 'One'+', Two'+', Three';
S := 'This isn''t difficult!';
S := 'This is a weird character: '#145' !';
:exmp.

:p.
As can be seen, the single quote character is represented by 2 single-quote
characters next to each other. Strange characters can be specified by their
character value (usually an ASCII code).
The example shows also that two strings can be added. The resulting string is
just the concatenation of the first with the second string, without spaces in
between them. Strings can not be substracted, however.

:p.
Whether the constant string is stored as an AnsiString or a short string
depends on the settings of the :hp2.{$H}:ehp2. switch.



.* ...................................................................
:h4 name=character_types_pcharstrings.PChar - Null terminated strings
:p.
&fpc. supports the Delphi implementation of the :hp1.PChar:ehp1. type. :hp1.PChar:ehp1.
is defined as a pointer to a \var{Char} type, but allows additional
operations.
The :hp1.PChar:ehp1. type can be understood best as the Pascal equivalent of a
C-style null-terminated string, i.e. a variable of type :hp1.PChar:ehp1. is a
pointer that points to an array of type :hp1.Char:ehp1., which is ended by a
null-character (#0).
&fpc. supports initializing of :hp1.PChar:ehp1. typed constants, or a direct
assignment. For example, the following pieces of code are equivalent:

:xmp.
program one;
var
  p: PChar;
begin
  P := 'This is a null-terminated string.';
  WriteLn (P);
end.
:exmp.

:p.
Results in the same as

:xmp.
program two;
const
  P: PChar = 'This is a null-terminated string.'
begin
  WriteLn (P);
end.
:exmp.

:p.
These examples also show that it is possible to write the contents of
the string to a file of type :hp1.Text:ehp1.
The :hp2.strings:ehp2. unit contains procedures and functions that manipulate the
:hp1.PChar:ehp1. type as in the standard C library.
Since it is equivalent to a pointer to a type :hp1.Char:ehp1. variable, it is
also possible to do the following:

:xmp.
program three;
var
  S: String[30];
  P: PChar;
begin
  S := 'This is a null-terminated string.'#0;
  P := @S[1];
  WriteLn (P);
end.
:exmp.

:p.
This will have the same result as the previous two examples.
Null-terminated strings cannot be added as normal Pascal
strings. If two :hp1.PChar:ehp1. strings must be concatenated; the functions from
the unit :hp2.strings:ehp2. must be used.

:p.
However, it is possible to do some pointer arithmetic. The
operators + and - can be used to do operations 
on :hp1.PChar:ehp1. pointers.
In the table below, :hp1.P:ehp1. and :hp1.Q:ehp1. are of type :hp1.PChar:ehp1., and
:hp1.I:ehp1. is of type :hp1.Longint:ehp1..

:cgraphic.
 :hp2.Operation                                                  Result:ehp2.
────────────────────────────────────────────────────────────────────
 P + I                       Adds I to the address pointed to by P.
 I + P                       Adds I to the address pointed to by P.
 P - I               Substracts I from the address pointed to by P.
 P - Q     Returns, as an integer, the distance between 2 addresses
                      (or the number of characters between P and Q)
────────────────────────────────────────────────────────────────────
:ecgraphic.



.* --------------------------------------------------------------
:h3 name=structured_types.Structured types
:p.
A structured type is a type that can hold multiple values in one variable.
Stuctured types can be nested to unlimited levels.

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Structured Types:ehp2.

&ra.&ra.─── structured type  ──┬────  array type    ────┬───────────────────────────&ra.&la.
                         ├────  record type   ────┤
                         ├────  object type   ────┤
                         ├────  class type    ────┤
                         ├─ class reference type ─┤
                         ├──── interface type ────┤
                         ├────    set type    ────┤
                         └─────  file type  ──────┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
Unlike Delphi, &fpc. does not support the keyword :hp1.packed:ehp1. for all
structured types.  In the following sections each of the possible 
structured types is discussed. It will be mentioned when a type supports 
the :hp1.packed:ehp1. keyword.


.* ...................................................................
:h4 name=structured_types_packed.Packed structured types
:p.
When a structured type is declared, no assumptions should be made about
the internal position of the elements in the type. The compiler will lay
out the elements of the structure in memory as it thinks will be most
suitable. That is, the order of the elements will be kept, but the location
of the elements are not guaranteed, and is partially governed by the :hp2.$PACKRECORDS:ehp2.
directive (this directive is explained in the &progref.).

:p.
However, &fpc. allows controlling the layout with the :hp1.Packed:ehp1. and
:hp1.Bitpacked:ehp1. keywords. The meaning of these words depends on the context:

:parml tsize=15 break=none.
:pt.:hp2.Bitpacked:ehp2.
:pd.In this case, the compiler will attempt to align ordinal
types on bit boundaries, as explained below.

:pt.:hp2.Packed:ehp2.
:pd.The meaning of the :hp1.Packed:ehp1. keyword depends on the
situation:
:ol.
:li. In :hp1.MACPAS:ehp1. mode, it is equivalent to the :hp1.Bitpacked:ehp1. keyword.
:li. In other modes, with the :hp1.$BITPACKING:ehp1. directive set to :hp1.ON:ehp1.,
it is also equivalent to the :hp1.Bitpacked:ehp1. keyword.
:li. In other modes, with the :hp1.$BITPACKING:ehp1. directive set to :hp1.OFF:ehp1.,
it signifies normal packing on byte boundaries.
:lp. Packing on byte boundaries means that each new element of a structured type
starts on a byte boundary.
:eol.
:eparml.

:p.
The byte packing mechanism is simple: the compiler aligns each element of
the structure on the first available byte boundary, even if the size of the
previous element (small enumerated types, subrange types) is less than a
byte.

:p.
When using the bit packing mechanism, the compiler calculates for each
ordinal type how many bits are needed to store it. The next ordinal type
is then stored on the next free bit. Non-ordinal types - which include but
are not limited to - sets, floats, strings, (bitpacked) records, (bitpacked)
arrays, pointers, classes, objects, and procedural variables, are stored
on the first available byte boundary.

:p.
Note that the internals of the bitpacking are opaque: they can change
at any time in the future. What is more: the internal packing depends
on the endianness of the platform for which the compilation is done,
and no conversion between platforms are possible. This makes bitpacked
structures unsuitable for storing on disk or transport over networks.
The format is however the same as the one used by the GNU Pascal
Compiler, and the &fpc. team aims to retain this compatibility in the future.

:p.
There are some more restrictions to elements of bitpacked structures:

:ul.
:li. The address cannot be retrieved, unless the bit size is a multiple of
8 and the element happens to be stored on a byte boundary.
:li. An element of a bitpacked structure cannot be used as a var parameter,
unless the bit size is a multiple of 8 and the element happens to be stored 
on a byte boundary.
:eul.

:p.
To determine the size of an element in a bitpacked structure, there is the 
:hp1.BitSizeOf():ehp1. function. It returns the size - in bits - of the element. 
For other types or elements of structures which are not bitpacked, this will 
simply return the size in bytes multiplied by 8, i.e., the return value is 
then the same as :hp1.8*SizeOf:ehp1..

:p.
The size of bitpacked records and arrays is limited:

:ul.
:li. On 32 bit systems the maximal size is 2^29 (2 to the power 29) bytes (512 MB).
:li. On 64 bit systems the maximal size is 2^61 (2 to the power 61) bytes.
:eul.

:p.
The reason is that the offset of an element must be calculated with 
the maximum integer size of the system.


.* ...................................................................
:h4 name=structured_types_arrays.Arrays
:p.
&fpc. supports arrays as in &tp.. Multi-dimensional arrays and (bit)packed 
arrays are also supported, as well as the dynamic arrays of &delphi.:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Array types:ehp2.

&ra.&ra.── array type ─┬───────────────┬─ :hp2.array:ehp2. ─┬────────────────────────────┬─ :hp2.of:ehp2. ─ type ──&ra.&la.
                 ├─   :hp2.packed:ehp2.    ─┤         └─ :hp2.[:ehp2. ─┬─ ordinal type ─┬─ :hp2.]:ehp2. ─┘
                 └─  :hp2.bitpacked:ehp2.  ─┘               ^────── , ───────┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.



:h5.Static arrays
:p.
When the range of the array is included in the array definition, it is
called a static array. Trying to access an element with an index that is
outside the declared range will generate a run-time error (if range checking
is on).  The following is an example of a valid array declaration:

:xmp.
type
  RealArray = Array [1..100] of Real;
:exmp.

:p.
Valid indexes for accessing an element of the array are between 1 and 100,
where the borders 1 and 100 are included.
As in &tp., if the array component type is in itself an array, it is
possible to combine the two arrays into one multi-dimensional array. The
following declaration:

:xmp.
type
   APoints = array[1..100] of Array[1..3] of Real;
:exmp.

:p.
is equivalent to the declaration:

:xmp.
type
   APoints = array[1..100, 1..3] of Real;
:exmp.

:p.
The functions :hp1.High():ehp1. and :hp1.Low():ehp1. return the high and low bounds of
the leftmost index type of the array. In the above case, this would be 100
and 1. You should use them whenever possible, since it improves maintainability
of your code. The use of both functions is just as efficient as using
constants, because they are evaluated at compile time.

:p.
When static array-type variables are assigned to each other, the contents of the
whole array is copied. This is also true for multi-dimensional arrays:

:xmp.
program testarray1;

type
  TA = Array[0..9, 0..9] of Integer;
  
var   
  A, B: TA;
  I, J: Integer;
begin
  for I := 0 to 9 do
    for J := 0 to 9 do 
      A[I,J] := I * J;
  for I := 0 to 9 do
  begin
    for J := 0 to 9 do 
      Write(A[I, J]:2, ' ');
    writeln;
  end;
  B := A;
  writeln;
  for I := 0 to 9 do
    for J := 0 to 9 do 
      A[9-I, 9-J] := I * J;
  for I := 0 to 9 do
  begin
    for J := 0 to 9 do 
      write(B[I, J]:2, ' ');
    writeln;
  end;
end.  
:exmp.

:p.
The output of this program will be 2 identical matrices.



:h5.Dynamic arrays
:p.
As of version 1.1, &fpc. also knows dynamic arrays: In that case the array
range is omitted, as in the following example:

:xmp.
Type
  TByteArray = Array of Byte;
:exmp.

:p.
When declaring a variable of a dynamic array type, the initial length of the
array is zero. The actual length of the array must be set with the standard
:hp1.SetLength():ehp1. function, which will allocate the necessary memory to contain 
the array elements on the heap. The following example will set the length to
1000:

:xmp.
var 
  A: TByteArray;
begin
  SetLength(A, 1000);
:exmp.

:p.
After a call to :hp1.SetLength():ehp1., valid array indexes are 0 to 999: the array
index is always zero-based.

:p.
Note that the length of the array is set in elements, not in bytes of 
allocated memory (although these may be the same). The amount of 
memory allocated is the size of the array multiplied by the size of 
1 element in the array. The memory will be disposed of at the exit of the
current procedure or function. 

:p.
It is also possible to resize the array: in that case, as much of the 
elements in the array as will fit in the new size, will be kept. The array
can be resized to zero, which effectively resets the variable.

:p.
At all times, trying to access an element of the array with an index 
that is not in the current length of the array will generate a run-time 
error.

:p.
Dynamic arrays are reference counted: assignment of one dynamic array-type 
variable to another will let both variables point to the same array. 
Contrary to ansistrings, an assignment to an element of one array will 
be reflected in the other: there is no copy-on-write. Consider the following
example:

:xmp.
var
  A, B: TByteArray;
begin
  SetLength(A, 10);
  A[0] := 33;
  B := A;
  A[0] := 31;
:exmp.

:p.
After the second assignment, the first element in B will also contain 31.

:p.
It can also be seen from the output of the following example:

:xmp.
program testarray1;

type
  TA = array of array of Integer;
  
var   
  A, B: TA;
  I, J: Integer;
begin
  Setlength(A, 10, 10);
  for I := 0 to 9 do
    for J := 0 to 9 do 
      A[I, J] := I * J;
  for I:=0 to 9 do
  begin
    for J := 0 to 9 do 
      Write(A[I, J]:2, ' ');
    writeln;
  end;
  B := A;
  writeln;
  for I := 0 to 9 do
    for J := 0 to 9 do 
      A[9-I, 9-J] := I * J;
  for I := 0 to 9 do
  begin
    for J := 0 to 9 do 
      Write(B[I, J]:2, ' ');
    writeln;
  end;
end.  
:exmp.

:p.
The output of this program will be a matrix of numbers, and then the same
matrix, mirrorred.

:p.
As remarked earlier, dynamic arrays are reference counted: if in one of the previous examples A
goes out of scope and B does not, then the array is not yet disposed of: the
reference count of A (and B) is decreased with 1. As soon as the reference
count reaches zero the memory, allocated for the contents of the array, is disposed of.

:p.
It is also possible to copy and/or resize the array with the standard 
:hp1.Copy():ehp1. function, which acts as the copy function for strings:

:xmp.
program testarray3;

type
  TA = array of Integer;
  
var   
  A, B: TA;
  I: Integer;

begin
  Setlength(A, 10);
  for I := 0 to 9 do
      A[I] := I;
  B := Copy(A, 3, 6);    
  for I := 0 to 5 do
    Writeln(B[I]);
end.  
:exmp.

:p.
The :hp1.Copy():ehp1. function will copy 6 elements of the array to a new array.
Starting at the element at index 3 (i.e. the fourth element) of the array.

:p.
The :hp1.Length():ehp1. function will return the number of elements in the array.
The :hp1.Low():ehp1. function on a dynamic array will always return 0, and the
:hp1.High():ehp1. function will return the value :hp1.Length-1:ehp1., i.e., the value of the
highest allowed array index. 



:h5.Packing and unpacking an array
:p.
Arrays can be packed and bitpacked. Two array types which have the same index
type and element type, but which are differently packed are not assignment 
compatible.

:p.
However, it is possible to convert a normal array to a bitpacked array with the
:hp1.pack:ehp1. routine. The reverse operation is possible as well; a bitpacked
array can be converted to a normally packed array using the :hp1.unpack:ehp1.
routine, as in the following example:

:xmp.
var
  foo: array [ 'a'..'f' ] of Boolean 
    = ( false, false, true, false, false, false );
  bar: packed array [ 42..47 ] of Boolean;
  baz: array [ '0'..'5' ] of Boolean;

begin
  pack(foo,'a',bar);
  unpack(bar,baz,'0');
end.
:exmp.

:p.
More information about the pack and unpack routines can be found in the
:hp2.system:ehp2. unit reference.


:h4 name='record_types'.Record types
:p.
&fpc. supports fixed records and records with variant parts.
The syntax diagram for a record type is:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Record types:ehp2.

&ra.&ra.── record type ─┬───────────────┬─ :hp2.record:ehp2. ─┬──────────────┬─ :hp2.end:ehp2. ───────────&ra.&la.
                  ├─   :hp2.packed:ehp2.    ─┤          └─ field list ─┘
                  └─  :hp2.bitpacked:ehp2.  ─┘

&ra.&ra.── field list ─┬─── fixed fields ───────────────────────┬─┬─────┬───────────&ra.&la.
                 └─┬─────────────────────┬─ variant part ─┘ └─ ; ─┘
                   └─  fixed fields ─ ; ─┘

&ra.&ra.── fixed fields ─┬─ identifier list ─ : ─ type ─┬───────────────────────────&ra.&la.
                   └──────────── ; ───────────────┘

&ra.&ra.── variant part ─ :hp2.case:ehp2. ─┬────────────────┬─ ordinal type identifier ─ :hp2.of:ehp2. ─┬─ variant─┬───&ra.&la.
                          └ identifier ─ : ┘                                ^──── ; ───┘

&ra.&ra.── variant ─┬─ constant ─ , ─┬─ : ─ ( ─┬────────────────┬─ ) ───────────────&ra.&la.
              ^────────────────┘         ^── field list ──┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
So the following are valid record type declarations:

:xmp.
type
  Point = record
    X, Y, Z: Real;
  end;

  RPoint = record
    case Boolean of
      False: (X, Y, Z: Real);
      True: (R, theta, phi: Real);
  end;

  BetterRPoint = record
    case UsePolar: Boolean of
      False: (X, Y, Z: Real);
      True: (R, theta, phi: Real);
  end;
:exmp.


.* :fn id=record1.
.* However, it is up to the programmer to maintain this field.
.* :efn.

:p.
The variant part must be last in the record. The optional identifier in the
case statement serves to access the tag field value, which otherwise would
be invisible to the programmer. It can be used to see which variant is
active at a certain time [However, it is up to the programmer to maintain this field].
.* :link refid=record1 reftype=fn.(footnote here):elink.].
In effect, it introduces a new field in the record.

:note.
It is possible to nest variant parts, as in&colon.

:xmp.
type
  MyRec = Record
    X: Longint;
    case byte of
      2: (Y: Longint;
      case byte of
        3: (Z: Longint);
      );
  end;
:exmp.

:p.
By default the size of a record is the sum of the sizes of its fields, each size of a
field is rounded up to a power of two. If the record contains a variant part, the size
of the variant part is the size of the biggest variant, plus the size of the
tag field type :hp1.if an identifier was declared for it:ehp1.. Here also, the size of
each part is first rounded up to two. So in the above example:
:ul.
:li. :hp1.SizeOf():ehp1. would return 24 for :hp1.Point:ehp1.
:li. It would result in 24 for :hp1.RPoint}:ehp1.
:li. Finally, 26 would be the size of :hp1.BetterRPoint:ehp1. 
:li. For :hp1.MyRec:ehp1., the value would be 12.
:eul.

:p.
If a typed file with records, produced by a &tp. program, must be read,
then chances are that attempting to read that file correctly will fail.
The reason for this is that by default, elements of a record are aligned at
2-byte boundaries, for performance reasons. 
:p.
This default behaviour can be changed with the :hp1.{$PACKRECORDS N}:ehp1. 
switch. Possible values for :hp1.N:ehp1. are 1, 2, 4, 16 or :hp1.Default:ehp1.
This switch tells the compiler to align elements of a record or object or
class that have size larger than :hp1.n:ehp1. on :hp1.n:ehp1. byte boundaries.
:p.
Elements that have size smaller or equal than :hp1.n:ehp1. are aligned on
natural boundaries, i.e. to the first power of two that is larger than or
equal to the size of the record element.
:p.
The keyword :hp1.Default:ehp1. selects the default value for the platform
that the code is compiled for (currently, this is 2 on all platforms)
Take a look at the following program:

:xmp.
Program PackRecordsDemo;

type
    {$PackRecords 2}
    Trec1 = Record
      A : byte;
      B : Word;
    end;

    {$PackRecords 1}
    Trec2 = Record
      A : Byte;
      B : Word;
    end;

    {$PackRecords 2}
    Trec3 = Record
      A,B : byte;
    end;

    {$PackRecords 1}
    Trec4 = Record
      A,B : Byte;
    end;
   
    {$PackRecords 4}
    Trec5 = Record
      A : Byte;
      B : Array[1..3] of byte;
      C : byte;
    end;

    {$PackRecords 8}
    Trec6 = Record
      A : Byte;
      B : Array[1..3] of byte;
      C : byte;
    end;
   
    {$PackRecords 4}
    Trec7 = Record
      A : Byte;
      B : Array[1..7] of byte;
      C : byte;
    end;

    {$PackRecords 8}
    Trec8 = Record
      A : Byte;
      B : Array[1..7] of byte;
      C : byte;
    end;

var
  rec1 : TRec1;
  rec2 : TRec2;
  rec3 : TRec3;
  rec4 : TRec4;
  rec5 : TRec5;
  rec6 : TRec6;
  rec7 : TRec7;
  rec8 : TRec8;

begin
  Write ('Size TRec1 : ',SizeOf(Trec1));
  Writeln (' Offset B : ',Longint(@rec1.B)-Longint(@rec1));
  Write ('Size TRec2 : ',SizeOf(Trec2));
  Writeln (' Offset B : ',Longint(@rec2.B)-Longint(@rec2));
  Write ('Size TRec3 : ',SizeOf(Trec3));
  Writeln (' Offset B : ',Longint(@rec3.B)-Longint(@rec3));
  Write ('Size TRec4 : ',SizeOf(Trec4));
  Writeln (' Offset B : ',Longint(@rec4.B)-Longint(@rec4));
  Write ('Size TRec5 : ',SizeOf(Trec5));
  Writeln (' Offset B : ',Longint(@rec5.B)-Longint(@rec5),
           ' Offset C : ',Longint(@rec5.C)-Longint(@rec5));
  Write ('Size TRec6 : ',SizeOf(Trec6));
  Writeln (' Offset B : ',Longint(@rec6.B)-Longint(@rec6),
           ' Offset C : ',Longint(@rec6.C)-Longint(@rec6));
  Write ('Size TRec7 : ',SizeOf(Trec7));
  Writeln (' Offset B : ',Longint(@rec7.B)-Longint(@rec7),
           ' Offset C : ',Longint(@rec7.C)-Longint(@rec7));
  Write ('Size TRec8 : ',SizeOf(Trec8));
  Writeln (' Offset B : ',Longint(@rec8.B)-Longint(@rec8),
           ' Offset C : ',Longint(@rec8.C)-Longint(@rec8));
end.
:exmp.

:p.
The output of this program will be:

:xmp.
Size TRec1 : 4 Offset B : 2
Size TRec2 : 3 Offset B : 1
Size TRec3 : 2 Offset B : 1
Size TRec4 : 2 Offset B : 1
Size TRec5 : 8 Offset B : 4 Offset C : 7
Size TRec6 : 8 Offset B : 4 Offset C : 7
Size TRec7 : 12 Offset B : 4 Offset C : 11
Size TRec8 : 16 Offset B : 8 Offset C : 15
:exmp.

:p.
And this is as expected:

:ul.
:li. In Trec1, since B has size 2, it is aligned on a 2 byte boundary, thus leaving an empty byte
between A and B, and making the total size 4. In Trec2, B is aligned on a 1-byte boundary,
right after A, hence, the total size of the record is 3.

:li.For Trec3, the sizes of A,B are 1, and hence they are aligned on 1 byte boundaries. The same
is true for Trec4.

:li.For Trec5, since the size of B – 3 – is smaller than 4, B will be on a 4-byte boundary, as this
is the first power of two that is larger than its size. The same holds for Trec6.

:li.For Trec7, B is aligned on a 4 byte boundary, since its size – 7 – is larger than 4. However, in
Trec8, it is aligned on a 8-byte boundary, since 8 is the first power of two that is greater than
7, thus making the total size of the record 16.
:eul.

:p.
&fpc. supports also the 'packed record', this is a record where all the elements are byte-aligned.
Thus the two following declarations are equivalent:

:xmp.
     {$PackRecords 1}
     Trec2 = Record
       A : Byte;
       B : Word;
     end;
     {$PackRecords 2}
:exmp.

:p.
and

:xmp.
     Trec2 = Packed Record
       A : Byte;
       B : Word;
     end;
:exmp.

:p.
Note the :hp1.{$PackRecords 2}:ehp1. after the first declaration!



:h4 name='set_types'.Set types
:p.
&fpc. supports the set types as in &tp.. The prototype of a set declaration is:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Set types:ehp2.

&ra.&ra.── set type ── :hp2.set:ehp2. ── :hp2.of:ehp2. ── ordinal type ───────────────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
Each of the elements of :hp1.SetType:ehp1. must be of type :hp1.TargetType:ehp1..
:hp1.TargetType:ehp1. can be any ordinal
type with a range between 0 and 255. A set can contain at most 255 elements.
The following are valid set declaration:

:xmp.
type
  Junk = set of Char;

  Days = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
  WorkDays: set of Days;
:exmp.

:p.
Given these declarations, the following assignment is legal:

:xmp.
WorkDays := [Mon, Tue, Wed, Thu, Fri];
:exmp.

:p.
The compiler stores small sets (less than 32 elements) in a Longint, if the
type range allows it. This
allows for faster processing and decreases program size. Otherwise, sets
are stored in 32 bytes.
:p.
Several operations can be done on sets: taking unions or differences, adding
or removing elements,
comparisons. These are documented in :link refid='set_operators' reftype=hd.set operators:elink..



:h4 name='file_types'.File types
:p.
File types are types that store a sequence of some base type, which can be any type except another file
type. It can contain (in principle) an infinite number of elements. File types are used commonly to
store data on disk. However, nothing prevents the programmer, from writing a file driver that stores
its data for instance in memory.
:p.
Here is the type declaration for a file type:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.File types:ehp2.

&ra.&ra.── file type ── :hp2.file:ehp2. ─┬─────────────┬───────────────────────────────────────&ra.&la.
                        └─ :hp2.of:ehp2. ─ type ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
If no type identifier is given, then the file is an untyped file; it can be
considered as equivalent to a file
of bytes. Untyped files require special commands to act on them
(see :hp1.Blockread:ehp1., :hp1.Blockwrite:ehp1.).
The following declaration declares a file of records:

:xmp.
type
  Point = Record
     X,Y,Z : real;
     end;
  PointFile = File of Point;
:exmp.

:p.
Internally, files are represented by the :hp1.FileRec:ehp1. record, which is declared
in the Dos or SysUtils units.
:p.
A special file type is the :hp1.Text:ehp1. file type, represented by the :hp1.TextRec:ehp1. record.
A file of type :hp1.Text:ehp1. uses special input-output routines. The default :hp1.Input:ehp1.,
:hp1.Output:ehp1. and :hp1.StdErr:ehp1. file types are defined in the system unit: they are all of
type :hp1.Text:ehp1., and are opened by the system unit initialization code.




.* --------------------------------------------------------------
:h3 name=pointer_types.Pointers
:p.
&fpc. supports the use of pointers. A variable of the pointer type contains an
address in memory, where the data of another variable may be stored. A pointer
type can be defined as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Pointer types:ehp2.

&ra.&ra.── pointer type ── ^ ── type identifier ────────────────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
As can be seen from this diagram, pointers are typed, which means that they point to a particular
kind of data. The type of this data must be known at compile time.
:p.
Dereferencing the pointer (denoted by adding ^ after the variable name) behaves then like a variable.
This variable has the type declared in the pointer declaration, and the variable is stored in the address
that is pointed to by the pointer variable. Consider the following example:

:xmp.
program pointers;

type
  Buffer = String[255];
  BufPtr = ^Buffer;

var
  B: Buffer;
  BP: BufPtr;
  PP: Pointer;
etc...
:exmp.

:p.
In this example, BP :hp1.is a pointer to:ehp1. a Buffer type; while B is a variable of type Buffer. B takes
256 bytes memory, and BP only takes 4 (or 8) bytes of memory: enough memory to store an address.
:p.
The expression

:xmp.
BP^
:exmp.

:p.
is known as the dereferencing of BP. The result is of type Buffer, so

:xmp.
BP^[23]
:exmp.

:p.
Denotes the 23-rd character in the string pointed to by BP.

:nt. &fpc. treats pointers much the same way as C does. This means that a pointer to some type
can be treated as being an array of this type.
:p.
From this point of view, the pointer then points to the zeroeth element of this
array. Thus the following pointer declaration

:xmp.
var
  p: ^Longint;
:exmp.

:p.
can be considered equivalent to the following array declaration:

:xmp.
var
  p: array[0..Infinity] of Longint;
:exmp.

:p.
The difference is that the former declaration allocates memory for the pointer only (not for the array),
and the second declaration allocates memory for the entire array. If the former is used, the memory
must be allocated manually, using the :hp1.Getmem:ehp1. function. The reference P^ is then the same as p[0].
The following program illustrates this maybe more clear:

:xmp.
program PointerArray;
var
  i: Longint;
  p: ^Longint;
  pp: array[0..100] of Longint;
begin
  for i := 0 to 100 do pp[i] := i; { Fill array }
  p := @pp[0];                     { Let p point to pp }
  for i := 0 to 100 do
    if p[i] <> pp[i] then
      WriteLn ('Ohoh, problem !')
end.
:exmp.:ent.

:p.
&fpc. supports pointer arithmetic as C does. This means that, if P is a typed pointer, the
instructions

:xmp.
Inc(P);
Dec(P);
:exmp.

:p.
Will increase, respectively decrease the address the pointer points to with the size of the type P is a
pointer to. For example

:xmp.
var
  P: ^Longint;
&dot.&dot.&dot.
 Inc(p);
:exmp.

:p.
will increase P with 4, because 4 is the size of a longint. If the pointer is untyped, a size of 1 byte is
assumed (i.e. as if the pointer were a pointer to a byte: ^byte.)
:p.
Normal arithmetic operators on pointers can also be used, that is, the following are valid pointer
arithmetic operations:

:xmp.
var
  p1, p2: ^Longint;
  L: Longint;
begin
  P1 := @P2;
  P2 := @L;
  L  := P1-P2;
  P1 := P1-4;
  P2 := P2+4;
end.
:exmp.

:p.
Here, the value that is added or substracted is multiplied by the size of the type the pointer points to.
In the previous example P1 will be decremented by 16 bytes, and P2 will be incremented by 16.



.* --------------------------------------------------------------
:h3 name=forward_type_declarations.Forward type declarations
:p.
Programs often need to maintain a linked list of records. Each record then contains a pointer to the
next record (and possibly to the previous record as well). For type safety, it is best to define this
pointer as a typed pointer, so the next record can be allocated on the heap using the New call. In
order to do so, the record should be defined something like this:

:xmp.
type
  TListItem = Record
     Data: Integer;
     Next: ^TListItem;
  end;
:exmp.

:p.
When trying to compile this, the compiler will complain that the TListItem type is not yet defined
when it encounters the :hp1.Next:ehp1. declaration: This is correct, as the definition is still being parsed.
:p.
To be able to have the Next element as a typed pointer, a 'Forward type declaration'
must be introduced:

:xmp.
type
  PListItem = ^TListItem;
  TListItem = Record
  Data : Integer;
  Next : PTListItem;
end;
:exmp.

:p.
When the compiler encounters a typed pointer declaration where the referenced type is not yet known,
it postpones resolving the reference till later. The pointer definition is a 'Forward type declaration'.
:p.
The referenced type should be introduced later in the same Type block. No other block may come
between the definition of the pointer type and the referenced type. Indeed, even the word Type
itself may not re-appear: in effect it would start a new type-block, causing the compiler to resolve all
pending declarations in the current block.
:p.
In most cases, the definition of the referenced type will follow immediatly after the definition of
the pointer type, as shown in the above listing. The forward defined type can be used in any type
definition following its declaration.
:p.
Note that a forward type declaration is only possible with pointer types and classes, not with other
types.



.* --------------------------------------------------------------
:h3 name=procedural_types.Procedural types
:p.
&fpc. has support for procedural types, although it differs a little from the &tp. or
&delphi. implementation of them. The type declaration remains the same, as can be seen in the following
syntax diagram:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Procedural types:ehp2.

&ra.&ra.── procedural type ─┬─ function header ──┬┬───────────────┬┬─────────────────────┬──────&ra.&la.
                      └─ procedure header ─┘└─ :hp2.of:ehp2. ─ :hp2.object:ehp2. ─┘└─ ; ─ call modifier ─┘

&ra.&ra.── function header ── :hp2.function:ehp2. ─ formal parameter list ─ : ─ result type ───────────────&ra.&la.

&ra.&ra.── procedure header ── :hp2.procedure:ehp2. ─ formal parameter list ───────────────────────────────&ra.&la.

&ra.&ra.── call modifiers ─┬─ :hp2.register:ehp2. ─┬───────────────────────────────────────────────────────&ra.&la.
                     ├─  :hp2.cdecl:ehp2.   ─┤
                     ├─  :hp2.pascal:ehp2.  ─┤
                     ├─ :hp2.stdcall:ehp2.  ─┤
                     ├─ :hp2.safecall:ehp2. ─┤
                     └─  :hp2.inline:ehp2.  ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
For a description of formal parameter lists, see :link refid='procedure_declarations' reftype=hd. Procedure declarations:elink..
The two following examples are valid type declarations:

:xmp.
type
  TOneArg = Procedure (Var X : integer);
  TNoArg = Function : Real;
var
  proc : TOneArg;
  func : TNoArg;
:exmp.

:p.
One can assign the following values to a procedural type variable:

:ol.
:li. Nil, for both normal procedure pointers and method pointers.
:li. A variable reference of a procedural type, i.e. another variable of the same type.
:li. A global procedure or function address, with matching function or procedure header
and calling convention.
:li. A method address.
:eol.

:p.
Given these declarations, the following assignments are valid:

:xmp.
Procedure printit (Var X : Integer);
begin
  WriteLn (x);
end;
&dot.&dot.&dot.
Proc := @printit;
Func := @Pi;
:exmp.

:p.
From this example, the difference with &tp. is clear: In &tp. it isn't necessary to
use the address operator (@) when assigning a procedural type variable, whereas
in &fpc. it is required. In case the :hp1.-MDelphi:ehp1. or :hp1.-MTP:ehp1. switches
are used, the address operator can be dropped.

:nt.
The modifiers concerning the calling conventions must be the same as the
declaration; i.e. the following code would give an error:

:xmp.
type
  TOneArgCcall = procedure(Var X: integer); cdecl;

var
  proc: TOneArgCcall;

procedure printit(Var X : Integer);
begin
  WriteLn (x);
end;

begin
  Proc := @printit;
end.
:exmp.

Because the TOneArgCcall type is a procedure that uses the cdecl calling convention.
:ent.



.* --------------------------------------------------------------
:h3 name=variant_types.Variant types
:ul.
:li.:link refid='variant_types_definition' reftype='hd'.Definition:elink.
:li.:link refid='variant_types_inassignment' reftype='hd'.Variants in assignments and expressions:elink.
:li.:link refid='variant_types_andinterfaces' reftype='hd'.Variants and interfaces:elink.
:eul.

.* ...............................................................
:h4 name='variant_types_definition'.Definition
:p.
As of version 1.1, &fpc. has support for variants. For maximum variant support it is recommended to
add the variants unit to the uses clause of every unit that uses variants in some way: the variants unit
contains support for examining and transforming variants other than the default support offered by
the :hp1.System:ehp1. or :hp1.ObjPas:ehp1. units.
:p.
The type of a value stored in a variant is only determined at runtime: it depends what has been
assigned to the to the variant. Almost any simple type can be assigned to variants: ordinal types,
string types, int64 types.
:p.
Structured types such as sets, records, arrays, files, objects and classes are not assignment-compatible
with a variant, as well as pointers. Interfaces and COM or CORBA objects can be assigned to a
variant (basically because they are simply a pointer).
:p.
This means that the following assignments are valid:

:xmp.
type
  TMyEnum = (One, Two, Three);

var
  V: Variant;
  I: Integer;
  B: Byte;
  W: Word;
  Q: Int64;
  E: Extended;
  D: Double;
  En: TMyEnum;
  AS: AnsiString;
  WS: WideString;

begin
  V := I;
  V := B;
  V := W;
  V := Q;
  V := E;
  V := En;
  V := D:
  V := AS;
  V := WS;
end;
:exmp.

:p.
And of course vice-versa as well.
:p.
A variant can hold an an array of values: All elements in the array have the same type (but can be of
type 'variant'). For a variant that contains an array, the variant can be indexed:

:xmp.
program testv;

uses
  Variants;

var
  A: variant;
  I: integer;
begin
  A := VarArrayCreate([1, 10], varInteger);
  For I := 1 to 10 do
    A[I] := I;
end.
:exmp.

:p.
For the explanation of :hp1.VarArrayCreate:ehp1., see Unit Reference.
:p.
Note that when the array contains a string, this is not considered an 'array of characters', and so the
variant cannot be indexed to retrieve a character at a certain position in the string.



.* ..................................................................
:h4 name='variant_types_inassignment'.Variants in assignments and expressions
:p.
As can be seen from the definition above, most simple types can be assigned to a variant. Likewise,
a variant can be assigned to a simple type: If possible, the value of the variant will be converted to
the type that is being assigned to. This may fail: Assigning a variant containing a string to an integer
will fail unless the string represents a valid integer. In the following example, the first assignment
will work, the second will fail:

:xmp.
program testv3;

uses
  Variants;

var
  V: Variant;
  I: Integer;

begin
  V := '100';
  I := V;
  writeln('I : ', I);
  V := 'Something else';
  I := V;
  writeln('I : ', I);
end.
:exmp.

:p.
The first assignment will work, but the second will not, as :hp1.Something else:ehp1. cannot be converted
to a valid integer value. An EConvertError exception will be the result.
:p.
The result of an expression involving a variant will be of type variant again, but this can be assigned
to a variable of a different type - if the result can be converted to a variable of this type.
:p.
Note that expressions involving variants take more time to be evaluated, and should therefore be used
with caution. If a lot of calculations need to be made, it is best to avoid the use of variants.
:p.
When considering implicit type conversions (e.g. byte to integer, integer to double, char to string)
the compiler will ignore variants unless a variant appears explicitly in the expression.




.* ..................................................................
:h4 name='variant_types_andinterfaces'.Variants and interfaces
:p.
:note.Dispatch interface support for variants is currently broken in the compiler.
:p.
Variants can contain a reference to an interface - a normal interface (descending from IInterface)
or a dispatchinterface (descending from IDispatch). Variants containing a reference to a dispatch
interface can be used to control the object behind it: the compiler will use late binding to perform
the call to the dispatch interface: there will be no run-time checking of the function names and
parameters or arguments given to the functions. The result type is also not checked. The compiler
will simply insert code to make the dispatch call and retrieve the result.
:p.
This means basically, that you can do the following on Windows:

:xmp.
var
  W: Variant;
  V: String;
begin
  W := CreateOleObject('Word.Application');
  V := W.Application.Version;
  Writeln('Installed version of MS Word is : ', V);
end;
:exmp.

:p.
The line

:xmp.
V := W.Application.Version;
:exmp.

:p.
is executed by inserting the necessary code to query the dispatch interface stored in the variant W, and
execute the call if the needed dispatch information is found.



.* ==============================================================
:h2 name=variables.Variables
:p.
:ul.
:li.:link refid='variables_definition' reftype='hd'.Definition:elink.
:li.:link refid='variables_declaration' reftype='hd'.Declaration:elink.
:li.:link refid='variables_scope' reftype='hd'.Scope:elink.
:li.:link refid='variables_initializedvars' reftype='hd'.Initialized variables:elink.
:li.:link refid='variables_threadvars' reftype='hd'.Thread variables:elink.
:li.:link refid='variables_properties' reftype='hd'.Properties:elink.
:eul.

:h3 name='variables_definition'.Definition
:p.
Variables are explicitly named memory locations with a certain type. When assigning values to
variables, the Free Pascal compiler generates machine code to move the value to the memory location
reserved for this variable. Where this variable is stored depends on where it is declared:

:ul.
:li.Global variables are variables declared in a unit or program, but not inside a procedure or
function. They are stored in fixed memory locations, and are available during the whole execution
time of the program.
:li.Local variables are declared inside a procedure or function. Their value is stored on the
program stack, i.e. not at fixed locations.
:eul.

:p.
The &fpc. compiler handles the allocation of these memory locations transparantly, although
this location can be influenced in the declaration.
:p.
The &fpc. compiler also handles reading values from or writing values to the variables transparantly.
But even this can be explicitly handled by the programmer when using properties.
:p.
Variables must be explicitly declared when they are needed. No memory is allocated unless a variable
is declared. Using an variable identifier (for instance, a loop variable) which is not declared first, is
an error which will be reported by the compiler.



:h3 name='variables_declaration'.Declaration
:p.
The variables must be declared in a variable declaration section of a unit or a procedure or function.
It looks as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Variable declaration:ehp2.

&ra.&ra.── variable declaration  ── identifier ── : ── type ─┬──────────────────┬──&ra.
                                                       └─ :hp2.=:ehp2. ─ expression ─┘

&ra.───┬──────────────────────┬─ hintdirective ── :hp2.;:ehp2. ─────────────────────────────&ra.&la.
    └─ variable modifiers ─┘

&ra.&ra.── variable modifiers ─┬─┬───── :hp2.absolute:ehp2. ─┬─ integer expression ─┬───────────────────────┬┬─&ra.
                         ^ │                └─     identifier     ─┘                       ││
                         │ ├────────────────────── :hp2.; export:ehp2. ───────────────────────────────┤│
                         │ ├────────────────────── :hp2.; cvar:ehp2. ─────────────────────────────────┤│
                         │ ├─ :hp2.; external:ehp2. ─┬───────────────────┬┬──────────────────────────┬┤│
                         │ │              └─ string constant ─┘└─ :hp2.name:ehp2. ─ string constant ─┘││
                         │ └────────────────────── hintdirective ──────────────────────────┘│
                         └──────────────────────────────────────────────────────────────────┘

&ra.─────────────────────────────────────────────────────────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
This means that the following are valid variable declarations:

:xmp.
var
  curterm1: integer;

  curterm2: integer; cvar;
  curterm3: integer; cvar; external;

  curterm4: integer; external name 'curterm3';
  curterm5: integer; external 'libc' name 'curterm9';

  curterm6: integer absolute curterm1;

  curterm7: integer; cvar; export;
  curterm8: integer; cvar; public;
  curterm9: integer; export name 'me';
  curterm10: integer; public name 'ma';

  curterm11: integer = 1;
:exmp.

:p.
The difference between these declarations is as follows:

:ol.
:li.The first form (curterm1) defines a regular variable. The compiler manages everything by
itself.
:li.The second form (curterm2) declares also a regular variable, but specifies that the assembler
name for this variable equals the name of the variable as written in the source.
:li.The third form (curterm3) declares a variable which is located externally: the compiler will
assume memory is located elsewhere, and that the assembler name of this location is specified
by the name of the variable, as written in the source. The name may not be specified.
:li.The fourth form is completely equivalent to the third, it declares a variable which is stored
externally, and explicitly gives the assembler name of the location. If cvar is not used, the
name must be specified.
:li.The fifth form is a variant of the fourth form, only the name of the library in which the memory
is reserved is specified as well.
:li.The sixth form declares a variable (curterm6), and tells the compiler that it is stored in the
same location as another variable (curterm1).
:li.The seventh form declares a variable (curterm7), and tells the compiler that the assembler
label of this variable should be the name of the variable (case sensitive) and must be made
public. i.e. it can be referenced from other object files.
:li.The eighth form (curterm8) is equivalent to the seventh: 'public' is an alias for 'export'.
:li.The ninth and tenth form are equivalent: they specify the assembler name of the variable.
:li.The eleventh form declares a variable (curterm11) and initializes it with a value (1 in the
above case).
:eol.

:p.
Note that assembler names must be unique. It’s not possible to declare or export 2 variables with the
same assembler name.


:h3 name='variables_scope'.Scope
:p.
Variables, just as any identifier, obey the general rules of scope. In addition,
initialized variables are initialized when they enter scope:

:ul.
:li.Global initialized variables are initialized once, when the program starts.
:li.Local initialized variables are initialized each time the procedure is entered.
:eul.

:p.
Note that the behaviour for local initialized variables is different from the one
of a local typed constant. A local typed constant behaves like a global initialized
variable.


:h3 name='variables_initializedvars'.Initialized variables
:p.
By default, variables in Pascal are not initialized after their declaration. Any assumption that they
contain 0 or any other default value is erroneous: They can contain rubbish. To remedy this, the
concept of initialized variables exists. The difference with normal variables is that their declaration
includes an initial value, as can be seen in the diagram in the previous section.
:p.
Given the declaration:

:xmp.
var
  S: String = 'This is an initialized string';
:exmp.

:p.
The value of the variable following will be initialized with the provided value. The following is an
even better way of doing this:

:xmp.
const
  SDefault = 'This is an initialized string';
  
var
  S: String = SDefault;
:exmp.

:p.
Initialization is often used to initialize arrays and records. For arrays, the initialized elements must
be specified, surrounded by round brackets, and separated by commas. The number of initialized
elements must be exactly the same as the number of elements in the declaration of the type. As an
example:

:xmp.
var
  tt: array [1..3] of string[20] = ('ikke', 'gij', 'hij');
  ti: array [1..3] of Longint = (1,2,3);
:exmp.

:p.
For constant records, each element of the record should be specified, in
the form :hp1.Field: Value:ehp1., separated by semicolons, and surrounded by round
brackets.

:xmp.
type
  Point = record
    X, Y: Real
  end;
var
  Origin: Point = (X:0.0; Y:0.0);
:exmp.

:p.
The order of the fields in a constant record needs to be the same as in the type declaration, otherwise
a compile-time error will occur.

:nt.
It should be stressed that initialized variables are initialized when they come into scope, in difference
with typed constants, which are initialized at program start. This is also true for local initialized
variables. Local initialized are initialized whenever the routine is called. Any changes that occurred
in the previous invocation of the routine will be undone, because they are again initialized.
:ent.


:h3 name='variables_threadvars'.Thread variables
:p.
For a program which uses threads, the variables can be really global, i.e. the same for all threads, or
thread-local: this means that each thread gets a copy of the variable. Local variables (defined inside
a procedure) are always thread-local. Global variables are normally the same for all threads. A
global variable can be declared thread-local by replacing the :hp1.var:ehp1. keyword at the start of the variable
declaration block with :hp1.Threadvar:ehp1.:

:xmp.
Threadvar
  IOResult: Integer;
:exmp.

:p.
If no threads are used, the variable behaves as an ordinary variable. If threads are used then a copy is
made for each thread (including the main thread). Note that the copy is made with the original value
of the variable, :hp1.not:ehp1. with the value of the variable at the time the thread is started.
:p.
Threadvars should be used sparingly: There is an overhead for retrieving or setting the variable's
value. If possible at all, consider using local variables; they are always faster than thread variables.
:p.
Threads are not enabled by default. For more information about programming threads, see the chapter
on threads in the &progref..


:h3 name='variables_properties'.Properties
:p.
A global block can declare properties, just as they could be defined in a class. The difference is that
the global property does not need a class instance: there is only 1 instance of this property. Other
than that, a global property behaves like a class property. The read/write specifiers for the global
property must also be regular procedures, not methods.
:p.
The concept of a global property is specific to &fpc., and does not exist in &delphi.. :hp1.ObjFPC:ehp1.
mode is required to work with properties.
:p.
The concept of a global property can be used to 'hide' the location of the value, or to calculate the
value on the fly, or to check the values which are written to the property.
:p.
The declaration is as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Properties:ehp2.

&ra.&ra.── property definition  ── identifier ─┬──────────────────────┬─ property specifiers ──────&ra.&la.
                                         └─ property interface ─┘

&ra.&ra.── property interface ─┬───────────────────────────┬─ : ── type identifier ──&ra.
                         └─ property parameter list ─┘

&ra.───┬───────────────────────────┬─────────────────────────────────────────────&ra.&la.
    └─ :hp2.index:ehp2. ─ integerconstant ─┘

&ra.&ra.── property parameter list ── :hp2.[:ehp2. ─┬─ parameter declaration ─┬─ :hp2.]:ehp2. ────────────&ra.&la.
                                   ^─────────── ; ───────────┘

&ra.&ra.── property specifiers ─┬──────────────────┬┬───────────────────┬┬─────────────────────┬───&ra.&la.
                          └─ read specifier ─┘└─ write specifier ─┘└─ default specifier ─┘

&ra.&ra.── read specifier ── :hp2.read:ehp2. ── field or function ─────────────────────────────&ra.&la.

&ra.&ra.── write specifier ── :hp2.write:ehp2. ── field or function ───────────────────────────&ra.&la.

&ra.&ra.── default specifier ──┬─ :hp2.default:ehp2. ──┬────────────┬┬─────────────────────────&ra.&la.
                         │            └─ constant ─┘│
                         └──────── :hp2.nodefault:ehp2. ───────┘                      

&ra.&ra.── field or procedure ──┬─── field identifier ───┬──────────────────────────&ra.&la.
                          └─ procedure identifier ─┘

&ra.&ra.── field or function ──┬─── field identifier ───┬───────────────────────────&ra.&la.
                         └─ function identifier ──┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
The following is an example:

:xmp.
unit testprop;

{$mode objfpc}{$H+}

interface

function GetMyInt: Integer;
procedure SetMyInt(AValue: Integer);

property MyProp: Integer read GetMyInt write SetMyInt;
  
implementation

uses
  sysutils;

var
  FMyInt: Integer;  
  
function GetMyInt: Integer;
begin
  Result := FMyInt;
end;

procedure SetMyInt(AValue: Integer);
begin
  if ((AValue mod 2) = 1) then
    raise Exception.Create('MyProp can only contain even value');
  FMyInt := AValue;  
end;

end.
:exmp.

:p.
The read/write specifiers can be hidden by declaring them in another unit which must be in the :hp1.uses:ehp1.
clause of the unit. This can be used to hide the read/write access specifiers for programmers, just as
if they were in a private section of a class (discussed below). For the previous example, this could
look as follows:

:xmp.
unit testrw;

{$mode objfpc}{$H+}

interface

function GetMyInt: Integer;
procedure SetMyInt(AValue: Integer);

implementation

uses
  sysutils;

var
  FMyInt: Integer;  
  
function GetMyInt: Integer;
begin
  Result := FMyInt;
end;

procedure SetMyInt(AValue: Integer);
begin
  If ((AValue mod 2) = 1) then
    Raise Exception.Create('Only even values are allowed');
  FMyInt := AValue;  
end;

end.
:exmp.

:p.
The unit testprop would then look like:

:xmp.
unit testprop;

{$mode objfpc}{$H+}

interface

uses
  testrw;

property MyProp: Integer read GetMyInt write SetMyInt;

implementation

end.  
:exmp.

:p.
More information about properties can be found in the :link refid='classes' reftype='hd'.Classes chapter:elink..




.* ==============================================================
:h2 name=objects.Objects
:p.
:ul.
:li.:link refid='objects_declaration' reftype='hd'.Declaration:elink.
:li.:link refid='objects_fields' reftype='hd'.Fields:elink.
:li.:link refid='objects_staticfields' reftype='hd'.Static Fields:elink.
:li.:link refid='objects_constructordestructor' reftype='hd'.Constructors and Destructors:elink.
:li.:link refid='objects_methods' reftype='hd'.Methods:elink.
:li.:link refid='objects_visibility' reftype='hd'.Visibility:elink.
:eul.

:h3 name='objects_declaration'.Declaration
:p.
&fpc. supports object oriented programming. In fact, most of the compiler is written using
objects. Here we present some technical questions regarding object oriented programming in &fpc.

:p.
Objects should be treated as a special kind of record. The record contains all the fields that are
declared in the objects definition, and pointers to the methods that are associated to the objects’ type.

:p.
An object is declared just as a record would be declared; except that now, procedures and functions
can be declared as if they were part of the record. Objects can “inherit” fields and methods from
“parent” objects. This means that these fields and methods can be used as if they were included in
the objects declared as a “child” object.

:p.
Furthermore, a concept of visibility is introduced: fields, procedures and functions can be declared as
public, protected or private. By default, fields and methods are public, and are exported
outside the current unit.

:p.
Fields or methods that are declared private are only accessible in the current unit: their scope is
limited to the implementation of the current unit.

:p.
The prototype declaration of an object is as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Object types:ehp2.

&ra.&ra.───┬──────────┬─ :hp2.object:ehp2. ─┬────────────┬─┬─ component list ─┬─ end ────────────&ra.&la.
     └─ :hp2.packed:ehp2. ─┘          └─ heritage ─┘ ^──────────────────┘

&ra.&ra.─── heritage ── ( ── object type identifier ── ) ─────────────────────────────&ra.&la.

&ra.&ra.─── component list ─┬───────────────────────────────┬┬──────────────────────┬─&ra.
                      └─ object visibility specifier ─┘└┬─ field definition ─┬┘
                                                        ^────────────────────┘

&ra.───┬───────────────────────┬───────────────────────────────────────────────────&ra.&la.
    └┬─ method definition ─┬┘
     ^─────────────────────┘

&ra.&ra.─── field definition ── identifier list ── : ── type ── ; ──┬───────────┬─────&ra.&la.
                                                              └─ :hp2.static;:ehp2. ─┘

&ra.&ra.─── object visibility specifier ─┬─  :hp2.private:ehp2.  ─┬──────────────────────────────&ra.&la.
                                   ├─ :hp2.protected:ehp2. ─┤
                                   └─  :hp2.public:ehp2.   ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

.* TODO:  The "method definition" is missing from the above graph.

:p.
As can be seen, as many private and public blocks as needed can be declared.

:p.
The following is a valid definition of an object:

:xmp.
type
  TObj = object
  private
    Caption: ShortString;
  public
    constructor init;
    destructor done;
    procedure SetCaption(AValue: String);
    property GetCaption: String;
  end;
:exmp.

:p.
It contains a constructor/destructor pair, and a method to get and set a
caption. The :hp1.Caption:ehp1. field is private to the object: it cannot be accessed
outside the unit in which :hp1.TObj:ehp1. is declared.

:note.
In MacPas mode, the :hp1.Object:ehp1. keyword is replaced by the :hp1.class:ehp1.
keyword for compatibility with other pascal compilers available on the Mac. 
That means that objects cannot be used in MacPas mode.

:nt.
&fpc. also supports the packed object. This is the same as an object, only
the elements (fields) of the object are byte-aligned, just as in the packed
record. The declaration of a packed object is similar to the declaration
of a packed record:

:xmp.
type
  TObj = packed object
    constructor init;
    ...
  end;
  Pobj = ^TObj;

var
  pp: Pobj;
:exmp.

:p.
Similarly, the :hp1.{$PackRecords}:ehp1. directive acts on objects as well.
:ent.


:h3 name='objects_fields'.Fields
:p.
Object Fields are like record fields. They are accessed in the same way as
a record field  would be accessed: by using a qualified identifier. Given the
following declaration:

:xmp.
type
  TAnObject = object
    AField: Longint;
    procedure AMethod;
  end;

var
  AnObject: TAnObject;
:exmp.

:p.
then the following would be a valid assignment:

:xmp.
AnObject.AField := 0;
:exmp.

:p.
Inside methods, fields can be accessed using the short identifier:

:xmp.
procedure TAnObject.AMethod;
begin
  ...
  AField := 0;
  ...
end;
:exmp.

:p.
Or, one can use the :hp1.self:ehp1. identifier. The :hp1.self:ehp1. identifier refers
to the current instance of the object:

:xmp.
procedure TAnObject.AMethod;
begin
  ...
  self.AField := 0;
  ...
end;
:exmp.

:p.
One cannot access fields that are in a private or protected sections of an object from
outside the objects’ methods. If this is attempted anyway, the compiler will complain about
an unknown identifier.

:p.
It is also possible to use the :hp1.with:ehp1. statement with an object instance,
just as with a record:

:xmp.
with AnObject do
begin
  AField := 12;
  AMethod;
end;
:exmp.

:p.
In this example, between the :hp1.begin:ehp1. and :hp1.end:ehp1., it is as if
:hp1.AnObject:ehp1. was prepended to the :hp1.AField:ehp1. and :hp1.AMethod:ehp1.
identifiers. More about this in :link refid='statements_structured_with' reftype='hd'.The With Statement:elink..


:h3 name='objects_staticfields'.Static Fields
:p.
When the :hp1.{$STATIC ON}:ehp1. directive is active, then an object
can contain static fields: these fields are global to the object type, and act
like global variables, but are known only as part of the object. They can be
referenced from within the objects methods, but can also be referenced from
outside the object by providing the fully qualified name.

:p.
For instance, the output of the following program:

:xmp.
{$static on}
type
  cl = object
    l: longint; static;
  end;

var
  c1, c2: cl;
begin
  c1.l := 2;
  writeln(c2.l);
  c2.l := 3;
  writeln(c1.l);
  writeln(cl.l);
end.
:exmp.

:p.
will be the following

:xmp.
2
3
3
:exmp.

:p.
Note that the last line of code references the object type itself (cl), 
and not an instance of the object (cl1 or cl2).


:h3 name='objects_constructordestructor'.Constructors and Destructors
:p.
As can be seen in the syntax diagram for an object declaration, &fpc. supports
constructors and destructors. The programmer is responsible for calling the
constructor and the destructor explicitly when using objects.

:p.
The declaration of a constructor or destructor is as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Constructors and Destructors:ehp2.

&ra.&ra.─── constructor declaration ── constructor header ── ; ── subroutine block ──────&ra.&la.

&ra.&ra.─── destructor declaration ── destructor header ── ; ── subroutine block ────────&ra.&la.

&ra.&ra.─── constructor header ── :hp2.constructor:ehp2. ──┬───────── identifier ──────────┬────────&ra.
                                          └─ qualified method identifier ─┘

&ra.──── formal parameter list ───────────────────────────────────────────────────────&ra.&la.

&ra.&ra.─── destructor header ── :hp2.destructor:ehp2. ──┬───────── identifier ──────────┬──────────&ra.
                                        └─ qualified method identifier ─┘

&ra.──── formal parameter list ───────────────────────────────────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
A constructor/destructor pair is :hp1.required:ehp1. if the object uses virtual methods.
The reason is that for an object with virtual methods, some internal
housekeeping must be done: this housekeeping is done by the
constructor [A pointer to the VMT must be set up].

:p.
In the declaration of the object type, a simple identifier should be used
for the name of the constuctor or destructor. When the constructor or destructor
is implemented, A qualified method identifier should be used,
i.e. an identifier of the form :hp1.objectidentifier.methodidentifier:ehp1..

:p.
&fpc. supports also the extended syntax of the :hp1.New:ehp1. and :hp1.Dispose:ehp1.
procedures. In case a dynamic variable of an object type must be allocated
the constructor’s name can be specified in the call to :hp1.New:ehp1..
The :hp1.New:ehp1. is implemented as a function which returns a pointer to the
instantiated object. Consider the following declarations:

:xmp.
type
  TObj = object;
    constructor init;
    ...
  end;
  Pobj = ^TObj;

var
  PP: Pobj;
:exmp.

:p.
Then the following 3 calls are equivalent:

:xmp.
pp := new (Pobj,Init);
:exmp.

:p.
and

:xmp.
new(pp,init);
:exmp.

:p.
and also

:xmp.
new (pp);
pp^.init;
:exmp.

:p.
In the last case, the compiler will issue a warning that the
extended syntax of :hp1.New:ehp1. and :hp1.Dispose:ehp1. must be used to generate instances of an
object. It is possible to ignore this warning, but it’s better programming practice to
use the extended syntax to create instances of an object.
Similarly, the :hp1.Dispose:ehp1. procedure accepts the name of a destructor. The
destructor will then be called, before removing the object from the heap.

:p.
In view of the compiler warning remark, the following chapter presents the
&delphi. approach to object-oriented programming, and may be considered a
more natural way of object-oriented programming.


:h3 name='objects_methods'.Methods
:p.
Object methods are just like ordinary procedures or functions, only they
have an implicit extra parameter: :hp1.self:ehp1.. Self points to the object
with which the method was invoked.
When implementing methods, the fully qualified identifier must be given
in the function header. When declaring methods, a normal identifier must be
given.


:h4 name='objects_methods_declaration'.Declaration
:p.
The declaration of a method is much like a normal function or procedure
declaration, with some additional specifiers, as can be seen from the
following diagram, which is part of the object declaration:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Methods:ehp2.

&ra.&ra.─── method definition ─┬─  function header   ─┬─ ; ── method directives ─────────────&ra.&la.
                         ├─  procedure header  ─┤
                         ├─ constructor header ─┤
                         └─ destructor header  ─┘

&ra.&ra.─── method directives ─┬──────────────────────────────────┬┬─────────────────────┬───&ra.&la.
                         └─ :hp2.virtual:ehp2. ─ ; ─┬────────────────┬─┘└─ call modifier ─ ; ─┘
                                         └─ :hp2.abstract:ehp2. ─ ; ─┘
                                               
└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
from the point of view of declarations, :hp1.Method definitions:ehp1. are 
normal function or procedure declarations.
Contrary to &tp. and &delphi., fields can be declared after methods in the same 
block, i.e. the following will generate an error when compiling with &delphi.
or &tp., but not with &fpc.:

:xmp.
type 
  MyObj = object
    procedure Doit;
    Field: Longint;
  end;
:exmp.



:h4 name='objects_method_invocation'.Method invocation
:p.
Methods are called just as normal procedures are called, only they have an
object instance identifier prepended to them (see also :link refid='statements' reftype='hd'.Statements:elink.).
To determine which method is called, it is necessary to know the type of
the method. We treat the different types in what follows.

:ul.
:li.
:link refid='objects_static_methods' reftype='hd'.Static Methods:elink.
:li.
:link refid='objects_virtual_methods' reftype='hd'.Virtual Methods:elink.
:li.
:link refid='objects_abstract_methods' reftype='hd'.Abstract Methods:elink.
:eul.

:h5 name='objects_static_methods'.Static Methods
:p.
Static methods are methods that have been declared without a abstract or virtual keyword.
When calling a static method, the declared (i.e. compile time) method of the object is used. For
example, consider the following declarations:

:xmp.
type
  TParent = object
    ...
    procedure Doit;
    ...
    end;

  PParent = ^TParent;

  TChild = Object(TParent)
    ...
    procedure Doit;
    ...
    end;

  PChild = ^TChild;
:exmp.

:p.
As it is visible, both the parent and child objects have a method
called :hp1.Doit:ehp1.. Consider now the following declarations and calls:

:xmp.
var
  ParentA, ParentB: PParent;
  Child: PChild;
begin
   ParentA := New(PParent,Init);
   ParentB := New(PChild,Init);
   Child := New(PChild,Init);
   ParentA^.Doit;
   ParentB^.Doit;
   Child^.Doit;
:exmp.

:p.
Of the three invocations of :hp1.Doit:ehp1., only the last one will call
:hp1.TChild.Doit:ehp1., the other two calls will call :hp1.TParent.Doit:ehp1..
This is because for static methods, the compiler determines at compile
time which method should be called. Since :hp1.ParentB:ehp1. is of type
:hp1.TParent:ehp1., the compiler decides that it must be called with
:hp1.TParent.Doit:ehp1., even though it will be created as a :hp1.TChild:ehp1..
There may be times when the method that is actually called should
depend on the actual type of the object at run-time. If so, the method
cannot be a static method, but must be a virtual method.


:h5 name='objects_virtual_methods'.Virtual Methods
:p.
To remedy the situation in the previous section, :hp1.virtual:ehp1. methods are
created. This is simply done by appending the method declaration with the
:hp1.virtual:ehp1. modifier. The descendent object can then override the method
with a new implementation by re-declaring the method (with the same
parameter list) using the :hp1.virtual:ehp1. keyword.

:p.
Going back to the previous example, consider the following alternative
declaration:

:xmp.
type
  TParent = object
    ...
    procedure Doit; virtual;
    ...
    end;

  PParent = ^TParent;

  TChild = Object(TParent)
    ...
    procedure Doit; virtual;
    ...
    end;

  PChild = ^TChild;
:exmp.

:p.
As it is visible, both the parent and child objects have a method called
:hp1.Doit:ehp1.. Consider now the following declarations and calls:

:xmp.
var 
  ParentA, ParentB: PParent;
  Child: PChild;
begin
   ParentA := New(PParent, Init);
   ParentB := New(PChild, Init);
   Child := New(PChild, Init);
   ParentA^.Doit;
   ParentB^.Doit;
   Child^.Doit;
:exmp.

:p.
Now, different methods will be called, depending on the actual run-time type
of the object. For :hp1.ParentA:ehp1., nothing changes, since it is created as
a :hp1.TParent:ehp1. instance. For :hp1.Child:ehp1., the situation also doesn't
change: it is again created as an instance of :hp1.TChild:ehp1..

:p.
For :hp1.ParentB:ehp1. however, the situation does change: Even though it was
declared as a :hp1.TParent:ehp1., it is created as an instance of :hp1.TChild:ehp1..
Now, when the program runs, before calling :hp1.Doit:ehp1., the program
checks what the actual type of :hp1.ParentB:ehp1. is, and only then decides which
method must be called. Seeing that :hp1.ParentB:ehp1. is of type :hp1.TChild:ehp1.,
:hp1.TChild.Doit:ehp1. will be called. The code for this run-time checking of
the actual type of an object is inserted by the compiler at compile time.

The :hp1.TChild.Doit:ehp1. is said to "override" the
:hp1.TParent.Doit:ehp1.. It is possible to acces the :hp1.TParent.Doit:ehp1. from
within the :hp1.TChild.Doit:ehp1., with the :hp1.inherited:ehp1. keyword:

:xmp.
procedure TChild.Doit;
begin
  inherited Doit;
  ...
end;
:exmp.

:p.
In the above example, when :hp1.TChild.Doit:ehp1. is called, the first thing it
does is call :hp1.TParent.Doit:ehp1.. The inherited keyword cannot be used in
static methods, only on virtual methods.

:p.
To be able to do this, the compiler keeps - per object type - a table with
virtual methods: the VMT (Virtual Method Table). This is simply a table 
with pointers to each of the virtual methods: each virtual method has its
fixed location in this table (an index). The compiler uses this table to 
look up the actual method that must be used. When a descendent object
overrides a method, the entry of the parent method is overwritten in the
VMT. More information about the VMT can be found in :link reftype=hd database='prog.inf' refid=0.&progref.:elink..

:p.
As remarked earlier, objects that have a VMT must be initialized with a
constructor: the object variable must be initialized with a pointer to
the VMT of the actual type that it was created with.


:h5 name='objects_abstract_methods'.Abstract Methods
:p.
An abstract method is a special kind of virtual method. A method that is
declared :hp1.abstract:ehp1. does not have an implementation for this method. 
It is up to inherited objects to override and implement this method.

:p.
From this it follows that a method can not be abstract if it is not virtual 
(this can be seen from the syntax diagram). A second consequence is that 
an instance of an object that has an abstract method cannot be created
directly.

:p.
The reason is obvious: there is no method where the compiler could jump to!
A method that is declared :hp1.abstract:ehp1. does not have an implementation for
this method. It is up to inherited objects to override and implement this
method. Continuing our example, take a look at this:

:xmp.
type
  TParent = object
    ...
    procedure Doit; virtual; abstract;
    ...
    end;

  PParent=^TParent;

  TChild = Object(TParent)
    ...
    procedure Doit;virtual;
    ...
    end;

  PChild = ^TChild;
:exmp.

:p.
As it is visible, both the parent and child objects have a method called
:hp1.Doit:ehp1.. Consider now the following declarations and calls:

:xmp.
var
  ParentA, ParentB: PParent;
  Child: PChild;
begin
   ParentA := New(PParent, Init);
   ParentB := New(PChild, Init);
   Child := New(PChild, Init);
   ParentA^.Doit;
   ParentB^.Doit;
   Child^.Doit;
:exmp.

:p.
First of all, Line 3 will generate a compiler error, stating that one cannot
generate instances of objects with abstract methods: The compiler has
detected that :hp1.PParent:ehp1. points to an object which has an abstract
method. Commenting line 3 would allow compilation of the program.

:nt.
If an abstract method is overridden, The parent method cannot be called
with :hp1.inherited:ehp1., since there is no parent method; The compiler
will detect this, and complain about it, like this:

:xmp.
testo.pp(32,3) Error: Abstract methods can't be called directly
:exmp.

:p.
If, through some mechanism, an abstract method is called at run-time,
then a run-time error will occur. (run-time error 211, to be precise)
:ent.

:h3 name='objects_visibility'.Visibility
:p.
For objects, three visibility specifiers exist: :hp1.private:ehp1., :hp1.protected:ehp1. and
:hp1.public:ehp1.. If a visibility specifier is not specified, :hp1.public:ehp1.
is assumed. Both methods and fields can be hidden from a programmer by putting them
in a :hp1.private:ehp1. section. The exact visibility rule is as follows:

:parml tsize=15 break=none.
:pt.:hp2.Private:ehp2.
:pd. All fields and methods that are in a :hp1.private:ehp1. block,
can only be accessed in the module (i.e. unit or program) that contains
the object definition.
They can be accessed from inside the object's methods or from outside them
e.g. from other objects' methods, or global functions.

:pt.:hp2.Protected:ehp2.
:pd. Is the same as :hp1.Private:ehp1., except that the members of
a :hp1.Protected:ehp1. section are also accessible to descendent types, even if
they are implemented in other modules.

:pt.:hp2.Public:ehp2.
:pd. Fields and methods are always accessible, from everywhere.
Fields and methods in a :hp1.Public:ehp1. section behave as though they were part
of an ordinary :hp1.record:ehp1. type.
:eparml.



.* ==============================================================
:h2 name=classes.Classes
:p.
In the &delphi. approach to Object Oriented Programming, everything revolves
around the concept of 'Classes'.  A class can be seen as a pointer to an
object, or a pointer to a record, with methods associated with it.

:p.
The difference between objects and classes is mainly that an object
is allocated on the stack, as an ordinary record would be, and that
classes are always allocated on the heap. In the following example:

:xmp.
var
  A: TSomeObject; // an Object
  B: TSomeClass;  // a Class
:exmp.

:p.
The main difference is that the variable A will take up as much 
space on the stack as the size of the object (TSomeObject). The
variable B, on the other hand, will always take just the size of
a pointer on the stack. The actual class data is on the heap.

:p.
From this, a second difference follows: a class must :hp2.always:ehp2. be initialized
through its constructor, whereas for an object, this is not necessary.
Calling the constructor allocates the necessary memory on the heap for the
class instance data. 

:nt.
In earlier versions of &fpc. it was necessary, in order to use classes,
to put the :hp1.objpas:ehp1. unit in the uses clause of a unit or program.
:hp1. This is no longer needed as of version 0.99.12.:ehp1. As of this version,
the unit will be loaded automatically when the :hp2.-MObjfpc:ehp2. or
:hp2.-MDelphi:ehp2. options are specified, or their corresponding directives are
used:

:xmp.
{$mode objfpc}
{$mode delphi}
:exmp.

:p.
In fact, the compiler will give a warning if it encounters the
:hp1.objpas:ehp1. unit in a uses clause.
:ent.

:h3.Class definitions
:p.
The prototype declaration of a class is as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Class types:ehp2.

&ra.&ra.───┬──────────┬─ :hp2.class:ehp2. ─┬────────────┬─┬────────────────────┬─ end ──────────────&ra.&la.
     └─ :hp2.packed:ehp2. ─┘         └─ heritage ─┘ └┬ component list ─┬─┘
                                          ^─────────────────┘

&ra.&ra.─── heritage ── ( ── class type identifier ──┬──────────────────────────┬─ ) ────&ra.&la.
                                               └─ implemented interfaces ─┘ 

&ra.&ra.─── implemented interfaces ─┬ , ── interface identifier ──┬──────────────────────&ra.&la.
                              ^─────────────────────────────┘ 

&ra.&ra.─── component list ─┬────────────────────────┬┬──────────────────────┬─&ra.
                      └─ visibility specifier ─┘└┬─ field definition ─┬┘
                                                 ^────────────────────┘

&ra.───┬───────────────────────────┬──────────────────────────────────────────────────&ra.&la.
    └┬┬── method definition ──┬┬┘
     │└─ property definition ─┘│
     ^─────────────────────────┘

&ra.&ra.─── field definition ── identifier list ── : ── type ── ; ──┬───────────┬────────&ra.&la.
                                                              └─ :hp2.static;:ehp2. ─┘

&ra.&ra.─── method definition ─┬┬─────────┬┬─ function header ──┬┬── ; ────────&ra.
                         │└─ :hp2.class:ehp2. ─┘└─ procedure header ─┘│
                         ├───── constructor header ────────┤
                         └───── destructor header ─────────┘

&ra.────┬─────────────────────────────────────────┬┬──────────────────────┬───────────&ra.&la.
     └─┬┬─ :hp2.virtual:ehp2. ─┬┬──────────────────┬┬─ ; ─┘└─ call modifiers ─ ; ─┘
       │└─ :hp2.dynamic:ehp2. ─┘└─ ; ── :hp2.abstract:ehp2. ──┘│
       ├──────── :hp2.override:ehp2. ───────────────┤
       └─ :hp2.message:ehp2. ─┬─ integer constant ─┬┘
                   └─ string constant ──┘
                         
&ra.&ra.─── class visibility specifier ─┬─  :hp2.private:ehp2.  ─┬──────────────────────────────────&ra.&la.
                                  ├─ :hp2.protected:ehp2. ─┤
                                  ├─  :hp2.public:ehp2.   ─┤
                                  └─ :hp2.published:ehp2. ─┘

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
:nt.
In MacPas mode, the :hp1.Object:ehp1. keyword is replaced by the :hp1.class:ehp1.
keyword for compatibility with other pascal compilers available on the Mac. 
That means that in MacPas mode, the reserved word 'class' in the above
diagram may be replaced by the reserved word 'object'.
:ent.

:p.
In a class declaration, as many :hp1.private:ehp1., :hp1.protected:ehp1., :hp1.published:ehp1.
and :hp1.public:ehp1. blocks as needed can be used: the various blocks can be
repeated, and there is no special order in which they must appear.

:p.
Methods are normal function or procedure declarations.
As can be seen, the declaration of a class is almost identical to the
declaration of an object. The real difference between objects and classes
is in the way they are created (see further in this chapter).
The visibility of the different sections are as follows:

:parml tsize=15 break=none.
:pt.:hp2.Private:ehp2.
:pd. All fields and methods that are in a :hp1.private:ehp1. block, can
only be accessed in the module (i.e. unit) that contains the class definition.
They can be accessed from inside the classes' methods or from outside them
(e.g. from other classes' methods).

:pt.:hp2.Protected:ehp2.
:pd.Is the same as :hp1.Private:ehp1., except that the members of
a :hp1.Protected:ehp1. section are also accessible to descendent types, even if
they are implemented in other modules.

:pt.:hp2.Public:ehp2.
:pd.sections are always accessible.

:pt.:hp2.Published:ehp2.
:pd.Is the same as a :hp1.Public:ehp1. section, but the compiler
generates also type information that is needed for automatic streaming of
these classes if the compiler is in the {$M+} state. Fields defined in 
a :hp1.published:ehp1. section must be of class type.
Array properties cannot be in a published section.

:eparml.

:p.
In the syntax diagram, it can be seen that a class can list implemented
interfaces. This feature will be discussed in the next chapter.

:p.
Classes can contain :hp1.class:ehp1. methods: these are functions that do not
require an instance. The :hp1.self:ehp1. identifier is valid in such methods, 
but refers to the class pointer (the VMT). 

:p.
Similar to objects, if the {$STATIC ON} directive is active, then a class
can contain static fields: these fields are global to the class, and act
like global variables, but are known only as part of the class. They can be
referenced from within the classes' methods, but can also be referenced from
outside the class by providing the fully qualified name.

:p.
For instance, the output of the following program:

:xmp.
{$mode objfpc}
{$static on}
type
  TMyClass = class
    l: longint; static;
  end;
var
  c1, c2: TMyClass;
begin
  c1 := TMyClass.create;
  c2 := TMyClass.create;
  c1.l := 2;
  writeln(c2.l);
  c2.l := 3;
  writeln(c1.l);
  writeln(TMyClass.l);
end.
:exmp.

:p.
will be the following

:xmp.
2
3
3
:exmp.

:p.
Note that the last line of code references the class type itself (TMyClass), 
and not an instance of the class (c1 or c2).

:p.
It is also possible to define class reference types:


:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Class reference type:ehp2.

&ra.&ra.─── :hp2.class of:ehp2. ── classtype ──────────────────────────────────────────────────&ra.&la.

└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
Class reference types are used to create instances of a certain class, which
is not yet known at compile time, but which is specified at run time. 
Essentially, a variable of a class reference type contains a pointer to the
definition of the speficied class. This can be used to construct an instance 
of the class corresponding to the definition, or to check inheritance. 
The following example shows how it works:

:xmp.
type
  TComponentClass = class of TComponent;

function CreateComponent(AClass: TComponentClass; AOwner: TComponent): TComponent;
begin
  // ...
  Result := AClass.Create(AOwner);
  // ...
end;
:exmp.

:p.
This function can be passed a class reference of any class that descends
from :hp1.TComponent:ehp1.. The following is a valid call:

:xmp.
var
  C: TComponent;
begin
  C := CreateComponent(TEdit, Form1);
end;
:exmp.

:p.
On return of the :hp1.CreateComponent:ehp1. function, C will contain an 
instance of the class TEdit. Note that the following call will fail to
compile:

:xmp.
var
  C: TComponent;
begin
  C := CreateComponent(TStream, Form1);
end;
:exmp.

:p.
because :hp1.TStream:ehp1. does not descend from :hp1.TComponent:ehp1., and
:hp1.AClass:ehp1. refers to a :hp1.TComponent:ehp1. class. The compiler can
(and will) check this at compile time, and will produce an error.

:p.
References to classes can also be used to check inheritance:
:xmp.
type
  TMinClass = class of TMyClass;
  TMaxClass = class of TMyClassChild;

function CheckObjectBetween(Instance: TObject): boolean;
begin
  if not (Instance is TMinClass) 
     or ((Instance is TMaxClass) and (Instance.ClassType <> TMaxClass)) then
    raise Exception.Create('SomeError')
end;
:exmp.

:p.
The above example will raise an exception if the passed instance
is not a descendent of :hp1.TMinClass:ehp1. or a descendent if :hp1.TMaxClass:ehp1..

:p.
More about instantiating a class can be found in the next section.



:h3.Class instantiation
:p.
Classes must be created using one of their constructors (there can be
multiple constructors). Remember that a class is a pointer to an object on
the heap. When a variable of some class is declared, the compiler just 
allocates room for this pointer, not the entire object. The constructor of
a class returns a pointer to an initialized instance of the object on the
heap. So, to initialize an instance of some class, one would do the following:

:xmp.
  ClassVar := ClassType.ConstructorName;
:exmp.

:p.
The extended syntax of :hp1.new:ehp1. and :hp1.dispose:ehp1. can :hp2.not:ehp2. be used to
instantiate and destroy class instances.
That construct is reserved for use with objects only.
Calling the constructor will provoke a call to :hp1.getmem:ehp1., to allocate
enough space to hold the class instance data.
After that, the constuctor's code is executed.
The constructor has a pointer to its data, in :hp1.self:ehp1..

:nt.
:lm margin=5.
:ul.
:li. The {$PackRecords} directive also affects classes.
i.e. the alignment in memory of the different fields depends on the
value of  the {$PackRecords} directive.
:li. Just as for objects and records, a packed class can be declared.
This has the same effect as on an object, or record, namely that the
elements are aligned on 1-byte boundaries. i.e. as close as possible.
:li. :hp1.SizeOf(class):ehp1. will return the same as :hp1.SizeOf(Pointer):ehp1., 
since a class is but a pointer to an object. To get the size of the class 
instance data, use the :hp1.TObject.InstanceSize:ehp1. method.
:eul.
:ent.



:h3.Methods
:p.
:ul.
:li.:link refid='class_declaration' reftype='hd'.Declaration:elink.
:li.:link refid='class_invocation' reftype='hd'.Invocation:elink.
:li.:link refid='class_virtual_methods' reftype='hd'.Virtual methods:elink.
:li.:link refid='class_class_methods' reftype='hd'.Class methods:elink.
:li.:link refid='class_message_methods' reftype='hd'.Message methods:elink.
:li.:link refid='class_using_inherited' reftype='hd'.Using inherited:elink.
:eul.


:h4 name='class_declaration'.Declaration
:p.
Declaration of methods in classes follows the same rules as method
declarations in objects:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Methods:ehp2.

&ra.&ra.─── method definition ─┬─  function header   ─┬─ ; ── method directives ─────────────────&ra.&la.
                         ├─  procedure header  ─┤
                         ├─ constructor header ─┤
                         └─ destructor header  ─┘

&ra.&ra.─── method directives ──┬──────────────────────────────────┬┬──────────────────────┬─────&ra.&la.
                          ├─ :hp2.virtual:ehp2. ─ ; ─┬─────────────────┬┘└─ call modifiers ─ ; ─┘
                          │               └─ :hp2.abstract:ehp2. ─ ; ──┤
                          ├──────── :hp2.reintroduce:ehp2. ─ ; ────────┤
                          └─ :hp2.message:ehp2. ─ constant expression ─┘
                         
└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.


:h4 name='class_invocation'.Invocation
:p.
Method invocation for classes is no different than for objects. The
following is a valid method invocation:

:xmp.
var
  AnObject: TAnObject;
begin
  AnObject := TAnObject.Create;
  AnObject.AMethod;
:exmp.



:h4 name='class_virtual_methods'.Virtual methods
:p.
Classes have virtual methods, just as objects do. There is however a
difference between the two. For objects, it is sufficient to redeclare the
same method in a descendent object with the keyword :hp1.virtual:ehp1. to
override it. For classes, the situation is different: virtual methods 
:hp2.must:ehp2. be overridden with the :hp1.override:ehp1. keyword. Failing to do so,
will start a :hp2.new:ehp2. batch of virtual methods, hiding the previous
one.  The :hp1.Inherited:ehp1. keyword will not jump to the inherited method, if
:hp1.Virtual:ehp1. was used.

:p.
The following code is :hp2.wrong:ehp2.:

:xmp.
type 
  TObjParent = class
    procedure MyProc; virtual;
  end;
  
  ObjChild = class(TObjParent)
    procedure MyProc; virtual;
  end;
:exmp.

:p.
The compiler will produce a warning:

:xmp.
Warning: An inherited method is hidden by OBJCHILD.MYPROC
:exmp.

:p.
The compiler will compile it, but using :hp1.Inherited:ehp1. can
produce strange effects.

:p.
The correct declaration is as follows:

:xmp.
type 
  TObjParent = class
    procedure MyProc; virtual;
  end;
  
  TObjChild  = Class(TObjParent)
    procedure MyProc; override;
  end;
:exmp.

:p.
This will compile and run without warnings or errors.

:p.
If the virtual method should really be replaced with a method with the 
same name, then the :hp1.reintroduce:ehp1. keyword can be used:

:xmp.
type 
  TObjParent = class
    procedure MyProc; virtual;
  end;
  
  TObjChild  = Class(TObjParent)
    procedure MyProc; reintroduce;
  end;
:exmp.

:p.
This new method is no longer virtual.

:p.
To be able to do this, the compiler keeps - per class type - a table with
virtual methods: the VMT (Virtual Method Table). This is simply a table 
with pointers to each of the virtual methods: each virtual method has its
fixed location in this table (an index). The compiler uses this table to 
look up the actual method that must be used at runtime. When a descendent object
overrides a method, the entry of the parent method is overwritten in the
VMT. More information about the VMT can be found in the &progref..

:nt.
The keyword 'virtual' can be replaced with the 'dynamic' keyword: dynamic
methods behave the same as virtual methods. Unlike in &delphi., in &fpc. the
implementation of dynamic methods is equal to the implementation of virtual
methods.
:ent.



:h4 name='class_class_methods'.Class methods
:p.
Class methods are identified by the keyword :hp1.Class:ehp1. in front of the
procedure or function declaration, as in the following example:

:xmp.
  class function ClassName: string;
:exmp.

:p.
Class methods are methods that do not have an instance (i.e. Self does not
point to a class instance) but which follow the scoping and inheritance 
rules of a class. They can be used to return information about the current
class, for instance for registration or use in a class factory. Since no 
instance is available, no information available in instances can be used.

:p.
Class methods can be called from inside a regular method, but can also be called 
using a class identifier:

:xmp.
var
  AClass: TClass;
begin
  ...
  if CompareText(AClass.ClassName,'TCOMPONENT')=0 then
  ...
:exmp.

:p.
But calling them from an instance is also possible:

:xmp.
var
  MyClass: TObject;
begin
  ...
  MyClass := TObject.Create;
  if CompareText(MyClass.ClassName, 'TCOMPONENT')=0 then
  ...
:exmp.

:p.
The reverse is not possible: Inside a class method, the Self identifier 
points to the VMT table of the class. No fields, properties or 
regular methods are available inside a class method. Accessing a regular 
property or method will result in a compiler error. 

:p.
Note that class methods can be virtual, and can be overridden.

:p.
Class methods cannot be used as read or write specifiers for a
property.



:h4 name='class_message_methods'.Message methods
:p.
New in classes are :hp1.message:ehp1. methods. Pointers to message methods are
stored in a special table, together with the integer or string constant that
they were declared with. They are primarily intended to ease programming of
callback functions in several GUI toolkits, such as Win32 or
GTK. In difference with &delphi., &fpc. also accepts strings as message
identifiers. Message methods are always virtual.

:p.
As can be seen in the class declaration diagram, message methods are 
declared with a :hp1.message:ehp1. keyword, followed by an integer constant
expression.

:p.
Additionally, they can take only one var argument (typed or not):

:xmp.
  procedure TMyObject.MyHandler(var Msg); message 1;
:exmp.

:p.
The method implementation of a message function is not different from an
ordinary method. It is also possible to call a message method directly,
but this should not be done. Instead, the TObject.Dispatch method
should be used. Message methods are automatically virtual,
i.e. they can be overridden in descendent classes.

:p.
The TObject.Dispatch method can be used to call a message handler.
It is declared in the :hp1.System:ehp1. unit and will accept a var
parameter which must have at the first position a cardinal with the
message ID that should be called. For example:

:xmp.
type
  TMsg = record
    MsgID: Cardinal;
    Data: Pointer;
  end;
  
var
  Msg: TMSg;
begin
  ...
  MyObject.Dispatch(Msg);
:exmp.

:p.
In this example, the Dispatch() method will look at the object and all
its ancestors (starting at the object, and searching up the inheritance 
class tree), to see if a message method with message :hp1.MsgID:ehp1. has been
declared. If such a method is found, it is called, and passed the
Msg parameter.

:p.
If no such method is found, :hp1.DefaultHandlerStr():ehp1. is called.
DefaultHandlerStr() is a virtual method of TObject that doesn't do
anything, but which can be overridden to provide any processing that might be
needed. DefaultHandlerStr() is declared as follows:

:xmp.
  procedure DefaultHandlerStr(var message); virtual;
:exmp.

:p.
In addition to this mechanism, a string message method accepts a :hp1.self:ehp1.
parameter:

:xmp.
  procedure StrMsgHandler(Data: Pointer; Self: TMyObject); Message 'OnClick';
:exmp.

:p.
When encountering such a method, the compiler will generate code that loads
the :hp1.Self:ehp1. parameter into the object instance pointer. The result of
this is that it is possible to pass Self as a parameter to such a
method.

:nt.
The type of the :hp1.Self:ehp1. parameter must be of the same class
as the class the method is defined in.
:ent.



:h4 name='class_using_inherited'.Using inherited
:p.
In an overridden virtual method, it is often necessary to call the parent
class' implementation of the virtual method. This can be done with the
:hp1.inherited:ehp1. keyword. Likewise, the :hp1.inherited:ehp1. keyword can be used
to call any method of the parent class.

:p.
The first case is the simplest:

:xmp.
type
  TMyClass = class(TComponent)
    constructor Create(AOwner: TComponent); override;
  end;

constructor TMyClass.Create(AOwner: TComponent); 
begin
  inherited;
  // Do more things
end;
:exmp.

:p.
In the above example, the :hp1.Inherited:ehp1. statement will call Create()
of TComponent, passing it AOwner as a parameter: the same
parameters that were passed to the current method will be passed to the
parent's method. They must not be specified again: if none are specified,
the compiler will pass the same arguments as the ones received.

:p.
The second case is slightly more complicated:

:xmp.
type
  TMyClass = class(TComponent)
    constructor Create(AOwner: TComponent); override;
    constructor CreateNew(AOwner: TComponent; DoExtra: Boolean);
  end;

constructor TMyClass.Create(AOwner: TComponent); 
begin
  inherited;
end;

constructor TMyClass.CreateNew(AOwner: TComponent; DoExtra: Boolean); 
begin
  inherited Create(AOwner);
  // Do stuff
end;
:exmp.

:p.
The CreateNew() method will first call TComponent.Create() and
will pass it AOwner as a parameter. It will not call
TMyClass.Create().

:p.
Although the examples were given using constructors, the use of
Inherited is not restricted to constructors, it can be used
for any procedure or function or destructor as well.



:h3.Properties
:p.
:ul.
:li.:link refid='class_prop_definition' reftype='hd'.Definition:elink.
:li.:link refid='class_indexed props' reftype='hd'.Indexed properties:elink.
:li.:link refid='class_array props' reftype='hd'.Array properties:elink.
:li.:link refid='class_default props' reftype='hd'.Default properties:elink.
:li.:link refid='class_storage information' reftype='hd'.Storage information:elink.
:li.:link refid='class_overriding props' reftype='hd'.Overriding properties:elink.
:eul.





:h4 name='class_prop_definition'.Definition
:p.
Classes can contain properties as part of their fields list. A property
acts like a normal field, i.e. its value can be retrieved or set, but it
allows to redirect the access of the field through functions and
procedures. They provide a means to associate an action with an assignment
of, or a reading from a class 'field'. This allows for e.g. checking that a
value is valid when assigning, or, when reading, it allows to construct the
value on the fly. Moreover, properties can be read-only or write only.
The prototype declaration of a property is as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Properties:ehp2.

&ra.&ra.─── property definition ── :hp2.property:ehp2. ── identifier ─┬──────────────────────┬───&ra.
                                                     └─ property interface ─┘
                                                     
&ra.──── property specifier ── hint directive ─────────────────────────────────────&ra.&la.

&ra.&ra.─── property interface ─┬───────────────────────────┬─ : ── type identifier ──&ra.
                          └─ property parameter list ─┘

&ra.─────┬─────────────────────────────┬───────────────────────────────────────────&ra.&la.
      └─ :hp2.index:ehp2. ── integer constant ─┘

&ra.&ra.─── property parameter list ── [ ── parameter declaration ─┬──────────────────&ra.&la.
                                    ^───────── ; ────────────┘

&ra.&ra.─── property specifiers ──┬──────────────────┬─┬────────────────────────┬─────&ra.
                            └─ read specifier ─┘ ├─── write specifier ────┤
                                                 └─ implements specifier ─┘

&ra.─────┬─────────────────────┬┬────────────────────┬─────────────────────────────&ra.
      └─ default specifier ─┘└─ stored specifier ─┘

&ra.─────┬────────────────────────────────────┬────────────────────────────────────&ra.&la.
      └─ default array property specifier ─┘

&ra.&ra.──── read specifier ── :hp2.read:ehp2. ── field or method ───────────────────────────────&ra.&la.

&ra.&ra.──── write specifier ── :hp2.write:ehp2. ── field or method ─────────────────────────────&ra.&la.

&ra.&ra.──── implements specifier ── :hp2.implements:ehp2. ── identifier ────────────────────────&ra.&la.

&ra.&ra.──── default specifier ─┬─ :hp2.default:ehp2. ─┬────────────┬┬───────────────────────────&ra.&la.
                          │           └─ constant ─┘│
                          └───── :hp2.nodefault:ehp2. ─────────┘

&ra.&ra.──── stored specifier ── :hp2.stored:ehp2. ─┬─ constant ───┬─────────────────────────────&ra.&la.
                                   └─ identifier ─┘
                          
&ra.&ra.──── field or method ──┬─ field identifier ──┬────────────────────────────────&ra.&la.
                         └─ method identifier ─┘

&ra.&ra.──── default array property specifier ── ; ── :hp2.default:ehp2. ────────────────────────&ra.&la.
                         
└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
A :hp1.read specifier:ehp1. is either the name of a field that contains the property, or the name of a
method function that has the same return type as the property type. In the case of a simple type, this
function must not accept an argument. In case of an array property, the function must accept a single
argument of the same type as the index. In case of an indexed property, it must accept a integer as an
argument.

:p.
A :hp1.read specifier:ehp1. is optional, making the property write-only. Note that class methods cannot
be used as read specifiers.

:p.
A :hp1.write specifier:ehp1. is optional: If there is no :hp1.write specifier:ehp1., the property is read-only.
A write specifier is either the name of a field, or the name of a method procedure that accepts as a sole
argument a variable of the same type as the property. In case of an array property, the procedure must
accept 2 arguments: the first argument must have the same type as the index, the second argument
must be of the same type as the property. Similarly, in case of an indexed property, the first parameter
must be an integer.

:p.
The section (private, published) in which the specified function or procedure resides is irrelevant.
Usually, however, this will be a protected or private method.

:p.
For example, given the following declaration:

:xmp.
type
  MyClass = class
  private
    Field1: Longint;
    Field2: Longint;
    Field3: Longint;
    procedure Sety(AValue: Longint);
    function Gety: Longint;
    function Getz: Longint;
  public
    property X: Longint read Field1 write Field2;
    property Y: Longint read GetY write Sety;
    property Z: Longint read GetZ;
  end;

var 
  MyClass: TMyClass;
:exmp.

:p.
The following are valid statements:

:xmp.
WriteLn ('X : ', MyClass.X);
WriteLn ('Y : ', MyClass.Y);
WriteLn ('Z : ', MyClass.Z);
MyClass.X := 0;
MyClass.Y := 0;
:exmp.

:p.
But the following would generate an error:

:xmp.
MyClass.Z := 0;
:exmp.

:p.
because Z is a read-only property.

:p.
What happens in the above statements is that when a value needs to be read,
the compiler inserts a call to the various :hp1.getNNN:ehp1. methods of the
object, and the result of this call is used. When an assignment is made,
the compiler passes the value that must be assigned as a paramater to
the various :hp1.setNNN:ehp1. methods.

:p.
Because of this mechanism, properties cannot be passed as var arguments to a
function or procedure, since there is no known address of the property (at
least, not always).


:h4 name='class_indexed props'.Indexed properties
:p.
If the property definition contains an index, then the read and write specifiers must be a function and
a procedure. Moreover, these functions require an additional parameter: An integer parameter. This
allows to read or write several properties with the same function. For this, the properties must have
the same type. The following is an example of a property with an index:

:xmp.
{$mode objfpc}
type 
  TPoint = class(TObject)
  private
    FX: Longint;
    FY: Longint;
    function GetCoord(Index: Integer): Longint;
    procedure SetCoord(Index: Integer; Value: Longint);
  public
    property X: Longint index 1 read GetCoord write SetCoord;
    property Y: Longint index 2 read GetCoord write SetCoord;
    property Coords[Index: Integer]: Longint read GetCoord;
  end;

procedure TPoint.SetCoord(Index: Integer; Value: Longint);
begin
  case Index of
   1 : FX := Value;
   2 : FY := Value;
  end;
end;

function TPoint.GetCoord(Index: Integer): Longint;
begin
  case Index of
   1 : Result := FX;
   2 : Result := FY;
  end;
end;

var 
  P: TPoint;

begin
  P := TPoint.Create;
  P.X := 2;
  P.Y := 3;
  with P do
    WriteLn('X=', X, ' Y=', Y);
end.
:exmp.

:p.
When the compiler encounters an assignment to X, then :hp1.SetCoord:ehp1. is called with as first parameter
the index (1 in the above case) and with as a second parameter the value to be set. Conversely, when
reading the value of X, the compiler calls :hp1.GetCoord:ehp1. and passes it index 1. Indexes can only be
integer values.


:h4 name='class_array props'.Array properties
:p.
Array properties also exist. These are properties that accept an
index, just as an array does. Only now the index doesn't have to be an
ordinal type, but can be any type.

:p.
A :hp1.read specifier:ehp1. for an array property is the name method function
that has the same return type as the property type.
The function must accept as a sole arguent a variable of the same type as
the index type. For an array property, one cannot specify fields as :hp1.read
specifiers:ehp1..

:p.
A :hp1.write specifier:ehp1. for an array property is the name of a method
procedure that accepts two arguments: The first argument has the same
type as the index, and the second argument is a parameter of the same
type as the property type. As an example, see the following declaration:

:xmp.
type 
  TIntList = class
  private
    Function GetInt(I: Longint): Longint;
    Function GetAsString(A: String): String;
    Procedure SetInt(I: Longint; Value: Longint;);
    Procedure SetAsString(A: String; Value: String);
  public
    property Items[i: Longint]: Longint read GetInt write SetInt;
    property StrItems[S: String]: String read GetAsString write SetAsstring;
  end;

var 
  AIntList: TIntList;
:exmp.

:p.
Then the following statements would be valid:

:xmp.
AIntList.Items[26] := 1;
AIntList.StrItems['twenty-five'] := 'zero';
WriteLn('Item 26 : ', AIntList.Items[26]);
WriteLn('Item 25 : ', AIntList.StrItems['twenty-five']);
:exmp.

:p.
While the following statements would generate errors:

:xmp.
AIntList.Items['twenty-five'] := 1;
AIntList.StrItems[26] := 'zero';
:exmp.

:p.
Because the index types are wrong.


:h4 name='class_default props'.Default properties
:p.
Array properties can be declared as :hp1.default:ehp1. properties. This means that
it is not necessary to specify the property name when assigning or reading
it. In the previous example, if the definition of the items property would
have been

:xmp.
property Items[i: Longint]: Longint read GetInt write SetInt; default;
:exmp.

:p.
Then the assignment

:xmp.
AIntList.Items[26] := 1;
:exmp.

:p.
Would be equivalent to the following abbreviation.

:xmp.
AIntList[26] := 1;
:exmp.

:p.
Only one default property per class is allowed, and descendent classes
cannot redeclare the default property.



:h4 name='class_storage information'.Storage information
:p.
The :hp1.stored specifier:ehp1. should be either a boolean constant, a boolean
field of the class, or a parameterless function which returns a boolean
result. This specifier has no result on the class behaviour. It is an aid
for the streaming system: the stored specifier is specified in the RTTI
generated for a class (it can only be streamed if RTTI is generated), 
and is used to determine whether a property should be streamed or not: 
it saves space in a stream. It is not possible to specify the 'Stored'
directive for array properties.

:p.
The :hp1.default specifier:ehp1. can be specified for ordinal types and sets.
It serves the same purpose as the :hp1.stored specifier:ehp1.: Properties that
have as value their default value, will not be written to the stream by the
streaming system. The default value is stored in the RTTI that is generated
for the class. Note that

:ol.
:li.When the class is instantiated, the default value is not automatically
applied to the property, it is the responsability of the programmer to do
this in the constructor of the class.
:li.The value 2147483648 cannot be used as a default value, as it is used
internally to denote 'nodefault'.
:li.It is not possible to specify a default for array properties.
:eol.


:h4 name='class_overriding props'.Overriding properties
:p.
aoeu
.* START HERE !!!!!!!!!!!!!!!!!!!!!!
:h5.*** START HERE ***

.* ==============================================================
:h2 name=interfaces.Interfaces
:p.aa

.* ==============================================================
:h2 name=generics.Generics
:p.aa

.* ==============================================================
:h2 name=expressions.Expressions
:p.aa
:h3 name=expression_syntax.Expression syntax
:p.aa
:h3 name=function_calls.Function calls
:p.aa
:h3 name=set_constructors.Set constructors
:p.aa
:h3 name=value_typecasts.Value typecasts
:p.aa
:h3 name=varibale_typecasts.Variable typecasts
:p.aa
:h3 name=unaligned_typecasts.Unaligned typecasts
:p.aa
:h3 name=the_at_operator.The @ operator
:p.aa
:h3 name=operators.Operators
:p.aa
:h4 name=arithmetic_operators.Arithmetic operators
:p.aa
:h4 name=logical_operators.Logical operators
:p.aa
:h4 name=boolean_operators.Boolean operators
:p.aa
:h4 name=string_operators.String operators
:p.aa
:h4 name=set_operators.Set operators
:p.aa
:h4 name=relational_operators.Relational operators
:p.aa
:h4 name=class_operators.Class operators
:p.aa

.* ==============================================================
:h2 name=statements.Statements
:p.aa
:h3 name=statements_simple.Simple Statements
:p.aa
:h3 name=statements_structured.Structured Statements
:p.aa
:h4 name=statements_structured_with.The With Statement
:p.aa

.* ==============================================================
:h2 name=functions.Using functions and procedures
:p.aa
:h3 name=procedure_declarations.Procedure declarations
:p.aa
:h3 name=function-declarations.Function declarations
:p.aa

.* ==============================================================
:h2 name=operator_overloading.Operator overloading
:p.aa





.* ==============================================================
:h2 name=programs_units_blocks.Programs&comma. units and blocks
:p.A Pascal program can consist of modules called :hp1.units:ehp1.. A unit can be used
to group pieces of code together, or to give someone code without giving
the sources.
Both programs and units consist of code blocks, which are mixtures of
statements, procedures, and variable or type declarations.

.* --------------------------------------------------------------
:h3.Programs
:p.A Pascal program consists of the program header, followed possibly by a
'uses' clause, and a block.

:xmp.
                     [diagram goes here]
:exmp.

:p.
The program header is provided for backwards compatibility, and is ignored
by the compiler.

:p.
The uses clause serves to identify all units that are needed by the program.
All identifiers which are declared in the the interface section of the units
in the uses clause are added to the known identifiers of the program.
The system unit doesn't have to be in this list, since it is always loaded
by the compiler.

:p.
The order in which the units appear is significant, it determines in
which order they are initialized. Units are initialized in the same order
as they appear in the uses clause. Identifiers are searched in the opposite
order, i.e. when the compiler searches for an identifier, then it looks
first in the last unit in the uses clause, then the last but one, and so on.
This is important in case two units declare different types with the same
identifier.

:p.
When the compiler looks for unit files, it adds the extension :hp1..ppu:ehp1.
to the name of the unit. On &linux. and in operating systems where filenames 
are case sensitive when looking for a unit, the following mechanism is
used:

:ol.
:li.The unit is first looked for in the original case.
:li.The unit is looked for in all-lowercase letters.
:li.The unit is looked for in all-uppercase letters.
:eol.

:p.
Additionally, If a unit name is longer than 8 characters, the compiler 
will first look for a unit name with this length, and then it will 
truncate the name to 8 characters and look for it again. 
For compatibility reasons, this is also true on platforms that 
support long file names.

:p.
Note that the above search is performed in each directory in the search
path. 

:p.
The program block contains the statements that will be executed when the
program is started. Note that these statements need not necessarily be the 
first statements that are executed: the initialization code of the units
may also contain statements that are executed prior to the program code.

:p.
The structure of a program block is discussed below.

.* --------------------------------------------------------------
:h3.Units
:p.A unit contains a set of declarations, procedures and functions that can be
used by a program or another unit. The syntax for a unit is as follows:

:cgraphic.
┌──────────────────────────────────────────────────────────────────────────────┐
:hp2.Units:ehp2.

&ra.&ra.─── unit ── unit header ── interface part ──  implementation part ────────────&ra.

&ra.─────┬────────────────────────────────────────────┬─ :hp2.end:ehp2. ── . ─────────────────&ra.&la.
      ├─ initialization part ─┬───────────────────┬┤
      │                       └ finalization part ┘│
      └─  :hp2.begin:ehp2. ─┬─ statement ─┬───────────────────┘
                 ^───── ; ─────┘

&ra.&ra.─── unit header ── :hp2.unit:ehp2. ── unit identifier ── ; ──────────────────────────────&ra.&la.
                                                     
&ra.&ra.─── interface part ── :hp2.interface:ehp2. ─┬───────────────┬┬┬─────────────────────────────┬┬──&ra.&la.
                                   └─ uses clause ─┘│├─ constant declaration part ─┤│
                                                    │├─   type declatation part   ─┤│
                                                    │└─  procedure headers part   ─┘│
                                                    ^───────────────────────────────┘
                                   
&ra.&ra.─── procedure headers part ─┬─ procedure header ─┬─ ; ─┬───────────────────────┬─────&ra.&la.
                              └─ function header  ─┘     └─ call modifiers ── ; ─┘

&ra.&ra.─── implementation part ── :hp2.implementation:ehp2. ─┬───────────────┬─ declaration part ──────&ra.&la.
                                             ^─ uses clause ─┘

&ra.&ra.─── initialization part ── :hp2.initialization:ehp2. ─┬─ statement ─┬────────────────────&ra.&la.
                                             ^───── ; ─────┘

&ra.&ra.─── finalization part ── :hp2.finalization:ehp2. ─┬─ statement ─┬────────────────────────&ra.&la.
                                         ^───── ; ─────┘
                         
└──────────────────────────────────────────────────────────────────────────────┘
:ecgraphic.

:p.
As can be seen from the syntax diagram, a unit always consists of a
interface and an implementation part. Optionally, there is an initialization
block and a finalization block, containing code that will be executed when
the program is started, and when the program stops, respectively.

:p.
Both the interface part or implementation part can be empty, but the
keywords :hp1.Interface:ehp1. and :hp1.implementation:ehp1. must be specified.
The following is a completely valid unit:

:xmp.
unit a;

interface

implementation

end.
:exmp.

:p.
The interface part declares all identifiers that must be exported from the
unit. This can be constant, type or variable identifiers, and also procedure
or function identifier declarations.  The interface part cannot contain code
that is executed: only declarations are allowed. The following is a valid
interface part:

:xmp.
unit a;

interface

uses b;

function MyFunction: SomeBType;

implementation
:exmp.

:p.
The type :hp1.SomeBType:ehp1. is defined in unit :hp1.b:ehp1..

:p.
All functions and methods that are declared in the interface part must
be implemented in the implementation part of the unit, except for
declarations of external functions or procedures. If a declared method 
or function is not implemented in the implementation part, the compiler
will give an error, for example the following:

:xmp.
unit unita;

interface

function MyFunction: Integer;

implementation

end.
:exmp.

:p.
Will result in the following error:

:xmp.
unita.pp(5,10) Error: Forward declaration not solved "MyFunction&colon.SmallInt;"
:exmp.

:p.
The implementation part is primarily intended for the implementation of the
functions and procedures declared in the interface part. However, it can
also contain declarations of it's own: The declarations inside the 
implementation part are :hp2.not:ehp2. accessible outside the unit. 

:p.
The initialization and finalization part of a unit are optional:

:p.
The initialization block is used to initialize certain variables or 
execute code that is necessary for the correct functioning of the unit. 
The initialization parts of the units
are executed in the order that the compiler loaded the units when compiling 
a program. They are executed before the first statement of the program is
executed.

:p.
The finalization part of the units are executed in the reverse order of the
initialization execution. They are used for instance to clean up any resources 
allocated in the initialization part  of the unit, or during the lifetime of
the program. The finalization part is always executed in the case of a
normal program termination: whether it is because the final :hp1.end:ehp1. is
reached in the program code or because a :hp1.Halt:ehp1. instruction was executed
somewhere.

:p.
In case the program stops during the execution of the initialization blocks
of one of the units, only the units that were already initialized will be
finalized. Note that if a :hp1.finalization:ehp1. block is present, an
:hp1.initialization:ehp1. block must be present, but it can be empty:

:xmp.
initialization

finalization
  CleanupUnit;
end.
:exmp.

:p.
An initialization section by itself (i.e. without finalization) may simply be 
replaced by a statement block. That is, the following:

:xmp.
initialization
  InitializeUnit;
end.
:exmp.

:p.
is completely equivalent to

:xmp.
begin
  InitializeUnit;
end.
:exmp.

.* --------------------------------------------------------------
:h3.Unit dependencies
:p.aa





.* ==============================================================
:h2 name=exceptions.Exceptions
:p.aa

.* ==============================================================
:h2 name=assembler.Using assembler
:p.
&fpc. supports the use of assembler in code, but not inline
assembler macros. To have more information on the processor
specific assembler syntax and its limitations, see the &progref..

.* --------------------------------------------------------------
:h3.Assembler statements
The following is an example of assembler inclusion in Pascal code.

:xmp.
 ...
 Statements;
 ...
 Asm
   the asm code here
   ...
 end;
 ...
 Statements;
:exmp.

:p.
The assembler instructions between the :hp1.Asm:ehp1. and :hp1.end:ehp1. keywords will
be inserted in the assembler generated by the compiler.
Conditionals can be used in assembler code, the compiler will recognise them,
and treat them as any other conditionals.

.* --------------------------------------------------------------
:h3.Assembler procedures and functions
:p.
Assembler procedures and functions are declared using the
:hp1.Assembler:ehp1. directive.  This permits the code generator to make a number
of code generation optimizations.

:p.
The code generator does not generate any stack frame (entry and exit
code for the routine) if it contains no local variables and no
parameters. In the case of functions, ordinal values must be returned
in the accumulator. In the case of floating point values, these depend
on the target processor and emulation options.

:h2.Object Pascal Grammar
:p.This section describes the Object Pascal grammar in a EBNF
(Extended Backus–Naur Form) like style. The syntax only covers the
:hp1.ObjFPC:ehp1. mode of the &fpc. compiler.

:cgraphic.
Goal -> (Program | Package | Library | Unit)
Program -> [PROGRAM Ident ['(' IdentList ')'] ';']
           ProgramBlock '.'
Unit -> UNIT Ident [HintDirective] ';'
        InterfaceSection
        ImplementationSection
        InitSection '.'
Package -> PACKAGE Ident ';'
           [RequiresClause]
           [ContainsClause]
           END '.'
Library -> LIBRARY Ident ';'
           ProgramBlock '.'
ProgramBlock -> [UsesClause]
                Block
UsesClause -> USES IdentList ';'
HintDirective -> deprecated  [String]
              -> experimental
              -> library
              -> platform
              -> unimplemented
InterfaceSection -> INTERFACE
                    [UsesClause]
                    [InterfaceDecl]...
InterfaceDecl ->  ConstSection
              ->  TypeSection
              ->  VarSection
              ->  ExportedHeading
ExportedHeading -> ProcedureHeading ';' [Directive]
                -> FunctionHeading ';' [Directive]
ImplementationSection -> IMPLEMENTATION
                         [UsesClause]
                         [DeclSection]...
                         [ExportsStmt]...
Block -> [DeclSection]
         [ExportsStmt]...
         CompoundStmt
         [ExportsStmt]...
ExportsStmt -> EXPORTS ExportsItem [, ExportsItem]...
ExportsItem -> Ident [NAME|INDEX "'" ConstExpr "'"]
                     [INDEX|NAME "'" ConstExpr "'"]
DeclSection -> LabelDeclSection
            -> ConstSection
            -> TypeSection
            -> VarSection
            -> ProcedureDeclSection
LabelDeclSection -> LABEL LabelId
ConstSection -> CONST (ConstantDecl ';')...
ConstantDecl -> Ident '=' ConstExpr [HintDirective]
             -> Ident ':' TypeId '=' TypedConstant [HintDirective]
TypeSection -> TYPE (TypeDecl ';')
TypeDecl -> Ident '=' [TYPE] Type [HintDirective]
         -> Ident '=' [TYPE] RestrictedType [HintDirective]
TypedConstant -> (ConstExpr | ArrayConstant | RecordConstant)
ArrayConstant -> '(' TypedConstant ',' ')'
RecordConstant -> '(' RecordFieldConstant ';'... ')'
RecordFieldConstant -> Ident ':' TypedConstant
Type -> TypeId
     -> SimpleType
     -> StrucType
     -> PointerType
     -> StringType
     -> ProcedureType
     -> VariantType
     -> ClassRefType
RestrictedType -> ObjectType
               -> ClassType
               -> InterfaceType
ClassRefType -> CLASS OF TypeId
SimpleType -> (OrdinalType | RealType)
RealType ->  REAL48
         ->  REAL
         ->  SINGLE
         ->  DOUBLE
         ->  EXTENDED
         ->  CURRENCY
         ->  COMP
OrdinalType -> (SubrangeType | EnumeratedType | OrdIdent)
OrdIdent ->  SHORTINT
         ->  SMALLINT
         ->  INTEGER
         ->  BYTE
         ->  LONGINT
         ->  INT64
         ->  WORD
         ->  BOOLEAN
         ->  CHAR
         ->  WIDECHAR
         ->  LONGWORD
         ->  PCHAR
VariantType -> VARIANT
            -> OLEVARIANT
SubrangeType -> ConstExpr '..' ConstExpr
EnumeratedType -> '(' EnumeratedTypeElement ','... ')'
EnumeratedTypeElement -> Ident [ '=' ConstExpr ]
StringType -> STRING
           -> ANSISTRING
           -> WIDESTRING
           -> STRING '[' ConstExpr ']'
           -> UNICODESTRING
StrucType -> [PACKED] (ArrayType | SetType | FileType | RecType [PACKED])
ArrayType -> ARRAY ['[' OrdinalType ','... ']'] OF Type [HintDirective]
RecType -> RECORD [FieldList] END [HintDirective]
FieldList -> FieldDecl ';'... [VariantSection] [';']
FieldDecl -> IdentList ':' Type [HintDirective]
VariantSection -> CASE [Ident ':'] TypeId OF RecVariant ';'...
RecVariant -> ConstExpr ','... ':' '(' [FieldList] ')'
SetType -> SET OF OrdinalType [HintDirective]
FileType -> FILE OF TypeId [HintDirective]
PointerType -> '^' TypeId [HintDirective]
ProcedureType -> (ProcedureHeading | FunctionHeading) [OF OBJECT]
VarSection -> VAR (VarDecl ';')...
VarDecl
  On Windows -> IdentList ':' Type [(ABSOLUTE (Ident | ConstExpr)) | '=' ConstExpr] [HintDirective]
  On Linux   -> IdentList ':' Type [ABSOLUTE (Ident) | '=' ConstExpr] [HintDirective]
Expression -> SimpleExpression [RelOp SimpleExpression]...
SimpleExpression -> ['+' | '-'] Term [AddOp Term]...
Term -> Factor [MulOp Factor]...
Factor ->  Designator ['(' ExprList ')']
        -> '@' Designator
        -> Number
        -> String
        -> NIL
        -> '(' Expression ')'
        -> NOT Factor
        -> SetConstructor
        -> TypeId '(' Expression ')'
RelOp ->  '>'
      ->  '<'
      ->  '<='
      ->  '>='
      ->  '<>'
      ->  IN
      ->  IS
      ->  AS
AddOp ->  '+'
      ->  '-'
      ->  OR
      ->  XOR
MulOp ->  '*'
      ->  '/'
      ->  DIV
      ->  MOD
      ->  AND
      ->  SHL
      ->  SHR
Designator -> QualId ['.' Ident | '[' ExprList ']' | '^']...
SetConstructor -> '[' [SetElement ','...] ']'
SetElement -> Expression ['..' Expression]
ExprList -> Expression ','...
Statement -> [LabelId ':'] [SimpleStatement | StructStmt]
StmtList -> Statement ';'
SimpleStatement -> Designator ['(' [ExprList] ')']
                -> Designator ':=' Expression
                -> INHERITED
                -> GOTO LabelId
StructStmt -> CompoundStmt
           -> ConditionalStmt
           -> LoopStmt
           -> WithStmt
           -> TryExceptStmt
           -> TryFinallyStmt
           -> RaiseStmt
           -> AssemblerStmt
CompoundStmt -> BEGIN StmtList END
ConditionalStmt -> IfStmt
                -> CaseStmt
IfStmt -> IF Expression THEN Statement [ELSE Statement]
CaseStmt -> CASE Expression OF CaseSelector ';'... [ELSE StmtList] [';'] END
CaseSelector -> CaseLabel ','... ':' Statement
CaseLabel -> ConstExpr ['..' ConstExpr]
LoopStmt -> RepeatStmt
         -> WhileStmt
         -> ForStmt
         -> ForInStmt
RepeatStmt -> REPEAT Statement UNTIL Expression
WhileStmt -> WHILE Expression DO Statement
ForStmt -> FOR QualId ':=' Expression (TO | DOWNTO) Expression DO Statement
ForInStmt -> FOR QualId IN Expression DO Statement
WithStmt -> WITH IdentList DO Statement
TryExceptStmt -> TRY
                   Statement...
                 EXCEPT
                   ExceptionBlock
                 END
ExceptionBlock -> [ON [Ident ':'] TypeID DO Statement]...
                  [ELSE Statement...]
TryFinallyStmt -> TRY
                    Statement
                  FINALLY
                    Statement
                  END
RaiseStmt -> RAISE [object] [AT address]
AssemblerStatement -> ASM
                   -> <assemblylanguage>
                   -> END
ProcedureDeclSection -> ProcedureDecl
                     -> FunctionDecl
ProcedureDecl -> ProcedureHeading ';' [Directive] [HintDirective]
                 Block ';'
FunctionDecl -> FunctionHeading ';' [Directive] [HintDirective]
                Block ';'
FunctionHeading -> FUNCTION Ident [FormalParameters] ':' (SimpleType | STRING)
ProcedureHeading -> PROCEDURE Ident [FormalParameters]
FormalParameters -> '(' [FormalParm ';'...] ')'
FormalParm -> [VAR | CONST | CONSTREF | OUT] Parameter
Parameter -> IdentList [':' ([ARRAY OF] SimpleType | STRING | FILE)]
          -> Ident ':' SimpleType '=' ConstExpr
Directive ->  CDECL
          ->  REGISTER
          ->  DYNAMIC
          ->  VIRTUAL
          ->  EXPORT
          ->  EXTERNAL
          ->  NEAR
          ->  FAR
          ->  FORWARD
          ->  MESSAGE ConstExpr
          ->  OVERRIDE
          ->  OVERLOAD
          ->  PASCAL
          ->  REINTRODUCE
          ->  SAFECALL
          ->  STDCALL
          ->  VARARGS
          ->  LOCAL
          ->  ABSTRACT
ObjectType -> OBJECT [ObjHeritage] [ObjFieldList] [MethodList] END
ObjHeritage -> '(' QualId ')'
MethodList -> (MethodHeading [';' VIRTUAL]) ';'...
MethodHeading ->  ProcedureHeading
              ->  FunctionHeading
              ->  ConstructorHeading
              ->  DestructorHeading
ConstructorHeading -> CONSTRUCTOR Ident [FormalParameters]
DestructorHeading -> DESTRUCTOR Ident [FormalParameters]
ObjFieldList -> (IdentList ':' Type) ';'
InitSection -> INITIALIZATION StmtList [FINALIZATION StmtList] END
            -> BEGIN StmtList END
            -> END
ClassType -> CLASS [ClassHeritage]
             [ClassVisibility]
             [ClassFieldList]
             [ClassMethodList]
             [ClassPropertyList]
             END
ClassHeritage -> '(' IdentList ')'
ClassVisibility -> [[STRICT] PRIVATE | PROTECTED | PUBLIC | PUBLISHED]
ClassFieldList -> (ClassVisibility ObjFieldList) ';'...
ClassMethodList -> (ClassVisibility MethodList) ';'...
ClassPropertyList -> (ClassVisibility PropertyList ';')...
PropertyList -> PROPERTY Ident [PropertyInterface] [PropertySpecifiers] [HintDirective]
PropertyInterface -> [PropertyParameterList] ':' Ident
PropertyParameterList -> '[' (IdentList ':' TypeId) ';'... ']'
PropertySpecifiers -> [INDEX ConstExpr]
                      [READ Ident]
                      [WRITE Ident]
                      [STORED (Ident | Constant)]
                      [(DEFAULT ConstExpr) | NODEFAULT]
                      [IMPLEMENTS TypeId]
InterfaceType -> INTERFACE [InterfaceHeritage]
                 [ClassMethodList]
                 [ClassPropertyList]
                 ...
                 END
InterfaceHeritage -> '(' IdentList ')'
RequiresClause -> REQUIRES IdentList... ';'
ContainsClause -> CONTAINS IdentList... ';'
IdentList -> Ident ','...
QualId -> [UnitId '.'] Ident
TypeId -> [UnitId '.'] <type-identifier>
Ident -> <identifier>
ConstExpr -> <constant-expression>
UnitId -> <unit-identifier>
LabelId -> <label-identifier>
Number -> <number>
String -> <string>

:ecgraphic.

:euserdoc.