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