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