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