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