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 version_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 version_constant NEWLINE {
423 if (parser->version_set) {
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 version_constant IDENTIFIER NEWLINE {
429 if (parser->version_set) {
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);
470 /* Both octal and hexadecimal constants begin with 0. */
471 if ($1[0] == '0' && $1[1] != '\0') {
472 glcpp_error(&@1, parser, "invalid #version \"%s\" (not a decimal constant)", $1);
475 $$ = strtoll($1, NULL, 10);
482 $$.undefined_macro = NULL;
487 $$.undefined_macro = linear_strdup(parser->linalloc, $1);
489 $$.undefined_macro = NULL;
491 | expression OR expression {
492 $$.value = $1.value || $3.value;
494 /* Short-circuit: Only flag undefined from right side
495 * if left side evaluates to false.
497 if ($1.undefined_macro)
498 $$.undefined_macro = $1.undefined_macro;
500 $$.undefined_macro = $3.undefined_macro;
502 | expression AND expression {
503 $$.value = $1.value && $3.value;
505 /* Short-circuit: Only flag undefined from right-side
506 * if left side evaluates to true.
508 if ($1.undefined_macro)
509 $$.undefined_macro = $1.undefined_macro;
511 $$.undefined_macro = $3.undefined_macro;
513 | expression '|' expression {
514 $$.value = $1.value | $3.value;
515 if ($1.undefined_macro)
516 $$.undefined_macro = $1.undefined_macro;
518 $$.undefined_macro = $3.undefined_macro;
520 | expression '^' expression {
521 $$.value = $1.value ^ $3.value;
522 if ($1.undefined_macro)
523 $$.undefined_macro = $1.undefined_macro;
525 $$.undefined_macro = $3.undefined_macro;
527 | expression '&' expression {
528 $$.value = $1.value & $3.value;
529 if ($1.undefined_macro)
530 $$.undefined_macro = $1.undefined_macro;
532 $$.undefined_macro = $3.undefined_macro;
534 | expression NOT_EQUAL expression {
535 $$.value = $1.value != $3.value;
536 if ($1.undefined_macro)
537 $$.undefined_macro = $1.undefined_macro;
539 $$.undefined_macro = $3.undefined_macro;
541 | expression EQUAL expression {
542 $$.value = $1.value == $3.value;
543 if ($1.undefined_macro)
544 $$.undefined_macro = $1.undefined_macro;
546 $$.undefined_macro = $3.undefined_macro;
548 | expression GREATER_OR_EQUAL expression {
549 $$.value = $1.value >= $3.value;
550 if ($1.undefined_macro)
551 $$.undefined_macro = $1.undefined_macro;
553 $$.undefined_macro = $3.undefined_macro;
555 | expression LESS_OR_EQUAL expression {
556 $$.value = $1.value <= $3.value;
557 if ($1.undefined_macro)
558 $$.undefined_macro = $1.undefined_macro;
560 $$.undefined_macro = $3.undefined_macro;
562 | expression '>' expression {
563 $$.value = $1.value > $3.value;
564 if ($1.undefined_macro)
565 $$.undefined_macro = $1.undefined_macro;
567 $$.undefined_macro = $3.undefined_macro;
569 | expression '<' expression {
570 $$.value = $1.value < $3.value;
571 if ($1.undefined_macro)
572 $$.undefined_macro = $1.undefined_macro;
574 $$.undefined_macro = $3.undefined_macro;
576 | expression RIGHT_SHIFT expression {
577 $$.value = $1.value >> $3.value;
578 if ($1.undefined_macro)
579 $$.undefined_macro = $1.undefined_macro;
581 $$.undefined_macro = $3.undefined_macro;
583 | expression LEFT_SHIFT expression {
584 $$.value = $1.value << $3.value;
585 if ($1.undefined_macro)
586 $$.undefined_macro = $1.undefined_macro;
588 $$.undefined_macro = $3.undefined_macro;
590 | expression '-' expression {
591 $$.value = $1.value - $3.value;
592 if ($1.undefined_macro)
593 $$.undefined_macro = $1.undefined_macro;
595 $$.undefined_macro = $3.undefined_macro;
597 | expression '+' expression {
598 $$.value = $1.value + $3.value;
599 if ($1.undefined_macro)
600 $$.undefined_macro = $1.undefined_macro;
602 $$.undefined_macro = $3.undefined_macro;
604 | expression '%' expression {
606 yyerror (& @1, parser,
607 "zero modulus in preprocessor directive");
609 $$.value = $1.value % $3.value;
611 if ($1.undefined_macro)
612 $$.undefined_macro = $1.undefined_macro;
614 $$.undefined_macro = $3.undefined_macro;
616 | expression '/' expression {
618 yyerror (& @1, parser,
619 "division by 0 in preprocessor directive");
621 $$.value = $1.value / $3.value;
623 if ($1.undefined_macro)
624 $$.undefined_macro = $1.undefined_macro;
626 $$.undefined_macro = $3.undefined_macro;
628 | expression '*' expression {
629 $$.value = $1.value * $3.value;
630 if ($1.undefined_macro)
631 $$.undefined_macro = $1.undefined_macro;
633 $$.undefined_macro = $3.undefined_macro;
635 | '!' expression %prec UNARY {
636 $$.value = ! $2.value;
637 $$.undefined_macro = $2.undefined_macro;
639 | '~' expression %prec UNARY {
640 $$.value = ~ $2.value;
641 $$.undefined_macro = $2.undefined_macro;
643 | '-' expression %prec UNARY {
644 $$.value = - $2.value;
645 $$.undefined_macro = $2.undefined_macro;
647 | '+' expression %prec UNARY {
648 $$.value = + $2.value;
649 $$.undefined_macro = $2.undefined_macro;
651 | '(' expression ')' {
658 $$ = _string_list_create (parser);
659 _string_list_append_item (parser, $$, $1);
661 | identifier_list ',' IDENTIFIER {
663 _string_list_append_item (parser, $$, $3);
668 NEWLINE { $$ = NULL; }
673 /* empty */ { $$ = NULL; }
680 glcpp_error(&@1, parser, "extra tokens at end of directive");
685 preprocessing_token {
686 parser->space_tokens = 1;
687 $$ = _token_list_create (parser);
688 _token_list_append (parser, $$, $1);
690 | pp_tokens preprocessing_token {
692 _token_list_append (parser, $$, $2);
698 $$ = _token_create_str (parser, IDENTIFIER, $1);
699 $$->location = yylloc;
702 $$ = _token_create_str (parser, INTEGER_STRING, $1);
703 $$->location = yylloc;
706 $$ = _token_create_ival (parser, $1, $1);
707 $$->location = yylloc;
710 $$ = _token_create_ival (parser, DEFINED, DEFINED);
711 $$->location = yylloc;
714 $$ = _token_create_str (parser, OTHER, $1);
715 $$->location = yylloc;
718 $$ = _token_create_ival (parser, SPACE, SPACE);
719 $$->location = yylloc;
739 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
740 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
743 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
744 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
745 | EQUAL { $$ = EQUAL; }
746 | NOT_EQUAL { $$ = NOT_EQUAL; }
754 | PASTE { $$ = PASTE; }
755 | PLUS_PLUS { $$ = PLUS_PLUS; }
756 | MINUS_MINUS { $$ = MINUS_MINUS; }
762 _string_list_create(glcpp_parser_t *parser)
766 list = linear_alloc_child(parser->linalloc, sizeof(string_list_t));
774 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
779 node = linear_alloc_child(parser->linalloc, sizeof(string_node_t));
780 node->str = linear_strdup(parser->linalloc, str);
784 if (list->head == NULL) {
787 list->tail->next = node;
794 _string_list_contains(string_list_t *list, const char *member, int *index)
802 for (i = 0, node = list->head; node; i++, node = node->next) {
803 if (strcmp (node->str, member) == 0) {
813 /* Return duplicate string in list (if any), NULL otherwise. */
815 _string_list_has_duplicate(string_list_t *list)
817 string_node_t *node, *dup;
822 for (node = list->head; node; node = node->next) {
823 for (dup = node->next; dup; dup = dup->next) {
824 if (strcmp (node->str, dup->str) == 0)
833 _string_list_length(string_list_t *list)
841 for (node = list->head; node; node = node->next)
848 _string_list_equal(string_list_t *a, string_list_t *b)
850 string_node_t *node_a, *node_b;
852 if (a == NULL && b == NULL)
855 if (a == NULL || b == NULL)
858 for (node_a = a->head, node_b = b->head;
860 node_a = node_a->next, node_b = node_b->next)
862 if (strcmp (node_a->str, node_b->str))
866 /* Catch the case of lists being different lengths, (which
867 * would cause the loop above to terminate after the shorter
869 return node_a == node_b;
873 _argument_list_create(glcpp_parser_t *parser)
875 argument_list_t *list;
877 list = linear_alloc_child(parser->linalloc, sizeof(argument_list_t));
885 _argument_list_append(glcpp_parser_t *parser,
886 argument_list_t *list, token_list_t *argument)
888 argument_node_t *node;
890 node = linear_alloc_child(parser->linalloc, sizeof(argument_node_t));
891 node->argument = argument;
895 if (list->head == NULL) {
898 list->tail->next = node;
905 _argument_list_length(argument_list_t *list)
908 argument_node_t *node;
913 for (node = list->head; node; node = node->next)
920 _argument_list_member_at(argument_list_t *list, int index)
922 argument_node_t *node;
929 for (i = 0; i < index; i++) {
936 return node->argument;
942 _token_create_str(glcpp_parser_t *parser, int type, char *str)
946 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
948 token->value.str = str;
954 _token_create_ival(glcpp_parser_t *parser, int type, int ival)
958 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
960 token->value.ival = ival;
966 _token_list_create(glcpp_parser_t *parser)
970 list = linear_alloc_child(parser->linalloc, sizeof(token_list_t));
973 list->non_space_tail = NULL;
979 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token)
983 node = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
987 if (list->head == NULL) {
990 list->tail->next = node;
994 if (token->type != SPACE)
995 list->non_space_tail = node;
999 _token_list_append_list(token_list_t *list, token_list_t *tail)
1001 if (tail == NULL || tail->head == NULL)
1004 if (list->head == NULL) {
1005 list->head = tail->head;
1007 list->tail->next = tail->head;
1010 list->tail = tail->tail;
1011 list->non_space_tail = tail->non_space_tail;
1014 static token_list_t *
1015 _token_list_copy(glcpp_parser_t *parser, token_list_t *other)
1023 copy = _token_list_create (parser);
1024 for (node = other->head; node; node = node->next) {
1025 token_t *new_token = linear_alloc_child(parser->linalloc, sizeof(token_t));
1026 *new_token = *node->token;
1027 _token_list_append (parser, copy, new_token);
1034 _token_list_trim_trailing_space(token_list_t *list)
1036 if (list->non_space_tail) {
1037 list->non_space_tail->next = NULL;
1038 list->tail = list->non_space_tail;
1043 _token_list_is_empty_ignoring_space(token_list_t *l)
1051 while (n != NULL && n->token->type == SPACE)
1058 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1060 token_node_t *node_a, *node_b;
1062 if (a == NULL || b == NULL) {
1063 int a_empty = _token_list_is_empty_ignoring_space(a);
1064 int b_empty = _token_list_is_empty_ignoring_space(b);
1065 return a_empty == b_empty;
1073 if (node_a == NULL && node_b == NULL)
1076 if (node_a == NULL || node_b == NULL)
1078 /* Make sure whitespace appears in the same places in both.
1079 * It need not be exactly the same amount of whitespace,
1082 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1083 while (node_a && node_a->token->type == SPACE)
1084 node_a = node_a->next;
1085 while (node_b && node_b->token->type == SPACE)
1086 node_b = node_b->next;
1090 if (node_a->token->type != node_b->token->type)
1093 switch (node_a->token->type) {
1095 if (node_a->token->value.ival != node_b->token->value.ival) {
1100 case INTEGER_STRING:
1102 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1108 node_a = node_a->next;
1109 node_b = node_b->next;
1116 _token_print(char **out, size_t *len, token_t *token)
1118 if (token->type < 256) {
1119 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1123 switch (token->type) {
1125 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1128 case INTEGER_STRING:
1130 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1133 ralloc_asprintf_rewrite_tail (out, len, " ");
1136 ralloc_asprintf_rewrite_tail (out, len, "<<");
1139 ralloc_asprintf_rewrite_tail (out, len, ">>");
1142 ralloc_asprintf_rewrite_tail (out, len, "<=");
1144 case GREATER_OR_EQUAL:
1145 ralloc_asprintf_rewrite_tail (out, len, ">=");
1148 ralloc_asprintf_rewrite_tail (out, len, "==");
1151 ralloc_asprintf_rewrite_tail (out, len, "!=");
1154 ralloc_asprintf_rewrite_tail (out, len, "&&");
1157 ralloc_asprintf_rewrite_tail (out, len, "||");
1160 ralloc_asprintf_rewrite_tail (out, len, "##");
1163 ralloc_asprintf_rewrite_tail (out, len, "++");
1166 ralloc_asprintf_rewrite_tail (out, len, "--");
1169 ralloc_asprintf_rewrite_tail (out, len, "defined");
1172 /* Nothing to print. */
1175 assert(!"Error: Don't know how to print token.");
1181 /* Return a new token formed by pasting 'token' and 'other'. Note that this
1182 * function may return 'token' or 'other' directly rather than allocating
1185 * Caution: Only very cursory error-checking is performed to see if
1186 * the final result is a valid single token. */
1188 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1190 token_t *combined = NULL;
1192 /* Pasting a placeholder onto anything makes no change. */
1193 if (other->type == PLACEHOLDER)
1196 /* When 'token' is a placeholder, just return 'other'. */
1197 if (token->type == PLACEHOLDER)
1200 /* A very few single-character punctuators can be combined
1201 * with another to form a multi-character punctuator. */
1202 switch (token->type) {
1204 if (other->type == '<')
1205 combined = _token_create_ival (parser, LEFT_SHIFT, LEFT_SHIFT);
1206 else if (other->type == '=')
1207 combined = _token_create_ival (parser, LESS_OR_EQUAL, LESS_OR_EQUAL);
1210 if (other->type == '>')
1211 combined = _token_create_ival (parser, RIGHT_SHIFT, RIGHT_SHIFT);
1212 else if (other->type == '=')
1213 combined = _token_create_ival (parser, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1216 if (other->type == '=')
1217 combined = _token_create_ival (parser, EQUAL, EQUAL);
1220 if (other->type == '=')
1221 combined = _token_create_ival (parser, NOT_EQUAL, NOT_EQUAL);
1224 if (other->type == '&')
1225 combined = _token_create_ival (parser, AND, AND);
1228 if (other->type == '|')
1229 combined = _token_create_ival (parser, OR, OR);
1233 if (combined != NULL) {
1234 /* Inherit the location from the first token */
1235 combined->location = token->location;
1239 /* Two string-valued (or integer) tokens can usually just be
1240 * mashed together. (We also handle a string followed by an
1241 * integer here as well.)
1243 * There are some exceptions here. Notably, if the first token
1244 * is an integer (or a string representing an integer), then
1245 * the second token must also be an integer or must be a
1246 * string representing an integer that begins with a digit.
1248 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1249 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1254 /* Check that pasting onto an integer doesn't create a
1255 * non-integer, (that is, only digits can be
1257 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1258 switch (other->type) {
1259 case INTEGER_STRING:
1260 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1264 if (other->value.ival < 0)
1272 if (token->type == INTEGER)
1273 str = linear_asprintf(parser->linalloc, "%" PRIiMAX, token->value.ival);
1275 str = linear_strdup(parser->linalloc, token->value.str);
1277 if (other->type == INTEGER)
1278 linear_asprintf_append(parser->linalloc, &str, "%" PRIiMAX, other->value.ival);
1280 linear_strcat(parser->linalloc, &str, other->value.str);
1282 /* New token is same type as original token, unless we
1283 * started with an integer, in which case we will be
1284 * creating an integer-string. */
1285 combined_type = token->type;
1286 if (combined_type == INTEGER)
1287 combined_type = INTEGER_STRING;
1289 combined = _token_create_str (parser, combined_type, str);
1290 combined->location = token->location;
1295 glcpp_error (&token->location, parser, "");
1296 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1297 _token_print (&parser->info_log, &parser->info_log_length, token);
1298 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1299 _token_print (&parser->info_log, &parser->info_log_length, other);
1300 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1306 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1313 for (node = list->head; node; node = node->next)
1314 _token_print (&parser->output, &parser->output_length, node->token);
1318 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1320 glcpp_error(locp, parser, "%s", error);
1324 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1329 tok = _token_create_ival (parser, INTEGER, value);
1331 list = _token_list_create(parser);
1332 _token_list_append(parser, list, tok);
1333 _define_object_macro(parser, NULL, name, list);
1337 glcpp_parser_create(const struct gl_extensions *extension_list,
1338 glcpp_extension_iterator extensions, void *state, gl_api api)
1340 glcpp_parser_t *parser;
1342 parser = ralloc (NULL, glcpp_parser_t);
1344 glcpp_lex_init_extra (parser, &parser->scanner);
1345 parser->defines = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
1346 _mesa_key_string_equal);
1347 parser->linalloc = linear_alloc_parent(parser, 0);
1348 parser->active = NULL;
1349 parser->lexing_directive = 0;
1350 parser->lexing_version_directive = 0;
1351 parser->space_tokens = 1;
1352 parser->last_token_was_newline = 0;
1353 parser->last_token_was_space = 0;
1354 parser->first_non_space_token_this_line = 1;
1355 parser->newline_as_space = 0;
1356 parser->in_control_line = 0;
1357 parser->paren_count = 0;
1358 parser->commented_newlines = 0;
1360 parser->skip_stack = NULL;
1361 parser->skipping = 0;
1363 parser->lex_from_list = NULL;
1364 parser->lex_from_node = NULL;
1366 parser->output = ralloc_strdup(parser, "");
1367 parser->output_length = 0;
1368 parser->info_log = ralloc_strdup(parser, "");
1369 parser->info_log_length = 0;
1372 parser->extensions = extensions;
1373 parser->extension_list = extension_list;
1374 parser->state = state;
1376 parser->version = 0;
1377 parser->version_set = false;
1379 parser->has_new_line_number = 0;
1380 parser->new_line_number = 1;
1381 parser->has_new_source_number = 0;
1382 parser->new_source_number = 0;
1384 parser->is_gles = false;
1390 glcpp_parser_destroy(glcpp_parser_t *parser)
1392 glcpp_lex_destroy (parser->scanner);
1393 _mesa_hash_table_destroy(parser->defines, NULL);
1394 ralloc_free (parser);
1397 typedef enum function_status
1399 FUNCTION_STATUS_SUCCESS,
1400 FUNCTION_NOT_A_FUNCTION,
1401 FUNCTION_UNBALANCED_PARENTHESES
1402 } function_status_t;
1404 /* Find a set of function-like macro arguments by looking for a
1405 * balanced set of parentheses.
1407 * When called, 'node' should be the opening-parenthesis token, (or
1408 * perhaps preceeding SPACE tokens). Upon successful return *last will
1409 * be the last consumed node, (corresponding to the closing right
1414 * FUNCTION_STATUS_SUCCESS:
1416 * Successfully parsed a set of function arguments.
1418 * FUNCTION_NOT_A_FUNCTION:
1420 * Macro name not followed by a '('. This is not an error, but
1421 * simply that the macro name should be treated as a non-macro.
1423 * FUNCTION_UNBALANCED_PARENTHESES
1425 * Macro name is not followed by a balanced set of parentheses.
1427 static function_status_t
1428 _arguments_parse(glcpp_parser_t *parser,
1429 argument_list_t *arguments, token_node_t *node,
1430 token_node_t **last)
1432 token_list_t *argument;
1437 /* Ignore whitespace before first parenthesis. */
1438 while (node && node->token->type == SPACE)
1441 if (node == NULL || node->token->type != '(')
1442 return FUNCTION_NOT_A_FUNCTION;
1446 argument = _token_list_create (parser);
1447 _argument_list_append (parser, arguments, argument);
1449 for (paren_count = 1; node; node = node->next) {
1450 if (node->token->type == '(') {
1452 } else if (node->token->type == ')') {
1454 if (paren_count == 0)
1458 if (node->token->type == ',' && paren_count == 1) {
1459 _token_list_trim_trailing_space (argument);
1460 argument = _token_list_create (parser);
1461 _argument_list_append (parser, arguments, argument);
1463 if (argument->head == NULL) {
1464 /* Don't treat initial whitespace as part of the argument. */
1465 if (node->token->type == SPACE)
1468 _token_list_append(parser, argument, node->token);
1473 return FUNCTION_UNBALANCED_PARENTHESES;
1477 return FUNCTION_STATUS_SUCCESS;
1480 static token_list_t *
1481 _token_list_create_with_one_ival(glcpp_parser_t *parser, int type, int ival)
1486 list = _token_list_create(parser);
1487 node = _token_create_ival(parser, type, ival);
1488 _token_list_append(parser, list, node);
1493 static token_list_t *
1494 _token_list_create_with_one_space(glcpp_parser_t *parser)
1496 return _token_list_create_with_one_ival(parser, SPACE, SPACE);
1499 static token_list_t *
1500 _token_list_create_with_one_integer(glcpp_parser_t *parser, int ival)
1502 return _token_list_create_with_one_ival(parser, INTEGER, ival);
1505 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1507 * Note: This function must only be called when "node" is a DEFINED token,
1508 * (and will abort with an assertion failure otherwise).
1510 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1511 * (optionally preceded and followed by '(' and ')' tokens) then the following
1514 * If the identifier is a defined macro, this function returns 1.
1516 * If the identifier is not a defined macro, this function returns 0.
1518 * In either case, *last will be updated to the last node in the list
1519 * consumed by the evaluation, (either the token of the identifier or the
1520 * token of the closing parenthesis).
1522 * In all other cases, (such as "node is the final node of the list", or
1523 * "missing closing parenthesis", etc.), this function generates a
1524 * preprocessor error, returns -1 and *last will not be set.
1527 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1528 token_node_t **last)
1530 token_node_t *argument, *defined = node;
1532 assert(node->token->type == DEFINED);
1536 /* Ignore whitespace after DEFINED token. */
1537 while (node && node->token->type == SPACE)
1543 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1545 } else if (node->token->type == '(') {
1548 /* Ignore whitespace after '(' token. */
1549 while (node && node->token->type == SPACE)
1552 if (node == NULL || (node->token->type != IDENTIFIER &&
1553 node->token->type != OTHER)) {
1561 /* Ignore whitespace after identifier, before ')' token. */
1562 while (node && node->token->type == SPACE)
1565 if (node == NULL || node->token->type != ')')
1573 return _mesa_hash_table_search(parser->defines,
1574 argument->token->value.str) ? 1 : 0;
1577 glcpp_error (&defined->token->location, parser,
1578 "\"defined\" not followed by an identifier");
1582 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1585 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1588 token_node_t *node, *node_prev, *replacement, *last = NULL;
1599 if (node->token->type != DEFINED)
1602 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1606 replacement = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
1607 replacement->token = _token_create_ival (parser, INTEGER, value);
1609 /* Splice replacement node into list, replacing from "node"
1610 * through "last". */
1612 node_prev->next = replacement;
1614 list->head = replacement;
1615 replacement->next = last->next;
1616 if (last == list->tail)
1617 list->tail = replacement;
1627 /* Perform macro expansion on 'list', placing the resulting tokens
1628 * into a new list which is initialized with a first token of type
1629 * 'head_token_type'. Then begin lexing from the resulting list,
1630 * (return to the current lexing source when this list is exhausted).
1632 * See the documentation of _glcpp_parser_expand_token_list for a description
1633 * of the "mode" parameter.
1636 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1637 token_list_t *list, expansion_mode_t mode)
1639 token_list_t *expanded;
1642 expanded = _token_list_create (parser);
1643 token = _token_create_ival (parser, head_token_type, head_token_type);
1644 _token_list_append (parser, expanded, token);
1645 _glcpp_parser_expand_token_list (parser, list, mode);
1646 _token_list_append_list (expanded, list);
1647 glcpp_parser_lex_from (parser, expanded);
1651 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1657 token_node_t *next_non_space;
1659 /* Look ahead for a PASTE token, skipping space. */
1660 next_non_space = node->next;
1661 while (next_non_space && next_non_space->token->type == SPACE)
1662 next_non_space = next_non_space->next;
1664 if (next_non_space == NULL)
1667 if (next_non_space->token->type != PASTE) {
1668 node = next_non_space;
1672 /* Now find the next non-space token after the PASTE. */
1673 next_non_space = next_non_space->next;
1674 while (next_non_space && next_non_space->token->type == SPACE)
1675 next_non_space = next_non_space->next;
1677 if (next_non_space == NULL) {
1678 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1682 node->token = _token_paste(parser, node->token, next_non_space->token);
1683 node->next = next_non_space->next;
1684 if (next_non_space == list->tail)
1688 list->non_space_tail = list->tail;
1691 /* This is a helper function that's essentially part of the
1692 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1693 * except for by that function.
1695 * Returns NULL if node is a simple token with no expansion, (that is,
1696 * although 'node' corresponds to an identifier defined as a
1697 * function-like macro, it is not followed with a parenthesized
1700 * Compute the complete expansion of node (which is a function-like
1701 * macro) and subsequent nodes which are arguments.
1703 * Returns the token list that results from the expansion and sets
1704 * *last to the last node in the list that was consumed by the
1705 * expansion. Specifically, *last will be set as follows: as the
1706 * token of the closing right parenthesis.
1708 * See the documentation of _glcpp_parser_expand_token_list for a description
1709 * of the "mode" parameter.
1711 static token_list_t *
1712 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1713 token_node_t **last, expansion_mode_t mode)
1715 struct hash_entry *entry;
1717 const char *identifier;
1718 argument_list_t *arguments;
1719 function_status_t status;
1720 token_list_t *substituted;
1721 int parameter_index;
1723 identifier = node->token->value.str;
1725 entry = _mesa_hash_table_search(parser->defines, identifier);
1726 macro = entry ? entry->data : NULL;
1728 assert(macro->is_function);
1730 arguments = _argument_list_create(parser);
1731 status = _arguments_parse(parser, arguments, node, last);
1734 case FUNCTION_STATUS_SUCCESS:
1736 case FUNCTION_NOT_A_FUNCTION:
1738 case FUNCTION_UNBALANCED_PARENTHESES:
1739 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1743 /* Replace a macro defined as empty with a SPACE token. */
1744 if (macro->replacements == NULL) {
1745 return _token_list_create_with_one_space(parser);
1748 if (!((_argument_list_length (arguments) ==
1749 _string_list_length (macro->parameters)) ||
1750 (_string_list_length (macro->parameters) == 0 &&
1751 _argument_list_length (arguments) == 1 &&
1752 arguments->head->argument->head == NULL))) {
1753 glcpp_error(&node->token->location, parser,
1754 "Error: macro %s invoked with %d arguments (expected %d)\n",
1755 identifier, _argument_list_length (arguments),
1756 _string_list_length(macro->parameters));
1760 /* Perform argument substitution on the replacement list. */
1761 substituted = _token_list_create(parser);
1763 for (node = macro->replacements->head; node; node = node->next) {
1764 if (node->token->type == IDENTIFIER &&
1765 _string_list_contains(macro->parameters, node->token->value.str,
1766 ¶meter_index)) {
1767 token_list_t *argument;
1768 argument = _argument_list_member_at(arguments, parameter_index);
1769 /* Before substituting, we expand the argument tokens, or append a
1770 * placeholder token for an empty argument. */
1771 if (argument->head) {
1772 token_list_t *expanded_argument;
1773 expanded_argument = _token_list_copy(parser, argument);
1774 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1775 _token_list_append_list(substituted, expanded_argument);
1779 new_token = _token_create_ival(parser, PLACEHOLDER,
1781 _token_list_append(parser, substituted, new_token);
1784 _token_list_append(parser, substituted, node->token);
1788 /* After argument substitution, and before further expansion
1789 * below, implement token pasting. */
1791 _token_list_trim_trailing_space(substituted);
1793 _glcpp_parser_apply_pastes(parser, substituted);
1798 /* Compute the complete expansion of node, (and subsequent nodes after
1799 * 'node' in the case that 'node' is a function-like macro and
1800 * subsequent nodes are arguments).
1802 * Returns NULL if node is a simple token with no expansion.
1804 * Otherwise, returns the token list that results from the expansion
1805 * and sets *last to the last node in the list that was consumed by
1806 * the expansion. Specifically, *last will be set as follows:
1808 * As 'node' in the case of object-like macro expansion.
1810 * As the token of the closing right parenthesis in the case of
1811 * function-like macro expansion.
1813 * See the documentation of _glcpp_parser_expand_token_list for a description
1814 * of the "mode" parameter.
1816 static token_list_t *
1817 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1818 token_node_t **last, expansion_mode_t mode)
1820 token_t *token = node->token;
1821 const char *identifier;
1822 struct hash_entry *entry;
1825 /* We only expand identifiers */
1826 if (token->type != IDENTIFIER) {
1831 identifier = token->value.str;
1833 /* Special handling for __LINE__ and __FILE__, (not through
1834 * the hash table). */
1835 if (strcmp(identifier, "__LINE__") == 0)
1836 return _token_list_create_with_one_integer(parser, node->token->location.first_line);
1838 if (strcmp(identifier, "__FILE__") == 0)
1839 return _token_list_create_with_one_integer(parser, node->token->location.source);
1841 /* Look up this identifier in the hash table. */
1842 entry = _mesa_hash_table_search(parser->defines, identifier);
1843 macro = entry ? entry->data : NULL;
1845 /* Not a macro, so no expansion needed. */
1849 /* Finally, don't expand this macro if we're already actively
1850 * expanding it, (to avoid infinite recursion). */
1851 if (_parser_active_list_contains (parser, identifier)) {
1852 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1853 * future expansion of this unexpanded token. */
1855 token_list_t *expansion;
1858 str = linear_strdup(parser->linalloc, token->value.str);
1859 final = _token_create_str(parser, OTHER, str);
1860 expansion = _token_list_create(parser);
1861 _token_list_append(parser, expansion, final);
1865 if (! macro->is_function) {
1866 token_list_t *replacement;
1868 /* Replace a macro defined as empty with a SPACE token. */
1869 if (macro->replacements == NULL)
1870 return _token_list_create_with_one_space(parser);
1872 replacement = _token_list_copy(parser, macro->replacements);
1873 _glcpp_parser_apply_pastes(parser, replacement);
1877 return _glcpp_parser_expand_function(parser, node, last, mode);
1880 /* Push a new identifier onto the parser's active list.
1882 * Here, 'marker' is the token node that appears in the list after the
1883 * expansion of 'identifier'. That is, when the list iterator begins
1884 * examining 'marker', then it is time to pop this node from the
1888 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
1889 token_node_t *marker)
1891 active_list_t *node;
1893 node = linear_alloc_child(parser->linalloc, sizeof(active_list_t));
1894 node->identifier = linear_strdup(parser->linalloc, identifier);
1895 node->marker = marker;
1896 node->next = parser->active;
1898 parser->active = node;
1902 _parser_active_list_pop(glcpp_parser_t *parser)
1904 active_list_t *node = parser->active;
1907 parser->active = NULL;
1911 node = parser->active->next;
1912 parser->active = node;
1916 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
1918 active_list_t *node;
1920 if (parser->active == NULL)
1923 for (node = parser->active; node; node = node->next)
1924 if (strcmp(node->identifier, identifier) == 0)
1930 /* Walk over the token list replacing nodes with their expansion.
1931 * Whenever nodes are expanded the walking will walk over the new
1932 * nodes, continuing to expand as necessary. The results are placed in
1935 * The "mode" argument controls the handling of any DEFINED tokens that
1936 * result from expansion as follows:
1938 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
1939 * left in the final list, unevaluated. This is the correct mode
1940 * for expanding any list in any context other than a
1941 * preprocessor conditional, (#if or #elif).
1943 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
1944 * evaluated to 0 or 1 tokens depending on whether the following
1945 * token is the name of a defined macro. If the DEFINED token is
1946 * not followed by an (optionally parenthesized) identifier, then
1947 * an error will be generated. This the correct mode for
1948 * expanding any list in the context of a preprocessor
1949 * conditional, (#if or #elif).
1952 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
1953 expansion_mode_t mode)
1955 token_node_t *node_prev;
1956 token_node_t *node, *last = NULL;
1957 token_list_t *expansion;
1958 active_list_t *active_initial = parser->active;
1963 _token_list_trim_trailing_space (list);
1968 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
1969 _glcpp_parser_evaluate_defined_in_list (parser, list);
1973 while (parser->active && parser->active->marker == node)
1974 _parser_active_list_pop (parser);
1976 expansion = _glcpp_parser_expand_node (parser, node, &last, mode);
1980 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
1981 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
1984 for (n = node; n != last->next; n = n->next)
1985 while (parser->active && parser->active->marker == n) {
1986 _parser_active_list_pop (parser);
1989 _parser_active_list_push(parser, node->token->value.str, last->next);
1991 /* Splice expansion into list, supporting a simple deletion if the
1992 * expansion is empty.
1994 if (expansion->head) {
1996 node_prev->next = expansion->head;
1998 list->head = expansion->head;
1999 expansion->tail->next = last->next;
2000 if (last == list->tail)
2001 list->tail = expansion->tail;
2004 node_prev->next = last->next;
2006 list->head = last->next;
2007 if (last == list->tail)
2013 node = node_prev ? node_prev->next : list->head;
2016 /* Remove any lingering effects of this invocation on the
2017 * active list. That is, pop until the list looks like it did
2018 * at the beginning of this function. */
2019 while (parser->active && parser->active != active_initial)
2020 _parser_active_list_pop (parser);
2022 list->non_space_tail = list->tail;
2026 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
2032 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2034 _token_list_trim_trailing_space (list);
2036 _token_list_print (parser, list);
2040 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2041 const char *identifier)
2043 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2044 * the GLSL ES spec (all versions) say:
2046 * "All macro names containing two consecutive underscores ( __ )
2047 * are reserved for future use as predefined macro names. All
2048 * macro names prefixed with "GL_" ("GL" followed by a single
2049 * underscore) are also reserved."
2051 * The intention is that names containing __ are reserved for internal
2052 * use by the implementation, and names prefixed with GL_ are reserved
2053 * for use by Khronos. Since every extension adds a name prefixed
2054 * with GL_ (i.e., the name of the extension), that should be an
2055 * error. Names simply containing __ are dangerous to use, but should
2058 * A future version of the GLSL specification will clarify this.
2060 if (strstr(identifier, "__")) {
2061 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2062 "for use by the implementation.\n");
2064 if (strncmp(identifier, "GL_", 3) == 0) {
2065 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2067 if (strcmp(identifier, "defined") == 0) {
2068 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2073 _macro_equal(macro_t *a, macro_t *b)
2075 if (a->is_function != b->is_function)
2078 if (a->is_function) {
2079 if (! _string_list_equal (a->parameters, b->parameters))
2083 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2087 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2088 const char *identifier, token_list_t *replacements)
2090 macro_t *macro, *previous;
2091 struct hash_entry *entry;
2093 /* We define pre-defined macros before we've started parsing the actual
2094 * file. So if there's no location defined yet, that's what were doing and
2095 * we don't want to generate an error for using the reserved names. */
2097 _check_for_reserved_macro_name(parser, loc, identifier);
2099 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2101 macro->is_function = 0;
2102 macro->parameters = NULL;
2103 macro->identifier = linear_strdup(parser->linalloc, identifier);
2104 macro->replacements = replacements;
2106 entry = _mesa_hash_table_search(parser->defines, identifier);
2107 previous = entry ? entry->data : NULL;
2109 if (_macro_equal (macro, previous)) {
2112 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2115 _mesa_hash_table_insert (parser->defines, identifier, macro);
2119 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2120 const char *identifier, string_list_t *parameters,
2121 token_list_t *replacements)
2123 macro_t *macro, *previous;
2124 struct hash_entry *entry;
2127 _check_for_reserved_macro_name(parser, loc, identifier);
2129 /* Check for any duplicate parameter names. */
2130 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2131 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2134 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2136 macro->is_function = 1;
2137 macro->parameters = parameters;
2138 macro->identifier = linear_strdup(parser->linalloc, identifier);
2139 macro->replacements = replacements;
2141 entry = _mesa_hash_table_search(parser->defines, identifier);
2142 previous = entry ? entry->data : NULL;
2144 if (_macro_equal (macro, previous)) {
2147 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2150 _mesa_hash_table_insert(parser->defines, identifier, macro);
2154 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2159 if (parser->lex_from_list == NULL) {
2160 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2162 /* XXX: This ugly block of code exists for the sole
2163 * purpose of converting a NEWLINE token into a SPACE
2164 * token, but only in the case where we have seen a
2165 * function-like macro name, but have not yet seen its
2166 * closing parenthesis.
2168 * There's perhaps a more compact way to do this with
2169 * mid-rule actions in the grammar.
2171 * I'm definitely not pleased with the complexity of
2174 if (parser->newline_as_space) {
2176 parser->paren_count++;
2177 } else if (ret == ')') {
2178 parser->paren_count--;
2179 if (parser->paren_count == 0)
2180 parser->newline_as_space = 0;
2181 } else if (ret == NEWLINE) {
2183 } else if (ret != SPACE) {
2184 if (parser->paren_count == 0)
2185 parser->newline_as_space = 0;
2187 } else if (parser->in_control_line) {
2189 parser->in_control_line = 0;
2191 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2192 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2193 ret == ENDIF || ret == HASH_TOKEN) {
2194 parser->in_control_line = 1;
2195 } else if (ret == IDENTIFIER) {
2196 struct hash_entry *entry = _mesa_hash_table_search(parser->defines,
2198 macro_t *macro = entry ? entry->data : NULL;
2199 if (macro && macro->is_function) {
2200 parser->newline_as_space = 1;
2201 parser->paren_count = 0;
2208 node = parser->lex_from_node;
2211 parser->lex_from_list = NULL;
2215 *yylval = node->token->value;
2216 ret = node->token->type;
2218 parser->lex_from_node = node->next;
2224 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2228 assert (parser->lex_from_list == NULL);
2230 /* Copy list, eliminating any space tokens. */
2231 parser->lex_from_list = _token_list_create (parser);
2233 for (node = list->head; node; node = node->next) {
2234 if (node->token->type == SPACE)
2236 _token_list_append (parser, parser->lex_from_list, node->token);
2239 parser->lex_from_node = parser->lex_from_list->head;
2241 /* It's possible the list consisted of nothing but whitespace. */
2242 if (parser->lex_from_node == NULL) {
2243 parser->lex_from_list = NULL;
2248 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2251 skip_type_t current = SKIP_NO_SKIP;
2254 if (parser->skip_stack)
2255 current = parser->skip_stack->type;
2257 node = linear_alloc_child(parser->linalloc, sizeof(skip_node_t));
2260 if (current == SKIP_NO_SKIP) {
2262 node->type = SKIP_NO_SKIP;
2264 node->type = SKIP_TO_ELSE;
2266 node->type = SKIP_TO_ENDIF;
2269 node->has_else = false;
2270 node->next = parser->skip_stack;
2271 parser->skip_stack = node;
2275 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2276 const char *type, int condition)
2278 if (parser->skip_stack == NULL) {
2279 glcpp_error (loc, parser, "#%s without #if\n", type);
2283 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2285 parser->skip_stack->type = SKIP_NO_SKIP;
2287 parser->skip_stack->type = SKIP_TO_ENDIF;
2292 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2296 if (parser->skip_stack == NULL) {
2297 glcpp_error (loc, parser, "#endif without #if\n");
2301 node = parser->skip_stack;
2302 parser->skip_stack = node->next;
2306 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2307 const char *es_identifier,
2308 bool explicitly_set)
2310 if (parser->version_set)
2313 parser->version = version;
2314 parser->version_set = true;
2316 add_builtin_define (parser, "__VERSION__", version);
2318 parser->is_gles = (version == 100) ||
2319 (es_identifier && (strcmp(es_identifier, "es") == 0));
2321 /* Add pre-defined macros. */
2322 if (parser->is_gles)
2323 add_builtin_define(parser, "GL_ES", 1);
2324 else if (version >= 150)
2325 add_builtin_define(parser, "GL_core_profile", 1);
2327 /* Currently, all ES2/ES3 implementations support highp in the
2328 * fragment shader, so we always define this macro in ES2/ES3.
2329 * If we ever get a driver that doesn't support highp, we'll
2330 * need to add a flag to the gl_context and check that here.
2332 if (version >= 130 || parser->is_gles)
2333 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2335 /* Add all the extension macros available in this context */
2336 if (parser->extensions)
2337 parser->extensions(parser->state, add_builtin_define, parser,
2338 version, parser->is_gles);
2340 if (parser->extension_list) {
2341 /* If MESA_shader_integer_functions is supported, then the building
2342 * blocks required for the 64x64 => 64 multiply exist. Add defines for
2343 * those functions so that they can be tested.
2345 if (parser->extension_list->MESA_shader_integer_functions) {
2346 add_builtin_define(parser, "__have_builtin_builtin_umul64", 1);
2350 if (explicitly_set) {
2351 ralloc_asprintf_rewrite_tail(&parser->output, &parser->output_length,
2352 "#version %" PRIiMAX "%s%s", version,
2353 es_identifier ? " " : "",
2354 es_identifier ? es_identifier : "");
2358 /* GLSL version if no version is explicitly specified. */
2359 #define IMPLICIT_GLSL_VERSION 110
2361 /* GLSL ES version if no version is explicitly specified. */
2362 #define IMPLICIT_GLSL_ES_VERSION 100
2365 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2367 int language_version = parser->api == API_OPENGLES2 ?
2368 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2370 _glcpp_parser_handle_version_declaration(parser, language_version,