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/mtypes.h"
33 #include "util/strndup.h"
36 _mesa_lookup_shader_include(struct gl_context *ctx, char *path,
40 _mesa_get_shader_include_cursor(struct gl_shared_state *shared);
43 _mesa_set_shader_include_cursor(struct gl_shared_state *shared, size_t cursor);
46 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
49 _define_object_macro(glcpp_parser_t *parser,
52 token_list_t *replacements);
55 _define_function_macro(glcpp_parser_t *parser,
58 string_list_t *parameters,
59 token_list_t *replacements);
61 static string_list_t *
62 _string_list_create(glcpp_parser_t *parser);
65 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
69 _string_list_contains(string_list_t *list, const char *member, int *index);
72 _string_list_has_duplicate(string_list_t *list);
75 _string_list_length(string_list_t *list);
78 _string_list_equal(string_list_t *a, string_list_t *b);
80 static argument_list_t *
81 _argument_list_create(glcpp_parser_t *parser);
84 _argument_list_append(glcpp_parser_t *parser, argument_list_t *list,
85 token_list_t *argument);
88 _argument_list_length(argument_list_t *list);
91 _argument_list_member_at(argument_list_t *list, int index);
94 _token_create_str(glcpp_parser_t *parser, int type, char *str);
97 _token_create_ival(glcpp_parser_t *parser, int type, int ival);
100 _token_list_create(glcpp_parser_t *parser);
103 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token);
106 _token_list_append_list(token_list_t *list, token_list_t *tail);
109 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b);
112 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
113 token_node_t *marker);
116 _parser_active_list_pop(glcpp_parser_t *parser);
119 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier);
122 EXPANSION_MODE_IGNORE_DEFINED,
123 EXPANSION_MODE_EVALUATE_DEFINED
126 /* Expand list, and begin lexing from the result (after first
127 * prefixing a token of type 'head_token_type').
130 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
131 token_list_t *list, expansion_mode_t mode);
133 /* Perform macro expansion in-place on the given list. */
135 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
136 expansion_mode_t mode);
139 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
143 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
147 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
148 const char *type, int condition);
151 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc);
154 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
155 const char *ident, bool explicitly_set);
158 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
161 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list);
163 struct define_include {
164 glcpp_parser_t *parser;
169 glcpp_parser_copy_defines(const void *key, void *data, void *closure);
172 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
188 %parse-param {glcpp_parser_t *parser}
189 %lex-param {glcpp_parser_t *parser}
193 /* We use HASH_TOKEN, DEFINE_TOKEN and VERSION_TOKEN (as opposed to
194 * HASH, DEFINE, and VERSION) to avoid conflicts with other symbols,
195 * (such as the <HASH> and <DEFINE> start conditions in the lexer). */
196 %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 PATH INCLUDE
198 %type <ival> INTEGER operator SPACE integer_constant version_constant
199 %type <expression_value> expression
200 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA PATH INCLUDE
201 %type <string_list> identifier_list
202 %type <token> preprocessing_token
203 %type <token_list> pp_tokens replacement_list text_line
209 %left EQUAL NOT_EQUAL
210 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
211 %left LEFT_SHIFT RIGHT_SHIFT
229 _glcpp_parser_print_expanded_token_list (parser, $1);
230 _mesa_string_buffer_append_char(parser->output, '\n');
236 IF_EXPANDED expression NEWLINE {
237 if (parser->is_gles && $2.undefined_macro)
238 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
239 _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
241 | ELIF_EXPANDED expression NEWLINE {
242 if (parser->is_gles && $2.undefined_macro)
243 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
244 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
246 | LINE_EXPANDED integer_constant NEWLINE {
247 parser->has_new_line_number = 1;
248 parser->new_line_number = $2;
249 _mesa_string_buffer_printf(parser->output, "#line %" PRIiMAX "\n", $2);
251 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
252 parser->has_new_line_number = 1;
253 parser->new_line_number = $2;
254 parser->has_new_source_number = 1;
255 parser->new_source_number = $3;
256 _mesa_string_buffer_printf(parser->output,
257 "#line %" PRIiMAX " %" PRIiMAX "\n",
260 | LINE_EXPANDED integer_constant PATH NEWLINE {
261 parser->has_new_line_number = 1;
262 parser->new_line_number = $2;
263 _mesa_string_buffer_printf(parser->output,
264 "#line %" PRIiMAX " %s\n",
270 OBJ_IDENTIFIER replacement_list NEWLINE {
271 _define_object_macro (parser, & @1, $1, $2);
273 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
274 _define_function_macro (parser, & @1, $1, NULL, $4);
276 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
277 _define_function_macro (parser, & @1, $1, $3, $5);
282 control_line_success {
283 _mesa_string_buffer_append_char(parser->output, '\n');
286 | HASH_TOKEN LINE pp_tokens NEWLINE {
288 if (parser->skip_stack == NULL ||
289 parser->skip_stack->type == SKIP_NO_SKIP)
291 _glcpp_parser_expand_and_lex_from (parser,
293 EXPANSION_MODE_IGNORE_DEFINED);
298 control_line_success:
299 HASH_TOKEN DEFINE_TOKEN define
300 | HASH_TOKEN UNDEF IDENTIFIER NEWLINE {
301 struct hash_entry *entry;
303 /* Section 3.4 (Preprocessor) of the GLSL ES 3.00 spec says:
305 * It is an error to undefine or to redefine a built-in
306 * (pre-defined) macro name.
308 * The GLSL ES 1.00 spec does not contain this text, but
309 * dEQP's preprocess test in GLES2 checks for it.
311 * Section 3.3 (Preprocessor) revision 7, of the GLSL 4.50
314 * By convention, all macro names containing two consecutive
315 * underscores ( __ ) are reserved for use by underlying
316 * software layers. Defining or undefining such a name
317 * in a shader does not itself result in an error, but may
318 * result in unintended behaviors that stem from having
319 * multiple definitions of the same name. All macro names
320 * prefixed with "GL_" (...) are also reseved, and defining
321 * such a name results in a compile-time error.
323 * The code below implements the same checks as GLSLang.
325 if (strncmp("GL_", $3, 3) == 0)
326 glcpp_error(& @1, parser, "Built-in (pre-defined)"
327 " names beginning with GL_ cannot be undefined.");
328 else if (strstr($3, "__") != NULL) {
330 && parser->version >= 300
331 && (strcmp("__LINE__", $3) == 0
332 || strcmp("__FILE__", $3) == 0
333 || strcmp("__VERSION__", $3) == 0)) {
334 glcpp_error(& @1, parser, "Built-in (pre-defined)"
335 " names cannot be undefined.");
336 } else if (parser->is_gles && parser->version <= 300) {
337 glcpp_error(& @1, parser,
338 " names containing consecutive underscores"
341 glcpp_warning(& @1, parser,
342 " names containing consecutive underscores"
347 entry = _mesa_hash_table_search (parser->defines, $3);
349 _mesa_hash_table_remove (parser->defines, entry);
352 | HASH_TOKEN INCLUDE NEWLINE {
353 size_t include_cursor = _mesa_get_shader_include_cursor(parser->gl_ctx->Shared);
355 /* Remove leading and trailing "" or <> */
356 char *start = strchr($2, '"');
358 _mesa_set_shader_include_cursor(parser->gl_ctx->Shared, 0);
359 start = strchr($2, '<');
361 char *path = strndup(start + 1, strlen(start + 1) - 1);
364 _mesa_lookup_shader_include(parser->gl_ctx, path, false);
368 glcpp_error(&@1, parser, "%s not found", $2);
370 /* Create a temporary parser with the same settings */
371 glcpp_parser_t *tmp_parser =
372 glcpp_parser_create(parser->gl_ctx, parser->extensions, parser->state);
373 tmp_parser->version_set = true;
374 tmp_parser->version = parser->version;
376 /* Set the shader source and run the lexer */
377 glcpp_lex_set_source_string(tmp_parser, shader);
379 /* Copy any existing define macros to the temporary
380 * shade include parser.
382 struct define_include di;
383 di.parser = tmp_parser;
386 hash_table_call_foreach(parser->defines,
387 glcpp_parser_copy_defines,
390 /* Print out '#include' to the glsl parser. We do this
391 * so that it can do the error checking require to
392 * make sure the ARB_shading_language_include
393 * extension is enabled.
395 _mesa_string_buffer_printf(parser->output, "#include\n");
397 /* Parse the include string before adding to the
398 * preprocessor output.
400 glcpp_parser_parse(tmp_parser);
401 _mesa_string_buffer_printf(parser->info_log, "%s",
402 tmp_parser->info_log->buf);
403 _mesa_string_buffer_printf(parser->output, "%s",
404 tmp_parser->output->buf);
406 /* Copy any new define macros to the parent parser
407 * and steal the memory of our temp parser so we don't
408 * free these new defines before they are no longer
413 ralloc_steal(parser, tmp_parser);
415 hash_table_call_foreach(tmp_parser->defines,
416 glcpp_parser_copy_defines,
419 /* Destroy tmp parser memory we no longer need */
420 glcpp_lex_destroy(tmp_parser->scanner);
421 _mesa_hash_table_destroy(tmp_parser->defines, NULL);
424 _mesa_set_shader_include_cursor(parser->gl_ctx->Shared, include_cursor);
426 | HASH_TOKEN IF pp_tokens NEWLINE {
427 /* Be careful to only evaluate the 'if' expression if
428 * we are not skipping. When we are skipping, we
429 * simply push a new 0-valued 'if' onto the skip
432 * This avoids generating diagnostics for invalid
433 * expressions that are being skipped. */
434 if (parser->skip_stack == NULL ||
435 parser->skip_stack->type == SKIP_NO_SKIP)
437 _glcpp_parser_expand_and_lex_from (parser,
439 EXPANSION_MODE_EVALUATE_DEFINED);
443 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
444 parser->skip_stack->type = SKIP_TO_ENDIF;
447 | HASH_TOKEN IF NEWLINE {
448 /* #if without an expression is only an error if we
449 * are not skipping */
450 if (parser->skip_stack == NULL ||
451 parser->skip_stack->type == SKIP_NO_SKIP)
453 glcpp_error(& @1, parser, "#if with no expression");
455 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
457 | HASH_TOKEN IFDEF IDENTIFIER junk NEWLINE {
458 struct hash_entry *entry =
459 _mesa_hash_table_search(parser->defines, $3);
460 macro_t *macro = entry ? entry->data : NULL;
461 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
463 | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE {
464 struct hash_entry *entry =
465 _mesa_hash_table_search(parser->defines, $3);
466 macro_t *macro = entry ? entry->data : NULL;
467 _glcpp_parser_skip_stack_push_if (parser, & @3, macro == NULL);
469 | HASH_TOKEN ELIF pp_tokens NEWLINE {
470 /* Be careful to only evaluate the 'elif' expression
471 * if we are not skipping. When we are skipping, we
472 * simply change to a 0-valued 'elif' on the skip
475 * This avoids generating diagnostics for invalid
476 * expressions that are being skipped. */
477 if (parser->skip_stack &&
478 parser->skip_stack->type == SKIP_TO_ELSE)
480 _glcpp_parser_expand_and_lex_from (parser,
482 EXPANSION_MODE_EVALUATE_DEFINED);
484 else if (parser->skip_stack &&
485 parser->skip_stack->has_else)
487 glcpp_error(& @1, parser, "#elif after #else");
491 _glcpp_parser_skip_stack_change_if (parser, & @1,
495 | HASH_TOKEN ELIF NEWLINE {
496 /* #elif without an expression is an error unless we
498 if (parser->skip_stack &&
499 parser->skip_stack->type == SKIP_TO_ELSE)
501 glcpp_error(& @1, parser, "#elif with no expression");
503 else if (parser->skip_stack &&
504 parser->skip_stack->has_else)
506 glcpp_error(& @1, parser, "#elif after #else");
510 _glcpp_parser_skip_stack_change_if (parser, & @1,
512 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
515 | HASH_TOKEN ELSE { parser->lexing_directive = 1; } NEWLINE {
516 if (parser->skip_stack &&
517 parser->skip_stack->has_else)
519 glcpp_error(& @1, parser, "multiple #else");
523 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
524 if (parser->skip_stack)
525 parser->skip_stack->has_else = true;
529 _glcpp_parser_skip_stack_pop (parser, & @1);
531 | HASH_TOKEN VERSION_TOKEN version_constant NEWLINE {
532 if (parser->version_set) {
533 glcpp_error(& @1, parser, "#version must appear on the first line");
535 _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
537 | HASH_TOKEN VERSION_TOKEN version_constant IDENTIFIER NEWLINE {
538 if (parser->version_set) {
539 glcpp_error(& @1, parser, "#version must appear on the first line");
541 _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
543 | HASH_TOKEN NEWLINE {
544 glcpp_parser_resolve_implicit_version(parser);
546 | HASH_TOKEN PRAGMA NEWLINE {
547 _mesa_string_buffer_printf(parser->output, "#%s", $2);
552 HASH_TOKEN ERROR_TOKEN NEWLINE {
553 glcpp_error(& @1, parser, "#%s", $2);
555 | HASH_TOKEN DEFINE_TOKEN NEWLINE {
556 glcpp_error (& @1, parser, "#define without macro name");
558 | HASH_TOKEN GARBAGE pp_tokens NEWLINE {
559 glcpp_error (& @1, parser, "Illegal non-directive after #");
565 /* let strtoll detect the base */
566 $$ = strtoll ($1, NULL, 0);
574 /* Both octal and hexadecimal constants begin with 0. */
575 if ($1[0] == '0' && $1[1] != '\0') {
576 glcpp_error(&@1, parser, "invalid #version \"%s\" (not a decimal constant)", $1);
579 $$ = strtoll($1, NULL, 10);
586 $$.undefined_macro = NULL;
591 $$.undefined_macro = linear_strdup(parser->linalloc, $1);
593 $$.undefined_macro = NULL;
595 | expression OR expression {
596 $$.value = $1.value || $3.value;
598 /* Short-circuit: Only flag undefined from right side
599 * if left side evaluates to false.
601 if ($1.undefined_macro)
602 $$.undefined_macro = $1.undefined_macro;
604 $$.undefined_macro = $3.undefined_macro;
606 | expression AND expression {
607 $$.value = $1.value && $3.value;
609 /* Short-circuit: Only flag undefined from right-side
610 * if left side evaluates to true.
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 '^' expression {
625 $$.value = $1.value ^ $3.value;
626 if ($1.undefined_macro)
627 $$.undefined_macro = $1.undefined_macro;
629 $$.undefined_macro = $3.undefined_macro;
631 | expression '&' expression {
632 $$.value = $1.value & $3.value;
633 if ($1.undefined_macro)
634 $$.undefined_macro = $1.undefined_macro;
636 $$.undefined_macro = $3.undefined_macro;
638 | expression NOT_EQUAL expression {
639 $$.value = $1.value != $3.value;
640 if ($1.undefined_macro)
641 $$.undefined_macro = $1.undefined_macro;
643 $$.undefined_macro = $3.undefined_macro;
645 | expression EQUAL expression {
646 $$.value = $1.value == $3.value;
647 if ($1.undefined_macro)
648 $$.undefined_macro = $1.undefined_macro;
650 $$.undefined_macro = $3.undefined_macro;
652 | expression GREATER_OR_EQUAL expression {
653 $$.value = $1.value >= $3.value;
654 if ($1.undefined_macro)
655 $$.undefined_macro = $1.undefined_macro;
657 $$.undefined_macro = $3.undefined_macro;
659 | expression LESS_OR_EQUAL expression {
660 $$.value = $1.value <= $3.value;
661 if ($1.undefined_macro)
662 $$.undefined_macro = $1.undefined_macro;
664 $$.undefined_macro = $3.undefined_macro;
666 | expression '>' expression {
667 $$.value = $1.value > $3.value;
668 if ($1.undefined_macro)
669 $$.undefined_macro = $1.undefined_macro;
671 $$.undefined_macro = $3.undefined_macro;
673 | expression '<' expression {
674 $$.value = $1.value < $3.value;
675 if ($1.undefined_macro)
676 $$.undefined_macro = $1.undefined_macro;
678 $$.undefined_macro = $3.undefined_macro;
680 | expression RIGHT_SHIFT expression {
681 $$.value = $1.value >> $3.value;
682 if ($1.undefined_macro)
683 $$.undefined_macro = $1.undefined_macro;
685 $$.undefined_macro = $3.undefined_macro;
687 | expression LEFT_SHIFT expression {
688 $$.value = $1.value << $3.value;
689 if ($1.undefined_macro)
690 $$.undefined_macro = $1.undefined_macro;
692 $$.undefined_macro = $3.undefined_macro;
694 | expression '-' expression {
695 $$.value = $1.value - $3.value;
696 if ($1.undefined_macro)
697 $$.undefined_macro = $1.undefined_macro;
699 $$.undefined_macro = $3.undefined_macro;
701 | expression '+' expression {
702 $$.value = $1.value + $3.value;
703 if ($1.undefined_macro)
704 $$.undefined_macro = $1.undefined_macro;
706 $$.undefined_macro = $3.undefined_macro;
708 | expression '%' expression {
710 yyerror (& @1, parser,
711 "zero modulus in preprocessor directive");
713 $$.value = $1.value % $3.value;
715 if ($1.undefined_macro)
716 $$.undefined_macro = $1.undefined_macro;
718 $$.undefined_macro = $3.undefined_macro;
720 | expression '/' expression {
722 yyerror (& @1, parser,
723 "division by 0 in preprocessor directive");
725 $$.value = $1.value / $3.value;
727 if ($1.undefined_macro)
728 $$.undefined_macro = $1.undefined_macro;
730 $$.undefined_macro = $3.undefined_macro;
732 | expression '*' expression {
733 $$.value = $1.value * $3.value;
734 if ($1.undefined_macro)
735 $$.undefined_macro = $1.undefined_macro;
737 $$.undefined_macro = $3.undefined_macro;
739 | '!' expression %prec UNARY {
740 $$.value = ! $2.value;
741 $$.undefined_macro = $2.undefined_macro;
743 | '~' expression %prec UNARY {
744 $$.value = ~ $2.value;
745 $$.undefined_macro = $2.undefined_macro;
747 | '-' expression %prec UNARY {
748 $$.value = - $2.value;
749 $$.undefined_macro = $2.undefined_macro;
751 | '+' expression %prec UNARY {
752 $$.value = + $2.value;
753 $$.undefined_macro = $2.undefined_macro;
755 | '(' expression ')' {
762 $$ = _string_list_create (parser);
763 _string_list_append_item (parser, $$, $1);
765 | identifier_list ',' IDENTIFIER {
767 _string_list_append_item (parser, $$, $3);
772 NEWLINE { $$ = NULL; }
777 /* empty */ { $$ = NULL; }
784 glcpp_error(&@1, parser, "extra tokens at end of directive");
789 preprocessing_token {
790 parser->space_tokens = 1;
791 $$ = _token_list_create (parser);
792 _token_list_append (parser, $$, $1);
794 | pp_tokens preprocessing_token {
796 _token_list_append (parser, $$, $2);
802 $$ = _token_create_str (parser, IDENTIFIER, $1);
803 $$->location = yylloc;
806 $$ = _token_create_str (parser, INTEGER_STRING, $1);
807 $$->location = yylloc;
810 $$ = _token_create_str (parser, PATH, $1);
811 $$->location = yylloc;
814 $$ = _token_create_ival (parser, $1, $1);
815 $$->location = yylloc;
818 $$ = _token_create_ival (parser, DEFINED, DEFINED);
819 $$->location = yylloc;
822 $$ = _token_create_str (parser, OTHER, $1);
823 $$->location = yylloc;
826 $$ = _token_create_ival (parser, SPACE, SPACE);
827 $$->location = yylloc;
847 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
848 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
851 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
852 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
853 | EQUAL { $$ = EQUAL; }
854 | NOT_EQUAL { $$ = NOT_EQUAL; }
862 | PASTE { $$ = PASTE; }
863 | PLUS_PLUS { $$ = PLUS_PLUS; }
864 | MINUS_MINUS { $$ = MINUS_MINUS; }
870 _string_list_create(glcpp_parser_t *parser)
874 list = linear_alloc_child(parser->linalloc, sizeof(string_list_t));
882 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
887 node = linear_alloc_child(parser->linalloc, sizeof(string_node_t));
888 node->str = linear_strdup(parser->linalloc, str);
892 if (list->head == NULL) {
895 list->tail->next = node;
902 _string_list_contains(string_list_t *list, const char *member, int *index)
910 for (i = 0, node = list->head; node; i++, node = node->next) {
911 if (strcmp (node->str, member) == 0) {
921 /* Return duplicate string in list (if any), NULL otherwise. */
923 _string_list_has_duplicate(string_list_t *list)
925 string_node_t *node, *dup;
930 for (node = list->head; node; node = node->next) {
931 for (dup = node->next; dup; dup = dup->next) {
932 if (strcmp (node->str, dup->str) == 0)
941 _string_list_length(string_list_t *list)
949 for (node = list->head; node; node = node->next)
956 _string_list_equal(string_list_t *a, string_list_t *b)
958 string_node_t *node_a, *node_b;
960 if (a == NULL && b == NULL)
963 if (a == NULL || b == NULL)
966 for (node_a = a->head, node_b = b->head;
968 node_a = node_a->next, node_b = node_b->next)
970 if (strcmp (node_a->str, node_b->str))
974 /* Catch the case of lists being different lengths, (which
975 * would cause the loop above to terminate after the shorter
977 return node_a == node_b;
981 _argument_list_create(glcpp_parser_t *parser)
983 argument_list_t *list;
985 list = linear_alloc_child(parser->linalloc, sizeof(argument_list_t));
993 _argument_list_append(glcpp_parser_t *parser,
994 argument_list_t *list, token_list_t *argument)
996 argument_node_t *node;
998 node = linear_alloc_child(parser->linalloc, sizeof(argument_node_t));
999 node->argument = argument;
1003 if (list->head == NULL) {
1006 list->tail->next = node;
1013 _argument_list_length(argument_list_t *list)
1016 argument_node_t *node;
1021 for (node = list->head; node; node = node->next)
1028 _argument_list_member_at(argument_list_t *list, int index)
1030 argument_node_t *node;
1037 for (i = 0; i < index; i++) {
1044 return node->argument;
1050 _token_create_str(glcpp_parser_t *parser, int type, char *str)
1054 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
1056 token->value.str = str;
1062 _token_create_ival(glcpp_parser_t *parser, int type, int ival)
1066 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
1068 token->value.ival = ival;
1074 _token_list_create(glcpp_parser_t *parser)
1078 list = linear_alloc_child(parser->linalloc, sizeof(token_list_t));
1081 list->non_space_tail = NULL;
1087 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token)
1091 node = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
1092 node->token = token;
1095 if (list->head == NULL) {
1098 list->tail->next = node;
1102 if (token->type != SPACE)
1103 list->non_space_tail = node;
1107 _token_list_append_list(token_list_t *list, token_list_t *tail)
1109 if (tail == NULL || tail->head == NULL)
1112 if (list->head == NULL) {
1113 list->head = tail->head;
1115 list->tail->next = tail->head;
1118 list->tail = tail->tail;
1119 list->non_space_tail = tail->non_space_tail;
1122 static token_list_t *
1123 _token_list_copy(glcpp_parser_t *parser, token_list_t *other)
1131 copy = _token_list_create (parser);
1132 for (node = other->head; node; node = node->next) {
1133 token_t *new_token = linear_alloc_child(parser->linalloc, sizeof(token_t));
1134 *new_token = *node->token;
1135 _token_list_append (parser, copy, new_token);
1142 _token_list_trim_trailing_space(token_list_t *list)
1144 if (list->non_space_tail) {
1145 list->non_space_tail->next = NULL;
1146 list->tail = list->non_space_tail;
1151 _token_list_is_empty_ignoring_space(token_list_t *l)
1159 while (n != NULL && n->token->type == SPACE)
1166 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1168 token_node_t *node_a, *node_b;
1170 if (a == NULL || b == NULL) {
1171 int a_empty = _token_list_is_empty_ignoring_space(a);
1172 int b_empty = _token_list_is_empty_ignoring_space(b);
1173 return a_empty == b_empty;
1181 if (node_a == NULL && node_b == NULL)
1184 /* Ignore trailing whitespace */
1185 if (node_a == NULL && node_b->token->type == SPACE) {
1186 while (node_b && node_b->token->type == SPACE)
1187 node_b = node_b->next;
1190 if (node_a == NULL && node_b == NULL)
1193 if (node_b == NULL && node_a->token->type == SPACE) {
1194 while (node_a && node_a->token->type == SPACE)
1195 node_a = node_a->next;
1198 if (node_a == NULL && node_b == NULL)
1201 if (node_a == NULL || node_b == NULL)
1203 /* Make sure whitespace appears in the same places in both.
1204 * It need not be exactly the same amount of whitespace,
1207 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1208 while (node_a && node_a->token->type == SPACE)
1209 node_a = node_a->next;
1210 while (node_b && node_b->token->type == SPACE)
1211 node_b = node_b->next;
1215 if (node_a->token->type != node_b->token->type)
1218 switch (node_a->token->type) {
1220 if (node_a->token->value.ival != node_b->token->value.ival) {
1225 case INTEGER_STRING:
1227 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1233 node_a = node_a->next;
1234 node_b = node_b->next;
1241 _token_print(struct _mesa_string_buffer *out, token_t *token)
1243 if (token->type < 256) {
1244 _mesa_string_buffer_append_char(out, token->type);
1248 switch (token->type) {
1250 _mesa_string_buffer_printf(out, "%" PRIiMAX, token->value.ival);
1253 case INTEGER_STRING:
1256 _mesa_string_buffer_append(out, token->value.str);
1259 _mesa_string_buffer_append_char(out, ' ');
1262 _mesa_string_buffer_append(out, "<<");
1265 _mesa_string_buffer_append(out, ">>");
1268 _mesa_string_buffer_append(out, "<=");
1270 case GREATER_OR_EQUAL:
1271 _mesa_string_buffer_append(out, ">=");
1274 _mesa_string_buffer_append(out, "==");
1277 _mesa_string_buffer_append(out, "!=");
1280 _mesa_string_buffer_append(out, "&&");
1283 _mesa_string_buffer_append(out, "||");
1286 _mesa_string_buffer_append(out, "##");
1289 _mesa_string_buffer_append(out, "++");
1292 _mesa_string_buffer_append(out, "--");
1295 _mesa_string_buffer_append(out, "defined");
1298 /* Nothing to print. */
1301 assert(!"Error: Don't know how to print token.");
1307 /* Return a new token formed by pasting 'token' and 'other'. Note that this
1308 * function may return 'token' or 'other' directly rather than allocating
1311 * Caution: Only very cursory error-checking is performed to see if
1312 * the final result is a valid single token. */
1314 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1316 token_t *combined = NULL;
1318 /* Pasting a placeholder onto anything makes no change. */
1319 if (other->type == PLACEHOLDER)
1322 /* When 'token' is a placeholder, just return 'other'. */
1323 if (token->type == PLACEHOLDER)
1326 /* A very few single-character punctuators can be combined
1327 * with another to form a multi-character punctuator. */
1328 switch (token->type) {
1330 if (other->type == '<')
1331 combined = _token_create_ival (parser, LEFT_SHIFT, LEFT_SHIFT);
1332 else if (other->type == '=')
1333 combined = _token_create_ival (parser, LESS_OR_EQUAL, LESS_OR_EQUAL);
1336 if (other->type == '>')
1337 combined = _token_create_ival (parser, RIGHT_SHIFT, RIGHT_SHIFT);
1338 else if (other->type == '=')
1339 combined = _token_create_ival (parser, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1342 if (other->type == '=')
1343 combined = _token_create_ival (parser, EQUAL, EQUAL);
1346 if (other->type == '=')
1347 combined = _token_create_ival (parser, NOT_EQUAL, NOT_EQUAL);
1350 if (other->type == '&')
1351 combined = _token_create_ival (parser, AND, AND);
1354 if (other->type == '|')
1355 combined = _token_create_ival (parser, OR, OR);
1359 if (combined != NULL) {
1360 /* Inherit the location from the first token */
1361 combined->location = token->location;
1365 /* Two string-valued (or integer) tokens can usually just be
1366 * mashed together. (We also handle a string followed by an
1367 * integer here as well.)
1369 * There are some exceptions here. Notably, if the first token
1370 * is an integer (or a string representing an integer), then
1371 * the second token must also be an integer or must be a
1372 * string representing an integer that begins with a digit.
1374 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1375 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1380 /* Check that pasting onto an integer doesn't create a
1381 * non-integer, (that is, only digits can be
1383 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1384 switch (other->type) {
1385 case INTEGER_STRING:
1386 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1390 if (other->value.ival < 0)
1398 if (token->type == INTEGER)
1399 str = linear_asprintf(parser->linalloc, "%" PRIiMAX, token->value.ival);
1401 str = linear_strdup(parser->linalloc, token->value.str);
1403 if (other->type == INTEGER)
1404 linear_asprintf_append(parser->linalloc, &str, "%" PRIiMAX, other->value.ival);
1406 linear_strcat(parser->linalloc, &str, other->value.str);
1408 /* New token is same type as original token, unless we
1409 * started with an integer, in which case we will be
1410 * creating an integer-string. */
1411 combined_type = token->type;
1412 if (combined_type == INTEGER)
1413 combined_type = INTEGER_STRING;
1415 combined = _token_create_str (parser, combined_type, str);
1416 combined->location = token->location;
1421 glcpp_error (&token->location, parser, "");
1422 _mesa_string_buffer_append(parser->info_log, "Pasting \"");
1423 _token_print(parser->info_log, token);
1424 _mesa_string_buffer_append(parser->info_log, "\" and \"");
1425 _token_print(parser->info_log, other);
1426 _mesa_string_buffer_append(parser->info_log, "\" does not give a valid preprocessing token.\n");
1432 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1439 for (node = list->head; node; node = node->next)
1440 _token_print(parser->output, node->token);
1444 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1446 glcpp_error(locp, parser, "%s", error);
1450 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1455 tok = _token_create_ival (parser, INTEGER, value);
1457 list = _token_list_create(parser);
1458 _token_list_append(parser, list, tok);
1459 _define_object_macro(parser, NULL, name, list);
1462 /* Initial output buffer size, 4096 minus ralloc() overhead. It was selected
1463 * to minimize total amount of allocated memory during shader-db run.
1465 #define INITIAL_PP_OUTPUT_BUF_SIZE 4048
1468 glcpp_parser_create(struct gl_context *gl_ctx,
1469 glcpp_extension_iterator extensions, void *state)
1471 glcpp_parser_t *parser;
1473 parser = ralloc (NULL, glcpp_parser_t);
1475 glcpp_lex_init_extra (parser, &parser->scanner);
1476 parser->defines = _mesa_hash_table_create(NULL, _mesa_hash_string,
1477 _mesa_key_string_equal);
1478 parser->linalloc = linear_alloc_parent(parser, 0);
1479 parser->active = NULL;
1480 parser->lexing_directive = 0;
1481 parser->lexing_version_directive = 0;
1482 parser->space_tokens = 1;
1483 parser->last_token_was_newline = 0;
1484 parser->last_token_was_space = 0;
1485 parser->first_non_space_token_this_line = 1;
1486 parser->newline_as_space = 0;
1487 parser->in_control_line = 0;
1488 parser->paren_count = 0;
1489 parser->commented_newlines = 0;
1491 parser->skip_stack = NULL;
1492 parser->skipping = 0;
1494 parser->lex_from_list = NULL;
1495 parser->lex_from_node = NULL;
1497 parser->output = _mesa_string_buffer_create(parser,
1498 INITIAL_PP_OUTPUT_BUF_SIZE);
1499 parser->info_log = _mesa_string_buffer_create(parser,
1500 INITIAL_PP_OUTPUT_BUF_SIZE);
1503 parser->gl_ctx = gl_ctx;
1504 parser->extensions = extensions;
1505 parser->extension_list = &gl_ctx->Extensions;
1506 parser->state = state;
1507 parser->api = gl_ctx->API;
1508 parser->version = 0;
1509 parser->version_set = false;
1511 parser->has_new_line_number = 0;
1512 parser->new_line_number = 1;
1513 parser->has_new_source_number = 0;
1514 parser->new_source_number = 0;
1516 parser->is_gles = false;
1522 glcpp_parser_destroy(glcpp_parser_t *parser)
1524 glcpp_lex_destroy (parser->scanner);
1525 _mesa_hash_table_destroy(parser->defines, NULL);
1526 ralloc_free (parser);
1529 typedef enum function_status
1531 FUNCTION_STATUS_SUCCESS,
1532 FUNCTION_NOT_A_FUNCTION,
1533 FUNCTION_UNBALANCED_PARENTHESES
1534 } function_status_t;
1536 /* Find a set of function-like macro arguments by looking for a
1537 * balanced set of parentheses.
1539 * When called, 'node' should be the opening-parenthesis token, (or
1540 * perhaps preceeding SPACE tokens). Upon successful return *last will
1541 * be the last consumed node, (corresponding to the closing right
1546 * FUNCTION_STATUS_SUCCESS:
1548 * Successfully parsed a set of function arguments.
1550 * FUNCTION_NOT_A_FUNCTION:
1552 * Macro name not followed by a '('. This is not an error, but
1553 * simply that the macro name should be treated as a non-macro.
1555 * FUNCTION_UNBALANCED_PARENTHESES
1557 * Macro name is not followed by a balanced set of parentheses.
1559 static function_status_t
1560 _arguments_parse(glcpp_parser_t *parser,
1561 argument_list_t *arguments, token_node_t *node,
1562 token_node_t **last)
1564 token_list_t *argument;
1569 /* Ignore whitespace before first parenthesis. */
1570 while (node && node->token->type == SPACE)
1573 if (node == NULL || node->token->type != '(')
1574 return FUNCTION_NOT_A_FUNCTION;
1578 argument = _token_list_create (parser);
1579 _argument_list_append (parser, arguments, argument);
1581 for (paren_count = 1; node; node = node->next) {
1582 if (node->token->type == '(') {
1584 } else if (node->token->type == ')') {
1586 if (paren_count == 0)
1590 if (node->token->type == ',' && paren_count == 1) {
1591 _token_list_trim_trailing_space (argument);
1592 argument = _token_list_create (parser);
1593 _argument_list_append (parser, arguments, argument);
1595 if (argument->head == NULL) {
1596 /* Don't treat initial whitespace as part of the argument. */
1597 if (node->token->type == SPACE)
1600 _token_list_append(parser, argument, node->token);
1605 return FUNCTION_UNBALANCED_PARENTHESES;
1609 return FUNCTION_STATUS_SUCCESS;
1612 static token_list_t *
1613 _token_list_create_with_one_ival(glcpp_parser_t *parser, int type, int ival)
1618 list = _token_list_create(parser);
1619 node = _token_create_ival(parser, type, ival);
1620 _token_list_append(parser, list, node);
1625 static token_list_t *
1626 _token_list_create_with_one_space(glcpp_parser_t *parser)
1628 return _token_list_create_with_one_ival(parser, SPACE, SPACE);
1631 static token_list_t *
1632 _token_list_create_with_one_integer(glcpp_parser_t *parser, int ival)
1634 return _token_list_create_with_one_ival(parser, INTEGER, ival);
1637 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1639 * Note: This function must only be called when "node" is a DEFINED token,
1640 * (and will abort with an assertion failure otherwise).
1642 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1643 * (optionally preceded and followed by '(' and ')' tokens) then the following
1646 * If the identifier is a defined macro, this function returns 1.
1648 * If the identifier is not a defined macro, this function returns 0.
1650 * In either case, *last will be updated to the last node in the list
1651 * consumed by the evaluation, (either the token of the identifier or the
1652 * token of the closing parenthesis).
1654 * In all other cases, (such as "node is the final node of the list", or
1655 * "missing closing parenthesis", etc.), this function generates a
1656 * preprocessor error, returns -1 and *last will not be set.
1659 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1660 token_node_t **last)
1662 token_node_t *argument, *defined = node;
1664 assert(node->token->type == DEFINED);
1668 /* Ignore whitespace after DEFINED token. */
1669 while (node && node->token->type == SPACE)
1675 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1677 } else if (node->token->type == '(') {
1680 /* Ignore whitespace after '(' token. */
1681 while (node && node->token->type == SPACE)
1684 if (node == NULL || (node->token->type != IDENTIFIER &&
1685 node->token->type != OTHER)) {
1693 /* Ignore whitespace after identifier, before ')' token. */
1694 while (node && node->token->type == SPACE)
1697 if (node == NULL || node->token->type != ')')
1705 return _mesa_hash_table_search(parser->defines,
1706 argument->token->value.str) ? 1 : 0;
1709 glcpp_error (&defined->token->location, parser,
1710 "\"defined\" not followed by an identifier");
1714 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1717 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1720 token_node_t *node, *node_prev, *replacement, *last = NULL;
1731 if (node->token->type != DEFINED)
1734 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1738 replacement = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
1739 replacement->token = _token_create_ival (parser, INTEGER, value);
1741 /* Splice replacement node into list, replacing from "node"
1742 * through "last". */
1744 node_prev->next = replacement;
1746 list->head = replacement;
1747 replacement->next = last->next;
1748 if (last == list->tail)
1749 list->tail = replacement;
1759 /* Perform macro expansion on 'list', placing the resulting tokens
1760 * into a new list which is initialized with a first token of type
1761 * 'head_token_type'. Then begin lexing from the resulting list,
1762 * (return to the current lexing source when this list is exhausted).
1764 * See the documentation of _glcpp_parser_expand_token_list for a description
1765 * of the "mode" parameter.
1768 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1769 token_list_t *list, expansion_mode_t mode)
1771 token_list_t *expanded;
1774 expanded = _token_list_create (parser);
1775 token = _token_create_ival (parser, head_token_type, head_token_type);
1776 _token_list_append (parser, expanded, token);
1777 _glcpp_parser_expand_token_list (parser, list, mode);
1778 _token_list_append_list (expanded, list);
1779 glcpp_parser_lex_from (parser, expanded);
1783 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1789 token_node_t *next_non_space;
1791 /* Look ahead for a PASTE token, skipping space. */
1792 next_non_space = node->next;
1793 while (next_non_space && next_non_space->token->type == SPACE)
1794 next_non_space = next_non_space->next;
1796 if (next_non_space == NULL)
1799 if (next_non_space->token->type != PASTE) {
1800 node = next_non_space;
1804 /* Now find the next non-space token after the PASTE. */
1805 next_non_space = next_non_space->next;
1806 while (next_non_space && next_non_space->token->type == SPACE)
1807 next_non_space = next_non_space->next;
1809 if (next_non_space == NULL) {
1810 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1814 node->token = _token_paste(parser, node->token, next_non_space->token);
1815 node->next = next_non_space->next;
1816 if (next_non_space == list->tail)
1820 list->non_space_tail = list->tail;
1823 /* This is a helper function that's essentially part of the
1824 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1825 * except for by that function.
1827 * Returns NULL if node is a simple token with no expansion, (that is,
1828 * although 'node' corresponds to an identifier defined as a
1829 * function-like macro, it is not followed with a parenthesized
1832 * Compute the complete expansion of node (which is a function-like
1833 * macro) and subsequent nodes which are arguments.
1835 * Returns the token list that results from the expansion and sets
1836 * *last to the last node in the list that was consumed by the
1837 * expansion. Specifically, *last will be set as follows: as the
1838 * token of the closing right parenthesis.
1840 * See the documentation of _glcpp_parser_expand_token_list for a description
1841 * of the "mode" parameter.
1843 static token_list_t *
1844 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1845 token_node_t **last, expansion_mode_t mode)
1847 struct hash_entry *entry;
1849 const char *identifier;
1850 argument_list_t *arguments;
1851 function_status_t status;
1852 token_list_t *substituted;
1853 int parameter_index;
1855 identifier = node->token->value.str;
1857 entry = _mesa_hash_table_search(parser->defines, identifier);
1858 macro = entry ? entry->data : NULL;
1860 assert(macro->is_function);
1862 arguments = _argument_list_create(parser);
1863 status = _arguments_parse(parser, arguments, node, last);
1866 case FUNCTION_STATUS_SUCCESS:
1868 case FUNCTION_NOT_A_FUNCTION:
1870 case FUNCTION_UNBALANCED_PARENTHESES:
1871 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1875 /* Replace a macro defined as empty with a SPACE token. */
1876 if (macro->replacements == NULL) {
1877 return _token_list_create_with_one_space(parser);
1880 if (!((_argument_list_length (arguments) ==
1881 _string_list_length (macro->parameters)) ||
1882 (_string_list_length (macro->parameters) == 0 &&
1883 _argument_list_length (arguments) == 1 &&
1884 arguments->head->argument->head == NULL))) {
1885 glcpp_error(&node->token->location, parser,
1886 "Error: macro %s invoked with %d arguments (expected %d)\n",
1887 identifier, _argument_list_length (arguments),
1888 _string_list_length(macro->parameters));
1892 /* Perform argument substitution on the replacement list. */
1893 substituted = _token_list_create(parser);
1895 for (node = macro->replacements->head; node; node = node->next) {
1896 if (node->token->type == IDENTIFIER &&
1897 _string_list_contains(macro->parameters, node->token->value.str,
1898 ¶meter_index)) {
1899 token_list_t *argument;
1900 argument = _argument_list_member_at(arguments, parameter_index);
1901 /* Before substituting, we expand the argument tokens, or append a
1902 * placeholder token for an empty argument. */
1903 if (argument->head) {
1904 token_list_t *expanded_argument;
1905 expanded_argument = _token_list_copy(parser, argument);
1906 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1907 _token_list_append_list(substituted, expanded_argument);
1911 new_token = _token_create_ival(parser, PLACEHOLDER,
1913 _token_list_append(parser, substituted, new_token);
1916 _token_list_append(parser, substituted, node->token);
1920 /* After argument substitution, and before further expansion
1921 * below, implement token pasting. */
1923 _token_list_trim_trailing_space(substituted);
1925 _glcpp_parser_apply_pastes(parser, substituted);
1930 /* Compute the complete expansion of node, (and subsequent nodes after
1931 * 'node' in the case that 'node' is a function-like macro and
1932 * subsequent nodes are arguments).
1934 * Returns NULL if node is a simple token with no expansion.
1936 * Otherwise, returns the token list that results from the expansion
1937 * and sets *last to the last node in the list that was consumed by
1938 * the expansion. Specifically, *last will be set as follows:
1940 * As 'node' in the case of object-like macro expansion.
1942 * As the token of the closing right parenthesis in the case of
1943 * function-like macro expansion.
1945 * See the documentation of _glcpp_parser_expand_token_list for a description
1946 * of the "mode" parameter.
1948 static token_list_t *
1949 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1950 token_node_t **last, expansion_mode_t mode,
1953 token_t *token = node->token;
1954 const char *identifier;
1955 struct hash_entry *entry;
1958 /* We only expand identifiers */
1959 if (token->type != IDENTIFIER) {
1964 identifier = token->value.str;
1966 /* Special handling for __LINE__ and __FILE__, (not through
1967 * the hash table). */
1968 if (*identifier == '_') {
1969 if (strcmp(identifier, "__LINE__") == 0)
1970 return _token_list_create_with_one_integer(parser, line);
1972 if (strcmp(identifier, "__FILE__") == 0)
1973 return _token_list_create_with_one_integer(parser,
1974 node->token->location.source);
1977 /* Look up this identifier in the hash table. */
1978 entry = _mesa_hash_table_search(parser->defines, identifier);
1979 macro = entry ? entry->data : NULL;
1981 /* Not a macro, so no expansion needed. */
1985 /* Finally, don't expand this macro if we're already actively
1986 * expanding it, (to avoid infinite recursion). */
1987 if (_parser_active_list_contains (parser, identifier)) {
1988 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1989 * future expansion of this unexpanded token. */
1991 token_list_t *expansion;
1994 str = linear_strdup(parser->linalloc, token->value.str);
1995 final = _token_create_str(parser, OTHER, str);
1996 expansion = _token_list_create(parser);
1997 _token_list_append(parser, expansion, final);
2001 if (! macro->is_function) {
2002 token_list_t *replacement;
2004 /* Replace a macro defined as empty with a SPACE token. */
2005 if (macro->replacements == NULL)
2006 return _token_list_create_with_one_space(parser);
2008 replacement = _token_list_copy(parser, macro->replacements);
2009 _glcpp_parser_apply_pastes(parser, replacement);
2013 return _glcpp_parser_expand_function(parser, node, last, mode);
2016 /* Push a new identifier onto the parser's active list.
2018 * Here, 'marker' is the token node that appears in the list after the
2019 * expansion of 'identifier'. That is, when the list iterator begins
2020 * examining 'marker', then it is time to pop this node from the
2024 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
2025 token_node_t *marker)
2027 active_list_t *node;
2029 node = linear_alloc_child(parser->linalloc, sizeof(active_list_t));
2030 node->identifier = linear_strdup(parser->linalloc, identifier);
2031 node->marker = marker;
2032 node->next = parser->active;
2034 parser->active = node;
2038 _parser_active_list_pop(glcpp_parser_t *parser)
2040 active_list_t *node = parser->active;
2043 parser->active = NULL;
2047 node = parser->active->next;
2048 parser->active = node;
2052 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
2054 active_list_t *node;
2056 if (parser->active == NULL)
2059 for (node = parser->active; node; node = node->next)
2060 if (strcmp(node->identifier, identifier) == 0)
2066 /* Walk over the token list replacing nodes with their expansion.
2067 * Whenever nodes are expanded the walking will walk over the new
2068 * nodes, continuing to expand as necessary. The results are placed in
2071 * The "mode" argument controls the handling of any DEFINED tokens that
2072 * result from expansion as follows:
2074 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
2075 * left in the final list, unevaluated. This is the correct mode
2076 * for expanding any list in any context other than a
2077 * preprocessor conditional, (#if or #elif).
2079 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
2080 * evaluated to 0 or 1 tokens depending on whether the following
2081 * token is the name of a defined macro. If the DEFINED token is
2082 * not followed by an (optionally parenthesized) identifier, then
2083 * an error will be generated. This the correct mode for
2084 * expanding any list in the context of a preprocessor
2085 * conditional, (#if or #elif).
2088 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
2089 expansion_mode_t mode)
2091 token_node_t *node_prev;
2092 token_node_t *node, *last = NULL;
2093 token_list_t *expansion;
2094 active_list_t *active_initial = parser->active;
2100 _token_list_trim_trailing_space (list);
2102 line = list->tail->token->location.last_line;
2107 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
2108 _glcpp_parser_evaluate_defined_in_list (parser, list);
2112 while (parser->active && parser->active->marker == node)
2113 _parser_active_list_pop (parser);
2115 expansion = _glcpp_parser_expand_node (parser, node, &last, mode, line);
2119 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
2120 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
2123 for (n = node; n != last->next; n = n->next)
2124 while (parser->active && parser->active->marker == n) {
2125 _parser_active_list_pop (parser);
2128 _parser_active_list_push(parser, node->token->value.str, last->next);
2130 /* Splice expansion into list, supporting a simple deletion if the
2131 * expansion is empty.
2133 if (expansion->head) {
2135 node_prev->next = expansion->head;
2137 list->head = expansion->head;
2138 expansion->tail->next = last->next;
2139 if (last == list->tail)
2140 list->tail = expansion->tail;
2143 node_prev->next = last->next;
2145 list->head = last->next;
2146 if (last == list->tail)
2152 node = node_prev ? node_prev->next : list->head;
2155 /* Remove any lingering effects of this invocation on the
2156 * active list. That is, pop until the list looks like it did
2157 * at the beginning of this function. */
2158 while (parser->active && parser->active != active_initial)
2159 _parser_active_list_pop (parser);
2161 list->non_space_tail = list->tail;
2165 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
2171 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2173 _token_list_trim_trailing_space (list);
2175 _token_list_print (parser, list);
2179 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2180 const char *identifier)
2182 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2183 * the GLSL ES spec (all versions) say:
2185 * "All macro names containing two consecutive underscores ( __ )
2186 * are reserved for future use as predefined macro names. All
2187 * macro names prefixed with "GL_" ("GL" followed by a single
2188 * underscore) are also reserved."
2190 * The intention is that names containing __ are reserved for internal
2191 * use by the implementation, and names prefixed with GL_ are reserved
2192 * for use by Khronos. Since every extension adds a name prefixed
2193 * with GL_ (i.e., the name of the extension), that should be an
2194 * error. Names simply containing __ are dangerous to use, but should
2197 * A future version of the GLSL specification will clarify this.
2199 if (strstr(identifier, "__")) {
2200 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2201 "for use by the implementation.\n");
2203 if (strncmp(identifier, "GL_", 3) == 0) {
2204 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2206 if (strcmp(identifier, "defined") == 0) {
2207 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2212 _macro_equal(macro_t *a, macro_t *b)
2214 if (a->is_function != b->is_function)
2217 if (a->is_function) {
2218 if (! _string_list_equal (a->parameters, b->parameters))
2222 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2226 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2227 const char *identifier, token_list_t *replacements)
2229 macro_t *macro, *previous;
2230 struct hash_entry *entry;
2232 /* We define pre-defined macros before we've started parsing the actual
2233 * file. So if there's no location defined yet, that's what were doing and
2234 * we don't want to generate an error for using the reserved names. */
2236 _check_for_reserved_macro_name(parser, loc, identifier);
2238 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2240 macro->is_function = 0;
2241 macro->parameters = NULL;
2242 macro->identifier = linear_strdup(parser->linalloc, identifier);
2243 macro->replacements = replacements;
2245 entry = _mesa_hash_table_search(parser->defines, identifier);
2246 previous = entry ? entry->data : NULL;
2248 if (_macro_equal (macro, previous)) {
2251 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2254 _mesa_hash_table_insert (parser->defines, identifier, macro);
2258 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2259 const char *identifier, string_list_t *parameters,
2260 token_list_t *replacements)
2262 macro_t *macro, *previous;
2263 struct hash_entry *entry;
2266 _check_for_reserved_macro_name(parser, loc, identifier);
2268 /* Check for any duplicate parameter names. */
2269 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2270 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2273 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2275 macro->is_function = 1;
2276 macro->parameters = parameters;
2277 macro->identifier = linear_strdup(parser->linalloc, identifier);
2278 macro->replacements = replacements;
2280 entry = _mesa_hash_table_search(parser->defines, identifier);
2281 previous = entry ? entry->data : NULL;
2283 if (_macro_equal (macro, previous)) {
2286 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2289 _mesa_hash_table_insert(parser->defines, identifier, macro);
2293 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2298 if (parser->lex_from_list == NULL) {
2299 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2301 /* XXX: This ugly block of code exists for the sole
2302 * purpose of converting a NEWLINE token into a SPACE
2303 * token, but only in the case where we have seen a
2304 * function-like macro name, but have not yet seen its
2305 * closing parenthesis.
2307 * There's perhaps a more compact way to do this with
2308 * mid-rule actions in the grammar.
2310 * I'm definitely not pleased with the complexity of
2313 if (parser->newline_as_space) {
2315 parser->paren_count++;
2316 } else if (ret == ')') {
2317 parser->paren_count--;
2318 if (parser->paren_count == 0)
2319 parser->newline_as_space = 0;
2320 } else if (ret == NEWLINE) {
2322 } else if (ret != SPACE) {
2323 if (parser->paren_count == 0)
2324 parser->newline_as_space = 0;
2326 } else if (parser->in_control_line) {
2328 parser->in_control_line = 0;
2330 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2331 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2332 ret == ENDIF || ret == HASH_TOKEN) {
2333 parser->in_control_line = 1;
2334 } else if (ret == IDENTIFIER) {
2335 struct hash_entry *entry = _mesa_hash_table_search(parser->defines,
2337 macro_t *macro = entry ? entry->data : NULL;
2338 if (macro && macro->is_function) {
2339 parser->newline_as_space = 1;
2340 parser->paren_count = 0;
2347 node = parser->lex_from_node;
2350 parser->lex_from_list = NULL;
2354 *yylval = node->token->value;
2355 ret = node->token->type;
2357 parser->lex_from_node = node->next;
2363 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2367 assert (parser->lex_from_list == NULL);
2369 /* Copy list, eliminating any space tokens. */
2370 parser->lex_from_list = _token_list_create (parser);
2372 for (node = list->head; node; node = node->next) {
2373 if (node->token->type == SPACE)
2375 _token_list_append (parser, parser->lex_from_list, node->token);
2378 parser->lex_from_node = parser->lex_from_list->head;
2380 /* It's possible the list consisted of nothing but whitespace. */
2381 if (parser->lex_from_node == NULL) {
2382 parser->lex_from_list = NULL;
2387 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2390 skip_type_t current = SKIP_NO_SKIP;
2393 if (parser->skip_stack)
2394 current = parser->skip_stack->type;
2396 node = linear_alloc_child(parser->linalloc, sizeof(skip_node_t));
2399 if (current == SKIP_NO_SKIP) {
2401 node->type = SKIP_NO_SKIP;
2403 node->type = SKIP_TO_ELSE;
2405 node->type = SKIP_TO_ENDIF;
2408 node->has_else = false;
2409 node->next = parser->skip_stack;
2410 parser->skip_stack = node;
2414 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2415 const char *type, int condition)
2417 if (parser->skip_stack == NULL) {
2418 glcpp_error (loc, parser, "#%s without #if\n", type);
2422 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2424 parser->skip_stack->type = SKIP_NO_SKIP;
2426 parser->skip_stack->type = SKIP_TO_ENDIF;
2431 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2435 if (parser->skip_stack == NULL) {
2436 glcpp_error (loc, parser, "#endif without #if\n");
2440 node = parser->skip_stack;
2441 parser->skip_stack = node->next;
2445 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2446 const char *identifier,
2447 bool explicitly_set)
2449 if (parser->version_set)
2452 parser->version = version;
2453 parser->version_set = true;
2455 add_builtin_define (parser, "__VERSION__", version);
2457 parser->is_gles = (version == 100) ||
2458 (identifier && (strcmp(identifier, "es") == 0));
2459 bool is_compat = version >= 150 && identifier &&
2460 strcmp(identifier, "compatibility") == 0;
2462 /* Add pre-defined macros. */
2463 if (parser->is_gles)
2464 add_builtin_define(parser, "GL_ES", 1);
2466 add_builtin_define(parser, "GL_compatibility_profile", 1);
2467 else if (version >= 150)
2468 add_builtin_define(parser, "GL_core_profile", 1);
2470 /* Currently, all ES2/ES3 implementations support highp in the
2471 * fragment shader, so we always define this macro in ES2/ES3.
2472 * If we ever get a driver that doesn't support highp, we'll
2473 * need to add a flag to the gl_context and check that here.
2475 if (version >= 130 || parser->is_gles)
2476 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2478 /* Add all the extension macros available in this context */
2479 if (parser->extensions)
2480 parser->extensions(parser->state, add_builtin_define, parser,
2481 version, parser->is_gles);
2483 if (parser->extension_list) {
2484 /* If MESA_shader_integer_functions is supported, then the building
2485 * blocks required for the 64x64 => 64 multiply exist. Add defines for
2486 * those functions so that they can be tested.
2488 if (parser->extension_list->MESA_shader_integer_functions) {
2489 add_builtin_define(parser, "__have_builtin_builtin_sign64", 1);
2490 add_builtin_define(parser, "__have_builtin_builtin_umul64", 1);
2491 add_builtin_define(parser, "__have_builtin_builtin_udiv64", 1);
2492 add_builtin_define(parser, "__have_builtin_builtin_umod64", 1);
2493 add_builtin_define(parser, "__have_builtin_builtin_idiv64", 1);
2494 add_builtin_define(parser, "__have_builtin_builtin_imod64", 1);
2498 if (explicitly_set) {
2499 _mesa_string_buffer_printf(parser->output,
2500 "#version %" PRIiMAX "%s%s", version,
2501 identifier ? " " : "",
2502 identifier ? identifier : "");
2506 /* GLSL version if no version is explicitly specified. */
2507 #define IMPLICIT_GLSL_VERSION 110
2509 /* GLSL ES version if no version is explicitly specified. */
2510 #define IMPLICIT_GLSL_ES_VERSION 100
2513 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2515 int language_version = parser->api == API_OPENGLES2 ?
2516 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2518 _glcpp_parser_handle_version_declaration(parser, language_version,
2523 glcpp_parser_copy_defines(const void *key, void *data, void *closure)
2525 struct define_include *di = (struct define_include *) closure;
2526 macro_t *macro = (macro_t *) data;
2528 /* If we hit an error on a previous pass, just return */
2529 if (di->parser->error)
2532 const char *identifier = macro->identifier;
2533 struct hash_entry *entry = _mesa_hash_table_search(di->parser->defines,
2536 macro_t *previous = entry ? entry->data : NULL;
2538 if (_macro_equal(macro, previous)) {
2541 glcpp_error(di->loc, di->parser, "Redefinition of macro %s\n",
2545 _mesa_hash_table_insert(di->parser->defines, identifier, macro);