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,
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");
197 | HASH_LINE pp_tokens NEWLINE {
198 if (parser->skip_stack == NULL ||
199 parser->skip_stack->type == SKIP_NO_SKIP)
201 _glcpp_parser_expand_and_lex_from (parser,
206 _glcpp_parser_print_expanded_token_list (parser, $1);
207 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
215 IF_EXPANDED expression NEWLINE {
216 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
218 | ELIF_EXPANDED expression NEWLINE {
219 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
221 | LINE_EXPANDED integer_constant NEWLINE {
222 parser->has_new_line_number = 1;
223 parser->new_line_number = $2;
224 ralloc_asprintf_rewrite_tail (&parser->output,
225 &parser->output_length,
226 "#line %" PRIiMAX "\n",
229 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
230 parser->has_new_line_number = 1;
231 parser->new_line_number = $2;
232 parser->has_new_source_number = 1;
233 parser->new_source_number = $3;
234 ralloc_asprintf_rewrite_tail (&parser->output,
235 &parser->output_length,
236 "#line %" PRIiMAX " %" PRIiMAX "\n",
242 HASH_DEFINE OBJ_IDENTIFIER replacement_list NEWLINE {
243 _define_object_macro (parser, & @2, $2, $3);
245 | HASH_DEFINE FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
246 _define_function_macro (parser, & @2, $2, NULL, $5);
248 | HASH_DEFINE FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
249 _define_function_macro (parser, & @2, $2, $4, $6);
251 | HASH_UNDEF IDENTIFIER NEWLINE {
252 macro_t *macro = hash_table_find (parser->defines, $2);
254 hash_table_remove (parser->defines, $2);
259 | HASH_IF conditional_tokens NEWLINE {
260 /* Be careful to only evaluate the 'if' expression if
261 * we are not skipping. When we are skipping, we
262 * simply push a new 0-valued 'if' onto the skip
265 * This avoids generating diagnostics for invalid
266 * expressions that are being skipped. */
267 if (parser->skip_stack == NULL ||
268 parser->skip_stack->type == SKIP_NO_SKIP)
270 _glcpp_parser_expand_and_lex_from (parser,
275 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
276 parser->skip_stack->type = SKIP_TO_ENDIF;
280 /* #if without an expression is only an error if we
281 * are not skipping */
282 if (parser->skip_stack == NULL ||
283 parser->skip_stack->type == SKIP_NO_SKIP)
285 glcpp_error(& @1, parser, "#if with no expression");
287 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
289 | HASH_IFDEF IDENTIFIER junk NEWLINE {
290 macro_t *macro = hash_table_find (parser->defines, $2);
292 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
294 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
295 macro_t *macro = hash_table_find (parser->defines, $2);
297 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
299 | HASH_ELIF conditional_tokens NEWLINE {
300 /* Be careful to only evaluate the 'elif' expression
301 * if we are not skipping. When we are skipping, we
302 * simply change to a 0-valued 'elif' on the skip
305 * This avoids generating diagnostics for invalid
306 * expressions that are being skipped. */
307 if (parser->skip_stack &&
308 parser->skip_stack->type == SKIP_TO_ELSE)
310 _glcpp_parser_expand_and_lex_from (parser,
313 else if (parser->skip_stack &&
314 parser->skip_stack->has_else)
316 glcpp_error(& @1, parser, "#elif after #else");
320 _glcpp_parser_skip_stack_change_if (parser, & @1,
324 | HASH_ELIF NEWLINE {
325 /* #elif without an expression is an error unless we
327 if (parser->skip_stack &&
328 parser->skip_stack->type == SKIP_TO_ELSE)
330 glcpp_error(& @1, parser, "#elif with no expression");
332 else if (parser->skip_stack &&
333 parser->skip_stack->has_else)
335 glcpp_error(& @1, parser, "#elif after #else");
339 _glcpp_parser_skip_stack_change_if (parser, & @1,
341 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
345 if (parser->skip_stack &&
346 parser->skip_stack->has_else)
348 glcpp_error(& @1, parser, "multiple #else");
352 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
353 if (parser->skip_stack)
354 parser->skip_stack->has_else = true;
358 _glcpp_parser_skip_stack_pop (parser, & @1);
360 | HASH_VERSION integer_constant NEWLINE {
361 _glcpp_parser_handle_version_declaration(parser, $2, NULL);
363 | HASH_VERSION integer_constant IDENTIFIER NEWLINE {
364 _glcpp_parser_handle_version_declaration(parser, $2, $3);
371 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
372 $$ = strtoll ($1 + 2, NULL, 16);
373 } else if ($1[0] == '0') {
374 $$ = strtoll ($1, NULL, 8);
376 $$ = strtoll ($1, NULL, 10);
387 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $1);
390 | expression OR expression {
393 | expression AND expression {
396 | expression '|' expression {
399 | expression '^' expression {
402 | expression '&' expression {
405 | expression NOT_EQUAL expression {
408 | expression EQUAL expression {
411 | expression GREATER_OR_EQUAL expression {
414 | expression LESS_OR_EQUAL expression {
417 | expression '>' expression {
420 | expression '<' expression {
423 | expression RIGHT_SHIFT expression {
426 | expression LEFT_SHIFT expression {
429 | expression '-' expression {
432 | expression '+' expression {
435 | expression '%' expression {
437 yyerror (& @1, parser,
438 "zero modulus in preprocessor directive");
443 | expression '/' expression {
445 yyerror (& @1, parser,
446 "division by 0 in preprocessor directive");
451 | expression '*' expression {
454 | '!' expression %prec UNARY {
457 | '~' expression %prec UNARY {
460 | '-' expression %prec UNARY {
463 | '+' expression %prec UNARY {
466 | '(' expression ')' {
473 $$ = _string_list_create (parser);
474 _string_list_append_item ($$, $1);
475 ralloc_steal ($$, $1);
477 | identifier_list ',' IDENTIFIER {
479 _string_list_append_item ($$, $3);
480 ralloc_steal ($$, $3);
485 NEWLINE { $$ = NULL; }
491 yyerror (& @1, parser, "Invalid tokens after #");
496 /* empty */ { $$ = NULL; }
503 glcpp_warning(&@1, parser, "extra tokens at end of directive");
508 /* Handle "defined" operator */
510 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
511 $$ = _token_create_ival (parser, INTEGER, v);
513 | DEFINED '(' IDENTIFIER ')' {
514 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
515 $$ = _token_create_ival (parser, INTEGER, v);
517 | preprocessing_token
521 /* Exactly the same as pp_tokens, but using conditional_token */
523 $$ = _token_list_create (parser);
524 _token_list_append ($$, $1);
526 | conditional_tokens conditional_token {
528 _token_list_append ($$, $2);
533 preprocessing_token {
534 parser->space_tokens = 1;
535 $$ = _token_list_create (parser);
536 _token_list_append ($$, $1);
538 | pp_tokens preprocessing_token {
540 _token_list_append ($$, $2);
546 $$ = _token_create_str (parser, IDENTIFIER, $1);
547 $$->location = yylloc;
550 $$ = _token_create_str (parser, INTEGER_STRING, $1);
551 $$->location = yylloc;
554 $$ = _token_create_ival (parser, $1, $1);
555 $$->location = yylloc;
558 $$ = _token_create_str (parser, OTHER, $1);
559 $$->location = yylloc;
562 $$ = _token_create_ival (parser, SPACE, SPACE);
563 $$->location = yylloc;
583 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
584 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
587 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
588 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
589 | EQUAL { $$ = EQUAL; }
590 | NOT_EQUAL { $$ = NOT_EQUAL; }
598 | PASTE { $$ = PASTE; }
604 _string_list_create (void *ctx)
608 list = ralloc (ctx, string_list_t);
616 _string_list_append_item (string_list_t *list, const char *str)
620 node = ralloc (list, string_node_t);
621 node->str = ralloc_strdup (node, str);
625 if (list->head == NULL) {
628 list->tail->next = node;
635 _string_list_contains (string_list_t *list, const char *member, int *index)
643 for (i = 0, node = list->head; node; i++, node = node->next) {
644 if (strcmp (node->str, member) == 0) {
655 _string_list_length (string_list_t *list)
663 for (node = list->head; node; node = node->next)
670 _string_list_equal (string_list_t *a, string_list_t *b)
672 string_node_t *node_a, *node_b;
674 if (a == NULL && b == NULL)
677 if (a == NULL || b == NULL)
680 for (node_a = a->head, node_b = b->head;
682 node_a = node_a->next, node_b = node_b->next)
684 if (strcmp (node_a->str, node_b->str))
688 /* Catch the case of lists being different lengths, (which
689 * would cause the loop above to terminate after the shorter
691 return node_a == node_b;
695 _argument_list_create (void *ctx)
697 argument_list_t *list;
699 list = ralloc (ctx, argument_list_t);
707 _argument_list_append (argument_list_t *list, token_list_t *argument)
709 argument_node_t *node;
711 node = ralloc (list, argument_node_t);
712 node->argument = argument;
716 if (list->head == NULL) {
719 list->tail->next = node;
726 _argument_list_length (argument_list_t *list)
729 argument_node_t *node;
734 for (node = list->head; node; node = node->next)
741 _argument_list_member_at (argument_list_t *list, int index)
743 argument_node_t *node;
750 for (i = 0; i < index; i++) {
757 return node->argument;
762 /* Note: This function ralloc_steal()s the str pointer. */
764 _token_create_str (void *ctx, int type, char *str)
768 token = ralloc (ctx, token_t);
770 token->value.str = str;
772 ralloc_steal (token, str);
778 _token_create_ival (void *ctx, int type, int ival)
782 token = ralloc (ctx, token_t);
784 token->value.ival = ival;
790 _token_list_create (void *ctx)
794 list = ralloc (ctx, token_list_t);
797 list->non_space_tail = NULL;
803 _token_list_append (token_list_t *list, token_t *token)
807 node = ralloc (list, token_node_t);
811 if (list->head == NULL) {
814 list->tail->next = node;
818 if (token->type != SPACE)
819 list->non_space_tail = node;
823 _token_list_append_list (token_list_t *list, token_list_t *tail)
825 if (tail == NULL || tail->head == NULL)
828 if (list->head == NULL) {
829 list->head = tail->head;
831 list->tail->next = tail->head;
834 list->tail = tail->tail;
835 list->non_space_tail = tail->non_space_tail;
838 static token_list_t *
839 _token_list_copy (void *ctx, token_list_t *other)
847 copy = _token_list_create (ctx);
848 for (node = other->head; node; node = node->next) {
849 token_t *new_token = ralloc (copy, token_t);
850 *new_token = *node->token;
851 _token_list_append (copy, new_token);
858 _token_list_trim_trailing_space (token_list_t *list)
860 token_node_t *tail, *next;
862 if (list->non_space_tail) {
863 tail = list->non_space_tail->next;
864 list->non_space_tail->next = NULL;
865 list->tail = list->non_space_tail;
876 _token_list_is_empty_ignoring_space (token_list_t *l)
884 while (n != NULL && n->token->type == SPACE)
891 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
893 token_node_t *node_a, *node_b;
895 if (a == NULL || b == NULL) {
896 int a_empty = _token_list_is_empty_ignoring_space(a);
897 int b_empty = _token_list_is_empty_ignoring_space(b);
898 return a_empty == b_empty;
906 if (node_a == NULL && node_b == NULL)
909 if (node_a == NULL || node_b == NULL)
912 if (node_a->token->type == SPACE) {
913 node_a = node_a->next;
917 if (node_b->token->type == SPACE) {
918 node_b = node_b->next;
922 if (node_a->token->type != node_b->token->type)
925 switch (node_a->token->type) {
927 if (node_a->token->value.ival !=
928 node_b->token->value.ival)
936 if (strcmp (node_a->token->value.str,
937 node_b->token->value.str))
944 node_a = node_a->next;
945 node_b = node_b->next;
952 _token_print (char **out, size_t *len, token_t *token)
954 if (token->type < 256) {
955 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
959 switch (token->type) {
961 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
966 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
969 ralloc_asprintf_rewrite_tail (out, len, " ");
972 ralloc_asprintf_rewrite_tail (out, len, "<<");
975 ralloc_asprintf_rewrite_tail (out, len, ">>");
978 ralloc_asprintf_rewrite_tail (out, len, "<=");
980 case GREATER_OR_EQUAL:
981 ralloc_asprintf_rewrite_tail (out, len, ">=");
984 ralloc_asprintf_rewrite_tail (out, len, "==");
987 ralloc_asprintf_rewrite_tail (out, len, "!=");
990 ralloc_asprintf_rewrite_tail (out, len, "&&");
993 ralloc_asprintf_rewrite_tail (out, len, "||");
996 ralloc_asprintf_rewrite_tail (out, len, "##");
999 ralloc_asprintf_rewrite_tail (out, len, ",");
1002 /* Nothing to print. */
1005 assert(!"Error: Don't know how to print token.");
1010 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1011 * 'token' and 'other'. Note that this function may return 'token' or
1012 * 'other' directly rather than allocating anything new.
1014 * Caution: Only very cursory error-checking is performed to see if
1015 * the final result is a valid single token. */
1017 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1019 token_t *combined = NULL;
1021 /* Pasting a placeholder onto anything makes no change. */
1022 if (other->type == PLACEHOLDER)
1025 /* When 'token' is a placeholder, just return 'other'. */
1026 if (token->type == PLACEHOLDER)
1029 /* A very few single-character punctuators can be combined
1030 * with another to form a multi-character punctuator. */
1031 switch (token->type) {
1033 if (other->type == '<')
1034 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1035 else if (other->type == '=')
1036 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1039 if (other->type == '>')
1040 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1041 else if (other->type == '=')
1042 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1045 if (other->type == '=')
1046 combined = _token_create_ival (token, EQUAL, EQUAL);
1049 if (other->type == '=')
1050 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1053 if (other->type == '&')
1054 combined = _token_create_ival (token, AND, AND);
1057 if (other->type == '|')
1058 combined = _token_create_ival (token, OR, OR);
1062 if (combined != NULL) {
1063 /* Inherit the location from the first token */
1064 combined->location = token->location;
1068 /* Two string-valued (or integer) tokens can usually just be
1069 * mashed together. (We also handle a string followed by an
1070 * integer here as well.)
1072 * There are some exceptions here. Notably, if the first token
1073 * is an integer (or a string representing an integer), then
1074 * the second token must also be an integer or must be a
1075 * string representing an integer that begins with a digit.
1077 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1078 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1083 /* Check that pasting onto an integer doesn't create a
1084 * non-integer, (that is, only digits can be
1086 if (token->type == INTEGER_STRING || token->type == INTEGER)
1088 switch (other->type) {
1089 case INTEGER_STRING:
1090 if (other->value.str[0] < '0' ||
1091 other->value.str[0] > '9')
1095 if (other->value.ival < 0)
1103 if (token->type == INTEGER)
1104 str = ralloc_asprintf (token, "%" PRIiMAX,
1107 str = ralloc_strdup (token, token->value.str);
1110 if (other->type == INTEGER)
1111 ralloc_asprintf_append (&str, "%" PRIiMAX,
1114 ralloc_strcat (&str, other->value.str);
1116 /* New token is same type as original token, unless we
1117 * started with an integer, in which case we will be
1118 * creating an integer-string. */
1119 combined_type = token->type;
1120 if (combined_type == INTEGER)
1121 combined_type = INTEGER_STRING;
1123 combined = _token_create_str (token, combined_type, str);
1124 combined->location = token->location;
1129 glcpp_error (&token->location, parser, "");
1130 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1131 _token_print (&parser->info_log, &parser->info_log_length, token);
1132 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1133 _token_print (&parser->info_log, &parser->info_log_length, other);
1134 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1140 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1147 for (node = list->head; node; node = node->next)
1148 _token_print (&parser->output, &parser->output_length, node->token);
1152 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1154 glcpp_error(locp, parser, "%s", error);
1157 static void add_builtin_define(glcpp_parser_t *parser,
1158 const char *name, int value)
1163 tok = _token_create_ival (parser, INTEGER, value);
1165 list = _token_list_create(parser);
1166 _token_list_append(list, tok);
1167 _define_object_macro(parser, NULL, name, list);
1171 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1173 glcpp_parser_t *parser;
1174 int language_version;
1176 parser = ralloc (NULL, glcpp_parser_t);
1178 glcpp_lex_init_extra (parser, &parser->scanner);
1179 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1180 hash_table_string_compare);
1181 parser->active = NULL;
1182 parser->lexing_if = 0;
1183 parser->space_tokens = 1;
1184 parser->newline_as_space = 0;
1185 parser->in_control_line = 0;
1186 parser->paren_count = 0;
1187 parser->commented_newlines = 0;
1189 parser->skip_stack = NULL;
1191 parser->lex_from_list = NULL;
1192 parser->lex_from_node = NULL;
1194 parser->output = ralloc_strdup(parser, "");
1195 parser->output_length = 0;
1196 parser->info_log = ralloc_strdup(parser, "");
1197 parser->info_log_length = 0;
1200 parser->has_new_line_number = 0;
1201 parser->new_line_number = 1;
1202 parser->has_new_source_number = 0;
1203 parser->new_source_number = 0;
1205 parser->is_gles = false;
1207 /* Add pre-defined macros. */
1208 if (api == API_OPENGLES2) {
1209 parser->is_gles = true;
1210 add_builtin_define(parser, "GL_ES", 1);
1212 if (extensions != NULL) {
1213 if (extensions->OES_EGL_image_external)
1214 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
1217 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1218 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1220 if (extensions != NULL) {
1221 if (extensions->EXT_texture_array) {
1222 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1225 if (extensions->ARB_fragment_coord_conventions)
1226 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1229 if (extensions->ARB_explicit_attrib_location)
1230 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1232 if (extensions->ARB_shader_texture_lod)
1233 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
1235 if (extensions->ARB_draw_instanced)
1236 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
1238 if (extensions->ARB_conservative_depth) {
1239 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1240 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
1243 if (extensions->ARB_shader_bit_encoding)
1244 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
1246 if (extensions->ARB_uniform_buffer_object)
1247 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
1249 if (extensions->ARB_texture_cube_map_array)
1250 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
1252 if (extensions->ARB_shading_language_packing)
1253 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
1255 if (extensions->ARB_texture_multisample)
1256 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
1258 if (extensions->ARB_texture_query_levels)
1259 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
1261 if (extensions->ARB_texture_query_lod)
1262 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
1264 if (extensions->ARB_gpu_shader5)
1265 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
1267 if (extensions->AMD_vertex_shader_layer)
1268 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
1270 if (extensions->ARB_shading_language_420pack)
1271 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
1273 if (extensions->ARB_sample_shading)
1274 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
1276 if (extensions->EXT_shader_integer_mix)
1277 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
1279 if (extensions->ARB_texture_gather)
1280 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
1282 if (extensions->ARB_shader_atomic_counters)
1283 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
1285 if (extensions->AMD_shader_trinary_minmax)
1286 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
1290 language_version = 110;
1291 add_builtin_define(parser, "__VERSION__", language_version);
1297 glcpp_parser_destroy (glcpp_parser_t *parser)
1299 glcpp_lex_destroy (parser->scanner);
1300 hash_table_dtor (parser->defines);
1301 ralloc_free (parser);
1304 typedef enum function_status
1306 FUNCTION_STATUS_SUCCESS,
1307 FUNCTION_NOT_A_FUNCTION,
1308 FUNCTION_UNBALANCED_PARENTHESES
1309 } function_status_t;
1311 /* Find a set of function-like macro arguments by looking for a
1312 * balanced set of parentheses.
1314 * When called, 'node' should be the opening-parenthesis token, (or
1315 * perhaps preceeding SPACE tokens). Upon successful return *last will
1316 * be the last consumed node, (corresponding to the closing right
1321 * FUNCTION_STATUS_SUCCESS:
1323 * Successfully parsed a set of function arguments.
1325 * FUNCTION_NOT_A_FUNCTION:
1327 * Macro name not followed by a '('. This is not an error, but
1328 * simply that the macro name should be treated as a non-macro.
1330 * FUNCTION_UNBALANCED_PARENTHESES
1332 * Macro name is not followed by a balanced set of parentheses.
1334 static function_status_t
1335 _arguments_parse (argument_list_t *arguments,
1337 token_node_t **last)
1339 token_list_t *argument;
1344 /* Ignore whitespace before first parenthesis. */
1345 while (node && node->token->type == SPACE)
1348 if (node == NULL || node->token->type != '(')
1349 return FUNCTION_NOT_A_FUNCTION;
1353 argument = _token_list_create (arguments);
1354 _argument_list_append (arguments, argument);
1356 for (paren_count = 1; node; node = node->next) {
1357 if (node->token->type == '(')
1361 else if (node->token->type == ')')
1364 if (paren_count == 0)
1368 if (node->token->type == ',' &&
1371 _token_list_trim_trailing_space (argument);
1372 argument = _token_list_create (arguments);
1373 _argument_list_append (arguments, argument);
1376 if (argument->head == NULL) {
1377 /* Don't treat initial whitespace as
1378 * part of the arguement. */
1379 if (node->token->type == SPACE)
1382 _token_list_append (argument, node->token);
1387 return FUNCTION_UNBALANCED_PARENTHESES;
1391 return FUNCTION_STATUS_SUCCESS;
1394 static token_list_t *
1395 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1400 list = _token_list_create (ctx);
1401 node = _token_create_ival (list, type, ival);
1402 _token_list_append (list, node);
1407 static token_list_t *
1408 _token_list_create_with_one_space (void *ctx)
1410 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1413 static token_list_t *
1414 _token_list_create_with_one_integer (void *ctx, int ival)
1416 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1419 /* Perform macro expansion on 'list', placing the resulting tokens
1420 * into a new list which is initialized with a first token of type
1421 * 'head_token_type'. Then begin lexing from the resulting list,
1422 * (return to the current lexing source when this list is exhausted).
1425 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1426 int head_token_type,
1429 token_list_t *expanded;
1432 expanded = _token_list_create (parser);
1433 token = _token_create_ival (parser, head_token_type, head_token_type);
1434 _token_list_append (expanded, token);
1435 _glcpp_parser_expand_token_list (parser, list);
1436 _token_list_append_list (expanded, list);
1437 glcpp_parser_lex_from (parser, expanded);
1441 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1448 token_node_t *next_non_space;
1450 /* Look ahead for a PASTE token, skipping space. */
1451 next_non_space = node->next;
1452 while (next_non_space && next_non_space->token->type == SPACE)
1453 next_non_space = next_non_space->next;
1455 if (next_non_space == NULL)
1458 if (next_non_space->token->type != PASTE) {
1459 node = next_non_space;
1463 /* Now find the next non-space token after the PASTE. */
1464 next_non_space = next_non_space->next;
1465 while (next_non_space && next_non_space->token->type == SPACE)
1466 next_non_space = next_non_space->next;
1468 if (next_non_space == NULL) {
1469 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1473 node->token = _token_paste (parser, node->token, next_non_space->token);
1474 node->next = next_non_space->next;
1475 if (next_non_space == list->tail)
1479 list->non_space_tail = list->tail;
1482 /* This is a helper function that's essentially part of the
1483 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1484 * except for by that function.
1486 * Returns NULL if node is a simple token with no expansion, (that is,
1487 * although 'node' corresponds to an identifier defined as a
1488 * function-like macro, it is not followed with a parenthesized
1491 * Compute the complete expansion of node (which is a function-like
1492 * macro) and subsequent nodes which are arguments.
1494 * Returns the token list that results from the expansion and sets
1495 * *last to the last node in the list that was consumed by the
1496 * expansion. Specifically, *last will be set as follows: as the
1497 * token of the closing right parenthesis.
1499 static token_list_t *
1500 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1502 token_node_t **last)
1506 const char *identifier;
1507 argument_list_t *arguments;
1508 function_status_t status;
1509 token_list_t *substituted;
1510 int parameter_index;
1512 identifier = node->token->value.str;
1514 macro = hash_table_find (parser->defines, identifier);
1516 assert (macro->is_function);
1518 arguments = _argument_list_create (parser);
1519 status = _arguments_parse (arguments, node, last);
1522 case FUNCTION_STATUS_SUCCESS:
1524 case FUNCTION_NOT_A_FUNCTION:
1526 case FUNCTION_UNBALANCED_PARENTHESES:
1527 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1531 /* Replace a macro defined as empty with a SPACE token. */
1532 if (macro->replacements == NULL) {
1533 ralloc_free (arguments);
1534 return _token_list_create_with_one_space (parser);
1537 if (! ((_argument_list_length (arguments) ==
1538 _string_list_length (macro->parameters)) ||
1539 (_string_list_length (macro->parameters) == 0 &&
1540 _argument_list_length (arguments) == 1 &&
1541 arguments->head->argument->head == NULL)))
1543 glcpp_error (&node->token->location, parser,
1544 "Error: macro %s invoked with %d arguments (expected %d)\n",
1546 _argument_list_length (arguments),
1547 _string_list_length (macro->parameters));
1551 /* Perform argument substitution on the replacement list. */
1552 substituted = _token_list_create (arguments);
1554 for (node = macro->replacements->head; node; node = node->next)
1556 if (node->token->type == IDENTIFIER &&
1557 _string_list_contains (macro->parameters,
1558 node->token->value.str,
1561 token_list_t *argument;
1562 argument = _argument_list_member_at (arguments,
1564 /* Before substituting, we expand the argument
1565 * tokens, or append a placeholder token for
1566 * an empty argument. */
1567 if (argument->head) {
1568 token_list_t *expanded_argument;
1569 expanded_argument = _token_list_copy (parser,
1571 _glcpp_parser_expand_token_list (parser,
1573 _token_list_append_list (substituted,
1578 new_token = _token_create_ival (substituted,
1581 _token_list_append (substituted, new_token);
1584 _token_list_append (substituted, node->token);
1588 /* After argument substitution, and before further expansion
1589 * below, implement token pasting. */
1591 _token_list_trim_trailing_space (substituted);
1593 _glcpp_parser_apply_pastes (parser, substituted);
1598 /* Compute the complete expansion of node, (and subsequent nodes after
1599 * 'node' in the case that 'node' is a function-like macro and
1600 * subsequent nodes are arguments).
1602 * Returns NULL if node is a simple token with no expansion.
1604 * Otherwise, returns the token list that results from the expansion
1605 * and sets *last to the last node in the list that was consumed by
1606 * the expansion. Specifically, *last will be set as follows:
1608 * As 'node' in the case of object-like macro expansion.
1610 * As the token of the closing right parenthesis in the case of
1611 * function-like macro expansion.
1613 static token_list_t *
1614 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1616 token_node_t **last)
1618 token_t *token = node->token;
1619 const char *identifier;
1622 /* We only expand identifiers */
1623 if (token->type != IDENTIFIER) {
1624 /* We change any COMMA into a COMMA_FINAL to prevent
1625 * it being mistaken for an argument separator
1627 if (token->type == ',') {
1628 token->type = COMMA_FINAL;
1629 token->value.ival = COMMA_FINAL;
1636 identifier = token->value.str;
1638 /* Special handling for __LINE__ and __FILE__, (not through
1639 * the hash table). */
1640 if (strcmp(identifier, "__LINE__") == 0)
1641 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1643 if (strcmp(identifier, "__FILE__") == 0)
1644 return _token_list_create_with_one_integer (parser, node->token->location.source);
1646 /* Look up this identifier in the hash table. */
1647 macro = hash_table_find (parser->defines, identifier);
1649 /* Not a macro, so no expansion needed. */
1653 /* Finally, don't expand this macro if we're already actively
1654 * expanding it, (to avoid infinite recursion). */
1655 if (_parser_active_list_contains (parser, identifier)) {
1656 /* We change the token type here from IDENTIFIER to
1657 * OTHER to prevent any future expansion of this
1658 * unexpanded token. */
1660 token_list_t *expansion;
1663 str = ralloc_strdup (parser, token->value.str);
1664 final = _token_create_str (parser, OTHER, str);
1665 expansion = _token_list_create (parser);
1666 _token_list_append (expansion, final);
1670 if (! macro->is_function)
1672 token_list_t *replacement;
1674 /* Replace a macro defined as empty with a SPACE token. */
1675 if (macro->replacements == NULL)
1676 return _token_list_create_with_one_space (parser);
1678 replacement = _token_list_copy (parser, macro->replacements);
1679 _glcpp_parser_apply_pastes (parser, replacement);
1683 return _glcpp_parser_expand_function (parser, node, last);
1686 /* Push a new identifier onto the parser's active list.
1688 * Here, 'marker' is the token node that appears in the list after the
1689 * expansion of 'identifier'. That is, when the list iterator begins
1690 * examining 'marker', then it is time to pop this node from the
1694 _parser_active_list_push (glcpp_parser_t *parser,
1695 const char *identifier,
1696 token_node_t *marker)
1698 active_list_t *node;
1700 node = ralloc (parser->active, active_list_t);
1701 node->identifier = ralloc_strdup (node, identifier);
1702 node->marker = marker;
1703 node->next = parser->active;
1705 parser->active = node;
1709 _parser_active_list_pop (glcpp_parser_t *parser)
1711 active_list_t *node = parser->active;
1714 parser->active = NULL;
1718 node = parser->active->next;
1719 ralloc_free (parser->active);
1721 parser->active = node;
1725 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1727 active_list_t *node;
1729 if (parser->active == NULL)
1732 for (node = parser->active; node; node = node->next)
1733 if (strcmp (node->identifier, identifier) == 0)
1739 /* Walk over the token list replacing nodes with their expansion.
1740 * Whenever nodes are expanded the walking will walk over the new
1741 * nodes, continuing to expand as necessary. The results are placed in
1745 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1748 token_node_t *node_prev;
1749 token_node_t *node, *last = NULL;
1750 token_list_t *expansion;
1751 active_list_t *active_initial = parser->active;
1756 _token_list_trim_trailing_space (list);
1763 while (parser->active && parser->active->marker == node)
1764 _parser_active_list_pop (parser);
1766 expansion = _glcpp_parser_expand_node (parser, node, &last);
1770 for (n = node; n != last->next; n = n->next)
1771 while (parser->active &&
1772 parser->active->marker == n)
1774 _parser_active_list_pop (parser);
1777 _parser_active_list_push (parser,
1778 node->token->value.str,
1781 /* Splice expansion into list, supporting a
1782 * simple deletion if the expansion is
1784 if (expansion->head) {
1786 node_prev->next = expansion->head;
1788 list->head = expansion->head;
1789 expansion->tail->next = last->next;
1790 if (last == list->tail)
1791 list->tail = expansion->tail;
1794 node_prev->next = last->next;
1796 list->head = last->next;
1797 if (last == list->tail)
1803 node = node_prev ? node_prev->next : list->head;
1806 /* Remove any lingering effects of this invocation on the
1807 * active list. That is, pop until the list looks like it did
1808 * at the beginning of this function. */
1809 while (parser->active && parser->active != active_initial)
1810 _parser_active_list_pop (parser);
1812 list->non_space_tail = list->tail;
1816 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1822 _glcpp_parser_expand_token_list (parser, list);
1824 _token_list_trim_trailing_space (list);
1826 _token_list_print (parser, list);
1830 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1831 const char *identifier)
1833 /* According to the GLSL specification, macro names starting with "__"
1834 * or "GL_" are reserved for future use. So, don't allow them.
1836 if (strstr(identifier, "__")) {
1837 glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
1839 if (strncmp(identifier, "GL_", 3) == 0) {
1840 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1845 _macro_equal (macro_t *a, macro_t *b)
1847 if (a->is_function != b->is_function)
1850 if (a->is_function) {
1851 if (! _string_list_equal (a->parameters, b->parameters))
1855 return _token_list_equal_ignoring_space (a->replacements,
1860 _define_object_macro (glcpp_parser_t *parser,
1862 const char *identifier,
1863 token_list_t *replacements)
1865 macro_t *macro, *previous;
1868 _check_for_reserved_macro_name(parser, loc, identifier);
1870 macro = ralloc (parser, macro_t);
1872 macro->is_function = 0;
1873 macro->parameters = NULL;
1874 macro->identifier = ralloc_strdup (macro, identifier);
1875 macro->replacements = replacements;
1876 ralloc_steal (macro, replacements);
1878 previous = hash_table_find (parser->defines, identifier);
1880 if (_macro_equal (macro, previous)) {
1881 ralloc_free (macro);
1884 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1888 hash_table_insert (parser->defines, macro, identifier);
1892 _define_function_macro (glcpp_parser_t *parser,
1894 const char *identifier,
1895 string_list_t *parameters,
1896 token_list_t *replacements)
1898 macro_t *macro, *previous;
1900 _check_for_reserved_macro_name(parser, loc, identifier);
1902 macro = ralloc (parser, macro_t);
1903 ralloc_steal (macro, parameters);
1904 ralloc_steal (macro, replacements);
1906 macro->is_function = 1;
1907 macro->parameters = parameters;
1908 macro->identifier = ralloc_strdup (macro, identifier);
1909 macro->replacements = replacements;
1910 previous = hash_table_find (parser->defines, identifier);
1912 if (_macro_equal (macro, previous)) {
1913 ralloc_free (macro);
1916 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1920 hash_table_insert (parser->defines, macro, identifier);
1924 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1929 if (parser->lex_from_list == NULL) {
1930 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1932 /* XXX: This ugly block of code exists for the sole
1933 * purpose of converting a NEWLINE token into a SPACE
1934 * token, but only in the case where we have seen a
1935 * function-like macro name, but have not yet seen its
1936 * closing parenthesis.
1938 * There's perhaps a more compact way to do this with
1939 * mid-rule actions in the grammar.
1941 * I'm definitely not pleased with the complexity of
1944 if (parser->newline_as_space)
1947 parser->paren_count++;
1948 } else if (ret == ')') {
1949 parser->paren_count--;
1950 if (parser->paren_count == 0)
1951 parser->newline_as_space = 0;
1952 } else if (ret == NEWLINE) {
1954 } else if (ret != SPACE) {
1955 if (parser->paren_count == 0)
1956 parser->newline_as_space = 0;
1959 else if (parser->in_control_line)
1962 parser->in_control_line = 0;
1964 else if (ret == HASH_DEFINE ||
1965 ret == HASH_UNDEF || ret == HASH_IF ||
1966 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1967 ret == HASH_ELIF || ret == HASH_ELSE ||
1968 ret == HASH_ENDIF || ret == HASH)
1970 parser->in_control_line = 1;
1972 else if (ret == IDENTIFIER)
1975 macro = hash_table_find (parser->defines,
1977 if (macro && macro->is_function) {
1978 parser->newline_as_space = 1;
1979 parser->paren_count = 0;
1986 node = parser->lex_from_node;
1989 ralloc_free (parser->lex_from_list);
1990 parser->lex_from_list = NULL;
1994 *yylval = node->token->value;
1995 ret = node->token->type;
1997 parser->lex_from_node = node->next;
2003 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
2007 assert (parser->lex_from_list == NULL);
2009 /* Copy list, eliminating any space tokens. */
2010 parser->lex_from_list = _token_list_create (parser);
2012 for (node = list->head; node; node = node->next) {
2013 if (node->token->type == SPACE)
2015 _token_list_append (parser->lex_from_list, node->token);
2020 parser->lex_from_node = parser->lex_from_list->head;
2022 /* It's possible the list consisted of nothing but whitespace. */
2023 if (parser->lex_from_node == NULL) {
2024 ralloc_free (parser->lex_from_list);
2025 parser->lex_from_list = NULL;
2030 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
2033 skip_type_t current = SKIP_NO_SKIP;
2036 if (parser->skip_stack)
2037 current = parser->skip_stack->type;
2039 node = ralloc (parser, skip_node_t);
2042 if (current == SKIP_NO_SKIP) {
2044 node->type = SKIP_NO_SKIP;
2046 node->type = SKIP_TO_ELSE;
2048 node->type = SKIP_TO_ENDIF;
2051 node->has_else = false;
2052 node->next = parser->skip_stack;
2053 parser->skip_stack = node;
2057 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
2058 const char *type, int condition)
2060 if (parser->skip_stack == NULL) {
2061 glcpp_error (loc, parser, "%s without #if\n", type);
2065 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2067 parser->skip_stack->type = SKIP_NO_SKIP;
2069 parser->skip_stack->type = SKIP_TO_ENDIF;
2074 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
2078 if (parser->skip_stack == NULL) {
2079 glcpp_error (loc, parser, "#endif without #if\n");
2083 node = parser->skip_stack;
2084 parser->skip_stack = node->next;
2089 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2090 const char *es_identifier)
2092 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
2094 hash_table_remove (parser->defines, "__VERSION__");
2095 ralloc_free (macro);
2097 add_builtin_define (parser, "__VERSION__", version);
2099 /* If we didn't have a GLES context to begin with, (indicated
2100 * by parser->api), then the version declaration here might
2102 if (! parser->is_gles &&
2104 (es_identifier && (strcmp(es_identifier, "es") == 0))))
2106 parser->is_gles = true;
2107 add_builtin_define (parser, "GL_ES", 1);
2111 add_builtin_define(parser, "GL_core_profile", 1);
2113 /* Currently, all ES2/ES3 implementations support highp in the
2114 * fragment shader, so we always define this macro in ES2/ES3.
2115 * If we ever get a driver that doesn't support highp, we'll
2116 * need to add a flag to the gl_context and check that here.
2118 if (version >= 130 || parser->is_gles)
2119 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2121 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
2122 "#version %" PRIiMAX "%s%s", version,
2123 es_identifier ? " " : "",
2124 es_identifier ? es_identifier : "");