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_length (string_list_t *list);
64 _string_list_equal (string_list_t *a, string_list_t *b);
66 static argument_list_t *
67 _argument_list_create (void *ctx);
70 _argument_list_append (argument_list_t *list, token_list_t *argument);
73 _argument_list_length (argument_list_t *list);
76 _argument_list_member_at (argument_list_t *list, int index);
78 /* Note: This function ralloc_steal()s the str pointer. */
80 _token_create_str (void *ctx, int type, char *str);
83 _token_create_ival (void *ctx, int type, int ival);
86 _token_list_create (void *ctx);
89 _token_list_append (token_list_t *list, token_t *token);
92 _token_list_append_list (token_list_t *list, token_list_t *tail);
95 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
98 _parser_active_list_push (glcpp_parser_t *parser,
99 const char *identifier,
100 token_node_t *marker);
103 _parser_active_list_pop (glcpp_parser_t *parser);
106 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier);
108 /* Expand list, and begin lexing from the result (after first
109 * prefixing a token of type 'head_token_type').
112 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
116 /* Perform macro expansion in-place on the given list. */
118 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
122 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
126 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
130 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
131 const char *type, int condition);
134 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
137 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
138 const char *ident, bool explicitly_set);
141 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
144 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
147 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
163 %parse-param {glcpp_parser_t *parser}
164 %lex-param {glcpp_parser_t *parser}
167 %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE FUNC_IDENTIFIER OBJ_IDENTIFIER HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_LINE HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE
169 %type <ival> INTEGER operator SPACE integer_constant
170 %type <expression_value> expression
171 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
172 %type <string_list> identifier_list
173 %type <token> preprocessing_token conditional_token
174 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
180 %left EQUAL NOT_EQUAL
181 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
182 %left LEFT_SHIFT RIGHT_SHIFT
196 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
199 glcpp_parser_resolve_implicit_version(parser);
200 } pp_tokens NEWLINE {
202 if (parser->skip_stack == NULL ||
203 parser->skip_stack->type == SKIP_NO_SKIP)
205 _glcpp_parser_expand_and_lex_from (parser,
210 _glcpp_parser_print_expanded_token_list (parser, $1);
211 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
219 IF_EXPANDED expression NEWLINE {
220 if (parser->is_gles && $2.undefined_macro)
221 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
222 _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
224 | ELIF_EXPANDED expression NEWLINE {
225 if (parser->is_gles && $2.undefined_macro)
226 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
227 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
229 | LINE_EXPANDED integer_constant NEWLINE {
230 parser->has_new_line_number = 1;
231 parser->new_line_number = $2;
232 ralloc_asprintf_rewrite_tail (&parser->output,
233 &parser->output_length,
234 "#line %" PRIiMAX "\n",
237 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
238 parser->has_new_line_number = 1;
239 parser->new_line_number = $2;
240 parser->has_new_source_number = 1;
241 parser->new_source_number = $3;
242 ralloc_asprintf_rewrite_tail (&parser->output,
243 &parser->output_length,
244 "#line %" PRIiMAX " %" PRIiMAX "\n",
250 OBJ_IDENTIFIER replacement_list NEWLINE {
251 _define_object_macro (parser, & @1, $1, $2);
253 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
254 _define_function_macro (parser, & @1, $1, NULL, $4);
256 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
257 _define_function_macro (parser, & @1, $1, $3, $5);
263 glcpp_parser_resolve_implicit_version(parser);
266 glcpp_parser_resolve_implicit_version(parser);
267 } IDENTIFIER NEWLINE {
269 if (strcmp("__LINE__", $3) == 0
270 || strcmp("__FILE__", $3) == 0
271 || strcmp("__VERSION__", $3) == 0)
272 glcpp_error(& @1, parser, "Built-in (pre-defined)"
273 " macro names can not be undefined.");
275 macro = hash_table_find (parser->defines, $3);
277 hash_table_remove (parser->defines, $3);
283 glcpp_parser_resolve_implicit_version(parser);
284 } conditional_tokens NEWLINE {
285 /* Be careful to only evaluate the 'if' expression if
286 * we are not skipping. When we are skipping, we
287 * simply push a new 0-valued 'if' onto the skip
290 * This avoids generating diagnostics for invalid
291 * expressions that are being skipped. */
292 if (parser->skip_stack == NULL ||
293 parser->skip_stack->type == SKIP_NO_SKIP)
295 _glcpp_parser_expand_and_lex_from (parser,
300 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
301 parser->skip_stack->type = SKIP_TO_ENDIF;
305 /* #if without an expression is only an error if we
306 * are not skipping */
307 if (parser->skip_stack == NULL ||
308 parser->skip_stack->type == SKIP_NO_SKIP)
310 glcpp_error(& @1, parser, "#if with no expression");
312 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
315 glcpp_parser_resolve_implicit_version(parser);
316 } IDENTIFIER junk NEWLINE {
317 macro_t *macro = hash_table_find (parser->defines, $3);
319 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
322 glcpp_parser_resolve_implicit_version(parser);
323 } IDENTIFIER junk NEWLINE {
324 macro_t *macro = hash_table_find (parser->defines, $3);
326 _glcpp_parser_skip_stack_push_if (parser, & @2, macro == NULL);
328 | HASH_ELIF conditional_tokens NEWLINE {
329 /* Be careful to only evaluate the 'elif' expression
330 * if we are not skipping. When we are skipping, we
331 * simply change to a 0-valued 'elif' on the skip
334 * This avoids generating diagnostics for invalid
335 * expressions that are being skipped. */
336 if (parser->skip_stack &&
337 parser->skip_stack->type == SKIP_TO_ELSE)
339 _glcpp_parser_expand_and_lex_from (parser,
342 else if (parser->skip_stack &&
343 parser->skip_stack->has_else)
345 glcpp_error(& @1, parser, "#elif after #else");
349 _glcpp_parser_skip_stack_change_if (parser, & @1,
353 | HASH_ELIF NEWLINE {
354 /* #elif without an expression is an error unless we
356 if (parser->skip_stack &&
357 parser->skip_stack->type == SKIP_TO_ELSE)
359 glcpp_error(& @1, parser, "#elif with no expression");
361 else if (parser->skip_stack &&
362 parser->skip_stack->has_else)
364 glcpp_error(& @1, parser, "#elif after #else");
368 _glcpp_parser_skip_stack_change_if (parser, & @1,
370 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
373 | HASH_ELSE { parser->lexing_directive = 1; } NEWLINE {
374 if (parser->skip_stack &&
375 parser->skip_stack->has_else)
377 glcpp_error(& @1, parser, "multiple #else");
381 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
382 if (parser->skip_stack)
383 parser->skip_stack->has_else = true;
387 _glcpp_parser_skip_stack_pop (parser, & @1);
389 | HASH_VERSION integer_constant NEWLINE {
390 if (parser->version_resolved) {
391 glcpp_error(& @1, parser, "#version must appear on the first line");
393 _glcpp_parser_handle_version_declaration(parser, $2, NULL, true);
395 | HASH_VERSION integer_constant IDENTIFIER NEWLINE {
396 if (parser->version_resolved) {
397 glcpp_error(& @1, parser, "#version must appear on the first line");
399 _glcpp_parser_handle_version_declaration(parser, $2, $3, true);
402 glcpp_parser_resolve_implicit_version(parser);
408 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
409 $$ = strtoll ($1 + 2, NULL, 16);
410 } else if ($1[0] == '0') {
411 $$ = strtoll ($1, NULL, 8);
413 $$ = strtoll ($1, NULL, 10);
423 $$.undefined_macro = NULL;
428 $$.undefined_macro = ralloc_strdup (parser, $1);
430 $$.undefined_macro = NULL;
432 | expression OR expression {
433 $$.value = $1.value || $3.value;
435 /* Short-circuit: Only flag undefined from right side
436 * if left side evaluates to false.
438 if ($1.undefined_macro)
439 $$.undefined_macro = $1.undefined_macro;
441 $$.undefined_macro = $3.undefined_macro;
443 | expression AND expression {
444 $$.value = $1.value && $3.value;
446 /* Short-circuit: Only flag undefined from right-side
447 * if left side evaluates to true.
449 if ($1.undefined_macro)
450 $$.undefined_macro = $1.undefined_macro;
452 $$.undefined_macro = $3.undefined_macro;
454 | expression '|' expression {
455 $$.value = $1.value | $3.value;
456 if ($1.undefined_macro)
457 $$.undefined_macro = $1.undefined_macro;
459 $$.undefined_macro = $3.undefined_macro;
461 | expression '^' expression {
462 $$.value = $1.value ^ $3.value;
463 if ($1.undefined_macro)
464 $$.undefined_macro = $1.undefined_macro;
466 $$.undefined_macro = $3.undefined_macro;
468 | expression '&' expression {
469 $$.value = $1.value & $3.value;
470 if ($1.undefined_macro)
471 $$.undefined_macro = $1.undefined_macro;
473 $$.undefined_macro = $3.undefined_macro;
475 | expression NOT_EQUAL expression {
476 $$.value = $1.value != $3.value;
477 if ($1.undefined_macro)
478 $$.undefined_macro = $1.undefined_macro;
480 $$.undefined_macro = $3.undefined_macro;
482 | expression EQUAL expression {
483 $$.value = $1.value == $3.value;
484 if ($1.undefined_macro)
485 $$.undefined_macro = $1.undefined_macro;
487 $$.undefined_macro = $3.undefined_macro;
489 | expression GREATER_OR_EQUAL expression {
490 $$.value = $1.value >= $3.value;
491 if ($1.undefined_macro)
492 $$.undefined_macro = $1.undefined_macro;
494 $$.undefined_macro = $3.undefined_macro;
496 | expression LESS_OR_EQUAL expression {
497 $$.value = $1.value <= $3.value;
498 if ($1.undefined_macro)
499 $$.undefined_macro = $1.undefined_macro;
501 $$.undefined_macro = $3.undefined_macro;
503 | expression '>' expression {
504 $$.value = $1.value > $3.value;
505 if ($1.undefined_macro)
506 $$.undefined_macro = $1.undefined_macro;
508 $$.undefined_macro = $3.undefined_macro;
510 | expression '<' expression {
511 $$.value = $1.value < $3.value;
512 if ($1.undefined_macro)
513 $$.undefined_macro = $1.undefined_macro;
515 $$.undefined_macro = $3.undefined_macro;
517 | expression RIGHT_SHIFT 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 LEFT_SHIFT 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 '+' 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 '%' expression {
547 yyerror (& @1, parser,
548 "zero modulus in preprocessor directive");
550 $$.value = $1.value % $3.value;
552 if ($1.undefined_macro)
553 $$.undefined_macro = $1.undefined_macro;
555 $$.undefined_macro = $3.undefined_macro;
557 | expression '/' expression {
559 yyerror (& @1, parser,
560 "division by 0 in preprocessor directive");
562 $$.value = $1.value / $3.value;
564 if ($1.undefined_macro)
565 $$.undefined_macro = $1.undefined_macro;
567 $$.undefined_macro = $3.undefined_macro;
569 | expression '*' expression {
570 $$.value = $1.value * $3.value;
571 if ($1.undefined_macro)
572 $$.undefined_macro = $1.undefined_macro;
574 $$.undefined_macro = $3.undefined_macro;
576 | '!' expression %prec UNARY {
577 $$.value = ! $2.value;
578 $$.undefined_macro = $2.undefined_macro;
580 | '~' expression %prec UNARY {
581 $$.value = ~ $2.value;
582 $$.undefined_macro = $2.undefined_macro;
584 | '-' expression %prec UNARY {
585 $$.value = - $2.value;
586 $$.undefined_macro = $2.undefined_macro;
588 | '+' expression %prec UNARY {
589 $$.value = + $2.value;
590 $$.undefined_macro = $2.undefined_macro;
592 | '(' expression ')' {
599 $$ = _string_list_create (parser);
600 _string_list_append_item ($$, $1);
601 ralloc_steal ($$, $1);
603 | identifier_list ',' IDENTIFIER {
605 _string_list_append_item ($$, $3);
606 ralloc_steal ($$, $3);
611 NEWLINE { $$ = NULL; }
617 yyerror (& @1, parser, "Invalid tokens after #");
622 /* empty */ { $$ = NULL; }
629 glcpp_error(&@1, parser, "extra tokens at end of directive");
634 /* Handle "defined" operator */
636 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
637 $$ = _token_create_ival (parser, INTEGER, v);
639 | DEFINED '(' IDENTIFIER ')' {
640 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
641 $$ = _token_create_ival (parser, INTEGER, v);
643 | preprocessing_token
647 /* Exactly the same as pp_tokens, but using conditional_token */
649 $$ = _token_list_create (parser);
650 _token_list_append ($$, $1);
652 | conditional_tokens conditional_token {
654 _token_list_append ($$, $2);
659 preprocessing_token {
660 parser->space_tokens = 1;
661 $$ = _token_list_create (parser);
662 _token_list_append ($$, $1);
664 | pp_tokens preprocessing_token {
666 _token_list_append ($$, $2);
672 $$ = _token_create_str (parser, IDENTIFIER, $1);
673 $$->location = yylloc;
676 $$ = _token_create_str (parser, INTEGER_STRING, $1);
677 $$->location = yylloc;
680 $$ = _token_create_ival (parser, $1, $1);
681 $$->location = yylloc;
684 $$ = _token_create_str (parser, OTHER, $1);
685 $$->location = yylloc;
688 $$ = _token_create_ival (parser, SPACE, SPACE);
689 $$->location = yylloc;
709 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
710 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
713 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
714 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
715 | EQUAL { $$ = EQUAL; }
716 | NOT_EQUAL { $$ = NOT_EQUAL; }
724 | PASTE { $$ = PASTE; }
730 _string_list_create (void *ctx)
734 list = ralloc (ctx, string_list_t);
742 _string_list_append_item (string_list_t *list, const char *str)
746 node = ralloc (list, string_node_t);
747 node->str = ralloc_strdup (node, str);
751 if (list->head == NULL) {
754 list->tail->next = node;
761 _string_list_contains (string_list_t *list, const char *member, int *index)
769 for (i = 0, node = list->head; node; i++, node = node->next) {
770 if (strcmp (node->str, member) == 0) {
781 _string_list_length (string_list_t *list)
789 for (node = list->head; node; node = node->next)
796 _string_list_equal (string_list_t *a, string_list_t *b)
798 string_node_t *node_a, *node_b;
800 if (a == NULL && b == NULL)
803 if (a == NULL || b == NULL)
806 for (node_a = a->head, node_b = b->head;
808 node_a = node_a->next, node_b = node_b->next)
810 if (strcmp (node_a->str, node_b->str))
814 /* Catch the case of lists being different lengths, (which
815 * would cause the loop above to terminate after the shorter
817 return node_a == node_b;
821 _argument_list_create (void *ctx)
823 argument_list_t *list;
825 list = ralloc (ctx, argument_list_t);
833 _argument_list_append (argument_list_t *list, token_list_t *argument)
835 argument_node_t *node;
837 node = ralloc (list, argument_node_t);
838 node->argument = argument;
842 if (list->head == NULL) {
845 list->tail->next = node;
852 _argument_list_length (argument_list_t *list)
855 argument_node_t *node;
860 for (node = list->head; node; node = node->next)
867 _argument_list_member_at (argument_list_t *list, int index)
869 argument_node_t *node;
876 for (i = 0; i < index; i++) {
883 return node->argument;
888 /* Note: This function ralloc_steal()s the str pointer. */
890 _token_create_str (void *ctx, int type, char *str)
894 token = ralloc (ctx, token_t);
896 token->value.str = str;
898 ralloc_steal (token, str);
904 _token_create_ival (void *ctx, int type, int ival)
908 token = ralloc (ctx, token_t);
910 token->value.ival = ival;
916 _token_list_create (void *ctx)
920 list = ralloc (ctx, token_list_t);
923 list->non_space_tail = NULL;
929 _token_list_append (token_list_t *list, token_t *token)
933 node = ralloc (list, token_node_t);
937 if (list->head == NULL) {
940 list->tail->next = node;
944 if (token->type != SPACE)
945 list->non_space_tail = node;
949 _token_list_append_list (token_list_t *list, token_list_t *tail)
951 if (tail == NULL || tail->head == NULL)
954 if (list->head == NULL) {
955 list->head = tail->head;
957 list->tail->next = tail->head;
960 list->tail = tail->tail;
961 list->non_space_tail = tail->non_space_tail;
964 static token_list_t *
965 _token_list_copy (void *ctx, token_list_t *other)
973 copy = _token_list_create (ctx);
974 for (node = other->head; node; node = node->next) {
975 token_t *new_token = ralloc (copy, token_t);
976 *new_token = *node->token;
977 _token_list_append (copy, new_token);
984 _token_list_trim_trailing_space (token_list_t *list)
986 token_node_t *tail, *next;
988 if (list->non_space_tail) {
989 tail = list->non_space_tail->next;
990 list->non_space_tail->next = NULL;
991 list->tail = list->non_space_tail;
1002 _token_list_is_empty_ignoring_space (token_list_t *l)
1010 while (n != NULL && n->token->type == SPACE)
1017 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
1019 token_node_t *node_a, *node_b;
1021 if (a == NULL || b == NULL) {
1022 int a_empty = _token_list_is_empty_ignoring_space(a);
1023 int b_empty = _token_list_is_empty_ignoring_space(b);
1024 return a_empty == b_empty;
1032 if (node_a == NULL && node_b == NULL)
1035 if (node_a == NULL || node_b == NULL)
1037 /* Make sure whitespace appears in the same places in both.
1038 * It need not be exactly the same amount of whitespace,
1041 if (node_a->token->type == SPACE
1042 && node_b->token->type == SPACE) {
1043 while (node_a->token->type == SPACE)
1044 node_a = node_a->next;
1045 while (node_b->token->type == SPACE)
1046 node_b = node_b->next;
1050 if (node_a->token->type != node_b->token->type)
1053 switch (node_a->token->type) {
1055 if (node_a->token->value.ival !=
1056 node_b->token->value.ival)
1062 case INTEGER_STRING:
1064 if (strcmp (node_a->token->value.str,
1065 node_b->token->value.str))
1072 node_a = node_a->next;
1073 node_b = node_b->next;
1080 _token_print (char **out, size_t *len, token_t *token)
1082 if (token->type < 256) {
1083 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1087 switch (token->type) {
1089 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1092 case INTEGER_STRING:
1094 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1097 ralloc_asprintf_rewrite_tail (out, len, " ");
1100 ralloc_asprintf_rewrite_tail (out, len, "<<");
1103 ralloc_asprintf_rewrite_tail (out, len, ">>");
1106 ralloc_asprintf_rewrite_tail (out, len, "<=");
1108 case GREATER_OR_EQUAL:
1109 ralloc_asprintf_rewrite_tail (out, len, ">=");
1112 ralloc_asprintf_rewrite_tail (out, len, "==");
1115 ralloc_asprintf_rewrite_tail (out, len, "!=");
1118 ralloc_asprintf_rewrite_tail (out, len, "&&");
1121 ralloc_asprintf_rewrite_tail (out, len, "||");
1124 ralloc_asprintf_rewrite_tail (out, len, "##");
1127 ralloc_asprintf_rewrite_tail (out, len, ",");
1130 /* Nothing to print. */
1133 assert(!"Error: Don't know how to print token.");
1138 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1139 * 'token' and 'other'. Note that this function may return 'token' or
1140 * 'other' directly rather than allocating anything new.
1142 * Caution: Only very cursory error-checking is performed to see if
1143 * the final result is a valid single token. */
1145 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1147 token_t *combined = NULL;
1149 /* Pasting a placeholder onto anything makes no change. */
1150 if (other->type == PLACEHOLDER)
1153 /* When 'token' is a placeholder, just return 'other'. */
1154 if (token->type == PLACEHOLDER)
1157 /* A very few single-character punctuators can be combined
1158 * with another to form a multi-character punctuator. */
1159 switch (token->type) {
1161 if (other->type == '<')
1162 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1163 else if (other->type == '=')
1164 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1167 if (other->type == '>')
1168 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1169 else if (other->type == '=')
1170 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1173 if (other->type == '=')
1174 combined = _token_create_ival (token, EQUAL, EQUAL);
1177 if (other->type == '=')
1178 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1181 if (other->type == '&')
1182 combined = _token_create_ival (token, AND, AND);
1185 if (other->type == '|')
1186 combined = _token_create_ival (token, OR, OR);
1190 if (combined != NULL) {
1191 /* Inherit the location from the first token */
1192 combined->location = token->location;
1196 /* Two string-valued (or integer) tokens can usually just be
1197 * mashed together. (We also handle a string followed by an
1198 * integer here as well.)
1200 * There are some exceptions here. Notably, if the first token
1201 * is an integer (or a string representing an integer), then
1202 * the second token must also be an integer or must be a
1203 * string representing an integer that begins with a digit.
1205 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1206 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1211 /* Check that pasting onto an integer doesn't create a
1212 * non-integer, (that is, only digits can be
1214 if (token->type == INTEGER_STRING || token->type == INTEGER)
1216 switch (other->type) {
1217 case INTEGER_STRING:
1218 if (other->value.str[0] < '0' ||
1219 other->value.str[0] > '9')
1223 if (other->value.ival < 0)
1231 if (token->type == INTEGER)
1232 str = ralloc_asprintf (token, "%" PRIiMAX,
1235 str = ralloc_strdup (token, token->value.str);
1238 if (other->type == INTEGER)
1239 ralloc_asprintf_append (&str, "%" PRIiMAX,
1242 ralloc_strcat (&str, other->value.str);
1244 /* New token is same type as original token, unless we
1245 * started with an integer, in which case we will be
1246 * creating an integer-string. */
1247 combined_type = token->type;
1248 if (combined_type == INTEGER)
1249 combined_type = INTEGER_STRING;
1251 combined = _token_create_str (token, combined_type, str);
1252 combined->location = token->location;
1257 glcpp_error (&token->location, parser, "");
1258 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1259 _token_print (&parser->info_log, &parser->info_log_length, token);
1260 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1261 _token_print (&parser->info_log, &parser->info_log_length, other);
1262 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1268 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1275 for (node = list->head; node; node = node->next)
1276 _token_print (&parser->output, &parser->output_length, node->token);
1280 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1282 glcpp_error(locp, parser, "%s", error);
1285 static void add_builtin_define(glcpp_parser_t *parser,
1286 const char *name, int value)
1291 tok = _token_create_ival (parser, INTEGER, value);
1293 list = _token_list_create(parser);
1294 _token_list_append(list, tok);
1295 _define_object_macro(parser, NULL, name, list);
1299 glcpp_parser_create (const struct gl_extensions *extensions, gl_api api)
1301 glcpp_parser_t *parser;
1303 parser = ralloc (NULL, glcpp_parser_t);
1305 glcpp_lex_init_extra (parser, &parser->scanner);
1306 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1307 hash_table_string_compare);
1308 parser->active = NULL;
1309 parser->lexing_directive = 0;
1310 parser->space_tokens = 1;
1311 parser->last_token_was_newline = 0;
1312 parser->newline_as_space = 0;
1313 parser->in_control_line = 0;
1314 parser->paren_count = 0;
1315 parser->commented_newlines = 0;
1317 parser->skip_stack = NULL;
1319 parser->lex_from_list = NULL;
1320 parser->lex_from_node = NULL;
1322 parser->output = ralloc_strdup(parser, "");
1323 parser->output_length = 0;
1324 parser->info_log = ralloc_strdup(parser, "");
1325 parser->info_log_length = 0;
1328 parser->extensions = extensions;
1330 parser->version_resolved = false;
1332 parser->has_new_line_number = 0;
1333 parser->new_line_number = 1;
1334 parser->has_new_source_number = 0;
1335 parser->new_source_number = 0;
1341 glcpp_parser_destroy (glcpp_parser_t *parser)
1343 glcpp_lex_destroy (parser->scanner);
1344 hash_table_dtor (parser->defines);
1345 ralloc_free (parser);
1348 typedef enum function_status
1350 FUNCTION_STATUS_SUCCESS,
1351 FUNCTION_NOT_A_FUNCTION,
1352 FUNCTION_UNBALANCED_PARENTHESES
1353 } function_status_t;
1355 /* Find a set of function-like macro arguments by looking for a
1356 * balanced set of parentheses.
1358 * When called, 'node' should be the opening-parenthesis token, (or
1359 * perhaps preceeding SPACE tokens). Upon successful return *last will
1360 * be the last consumed node, (corresponding to the closing right
1365 * FUNCTION_STATUS_SUCCESS:
1367 * Successfully parsed a set of function arguments.
1369 * FUNCTION_NOT_A_FUNCTION:
1371 * Macro name not followed by a '('. This is not an error, but
1372 * simply that the macro name should be treated as a non-macro.
1374 * FUNCTION_UNBALANCED_PARENTHESES
1376 * Macro name is not followed by a balanced set of parentheses.
1378 static function_status_t
1379 _arguments_parse (argument_list_t *arguments,
1381 token_node_t **last)
1383 token_list_t *argument;
1388 /* Ignore whitespace before first parenthesis. */
1389 while (node && node->token->type == SPACE)
1392 if (node == NULL || node->token->type != '(')
1393 return FUNCTION_NOT_A_FUNCTION;
1397 argument = _token_list_create (arguments);
1398 _argument_list_append (arguments, argument);
1400 for (paren_count = 1; node; node = node->next) {
1401 if (node->token->type == '(')
1405 else if (node->token->type == ')')
1408 if (paren_count == 0)
1412 if (node->token->type == ',' &&
1415 _token_list_trim_trailing_space (argument);
1416 argument = _token_list_create (arguments);
1417 _argument_list_append (arguments, argument);
1420 if (argument->head == NULL) {
1421 /* Don't treat initial whitespace as
1422 * part of the arguement. */
1423 if (node->token->type == SPACE)
1426 _token_list_append (argument, node->token);
1431 return FUNCTION_UNBALANCED_PARENTHESES;
1435 return FUNCTION_STATUS_SUCCESS;
1438 static token_list_t *
1439 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1444 list = _token_list_create (ctx);
1445 node = _token_create_ival (list, type, ival);
1446 _token_list_append (list, node);
1451 static token_list_t *
1452 _token_list_create_with_one_space (void *ctx)
1454 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1457 static token_list_t *
1458 _token_list_create_with_one_integer (void *ctx, int ival)
1460 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1463 /* Perform macro expansion on 'list', placing the resulting tokens
1464 * into a new list which is initialized with a first token of type
1465 * 'head_token_type'. Then begin lexing from the resulting list,
1466 * (return to the current lexing source when this list is exhausted).
1469 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1470 int head_token_type,
1473 token_list_t *expanded;
1476 expanded = _token_list_create (parser);
1477 token = _token_create_ival (parser, head_token_type, head_token_type);
1478 _token_list_append (expanded, token);
1479 _glcpp_parser_expand_token_list (parser, list);
1480 _token_list_append_list (expanded, list);
1481 glcpp_parser_lex_from (parser, expanded);
1485 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1492 token_node_t *next_non_space;
1494 /* Look ahead for a PASTE token, skipping space. */
1495 next_non_space = node->next;
1496 while (next_non_space && next_non_space->token->type == SPACE)
1497 next_non_space = next_non_space->next;
1499 if (next_non_space == NULL)
1502 if (next_non_space->token->type != PASTE) {
1503 node = next_non_space;
1507 /* Now find the next non-space token after the PASTE. */
1508 next_non_space = next_non_space->next;
1509 while (next_non_space && next_non_space->token->type == SPACE)
1510 next_non_space = next_non_space->next;
1512 if (next_non_space == NULL) {
1513 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1517 node->token = _token_paste (parser, node->token, next_non_space->token);
1518 node->next = next_non_space->next;
1519 if (next_non_space == list->tail)
1523 list->non_space_tail = list->tail;
1526 /* This is a helper function that's essentially part of the
1527 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1528 * except for by that function.
1530 * Returns NULL if node is a simple token with no expansion, (that is,
1531 * although 'node' corresponds to an identifier defined as a
1532 * function-like macro, it is not followed with a parenthesized
1535 * Compute the complete expansion of node (which is a function-like
1536 * macro) and subsequent nodes which are arguments.
1538 * Returns the token list that results from the expansion and sets
1539 * *last to the last node in the list that was consumed by the
1540 * expansion. Specifically, *last will be set as follows: as the
1541 * token of the closing right parenthesis.
1543 static token_list_t *
1544 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1546 token_node_t **last)
1550 const char *identifier;
1551 argument_list_t *arguments;
1552 function_status_t status;
1553 token_list_t *substituted;
1554 int parameter_index;
1556 identifier = node->token->value.str;
1558 macro = hash_table_find (parser->defines, identifier);
1560 assert (macro->is_function);
1562 arguments = _argument_list_create (parser);
1563 status = _arguments_parse (arguments, node, last);
1566 case FUNCTION_STATUS_SUCCESS:
1568 case FUNCTION_NOT_A_FUNCTION:
1570 case FUNCTION_UNBALANCED_PARENTHESES:
1571 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1575 /* Replace a macro defined as empty with a SPACE token. */
1576 if (macro->replacements == NULL) {
1577 ralloc_free (arguments);
1578 return _token_list_create_with_one_space (parser);
1581 if (! ((_argument_list_length (arguments) ==
1582 _string_list_length (macro->parameters)) ||
1583 (_string_list_length (macro->parameters) == 0 &&
1584 _argument_list_length (arguments) == 1 &&
1585 arguments->head->argument->head == NULL)))
1587 glcpp_error (&node->token->location, parser,
1588 "Error: macro %s invoked with %d arguments (expected %d)\n",
1590 _argument_list_length (arguments),
1591 _string_list_length (macro->parameters));
1595 /* Perform argument substitution on the replacement list. */
1596 substituted = _token_list_create (arguments);
1598 for (node = macro->replacements->head; node; node = node->next)
1600 if (node->token->type == IDENTIFIER &&
1601 _string_list_contains (macro->parameters,
1602 node->token->value.str,
1605 token_list_t *argument;
1606 argument = _argument_list_member_at (arguments,
1608 /* Before substituting, we expand the argument
1609 * tokens, or append a placeholder token for
1610 * an empty argument. */
1611 if (argument->head) {
1612 token_list_t *expanded_argument;
1613 expanded_argument = _token_list_copy (parser,
1615 _glcpp_parser_expand_token_list (parser,
1617 _token_list_append_list (substituted,
1622 new_token = _token_create_ival (substituted,
1625 _token_list_append (substituted, new_token);
1628 _token_list_append (substituted, node->token);
1632 /* After argument substitution, and before further expansion
1633 * below, implement token pasting. */
1635 _token_list_trim_trailing_space (substituted);
1637 _glcpp_parser_apply_pastes (parser, substituted);
1642 /* Compute the complete expansion of node, (and subsequent nodes after
1643 * 'node' in the case that 'node' is a function-like macro and
1644 * subsequent nodes are arguments).
1646 * Returns NULL if node is a simple token with no expansion.
1648 * Otherwise, returns the token list that results from the expansion
1649 * and sets *last to the last node in the list that was consumed by
1650 * the expansion. Specifically, *last will be set as follows:
1652 * As 'node' in the case of object-like macro expansion.
1654 * As the token of the closing right parenthesis in the case of
1655 * function-like macro expansion.
1657 static token_list_t *
1658 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1660 token_node_t **last)
1662 token_t *token = node->token;
1663 const char *identifier;
1666 /* We only expand identifiers */
1667 if (token->type != IDENTIFIER) {
1668 /* We change any COMMA into a COMMA_FINAL to prevent
1669 * it being mistaken for an argument separator
1671 if (token->type == ',') {
1672 token->type = COMMA_FINAL;
1673 token->value.ival = COMMA_FINAL;
1680 identifier = token->value.str;
1682 /* Special handling for __LINE__ and __FILE__, (not through
1683 * the hash table). */
1684 if (strcmp(identifier, "__LINE__") == 0)
1685 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1687 if (strcmp(identifier, "__FILE__") == 0)
1688 return _token_list_create_with_one_integer (parser, node->token->location.source);
1690 /* Look up this identifier in the hash table. */
1691 macro = hash_table_find (parser->defines, identifier);
1693 /* Not a macro, so no expansion needed. */
1697 /* Finally, don't expand this macro if we're already actively
1698 * expanding it, (to avoid infinite recursion). */
1699 if (_parser_active_list_contains (parser, identifier)) {
1700 /* We change the token type here from IDENTIFIER to
1701 * OTHER to prevent any future expansion of this
1702 * unexpanded token. */
1704 token_list_t *expansion;
1707 str = ralloc_strdup (parser, token->value.str);
1708 final = _token_create_str (parser, OTHER, str);
1709 expansion = _token_list_create (parser);
1710 _token_list_append (expansion, final);
1714 if (! macro->is_function)
1716 token_list_t *replacement;
1718 /* Replace a macro defined as empty with a SPACE token. */
1719 if (macro->replacements == NULL)
1720 return _token_list_create_with_one_space (parser);
1722 replacement = _token_list_copy (parser, macro->replacements);
1723 _glcpp_parser_apply_pastes (parser, replacement);
1727 return _glcpp_parser_expand_function (parser, node, last);
1730 /* Push a new identifier onto the parser's active list.
1732 * Here, 'marker' is the token node that appears in the list after the
1733 * expansion of 'identifier'. That is, when the list iterator begins
1734 * examining 'marker', then it is time to pop this node from the
1738 _parser_active_list_push (glcpp_parser_t *parser,
1739 const char *identifier,
1740 token_node_t *marker)
1742 active_list_t *node;
1744 node = ralloc (parser->active, active_list_t);
1745 node->identifier = ralloc_strdup (node, identifier);
1746 node->marker = marker;
1747 node->next = parser->active;
1749 parser->active = node;
1753 _parser_active_list_pop (glcpp_parser_t *parser)
1755 active_list_t *node = parser->active;
1758 parser->active = NULL;
1762 node = parser->active->next;
1763 ralloc_free (parser->active);
1765 parser->active = node;
1769 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1771 active_list_t *node;
1773 if (parser->active == NULL)
1776 for (node = parser->active; node; node = node->next)
1777 if (strcmp (node->identifier, identifier) == 0)
1783 /* Walk over the token list replacing nodes with their expansion.
1784 * Whenever nodes are expanded the walking will walk over the new
1785 * nodes, continuing to expand as necessary. The results are placed in
1789 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1792 token_node_t *node_prev;
1793 token_node_t *node, *last = NULL;
1794 token_list_t *expansion;
1795 active_list_t *active_initial = parser->active;
1800 _token_list_trim_trailing_space (list);
1807 while (parser->active && parser->active->marker == node)
1808 _parser_active_list_pop (parser);
1810 expansion = _glcpp_parser_expand_node (parser, node, &last);
1814 for (n = node; n != last->next; n = n->next)
1815 while (parser->active &&
1816 parser->active->marker == n)
1818 _parser_active_list_pop (parser);
1821 _parser_active_list_push (parser,
1822 node->token->value.str,
1825 /* Splice expansion into list, supporting a
1826 * simple deletion if the expansion is
1828 if (expansion->head) {
1830 node_prev->next = expansion->head;
1832 list->head = expansion->head;
1833 expansion->tail->next = last->next;
1834 if (last == list->tail)
1835 list->tail = expansion->tail;
1838 node_prev->next = last->next;
1840 list->head = last->next;
1841 if (last == list->tail)
1847 node = node_prev ? node_prev->next : list->head;
1850 /* Remove any lingering effects of this invocation on the
1851 * active list. That is, pop until the list looks like it did
1852 * at the beginning of this function. */
1853 while (parser->active && parser->active != active_initial)
1854 _parser_active_list_pop (parser);
1856 list->non_space_tail = list->tail;
1860 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1866 _glcpp_parser_expand_token_list (parser, list);
1868 _token_list_trim_trailing_space (list);
1870 _token_list_print (parser, list);
1874 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1875 const char *identifier)
1877 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
1878 * the GLSL ES spec (all versions) say:
1880 * "All macro names containing two consecutive underscores ( __ )
1881 * are reserved for future use as predefined macro names. All
1882 * macro names prefixed with "GL_" ("GL" followed by a single
1883 * underscore) are also reserved."
1885 * The intention is that names containing __ are reserved for internal
1886 * use by the implementation, and names prefixed with GL_ are reserved
1887 * for use by Khronos. Since every extension adds a name prefixed
1888 * with GL_ (i.e., the name of the extension), that should be an
1889 * error. Names simply containing __ are dangerous to use, but should
1892 * A future version of the GLSL specification will clarify this.
1894 if (strstr(identifier, "__")) {
1895 glcpp_warning(loc, parser,
1896 "Macro names containing \"__\" are reserved "
1897 "for use by the implementation.\n");
1899 if (strncmp(identifier, "GL_", 3) == 0) {
1900 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1905 _macro_equal (macro_t *a, macro_t *b)
1907 if (a->is_function != b->is_function)
1910 if (a->is_function) {
1911 if (! _string_list_equal (a->parameters, b->parameters))
1915 return _token_list_equal_ignoring_space (a->replacements,
1920 _define_object_macro (glcpp_parser_t *parser,
1922 const char *identifier,
1923 token_list_t *replacements)
1925 macro_t *macro, *previous;
1928 _check_for_reserved_macro_name(parser, loc, identifier);
1930 macro = ralloc (parser, macro_t);
1932 macro->is_function = 0;
1933 macro->parameters = NULL;
1934 macro->identifier = ralloc_strdup (macro, identifier);
1935 macro->replacements = replacements;
1936 ralloc_steal (macro, replacements);
1938 previous = hash_table_find (parser->defines, identifier);
1940 if (_macro_equal (macro, previous)) {
1941 ralloc_free (macro);
1944 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1948 hash_table_insert (parser->defines, macro, identifier);
1952 _define_function_macro (glcpp_parser_t *parser,
1954 const char *identifier,
1955 string_list_t *parameters,
1956 token_list_t *replacements)
1958 macro_t *macro, *previous;
1960 _check_for_reserved_macro_name(parser, loc, identifier);
1962 macro = ralloc (parser, macro_t);
1963 ralloc_steal (macro, parameters);
1964 ralloc_steal (macro, replacements);
1966 macro->is_function = 1;
1967 macro->parameters = parameters;
1968 macro->identifier = ralloc_strdup (macro, identifier);
1969 macro->replacements = replacements;
1970 previous = hash_table_find (parser->defines, identifier);
1972 if (_macro_equal (macro, previous)) {
1973 ralloc_free (macro);
1976 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1980 hash_table_insert (parser->defines, macro, identifier);
1984 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1989 if (parser->lex_from_list == NULL) {
1990 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1992 /* XXX: This ugly block of code exists for the sole
1993 * purpose of converting a NEWLINE token into a SPACE
1994 * token, but only in the case where we have seen a
1995 * function-like macro name, but have not yet seen its
1996 * closing parenthesis.
1998 * There's perhaps a more compact way to do this with
1999 * mid-rule actions in the grammar.
2001 * I'm definitely not pleased with the complexity of
2004 if (parser->newline_as_space)
2007 parser->paren_count++;
2008 } else if (ret == ')') {
2009 parser->paren_count--;
2010 if (parser->paren_count == 0)
2011 parser->newline_as_space = 0;
2012 } else if (ret == NEWLINE) {
2014 } else if (ret != SPACE) {
2015 if (parser->paren_count == 0)
2016 parser->newline_as_space = 0;
2019 else if (parser->in_control_line)
2022 parser->in_control_line = 0;
2024 else if (ret == HASH_DEFINE ||
2025 ret == HASH_UNDEF || ret == HASH_IF ||
2026 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
2027 ret == HASH_ELIF || ret == HASH_ELSE ||
2028 ret == HASH_ENDIF || ret == HASH)
2030 parser->in_control_line = 1;
2032 else if (ret == IDENTIFIER)
2035 macro = hash_table_find (parser->defines,
2037 if (macro && macro->is_function) {
2038 parser->newline_as_space = 1;
2039 parser->paren_count = 0;
2046 node = parser->lex_from_node;
2049 ralloc_free (parser->lex_from_list);
2050 parser->lex_from_list = NULL;
2054 *yylval = node->token->value;
2055 ret = node->token->type;
2057 parser->lex_from_node = node->next;
2063 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
2067 assert (parser->lex_from_list == NULL);
2069 /* Copy list, eliminating any space tokens. */
2070 parser->lex_from_list = _token_list_create (parser);
2072 for (node = list->head; node; node = node->next) {
2073 if (node->token->type == SPACE)
2075 _token_list_append (parser->lex_from_list, node->token);
2080 parser->lex_from_node = parser->lex_from_list->head;
2082 /* It's possible the list consisted of nothing but whitespace. */
2083 if (parser->lex_from_node == NULL) {
2084 ralloc_free (parser->lex_from_list);
2085 parser->lex_from_list = NULL;
2090 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
2093 skip_type_t current = SKIP_NO_SKIP;
2096 if (parser->skip_stack)
2097 current = parser->skip_stack->type;
2099 node = ralloc (parser, skip_node_t);
2102 if (current == SKIP_NO_SKIP) {
2104 node->type = SKIP_NO_SKIP;
2106 node->type = SKIP_TO_ELSE;
2108 node->type = SKIP_TO_ENDIF;
2111 node->has_else = false;
2112 node->next = parser->skip_stack;
2113 parser->skip_stack = node;
2117 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
2118 const char *type, int condition)
2120 if (parser->skip_stack == NULL) {
2121 glcpp_error (loc, parser, "%s without #if\n", type);
2125 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2127 parser->skip_stack->type = SKIP_NO_SKIP;
2129 parser->skip_stack->type = SKIP_TO_ENDIF;
2134 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
2138 if (parser->skip_stack == NULL) {
2139 glcpp_error (loc, parser, "#endif without #if\n");
2143 node = parser->skip_stack;
2144 parser->skip_stack = node->next;
2149 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2150 const char *es_identifier,
2151 bool explicitly_set)
2153 const struct gl_extensions *extensions = parser->extensions;
2155 if (parser->version_resolved)
2158 parser->version_resolved = true;
2160 add_builtin_define (parser, "__VERSION__", version);
2162 parser->is_gles = (version == 100) ||
2164 (strcmp(es_identifier, "es") == 0));
2166 /* Add pre-defined macros. */
2167 if (parser->is_gles) {
2168 add_builtin_define(parser, "GL_ES", 1);
2169 add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
2171 if (extensions != NULL) {
2172 if (extensions->OES_EGL_image_external)
2173 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2176 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2177 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2178 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2179 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2182 if (extensions != NULL) {
2183 if (extensions->EXT_texture_array)
2184 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2186 if (extensions->ARB_arrays_of_arrays)
2187 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2189 if (extensions->ARB_fragment_coord_conventions)
2190 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2193 if (extensions->ARB_fragment_layer_viewport)
2194 add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
2196 if (extensions->ARB_explicit_attrib_location)
2197 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2199 if (extensions->ARB_explicit_uniform_location)
2200 add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
2202 if (extensions->ARB_shader_texture_lod)
2203 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2205 if (extensions->ARB_draw_instanced)
2206 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2208 if (extensions->ARB_conservative_depth) {
2209 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2210 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2213 if (extensions->ARB_shader_bit_encoding)
2214 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2216 if (extensions->ARB_uniform_buffer_object)
2217 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2219 if (extensions->ARB_texture_cube_map_array)
2220 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2222 if (extensions->ARB_shading_language_packing)
2223 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2225 if (extensions->ARB_texture_multisample)
2226 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2228 if (extensions->ARB_texture_query_levels)
2229 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2231 if (extensions->ARB_texture_query_lod)
2232 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2234 if (extensions->ARB_gpu_shader5)
2235 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2237 if (extensions->AMD_vertex_shader_layer)
2238 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2240 if (extensions->AMD_vertex_shader_viewport_index)
2241 add_builtin_define(parser, "GL_AMD_vertex_shader_viewport_index", 1);
2243 if (extensions->ARB_shading_language_420pack)
2244 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2246 if (extensions->ARB_sample_shading)
2247 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2249 if (extensions->ARB_texture_gather)
2250 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2252 if (extensions->ARB_shader_atomic_counters)
2253 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2255 if (extensions->ARB_viewport_array)
2256 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2258 if (extensions->ARB_compute_shader)
2259 add_builtin_define(parser, "GL_ARB_compute_shader", 1);
2261 if (extensions->ARB_shader_image_load_store)
2262 add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
2266 if (extensions != NULL) {
2267 if (extensions->EXT_shader_integer_mix)
2268 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2272 add_builtin_define(parser, "GL_core_profile", 1);
2274 /* Currently, all ES2/ES3 implementations support highp in the
2275 * fragment shader, so we always define this macro in ES2/ES3.
2276 * If we ever get a driver that doesn't support highp, we'll
2277 * need to add a flag to the gl_context and check that here.
2279 if (version >= 130 || parser->is_gles)
2280 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2282 if (explicitly_set) {
2283 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
2284 "#version %" PRIiMAX "%s%s", version,
2285 es_identifier ? " " : "",
2286 es_identifier ? es_identifier : "");
2290 /* GLSL version if no version is explicitly specified. */
2291 #define IMPLICIT_GLSL_VERSION 110
2293 /* GLSL ES version if no version is explicitly specified. */
2294 #define IMPLICIT_GLSL_ES_VERSION 100
2297 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2299 int language_version = parser->api == API_OPENGLES2 ?
2300 IMPLICIT_GLSL_ES_VERSION :
2301 IMPLICIT_GLSL_VERSION;
2303 _glcpp_parser_handle_version_declaration(parser, language_version,