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> expression INTEGER operator SPACE integer_constant
170 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
171 %type <string_list> identifier_list
172 %type <token> preprocessing_token conditional_token
173 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
179 %left EQUAL NOT_EQUAL
180 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
181 %left LEFT_SHIFT RIGHT_SHIFT
195 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
198 glcpp_parser_resolve_implicit_version(parser);
199 } pp_tokens NEWLINE {
201 if (parser->skip_stack == NULL ||
202 parser->skip_stack->type == SKIP_NO_SKIP)
204 _glcpp_parser_expand_and_lex_from (parser,
209 _glcpp_parser_print_expanded_token_list (parser, $1);
210 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
218 IF_EXPANDED expression NEWLINE {
219 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
221 | ELIF_EXPANDED expression NEWLINE {
222 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
224 | LINE_EXPANDED integer_constant NEWLINE {
225 parser->has_new_line_number = 1;
226 parser->new_line_number = $2;
227 ralloc_asprintf_rewrite_tail (&parser->output,
228 &parser->output_length,
229 "#line %" PRIiMAX "\n",
232 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
233 parser->has_new_line_number = 1;
234 parser->new_line_number = $2;
235 parser->has_new_source_number = 1;
236 parser->new_source_number = $3;
237 ralloc_asprintf_rewrite_tail (&parser->output,
238 &parser->output_length,
239 "#line %" PRIiMAX " %" PRIiMAX "\n",
245 OBJ_IDENTIFIER replacement_list NEWLINE {
246 _define_object_macro (parser, & @1, $1, $2);
248 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
249 _define_function_macro (parser, & @1, $1, NULL, $4);
251 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
252 _define_function_macro (parser, & @1, $1, $3, $5);
258 glcpp_parser_resolve_implicit_version(parser);
261 glcpp_parser_resolve_implicit_version(parser);
262 } IDENTIFIER NEWLINE {
263 if (strcmp("__LINE__", $3) == 0
264 || strcmp("__FILE__", $3) == 0
265 || strcmp("__VERSION__", $3) == 0)
266 glcpp_error(& @1, parser, "Built-in (pre-defined)"
267 " macro names can not be undefined.");
269 macro_t *macro = hash_table_find (parser->defines, $3);
271 hash_table_remove (parser->defines, $3);
277 glcpp_parser_resolve_implicit_version(parser);
278 } conditional_tokens NEWLINE {
279 /* Be careful to only evaluate the 'if' expression if
280 * we are not skipping. When we are skipping, we
281 * simply push a new 0-valued 'if' onto the skip
284 * This avoids generating diagnostics for invalid
285 * expressions that are being skipped. */
286 if (parser->skip_stack == NULL ||
287 parser->skip_stack->type == SKIP_NO_SKIP)
289 _glcpp_parser_expand_and_lex_from (parser,
294 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
295 parser->skip_stack->type = SKIP_TO_ENDIF;
299 /* #if without an expression is only an error if we
300 * are not skipping */
301 if (parser->skip_stack == NULL ||
302 parser->skip_stack->type == SKIP_NO_SKIP)
304 glcpp_error(& @1, parser, "#if with no expression");
306 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
309 glcpp_parser_resolve_implicit_version(parser);
310 } IDENTIFIER junk NEWLINE {
311 macro_t *macro = hash_table_find (parser->defines, $3);
313 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
316 glcpp_parser_resolve_implicit_version(parser);
317 } IDENTIFIER junk NEWLINE {
318 macro_t *macro = hash_table_find (parser->defines, $3);
320 _glcpp_parser_skip_stack_push_if (parser, & @2, macro == NULL);
322 | HASH_ELIF conditional_tokens NEWLINE {
323 /* Be careful to only evaluate the 'elif' expression
324 * if we are not skipping. When we are skipping, we
325 * simply change to a 0-valued 'elif' on the skip
328 * This avoids generating diagnostics for invalid
329 * expressions that are being skipped. */
330 if (parser->skip_stack &&
331 parser->skip_stack->type == SKIP_TO_ELSE)
333 _glcpp_parser_expand_and_lex_from (parser,
336 else if (parser->skip_stack &&
337 parser->skip_stack->has_else)
339 glcpp_error(& @1, parser, "#elif after #else");
343 _glcpp_parser_skip_stack_change_if (parser, & @1,
347 | HASH_ELIF NEWLINE {
348 /* #elif without an expression is an error unless we
350 if (parser->skip_stack &&
351 parser->skip_stack->type == SKIP_TO_ELSE)
353 glcpp_error(& @1, parser, "#elif with no expression");
355 else if (parser->skip_stack &&
356 parser->skip_stack->has_else)
358 glcpp_error(& @1, parser, "#elif after #else");
362 _glcpp_parser_skip_stack_change_if (parser, & @1,
364 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
368 if (parser->skip_stack &&
369 parser->skip_stack->has_else)
371 glcpp_error(& @1, parser, "multiple #else");
375 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
376 if (parser->skip_stack)
377 parser->skip_stack->has_else = true;
381 _glcpp_parser_skip_stack_pop (parser, & @1);
383 | HASH_VERSION integer_constant NEWLINE {
384 if (parser->version_resolved) {
385 glcpp_error(& @1, parser, "#version must appear on the first line");
387 _glcpp_parser_handle_version_declaration(parser, $2, NULL, true);
389 | HASH_VERSION integer_constant IDENTIFIER 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, $3, true);
396 glcpp_parser_resolve_implicit_version(parser);
402 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
403 $$ = strtoll ($1 + 2, NULL, 16);
404 } else if ($1[0] == '0') {
405 $$ = strtoll ($1, NULL, 8);
407 $$ = strtoll ($1, NULL, 10);
418 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $1);
421 | expression OR expression {
424 | expression AND expression {
427 | expression '|' expression {
430 | expression '^' expression {
433 | expression '&' expression {
436 | expression NOT_EQUAL expression {
439 | expression EQUAL expression {
442 | expression GREATER_OR_EQUAL expression {
445 | expression LESS_OR_EQUAL expression {
448 | expression '>' expression {
451 | expression '<' expression {
454 | expression RIGHT_SHIFT expression {
457 | expression LEFT_SHIFT expression {
460 | expression '-' expression {
463 | expression '+' expression {
466 | expression '%' expression {
468 yyerror (& @1, parser,
469 "zero modulus in preprocessor directive");
474 | expression '/' expression {
476 yyerror (& @1, parser,
477 "division by 0 in preprocessor directive");
482 | expression '*' expression {
485 | '!' expression %prec UNARY {
488 | '~' expression %prec UNARY {
491 | '-' expression %prec UNARY {
494 | '+' expression %prec UNARY {
497 | '(' expression ')' {
504 $$ = _string_list_create (parser);
505 _string_list_append_item ($$, $1);
506 ralloc_steal ($$, $1);
508 | identifier_list ',' IDENTIFIER {
510 _string_list_append_item ($$, $3);
511 ralloc_steal ($$, $3);
516 NEWLINE { $$ = NULL; }
522 yyerror (& @1, parser, "Invalid tokens after #");
527 /* empty */ { $$ = NULL; }
534 glcpp_warning(&@1, parser, "extra tokens at end of directive");
539 /* Handle "defined" operator */
541 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
542 $$ = _token_create_ival (parser, INTEGER, v);
544 | DEFINED '(' IDENTIFIER ')' {
545 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
546 $$ = _token_create_ival (parser, INTEGER, v);
548 | preprocessing_token
552 /* Exactly the same as pp_tokens, but using conditional_token */
554 $$ = _token_list_create (parser);
555 _token_list_append ($$, $1);
557 | conditional_tokens conditional_token {
559 _token_list_append ($$, $2);
564 preprocessing_token {
565 parser->space_tokens = 1;
566 $$ = _token_list_create (parser);
567 _token_list_append ($$, $1);
569 | pp_tokens preprocessing_token {
571 _token_list_append ($$, $2);
577 $$ = _token_create_str (parser, IDENTIFIER, $1);
578 $$->location = yylloc;
581 $$ = _token_create_str (parser, INTEGER_STRING, $1);
582 $$->location = yylloc;
585 $$ = _token_create_ival (parser, $1, $1);
586 $$->location = yylloc;
589 $$ = _token_create_str (parser, OTHER, $1);
590 $$->location = yylloc;
593 $$ = _token_create_ival (parser, SPACE, SPACE);
594 $$->location = yylloc;
614 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
615 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
618 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
619 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
620 | EQUAL { $$ = EQUAL; }
621 | NOT_EQUAL { $$ = NOT_EQUAL; }
629 | PASTE { $$ = PASTE; }
635 _string_list_create (void *ctx)
639 list = ralloc (ctx, string_list_t);
647 _string_list_append_item (string_list_t *list, const char *str)
651 node = ralloc (list, string_node_t);
652 node->str = ralloc_strdup (node, str);
656 if (list->head == NULL) {
659 list->tail->next = node;
666 _string_list_contains (string_list_t *list, const char *member, int *index)
674 for (i = 0, node = list->head; node; i++, node = node->next) {
675 if (strcmp (node->str, member) == 0) {
686 _string_list_length (string_list_t *list)
694 for (node = list->head; node; node = node->next)
701 _string_list_equal (string_list_t *a, string_list_t *b)
703 string_node_t *node_a, *node_b;
705 if (a == NULL && b == NULL)
708 if (a == NULL || b == NULL)
711 for (node_a = a->head, node_b = b->head;
713 node_a = node_a->next, node_b = node_b->next)
715 if (strcmp (node_a->str, node_b->str))
719 /* Catch the case of lists being different lengths, (which
720 * would cause the loop above to terminate after the shorter
722 return node_a == node_b;
726 _argument_list_create (void *ctx)
728 argument_list_t *list;
730 list = ralloc (ctx, argument_list_t);
738 _argument_list_append (argument_list_t *list, token_list_t *argument)
740 argument_node_t *node;
742 node = ralloc (list, argument_node_t);
743 node->argument = argument;
747 if (list->head == NULL) {
750 list->tail->next = node;
757 _argument_list_length (argument_list_t *list)
760 argument_node_t *node;
765 for (node = list->head; node; node = node->next)
772 _argument_list_member_at (argument_list_t *list, int index)
774 argument_node_t *node;
781 for (i = 0; i < index; i++) {
788 return node->argument;
793 /* Note: This function ralloc_steal()s the str pointer. */
795 _token_create_str (void *ctx, int type, char *str)
799 token = ralloc (ctx, token_t);
801 token->value.str = str;
803 ralloc_steal (token, str);
809 _token_create_ival (void *ctx, int type, int ival)
813 token = ralloc (ctx, token_t);
815 token->value.ival = ival;
821 _token_list_create (void *ctx)
825 list = ralloc (ctx, token_list_t);
828 list->non_space_tail = NULL;
834 _token_list_append (token_list_t *list, token_t *token)
838 node = ralloc (list, token_node_t);
842 if (list->head == NULL) {
845 list->tail->next = node;
849 if (token->type != SPACE)
850 list->non_space_tail = node;
854 _token_list_append_list (token_list_t *list, token_list_t *tail)
856 if (tail == NULL || tail->head == NULL)
859 if (list->head == NULL) {
860 list->head = tail->head;
862 list->tail->next = tail->head;
865 list->tail = tail->tail;
866 list->non_space_tail = tail->non_space_tail;
869 static token_list_t *
870 _token_list_copy (void *ctx, token_list_t *other)
878 copy = _token_list_create (ctx);
879 for (node = other->head; node; node = node->next) {
880 token_t *new_token = ralloc (copy, token_t);
881 *new_token = *node->token;
882 _token_list_append (copy, new_token);
889 _token_list_trim_trailing_space (token_list_t *list)
891 token_node_t *tail, *next;
893 if (list->non_space_tail) {
894 tail = list->non_space_tail->next;
895 list->non_space_tail->next = NULL;
896 list->tail = list->non_space_tail;
907 _token_list_is_empty_ignoring_space (token_list_t *l)
915 while (n != NULL && n->token->type == SPACE)
922 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
924 token_node_t *node_a, *node_b;
926 if (a == NULL || b == NULL) {
927 int a_empty = _token_list_is_empty_ignoring_space(a);
928 int b_empty = _token_list_is_empty_ignoring_space(b);
929 return a_empty == b_empty;
937 if (node_a == NULL && node_b == NULL)
940 if (node_a == NULL || node_b == NULL)
942 /* Make sure whitespace appears in the same places in both.
943 * It need not be exactly the same amount of whitespace,
946 if (node_a->token->type == SPACE
947 && node_b->token->type == SPACE) {
948 while (node_a->token->type == SPACE)
949 node_a = node_a->next;
950 while (node_b->token->type == SPACE)
951 node_b = node_b->next;
955 if (node_a->token->type != node_b->token->type)
958 switch (node_a->token->type) {
960 if (node_a->token->value.ival !=
961 node_b->token->value.ival)
969 if (strcmp (node_a->token->value.str,
970 node_b->token->value.str))
977 node_a = node_a->next;
978 node_b = node_b->next;
985 _token_print (char **out, size_t *len, token_t *token)
987 if (token->type < 256) {
988 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
992 switch (token->type) {
994 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
999 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1002 ralloc_asprintf_rewrite_tail (out, len, " ");
1005 ralloc_asprintf_rewrite_tail (out, len, "<<");
1008 ralloc_asprintf_rewrite_tail (out, len, ">>");
1011 ralloc_asprintf_rewrite_tail (out, len, "<=");
1013 case GREATER_OR_EQUAL:
1014 ralloc_asprintf_rewrite_tail (out, len, ">=");
1017 ralloc_asprintf_rewrite_tail (out, len, "==");
1020 ralloc_asprintf_rewrite_tail (out, len, "!=");
1023 ralloc_asprintf_rewrite_tail (out, len, "&&");
1026 ralloc_asprintf_rewrite_tail (out, len, "||");
1029 ralloc_asprintf_rewrite_tail (out, len, "##");
1032 ralloc_asprintf_rewrite_tail (out, len, ",");
1035 /* Nothing to print. */
1038 assert(!"Error: Don't know how to print token.");
1043 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1044 * 'token' and 'other'. Note that this function may return 'token' or
1045 * 'other' directly rather than allocating anything new.
1047 * Caution: Only very cursory error-checking is performed to see if
1048 * the final result is a valid single token. */
1050 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1052 token_t *combined = NULL;
1054 /* Pasting a placeholder onto anything makes no change. */
1055 if (other->type == PLACEHOLDER)
1058 /* When 'token' is a placeholder, just return 'other'. */
1059 if (token->type == PLACEHOLDER)
1062 /* A very few single-character punctuators can be combined
1063 * with another to form a multi-character punctuator. */
1064 switch (token->type) {
1066 if (other->type == '<')
1067 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1068 else if (other->type == '=')
1069 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1072 if (other->type == '>')
1073 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1074 else if (other->type == '=')
1075 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1078 if (other->type == '=')
1079 combined = _token_create_ival (token, EQUAL, EQUAL);
1082 if (other->type == '=')
1083 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1086 if (other->type == '&')
1087 combined = _token_create_ival (token, AND, AND);
1090 if (other->type == '|')
1091 combined = _token_create_ival (token, OR, OR);
1095 if (combined != NULL) {
1096 /* Inherit the location from the first token */
1097 combined->location = token->location;
1101 /* Two string-valued (or integer) tokens can usually just be
1102 * mashed together. (We also handle a string followed by an
1103 * integer here as well.)
1105 * There are some exceptions here. Notably, if the first token
1106 * is an integer (or a string representing an integer), then
1107 * the second token must also be an integer or must be a
1108 * string representing an integer that begins with a digit.
1110 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1111 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1116 /* Check that pasting onto an integer doesn't create a
1117 * non-integer, (that is, only digits can be
1119 if (token->type == INTEGER_STRING || token->type == INTEGER)
1121 switch (other->type) {
1122 case INTEGER_STRING:
1123 if (other->value.str[0] < '0' ||
1124 other->value.str[0] > '9')
1128 if (other->value.ival < 0)
1136 if (token->type == INTEGER)
1137 str = ralloc_asprintf (token, "%" PRIiMAX,
1140 str = ralloc_strdup (token, token->value.str);
1143 if (other->type == INTEGER)
1144 ralloc_asprintf_append (&str, "%" PRIiMAX,
1147 ralloc_strcat (&str, other->value.str);
1149 /* New token is same type as original token, unless we
1150 * started with an integer, in which case we will be
1151 * creating an integer-string. */
1152 combined_type = token->type;
1153 if (combined_type == INTEGER)
1154 combined_type = INTEGER_STRING;
1156 combined = _token_create_str (token, combined_type, str);
1157 combined->location = token->location;
1162 glcpp_error (&token->location, parser, "");
1163 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1164 _token_print (&parser->info_log, &parser->info_log_length, token);
1165 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1166 _token_print (&parser->info_log, &parser->info_log_length, other);
1167 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1173 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1180 for (node = list->head; node; node = node->next)
1181 _token_print (&parser->output, &parser->output_length, node->token);
1185 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1187 glcpp_error(locp, parser, "%s", error);
1190 static void add_builtin_define(glcpp_parser_t *parser,
1191 const char *name, int value)
1196 tok = _token_create_ival (parser, INTEGER, value);
1198 list = _token_list_create(parser);
1199 _token_list_append(list, tok);
1200 _define_object_macro(parser, NULL, name, list);
1204 glcpp_parser_create (const struct gl_extensions *extensions, gl_api api)
1206 glcpp_parser_t *parser;
1208 parser = ralloc (NULL, glcpp_parser_t);
1210 glcpp_lex_init_extra (parser, &parser->scanner);
1211 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1212 hash_table_string_compare);
1213 parser->active = NULL;
1214 parser->lexing_if = 0;
1215 parser->space_tokens = 1;
1216 parser->newline_as_space = 0;
1217 parser->in_control_line = 0;
1218 parser->paren_count = 0;
1219 parser->commented_newlines = 0;
1221 parser->skip_stack = NULL;
1223 parser->lex_from_list = NULL;
1224 parser->lex_from_node = NULL;
1226 parser->output = ralloc_strdup(parser, "");
1227 parser->output_length = 0;
1228 parser->info_log = ralloc_strdup(parser, "");
1229 parser->info_log_length = 0;
1232 parser->extensions = extensions;
1234 parser->version_resolved = false;
1236 parser->has_new_line_number = 0;
1237 parser->new_line_number = 1;
1238 parser->has_new_source_number = 0;
1239 parser->new_source_number = 0;
1245 glcpp_parser_destroy (glcpp_parser_t *parser)
1247 glcpp_lex_destroy (parser->scanner);
1248 hash_table_dtor (parser->defines);
1249 ralloc_free (parser);
1252 typedef enum function_status
1254 FUNCTION_STATUS_SUCCESS,
1255 FUNCTION_NOT_A_FUNCTION,
1256 FUNCTION_UNBALANCED_PARENTHESES
1257 } function_status_t;
1259 /* Find a set of function-like macro arguments by looking for a
1260 * balanced set of parentheses.
1262 * When called, 'node' should be the opening-parenthesis token, (or
1263 * perhaps preceeding SPACE tokens). Upon successful return *last will
1264 * be the last consumed node, (corresponding to the closing right
1269 * FUNCTION_STATUS_SUCCESS:
1271 * Successfully parsed a set of function arguments.
1273 * FUNCTION_NOT_A_FUNCTION:
1275 * Macro name not followed by a '('. This is not an error, but
1276 * simply that the macro name should be treated as a non-macro.
1278 * FUNCTION_UNBALANCED_PARENTHESES
1280 * Macro name is not followed by a balanced set of parentheses.
1282 static function_status_t
1283 _arguments_parse (argument_list_t *arguments,
1285 token_node_t **last)
1287 token_list_t *argument;
1292 /* Ignore whitespace before first parenthesis. */
1293 while (node && node->token->type == SPACE)
1296 if (node == NULL || node->token->type != '(')
1297 return FUNCTION_NOT_A_FUNCTION;
1301 argument = _token_list_create (arguments);
1302 _argument_list_append (arguments, argument);
1304 for (paren_count = 1; node; node = node->next) {
1305 if (node->token->type == '(')
1309 else if (node->token->type == ')')
1312 if (paren_count == 0)
1316 if (node->token->type == ',' &&
1319 _token_list_trim_trailing_space (argument);
1320 argument = _token_list_create (arguments);
1321 _argument_list_append (arguments, argument);
1324 if (argument->head == NULL) {
1325 /* Don't treat initial whitespace as
1326 * part of the arguement. */
1327 if (node->token->type == SPACE)
1330 _token_list_append (argument, node->token);
1335 return FUNCTION_UNBALANCED_PARENTHESES;
1339 return FUNCTION_STATUS_SUCCESS;
1342 static token_list_t *
1343 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1348 list = _token_list_create (ctx);
1349 node = _token_create_ival (list, type, ival);
1350 _token_list_append (list, node);
1355 static token_list_t *
1356 _token_list_create_with_one_space (void *ctx)
1358 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1361 static token_list_t *
1362 _token_list_create_with_one_integer (void *ctx, int ival)
1364 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1367 /* Perform macro expansion on 'list', placing the resulting tokens
1368 * into a new list which is initialized with a first token of type
1369 * 'head_token_type'. Then begin lexing from the resulting list,
1370 * (return to the current lexing source when this list is exhausted).
1373 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1374 int head_token_type,
1377 token_list_t *expanded;
1380 expanded = _token_list_create (parser);
1381 token = _token_create_ival (parser, head_token_type, head_token_type);
1382 _token_list_append (expanded, token);
1383 _glcpp_parser_expand_token_list (parser, list);
1384 _token_list_append_list (expanded, list);
1385 glcpp_parser_lex_from (parser, expanded);
1389 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1396 token_node_t *next_non_space;
1398 /* Look ahead for a PASTE token, skipping space. */
1399 next_non_space = node->next;
1400 while (next_non_space && next_non_space->token->type == SPACE)
1401 next_non_space = next_non_space->next;
1403 if (next_non_space == NULL)
1406 if (next_non_space->token->type != PASTE) {
1407 node = next_non_space;
1411 /* Now find the next non-space token after the PASTE. */
1412 next_non_space = next_non_space->next;
1413 while (next_non_space && next_non_space->token->type == SPACE)
1414 next_non_space = next_non_space->next;
1416 if (next_non_space == NULL) {
1417 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1421 node->token = _token_paste (parser, node->token, next_non_space->token);
1422 node->next = next_non_space->next;
1423 if (next_non_space == list->tail)
1427 list->non_space_tail = list->tail;
1430 /* This is a helper function that's essentially part of the
1431 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1432 * except for by that function.
1434 * Returns NULL if node is a simple token with no expansion, (that is,
1435 * although 'node' corresponds to an identifier defined as a
1436 * function-like macro, it is not followed with a parenthesized
1439 * Compute the complete expansion of node (which is a function-like
1440 * macro) and subsequent nodes which are arguments.
1442 * Returns the token list that results from the expansion and sets
1443 * *last to the last node in the list that was consumed by the
1444 * expansion. Specifically, *last will be set as follows: as the
1445 * token of the closing right parenthesis.
1447 static token_list_t *
1448 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1450 token_node_t **last)
1454 const char *identifier;
1455 argument_list_t *arguments;
1456 function_status_t status;
1457 token_list_t *substituted;
1458 int parameter_index;
1460 identifier = node->token->value.str;
1462 macro = hash_table_find (parser->defines, identifier);
1464 assert (macro->is_function);
1466 arguments = _argument_list_create (parser);
1467 status = _arguments_parse (arguments, node, last);
1470 case FUNCTION_STATUS_SUCCESS:
1472 case FUNCTION_NOT_A_FUNCTION:
1474 case FUNCTION_UNBALANCED_PARENTHESES:
1475 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1479 /* Replace a macro defined as empty with a SPACE token. */
1480 if (macro->replacements == NULL) {
1481 ralloc_free (arguments);
1482 return _token_list_create_with_one_space (parser);
1485 if (! ((_argument_list_length (arguments) ==
1486 _string_list_length (macro->parameters)) ||
1487 (_string_list_length (macro->parameters) == 0 &&
1488 _argument_list_length (arguments) == 1 &&
1489 arguments->head->argument->head == NULL)))
1491 glcpp_error (&node->token->location, parser,
1492 "Error: macro %s invoked with %d arguments (expected %d)\n",
1494 _argument_list_length (arguments),
1495 _string_list_length (macro->parameters));
1499 /* Perform argument substitution on the replacement list. */
1500 substituted = _token_list_create (arguments);
1502 for (node = macro->replacements->head; node; node = node->next)
1504 if (node->token->type == IDENTIFIER &&
1505 _string_list_contains (macro->parameters,
1506 node->token->value.str,
1509 token_list_t *argument;
1510 argument = _argument_list_member_at (arguments,
1512 /* Before substituting, we expand the argument
1513 * tokens, or append a placeholder token for
1514 * an empty argument. */
1515 if (argument->head) {
1516 token_list_t *expanded_argument;
1517 expanded_argument = _token_list_copy (parser,
1519 _glcpp_parser_expand_token_list (parser,
1521 _token_list_append_list (substituted,
1526 new_token = _token_create_ival (substituted,
1529 _token_list_append (substituted, new_token);
1532 _token_list_append (substituted, node->token);
1536 /* After argument substitution, and before further expansion
1537 * below, implement token pasting. */
1539 _token_list_trim_trailing_space (substituted);
1541 _glcpp_parser_apply_pastes (parser, substituted);
1546 /* Compute the complete expansion of node, (and subsequent nodes after
1547 * 'node' in the case that 'node' is a function-like macro and
1548 * subsequent nodes are arguments).
1550 * Returns NULL if node is a simple token with no expansion.
1552 * Otherwise, returns the token list that results from the expansion
1553 * and sets *last to the last node in the list that was consumed by
1554 * the expansion. Specifically, *last will be set as follows:
1556 * As 'node' in the case of object-like macro expansion.
1558 * As the token of the closing right parenthesis in the case of
1559 * function-like macro expansion.
1561 static token_list_t *
1562 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1564 token_node_t **last)
1566 token_t *token = node->token;
1567 const char *identifier;
1570 /* We only expand identifiers */
1571 if (token->type != IDENTIFIER) {
1572 /* We change any COMMA into a COMMA_FINAL to prevent
1573 * it being mistaken for an argument separator
1575 if (token->type == ',') {
1576 token->type = COMMA_FINAL;
1577 token->value.ival = COMMA_FINAL;
1584 identifier = token->value.str;
1586 /* Special handling for __LINE__ and __FILE__, (not through
1587 * the hash table). */
1588 if (strcmp(identifier, "__LINE__") == 0)
1589 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1591 if (strcmp(identifier, "__FILE__") == 0)
1592 return _token_list_create_with_one_integer (parser, node->token->location.source);
1594 /* Look up this identifier in the hash table. */
1595 macro = hash_table_find (parser->defines, identifier);
1597 /* Not a macro, so no expansion needed. */
1601 /* Finally, don't expand this macro if we're already actively
1602 * expanding it, (to avoid infinite recursion). */
1603 if (_parser_active_list_contains (parser, identifier)) {
1604 /* We change the token type here from IDENTIFIER to
1605 * OTHER to prevent any future expansion of this
1606 * unexpanded token. */
1608 token_list_t *expansion;
1611 str = ralloc_strdup (parser, token->value.str);
1612 final = _token_create_str (parser, OTHER, str);
1613 expansion = _token_list_create (parser);
1614 _token_list_append (expansion, final);
1618 if (! macro->is_function)
1620 token_list_t *replacement;
1622 /* Replace a macro defined as empty with a SPACE token. */
1623 if (macro->replacements == NULL)
1624 return _token_list_create_with_one_space (parser);
1626 replacement = _token_list_copy (parser, macro->replacements);
1627 _glcpp_parser_apply_pastes (parser, replacement);
1631 return _glcpp_parser_expand_function (parser, node, last);
1634 /* Push a new identifier onto the parser's active list.
1636 * Here, 'marker' is the token node that appears in the list after the
1637 * expansion of 'identifier'. That is, when the list iterator begins
1638 * examining 'marker', then it is time to pop this node from the
1642 _parser_active_list_push (glcpp_parser_t *parser,
1643 const char *identifier,
1644 token_node_t *marker)
1646 active_list_t *node;
1648 node = ralloc (parser->active, active_list_t);
1649 node->identifier = ralloc_strdup (node, identifier);
1650 node->marker = marker;
1651 node->next = parser->active;
1653 parser->active = node;
1657 _parser_active_list_pop (glcpp_parser_t *parser)
1659 active_list_t *node = parser->active;
1662 parser->active = NULL;
1666 node = parser->active->next;
1667 ralloc_free (parser->active);
1669 parser->active = node;
1673 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1675 active_list_t *node;
1677 if (parser->active == NULL)
1680 for (node = parser->active; node; node = node->next)
1681 if (strcmp (node->identifier, identifier) == 0)
1687 /* Walk over the token list replacing nodes with their expansion.
1688 * Whenever nodes are expanded the walking will walk over the new
1689 * nodes, continuing to expand as necessary. The results are placed in
1693 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1696 token_node_t *node_prev;
1697 token_node_t *node, *last = NULL;
1698 token_list_t *expansion;
1699 active_list_t *active_initial = parser->active;
1704 _token_list_trim_trailing_space (list);
1711 while (parser->active && parser->active->marker == node)
1712 _parser_active_list_pop (parser);
1714 expansion = _glcpp_parser_expand_node (parser, node, &last);
1718 for (n = node; n != last->next; n = n->next)
1719 while (parser->active &&
1720 parser->active->marker == n)
1722 _parser_active_list_pop (parser);
1725 _parser_active_list_push (parser,
1726 node->token->value.str,
1729 /* Splice expansion into list, supporting a
1730 * simple deletion if the expansion is
1732 if (expansion->head) {
1734 node_prev->next = expansion->head;
1736 list->head = expansion->head;
1737 expansion->tail->next = last->next;
1738 if (last == list->tail)
1739 list->tail = expansion->tail;
1742 node_prev->next = last->next;
1744 list->head = last->next;
1745 if (last == list->tail)
1751 node = node_prev ? node_prev->next : list->head;
1754 /* Remove any lingering effects of this invocation on the
1755 * active list. That is, pop until the list looks like it did
1756 * at the beginning of this function. */
1757 while (parser->active && parser->active != active_initial)
1758 _parser_active_list_pop (parser);
1760 list->non_space_tail = list->tail;
1764 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1770 _glcpp_parser_expand_token_list (parser, list);
1772 _token_list_trim_trailing_space (list);
1774 _token_list_print (parser, list);
1778 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1779 const char *identifier)
1781 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
1782 * the GLSL ES spec (all versions) say:
1784 * "All macro names containing two consecutive underscores ( __ )
1785 * are reserved for future use as predefined macro names. All
1786 * macro names prefixed with "GL_" ("GL" followed by a single
1787 * underscore) are also reserved."
1789 * The intention is that names containing __ are reserved for internal
1790 * use by the implementation, and names prefixed with GL_ are reserved
1791 * for use by Khronos. Since every extension adds a name prefixed
1792 * with GL_ (i.e., the name of the extension), that should be an
1793 * error. Names simply containing __ are dangerous to use, but should
1796 * A future version of the GLSL specification will clarify this.
1798 if (strstr(identifier, "__")) {
1799 glcpp_warning(loc, parser,
1800 "Macro names containing \"__\" are reserved "
1801 "for use by the implementation.\n");
1803 if (strncmp(identifier, "GL_", 3) == 0) {
1804 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1809 _macro_equal (macro_t *a, macro_t *b)
1811 if (a->is_function != b->is_function)
1814 if (a->is_function) {
1815 if (! _string_list_equal (a->parameters, b->parameters))
1819 return _token_list_equal_ignoring_space (a->replacements,
1824 _define_object_macro (glcpp_parser_t *parser,
1826 const char *identifier,
1827 token_list_t *replacements)
1829 macro_t *macro, *previous;
1832 _check_for_reserved_macro_name(parser, loc, identifier);
1834 macro = ralloc (parser, macro_t);
1836 macro->is_function = 0;
1837 macro->parameters = NULL;
1838 macro->identifier = ralloc_strdup (macro, identifier);
1839 macro->replacements = replacements;
1840 ralloc_steal (macro, replacements);
1842 previous = hash_table_find (parser->defines, identifier);
1844 if (_macro_equal (macro, previous)) {
1845 ralloc_free (macro);
1848 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1852 hash_table_insert (parser->defines, macro, identifier);
1856 _define_function_macro (glcpp_parser_t *parser,
1858 const char *identifier,
1859 string_list_t *parameters,
1860 token_list_t *replacements)
1862 macro_t *macro, *previous;
1864 _check_for_reserved_macro_name(parser, loc, identifier);
1866 macro = ralloc (parser, macro_t);
1867 ralloc_steal (macro, parameters);
1868 ralloc_steal (macro, replacements);
1870 macro->is_function = 1;
1871 macro->parameters = parameters;
1872 macro->identifier = ralloc_strdup (macro, identifier);
1873 macro->replacements = replacements;
1874 previous = hash_table_find (parser->defines, identifier);
1876 if (_macro_equal (macro, previous)) {
1877 ralloc_free (macro);
1880 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1884 hash_table_insert (parser->defines, macro, identifier);
1888 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1893 if (parser->lex_from_list == NULL) {
1894 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1896 /* XXX: This ugly block of code exists for the sole
1897 * purpose of converting a NEWLINE token into a SPACE
1898 * token, but only in the case where we have seen a
1899 * function-like macro name, but have not yet seen its
1900 * closing parenthesis.
1902 * There's perhaps a more compact way to do this with
1903 * mid-rule actions in the grammar.
1905 * I'm definitely not pleased with the complexity of
1908 if (parser->newline_as_space)
1911 parser->paren_count++;
1912 } else if (ret == ')') {
1913 parser->paren_count--;
1914 if (parser->paren_count == 0)
1915 parser->newline_as_space = 0;
1916 } else if (ret == NEWLINE) {
1918 } else if (ret != SPACE) {
1919 if (parser->paren_count == 0)
1920 parser->newline_as_space = 0;
1923 else if (parser->in_control_line)
1926 parser->in_control_line = 0;
1928 else if (ret == HASH_DEFINE ||
1929 ret == HASH_UNDEF || ret == HASH_IF ||
1930 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1931 ret == HASH_ELIF || ret == HASH_ELSE ||
1932 ret == HASH_ENDIF || ret == HASH)
1934 parser->in_control_line = 1;
1936 else if (ret == IDENTIFIER)
1939 macro = hash_table_find (parser->defines,
1941 if (macro && macro->is_function) {
1942 parser->newline_as_space = 1;
1943 parser->paren_count = 0;
1950 node = parser->lex_from_node;
1953 ralloc_free (parser->lex_from_list);
1954 parser->lex_from_list = NULL;
1958 *yylval = node->token->value;
1959 ret = node->token->type;
1961 parser->lex_from_node = node->next;
1967 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1971 assert (parser->lex_from_list == NULL);
1973 /* Copy list, eliminating any space tokens. */
1974 parser->lex_from_list = _token_list_create (parser);
1976 for (node = list->head; node; node = node->next) {
1977 if (node->token->type == SPACE)
1979 _token_list_append (parser->lex_from_list, node->token);
1984 parser->lex_from_node = parser->lex_from_list->head;
1986 /* It's possible the list consisted of nothing but whitespace. */
1987 if (parser->lex_from_node == NULL) {
1988 ralloc_free (parser->lex_from_list);
1989 parser->lex_from_list = NULL;
1994 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1997 skip_type_t current = SKIP_NO_SKIP;
2000 if (parser->skip_stack)
2001 current = parser->skip_stack->type;
2003 node = ralloc (parser, skip_node_t);
2006 if (current == SKIP_NO_SKIP) {
2008 node->type = SKIP_NO_SKIP;
2010 node->type = SKIP_TO_ELSE;
2012 node->type = SKIP_TO_ENDIF;
2015 node->has_else = false;
2016 node->next = parser->skip_stack;
2017 parser->skip_stack = node;
2021 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
2022 const char *type, int condition)
2024 if (parser->skip_stack == NULL) {
2025 glcpp_error (loc, parser, "%s without #if\n", type);
2029 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2031 parser->skip_stack->type = SKIP_NO_SKIP;
2033 parser->skip_stack->type = SKIP_TO_ENDIF;
2038 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
2042 if (parser->skip_stack == NULL) {
2043 glcpp_error (loc, parser, "#endif without #if\n");
2047 node = parser->skip_stack;
2048 parser->skip_stack = node->next;
2053 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2054 const char *es_identifier,
2055 bool explicitly_set)
2057 const struct gl_extensions *extensions = parser->extensions;
2059 if (parser->version_resolved)
2062 parser->version_resolved = true;
2064 add_builtin_define (parser, "__VERSION__", version);
2066 parser->is_gles = (version == 100) ||
2068 (strcmp(es_identifier, "es") == 0));
2070 /* Add pre-defined macros. */
2071 if (parser->is_gles) {
2072 add_builtin_define(parser, "GL_ES", 1);
2073 add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
2075 if (extensions != NULL) {
2076 if (extensions->OES_EGL_image_external)
2077 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2080 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2081 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2082 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2083 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2086 if (extensions != NULL) {
2087 if (extensions->EXT_texture_array)
2088 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2090 if (extensions->ARB_arrays_of_arrays)
2091 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2093 if (extensions->ARB_fragment_coord_conventions)
2094 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2097 if (extensions->ARB_fragment_layer_viewport)
2098 add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
2100 if (extensions->ARB_explicit_attrib_location)
2101 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2103 if (extensions->ARB_explicit_uniform_location)
2104 add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
2106 if (extensions->ARB_shader_texture_lod)
2107 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2109 if (extensions->ARB_draw_instanced)
2110 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2112 if (extensions->ARB_conservative_depth) {
2113 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2114 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2117 if (extensions->ARB_shader_bit_encoding)
2118 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2120 if (extensions->ARB_uniform_buffer_object)
2121 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2123 if (extensions->ARB_texture_cube_map_array)
2124 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2126 if (extensions->ARB_shading_language_packing)
2127 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2129 if (extensions->ARB_texture_multisample)
2130 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2132 if (extensions->ARB_texture_query_levels)
2133 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2135 if (extensions->ARB_texture_query_lod)
2136 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2138 if (extensions->ARB_gpu_shader5)
2139 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2141 if (extensions->AMD_vertex_shader_layer)
2142 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2144 if (extensions->AMD_vertex_shader_viewport_index)
2145 add_builtin_define(parser, "GL_AMD_vertex_shader_viewport_index", 1);
2147 if (extensions->ARB_shading_language_420pack)
2148 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2150 if (extensions->ARB_sample_shading)
2151 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2153 if (extensions->ARB_texture_gather)
2154 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2156 if (extensions->ARB_shader_atomic_counters)
2157 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2159 if (extensions->ARB_viewport_array)
2160 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2162 if (extensions->ARB_compute_shader)
2163 add_builtin_define(parser, "GL_ARB_compute_shader", 1);
2165 if (extensions->ARB_shader_image_load_store)
2166 add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
2170 if (extensions != NULL) {
2171 if (extensions->EXT_shader_integer_mix)
2172 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2176 add_builtin_define(parser, "GL_core_profile", 1);
2178 /* Currently, all ES2/ES3 implementations support highp in the
2179 * fragment shader, so we always define this macro in ES2/ES3.
2180 * If we ever get a driver that doesn't support highp, we'll
2181 * need to add a flag to the gl_context and check that here.
2183 if (version >= 130 || parser->is_gles)
2184 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2186 if (explicitly_set) {
2187 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
2188 "#version %" PRIiMAX "%s%s", version,
2189 es_identifier ? " " : "",
2190 es_identifier ? es_identifier : "");
2194 /* GLSL version if no version is explicitly specified. */
2195 #define IMPLICIT_GLSL_VERSION 110
2197 /* GLSL ES version if no version is explicitly specified. */
2198 #define IMPLICIT_GLSL_ES_VERSION 100
2201 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2203 int language_version = parser->api == API_OPENGLES2 ?
2204 IMPLICIT_GLSL_ES_VERSION :
2205 IMPLICIT_GLSL_VERSION;
2207 _glcpp_parser_handle_version_declaration(parser, language_version,