File: gnatpp-layout.adb

package info (click to toggle)
asis 2007-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,832 kB
  • ctags: 34
  • sloc: ada: 93,665; makefile: 225
file content (2502 lines) | stat: -rw-r--r-- 78,813 bytes parent folder | download
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
------------------------------------------------------------------------------
--                                                                          --
--                            GNATPP COMPONENTS                             --
--                                                                          --
--                        G N A T P P . L A Y O U T                         --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                    Copyright (C) 2001-2007, AdaCore                      --
--                                                                          --
-- GNATPP is free software; you can redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion.  GNATPP is  distributed in the  hope that it will  be  useful, but --
-- WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABI- --
-- LITY or  FITNESS  FOR A  PARTICULAR  PURPOSE. See the GNU General Public --
-- License  for more details. You  should  have  received a copy of the GNU --
-- General Public License  distributed with GNAT; see file COPYING. If not, --
-- write to the Free Software Foundation,  51 Franklin Street, Fifth Floor, --
-- Boston,                                                                  --
--                                                                          --
-- GNATPP is maintained by AdaCore (http://www.adacore.com)                 --
--                                                                          --
------------------------------------------------------------------------------

with Table;

with Asis.Clauses;                    use Asis.Clauses;
with Asis.Extensions;                 use Asis.Extensions;
with Asis.Declarations;               use Asis.Declarations;
with Asis.Elements;                   use Asis.Elements;
with Asis.Statements;                 use Asis.Statements;
with Asis.Definitions;                use Asis.Definitions;
with Asis.Expressions;                use Asis.Expressions;

with ASIS_UL.Options;

with GNATPP.State;                    use GNATPP.State;
with GNATPP.Common;                   use GNATPP.Common;
with GNATPP.Options;                  use GNATPP.Options;
with GNATPP.PP_Output;                use GNATPP.PP_Output;
with GNATPP.Output;                   use GNATPP.Output;
with GNATPP.Paragraphs;               use GNATPP.Paragraphs;
with GNATPP.Asis_Utilities;           use GNATPP.Asis_Utilities;
with GNATPP.General_Traversal_Stacks; use GNATPP.General_Traversal_Stacks;

package body GNATPP.Layout is

   -----------------------------------------------------------
   -- Table for collecting and computing the alignment info --
   -----------------------------------------------------------

   package Align_Info is new Table.Table
     (Table_Component_Type => Layout_Info,
      Table_Index_Type     => Natural,
      Table_Low_Bound      => 1,
      Table_Initial        => 100,
      Table_Increment      => 100,
      Table_Name           => "");
   --  In this table the information about lengths of the components of the
   --  "homogeneous"  constructions is stored in order to compute the
   --  possible alignment.

   -----------------------
   -- Local_Subprograms --
   -----------------------

   procedure Check_List_Layout
     (Curr_Pos             : in out Natural;
      Elements             :        Asis.Element_List;
      Needs_Separate_Lines :    out Boolean);
   --  This procedure checks, if the given Elements can be placed in one line
   --  starting from Curr_Pos. It is supposed, that the Elements in the
   --  pretty-printed output are separated by ', ', and the last one is
   --  followed by ') ' or ');', so we are adding '2'to the space needed for
   --  each element.
   --
   --  If all the Elements can be put in one line, Curr_Pos is set as if they
   --  are sent to the output and, therefore, it can be further used in layout
   --  computation, Needs_Separate_Lines is set OFF. Otherwise Curr_Pos should
   --  be considered by the client as undefined, and Needs_Separate_Lines is
   --  set ON.
   --
   --  There are two reasons for making the decision that Elements can not
   --  be put in the same line:
   --  (1) When simulating outputting these Elements we exceed the maximum
   --      line limit;
   --  (2) One of the Elements contain a comment inside its text image;
   --
   --  We do NOT consider comments BETWEEN Elements as the reason to print
   --  them on separate lines

   procedure Check_List_Layout_And_Max_Elem
     (Curr_Pos             : in out Natural;
      Elements             : Asis.Element_List;
      Needs_Separate_Lines : out Boolean;
      Max_Element_Space    : out Positive);
   --  Similar to Check_List_Layout, but also detects the space needed for the
   --  longest element in the list

   procedure Compute_Associations_Alignment
     (Start_Pos :     Natural;
      Elements  :     Asis.Element_List;
      Arrow_Pos : out Natural;
      Max_Expr  : out Natural);
   --  This procedure analyzes the list of associations and computes the layout
   --  parameters for aligning the associations (which are supposed to be
   --  placed on separate lines). Arrow_Pos is set to the starting position of
   --  the arrow delimiter. Max_Expr defines the length of the component
   --  expression (or of the first component thereof) which still allows to
   --  align the component association (that is, if the expression or its
   --  first component is longer then Max_Expr, the enclosing association
   --  should not be aligned.
   --
   --  At the moment computing the Max_Expr value is not implemented

   function Take_Then_Into_Account return Boolean;
   --  Checks if we have to take into account the 'THEN' keyword to avoid
   --  formatting like this
   --
   --  if .............
   --  then

   -----------------------
   -- Check_List_Layout --
   -----------------------

   procedure Check_List_Layout
     (Curr_Pos             : in out Natural;
      Elements             : Asis.Element_List;
      Needs_Separate_Lines : out Boolean)
   is
      Next_Space     : Natural;
      Has_Comments   : Boolean            := False;
   begin

      Needs_Separate_Lines := False;

      for J in Elements'Range loop

         Detect_Possible_Layout
           (The_Element     => Elements (J),
            Space_In_Output => Next_Space,
            Comment_Inside  => Has_Comments);

         Curr_Pos := Curr_Pos + Next_Space + 2;
         --  '+ 2' stands for ', ' or ') '

         if Has_Comments or else
            Curr_Pos > Max_Line_Length
         then
            Needs_Separate_Lines := True;
            exit;
         end if;

      end loop;

   end Check_List_Layout;

   ------------------------------------
   -- Check_List_Layout_And_Max_Elem --
   ------------------------------------

   procedure Check_List_Layout_And_Max_Elem
     (Curr_Pos             : in out Natural;
      Elements             : Asis.Element_List;
      Needs_Separate_Lines : out Boolean;
      Max_Element_Space    : out Positive)
   is
      Next_Space     : Natural;
      Has_Comments   : Boolean            := False;
   begin

      Needs_Separate_Lines := False;
      Max_Element_Space    := 1;

      for J in Elements'Range loop

         Detect_Possible_Layout
           (The_Element     => Elements (J),
            Space_In_Output => Next_Space,
            Comment_Inside  => Has_Comments);

         Curr_Pos := Curr_Pos + Next_Space + 2;
         --  '+ 2' stands for ', ' or ') '

         if Has_Comments or else
            Curr_Pos > Max_Line_Length
         then
            Needs_Separate_Lines := True;
         end if;

         if Next_Space > Max_Element_Space then
            Max_Element_Space := Next_Space;
         end if;

      end loop;

   end Check_List_Layout_And_Max_Elem;

   -------------------------------------
   -- Compute_Array_Definition_Layout --
   -------------------------------------

   procedure Compute_Array_Definition_Layout (E : Asis.Element) is
      Arg_Kind : constant Flat_Element_Kinds := Flat_Element_Kind (E);

      Next_Out_Pos  : Natural;
      Next_Space    : Natural;
      Has_Comments  : Boolean;

      Start_From_New_Line : Boolean := False;

      function Get_Index_Defs return Element_List;
      --  returns the list of the index definitions

      function Get_Index_Defs return Element_List is
      begin

         case Arg_Kind is
            when A_Constrained_Array_Definition        |
                 A_Formal_Constrained_Array_Definition =>
               return Discrete_Subtype_Definitions (E);
            when others =>
               return Index_Subtype_Definitions (E);
         end case;

      end Get_Index_Defs;
   begin

      if Is_New_Output_Line then
         Next_Out_Pos := (Logical_Depth + 1) * PP_Indentation;
      else
         Next_Out_Pos := Output_Pos + 1;
      end if;

      Detect_Possible_Layout
        (The_Element     => E,
         Space_In_Output => Next_Space,
         Comment_Inside  => Has_Comments);

      if Next_Out_Pos + Next_Space < Max_Line_Length then
         Current_State.Layout.Flag1 := False;

         if not Has_Comments then
            Current_State.Layout.Flag2 := False;

            --  Everything is on one line...
            return;

            --  Otherwise we have to check if all the indexes can be placed
            --  into one line
         end if;

      else
         Current_State.Layout.Flag1 := True;
      end if;

      --  If we are here, we may think, that the first index start from
      --  the position corresponding to the situation when "ARRAY' is
      --  printed in the new line

      Next_Out_Pos := (Logical_Depth + 1) * PP_Indentation + 6;

      Check_List_Layout
        (Curr_Pos             => Next_Out_Pos,
         Elements             => Get_Index_Defs,
         Needs_Separate_Lines => Start_From_New_Line);

      if not Start_From_New_Line then

         Detect_Possible_Layout
           (The_Element     => Array_Component_Definition (E),
            Space_In_Output => Next_Space,
            Comment_Inside  => Has_Comments);

         if Has_Comments then
            Start_From_New_Line := True;
         else
            Next_Out_Pos := Next_Out_Pos + 4 + Next_Space + 1;

            if Next_Out_Pos > Max_Line_Length then
               Start_From_New_Line := True;
            end if;

         end if;

         if not Start_From_New_Line
           and then
            (Arg_Kind = A_Formal_Unconstrained_Array_Definition or else
             Arg_Kind = An_Unconstrained_Array_Definition)
         then
            --  Taking into account 'range <>' in index definitions
            Next_Out_Pos :=
               Next_Out_Pos + Get_Index_Defs'Length * 9;

            if Next_Out_Pos > Max_Line_Length then
               Start_From_New_Line := True;
            end if;
         end if;

      end if;

      Current_State.Layout.Flag2 := Start_From_New_Line;
   end Compute_Array_Definition_Layout;

   ----------------------------------------
   -- Compute_Assign_Statement_Alignment --
   ----------------------------------------

   procedure Compute_Assign_Statement_Alignment
     (Start_Pos  :     Natural;
      Elements   :     Asis.Element_List;
      Assign_Pos : out Natural)
   is
      Left_Part : Asis.Element;

      Next_Space           : Natural;
      Tmp_Layout           : Layout_Info;
      Alignment_Impossible : Boolean;
   begin

      Align_Info.Init;

      Assign_Pos := 0;

      for J in Elements'Range loop

         Left_Part := Assignment_Variable_Name (Elements (J));

         --  ??? This is a very primitive approach! We did not check if the
         --  statement can fit the line and if it contains comments inside
         --  ??? (The same is the case for all the other alignments!!!)

         Detect_Possible_Layout
           (The_Element     => Left_Part,
            Space_In_Output => Next_Space,
            Comment_Inside  => Alignment_Impossible);

         if not Alignment_Impossible and then
            not Is_Nil (Label_Names (Elements (J)))
         then
            --  No alignment in case of labeled statement(s)
            Alignment_Impossible := True;
         end if;

         if Alignment_Impossible then
            --  we just give up
            exit;
         end if;

         Tmp_Layout.Pos2 := Start_Pos + Next_Space + 1;

         if Tmp_Layout.Pos2 > Max_Line_Length then
            --  we just give up
            exit;
         end if;

         Align_Info.Append (Tmp_Layout);

      end loop;

      if not Alignment_Impossible then

         for J in 1 .. Align_Info.Last loop

            if Align_Info.Table (J).Pos2 > Assign_Pos then
               Assign_Pos := Align_Info.Table (J).Pos2;
            end if;

         end loop;

      end if;

   end Compute_Assign_Statement_Alignment;

   ------------------------------------
   -- Compute_Associations_Alignment --
   ------------------------------------

   procedure Compute_Associations_Alignment
     (Start_Pos :     Natural;
      Elements  :     Asis.Element_List;
      Arrow_Pos : out Natural;
      Max_Expr  : out Natural)
   is
      Choices : Element_List_Access;

      Tmp_Layout             : Layout_Info;
      Next_Pos               : Natural;
      Alignment_Impossible   : Boolean := True;
      --  By default we are pessimistic...

      function Get_Choices (E : Asis.Element) return Element_List;
      --  Returns the list of choices from the association E

      function Get_Choices (E : Asis.Element) return Element_List is
         Tmp_El : Asis.Element;
      begin
         case Flat_Element_Kind (E) is
            when A_Record_Component_Association =>
               return Record_Component_Choices (E);
            when An_Array_Component_Association =>
               return Array_Component_Choices (E);
            when A_Discriminant_Association =>
               return Discriminant_Selector_Names (E);
            when A_Parameter_Association       |
                 A_Generic_Association         |
                 A_Pragma_Argument_Association =>
               Tmp_El := Formal_Parameter (E);

               if not Is_Nil (Tmp_El) then
                  return (1 => Tmp_El);
               end if;

            when others =>
               null;
         end case;

         return Nil_Element_List;
      end Get_Choices;

   begin
      Arrow_Pos := 0;
      Max_Expr  := 0;

      Align_Info.Init;

      for J in Elements'Range loop

         Choices := new Element_List'(Get_Choices (Elements (J)));

         if Choices'Length /= 0 then

            Next_Pos := Start_Pos;

            Check_List_Layout
              (Curr_Pos             => Next_Pos,
               Elements             => Choices.all,
               Needs_Separate_Lines => Alignment_Impossible);

            if Alignment_Impossible then
               --  we just give up
               exit;
            end if;

            Tmp_Layout.Pos1 := Next_Pos - 1;
            Align_Info.Append (Tmp_Layout);

         end if;

         Free (Choices);
      end loop;

      if not Alignment_Impossible then
         --  At the moment we just compute the maximum length of
         --  choice list. later we are going to get smarter and to
         --  detect associations for which alignment does not make sense

         for J in 1 .. Align_Info.Last loop

            if Align_Info.Table (J).Pos1 > Arrow_Pos then
               Arrow_Pos := Align_Info.Table (J).Pos1;
            end if;

         end loop;

      end if;

   end Compute_Associations_Alignment;

   ------------------------------
   -- Compute_Aggregate_Layout --
   ------------------------------

   procedure Compute_Aggregate_Layout (E : Asis.Element) is
      Encl_Kind        : constant Flat_Element_Kinds := Get_Enclosing_Kind;
      Arg_Kind         : constant Flat_Element_Kinds := Flat_Element_Kind (E);
      All_Associations : Element_List_Access;
      Tmp_Expr         : Asis.Element;

      Next_Curr_Pos    : Natural;
      Next_Space       : Natural;
      Has_Comments     : Boolean            := False;

      --  Layout parameters to set:
      Separate_Line    : Boolean := False;
      Start_Pos        : Natural := 0;
      Arrow_Position   : Natural := 0;
      Max_Expr_Len     : Natural := 0;

      function Get_All_Associations return Element_List;
      --  returns the list of all the component associations of the argument
      --  aggregate

      function Get_All_Associations return Element_List is
      begin

         case Arg_Kind is
            when A_Record_Aggregate     |
                 An_Extension_Aggregate =>

               return Record_Component_Associations (E);

            when A_Positional_Array_Aggregate |
                 A_Named_Array_Aggregate      =>

               return Array_Component_Associations (E);

            when A_Discriminant_Constraint =>

               return Discriminant_Associations (E);

            when others =>

               return Nil_Element_List;

         end case;

      end Get_All_Associations;

   begin
      Next_Curr_Pos := Get_Output_Pos + 1; --  '+ 1' is for ' ('

      --  Let's first check the shortest possible aggregate

      if (Arg_Kind = An_Extension_Aggregate and then
          Next_Curr_Pos + 10 > Max_Line_Length)
        or else
         Next_Curr_Pos + 6 > Max_Line_Length
      then
         Separate_Line := True;
         goto Done;
      end if;

      if Arg_Kind = An_Extension_Aggregate then
         --  First, we have to check if there is enough room for ancestor_part
         Tmp_Expr := Extension_Aggregate_Expression (E);

         Detect_Possible_Layout
           (The_Element     => Tmp_Expr,
            Space_In_Output => Next_Space,
            Comment_Inside  => Has_Comments);

         Next_Curr_Pos := Next_Curr_Pos + 6; --  '+ 6' for ' WITH '

         if Has_Comments or else
            Next_Curr_Pos + 3 > Max_Line_Length
         then
            Separate_Line := True;
            goto Done;
         end if;

      end if;

      All_Associations := new Asis.Element_List'(Get_All_Associations);

      if All_Associations'Length = 0
        and then
         (Arg_Kind = A_Record_Aggregate
            or else
          Arg_Kind = An_Extension_Aggregate)
      then
         --  (null record);
         Separate_Line := Next_Curr_Pos + 13 > Max_Line_Length;
      else
         Check_List_Layout
           (Curr_Pos             => Next_Curr_Pos,
            Elements             => All_Associations.all,
            Needs_Separate_Lines => Separate_Line);
      end if;

      <<Done>> Set_Comp_Asctns_On_Sep_Lines (Separate_Line);

      --  Setting the starting position of the aggregate (and its
      --  associations)
      --
      --  The goal is to have a smart formatting for multi-level aggregates
      --
      --  There is no limits for getting smarter and smarter... At the moment
      --  we are not very clever

      if Separate_Line and then
         Encl_Kind in A_Record_Component_Association ..
                      An_Array_Component_Association
      then
         Start_Pos := Get_Output_Pos + 1;
      end if;

      Set_Comp_Asctns_Start_Pos (Start_Pos);

      --  Computing the associations alignment

      if Separate_Line      and then
         Align_Arrows       and then
         Arg_Kind /= A_Positional_Array_Aggregate
      then

         if Start_Pos = 0 then
            Start_Pos := (Logical_Depth + 1) * PP_Indentation + 1;
         end if;

         if All_Associations = null then
            --  May be the case if we've jumped to the Done label
            All_Associations := new Asis.Element_List'(Get_All_Associations);
         end if;

         Compute_Associations_Alignment
           (Start_Pos => Start_Pos,
            Elements  => All_Associations.all,
            Arrow_Pos => Arrow_Position,
            Max_Expr  => Max_Expr_Len);

      end if;

      Set_Arrow_Start_Pos (Arrow_Position);

      Free (All_Associations);

   end Compute_Aggregate_Layout;

   -------------------------
   -- Compute_Call_Layout --
   -------------------------

   procedure Compute_Call_Layout (E : Asis.Element) is
      Arg_Kind  : constant Flat_Element_Kinds := Flat_Element_Kind (E);
      Encl_Kind : constant Flat_Element_Kinds := Get_Enclosing_Kind;

      All_Associations   : Element_List_Access;
      Called_Pref        : Asis.Element;

      Next_Curr_Pos      : Natural;
      Old_Next_Curr_Pos  : Natural;
      Next_Space         : Natural;
      Next_Call_Space    : Natural := 0;
      Has_Comments       : Boolean := False;
      Longest_Par        : Positive;

      --  Parameters to set:

      Pars_Flag      : Boolean := False;
      Move_Call_Flag : Boolean := False;
      Start_Par      : Natural := 0;
      Start_Call     : Natural := 0;
      Start_Arrow    : Natural := 0;
      Expr_Size      : Natural := 0;

      function Get_All_Associations return Element_List;
      --  returns the list of all the parameter associations of the call

      function Get_All_Associations return Element_List is
      begin

         case Arg_Kind is
            when A_Function_Call =>

               return Function_Call_Parameters (E);

            when An_Entry_Call_Statement    |
                 A_Procedure_Call_Statement =>

               return Call_Statement_Parameters (E);

            when Flat_Pragma_Kinds =>

               return Pragma_Argument_Associations (E);

            when others =>

               return Nil_Element_List;

         end case;

      end Get_All_Associations;

   begin

      if Arg_Kind = A_Function_Call then
         Called_Pref := Prefix (E);
      elsif Arg_Kind not in Flat_Pragma_Kinds then
         Called_Pref := Called_Name (E);
      end if;

      Next_Curr_Pos := Get_Output_Pos;

      if Arg_Kind = A_Function_Call and then
         not Space_Just_Printed     and then
         not Postponed_Space
      then
         Next_Curr_Pos := Next_Curr_Pos + 1;
         --  '+ 1' is for ' ' before the call
      end if;

      if Arg_Kind not in Flat_Pragma_Kinds then
         Detect_Possible_Layout
           (The_Element     => Called_Pref,
            Space_In_Output => Next_Space,
            Comment_Inside  => Has_Comments);

         if Arg_Kind = A_Function_Call          and then
            ((Encl_Kind = A_Parameter_Association and then
             not (Is_Nil (Formal_Parameter (Get_Enclosing_Element))))
            or else
             Last_Dlm = Assignment_Dlm)
         then
            Detect_Possible_Layout
              (The_Element     => E,
               Space_In_Output => Next_Call_Space,
               Comment_Inside  => Has_Comments);

         end if;

      else
         --  In case of a pragma, we have already printed out the keyword
         --  'PRAGMA" and the pragma name.
         Next_Space := 0;
      end if;

      Old_Next_Curr_Pos := Next_Curr_Pos;
      Next_Curr_Pos     := Next_Curr_Pos + Next_Space + 2;
      --  '+ 2" is for ' (' or ';'

      if Next_Curr_Pos > Max_Line_Length
        or else
           (((Encl_Kind = A_Parameter_Association and then
              not (Is_Nil (Formal_Parameter (Get_Enclosing_Element))))
           or else
             Last_Dlm = Assignment_Dlm)
         and then
           (Old_Next_Curr_Pos + Next_Call_Space + 1 > Max_Line_Length or else
            Has_Comments))
      then

         --  If we are in multi-level call, we try to move the call left to
         --  provide some reasonable layout...

         Move_Call_Flag := True;

         if Arg_Kind = A_Function_Call and then
            (Encl_Kind = A_Parameter_Association or else
             Last_Dlm = Assignment_Dlm)
         then

            if Encl_Kind = A_Parameter_Association then
               Start_Call := Enclosing_Association_Start_Pos;
            else
               Start_Call := Logical_Depth * PP_Indentation + 1;
            end if;

            if Start_Call > 0 then
               Start_Call := Start_Call + PP_Indentation;
            else
               Start_Call := (Logical_Depth + 1) * PP_Indentation + 1;
            end if;

            if Start_Call + Next_Space > Max_Line_Length then

               Start_Call :=
                  (Start_Call / PP_Indentation) * PP_Indentation +
                  1 - PP_Indentation;

               while Start_Call + Next_Space > Max_Line_Length loop
                  Start_Call := Start_Call - PP_Indentation;

                  if Start_Call <= PP_Indentation then
                     Start_Call := 1;
                     exit;
                  end if;
               end loop;

            end if;

         else
            --  What else can we do with a procedure or entry call?...
            Start_Call := 1;
         end if;

         if Start_Call = 1 then
            --  The situation is definitely hard - we can not provide any
            --  reasonable formatting
            Error ("the line is too long");
         end if;

         Next_Curr_Pos  := Start_Call + Next_Space + 2;

      end if;

      if Arg_Kind = A_Function_Call and then
         Take_Then_Into_Account
      then
         Next_Curr_Pos := Next_Curr_Pos + 5;
         --  We need this to avoid the formatting like this:
         --
         --        if Some_Condition (Some_Par)
         --        then
         --           ...
      end if;

      --  Now, computing the parameter associations layout

      All_Associations := new Element_List'(Get_All_Associations);

      Check_List_Layout_And_Max_Elem
        (Curr_Pos             => Next_Curr_Pos,
         Elements             => All_Associations.all,
         Needs_Separate_Lines => Pars_Flag,
         Max_Element_Space    => Longest_Par);

      if Pars_Flag then
         --  We have to set the starting position for associations

         if Move_Call_Flag then
            Start_Par := Start_Call;
         elsif Arg_Kind = A_Function_Call then
            Start_Par := Output_Pos;
         else
            Start_Par := Logical_Depth * PP_Indentation;
         end if;

         Start_Par := Start_Par + PP_Indentation;

         if not Move_Call_Flag then
            Start_Par := Start_Par + 1;
         end if;

         if Arg_Kind = A_Function_Call                    and then
            Start_Par + Longest_Par + 1 > Max_Line_Length and then
            Last_Dlm = Arrow_Dlm                          and then
            not Move_Call_Flag
         then
            --  Here we have a problem: even we place each parameter
            --  association on a separate line, there is not enough place
            --  for the longest association. here we try to move all the call
            --  left.
            --
            --  The only situation when we try to get the reasonable layout
            --  is if the call is the function call being the argument of
            --  some parameter association (or the right part of component
            --   association... what else?...

            Move_Call_Flag := True;

            --  We are in the association after '=>'
            --  And if we are here, we may be sure that this association
            --  starts from the new line
            Start_Call := Enclosing_Association_Start_Pos + PP_Indentation;

            --  Now check that it really helps, and if not - just move
            --  all the call to the leftmost position:

--            if not (Start_Call + PP_Indentation + 1 +
--                    Longest_Par + 1 > Available_In_Output)
--            then
--               Start_Call := 1;
--            end if;

--            Start_Par := Start_Call + PP_Indentation + 1;
            Start_Par := Start_Call + PP_Indentation;
         end if;

      end if;

      --  Setting the layout info:
      Set_Pars_On_Sep_Lines  (Separate_Line => Pars_Flag);
      Set_Move_All_Call_Left (Move_It       => Move_Call_Flag);
      Set_Start_Par_Pos      (Position      => Start_Par);
      Set_Start_Call_Pos     (Position      => Start_Call);

      --  Compute and set alignment:
      if Pars_Flag and then Align_Arrows then

         Compute_Associations_Alignment
           (Start_Pos => Start_Par,
            Elements  => All_Associations.all,
            Arrow_Pos => Start_Arrow,
            Max_Expr  => Expr_Size);

      end if;

      Set_Arrow_Start_Pos (Start_Arrow);

      Free (All_Associations);

   end Compute_Call_Layout;

   ---------------------------------
   -- Compute_Case_Choices_Layout --
   ---------------------------------

   procedure Compute_Case_Choices_Layout (E : Asis.Element) is
      Arg_Kind : constant Flat_Element_Kinds := Flat_Element_Kind (E);

      function Get_Choices return Element_List;
      --  returns the list of choices from E

      function Get_Choices return Element_List is
      begin

         case Arg_Kind is
            when A_Case_Path =>
               return Case_Statement_Alternative_Choices (E);
            when An_Exception_Handler =>
               return Exception_Choices (E);
            when A_Variant =>
               return Variant_Choices (E);
            when A_Record_Component_Association =>
               return Record_Component_Choices (E);
            when An_Array_Component_Association =>
               return Array_Component_Choices (E);
            when others =>
               return Nil_Element_List;
         end case;

      end Get_Choices;

      Choices : constant Element_List := Get_Choices;

      --  Layout parameters to compute and to set
      Choices_On_Separate_Lines : Boolean := False;
      Start_Choice              : Natural := 0;
      Start_Vertical_Line       : Natural := 0;

      Choice_Space         : Positive;
      Tmp_Pos              : Positive;
      Alignment_Impossible : Boolean;
      Tmp_Layout           : Layout_Info;

   begin
      Current_State.Layout.Pos1  := 0;
      Current_State.Layout.Pos2  := 0;
      Current_State.Layout.Pos3  := 0;
      Current_State.Layout.Flag1 := False;
      Current_State.Layout.Flag2 := False;
      Current_State.Layout.Flag5 := False;

      if Choices'Length > 1 then

         if Is_New_Output_Line and then
            Arg_Kind not in Flat_Association_Kinds  --   ???
         then
            PP_Space;
         end if;

         Tmp_Pos := Get_Output_Pos;

         Check_List_Layout
           (Curr_Pos             => Tmp_Pos,
            Elements             => Choices,
            Needs_Separate_Lines => Choices_On_Separate_Lines);

         if not Choices_On_Separate_Lines then
            --  We can not be sure that we can place all the choices into
            --  one line:
            --  - Check_List_Layout supposes that the list elements are
            --    separated by two characters (',' and ' '), but here we
            --    have three characters ' | '
            --  - we have also to place ' =>'

            if Tmp_Pos + (Choices'Length - 1) + 3 > Max_Line_Length then
               Choices_On_Separate_Lines := True;
            end if;

         end if;

         if Choices_On_Separate_Lines then
            Start_Choice := Get_Output_Pos;

            Align_Info.Init;

               for J in Choices'Range loop

                  Detect_Possible_Layout
                    (The_Element     => Choices (J),
                     Space_In_Output => Choice_Space,
                     Comment_Inside  => Alignment_Impossible);

                  if Alignment_Impossible or else
                     Choice_Space + 2 > Available_In_Output
                  then
                     Alignment_Impossible := True;
                     exit;
                  else
                     Tmp_Layout.Pos1 := Start_Choice + Choice_Space + 1;
                     Align_Info.Append (Tmp_Layout);
                  end if;

               end loop;

               if not Alignment_Impossible then

                  for J in 1 .. Align_Info.Last loop

                     if Align_Info.Table (J).Pos1 > Start_Vertical_Line then
                        Start_Vertical_Line := Align_Info.Table (J).Pos1;
                     end if;

                  end loop;

               end if;

               Current_State.Layout.Pos1  := Start_Choice;
               Current_State.Layout.Pos2  := Start_Vertical_Line;
               Current_State.Layout.Flag1 := Choices_On_Separate_Lines;

               if Start_Vertical_Line > 0 then
                  Current_State.Layout.Flag5 := True;
               end if;

         end if;

      end if;

   end Compute_Case_Choices_Layout;

   ----------------------------------------
   -- Compute_Component_Clause_Alignment --
   ----------------------------------------

   procedure Compute_Component_Clause_Alignment
     (Start_Pos  :     Natural;
      Elements   :     Asis.Element_List;
      At_Pos     : out Natural)
   is
      Comp_Name : Asis.Element;
      --  Component name in a component clause

      At_Pos_Tmp : Natural;
   begin

      At_Pos := 0;

      for J in Elements'Range loop

         Comp_Name := Representation_Clause_Name (Elements (J));

         --  ??? This is a very primitive approach! We did not check if the
         --  component clause can fit the line and if it contains comments
         --  inside
         --  ??? (The same is the case for all the other alignments!!!)

         At_Pos_Tmp := Start_Pos + Name_Image (Comp_Name)'Length + 1;

         At_Pos := Natural'Max (At_Pos, At_Pos_Tmp);

      end loop;

   end Compute_Component_Clause_Alignment;

   ---------------------------------------
   -- Compute_Context_Clauses_Alignment --
   ---------------------------------------

   procedure Compute_Context_Clauses_Alignment
     (Clause_Elements : Asis.Element_List;
      Use_Pos         : out Natural)
   is
      With_Space           : Natural := 0;
      Alignment_Impossible : Boolean;
      Tmp_Layout           : Layout_Info;
   begin
      Use_Pos := 0;

      if Clause_Elements'Length <= 3 then
         return;
      end if;

      Align_Info.Init;

      for J in Clause_Elements'Range loop

         if Flat_Element_Kind (Clause_Elements (J)) = A_With_Clause then

            Detect_Possible_Layout
              (The_Element     => Clause_Elements (J),
               Space_In_Output => With_Space,
               Comment_Inside  => Alignment_Impossible);

            if Alignment_Impossible then
               exit;
            else
               Tmp_Layout.Pos1 := With_Space + 1;
               Align_Info.Append (Tmp_Layout);
            end if;

         end if;

      end loop;

      if not Alignment_Impossible then
         --  At the moment we just compute the maximum length of with clause

         for J in 1 .. Align_Info.Last loop

            if Align_Info.Table (J).Pos1 > Use_Pos then
               Use_Pos := Align_Info.Table (J).Pos1;
            end if;

         end loop;

      end if;

   end Compute_Context_Clauses_Alignment;

   ------------------------------------
   -- Compute_Declarations_Alignment --
   ------------------------------------

   procedure Compute_Declarations_Alignment
     (Start_Pos  :     Natural;
      Elements   :     Asis.Element_List;
      Colon_Pos  : out Natural;
      Assign_Pos : out Natural)
   is
      Nms                    : Element_List_Access;

      Tmp_Layout             : Layout_Info;
      Next_Pos               : Natural;
      Alignment_Impossible   : Boolean;
      Def_El                 : Element;
      Def_Space              : Natural := 0;
      Comments_Present       : Boolean;

      function Definition (E : Element) return Element;
      --  Returns the definition part of the declaration. Different ASIS
      --  queries may be needed for different kinds of declarations, so this
      --  function takes care of this.

      function Assignment_Present (E : Element) return Boolean;
      --  Check if E has an assignment sign (and the initialization
      --  expression) as a part of its structure

      function Assignment_Present (E : Element) return Boolean is
         Arg_Kind : constant Flat_Element_Kinds := Flat_Element_Kind (E);
         Result   : Boolean                     := False;
      begin

         case Arg_Kind is
            when An_Integer_Number_Declaration |
                 A_Real_Number_Declaration     |
                 A_Constant_Declaration        =>
               Result := True;

            when A_Variable_Declaration       |
                 A_Discriminant_Specification |
                 A_Component_Declaration      |
                 A_Parameter_Specification    |
                 A_Formal_Object_Declaration  =>

               Result := not Is_Nil (Initialization_Expression (E));

            when others =>
               null;
         end case;

         return Result;
      end Assignment_Present;

      function Definition (E : Element) return Element is
      begin

         case Flat_Element_Kind (E) is
            when A_Discriminant_Specification   |
                 A_Parameter_Specification      |
                 A_Formal_Object_Declaration    |
                 An_Object_Renaming_Declaration =>

               if ASIS_UL.Options.ASIS_2005_Mode then
                  return Object_Declaration_View (E);
               else
                  return Declaration_Subtype_Mark (E);
               end if;

            when A_Variable_Declaration          |
                 A_Constant_Declaration          |
                 A_Deferred_Constant_Declaration |
                 A_Component_Declaration         =>

               return Object_Declaration_View (E);

            when An_Integer_Number_Declaration |
                 A_Real_Number_Declaration     =>

               return Nil_Element;

            when others =>
               pragma Assert (False);
               return Nil_Element;
         end case;

      end Definition;

   begin
      Colon_Pos  := 0;
      Assign_Pos := 0;

      if Elements'Length = 0 or else
         (Elements'Length = 1 and then not Is_Function (Get_Enclosing_Kind))
      then
         return;
      end if;

      Align_Info.Init;

      for J in Elements'Range loop

         Nms := new Element_List'(Names (Elements (J)));

         Next_Pos := Start_Pos;

         Check_List_Layout
           (Curr_Pos             => Next_Pos,
            Elements             => Nms.all,
            Needs_Separate_Lines => Alignment_Impossible);

         if Alignment_Impossible then
            --  we just give up
            exit;
         end if;

         Tmp_Layout.Pos1 := Next_Pos - 1;

         if Align_Asign_In_Decl then

            Def_El := Definition (Elements (J));

            if Is_Nil (Def_El) then
               Def_Space := 0;
            else
               Detect_Possible_Layout
                 (The_Element     => Def_El,
                  Space_In_Output => Def_Space,
                  Comment_Inside  => Comments_Present);
            end if;

            if Comments_Present or else
               not Assignment_Present (Elements (J))
            then
               Def_Space := 0;

            else
               --  Compute the position of the assignment sign, taking
               --  into account all the additional keywords

               Next_Pos := Next_Pos + 2;

               case Mode_Kind (Elements (J)) is
                  when Not_A_Mode =>
                     null;
                  when A_Default_In_Mode =>
                     null;
--                     if Add_Default_In then
--                        Def_Space := Def_Space + 3;
--                     end if;

                  when An_In_Mode =>
                     Def_Space := Def_Space + 3;
                  when An_Out_Mode =>
                     Def_Space := Def_Space + 4;
                  when An_In_Out_Mode =>
                     Def_Space := Def_Space + 7;
               end case;

               if Trait_Kind (Elements (J)) = An_Aliased_Trait then
                  Def_Space := Def_Space + 8;
               end if;

               case Flat_Element_Kind (Elements (J)) is
                  when A_Constant_Declaration          |
                       A_Deferred_Constant_Declaration |
                       An_Integer_Number_Declaration   |
                       A_Real_Number_Declaration       =>

                     Def_Space := Def_Space + 9;
                  when others =>
                     null;
               end case;

               if Next_Pos + Def_Space + 1 > Max_Line_Length then
                  Def_Space := 0;
               end if;

            end if;

         end if;

         Tmp_Layout.Pos2 := Def_Space;

         Align_Info.Append (Tmp_Layout);

         Free (Nms);
      end loop;

      if not Alignment_Impossible then
         --  At the moment we just compute the maximum length of
         --  name list.

         for J in 1 .. Align_Info.Last loop

            if Align_Info.Table (J).Pos1 > Colon_Pos then
               Colon_Pos := Align_Info.Table (J).Pos1;
            end if;

            if Align_Info.Table (J).Pos2 > Assign_Pos then
               Assign_Pos := Align_Info.Table (J).Pos2;
            end if;

         end loop;

         if Align_Asign_In_Decl                          and then
            Assign_Pos > 0                               and then
            Assign_Pos + Colon_Pos + 4 < Max_Line_Length

         then
            Assign_Pos := Assign_Pos + Colon_Pos + 3;
         else
            Assign_Pos := 0;
         end if;

      end if;

   end Compute_Declarations_Alignment;

   ----------------------------------
   -- Compute_Discriminants_Layout --
   ----------------------------------

   procedure Compute_Discriminants_Layout (E : Asis.Element) is
      Next_Curr_Pos  : Natural;
      Separate_Lines : Boolean := False;
   begin
      Next_Curr_Pos := Get_Output_Pos + 2; --  '+ 2' is for ' ('

      Check_List_Layout
        (Curr_Pos             => Next_Curr_Pos,
         Elements             => Discriminants (E),
         Needs_Separate_Lines => Separate_Lines);

      Current_State.Layout.Flag1 := Separate_Lines;

   end Compute_Discriminants_Layout;

   -----------------------------
   -- Compute_Enum_Def_Layout --
   -----------------------------

   procedure Compute_Enum_Def_Layout (E : Asis.Element) is
      Next_Space         : Natural;
      Has_Comments       : Boolean := False;

      Sep_Lines          : Boolean := False;
   begin
      Current_State.Layout.Pos1  := 0;
      Current_State.Layout.Pos2  := 0;
      Current_State.Layout.Pos3  := 0;
      Current_State.Layout.Flag1 := False;
      Current_State.Layout.Flag2 := False;

      Detect_Possible_Layout
        (The_Element     => E,
         Space_In_Output => Next_Space,
         Comment_Inside  => Has_Comments);

      if Has_Comments
        or else Output_Pos + Next_Space + 2 > Max_Line_Length
        or else Enumeration_Literal_Declarations (E)'Length > Max_Enums_On_Line
      then
         Sep_Lines := True;
      end if;

      Current_State.Layout.Flag1 := Sep_Lines;
   end Compute_Enum_Def_Layout;

   -------------------------------------
   --  Compute_Generic_Actuals_Layout --
   -------------------------------------

   procedure Compute_Generic_Actuals_Layout is
      All_Associations : constant Element_List :=
         Generic_Actual_Part (Get_Enclosing_Element);
      Start_Arrow      : Natural := 0;
      Expr_Size        : Natural := 0;
   begin

      Compute_Associations_Alignment
        (Start_Pos => Output_Pos,
         Elements  => All_Associations,
         Arrow_Pos => Start_Arrow,
         Max_Expr  => Expr_Size);

      if Start_Arrow > 0 then
         Set_Generic_Arrow_Start_Pos (Start_Arrow);
      end if;

   end Compute_Generic_Actuals_Layout;

   -------------------------------
   -- Compute_Infix_Call_Layout --
   -------------------------------

   procedure Compute_Infix_Call_Layout (E : Asis.Element) is

      Arg_Kind : constant Flat_Element_Kinds := Flat_Element_Kind (E);
      Next_Pos : Natural                     := Output_Pos;

      Next_Space   : Natural;
      Has_Comments : Boolean;

      Op_Kind    : Flat_Element_Kinds := Arg_Kind;

      Same_Priority_Sequence : Boolean := False;
      Enclosing_Call : Asis.Element := Nil_Element;
      Encl_Call_Kind : Flat_Element_Kinds;

      Move_In_Stack : Natural := 0;

   begin
      Current_State.Layout.Pos1  := 0;   --  ???
      Current_State.Layout.Pos2  := 0;   --  ???
      Current_State.Layout.Pos3  := 0;   --  ???
      Current_State.Layout.Flag1 := False;   --  ???
      Current_State.Layout.Flag2 := False;   --  ???

      if Postponed_Space and then
         not Space_Just_Printed
      then
         Next_Pos := Next_Pos + 1;
      end if;

      --  First, check if we are in the sequence of same-priority operations

      case Get_Enclosing_Kind is

         when An_And_Then_Short_Circuit |
              An_Or_Else_Short_Circuit  =>
            Enclosing_Call := Get_Enclosing_Element;
         when A_Parameter_Association =>
            Move_In_Stack := 1;
            Enclosing_Call := Get_Enclosing_Element (Move_In_Stack);
         when others =>
            null;
      end case;

      if not Is_Nil (Enclosing_Call) then

         Encl_Call_Kind := Flat_Element_Kind (Enclosing_Call);

         case Encl_Call_Kind is

            when An_And_Then_Short_Circuit |
                 An_Or_Else_Short_Circuit  =>

               Same_Priority_Sequence := Encl_Call_Kind = Arg_Kind;

            when A_Function_Call =>

               if Arg_Kind = A_Function_Call then
                  Op_Kind := Flat_Element_Kind (Prefix (E));
               end if;

               if not Is_Prefix_Call (Enclosing_Call) then
                  Same_Priority_Sequence :=
                     Are_Of_Same_Priority
                       (Op_Kind,
                        Flat_Element_Kind (Prefix (Enclosing_Call)));
               end if;

            when others =>
               null;
         end case;

      end if;

      if Same_Priority_Sequence then
         Current_State.Layout := Traversal_Stack.Top (Move_In_Stack).Layout;
         return;
      end if;

      --  If we are here, we have to compute the layout

      --  First, check if we have a simple case: all the call can fit one
      --  line

      Detect_Possible_Layout
        (The_Element     => E,
         Space_In_Output => Next_Space,
         Comment_Inside  => Has_Comments);

      if Take_Then_Into_Account then
         Next_Space := Next_Space + 5;
         --  We need this to avoid the formatting like this:
         --
         --        if Something + Something_Else
         --        then
         --           ...
      end if;

      if Has_Comments or else
         not (Next_Pos + Next_Space + 1 < Max_Line_Length)
      then
         --  We add 1 in case if ';' is needed after the call
         Current_State.Layout.Pos1  := Next_Pos;
         Current_State.Layout.Flag1 := True;
      end if;

   end Compute_Infix_Call_Layout;

   -----------------------------
   -- Compute_Obj_Decl_Layout --
   -----------------------------

   procedure Compute_Obj_Decl_Layout (E : Asis.Element) is
      Arg_Kind   : constant Flat_Element_Kinds := Flat_Element_Kind (E);
      Next_Pos   : Natural;
      Line_Break : Boolean;

      Tmp_El     : Asis.Element;
      Tmp_Space  : Natural;

      function Get_Definition (E : Asis.Element) return Asis.Element;
      --  Returns the definition part of the declaration. This function
      --  is trivial at the moment, but some more variants may be added when
      --  we extend the number of cases where Compute_Obj_Decl_Layout is
      --  applied

      function Get_Definition (E : Asis.Element) return Asis.Element is
         Result : Asis.Element := Nil_Element;
      begin
         case Arg_Kind is
            when A_Variable_Declaration .. A_Deferred_Constant_Declaration =>
               Result := Object_Declaration_View (E);
            when others =>
               null;
         end case;

         return Result;
      end Get_Definition;

      function Get_Init_Expression (E : Asis.Element) return Asis.Element;
      --  Returns the initialization expression part of the declaration, if
      --  any.

      function Get_Init_Expression (E : Asis.Element) return Asis.Element is
         Result : Asis.Element := Nil_Element;
      begin
         case Arg_Kind is
            when A_Variable_Declaration        |
                 A_Constant_Declaration        |
                 An_Integer_Number_Declaration |
                 A_Real_Number_Declaration     |
                 A_Discriminant_Specification  |
                 A_Component_Declaration       |
                 A_Parameter_Specification     |
                 A_Formal_Object_Declaration   =>

               Result := Initialization_Expression (E);
            when others =>
               null;
         end case;

         return Result;
      end Get_Init_Expression;

   begin
      Current_State.Layout.Flag1 := False;   --  ???
      Current_State.Layout.Flag2 := False;   --  ???

      Next_Pos := Logical_Depth * PP_Indentation + 1;

      Check_List_Layout
        (Curr_Pos             => Next_Pos,
         Elements             => Names (E),
         Needs_Separate_Lines => Line_Break);

      if Line_Break then
         Current_State.Layout.Flag1 := True;   --  ???
         Next_Pos := (Logical_Depth + 1) * PP_Indentation + 1;
      end if;

      Next_Pos := Natural'Max (Next_Pos, Colon_In_Paragraph);

      Tmp_El := Get_Definition (E);

      Detect_Possible_Layout
        (The_Element     => Tmp_El,
         Space_In_Output => Tmp_Space,
         Comment_Inside  => Line_Break);

      if Arg_Kind = A_Constant_Declaration or else
         Arg_Kind = A_Deferred_Constant_Declaration
      then
         Tmp_Space := Tmp_Space + 9;
      end if;

      if Trait_Kind (E) = An_Aliased_Trait then
         Tmp_Space := Tmp_Space + 8;
      end if;

      if Next_Pos + Tmp_Space + 3 > Max_Line_Length then
         --  '+ 3' stands for ' :=', this also covers ';'
         Current_State.Layout.Flag1 := True;   --  ???
         Next_Pos := (Logical_Depth + 1) * PP_Indentation + 1;
      end if;

      Tmp_El := Get_Init_Expression (E);

      if Is_Nil (Tmp_El) or else
         Flat_Element_Kind (Tmp_El) in A_Record_Aggregate ..
                                       A_Named_Array_Aggregate
      then
         return;
      end if;

      Next_Pos :=
         Natural'Max (Next_Pos + Tmp_Space + 2, Assign_In_Paragraph) + 3;

      Detect_Possible_Layout
        (The_Element     => Tmp_El,
         Space_In_Output => Tmp_Space,
         Comment_Inside  => Line_Break);

      Next_Pos := Next_Pos + Tmp_Space + 1;

      if Next_Pos > Max_Line_Length then
         Current_State.Layout.Flag2 := True;   --  ???
      end if;

   end Compute_Obj_Decl_Layout;

   ------------------------------
   -- Compute_Parameter_Layout --
   ------------------------------

   procedure Compute_Parameter_Layout is
      Encl_El   : constant Asis.Element       := Get_Enclosing_Element;
      Encl_Kind : constant Flat_Element_Kinds := Flat_Element_Kind (Encl_El);

      All_Specs      : Element_List_Access;
      N_All_Spec     : Natural;
      Tmp_Res        : Asis.Element;

      Next_Curr_Pos  : Natural;
      Next_Space     : Natural;
      Has_Comments   : Boolean            := False;

      Separate_Lines : Boolean            := False;

      Colon_At       : Natural := 0;
      Assign_At      : Natural := 0;

      function Get_All_Specs return Element_List;
      --  returns the list of all the parameter specifications the argument
      --  belongs to

      function Get_All_Specs return Element_List is
      begin

         case Encl_Kind is
            when A_Procedure_Declaration          |
                 A_Null_Procedure_Declaration     |
                 A_Function_Declaration           |
                 A_Procedure_Body_Declaration     |
                 A_Function_Body_Declaration      |
                 A_Procedure_Renaming_Declaration |
                 A_Function_Renaming_Declaration  |
                 An_Entry_Declaration             |
                 An_Entry_Body_Declaration        |
                 A_Procedure_Body_Stub            |
                 A_Function_Body_Stub             |
                 A_Generic_Function_Declaration   |
                 A_Generic_Procedure_Declaration  |
                 A_Formal_Function_Declaration    |
                 A_Formal_Procedure_Declaration   =>

               return Parameter_Profile (Encl_El);

            when An_Access_To_Procedure                     |
                 An_Access_To_Protected_Procedure           |
                 An_Access_To_Function                      |
                 An_Access_To_Protected_Function            |
                 An_Anonymous_Access_To_Procedure           |
                 An_Anonymous_Access_To_Protected_Procedure |
                 An_Anonymous_Access_To_Function            |
                 An_Anonymous_Access_To_Protected_Function  =>

               return Access_To_Subprogram_Parameter_Profile (Encl_El);

            when An_Accept_Statement =>

               return Accept_Parameters (Encl_El);

            when others =>

               return Nil_Element_List;

         end case;

      end Get_All_Specs;

   begin
      --  So, we have to decide if all the parameter specification (??? not
      --  only) elements may be placed in the same line. Note, that this the
      --  same line where the enclosing construct is started

      All_Specs := new Asis.Element_List'(Get_All_Specs);

      --  The trivial criterium: if we have more then three parameter specs or
      --  more then two in a function - we always use separate lines for
      --  parameter specs.

      N_All_Spec := All_Specs'Length;

      if N_All_Spec > 3 or else
         (N_All_Spec > 2 and then Is_Function (Encl_Kind))
      then
         Separate_Lines := True;
         goto Done;
      end if;

      --  Now, trying to simulate putting all the parameter specs in one line:

      --  First, check is we already are on a new line:

      Next_Curr_Pos := Output_Pos + 2; --  '+ 2' stands for ' ('

      if Is_New_Output_Line or else
         Next_Curr_Pos + 7 > Max_Line_Length
      then
         --  May be, this is not the best decision ???
         Separate_Lines := True;
         goto Done;
      end if;

      Check_List_Layout
        (Curr_Pos             => Next_Curr_Pos,
         Elements             => All_Specs.all,
         Needs_Separate_Lines => Separate_Lines);

      --  Check if there is a room for 'IS'
      if not Separate_Lines and then
         Next_Curr_Pos + 2 > Max_Line_Length --  '+ 2' stands for 'is'
      then
         Separate_Lines := True;
      end if;

      --  in case of a function, we have to check if we have enough space
      --  for return Result_Type

      if not Separate_Lines and then
         Is_Function (Encl_Kind)
      then

         Next_Curr_Pos := Next_Curr_Pos + 6;  --  6 for 'RETURN'

         if Next_Curr_Pos + 5 > Max_Line_Length then
            --  '+ 5' is the most optimistic estimation 'return A is'
            Separate_Lines := True;
         else
            --  We have to compute the length of the return type

            if Encl_Kind = An_Access_To_Function
              or else Encl_Kind = An_Access_To_Protected_Function
              or else Encl_Kind = An_Anonymous_Access_To_Function
              or else Encl_Kind = An_Anonymous_Access_To_Protected_Function
            then
               Tmp_Res := Access_To_Function_Result_Profile (Encl_El);
            else
               Tmp_Res := Result_Profile (Encl_El);
            end if;

            Detect_Possible_Layout
              (The_Element     => Tmp_Res,
               Space_In_Output => Next_Space,
               Comment_Inside  => Has_Comments);

            Next_Curr_Pos := Next_Curr_Pos + 1 + Next_Space;
            --  '+ 1' means the space between 'RETURN' and the type name,

            if Has_Comments or else
               Next_Curr_Pos + 3 > Max_Line_Length
               --  '+ 3' means ' is'
            then
               Separate_Lines := True;
            end if;

         end if;

      end if;

      --  Check that there is enough room for "is abstract;" in case of
      --  abstract subprogram or for "is separate;" in case of body stub

      if (Trait_Kind (Encl_El) = An_Abstract_Trait or else
          Encl_Kind in A_Flat_Body_Stub)
        and then
         Next_Curr_Pos + 13 > Max_Line_Length
      then
         Separate_Lines := True;
      end if;

      <<Done>> Set_Par_Spec_On_Sep_Lines (Separate_Lines);

      --  Computing the alignment.

      if Separate_Lines and then Align_Colons_In_Decl then

         case Encl_Kind is

            when A_Procedure_Declaration          |
                 A_Null_Procedure_Declaration     |
                 A_Function_Declaration           |
                 A_Formal_Function_Declaration    |
                 A_Formal_Procedure_Declaration   |
                 A_Procedure_Renaming_Declaration |
                 A_Function_Renaming_Declaration  |
                 An_Entry_Declaration             |
                 A_Procedure_Body_Stub            |
                 A_Function_Body_Stub             |
                 A_Generic_Procedure_Declaration  |
                 A_Generic_Function_Declaration   |
                 An_Access_To_Function            |
                 An_Access_To_Protected_Function  =>

               Next_Curr_Pos := (Logical_Depth + 1) * PP_Indentation + 1;

            when others =>
               Next_Curr_Pos := Logical_Depth * PP_Indentation + 1;
         end case;

         Compute_Declarations_Alignment
           (Start_Pos  => Next_Curr_Pos,
            Elements   => All_Specs.all,
            Colon_Pos  => Colon_At,
            Assign_Pos => Assign_At);

         --  And now, in case of a function, we have to take into account
         --  the return type

         if Is_Function (Encl_Kind) then

            if Colon_At < Next_Curr_Pos + 5 then
               Assign_At := Assign_At + (Next_Curr_Pos + 5 - Colon_At);
               Colon_At  := Next_Curr_Pos + 5;
               --  ??? may be, here we have to recompute Assign_At if it
               --  ??? is too large
            end if;

         end if;

         Set_Assign_Start_Pos (Assign_At);
         Set_Colon_Start_Pos  (Colon_At);

      end if;

      Free (All_Specs);

   end Compute_Parameter_Layout;

   --------------------------------------------
   -- Compute_Simple_Expression_Range_Layout --
   --------------------------------------------

   procedure Compute_Simple_Expression_Range_Layout (E : Asis.Element) is
      Next_Space   : Positive;
      Has_Comments : Boolean;

      Separate_Line : Boolean := False;

      Then_Len : Natural := 0;
   begin
      Current_State.Layout.Pos1  := 0;
      Current_State.Layout.Pos2  := 0;
      Current_State.Layout.Pos3  := 0;
      Current_State.Layout.Flag1 := False;
      Current_State.Layout.Flag2 := False;

      if Get_Enclosing_Kind (1) in An_If_Path .. An_Elsif_Path and then
         Last_If_Path_Start = Current_Out_Line
      then
         Then_Len := 5;
      end if;

      Detect_Possible_Layout
        (The_Element     => E,
         Space_In_Output => Next_Space,
         Comment_Inside  => Has_Comments);

      if Has_Comments or else
         Next_Space + 2 + Then_Len > Available_In_Output
      then
         Separate_Line := True;
      end if;

      Current_State.Layout.Flag1 := Separate_Line;

      if not Has_Comments then
         --  Check if we have to start the right expression from the new line:

         if Next_Space + (Logical_Depth + 1) * PP_Indentation + 1 <
            Max_Line_Length
         then
            Separate_Line := False;
         end if;
      end if;

      Current_State.Layout.Flag2 := Separate_Line;

   end Compute_Simple_Expression_Range_Layout;

   ---------------------------
   -- Short_Circuit_Padding --
   ---------------------------

   function Short_Circuit_Padding return Positive is
      Encl_Expr_Kind : constant Flat_Element_Kinds := Get_Enclosing_Kind;
      Encl_Constr    : Asis.Element                := Get_Enclosing_Element;

      Result : Positive := Start_Par_Pos - 1;
   begin

      while Flat_Element_Kind (Encl_Constr) = Encl_Expr_Kind loop
         Encl_Constr := Enclosing_Element (Encl_Constr);
      end loop;

      case Flat_Element_Kind (Encl_Constr) is
         when An_If_Path             |
              An_Elsif_Path          |
              A_While_Loop_Statement =>

            --  The indentation has been already increased by the enclosed
            --  construct containing the expression
            Result := Logical_Depth * PP_Indentation;
         when others =>
            null;
      end case;

      return Result;

   end Short_Circuit_Padding;

   ----------------------------
   -- Take_Then_Into_Account --
   ----------------------------

   function Take_Then_Into_Account return Boolean is
      Encl_Kind : Flat_Element_Kinds := Get_Enclosing_Kind;
      Step_Up   : Natural := 0;

      Result    : Boolean            := False;
   begin

      if Last_If_Path_Start = Current_Out_Line then

         while Encl_Kind = A_Parenthesized_Expression loop
            Step_Up := Step_Up + 1;
            Encl_Kind := Get_Enclosing_Kind (Step_Up);
         end loop;

         case Encl_Kind is

            when An_If_Path .. An_Else_Path =>
               Result := True;

            when A_Parameter_Association =>

               if Function_Call_Parameters
                    (Get_Enclosing_Element (Step_Up + 1))'Length = 1
               then
                  Result := True;
               end if;

            when others =>
               null;
         end case;
      end if;

      return Result;
   end Take_Then_Into_Account;

   ----------------------
   -- Access functions --
   ----------------------

   function Align_Arrow_As_Vert_Line return Boolean is
   begin
      --  Some guard condition ???
      return Traversal_Stack.Top.Layout.Flag5;
   end Align_Arrow_As_Vert_Line;

   function Assign_Start_Pos return Natural is
      Step_Up : Natural := 0;
      Result  : Natural := 0;
   begin

      while not (Flat_Element_Kind (Traversal_Stack.Top (Step_Up).The_Element)
                 in Flat_Statement_Kinds
               or else
                 Flat_Element_Kind (Traversal_Stack.Top (Step_Up).The_Element)
                 in Flat_Declaration_Kinds)
      loop
         Step_Up := Step_Up + 1;
      end loop;

      if Flat_Element_Kind (Traversal_Stack.Top (Step_Up).The_Element) =
         A_Parameter_Specification
      then
         Result := Traversal_Stack.Top (Step_Up + 1).Layout.Pos2;
      end if;

      return Result;
   end Assign_Start_Pos;

   function Choice_On_Separate_Line return Boolean is
      Step_Up   : Natural            := 0;
      Result    : Boolean            := False;
      Encl_Kind : Flat_Element_Kinds := Get_Enclosing_Kind;
   begin

      if not Has_Choices (Encl_Kind) then
         Encl_Kind := Get_Enclosing_Kind (1);
         Step_Up   := 1;
      end if;

      if Has_Choices (Encl_Kind) then
         Result := Traversal_Stack.Top (Step_Up).Layout.Flag1;
      end if;

      return Result;
   end Choice_On_Separate_Line;

   function Choice_Start_Pos return Natural is
      Step_Up   : Natural            := 0;
      Result    : Natural            := 0;
      Encl_Kind : Flat_Element_Kinds := Get_Enclosing_Kind;
   begin
      if not Has_Choices (Encl_Kind) then
         Encl_Kind := Get_Enclosing_Kind (1);
         Step_Up   := 1;
      end if;

      if Has_Choices (Encl_Kind) then
         Result := Traversal_Stack.Top (Step_Up).Layout.Pos1;
      end if;

      return Result;

   end Choice_Start_Pos;

   function Colon_Start_Pos return Natural is
      Upper_Kind : constant Flat_Element_Kinds := Get_Enclosing_Kind;
      Step_Up    : Natural                     := 0;
   begin

      if Upper_Kind = A_Parameter_Specification then
         Step_Up := 1;
      end if;

      return Traversal_Stack.Top (Step_Up).Layout.Pos1;
   end Colon_Start_Pos;

   function Discriminants_On_Separate_Lines return Boolean is
      Result : Boolean := False;
   begin
      if Flat_Element_Kind (Current_State.The_Element) =
         A_Known_Discriminant_Part
      then
         Result := Current_State.Layout.Flag1;
      elsif Get_Enclosing_Kind = A_Known_Discriminant_Part then
         Result := Traversal_Stack.Top.Layout.Flag1;
      elsif Get_Enclosing_Kind = A_Discriminant_Specification and then
            Get_Enclosing_Kind (1) = A_Known_Discriminant_Part
      then
         Result := Traversal_Stack.Top (1).Layout.Flag1;
      end if;

      return Result;
   end Discriminants_On_Separate_Lines;

   function Enclosing_Association_Start_Pos return Natural is
   begin
      return Traversal_Stack.Top (1).Layout.Pos1;
   end Enclosing_Association_Start_Pos;

   function Enum_Literal_Def_On_Separate_Lines return Boolean
   is
   begin
      return Traversal_Stack.Top (1).Layout.Flag1;
   end Enum_Literal_Def_On_Separate_Lines;

   function Generic_Associations_On_Separate_Lines return Boolean is
   begin
      return Traversal_Stack.Top.Layout.Flag1;
   end Generic_Associations_On_Separate_Lines;

   function Impose_Extra_Indentation return Boolean is
   begin
      return Current_State.Layout.Flag1;
   end Impose_Extra_Indentation;

   function Is_And_Then_Arg return Boolean is
   begin
      return Current_State.Layout.Flag4;
   end Is_And_Then_Arg;

   function Is_First_Par_Association return Boolean is
   begin
      return Current_State.Layout.Flag1;
   end Is_First_Par_Association;

   function Is_Or_Else_Arg return Boolean is
   begin
      return Current_State.Layout.Flag5;
   end Is_Or_Else_Arg;

   function Start_Aggregate_From_New_Line return Boolean is
      Current_Kind : constant Flat_Element_Kinds :=
         Flat_Element_Kind (Current_State.The_Element);
   begin
      pragma Assert (False
         or else Current_Kind in A_Record_Aggregate .. A_Named_Array_Aggregate
         or else Current_Kind = A_Discriminant_Constraint);

      return (Current_State.Layout.Flag1 and then
              Current_State.Layout.Pos1 = 0);

   end Start_Aggregate_From_New_Line;

   function Start_Array_Def_From_New_Line return Boolean is
   begin
      return Current_State.Layout.Flag1;
   end Start_Array_Def_From_New_Line;

   function Start_Index_Def_From_New_Line return Boolean is
      Step_Up   : Natural            := 0;
      Result    : Boolean            := False;
      Encl_Kind : Flat_Element_Kinds;
   begin

      if Is_Array_Definition
           (Flat_Element_Kind (Current_State.The_Element))
      then
         Result := Current_State.Layout.Flag2;
      else

         --  going up and looking where we are...

         loop
            Encl_Kind := Get_Enclosing_Kind (Step_Up);

            if Is_Array_Definition (Encl_Kind) then
               Result := Traversal_Stack.Top (Step_Up).Layout.Flag2;
               exit;
            end if;

            case Encl_Kind is
               when Flat_Pragma_Kinds      |
                    Flat_Declaration_Kinds |
                    Flat_Association_Kinds |
                    Flat_Statement_Kinds   |
                    Flat_Clause_Kinds      =>
                  exit;
               when others => null;
            end case;

            Step_Up := Step_Up + 1;
         end loop;

      end if;

      return Result;
   end Start_Index_Def_From_New_Line;

   function Start_Range_Expression_From_New_Line return Boolean is
      Result : Boolean := False;
   begin

      if Get_Enclosing_Kind = A_Simple_Expression_Range
        or else
         Get_Enclosing_Kind = A_Discrete_Simple_Expression_Range
        or else
         Get_Enclosing_Kind =
           A_Discrete_Simple_Expression_Range_As_Subtype_Definition
      then

         if Is_Equal (Current_State.The_Element,
                      Lower_Bound (Get_Enclosing_Element))
         then
            Result := Traversal_Stack.Top.Layout.Flag1;
         elsif Is_Equal (Current_State.The_Element,
                         Upper_Bound (Get_Enclosing_Element))
         then
            Result := Traversal_Stack.Top.Layout.Flag2;
         end if;

      end if;

      return Result;
   end Start_Range_Expression_From_New_Line;

   function Start_Upper_Range_Expression_From_New_Line return Boolean is
      Result : Boolean := False;
   begin

      if (Get_Enclosing_Kind = A_Simple_Expression_Range
        or else
         Get_Enclosing_Kind = A_Discrete_Simple_Expression_Range
        or else
         Get_Enclosing_Kind =
           A_Discrete_Simple_Expression_Range_As_Subtype_Definition)
        and then
         Is_Equal (Current_State.The_Element,
                   Upper_Bound (Get_Enclosing_Element))
      then
         Result := Traversal_Stack.Top.Layout.Flag2;
      end if;

      return Result;
   end Start_Upper_Range_Expression_From_New_Line;

   function Vert_Line_Start_Pos return Natural is
      Step_Up   : Natural            := 0;
      Result    : Natural            := 0;
      Encl_Kind : Flat_Element_Kinds := Get_Enclosing_Kind;
   begin
      if not Has_Choices (Encl_Kind) then
         Encl_Kind := Get_Enclosing_Kind (1);
         Step_Up   := 1;
      end if;

      if Has_Choices (Encl_Kind) then
         Result := Traversal_Stack.Top (Step_Up).Layout.Pos2;
      end if;

      return Result;

   end Vert_Line_Start_Pos;

   -------------------------------------
   -- Layout info set/update routines --
   -------------------------------------

   Upper_Current_State : Traversal_Step_Record;
   --  Used in update routines for putting the layout info into the enclosing
   --  element layout record

   procedure Set_Assign_Start_Pos (Position : Natural) is
   begin
      Upper_Current_State             := Traversal_Stack.Pop;
      Upper_Current_State.Layout.Pos2 := Position;

      Traversal_Stack.Push (Upper_Current_State);
   end Set_Assign_Start_Pos;

   procedure Set_Colon_Start_Pos  (Position : Natural) is
   begin
      Upper_Current_State             := Traversal_Stack.Pop;
      Upper_Current_State.Layout.Pos1 := Position;

      Traversal_Stack.Push (Upper_Current_State);
   end Set_Colon_Start_Pos;

   procedure Set_Generic_Arrow_Start_Pos (Position : Natural) is
   begin
      Upper_Current_State             := Traversal_Stack.Pop;
      Upper_Current_State.Layout.Pos2 := Position;

      Traversal_Stack.Push (Upper_Current_State);
   end Set_Generic_Arrow_Start_Pos;

   procedure Set_Generic_Associations_On_Separate_Lines is
   begin
      Current_State.Layout.Flag1 := True;
   end Set_Generic_Associations_On_Separate_Lines;

   procedure Set_Impose_Extra_Indentation is
   begin
      Current_State.Layout.Flag1 := True;
   end Set_Impose_Extra_Indentation;

   procedure Set_Is_And_Then_Arg is
   begin
      Current_State.Layout.Flag4 := True;
   end Set_Is_And_Then_Arg;

   procedure Set_Is_First_Par_Association is
   begin
      Current_State.Layout.Flag1 := True;
   end Set_Is_First_Par_Association;

   procedure Set_Is_Or_Else_Arg is
   begin
      Current_State.Layout.Flag5 := True;
   end Set_Is_Or_Else_Arg;

   -------------------------------
   -- Old stuff, needs revising --
   -------------------------------

   function Get_Enclosing_Kind
     (Step_Down : Natural := 0)
      return      Flat_Element_Kinds
   is
   begin
      return Flat_Element_Kind (Get_Enclosing_Element (Step_Down));
   end Get_Enclosing_Kind;

   function Get_Enclosing_Element
     (Step_Down : Natural := 0)
      return      Asis.Element
   is
   begin
      return Traversal_Stack.Top (Step_Down).The_Element;
   end Get_Enclosing_Element;

   --  Aggregates:
   function Comp_Asctns_On_Sep_Lines return Boolean is
      Upper_Kind : constant Flat_Element_Kinds := Get_Enclosing_Kind;
   begin
      pragma Assert (False
         or else Upper_Kind in A_Record_Aggregate .. A_Named_Array_Aggregate
         or else Upper_Kind = A_Discriminant_Constraint);

      return Traversal_Stack.Top.Layout.Flag1;

   end Comp_Asctns_On_Sep_Lines;

   function Comp_Asctns_Start_Pos return Natural is
      Upper_Kind : constant Flat_Element_Kinds := Get_Enclosing_Kind;
   begin
      pragma Assert (False
         or else Upper_Kind in A_Record_Aggregate .. A_Named_Array_Aggregate
         or else Upper_Kind = A_Discriminant_Constraint);

      return Traversal_Stack.Top.Layout.Pos1;

   end Comp_Asctns_Start_Pos;

   function Arrow_Start_Pos return Natural is
      Result : Natural               := 0;
      Encl_Kind : Flat_Element_Kinds := Get_Enclosing_Kind (1);
      Step_Up   : Natural            := 1;
   begin

      if not Align_Arrow_As_Vert_Line then

         if Encl_Kind in Flat_Association_Kinds then
            Step_Up   := Step_Up + 1;
            Encl_Kind := Get_Enclosing_Kind (Step_Up);
         end if;

         if Encl_Kind = An_Entry_Call_Statement    or else
            Encl_Kind = A_Procedure_Call_Statement or else
            Encl_Kind = A_Function_Call            or else
            Encl_Kind = A_Record_Aggregate         or else
            Encl_Kind = An_Extension_Aggregate     or else
            Encl_Kind = A_Named_Array_Aggregate
         then
            Result := Traversal_Stack.Top (Step_Up).Layout.Pos2;
         end if;

      end if;

      return Result;
   end Arrow_Start_Pos;

   --  Elements having parameters? Parameter specifications?
   function Par_Spec_On_Separate_Lines return Boolean is
      Upper_Kind : constant Flat_Element_Kinds := Get_Enclosing_Kind;
   begin
      pragma Assert (Has_Parameters (Upper_Kind));

      return Traversal_Stack.Top.Layout.Flag1;

   end Par_Spec_On_Separate_Lines;

   --  Procedure, entry and (prefix) function calls:
   function Pars_On_Sep_Lines return Boolean is
   begin
      pragma Assert (Is_Call (Traversal_Stack.Top.The_Element));

      return Traversal_Stack.Top.Layout.Flag1;
   end Pars_On_Sep_Lines;

   function Move_All_Call_Left return Boolean is
   begin
--      pragma Assert (Is_Call (Traversal_Stack.Top.The_Element));

--      return Traversal_Stack.Top.Layout.Flag2;
      return Current_State.Layout.Flag2;
   end Move_All_Call_Left;

   function Start_Par_Pos (Step_Down : Natural := 0) return Natural is
   begin
      pragma Assert (Is_Call (Traversal_Stack.Top (Step_Down).The_Element));

      return Traversal_Stack.Top (Step_Down).Layout.Pos1;
   end Start_Par_Pos;

   function Start_Call_Pos return Natural is
   begin
--      pragma Assert (Is_Call (Traversal_Stack.Top.The_Element));

--      return Traversal_Stack.Top.Layout.Pos3;
      return Current_State.Layout.Pos3;
   end Start_Call_Pos;

   --  Component, object and number declarations

   --  Definitions of the form
   --   <list of names> : <definition> [:= init_Expression]

   function Init_Expr_On_Separate_Line return Boolean is
      Upper_Kind : constant Flat_Element_Kinds := Get_Enclosing_Kind;
   begin
      pragma Assert (May_Have_Init_Expr (Upper_Kind));

      return Traversal_Stack.Top.Layout.Flag2;
   end Init_Expr_On_Separate_Line;

   ----------------------------
   -- Layout update routines --
   ----------------------------

   --  Aggregates and discriminant constraints:
   procedure Set_Comp_Asctns_On_Sep_Lines (Separate_Line : Boolean) is
      Current_Kind : constant Flat_Element_Kinds :=
         Flat_Element_Kind (Current_State.The_Element);
   begin
      pragma Assert (False
         or else Current_Kind in A_Record_Aggregate .. A_Named_Array_Aggregate
         or else Current_Kind = A_Discriminant_Constraint);

      Current_State.Layout.Flag1 := Separate_Line;
   end Set_Comp_Asctns_On_Sep_Lines;

   procedure Set_Comp_Asctns_Start_Pos (Position : Natural) is
      Current_Kind : constant Flat_Element_Kinds :=
         Flat_Element_Kind (Current_State.The_Element);
   begin
      pragma Assert (False
         or else Current_Kind in A_Record_Aggregate .. A_Named_Array_Aggregate
         or else Current_Kind = A_Discriminant_Constraint);

      Current_State.Layout.Pos1 := Position;
   end Set_Comp_Asctns_Start_Pos;

   procedure Set_Arrow_Start_Pos (Position : Natural) is
      Current_Kind : constant Flat_Element_Kinds :=
         Flat_Element_Kind (Current_State.The_Element);
   begin
      pragma Assert (False
         or else Current_Kind in A_Record_Aggregate .. A_Named_Array_Aggregate
         or else Current_Kind = A_Discriminant_Constraint
         or else (Is_Call (Current_State.The_Element)));

      Current_State.Layout.Pos2 := Position;
   end Set_Arrow_Start_Pos;

   --  Elements having parameters? Parameter specifications?
   procedure Set_Par_Spec_On_Sep_Lines (Separate_Line : Boolean) is
      Upper_Current_State : Traversal_Step_Record := Traversal_Stack.Pop;
      Upper_Kind          : constant Flat_Element_Kinds :=
         Flat_Element_Kind (Upper_Current_State.The_Element);
   begin
      pragma Assert (Has_Parameters (Upper_Kind));

      Upper_Current_State.Layout.Flag1 := Separate_Line;

      Traversal_Stack.Push (Upper_Current_State);

   end Set_Par_Spec_On_Sep_Lines;

   --  Procedure, entry and (prefix) function calls:
   procedure Set_Pars_On_Sep_Lines  (Separate_Line : Boolean) is
   begin
      pragma Assert (Is_Call (Current_State.The_Element));

      Current_State.Layout.Flag1 := Separate_Line;
   end Set_Pars_On_Sep_Lines;

   procedure Set_Move_All_Call_Left (Move_It : Boolean) is
   begin
      pragma Assert (Is_Call (Current_State.The_Element));

      Current_State.Layout.Flag2 := Move_It;
   end Set_Move_All_Call_Left;

   procedure Set_Start_Par_Pos (Position : Natural) is
   begin
      pragma Assert (Is_Call (Current_State.The_Element));

      Current_State.Layout.Pos1 := Position;
   end Set_Start_Par_Pos;

   procedure Set_Start_Call_Pos (Position : Natural) is
   begin
      pragma Assert (Is_Call (Current_State.The_Element));

      Current_State.Layout.Pos3 := Position;
   end Set_Start_Call_Pos;

end GNATPP.Layout;