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(void *ctx);
55 _string_list_append_item(string_list_t *list, const char *str);
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(void *ctx);
73 _argument_list_append(argument_list_t *list, token_list_t *argument);
76 _argument_list_length(argument_list_t *list);
79 _argument_list_member_at(argument_list_t *list, int index);
81 /* Note: This function ralloc_steal()s the str pointer. */
83 _token_create_str(void *ctx, int type, char *str);
86 _token_create_ival(void *ctx, int type, int ival);
89 _token_list_create(void *ctx);
92 _token_list_append(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);
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
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 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 {
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 macro = hash_table_find (parser->defines, $3);
314 hash_table_remove (parser->defines, $3);
319 | HASH_TOKEN IF pp_tokens NEWLINE {
320 /* Be careful to only evaluate the 'if' expression if
321 * we are not skipping. When we are skipping, we
322 * simply push a new 0-valued 'if' onto the skip
325 * This avoids generating diagnostics for invalid
326 * expressions that are being skipped. */
327 if (parser->skip_stack == NULL ||
328 parser->skip_stack->type == SKIP_NO_SKIP)
330 _glcpp_parser_expand_and_lex_from (parser,
332 EXPANSION_MODE_EVALUATE_DEFINED);
336 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
337 parser->skip_stack->type = SKIP_TO_ENDIF;
340 | HASH_TOKEN IF NEWLINE {
341 /* #if without an expression is only an error if we
342 * are not skipping */
343 if (parser->skip_stack == NULL ||
344 parser->skip_stack->type == SKIP_NO_SKIP)
346 glcpp_error(& @1, parser, "#if with no expression");
348 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
350 | HASH_TOKEN IFDEF IDENTIFIER junk NEWLINE {
351 macro_t *macro = hash_table_find (parser->defines, $3);
353 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
355 | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE {
356 macro_t *macro = hash_table_find (parser->defines, $3);
358 _glcpp_parser_skip_stack_push_if (parser, & @3, macro == NULL);
360 | HASH_TOKEN ELIF pp_tokens NEWLINE {
361 /* Be careful to only evaluate the 'elif' expression
362 * if we are not skipping. When we are skipping, we
363 * simply change to a 0-valued 'elif' on the skip
366 * This avoids generating diagnostics for invalid
367 * expressions that are being skipped. */
368 if (parser->skip_stack &&
369 parser->skip_stack->type == SKIP_TO_ELSE)
371 _glcpp_parser_expand_and_lex_from (parser,
373 EXPANSION_MODE_EVALUATE_DEFINED);
375 else if (parser->skip_stack &&
376 parser->skip_stack->has_else)
378 glcpp_error(& @1, parser, "#elif after #else");
382 _glcpp_parser_skip_stack_change_if (parser, & @1,
386 | HASH_TOKEN ELIF NEWLINE {
387 /* #elif without an expression is an error unless we
389 if (parser->skip_stack &&
390 parser->skip_stack->type == SKIP_TO_ELSE)
392 glcpp_error(& @1, parser, "#elif with no expression");
394 else if (parser->skip_stack &&
395 parser->skip_stack->has_else)
397 glcpp_error(& @1, parser, "#elif after #else");
401 _glcpp_parser_skip_stack_change_if (parser, & @1,
403 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
406 | HASH_TOKEN ELSE { parser->lexing_directive = 1; } NEWLINE {
407 if (parser->skip_stack &&
408 parser->skip_stack->has_else)
410 glcpp_error(& @1, parser, "multiple #else");
414 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
415 if (parser->skip_stack)
416 parser->skip_stack->has_else = true;
420 _glcpp_parser_skip_stack_pop (parser, & @1);
422 | HASH_TOKEN VERSION_TOKEN integer_constant NEWLINE {
423 if (parser->version != 0) {
424 glcpp_error(& @1, parser, "#version must appear on the first line");
426 _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
428 | HASH_TOKEN VERSION_TOKEN integer_constant IDENTIFIER NEWLINE {
429 if (parser->version != 0) {
430 glcpp_error(& @1, parser, "#version must appear on the first line");
432 _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
434 | HASH_TOKEN NEWLINE {
435 glcpp_parser_resolve_implicit_version(parser);
437 | HASH_TOKEN PRAGMA NEWLINE {
438 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#%s", $2);
443 HASH_TOKEN ERROR_TOKEN NEWLINE {
444 glcpp_error(& @1, parser, "#%s", $2);
446 | HASH_TOKEN DEFINE_TOKEN NEWLINE {
447 glcpp_error (& @1, parser, "#define without macro name");
449 | HASH_TOKEN GARBAGE pp_tokens NEWLINE {
450 glcpp_error (& @1, parser, "Illegal non-directive after #");
456 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
457 $$ = strtoll ($1 + 2, NULL, 16);
458 } else if ($1[0] == '0') {
459 $$ = strtoll ($1, NULL, 8);
461 $$ = strtoll ($1, NULL, 10);
471 $$.undefined_macro = NULL;
476 $$.undefined_macro = ralloc_strdup (parser, $1);
478 $$.undefined_macro = NULL;
480 | expression OR expression {
481 $$.value = $1.value || $3.value;
483 /* Short-circuit: Only flag undefined from right side
484 * if left side evaluates to false.
486 if ($1.undefined_macro)
487 $$.undefined_macro = $1.undefined_macro;
489 $$.undefined_macro = $3.undefined_macro;
491 | expression AND expression {
492 $$.value = $1.value && $3.value;
494 /* Short-circuit: Only flag undefined from right-side
495 * if left side evaluates to true.
497 if ($1.undefined_macro)
498 $$.undefined_macro = $1.undefined_macro;
500 $$.undefined_macro = $3.undefined_macro;
502 | expression '|' expression {
503 $$.value = $1.value | $3.value;
504 if ($1.undefined_macro)
505 $$.undefined_macro = $1.undefined_macro;
507 $$.undefined_macro = $3.undefined_macro;
509 | expression '^' expression {
510 $$.value = $1.value ^ $3.value;
511 if ($1.undefined_macro)
512 $$.undefined_macro = $1.undefined_macro;
514 $$.undefined_macro = $3.undefined_macro;
516 | expression '&' expression {
517 $$.value = $1.value & $3.value;
518 if ($1.undefined_macro)
519 $$.undefined_macro = $1.undefined_macro;
521 $$.undefined_macro = $3.undefined_macro;
523 | expression NOT_EQUAL expression {
524 $$.value = $1.value != $3.value;
525 if ($1.undefined_macro)
526 $$.undefined_macro = $1.undefined_macro;
528 $$.undefined_macro = $3.undefined_macro;
530 | expression EQUAL expression {
531 $$.value = $1.value == $3.value;
532 if ($1.undefined_macro)
533 $$.undefined_macro = $1.undefined_macro;
535 $$.undefined_macro = $3.undefined_macro;
537 | expression GREATER_OR_EQUAL expression {
538 $$.value = $1.value >= $3.value;
539 if ($1.undefined_macro)
540 $$.undefined_macro = $1.undefined_macro;
542 $$.undefined_macro = $3.undefined_macro;
544 | expression LESS_OR_EQUAL expression {
545 $$.value = $1.value <= $3.value;
546 if ($1.undefined_macro)
547 $$.undefined_macro = $1.undefined_macro;
549 $$.undefined_macro = $3.undefined_macro;
551 | expression '>' expression {
552 $$.value = $1.value > $3.value;
553 if ($1.undefined_macro)
554 $$.undefined_macro = $1.undefined_macro;
556 $$.undefined_macro = $3.undefined_macro;
558 | expression '<' expression {
559 $$.value = $1.value < $3.value;
560 if ($1.undefined_macro)
561 $$.undefined_macro = $1.undefined_macro;
563 $$.undefined_macro = $3.undefined_macro;
565 | expression RIGHT_SHIFT expression {
566 $$.value = $1.value >> $3.value;
567 if ($1.undefined_macro)
568 $$.undefined_macro = $1.undefined_macro;
570 $$.undefined_macro = $3.undefined_macro;
572 | expression LEFT_SHIFT expression {
573 $$.value = $1.value << $3.value;
574 if ($1.undefined_macro)
575 $$.undefined_macro = $1.undefined_macro;
577 $$.undefined_macro = $3.undefined_macro;
579 | expression '-' expression {
580 $$.value = $1.value - $3.value;
581 if ($1.undefined_macro)
582 $$.undefined_macro = $1.undefined_macro;
584 $$.undefined_macro = $3.undefined_macro;
586 | expression '+' expression {
587 $$.value = $1.value + $3.value;
588 if ($1.undefined_macro)
589 $$.undefined_macro = $1.undefined_macro;
591 $$.undefined_macro = $3.undefined_macro;
593 | expression '%' expression {
595 yyerror (& @1, parser,
596 "zero modulus in preprocessor directive");
598 $$.value = $1.value % $3.value;
600 if ($1.undefined_macro)
601 $$.undefined_macro = $1.undefined_macro;
603 $$.undefined_macro = $3.undefined_macro;
605 | expression '/' expression {
607 yyerror (& @1, parser,
608 "division by 0 in preprocessor directive");
610 $$.value = $1.value / $3.value;
612 if ($1.undefined_macro)
613 $$.undefined_macro = $1.undefined_macro;
615 $$.undefined_macro = $3.undefined_macro;
617 | expression '*' expression {
618 $$.value = $1.value * $3.value;
619 if ($1.undefined_macro)
620 $$.undefined_macro = $1.undefined_macro;
622 $$.undefined_macro = $3.undefined_macro;
624 | '!' expression %prec UNARY {
625 $$.value = ! $2.value;
626 $$.undefined_macro = $2.undefined_macro;
628 | '~' expression %prec UNARY {
629 $$.value = ~ $2.value;
630 $$.undefined_macro = $2.undefined_macro;
632 | '-' expression %prec UNARY {
633 $$.value = - $2.value;
634 $$.undefined_macro = $2.undefined_macro;
636 | '+' expression %prec UNARY {
637 $$.value = + $2.value;
638 $$.undefined_macro = $2.undefined_macro;
640 | '(' expression ')' {
647 $$ = _string_list_create (parser);
648 _string_list_append_item ($$, $1);
649 ralloc_steal ($$, $1);
651 | identifier_list ',' IDENTIFIER {
653 _string_list_append_item ($$, $3);
654 ralloc_steal ($$, $3);
659 NEWLINE { $$ = NULL; }
664 /* empty */ { $$ = NULL; }
671 glcpp_error(&@1, parser, "extra tokens at end of directive");
676 preprocessing_token {
677 parser->space_tokens = 1;
678 $$ = _token_list_create (parser);
679 _token_list_append ($$, $1);
681 | pp_tokens preprocessing_token {
683 _token_list_append ($$, $2);
689 $$ = _token_create_str (parser, IDENTIFIER, $1);
690 $$->location = yylloc;
693 $$ = _token_create_str (parser, INTEGER_STRING, $1);
694 $$->location = yylloc;
697 $$ = _token_create_ival (parser, $1, $1);
698 $$->location = yylloc;
701 $$ = _token_create_ival (parser, DEFINED, DEFINED);
702 $$->location = yylloc;
705 $$ = _token_create_str (parser, OTHER, $1);
706 $$->location = yylloc;
709 $$ = _token_create_ival (parser, SPACE, SPACE);
710 $$->location = yylloc;
730 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
731 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
734 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
735 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
736 | EQUAL { $$ = EQUAL; }
737 | NOT_EQUAL { $$ = NOT_EQUAL; }
745 | PASTE { $$ = PASTE; }
746 | PLUS_PLUS { $$ = PLUS_PLUS; }
747 | MINUS_MINUS { $$ = MINUS_MINUS; }
753 _string_list_create(void *ctx)
757 list = ralloc (ctx, string_list_t);
765 _string_list_append_item(string_list_t *list, const char *str)
769 node = ralloc (list, string_node_t);
770 node->str = ralloc_strdup (node, str);
774 if (list->head == NULL) {
777 list->tail->next = node;
784 _string_list_contains(string_list_t *list, const char *member, int *index)
792 for (i = 0, node = list->head; node; i++, node = node->next) {
793 if (strcmp (node->str, member) == 0) {
803 /* Return duplicate string in list (if any), NULL otherwise. */
805 _string_list_has_duplicate(string_list_t *list)
807 string_node_t *node, *dup;
812 for (node = list->head; node; node = node->next) {
813 for (dup = node->next; dup; dup = dup->next) {
814 if (strcmp (node->str, dup->str) == 0)
823 _string_list_length(string_list_t *list)
831 for (node = list->head; node; node = node->next)
838 _string_list_equal(string_list_t *a, string_list_t *b)
840 string_node_t *node_a, *node_b;
842 if (a == NULL && b == NULL)
845 if (a == NULL || b == NULL)
848 for (node_a = a->head, node_b = b->head;
850 node_a = node_a->next, node_b = node_b->next)
852 if (strcmp (node_a->str, node_b->str))
856 /* Catch the case of lists being different lengths, (which
857 * would cause the loop above to terminate after the shorter
859 return node_a == node_b;
863 _argument_list_create(void *ctx)
865 argument_list_t *list;
867 list = ralloc (ctx, argument_list_t);
875 _argument_list_append(argument_list_t *list, token_list_t *argument)
877 argument_node_t *node;
879 node = ralloc (list, argument_node_t);
880 node->argument = argument;
884 if (list->head == NULL) {
887 list->tail->next = node;
894 _argument_list_length(argument_list_t *list)
897 argument_node_t *node;
902 for (node = list->head; node; node = node->next)
909 _argument_list_member_at(argument_list_t *list, int index)
911 argument_node_t *node;
918 for (i = 0; i < index; i++) {
925 return node->argument;
930 /* Note: This function ralloc_steal()s the str pointer. */
932 _token_create_str(void *ctx, int type, char *str)
936 token = ralloc (ctx, token_t);
938 token->value.str = str;
940 ralloc_steal (token, str);
946 _token_create_ival(void *ctx, int type, int ival)
950 token = ralloc (ctx, token_t);
952 token->value.ival = ival;
958 _token_list_create(void *ctx)
962 list = ralloc (ctx, token_list_t);
965 list->non_space_tail = NULL;
971 _token_list_append(token_list_t *list, token_t *token)
975 node = ralloc (list, token_node_t);
979 if (list->head == NULL) {
982 list->tail->next = node;
986 if (token->type != SPACE)
987 list->non_space_tail = node;
991 _token_list_append_list(token_list_t *list, token_list_t *tail)
993 if (tail == NULL || tail->head == NULL)
996 if (list->head == NULL) {
997 list->head = tail->head;
999 list->tail->next = tail->head;
1002 list->tail = tail->tail;
1003 list->non_space_tail = tail->non_space_tail;
1006 static token_list_t *
1007 _token_list_copy(void *ctx, token_list_t *other)
1015 copy = _token_list_create (ctx);
1016 for (node = other->head; node; node = node->next) {
1017 token_t *new_token = ralloc (copy, token_t);
1018 *new_token = *node->token;
1019 _token_list_append (copy, new_token);
1026 _token_list_trim_trailing_space(token_list_t *list)
1028 token_node_t *tail, *next;
1030 if (list->non_space_tail) {
1031 tail = list->non_space_tail->next;
1032 list->non_space_tail->next = NULL;
1033 list->tail = list->non_space_tail;
1044 _token_list_is_empty_ignoring_space(token_list_t *l)
1052 while (n != NULL && n->token->type == SPACE)
1059 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1061 token_node_t *node_a, *node_b;
1063 if (a == NULL || b == NULL) {
1064 int a_empty = _token_list_is_empty_ignoring_space(a);
1065 int b_empty = _token_list_is_empty_ignoring_space(b);
1066 return a_empty == b_empty;
1074 if (node_a == NULL && node_b == NULL)
1077 if (node_a == NULL || node_b == NULL)
1079 /* Make sure whitespace appears in the same places in both.
1080 * It need not be exactly the same amount of whitespace,
1083 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1084 while (node_a && node_a->token->type == SPACE)
1085 node_a = node_a->next;
1086 while (node_b && node_b->token->type == SPACE)
1087 node_b = node_b->next;
1091 if (node_a->token->type != node_b->token->type)
1094 switch (node_a->token->type) {
1096 if (node_a->token->value.ival != node_b->token->value.ival) {
1101 case INTEGER_STRING:
1103 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1109 node_a = node_a->next;
1110 node_b = node_b->next;
1117 _token_print(char **out, size_t *len, token_t *token)
1119 if (token->type < 256) {
1120 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1124 switch (token->type) {
1126 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1129 case INTEGER_STRING:
1131 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1134 ralloc_asprintf_rewrite_tail (out, len, " ");
1137 ralloc_asprintf_rewrite_tail (out, len, "<<");
1140 ralloc_asprintf_rewrite_tail (out, len, ">>");
1143 ralloc_asprintf_rewrite_tail (out, len, "<=");
1145 case GREATER_OR_EQUAL:
1146 ralloc_asprintf_rewrite_tail (out, len, ">=");
1149 ralloc_asprintf_rewrite_tail (out, len, "==");
1152 ralloc_asprintf_rewrite_tail (out, len, "!=");
1155 ralloc_asprintf_rewrite_tail (out, len, "&&");
1158 ralloc_asprintf_rewrite_tail (out, len, "||");
1161 ralloc_asprintf_rewrite_tail (out, len, "##");
1164 ralloc_asprintf_rewrite_tail (out, len, "++");
1167 ralloc_asprintf_rewrite_tail (out, len, "--");
1170 ralloc_asprintf_rewrite_tail (out, len, "defined");
1173 /* Nothing to print. */
1176 assert(!"Error: Don't know how to print token.");
1182 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1183 * 'token' and 'other'. Note that this function may return 'token' or
1184 * 'other' directly rather than allocating anything new.
1186 * Caution: Only very cursory error-checking is performed to see if
1187 * the final result is a valid single token. */
1189 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1191 token_t *combined = NULL;
1193 /* Pasting a placeholder onto anything makes no change. */
1194 if (other->type == PLACEHOLDER)
1197 /* When 'token' is a placeholder, just return 'other'. */
1198 if (token->type == PLACEHOLDER)
1201 /* A very few single-character punctuators can be combined
1202 * with another to form a multi-character punctuator. */
1203 switch (token->type) {
1205 if (other->type == '<')
1206 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1207 else if (other->type == '=')
1208 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1211 if (other->type == '>')
1212 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1213 else if (other->type == '=')
1214 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1217 if (other->type == '=')
1218 combined = _token_create_ival (token, EQUAL, EQUAL);
1221 if (other->type == '=')
1222 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1225 if (other->type == '&')
1226 combined = _token_create_ival (token, AND, AND);
1229 if (other->type == '|')
1230 combined = _token_create_ival (token, OR, OR);
1234 if (combined != NULL) {
1235 /* Inherit the location from the first token */
1236 combined->location = token->location;
1240 /* Two string-valued (or integer) tokens can usually just be
1241 * mashed together. (We also handle a string followed by an
1242 * integer here as well.)
1244 * There are some exceptions here. Notably, if the first token
1245 * is an integer (or a string representing an integer), then
1246 * the second token must also be an integer or must be a
1247 * string representing an integer that begins with a digit.
1249 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1250 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1255 /* Check that pasting onto an integer doesn't create a
1256 * non-integer, (that is, only digits can be
1258 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1259 switch (other->type) {
1260 case INTEGER_STRING:
1261 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1265 if (other->value.ival < 0)
1273 if (token->type == INTEGER)
1274 str = ralloc_asprintf (token, "%" PRIiMAX, token->value.ival);
1276 str = ralloc_strdup (token, token->value.str);
1278 if (other->type == INTEGER)
1279 ralloc_asprintf_append (&str, "%" PRIiMAX, other->value.ival);
1281 ralloc_strcat (&str, other->value.str);
1283 /* New token is same type as original token, unless we
1284 * started with an integer, in which case we will be
1285 * creating an integer-string. */
1286 combined_type = token->type;
1287 if (combined_type == INTEGER)
1288 combined_type = INTEGER_STRING;
1290 combined = _token_create_str (token, combined_type, str);
1291 combined->location = token->location;
1296 glcpp_error (&token->location, parser, "");
1297 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1298 _token_print (&parser->info_log, &parser->info_log_length, token);
1299 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1300 _token_print (&parser->info_log, &parser->info_log_length, other);
1301 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1307 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1314 for (node = list->head; node; node = node->next)
1315 _token_print (&parser->output, &parser->output_length, node->token);
1319 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1321 glcpp_error(locp, parser, "%s", error);
1325 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1330 tok = _token_create_ival (parser, INTEGER, value);
1332 list = _token_list_create(parser);
1333 _token_list_append(list, tok);
1334 _define_object_macro(parser, NULL, name, list);
1338 glcpp_parser_create(const struct gl_extensions *extensions, 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 = hash_table_ctor(32, hash_table_string_hash,
1346 hash_table_string_compare);
1347 parser->active = NULL;
1348 parser->lexing_directive = 0;
1349 parser->space_tokens = 1;
1350 parser->last_token_was_newline = 0;
1351 parser->last_token_was_space = 0;
1352 parser->first_non_space_token_this_line = 1;
1353 parser->newline_as_space = 0;
1354 parser->in_control_line = 0;
1355 parser->paren_count = 0;
1356 parser->commented_newlines = 0;
1358 parser->skip_stack = NULL;
1359 parser->skipping = 0;
1361 parser->lex_from_list = NULL;
1362 parser->lex_from_node = NULL;
1364 parser->output = ralloc_strdup(parser, "");
1365 parser->output_length = 0;
1366 parser->info_log = ralloc_strdup(parser, "");
1367 parser->info_log_length = 0;
1370 parser->extensions = extensions;
1372 parser->version = 0;
1374 parser->has_new_line_number = 0;
1375 parser->new_line_number = 1;
1376 parser->has_new_source_number = 0;
1377 parser->new_source_number = 0;
1383 glcpp_parser_destroy(glcpp_parser_t *parser)
1385 glcpp_lex_destroy (parser->scanner);
1386 hash_table_dtor (parser->defines);
1387 ralloc_free (parser);
1390 typedef enum function_status
1392 FUNCTION_STATUS_SUCCESS,
1393 FUNCTION_NOT_A_FUNCTION,
1394 FUNCTION_UNBALANCED_PARENTHESES
1395 } function_status_t;
1397 /* Find a set of function-like macro arguments by looking for a
1398 * balanced set of parentheses.
1400 * When called, 'node' should be the opening-parenthesis token, (or
1401 * perhaps preceeding SPACE tokens). Upon successful return *last will
1402 * be the last consumed node, (corresponding to the closing right
1407 * FUNCTION_STATUS_SUCCESS:
1409 * Successfully parsed a set of function arguments.
1411 * FUNCTION_NOT_A_FUNCTION:
1413 * Macro name not followed by a '('. This is not an error, but
1414 * simply that the macro name should be treated as a non-macro.
1416 * FUNCTION_UNBALANCED_PARENTHESES
1418 * Macro name is not followed by a balanced set of parentheses.
1420 static function_status_t
1421 _arguments_parse(argument_list_t *arguments, token_node_t *node,
1422 token_node_t **last)
1424 token_list_t *argument;
1429 /* Ignore whitespace before first parenthesis. */
1430 while (node && node->token->type == SPACE)
1433 if (node == NULL || node->token->type != '(')
1434 return FUNCTION_NOT_A_FUNCTION;
1438 argument = _token_list_create (arguments);
1439 _argument_list_append (arguments, argument);
1441 for (paren_count = 1; node; node = node->next) {
1442 if (node->token->type == '(') {
1444 } else if (node->token->type == ')') {
1446 if (paren_count == 0)
1450 if (node->token->type == ',' && paren_count == 1) {
1451 _token_list_trim_trailing_space (argument);
1452 argument = _token_list_create (arguments);
1453 _argument_list_append (arguments, argument);
1455 if (argument->head == NULL) {
1456 /* Don't treat initial whitespace as part of the argument. */
1457 if (node->token->type == SPACE)
1460 _token_list_append (argument, node->token);
1465 return FUNCTION_UNBALANCED_PARENTHESES;
1469 return FUNCTION_STATUS_SUCCESS;
1472 static token_list_t *
1473 _token_list_create_with_one_ival(void *ctx, int type, int ival)
1478 list = _token_list_create(ctx);
1479 node = _token_create_ival(list, type, ival);
1480 _token_list_append(list, node);
1485 static token_list_t *
1486 _token_list_create_with_one_space(void *ctx)
1488 return _token_list_create_with_one_ival(ctx, SPACE, SPACE);
1491 static token_list_t *
1492 _token_list_create_with_one_integer(void *ctx, int ival)
1494 return _token_list_create_with_one_ival(ctx, INTEGER, ival);
1497 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1499 * Note: This function must only be called when "node" is a DEFINED token,
1500 * (and will abort with an assertion failure otherwise).
1502 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1503 * (optionally preceded and followed by '(' and ')' tokens) then the following
1506 * If the identifier is a defined macro, this function returns 1.
1508 * If the identifier is not a defined macro, this function returns 0.
1510 * In either case, *last will be updated to the last node in the list
1511 * consumed by the evaluation, (either the token of the identifier or the
1512 * token of the closing parenthesis).
1514 * In all other cases, (such as "node is the final node of the list", or
1515 * "missing closing parenthesis", etc.), this function generates a
1516 * preprocessor error, returns -1 and *last will not be set.
1519 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1520 token_node_t **last)
1522 token_node_t *argument, *defined = node;
1524 assert(node->token->type == DEFINED);
1528 /* Ignore whitespace after DEFINED token. */
1529 while (node && node->token->type == SPACE)
1535 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1537 } else if (node->token->type == '(') {
1540 /* Ignore whitespace after '(' token. */
1541 while (node && node->token->type == SPACE)
1544 if (node == NULL || (node->token->type != IDENTIFIER &&
1545 node->token->type != OTHER)) {
1553 /* Ignore whitespace after identifier, before ')' token. */
1554 while (node && node->token->type == SPACE)
1557 if (node == NULL || node->token->type != ')')
1565 return hash_table_find(parser->defines,
1566 argument->token->value.str) ? 1 : 0;
1569 glcpp_error (&defined->token->location, parser,
1570 "\"defined\" not followed by an identifier");
1574 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1577 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1580 token_node_t *node, *node_prev, *replacement, *last = NULL;
1591 if (node->token->type != DEFINED)
1594 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1598 replacement = ralloc (list, token_node_t);
1599 replacement->token = _token_create_ival (list, INTEGER, value);
1601 /* Splice replacement node into list, replacing from "node"
1602 * through "last". */
1604 node_prev->next = replacement;
1606 list->head = replacement;
1607 replacement->next = last->next;
1608 if (last == list->tail)
1609 list->tail = replacement;
1619 /* Perform macro expansion on 'list', placing the resulting tokens
1620 * into a new list which is initialized with a first token of type
1621 * 'head_token_type'. Then begin lexing from the resulting list,
1622 * (return to the current lexing source when this list is exhausted).
1624 * See the documentation of _glcpp_parser_expand_token_list for a description
1625 * of the "mode" parameter.
1628 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1629 token_list_t *list, expansion_mode_t mode)
1631 token_list_t *expanded;
1634 expanded = _token_list_create (parser);
1635 token = _token_create_ival (parser, head_token_type, head_token_type);
1636 _token_list_append (expanded, token);
1637 _glcpp_parser_expand_token_list (parser, list, mode);
1638 _token_list_append_list (expanded, list);
1639 glcpp_parser_lex_from (parser, expanded);
1643 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1649 token_node_t *next_non_space;
1651 /* Look ahead for a PASTE token, skipping space. */
1652 next_non_space = node->next;
1653 while (next_non_space && next_non_space->token->type == SPACE)
1654 next_non_space = next_non_space->next;
1656 if (next_non_space == NULL)
1659 if (next_non_space->token->type != PASTE) {
1660 node = next_non_space;
1664 /* Now find the next non-space token after the PASTE. */
1665 next_non_space = next_non_space->next;
1666 while (next_non_space && next_non_space->token->type == SPACE)
1667 next_non_space = next_non_space->next;
1669 if (next_non_space == NULL) {
1670 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1674 node->token = _token_paste(parser, node->token, next_non_space->token);
1675 node->next = next_non_space->next;
1676 if (next_non_space == list->tail)
1680 list->non_space_tail = list->tail;
1683 /* This is a helper function that's essentially part of the
1684 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1685 * except for by that function.
1687 * Returns NULL if node is a simple token with no expansion, (that is,
1688 * although 'node' corresponds to an identifier defined as a
1689 * function-like macro, it is not followed with a parenthesized
1692 * Compute the complete expansion of node (which is a function-like
1693 * macro) and subsequent nodes which are arguments.
1695 * Returns the token list that results from the expansion and sets
1696 * *last to the last node in the list that was consumed by the
1697 * expansion. Specifically, *last will be set as follows: as the
1698 * token of the closing right parenthesis.
1700 * See the documentation of _glcpp_parser_expand_token_list for a description
1701 * of the "mode" parameter.
1703 static token_list_t *
1704 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1705 token_node_t **last, expansion_mode_t mode)
1708 const char *identifier;
1709 argument_list_t *arguments;
1710 function_status_t status;
1711 token_list_t *substituted;
1712 int parameter_index;
1714 identifier = node->token->value.str;
1716 macro = hash_table_find(parser->defines, identifier);
1718 assert(macro->is_function);
1720 arguments = _argument_list_create(parser);
1721 status = _arguments_parse(arguments, node, last);
1724 case FUNCTION_STATUS_SUCCESS:
1726 case FUNCTION_NOT_A_FUNCTION:
1728 case FUNCTION_UNBALANCED_PARENTHESES:
1729 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1733 /* Replace a macro defined as empty with a SPACE token. */
1734 if (macro->replacements == NULL) {
1735 ralloc_free(arguments);
1736 return _token_list_create_with_one_space(parser);
1739 if (!((_argument_list_length (arguments) ==
1740 _string_list_length (macro->parameters)) ||
1741 (_string_list_length (macro->parameters) == 0 &&
1742 _argument_list_length (arguments) == 1 &&
1743 arguments->head->argument->head == NULL))) {
1744 glcpp_error(&node->token->location, parser,
1745 "Error: macro %s invoked with %d arguments (expected %d)\n",
1746 identifier, _argument_list_length (arguments),
1747 _string_list_length(macro->parameters));
1751 /* Perform argument substitution on the replacement list. */
1752 substituted = _token_list_create(arguments);
1754 for (node = macro->replacements->head; node; node = node->next) {
1755 if (node->token->type == IDENTIFIER &&
1756 _string_list_contains(macro->parameters, node->token->value.str,
1757 ¶meter_index)) {
1758 token_list_t *argument;
1759 argument = _argument_list_member_at(arguments, parameter_index);
1760 /* Before substituting, we expand the argument tokens, or append a
1761 * placeholder token for an empty argument. */
1762 if (argument->head) {
1763 token_list_t *expanded_argument;
1764 expanded_argument = _token_list_copy(parser, argument);
1765 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1766 _token_list_append_list(substituted, expanded_argument);
1770 new_token = _token_create_ival(substituted, PLACEHOLDER,
1772 _token_list_append(substituted, new_token);
1775 _token_list_append(substituted, node->token);
1779 /* After argument substitution, and before further expansion
1780 * below, implement token pasting. */
1782 _token_list_trim_trailing_space(substituted);
1784 _glcpp_parser_apply_pastes(parser, substituted);
1789 /* Compute the complete expansion of node, (and subsequent nodes after
1790 * 'node' in the case that 'node' is a function-like macro and
1791 * subsequent nodes are arguments).
1793 * Returns NULL if node is a simple token with no expansion.
1795 * Otherwise, returns the token list that results from the expansion
1796 * and sets *last to the last node in the list that was consumed by
1797 * the expansion. Specifically, *last will be set as follows:
1799 * As 'node' in the case of object-like macro expansion.
1801 * As the token of the closing right parenthesis in the case of
1802 * function-like macro expansion.
1804 * See the documentation of _glcpp_parser_expand_token_list for a description
1805 * of the "mode" parameter.
1807 static token_list_t *
1808 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1809 token_node_t **last, expansion_mode_t mode)
1811 token_t *token = node->token;
1812 const char *identifier;
1815 /* We only expand identifiers */
1816 if (token->type != IDENTIFIER) {
1821 identifier = token->value.str;
1823 /* Special handling for __LINE__ and __FILE__, (not through
1824 * the hash table). */
1825 if (strcmp(identifier, "__LINE__") == 0)
1826 return _token_list_create_with_one_integer(parser, node->token->location.first_line);
1828 if (strcmp(identifier, "__FILE__") == 0)
1829 return _token_list_create_with_one_integer(parser, node->token->location.source);
1831 /* Look up this identifier in the hash table. */
1832 macro = hash_table_find(parser->defines, identifier);
1834 /* Not a macro, so no expansion needed. */
1838 /* Finally, don't expand this macro if we're already actively
1839 * expanding it, (to avoid infinite recursion). */
1840 if (_parser_active_list_contains (parser, identifier)) {
1841 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1842 * future expansion of this unexpanded token. */
1844 token_list_t *expansion;
1847 str = ralloc_strdup(parser, token->value.str);
1848 final = _token_create_str(parser, OTHER, str);
1849 expansion = _token_list_create(parser);
1850 _token_list_append(expansion, final);
1854 if (! macro->is_function) {
1855 token_list_t *replacement;
1857 /* Replace a macro defined as empty with a SPACE token. */
1858 if (macro->replacements == NULL)
1859 return _token_list_create_with_one_space(parser);
1861 replacement = _token_list_copy(parser, macro->replacements);
1862 _glcpp_parser_apply_pastes(parser, replacement);
1866 return _glcpp_parser_expand_function(parser, node, last, mode);
1869 /* Push a new identifier onto the parser's active list.
1871 * Here, 'marker' is the token node that appears in the list after the
1872 * expansion of 'identifier'. That is, when the list iterator begins
1873 * examining 'marker', then it is time to pop this node from the
1877 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
1878 token_node_t *marker)
1880 active_list_t *node;
1882 node = ralloc(parser->active, active_list_t);
1883 node->identifier = ralloc_strdup(node, identifier);
1884 node->marker = marker;
1885 node->next = parser->active;
1887 parser->active = node;
1891 _parser_active_list_pop(glcpp_parser_t *parser)
1893 active_list_t *node = parser->active;
1896 parser->active = NULL;
1900 node = parser->active->next;
1901 ralloc_free (parser->active);
1903 parser->active = node;
1907 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
1909 active_list_t *node;
1911 if (parser->active == NULL)
1914 for (node = parser->active; node; node = node->next)
1915 if (strcmp(node->identifier, identifier) == 0)
1921 /* Walk over the token list replacing nodes with their expansion.
1922 * Whenever nodes are expanded the walking will walk over the new
1923 * nodes, continuing to expand as necessary. The results are placed in
1926 * The "mode" argument controls the handling of any DEFINED tokens that
1927 * result from expansion as follows:
1929 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
1930 * left in the final list, unevaluated. This is the correct mode
1931 * for expanding any list in any context other than a
1932 * preprocessor conditional, (#if or #elif).
1934 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
1935 * evaluated to 0 or 1 tokens depending on whether the following
1936 * token is the name of a defined macro. If the DEFINED token is
1937 * not followed by an (optionally parenthesized) identifier, then
1938 * an error will be generated. This the correct mode for
1939 * expanding any list in the context of a preprocessor
1940 * conditional, (#if or #elif).
1943 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
1944 expansion_mode_t mode)
1946 token_node_t *node_prev;
1947 token_node_t *node, *last = NULL;
1948 token_list_t *expansion;
1949 active_list_t *active_initial = parser->active;
1954 _token_list_trim_trailing_space (list);
1959 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
1960 _glcpp_parser_evaluate_defined_in_list (parser, list);
1964 while (parser->active && parser->active->marker == node)
1965 _parser_active_list_pop (parser);
1967 expansion = _glcpp_parser_expand_node (parser, node, &last, mode);
1971 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
1972 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
1975 for (n = node; n != last->next; n = n->next)
1976 while (parser->active && parser->active->marker == n) {
1977 _parser_active_list_pop (parser);
1980 _parser_active_list_push(parser, node->token->value.str, last->next);
1982 /* Splice expansion into list, supporting a simple deletion if the
1983 * expansion is empty.
1985 if (expansion->head) {
1987 node_prev->next = expansion->head;
1989 list->head = expansion->head;
1990 expansion->tail->next = last->next;
1991 if (last == list->tail)
1992 list->tail = expansion->tail;
1995 node_prev->next = last->next;
1997 list->head = last->next;
1998 if (last == list->tail)
2004 node = node_prev ? node_prev->next : list->head;
2007 /* Remove any lingering effects of this invocation on the
2008 * active list. That is, pop until the list looks like it did
2009 * at the beginning of this function. */
2010 while (parser->active && parser->active != active_initial)
2011 _parser_active_list_pop (parser);
2013 list->non_space_tail = list->tail;
2017 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
2023 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2025 _token_list_trim_trailing_space (list);
2027 _token_list_print (parser, list);
2031 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2032 const char *identifier)
2034 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2035 * the GLSL ES spec (all versions) say:
2037 * "All macro names containing two consecutive underscores ( __ )
2038 * are reserved for future use as predefined macro names. All
2039 * macro names prefixed with "GL_" ("GL" followed by a single
2040 * underscore) are also reserved."
2042 * The intention is that names containing __ are reserved for internal
2043 * use by the implementation, and names prefixed with GL_ are reserved
2044 * for use by Khronos. Since every extension adds a name prefixed
2045 * with GL_ (i.e., the name of the extension), that should be an
2046 * error. Names simply containing __ are dangerous to use, but should
2049 * A future version of the GLSL specification will clarify this.
2051 if (strstr(identifier, "__")) {
2052 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2053 "for use by the implementation.\n");
2055 if (strncmp(identifier, "GL_", 3) == 0) {
2056 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2058 if (strcmp(identifier, "defined") == 0) {
2059 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2064 _macro_equal(macro_t *a, macro_t *b)
2066 if (a->is_function != b->is_function)
2069 if (a->is_function) {
2070 if (! _string_list_equal (a->parameters, b->parameters))
2074 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2078 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2079 const char *identifier, token_list_t *replacements)
2081 macro_t *macro, *previous;
2083 /* We define pre-defined macros before we've started parsing the actual
2084 * file. So if there's no location defined yet, that's what were doing and
2085 * we don't want to generate an error for using the reserved names. */
2087 _check_for_reserved_macro_name(parser, loc, identifier);
2089 macro = ralloc (parser, macro_t);
2091 macro->is_function = 0;
2092 macro->parameters = NULL;
2093 macro->identifier = ralloc_strdup (macro, identifier);
2094 macro->replacements = replacements;
2095 ralloc_steal (macro, replacements);
2097 previous = hash_table_find (parser->defines, identifier);
2099 if (_macro_equal (macro, previous)) {
2100 ralloc_free (macro);
2103 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2106 hash_table_insert (parser->defines, macro, identifier);
2110 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2111 const char *identifier, string_list_t *parameters,
2112 token_list_t *replacements)
2114 macro_t *macro, *previous;
2117 _check_for_reserved_macro_name(parser, loc, identifier);
2119 /* Check for any duplicate parameter names. */
2120 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2121 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2124 macro = ralloc (parser, macro_t);
2125 ralloc_steal (macro, parameters);
2126 ralloc_steal (macro, replacements);
2128 macro->is_function = 1;
2129 macro->parameters = parameters;
2130 macro->identifier = ralloc_strdup (macro, identifier);
2131 macro->replacements = replacements;
2132 previous = hash_table_find (parser->defines, identifier);
2134 if (_macro_equal (macro, previous)) {
2135 ralloc_free (macro);
2138 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2141 hash_table_insert(parser->defines, macro, identifier);
2145 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2150 if (parser->lex_from_list == NULL) {
2151 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2153 /* XXX: This ugly block of code exists for the sole
2154 * purpose of converting a NEWLINE token into a SPACE
2155 * token, but only in the case where we have seen a
2156 * function-like macro name, but have not yet seen its
2157 * closing parenthesis.
2159 * There's perhaps a more compact way to do this with
2160 * mid-rule actions in the grammar.
2162 * I'm definitely not pleased with the complexity of
2165 if (parser->newline_as_space) {
2167 parser->paren_count++;
2168 } else if (ret == ')') {
2169 parser->paren_count--;
2170 if (parser->paren_count == 0)
2171 parser->newline_as_space = 0;
2172 } else if (ret == NEWLINE) {
2174 } else if (ret != SPACE) {
2175 if (parser->paren_count == 0)
2176 parser->newline_as_space = 0;
2178 } else if (parser->in_control_line) {
2180 parser->in_control_line = 0;
2182 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2183 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2184 ret == ENDIF || ret == HASH_TOKEN) {
2185 parser->in_control_line = 1;
2186 } else if (ret == IDENTIFIER) {
2188 macro = hash_table_find (parser->defines,
2190 if (macro && macro->is_function) {
2191 parser->newline_as_space = 1;
2192 parser->paren_count = 0;
2199 node = parser->lex_from_node;
2202 ralloc_free (parser->lex_from_list);
2203 parser->lex_from_list = NULL;
2207 *yylval = node->token->value;
2208 ret = node->token->type;
2210 parser->lex_from_node = node->next;
2216 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2220 assert (parser->lex_from_list == NULL);
2222 /* Copy list, eliminating any space tokens. */
2223 parser->lex_from_list = _token_list_create (parser);
2225 for (node = list->head; node; node = node->next) {
2226 if (node->token->type == SPACE)
2228 _token_list_append (parser->lex_from_list, node->token);
2233 parser->lex_from_node = parser->lex_from_list->head;
2235 /* It's possible the list consisted of nothing but whitespace. */
2236 if (parser->lex_from_node == NULL) {
2237 ralloc_free (parser->lex_from_list);
2238 parser->lex_from_list = NULL;
2243 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2246 skip_type_t current = SKIP_NO_SKIP;
2249 if (parser->skip_stack)
2250 current = parser->skip_stack->type;
2252 node = ralloc (parser, skip_node_t);
2255 if (current == SKIP_NO_SKIP) {
2257 node->type = SKIP_NO_SKIP;
2259 node->type = SKIP_TO_ELSE;
2261 node->type = SKIP_TO_ENDIF;
2264 node->has_else = false;
2265 node->next = parser->skip_stack;
2266 parser->skip_stack = node;
2270 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2271 const char *type, int condition)
2273 if (parser->skip_stack == NULL) {
2274 glcpp_error (loc, parser, "#%s without #if\n", type);
2278 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2280 parser->skip_stack->type = SKIP_NO_SKIP;
2282 parser->skip_stack->type = SKIP_TO_ENDIF;
2287 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2291 if (parser->skip_stack == NULL) {
2292 glcpp_error (loc, parser, "#endif without #if\n");
2296 node = parser->skip_stack;
2297 parser->skip_stack = node->next;
2302 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2303 const char *es_identifier,
2304 bool explicitly_set)
2306 const struct gl_extensions *extensions = parser->extensions;
2308 if (parser->version != 0)
2311 parser->version = version;
2313 add_builtin_define (parser, "__VERSION__", version);
2315 parser->is_gles = (version == 100) ||
2316 (es_identifier && (strcmp(es_identifier, "es") == 0));
2318 /* Add pre-defined macros. */
2319 if (parser->is_gles) {
2320 add_builtin_define(parser, "GL_ES", 1);
2321 add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
2322 add_builtin_define(parser, "GL_EXT_draw_buffers", 1);
2324 if (extensions != NULL) {
2325 if (extensions->OES_EGL_image_external)
2326 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2327 if (extensions->OES_sample_variables) {
2328 add_builtin_define(parser, "GL_OES_sample_variables", 1);
2329 add_builtin_define(parser, "GL_OES_shader_multisample_interpolation", 1);
2331 if (extensions->OES_standard_derivatives)
2332 add_builtin_define(parser, "GL_OES_standard_derivatives", 1);
2333 if (extensions->ARB_texture_multisample)
2334 add_builtin_define(parser, "GL_OES_texture_storage_multisample_2d_array", 1);
2335 if (extensions->ARB_blend_func_extended)
2336 add_builtin_define(parser, "GL_EXT_blend_func_extended", 1);
2337 if (extensions->ARB_cull_distance)
2338 add_builtin_define(parser, "GL_EXT_clip_cull_distance", 1);
2340 if (version >= 310) {
2341 if (extensions->ARB_shader_image_load_store)
2342 add_builtin_define(parser, "GL_OES_shader_image_atomic", 1);
2344 if (extensions->OES_geometry_shader) {
2345 add_builtin_define(parser, "GL_OES_geometry_point_size", 1);
2346 add_builtin_define(parser, "GL_OES_geometry_shader", 1);
2348 if (extensions->ARB_gpu_shader5) {
2349 add_builtin_define(parser, "GL_EXT_gpu_shader5", 1);
2350 add_builtin_define(parser, "GL_OES_gpu_shader5", 1);
2352 if (extensions->OES_texture_buffer) {
2353 add_builtin_define(parser, "GL_EXT_texture_buffer", 1);
2354 add_builtin_define(parser, "GL_OES_texture_buffer", 1);
2357 if (extensions->OES_shader_io_blocks) {
2358 add_builtin_define(parser, "GL_EXT_shader_io_blocks", 1);
2359 add_builtin_define(parser, "GL_OES_shader_io_blocks", 1);
2364 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2365 add_builtin_define(parser, "GL_ARB_enhanced_layouts", 1);
2366 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2367 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2368 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2370 if (extensions != NULL) {
2371 if (extensions->EXT_texture_array)
2372 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2374 if (extensions->ARB_ES3_1_compatibility)
2375 add_builtin_define(parser, "GL_ARB_ES3_1_compatibility", 1);
2377 if (extensions->ARB_arrays_of_arrays)
2378 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2380 if (extensions->ARB_fragment_coord_conventions) {
2381 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2385 if (extensions->ARB_fragment_layer_viewport)
2386 add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
2388 if (extensions->ARB_explicit_attrib_location)
2389 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2391 if (extensions->ARB_explicit_uniform_location)
2392 add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
2394 if (extensions->ARB_shader_texture_lod)
2395 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2397 if (extensions->ARB_draw_instanced)
2398 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2400 if (extensions->ARB_conservative_depth) {
2401 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2402 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2405 if (extensions->ARB_shader_bit_encoding)
2406 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2408 if (extensions->ARB_shader_clock)
2409 add_builtin_define(parser, "GL_ARB_shader_clock", 1);
2411 if (extensions->ARB_uniform_buffer_object)
2412 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2414 if (extensions->ARB_texture_cube_map_array)
2415 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2417 if (extensions->ARB_shading_language_packing)
2418 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2420 if (extensions->ARB_texture_multisample)
2421 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2423 if (extensions->ARB_texture_query_levels)
2424 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2426 if (extensions->ARB_texture_query_lod)
2427 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2429 if (extensions->ARB_gpu_shader5)
2430 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2432 if (extensions->ARB_gpu_shader_fp64)
2433 add_builtin_define(parser, "GL_ARB_gpu_shader_fp64", 1);
2435 if (extensions->ARB_vertex_attrib_64bit)
2436 add_builtin_define(parser, "GL_ARB_vertex_attrib_64bit", 1);
2438 if (extensions->AMD_vertex_shader_layer)
2439 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2441 if (extensions->AMD_vertex_shader_viewport_index)
2442 add_builtin_define(parser, "GL_AMD_vertex_shader_viewport_index", 1);
2444 if (extensions->ARB_shading_language_420pack)
2445 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2447 if (extensions->ARB_sample_shading)
2448 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2450 if (extensions->ARB_texture_gather)
2451 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2453 if (extensions->ARB_shader_atomic_counters)
2454 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2456 if (extensions->ARB_shader_atomic_counter_ops)
2457 add_builtin_define(parser, "GL_ARB_shader_atomic_counter_ops", 1);
2459 if (extensions->ARB_viewport_array)
2460 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2462 if (extensions->ARB_compute_shader)
2463 add_builtin_define(parser, "GL_ARB_compute_shader", 1);
2465 if (extensions->ARB_shader_image_load_store)
2466 add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
2468 if (extensions->ARB_shader_image_size)
2469 add_builtin_define(parser, "GL_ARB_shader_image_size", 1);
2471 if (extensions->ARB_shader_texture_image_samples)
2472 add_builtin_define(parser, "GL_ARB_shader_texture_image_samples", 1);
2474 if (extensions->ARB_derivative_control)
2475 add_builtin_define(parser, "GL_ARB_derivative_control", 1);
2477 if (extensions->ARB_shader_precision)
2478 add_builtin_define(parser, "GL_ARB_shader_precision", 1);
2480 if (extensions->ARB_shader_storage_buffer_object)
2481 add_builtin_define(parser, "GL_ARB_shader_storage_buffer_object", 1);
2483 if (extensions->ARB_tessellation_shader)
2484 add_builtin_define(parser, "GL_ARB_tessellation_shader", 1);
2486 if (extensions->ARB_shader_subroutine)
2487 add_builtin_define(parser, "GL_ARB_shader_subroutine", 1);
2489 if (extensions->ARB_shader_draw_parameters)
2490 add_builtin_define(parser, "GL_ARB_shader_draw_parameters", 1);
2492 if (extensions->ARB_cull_distance)
2493 add_builtin_define(parser, "GL_ARB_cull_distance", 1);
2497 if (extensions != NULL) {
2498 if (extensions->EXT_shader_integer_mix)
2499 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2501 if (extensions->EXT_shader_samples_identical)
2502 add_builtin_define(parser, "GL_EXT_shader_samples_identical", 1);
2506 add_builtin_define(parser, "GL_core_profile", 1);
2508 /* Currently, all ES2/ES3 implementations support highp in the
2509 * fragment shader, so we always define this macro in ES2/ES3.
2510 * If we ever get a driver that doesn't support highp, we'll
2511 * need to add a flag to the gl_context and check that here.
2513 if (version >= 130 || parser->is_gles)
2514 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2516 if (explicitly_set) {
2517 ralloc_asprintf_rewrite_tail(&parser->output, &parser->output_length,
2518 "#version %" PRIiMAX "%s%s", version,
2519 es_identifier ? " " : "",
2520 es_identifier ? es_identifier : "");
2524 /* GLSL version if no version is explicitly specified. */
2525 #define IMPLICIT_GLSL_VERSION 110
2527 /* GLSL ES version if no version is explicitly specified. */
2528 #define IMPLICIT_GLSL_ES_VERSION 100
2531 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2533 int language_version = parser->api == API_OPENGLES2 ?
2534 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2536 _glcpp_parser_handle_version_declaration(parser, language_version,