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