3 * Copyright © 2010 Intel Corporation
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:
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
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.
32 #include "main/core.h" /* for struct gl_extensions */
33 #include "main/mtypes.h" /* for gl_api enum */
36 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
39 _define_object_macro(glcpp_parser_t *parser,
42 token_list_t *replacements);
45 _define_function_macro(glcpp_parser_t *parser,
48 string_list_t *parameters,
49 token_list_t *replacements);
51 static string_list_t *
52 _string_list_create(glcpp_parser_t *parser);
55 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
59 _string_list_contains(string_list_t *list, const char *member, int *index);
62 _string_list_has_duplicate(string_list_t *list);
65 _string_list_length(string_list_t *list);
68 _string_list_equal(string_list_t *a, string_list_t *b);
70 static argument_list_t *
71 _argument_list_create(glcpp_parser_t *parser);
74 _argument_list_append(glcpp_parser_t *parser, argument_list_t *list,
75 token_list_t *argument);
78 _argument_list_length(argument_list_t *list);
81 _argument_list_member_at(argument_list_t *list, int index);
84 _token_create_str(glcpp_parser_t *parser, int type, char *str);
87 _token_create_ival(glcpp_parser_t *parser, int type, int ival);
90 _token_list_create(glcpp_parser_t *parser);
93 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token);
96 _token_list_append_list(token_list_t *list, token_list_t *tail);
99 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b);
102 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
103 token_node_t *marker);
106 _parser_active_list_pop(glcpp_parser_t *parser);
109 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier);
112 EXPANSION_MODE_IGNORE_DEFINED,
113 EXPANSION_MODE_EVALUATE_DEFINED
116 /* Expand list, and begin lexing from the result (after first
117 * prefixing a token of type 'head_token_type').
120 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
121 token_list_t *list, expansion_mode_t mode);
123 /* Perform macro expansion in-place on the given list. */
125 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
126 expansion_mode_t mode);
129 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
133 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
137 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
138 const char *type, int condition);
141 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc);
144 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
145 const char *ident, bool explicitly_set);
148 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
151 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list);
154 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
170 %parse-param {glcpp_parser_t *parser}
171 %lex-param {glcpp_parser_t *parser}
175 /* We use HASH_TOKEN, DEFINE_TOKEN and VERSION_TOKEN (as opposed to
176 * HASH, DEFINE, and VERSION) to avoid conflicts with other symbols,
177 * (such as the <HASH> and <DEFINE> start conditions in the lexer). */
178 %token DEFINED ELIF_EXPANDED HASH_TOKEN DEFINE_TOKEN FUNC_IDENTIFIER OBJ_IDENTIFIER ELIF ELSE ENDIF ERROR_TOKEN IF IFDEF IFNDEF LINE PRAGMA UNDEF VERSION_TOKEN GARBAGE IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE PLUS_PLUS MINUS_MINUS
180 %type <ival> INTEGER operator SPACE integer_constant
181 %type <expression_value> expression
182 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA
183 %type <string_list> identifier_list
184 %type <token> preprocessing_token
185 %type <token_list> pp_tokens replacement_list text_line
191 %left EQUAL NOT_EQUAL
192 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
193 %left LEFT_SHIFT RIGHT_SHIFT
211 _glcpp_parser_print_expanded_token_list (parser, $1);
212 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
218 IF_EXPANDED expression NEWLINE {
219 if (parser->is_gles && $2.undefined_macro)
220 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
221 _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
223 | ELIF_EXPANDED expression NEWLINE {
224 if (parser->is_gles && $2.undefined_macro)
225 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
226 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
228 | LINE_EXPANDED integer_constant NEWLINE {
229 parser->has_new_line_number = 1;
230 parser->new_line_number = $2;
231 ralloc_asprintf_rewrite_tail (&parser->output,
232 &parser->output_length,
233 "#line %" PRIiMAX "\n",
236 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
237 parser->has_new_line_number = 1;
238 parser->new_line_number = $2;
239 parser->has_new_source_number = 1;
240 parser->new_source_number = $3;
241 ralloc_asprintf_rewrite_tail (&parser->output,
242 &parser->output_length,
243 "#line %" PRIiMAX " %" PRIiMAX "\n",
249 OBJ_IDENTIFIER replacement_list NEWLINE {
250 _define_object_macro (parser, & @1, $1, $2);
252 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
253 _define_function_macro (parser, & @1, $1, NULL, $4);
255 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
256 _define_function_macro (parser, & @1, $1, $3, $5);
261 control_line_success {
262 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
265 | HASH_TOKEN LINE pp_tokens NEWLINE {
267 if (parser->skip_stack == NULL ||
268 parser->skip_stack->type == SKIP_NO_SKIP)
270 _glcpp_parser_expand_and_lex_from (parser,
272 EXPANSION_MODE_IGNORE_DEFINED);
277 control_line_success:
278 HASH_TOKEN DEFINE_TOKEN define
279 | HASH_TOKEN UNDEF IDENTIFIER NEWLINE {
280 struct hash_entry *entry;
282 /* Section 3.4 (Preprocessor) of the GLSL ES 3.00 spec says:
284 * It is an error to undefine or to redefine a built-in
285 * (pre-defined) macro name.
287 * The GLSL ES 1.00 spec does not contain this text.
289 * Section 3.3 (Preprocessor) of the GLSL 1.30 spec says:
291 * #define and #undef functionality are defined as is
292 * standard for C++ preprocessors for macro definitions
293 * both with and without macro parameters.
295 * At least as far as I can tell GCC allow '#undef __FILE__'.
296 * Furthermore, there are desktop OpenGL conformance tests
297 * that expect '#undef __VERSION__' and '#undef
298 * GL_core_profile' to work.
300 * Only disallow #undef of pre-defined macros on GLSL ES >=
303 if (parser->is_gles &&
304 parser->version >= 300 &&
305 (strcmp("__LINE__", $3) == 0
306 || strcmp("__FILE__", $3) == 0
307 || strcmp("__VERSION__", $3) == 0
308 || strncmp("GL_", $3, 3) == 0))
309 glcpp_error(& @1, parser, "Built-in (pre-defined)"
310 " macro names cannot be undefined.");
312 entry = _mesa_hash_table_search (parser->defines, $3);
314 _mesa_hash_table_remove (parser->defines, entry);
317 | HASH_TOKEN IF pp_tokens NEWLINE {
318 /* Be careful to only evaluate the 'if' expression if
319 * we are not skipping. When we are skipping, we
320 * simply push a new 0-valued 'if' onto the skip
323 * This avoids generating diagnostics for invalid
324 * expressions that are being skipped. */
325 if (parser->skip_stack == NULL ||
326 parser->skip_stack->type == SKIP_NO_SKIP)
328 _glcpp_parser_expand_and_lex_from (parser,
330 EXPANSION_MODE_EVALUATE_DEFINED);
334 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
335 parser->skip_stack->type = SKIP_TO_ENDIF;
338 | HASH_TOKEN IF NEWLINE {
339 /* #if without an expression is only an error if we
340 * are not skipping */
341 if (parser->skip_stack == NULL ||
342 parser->skip_stack->type == SKIP_NO_SKIP)
344 glcpp_error(& @1, parser, "#if with no expression");
346 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
348 | HASH_TOKEN IFDEF IDENTIFIER junk NEWLINE {
349 struct hash_entry *entry =
350 _mesa_hash_table_search(parser->defines, $3);
351 macro_t *macro = entry ? entry->data : NULL;
352 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
354 | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE {
355 struct hash_entry *entry =
356 _mesa_hash_table_search(parser->defines, $3);
357 macro_t *macro = entry ? entry->data : NULL;
358 _glcpp_parser_skip_stack_push_if (parser, & @3, macro == NULL);
360 | HASH_TOKEN ELIF pp_tokens NEWLINE {
361 /* Be careful to only evaluate the 'elif' expression
362 * if we are not skipping. When we are skipping, we
363 * simply change to a 0-valued 'elif' on the skip
366 * This avoids generating diagnostics for invalid
367 * expressions that are being skipped. */
368 if (parser->skip_stack &&
369 parser->skip_stack->type == SKIP_TO_ELSE)
371 _glcpp_parser_expand_and_lex_from (parser,
373 EXPANSION_MODE_EVALUATE_DEFINED);
375 else if (parser->skip_stack &&
376 parser->skip_stack->has_else)
378 glcpp_error(& @1, parser, "#elif after #else");
382 _glcpp_parser_skip_stack_change_if (parser, & @1,
386 | HASH_TOKEN ELIF NEWLINE {
387 /* #elif without an expression is an error unless we
389 if (parser->skip_stack &&
390 parser->skip_stack->type == SKIP_TO_ELSE)
392 glcpp_error(& @1, parser, "#elif with no expression");
394 else if (parser->skip_stack &&
395 parser->skip_stack->has_else)
397 glcpp_error(& @1, parser, "#elif after #else");
401 _glcpp_parser_skip_stack_change_if (parser, & @1,
403 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
406 | HASH_TOKEN ELSE { parser->lexing_directive = 1; } NEWLINE {
407 if (parser->skip_stack &&
408 parser->skip_stack->has_else)
410 glcpp_error(& @1, parser, "multiple #else");
414 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
415 if (parser->skip_stack)
416 parser->skip_stack->has_else = true;
420 _glcpp_parser_skip_stack_pop (parser, & @1);
422 | HASH_TOKEN VERSION_TOKEN integer_constant NEWLINE {
423 if (parser->version != 0) {
424 glcpp_error(& @1, parser, "#version must appear on the first line");
426 _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
428 | HASH_TOKEN VERSION_TOKEN integer_constant IDENTIFIER NEWLINE {
429 if (parser->version != 0) {
430 glcpp_error(& @1, parser, "#version must appear on the first line");
432 _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
434 | HASH_TOKEN NEWLINE {
435 glcpp_parser_resolve_implicit_version(parser);
437 | HASH_TOKEN PRAGMA NEWLINE {
438 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#%s", $2);
443 HASH_TOKEN ERROR_TOKEN NEWLINE {
444 glcpp_error(& @1, parser, "#%s", $2);
446 | HASH_TOKEN DEFINE_TOKEN NEWLINE {
447 glcpp_error (& @1, parser, "#define without macro name");
449 | HASH_TOKEN GARBAGE pp_tokens NEWLINE {
450 glcpp_error (& @1, parser, "Illegal non-directive after #");
456 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
457 $$ = strtoll ($1 + 2, NULL, 16);
458 } else if ($1[0] == '0') {
459 $$ = strtoll ($1, NULL, 8);
461 $$ = strtoll ($1, NULL, 10);
471 $$.undefined_macro = NULL;
476 $$.undefined_macro = linear_strdup(parser->linalloc, $1);
478 $$.undefined_macro = NULL;
480 | expression OR expression {
481 $$.value = $1.value || $3.value;
483 /* Short-circuit: Only flag undefined from right side
484 * if left side evaluates to false.
486 if ($1.undefined_macro)
487 $$.undefined_macro = $1.undefined_macro;
489 $$.undefined_macro = $3.undefined_macro;
491 | expression AND expression {
492 $$.value = $1.value && $3.value;
494 /* Short-circuit: Only flag undefined from right-side
495 * if left side evaluates to true.
497 if ($1.undefined_macro)
498 $$.undefined_macro = $1.undefined_macro;
500 $$.undefined_macro = $3.undefined_macro;
502 | expression '|' expression {
503 $$.value = $1.value | $3.value;
504 if ($1.undefined_macro)
505 $$.undefined_macro = $1.undefined_macro;
507 $$.undefined_macro = $3.undefined_macro;
509 | expression '^' expression {
510 $$.value = $1.value ^ $3.value;
511 if ($1.undefined_macro)
512 $$.undefined_macro = $1.undefined_macro;
514 $$.undefined_macro = $3.undefined_macro;
516 | expression '&' expression {
517 $$.value = $1.value & $3.value;
518 if ($1.undefined_macro)
519 $$.undefined_macro = $1.undefined_macro;
521 $$.undefined_macro = $3.undefined_macro;
523 | expression NOT_EQUAL expression {
524 $$.value = $1.value != $3.value;
525 if ($1.undefined_macro)
526 $$.undefined_macro = $1.undefined_macro;
528 $$.undefined_macro = $3.undefined_macro;
530 | expression EQUAL expression {
531 $$.value = $1.value == $3.value;
532 if ($1.undefined_macro)
533 $$.undefined_macro = $1.undefined_macro;
535 $$.undefined_macro = $3.undefined_macro;
537 | expression GREATER_OR_EQUAL expression {
538 $$.value = $1.value >= $3.value;
539 if ($1.undefined_macro)
540 $$.undefined_macro = $1.undefined_macro;
542 $$.undefined_macro = $3.undefined_macro;
544 | expression LESS_OR_EQUAL expression {
545 $$.value = $1.value <= $3.value;
546 if ($1.undefined_macro)
547 $$.undefined_macro = $1.undefined_macro;
549 $$.undefined_macro = $3.undefined_macro;
551 | expression '>' expression {
552 $$.value = $1.value > $3.value;
553 if ($1.undefined_macro)
554 $$.undefined_macro = $1.undefined_macro;
556 $$.undefined_macro = $3.undefined_macro;
558 | expression '<' expression {
559 $$.value = $1.value < $3.value;
560 if ($1.undefined_macro)
561 $$.undefined_macro = $1.undefined_macro;
563 $$.undefined_macro = $3.undefined_macro;
565 | expression RIGHT_SHIFT expression {
566 $$.value = $1.value >> $3.value;
567 if ($1.undefined_macro)
568 $$.undefined_macro = $1.undefined_macro;
570 $$.undefined_macro = $3.undefined_macro;
572 | expression LEFT_SHIFT expression {
573 $$.value = $1.value << $3.value;
574 if ($1.undefined_macro)
575 $$.undefined_macro = $1.undefined_macro;
577 $$.undefined_macro = $3.undefined_macro;
579 | expression '-' expression {
580 $$.value = $1.value - $3.value;
581 if ($1.undefined_macro)
582 $$.undefined_macro = $1.undefined_macro;
584 $$.undefined_macro = $3.undefined_macro;
586 | expression '+' expression {
587 $$.value = $1.value + $3.value;
588 if ($1.undefined_macro)
589 $$.undefined_macro = $1.undefined_macro;
591 $$.undefined_macro = $3.undefined_macro;
593 | expression '%' expression {
595 yyerror (& @1, parser,
596 "zero modulus in preprocessor directive");
598 $$.value = $1.value % $3.value;
600 if ($1.undefined_macro)
601 $$.undefined_macro = $1.undefined_macro;
603 $$.undefined_macro = $3.undefined_macro;
605 | expression '/' expression {
607 yyerror (& @1, parser,
608 "division by 0 in preprocessor directive");
610 $$.value = $1.value / $3.value;
612 if ($1.undefined_macro)
613 $$.undefined_macro = $1.undefined_macro;
615 $$.undefined_macro = $3.undefined_macro;
617 | expression '*' expression {
618 $$.value = $1.value * $3.value;
619 if ($1.undefined_macro)
620 $$.undefined_macro = $1.undefined_macro;
622 $$.undefined_macro = $3.undefined_macro;
624 | '!' expression %prec UNARY {
625 $$.value = ! $2.value;
626 $$.undefined_macro = $2.undefined_macro;
628 | '~' expression %prec UNARY {
629 $$.value = ~ $2.value;
630 $$.undefined_macro = $2.undefined_macro;
632 | '-' expression %prec UNARY {
633 $$.value = - $2.value;
634 $$.undefined_macro = $2.undefined_macro;
636 | '+' expression %prec UNARY {
637 $$.value = + $2.value;
638 $$.undefined_macro = $2.undefined_macro;
640 | '(' expression ')' {
647 $$ = _string_list_create (parser);
648 _string_list_append_item (parser, $$, $1);
650 | identifier_list ',' IDENTIFIER {
652 _string_list_append_item (parser, $$, $3);
657 NEWLINE { $$ = NULL; }
662 /* empty */ { $$ = NULL; }
669 glcpp_error(&@1, parser, "extra tokens at end of directive");
674 preprocessing_token {
675 parser->space_tokens = 1;
676 $$ = _token_list_create (parser);
677 _token_list_append (parser, $$, $1);
679 | pp_tokens preprocessing_token {
681 _token_list_append (parser, $$, $2);
687 $$ = _token_create_str (parser, IDENTIFIER, $1);
688 $$->location = yylloc;
691 $$ = _token_create_str (parser, INTEGER_STRING, $1);
692 $$->location = yylloc;
695 $$ = _token_create_ival (parser, $1, $1);
696 $$->location = yylloc;
699 $$ = _token_create_ival (parser, DEFINED, DEFINED);
700 $$->location = yylloc;
703 $$ = _token_create_str (parser, OTHER, $1);
704 $$->location = yylloc;
707 $$ = _token_create_ival (parser, SPACE, SPACE);
708 $$->location = yylloc;
728 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
729 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
732 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
733 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
734 | EQUAL { $$ = EQUAL; }
735 | NOT_EQUAL { $$ = NOT_EQUAL; }
743 | PASTE { $$ = PASTE; }
744 | PLUS_PLUS { $$ = PLUS_PLUS; }
745 | MINUS_MINUS { $$ = MINUS_MINUS; }
751 _string_list_create(glcpp_parser_t *parser)
755 list = linear_alloc_child(parser->linalloc, sizeof(string_list_t));
763 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
768 node = linear_alloc_child(parser->linalloc, sizeof(string_node_t));
769 node->str = linear_strdup(parser->linalloc, str);
773 if (list->head == NULL) {
776 list->tail->next = node;
783 _string_list_contains(string_list_t *list, const char *member, int *index)
791 for (i = 0, node = list->head; node; i++, node = node->next) {
792 if (strcmp (node->str, member) == 0) {
802 /* Return duplicate string in list (if any), NULL otherwise. */
804 _string_list_has_duplicate(string_list_t *list)
806 string_node_t *node, *dup;
811 for (node = list->head; node; node = node->next) {
812 for (dup = node->next; dup; dup = dup->next) {
813 if (strcmp (node->str, dup->str) == 0)
822 _string_list_length(string_list_t *list)
830 for (node = list->head; node; node = node->next)
837 _string_list_equal(string_list_t *a, string_list_t *b)
839 string_node_t *node_a, *node_b;
841 if (a == NULL && b == NULL)
844 if (a == NULL || b == NULL)
847 for (node_a = a->head, node_b = b->head;
849 node_a = node_a->next, node_b = node_b->next)
851 if (strcmp (node_a->str, node_b->str))
855 /* Catch the case of lists being different lengths, (which
856 * would cause the loop above to terminate after the shorter
858 return node_a == node_b;
862 _argument_list_create(glcpp_parser_t *parser)
864 argument_list_t *list;
866 list = linear_alloc_child(parser->linalloc, sizeof(argument_list_t));
874 _argument_list_append(glcpp_parser_t *parser,
875 argument_list_t *list, token_list_t *argument)
877 argument_node_t *node;
879 node = linear_alloc_child(parser->linalloc, sizeof(argument_node_t));
880 node->argument = argument;
884 if (list->head == NULL) {
887 list->tail->next = node;
894 _argument_list_length(argument_list_t *list)
897 argument_node_t *node;
902 for (node = list->head; node; node = node->next)
909 _argument_list_member_at(argument_list_t *list, int index)
911 argument_node_t *node;
918 for (i = 0; i < index; i++) {
925 return node->argument;
931 _token_create_str(glcpp_parser_t *parser, int type, char *str)
935 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
937 token->value.str = str;
943 _token_create_ival(glcpp_parser_t *parser, int type, int ival)
947 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
949 token->value.ival = ival;
955 _token_list_create(glcpp_parser_t *parser)
959 list = linear_alloc_child(parser->linalloc, sizeof(token_list_t));
962 list->non_space_tail = NULL;
968 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token)
972 node = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
976 if (list->head == NULL) {
979 list->tail->next = node;
983 if (token->type != SPACE)
984 list->non_space_tail = node;
988 _token_list_append_list(token_list_t *list, token_list_t *tail)
990 if (tail == NULL || tail->head == NULL)
993 if (list->head == NULL) {
994 list->head = tail->head;
996 list->tail->next = tail->head;
999 list->tail = tail->tail;
1000 list->non_space_tail = tail->non_space_tail;
1003 static token_list_t *
1004 _token_list_copy(glcpp_parser_t *parser, token_list_t *other)
1012 copy = _token_list_create (parser);
1013 for (node = other->head; node; node = node->next) {
1014 token_t *new_token = linear_alloc_child(parser->linalloc, sizeof(token_t));
1015 *new_token = *node->token;
1016 _token_list_append (parser, copy, new_token);
1023 _token_list_trim_trailing_space(token_list_t *list)
1025 if (list->non_space_tail) {
1026 list->non_space_tail->next = NULL;
1027 list->tail = list->non_space_tail;
1032 _token_list_is_empty_ignoring_space(token_list_t *l)
1040 while (n != NULL && n->token->type == SPACE)
1047 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1049 token_node_t *node_a, *node_b;
1051 if (a == NULL || b == NULL) {
1052 int a_empty = _token_list_is_empty_ignoring_space(a);
1053 int b_empty = _token_list_is_empty_ignoring_space(b);
1054 return a_empty == b_empty;
1062 if (node_a == NULL && node_b == NULL)
1065 if (node_a == NULL || node_b == NULL)
1067 /* Make sure whitespace appears in the same places in both.
1068 * It need not be exactly the same amount of whitespace,
1071 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1072 while (node_a && node_a->token->type == SPACE)
1073 node_a = node_a->next;
1074 while (node_b && node_b->token->type == SPACE)
1075 node_b = node_b->next;
1079 if (node_a->token->type != node_b->token->type)
1082 switch (node_a->token->type) {
1084 if (node_a->token->value.ival != node_b->token->value.ival) {
1089 case INTEGER_STRING:
1091 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1097 node_a = node_a->next;
1098 node_b = node_b->next;
1105 _token_print(char **out, size_t *len, token_t *token)
1107 if (token->type < 256) {
1108 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1112 switch (token->type) {
1114 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1117 case INTEGER_STRING:
1119 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1122 ralloc_asprintf_rewrite_tail (out, len, " ");
1125 ralloc_asprintf_rewrite_tail (out, len, "<<");
1128 ralloc_asprintf_rewrite_tail (out, len, ">>");
1131 ralloc_asprintf_rewrite_tail (out, len, "<=");
1133 case GREATER_OR_EQUAL:
1134 ralloc_asprintf_rewrite_tail (out, len, ">=");
1137 ralloc_asprintf_rewrite_tail (out, len, "==");
1140 ralloc_asprintf_rewrite_tail (out, len, "!=");
1143 ralloc_asprintf_rewrite_tail (out, len, "&&");
1146 ralloc_asprintf_rewrite_tail (out, len, "||");
1149 ralloc_asprintf_rewrite_tail (out, len, "##");
1152 ralloc_asprintf_rewrite_tail (out, len, "++");
1155 ralloc_asprintf_rewrite_tail (out, len, "--");
1158 ralloc_asprintf_rewrite_tail (out, len, "defined");
1161 /* Nothing to print. */
1164 assert(!"Error: Don't know how to print token.");
1170 /* Return a new token formed by pasting 'token' and 'other'. Note that this
1171 * function may return 'token' or 'other' directly rather than allocating
1174 * Caution: Only very cursory error-checking is performed to see if
1175 * the final result is a valid single token. */
1177 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1179 token_t *combined = NULL;
1181 /* Pasting a placeholder onto anything makes no change. */
1182 if (other->type == PLACEHOLDER)
1185 /* When 'token' is a placeholder, just return 'other'. */
1186 if (token->type == PLACEHOLDER)
1189 /* A very few single-character punctuators can be combined
1190 * with another to form a multi-character punctuator. */
1191 switch (token->type) {
1193 if (other->type == '<')
1194 combined = _token_create_ival (parser, LEFT_SHIFT, LEFT_SHIFT);
1195 else if (other->type == '=')
1196 combined = _token_create_ival (parser, LESS_OR_EQUAL, LESS_OR_EQUAL);
1199 if (other->type == '>')
1200 combined = _token_create_ival (parser, RIGHT_SHIFT, RIGHT_SHIFT);
1201 else if (other->type == '=')
1202 combined = _token_create_ival (parser, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1205 if (other->type == '=')
1206 combined = _token_create_ival (parser, EQUAL, EQUAL);
1209 if (other->type == '=')
1210 combined = _token_create_ival (parser, NOT_EQUAL, NOT_EQUAL);
1213 if (other->type == '&')
1214 combined = _token_create_ival (parser, AND, AND);
1217 if (other->type == '|')
1218 combined = _token_create_ival (parser, OR, OR);
1222 if (combined != NULL) {
1223 /* Inherit the location from the first token */
1224 combined->location = token->location;
1228 /* Two string-valued (or integer) tokens can usually just be
1229 * mashed together. (We also handle a string followed by an
1230 * integer here as well.)
1232 * There are some exceptions here. Notably, if the first token
1233 * is an integer (or a string representing an integer), then
1234 * the second token must also be an integer or must be a
1235 * string representing an integer that begins with a digit.
1237 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1238 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1243 /* Check that pasting onto an integer doesn't create a
1244 * non-integer, (that is, only digits can be
1246 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1247 switch (other->type) {
1248 case INTEGER_STRING:
1249 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1253 if (other->value.ival < 0)
1261 if (token->type == INTEGER)
1262 str = linear_asprintf(parser->linalloc, "%" PRIiMAX, token->value.ival);
1264 str = linear_strdup(parser->linalloc, token->value.str);
1266 if (other->type == INTEGER)
1267 linear_asprintf_append(parser->linalloc, &str, "%" PRIiMAX, other->value.ival);
1269 linear_strcat(parser->linalloc, &str, other->value.str);
1271 /* New token is same type as original token, unless we
1272 * started with an integer, in which case we will be
1273 * creating an integer-string. */
1274 combined_type = token->type;
1275 if (combined_type == INTEGER)
1276 combined_type = INTEGER_STRING;
1278 combined = _token_create_str (parser, combined_type, str);
1279 combined->location = token->location;
1284 glcpp_error (&token->location, parser, "");
1285 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1286 _token_print (&parser->info_log, &parser->info_log_length, token);
1287 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1288 _token_print (&parser->info_log, &parser->info_log_length, other);
1289 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1295 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1302 for (node = list->head; node; node = node->next)
1303 _token_print (&parser->output, &parser->output_length, node->token);
1307 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1309 glcpp_error(locp, parser, "%s", error);
1313 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1318 tok = _token_create_ival (parser, INTEGER, value);
1320 list = _token_list_create(parser);
1321 _token_list_append(parser, list, tok);
1322 _define_object_macro(parser, NULL, name, list);
1326 glcpp_parser_create(glcpp_extension_iterator extensions, void *state, gl_api api)
1328 glcpp_parser_t *parser;
1330 parser = ralloc (NULL, glcpp_parser_t);
1332 glcpp_lex_init_extra (parser, &parser->scanner);
1333 parser->defines = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
1334 _mesa_key_string_equal);
1335 parser->linalloc = linear_alloc_parent(parser, 0);
1336 parser->active = NULL;
1337 parser->lexing_directive = 0;
1338 parser->lexing_version_directive = 0;
1339 parser->space_tokens = 1;
1340 parser->last_token_was_newline = 0;
1341 parser->last_token_was_space = 0;
1342 parser->first_non_space_token_this_line = 1;
1343 parser->newline_as_space = 0;
1344 parser->in_control_line = 0;
1345 parser->paren_count = 0;
1346 parser->commented_newlines = 0;
1348 parser->skip_stack = NULL;
1349 parser->skipping = 0;
1351 parser->lex_from_list = NULL;
1352 parser->lex_from_node = NULL;
1354 parser->output = ralloc_strdup(parser, "");
1355 parser->output_length = 0;
1356 parser->info_log = ralloc_strdup(parser, "");
1357 parser->info_log_length = 0;
1360 parser->extensions = extensions;
1361 parser->state = state;
1363 parser->version = 0;
1365 parser->has_new_line_number = 0;
1366 parser->new_line_number = 1;
1367 parser->has_new_source_number = 0;
1368 parser->new_source_number = 0;
1370 parser->is_gles = false;
1376 glcpp_parser_destroy(glcpp_parser_t *parser)
1378 glcpp_lex_destroy (parser->scanner);
1379 _mesa_hash_table_destroy(parser->defines, NULL);
1380 ralloc_free (parser);
1383 typedef enum function_status
1385 FUNCTION_STATUS_SUCCESS,
1386 FUNCTION_NOT_A_FUNCTION,
1387 FUNCTION_UNBALANCED_PARENTHESES
1388 } function_status_t;
1390 /* Find a set of function-like macro arguments by looking for a
1391 * balanced set of parentheses.
1393 * When called, 'node' should be the opening-parenthesis token, (or
1394 * perhaps preceeding SPACE tokens). Upon successful return *last will
1395 * be the last consumed node, (corresponding to the closing right
1400 * FUNCTION_STATUS_SUCCESS:
1402 * Successfully parsed a set of function arguments.
1404 * FUNCTION_NOT_A_FUNCTION:
1406 * Macro name not followed by a '('. This is not an error, but
1407 * simply that the macro name should be treated as a non-macro.
1409 * FUNCTION_UNBALANCED_PARENTHESES
1411 * Macro name is not followed by a balanced set of parentheses.
1413 static function_status_t
1414 _arguments_parse(glcpp_parser_t *parser,
1415 argument_list_t *arguments, token_node_t *node,
1416 token_node_t **last)
1418 token_list_t *argument;
1423 /* Ignore whitespace before first parenthesis. */
1424 while (node && node->token->type == SPACE)
1427 if (node == NULL || node->token->type != '(')
1428 return FUNCTION_NOT_A_FUNCTION;
1432 argument = _token_list_create (parser);
1433 _argument_list_append (parser, arguments, argument);
1435 for (paren_count = 1; node; node = node->next) {
1436 if (node->token->type == '(') {
1438 } else if (node->token->type == ')') {
1440 if (paren_count == 0)
1444 if (node->token->type == ',' && paren_count == 1) {
1445 _token_list_trim_trailing_space (argument);
1446 argument = _token_list_create (parser);
1447 _argument_list_append (parser, arguments, argument);
1449 if (argument->head == NULL) {
1450 /* Don't treat initial whitespace as part of the argument. */
1451 if (node->token->type == SPACE)
1454 _token_list_append(parser, argument, node->token);
1459 return FUNCTION_UNBALANCED_PARENTHESES;
1463 return FUNCTION_STATUS_SUCCESS;
1466 static token_list_t *
1467 _token_list_create_with_one_ival(glcpp_parser_t *parser, int type, int ival)
1472 list = _token_list_create(parser);
1473 node = _token_create_ival(parser, type, ival);
1474 _token_list_append(parser, list, node);
1479 static token_list_t *
1480 _token_list_create_with_one_space(glcpp_parser_t *parser)
1482 return _token_list_create_with_one_ival(parser, SPACE, SPACE);
1485 static token_list_t *
1486 _token_list_create_with_one_integer(glcpp_parser_t *parser, int ival)
1488 return _token_list_create_with_one_ival(parser, INTEGER, ival);
1491 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1493 * Note: This function must only be called when "node" is a DEFINED token,
1494 * (and will abort with an assertion failure otherwise).
1496 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1497 * (optionally preceded and followed by '(' and ')' tokens) then the following
1500 * If the identifier is a defined macro, this function returns 1.
1502 * If the identifier is not a defined macro, this function returns 0.
1504 * In either case, *last will be updated to the last node in the list
1505 * consumed by the evaluation, (either the token of the identifier or the
1506 * token of the closing parenthesis).
1508 * In all other cases, (such as "node is the final node of the list", or
1509 * "missing closing parenthesis", etc.), this function generates a
1510 * preprocessor error, returns -1 and *last will not be set.
1513 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1514 token_node_t **last)
1516 token_node_t *argument, *defined = node;
1518 assert(node->token->type == DEFINED);
1522 /* Ignore whitespace after DEFINED token. */
1523 while (node && node->token->type == SPACE)
1529 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1531 } else if (node->token->type == '(') {
1534 /* Ignore whitespace after '(' token. */
1535 while (node && node->token->type == SPACE)
1538 if (node == NULL || (node->token->type != IDENTIFIER &&
1539 node->token->type != OTHER)) {
1547 /* Ignore whitespace after identifier, before ')' token. */
1548 while (node && node->token->type == SPACE)
1551 if (node == NULL || node->token->type != ')')
1559 return _mesa_hash_table_search(parser->defines,
1560 argument->token->value.str) ? 1 : 0;
1563 glcpp_error (&defined->token->location, parser,
1564 "\"defined\" not followed by an identifier");
1568 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1571 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1574 token_node_t *node, *node_prev, *replacement, *last = NULL;
1585 if (node->token->type != DEFINED)
1588 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1592 replacement = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
1593 replacement->token = _token_create_ival (parser, INTEGER, value);
1595 /* Splice replacement node into list, replacing from "node"
1596 * through "last". */
1598 node_prev->next = replacement;
1600 list->head = replacement;
1601 replacement->next = last->next;
1602 if (last == list->tail)
1603 list->tail = replacement;
1613 /* Perform macro expansion on 'list', placing the resulting tokens
1614 * into a new list which is initialized with a first token of type
1615 * 'head_token_type'. Then begin lexing from the resulting list,
1616 * (return to the current lexing source when this list is exhausted).
1618 * See the documentation of _glcpp_parser_expand_token_list for a description
1619 * of the "mode" parameter.
1622 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1623 token_list_t *list, expansion_mode_t mode)
1625 token_list_t *expanded;
1628 expanded = _token_list_create (parser);
1629 token = _token_create_ival (parser, head_token_type, head_token_type);
1630 _token_list_append (parser, expanded, token);
1631 _glcpp_parser_expand_token_list (parser, list, mode);
1632 _token_list_append_list (expanded, list);
1633 glcpp_parser_lex_from (parser, expanded);
1637 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1643 token_node_t *next_non_space;
1645 /* Look ahead for a PASTE token, skipping space. */
1646 next_non_space = node->next;
1647 while (next_non_space && next_non_space->token->type == SPACE)
1648 next_non_space = next_non_space->next;
1650 if (next_non_space == NULL)
1653 if (next_non_space->token->type != PASTE) {
1654 node = next_non_space;
1658 /* Now find the next non-space token after the PASTE. */
1659 next_non_space = next_non_space->next;
1660 while (next_non_space && next_non_space->token->type == SPACE)
1661 next_non_space = next_non_space->next;
1663 if (next_non_space == NULL) {
1664 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1668 node->token = _token_paste(parser, node->token, next_non_space->token);
1669 node->next = next_non_space->next;
1670 if (next_non_space == list->tail)
1674 list->non_space_tail = list->tail;
1677 /* This is a helper function that's essentially part of the
1678 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1679 * except for by that function.
1681 * Returns NULL if node is a simple token with no expansion, (that is,
1682 * although 'node' corresponds to an identifier defined as a
1683 * function-like macro, it is not followed with a parenthesized
1686 * Compute the complete expansion of node (which is a function-like
1687 * macro) and subsequent nodes which are arguments.
1689 * Returns the token list that results from the expansion and sets
1690 * *last to the last node in the list that was consumed by the
1691 * expansion. Specifically, *last will be set as follows: as the
1692 * token of the closing right parenthesis.
1694 * See the documentation of _glcpp_parser_expand_token_list for a description
1695 * of the "mode" parameter.
1697 static token_list_t *
1698 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1699 token_node_t **last, expansion_mode_t mode)
1701 struct hash_entry *entry;
1703 const char *identifier;
1704 argument_list_t *arguments;
1705 function_status_t status;
1706 token_list_t *substituted;
1707 int parameter_index;
1709 identifier = node->token->value.str;
1711 entry = _mesa_hash_table_search(parser->defines, identifier);
1712 macro = entry ? entry->data : NULL;
1714 assert(macro->is_function);
1716 arguments = _argument_list_create(parser);
1717 status = _arguments_parse(parser, arguments, node, last);
1720 case FUNCTION_STATUS_SUCCESS:
1722 case FUNCTION_NOT_A_FUNCTION:
1724 case FUNCTION_UNBALANCED_PARENTHESES:
1725 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1729 /* Replace a macro defined as empty with a SPACE token. */
1730 if (macro->replacements == NULL) {
1731 return _token_list_create_with_one_space(parser);
1734 if (!((_argument_list_length (arguments) ==
1735 _string_list_length (macro->parameters)) ||
1736 (_string_list_length (macro->parameters) == 0 &&
1737 _argument_list_length (arguments) == 1 &&
1738 arguments->head->argument->head == NULL))) {
1739 glcpp_error(&node->token->location, parser,
1740 "Error: macro %s invoked with %d arguments (expected %d)\n",
1741 identifier, _argument_list_length (arguments),
1742 _string_list_length(macro->parameters));
1746 /* Perform argument substitution on the replacement list. */
1747 substituted = _token_list_create(parser);
1749 for (node = macro->replacements->head; node; node = node->next) {
1750 if (node->token->type == IDENTIFIER &&
1751 _string_list_contains(macro->parameters, node->token->value.str,
1752 ¶meter_index)) {
1753 token_list_t *argument;
1754 argument = _argument_list_member_at(arguments, parameter_index);
1755 /* Before substituting, we expand the argument tokens, or append a
1756 * placeholder token for an empty argument. */
1757 if (argument->head) {
1758 token_list_t *expanded_argument;
1759 expanded_argument = _token_list_copy(parser, argument);
1760 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1761 _token_list_append_list(substituted, expanded_argument);
1765 new_token = _token_create_ival(parser, PLACEHOLDER,
1767 _token_list_append(parser, substituted, new_token);
1770 _token_list_append(parser, substituted, node->token);
1774 /* After argument substitution, and before further expansion
1775 * below, implement token pasting. */
1777 _token_list_trim_trailing_space(substituted);
1779 _glcpp_parser_apply_pastes(parser, substituted);
1784 /* Compute the complete expansion of node, (and subsequent nodes after
1785 * 'node' in the case that 'node' is a function-like macro and
1786 * subsequent nodes are arguments).
1788 * Returns NULL if node is a simple token with no expansion.
1790 * Otherwise, returns the token list that results from the expansion
1791 * and sets *last to the last node in the list that was consumed by
1792 * the expansion. Specifically, *last will be set as follows:
1794 * As 'node' in the case of object-like macro expansion.
1796 * As the token of the closing right parenthesis in the case of
1797 * function-like macro expansion.
1799 * See the documentation of _glcpp_parser_expand_token_list for a description
1800 * of the "mode" parameter.
1802 static token_list_t *
1803 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1804 token_node_t **last, expansion_mode_t mode)
1806 token_t *token = node->token;
1807 const char *identifier;
1808 struct hash_entry *entry;
1811 /* We only expand identifiers */
1812 if (token->type != IDENTIFIER) {
1817 identifier = token->value.str;
1819 /* Special handling for __LINE__ and __FILE__, (not through
1820 * the hash table). */
1821 if (strcmp(identifier, "__LINE__") == 0)
1822 return _token_list_create_with_one_integer(parser, node->token->location.first_line);
1824 if (strcmp(identifier, "__FILE__") == 0)
1825 return _token_list_create_with_one_integer(parser, node->token->location.source);
1827 /* Look up this identifier in the hash table. */
1828 entry = _mesa_hash_table_search(parser->defines, identifier);
1829 macro = entry ? entry->data : NULL;
1831 /* Not a macro, so no expansion needed. */
1835 /* Finally, don't expand this macro if we're already actively
1836 * expanding it, (to avoid infinite recursion). */
1837 if (_parser_active_list_contains (parser, identifier)) {
1838 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1839 * future expansion of this unexpanded token. */
1841 token_list_t *expansion;
1844 str = linear_strdup(parser->linalloc, token->value.str);
1845 final = _token_create_str(parser, OTHER, str);
1846 expansion = _token_list_create(parser);
1847 _token_list_append(parser, expansion, final);
1851 if (! macro->is_function) {
1852 token_list_t *replacement;
1854 /* Replace a macro defined as empty with a SPACE token. */
1855 if (macro->replacements == NULL)
1856 return _token_list_create_with_one_space(parser);
1858 replacement = _token_list_copy(parser, macro->replacements);
1859 _glcpp_parser_apply_pastes(parser, replacement);
1863 return _glcpp_parser_expand_function(parser, node, last, mode);
1866 /* Push a new identifier onto the parser's active list.
1868 * Here, 'marker' is the token node that appears in the list after the
1869 * expansion of 'identifier'. That is, when the list iterator begins
1870 * examining 'marker', then it is time to pop this node from the
1874 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
1875 token_node_t *marker)
1877 active_list_t *node;
1879 node = linear_alloc_child(parser->linalloc, sizeof(active_list_t));
1880 node->identifier = linear_strdup(parser->linalloc, identifier);
1881 node->marker = marker;
1882 node->next = parser->active;
1884 parser->active = node;
1888 _parser_active_list_pop(glcpp_parser_t *parser)
1890 active_list_t *node = parser->active;
1893 parser->active = NULL;
1897 node = parser->active->next;
1898 parser->active = node;
1902 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
1904 active_list_t *node;
1906 if (parser->active == NULL)
1909 for (node = parser->active; node; node = node->next)
1910 if (strcmp(node->identifier, identifier) == 0)
1916 /* Walk over the token list replacing nodes with their expansion.
1917 * Whenever nodes are expanded the walking will walk over the new
1918 * nodes, continuing to expand as necessary. The results are placed in
1921 * The "mode" argument controls the handling of any DEFINED tokens that
1922 * result from expansion as follows:
1924 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
1925 * left in the final list, unevaluated. This is the correct mode
1926 * for expanding any list in any context other than a
1927 * preprocessor conditional, (#if or #elif).
1929 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
1930 * evaluated to 0 or 1 tokens depending on whether the following
1931 * token is the name of a defined macro. If the DEFINED token is
1932 * not followed by an (optionally parenthesized) identifier, then
1933 * an error will be generated. This the correct mode for
1934 * expanding any list in the context of a preprocessor
1935 * conditional, (#if or #elif).
1938 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
1939 expansion_mode_t mode)
1941 token_node_t *node_prev;
1942 token_node_t *node, *last = NULL;
1943 token_list_t *expansion;
1944 active_list_t *active_initial = parser->active;
1949 _token_list_trim_trailing_space (list);
1954 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
1955 _glcpp_parser_evaluate_defined_in_list (parser, list);
1959 while (parser->active && parser->active->marker == node)
1960 _parser_active_list_pop (parser);
1962 expansion = _glcpp_parser_expand_node (parser, node, &last, mode);
1966 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
1967 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
1970 for (n = node; n != last->next; n = n->next)
1971 while (parser->active && parser->active->marker == n) {
1972 _parser_active_list_pop (parser);
1975 _parser_active_list_push(parser, node->token->value.str, last->next);
1977 /* Splice expansion into list, supporting a simple deletion if the
1978 * expansion is empty.
1980 if (expansion->head) {
1982 node_prev->next = expansion->head;
1984 list->head = expansion->head;
1985 expansion->tail->next = last->next;
1986 if (last == list->tail)
1987 list->tail = expansion->tail;
1990 node_prev->next = last->next;
1992 list->head = last->next;
1993 if (last == list->tail)
1999 node = node_prev ? node_prev->next : list->head;
2002 /* Remove any lingering effects of this invocation on the
2003 * active list. That is, pop until the list looks like it did
2004 * at the beginning of this function. */
2005 while (parser->active && parser->active != active_initial)
2006 _parser_active_list_pop (parser);
2008 list->non_space_tail = list->tail;
2012 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
2018 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2020 _token_list_trim_trailing_space (list);
2022 _token_list_print (parser, list);
2026 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2027 const char *identifier)
2029 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2030 * the GLSL ES spec (all versions) say:
2032 * "All macro names containing two consecutive underscores ( __ )
2033 * are reserved for future use as predefined macro names. All
2034 * macro names prefixed with "GL_" ("GL" followed by a single
2035 * underscore) are also reserved."
2037 * The intention is that names containing __ are reserved for internal
2038 * use by the implementation, and names prefixed with GL_ are reserved
2039 * for use by Khronos. Since every extension adds a name prefixed
2040 * with GL_ (i.e., the name of the extension), that should be an
2041 * error. Names simply containing __ are dangerous to use, but should
2044 * A future version of the GLSL specification will clarify this.
2046 if (strstr(identifier, "__")) {
2047 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2048 "for use by the implementation.\n");
2050 if (strncmp(identifier, "GL_", 3) == 0) {
2051 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2053 if (strcmp(identifier, "defined") == 0) {
2054 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2059 _macro_equal(macro_t *a, macro_t *b)
2061 if (a->is_function != b->is_function)
2064 if (a->is_function) {
2065 if (! _string_list_equal (a->parameters, b->parameters))
2069 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2073 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2074 const char *identifier, token_list_t *replacements)
2076 macro_t *macro, *previous;
2077 struct hash_entry *entry;
2079 /* We define pre-defined macros before we've started parsing the actual
2080 * file. So if there's no location defined yet, that's what were doing and
2081 * we don't want to generate an error for using the reserved names. */
2083 _check_for_reserved_macro_name(parser, loc, identifier);
2085 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2087 macro->is_function = 0;
2088 macro->parameters = NULL;
2089 macro->identifier = linear_strdup(parser->linalloc, identifier);
2090 macro->replacements = replacements;
2092 entry = _mesa_hash_table_search(parser->defines, identifier);
2093 previous = entry ? entry->data : NULL;
2095 if (_macro_equal (macro, previous)) {
2098 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2101 _mesa_hash_table_insert (parser->defines, identifier, macro);
2105 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2106 const char *identifier, string_list_t *parameters,
2107 token_list_t *replacements)
2109 macro_t *macro, *previous;
2110 struct hash_entry *entry;
2113 _check_for_reserved_macro_name(parser, loc, identifier);
2115 /* Check for any duplicate parameter names. */
2116 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2117 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2120 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2122 macro->is_function = 1;
2123 macro->parameters = parameters;
2124 macro->identifier = linear_strdup(parser->linalloc, identifier);
2125 macro->replacements = replacements;
2127 entry = _mesa_hash_table_search(parser->defines, identifier);
2128 previous = entry ? entry->data : NULL;
2130 if (_macro_equal (macro, previous)) {
2133 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2136 _mesa_hash_table_insert(parser->defines, identifier, macro);
2140 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2145 if (parser->lex_from_list == NULL) {
2146 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2148 /* XXX: This ugly block of code exists for the sole
2149 * purpose of converting a NEWLINE token into a SPACE
2150 * token, but only in the case where we have seen a
2151 * function-like macro name, but have not yet seen its
2152 * closing parenthesis.
2154 * There's perhaps a more compact way to do this with
2155 * mid-rule actions in the grammar.
2157 * I'm definitely not pleased with the complexity of
2160 if (parser->newline_as_space) {
2162 parser->paren_count++;
2163 } else if (ret == ')') {
2164 parser->paren_count--;
2165 if (parser->paren_count == 0)
2166 parser->newline_as_space = 0;
2167 } else if (ret == NEWLINE) {
2169 } else if (ret != SPACE) {
2170 if (parser->paren_count == 0)
2171 parser->newline_as_space = 0;
2173 } else if (parser->in_control_line) {
2175 parser->in_control_line = 0;
2177 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2178 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2179 ret == ENDIF || ret == HASH_TOKEN) {
2180 parser->in_control_line = 1;
2181 } else if (ret == IDENTIFIER) {
2182 struct hash_entry *entry = _mesa_hash_table_search(parser->defines,
2184 macro_t *macro = entry ? entry->data : NULL;
2185 if (macro && macro->is_function) {
2186 parser->newline_as_space = 1;
2187 parser->paren_count = 0;
2194 node = parser->lex_from_node;
2197 parser->lex_from_list = NULL;
2201 *yylval = node->token->value;
2202 ret = node->token->type;
2204 parser->lex_from_node = node->next;
2210 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2214 assert (parser->lex_from_list == NULL);
2216 /* Copy list, eliminating any space tokens. */
2217 parser->lex_from_list = _token_list_create (parser);
2219 for (node = list->head; node; node = node->next) {
2220 if (node->token->type == SPACE)
2222 _token_list_append (parser, parser->lex_from_list, node->token);
2225 parser->lex_from_node = parser->lex_from_list->head;
2227 /* It's possible the list consisted of nothing but whitespace. */
2228 if (parser->lex_from_node == NULL) {
2229 parser->lex_from_list = NULL;
2234 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2237 skip_type_t current = SKIP_NO_SKIP;
2240 if (parser->skip_stack)
2241 current = parser->skip_stack->type;
2243 node = linear_alloc_child(parser->linalloc, sizeof(skip_node_t));
2246 if (current == SKIP_NO_SKIP) {
2248 node->type = SKIP_NO_SKIP;
2250 node->type = SKIP_TO_ELSE;
2252 node->type = SKIP_TO_ENDIF;
2255 node->has_else = false;
2256 node->next = parser->skip_stack;
2257 parser->skip_stack = node;
2261 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2262 const char *type, int condition)
2264 if (parser->skip_stack == NULL) {
2265 glcpp_error (loc, parser, "#%s without #if\n", type);
2269 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2271 parser->skip_stack->type = SKIP_NO_SKIP;
2273 parser->skip_stack->type = SKIP_TO_ENDIF;
2278 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2282 if (parser->skip_stack == NULL) {
2283 glcpp_error (loc, parser, "#endif without #if\n");
2287 node = parser->skip_stack;
2288 parser->skip_stack = node->next;
2292 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2293 const char *es_identifier,
2294 bool explicitly_set)
2296 if (parser->version != 0)
2299 parser->version = version;
2301 add_builtin_define (parser, "__VERSION__", version);
2303 parser->is_gles = (version == 100) ||
2304 (es_identifier && (strcmp(es_identifier, "es") == 0));
2306 /* Add pre-defined macros. */
2307 if (parser->is_gles)
2308 add_builtin_define(parser, "GL_ES", 1);
2309 else if (version >= 150)
2310 add_builtin_define(parser, "GL_core_profile", 1);
2312 /* Currently, all ES2/ES3 implementations support highp in the
2313 * fragment shader, so we always define this macro in ES2/ES3.
2314 * If we ever get a driver that doesn't support highp, we'll
2315 * need to add a flag to the gl_context and check that here.
2317 if (version >= 130 || parser->is_gles)
2318 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2320 /* Add all the extension macros available in this context */
2321 if (parser->extensions)
2322 parser->extensions(parser->state, add_builtin_define, parser,
2323 version, parser->is_gles);
2325 if (explicitly_set) {
2326 ralloc_asprintf_rewrite_tail(&parser->output, &parser->output_length,
2327 "#version %" PRIiMAX "%s%s", version,
2328 es_identifier ? " " : "",
2329 es_identifier ? es_identifier : "");
2333 /* GLSL version if no version is explicitly specified. */
2334 #define IMPLICIT_GLSL_VERSION 110
2336 /* GLSL ES version if no version is explicitly specified. */
2337 #define IMPLICIT_GLSL_ES_VERSION 100
2340 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2342 int language_version = parser->api == API_OPENGLES2 ?
2343 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2345 _glcpp_parser_handle_version_declaration(parser, language_version,