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"
35 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
38 _define_object_macro(glcpp_parser_t *parser,
41 token_list_t *replacements);
44 _define_function_macro(glcpp_parser_t *parser,
47 string_list_t *parameters,
48 token_list_t *replacements);
50 static string_list_t *
51 _string_list_create(glcpp_parser_t *parser);
54 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
58 _string_list_contains(string_list_t *list, const char *member, int *index);
61 _string_list_has_duplicate(string_list_t *list);
64 _string_list_length(string_list_t *list);
67 _string_list_equal(string_list_t *a, string_list_t *b);
69 static argument_list_t *
70 _argument_list_create(glcpp_parser_t *parser);
73 _argument_list_append(glcpp_parser_t *parser, argument_list_t *list,
74 token_list_t *argument);
77 _argument_list_length(argument_list_t *list);
80 _argument_list_member_at(argument_list_t *list, int index);
83 _token_create_str(glcpp_parser_t *parser, int type, char *str);
86 _token_create_ival(glcpp_parser_t *parser, int type, int ival);
89 _token_list_create(glcpp_parser_t *parser);
92 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token);
95 _token_list_append_list(token_list_t *list, token_list_t *tail);
98 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b);
101 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
102 token_node_t *marker);
105 _parser_active_list_pop(glcpp_parser_t *parser);
108 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier);
111 EXPANSION_MODE_IGNORE_DEFINED,
112 EXPANSION_MODE_EVALUATE_DEFINED
115 /* Expand list, and begin lexing from the result (after first
116 * prefixing a token of type 'head_token_type').
119 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
120 token_list_t *list, expansion_mode_t mode);
122 /* Perform macro expansion in-place on the given list. */
124 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
125 expansion_mode_t mode);
128 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
132 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
136 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
137 const char *type, int condition);
140 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc);
143 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
144 const char *ident, bool explicitly_set);
147 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
150 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list);
153 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
158 %define parse.error verbose
169 %parse-param {glcpp_parser_t *parser}
170 %lex-param {glcpp_parser_t *parser}
174 /* We use HASH_TOKEN, DEFINE_TOKEN and VERSION_TOKEN (as opposed to
175 * HASH, DEFINE, and VERSION) to avoid conflicts with other symbols,
176 * (such as the <HASH> and <DEFINE> start conditions in the lexer). */
177 %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
179 %type <ival> INTEGER operator SPACE integer_constant version_constant
180 %type <expression_value> expression
181 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA
182 %type <string_list> identifier_list
183 %type <token> preprocessing_token
184 %type <token_list> pp_tokens replacement_list text_line
190 %left EQUAL NOT_EQUAL
191 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
192 %left LEFT_SHIFT RIGHT_SHIFT
210 _glcpp_parser_print_expanded_token_list (parser, $1);
211 _mesa_string_buffer_append_char(parser->output, '\n');
217 IF_EXPANDED expression NEWLINE {
218 if (parser->is_gles && $2.undefined_macro)
219 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
220 _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
222 | ELIF_EXPANDED expression NEWLINE {
223 if (parser->is_gles && $2.undefined_macro)
224 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
225 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
227 | LINE_EXPANDED integer_constant NEWLINE {
228 parser->has_new_line_number = 1;
229 parser->new_line_number = $2;
230 _mesa_string_buffer_printf(parser->output, "#line %" PRIiMAX "\n", $2);
232 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
233 parser->has_new_line_number = 1;
234 parser->new_line_number = $2;
235 parser->has_new_source_number = 1;
236 parser->new_source_number = $3;
237 _mesa_string_buffer_printf(parser->output,
238 "#line %" PRIiMAX " %" PRIiMAX "\n",
244 OBJ_IDENTIFIER replacement_list NEWLINE {
245 _define_object_macro (parser, & @1, $1, $2);
247 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
248 _define_function_macro (parser, & @1, $1, NULL, $4);
250 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
251 _define_function_macro (parser, & @1, $1, $3, $5);
256 control_line_success {
257 _mesa_string_buffer_append_char(parser->output, '\n');
260 | HASH_TOKEN LINE pp_tokens NEWLINE {
262 if (parser->skip_stack == NULL ||
263 parser->skip_stack->type == SKIP_NO_SKIP)
265 _glcpp_parser_expand_and_lex_from (parser,
267 EXPANSION_MODE_IGNORE_DEFINED);
272 control_line_success:
273 HASH_TOKEN DEFINE_TOKEN define
274 | HASH_TOKEN UNDEF IDENTIFIER NEWLINE {
275 struct hash_entry *entry;
277 /* Section 3.4 (Preprocessor) of the GLSL ES 3.00 spec says:
279 * It is an error to undefine or to redefine a built-in
280 * (pre-defined) macro name.
282 * The GLSL ES 1.00 spec does not contain this text, but
283 * dEQP's preprocess test in GLES2 checks for it.
285 * Section 3.3 (Preprocessor) revision 7, of the GLSL 4.50
288 * By convention, all macro names containing two consecutive
289 * underscores ( __ ) are reserved for use by underlying
290 * software layers. Defining or undefining such a name
291 * in a shader does not itself result in an error, but may
292 * result in unintended behaviors that stem from having
293 * multiple definitions of the same name. All macro names
294 * prefixed with "GL_" (...) are also reseved, and defining
295 * such a name results in a compile-time error.
297 * The code below implements the same checks as GLSLang.
299 if (strncmp("GL_", $3, 3) == 0)
300 glcpp_error(& @1, parser, "Built-in (pre-defined)"
301 " names beginning with GL_ cannot be undefined.");
302 else if (strstr($3, "__") != NULL) {
304 && parser->version >= 300
305 && (strcmp("__LINE__", $3) == 0
306 || strcmp("__FILE__", $3) == 0
307 || strcmp("__VERSION__", $3) == 0)) {
308 glcpp_error(& @1, parser, "Built-in (pre-defined)"
309 " names cannot be undefined.");
310 } else if (parser->is_gles && parser->version <= 300) {
311 glcpp_error(& @1, parser,
312 " names containing consecutive underscores"
315 glcpp_warning(& @1, parser,
316 " names containing consecutive underscores"
321 entry = _mesa_hash_table_search (parser->defines, $3);
323 _mesa_hash_table_remove (parser->defines, entry);
326 | HASH_TOKEN IF pp_tokens NEWLINE {
327 /* Be careful to only evaluate the 'if' expression if
328 * we are not skipping. When we are skipping, we
329 * simply push a new 0-valued 'if' onto the skip
332 * This avoids generating diagnostics for invalid
333 * expressions that are being skipped. */
334 if (parser->skip_stack == NULL ||
335 parser->skip_stack->type == SKIP_NO_SKIP)
337 _glcpp_parser_expand_and_lex_from (parser,
339 EXPANSION_MODE_EVALUATE_DEFINED);
343 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
344 parser->skip_stack->type = SKIP_TO_ENDIF;
347 | HASH_TOKEN IF NEWLINE {
348 /* #if without an expression is only an error if we
349 * are not skipping */
350 if (parser->skip_stack == NULL ||
351 parser->skip_stack->type == SKIP_NO_SKIP)
353 glcpp_error(& @1, parser, "#if with no expression");
355 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
357 | HASH_TOKEN IFDEF IDENTIFIER junk NEWLINE {
358 struct hash_entry *entry =
359 _mesa_hash_table_search(parser->defines, $3);
360 macro_t *macro = entry ? entry->data : NULL;
361 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
363 | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE {
364 struct hash_entry *entry =
365 _mesa_hash_table_search(parser->defines, $3);
366 macro_t *macro = entry ? entry->data : NULL;
367 _glcpp_parser_skip_stack_push_if (parser, & @3, macro == NULL);
369 | HASH_TOKEN ELIF pp_tokens NEWLINE {
370 /* Be careful to only evaluate the 'elif' expression
371 * if we are not skipping. When we are skipping, we
372 * simply change to a 0-valued 'elif' on the skip
375 * This avoids generating diagnostics for invalid
376 * expressions that are being skipped. */
377 if (parser->skip_stack &&
378 parser->skip_stack->type == SKIP_TO_ELSE)
380 _glcpp_parser_expand_and_lex_from (parser,
382 EXPANSION_MODE_EVALUATE_DEFINED);
384 else if (parser->skip_stack &&
385 parser->skip_stack->has_else)
387 glcpp_error(& @1, parser, "#elif after #else");
391 _glcpp_parser_skip_stack_change_if (parser, & @1,
395 | HASH_TOKEN ELIF NEWLINE {
396 /* #elif without an expression is an error unless we
398 if (parser->skip_stack &&
399 parser->skip_stack->type == SKIP_TO_ELSE)
401 glcpp_error(& @1, parser, "#elif with no expression");
403 else if (parser->skip_stack &&
404 parser->skip_stack->has_else)
406 glcpp_error(& @1, parser, "#elif after #else");
410 _glcpp_parser_skip_stack_change_if (parser, & @1,
412 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
415 | HASH_TOKEN ELSE { parser->lexing_directive = 1; } NEWLINE {
416 if (parser->skip_stack &&
417 parser->skip_stack->has_else)
419 glcpp_error(& @1, parser, "multiple #else");
423 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
424 if (parser->skip_stack)
425 parser->skip_stack->has_else = true;
429 _glcpp_parser_skip_stack_pop (parser, & @1);
431 | HASH_TOKEN VERSION_TOKEN version_constant NEWLINE {
432 if (parser->version_set) {
433 glcpp_error(& @1, parser, "#version must appear on the first line");
435 _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
437 | HASH_TOKEN VERSION_TOKEN version_constant IDENTIFIER NEWLINE {
438 if (parser->version_set) {
439 glcpp_error(& @1, parser, "#version must appear on the first line");
441 _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
443 | HASH_TOKEN NEWLINE {
444 glcpp_parser_resolve_implicit_version(parser);
446 | HASH_TOKEN PRAGMA NEWLINE {
447 _mesa_string_buffer_printf(parser->output, "#%s", $2);
452 HASH_TOKEN ERROR_TOKEN NEWLINE {
453 glcpp_error(& @1, parser, "#%s", $2);
455 | HASH_TOKEN DEFINE_TOKEN NEWLINE {
456 glcpp_error (& @1, parser, "#define without macro name");
458 | HASH_TOKEN GARBAGE pp_tokens NEWLINE {
459 glcpp_error (& @1, parser, "Illegal non-directive after #");
465 /* let strtoll detect the base */
466 $$ = strtoll ($1, NULL, 0);
474 /* Both octal and hexadecimal constants begin with 0. */
475 if ($1[0] == '0' && $1[1] != '\0') {
476 glcpp_error(&@1, parser, "invalid #version \"%s\" (not a decimal constant)", $1);
479 $$ = strtoll($1, NULL, 10);
486 $$.undefined_macro = NULL;
491 $$.undefined_macro = linear_strdup(parser->linalloc, $1);
493 $$.undefined_macro = NULL;
495 | expression OR expression {
496 $$.value = $1.value || $3.value;
498 /* Short-circuit: Only flag undefined from right side
499 * if left side evaluates to false.
501 if ($1.undefined_macro)
502 $$.undefined_macro = $1.undefined_macro;
504 $$.undefined_macro = $3.undefined_macro;
506 | expression AND expression {
507 $$.value = $1.value && $3.value;
509 /* Short-circuit: Only flag undefined from right-side
510 * if left side evaluates to true.
512 if ($1.undefined_macro)
513 $$.undefined_macro = $1.undefined_macro;
515 $$.undefined_macro = $3.undefined_macro;
517 | expression '|' expression {
518 $$.value = $1.value | $3.value;
519 if ($1.undefined_macro)
520 $$.undefined_macro = $1.undefined_macro;
522 $$.undefined_macro = $3.undefined_macro;
524 | expression '^' expression {
525 $$.value = $1.value ^ $3.value;
526 if ($1.undefined_macro)
527 $$.undefined_macro = $1.undefined_macro;
529 $$.undefined_macro = $3.undefined_macro;
531 | expression '&' expression {
532 $$.value = $1.value & $3.value;
533 if ($1.undefined_macro)
534 $$.undefined_macro = $1.undefined_macro;
536 $$.undefined_macro = $3.undefined_macro;
538 | expression NOT_EQUAL expression {
539 $$.value = $1.value != $3.value;
540 if ($1.undefined_macro)
541 $$.undefined_macro = $1.undefined_macro;
543 $$.undefined_macro = $3.undefined_macro;
545 | expression EQUAL expression {
546 $$.value = $1.value == $3.value;
547 if ($1.undefined_macro)
548 $$.undefined_macro = $1.undefined_macro;
550 $$.undefined_macro = $3.undefined_macro;
552 | expression GREATER_OR_EQUAL expression {
553 $$.value = $1.value >= $3.value;
554 if ($1.undefined_macro)
555 $$.undefined_macro = $1.undefined_macro;
557 $$.undefined_macro = $3.undefined_macro;
559 | expression LESS_OR_EQUAL expression {
560 $$.value = $1.value <= $3.value;
561 if ($1.undefined_macro)
562 $$.undefined_macro = $1.undefined_macro;
564 $$.undefined_macro = $3.undefined_macro;
566 | expression '>' expression {
567 $$.value = $1.value > $3.value;
568 if ($1.undefined_macro)
569 $$.undefined_macro = $1.undefined_macro;
571 $$.undefined_macro = $3.undefined_macro;
573 | expression '<' expression {
574 $$.value = $1.value < $3.value;
575 if ($1.undefined_macro)
576 $$.undefined_macro = $1.undefined_macro;
578 $$.undefined_macro = $3.undefined_macro;
580 | expression RIGHT_SHIFT expression {
581 $$.value = $1.value >> $3.value;
582 if ($1.undefined_macro)
583 $$.undefined_macro = $1.undefined_macro;
585 $$.undefined_macro = $3.undefined_macro;
587 | expression LEFT_SHIFT expression {
588 $$.value = $1.value << $3.value;
589 if ($1.undefined_macro)
590 $$.undefined_macro = $1.undefined_macro;
592 $$.undefined_macro = $3.undefined_macro;
594 | expression '-' expression {
595 $$.value = $1.value - $3.value;
596 if ($1.undefined_macro)
597 $$.undefined_macro = $1.undefined_macro;
599 $$.undefined_macro = $3.undefined_macro;
601 | expression '+' expression {
602 $$.value = $1.value + $3.value;
603 if ($1.undefined_macro)
604 $$.undefined_macro = $1.undefined_macro;
606 $$.undefined_macro = $3.undefined_macro;
608 | expression '%' expression {
610 yyerror (& @1, parser,
611 "zero modulus in preprocessor directive");
613 $$.value = $1.value % $3.value;
615 if ($1.undefined_macro)
616 $$.undefined_macro = $1.undefined_macro;
618 $$.undefined_macro = $3.undefined_macro;
620 | expression '/' expression {
622 yyerror (& @1, parser,
623 "division by 0 in preprocessor directive");
625 $$.value = $1.value / $3.value;
627 if ($1.undefined_macro)
628 $$.undefined_macro = $1.undefined_macro;
630 $$.undefined_macro = $3.undefined_macro;
632 | expression '*' expression {
633 $$.value = $1.value * $3.value;
634 if ($1.undefined_macro)
635 $$.undefined_macro = $1.undefined_macro;
637 $$.undefined_macro = $3.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 %prec UNARY {
652 $$.value = + $2.value;
653 $$.undefined_macro = $2.undefined_macro;
655 | '(' expression ')' {
662 $$ = _string_list_create (parser);
663 _string_list_append_item (parser, $$, $1);
665 | identifier_list ',' IDENTIFIER {
667 _string_list_append_item (parser, $$, $3);
672 NEWLINE { $$ = NULL; }
677 /* empty */ { $$ = NULL; }
684 glcpp_error(&@1, parser, "extra tokens at end of directive");
689 preprocessing_token {
690 parser->space_tokens = 1;
691 $$ = _token_list_create (parser);
692 _token_list_append (parser, $$, $1);
694 | pp_tokens preprocessing_token {
696 _token_list_append (parser, $$, $2);
702 $$ = _token_create_str (parser, IDENTIFIER, $1);
703 $$->location = yylloc;
706 $$ = _token_create_str (parser, INTEGER_STRING, $1);
707 $$->location = yylloc;
710 $$ = _token_create_ival (parser, $1, $1);
711 $$->location = yylloc;
714 $$ = _token_create_ival (parser, DEFINED, DEFINED);
715 $$->location = yylloc;
718 $$ = _token_create_str (parser, OTHER, $1);
719 $$->location = yylloc;
722 $$ = _token_create_ival (parser, SPACE, SPACE);
723 $$->location = yylloc;
743 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
744 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
747 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
748 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
749 | EQUAL { $$ = EQUAL; }
750 | NOT_EQUAL { $$ = NOT_EQUAL; }
758 | PASTE { $$ = PASTE; }
759 | PLUS_PLUS { $$ = PLUS_PLUS; }
760 | MINUS_MINUS { $$ = MINUS_MINUS; }
766 _string_list_create(glcpp_parser_t *parser)
770 list = linear_alloc_child(parser->linalloc, sizeof(string_list_t));
778 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
783 node = linear_alloc_child(parser->linalloc, sizeof(string_node_t));
784 node->str = linear_strdup(parser->linalloc, str);
788 if (list->head == NULL) {
791 list->tail->next = node;
798 _string_list_contains(string_list_t *list, const char *member, int *index)
806 for (i = 0, node = list->head; node; i++, node = node->next) {
807 if (strcmp (node->str, member) == 0) {
817 /* Return duplicate string in list (if any), NULL otherwise. */
819 _string_list_has_duplicate(string_list_t *list)
821 string_node_t *node, *dup;
826 for (node = list->head; node; node = node->next) {
827 for (dup = node->next; dup; dup = dup->next) {
828 if (strcmp (node->str, dup->str) == 0)
837 _string_list_length(string_list_t *list)
845 for (node = list->head; node; node = node->next)
852 _string_list_equal(string_list_t *a, string_list_t *b)
854 string_node_t *node_a, *node_b;
856 if (a == NULL && b == NULL)
859 if (a == NULL || b == NULL)
862 for (node_a = a->head, node_b = b->head;
864 node_a = node_a->next, node_b = node_b->next)
866 if (strcmp (node_a->str, node_b->str))
870 /* Catch the case of lists being different lengths, (which
871 * would cause the loop above to terminate after the shorter
873 return node_a == node_b;
877 _argument_list_create(glcpp_parser_t *parser)
879 argument_list_t *list;
881 list = linear_alloc_child(parser->linalloc, sizeof(argument_list_t));
889 _argument_list_append(glcpp_parser_t *parser,
890 argument_list_t *list, token_list_t *argument)
892 argument_node_t *node;
894 node = linear_alloc_child(parser->linalloc, sizeof(argument_node_t));
895 node->argument = argument;
899 if (list->head == NULL) {
902 list->tail->next = node;
909 _argument_list_length(argument_list_t *list)
912 argument_node_t *node;
917 for (node = list->head; node; node = node->next)
924 _argument_list_member_at(argument_list_t *list, int index)
926 argument_node_t *node;
933 for (i = 0; i < index; i++) {
940 return node->argument;
946 _token_create_str(glcpp_parser_t *parser, int type, char *str)
950 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
952 token->value.str = str;
958 _token_create_ival(glcpp_parser_t *parser, int type, int ival)
962 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
964 token->value.ival = ival;
970 _token_list_create(glcpp_parser_t *parser)
974 list = linear_alloc_child(parser->linalloc, sizeof(token_list_t));
977 list->non_space_tail = NULL;
983 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token)
987 node = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
991 if (list->head == NULL) {
994 list->tail->next = node;
998 if (token->type != SPACE)
999 list->non_space_tail = node;
1003 _token_list_append_list(token_list_t *list, token_list_t *tail)
1005 if (tail == NULL || tail->head == NULL)
1008 if (list->head == NULL) {
1009 list->head = tail->head;
1011 list->tail->next = tail->head;
1014 list->tail = tail->tail;
1015 list->non_space_tail = tail->non_space_tail;
1018 static token_list_t *
1019 _token_list_copy(glcpp_parser_t *parser, token_list_t *other)
1027 copy = _token_list_create (parser);
1028 for (node = other->head; node; node = node->next) {
1029 token_t *new_token = linear_alloc_child(parser->linalloc, sizeof(token_t));
1030 *new_token = *node->token;
1031 _token_list_append (parser, copy, new_token);
1038 _token_list_trim_trailing_space(token_list_t *list)
1040 if (list->non_space_tail) {
1041 list->non_space_tail->next = NULL;
1042 list->tail = list->non_space_tail;
1047 _token_list_is_empty_ignoring_space(token_list_t *l)
1055 while (n != NULL && n->token->type == SPACE)
1062 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1064 token_node_t *node_a, *node_b;
1066 if (a == NULL || b == NULL) {
1067 int a_empty = _token_list_is_empty_ignoring_space(a);
1068 int b_empty = _token_list_is_empty_ignoring_space(b);
1069 return a_empty == b_empty;
1077 if (node_a == NULL && node_b == NULL)
1080 /* Ignore trailing whitespace */
1081 if (node_a == NULL && node_b->token->type == SPACE) {
1082 while (node_b && node_b->token->type == SPACE)
1083 node_b = node_b->next;
1086 if (node_b == NULL && node_a->token->type == SPACE) {
1087 while (node_a && node_a->token->type == SPACE)
1088 node_a = node_a->next;
1091 if (node_a == NULL && node_b == NULL)
1094 if (node_a == NULL || node_b == NULL)
1096 /* Make sure whitespace appears in the same places in both.
1097 * It need not be exactly the same amount of whitespace,
1100 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1101 while (node_a && node_a->token->type == SPACE)
1102 node_a = node_a->next;
1103 while (node_b && node_b->token->type == SPACE)
1104 node_b = node_b->next;
1108 if (node_a->token->type != node_b->token->type)
1111 switch (node_a->token->type) {
1113 if (node_a->token->value.ival != node_b->token->value.ival) {
1118 case INTEGER_STRING:
1120 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1126 node_a = node_a->next;
1127 node_b = node_b->next;
1134 _token_print(struct _mesa_string_buffer *out, token_t *token)
1136 if (token->type < 256) {
1137 _mesa_string_buffer_append_char(out, token->type);
1141 switch (token->type) {
1143 _mesa_string_buffer_printf(out, "%" PRIiMAX, token->value.ival);
1146 case INTEGER_STRING:
1148 _mesa_string_buffer_append(out, token->value.str);
1151 _mesa_string_buffer_append_char(out, ' ');
1154 _mesa_string_buffer_append(out, "<<");
1157 _mesa_string_buffer_append(out, ">>");
1160 _mesa_string_buffer_append(out, "<=");
1162 case GREATER_OR_EQUAL:
1163 _mesa_string_buffer_append(out, ">=");
1166 _mesa_string_buffer_append(out, "==");
1169 _mesa_string_buffer_append(out, "!=");
1172 _mesa_string_buffer_append(out, "&&");
1175 _mesa_string_buffer_append(out, "||");
1178 _mesa_string_buffer_append(out, "##");
1181 _mesa_string_buffer_append(out, "++");
1184 _mesa_string_buffer_append(out, "--");
1187 _mesa_string_buffer_append(out, "defined");
1190 /* Nothing to print. */
1193 assert(!"Error: Don't know how to print token.");
1199 /* Return a new token formed by pasting 'token' and 'other'. Note that this
1200 * function may return 'token' or 'other' directly rather than allocating
1203 * Caution: Only very cursory error-checking is performed to see if
1204 * the final result is a valid single token. */
1206 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1208 token_t *combined = NULL;
1210 /* Pasting a placeholder onto anything makes no change. */
1211 if (other->type == PLACEHOLDER)
1214 /* When 'token' is a placeholder, just return 'other'. */
1215 if (token->type == PLACEHOLDER)
1218 /* A very few single-character punctuators can be combined
1219 * with another to form a multi-character punctuator. */
1220 switch (token->type) {
1222 if (other->type == '<')
1223 combined = _token_create_ival (parser, LEFT_SHIFT, LEFT_SHIFT);
1224 else if (other->type == '=')
1225 combined = _token_create_ival (parser, LESS_OR_EQUAL, LESS_OR_EQUAL);
1228 if (other->type == '>')
1229 combined = _token_create_ival (parser, RIGHT_SHIFT, RIGHT_SHIFT);
1230 else if (other->type == '=')
1231 combined = _token_create_ival (parser, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1234 if (other->type == '=')
1235 combined = _token_create_ival (parser, EQUAL, EQUAL);
1238 if (other->type == '=')
1239 combined = _token_create_ival (parser, NOT_EQUAL, NOT_EQUAL);
1242 if (other->type == '&')
1243 combined = _token_create_ival (parser, AND, AND);
1246 if (other->type == '|')
1247 combined = _token_create_ival (parser, OR, OR);
1251 if (combined != NULL) {
1252 /* Inherit the location from the first token */
1253 combined->location = token->location;
1257 /* Two string-valued (or integer) tokens can usually just be
1258 * mashed together. (We also handle a string followed by an
1259 * integer here as well.)
1261 * There are some exceptions here. Notably, if the first token
1262 * is an integer (or a string representing an integer), then
1263 * the second token must also be an integer or must be a
1264 * string representing an integer that begins with a digit.
1266 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1267 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1272 /* Check that pasting onto an integer doesn't create a
1273 * non-integer, (that is, only digits can be
1275 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1276 switch (other->type) {
1277 case INTEGER_STRING:
1278 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1282 if (other->value.ival < 0)
1290 if (token->type == INTEGER)
1291 str = linear_asprintf(parser->linalloc, "%" PRIiMAX, token->value.ival);
1293 str = linear_strdup(parser->linalloc, token->value.str);
1295 if (other->type == INTEGER)
1296 linear_asprintf_append(parser->linalloc, &str, "%" PRIiMAX, other->value.ival);
1298 linear_strcat(parser->linalloc, &str, other->value.str);
1300 /* New token is same type as original token, unless we
1301 * started with an integer, in which case we will be
1302 * creating an integer-string. */
1303 combined_type = token->type;
1304 if (combined_type == INTEGER)
1305 combined_type = INTEGER_STRING;
1307 combined = _token_create_str (parser, combined_type, str);
1308 combined->location = token->location;
1313 glcpp_error (&token->location, parser, "");
1314 _mesa_string_buffer_append(parser->info_log, "Pasting \"");
1315 _token_print(parser->info_log, token);
1316 _mesa_string_buffer_append(parser->info_log, "\" and \"");
1317 _token_print(parser->info_log, other);
1318 _mesa_string_buffer_append(parser->info_log, "\" does not give a valid preprocessing token.\n");
1324 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1331 for (node = list->head; node; node = node->next)
1332 _token_print(parser->output, node->token);
1336 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1338 glcpp_error(locp, parser, "%s", error);
1342 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1347 tok = _token_create_ival (parser, INTEGER, value);
1349 list = _token_list_create(parser);
1350 _token_list_append(parser, list, tok);
1351 _define_object_macro(parser, NULL, name, list);
1354 /* Initial output buffer size, 4096 minus ralloc() overhead. It was selected
1355 * to minimize total amount of allocated memory during shader-db run.
1357 #define INITIAL_PP_OUTPUT_BUF_SIZE 4048
1360 glcpp_parser_create(const struct gl_extensions *extension_list,
1361 glcpp_extension_iterator extensions, void *state, gl_api api)
1363 glcpp_parser_t *parser;
1365 parser = ralloc (NULL, glcpp_parser_t);
1367 glcpp_lex_init_extra (parser, &parser->scanner);
1368 parser->defines = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
1369 _mesa_key_string_equal);
1370 parser->linalloc = linear_alloc_parent(parser, 0);
1371 parser->active = NULL;
1372 parser->lexing_directive = 0;
1373 parser->lexing_version_directive = 0;
1374 parser->space_tokens = 1;
1375 parser->last_token_was_newline = 0;
1376 parser->last_token_was_space = 0;
1377 parser->first_non_space_token_this_line = 1;
1378 parser->newline_as_space = 0;
1379 parser->in_control_line = 0;
1380 parser->paren_count = 0;
1381 parser->commented_newlines = 0;
1383 parser->skip_stack = NULL;
1384 parser->skipping = 0;
1386 parser->lex_from_list = NULL;
1387 parser->lex_from_node = NULL;
1389 parser->output = _mesa_string_buffer_create(parser,
1390 INITIAL_PP_OUTPUT_BUF_SIZE);
1391 parser->info_log = _mesa_string_buffer_create(parser,
1392 INITIAL_PP_OUTPUT_BUF_SIZE);
1395 parser->extensions = extensions;
1396 parser->extension_list = extension_list;
1397 parser->state = state;
1399 parser->version = 0;
1400 parser->version_set = false;
1402 parser->has_new_line_number = 0;
1403 parser->new_line_number = 1;
1404 parser->has_new_source_number = 0;
1405 parser->new_source_number = 0;
1407 parser->is_gles = false;
1413 glcpp_parser_destroy(glcpp_parser_t *parser)
1415 glcpp_lex_destroy (parser->scanner);
1416 _mesa_hash_table_destroy(parser->defines, NULL);
1417 ralloc_free (parser);
1420 typedef enum function_status
1422 FUNCTION_STATUS_SUCCESS,
1423 FUNCTION_NOT_A_FUNCTION,
1424 FUNCTION_UNBALANCED_PARENTHESES
1425 } function_status_t;
1427 /* Find a set of function-like macro arguments by looking for a
1428 * balanced set of parentheses.
1430 * When called, 'node' should be the opening-parenthesis token, (or
1431 * perhaps preceeding SPACE tokens). Upon successful return *last will
1432 * be the last consumed node, (corresponding to the closing right
1437 * FUNCTION_STATUS_SUCCESS:
1439 * Successfully parsed a set of function arguments.
1441 * FUNCTION_NOT_A_FUNCTION:
1443 * Macro name not followed by a '('. This is not an error, but
1444 * simply that the macro name should be treated as a non-macro.
1446 * FUNCTION_UNBALANCED_PARENTHESES
1448 * Macro name is not followed by a balanced set of parentheses.
1450 static function_status_t
1451 _arguments_parse(glcpp_parser_t *parser,
1452 argument_list_t *arguments, token_node_t *node,
1453 token_node_t **last)
1455 token_list_t *argument;
1460 /* Ignore whitespace before first parenthesis. */
1461 while (node && node->token->type == SPACE)
1464 if (node == NULL || node->token->type != '(')
1465 return FUNCTION_NOT_A_FUNCTION;
1469 argument = _token_list_create (parser);
1470 _argument_list_append (parser, arguments, argument);
1472 for (paren_count = 1; node; node = node->next) {
1473 if (node->token->type == '(') {
1475 } else if (node->token->type == ')') {
1477 if (paren_count == 0)
1481 if (node->token->type == ',' && paren_count == 1) {
1482 _token_list_trim_trailing_space (argument);
1483 argument = _token_list_create (parser);
1484 _argument_list_append (parser, arguments, argument);
1486 if (argument->head == NULL) {
1487 /* Don't treat initial whitespace as part of the argument. */
1488 if (node->token->type == SPACE)
1491 _token_list_append(parser, argument, node->token);
1496 return FUNCTION_UNBALANCED_PARENTHESES;
1500 return FUNCTION_STATUS_SUCCESS;
1503 static token_list_t *
1504 _token_list_create_with_one_ival(glcpp_parser_t *parser, int type, int ival)
1509 list = _token_list_create(parser);
1510 node = _token_create_ival(parser, type, ival);
1511 _token_list_append(parser, list, node);
1516 static token_list_t *
1517 _token_list_create_with_one_space(glcpp_parser_t *parser)
1519 return _token_list_create_with_one_ival(parser, SPACE, SPACE);
1522 static token_list_t *
1523 _token_list_create_with_one_integer(glcpp_parser_t *parser, int ival)
1525 return _token_list_create_with_one_ival(parser, INTEGER, ival);
1528 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1530 * Note: This function must only be called when "node" is a DEFINED token,
1531 * (and will abort with an assertion failure otherwise).
1533 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1534 * (optionally preceded and followed by '(' and ')' tokens) then the following
1537 * If the identifier is a defined macro, this function returns 1.
1539 * If the identifier is not a defined macro, this function returns 0.
1541 * In either case, *last will be updated to the last node in the list
1542 * consumed by the evaluation, (either the token of the identifier or the
1543 * token of the closing parenthesis).
1545 * In all other cases, (such as "node is the final node of the list", or
1546 * "missing closing parenthesis", etc.), this function generates a
1547 * preprocessor error, returns -1 and *last will not be set.
1550 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1551 token_node_t **last)
1553 token_node_t *argument, *defined = node;
1555 assert(node->token->type == DEFINED);
1559 /* Ignore whitespace after DEFINED token. */
1560 while (node && node->token->type == SPACE)
1566 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1568 } else if (node->token->type == '(') {
1571 /* Ignore whitespace after '(' token. */
1572 while (node && node->token->type == SPACE)
1575 if (node == NULL || (node->token->type != IDENTIFIER &&
1576 node->token->type != OTHER)) {
1584 /* Ignore whitespace after identifier, before ')' token. */
1585 while (node && node->token->type == SPACE)
1588 if (node == NULL || node->token->type != ')')
1596 return _mesa_hash_table_search(parser->defines,
1597 argument->token->value.str) ? 1 : 0;
1600 glcpp_error (&defined->token->location, parser,
1601 "\"defined\" not followed by an identifier");
1605 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1608 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1611 token_node_t *node, *node_prev, *replacement, *last = NULL;
1622 if (node->token->type != DEFINED)
1625 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1629 replacement = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
1630 replacement->token = _token_create_ival (parser, INTEGER, value);
1632 /* Splice replacement node into list, replacing from "node"
1633 * through "last". */
1635 node_prev->next = replacement;
1637 list->head = replacement;
1638 replacement->next = last->next;
1639 if (last == list->tail)
1640 list->tail = replacement;
1650 /* Perform macro expansion on 'list', placing the resulting tokens
1651 * into a new list which is initialized with a first token of type
1652 * 'head_token_type'. Then begin lexing from the resulting list,
1653 * (return to the current lexing source when this list is exhausted).
1655 * See the documentation of _glcpp_parser_expand_token_list for a description
1656 * of the "mode" parameter.
1659 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1660 token_list_t *list, expansion_mode_t mode)
1662 token_list_t *expanded;
1665 expanded = _token_list_create (parser);
1666 token = _token_create_ival (parser, head_token_type, head_token_type);
1667 _token_list_append (parser, expanded, token);
1668 _glcpp_parser_expand_token_list (parser, list, mode);
1669 _token_list_append_list (expanded, list);
1670 glcpp_parser_lex_from (parser, expanded);
1674 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1680 token_node_t *next_non_space;
1682 /* Look ahead for a PASTE token, skipping space. */
1683 next_non_space = node->next;
1684 while (next_non_space && next_non_space->token->type == SPACE)
1685 next_non_space = next_non_space->next;
1687 if (next_non_space == NULL)
1690 if (next_non_space->token->type != PASTE) {
1691 node = next_non_space;
1695 /* Now find the next non-space token after the PASTE. */
1696 next_non_space = next_non_space->next;
1697 while (next_non_space && next_non_space->token->type == SPACE)
1698 next_non_space = next_non_space->next;
1700 if (next_non_space == NULL) {
1701 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1705 node->token = _token_paste(parser, node->token, next_non_space->token);
1706 node->next = next_non_space->next;
1707 if (next_non_space == list->tail)
1711 list->non_space_tail = list->tail;
1714 /* This is a helper function that's essentially part of the
1715 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1716 * except for by that function.
1718 * Returns NULL if node is a simple token with no expansion, (that is,
1719 * although 'node' corresponds to an identifier defined as a
1720 * function-like macro, it is not followed with a parenthesized
1723 * Compute the complete expansion of node (which is a function-like
1724 * macro) and subsequent nodes which are arguments.
1726 * Returns the token list that results from the expansion and sets
1727 * *last to the last node in the list that was consumed by the
1728 * expansion. Specifically, *last will be set as follows: as the
1729 * token of the closing right parenthesis.
1731 * See the documentation of _glcpp_parser_expand_token_list for a description
1732 * of the "mode" parameter.
1734 static token_list_t *
1735 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1736 token_node_t **last, expansion_mode_t mode)
1738 struct hash_entry *entry;
1740 const char *identifier;
1741 argument_list_t *arguments;
1742 function_status_t status;
1743 token_list_t *substituted;
1744 int parameter_index;
1746 identifier = node->token->value.str;
1748 entry = _mesa_hash_table_search(parser->defines, identifier);
1749 macro = entry ? entry->data : NULL;
1751 assert(macro->is_function);
1753 arguments = _argument_list_create(parser);
1754 status = _arguments_parse(parser, arguments, node, last);
1757 case FUNCTION_STATUS_SUCCESS:
1759 case FUNCTION_NOT_A_FUNCTION:
1761 case FUNCTION_UNBALANCED_PARENTHESES:
1762 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1766 /* Replace a macro defined as empty with a SPACE token. */
1767 if (macro->replacements == NULL) {
1768 return _token_list_create_with_one_space(parser);
1771 if (!((_argument_list_length (arguments) ==
1772 _string_list_length (macro->parameters)) ||
1773 (_string_list_length (macro->parameters) == 0 &&
1774 _argument_list_length (arguments) == 1 &&
1775 arguments->head->argument->head == NULL))) {
1776 glcpp_error(&node->token->location, parser,
1777 "Error: macro %s invoked with %d arguments (expected %d)\n",
1778 identifier, _argument_list_length (arguments),
1779 _string_list_length(macro->parameters));
1783 /* Perform argument substitution on the replacement list. */
1784 substituted = _token_list_create(parser);
1786 for (node = macro->replacements->head; node; node = node->next) {
1787 if (node->token->type == IDENTIFIER &&
1788 _string_list_contains(macro->parameters, node->token->value.str,
1789 ¶meter_index)) {
1790 token_list_t *argument;
1791 argument = _argument_list_member_at(arguments, parameter_index);
1792 /* Before substituting, we expand the argument tokens, or append a
1793 * placeholder token for an empty argument. */
1794 if (argument->head) {
1795 token_list_t *expanded_argument;
1796 expanded_argument = _token_list_copy(parser, argument);
1797 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1798 _token_list_append_list(substituted, expanded_argument);
1802 new_token = _token_create_ival(parser, PLACEHOLDER,
1804 _token_list_append(parser, substituted, new_token);
1807 _token_list_append(parser, substituted, node->token);
1811 /* After argument substitution, and before further expansion
1812 * below, implement token pasting. */
1814 _token_list_trim_trailing_space(substituted);
1816 _glcpp_parser_apply_pastes(parser, substituted);
1821 /* Compute the complete expansion of node, (and subsequent nodes after
1822 * 'node' in the case that 'node' is a function-like macro and
1823 * subsequent nodes are arguments).
1825 * Returns NULL if node is a simple token with no expansion.
1827 * Otherwise, returns the token list that results from the expansion
1828 * and sets *last to the last node in the list that was consumed by
1829 * the expansion. Specifically, *last will be set as follows:
1831 * As 'node' in the case of object-like macro expansion.
1833 * As the token of the closing right parenthesis in the case of
1834 * function-like macro expansion.
1836 * See the documentation of _glcpp_parser_expand_token_list for a description
1837 * of the "mode" parameter.
1839 static token_list_t *
1840 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1841 token_node_t **last, expansion_mode_t mode)
1843 token_t *token = node->token;
1844 const char *identifier;
1845 struct hash_entry *entry;
1848 /* We only expand identifiers */
1849 if (token->type != IDENTIFIER) {
1854 identifier = token->value.str;
1856 /* Special handling for __LINE__ and __FILE__, (not through
1857 * the hash table). */
1858 if (*identifier == '_') {
1859 if (strcmp(identifier, "__LINE__") == 0)
1860 return _token_list_create_with_one_integer(parser,
1861 node->token->location.first_line);
1863 if (strcmp(identifier, "__FILE__") == 0)
1864 return _token_list_create_with_one_integer(parser,
1865 node->token->location.source);
1868 /* Look up this identifier in the hash table. */
1869 entry = _mesa_hash_table_search(parser->defines, identifier);
1870 macro = entry ? entry->data : NULL;
1872 /* Not a macro, so no expansion needed. */
1876 /* Finally, don't expand this macro if we're already actively
1877 * expanding it, (to avoid infinite recursion). */
1878 if (_parser_active_list_contains (parser, identifier)) {
1879 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1880 * future expansion of this unexpanded token. */
1882 token_list_t *expansion;
1885 str = linear_strdup(parser->linalloc, token->value.str);
1886 final = _token_create_str(parser, OTHER, str);
1887 expansion = _token_list_create(parser);
1888 _token_list_append(parser, expansion, final);
1892 if (! macro->is_function) {
1893 token_list_t *replacement;
1895 /* Replace a macro defined as empty with a SPACE token. */
1896 if (macro->replacements == NULL)
1897 return _token_list_create_with_one_space(parser);
1899 replacement = _token_list_copy(parser, macro->replacements);
1900 _glcpp_parser_apply_pastes(parser, replacement);
1904 return _glcpp_parser_expand_function(parser, node, last, mode);
1907 /* Push a new identifier onto the parser's active list.
1909 * Here, 'marker' is the token node that appears in the list after the
1910 * expansion of 'identifier'. That is, when the list iterator begins
1911 * examining 'marker', then it is time to pop this node from the
1915 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
1916 token_node_t *marker)
1918 active_list_t *node;
1920 node = linear_alloc_child(parser->linalloc, sizeof(active_list_t));
1921 node->identifier = linear_strdup(parser->linalloc, identifier);
1922 node->marker = marker;
1923 node->next = parser->active;
1925 parser->active = node;
1929 _parser_active_list_pop(glcpp_parser_t *parser)
1931 active_list_t *node = parser->active;
1934 parser->active = NULL;
1938 node = parser->active->next;
1939 parser->active = node;
1943 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
1945 active_list_t *node;
1947 if (parser->active == NULL)
1950 for (node = parser->active; node; node = node->next)
1951 if (strcmp(node->identifier, identifier) == 0)
1957 /* Walk over the token list replacing nodes with their expansion.
1958 * Whenever nodes are expanded the walking will walk over the new
1959 * nodes, continuing to expand as necessary. The results are placed in
1962 * The "mode" argument controls the handling of any DEFINED tokens that
1963 * result from expansion as follows:
1965 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
1966 * left in the final list, unevaluated. This is the correct mode
1967 * for expanding any list in any context other than a
1968 * preprocessor conditional, (#if or #elif).
1970 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
1971 * evaluated to 0 or 1 tokens depending on whether the following
1972 * token is the name of a defined macro. If the DEFINED token is
1973 * not followed by an (optionally parenthesized) identifier, then
1974 * an error will be generated. This the correct mode for
1975 * expanding any list in the context of a preprocessor
1976 * conditional, (#if or #elif).
1979 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
1980 expansion_mode_t mode)
1982 token_node_t *node_prev;
1983 token_node_t *node, *last = NULL;
1984 token_list_t *expansion;
1985 active_list_t *active_initial = parser->active;
1990 _token_list_trim_trailing_space (list);
1995 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
1996 _glcpp_parser_evaluate_defined_in_list (parser, list);
2000 while (parser->active && parser->active->marker == node)
2001 _parser_active_list_pop (parser);
2003 expansion = _glcpp_parser_expand_node (parser, node, &last, mode);
2007 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
2008 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
2011 for (n = node; n != last->next; n = n->next)
2012 while (parser->active && parser->active->marker == n) {
2013 _parser_active_list_pop (parser);
2016 _parser_active_list_push(parser, node->token->value.str, last->next);
2018 /* Splice expansion into list, supporting a simple deletion if the
2019 * expansion is empty.
2021 if (expansion->head) {
2023 node_prev->next = expansion->head;
2025 list->head = expansion->head;
2026 expansion->tail->next = last->next;
2027 if (last == list->tail)
2028 list->tail = expansion->tail;
2031 node_prev->next = last->next;
2033 list->head = last->next;
2034 if (last == list->tail)
2040 node = node_prev ? node_prev->next : list->head;
2043 /* Remove any lingering effects of this invocation on the
2044 * active list. That is, pop until the list looks like it did
2045 * at the beginning of this function. */
2046 while (parser->active && parser->active != active_initial)
2047 _parser_active_list_pop (parser);
2049 list->non_space_tail = list->tail;
2053 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
2059 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2061 _token_list_trim_trailing_space (list);
2063 _token_list_print (parser, list);
2067 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2068 const char *identifier)
2070 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2071 * the GLSL ES spec (all versions) say:
2073 * "All macro names containing two consecutive underscores ( __ )
2074 * are reserved for future use as predefined macro names. All
2075 * macro names prefixed with "GL_" ("GL" followed by a single
2076 * underscore) are also reserved."
2078 * The intention is that names containing __ are reserved for internal
2079 * use by the implementation, and names prefixed with GL_ are reserved
2080 * for use by Khronos. Since every extension adds a name prefixed
2081 * with GL_ (i.e., the name of the extension), that should be an
2082 * error. Names simply containing __ are dangerous to use, but should
2085 * A future version of the GLSL specification will clarify this.
2087 if (strstr(identifier, "__")) {
2088 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2089 "for use by the implementation.\n");
2091 if (strncmp(identifier, "GL_", 3) == 0) {
2092 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2094 if (strcmp(identifier, "defined") == 0) {
2095 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2100 _macro_equal(macro_t *a, macro_t *b)
2102 if (a->is_function != b->is_function)
2105 if (a->is_function) {
2106 if (! _string_list_equal (a->parameters, b->parameters))
2110 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2114 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2115 const char *identifier, token_list_t *replacements)
2117 macro_t *macro, *previous;
2118 struct hash_entry *entry;
2120 /* We define pre-defined macros before we've started parsing the actual
2121 * file. So if there's no location defined yet, that's what were doing and
2122 * we don't want to generate an error for using the reserved names. */
2124 _check_for_reserved_macro_name(parser, loc, identifier);
2126 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2128 macro->is_function = 0;
2129 macro->parameters = NULL;
2130 macro->identifier = linear_strdup(parser->linalloc, identifier);
2131 macro->replacements = replacements;
2133 entry = _mesa_hash_table_search(parser->defines, identifier);
2134 previous = entry ? entry->data : NULL;
2136 if (_macro_equal (macro, previous)) {
2139 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2142 _mesa_hash_table_insert (parser->defines, identifier, macro);
2146 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2147 const char *identifier, string_list_t *parameters,
2148 token_list_t *replacements)
2150 macro_t *macro, *previous;
2151 struct hash_entry *entry;
2154 _check_for_reserved_macro_name(parser, loc, identifier);
2156 /* Check for any duplicate parameter names. */
2157 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2158 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2161 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2163 macro->is_function = 1;
2164 macro->parameters = parameters;
2165 macro->identifier = linear_strdup(parser->linalloc, identifier);
2166 macro->replacements = replacements;
2168 entry = _mesa_hash_table_search(parser->defines, identifier);
2169 previous = entry ? entry->data : NULL;
2171 if (_macro_equal (macro, previous)) {
2174 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2177 _mesa_hash_table_insert(parser->defines, identifier, macro);
2181 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2186 if (parser->lex_from_list == NULL) {
2187 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2189 /* XXX: This ugly block of code exists for the sole
2190 * purpose of converting a NEWLINE token into a SPACE
2191 * token, but only in the case where we have seen a
2192 * function-like macro name, but have not yet seen its
2193 * closing parenthesis.
2195 * There's perhaps a more compact way to do this with
2196 * mid-rule actions in the grammar.
2198 * I'm definitely not pleased with the complexity of
2201 if (parser->newline_as_space) {
2203 parser->paren_count++;
2204 } else if (ret == ')') {
2205 parser->paren_count--;
2206 if (parser->paren_count == 0)
2207 parser->newline_as_space = 0;
2208 } else if (ret == NEWLINE) {
2210 } else if (ret != SPACE) {
2211 if (parser->paren_count == 0)
2212 parser->newline_as_space = 0;
2214 } else if (parser->in_control_line) {
2216 parser->in_control_line = 0;
2218 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2219 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2220 ret == ENDIF || ret == HASH_TOKEN) {
2221 parser->in_control_line = 1;
2222 } else if (ret == IDENTIFIER) {
2223 struct hash_entry *entry = _mesa_hash_table_search(parser->defines,
2225 macro_t *macro = entry ? entry->data : NULL;
2226 if (macro && macro->is_function) {
2227 parser->newline_as_space = 1;
2228 parser->paren_count = 0;
2235 node = parser->lex_from_node;
2238 parser->lex_from_list = NULL;
2242 *yylval = node->token->value;
2243 ret = node->token->type;
2245 parser->lex_from_node = node->next;
2251 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2255 assert (parser->lex_from_list == NULL);
2257 /* Copy list, eliminating any space tokens. */
2258 parser->lex_from_list = _token_list_create (parser);
2260 for (node = list->head; node; node = node->next) {
2261 if (node->token->type == SPACE)
2263 _token_list_append (parser, parser->lex_from_list, node->token);
2266 parser->lex_from_node = parser->lex_from_list->head;
2268 /* It's possible the list consisted of nothing but whitespace. */
2269 if (parser->lex_from_node == NULL) {
2270 parser->lex_from_list = NULL;
2275 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2278 skip_type_t current = SKIP_NO_SKIP;
2281 if (parser->skip_stack)
2282 current = parser->skip_stack->type;
2284 node = linear_alloc_child(parser->linalloc, sizeof(skip_node_t));
2287 if (current == SKIP_NO_SKIP) {
2289 node->type = SKIP_NO_SKIP;
2291 node->type = SKIP_TO_ELSE;
2293 node->type = SKIP_TO_ENDIF;
2296 node->has_else = false;
2297 node->next = parser->skip_stack;
2298 parser->skip_stack = node;
2302 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2303 const char *type, int condition)
2305 if (parser->skip_stack == NULL) {
2306 glcpp_error (loc, parser, "#%s without #if\n", type);
2310 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2312 parser->skip_stack->type = SKIP_NO_SKIP;
2314 parser->skip_stack->type = SKIP_TO_ENDIF;
2319 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2323 if (parser->skip_stack == NULL) {
2324 glcpp_error (loc, parser, "#endif without #if\n");
2328 node = parser->skip_stack;
2329 parser->skip_stack = node->next;
2333 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2334 const char *identifier,
2335 bool explicitly_set)
2337 if (parser->version_set)
2340 parser->version = version;
2341 parser->version_set = true;
2343 add_builtin_define (parser, "__VERSION__", version);
2345 parser->is_gles = (version == 100) ||
2346 (identifier && (strcmp(identifier, "es") == 0));
2347 bool is_compat = version >= 150 && identifier &&
2348 strcmp(identifier, "compatibility") == 0;
2350 /* Add pre-defined macros. */
2351 if (parser->is_gles)
2352 add_builtin_define(parser, "GL_ES", 1);
2354 add_builtin_define(parser, "GL_compatibility_profile", 1);
2355 else if (version >= 150)
2356 add_builtin_define(parser, "GL_core_profile", 1);
2358 /* Currently, all ES2/ES3 implementations support highp in the
2359 * fragment shader, so we always define this macro in ES2/ES3.
2360 * If we ever get a driver that doesn't support highp, we'll
2361 * need to add a flag to the gl_context and check that here.
2363 if (version >= 130 || parser->is_gles)
2364 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2366 /* Add all the extension macros available in this context */
2367 if (parser->extensions)
2368 parser->extensions(parser->state, add_builtin_define, parser,
2369 version, parser->is_gles);
2371 if (parser->extension_list) {
2372 /* If MESA_shader_integer_functions is supported, then the building
2373 * blocks required for the 64x64 => 64 multiply exist. Add defines for
2374 * those functions so that they can be tested.
2376 if (parser->extension_list->MESA_shader_integer_functions) {
2377 add_builtin_define(parser, "__have_builtin_builtin_sign64", 1);
2378 add_builtin_define(parser, "__have_builtin_builtin_umul64", 1);
2379 add_builtin_define(parser, "__have_builtin_builtin_udiv64", 1);
2380 add_builtin_define(parser, "__have_builtin_builtin_umod64", 1);
2381 add_builtin_define(parser, "__have_builtin_builtin_idiv64", 1);
2382 add_builtin_define(parser, "__have_builtin_builtin_imod64", 1);
2386 if (explicitly_set) {
2387 _mesa_string_buffer_printf(parser->output,
2388 "#version %" PRIiMAX "%s%s", version,
2389 identifier ? " " : "",
2390 identifier ? identifier : "");
2394 /* GLSL version if no version is explicitly specified. */
2395 #define IMPLICIT_GLSL_VERSION 110
2397 /* GLSL ES version if no version is explicitly specified. */
2398 #define IMPLICIT_GLSL_ES_VERSION 100
2401 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2403 int language_version = parser->api == API_OPENGLES2 ?
2404 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2406 _glcpp_parser_handle_version_declaration(parser, language_version,