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