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 macro_t *macro = hash_table_find (parser->defines, $3);
265 hash_table_remove (parser->defines, $3);
271 glcpp_parser_resolve_implicit_version(parser);
272 } conditional_tokens NEWLINE {
273 /* Be careful to only evaluate the 'if' expression if
274 * we are not skipping. When we are skipping, we
275 * simply push a new 0-valued 'if' onto the skip
278 * This avoids generating diagnostics for invalid
279 * expressions that are being skipped. */
280 if (parser->skip_stack == NULL ||
281 parser->skip_stack->type == SKIP_NO_SKIP)
283 _glcpp_parser_expand_and_lex_from (parser,
288 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
289 parser->skip_stack->type = SKIP_TO_ENDIF;
293 /* #if without an expression is only an error if we
294 * are not skipping */
295 if (parser->skip_stack == NULL ||
296 parser->skip_stack->type == SKIP_NO_SKIP)
298 glcpp_error(& @1, parser, "#if with no expression");
300 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
303 glcpp_parser_resolve_implicit_version(parser);
304 } IDENTIFIER junk NEWLINE {
305 macro_t *macro = hash_table_find (parser->defines, $3);
307 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
310 glcpp_parser_resolve_implicit_version(parser);
311 } IDENTIFIER junk NEWLINE {
312 macro_t *macro = hash_table_find (parser->defines, $3);
314 _glcpp_parser_skip_stack_push_if (parser, & @2, macro == NULL);
316 | HASH_ELIF conditional_tokens NEWLINE {
317 /* Be careful to only evaluate the 'elif' expression
318 * if we are not skipping. When we are skipping, we
319 * simply change to a 0-valued 'elif' on the skip
322 * This avoids generating diagnostics for invalid
323 * expressions that are being skipped. */
324 if (parser->skip_stack &&
325 parser->skip_stack->type == SKIP_TO_ELSE)
327 _glcpp_parser_expand_and_lex_from (parser,
330 else if (parser->skip_stack &&
331 parser->skip_stack->has_else)
333 glcpp_error(& @1, parser, "#elif after #else");
337 _glcpp_parser_skip_stack_change_if (parser, & @1,
341 | HASH_ELIF NEWLINE {
342 /* #elif without an expression is an error unless we
344 if (parser->skip_stack &&
345 parser->skip_stack->type == SKIP_TO_ELSE)
347 glcpp_error(& @1, parser, "#elif with no expression");
349 else if (parser->skip_stack &&
350 parser->skip_stack->has_else)
352 glcpp_error(& @1, parser, "#elif after #else");
356 _glcpp_parser_skip_stack_change_if (parser, & @1,
358 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
362 if (parser->skip_stack &&
363 parser->skip_stack->has_else)
365 glcpp_error(& @1, parser, "multiple #else");
369 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
370 if (parser->skip_stack)
371 parser->skip_stack->has_else = true;
375 _glcpp_parser_skip_stack_pop (parser, & @1);
377 | HASH_VERSION integer_constant NEWLINE {
378 if (parser->version_resolved) {
379 glcpp_error(& @1, parser, "#version must appear on the first line");
381 _glcpp_parser_handle_version_declaration(parser, $2, NULL, true);
383 | HASH_VERSION integer_constant IDENTIFIER 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, $3, true);
390 glcpp_parser_resolve_implicit_version(parser);
396 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
397 $$ = strtoll ($1 + 2, NULL, 16);
398 } else if ($1[0] == '0') {
399 $$ = strtoll ($1, NULL, 8);
401 $$ = strtoll ($1, NULL, 10);
412 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $1);
415 | expression OR expression {
418 | expression AND expression {
421 | expression '|' expression {
424 | expression '^' expression {
427 | expression '&' expression {
430 | expression NOT_EQUAL expression {
433 | expression EQUAL expression {
436 | expression GREATER_OR_EQUAL expression {
439 | expression LESS_OR_EQUAL expression {
442 | expression '>' expression {
445 | expression '<' expression {
448 | expression RIGHT_SHIFT expression {
451 | expression LEFT_SHIFT expression {
454 | expression '-' expression {
457 | expression '+' expression {
460 | expression '%' expression {
462 yyerror (& @1, parser,
463 "zero modulus in preprocessor directive");
468 | expression '/' expression {
470 yyerror (& @1, parser,
471 "division by 0 in preprocessor directive");
476 | expression '*' expression {
479 | '!' expression %prec UNARY {
482 | '~' expression %prec UNARY {
485 | '-' expression %prec UNARY {
488 | '+' expression %prec UNARY {
491 | '(' expression ')' {
498 $$ = _string_list_create (parser);
499 _string_list_append_item ($$, $1);
500 ralloc_steal ($$, $1);
502 | identifier_list ',' IDENTIFIER {
504 _string_list_append_item ($$, $3);
505 ralloc_steal ($$, $3);
510 NEWLINE { $$ = NULL; }
516 yyerror (& @1, parser, "Invalid tokens after #");
521 /* empty */ { $$ = NULL; }
528 glcpp_warning(&@1, parser, "extra tokens at end of directive");
533 /* Handle "defined" operator */
535 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
536 $$ = _token_create_ival (parser, INTEGER, v);
538 | DEFINED '(' IDENTIFIER ')' {
539 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
540 $$ = _token_create_ival (parser, INTEGER, v);
542 | preprocessing_token
546 /* Exactly the same as pp_tokens, but using conditional_token */
548 $$ = _token_list_create (parser);
549 _token_list_append ($$, $1);
551 | conditional_tokens conditional_token {
553 _token_list_append ($$, $2);
558 preprocessing_token {
559 parser->space_tokens = 1;
560 $$ = _token_list_create (parser);
561 _token_list_append ($$, $1);
563 | pp_tokens preprocessing_token {
565 _token_list_append ($$, $2);
571 $$ = _token_create_str (parser, IDENTIFIER, $1);
572 $$->location = yylloc;
575 $$ = _token_create_str (parser, INTEGER_STRING, $1);
576 $$->location = yylloc;
579 $$ = _token_create_ival (parser, $1, $1);
580 $$->location = yylloc;
583 $$ = _token_create_str (parser, OTHER, $1);
584 $$->location = yylloc;
587 $$ = _token_create_ival (parser, SPACE, SPACE);
588 $$->location = yylloc;
608 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
609 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
612 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
613 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
614 | EQUAL { $$ = EQUAL; }
615 | NOT_EQUAL { $$ = NOT_EQUAL; }
623 | PASTE { $$ = PASTE; }
629 _string_list_create (void *ctx)
633 list = ralloc (ctx, string_list_t);
641 _string_list_append_item (string_list_t *list, const char *str)
645 node = ralloc (list, string_node_t);
646 node->str = ralloc_strdup (node, str);
650 if (list->head == NULL) {
653 list->tail->next = node;
660 _string_list_contains (string_list_t *list, const char *member, int *index)
668 for (i = 0, node = list->head; node; i++, node = node->next) {
669 if (strcmp (node->str, member) == 0) {
680 _string_list_length (string_list_t *list)
688 for (node = list->head; node; node = node->next)
695 _string_list_equal (string_list_t *a, string_list_t *b)
697 string_node_t *node_a, *node_b;
699 if (a == NULL && b == NULL)
702 if (a == NULL || b == NULL)
705 for (node_a = a->head, node_b = b->head;
707 node_a = node_a->next, node_b = node_b->next)
709 if (strcmp (node_a->str, node_b->str))
713 /* Catch the case of lists being different lengths, (which
714 * would cause the loop above to terminate after the shorter
716 return node_a == node_b;
720 _argument_list_create (void *ctx)
722 argument_list_t *list;
724 list = ralloc (ctx, argument_list_t);
732 _argument_list_append (argument_list_t *list, token_list_t *argument)
734 argument_node_t *node;
736 node = ralloc (list, argument_node_t);
737 node->argument = argument;
741 if (list->head == NULL) {
744 list->tail->next = node;
751 _argument_list_length (argument_list_t *list)
754 argument_node_t *node;
759 for (node = list->head; node; node = node->next)
766 _argument_list_member_at (argument_list_t *list, int index)
768 argument_node_t *node;
775 for (i = 0; i < index; i++) {
782 return node->argument;
787 /* Note: This function ralloc_steal()s the str pointer. */
789 _token_create_str (void *ctx, int type, char *str)
793 token = ralloc (ctx, token_t);
795 token->value.str = str;
797 ralloc_steal (token, str);
803 _token_create_ival (void *ctx, int type, int ival)
807 token = ralloc (ctx, token_t);
809 token->value.ival = ival;
815 _token_list_create (void *ctx)
819 list = ralloc (ctx, token_list_t);
822 list->non_space_tail = NULL;
828 _token_list_append (token_list_t *list, token_t *token)
832 node = ralloc (list, token_node_t);
836 if (list->head == NULL) {
839 list->tail->next = node;
843 if (token->type != SPACE)
844 list->non_space_tail = node;
848 _token_list_append_list (token_list_t *list, token_list_t *tail)
850 if (tail == NULL || tail->head == NULL)
853 if (list->head == NULL) {
854 list->head = tail->head;
856 list->tail->next = tail->head;
859 list->tail = tail->tail;
860 list->non_space_tail = tail->non_space_tail;
863 static token_list_t *
864 _token_list_copy (void *ctx, token_list_t *other)
872 copy = _token_list_create (ctx);
873 for (node = other->head; node; node = node->next) {
874 token_t *new_token = ralloc (copy, token_t);
875 *new_token = *node->token;
876 _token_list_append (copy, new_token);
883 _token_list_trim_trailing_space (token_list_t *list)
885 token_node_t *tail, *next;
887 if (list->non_space_tail) {
888 tail = list->non_space_tail->next;
889 list->non_space_tail->next = NULL;
890 list->tail = list->non_space_tail;
901 _token_list_is_empty_ignoring_space (token_list_t *l)
909 while (n != NULL && n->token->type == SPACE)
916 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
918 token_node_t *node_a, *node_b;
920 if (a == NULL || b == NULL) {
921 int a_empty = _token_list_is_empty_ignoring_space(a);
922 int b_empty = _token_list_is_empty_ignoring_space(b);
923 return a_empty == b_empty;
931 if (node_a == NULL && node_b == NULL)
934 if (node_a == NULL || node_b == NULL)
937 if (node_a->token->type == SPACE) {
938 node_a = node_a->next;
942 if (node_b->token->type == SPACE) {
943 node_b = node_b->next;
947 if (node_a->token->type != node_b->token->type)
950 switch (node_a->token->type) {
952 if (node_a->token->value.ival !=
953 node_b->token->value.ival)
961 if (strcmp (node_a->token->value.str,
962 node_b->token->value.str))
969 node_a = node_a->next;
970 node_b = node_b->next;
977 _token_print (char **out, size_t *len, token_t *token)
979 if (token->type < 256) {
980 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
984 switch (token->type) {
986 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
991 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
994 ralloc_asprintf_rewrite_tail (out, len, " ");
997 ralloc_asprintf_rewrite_tail (out, len, "<<");
1000 ralloc_asprintf_rewrite_tail (out, len, ">>");
1003 ralloc_asprintf_rewrite_tail (out, len, "<=");
1005 case GREATER_OR_EQUAL:
1006 ralloc_asprintf_rewrite_tail (out, len, ">=");
1009 ralloc_asprintf_rewrite_tail (out, len, "==");
1012 ralloc_asprintf_rewrite_tail (out, len, "!=");
1015 ralloc_asprintf_rewrite_tail (out, len, "&&");
1018 ralloc_asprintf_rewrite_tail (out, len, "||");
1021 ralloc_asprintf_rewrite_tail (out, len, "##");
1024 ralloc_asprintf_rewrite_tail (out, len, ",");
1027 /* Nothing to print. */
1030 assert(!"Error: Don't know how to print token.");
1035 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1036 * 'token' and 'other'. Note that this function may return 'token' or
1037 * 'other' directly rather than allocating anything new.
1039 * Caution: Only very cursory error-checking is performed to see if
1040 * the final result is a valid single token. */
1042 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1044 token_t *combined = NULL;
1046 /* Pasting a placeholder onto anything makes no change. */
1047 if (other->type == PLACEHOLDER)
1050 /* When 'token' is a placeholder, just return 'other'. */
1051 if (token->type == PLACEHOLDER)
1054 /* A very few single-character punctuators can be combined
1055 * with another to form a multi-character punctuator. */
1056 switch (token->type) {
1058 if (other->type == '<')
1059 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1060 else if (other->type == '=')
1061 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1064 if (other->type == '>')
1065 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1066 else if (other->type == '=')
1067 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1070 if (other->type == '=')
1071 combined = _token_create_ival (token, EQUAL, EQUAL);
1074 if (other->type == '=')
1075 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1078 if (other->type == '&')
1079 combined = _token_create_ival (token, AND, AND);
1082 if (other->type == '|')
1083 combined = _token_create_ival (token, OR, OR);
1087 if (combined != NULL) {
1088 /* Inherit the location from the first token */
1089 combined->location = token->location;
1093 /* Two string-valued (or integer) tokens can usually just be
1094 * mashed together. (We also handle a string followed by an
1095 * integer here as well.)
1097 * There are some exceptions here. Notably, if the first token
1098 * is an integer (or a string representing an integer), then
1099 * the second token must also be an integer or must be a
1100 * string representing an integer that begins with a digit.
1102 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1103 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1108 /* Check that pasting onto an integer doesn't create a
1109 * non-integer, (that is, only digits can be
1111 if (token->type == INTEGER_STRING || token->type == INTEGER)
1113 switch (other->type) {
1114 case INTEGER_STRING:
1115 if (other->value.str[0] < '0' ||
1116 other->value.str[0] > '9')
1120 if (other->value.ival < 0)
1128 if (token->type == INTEGER)
1129 str = ralloc_asprintf (token, "%" PRIiMAX,
1132 str = ralloc_strdup (token, token->value.str);
1135 if (other->type == INTEGER)
1136 ralloc_asprintf_append (&str, "%" PRIiMAX,
1139 ralloc_strcat (&str, other->value.str);
1141 /* New token is same type as original token, unless we
1142 * started with an integer, in which case we will be
1143 * creating an integer-string. */
1144 combined_type = token->type;
1145 if (combined_type == INTEGER)
1146 combined_type = INTEGER_STRING;
1148 combined = _token_create_str (token, combined_type, str);
1149 combined->location = token->location;
1154 glcpp_error (&token->location, parser, "");
1155 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1156 _token_print (&parser->info_log, &parser->info_log_length, token);
1157 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1158 _token_print (&parser->info_log, &parser->info_log_length, other);
1159 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1165 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1172 for (node = list->head; node; node = node->next)
1173 _token_print (&parser->output, &parser->output_length, node->token);
1177 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1179 glcpp_error(locp, parser, "%s", error);
1182 static void add_builtin_define(glcpp_parser_t *parser,
1183 const char *name, int value)
1188 tok = _token_create_ival (parser, INTEGER, value);
1190 list = _token_list_create(parser);
1191 _token_list_append(list, tok);
1192 _define_object_macro(parser, NULL, name, list);
1196 glcpp_parser_create (const struct gl_extensions *extensions, gl_api api)
1198 glcpp_parser_t *parser;
1200 parser = ralloc (NULL, glcpp_parser_t);
1202 glcpp_lex_init_extra (parser, &parser->scanner);
1203 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1204 hash_table_string_compare);
1205 parser->active = NULL;
1206 parser->lexing_if = 0;
1207 parser->space_tokens = 1;
1208 parser->newline_as_space = 0;
1209 parser->in_control_line = 0;
1210 parser->paren_count = 0;
1211 parser->commented_newlines = 0;
1213 parser->skip_stack = NULL;
1215 parser->lex_from_list = NULL;
1216 parser->lex_from_node = NULL;
1218 parser->output = ralloc_strdup(parser, "");
1219 parser->output_length = 0;
1220 parser->info_log = ralloc_strdup(parser, "");
1221 parser->info_log_length = 0;
1224 parser->extensions = extensions;
1226 parser->version_resolved = false;
1228 parser->has_new_line_number = 0;
1229 parser->new_line_number = 1;
1230 parser->has_new_source_number = 0;
1231 parser->new_source_number = 0;
1237 glcpp_parser_destroy (glcpp_parser_t *parser)
1239 glcpp_lex_destroy (parser->scanner);
1240 hash_table_dtor (parser->defines);
1241 ralloc_free (parser);
1244 typedef enum function_status
1246 FUNCTION_STATUS_SUCCESS,
1247 FUNCTION_NOT_A_FUNCTION,
1248 FUNCTION_UNBALANCED_PARENTHESES
1249 } function_status_t;
1251 /* Find a set of function-like macro arguments by looking for a
1252 * balanced set of parentheses.
1254 * When called, 'node' should be the opening-parenthesis token, (or
1255 * perhaps preceeding SPACE tokens). Upon successful return *last will
1256 * be the last consumed node, (corresponding to the closing right
1261 * FUNCTION_STATUS_SUCCESS:
1263 * Successfully parsed a set of function arguments.
1265 * FUNCTION_NOT_A_FUNCTION:
1267 * Macro name not followed by a '('. This is not an error, but
1268 * simply that the macro name should be treated as a non-macro.
1270 * FUNCTION_UNBALANCED_PARENTHESES
1272 * Macro name is not followed by a balanced set of parentheses.
1274 static function_status_t
1275 _arguments_parse (argument_list_t *arguments,
1277 token_node_t **last)
1279 token_list_t *argument;
1284 /* Ignore whitespace before first parenthesis. */
1285 while (node && node->token->type == SPACE)
1288 if (node == NULL || node->token->type != '(')
1289 return FUNCTION_NOT_A_FUNCTION;
1293 argument = _token_list_create (arguments);
1294 _argument_list_append (arguments, argument);
1296 for (paren_count = 1; node; node = node->next) {
1297 if (node->token->type == '(')
1301 else if (node->token->type == ')')
1304 if (paren_count == 0)
1308 if (node->token->type == ',' &&
1311 _token_list_trim_trailing_space (argument);
1312 argument = _token_list_create (arguments);
1313 _argument_list_append (arguments, argument);
1316 if (argument->head == NULL) {
1317 /* Don't treat initial whitespace as
1318 * part of the arguement. */
1319 if (node->token->type == SPACE)
1322 _token_list_append (argument, node->token);
1327 return FUNCTION_UNBALANCED_PARENTHESES;
1331 return FUNCTION_STATUS_SUCCESS;
1334 static token_list_t *
1335 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1340 list = _token_list_create (ctx);
1341 node = _token_create_ival (list, type, ival);
1342 _token_list_append (list, node);
1347 static token_list_t *
1348 _token_list_create_with_one_space (void *ctx)
1350 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1353 static token_list_t *
1354 _token_list_create_with_one_integer (void *ctx, int ival)
1356 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1359 /* Perform macro expansion on 'list', placing the resulting tokens
1360 * into a new list which is initialized with a first token of type
1361 * 'head_token_type'. Then begin lexing from the resulting list,
1362 * (return to the current lexing source when this list is exhausted).
1365 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1366 int head_token_type,
1369 token_list_t *expanded;
1372 expanded = _token_list_create (parser);
1373 token = _token_create_ival (parser, head_token_type, head_token_type);
1374 _token_list_append (expanded, token);
1375 _glcpp_parser_expand_token_list (parser, list);
1376 _token_list_append_list (expanded, list);
1377 glcpp_parser_lex_from (parser, expanded);
1381 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1388 token_node_t *next_non_space;
1390 /* Look ahead for a PASTE token, skipping space. */
1391 next_non_space = node->next;
1392 while (next_non_space && next_non_space->token->type == SPACE)
1393 next_non_space = next_non_space->next;
1395 if (next_non_space == NULL)
1398 if (next_non_space->token->type != PASTE) {
1399 node = next_non_space;
1403 /* Now find the next non-space token after the PASTE. */
1404 next_non_space = next_non_space->next;
1405 while (next_non_space && next_non_space->token->type == SPACE)
1406 next_non_space = next_non_space->next;
1408 if (next_non_space == NULL) {
1409 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1413 node->token = _token_paste (parser, node->token, next_non_space->token);
1414 node->next = next_non_space->next;
1415 if (next_non_space == list->tail)
1419 list->non_space_tail = list->tail;
1422 /* This is a helper function that's essentially part of the
1423 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1424 * except for by that function.
1426 * Returns NULL if node is a simple token with no expansion, (that is,
1427 * although 'node' corresponds to an identifier defined as a
1428 * function-like macro, it is not followed with a parenthesized
1431 * Compute the complete expansion of node (which is a function-like
1432 * macro) and subsequent nodes which are arguments.
1434 * Returns the token list that results from the expansion and sets
1435 * *last to the last node in the list that was consumed by the
1436 * expansion. Specifically, *last will be set as follows: as the
1437 * token of the closing right parenthesis.
1439 static token_list_t *
1440 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1442 token_node_t **last)
1446 const char *identifier;
1447 argument_list_t *arguments;
1448 function_status_t status;
1449 token_list_t *substituted;
1450 int parameter_index;
1452 identifier = node->token->value.str;
1454 macro = hash_table_find (parser->defines, identifier);
1456 assert (macro->is_function);
1458 arguments = _argument_list_create (parser);
1459 status = _arguments_parse (arguments, node, last);
1462 case FUNCTION_STATUS_SUCCESS:
1464 case FUNCTION_NOT_A_FUNCTION:
1466 case FUNCTION_UNBALANCED_PARENTHESES:
1467 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1471 /* Replace a macro defined as empty with a SPACE token. */
1472 if (macro->replacements == NULL) {
1473 ralloc_free (arguments);
1474 return _token_list_create_with_one_space (parser);
1477 if (! ((_argument_list_length (arguments) ==
1478 _string_list_length (macro->parameters)) ||
1479 (_string_list_length (macro->parameters) == 0 &&
1480 _argument_list_length (arguments) == 1 &&
1481 arguments->head->argument->head == NULL)))
1483 glcpp_error (&node->token->location, parser,
1484 "Error: macro %s invoked with %d arguments (expected %d)\n",
1486 _argument_list_length (arguments),
1487 _string_list_length (macro->parameters));
1491 /* Perform argument substitution on the replacement list. */
1492 substituted = _token_list_create (arguments);
1494 for (node = macro->replacements->head; node; node = node->next)
1496 if (node->token->type == IDENTIFIER &&
1497 _string_list_contains (macro->parameters,
1498 node->token->value.str,
1501 token_list_t *argument;
1502 argument = _argument_list_member_at (arguments,
1504 /* Before substituting, we expand the argument
1505 * tokens, or append a placeholder token for
1506 * an empty argument. */
1507 if (argument->head) {
1508 token_list_t *expanded_argument;
1509 expanded_argument = _token_list_copy (parser,
1511 _glcpp_parser_expand_token_list (parser,
1513 _token_list_append_list (substituted,
1518 new_token = _token_create_ival (substituted,
1521 _token_list_append (substituted, new_token);
1524 _token_list_append (substituted, node->token);
1528 /* After argument substitution, and before further expansion
1529 * below, implement token pasting. */
1531 _token_list_trim_trailing_space (substituted);
1533 _glcpp_parser_apply_pastes (parser, substituted);
1538 /* Compute the complete expansion of node, (and subsequent nodes after
1539 * 'node' in the case that 'node' is a function-like macro and
1540 * subsequent nodes are arguments).
1542 * Returns NULL if node is a simple token with no expansion.
1544 * Otherwise, returns the token list that results from the expansion
1545 * and sets *last to the last node in the list that was consumed by
1546 * the expansion. Specifically, *last will be set as follows:
1548 * As 'node' in the case of object-like macro expansion.
1550 * As the token of the closing right parenthesis in the case of
1551 * function-like macro expansion.
1553 static token_list_t *
1554 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1556 token_node_t **last)
1558 token_t *token = node->token;
1559 const char *identifier;
1562 /* We only expand identifiers */
1563 if (token->type != IDENTIFIER) {
1564 /* We change any COMMA into a COMMA_FINAL to prevent
1565 * it being mistaken for an argument separator
1567 if (token->type == ',') {
1568 token->type = COMMA_FINAL;
1569 token->value.ival = COMMA_FINAL;
1576 identifier = token->value.str;
1578 /* Special handling for __LINE__ and __FILE__, (not through
1579 * the hash table). */
1580 if (strcmp(identifier, "__LINE__") == 0)
1581 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1583 if (strcmp(identifier, "__FILE__") == 0)
1584 return _token_list_create_with_one_integer (parser, node->token->location.source);
1586 /* Look up this identifier in the hash table. */
1587 macro = hash_table_find (parser->defines, identifier);
1589 /* Not a macro, so no expansion needed. */
1593 /* Finally, don't expand this macro if we're already actively
1594 * expanding it, (to avoid infinite recursion). */
1595 if (_parser_active_list_contains (parser, identifier)) {
1596 /* We change the token type here from IDENTIFIER to
1597 * OTHER to prevent any future expansion of this
1598 * unexpanded token. */
1600 token_list_t *expansion;
1603 str = ralloc_strdup (parser, token->value.str);
1604 final = _token_create_str (parser, OTHER, str);
1605 expansion = _token_list_create (parser);
1606 _token_list_append (expansion, final);
1610 if (! macro->is_function)
1612 token_list_t *replacement;
1614 /* Replace a macro defined as empty with a SPACE token. */
1615 if (macro->replacements == NULL)
1616 return _token_list_create_with_one_space (parser);
1618 replacement = _token_list_copy (parser, macro->replacements);
1619 _glcpp_parser_apply_pastes (parser, replacement);
1623 return _glcpp_parser_expand_function (parser, node, last);
1626 /* Push a new identifier onto the parser's active list.
1628 * Here, 'marker' is the token node that appears in the list after the
1629 * expansion of 'identifier'. That is, when the list iterator begins
1630 * examining 'marker', then it is time to pop this node from the
1634 _parser_active_list_push (glcpp_parser_t *parser,
1635 const char *identifier,
1636 token_node_t *marker)
1638 active_list_t *node;
1640 node = ralloc (parser->active, active_list_t);
1641 node->identifier = ralloc_strdup (node, identifier);
1642 node->marker = marker;
1643 node->next = parser->active;
1645 parser->active = node;
1649 _parser_active_list_pop (glcpp_parser_t *parser)
1651 active_list_t *node = parser->active;
1654 parser->active = NULL;
1658 node = parser->active->next;
1659 ralloc_free (parser->active);
1661 parser->active = node;
1665 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1667 active_list_t *node;
1669 if (parser->active == NULL)
1672 for (node = parser->active; node; node = node->next)
1673 if (strcmp (node->identifier, identifier) == 0)
1679 /* Walk over the token list replacing nodes with their expansion.
1680 * Whenever nodes are expanded the walking will walk over the new
1681 * nodes, continuing to expand as necessary. The results are placed in
1685 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1688 token_node_t *node_prev;
1689 token_node_t *node, *last = NULL;
1690 token_list_t *expansion;
1691 active_list_t *active_initial = parser->active;
1696 _token_list_trim_trailing_space (list);
1703 while (parser->active && parser->active->marker == node)
1704 _parser_active_list_pop (parser);
1706 expansion = _glcpp_parser_expand_node (parser, node, &last);
1710 for (n = node; n != last->next; n = n->next)
1711 while (parser->active &&
1712 parser->active->marker == n)
1714 _parser_active_list_pop (parser);
1717 _parser_active_list_push (parser,
1718 node->token->value.str,
1721 /* Splice expansion into list, supporting a
1722 * simple deletion if the expansion is
1724 if (expansion->head) {
1726 node_prev->next = expansion->head;
1728 list->head = expansion->head;
1729 expansion->tail->next = last->next;
1730 if (last == list->tail)
1731 list->tail = expansion->tail;
1734 node_prev->next = last->next;
1736 list->head = last->next;
1737 if (last == list->tail)
1743 node = node_prev ? node_prev->next : list->head;
1746 /* Remove any lingering effects of this invocation on the
1747 * active list. That is, pop until the list looks like it did
1748 * at the beginning of this function. */
1749 while (parser->active && parser->active != active_initial)
1750 _parser_active_list_pop (parser);
1752 list->non_space_tail = list->tail;
1756 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1762 _glcpp_parser_expand_token_list (parser, list);
1764 _token_list_trim_trailing_space (list);
1766 _token_list_print (parser, list);
1770 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1771 const char *identifier)
1773 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
1774 * the GLSL ES spec (all versions) say:
1776 * "All macro names containing two consecutive underscores ( __ )
1777 * are reserved for future use as predefined macro names. All
1778 * macro names prefixed with "GL_" ("GL" followed by a single
1779 * underscore) are also reserved."
1781 * The intention is that names containing __ are reserved for internal
1782 * use by the implementation, and names prefixed with GL_ are reserved
1783 * for use by Khronos. Since every extension adds a name prefixed
1784 * with GL_ (i.e., the name of the extension), that should be an
1785 * error. Names simply containing __ are dangerous to use, but should
1788 * A future version of the GLSL specification will clarify this.
1790 if (strstr(identifier, "__")) {
1791 glcpp_warning(loc, parser,
1792 "Macro names containing \"__\" are reserved "
1793 "for use by the implementation.\n");
1795 if (strncmp(identifier, "GL_", 3) == 0) {
1796 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1801 _macro_equal (macro_t *a, macro_t *b)
1803 if (a->is_function != b->is_function)
1806 if (a->is_function) {
1807 if (! _string_list_equal (a->parameters, b->parameters))
1811 return _token_list_equal_ignoring_space (a->replacements,
1816 _define_object_macro (glcpp_parser_t *parser,
1818 const char *identifier,
1819 token_list_t *replacements)
1821 macro_t *macro, *previous;
1824 _check_for_reserved_macro_name(parser, loc, identifier);
1826 macro = ralloc (parser, macro_t);
1828 macro->is_function = 0;
1829 macro->parameters = NULL;
1830 macro->identifier = ralloc_strdup (macro, identifier);
1831 macro->replacements = replacements;
1832 ralloc_steal (macro, replacements);
1834 previous = hash_table_find (parser->defines, identifier);
1836 if (_macro_equal (macro, previous)) {
1837 ralloc_free (macro);
1840 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1844 hash_table_insert (parser->defines, macro, identifier);
1848 _define_function_macro (glcpp_parser_t *parser,
1850 const char *identifier,
1851 string_list_t *parameters,
1852 token_list_t *replacements)
1854 macro_t *macro, *previous;
1856 _check_for_reserved_macro_name(parser, loc, identifier);
1858 macro = ralloc (parser, macro_t);
1859 ralloc_steal (macro, parameters);
1860 ralloc_steal (macro, replacements);
1862 macro->is_function = 1;
1863 macro->parameters = parameters;
1864 macro->identifier = ralloc_strdup (macro, identifier);
1865 macro->replacements = replacements;
1866 previous = hash_table_find (parser->defines, identifier);
1868 if (_macro_equal (macro, previous)) {
1869 ralloc_free (macro);
1872 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1876 hash_table_insert (parser->defines, macro, identifier);
1880 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1885 if (parser->lex_from_list == NULL) {
1886 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1888 /* XXX: This ugly block of code exists for the sole
1889 * purpose of converting a NEWLINE token into a SPACE
1890 * token, but only in the case where we have seen a
1891 * function-like macro name, but have not yet seen its
1892 * closing parenthesis.
1894 * There's perhaps a more compact way to do this with
1895 * mid-rule actions in the grammar.
1897 * I'm definitely not pleased with the complexity of
1900 if (parser->newline_as_space)
1903 parser->paren_count++;
1904 } else if (ret == ')') {
1905 parser->paren_count--;
1906 if (parser->paren_count == 0)
1907 parser->newline_as_space = 0;
1908 } else if (ret == NEWLINE) {
1910 } else if (ret != SPACE) {
1911 if (parser->paren_count == 0)
1912 parser->newline_as_space = 0;
1915 else if (parser->in_control_line)
1918 parser->in_control_line = 0;
1920 else if (ret == HASH_DEFINE ||
1921 ret == HASH_UNDEF || ret == HASH_IF ||
1922 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1923 ret == HASH_ELIF || ret == HASH_ELSE ||
1924 ret == HASH_ENDIF || ret == HASH)
1926 parser->in_control_line = 1;
1928 else if (ret == IDENTIFIER)
1931 macro = hash_table_find (parser->defines,
1933 if (macro && macro->is_function) {
1934 parser->newline_as_space = 1;
1935 parser->paren_count = 0;
1942 node = parser->lex_from_node;
1945 ralloc_free (parser->lex_from_list);
1946 parser->lex_from_list = NULL;
1950 *yylval = node->token->value;
1951 ret = node->token->type;
1953 parser->lex_from_node = node->next;
1959 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1963 assert (parser->lex_from_list == NULL);
1965 /* Copy list, eliminating any space tokens. */
1966 parser->lex_from_list = _token_list_create (parser);
1968 for (node = list->head; node; node = node->next) {
1969 if (node->token->type == SPACE)
1971 _token_list_append (parser->lex_from_list, node->token);
1976 parser->lex_from_node = parser->lex_from_list->head;
1978 /* It's possible the list consisted of nothing but whitespace. */
1979 if (parser->lex_from_node == NULL) {
1980 ralloc_free (parser->lex_from_list);
1981 parser->lex_from_list = NULL;
1986 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1989 skip_type_t current = SKIP_NO_SKIP;
1992 if (parser->skip_stack)
1993 current = parser->skip_stack->type;
1995 node = ralloc (parser, skip_node_t);
1998 if (current == SKIP_NO_SKIP) {
2000 node->type = SKIP_NO_SKIP;
2002 node->type = SKIP_TO_ELSE;
2004 node->type = SKIP_TO_ENDIF;
2007 node->has_else = false;
2008 node->next = parser->skip_stack;
2009 parser->skip_stack = node;
2013 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
2014 const char *type, int condition)
2016 if (parser->skip_stack == NULL) {
2017 glcpp_error (loc, parser, "%s without #if\n", type);
2021 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2023 parser->skip_stack->type = SKIP_NO_SKIP;
2025 parser->skip_stack->type = SKIP_TO_ENDIF;
2030 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
2034 if (parser->skip_stack == NULL) {
2035 glcpp_error (loc, parser, "#endif without #if\n");
2039 node = parser->skip_stack;
2040 parser->skip_stack = node->next;
2045 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2046 const char *es_identifier,
2047 bool explicitly_set)
2049 const struct gl_extensions *extensions = parser->extensions;
2051 if (parser->version_resolved)
2054 parser->version_resolved = true;
2056 add_builtin_define (parser, "__VERSION__", version);
2058 parser->is_gles = (version == 100) ||
2060 (strcmp(es_identifier, "es") == 0));
2062 /* Add pre-defined macros. */
2063 if (parser->is_gles) {
2064 add_builtin_define(parser, "GL_ES", 1);
2065 add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
2067 if (extensions != NULL) {
2068 if (extensions->OES_EGL_image_external)
2069 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2072 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2073 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2074 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2075 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2078 if (extensions != NULL) {
2079 if (extensions->EXT_texture_array)
2080 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2082 if (extensions->ARB_arrays_of_arrays)
2083 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2085 if (extensions->ARB_fragment_coord_conventions)
2086 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2089 if (extensions->ARB_fragment_layer_viewport)
2090 add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
2092 if (extensions->ARB_explicit_attrib_location)
2093 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2095 if (extensions->ARB_explicit_uniform_location)
2096 add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
2098 if (extensions->ARB_shader_texture_lod)
2099 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2101 if (extensions->ARB_draw_instanced)
2102 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2104 if (extensions->ARB_conservative_depth) {
2105 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2106 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2109 if (extensions->ARB_shader_bit_encoding)
2110 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2112 if (extensions->ARB_uniform_buffer_object)
2113 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2115 if (extensions->ARB_texture_cube_map_array)
2116 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2118 if (extensions->ARB_shading_language_packing)
2119 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2121 if (extensions->ARB_texture_multisample)
2122 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2124 if (extensions->ARB_texture_query_levels)
2125 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2127 if (extensions->ARB_texture_query_lod)
2128 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2130 if (extensions->ARB_gpu_shader5)
2131 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2133 if (extensions->AMD_vertex_shader_layer)
2134 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2136 if (extensions->ARB_shading_language_420pack)
2137 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2139 if (extensions->ARB_sample_shading)
2140 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2142 if (extensions->ARB_texture_gather)
2143 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2145 if (extensions->ARB_shader_atomic_counters)
2146 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2148 if (extensions->ARB_viewport_array)
2149 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2153 if (extensions != NULL) {
2154 if (extensions->EXT_shader_integer_mix)
2155 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2159 add_builtin_define(parser, "GL_core_profile", 1);
2161 /* Currently, all ES2/ES3 implementations support highp in the
2162 * fragment shader, so we always define this macro in ES2/ES3.
2163 * If we ever get a driver that doesn't support highp, we'll
2164 * need to add a flag to the gl_context and check that here.
2166 if (version >= 130 || parser->is_gles)
2167 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2169 if (explicitly_set) {
2170 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
2171 "#version %" PRIiMAX "%s%s", version,
2172 es_identifier ? " " : "",
2173 es_identifier ? es_identifier : "");
2177 /* GLSL version if no version is explicitly specified. */
2178 #define IMPLICIT_GLSL_VERSION 110
2180 /* GLSL ES version if no version is explicitly specified. */
2181 #define IMPLICIT_GLSL_ES_VERSION 100
2184 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2186 int language_version = parser->api == API_OPENGLES2 ?
2187 IMPLICIT_GLSL_ES_VERSION :
2188 IMPLICIT_GLSL_VERSION;
2190 _glcpp_parser_handle_version_declaration(parser, language_version,