f28d8531eb1de8b2ca5a022afecd12a64852e252
[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 static void
89 _token_list_append (token_list_t *list, token_t *token);
90
91 static void
92 _token_list_append_list (token_list_t *list, token_list_t *tail);
93
94 static int
95 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
96
97 static void
98 _parser_active_list_push (glcpp_parser_t *parser,
99 const char *identifier,
100 token_node_t *marker);
101
102 static void
103 _parser_active_list_pop (glcpp_parser_t *parser);
104
105 static int
106 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier);
107
108 /* Expand list, and begin lexing from the result (after first
109 * prefixing a token of type 'head_token_type').
110 */
111 static void
112 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
113 int head_token_type,
114 token_list_t *list);
115
116 /* Perform macro expansion in-place on the given list. */
117 static void
118 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
119 token_list_t *list);
120
121 static void
122 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
123 token_list_t *list);
124
125 static void
126 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
127 int condition);
128
129 static void
130 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
131 const char *type, int condition);
132
133 static void
134 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
135
136 static void
137 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
138 const char *ident, bool explicitly_set);
139
140 static int
141 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
142
143 static void
144 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
145
146 static void
147 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
148
149 %}
150
151 %pure-parser
152 %error-verbose
153
154 %locations
155 %initial-action {
156 @$.first_line = 1;
157 @$.first_column = 1;
158 @$.last_line = 1;
159 @$.last_column = 1;
160 @$.source = 0;
161 }
162
163 %parse-param {glcpp_parser_t *parser}
164 %lex-param {glcpp_parser_t *parser}
165
166 %expect 0
167 %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE FUNC_IDENTIFIER OBJ_IDENTIFIER HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_LINE HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE
168 %token PASTE
169 %type <ival> expression INTEGER operator SPACE integer_constant
170 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
171 %type <string_list> identifier_list
172 %type <token> preprocessing_token conditional_token
173 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
174 %left OR
175 %left AND
176 %left '|'
177 %left '^'
178 %left '&'
179 %left EQUAL NOT_EQUAL
180 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
181 %left LEFT_SHIFT RIGHT_SHIFT
182 %left '+' '-'
183 %left '*' '/' '%'
184 %right UNARY
185
186 %%
187
188 input:
189 /* empty */
190 | input line
191 ;
192
193 line:
194 control_line {
195 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
196 }
197 | HASH_LINE {
198 glcpp_parser_resolve_implicit_version(parser);
199 } pp_tokens NEWLINE {
200
201 if (parser->skip_stack == NULL ||
202 parser->skip_stack->type == SKIP_NO_SKIP)
203 {
204 _glcpp_parser_expand_and_lex_from (parser,
205 LINE_EXPANDED, $3);
206 }
207 }
208 | text_line {
209 _glcpp_parser_print_expanded_token_list (parser, $1);
210 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
211 ralloc_free ($1);
212 }
213 | expanded_line
214 | HASH non_directive
215 ;
216
217 expanded_line:
218 IF_EXPANDED expression NEWLINE {
219 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
220 }
221 | ELIF_EXPANDED expression NEWLINE {
222 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
223 }
224 | LINE_EXPANDED integer_constant NEWLINE {
225 parser->has_new_line_number = 1;
226 parser->new_line_number = $2;
227 ralloc_asprintf_rewrite_tail (&parser->output,
228 &parser->output_length,
229 "#line %" PRIiMAX "\n",
230 $2);
231 }
232 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
233 parser->has_new_line_number = 1;
234 parser->new_line_number = $2;
235 parser->has_new_source_number = 1;
236 parser->new_source_number = $3;
237 ralloc_asprintf_rewrite_tail (&parser->output,
238 &parser->output_length,
239 "#line %" PRIiMAX " %" PRIiMAX "\n",
240 $2, $3);
241 }
242 ;
243
244 define:
245 OBJ_IDENTIFIER replacement_list NEWLINE {
246 _define_object_macro (parser, & @1, $1, $2);
247 }
248 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
249 _define_function_macro (parser, & @1, $1, NULL, $4);
250 }
251 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
252 _define_function_macro (parser, & @1, $1, $3, $5);
253 }
254 ;
255
256 control_line:
257 HASH_DEFINE {
258 glcpp_parser_resolve_implicit_version(parser);
259 } define
260 | HASH_UNDEF {
261 glcpp_parser_resolve_implicit_version(parser);
262 } IDENTIFIER NEWLINE {
263 macro_t *macro = hash_table_find (parser->defines, $3);
264 if (macro) {
265 hash_table_remove (parser->defines, $3);
266 ralloc_free (macro);
267 }
268 ralloc_free ($3);
269 }
270 | HASH_IF {
271 glcpp_parser_resolve_implicit_version(parser);
272 } conditional_tokens NEWLINE {
273 /* Be careful to only evaluate the 'if' expression if
274 * we are not skipping. When we are skipping, we
275 * simply push a new 0-valued 'if' onto the skip
276 * stack.
277 *
278 * This avoids generating diagnostics for invalid
279 * expressions that are being skipped. */
280 if (parser->skip_stack == NULL ||
281 parser->skip_stack->type == SKIP_NO_SKIP)
282 {
283 _glcpp_parser_expand_and_lex_from (parser,
284 IF_EXPANDED, $3);
285 }
286 else
287 {
288 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
289 parser->skip_stack->type = SKIP_TO_ENDIF;
290 }
291 }
292 | HASH_IF NEWLINE {
293 /* #if without an expression is only an error if we
294 * are not skipping */
295 if (parser->skip_stack == NULL ||
296 parser->skip_stack->type == SKIP_NO_SKIP)
297 {
298 glcpp_error(& @1, parser, "#if with no expression");
299 }
300 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
301 }
302 | HASH_IFDEF {
303 glcpp_parser_resolve_implicit_version(parser);
304 } IDENTIFIER junk NEWLINE {
305 macro_t *macro = hash_table_find (parser->defines, $3);
306 ralloc_free ($3);
307 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
308 }
309 | HASH_IFNDEF {
310 glcpp_parser_resolve_implicit_version(parser);
311 } IDENTIFIER junk NEWLINE {
312 macro_t *macro = hash_table_find (parser->defines, $3);
313 ralloc_free ($3);
314 _glcpp_parser_skip_stack_push_if (parser, & @2, macro == NULL);
315 }
316 | HASH_ELIF conditional_tokens NEWLINE {
317 /* Be careful to only evaluate the 'elif' expression
318 * if we are not skipping. When we are skipping, we
319 * simply change to a 0-valued 'elif' on the skip
320 * stack.
321 *
322 * This avoids generating diagnostics for invalid
323 * expressions that are being skipped. */
324 if (parser->skip_stack &&
325 parser->skip_stack->type == SKIP_TO_ELSE)
326 {
327 _glcpp_parser_expand_and_lex_from (parser,
328 ELIF_EXPANDED, $2);
329 }
330 else if (parser->skip_stack &&
331 parser->skip_stack->has_else)
332 {
333 glcpp_error(& @1, parser, "#elif after #else");
334 }
335 else
336 {
337 _glcpp_parser_skip_stack_change_if (parser, & @1,
338 "elif", 0);
339 }
340 }
341 | HASH_ELIF NEWLINE {
342 /* #elif without an expression is an error unless we
343 * are skipping. */
344 if (parser->skip_stack &&
345 parser->skip_stack->type == SKIP_TO_ELSE)
346 {
347 glcpp_error(& @1, parser, "#elif with no expression");
348 }
349 else if (parser->skip_stack &&
350 parser->skip_stack->has_else)
351 {
352 glcpp_error(& @1, parser, "#elif after #else");
353 }
354 else
355 {
356 _glcpp_parser_skip_stack_change_if (parser, & @1,
357 "elif", 0);
358 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
359 }
360 }
361 | HASH_ELSE {
362 if (parser->skip_stack &&
363 parser->skip_stack->has_else)
364 {
365 glcpp_error(& @1, parser, "multiple #else");
366 }
367 else
368 {
369 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
370 if (parser->skip_stack)
371 parser->skip_stack->has_else = true;
372 }
373 } NEWLINE
374 | HASH_ENDIF {
375 _glcpp_parser_skip_stack_pop (parser, & @1);
376 } NEWLINE
377 | HASH_VERSION integer_constant NEWLINE {
378 if (parser->version_resolved) {
379 glcpp_error(& @1, parser, "#version must appear on the first line");
380 }
381 _glcpp_parser_handle_version_declaration(parser, $2, NULL, true);
382 }
383 | HASH_VERSION integer_constant IDENTIFIER NEWLINE {
384 if (parser->version_resolved) {
385 glcpp_error(& @1, parser, "#version must appear on the first line");
386 }
387 _glcpp_parser_handle_version_declaration(parser, $2, $3, true);
388 }
389 | HASH NEWLINE {
390 glcpp_parser_resolve_implicit_version(parser);
391 }
392 ;
393
394 integer_constant:
395 INTEGER_STRING {
396 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
397 $$ = strtoll ($1 + 2, NULL, 16);
398 } else if ($1[0] == '0') {
399 $$ = strtoll ($1, NULL, 8);
400 } else {
401 $$ = strtoll ($1, NULL, 10);
402 }
403 }
404 | INTEGER {
405 $$ = $1;
406 }
407
408 expression:
409 integer_constant
410 | IDENTIFIER {
411 if (parser->is_gles)
412 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $1);
413 $$ = 0;
414 }
415 | expression OR expression {
416 $$ = $1 || $3;
417 }
418 | expression AND expression {
419 $$ = $1 && $3;
420 }
421 | expression '|' expression {
422 $$ = $1 | $3;
423 }
424 | expression '^' expression {
425 $$ = $1 ^ $3;
426 }
427 | expression '&' expression {
428 $$ = $1 & $3;
429 }
430 | expression NOT_EQUAL expression {
431 $$ = $1 != $3;
432 }
433 | expression EQUAL expression {
434 $$ = $1 == $3;
435 }
436 | expression GREATER_OR_EQUAL expression {
437 $$ = $1 >= $3;
438 }
439 | expression LESS_OR_EQUAL expression {
440 $$ = $1 <= $3;
441 }
442 | expression '>' expression {
443 $$ = $1 > $3;
444 }
445 | expression '<' expression {
446 $$ = $1 < $3;
447 }
448 | expression RIGHT_SHIFT expression {
449 $$ = $1 >> $3;
450 }
451 | expression LEFT_SHIFT expression {
452 $$ = $1 << $3;
453 }
454 | expression '-' expression {
455 $$ = $1 - $3;
456 }
457 | expression '+' expression {
458 $$ = $1 + $3;
459 }
460 | expression '%' expression {
461 if ($3 == 0) {
462 yyerror (& @1, parser,
463 "zero modulus in preprocessor directive");
464 } else {
465 $$ = $1 % $3;
466 }
467 }
468 | expression '/' expression {
469 if ($3 == 0) {
470 yyerror (& @1, parser,
471 "division by 0 in preprocessor directive");
472 } else {
473 $$ = $1 / $3;
474 }
475 }
476 | expression '*' expression {
477 $$ = $1 * $3;
478 }
479 | '!' expression %prec UNARY {
480 $$ = ! $2;
481 }
482 | '~' expression %prec UNARY {
483 $$ = ~ $2;
484 }
485 | '-' expression %prec UNARY {
486 $$ = - $2;
487 }
488 | '+' expression %prec UNARY {
489 $$ = + $2;
490 }
491 | '(' expression ')' {
492 $$ = $2;
493 }
494 ;
495
496 identifier_list:
497 IDENTIFIER {
498 $$ = _string_list_create (parser);
499 _string_list_append_item ($$, $1);
500 ralloc_steal ($$, $1);
501 }
502 | identifier_list ',' IDENTIFIER {
503 $$ = $1;
504 _string_list_append_item ($$, $3);
505 ralloc_steal ($$, $3);
506 }
507 ;
508
509 text_line:
510 NEWLINE { $$ = NULL; }
511 | pp_tokens NEWLINE
512 ;
513
514 non_directive:
515 pp_tokens NEWLINE {
516 yyerror (& @1, parser, "Invalid tokens after #");
517 }
518 ;
519
520 replacement_list:
521 /* empty */ { $$ = NULL; }
522 | pp_tokens
523 ;
524
525 junk:
526 /* empty */
527 | pp_tokens {
528 glcpp_warning(&@1, parser, "extra tokens at end of directive");
529 }
530 ;
531
532 conditional_token:
533 /* Handle "defined" operator */
534 DEFINED IDENTIFIER {
535 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
536 $$ = _token_create_ival (parser, INTEGER, v);
537 }
538 | DEFINED '(' IDENTIFIER ')' {
539 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
540 $$ = _token_create_ival (parser, INTEGER, v);
541 }
542 | preprocessing_token
543 ;
544
545 conditional_tokens:
546 /* Exactly the same as pp_tokens, but using conditional_token */
547 conditional_token {
548 $$ = _token_list_create (parser);
549 _token_list_append ($$, $1);
550 }
551 | conditional_tokens conditional_token {
552 $$ = $1;
553 _token_list_append ($$, $2);
554 }
555 ;
556
557 pp_tokens:
558 preprocessing_token {
559 parser->space_tokens = 1;
560 $$ = _token_list_create (parser);
561 _token_list_append ($$, $1);
562 }
563 | pp_tokens preprocessing_token {
564 $$ = $1;
565 _token_list_append ($$, $2);
566 }
567 ;
568
569 preprocessing_token:
570 IDENTIFIER {
571 $$ = _token_create_str (parser, IDENTIFIER, $1);
572 $$->location = yylloc;
573 }
574 | INTEGER_STRING {
575 $$ = _token_create_str (parser, INTEGER_STRING, $1);
576 $$->location = yylloc;
577 }
578 | operator {
579 $$ = _token_create_ival (parser, $1, $1);
580 $$->location = yylloc;
581 }
582 | OTHER {
583 $$ = _token_create_str (parser, OTHER, $1);
584 $$->location = yylloc;
585 }
586 | SPACE {
587 $$ = _token_create_ival (parser, SPACE, SPACE);
588 $$->location = yylloc;
589 }
590 ;
591
592 operator:
593 '[' { $$ = '['; }
594 | ']' { $$ = ']'; }
595 | '(' { $$ = '('; }
596 | ')' { $$ = ')'; }
597 | '{' { $$ = '{'; }
598 | '}' { $$ = '}'; }
599 | '.' { $$ = '.'; }
600 | '&' { $$ = '&'; }
601 | '*' { $$ = '*'; }
602 | '+' { $$ = '+'; }
603 | '-' { $$ = '-'; }
604 | '~' { $$ = '~'; }
605 | '!' { $$ = '!'; }
606 | '/' { $$ = '/'; }
607 | '%' { $$ = '%'; }
608 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
609 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
610 | '<' { $$ = '<'; }
611 | '>' { $$ = '>'; }
612 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
613 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
614 | EQUAL { $$ = EQUAL; }
615 | NOT_EQUAL { $$ = NOT_EQUAL; }
616 | '^' { $$ = '^'; }
617 | '|' { $$ = '|'; }
618 | AND { $$ = AND; }
619 | OR { $$ = OR; }
620 | ';' { $$ = ';'; }
621 | ',' { $$ = ','; }
622 | '=' { $$ = '='; }
623 | PASTE { $$ = PASTE; }
624 ;
625
626 %%
627
628 string_list_t *
629 _string_list_create (void *ctx)
630 {
631 string_list_t *list;
632
633 list = ralloc (ctx, string_list_t);
634 list->head = NULL;
635 list->tail = NULL;
636
637 return list;
638 }
639
640 void
641 _string_list_append_item (string_list_t *list, const char *str)
642 {
643 string_node_t *node;
644
645 node = ralloc (list, string_node_t);
646 node->str = ralloc_strdup (node, str);
647
648 node->next = NULL;
649
650 if (list->head == NULL) {
651 list->head = node;
652 } else {
653 list->tail->next = node;
654 }
655
656 list->tail = node;
657 }
658
659 int
660 _string_list_contains (string_list_t *list, const char *member, int *index)
661 {
662 string_node_t *node;
663 int i;
664
665 if (list == NULL)
666 return 0;
667
668 for (i = 0, node = list->head; node; i++, node = node->next) {
669 if (strcmp (node->str, member) == 0) {
670 if (index)
671 *index = i;
672 return 1;
673 }
674 }
675
676 return 0;
677 }
678
679 int
680 _string_list_length (string_list_t *list)
681 {
682 int length = 0;
683 string_node_t *node;
684
685 if (list == NULL)
686 return 0;
687
688 for (node = list->head; node; node = node->next)
689 length++;
690
691 return length;
692 }
693
694 int
695 _string_list_equal (string_list_t *a, string_list_t *b)
696 {
697 string_node_t *node_a, *node_b;
698
699 if (a == NULL && b == NULL)
700 return 1;
701
702 if (a == NULL || b == NULL)
703 return 0;
704
705 for (node_a = a->head, node_b = b->head;
706 node_a && node_b;
707 node_a = node_a->next, node_b = node_b->next)
708 {
709 if (strcmp (node_a->str, node_b->str))
710 return 0;
711 }
712
713 /* Catch the case of lists being different lengths, (which
714 * would cause the loop above to terminate after the shorter
715 * list). */
716 return node_a == node_b;
717 }
718
719 argument_list_t *
720 _argument_list_create (void *ctx)
721 {
722 argument_list_t *list;
723
724 list = ralloc (ctx, argument_list_t);
725 list->head = NULL;
726 list->tail = NULL;
727
728 return list;
729 }
730
731 void
732 _argument_list_append (argument_list_t *list, token_list_t *argument)
733 {
734 argument_node_t *node;
735
736 node = ralloc (list, argument_node_t);
737 node->argument = argument;
738
739 node->next = NULL;
740
741 if (list->head == NULL) {
742 list->head = node;
743 } else {
744 list->tail->next = node;
745 }
746
747 list->tail = node;
748 }
749
750 int
751 _argument_list_length (argument_list_t *list)
752 {
753 int length = 0;
754 argument_node_t *node;
755
756 if (list == NULL)
757 return 0;
758
759 for (node = list->head; node; node = node->next)
760 length++;
761
762 return length;
763 }
764
765 token_list_t *
766 _argument_list_member_at (argument_list_t *list, int index)
767 {
768 argument_node_t *node;
769 int i;
770
771 if (list == NULL)
772 return NULL;
773
774 node = list->head;
775 for (i = 0; i < index; i++) {
776 node = node->next;
777 if (node == NULL)
778 break;
779 }
780
781 if (node)
782 return node->argument;
783
784 return NULL;
785 }
786
787 /* Note: This function ralloc_steal()s the str pointer. */
788 token_t *
789 _token_create_str (void *ctx, int type, char *str)
790 {
791 token_t *token;
792
793 token = ralloc (ctx, token_t);
794 token->type = type;
795 token->value.str = str;
796
797 ralloc_steal (token, str);
798
799 return token;
800 }
801
802 token_t *
803 _token_create_ival (void *ctx, int type, int ival)
804 {
805 token_t *token;
806
807 token = ralloc (ctx, token_t);
808 token->type = type;
809 token->value.ival = ival;
810
811 return token;
812 }
813
814 token_list_t *
815 _token_list_create (void *ctx)
816 {
817 token_list_t *list;
818
819 list = ralloc (ctx, token_list_t);
820 list->head = NULL;
821 list->tail = NULL;
822 list->non_space_tail = NULL;
823
824 return list;
825 }
826
827 void
828 _token_list_append (token_list_t *list, token_t *token)
829 {
830 token_node_t *node;
831
832 node = ralloc (list, token_node_t);
833 node->token = token;
834 node->next = NULL;
835
836 if (list->head == NULL) {
837 list->head = node;
838 } else {
839 list->tail->next = node;
840 }
841
842 list->tail = node;
843 if (token->type != SPACE)
844 list->non_space_tail = node;
845 }
846
847 void
848 _token_list_append_list (token_list_t *list, token_list_t *tail)
849 {
850 if (tail == NULL || tail->head == NULL)
851 return;
852
853 if (list->head == NULL) {
854 list->head = tail->head;
855 } else {
856 list->tail->next = tail->head;
857 }
858
859 list->tail = tail->tail;
860 list->non_space_tail = tail->non_space_tail;
861 }
862
863 static token_list_t *
864 _token_list_copy (void *ctx, token_list_t *other)
865 {
866 token_list_t *copy;
867 token_node_t *node;
868
869 if (other == NULL)
870 return NULL;
871
872 copy = _token_list_create (ctx);
873 for (node = other->head; node; node = node->next) {
874 token_t *new_token = ralloc (copy, token_t);
875 *new_token = *node->token;
876 _token_list_append (copy, new_token);
877 }
878
879 return copy;
880 }
881
882 static void
883 _token_list_trim_trailing_space (token_list_t *list)
884 {
885 token_node_t *tail, *next;
886
887 if (list->non_space_tail) {
888 tail = list->non_space_tail->next;
889 list->non_space_tail->next = NULL;
890 list->tail = list->non_space_tail;
891
892 while (tail) {
893 next = tail->next;
894 ralloc_free (tail);
895 tail = next;
896 }
897 }
898 }
899
900 static int
901 _token_list_is_empty_ignoring_space (token_list_t *l)
902 {
903 token_node_t *n;
904
905 if (l == NULL)
906 return 1;
907
908 n = l->head;
909 while (n != NULL && n->token->type == SPACE)
910 n = n->next;
911
912 return n == NULL;
913 }
914
915 int
916 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
917 {
918 token_node_t *node_a, *node_b;
919
920 if (a == NULL || b == NULL) {
921 int a_empty = _token_list_is_empty_ignoring_space(a);
922 int b_empty = _token_list_is_empty_ignoring_space(b);
923 return a_empty == b_empty;
924 }
925
926 node_a = a->head;
927 node_b = b->head;
928
929 while (1)
930 {
931 if (node_a == NULL && node_b == NULL)
932 break;
933
934 if (node_a == NULL || node_b == NULL)
935 return 0;
936
937 if (node_a->token->type == SPACE) {
938 node_a = node_a->next;
939 continue;
940 }
941
942 if (node_b->token->type == SPACE) {
943 node_b = node_b->next;
944 continue;
945 }
946
947 if (node_a->token->type != node_b->token->type)
948 return 0;
949
950 switch (node_a->token->type) {
951 case INTEGER:
952 if (node_a->token->value.ival !=
953 node_b->token->value.ival)
954 {
955 return 0;
956 }
957 break;
958 case IDENTIFIER:
959 case INTEGER_STRING:
960 case OTHER:
961 if (strcmp (node_a->token->value.str,
962 node_b->token->value.str))
963 {
964 return 0;
965 }
966 break;
967 }
968
969 node_a = node_a->next;
970 node_b = node_b->next;
971 }
972
973 return 1;
974 }
975
976 static void
977 _token_print (char **out, size_t *len, token_t *token)
978 {
979 if (token->type < 256) {
980 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
981 return;
982 }
983
984 switch (token->type) {
985 case INTEGER:
986 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
987 break;
988 case IDENTIFIER:
989 case INTEGER_STRING:
990 case OTHER:
991 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
992 break;
993 case SPACE:
994 ralloc_asprintf_rewrite_tail (out, len, " ");
995 break;
996 case LEFT_SHIFT:
997 ralloc_asprintf_rewrite_tail (out, len, "<<");
998 break;
999 case RIGHT_SHIFT:
1000 ralloc_asprintf_rewrite_tail (out, len, ">>");
1001 break;
1002 case LESS_OR_EQUAL:
1003 ralloc_asprintf_rewrite_tail (out, len, "<=");
1004 break;
1005 case GREATER_OR_EQUAL:
1006 ralloc_asprintf_rewrite_tail (out, len, ">=");
1007 break;
1008 case EQUAL:
1009 ralloc_asprintf_rewrite_tail (out, len, "==");
1010 break;
1011 case NOT_EQUAL:
1012 ralloc_asprintf_rewrite_tail (out, len, "!=");
1013 break;
1014 case AND:
1015 ralloc_asprintf_rewrite_tail (out, len, "&&");
1016 break;
1017 case OR:
1018 ralloc_asprintf_rewrite_tail (out, len, "||");
1019 break;
1020 case PASTE:
1021 ralloc_asprintf_rewrite_tail (out, len, "##");
1022 break;
1023 case COMMA_FINAL:
1024 ralloc_asprintf_rewrite_tail (out, len, ",");
1025 break;
1026 case PLACEHOLDER:
1027 /* Nothing to print. */
1028 break;
1029 default:
1030 assert(!"Error: Don't know how to print token.");
1031 break;
1032 }
1033 }
1034
1035 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1036 * 'token' and 'other'. Note that this function may return 'token' or
1037 * 'other' directly rather than allocating anything new.
1038 *
1039 * Caution: Only very cursory error-checking is performed to see if
1040 * the final result is a valid single token. */
1041 static token_t *
1042 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1043 {
1044 token_t *combined = NULL;
1045
1046 /* Pasting a placeholder onto anything makes no change. */
1047 if (other->type == PLACEHOLDER)
1048 return token;
1049
1050 /* When 'token' is a placeholder, just return 'other'. */
1051 if (token->type == PLACEHOLDER)
1052 return other;
1053
1054 /* A very few single-character punctuators can be combined
1055 * with another to form a multi-character punctuator. */
1056 switch (token->type) {
1057 case '<':
1058 if (other->type == '<')
1059 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1060 else if (other->type == '=')
1061 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1062 break;
1063 case '>':
1064 if (other->type == '>')
1065 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1066 else if (other->type == '=')
1067 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1068 break;
1069 case '=':
1070 if (other->type == '=')
1071 combined = _token_create_ival (token, EQUAL, EQUAL);
1072 break;
1073 case '!':
1074 if (other->type == '=')
1075 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1076 break;
1077 case '&':
1078 if (other->type == '&')
1079 combined = _token_create_ival (token, AND, AND);
1080 break;
1081 case '|':
1082 if (other->type == '|')
1083 combined = _token_create_ival (token, OR, OR);
1084 break;
1085 }
1086
1087 if (combined != NULL) {
1088 /* Inherit the location from the first token */
1089 combined->location = token->location;
1090 return combined;
1091 }
1092
1093 /* Two string-valued (or integer) tokens can usually just be
1094 * mashed together. (We also handle a string followed by an
1095 * integer here as well.)
1096 *
1097 * There are some exceptions here. Notably, if the first token
1098 * is an integer (or a string representing an integer), then
1099 * the second token must also be an integer or must be a
1100 * string representing an integer that begins with a digit.
1101 */
1102 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1103 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1104 {
1105 char *str;
1106 int combined_type;
1107
1108 /* Check that pasting onto an integer doesn't create a
1109 * non-integer, (that is, only digits can be
1110 * pasted. */
1111 if (token->type == INTEGER_STRING || token->type == INTEGER)
1112 {
1113 switch (other->type) {
1114 case INTEGER_STRING:
1115 if (other->value.str[0] < '0' ||
1116 other->value.str[0] > '9')
1117 goto FAIL;
1118 break;
1119 case INTEGER:
1120 if (other->value.ival < 0)
1121 goto FAIL;
1122 break;
1123 default:
1124 goto FAIL;
1125 }
1126 }
1127
1128 if (token->type == INTEGER)
1129 str = ralloc_asprintf (token, "%" PRIiMAX,
1130 token->value.ival);
1131 else
1132 str = ralloc_strdup (token, token->value.str);
1133
1134
1135 if (other->type == INTEGER)
1136 ralloc_asprintf_append (&str, "%" PRIiMAX,
1137 other->value.ival);
1138 else
1139 ralloc_strcat (&str, other->value.str);
1140
1141 /* New token is same type as original token, unless we
1142 * started with an integer, in which case we will be
1143 * creating an integer-string. */
1144 combined_type = token->type;
1145 if (combined_type == INTEGER)
1146 combined_type = INTEGER_STRING;
1147
1148 combined = _token_create_str (token, combined_type, str);
1149 combined->location = token->location;
1150 return combined;
1151 }
1152
1153 FAIL:
1154 glcpp_error (&token->location, parser, "");
1155 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1156 _token_print (&parser->info_log, &parser->info_log_length, token);
1157 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1158 _token_print (&parser->info_log, &parser->info_log_length, other);
1159 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1160
1161 return token;
1162 }
1163
1164 static void
1165 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1166 {
1167 token_node_t *node;
1168
1169 if (list == NULL)
1170 return;
1171
1172 for (node = list->head; node; node = node->next)
1173 _token_print (&parser->output, &parser->output_length, node->token);
1174 }
1175
1176 void
1177 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1178 {
1179 glcpp_error(locp, parser, "%s", error);
1180 }
1181
1182 static void add_builtin_define(glcpp_parser_t *parser,
1183 const char *name, int value)
1184 {
1185 token_t *tok;
1186 token_list_t *list;
1187
1188 tok = _token_create_ival (parser, INTEGER, value);
1189
1190 list = _token_list_create(parser);
1191 _token_list_append(list, tok);
1192 _define_object_macro(parser, NULL, name, list);
1193 }
1194
1195 glcpp_parser_t *
1196 glcpp_parser_create (const struct gl_extensions *extensions, gl_api api)
1197 {
1198 glcpp_parser_t *parser;
1199
1200 parser = ralloc (NULL, glcpp_parser_t);
1201
1202 glcpp_lex_init_extra (parser, &parser->scanner);
1203 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1204 hash_table_string_compare);
1205 parser->active = NULL;
1206 parser->lexing_if = 0;
1207 parser->space_tokens = 1;
1208 parser->newline_as_space = 0;
1209 parser->in_control_line = 0;
1210 parser->paren_count = 0;
1211 parser->commented_newlines = 0;
1212
1213 parser->skip_stack = NULL;
1214
1215 parser->lex_from_list = NULL;
1216 parser->lex_from_node = NULL;
1217
1218 parser->output = ralloc_strdup(parser, "");
1219 parser->output_length = 0;
1220 parser->info_log = ralloc_strdup(parser, "");
1221 parser->info_log_length = 0;
1222 parser->error = 0;
1223
1224 parser->extensions = extensions;
1225 parser->api = api;
1226 parser->version_resolved = false;
1227
1228 parser->has_new_line_number = 0;
1229 parser->new_line_number = 1;
1230 parser->has_new_source_number = 0;
1231 parser->new_source_number = 0;
1232
1233 return parser;
1234 }
1235
1236 void
1237 glcpp_parser_destroy (glcpp_parser_t *parser)
1238 {
1239 glcpp_lex_destroy (parser->scanner);
1240 hash_table_dtor (parser->defines);
1241 ralloc_free (parser);
1242 }
1243
1244 typedef enum function_status
1245 {
1246 FUNCTION_STATUS_SUCCESS,
1247 FUNCTION_NOT_A_FUNCTION,
1248 FUNCTION_UNBALANCED_PARENTHESES
1249 } function_status_t;
1250
1251 /* Find a set of function-like macro arguments by looking for a
1252 * balanced set of parentheses.
1253 *
1254 * When called, 'node' should be the opening-parenthesis token, (or
1255 * perhaps preceeding SPACE tokens). Upon successful return *last will
1256 * be the last consumed node, (corresponding to the closing right
1257 * parenthesis).
1258 *
1259 * Return values:
1260 *
1261 * FUNCTION_STATUS_SUCCESS:
1262 *
1263 * Successfully parsed a set of function arguments.
1264 *
1265 * FUNCTION_NOT_A_FUNCTION:
1266 *
1267 * Macro name not followed by a '('. This is not an error, but
1268 * simply that the macro name should be treated as a non-macro.
1269 *
1270 * FUNCTION_UNBALANCED_PARENTHESES
1271 *
1272 * Macro name is not followed by a balanced set of parentheses.
1273 */
1274 static function_status_t
1275 _arguments_parse (argument_list_t *arguments,
1276 token_node_t *node,
1277 token_node_t **last)
1278 {
1279 token_list_t *argument;
1280 int paren_count;
1281
1282 node = node->next;
1283
1284 /* Ignore whitespace before first parenthesis. */
1285 while (node && node->token->type == SPACE)
1286 node = node->next;
1287
1288 if (node == NULL || node->token->type != '(')
1289 return FUNCTION_NOT_A_FUNCTION;
1290
1291 node = node->next;
1292
1293 argument = _token_list_create (arguments);
1294 _argument_list_append (arguments, argument);
1295
1296 for (paren_count = 1; node; node = node->next) {
1297 if (node->token->type == '(')
1298 {
1299 paren_count++;
1300 }
1301 else if (node->token->type == ')')
1302 {
1303 paren_count--;
1304 if (paren_count == 0)
1305 break;
1306 }
1307
1308 if (node->token->type == ',' &&
1309 paren_count == 1)
1310 {
1311 _token_list_trim_trailing_space (argument);
1312 argument = _token_list_create (arguments);
1313 _argument_list_append (arguments, argument);
1314 }
1315 else {
1316 if (argument->head == NULL) {
1317 /* Don't treat initial whitespace as
1318 * part of the arguement. */
1319 if (node->token->type == SPACE)
1320 continue;
1321 }
1322 _token_list_append (argument, node->token);
1323 }
1324 }
1325
1326 if (paren_count)
1327 return FUNCTION_UNBALANCED_PARENTHESES;
1328
1329 *last = node;
1330
1331 return FUNCTION_STATUS_SUCCESS;
1332 }
1333
1334 static token_list_t *
1335 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1336 {
1337 token_list_t *list;
1338 token_t *node;
1339
1340 list = _token_list_create (ctx);
1341 node = _token_create_ival (list, type, ival);
1342 _token_list_append (list, node);
1343
1344 return list;
1345 }
1346
1347 static token_list_t *
1348 _token_list_create_with_one_space (void *ctx)
1349 {
1350 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1351 }
1352
1353 static token_list_t *
1354 _token_list_create_with_one_integer (void *ctx, int ival)
1355 {
1356 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1357 }
1358
1359 /* Perform macro expansion on 'list', placing the resulting tokens
1360 * into a new list which is initialized with a first token of type
1361 * 'head_token_type'. Then begin lexing from the resulting list,
1362 * (return to the current lexing source when this list is exhausted).
1363 */
1364 static void
1365 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1366 int head_token_type,
1367 token_list_t *list)
1368 {
1369 token_list_t *expanded;
1370 token_t *token;
1371
1372 expanded = _token_list_create (parser);
1373 token = _token_create_ival (parser, head_token_type, head_token_type);
1374 _token_list_append (expanded, token);
1375 _glcpp_parser_expand_token_list (parser, list);
1376 _token_list_append_list (expanded, list);
1377 glcpp_parser_lex_from (parser, expanded);
1378 }
1379
1380 static void
1381 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1382 {
1383 token_node_t *node;
1384
1385 node = list->head;
1386 while (node)
1387 {
1388 token_node_t *next_non_space;
1389
1390 /* Look ahead for a PASTE token, skipping space. */
1391 next_non_space = node->next;
1392 while (next_non_space && next_non_space->token->type == SPACE)
1393 next_non_space = next_non_space->next;
1394
1395 if (next_non_space == NULL)
1396 break;
1397
1398 if (next_non_space->token->type != PASTE) {
1399 node = next_non_space;
1400 continue;
1401 }
1402
1403 /* Now find the next non-space token after the PASTE. */
1404 next_non_space = next_non_space->next;
1405 while (next_non_space && next_non_space->token->type == SPACE)
1406 next_non_space = next_non_space->next;
1407
1408 if (next_non_space == NULL) {
1409 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1410 return;
1411 }
1412
1413 node->token = _token_paste (parser, node->token, next_non_space->token);
1414 node->next = next_non_space->next;
1415 if (next_non_space == list->tail)
1416 list->tail = node;
1417 }
1418
1419 list->non_space_tail = list->tail;
1420 }
1421
1422 /* This is a helper function that's essentially part of the
1423 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1424 * except for by that function.
1425 *
1426 * Returns NULL if node is a simple token with no expansion, (that is,
1427 * although 'node' corresponds to an identifier defined as a
1428 * function-like macro, it is not followed with a parenthesized
1429 * argument list).
1430 *
1431 * Compute the complete expansion of node (which is a function-like
1432 * macro) and subsequent nodes which are arguments.
1433 *
1434 * Returns the token list that results from the expansion and sets
1435 * *last to the last node in the list that was consumed by the
1436 * expansion. Specifically, *last will be set as follows: as the
1437 * token of the closing right parenthesis.
1438 */
1439 static token_list_t *
1440 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1441 token_node_t *node,
1442 token_node_t **last)
1443
1444 {
1445 macro_t *macro;
1446 const char *identifier;
1447 argument_list_t *arguments;
1448 function_status_t status;
1449 token_list_t *substituted;
1450 int parameter_index;
1451
1452 identifier = node->token->value.str;
1453
1454 macro = hash_table_find (parser->defines, identifier);
1455
1456 assert (macro->is_function);
1457
1458 arguments = _argument_list_create (parser);
1459 status = _arguments_parse (arguments, node, last);
1460
1461 switch (status) {
1462 case FUNCTION_STATUS_SUCCESS:
1463 break;
1464 case FUNCTION_NOT_A_FUNCTION:
1465 return NULL;
1466 case FUNCTION_UNBALANCED_PARENTHESES:
1467 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1468 return NULL;
1469 }
1470
1471 /* Replace a macro defined as empty with a SPACE token. */
1472 if (macro->replacements == NULL) {
1473 ralloc_free (arguments);
1474 return _token_list_create_with_one_space (parser);
1475 }
1476
1477 if (! ((_argument_list_length (arguments) ==
1478 _string_list_length (macro->parameters)) ||
1479 (_string_list_length (macro->parameters) == 0 &&
1480 _argument_list_length (arguments) == 1 &&
1481 arguments->head->argument->head == NULL)))
1482 {
1483 glcpp_error (&node->token->location, parser,
1484 "Error: macro %s invoked with %d arguments (expected %d)\n",
1485 identifier,
1486 _argument_list_length (arguments),
1487 _string_list_length (macro->parameters));
1488 return NULL;
1489 }
1490
1491 /* Perform argument substitution on the replacement list. */
1492 substituted = _token_list_create (arguments);
1493
1494 for (node = macro->replacements->head; node; node = node->next)
1495 {
1496 if (node->token->type == IDENTIFIER &&
1497 _string_list_contains (macro->parameters,
1498 node->token->value.str,
1499 &parameter_index))
1500 {
1501 token_list_t *argument;
1502 argument = _argument_list_member_at (arguments,
1503 parameter_index);
1504 /* Before substituting, we expand the argument
1505 * tokens, or append a placeholder token for
1506 * an empty argument. */
1507 if (argument->head) {
1508 token_list_t *expanded_argument;
1509 expanded_argument = _token_list_copy (parser,
1510 argument);
1511 _glcpp_parser_expand_token_list (parser,
1512 expanded_argument);
1513 _token_list_append_list (substituted,
1514 expanded_argument);
1515 } else {
1516 token_t *new_token;
1517
1518 new_token = _token_create_ival (substituted,
1519 PLACEHOLDER,
1520 PLACEHOLDER);
1521 _token_list_append (substituted, new_token);
1522 }
1523 } else {
1524 _token_list_append (substituted, node->token);
1525 }
1526 }
1527
1528 /* After argument substitution, and before further expansion
1529 * below, implement token pasting. */
1530
1531 _token_list_trim_trailing_space (substituted);
1532
1533 _glcpp_parser_apply_pastes (parser, substituted);
1534
1535 return substituted;
1536 }
1537
1538 /* Compute the complete expansion of node, (and subsequent nodes after
1539 * 'node' in the case that 'node' is a function-like macro and
1540 * subsequent nodes are arguments).
1541 *
1542 * Returns NULL if node is a simple token with no expansion.
1543 *
1544 * Otherwise, returns the token list that results from the expansion
1545 * and sets *last to the last node in the list that was consumed by
1546 * the expansion. Specifically, *last will be set as follows:
1547 *
1548 * As 'node' in the case of object-like macro expansion.
1549 *
1550 * As the token of the closing right parenthesis in the case of
1551 * function-like macro expansion.
1552 */
1553 static token_list_t *
1554 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1555 token_node_t *node,
1556 token_node_t **last)
1557 {
1558 token_t *token = node->token;
1559 const char *identifier;
1560 macro_t *macro;
1561
1562 /* We only expand identifiers */
1563 if (token->type != IDENTIFIER) {
1564 /* We change any COMMA into a COMMA_FINAL to prevent
1565 * it being mistaken for an argument separator
1566 * later. */
1567 if (token->type == ',') {
1568 token->type = COMMA_FINAL;
1569 token->value.ival = COMMA_FINAL;
1570 }
1571
1572 return NULL;
1573 }
1574
1575 *last = node;
1576 identifier = token->value.str;
1577
1578 /* Special handling for __LINE__ and __FILE__, (not through
1579 * the hash table). */
1580 if (strcmp(identifier, "__LINE__") == 0)
1581 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1582
1583 if (strcmp(identifier, "__FILE__") == 0)
1584 return _token_list_create_with_one_integer (parser, node->token->location.source);
1585
1586 /* Look up this identifier in the hash table. */
1587 macro = hash_table_find (parser->defines, identifier);
1588
1589 /* Not a macro, so no expansion needed. */
1590 if (macro == NULL)
1591 return NULL;
1592
1593 /* Finally, don't expand this macro if we're already actively
1594 * expanding it, (to avoid infinite recursion). */
1595 if (_parser_active_list_contains (parser, identifier)) {
1596 /* We change the token type here from IDENTIFIER to
1597 * OTHER to prevent any future expansion of this
1598 * unexpanded token. */
1599 char *str;
1600 token_list_t *expansion;
1601 token_t *final;
1602
1603 str = ralloc_strdup (parser, token->value.str);
1604 final = _token_create_str (parser, OTHER, str);
1605 expansion = _token_list_create (parser);
1606 _token_list_append (expansion, final);
1607 return expansion;
1608 }
1609
1610 if (! macro->is_function)
1611 {
1612 token_list_t *replacement;
1613
1614 /* Replace a macro defined as empty with a SPACE token. */
1615 if (macro->replacements == NULL)
1616 return _token_list_create_with_one_space (parser);
1617
1618 replacement = _token_list_copy (parser, macro->replacements);
1619 _glcpp_parser_apply_pastes (parser, replacement);
1620 return replacement;
1621 }
1622
1623 return _glcpp_parser_expand_function (parser, node, last);
1624 }
1625
1626 /* Push a new identifier onto the parser's active list.
1627 *
1628 * Here, 'marker' is the token node that appears in the list after the
1629 * expansion of 'identifier'. That is, when the list iterator begins
1630 * examining 'marker', then it is time to pop this node from the
1631 * active stack.
1632 */
1633 static void
1634 _parser_active_list_push (glcpp_parser_t *parser,
1635 const char *identifier,
1636 token_node_t *marker)
1637 {
1638 active_list_t *node;
1639
1640 node = ralloc (parser->active, active_list_t);
1641 node->identifier = ralloc_strdup (node, identifier);
1642 node->marker = marker;
1643 node->next = parser->active;
1644
1645 parser->active = node;
1646 }
1647
1648 static void
1649 _parser_active_list_pop (glcpp_parser_t *parser)
1650 {
1651 active_list_t *node = parser->active;
1652
1653 if (node == NULL) {
1654 parser->active = NULL;
1655 return;
1656 }
1657
1658 node = parser->active->next;
1659 ralloc_free (parser->active);
1660
1661 parser->active = node;
1662 }
1663
1664 static int
1665 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1666 {
1667 active_list_t *node;
1668
1669 if (parser->active == NULL)
1670 return 0;
1671
1672 for (node = parser->active; node; node = node->next)
1673 if (strcmp (node->identifier, identifier) == 0)
1674 return 1;
1675
1676 return 0;
1677 }
1678
1679 /* Walk over the token list replacing nodes with their expansion.
1680 * Whenever nodes are expanded the walking will walk over the new
1681 * nodes, continuing to expand as necessary. The results are placed in
1682 * 'list' itself;
1683 */
1684 static void
1685 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1686 token_list_t *list)
1687 {
1688 token_node_t *node_prev;
1689 token_node_t *node, *last = NULL;
1690 token_list_t *expansion;
1691 active_list_t *active_initial = parser->active;
1692
1693 if (list == NULL)
1694 return;
1695
1696 _token_list_trim_trailing_space (list);
1697
1698 node_prev = NULL;
1699 node = list->head;
1700
1701 while (node) {
1702
1703 while (parser->active && parser->active->marker == node)
1704 _parser_active_list_pop (parser);
1705
1706 expansion = _glcpp_parser_expand_node (parser, node, &last);
1707 if (expansion) {
1708 token_node_t *n;
1709
1710 for (n = node; n != last->next; n = n->next)
1711 while (parser->active &&
1712 parser->active->marker == n)
1713 {
1714 _parser_active_list_pop (parser);
1715 }
1716
1717 _parser_active_list_push (parser,
1718 node->token->value.str,
1719 last->next);
1720
1721 /* Splice expansion into list, supporting a
1722 * simple deletion if the expansion is
1723 * empty. */
1724 if (expansion->head) {
1725 if (node_prev)
1726 node_prev->next = expansion->head;
1727 else
1728 list->head = expansion->head;
1729 expansion->tail->next = last->next;
1730 if (last == list->tail)
1731 list->tail = expansion->tail;
1732 } else {
1733 if (node_prev)
1734 node_prev->next = last->next;
1735 else
1736 list->head = last->next;
1737 if (last == list->tail)
1738 list->tail = NULL;
1739 }
1740 } else {
1741 node_prev = node;
1742 }
1743 node = node_prev ? node_prev->next : list->head;
1744 }
1745
1746 /* Remove any lingering effects of this invocation on the
1747 * active list. That is, pop until the list looks like it did
1748 * at the beginning of this function. */
1749 while (parser->active && parser->active != active_initial)
1750 _parser_active_list_pop (parser);
1751
1752 list->non_space_tail = list->tail;
1753 }
1754
1755 void
1756 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1757 token_list_t *list)
1758 {
1759 if (list == NULL)
1760 return;
1761
1762 _glcpp_parser_expand_token_list (parser, list);
1763
1764 _token_list_trim_trailing_space (list);
1765
1766 _token_list_print (parser, list);
1767 }
1768
1769 static void
1770 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1771 const char *identifier)
1772 {
1773 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
1774 * the GLSL ES spec (all versions) say:
1775 *
1776 * "All macro names containing two consecutive underscores ( __ )
1777 * are reserved for future use as predefined macro names. All
1778 * macro names prefixed with "GL_" ("GL" followed by a single
1779 * underscore) are also reserved."
1780 *
1781 * The intention is that names containing __ are reserved for internal
1782 * use by the implementation, and names prefixed with GL_ are reserved
1783 * for use by Khronos. Since every extension adds a name prefixed
1784 * with GL_ (i.e., the name of the extension), that should be an
1785 * error. Names simply containing __ are dangerous to use, but should
1786 * be allowed.
1787 *
1788 * A future version of the GLSL specification will clarify this.
1789 */
1790 if (strstr(identifier, "__")) {
1791 glcpp_warning(loc, parser,
1792 "Macro names containing \"__\" are reserved "
1793 "for use by the implementation.\n");
1794 }
1795 if (strncmp(identifier, "GL_", 3) == 0) {
1796 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1797 }
1798 }
1799
1800 static int
1801 _macro_equal (macro_t *a, macro_t *b)
1802 {
1803 if (a->is_function != b->is_function)
1804 return 0;
1805
1806 if (a->is_function) {
1807 if (! _string_list_equal (a->parameters, b->parameters))
1808 return 0;
1809 }
1810
1811 return _token_list_equal_ignoring_space (a->replacements,
1812 b->replacements);
1813 }
1814
1815 void
1816 _define_object_macro (glcpp_parser_t *parser,
1817 YYLTYPE *loc,
1818 const char *identifier,
1819 token_list_t *replacements)
1820 {
1821 macro_t *macro, *previous;
1822
1823 if (loc != NULL)
1824 _check_for_reserved_macro_name(parser, loc, identifier);
1825
1826 macro = ralloc (parser, macro_t);
1827
1828 macro->is_function = 0;
1829 macro->parameters = NULL;
1830 macro->identifier = ralloc_strdup (macro, identifier);
1831 macro->replacements = replacements;
1832 ralloc_steal (macro, replacements);
1833
1834 previous = hash_table_find (parser->defines, identifier);
1835 if (previous) {
1836 if (_macro_equal (macro, previous)) {
1837 ralloc_free (macro);
1838 return;
1839 }
1840 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1841 identifier);
1842 }
1843
1844 hash_table_insert (parser->defines, macro, identifier);
1845 }
1846
1847 void
1848 _define_function_macro (glcpp_parser_t *parser,
1849 YYLTYPE *loc,
1850 const char *identifier,
1851 string_list_t *parameters,
1852 token_list_t *replacements)
1853 {
1854 macro_t *macro, *previous;
1855
1856 _check_for_reserved_macro_name(parser, loc, identifier);
1857
1858 macro = ralloc (parser, macro_t);
1859 ralloc_steal (macro, parameters);
1860 ralloc_steal (macro, replacements);
1861
1862 macro->is_function = 1;
1863 macro->parameters = parameters;
1864 macro->identifier = ralloc_strdup (macro, identifier);
1865 macro->replacements = replacements;
1866 previous = hash_table_find (parser->defines, identifier);
1867 if (previous) {
1868 if (_macro_equal (macro, previous)) {
1869 ralloc_free (macro);
1870 return;
1871 }
1872 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1873 identifier);
1874 }
1875
1876 hash_table_insert (parser->defines, macro, identifier);
1877 }
1878
1879 static int
1880 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1881 {
1882 token_node_t *node;
1883 int ret;
1884
1885 if (parser->lex_from_list == NULL) {
1886 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1887
1888 /* XXX: This ugly block of code exists for the sole
1889 * purpose of converting a NEWLINE token into a SPACE
1890 * token, but only in the case where we have seen a
1891 * function-like macro name, but have not yet seen its
1892 * closing parenthesis.
1893 *
1894 * There's perhaps a more compact way to do this with
1895 * mid-rule actions in the grammar.
1896 *
1897 * I'm definitely not pleased with the complexity of
1898 * this code here.
1899 */
1900 if (parser->newline_as_space)
1901 {
1902 if (ret == '(') {
1903 parser->paren_count++;
1904 } else if (ret == ')') {
1905 parser->paren_count--;
1906 if (parser->paren_count == 0)
1907 parser->newline_as_space = 0;
1908 } else if (ret == NEWLINE) {
1909 ret = SPACE;
1910 } else if (ret != SPACE) {
1911 if (parser->paren_count == 0)
1912 parser->newline_as_space = 0;
1913 }
1914 }
1915 else if (parser->in_control_line)
1916 {
1917 if (ret == NEWLINE)
1918 parser->in_control_line = 0;
1919 }
1920 else if (ret == HASH_DEFINE ||
1921 ret == HASH_UNDEF || ret == HASH_IF ||
1922 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1923 ret == HASH_ELIF || ret == HASH_ELSE ||
1924 ret == HASH_ENDIF || ret == HASH)
1925 {
1926 parser->in_control_line = 1;
1927 }
1928 else if (ret == IDENTIFIER)
1929 {
1930 macro_t *macro;
1931 macro = hash_table_find (parser->defines,
1932 yylval->str);
1933 if (macro && macro->is_function) {
1934 parser->newline_as_space = 1;
1935 parser->paren_count = 0;
1936 }
1937 }
1938
1939 return ret;
1940 }
1941
1942 node = parser->lex_from_node;
1943
1944 if (node == NULL) {
1945 ralloc_free (parser->lex_from_list);
1946 parser->lex_from_list = NULL;
1947 return NEWLINE;
1948 }
1949
1950 *yylval = node->token->value;
1951 ret = node->token->type;
1952
1953 parser->lex_from_node = node->next;
1954
1955 return ret;
1956 }
1957
1958 static void
1959 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1960 {
1961 token_node_t *node;
1962
1963 assert (parser->lex_from_list == NULL);
1964
1965 /* Copy list, eliminating any space tokens. */
1966 parser->lex_from_list = _token_list_create (parser);
1967
1968 for (node = list->head; node; node = node->next) {
1969 if (node->token->type == SPACE)
1970 continue;
1971 _token_list_append (parser->lex_from_list, node->token);
1972 }
1973
1974 ralloc_free (list);
1975
1976 parser->lex_from_node = parser->lex_from_list->head;
1977
1978 /* It's possible the list consisted of nothing but whitespace. */
1979 if (parser->lex_from_node == NULL) {
1980 ralloc_free (parser->lex_from_list);
1981 parser->lex_from_list = NULL;
1982 }
1983 }
1984
1985 static void
1986 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1987 int condition)
1988 {
1989 skip_type_t current = SKIP_NO_SKIP;
1990 skip_node_t *node;
1991
1992 if (parser->skip_stack)
1993 current = parser->skip_stack->type;
1994
1995 node = ralloc (parser, skip_node_t);
1996 node->loc = *loc;
1997
1998 if (current == SKIP_NO_SKIP) {
1999 if (condition)
2000 node->type = SKIP_NO_SKIP;
2001 else
2002 node->type = SKIP_TO_ELSE;
2003 } else {
2004 node->type = SKIP_TO_ENDIF;
2005 }
2006
2007 node->has_else = false;
2008 node->next = parser->skip_stack;
2009 parser->skip_stack = node;
2010 }
2011
2012 static void
2013 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
2014 const char *type, int condition)
2015 {
2016 if (parser->skip_stack == NULL) {
2017 glcpp_error (loc, parser, "%s without #if\n", type);
2018 return;
2019 }
2020
2021 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2022 if (condition)
2023 parser->skip_stack->type = SKIP_NO_SKIP;
2024 } else {
2025 parser->skip_stack->type = SKIP_TO_ENDIF;
2026 }
2027 }
2028
2029 static void
2030 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
2031 {
2032 skip_node_t *node;
2033
2034 if (parser->skip_stack == NULL) {
2035 glcpp_error (loc, parser, "#endif without #if\n");
2036 return;
2037 }
2038
2039 node = parser->skip_stack;
2040 parser->skip_stack = node->next;
2041 ralloc_free (node);
2042 }
2043
2044 static void
2045 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2046 const char *es_identifier,
2047 bool explicitly_set)
2048 {
2049 const struct gl_extensions *extensions = parser->extensions;
2050
2051 if (parser->version_resolved)
2052 return;
2053
2054 parser->version_resolved = true;
2055
2056 add_builtin_define (parser, "__VERSION__", version);
2057
2058 parser->is_gles = (version == 100) ||
2059 (es_identifier &&
2060 (strcmp(es_identifier, "es") == 0));
2061
2062 /* Add pre-defined macros. */
2063 if (parser->is_gles) {
2064 add_builtin_define(parser, "GL_ES", 1);
2065
2066 if (extensions != NULL) {
2067 if (extensions->OES_EGL_image_external)
2068 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2069 }
2070 } else {
2071 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2072 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2073 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2074
2075
2076 if (extensions != NULL) {
2077 if (extensions->EXT_texture_array)
2078 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2079
2080 if (extensions->ARB_arrays_of_arrays)
2081 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2082
2083 if (extensions->ARB_fragment_coord_conventions)
2084 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2085 1);
2086
2087 if (extensions->ARB_explicit_attrib_location)
2088 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2089
2090 if (extensions->ARB_shader_texture_lod)
2091 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2092
2093 if (extensions->ARB_draw_instanced)
2094 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2095
2096 if (extensions->ARB_conservative_depth) {
2097 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2098 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2099 }
2100
2101 if (extensions->ARB_shader_bit_encoding)
2102 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2103
2104 if (extensions->ARB_uniform_buffer_object)
2105 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2106
2107 if (extensions->ARB_texture_cube_map_array)
2108 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2109
2110 if (extensions->ARB_shading_language_packing)
2111 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2112
2113 if (extensions->ARB_texture_multisample)
2114 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2115
2116 if (extensions->ARB_texture_query_levels)
2117 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2118
2119 if (extensions->ARB_texture_query_lod)
2120 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2121
2122 if (extensions->ARB_gpu_shader5)
2123 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2124
2125 if (extensions->AMD_vertex_shader_layer)
2126 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2127
2128 if (extensions->ARB_shading_language_420pack)
2129 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2130
2131 if (extensions->ARB_sample_shading)
2132 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2133
2134 if (extensions->ARB_texture_gather)
2135 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2136
2137 if (extensions->ARB_separate_shader_objects)
2138 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2139
2140 if (extensions->ARB_shader_atomic_counters)
2141 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2142
2143 if (extensions->ARB_viewport_array)
2144 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2145
2146 if (extensions->ARB_compute_shader)
2147 add_builtin_define(parser, "GL_ARB_compute_shader", 1);
2148
2149 if (extensions->ARB_shader_image_load_store)
2150 add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
2151 }
2152 }
2153
2154 if (extensions != NULL) {
2155 if (extensions->EXT_shader_integer_mix)
2156 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2157 }
2158
2159 if (version >= 150)
2160 add_builtin_define(parser, "GL_core_profile", 1);
2161
2162 /* Currently, all ES2/ES3 implementations support highp in the
2163 * fragment shader, so we always define this macro in ES2/ES3.
2164 * If we ever get a driver that doesn't support highp, we'll
2165 * need to add a flag to the gl_context and check that here.
2166 */
2167 if (version >= 130 || parser->is_gles)
2168 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2169
2170 if (explicitly_set) {
2171 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
2172 "#version %" PRIiMAX "%s%s", version,
2173 es_identifier ? " " : "",
2174 es_identifier ? es_identifier : "");
2175 }
2176 }
2177
2178 /* GLSL version if no version is explicitly specified. */
2179 #define IMPLICIT_GLSL_VERSION 110
2180
2181 /* GLSL ES version if no version is explicitly specified. */
2182 #define IMPLICIT_GLSL_ES_VERSION 100
2183
2184 void
2185 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2186 {
2187 int language_version = parser->api == API_OPENGLES2 ?
2188 IMPLICIT_GLSL_ES_VERSION :
2189 IMPLICIT_GLSL_VERSION;
2190
2191 _glcpp_parser_handle_version_declaration(parser, language_version,
2192 NULL, false);
2193 }