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
198 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
201 glcpp_parser_resolve_implicit_version(parser);
202 } pp_tokens NEWLINE {
204 if (parser->skip_stack == NULL ||
205 parser->skip_stack->type == SKIP_NO_SKIP)
207 _glcpp_parser_expand_and_lex_from (parser,
212 _glcpp_parser_print_expanded_token_list (parser, $1);
213 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
221 IF_EXPANDED expression NEWLINE {
222 if (parser->is_gles && $2.undefined_macro)
223 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
224 _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
226 | ELIF_EXPANDED expression NEWLINE {
227 if (parser->is_gles && $2.undefined_macro)
228 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
229 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
231 | LINE_EXPANDED integer_constant NEWLINE {
232 parser->has_new_line_number = 1;
233 parser->new_line_number = $2;
234 ralloc_asprintf_rewrite_tail (&parser->output,
235 &parser->output_length,
236 "#line %" PRIiMAX "\n",
239 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
240 parser->has_new_line_number = 1;
241 parser->new_line_number = $2;
242 parser->has_new_source_number = 1;
243 parser->new_source_number = $3;
244 ralloc_asprintf_rewrite_tail (&parser->output,
245 &parser->output_length,
246 "#line %" PRIiMAX " %" PRIiMAX "\n",
252 OBJ_IDENTIFIER replacement_list NEWLINE {
253 _define_object_macro (parser, & @1, $1, $2);
255 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
256 _define_function_macro (parser, & @1, $1, NULL, $4);
258 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
259 _define_function_macro (parser, & @1, $1, $3, $5);
265 glcpp_parser_resolve_implicit_version(parser);
268 glcpp_parser_resolve_implicit_version(parser);
269 } IDENTIFIER NEWLINE {
271 if (strcmp("__LINE__", $3) == 0
272 || strcmp("__FILE__", $3) == 0
273 || strcmp("__VERSION__", $3) == 0)
274 glcpp_error(& @1, parser, "Built-in (pre-defined)"
275 " macro names can not be undefined.");
277 macro = hash_table_find (parser->defines, $3);
279 hash_table_remove (parser->defines, $3);
285 glcpp_parser_resolve_implicit_version(parser);
286 } conditional_tokens NEWLINE {
287 /* Be careful to only evaluate the 'if' expression if
288 * we are not skipping. When we are skipping, we
289 * simply push a new 0-valued 'if' onto the skip
292 * This avoids generating diagnostics for invalid
293 * expressions that are being skipped. */
294 if (parser->skip_stack == NULL ||
295 parser->skip_stack->type == SKIP_NO_SKIP)
297 _glcpp_parser_expand_and_lex_from (parser,
302 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
303 parser->skip_stack->type = SKIP_TO_ENDIF;
307 /* #if without an expression is only an error if we
308 * are not skipping */
309 if (parser->skip_stack == NULL ||
310 parser->skip_stack->type == SKIP_NO_SKIP)
312 glcpp_error(& @1, parser, "#if with no expression");
314 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
317 glcpp_parser_resolve_implicit_version(parser);
318 } IDENTIFIER junk NEWLINE {
319 macro_t *macro = hash_table_find (parser->defines, $3);
321 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
324 glcpp_parser_resolve_implicit_version(parser);
325 } IDENTIFIER junk NEWLINE {
326 macro_t *macro = hash_table_find (parser->defines, $3);
328 _glcpp_parser_skip_stack_push_if (parser, & @2, macro == NULL);
330 | HASH_ELIF conditional_tokens NEWLINE {
331 /* Be careful to only evaluate the 'elif' expression
332 * if we are not skipping. When we are skipping, we
333 * simply change to a 0-valued 'elif' on the skip
336 * This avoids generating diagnostics for invalid
337 * expressions that are being skipped. */
338 if (parser->skip_stack &&
339 parser->skip_stack->type == SKIP_TO_ELSE)
341 _glcpp_parser_expand_and_lex_from (parser,
344 else if (parser->skip_stack &&
345 parser->skip_stack->has_else)
347 glcpp_error(& @1, parser, "#elif after #else");
351 _glcpp_parser_skip_stack_change_if (parser, & @1,
355 | HASH_ELIF NEWLINE {
356 /* #elif without an expression is an error unless we
358 if (parser->skip_stack &&
359 parser->skip_stack->type == SKIP_TO_ELSE)
361 glcpp_error(& @1, parser, "#elif with no expression");
363 else if (parser->skip_stack &&
364 parser->skip_stack->has_else)
366 glcpp_error(& @1, parser, "#elif after #else");
370 _glcpp_parser_skip_stack_change_if (parser, & @1,
372 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
375 | HASH_ELSE { parser->lexing_directive = 1; } NEWLINE {
376 if (parser->skip_stack &&
377 parser->skip_stack->has_else)
379 glcpp_error(& @1, parser, "multiple #else");
383 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
384 if (parser->skip_stack)
385 parser->skip_stack->has_else = true;
389 _glcpp_parser_skip_stack_pop (parser, & @1);
391 | HASH_VERSION integer_constant NEWLINE {
392 if (parser->version_resolved) {
393 glcpp_error(& @1, parser, "#version must appear on the first line");
395 _glcpp_parser_handle_version_declaration(parser, $2, NULL, true);
397 | HASH_VERSION integer_constant IDENTIFIER NEWLINE {
398 if (parser->version_resolved) {
399 glcpp_error(& @1, parser, "#version must appear on the first line");
401 _glcpp_parser_handle_version_declaration(parser, $2, $3, true);
404 glcpp_parser_resolve_implicit_version(parser);
410 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
411 $$ = strtoll ($1 + 2, NULL, 16);
412 } else if ($1[0] == '0') {
413 $$ = strtoll ($1, NULL, 8);
415 $$ = strtoll ($1, NULL, 10);
425 $$.undefined_macro = NULL;
430 $$.undefined_macro = ralloc_strdup (parser, $1);
432 $$.undefined_macro = NULL;
434 | expression OR expression {
435 $$.value = $1.value || $3.value;
437 /* Short-circuit: Only flag undefined from right side
438 * if left side evaluates to false.
440 if ($1.undefined_macro)
441 $$.undefined_macro = $1.undefined_macro;
443 $$.undefined_macro = $3.undefined_macro;
445 | expression AND expression {
446 $$.value = $1.value && $3.value;
448 /* Short-circuit: Only flag undefined from right-side
449 * if left side evaluates to true.
451 if ($1.undefined_macro)
452 $$.undefined_macro = $1.undefined_macro;
454 $$.undefined_macro = $3.undefined_macro;
456 | expression '|' expression {
457 $$.value = $1.value | $3.value;
458 if ($1.undefined_macro)
459 $$.undefined_macro = $1.undefined_macro;
461 $$.undefined_macro = $3.undefined_macro;
463 | expression '^' expression {
464 $$.value = $1.value ^ $3.value;
465 if ($1.undefined_macro)
466 $$.undefined_macro = $1.undefined_macro;
468 $$.undefined_macro = $3.undefined_macro;
470 | expression '&' expression {
471 $$.value = $1.value & $3.value;
472 if ($1.undefined_macro)
473 $$.undefined_macro = $1.undefined_macro;
475 $$.undefined_macro = $3.undefined_macro;
477 | expression NOT_EQUAL expression {
478 $$.value = $1.value != $3.value;
479 if ($1.undefined_macro)
480 $$.undefined_macro = $1.undefined_macro;
482 $$.undefined_macro = $3.undefined_macro;
484 | expression EQUAL expression {
485 $$.value = $1.value == $3.value;
486 if ($1.undefined_macro)
487 $$.undefined_macro = $1.undefined_macro;
489 $$.undefined_macro = $3.undefined_macro;
491 | expression GREATER_OR_EQUAL expression {
492 $$.value = $1.value >= $3.value;
493 if ($1.undefined_macro)
494 $$.undefined_macro = $1.undefined_macro;
496 $$.undefined_macro = $3.undefined_macro;
498 | expression LESS_OR_EQUAL expression {
499 $$.value = $1.value <= $3.value;
500 if ($1.undefined_macro)
501 $$.undefined_macro = $1.undefined_macro;
503 $$.undefined_macro = $3.undefined_macro;
505 | expression '>' expression {
506 $$.value = $1.value > $3.value;
507 if ($1.undefined_macro)
508 $$.undefined_macro = $1.undefined_macro;
510 $$.undefined_macro = $3.undefined_macro;
512 | expression '<' expression {
513 $$.value = $1.value < $3.value;
514 if ($1.undefined_macro)
515 $$.undefined_macro = $1.undefined_macro;
517 $$.undefined_macro = $3.undefined_macro;
519 | expression RIGHT_SHIFT expression {
520 $$.value = $1.value >> $3.value;
521 if ($1.undefined_macro)
522 $$.undefined_macro = $1.undefined_macro;
524 $$.undefined_macro = $3.undefined_macro;
526 | expression LEFT_SHIFT expression {
527 $$.value = $1.value << $3.value;
528 if ($1.undefined_macro)
529 $$.undefined_macro = $1.undefined_macro;
531 $$.undefined_macro = $3.undefined_macro;
533 | expression '-' expression {
534 $$.value = $1.value - $3.value;
535 if ($1.undefined_macro)
536 $$.undefined_macro = $1.undefined_macro;
538 $$.undefined_macro = $3.undefined_macro;
540 | expression '+' expression {
541 $$.value = $1.value + $3.value;
542 if ($1.undefined_macro)
543 $$.undefined_macro = $1.undefined_macro;
545 $$.undefined_macro = $3.undefined_macro;
547 | expression '%' expression {
549 yyerror (& @1, parser,
550 "zero modulus in preprocessor directive");
552 $$.value = $1.value % $3.value;
554 if ($1.undefined_macro)
555 $$.undefined_macro = $1.undefined_macro;
557 $$.undefined_macro = $3.undefined_macro;
559 | expression '/' expression {
561 yyerror (& @1, parser,
562 "division by 0 in preprocessor directive");
564 $$.value = $1.value / $3.value;
566 if ($1.undefined_macro)
567 $$.undefined_macro = $1.undefined_macro;
569 $$.undefined_macro = $3.undefined_macro;
571 | expression '*' expression {
572 $$.value = $1.value * $3.value;
573 if ($1.undefined_macro)
574 $$.undefined_macro = $1.undefined_macro;
576 $$.undefined_macro = $3.undefined_macro;
578 | '!' expression %prec UNARY {
579 $$.value = ! $2.value;
580 $$.undefined_macro = $2.undefined_macro;
582 | '~' expression %prec UNARY {
583 $$.value = ~ $2.value;
584 $$.undefined_macro = $2.undefined_macro;
586 | '-' expression %prec UNARY {
587 $$.value = - $2.value;
588 $$.undefined_macro = $2.undefined_macro;
590 | '+' expression %prec UNARY {
591 $$.value = + $2.value;
592 $$.undefined_macro = $2.undefined_macro;
594 | '(' expression ')' {
601 $$ = _string_list_create (parser);
602 _string_list_append_item ($$, $1);
603 ralloc_steal ($$, $1);
605 | identifier_list ',' IDENTIFIER {
607 _string_list_append_item ($$, $3);
608 ralloc_steal ($$, $3);
613 NEWLINE { $$ = NULL; }
619 yyerror (& @1, parser, "Invalid tokens after #");
624 /* empty */ { $$ = NULL; }
631 glcpp_error(&@1, parser, "extra tokens at end of directive");
636 /* Handle "defined" operator */
638 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
639 $$ = _token_create_ival (parser, INTEGER, v);
641 | DEFINED '(' IDENTIFIER ')' {
642 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
643 $$ = _token_create_ival (parser, INTEGER, v);
645 | preprocessing_token
649 /* Exactly the same as pp_tokens, but using conditional_token */
651 $$ = _token_list_create (parser);
652 _token_list_append ($$, $1);
654 | conditional_tokens conditional_token {
656 _token_list_append ($$, $2);
661 preprocessing_token {
662 parser->space_tokens = 1;
663 $$ = _token_list_create (parser);
664 _token_list_append ($$, $1);
666 | pp_tokens preprocessing_token {
668 _token_list_append ($$, $2);
674 $$ = _token_create_str (parser, IDENTIFIER, $1);
675 $$->location = yylloc;
678 $$ = _token_create_str (parser, INTEGER_STRING, $1);
679 $$->location = yylloc;
682 $$ = _token_create_ival (parser, $1, $1);
683 $$->location = yylloc;
686 $$ = _token_create_str (parser, OTHER, $1);
687 $$->location = yylloc;
690 $$ = _token_create_ival (parser, SPACE, SPACE);
691 $$->location = yylloc;
711 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
712 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
715 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
716 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
717 | EQUAL { $$ = EQUAL; }
718 | NOT_EQUAL { $$ = NOT_EQUAL; }
726 | PASTE { $$ = PASTE; }
732 _string_list_create (void *ctx)
736 list = ralloc (ctx, string_list_t);
744 _string_list_append_item (string_list_t *list, const char *str)
748 node = ralloc (list, string_node_t);
749 node->str = ralloc_strdup (node, str);
753 if (list->head == NULL) {
756 list->tail->next = node;
763 _string_list_contains (string_list_t *list, const char *member, int *index)
771 for (i = 0, node = list->head; node; i++, node = node->next) {
772 if (strcmp (node->str, member) == 0) {
783 _string_list_length (string_list_t *list)
791 for (node = list->head; node; node = node->next)
798 _string_list_equal (string_list_t *a, string_list_t *b)
800 string_node_t *node_a, *node_b;
802 if (a == NULL && b == NULL)
805 if (a == NULL || b == NULL)
808 for (node_a = a->head, node_b = b->head;
810 node_a = node_a->next, node_b = node_b->next)
812 if (strcmp (node_a->str, node_b->str))
816 /* Catch the case of lists being different lengths, (which
817 * would cause the loop above to terminate after the shorter
819 return node_a == node_b;
823 _argument_list_create (void *ctx)
825 argument_list_t *list;
827 list = ralloc (ctx, argument_list_t);
835 _argument_list_append (argument_list_t *list, token_list_t *argument)
837 argument_node_t *node;
839 node = ralloc (list, argument_node_t);
840 node->argument = argument;
844 if (list->head == NULL) {
847 list->tail->next = node;
854 _argument_list_length (argument_list_t *list)
857 argument_node_t *node;
862 for (node = list->head; node; node = node->next)
869 _argument_list_member_at (argument_list_t *list, int index)
871 argument_node_t *node;
878 for (i = 0; i < index; i++) {
885 return node->argument;
890 /* Note: This function ralloc_steal()s the str pointer. */
892 _token_create_str (void *ctx, int type, char *str)
896 token = ralloc (ctx, token_t);
898 token->value.str = str;
900 ralloc_steal (token, str);
906 _token_create_ival (void *ctx, int type, int ival)
910 token = ralloc (ctx, token_t);
912 token->value.ival = ival;
918 _token_list_create (void *ctx)
922 list = ralloc (ctx, token_list_t);
925 list->non_space_tail = NULL;
931 _token_list_append (token_list_t *list, token_t *token)
935 node = ralloc (list, token_node_t);
939 if (list->head == NULL) {
942 list->tail->next = node;
946 if (token->type != SPACE)
947 list->non_space_tail = node;
951 _token_list_append_list (token_list_t *list, token_list_t *tail)
953 if (tail == NULL || tail->head == NULL)
956 if (list->head == NULL) {
957 list->head = tail->head;
959 list->tail->next = tail->head;
962 list->tail = tail->tail;
963 list->non_space_tail = tail->non_space_tail;
966 static token_list_t *
967 _token_list_copy (void *ctx, token_list_t *other)
975 copy = _token_list_create (ctx);
976 for (node = other->head; node; node = node->next) {
977 token_t *new_token = ralloc (copy, token_t);
978 *new_token = *node->token;
979 _token_list_append (copy, new_token);
986 _token_list_trim_trailing_space (token_list_t *list)
988 token_node_t *tail, *next;
990 if (list->non_space_tail) {
991 tail = list->non_space_tail->next;
992 list->non_space_tail->next = NULL;
993 list->tail = list->non_space_tail;
1004 _token_list_is_empty_ignoring_space (token_list_t *l)
1012 while (n != NULL && n->token->type == SPACE)
1019 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
1021 token_node_t *node_a, *node_b;
1023 if (a == NULL || b == NULL) {
1024 int a_empty = _token_list_is_empty_ignoring_space(a);
1025 int b_empty = _token_list_is_empty_ignoring_space(b);
1026 return a_empty == b_empty;
1034 if (node_a == NULL && node_b == NULL)
1037 if (node_a == NULL || node_b == NULL)
1039 /* Make sure whitespace appears in the same places in both.
1040 * It need not be exactly the same amount of whitespace,
1043 if (node_a->token->type == SPACE
1044 && node_b->token->type == SPACE) {
1045 while (node_a->token->type == SPACE)
1046 node_a = node_a->next;
1047 while (node_b->token->type == SPACE)
1048 node_b = node_b->next;
1052 if (node_a->token->type != node_b->token->type)
1055 switch (node_a->token->type) {
1057 if (node_a->token->value.ival !=
1058 node_b->token->value.ival)
1064 case INTEGER_STRING:
1066 if (strcmp (node_a->token->value.str,
1067 node_b->token->value.str))
1074 node_a = node_a->next;
1075 node_b = node_b->next;
1082 _token_print (char **out, size_t *len, token_t *token)
1084 if (token->type < 256) {
1085 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1089 switch (token->type) {
1091 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1094 case INTEGER_STRING:
1096 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1099 ralloc_asprintf_rewrite_tail (out, len, " ");
1102 ralloc_asprintf_rewrite_tail (out, len, "<<");
1105 ralloc_asprintf_rewrite_tail (out, len, ">>");
1108 ralloc_asprintf_rewrite_tail (out, len, "<=");
1110 case GREATER_OR_EQUAL:
1111 ralloc_asprintf_rewrite_tail (out, len, ">=");
1114 ralloc_asprintf_rewrite_tail (out, len, "==");
1117 ralloc_asprintf_rewrite_tail (out, len, "!=");
1120 ralloc_asprintf_rewrite_tail (out, len, "&&");
1123 ralloc_asprintf_rewrite_tail (out, len, "||");
1126 ralloc_asprintf_rewrite_tail (out, len, "##");
1129 ralloc_asprintf_rewrite_tail (out, len, ",");
1132 /* Nothing to print. */
1135 assert(!"Error: Don't know how to print token.");
1140 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1141 * 'token' and 'other'. Note that this function may return 'token' or
1142 * 'other' directly rather than allocating anything new.
1144 * Caution: Only very cursory error-checking is performed to see if
1145 * the final result is a valid single token. */
1147 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1149 token_t *combined = NULL;
1151 /* Pasting a placeholder onto anything makes no change. */
1152 if (other->type == PLACEHOLDER)
1155 /* When 'token' is a placeholder, just return 'other'. */
1156 if (token->type == PLACEHOLDER)
1159 /* A very few single-character punctuators can be combined
1160 * with another to form a multi-character punctuator. */
1161 switch (token->type) {
1163 if (other->type == '<')
1164 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1165 else if (other->type == '=')
1166 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1169 if (other->type == '>')
1170 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1171 else if (other->type == '=')
1172 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1175 if (other->type == '=')
1176 combined = _token_create_ival (token, EQUAL, EQUAL);
1179 if (other->type == '=')
1180 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1183 if (other->type == '&')
1184 combined = _token_create_ival (token, AND, AND);
1187 if (other->type == '|')
1188 combined = _token_create_ival (token, OR, OR);
1192 if (combined != NULL) {
1193 /* Inherit the location from the first token */
1194 combined->location = token->location;
1198 /* Two string-valued (or integer) tokens can usually just be
1199 * mashed together. (We also handle a string followed by an
1200 * integer here as well.)
1202 * There are some exceptions here. Notably, if the first token
1203 * is an integer (or a string representing an integer), then
1204 * the second token must also be an integer or must be a
1205 * string representing an integer that begins with a digit.
1207 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1208 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1213 /* Check that pasting onto an integer doesn't create a
1214 * non-integer, (that is, only digits can be
1216 if (token->type == INTEGER_STRING || token->type == INTEGER)
1218 switch (other->type) {
1219 case INTEGER_STRING:
1220 if (other->value.str[0] < '0' ||
1221 other->value.str[0] > '9')
1225 if (other->value.ival < 0)
1233 if (token->type == INTEGER)
1234 str = ralloc_asprintf (token, "%" PRIiMAX,
1237 str = ralloc_strdup (token, token->value.str);
1240 if (other->type == INTEGER)
1241 ralloc_asprintf_append (&str, "%" PRIiMAX,
1244 ralloc_strcat (&str, other->value.str);
1246 /* New token is same type as original token, unless we
1247 * started with an integer, in which case we will be
1248 * creating an integer-string. */
1249 combined_type = token->type;
1250 if (combined_type == INTEGER)
1251 combined_type = INTEGER_STRING;
1253 combined = _token_create_str (token, combined_type, str);
1254 combined->location = token->location;
1259 glcpp_error (&token->location, parser, "");
1260 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1261 _token_print (&parser->info_log, &parser->info_log_length, token);
1262 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1263 _token_print (&parser->info_log, &parser->info_log_length, other);
1264 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1270 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1277 for (node = list->head; node; node = node->next)
1278 _token_print (&parser->output, &parser->output_length, node->token);
1282 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1284 glcpp_error(locp, parser, "%s", error);
1287 static void add_builtin_define(glcpp_parser_t *parser,
1288 const char *name, int value)
1293 tok = _token_create_ival (parser, INTEGER, value);
1295 list = _token_list_create(parser);
1296 _token_list_append(list, tok);
1297 _define_object_macro(parser, NULL, name, list);
1301 glcpp_parser_create (const struct gl_extensions *extensions, gl_api api)
1303 glcpp_parser_t *parser;
1305 parser = ralloc (NULL, glcpp_parser_t);
1307 glcpp_lex_init_extra (parser, &parser->scanner);
1308 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1309 hash_table_string_compare);
1310 parser->active = NULL;
1311 parser->lexing_directive = 0;
1312 parser->space_tokens = 1;
1313 parser->last_token_was_newline = 0;
1314 parser->newline_as_space = 0;
1315 parser->in_control_line = 0;
1316 parser->paren_count = 0;
1317 parser->commented_newlines = 0;
1319 parser->skip_stack = NULL;
1320 parser->skipping = 0;
1322 parser->lex_from_list = NULL;
1323 parser->lex_from_node = NULL;
1325 parser->output = ralloc_strdup(parser, "");
1326 parser->output_length = 0;
1327 parser->info_log = ralloc_strdup(parser, "");
1328 parser->info_log_length = 0;
1331 parser->extensions = extensions;
1333 parser->version_resolved = false;
1335 parser->has_new_line_number = 0;
1336 parser->new_line_number = 1;
1337 parser->has_new_source_number = 0;
1338 parser->new_source_number = 0;
1344 glcpp_parser_destroy (glcpp_parser_t *parser)
1346 glcpp_lex_destroy (parser->scanner);
1347 hash_table_dtor (parser->defines);
1348 ralloc_free (parser);
1351 typedef enum function_status
1353 FUNCTION_STATUS_SUCCESS,
1354 FUNCTION_NOT_A_FUNCTION,
1355 FUNCTION_UNBALANCED_PARENTHESES
1356 } function_status_t;
1358 /* Find a set of function-like macro arguments by looking for a
1359 * balanced set of parentheses.
1361 * When called, 'node' should be the opening-parenthesis token, (or
1362 * perhaps preceeding SPACE tokens). Upon successful return *last will
1363 * be the last consumed node, (corresponding to the closing right
1368 * FUNCTION_STATUS_SUCCESS:
1370 * Successfully parsed a set of function arguments.
1372 * FUNCTION_NOT_A_FUNCTION:
1374 * Macro name not followed by a '('. This is not an error, but
1375 * simply that the macro name should be treated as a non-macro.
1377 * FUNCTION_UNBALANCED_PARENTHESES
1379 * Macro name is not followed by a balanced set of parentheses.
1381 static function_status_t
1382 _arguments_parse (argument_list_t *arguments,
1384 token_node_t **last)
1386 token_list_t *argument;
1391 /* Ignore whitespace before first parenthesis. */
1392 while (node && node->token->type == SPACE)
1395 if (node == NULL || node->token->type != '(')
1396 return FUNCTION_NOT_A_FUNCTION;
1400 argument = _token_list_create (arguments);
1401 _argument_list_append (arguments, argument);
1403 for (paren_count = 1; node; node = node->next) {
1404 if (node->token->type == '(')
1408 else if (node->token->type == ')')
1411 if (paren_count == 0)
1415 if (node->token->type == ',' &&
1418 _token_list_trim_trailing_space (argument);
1419 argument = _token_list_create (arguments);
1420 _argument_list_append (arguments, argument);
1423 if (argument->head == NULL) {
1424 /* Don't treat initial whitespace as
1425 * part of the arguement. */
1426 if (node->token->type == SPACE)
1429 _token_list_append (argument, node->token);
1434 return FUNCTION_UNBALANCED_PARENTHESES;
1438 return FUNCTION_STATUS_SUCCESS;
1441 static token_list_t *
1442 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1447 list = _token_list_create (ctx);
1448 node = _token_create_ival (list, type, ival);
1449 _token_list_append (list, node);
1454 static token_list_t *
1455 _token_list_create_with_one_space (void *ctx)
1457 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1460 static token_list_t *
1461 _token_list_create_with_one_integer (void *ctx, int ival)
1463 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1466 /* Perform macro expansion on 'list', placing the resulting tokens
1467 * into a new list which is initialized with a first token of type
1468 * 'head_token_type'. Then begin lexing from the resulting list,
1469 * (return to the current lexing source when this list is exhausted).
1472 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1473 int head_token_type,
1476 token_list_t *expanded;
1479 expanded = _token_list_create (parser);
1480 token = _token_create_ival (parser, head_token_type, head_token_type);
1481 _token_list_append (expanded, token);
1482 _glcpp_parser_expand_token_list (parser, list);
1483 _token_list_append_list (expanded, list);
1484 glcpp_parser_lex_from (parser, expanded);
1488 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1495 token_node_t *next_non_space;
1497 /* Look ahead for a PASTE token, skipping space. */
1498 next_non_space = node->next;
1499 while (next_non_space && next_non_space->token->type == SPACE)
1500 next_non_space = next_non_space->next;
1502 if (next_non_space == NULL)
1505 if (next_non_space->token->type != PASTE) {
1506 node = next_non_space;
1510 /* Now find the next non-space token after the PASTE. */
1511 next_non_space = next_non_space->next;
1512 while (next_non_space && next_non_space->token->type == SPACE)
1513 next_non_space = next_non_space->next;
1515 if (next_non_space == NULL) {
1516 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1520 node->token = _token_paste (parser, node->token, next_non_space->token);
1521 node->next = next_non_space->next;
1522 if (next_non_space == list->tail)
1526 list->non_space_tail = list->tail;
1529 /* This is a helper function that's essentially part of the
1530 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1531 * except for by that function.
1533 * Returns NULL if node is a simple token with no expansion, (that is,
1534 * although 'node' corresponds to an identifier defined as a
1535 * function-like macro, it is not followed with a parenthesized
1538 * Compute the complete expansion of node (which is a function-like
1539 * macro) and subsequent nodes which are arguments.
1541 * Returns the token list that results from the expansion and sets
1542 * *last to the last node in the list that was consumed by the
1543 * expansion. Specifically, *last will be set as follows: as the
1544 * token of the closing right parenthesis.
1546 static token_list_t *
1547 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1549 token_node_t **last)
1553 const char *identifier;
1554 argument_list_t *arguments;
1555 function_status_t status;
1556 token_list_t *substituted;
1557 int parameter_index;
1559 identifier = node->token->value.str;
1561 macro = hash_table_find (parser->defines, identifier);
1563 assert (macro->is_function);
1565 arguments = _argument_list_create (parser);
1566 status = _arguments_parse (arguments, node, last);
1569 case FUNCTION_STATUS_SUCCESS:
1571 case FUNCTION_NOT_A_FUNCTION:
1573 case FUNCTION_UNBALANCED_PARENTHESES:
1574 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1578 /* Replace a macro defined as empty with a SPACE token. */
1579 if (macro->replacements == NULL) {
1580 ralloc_free (arguments);
1581 return _token_list_create_with_one_space (parser);
1584 if (! ((_argument_list_length (arguments) ==
1585 _string_list_length (macro->parameters)) ||
1586 (_string_list_length (macro->parameters) == 0 &&
1587 _argument_list_length (arguments) == 1 &&
1588 arguments->head->argument->head == NULL)))
1590 glcpp_error (&node->token->location, parser,
1591 "Error: macro %s invoked with %d arguments (expected %d)\n",
1593 _argument_list_length (arguments),
1594 _string_list_length (macro->parameters));
1598 /* Perform argument substitution on the replacement list. */
1599 substituted = _token_list_create (arguments);
1601 for (node = macro->replacements->head; node; node = node->next)
1603 if (node->token->type == IDENTIFIER &&
1604 _string_list_contains (macro->parameters,
1605 node->token->value.str,
1608 token_list_t *argument;
1609 argument = _argument_list_member_at (arguments,
1611 /* Before substituting, we expand the argument
1612 * tokens, or append a placeholder token for
1613 * an empty argument. */
1614 if (argument->head) {
1615 token_list_t *expanded_argument;
1616 expanded_argument = _token_list_copy (parser,
1618 _glcpp_parser_expand_token_list (parser,
1620 _token_list_append_list (substituted,
1625 new_token = _token_create_ival (substituted,
1628 _token_list_append (substituted, new_token);
1631 _token_list_append (substituted, node->token);
1635 /* After argument substitution, and before further expansion
1636 * below, implement token pasting. */
1638 _token_list_trim_trailing_space (substituted);
1640 _glcpp_parser_apply_pastes (parser, substituted);
1645 /* Compute the complete expansion of node, (and subsequent nodes after
1646 * 'node' in the case that 'node' is a function-like macro and
1647 * subsequent nodes are arguments).
1649 * Returns NULL if node is a simple token with no expansion.
1651 * Otherwise, returns the token list that results from the expansion
1652 * and sets *last to the last node in the list that was consumed by
1653 * the expansion. Specifically, *last will be set as follows:
1655 * As 'node' in the case of object-like macro expansion.
1657 * As the token of the closing right parenthesis in the case of
1658 * function-like macro expansion.
1660 static token_list_t *
1661 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1663 token_node_t **last)
1665 token_t *token = node->token;
1666 const char *identifier;
1669 /* We only expand identifiers */
1670 if (token->type != IDENTIFIER) {
1671 /* We change any COMMA into a COMMA_FINAL to prevent
1672 * it being mistaken for an argument separator
1674 if (token->type == ',') {
1675 token->type = COMMA_FINAL;
1676 token->value.ival = COMMA_FINAL;
1683 identifier = token->value.str;
1685 /* Special handling for __LINE__ and __FILE__, (not through
1686 * the hash table). */
1687 if (strcmp(identifier, "__LINE__") == 0)
1688 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1690 if (strcmp(identifier, "__FILE__") == 0)
1691 return _token_list_create_with_one_integer (parser, node->token->location.source);
1693 /* Look up this identifier in the hash table. */
1694 macro = hash_table_find (parser->defines, identifier);
1696 /* Not a macro, so no expansion needed. */
1700 /* Finally, don't expand this macro if we're already actively
1701 * expanding it, (to avoid infinite recursion). */
1702 if (_parser_active_list_contains (parser, identifier)) {
1703 /* We change the token type here from IDENTIFIER to
1704 * OTHER to prevent any future expansion of this
1705 * unexpanded token. */
1707 token_list_t *expansion;
1710 str = ralloc_strdup (parser, token->value.str);
1711 final = _token_create_str (parser, OTHER, str);
1712 expansion = _token_list_create (parser);
1713 _token_list_append (expansion, final);
1717 if (! macro->is_function)
1719 token_list_t *replacement;
1721 /* Replace a macro defined as empty with a SPACE token. */
1722 if (macro->replacements == NULL)
1723 return _token_list_create_with_one_space (parser);
1725 replacement = _token_list_copy (parser, macro->replacements);
1726 _glcpp_parser_apply_pastes (parser, replacement);
1730 return _glcpp_parser_expand_function (parser, node, last);
1733 /* Push a new identifier onto the parser's active list.
1735 * Here, 'marker' is the token node that appears in the list after the
1736 * expansion of 'identifier'. That is, when the list iterator begins
1737 * examining 'marker', then it is time to pop this node from the
1741 _parser_active_list_push (glcpp_parser_t *parser,
1742 const char *identifier,
1743 token_node_t *marker)
1745 active_list_t *node;
1747 node = ralloc (parser->active, active_list_t);
1748 node->identifier = ralloc_strdup (node, identifier);
1749 node->marker = marker;
1750 node->next = parser->active;
1752 parser->active = node;
1756 _parser_active_list_pop (glcpp_parser_t *parser)
1758 active_list_t *node = parser->active;
1761 parser->active = NULL;
1765 node = parser->active->next;
1766 ralloc_free (parser->active);
1768 parser->active = node;
1772 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1774 active_list_t *node;
1776 if (parser->active == NULL)
1779 for (node = parser->active; node; node = node->next)
1780 if (strcmp (node->identifier, identifier) == 0)
1786 /* Walk over the token list replacing nodes with their expansion.
1787 * Whenever nodes are expanded the walking will walk over the new
1788 * nodes, continuing to expand as necessary. The results are placed in
1792 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1795 token_node_t *node_prev;
1796 token_node_t *node, *last = NULL;
1797 token_list_t *expansion;
1798 active_list_t *active_initial = parser->active;
1803 _token_list_trim_trailing_space (list);
1810 while (parser->active && parser->active->marker == node)
1811 _parser_active_list_pop (parser);
1813 expansion = _glcpp_parser_expand_node (parser, node, &last);
1817 for (n = node; n != last->next; n = n->next)
1818 while (parser->active &&
1819 parser->active->marker == n)
1821 _parser_active_list_pop (parser);
1824 _parser_active_list_push (parser,
1825 node->token->value.str,
1828 /* Splice expansion into list, supporting a
1829 * simple deletion if the expansion is
1831 if (expansion->head) {
1833 node_prev->next = expansion->head;
1835 list->head = expansion->head;
1836 expansion->tail->next = last->next;
1837 if (last == list->tail)
1838 list->tail = expansion->tail;
1841 node_prev->next = last->next;
1843 list->head = last->next;
1844 if (last == list->tail)
1850 node = node_prev ? node_prev->next : list->head;
1853 /* Remove any lingering effects of this invocation on the
1854 * active list. That is, pop until the list looks like it did
1855 * at the beginning of this function. */
1856 while (parser->active && parser->active != active_initial)
1857 _parser_active_list_pop (parser);
1859 list->non_space_tail = list->tail;
1863 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1869 _glcpp_parser_expand_token_list (parser, list);
1871 _token_list_trim_trailing_space (list);
1873 _token_list_print (parser, list);
1877 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1878 const char *identifier)
1880 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
1881 * the GLSL ES spec (all versions) say:
1883 * "All macro names containing two consecutive underscores ( __ )
1884 * are reserved for future use as predefined macro names. All
1885 * macro names prefixed with "GL_" ("GL" followed by a single
1886 * underscore) are also reserved."
1888 * The intention is that names containing __ are reserved for internal
1889 * use by the implementation, and names prefixed with GL_ are reserved
1890 * for use by Khronos. Since every extension adds a name prefixed
1891 * with GL_ (i.e., the name of the extension), that should be an
1892 * error. Names simply containing __ are dangerous to use, but should
1895 * A future version of the GLSL specification will clarify this.
1897 if (strstr(identifier, "__")) {
1898 glcpp_warning(loc, parser,
1899 "Macro names containing \"__\" are reserved "
1900 "for use by the implementation.\n");
1902 if (strncmp(identifier, "GL_", 3) == 0) {
1903 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1908 _macro_equal (macro_t *a, macro_t *b)
1910 if (a->is_function != b->is_function)
1913 if (a->is_function) {
1914 if (! _string_list_equal (a->parameters, b->parameters))
1918 return _token_list_equal_ignoring_space (a->replacements,
1923 _define_object_macro (glcpp_parser_t *parser,
1925 const char *identifier,
1926 token_list_t *replacements)
1928 macro_t *macro, *previous;
1931 _check_for_reserved_macro_name(parser, loc, identifier);
1933 macro = ralloc (parser, macro_t);
1935 macro->is_function = 0;
1936 macro->parameters = NULL;
1937 macro->identifier = ralloc_strdup (macro, identifier);
1938 macro->replacements = replacements;
1939 ralloc_steal (macro, replacements);
1941 previous = hash_table_find (parser->defines, identifier);
1943 if (_macro_equal (macro, previous)) {
1944 ralloc_free (macro);
1947 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1951 hash_table_insert (parser->defines, macro, identifier);
1955 _define_function_macro (glcpp_parser_t *parser,
1957 const char *identifier,
1958 string_list_t *parameters,
1959 token_list_t *replacements)
1961 macro_t *macro, *previous;
1963 _check_for_reserved_macro_name(parser, loc, identifier);
1965 macro = ralloc (parser, macro_t);
1966 ralloc_steal (macro, parameters);
1967 ralloc_steal (macro, replacements);
1969 macro->is_function = 1;
1970 macro->parameters = parameters;
1971 macro->identifier = ralloc_strdup (macro, identifier);
1972 macro->replacements = replacements;
1973 previous = hash_table_find (parser->defines, identifier);
1975 if (_macro_equal (macro, previous)) {
1976 ralloc_free (macro);
1979 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1983 hash_table_insert (parser->defines, macro, identifier);
1987 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1992 if (parser->lex_from_list == NULL) {
1993 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1995 /* XXX: This ugly block of code exists for the sole
1996 * purpose of converting a NEWLINE token into a SPACE
1997 * token, but only in the case where we have seen a
1998 * function-like macro name, but have not yet seen its
1999 * closing parenthesis.
2001 * There's perhaps a more compact way to do this with
2002 * mid-rule actions in the grammar.
2004 * I'm definitely not pleased with the complexity of
2007 if (parser->newline_as_space)
2010 parser->paren_count++;
2011 } else if (ret == ')') {
2012 parser->paren_count--;
2013 if (parser->paren_count == 0)
2014 parser->newline_as_space = 0;
2015 } else if (ret == NEWLINE) {
2017 } else if (ret != SPACE) {
2018 if (parser->paren_count == 0)
2019 parser->newline_as_space = 0;
2022 else if (parser->in_control_line)
2025 parser->in_control_line = 0;
2027 else if (ret == HASH_DEFINE ||
2028 ret == HASH_UNDEF || ret == HASH_IF ||
2029 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
2030 ret == HASH_ELIF || ret == HASH_ELSE ||
2031 ret == HASH_ENDIF || ret == HASH)
2033 parser->in_control_line = 1;
2035 else if (ret == IDENTIFIER)
2038 macro = hash_table_find (parser->defines,
2040 if (macro && macro->is_function) {
2041 parser->newline_as_space = 1;
2042 parser->paren_count = 0;
2049 node = parser->lex_from_node;
2052 ralloc_free (parser->lex_from_list);
2053 parser->lex_from_list = NULL;
2057 *yylval = node->token->value;
2058 ret = node->token->type;
2060 parser->lex_from_node = node->next;
2066 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
2070 assert (parser->lex_from_list == NULL);
2072 /* Copy list, eliminating any space tokens. */
2073 parser->lex_from_list = _token_list_create (parser);
2075 for (node = list->head; node; node = node->next) {
2076 if (node->token->type == SPACE)
2078 _token_list_append (parser->lex_from_list, node->token);
2083 parser->lex_from_node = parser->lex_from_list->head;
2085 /* It's possible the list consisted of nothing but whitespace. */
2086 if (parser->lex_from_node == NULL) {
2087 ralloc_free (parser->lex_from_list);
2088 parser->lex_from_list = NULL;
2093 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
2096 skip_type_t current = SKIP_NO_SKIP;
2099 if (parser->skip_stack)
2100 current = parser->skip_stack->type;
2102 node = ralloc (parser, skip_node_t);
2105 if (current == SKIP_NO_SKIP) {
2107 node->type = SKIP_NO_SKIP;
2109 node->type = SKIP_TO_ELSE;
2111 node->type = SKIP_TO_ENDIF;
2114 node->has_else = false;
2115 node->next = parser->skip_stack;
2116 parser->skip_stack = node;
2120 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
2121 const char *type, int condition)
2123 if (parser->skip_stack == NULL) {
2124 glcpp_error (loc, parser, "#%s without #if\n", type);
2128 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2130 parser->skip_stack->type = SKIP_NO_SKIP;
2132 parser->skip_stack->type = SKIP_TO_ENDIF;
2137 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
2141 if (parser->skip_stack == NULL) {
2142 glcpp_error (loc, parser, "#endif without #if\n");
2146 node = parser->skip_stack;
2147 parser->skip_stack = node->next;
2152 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2153 const char *es_identifier,
2154 bool explicitly_set)
2156 const struct gl_extensions *extensions = parser->extensions;
2158 if (parser->version_resolved)
2161 parser->version_resolved = true;
2163 add_builtin_define (parser, "__VERSION__", version);
2165 parser->is_gles = (version == 100) ||
2167 (strcmp(es_identifier, "es") == 0));
2169 /* Add pre-defined macros. */
2170 if (parser->is_gles) {
2171 add_builtin_define(parser, "GL_ES", 1);
2172 add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
2174 if (extensions != NULL) {
2175 if (extensions->OES_EGL_image_external)
2176 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2179 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2180 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2181 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2182 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2185 if (extensions != NULL) {
2186 if (extensions->EXT_texture_array)
2187 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2189 if (extensions->ARB_arrays_of_arrays)
2190 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2192 if (extensions->ARB_fragment_coord_conventions)
2193 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2196 if (extensions->ARB_fragment_layer_viewport)
2197 add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
2199 if (extensions->ARB_explicit_attrib_location)
2200 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2202 if (extensions->ARB_explicit_uniform_location)
2203 add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
2205 if (extensions->ARB_shader_texture_lod)
2206 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2208 if (extensions->ARB_draw_instanced)
2209 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2211 if (extensions->ARB_conservative_depth) {
2212 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2213 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2216 if (extensions->ARB_shader_bit_encoding)
2217 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2219 if (extensions->ARB_uniform_buffer_object)
2220 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2222 if (extensions->ARB_texture_cube_map_array)
2223 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2225 if (extensions->ARB_shading_language_packing)
2226 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2228 if (extensions->ARB_texture_multisample)
2229 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2231 if (extensions->ARB_texture_query_levels)
2232 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2234 if (extensions->ARB_texture_query_lod)
2235 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2237 if (extensions->ARB_gpu_shader5)
2238 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2240 if (extensions->AMD_vertex_shader_layer)
2241 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2243 if (extensions->AMD_vertex_shader_viewport_index)
2244 add_builtin_define(parser, "GL_AMD_vertex_shader_viewport_index", 1);
2246 if (extensions->ARB_shading_language_420pack)
2247 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2249 if (extensions->ARB_sample_shading)
2250 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2252 if (extensions->ARB_texture_gather)
2253 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2255 if (extensions->ARB_shader_atomic_counters)
2256 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2258 if (extensions->ARB_viewport_array)
2259 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2261 if (extensions->ARB_compute_shader)
2262 add_builtin_define(parser, "GL_ARB_compute_shader", 1);
2264 if (extensions->ARB_shader_image_load_store)
2265 add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
2269 if (extensions != NULL) {
2270 if (extensions->EXT_shader_integer_mix)
2271 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2275 add_builtin_define(parser, "GL_core_profile", 1);
2277 /* Currently, all ES2/ES3 implementations support highp in the
2278 * fragment shader, so we always define this macro in ES2/ES3.
2279 * If we ever get a driver that doesn't support highp, we'll
2280 * need to add a flag to the gl_context and check that here.
2282 if (version >= 130 || parser->is_gles)
2283 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2285 if (explicitly_set) {
2286 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
2287 "#version %" PRIiMAX "%s%s", version,
2288 es_identifier ? " " : "",
2289 es_identifier ? es_identifier : "");
2293 /* GLSL version if no version is explicitly specified. */
2294 #define IMPLICIT_GLSL_VERSION 110
2296 /* GLSL ES version if no version is explicitly specified. */
2297 #define IMPLICIT_GLSL_ES_VERSION 100
2300 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2302 int language_version = parser->api == API_OPENGLES2 ?
2303 IMPLICIT_GLSL_ES_VERSION :
2304 IMPLICIT_GLSL_VERSION;
2306 _glcpp_parser_handle_version_declaration(parser, language_version,