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
|
%{
/*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include "glcpp.h"
#include "main/core.h" /* for struct gl_extensions */
#include "main/mtypes.h" /* for gl_api enum */
static void
yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
static void
_define_object_macro (glcpp_parser_t *parser,
YYLTYPE *loc,
const char *macro,
token_list_t *replacements);
static void
_define_function_macro (glcpp_parser_t *parser,
YYLTYPE *loc,
const char *macro,
string_list_t *parameters,
token_list_t *replacements);
static string_list_t *
_string_list_create (void *ctx);
static void
_string_list_append_item (string_list_t *list, const char *str);
static int
_string_list_contains (string_list_t *list, const char *member, int *index);
static int
_string_list_length (string_list_t *list);
static int
_string_list_equal (string_list_t *a, string_list_t *b);
static argument_list_t *
_argument_list_create (void *ctx);
static void
_argument_list_append (argument_list_t *list, token_list_t *argument);
static int
_argument_list_length (argument_list_t *list);
static token_list_t *
_argument_list_member_at (argument_list_t *list, int index);
/* Note: This function ralloc_steal()s the str pointer. */
static token_t *
_token_create_str (void *ctx, int type, char *str);
static token_t *
_token_create_ival (void *ctx, int type, int ival);
static token_list_t *
_token_list_create (void *ctx);
static void
_token_list_append (token_list_t *list, token_t *token);
static void
_token_list_append_list (token_list_t *list, token_list_t *tail);
static int
_token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
static void
_parser_active_list_push (glcpp_parser_t *parser,
const char *identifier,
token_node_t *marker);
static void
_parser_active_list_pop (glcpp_parser_t *parser);
static int
_parser_active_list_contains (glcpp_parser_t *parser, const char *identifier);
/* Expand list, and begin lexing from the result (after first
* prefixing a token of type 'head_token_type').
*/
static void
_glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
int head_token_type,
token_list_t *list);
/* Perform macro expansion in-place on the given list. */
static void
_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
token_list_t *list);
static void
_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
token_list_t *list);
static void
_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
int condition);
static void
_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
const char *type, int condition);
static void
_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
static void
_glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
const char *ident, bool explicitly_set);
static int
glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
static void
glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
static void
add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
%}
%pure-parser
%error-verbose
%locations
%initial-action {
@$.first_line = 1;
@$.first_column = 1;
@$.last_line = 1;
@$.last_column = 1;
@$.source = 0;
}
%parse-param {glcpp_parser_t *parser}
%lex-param {glcpp_parser_t *parser}
%expect 0
%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE FUNC_IDENTIFIER OBJ_IDENTIFIER HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_LINE HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE
%token PASTE
%type <ival> expression INTEGER operator SPACE integer_constant
%type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
%type <string_list> identifier_list
%type <token> preprocessing_token conditional_token
%type <token_list> pp_tokens replacement_list text_line conditional_tokens
%left OR
%left AND
%left '|'
%left '^'
%left '&'
%left EQUAL NOT_EQUAL
%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
%left LEFT_SHIFT RIGHT_SHIFT
%left '+' '-'
%left '*' '/' '%'
%right UNARY
%%
input:
/* empty */
| input line
;
line:
control_line {
ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
}
| HASH_LINE {
glcpp_parser_resolve_implicit_version(parser);
} pp_tokens NEWLINE {
if (parser->skip_stack == NULL ||
parser->skip_stack->type == SKIP_NO_SKIP)
{
_glcpp_parser_expand_and_lex_from (parser,
LINE_EXPANDED, $3);
}
}
| text_line {
_glcpp_parser_print_expanded_token_list (parser, $1);
ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
ralloc_free ($1);
}
| expanded_line
| HASH non_directive
;
expanded_line:
IF_EXPANDED expression NEWLINE {
_glcpp_parser_skip_stack_push_if (parser, & @1, $2);
}
| ELIF_EXPANDED expression NEWLINE {
_glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
}
| LINE_EXPANDED integer_constant NEWLINE {
parser->has_new_line_number = 1;
parser->new_line_number = $2;
ralloc_asprintf_rewrite_tail (&parser->output,
&parser->output_length,
"#line %" PRIiMAX "\n",
$2);
}
| LINE_EXPANDED integer_constant integer_constant NEWLINE {
parser->has_new_line_number = 1;
parser->new_line_number = $2;
parser->has_new_source_number = 1;
parser->new_source_number = $3;
ralloc_asprintf_rewrite_tail (&parser->output,
&parser->output_length,
"#line %" PRIiMAX " %" PRIiMAX "\n",
$2, $3);
}
;
define:
OBJ_IDENTIFIER replacement_list NEWLINE {
_define_object_macro (parser, & @1, $1, $2);
}
| FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
_define_function_macro (parser, & @1, $1, NULL, $4);
}
| FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
_define_function_macro (parser, & @1, $1, $3, $5);
}
;
control_line:
HASH_DEFINE {
glcpp_parser_resolve_implicit_version(parser);
} define
| HASH_UNDEF {
glcpp_parser_resolve_implicit_version(parser);
} IDENTIFIER NEWLINE {
if (strcmp("__LINE__", $3) == 0
|| strcmp("__FILE__", $3) == 0
|| strcmp("__VERSION__", $3) == 0)
glcpp_error(& @1, parser, "Built-in (pre-defined)"
" macro names can not be undefined.");
macro_t *macro = hash_table_find (parser->defines, $3);
if (macro) {
hash_table_remove (parser->defines, $3);
ralloc_free (macro);
}
ralloc_free ($3);
}
| HASH_IF {
glcpp_parser_resolve_implicit_version(parser);
} conditional_tokens NEWLINE {
/* Be careful to only evaluate the 'if' expression if
* we are not skipping. When we are skipping, we
* simply push a new 0-valued 'if' onto the skip
* stack.
*
* This avoids generating diagnostics for invalid
* expressions that are being skipped. */
if (parser->skip_stack == NULL ||
parser->skip_stack->type == SKIP_NO_SKIP)
{
_glcpp_parser_expand_and_lex_from (parser,
IF_EXPANDED, $3);
}
else
{
_glcpp_parser_skip_stack_push_if (parser, & @1, 0);
parser->skip_stack->type = SKIP_TO_ENDIF;
}
}
| HASH_IF NEWLINE {
/* #if without an expression is only an error if we
* are not skipping */
if (parser->skip_stack == NULL ||
parser->skip_stack->type == SKIP_NO_SKIP)
{
glcpp_error(& @1, parser, "#if with no expression");
}
_glcpp_parser_skip_stack_push_if (parser, & @1, 0);
}
| HASH_IFDEF {
glcpp_parser_resolve_implicit_version(parser);
} IDENTIFIER junk NEWLINE {
macro_t *macro = hash_table_find (parser->defines, $3);
ralloc_free ($3);
_glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
}
| HASH_IFNDEF {
glcpp_parser_resolve_implicit_version(parser);
} IDENTIFIER junk NEWLINE {
macro_t *macro = hash_table_find (parser->defines, $3);
ralloc_free ($3);
_glcpp_parser_skip_stack_push_if (parser, & @2, macro == NULL);
}
| HASH_ELIF conditional_tokens NEWLINE {
/* Be careful to only evaluate the 'elif' expression
* if we are not skipping. When we are skipping, we
* simply change to a 0-valued 'elif' on the skip
* stack.
*
* This avoids generating diagnostics for invalid
* expressions that are being skipped. */
if (parser->skip_stack &&
parser->skip_stack->type == SKIP_TO_ELSE)
{
_glcpp_parser_expand_and_lex_from (parser,
ELIF_EXPANDED, $2);
}
else if (parser->skip_stack &&
parser->skip_stack->has_else)
{
glcpp_error(& @1, parser, "#elif after #else");
}
else
{
_glcpp_parser_skip_stack_change_if (parser, & @1,
"elif", 0);
}
}
| HASH_ELIF NEWLINE {
/* #elif without an expression is an error unless we
* are skipping. */
if (parser->skip_stack &&
parser->skip_stack->type == SKIP_TO_ELSE)
{
glcpp_error(& @1, parser, "#elif with no expression");
}
else if (parser->skip_stack &&
parser->skip_stack->has_else)
{
glcpp_error(& @1, parser, "#elif after #else");
}
else
{
_glcpp_parser_skip_stack_change_if (parser, & @1,
"elif", 0);
glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
}
}
| HASH_ELSE {
if (parser->skip_stack &&
parser->skip_stack->has_else)
{
glcpp_error(& @1, parser, "multiple #else");
}
else
{
_glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
if (parser->skip_stack)
parser->skip_stack->has_else = true;
}
} NEWLINE
| HASH_ENDIF {
_glcpp_parser_skip_stack_pop (parser, & @1);
} NEWLINE
| HASH_VERSION integer_constant NEWLINE {
if (parser->version_resolved) {
glcpp_error(& @1, parser, "#version must appear on the first line");
}
_glcpp_parser_handle_version_declaration(parser, $2, NULL, true);
}
| HASH_VERSION integer_constant IDENTIFIER NEWLINE {
if (parser->version_resolved) {
glcpp_error(& @1, parser, "#version must appear on the first line");
}
_glcpp_parser_handle_version_declaration(parser, $2, $3, true);
}
| HASH NEWLINE {
glcpp_parser_resolve_implicit_version(parser);
}
;
integer_constant:
INTEGER_STRING {
if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
$$ = strtoll ($1 + 2, NULL, 16);
} else if ($1[0] == '0') {
$$ = strtoll ($1, NULL, 8);
} else {
$$ = strtoll ($1, NULL, 10);
}
}
| INTEGER {
$$ = $1;
}
expression:
integer_constant
| IDENTIFIER {
if (parser->is_gles)
glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $1);
$$ = 0;
}
| expression OR expression {
$$ = $1 || $3;
}
| expression AND expression {
$$ = $1 && $3;
}
| expression '|' expression {
$$ = $1 | $3;
}
| expression '^' expression {
$$ = $1 ^ $3;
}
| expression '&' expression {
$$ = $1 & $3;
}
| expression NOT_EQUAL expression {
$$ = $1 != $3;
}
| expression EQUAL expression {
$$ = $1 == $3;
}
| expression GREATER_OR_EQUAL expression {
$$ = $1 >= $3;
}
| expression LESS_OR_EQUAL expression {
$$ = $1 <= $3;
}
| expression '>' expression {
$$ = $1 > $3;
}
| expression '<' expression {
$$ = $1 < $3;
}
| expression RIGHT_SHIFT expression {
$$ = $1 >> $3;
}
| expression LEFT_SHIFT expression {
$$ = $1 << $3;
}
| expression '-' expression {
$$ = $1 - $3;
}
| expression '+' expression {
$$ = $1 + $3;
}
| expression '%' expression {
if ($3 == 0) {
yyerror (& @1, parser,
"zero modulus in preprocessor directive");
} else {
$$ = $1 % $3;
}
}
| expression '/' expression {
if ($3 == 0) {
yyerror (& @1, parser,
"division by 0 in preprocessor directive");
} else {
$$ = $1 / $3;
}
}
| expression '*' expression {
$$ = $1 * $3;
}
| '!' expression %prec UNARY {
$$ = ! $2;
}
| '~' expression %prec UNARY {
$$ = ~ $2;
}
| '-' expression %prec UNARY {
$$ = - $2;
}
| '+' expression %prec UNARY {
$$ = + $2;
}
| '(' expression ')' {
$$ = $2;
}
;
identifier_list:
IDENTIFIER {
$$ = _string_list_create (parser);
_string_list_append_item ($$, $1);
ralloc_steal ($$, $1);
}
| identifier_list ',' IDENTIFIER {
$$ = $1;
_string_list_append_item ($$, $3);
ralloc_steal ($$, $3);
}
;
text_line:
NEWLINE { $$ = NULL; }
| pp_tokens NEWLINE
;
non_directive:
pp_tokens NEWLINE {
yyerror (& @1, parser, "Invalid tokens after #");
}
;
replacement_list:
/* empty */ { $$ = NULL; }
| pp_tokens
;
junk:
/* empty */
| pp_tokens {
glcpp_warning(&@1, parser, "extra tokens at end of directive");
}
;
conditional_token:
/* Handle "defined" operator */
DEFINED IDENTIFIER {
int v = hash_table_find (parser->defines, $2) ? 1 : 0;
$$ = _token_create_ival (parser, INTEGER, v);
}
| DEFINED '(' IDENTIFIER ')' {
int v = hash_table_find (parser->defines, $3) ? 1 : 0;
$$ = _token_create_ival (parser, INTEGER, v);
}
| preprocessing_token
;
conditional_tokens:
/* Exactly the same as pp_tokens, but using conditional_token */
conditional_token {
$$ = _token_list_create (parser);
_token_list_append ($$, $1);
}
| conditional_tokens conditional_token {
$$ = $1;
_token_list_append ($$, $2);
}
;
pp_tokens:
preprocessing_token {
parser->space_tokens = 1;
$$ = _token_list_create (parser);
_token_list_append ($$, $1);
}
| pp_tokens preprocessing_token {
$$ = $1;
_token_list_append ($$, $2);
}
;
preprocessing_token:
IDENTIFIER {
$$ = _token_create_str (parser, IDENTIFIER, $1);
$$->location = yylloc;
}
| INTEGER_STRING {
$$ = _token_create_str (parser, INTEGER_STRING, $1);
$$->location = yylloc;
}
| operator {
$$ = _token_create_ival (parser, $1, $1);
$$->location = yylloc;
}
| OTHER {
$$ = _token_create_str (parser, OTHER, $1);
$$->location = yylloc;
}
| SPACE {
$$ = _token_create_ival (parser, SPACE, SPACE);
$$->location = yylloc;
}
;
operator:
'[' { $$ = '['; }
| ']' { $$ = ']'; }
| '(' { $$ = '('; }
| ')' { $$ = ')'; }
| '{' { $$ = '{'; }
| '}' { $$ = '}'; }
| '.' { $$ = '.'; }
| '&' { $$ = '&'; }
| '*' { $$ = '*'; }
| '+' { $$ = '+'; }
| '-' { $$ = '-'; }
| '~' { $$ = '~'; }
| '!' { $$ = '!'; }
| '/' { $$ = '/'; }
| '%' { $$ = '%'; }
| LEFT_SHIFT { $$ = LEFT_SHIFT; }
| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
| '<' { $$ = '<'; }
| '>' { $$ = '>'; }
| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
| EQUAL { $$ = EQUAL; }
| NOT_EQUAL { $$ = NOT_EQUAL; }
| '^' { $$ = '^'; }
| '|' { $$ = '|'; }
| AND { $$ = AND; }
| OR { $$ = OR; }
| ';' { $$ = ';'; }
| ',' { $$ = ','; }
| '=' { $$ = '='; }
| PASTE { $$ = PASTE; }
;
%%
string_list_t *
_string_list_create (void *ctx)
{
string_list_t *list;
list = ralloc (ctx, string_list_t);
list->head = NULL;
list->tail = NULL;
return list;
}
void
_string_list_append_item (string_list_t *list, const char *str)
{
string_node_t *node;
node = ralloc (list, string_node_t);
node->str = ralloc_strdup (node, str);
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
list->tail->next = node;
}
list->tail = node;
}
int
_string_list_contains (string_list_t *list, const char *member, int *index)
{
string_node_t *node;
int i;
if (list == NULL)
return 0;
for (i = 0, node = list->head; node; i++, node = node->next) {
if (strcmp (node->str, member) == 0) {
if (index)
*index = i;
return 1;
}
}
return 0;
}
int
_string_list_length (string_list_t *list)
{
int length = 0;
string_node_t *node;
if (list == NULL)
return 0;
for (node = list->head; node; node = node->next)
length++;
return length;
}
int
_string_list_equal (string_list_t *a, string_list_t *b)
{
string_node_t *node_a, *node_b;
if (a == NULL && b == NULL)
return 1;
if (a == NULL || b == NULL)
return 0;
for (node_a = a->head, node_b = b->head;
node_a && node_b;
node_a = node_a->next, node_b = node_b->next)
{
if (strcmp (node_a->str, node_b->str))
return 0;
}
/* Catch the case of lists being different lengths, (which
* would cause the loop above to terminate after the shorter
* list). */
return node_a == node_b;
}
argument_list_t *
_argument_list_create (void *ctx)
{
argument_list_t *list;
list = ralloc (ctx, argument_list_t);
list->head = NULL;
list->tail = NULL;
return list;
}
void
_argument_list_append (argument_list_t *list, token_list_t *argument)
{
argument_node_t *node;
node = ralloc (list, argument_node_t);
node->argument = argument;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
list->tail->next = node;
}
list->tail = node;
}
int
_argument_list_length (argument_list_t *list)
{
int length = 0;
argument_node_t *node;
if (list == NULL)
return 0;
for (node = list->head; node; node = node->next)
length++;
return length;
}
token_list_t *
_argument_list_member_at (argument_list_t *list, int index)
{
argument_node_t *node;
int i;
if (list == NULL)
return NULL;
node = list->head;
for (i = 0; i < index; i++) {
node = node->next;
if (node == NULL)
break;
}
if (node)
return node->argument;
return NULL;
}
/* Note: This function ralloc_steal()s the str pointer. */
token_t *
_token_create_str (void *ctx, int type, char *str)
{
token_t *token;
token = ralloc (ctx, token_t);
token->type = type;
token->value.str = str;
ralloc_steal (token, str);
return token;
}
token_t *
_token_create_ival (void *ctx, int type, int ival)
{
token_t *token;
token = ralloc (ctx, token_t);
token->type = type;
token->value.ival = ival;
return token;
}
token_list_t *
_token_list_create (void *ctx)
{
token_list_t *list;
list = ralloc (ctx, token_list_t);
list->head = NULL;
list->tail = NULL;
list->non_space_tail = NULL;
return list;
}
void
_token_list_append (token_list_t *list, token_t *token)
{
token_node_t *node;
node = ralloc (list, token_node_t);
node->token = token;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
list->tail->next = node;
}
list->tail = node;
if (token->type != SPACE)
list->non_space_tail = node;
}
void
_token_list_append_list (token_list_t *list, token_list_t *tail)
{
if (tail == NULL || tail->head == NULL)
return;
if (list->head == NULL) {
list->head = tail->head;
} else {
list->tail->next = tail->head;
}
list->tail = tail->tail;
list->non_space_tail = tail->non_space_tail;
}
static token_list_t *
_token_list_copy (void *ctx, token_list_t *other)
{
token_list_t *copy;
token_node_t *node;
if (other == NULL)
return NULL;
copy = _token_list_create (ctx);
for (node = other->head; node; node = node->next) {
token_t *new_token = ralloc (copy, token_t);
*new_token = *node->token;
_token_list_append (copy, new_token);
}
return copy;
}
static void
_token_list_trim_trailing_space (token_list_t *list)
{
token_node_t *tail, *next;
if (list->non_space_tail) {
tail = list->non_space_tail->next;
list->non_space_tail->next = NULL;
list->tail = list->non_space_tail;
while (tail) {
next = tail->next;
ralloc_free (tail);
tail = next;
}
}
}
static int
_token_list_is_empty_ignoring_space (token_list_t *l)
{
token_node_t *n;
if (l == NULL)
return 1;
n = l->head;
while (n != NULL && n->token->type == SPACE)
n = n->next;
return n == NULL;
}
int
_token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
{
token_node_t *node_a, *node_b;
if (a == NULL || b == NULL) {
int a_empty = _token_list_is_empty_ignoring_space(a);
int b_empty = _token_list_is_empty_ignoring_space(b);
return a_empty == b_empty;
}
node_a = a->head;
node_b = b->head;
while (1)
{
if (node_a == NULL && node_b == NULL)
break;
if (node_a == NULL || node_b == NULL)
return 0;
if (node_a->token->type == SPACE) {
node_a = node_a->next;
continue;
}
if (node_b->token->type == SPACE) {
node_b = node_b->next;
continue;
}
if (node_a->token->type != node_b->token->type)
return 0;
switch (node_a->token->type) {
case INTEGER:
if (node_a->token->value.ival !=
node_b->token->value.ival)
{
return 0;
}
break;
case IDENTIFIER:
case INTEGER_STRING:
case OTHER:
if (strcmp (node_a->token->value.str,
node_b->token->value.str))
{
return 0;
}
break;
}
node_a = node_a->next;
node_b = node_b->next;
}
return 1;
}
static void
_token_print (char **out, size_t *len, token_t *token)
{
if (token->type < 256) {
ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
return;
}
switch (token->type) {
case INTEGER:
ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
break;
case IDENTIFIER:
case INTEGER_STRING:
case OTHER:
ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
break;
case SPACE:
ralloc_asprintf_rewrite_tail (out, len, " ");
break;
case LEFT_SHIFT:
ralloc_asprintf_rewrite_tail (out, len, "<<");
break;
case RIGHT_SHIFT:
ralloc_asprintf_rewrite_tail (out, len, ">>");
break;
case LESS_OR_EQUAL:
ralloc_asprintf_rewrite_tail (out, len, "<=");
break;
case GREATER_OR_EQUAL:
ralloc_asprintf_rewrite_tail (out, len, ">=");
break;
case EQUAL:
ralloc_asprintf_rewrite_tail (out, len, "==");
break;
case NOT_EQUAL:
ralloc_asprintf_rewrite_tail (out, len, "!=");
break;
case AND:
ralloc_asprintf_rewrite_tail (out, len, "&&");
break;
case OR:
ralloc_asprintf_rewrite_tail (out, len, "||");
break;
case PASTE:
ralloc_asprintf_rewrite_tail (out, len, "##");
break;
case COMMA_FINAL:
ralloc_asprintf_rewrite_tail (out, len, ",");
break;
case PLACEHOLDER:
/* Nothing to print. */
break;
default:
assert(!"Error: Don't know how to print token.");
break;
}
}
/* Return a new token (ralloc()ed off of 'token') formed by pasting
* 'token' and 'other'. Note that this function may return 'token' or
* 'other' directly rather than allocating anything new.
*
* Caution: Only very cursory error-checking is performed to see if
* the final result is a valid single token. */
static token_t *
_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
{
token_t *combined = NULL;
/* Pasting a placeholder onto anything makes no change. */
if (other->type == PLACEHOLDER)
return token;
/* When 'token' is a placeholder, just return 'other'. */
if (token->type == PLACEHOLDER)
return other;
/* A very few single-character punctuators can be combined
* with another to form a multi-character punctuator. */
switch (token->type) {
case '<':
if (other->type == '<')
combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
else if (other->type == '=')
combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
break;
case '>':
if (other->type == '>')
combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
else if (other->type == '=')
combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
break;
case '=':
if (other->type == '=')
combined = _token_create_ival (token, EQUAL, EQUAL);
break;
case '!':
if (other->type == '=')
combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
break;
case '&':
if (other->type == '&')
combined = _token_create_ival (token, AND, AND);
break;
case '|':
if (other->type == '|')
combined = _token_create_ival (token, OR, OR);
break;
}
if (combined != NULL) {
/* Inherit the location from the first token */
combined->location = token->location;
return combined;
}
/* Two string-valued (or integer) tokens can usually just be
* mashed together. (We also handle a string followed by an
* integer here as well.)
*
* There are some exceptions here. Notably, if the first token
* is an integer (or a string representing an integer), then
* the second token must also be an integer or must be a
* string representing an integer that begins with a digit.
*/
if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
(other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
{
char *str;
int combined_type;
/* Check that pasting onto an integer doesn't create a
* non-integer, (that is, only digits can be
* pasted. */
if (token->type == INTEGER_STRING || token->type == INTEGER)
{
switch (other->type) {
case INTEGER_STRING:
if (other->value.str[0] < '0' ||
other->value.str[0] > '9')
goto FAIL;
break;
case INTEGER:
if (other->value.ival < 0)
goto FAIL;
break;
default:
goto FAIL;
}
}
if (token->type == INTEGER)
str = ralloc_asprintf (token, "%" PRIiMAX,
token->value.ival);
else
str = ralloc_strdup (token, token->value.str);
if (other->type == INTEGER)
ralloc_asprintf_append (&str, "%" PRIiMAX,
other->value.ival);
else
ralloc_strcat (&str, other->value.str);
/* New token is same type as original token, unless we
* started with an integer, in which case we will be
* creating an integer-string. */
combined_type = token->type;
if (combined_type == INTEGER)
combined_type = INTEGER_STRING;
combined = _token_create_str (token, combined_type, str);
combined->location = token->location;
return combined;
}
FAIL:
glcpp_error (&token->location, parser, "");
ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
_token_print (&parser->info_log, &parser->info_log_length, token);
ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
_token_print (&parser->info_log, &parser->info_log_length, other);
ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
return token;
}
static void
_token_list_print (glcpp_parser_t *parser, token_list_t *list)
{
token_node_t *node;
if (list == NULL)
return;
for (node = list->head; node; node = node->next)
_token_print (&parser->output, &parser->output_length, node->token);
}
void
yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
{
glcpp_error(locp, parser, "%s", error);
}
static void add_builtin_define(glcpp_parser_t *parser,
const char *name, int value)
{
token_t *tok;
token_list_t *list;
tok = _token_create_ival (parser, INTEGER, value);
list = _token_list_create(parser);
_token_list_append(list, tok);
_define_object_macro(parser, NULL, name, list);
}
glcpp_parser_t *
glcpp_parser_create (const struct gl_extensions *extensions, gl_api api)
{
glcpp_parser_t *parser;
parser = ralloc (NULL, glcpp_parser_t);
glcpp_lex_init_extra (parser, &parser->scanner);
parser->defines = hash_table_ctor (32, hash_table_string_hash,
hash_table_string_compare);
parser->active = NULL;
parser->lexing_if = 0;
parser->space_tokens = 1;
parser->newline_as_space = 0;
parser->in_control_line = 0;
parser->paren_count = 0;
parser->commented_newlines = 0;
parser->skip_stack = NULL;
parser->lex_from_list = NULL;
parser->lex_from_node = NULL;
parser->output = ralloc_strdup(parser, "");
parser->output_length = 0;
parser->info_log = ralloc_strdup(parser, "");
parser->info_log_length = 0;
parser->error = 0;
parser->extensions = extensions;
parser->api = api;
parser->version_resolved = false;
parser->has_new_line_number = 0;
parser->new_line_number = 1;
parser->has_new_source_number = 0;
parser->new_source_number = 0;
return parser;
}
void
glcpp_parser_destroy (glcpp_parser_t *parser)
{
glcpp_lex_destroy (parser->scanner);
hash_table_dtor (parser->defines);
ralloc_free (parser);
}
typedef enum function_status
{
FUNCTION_STATUS_SUCCESS,
FUNCTION_NOT_A_FUNCTION,
FUNCTION_UNBALANCED_PARENTHESES
} function_status_t;
/* Find a set of function-like macro arguments by looking for a
* balanced set of parentheses.
*
* When called, 'node' should be the opening-parenthesis token, (or
* perhaps preceeding SPACE tokens). Upon successful return *last will
* be the last consumed node, (corresponding to the closing right
* parenthesis).
*
* Return values:
*
* FUNCTION_STATUS_SUCCESS:
*
* Successfully parsed a set of function arguments.
*
* FUNCTION_NOT_A_FUNCTION:
*
* Macro name not followed by a '('. This is not an error, but
* simply that the macro name should be treated as a non-macro.
*
* FUNCTION_UNBALANCED_PARENTHESES
*
* Macro name is not followed by a balanced set of parentheses.
*/
static function_status_t
_arguments_parse (argument_list_t *arguments,
token_node_t *node,
token_node_t **last)
{
token_list_t *argument;
int paren_count;
node = node->next;
/* Ignore whitespace before first parenthesis. */
while (node && node->token->type == SPACE)
node = node->next;
if (node == NULL || node->token->type != '(')
return FUNCTION_NOT_A_FUNCTION;
node = node->next;
argument = _token_list_create (arguments);
_argument_list_append (arguments, argument);
for (paren_count = 1; node; node = node->next) {
if (node->token->type == '(')
{
paren_count++;
}
else if (node->token->type == ')')
{
paren_count--;
if (paren_count == 0)
break;
}
if (node->token->type == ',' &&
paren_count == 1)
{
_token_list_trim_trailing_space (argument);
argument = _token_list_create (arguments);
_argument_list_append (arguments, argument);
}
else {
if (argument->head == NULL) {
/* Don't treat initial whitespace as
* part of the arguement. */
if (node->token->type == SPACE)
continue;
}
_token_list_append (argument, node->token);
}
}
if (paren_count)
return FUNCTION_UNBALANCED_PARENTHESES;
*last = node;
return FUNCTION_STATUS_SUCCESS;
}
static token_list_t *
_token_list_create_with_one_ival (void *ctx, int type, int ival)
{
token_list_t *list;
token_t *node;
list = _token_list_create (ctx);
node = _token_create_ival (list, type, ival);
_token_list_append (list, node);
return list;
}
static token_list_t *
_token_list_create_with_one_space (void *ctx)
{
return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
}
static token_list_t *
_token_list_create_with_one_integer (void *ctx, int ival)
{
return _token_list_create_with_one_ival (ctx, INTEGER, ival);
}
/* Perform macro expansion on 'list', placing the resulting tokens
* into a new list which is initialized with a first token of type
* 'head_token_type'. Then begin lexing from the resulting list,
* (return to the current lexing source when this list is exhausted).
*/
static void
_glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
int head_token_type,
token_list_t *list)
{
token_list_t *expanded;
token_t *token;
expanded = _token_list_create (parser);
token = _token_create_ival (parser, head_token_type, head_token_type);
_token_list_append (expanded, token);
_glcpp_parser_expand_token_list (parser, list);
_token_list_append_list (expanded, list);
glcpp_parser_lex_from (parser, expanded);
}
static void
_glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
{
token_node_t *node;
node = list->head;
while (node)
{
token_node_t *next_non_space;
/* Look ahead for a PASTE token, skipping space. */
next_non_space = node->next;
while (next_non_space && next_non_space->token->type == SPACE)
next_non_space = next_non_space->next;
if (next_non_space == NULL)
break;
if (next_non_space->token->type != PASTE) {
node = next_non_space;
continue;
}
/* Now find the next non-space token after the PASTE. */
next_non_space = next_non_space->next;
while (next_non_space && next_non_space->token->type == SPACE)
next_non_space = next_non_space->next;
if (next_non_space == NULL) {
yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
return;
}
node->token = _token_paste (parser, node->token, next_non_space->token);
node->next = next_non_space->next;
if (next_non_space == list->tail)
list->tail = node;
}
list->non_space_tail = list->tail;
}
/* This is a helper function that's essentially part of the
* implementation of _glcpp_parser_expand_node. It shouldn't be called
* except for by that function.
*
* Returns NULL if node is a simple token with no expansion, (that is,
* although 'node' corresponds to an identifier defined as a
* function-like macro, it is not followed with a parenthesized
* argument list).
*
* Compute the complete expansion of node (which is a function-like
* macro) and subsequent nodes which are arguments.
*
* Returns the token list that results from the expansion and sets
* *last to the last node in the list that was consumed by the
* expansion. Specifically, *last will be set as follows: as the
* token of the closing right parenthesis.
*/
static token_list_t *
_glcpp_parser_expand_function (glcpp_parser_t *parser,
token_node_t *node,
token_node_t **last)
{
macro_t *macro;
const char *identifier;
argument_list_t *arguments;
function_status_t status;
token_list_t *substituted;
int parameter_index;
identifier = node->token->value.str;
macro = hash_table_find (parser->defines, identifier);
assert (macro->is_function);
arguments = _argument_list_create (parser);
status = _arguments_parse (arguments, node, last);
switch (status) {
case FUNCTION_STATUS_SUCCESS:
break;
case FUNCTION_NOT_A_FUNCTION:
return NULL;
case FUNCTION_UNBALANCED_PARENTHESES:
glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
return NULL;
}
/* Replace a macro defined as empty with a SPACE token. */
if (macro->replacements == NULL) {
ralloc_free (arguments);
return _token_list_create_with_one_space (parser);
}
if (! ((_argument_list_length (arguments) ==
_string_list_length (macro->parameters)) ||
(_string_list_length (macro->parameters) == 0 &&
_argument_list_length (arguments) == 1 &&
arguments->head->argument->head == NULL)))
{
glcpp_error (&node->token->location, parser,
"Error: macro %s invoked with %d arguments (expected %d)\n",
identifier,
_argument_list_length (arguments),
_string_list_length (macro->parameters));
return NULL;
}
/* Perform argument substitution on the replacement list. */
substituted = _token_list_create (arguments);
for (node = macro->replacements->head; node; node = node->next)
{
if (node->token->type == IDENTIFIER &&
_string_list_contains (macro->parameters,
node->token->value.str,
¶meter_index))
{
token_list_t *argument;
argument = _argument_list_member_at (arguments,
parameter_index);
/* Before substituting, we expand the argument
* tokens, or append a placeholder token for
* an empty argument. */
if (argument->head) {
token_list_t *expanded_argument;
expanded_argument = _token_list_copy (parser,
argument);
_glcpp_parser_expand_token_list (parser,
expanded_argument);
_token_list_append_list (substituted,
expanded_argument);
} else {
token_t *new_token;
new_token = _token_create_ival (substituted,
PLACEHOLDER,
PLACEHOLDER);
_token_list_append (substituted, new_token);
}
} else {
_token_list_append (substituted, node->token);
}
}
/* After argument substitution, and before further expansion
* below, implement token pasting. */
_token_list_trim_trailing_space (substituted);
_glcpp_parser_apply_pastes (parser, substituted);
return substituted;
}
/* Compute the complete expansion of node, (and subsequent nodes after
* 'node' in the case that 'node' is a function-like macro and
* subsequent nodes are arguments).
*
* Returns NULL if node is a simple token with no expansion.
*
* Otherwise, returns the token list that results from the expansion
* and sets *last to the last node in the list that was consumed by
* the expansion. Specifically, *last will be set as follows:
*
* As 'node' in the case of object-like macro expansion.
*
* As the token of the closing right parenthesis in the case of
* function-like macro expansion.
*/
static token_list_t *
_glcpp_parser_expand_node (glcpp_parser_t *parser,
token_node_t *node,
token_node_t **last)
{
token_t *token = node->token;
const char *identifier;
macro_t *macro;
/* We only expand identifiers */
if (token->type != IDENTIFIER) {
/* We change any COMMA into a COMMA_FINAL to prevent
* it being mistaken for an argument separator
* later. */
if (token->type == ',') {
token->type = COMMA_FINAL;
token->value.ival = COMMA_FINAL;
}
return NULL;
}
*last = node;
identifier = token->value.str;
/* Special handling for __LINE__ and __FILE__, (not through
* the hash table). */
if (strcmp(identifier, "__LINE__") == 0)
return _token_list_create_with_one_integer (parser, node->token->location.first_line);
if (strcmp(identifier, "__FILE__") == 0)
return _token_list_create_with_one_integer (parser, node->token->location.source);
/* Look up this identifier in the hash table. */
macro = hash_table_find (parser->defines, identifier);
/* Not a macro, so no expansion needed. */
if (macro == NULL)
return NULL;
/* Finally, don't expand this macro if we're already actively
* expanding it, (to avoid infinite recursion). */
if (_parser_active_list_contains (parser, identifier)) {
/* We change the token type here from IDENTIFIER to
* OTHER to prevent any future expansion of this
* unexpanded token. */
char *str;
token_list_t *expansion;
token_t *final;
str = ralloc_strdup (parser, token->value.str);
final = _token_create_str (parser, OTHER, str);
expansion = _token_list_create (parser);
_token_list_append (expansion, final);
return expansion;
}
if (! macro->is_function)
{
token_list_t *replacement;
/* Replace a macro defined as empty with a SPACE token. */
if (macro->replacements == NULL)
return _token_list_create_with_one_space (parser);
replacement = _token_list_copy (parser, macro->replacements);
_glcpp_parser_apply_pastes (parser, replacement);
return replacement;
}
return _glcpp_parser_expand_function (parser, node, last);
}
/* Push a new identifier onto the parser's active list.
*
* Here, 'marker' is the token node that appears in the list after the
* expansion of 'identifier'. That is, when the list iterator begins
* examining 'marker', then it is time to pop this node from the
* active stack.
*/
static void
_parser_active_list_push (glcpp_parser_t *parser,
const char *identifier,
token_node_t *marker)
{
active_list_t *node;
node = ralloc (parser->active, active_list_t);
node->identifier = ralloc_strdup (node, identifier);
node->marker = marker;
node->next = parser->active;
parser->active = node;
}
static void
_parser_active_list_pop (glcpp_parser_t *parser)
{
active_list_t *node = parser->active;
if (node == NULL) {
parser->active = NULL;
return;
}
node = parser->active->next;
ralloc_free (parser->active);
parser->active = node;
}
static int
_parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
{
active_list_t *node;
if (parser->active == NULL)
return 0;
for (node = parser->active; node; node = node->next)
if (strcmp (node->identifier, identifier) == 0)
return 1;
return 0;
}
/* Walk over the token list replacing nodes with their expansion.
* Whenever nodes are expanded the walking will walk over the new
* nodes, continuing to expand as necessary. The results are placed in
* 'list' itself;
*/
static void
_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
token_list_t *list)
{
token_node_t *node_prev;
token_node_t *node, *last = NULL;
token_list_t *expansion;
active_list_t *active_initial = parser->active;
if (list == NULL)
return;
_token_list_trim_trailing_space (list);
node_prev = NULL;
node = list->head;
while (node) {
while (parser->active && parser->active->marker == node)
_parser_active_list_pop (parser);
expansion = _glcpp_parser_expand_node (parser, node, &last);
if (expansion) {
token_node_t *n;
for (n = node; n != last->next; n = n->next)
while (parser->active &&
parser->active->marker == n)
{
_parser_active_list_pop (parser);
}
_parser_active_list_push (parser,
node->token->value.str,
last->next);
/* Splice expansion into list, supporting a
* simple deletion if the expansion is
* empty. */
if (expansion->head) {
if (node_prev)
node_prev->next = expansion->head;
else
list->head = expansion->head;
expansion->tail->next = last->next;
if (last == list->tail)
list->tail = expansion->tail;
} else {
if (node_prev)
node_prev->next = last->next;
else
list->head = last->next;
if (last == list->tail)
list->tail = NULL;
}
} else {
node_prev = node;
}
node = node_prev ? node_prev->next : list->head;
}
/* Remove any lingering effects of this invocation on the
* active list. That is, pop until the list looks like it did
* at the beginning of this function. */
while (parser->active && parser->active != active_initial)
_parser_active_list_pop (parser);
list->non_space_tail = list->tail;
}
void
_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
token_list_t *list)
{
if (list == NULL)
return;
_glcpp_parser_expand_token_list (parser, list);
_token_list_trim_trailing_space (list);
_token_list_print (parser, list);
}
static void
_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
const char *identifier)
{
/* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
* the GLSL ES spec (all versions) say:
*
* "All macro names containing two consecutive underscores ( __ )
* are reserved for future use as predefined macro names. All
* macro names prefixed with "GL_" ("GL" followed by a single
* underscore) are also reserved."
*
* The intention is that names containing __ are reserved for internal
* use by the implementation, and names prefixed with GL_ are reserved
* for use by Khronos. Since every extension adds a name prefixed
* with GL_ (i.e., the name of the extension), that should be an
* error. Names simply containing __ are dangerous to use, but should
* be allowed.
*
* A future version of the GLSL specification will clarify this.
*/
if (strstr(identifier, "__")) {
glcpp_warning(loc, parser,
"Macro names containing \"__\" are reserved "
"for use by the implementation.\n");
}
if (strncmp(identifier, "GL_", 3) == 0) {
glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
}
}
static int
_macro_equal (macro_t *a, macro_t *b)
{
if (a->is_function != b->is_function)
return 0;
if (a->is_function) {
if (! _string_list_equal (a->parameters, b->parameters))
return 0;
}
return _token_list_equal_ignoring_space (a->replacements,
b->replacements);
}
void
_define_object_macro (glcpp_parser_t *parser,
YYLTYPE *loc,
const char *identifier,
token_list_t *replacements)
{
macro_t *macro, *previous;
if (loc != NULL)
_check_for_reserved_macro_name(parser, loc, identifier);
macro = ralloc (parser, macro_t);
macro->is_function = 0;
macro->parameters = NULL;
macro->identifier = ralloc_strdup (macro, identifier);
macro->replacements = replacements;
ralloc_steal (macro, replacements);
previous = hash_table_find (parser->defines, identifier);
if (previous) {
if (_macro_equal (macro, previous)) {
ralloc_free (macro);
return;
}
glcpp_error (loc, parser, "Redefinition of macro %s\n",
identifier);
}
hash_table_insert (parser->defines, macro, identifier);
}
void
_define_function_macro (glcpp_parser_t *parser,
YYLTYPE *loc,
const char *identifier,
string_list_t *parameters,
token_list_t *replacements)
{
macro_t *macro, *previous;
_check_for_reserved_macro_name(parser, loc, identifier);
macro = ralloc (parser, macro_t);
ralloc_steal (macro, parameters);
ralloc_steal (macro, replacements);
macro->is_function = 1;
macro->parameters = parameters;
macro->identifier = ralloc_strdup (macro, identifier);
macro->replacements = replacements;
previous = hash_table_find (parser->defines, identifier);
if (previous) {
if (_macro_equal (macro, previous)) {
ralloc_free (macro);
return;
}
glcpp_error (loc, parser, "Redefinition of macro %s\n",
identifier);
}
hash_table_insert (parser->defines, macro, identifier);
}
static int
glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
{
token_node_t *node;
int ret;
if (parser->lex_from_list == NULL) {
ret = glcpp_lex (yylval, yylloc, parser->scanner);
/* XXX: This ugly block of code exists for the sole
* purpose of converting a NEWLINE token into a SPACE
* token, but only in the case where we have seen a
* function-like macro name, but have not yet seen its
* closing parenthesis.
*
* There's perhaps a more compact way to do this with
* mid-rule actions in the grammar.
*
* I'm definitely not pleased with the complexity of
* this code here.
*/
if (parser->newline_as_space)
{
if (ret == '(') {
parser->paren_count++;
} else if (ret == ')') {
parser->paren_count--;
if (parser->paren_count == 0)
parser->newline_as_space = 0;
} else if (ret == NEWLINE) {
ret = SPACE;
} else if (ret != SPACE) {
if (parser->paren_count == 0)
parser->newline_as_space = 0;
}
}
else if (parser->in_control_line)
{
if (ret == NEWLINE)
parser->in_control_line = 0;
}
else if (ret == HASH_DEFINE ||
ret == HASH_UNDEF || ret == HASH_IF ||
ret == HASH_IFDEF || ret == HASH_IFNDEF ||
ret == HASH_ELIF || ret == HASH_ELSE ||
ret == HASH_ENDIF || ret == HASH)
{
parser->in_control_line = 1;
}
else if (ret == IDENTIFIER)
{
macro_t *macro;
macro = hash_table_find (parser->defines,
yylval->str);
if (macro && macro->is_function) {
parser->newline_as_space = 1;
parser->paren_count = 0;
}
}
return ret;
}
node = parser->lex_from_node;
if (node == NULL) {
ralloc_free (parser->lex_from_list);
parser->lex_from_list = NULL;
return NEWLINE;
}
*yylval = node->token->value;
ret = node->token->type;
parser->lex_from_node = node->next;
return ret;
}
static void
glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
{
token_node_t *node;
assert (parser->lex_from_list == NULL);
/* Copy list, eliminating any space tokens. */
parser->lex_from_list = _token_list_create (parser);
for (node = list->head; node; node = node->next) {
if (node->token->type == SPACE)
continue;
_token_list_append (parser->lex_from_list, node->token);
}
ralloc_free (list);
parser->lex_from_node = parser->lex_from_list->head;
/* It's possible the list consisted of nothing but whitespace. */
if (parser->lex_from_node == NULL) {
ralloc_free (parser->lex_from_list);
parser->lex_from_list = NULL;
}
}
static void
_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
int condition)
{
skip_type_t current = SKIP_NO_SKIP;
skip_node_t *node;
if (parser->skip_stack)
current = parser->skip_stack->type;
node = ralloc (parser, skip_node_t);
node->loc = *loc;
if (current == SKIP_NO_SKIP) {
if (condition)
node->type = SKIP_NO_SKIP;
else
node->type = SKIP_TO_ELSE;
} else {
node->type = SKIP_TO_ENDIF;
}
node->has_else = false;
node->next = parser->skip_stack;
parser->skip_stack = node;
}
static void
_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
const char *type, int condition)
{
if (parser->skip_stack == NULL) {
glcpp_error (loc, parser, "%s without #if\n", type);
return;
}
if (parser->skip_stack->type == SKIP_TO_ELSE) {
if (condition)
parser->skip_stack->type = SKIP_NO_SKIP;
} else {
parser->skip_stack->type = SKIP_TO_ENDIF;
}
}
static void
_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
{
skip_node_t *node;
if (parser->skip_stack == NULL) {
glcpp_error (loc, parser, "#endif without #if\n");
return;
}
node = parser->skip_stack;
parser->skip_stack = node->next;
ralloc_free (node);
}
static void
_glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
const char *es_identifier,
bool explicitly_set)
{
const struct gl_extensions *extensions = parser->extensions;
if (parser->version_resolved)
return;
parser->version_resolved = true;
add_builtin_define (parser, "__VERSION__", version);
parser->is_gles = (version == 100) ||
(es_identifier &&
(strcmp(es_identifier, "es") == 0));
/* Add pre-defined macros. */
if (parser->is_gles) {
add_builtin_define(parser, "GL_ES", 1);
add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
if (extensions != NULL) {
if (extensions->OES_EGL_image_external)
add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
}
} else {
add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
if (extensions != NULL) {
if (extensions->EXT_texture_array)
add_builtin_define(parser, "GL_EXT_texture_array", 1);
if (extensions->ARB_arrays_of_arrays)
add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
if (extensions->ARB_fragment_coord_conventions)
add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1);
if (extensions->ARB_fragment_layer_viewport)
add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
if (extensions->ARB_explicit_attrib_location)
add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
if (extensions->ARB_explicit_uniform_location)
add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
if (extensions->ARB_shader_texture_lod)
add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
if (extensions->ARB_draw_instanced)
add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
if (extensions->ARB_conservative_depth) {
add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
}
if (extensions->ARB_shader_bit_encoding)
add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
if (extensions->ARB_uniform_buffer_object)
add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
if (extensions->ARB_texture_cube_map_array)
add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
if (extensions->ARB_shading_language_packing)
add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
if (extensions->ARB_texture_multisample)
add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
if (extensions->ARB_texture_query_levels)
add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
if (extensions->ARB_texture_query_lod)
add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
if (extensions->ARB_gpu_shader5)
add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
if (extensions->AMD_vertex_shader_layer)
add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
if (extensions->AMD_vertex_shader_viewport_index)
add_builtin_define(parser, "GL_AMD_vertex_shader_viewport_index", 1);
if (extensions->ARB_shading_language_420pack)
add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
if (extensions->ARB_sample_shading)
add_builtin_define(parser, "GL_ARB_sample_shading", 1);
if (extensions->ARB_texture_gather)
add_builtin_define(parser, "GL_ARB_texture_gather", 1);
if (extensions->ARB_shader_atomic_counters)
add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
if (extensions->ARB_viewport_array)
add_builtin_define(parser, "GL_ARB_viewport_array", 1);
if (extensions->ARB_compute_shader)
add_builtin_define(parser, "GL_ARB_compute_shader", 1);
if (extensions->ARB_shader_image_load_store)
add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
}
}
if (extensions != NULL) {
if (extensions->EXT_shader_integer_mix)
add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
}
if (version >= 150)
add_builtin_define(parser, "GL_core_profile", 1);
/* Currently, all ES2/ES3 implementations support highp in the
* fragment shader, so we always define this macro in ES2/ES3.
* If we ever get a driver that doesn't support highp, we'll
* need to add a flag to the gl_context and check that here.
*/
if (version >= 130 || parser->is_gles)
add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
if (explicitly_set) {
ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
"#version %" PRIiMAX "%s%s", version,
es_identifier ? " " : "",
es_identifier ? es_identifier : "");
}
}
/* GLSL version if no version is explicitly specified. */
#define IMPLICIT_GLSL_VERSION 110
/* GLSL ES version if no version is explicitly specified. */
#define IMPLICIT_GLSL_ES_VERSION 100
void
glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
{
int language_version = parser->api == API_OPENGLES2 ?
IMPLICIT_GLSL_ES_VERSION :
IMPLICIT_GLSL_VERSION;
_glcpp_parser_handle_version_declaration(parser, language_version,
NULL, false);
}
|