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