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