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