glsl: Add arb_cull_distance support (v3)
[mesa.git] / src / compiler / glsl / glcpp / glcpp-parse.y
1 %{
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <inttypes.h>
30
31 #include "glcpp.h"
32 #include "main/core.h" /* for struct gl_extensions */
33 #include "main/mtypes.h" /* for gl_api enum */
34
35 static void
36 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
37
38 static void
39 _define_object_macro(glcpp_parser_t *parser,
40 YYLTYPE *loc,
41 const char *macro,
42 token_list_t *replacements);
43
44 static void
45 _define_function_macro(glcpp_parser_t *parser,
46 YYLTYPE *loc,
47 const char *macro,
48 string_list_t *parameters,
49 token_list_t *replacements);
50
51 static string_list_t *
52 _string_list_create(void *ctx);
53
54 static void
55 _string_list_append_item(string_list_t *list, const char *str);
56
57 static int
58 _string_list_contains(string_list_t *list, const char *member, int *index);
59
60 static const char *
61 _string_list_has_duplicate(string_list_t *list);
62
63 static int
64 _string_list_length(string_list_t *list);
65
66 static int
67 _string_list_equal(string_list_t *a, string_list_t *b);
68
69 static argument_list_t *
70 _argument_list_create(void *ctx);
71
72 static void
73 _argument_list_append(argument_list_t *list, token_list_t *argument);
74
75 static int
76 _argument_list_length(argument_list_t *list);
77
78 static token_list_t *
79 _argument_list_member_at(argument_list_t *list, int index);
80
81 /* Note: This function ralloc_steal()s the str pointer. */
82 static token_t *
83 _token_create_str(void *ctx, int type, char *str);
84
85 static token_t *
86 _token_create_ival(void *ctx, int type, int ival);
87
88 static token_list_t *
89 _token_list_create(void *ctx);
90
91 static void
92 _token_list_append(token_list_t *list, token_t *token);
93
94 static void
95 _token_list_append_list(token_list_t *list, token_list_t *tail);
96
97 static int
98 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b);
99
100 static void
101 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
102 token_node_t *marker);
103
104 static void
105 _parser_active_list_pop(glcpp_parser_t *parser);
106
107 static int
108 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier);
109
110 typedef enum {
111 EXPANSION_MODE_IGNORE_DEFINED,
112 EXPANSION_MODE_EVALUATE_DEFINED
113 } expansion_mode_t;
114
115 /* Expand list, and begin lexing from the result (after first
116 * prefixing a token of type 'head_token_type').
117 */
118 static void
119 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
120 token_list_t *list, expansion_mode_t mode);
121
122 /* Perform macro expansion in-place on the given list. */
123 static void
124 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
125 expansion_mode_t mode);
126
127 static void
128 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
129 token_list_t *list);
130
131 static void
132 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
133 int condition);
134
135 static void
136 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
137 const char *type, int condition);
138
139 static void
140 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc);
141
142 static void
143 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
144 const char *ident, bool explicitly_set);
145
146 static int
147 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
148
149 static void
150 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list);
151
152 static void
153 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
154
155 %}
156
157 %pure-parser
158 %error-verbose
159
160 %locations
161 %initial-action {
162 @$.first_line = 1;
163 @$.first_column = 1;
164 @$.last_line = 1;
165 @$.last_column = 1;
166 @$.source = 0;
167 }
168
169 %parse-param {glcpp_parser_t *parser}
170 %lex-param {glcpp_parser_t *parser}
171
172 %expect 0
173
174 /* We use HASH_TOKEN, DEFINE_TOKEN and VERSION_TOKEN (as opposed to
175 * HASH, DEFINE, and VERSION) to avoid conflicts with other symbols,
176 * (such as the <HASH> and <DEFINE> start conditions in the lexer). */
177 %token DEFINED ELIF_EXPANDED HASH_TOKEN DEFINE_TOKEN FUNC_IDENTIFIER OBJ_IDENTIFIER ELIF ELSE ENDIF ERROR_TOKEN IF IFDEF IFNDEF LINE PRAGMA UNDEF VERSION_TOKEN GARBAGE IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE PLUS_PLUS MINUS_MINUS
178 %token PASTE
179 %type <ival> INTEGER operator SPACE integer_constant
180 %type <expression_value> expression
181 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA
182 %type <string_list> identifier_list
183 %type <token> preprocessing_token
184 %type <token_list> pp_tokens replacement_list text_line
185 %left OR
186 %left AND
187 %left '|'
188 %left '^'
189 %left '&'
190 %left EQUAL NOT_EQUAL
191 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
192 %left LEFT_SHIFT RIGHT_SHIFT
193 %left '+' '-'
194 %left '*' '/' '%'
195 %right UNARY
196
197 %debug
198
199 %%
200
201 input:
202 /* empty */
203 | input line
204 ;
205
206 line:
207 control_line
208 | SPACE control_line
209 | text_line {
210 _glcpp_parser_print_expanded_token_list (parser, $1);
211 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
212 ralloc_free ($1);
213 }
214 | expanded_line
215 ;
216
217 expanded_line:
218 IF_EXPANDED expression NEWLINE {
219 if (parser->is_gles && $2.undefined_macro)
220 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
221 _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
222 }
223 | ELIF_EXPANDED expression NEWLINE {
224 if (parser->is_gles && $2.undefined_macro)
225 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
226 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
227 }
228 | LINE_EXPANDED integer_constant NEWLINE {
229 parser->has_new_line_number = 1;
230 parser->new_line_number = $2;
231 ralloc_asprintf_rewrite_tail (&parser->output,
232 &parser->output_length,
233 "#line %" PRIiMAX "\n",
234 $2);
235 }
236 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
237 parser->has_new_line_number = 1;
238 parser->new_line_number = $2;
239 parser->has_new_source_number = 1;
240 parser->new_source_number = $3;
241 ralloc_asprintf_rewrite_tail (&parser->output,
242 &parser->output_length,
243 "#line %" PRIiMAX " %" PRIiMAX "\n",
244 $2, $3);
245 }
246 ;
247
248 define:
249 OBJ_IDENTIFIER replacement_list NEWLINE {
250 _define_object_macro (parser, & @1, $1, $2);
251 }
252 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
253 _define_function_macro (parser, & @1, $1, NULL, $4);
254 }
255 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
256 _define_function_macro (parser, & @1, $1, $3, $5);
257 }
258 ;
259
260 control_line:
261 control_line_success {
262 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
263 }
264 | control_line_error
265 | HASH_TOKEN LINE pp_tokens NEWLINE {
266
267 if (parser->skip_stack == NULL ||
268 parser->skip_stack->type == SKIP_NO_SKIP)
269 {
270 _glcpp_parser_expand_and_lex_from (parser,
271 LINE_EXPANDED, $3,
272 EXPANSION_MODE_IGNORE_DEFINED);
273 }
274 }
275 ;
276
277 control_line_success:
278 HASH_TOKEN DEFINE_TOKEN define
279 | HASH_TOKEN UNDEF IDENTIFIER NEWLINE {
280 macro_t *macro;
281 if (strcmp("__LINE__", $3) == 0
282 || strcmp("__FILE__", $3) == 0
283 || strcmp("__VERSION__", $3) == 0
284 || strncmp("GL_", $3, 3) == 0)
285 glcpp_error(& @1, parser, "Built-in (pre-defined)"
286 " macro names cannot be undefined.");
287
288 macro = hash_table_find (parser->defines, $3);
289 if (macro) {
290 hash_table_remove (parser->defines, $3);
291 ralloc_free (macro);
292 }
293 ralloc_free ($3);
294 }
295 | HASH_TOKEN IF pp_tokens NEWLINE {
296 /* Be careful to only evaluate the 'if' expression if
297 * we are not skipping. When we are skipping, we
298 * simply push a new 0-valued 'if' onto the skip
299 * stack.
300 *
301 * This avoids generating diagnostics for invalid
302 * expressions that are being skipped. */
303 if (parser->skip_stack == NULL ||
304 parser->skip_stack->type == SKIP_NO_SKIP)
305 {
306 _glcpp_parser_expand_and_lex_from (parser,
307 IF_EXPANDED, $3,
308 EXPANSION_MODE_EVALUATE_DEFINED);
309 }
310 else
311 {
312 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
313 parser->skip_stack->type = SKIP_TO_ENDIF;
314 }
315 }
316 | HASH_TOKEN IF NEWLINE {
317 /* #if without an expression is only an error if we
318 * are not skipping */
319 if (parser->skip_stack == NULL ||
320 parser->skip_stack->type == SKIP_NO_SKIP)
321 {
322 glcpp_error(& @1, parser, "#if with no expression");
323 }
324 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
325 }
326 | HASH_TOKEN IFDEF IDENTIFIER junk NEWLINE {
327 macro_t *macro = hash_table_find (parser->defines, $3);
328 ralloc_free ($3);
329 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
330 }
331 | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE {
332 macro_t *macro = hash_table_find (parser->defines, $3);
333 ralloc_free ($3);
334 _glcpp_parser_skip_stack_push_if (parser, & @3, macro == NULL);
335 }
336 | HASH_TOKEN ELIF pp_tokens NEWLINE {
337 /* Be careful to only evaluate the 'elif' expression
338 * if we are not skipping. When we are skipping, we
339 * simply change to a 0-valued 'elif' on the skip
340 * stack.
341 *
342 * This avoids generating diagnostics for invalid
343 * expressions that are being skipped. */
344 if (parser->skip_stack &&
345 parser->skip_stack->type == SKIP_TO_ELSE)
346 {
347 _glcpp_parser_expand_and_lex_from (parser,
348 ELIF_EXPANDED, $3,
349 EXPANSION_MODE_EVALUATE_DEFINED);
350 }
351 else if (parser->skip_stack &&
352 parser->skip_stack->has_else)
353 {
354 glcpp_error(& @1, parser, "#elif after #else");
355 }
356 else
357 {
358 _glcpp_parser_skip_stack_change_if (parser, & @1,
359 "elif", 0);
360 }
361 }
362 | HASH_TOKEN ELIF NEWLINE {
363 /* #elif without an expression is an error unless we
364 * are skipping. */
365 if (parser->skip_stack &&
366 parser->skip_stack->type == SKIP_TO_ELSE)
367 {
368 glcpp_error(& @1, parser, "#elif with no expression");
369 }
370 else if (parser->skip_stack &&
371 parser->skip_stack->has_else)
372 {
373 glcpp_error(& @1, parser, "#elif after #else");
374 }
375 else
376 {
377 _glcpp_parser_skip_stack_change_if (parser, & @1,
378 "elif", 0);
379 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
380 }
381 }
382 | HASH_TOKEN ELSE { parser->lexing_directive = 1; } NEWLINE {
383 if (parser->skip_stack &&
384 parser->skip_stack->has_else)
385 {
386 glcpp_error(& @1, parser, "multiple #else");
387 }
388 else
389 {
390 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
391 if (parser->skip_stack)
392 parser->skip_stack->has_else = true;
393 }
394 }
395 | HASH_TOKEN ENDIF {
396 _glcpp_parser_skip_stack_pop (parser, & @1);
397 } NEWLINE
398 | HASH_TOKEN VERSION_TOKEN integer_constant NEWLINE {
399 if (parser->version_resolved) {
400 glcpp_error(& @1, parser, "#version must appear on the first line");
401 }
402 _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
403 }
404 | HASH_TOKEN VERSION_TOKEN integer_constant IDENTIFIER NEWLINE {
405 if (parser->version_resolved) {
406 glcpp_error(& @1, parser, "#version must appear on the first line");
407 }
408 _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
409 }
410 | HASH_TOKEN NEWLINE {
411 glcpp_parser_resolve_implicit_version(parser);
412 }
413 | HASH_TOKEN PRAGMA NEWLINE {
414 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#%s", $2);
415 }
416 ;
417
418 control_line_error:
419 HASH_TOKEN ERROR_TOKEN NEWLINE {
420 glcpp_error(& @1, parser, "#%s", $2);
421 }
422 | HASH_TOKEN DEFINE_TOKEN NEWLINE {
423 glcpp_error (& @1, parser, "#define without macro name");
424 }
425 | HASH_TOKEN GARBAGE pp_tokens NEWLINE {
426 glcpp_error (& @1, parser, "Illegal non-directive after #");
427 }
428 ;
429
430 integer_constant:
431 INTEGER_STRING {
432 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
433 $$ = strtoll ($1 + 2, NULL, 16);
434 } else if ($1[0] == '0') {
435 $$ = strtoll ($1, NULL, 8);
436 } else {
437 $$ = strtoll ($1, NULL, 10);
438 }
439 }
440 | INTEGER {
441 $$ = $1;
442 }
443
444 expression:
445 integer_constant {
446 $$.value = $1;
447 $$.undefined_macro = NULL;
448 }
449 | IDENTIFIER {
450 $$.value = 0;
451 if (parser->is_gles)
452 $$.undefined_macro = ralloc_strdup (parser, $1);
453 else
454 $$.undefined_macro = NULL;
455 }
456 | expression OR expression {
457 $$.value = $1.value || $3.value;
458
459 /* Short-circuit: Only flag undefined from right side
460 * if left side evaluates to false.
461 */
462 if ($1.undefined_macro)
463 $$.undefined_macro = $1.undefined_macro;
464 else if (! $1.value)
465 $$.undefined_macro = $3.undefined_macro;
466 }
467 | expression AND expression {
468 $$.value = $1.value && $3.value;
469
470 /* Short-circuit: Only flag undefined from right-side
471 * if left side evaluates to true.
472 */
473 if ($1.undefined_macro)
474 $$.undefined_macro = $1.undefined_macro;
475 else if ($1.value)
476 $$.undefined_macro = $3.undefined_macro;
477 }
478 | expression '|' expression {
479 $$.value = $1.value | $3.value;
480 if ($1.undefined_macro)
481 $$.undefined_macro = $1.undefined_macro;
482 else
483 $$.undefined_macro = $3.undefined_macro;
484 }
485 | expression '^' expression {
486 $$.value = $1.value ^ $3.value;
487 if ($1.undefined_macro)
488 $$.undefined_macro = $1.undefined_macro;
489 else
490 $$.undefined_macro = $3.undefined_macro;
491 }
492 | expression '&' expression {
493 $$.value = $1.value & $3.value;
494 if ($1.undefined_macro)
495 $$.undefined_macro = $1.undefined_macro;
496 else
497 $$.undefined_macro = $3.undefined_macro;
498 }
499 | expression NOT_EQUAL expression {
500 $$.value = $1.value != $3.value;
501 if ($1.undefined_macro)
502 $$.undefined_macro = $1.undefined_macro;
503 else
504 $$.undefined_macro = $3.undefined_macro;
505 }
506 | expression EQUAL expression {
507 $$.value = $1.value == $3.value;
508 if ($1.undefined_macro)
509 $$.undefined_macro = $1.undefined_macro;
510 else
511 $$.undefined_macro = $3.undefined_macro;
512 }
513 | expression GREATER_OR_EQUAL expression {
514 $$.value = $1.value >= $3.value;
515 if ($1.undefined_macro)
516 $$.undefined_macro = $1.undefined_macro;
517 else
518 $$.undefined_macro = $3.undefined_macro;
519 }
520 | expression LESS_OR_EQUAL expression {
521 $$.value = $1.value <= $3.value;
522 if ($1.undefined_macro)
523 $$.undefined_macro = $1.undefined_macro;
524 else
525 $$.undefined_macro = $3.undefined_macro;
526 }
527 | expression '>' expression {
528 $$.value = $1.value > $3.value;
529 if ($1.undefined_macro)
530 $$.undefined_macro = $1.undefined_macro;
531 else
532 $$.undefined_macro = $3.undefined_macro;
533 }
534 | expression '<' expression {
535 $$.value = $1.value < $3.value;
536 if ($1.undefined_macro)
537 $$.undefined_macro = $1.undefined_macro;
538 else
539 $$.undefined_macro = $3.undefined_macro;
540 }
541 | expression RIGHT_SHIFT expression {
542 $$.value = $1.value >> $3.value;
543 if ($1.undefined_macro)
544 $$.undefined_macro = $1.undefined_macro;
545 else
546 $$.undefined_macro = $3.undefined_macro;
547 }
548 | expression LEFT_SHIFT expression {
549 $$.value = $1.value << $3.value;
550 if ($1.undefined_macro)
551 $$.undefined_macro = $1.undefined_macro;
552 else
553 $$.undefined_macro = $3.undefined_macro;
554 }
555 | expression '-' expression {
556 $$.value = $1.value - $3.value;
557 if ($1.undefined_macro)
558 $$.undefined_macro = $1.undefined_macro;
559 else
560 $$.undefined_macro = $3.undefined_macro;
561 }
562 | expression '+' expression {
563 $$.value = $1.value + $3.value;
564 if ($1.undefined_macro)
565 $$.undefined_macro = $1.undefined_macro;
566 else
567 $$.undefined_macro = $3.undefined_macro;
568 }
569 | expression '%' expression {
570 if ($3.value == 0) {
571 yyerror (& @1, parser,
572 "zero modulus in preprocessor directive");
573 } else {
574 $$.value = $1.value % $3.value;
575 }
576 if ($1.undefined_macro)
577 $$.undefined_macro = $1.undefined_macro;
578 else
579 $$.undefined_macro = $3.undefined_macro;
580 }
581 | expression '/' expression {
582 if ($3.value == 0) {
583 yyerror (& @1, parser,
584 "division by 0 in preprocessor directive");
585 } else {
586 $$.value = $1.value / $3.value;
587 }
588 if ($1.undefined_macro)
589 $$.undefined_macro = $1.undefined_macro;
590 else
591 $$.undefined_macro = $3.undefined_macro;
592 }
593 | expression '*' expression {
594 $$.value = $1.value * $3.value;
595 if ($1.undefined_macro)
596 $$.undefined_macro = $1.undefined_macro;
597 else
598 $$.undefined_macro = $3.undefined_macro;
599 }
600 | '!' expression %prec UNARY {
601 $$.value = ! $2.value;
602 $$.undefined_macro = $2.undefined_macro;
603 }
604 | '~' expression %prec UNARY {
605 $$.value = ~ $2.value;
606 $$.undefined_macro = $2.undefined_macro;
607 }
608 | '-' expression %prec UNARY {
609 $$.value = - $2.value;
610 $$.undefined_macro = $2.undefined_macro;
611 }
612 | '+' expression %prec UNARY {
613 $$.value = + $2.value;
614 $$.undefined_macro = $2.undefined_macro;
615 }
616 | '(' expression ')' {
617 $$ = $2;
618 }
619 ;
620
621 identifier_list:
622 IDENTIFIER {
623 $$ = _string_list_create (parser);
624 _string_list_append_item ($$, $1);
625 ralloc_steal ($$, $1);
626 }
627 | identifier_list ',' IDENTIFIER {
628 $$ = $1;
629 _string_list_append_item ($$, $3);
630 ralloc_steal ($$, $3);
631 }
632 ;
633
634 text_line:
635 NEWLINE { $$ = NULL; }
636 | pp_tokens NEWLINE
637 ;
638
639 replacement_list:
640 /* empty */ { $$ = NULL; }
641 | pp_tokens
642 ;
643
644 junk:
645 /* empty */
646 | pp_tokens {
647 glcpp_error(&@1, parser, "extra tokens at end of directive");
648 }
649 ;
650
651 pp_tokens:
652 preprocessing_token {
653 parser->space_tokens = 1;
654 $$ = _token_list_create (parser);
655 _token_list_append ($$, $1);
656 }
657 | pp_tokens preprocessing_token {
658 $$ = $1;
659 _token_list_append ($$, $2);
660 }
661 ;
662
663 preprocessing_token:
664 IDENTIFIER {
665 $$ = _token_create_str (parser, IDENTIFIER, $1);
666 $$->location = yylloc;
667 }
668 | INTEGER_STRING {
669 $$ = _token_create_str (parser, INTEGER_STRING, $1);
670 $$->location = yylloc;
671 }
672 | operator {
673 $$ = _token_create_ival (parser, $1, $1);
674 $$->location = yylloc;
675 }
676 | DEFINED {
677 $$ = _token_create_ival (parser, DEFINED, DEFINED);
678 $$->location = yylloc;
679 }
680 | OTHER {
681 $$ = _token_create_str (parser, OTHER, $1);
682 $$->location = yylloc;
683 }
684 | SPACE {
685 $$ = _token_create_ival (parser, SPACE, SPACE);
686 $$->location = yylloc;
687 }
688 ;
689
690 operator:
691 '[' { $$ = '['; }
692 | ']' { $$ = ']'; }
693 | '(' { $$ = '('; }
694 | ')' { $$ = ')'; }
695 | '{' { $$ = '{'; }
696 | '}' { $$ = '}'; }
697 | '.' { $$ = '.'; }
698 | '&' { $$ = '&'; }
699 | '*' { $$ = '*'; }
700 | '+' { $$ = '+'; }
701 | '-' { $$ = '-'; }
702 | '~' { $$ = '~'; }
703 | '!' { $$ = '!'; }
704 | '/' { $$ = '/'; }
705 | '%' { $$ = '%'; }
706 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
707 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
708 | '<' { $$ = '<'; }
709 | '>' { $$ = '>'; }
710 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
711 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
712 | EQUAL { $$ = EQUAL; }
713 | NOT_EQUAL { $$ = NOT_EQUAL; }
714 | '^' { $$ = '^'; }
715 | '|' { $$ = '|'; }
716 | AND { $$ = AND; }
717 | OR { $$ = OR; }
718 | ';' { $$ = ';'; }
719 | ',' { $$ = ','; }
720 | '=' { $$ = '='; }
721 | PASTE { $$ = PASTE; }
722 | PLUS_PLUS { $$ = PLUS_PLUS; }
723 | MINUS_MINUS { $$ = MINUS_MINUS; }
724 ;
725
726 %%
727
728 string_list_t *
729 _string_list_create(void *ctx)
730 {
731 string_list_t *list;
732
733 list = ralloc (ctx, string_list_t);
734 list->head = NULL;
735 list->tail = NULL;
736
737 return list;
738 }
739
740 void
741 _string_list_append_item(string_list_t *list, const char *str)
742 {
743 string_node_t *node;
744
745 node = ralloc (list, string_node_t);
746 node->str = ralloc_strdup (node, str);
747
748 node->next = NULL;
749
750 if (list->head == NULL) {
751 list->head = node;
752 } else {
753 list->tail->next = node;
754 }
755
756 list->tail = node;
757 }
758
759 int
760 _string_list_contains(string_list_t *list, const char *member, int *index)
761 {
762 string_node_t *node;
763 int i;
764
765 if (list == NULL)
766 return 0;
767
768 for (i = 0, node = list->head; node; i++, node = node->next) {
769 if (strcmp (node->str, member) == 0) {
770 if (index)
771 *index = i;
772 return 1;
773 }
774 }
775
776 return 0;
777 }
778
779 /* Return duplicate string in list (if any), NULL otherwise. */
780 const char *
781 _string_list_has_duplicate(string_list_t *list)
782 {
783 string_node_t *node, *dup;
784
785 if (list == NULL)
786 return NULL;
787
788 for (node = list->head; node; node = node->next) {
789 for (dup = node->next; dup; dup = dup->next) {
790 if (strcmp (node->str, dup->str) == 0)
791 return node->str;
792 }
793 }
794
795 return NULL;
796 }
797
798 int
799 _string_list_length(string_list_t *list)
800 {
801 int length = 0;
802 string_node_t *node;
803
804 if (list == NULL)
805 return 0;
806
807 for (node = list->head; node; node = node->next)
808 length++;
809
810 return length;
811 }
812
813 int
814 _string_list_equal(string_list_t *a, string_list_t *b)
815 {
816 string_node_t *node_a, *node_b;
817
818 if (a == NULL && b == NULL)
819 return 1;
820
821 if (a == NULL || b == NULL)
822 return 0;
823
824 for (node_a = a->head, node_b = b->head;
825 node_a && node_b;
826 node_a = node_a->next, node_b = node_b->next)
827 {
828 if (strcmp (node_a->str, node_b->str))
829 return 0;
830 }
831
832 /* Catch the case of lists being different lengths, (which
833 * would cause the loop above to terminate after the shorter
834 * list). */
835 return node_a == node_b;
836 }
837
838 argument_list_t *
839 _argument_list_create(void *ctx)
840 {
841 argument_list_t *list;
842
843 list = ralloc (ctx, argument_list_t);
844 list->head = NULL;
845 list->tail = NULL;
846
847 return list;
848 }
849
850 void
851 _argument_list_append(argument_list_t *list, token_list_t *argument)
852 {
853 argument_node_t *node;
854
855 node = ralloc (list, argument_node_t);
856 node->argument = argument;
857
858 node->next = NULL;
859
860 if (list->head == NULL) {
861 list->head = node;
862 } else {
863 list->tail->next = node;
864 }
865
866 list->tail = node;
867 }
868
869 int
870 _argument_list_length(argument_list_t *list)
871 {
872 int length = 0;
873 argument_node_t *node;
874
875 if (list == NULL)
876 return 0;
877
878 for (node = list->head; node; node = node->next)
879 length++;
880
881 return length;
882 }
883
884 token_list_t *
885 _argument_list_member_at(argument_list_t *list, int index)
886 {
887 argument_node_t *node;
888 int i;
889
890 if (list == NULL)
891 return NULL;
892
893 node = list->head;
894 for (i = 0; i < index; i++) {
895 node = node->next;
896 if (node == NULL)
897 break;
898 }
899
900 if (node)
901 return node->argument;
902
903 return NULL;
904 }
905
906 /* Note: This function ralloc_steal()s the str pointer. */
907 token_t *
908 _token_create_str(void *ctx, int type, char *str)
909 {
910 token_t *token;
911
912 token = ralloc (ctx, token_t);
913 token->type = type;
914 token->value.str = str;
915
916 ralloc_steal (token, str);
917
918 return token;
919 }
920
921 token_t *
922 _token_create_ival(void *ctx, int type, int ival)
923 {
924 token_t *token;
925
926 token = ralloc (ctx, token_t);
927 token->type = type;
928 token->value.ival = ival;
929
930 return token;
931 }
932
933 token_list_t *
934 _token_list_create(void *ctx)
935 {
936 token_list_t *list;
937
938 list = ralloc (ctx, token_list_t);
939 list->head = NULL;
940 list->tail = NULL;
941 list->non_space_tail = NULL;
942
943 return list;
944 }
945
946 void
947 _token_list_append(token_list_t *list, token_t *token)
948 {
949 token_node_t *node;
950
951 node = ralloc (list, token_node_t);
952 node->token = token;
953 node->next = NULL;
954
955 if (list->head == NULL) {
956 list->head = node;
957 } else {
958 list->tail->next = node;
959 }
960
961 list->tail = node;
962 if (token->type != SPACE)
963 list->non_space_tail = node;
964 }
965
966 void
967 _token_list_append_list(token_list_t *list, token_list_t *tail)
968 {
969 if (tail == NULL || tail->head == NULL)
970 return;
971
972 if (list->head == NULL) {
973 list->head = tail->head;
974 } else {
975 list->tail->next = tail->head;
976 }
977
978 list->tail = tail->tail;
979 list->non_space_tail = tail->non_space_tail;
980 }
981
982 static token_list_t *
983 _token_list_copy(void *ctx, token_list_t *other)
984 {
985 token_list_t *copy;
986 token_node_t *node;
987
988 if (other == NULL)
989 return NULL;
990
991 copy = _token_list_create (ctx);
992 for (node = other->head; node; node = node->next) {
993 token_t *new_token = ralloc (copy, token_t);
994 *new_token = *node->token;
995 _token_list_append (copy, new_token);
996 }
997
998 return copy;
999 }
1000
1001 static void
1002 _token_list_trim_trailing_space(token_list_t *list)
1003 {
1004 token_node_t *tail, *next;
1005
1006 if (list->non_space_tail) {
1007 tail = list->non_space_tail->next;
1008 list->non_space_tail->next = NULL;
1009 list->tail = list->non_space_tail;
1010
1011 while (tail) {
1012 next = tail->next;
1013 ralloc_free (tail);
1014 tail = next;
1015 }
1016 }
1017 }
1018
1019 static int
1020 _token_list_is_empty_ignoring_space(token_list_t *l)
1021 {
1022 token_node_t *n;
1023
1024 if (l == NULL)
1025 return 1;
1026
1027 n = l->head;
1028 while (n != NULL && n->token->type == SPACE)
1029 n = n->next;
1030
1031 return n == NULL;
1032 }
1033
1034 int
1035 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1036 {
1037 token_node_t *node_a, *node_b;
1038
1039 if (a == NULL || b == NULL) {
1040 int a_empty = _token_list_is_empty_ignoring_space(a);
1041 int b_empty = _token_list_is_empty_ignoring_space(b);
1042 return a_empty == b_empty;
1043 }
1044
1045 node_a = a->head;
1046 node_b = b->head;
1047
1048 while (1)
1049 {
1050 if (node_a == NULL && node_b == NULL)
1051 break;
1052
1053 if (node_a == NULL || node_b == NULL)
1054 return 0;
1055 /* Make sure whitespace appears in the same places in both.
1056 * It need not be exactly the same amount of whitespace,
1057 * though.
1058 */
1059 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1060 while (node_a && node_a->token->type == SPACE)
1061 node_a = node_a->next;
1062 while (node_b && node_b->token->type == SPACE)
1063 node_b = node_b->next;
1064 continue;
1065 }
1066
1067 if (node_a->token->type != node_b->token->type)
1068 return 0;
1069
1070 switch (node_a->token->type) {
1071 case INTEGER:
1072 if (node_a->token->value.ival != node_b->token->value.ival) {
1073 return 0;
1074 }
1075 break;
1076 case IDENTIFIER:
1077 case INTEGER_STRING:
1078 case OTHER:
1079 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1080 return 0;
1081 }
1082 break;
1083 }
1084
1085 node_a = node_a->next;
1086 node_b = node_b->next;
1087 }
1088
1089 return 1;
1090 }
1091
1092 static void
1093 _token_print(char **out, size_t *len, token_t *token)
1094 {
1095 if (token->type < 256) {
1096 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1097 return;
1098 }
1099
1100 switch (token->type) {
1101 case INTEGER:
1102 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1103 break;
1104 case IDENTIFIER:
1105 case INTEGER_STRING:
1106 case OTHER:
1107 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1108 break;
1109 case SPACE:
1110 ralloc_asprintf_rewrite_tail (out, len, " ");
1111 break;
1112 case LEFT_SHIFT:
1113 ralloc_asprintf_rewrite_tail (out, len, "<<");
1114 break;
1115 case RIGHT_SHIFT:
1116 ralloc_asprintf_rewrite_tail (out, len, ">>");
1117 break;
1118 case LESS_OR_EQUAL:
1119 ralloc_asprintf_rewrite_tail (out, len, "<=");
1120 break;
1121 case GREATER_OR_EQUAL:
1122 ralloc_asprintf_rewrite_tail (out, len, ">=");
1123 break;
1124 case EQUAL:
1125 ralloc_asprintf_rewrite_tail (out, len, "==");
1126 break;
1127 case NOT_EQUAL:
1128 ralloc_asprintf_rewrite_tail (out, len, "!=");
1129 break;
1130 case AND:
1131 ralloc_asprintf_rewrite_tail (out, len, "&&");
1132 break;
1133 case OR:
1134 ralloc_asprintf_rewrite_tail (out, len, "||");
1135 break;
1136 case PASTE:
1137 ralloc_asprintf_rewrite_tail (out, len, "##");
1138 break;
1139 case PLUS_PLUS:
1140 ralloc_asprintf_rewrite_tail (out, len, "++");
1141 break;
1142 case MINUS_MINUS:
1143 ralloc_asprintf_rewrite_tail (out, len, "--");
1144 break;
1145 case DEFINED:
1146 ralloc_asprintf_rewrite_tail (out, len, "defined");
1147 break;
1148 case PLACEHOLDER:
1149 /* Nothing to print. */
1150 break;
1151 default:
1152 assert(!"Error: Don't know how to print token.");
1153
1154 break;
1155 }
1156 }
1157
1158 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1159 * 'token' and 'other'. Note that this function may return 'token' or
1160 * 'other' directly rather than allocating anything new.
1161 *
1162 * Caution: Only very cursory error-checking is performed to see if
1163 * the final result is a valid single token. */
1164 static token_t *
1165 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1166 {
1167 token_t *combined = NULL;
1168
1169 /* Pasting a placeholder onto anything makes no change. */
1170 if (other->type == PLACEHOLDER)
1171 return token;
1172
1173 /* When 'token' is a placeholder, just return 'other'. */
1174 if (token->type == PLACEHOLDER)
1175 return other;
1176
1177 /* A very few single-character punctuators can be combined
1178 * with another to form a multi-character punctuator. */
1179 switch (token->type) {
1180 case '<':
1181 if (other->type == '<')
1182 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1183 else if (other->type == '=')
1184 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1185 break;
1186 case '>':
1187 if (other->type == '>')
1188 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1189 else if (other->type == '=')
1190 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1191 break;
1192 case '=':
1193 if (other->type == '=')
1194 combined = _token_create_ival (token, EQUAL, EQUAL);
1195 break;
1196 case '!':
1197 if (other->type == '=')
1198 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1199 break;
1200 case '&':
1201 if (other->type == '&')
1202 combined = _token_create_ival (token, AND, AND);
1203 break;
1204 case '|':
1205 if (other->type == '|')
1206 combined = _token_create_ival (token, OR, OR);
1207 break;
1208 }
1209
1210 if (combined != NULL) {
1211 /* Inherit the location from the first token */
1212 combined->location = token->location;
1213 return combined;
1214 }
1215
1216 /* Two string-valued (or integer) tokens can usually just be
1217 * mashed together. (We also handle a string followed by an
1218 * integer here as well.)
1219 *
1220 * There are some exceptions here. Notably, if the first token
1221 * is an integer (or a string representing an integer), then
1222 * the second token must also be an integer or must be a
1223 * string representing an integer that begins with a digit.
1224 */
1225 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1226 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1227 {
1228 char *str;
1229 int combined_type;
1230
1231 /* Check that pasting onto an integer doesn't create a
1232 * non-integer, (that is, only digits can be
1233 * pasted. */
1234 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1235 switch (other->type) {
1236 case INTEGER_STRING:
1237 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1238 goto FAIL;
1239 break;
1240 case INTEGER:
1241 if (other->value.ival < 0)
1242 goto FAIL;
1243 break;
1244 default:
1245 goto FAIL;
1246 }
1247 }
1248
1249 if (token->type == INTEGER)
1250 str = ralloc_asprintf (token, "%" PRIiMAX, token->value.ival);
1251 else
1252 str = ralloc_strdup (token, token->value.str);
1253
1254 if (other->type == INTEGER)
1255 ralloc_asprintf_append (&str, "%" PRIiMAX, other->value.ival);
1256 else
1257 ralloc_strcat (&str, other->value.str);
1258
1259 /* New token is same type as original token, unless we
1260 * started with an integer, in which case we will be
1261 * creating an integer-string. */
1262 combined_type = token->type;
1263 if (combined_type == INTEGER)
1264 combined_type = INTEGER_STRING;
1265
1266 combined = _token_create_str (token, combined_type, str);
1267 combined->location = token->location;
1268 return combined;
1269 }
1270
1271 FAIL:
1272 glcpp_error (&token->location, parser, "");
1273 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1274 _token_print (&parser->info_log, &parser->info_log_length, token);
1275 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1276 _token_print (&parser->info_log, &parser->info_log_length, other);
1277 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1278
1279 return token;
1280 }
1281
1282 static void
1283 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1284 {
1285 token_node_t *node;
1286
1287 if (list == NULL)
1288 return;
1289
1290 for (node = list->head; node; node = node->next)
1291 _token_print (&parser->output, &parser->output_length, node->token);
1292 }
1293
1294 void
1295 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1296 {
1297 glcpp_error(locp, parser, "%s", error);
1298 }
1299
1300 static void
1301 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1302 {
1303 token_t *tok;
1304 token_list_t *list;
1305
1306 tok = _token_create_ival (parser, INTEGER, value);
1307
1308 list = _token_list_create(parser);
1309 _token_list_append(list, tok);
1310 _define_object_macro(parser, NULL, name, list);
1311 }
1312
1313 glcpp_parser_t *
1314 glcpp_parser_create(const struct gl_extensions *extensions, gl_api api)
1315 {
1316 glcpp_parser_t *parser;
1317
1318 parser = ralloc (NULL, glcpp_parser_t);
1319
1320 glcpp_lex_init_extra (parser, &parser->scanner);
1321 parser->defines = hash_table_ctor(32, hash_table_string_hash,
1322 hash_table_string_compare);
1323 parser->active = NULL;
1324 parser->lexing_directive = 0;
1325 parser->space_tokens = 1;
1326 parser->last_token_was_newline = 0;
1327 parser->last_token_was_space = 0;
1328 parser->first_non_space_token_this_line = 1;
1329 parser->newline_as_space = 0;
1330 parser->in_control_line = 0;
1331 parser->paren_count = 0;
1332 parser->commented_newlines = 0;
1333
1334 parser->skip_stack = NULL;
1335 parser->skipping = 0;
1336
1337 parser->lex_from_list = NULL;
1338 parser->lex_from_node = NULL;
1339
1340 parser->output = ralloc_strdup(parser, "");
1341 parser->output_length = 0;
1342 parser->info_log = ralloc_strdup(parser, "");
1343 parser->info_log_length = 0;
1344 parser->error = 0;
1345
1346 parser->extensions = extensions;
1347 parser->api = api;
1348 parser->version_resolved = false;
1349
1350 parser->has_new_line_number = 0;
1351 parser->new_line_number = 1;
1352 parser->has_new_source_number = 0;
1353 parser->new_source_number = 0;
1354
1355 return parser;
1356 }
1357
1358 void
1359 glcpp_parser_destroy(glcpp_parser_t *parser)
1360 {
1361 glcpp_lex_destroy (parser->scanner);
1362 hash_table_dtor (parser->defines);
1363 ralloc_free (parser);
1364 }
1365
1366 typedef enum function_status
1367 {
1368 FUNCTION_STATUS_SUCCESS,
1369 FUNCTION_NOT_A_FUNCTION,
1370 FUNCTION_UNBALANCED_PARENTHESES
1371 } function_status_t;
1372
1373 /* Find a set of function-like macro arguments by looking for a
1374 * balanced set of parentheses.
1375 *
1376 * When called, 'node' should be the opening-parenthesis token, (or
1377 * perhaps preceeding SPACE tokens). Upon successful return *last will
1378 * be the last consumed node, (corresponding to the closing right
1379 * parenthesis).
1380 *
1381 * Return values:
1382 *
1383 * FUNCTION_STATUS_SUCCESS:
1384 *
1385 * Successfully parsed a set of function arguments.
1386 *
1387 * FUNCTION_NOT_A_FUNCTION:
1388 *
1389 * Macro name not followed by a '('. This is not an error, but
1390 * simply that the macro name should be treated as a non-macro.
1391 *
1392 * FUNCTION_UNBALANCED_PARENTHESES
1393 *
1394 * Macro name is not followed by a balanced set of parentheses.
1395 */
1396 static function_status_t
1397 _arguments_parse(argument_list_t *arguments, token_node_t *node,
1398 token_node_t **last)
1399 {
1400 token_list_t *argument;
1401 int paren_count;
1402
1403 node = node->next;
1404
1405 /* Ignore whitespace before first parenthesis. */
1406 while (node && node->token->type == SPACE)
1407 node = node->next;
1408
1409 if (node == NULL || node->token->type != '(')
1410 return FUNCTION_NOT_A_FUNCTION;
1411
1412 node = node->next;
1413
1414 argument = _token_list_create (arguments);
1415 _argument_list_append (arguments, argument);
1416
1417 for (paren_count = 1; node; node = node->next) {
1418 if (node->token->type == '(') {
1419 paren_count++;
1420 } else if (node->token->type == ')') {
1421 paren_count--;
1422 if (paren_count == 0)
1423 break;
1424 }
1425
1426 if (node->token->type == ',' && paren_count == 1) {
1427 _token_list_trim_trailing_space (argument);
1428 argument = _token_list_create (arguments);
1429 _argument_list_append (arguments, argument);
1430 } else {
1431 if (argument->head == NULL) {
1432 /* Don't treat initial whitespace as part of the argument. */
1433 if (node->token->type == SPACE)
1434 continue;
1435 }
1436 _token_list_append (argument, node->token);
1437 }
1438 }
1439
1440 if (paren_count)
1441 return FUNCTION_UNBALANCED_PARENTHESES;
1442
1443 *last = node;
1444
1445 return FUNCTION_STATUS_SUCCESS;
1446 }
1447
1448 static token_list_t *
1449 _token_list_create_with_one_ival(void *ctx, int type, int ival)
1450 {
1451 token_list_t *list;
1452 token_t *node;
1453
1454 list = _token_list_create(ctx);
1455 node = _token_create_ival(list, type, ival);
1456 _token_list_append(list, node);
1457
1458 return list;
1459 }
1460
1461 static token_list_t *
1462 _token_list_create_with_one_space(void *ctx)
1463 {
1464 return _token_list_create_with_one_ival(ctx, SPACE, SPACE);
1465 }
1466
1467 static token_list_t *
1468 _token_list_create_with_one_integer(void *ctx, int ival)
1469 {
1470 return _token_list_create_with_one_ival(ctx, INTEGER, ival);
1471 }
1472
1473 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1474 *
1475 * Note: This function must only be called when "node" is a DEFINED token,
1476 * (and will abort with an assertion failure otherwise).
1477 *
1478 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1479 * (optionally preceded and followed by '(' and ')' tokens) then the following
1480 * occurs:
1481 *
1482 * If the identifier is a defined macro, this function returns 1.
1483 *
1484 * If the identifier is not a defined macro, this function returns 0.
1485 *
1486 * In either case, *last will be updated to the last node in the list
1487 * consumed by the evaluation, (either the token of the identifier or the
1488 * token of the closing parenthesis).
1489 *
1490 * In all other cases, (such as "node is the final node of the list", or
1491 * "missing closing parenthesis", etc.), this function generates a
1492 * preprocessor error, returns -1 and *last will not be set.
1493 */
1494 static int
1495 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1496 token_node_t **last)
1497 {
1498 token_node_t *argument, *defined = node;
1499
1500 assert(node->token->type == DEFINED);
1501
1502 node = node->next;
1503
1504 /* Ignore whitespace after DEFINED token. */
1505 while (node && node->token->type == SPACE)
1506 node = node->next;
1507
1508 if (node == NULL)
1509 goto FAIL;
1510
1511 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1512 argument = node;
1513 } else if (node->token->type == '(') {
1514 node = node->next;
1515
1516 /* Ignore whitespace after '(' token. */
1517 while (node && node->token->type == SPACE)
1518 node = node->next;
1519
1520 if (node == NULL || (node->token->type != IDENTIFIER &&
1521 node->token->type != OTHER)) {
1522 goto FAIL;
1523 }
1524
1525 argument = node;
1526
1527 node = node->next;
1528
1529 /* Ignore whitespace after identifier, before ')' token. */
1530 while (node && node->token->type == SPACE)
1531 node = node->next;
1532
1533 if (node == NULL || node->token->type != ')')
1534 goto FAIL;
1535 } else {
1536 goto FAIL;
1537 }
1538
1539 *last = node;
1540
1541 return hash_table_find(parser->defines,
1542 argument->token->value.str) ? 1 : 0;
1543
1544 FAIL:
1545 glcpp_error (&defined->token->location, parser,
1546 "\"defined\" not followed by an identifier");
1547 return -1;
1548 }
1549
1550 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1551 */
1552 static void
1553 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1554 token_list_t *list)
1555 {
1556 token_node_t *node, *node_prev, *replacement, *last = NULL;
1557 int value;
1558
1559 if (list == NULL)
1560 return;
1561
1562 node_prev = NULL;
1563 node = list->head;
1564
1565 while (node) {
1566
1567 if (node->token->type != DEFINED)
1568 goto NEXT;
1569
1570 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1571 if (value == -1)
1572 goto NEXT;
1573
1574 replacement = ralloc (list, token_node_t);
1575 replacement->token = _token_create_ival (list, INTEGER, value);
1576
1577 /* Splice replacement node into list, replacing from "node"
1578 * through "last". */
1579 if (node_prev)
1580 node_prev->next = replacement;
1581 else
1582 list->head = replacement;
1583 replacement->next = last->next;
1584 if (last == list->tail)
1585 list->tail = replacement;
1586
1587 node = replacement;
1588
1589 NEXT:
1590 node_prev = node;
1591 node = node->next;
1592 }
1593 }
1594
1595 /* Perform macro expansion on 'list', placing the resulting tokens
1596 * into a new list which is initialized with a first token of type
1597 * 'head_token_type'. Then begin lexing from the resulting list,
1598 * (return to the current lexing source when this list is exhausted).
1599 *
1600 * See the documentation of _glcpp_parser_expand_token_list for a description
1601 * of the "mode" parameter.
1602 */
1603 static void
1604 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1605 token_list_t *list, expansion_mode_t mode)
1606 {
1607 token_list_t *expanded;
1608 token_t *token;
1609
1610 expanded = _token_list_create (parser);
1611 token = _token_create_ival (parser, head_token_type, head_token_type);
1612 _token_list_append (expanded, token);
1613 _glcpp_parser_expand_token_list (parser, list, mode);
1614 _token_list_append_list (expanded, list);
1615 glcpp_parser_lex_from (parser, expanded);
1616 }
1617
1618 static void
1619 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1620 {
1621 token_node_t *node;
1622
1623 node = list->head;
1624 while (node) {
1625 token_node_t *next_non_space;
1626
1627 /* Look ahead for a PASTE token, skipping space. */
1628 next_non_space = node->next;
1629 while (next_non_space && next_non_space->token->type == SPACE)
1630 next_non_space = next_non_space->next;
1631
1632 if (next_non_space == NULL)
1633 break;
1634
1635 if (next_non_space->token->type != PASTE) {
1636 node = next_non_space;
1637 continue;
1638 }
1639
1640 /* Now find the next non-space token after the PASTE. */
1641 next_non_space = next_non_space->next;
1642 while (next_non_space && next_non_space->token->type == SPACE)
1643 next_non_space = next_non_space->next;
1644
1645 if (next_non_space == NULL) {
1646 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1647 return;
1648 }
1649
1650 node->token = _token_paste(parser, node->token, next_non_space->token);
1651 node->next = next_non_space->next;
1652 if (next_non_space == list->tail)
1653 list->tail = node;
1654 }
1655
1656 list->non_space_tail = list->tail;
1657 }
1658
1659 /* This is a helper function that's essentially part of the
1660 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1661 * except for by that function.
1662 *
1663 * Returns NULL if node is a simple token with no expansion, (that is,
1664 * although 'node' corresponds to an identifier defined as a
1665 * function-like macro, it is not followed with a parenthesized
1666 * argument list).
1667 *
1668 * Compute the complete expansion of node (which is a function-like
1669 * macro) and subsequent nodes which are arguments.
1670 *
1671 * Returns the token list that results from the expansion and sets
1672 * *last to the last node in the list that was consumed by the
1673 * expansion. Specifically, *last will be set as follows: as the
1674 * token of the closing right parenthesis.
1675 *
1676 * See the documentation of _glcpp_parser_expand_token_list for a description
1677 * of the "mode" parameter.
1678 */
1679 static token_list_t *
1680 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1681 token_node_t **last, expansion_mode_t mode)
1682 {
1683 macro_t *macro;
1684 const char *identifier;
1685 argument_list_t *arguments;
1686 function_status_t status;
1687 token_list_t *substituted;
1688 int parameter_index;
1689
1690 identifier = node->token->value.str;
1691
1692 macro = hash_table_find(parser->defines, identifier);
1693
1694 assert(macro->is_function);
1695
1696 arguments = _argument_list_create(parser);
1697 status = _arguments_parse(arguments, node, last);
1698
1699 switch (status) {
1700 case FUNCTION_STATUS_SUCCESS:
1701 break;
1702 case FUNCTION_NOT_A_FUNCTION:
1703 return NULL;
1704 case FUNCTION_UNBALANCED_PARENTHESES:
1705 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1706 return NULL;
1707 }
1708
1709 /* Replace a macro defined as empty with a SPACE token. */
1710 if (macro->replacements == NULL) {
1711 ralloc_free(arguments);
1712 return _token_list_create_with_one_space(parser);
1713 }
1714
1715 if (!((_argument_list_length (arguments) ==
1716 _string_list_length (macro->parameters)) ||
1717 (_string_list_length (macro->parameters) == 0 &&
1718 _argument_list_length (arguments) == 1 &&
1719 arguments->head->argument->head == NULL))) {
1720 glcpp_error(&node->token->location, parser,
1721 "Error: macro %s invoked with %d arguments (expected %d)\n",
1722 identifier, _argument_list_length (arguments),
1723 _string_list_length(macro->parameters));
1724 return NULL;
1725 }
1726
1727 /* Perform argument substitution on the replacement list. */
1728 substituted = _token_list_create(arguments);
1729
1730 for (node = macro->replacements->head; node; node = node->next) {
1731 if (node->token->type == IDENTIFIER &&
1732 _string_list_contains(macro->parameters, node->token->value.str,
1733 &parameter_index)) {
1734 token_list_t *argument;
1735 argument = _argument_list_member_at(arguments, parameter_index);
1736 /* Before substituting, we expand the argument tokens, or append a
1737 * placeholder token for an empty argument. */
1738 if (argument->head) {
1739 token_list_t *expanded_argument;
1740 expanded_argument = _token_list_copy(parser, argument);
1741 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1742 _token_list_append_list(substituted, expanded_argument);
1743 } else {
1744 token_t *new_token;
1745
1746 new_token = _token_create_ival(substituted, PLACEHOLDER,
1747 PLACEHOLDER);
1748 _token_list_append(substituted, new_token);
1749 }
1750 } else {
1751 _token_list_append(substituted, node->token);
1752 }
1753 }
1754
1755 /* After argument substitution, and before further expansion
1756 * below, implement token pasting. */
1757
1758 _token_list_trim_trailing_space(substituted);
1759
1760 _glcpp_parser_apply_pastes(parser, substituted);
1761
1762 return substituted;
1763 }
1764
1765 /* Compute the complete expansion of node, (and subsequent nodes after
1766 * 'node' in the case that 'node' is a function-like macro and
1767 * subsequent nodes are arguments).
1768 *
1769 * Returns NULL if node is a simple token with no expansion.
1770 *
1771 * Otherwise, returns the token list that results from the expansion
1772 * and sets *last to the last node in the list that was consumed by
1773 * the expansion. Specifically, *last will be set as follows:
1774 *
1775 * As 'node' in the case of object-like macro expansion.
1776 *
1777 * As the token of the closing right parenthesis in the case of
1778 * function-like macro expansion.
1779 *
1780 * See the documentation of _glcpp_parser_expand_token_list for a description
1781 * of the "mode" parameter.
1782 */
1783 static token_list_t *
1784 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1785 token_node_t **last, expansion_mode_t mode)
1786 {
1787 token_t *token = node->token;
1788 const char *identifier;
1789 macro_t *macro;
1790
1791 /* We only expand identifiers */
1792 if (token->type != IDENTIFIER) {
1793 return NULL;
1794 }
1795
1796 *last = node;
1797 identifier = token->value.str;
1798
1799 /* Special handling for __LINE__ and __FILE__, (not through
1800 * the hash table). */
1801 if (strcmp(identifier, "__LINE__") == 0)
1802 return _token_list_create_with_one_integer(parser, node->token->location.first_line);
1803
1804 if (strcmp(identifier, "__FILE__") == 0)
1805 return _token_list_create_with_one_integer(parser, node->token->location.source);
1806
1807 /* Look up this identifier in the hash table. */
1808 macro = hash_table_find(parser->defines, identifier);
1809
1810 /* Not a macro, so no expansion needed. */
1811 if (macro == NULL)
1812 return NULL;
1813
1814 /* Finally, don't expand this macro if we're already actively
1815 * expanding it, (to avoid infinite recursion). */
1816 if (_parser_active_list_contains (parser, identifier)) {
1817 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1818 * future expansion of this unexpanded token. */
1819 char *str;
1820 token_list_t *expansion;
1821 token_t *final;
1822
1823 str = ralloc_strdup(parser, token->value.str);
1824 final = _token_create_str(parser, OTHER, str);
1825 expansion = _token_list_create(parser);
1826 _token_list_append(expansion, final);
1827 return expansion;
1828 }
1829
1830 if (! macro->is_function) {
1831 token_list_t *replacement;
1832
1833 /* Replace a macro defined as empty with a SPACE token. */
1834 if (macro->replacements == NULL)
1835 return _token_list_create_with_one_space(parser);
1836
1837 replacement = _token_list_copy(parser, macro->replacements);
1838 _glcpp_parser_apply_pastes(parser, replacement);
1839 return replacement;
1840 }
1841
1842 return _glcpp_parser_expand_function(parser, node, last, mode);
1843 }
1844
1845 /* Push a new identifier onto the parser's active list.
1846 *
1847 * Here, 'marker' is the token node that appears in the list after the
1848 * expansion of 'identifier'. That is, when the list iterator begins
1849 * examining 'marker', then it is time to pop this node from the
1850 * active stack.
1851 */
1852 static void
1853 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
1854 token_node_t *marker)
1855 {
1856 active_list_t *node;
1857
1858 node = ralloc(parser->active, active_list_t);
1859 node->identifier = ralloc_strdup(node, identifier);
1860 node->marker = marker;
1861 node->next = parser->active;
1862
1863 parser->active = node;
1864 }
1865
1866 static void
1867 _parser_active_list_pop(glcpp_parser_t *parser)
1868 {
1869 active_list_t *node = parser->active;
1870
1871 if (node == NULL) {
1872 parser->active = NULL;
1873 return;
1874 }
1875
1876 node = parser->active->next;
1877 ralloc_free (parser->active);
1878
1879 parser->active = node;
1880 }
1881
1882 static int
1883 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
1884 {
1885 active_list_t *node;
1886
1887 if (parser->active == NULL)
1888 return 0;
1889
1890 for (node = parser->active; node; node = node->next)
1891 if (strcmp(node->identifier, identifier) == 0)
1892 return 1;
1893
1894 return 0;
1895 }
1896
1897 /* Walk over the token list replacing nodes with their expansion.
1898 * Whenever nodes are expanded the walking will walk over the new
1899 * nodes, continuing to expand as necessary. The results are placed in
1900 * 'list' itself.
1901 *
1902 * The "mode" argument controls the handling of any DEFINED tokens that
1903 * result from expansion as follows:
1904 *
1905 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
1906 * left in the final list, unevaluated. This is the correct mode
1907 * for expanding any list in any context other than a
1908 * preprocessor conditional, (#if or #elif).
1909 *
1910 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
1911 * evaluated to 0 or 1 tokens depending on whether the following
1912 * token is the name of a defined macro. If the DEFINED token is
1913 * not followed by an (optionally parenthesized) identifier, then
1914 * an error will be generated. This the correct mode for
1915 * expanding any list in the context of a preprocessor
1916 * conditional, (#if or #elif).
1917 */
1918 static void
1919 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
1920 expansion_mode_t mode)
1921 {
1922 token_node_t *node_prev;
1923 token_node_t *node, *last = NULL;
1924 token_list_t *expansion;
1925 active_list_t *active_initial = parser->active;
1926
1927 if (list == NULL)
1928 return;
1929
1930 _token_list_trim_trailing_space (list);
1931
1932 node_prev = NULL;
1933 node = list->head;
1934
1935 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
1936 _glcpp_parser_evaluate_defined_in_list (parser, list);
1937
1938 while (node) {
1939
1940 while (parser->active && parser->active->marker == node)
1941 _parser_active_list_pop (parser);
1942
1943 expansion = _glcpp_parser_expand_node (parser, node, &last, mode);
1944 if (expansion) {
1945 token_node_t *n;
1946
1947 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
1948 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
1949 }
1950
1951 for (n = node; n != last->next; n = n->next)
1952 while (parser->active && parser->active->marker == n) {
1953 _parser_active_list_pop (parser);
1954 }
1955
1956 _parser_active_list_push(parser, node->token->value.str, last->next);
1957
1958 /* Splice expansion into list, supporting a simple deletion if the
1959 * expansion is empty.
1960 */
1961 if (expansion->head) {
1962 if (node_prev)
1963 node_prev->next = expansion->head;
1964 else
1965 list->head = expansion->head;
1966 expansion->tail->next = last->next;
1967 if (last == list->tail)
1968 list->tail = expansion->tail;
1969 } else {
1970 if (node_prev)
1971 node_prev->next = last->next;
1972 else
1973 list->head = last->next;
1974 if (last == list->tail)
1975 list->tail = NULL;
1976 }
1977 } else {
1978 node_prev = node;
1979 }
1980 node = node_prev ? node_prev->next : list->head;
1981 }
1982
1983 /* Remove any lingering effects of this invocation on the
1984 * active list. That is, pop until the list looks like it did
1985 * at the beginning of this function. */
1986 while (parser->active && parser->active != active_initial)
1987 _parser_active_list_pop (parser);
1988
1989 list->non_space_tail = list->tail;
1990 }
1991
1992 void
1993 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
1994 token_list_t *list)
1995 {
1996 if (list == NULL)
1997 return;
1998
1999 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2000
2001 _token_list_trim_trailing_space (list);
2002
2003 _token_list_print (parser, list);
2004 }
2005
2006 static void
2007 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2008 const char *identifier)
2009 {
2010 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2011 * the GLSL ES spec (all versions) say:
2012 *
2013 * "All macro names containing two consecutive underscores ( __ )
2014 * are reserved for future use as predefined macro names. All
2015 * macro names prefixed with "GL_" ("GL" followed by a single
2016 * underscore) are also reserved."
2017 *
2018 * The intention is that names containing __ are reserved for internal
2019 * use by the implementation, and names prefixed with GL_ are reserved
2020 * for use by Khronos. Since every extension adds a name prefixed
2021 * with GL_ (i.e., the name of the extension), that should be an
2022 * error. Names simply containing __ are dangerous to use, but should
2023 * be allowed.
2024 *
2025 * A future version of the GLSL specification will clarify this.
2026 */
2027 if (strstr(identifier, "__")) {
2028 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2029 "for use by the implementation.\n");
2030 }
2031 if (strncmp(identifier, "GL_", 3) == 0) {
2032 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2033 }
2034 if (strcmp(identifier, "defined") == 0) {
2035 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2036 }
2037 }
2038
2039 static int
2040 _macro_equal(macro_t *a, macro_t *b)
2041 {
2042 if (a->is_function != b->is_function)
2043 return 0;
2044
2045 if (a->is_function) {
2046 if (! _string_list_equal (a->parameters, b->parameters))
2047 return 0;
2048 }
2049
2050 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2051 }
2052
2053 void
2054 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2055 const char *identifier, token_list_t *replacements)
2056 {
2057 macro_t *macro, *previous;
2058
2059 /* We define pre-defined macros before we've started parsing the actual
2060 * file. So if there's no location defined yet, that's what were doing and
2061 * we don't want to generate an error for using the reserved names. */
2062 if (loc != NULL)
2063 _check_for_reserved_macro_name(parser, loc, identifier);
2064
2065 macro = ralloc (parser, macro_t);
2066
2067 macro->is_function = 0;
2068 macro->parameters = NULL;
2069 macro->identifier = ralloc_strdup (macro, identifier);
2070 macro->replacements = replacements;
2071 ralloc_steal (macro, replacements);
2072
2073 previous = hash_table_find (parser->defines, identifier);
2074 if (previous) {
2075 if (_macro_equal (macro, previous)) {
2076 ralloc_free (macro);
2077 return;
2078 }
2079 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2080 }
2081
2082 hash_table_insert (parser->defines, macro, identifier);
2083 }
2084
2085 void
2086 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2087 const char *identifier, string_list_t *parameters,
2088 token_list_t *replacements)
2089 {
2090 macro_t *macro, *previous;
2091 const char *dup;
2092
2093 _check_for_reserved_macro_name(parser, loc, identifier);
2094
2095 /* Check for any duplicate parameter names. */
2096 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2097 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2098 }
2099
2100 macro = ralloc (parser, macro_t);
2101 ralloc_steal (macro, parameters);
2102 ralloc_steal (macro, replacements);
2103
2104 macro->is_function = 1;
2105 macro->parameters = parameters;
2106 macro->identifier = ralloc_strdup (macro, identifier);
2107 macro->replacements = replacements;
2108 previous = hash_table_find (parser->defines, identifier);
2109 if (previous) {
2110 if (_macro_equal (macro, previous)) {
2111 ralloc_free (macro);
2112 return;
2113 }
2114 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2115 }
2116
2117 hash_table_insert(parser->defines, macro, identifier);
2118 }
2119
2120 static int
2121 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2122 {
2123 token_node_t *node;
2124 int ret;
2125
2126 if (parser->lex_from_list == NULL) {
2127 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2128
2129 /* XXX: This ugly block of code exists for the sole
2130 * purpose of converting a NEWLINE token into a SPACE
2131 * token, but only in the case where we have seen a
2132 * function-like macro name, but have not yet seen its
2133 * closing parenthesis.
2134 *
2135 * There's perhaps a more compact way to do this with
2136 * mid-rule actions in the grammar.
2137 *
2138 * I'm definitely not pleased with the complexity of
2139 * this code here.
2140 */
2141 if (parser->newline_as_space) {
2142 if (ret == '(') {
2143 parser->paren_count++;
2144 } else if (ret == ')') {
2145 parser->paren_count--;
2146 if (parser->paren_count == 0)
2147 parser->newline_as_space = 0;
2148 } else if (ret == NEWLINE) {
2149 ret = SPACE;
2150 } else if (ret != SPACE) {
2151 if (parser->paren_count == 0)
2152 parser->newline_as_space = 0;
2153 }
2154 } else if (parser->in_control_line) {
2155 if (ret == NEWLINE)
2156 parser->in_control_line = 0;
2157 }
2158 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2159 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2160 ret == ENDIF || ret == HASH_TOKEN) {
2161 parser->in_control_line = 1;
2162 } else if (ret == IDENTIFIER) {
2163 macro_t *macro;
2164 macro = hash_table_find (parser->defines,
2165 yylval->str);
2166 if (macro && macro->is_function) {
2167 parser->newline_as_space = 1;
2168 parser->paren_count = 0;
2169 }
2170 }
2171
2172 return ret;
2173 }
2174
2175 node = parser->lex_from_node;
2176
2177 if (node == NULL) {
2178 ralloc_free (parser->lex_from_list);
2179 parser->lex_from_list = NULL;
2180 return NEWLINE;
2181 }
2182
2183 *yylval = node->token->value;
2184 ret = node->token->type;
2185
2186 parser->lex_from_node = node->next;
2187
2188 return ret;
2189 }
2190
2191 static void
2192 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2193 {
2194 token_node_t *node;
2195
2196 assert (parser->lex_from_list == NULL);
2197
2198 /* Copy list, eliminating any space tokens. */
2199 parser->lex_from_list = _token_list_create (parser);
2200
2201 for (node = list->head; node; node = node->next) {
2202 if (node->token->type == SPACE)
2203 continue;
2204 _token_list_append (parser->lex_from_list, node->token);
2205 }
2206
2207 ralloc_free (list);
2208
2209 parser->lex_from_node = parser->lex_from_list->head;
2210
2211 /* It's possible the list consisted of nothing but whitespace. */
2212 if (parser->lex_from_node == NULL) {
2213 ralloc_free (parser->lex_from_list);
2214 parser->lex_from_list = NULL;
2215 }
2216 }
2217
2218 static void
2219 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2220 int condition)
2221 {
2222 skip_type_t current = SKIP_NO_SKIP;
2223 skip_node_t *node;
2224
2225 if (parser->skip_stack)
2226 current = parser->skip_stack->type;
2227
2228 node = ralloc (parser, skip_node_t);
2229 node->loc = *loc;
2230
2231 if (current == SKIP_NO_SKIP) {
2232 if (condition)
2233 node->type = SKIP_NO_SKIP;
2234 else
2235 node->type = SKIP_TO_ELSE;
2236 } else {
2237 node->type = SKIP_TO_ENDIF;
2238 }
2239
2240 node->has_else = false;
2241 node->next = parser->skip_stack;
2242 parser->skip_stack = node;
2243 }
2244
2245 static void
2246 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2247 const char *type, int condition)
2248 {
2249 if (parser->skip_stack == NULL) {
2250 glcpp_error (loc, parser, "#%s without #if\n", type);
2251 return;
2252 }
2253
2254 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2255 if (condition)
2256 parser->skip_stack->type = SKIP_NO_SKIP;
2257 } else {
2258 parser->skip_stack->type = SKIP_TO_ENDIF;
2259 }
2260 }
2261
2262 static void
2263 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2264 {
2265 skip_node_t *node;
2266
2267 if (parser->skip_stack == NULL) {
2268 glcpp_error (loc, parser, "#endif without #if\n");
2269 return;
2270 }
2271
2272 node = parser->skip_stack;
2273 parser->skip_stack = node->next;
2274 ralloc_free (node);
2275 }
2276
2277 static void
2278 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2279 const char *es_identifier,
2280 bool explicitly_set)
2281 {
2282 const struct gl_extensions *extensions = parser->extensions;
2283
2284 if (parser->version_resolved)
2285 return;
2286
2287 parser->version_resolved = true;
2288
2289 add_builtin_define (parser, "__VERSION__", version);
2290
2291 parser->is_gles = (version == 100) ||
2292 (es_identifier && (strcmp(es_identifier, "es") == 0));
2293
2294 /* Add pre-defined macros. */
2295 if (parser->is_gles) {
2296 add_builtin_define(parser, "GL_ES", 1);
2297 add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
2298 add_builtin_define(parser, "GL_EXT_draw_buffers", 1);
2299
2300 if (extensions != NULL) {
2301 if (extensions->OES_EGL_image_external)
2302 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2303 if (extensions->OES_sample_variables) {
2304 add_builtin_define(parser, "GL_OES_sample_variables", 1);
2305 add_builtin_define(parser, "GL_OES_shader_multisample_interpolation", 1);
2306 }
2307 if (extensions->OES_standard_derivatives)
2308 add_builtin_define(parser, "GL_OES_standard_derivatives", 1);
2309 if (extensions->ARB_texture_multisample)
2310 add_builtin_define(parser, "GL_OES_texture_storage_multisample_2d_array", 1);
2311 if (extensions->ARB_blend_func_extended)
2312 add_builtin_define(parser, "GL_EXT_blend_func_extended", 1);
2313
2314 if (version >= 310) {
2315 if (extensions->ARB_shader_image_load_store)
2316 add_builtin_define(parser, "GL_OES_shader_image_atomic", 1);
2317
2318 if (extensions->OES_geometry_shader) {
2319 add_builtin_define(parser, "GL_OES_geometry_point_size", 1);
2320 add_builtin_define(parser, "GL_OES_geometry_shader", 1);
2321 }
2322 if (extensions->ARB_gpu_shader5) {
2323 add_builtin_define(parser, "GL_EXT_gpu_shader5", 1);
2324 add_builtin_define(parser, "GL_OES_gpu_shader5", 1);
2325 }
2326 if (extensions->OES_texture_buffer) {
2327 add_builtin_define(parser, "GL_EXT_texture_buffer", 1);
2328 add_builtin_define(parser, "GL_OES_texture_buffer", 1);
2329 }
2330 }
2331 }
2332 } else {
2333 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2334 add_builtin_define(parser, "GL_ARB_enhanced_layouts", 1);
2335 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2336 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2337 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2338
2339 if (extensions != NULL) {
2340 if (extensions->EXT_texture_array)
2341 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2342
2343 if (extensions->ARB_ES3_1_compatibility)
2344 add_builtin_define(parser, "GL_ARB_ES3_1_compatibility", 1);
2345
2346 if (extensions->ARB_arrays_of_arrays)
2347 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2348
2349 if (extensions->ARB_fragment_coord_conventions) {
2350 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2351 1);
2352 }
2353
2354 if (extensions->ARB_fragment_layer_viewport)
2355 add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
2356
2357 if (extensions->ARB_explicit_attrib_location)
2358 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2359
2360 if (extensions->ARB_explicit_uniform_location)
2361 add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
2362
2363 if (extensions->ARB_shader_texture_lod)
2364 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2365
2366 if (extensions->ARB_draw_instanced)
2367 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2368
2369 if (extensions->ARB_conservative_depth) {
2370 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2371 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2372 }
2373
2374 if (extensions->ARB_shader_bit_encoding)
2375 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2376
2377 if (extensions->ARB_shader_clock)
2378 add_builtin_define(parser, "GL_ARB_shader_clock", 1);
2379
2380 if (extensions->ARB_uniform_buffer_object)
2381 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2382
2383 if (extensions->ARB_texture_cube_map_array)
2384 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2385
2386 if (extensions->ARB_shading_language_packing)
2387 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2388
2389 if (extensions->ARB_texture_multisample)
2390 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2391
2392 if (extensions->ARB_texture_query_levels)
2393 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2394
2395 if (extensions->ARB_texture_query_lod)
2396 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2397
2398 if (extensions->ARB_gpu_shader5)
2399 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2400
2401 if (extensions->ARB_gpu_shader_fp64)
2402 add_builtin_define(parser, "GL_ARB_gpu_shader_fp64", 1);
2403
2404 if (extensions->ARB_vertex_attrib_64bit)
2405 add_builtin_define(parser, "GL_ARB_vertex_attrib_64bit", 1);
2406
2407 if (extensions->AMD_vertex_shader_layer)
2408 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2409
2410 if (extensions->AMD_vertex_shader_viewport_index)
2411 add_builtin_define(parser, "GL_AMD_vertex_shader_viewport_index", 1);
2412
2413 if (extensions->ARB_shading_language_420pack)
2414 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2415
2416 if (extensions->ARB_sample_shading)
2417 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2418
2419 if (extensions->ARB_texture_gather)
2420 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2421
2422 if (extensions->ARB_shader_atomic_counters)
2423 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2424
2425 if (extensions->ARB_shader_atomic_counter_ops)
2426 add_builtin_define(parser, "GL_ARB_shader_atomic_counter_ops", 1);
2427
2428 if (extensions->ARB_viewport_array)
2429 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2430
2431 if (extensions->ARB_compute_shader)
2432 add_builtin_define(parser, "GL_ARB_compute_shader", 1);
2433
2434 if (extensions->ARB_shader_image_load_store)
2435 add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
2436
2437 if (extensions->ARB_shader_image_size)
2438 add_builtin_define(parser, "GL_ARB_shader_image_size", 1);
2439
2440 if (extensions->ARB_shader_texture_image_samples)
2441 add_builtin_define(parser, "GL_ARB_shader_texture_image_samples", 1);
2442
2443 if (extensions->ARB_derivative_control)
2444 add_builtin_define(parser, "GL_ARB_derivative_control", 1);
2445
2446 if (extensions->ARB_shader_precision)
2447 add_builtin_define(parser, "GL_ARB_shader_precision", 1);
2448
2449 if (extensions->ARB_shader_storage_buffer_object)
2450 add_builtin_define(parser, "GL_ARB_shader_storage_buffer_object", 1);
2451
2452 if (extensions->ARB_tessellation_shader)
2453 add_builtin_define(parser, "GL_ARB_tessellation_shader", 1);
2454
2455 if (extensions->ARB_shader_subroutine)
2456 add_builtin_define(parser, "GL_ARB_shader_subroutine", 1);
2457
2458 if (extensions->ARB_shader_draw_parameters)
2459 add_builtin_define(parser, "GL_ARB_shader_draw_parameters", 1);
2460
2461 if (extensions->ARB_cull_distance)
2462 add_builtin_define(parser, "GL_ARB_cull_distance", 1);
2463 }
2464 }
2465
2466 if (extensions != NULL) {
2467 if (extensions->EXT_shader_integer_mix)
2468 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2469
2470 if (extensions->EXT_shader_samples_identical)
2471 add_builtin_define(parser, "GL_EXT_shader_samples_identical", 1);
2472 }
2473
2474 if (version >= 150)
2475 add_builtin_define(parser, "GL_core_profile", 1);
2476
2477 /* Currently, all ES2/ES3 implementations support highp in the
2478 * fragment shader, so we always define this macro in ES2/ES3.
2479 * If we ever get a driver that doesn't support highp, we'll
2480 * need to add a flag to the gl_context and check that here.
2481 */
2482 if (version >= 130 || parser->is_gles)
2483 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2484
2485 if (explicitly_set) {
2486 ralloc_asprintf_rewrite_tail(&parser->output, &parser->output_length,
2487 "#version %" PRIiMAX "%s%s", version,
2488 es_identifier ? " " : "",
2489 es_identifier ? es_identifier : "");
2490 }
2491 }
2492
2493 /* GLSL version if no version is explicitly specified. */
2494 #define IMPLICIT_GLSL_VERSION 110
2495
2496 /* GLSL ES version if no version is explicitly specified. */
2497 #define IMPLICIT_GLSL_ES_VERSION 100
2498
2499 void
2500 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2501 {
2502 int language_version = parser->api == API_OPENGLES2 ?
2503 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2504
2505 _glcpp_parser_handle_version_declaration(parser, language_version,
2506 NULL, false);
2507 }