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