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