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