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