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