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