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);
136 #define yylex glcpp_parser_lex
139 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
142 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
145 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
161 %parse-param {glcpp_parser_t *parser}
162 %lex-param {glcpp_parser_t *parser}
165 %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ 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
167 %type <ival> expression INTEGER operator SPACE integer_constant
168 %type <str> IDENTIFIER INTEGER_STRING OTHER
169 %type <string_list> identifier_list
170 %type <token> preprocessing_token conditional_token
171 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
177 %left EQUAL NOT_EQUAL
178 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
179 %left LEFT_SHIFT RIGHT_SHIFT
193 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
196 _glcpp_parser_print_expanded_token_list (parser, $1);
197 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
205 IF_EXPANDED expression NEWLINE {
206 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
208 | ELIF_EXPANDED expression NEWLINE {
209 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
211 | LINE_EXPANDED integer_constant NEWLINE {
212 parser->has_new_line_number = 1;
213 parser->new_line_number = $2;
214 ralloc_asprintf_rewrite_tail (&parser->output,
215 &parser->output_length,
219 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
220 parser->has_new_line_number = 1;
221 parser->new_line_number = $2;
222 parser->has_new_source_number = 1;
223 parser->new_source_number = $3;
224 ralloc_asprintf_rewrite_tail (&parser->output,
225 &parser->output_length,
226 "#line %" PRIiMAX " %" PRIiMAX,
232 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
233 _define_object_macro (parser, & @2, $2, $3);
235 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
236 _define_function_macro (parser, & @2, $2, NULL, $5);
238 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
239 _define_function_macro (parser, & @2, $2, $4, $6);
241 | HASH_UNDEF IDENTIFIER NEWLINE {
242 macro_t *macro = hash_table_find (parser->defines, $2);
244 hash_table_remove (parser->defines, $2);
249 | HASH_LINE pp_tokens NEWLINE {
250 if (parser->skip_stack == NULL ||
251 parser->skip_stack->type == SKIP_NO_SKIP)
253 _glcpp_parser_expand_and_lex_from (parser,
257 | HASH_IF conditional_tokens NEWLINE {
258 /* Be careful to only evaluate the 'if' expression if
259 * we are not skipping. When we are skipping, we
260 * simply push a new 0-valued 'if' onto the skip
263 * This avoids generating diagnostics for invalid
264 * expressions that are being skipped. */
265 if (parser->skip_stack == NULL ||
266 parser->skip_stack->type == SKIP_NO_SKIP)
268 _glcpp_parser_expand_and_lex_from (parser,
273 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
274 parser->skip_stack->type = SKIP_TO_ENDIF;
278 /* #if without an expression is only an error if we
279 * are not skipping */
280 if (parser->skip_stack == NULL ||
281 parser->skip_stack->type == SKIP_NO_SKIP)
283 glcpp_error(& @1, parser, "#if with no expression");
285 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
287 | HASH_IFDEF IDENTIFIER junk NEWLINE {
288 macro_t *macro = hash_table_find (parser->defines, $2);
290 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
292 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
293 macro_t *macro = hash_table_find (parser->defines, $2);
295 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
297 | HASH_ELIF conditional_tokens NEWLINE {
298 /* Be careful to only evaluate the 'elif' expression
299 * if we are not skipping. When we are skipping, we
300 * simply change to a 0-valued 'elif' on the skip
303 * This avoids generating diagnostics for invalid
304 * expressions that are being skipped. */
305 if (parser->skip_stack &&
306 parser->skip_stack->type == SKIP_TO_ELSE)
308 _glcpp_parser_expand_and_lex_from (parser,
313 _glcpp_parser_skip_stack_change_if (parser, & @1,
317 | HASH_ELIF NEWLINE {
318 /* #elif without an expression is an error unless we
320 if (parser->skip_stack &&
321 parser->skip_stack->type == SKIP_TO_ELSE)
323 glcpp_error(& @1, parser, "#elif with no expression");
327 _glcpp_parser_skip_stack_change_if (parser, & @1,
329 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
332 | HASH_ELSE NEWLINE {
333 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
335 | HASH_ENDIF NEWLINE {
336 _glcpp_parser_skip_stack_pop (parser, & @1);
338 | HASH_VERSION integer_constant NEWLINE {
339 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
341 hash_table_remove (parser->defines, "__VERSION__");
344 add_builtin_define (parser, "__VERSION__", $2);
347 add_builtin_define (parser, "GL_ES", 1);
349 /* Currently, all ES2 implementations support highp in the
350 * fragment shader, so we always define this macro in ES2.
351 * If we ever get a driver that doesn't support highp, we'll
352 * need to add a flag to the gl_context and check that here.
354 if ($2 >= 130 || $2 == 100)
355 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
357 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#version %" PRIiMAX, $2);
364 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
365 $$ = strtoll ($1 + 2, NULL, 16);
366 } else if ($1[0] == '0') {
367 $$ = strtoll ($1, NULL, 8);
369 $$ = strtoll ($1, NULL, 10);
381 | expression OR expression {
384 | expression AND expression {
387 | expression '|' expression {
390 | expression '^' expression {
393 | expression '&' expression {
396 | expression NOT_EQUAL expression {
399 | expression EQUAL expression {
402 | expression GREATER_OR_EQUAL expression {
405 | expression LESS_OR_EQUAL expression {
408 | expression '>' expression {
411 | expression '<' expression {
414 | expression RIGHT_SHIFT expression {
417 | expression LEFT_SHIFT expression {
420 | expression '-' expression {
423 | expression '+' expression {
426 | expression '%' expression {
428 yyerror (& @1, parser,
429 "zero modulus in preprocessor directive");
434 | expression '/' expression {
436 yyerror (& @1, parser,
437 "division by 0 in preprocessor directive");
442 | expression '*' expression {
445 | '!' expression %prec UNARY {
448 | '~' expression %prec UNARY {
451 | '-' expression %prec UNARY {
454 | '+' expression %prec UNARY {
457 | '(' expression ')' {
464 $$ = _string_list_create (parser);
465 _string_list_append_item ($$, $1);
466 ralloc_steal ($$, $1);
468 | identifier_list ',' IDENTIFIER {
470 _string_list_append_item ($$, $3);
471 ralloc_steal ($$, $3);
476 NEWLINE { $$ = NULL; }
482 yyerror (& @1, parser, "Invalid tokens after #");
487 /* empty */ { $$ = NULL; }
494 glcpp_warning(&@1, parser, "extra tokens at end of directive");
499 /* Handle "defined" operator */
501 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
502 $$ = _token_create_ival (parser, INTEGER, v);
504 | DEFINED '(' IDENTIFIER ')' {
505 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
506 $$ = _token_create_ival (parser, INTEGER, v);
508 | preprocessing_token
512 /* Exactly the same as pp_tokens, but using conditional_token */
514 $$ = _token_list_create (parser);
515 _token_list_append ($$, $1);
517 | conditional_tokens conditional_token {
519 _token_list_append ($$, $2);
524 preprocessing_token {
525 parser->space_tokens = 1;
526 $$ = _token_list_create (parser);
527 _token_list_append ($$, $1);
529 | pp_tokens preprocessing_token {
531 _token_list_append ($$, $2);
537 $$ = _token_create_str (parser, IDENTIFIER, $1);
538 $$->location = yylloc;
541 $$ = _token_create_str (parser, INTEGER_STRING, $1);
542 $$->location = yylloc;
545 $$ = _token_create_ival (parser, $1, $1);
546 $$->location = yylloc;
549 $$ = _token_create_str (parser, OTHER, $1);
550 $$->location = yylloc;
553 $$ = _token_create_ival (parser, SPACE, SPACE);
554 $$->location = yylloc;
574 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
575 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
578 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
579 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
580 | EQUAL { $$ = EQUAL; }
581 | NOT_EQUAL { $$ = NOT_EQUAL; }
589 | PASTE { $$ = PASTE; }
595 _string_list_create (void *ctx)
599 list = ralloc (ctx, string_list_t);
607 _string_list_append_item (string_list_t *list, const char *str)
611 node = ralloc (list, string_node_t);
612 node->str = ralloc_strdup (node, str);
616 if (list->head == NULL) {
619 list->tail->next = node;
626 _string_list_contains (string_list_t *list, const char *member, int *index)
634 for (i = 0, node = list->head; node; i++, node = node->next) {
635 if (strcmp (node->str, member) == 0) {
646 _string_list_length (string_list_t *list)
654 for (node = list->head; node; node = node->next)
661 _string_list_equal (string_list_t *a, string_list_t *b)
663 string_node_t *node_a, *node_b;
665 if (a == NULL && b == NULL)
668 if (a == NULL || b == NULL)
671 for (node_a = a->head, node_b = b->head;
673 node_a = node_a->next, node_b = node_b->next)
675 if (strcmp (node_a->str, node_b->str))
679 /* Catch the case of lists being different lengths, (which
680 * would cause the loop above to terminate after the shorter
682 return node_a == node_b;
686 _argument_list_create (void *ctx)
688 argument_list_t *list;
690 list = ralloc (ctx, argument_list_t);
698 _argument_list_append (argument_list_t *list, token_list_t *argument)
700 argument_node_t *node;
702 node = ralloc (list, argument_node_t);
703 node->argument = argument;
707 if (list->head == NULL) {
710 list->tail->next = node;
717 _argument_list_length (argument_list_t *list)
720 argument_node_t *node;
725 for (node = list->head; node; node = node->next)
732 _argument_list_member_at (argument_list_t *list, int index)
734 argument_node_t *node;
741 for (i = 0; i < index; i++) {
748 return node->argument;
753 /* Note: This function ralloc_steal()s the str pointer. */
755 _token_create_str (void *ctx, int type, char *str)
759 token = ralloc (ctx, token_t);
761 token->value.str = str;
763 ralloc_steal (token, str);
769 _token_create_ival (void *ctx, int type, int ival)
773 token = ralloc (ctx, token_t);
775 token->value.ival = ival;
781 _token_list_create (void *ctx)
785 list = ralloc (ctx, token_list_t);
788 list->non_space_tail = NULL;
794 _token_list_append (token_list_t *list, token_t *token)
798 node = ralloc (list, token_node_t);
802 if (list->head == NULL) {
805 list->tail->next = node;
809 if (token->type != SPACE)
810 list->non_space_tail = node;
814 _token_list_append_list (token_list_t *list, token_list_t *tail)
816 if (tail == NULL || tail->head == NULL)
819 if (list->head == NULL) {
820 list->head = tail->head;
822 list->tail->next = tail->head;
825 list->tail = tail->tail;
826 list->non_space_tail = tail->non_space_tail;
829 static token_list_t *
830 _token_list_copy (void *ctx, token_list_t *other)
838 copy = _token_list_create (ctx);
839 for (node = other->head; node; node = node->next) {
840 token_t *new_token = ralloc (copy, token_t);
841 *new_token = *node->token;
842 _token_list_append (copy, new_token);
849 _token_list_trim_trailing_space (token_list_t *list)
851 token_node_t *tail, *next;
853 if (list->non_space_tail) {
854 tail = list->non_space_tail->next;
855 list->non_space_tail->next = NULL;
856 list->tail = list->non_space_tail;
867 _token_list_is_empty_ignoring_space (token_list_t *l)
875 while (n != NULL && n->token->type == SPACE)
882 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
884 token_node_t *node_a, *node_b;
886 if (a == NULL || b == NULL) {
887 int a_empty = _token_list_is_empty_ignoring_space(a);
888 int b_empty = _token_list_is_empty_ignoring_space(b);
889 return a_empty == b_empty;
897 if (node_a == NULL && node_b == NULL)
900 if (node_a == NULL || node_b == NULL)
903 if (node_a->token->type == SPACE) {
904 node_a = node_a->next;
908 if (node_b->token->type == SPACE) {
909 node_b = node_b->next;
913 if (node_a->token->type != node_b->token->type)
916 switch (node_a->token->type) {
918 if (node_a->token->value.ival !=
919 node_b->token->value.ival)
927 if (strcmp (node_a->token->value.str,
928 node_b->token->value.str))
935 node_a = node_a->next;
936 node_b = node_b->next;
943 _token_print (char **out, size_t *len, token_t *token)
945 if (token->type < 256) {
946 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
950 switch (token->type) {
952 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
957 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
960 ralloc_asprintf_rewrite_tail (out, len, " ");
963 ralloc_asprintf_rewrite_tail (out, len, "<<");
966 ralloc_asprintf_rewrite_tail (out, len, ">>");
969 ralloc_asprintf_rewrite_tail (out, len, "<=");
971 case GREATER_OR_EQUAL:
972 ralloc_asprintf_rewrite_tail (out, len, ">=");
975 ralloc_asprintf_rewrite_tail (out, len, "==");
978 ralloc_asprintf_rewrite_tail (out, len, "!=");
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 /* Nothing to print. */
996 assert(!"Error: Don't know how to print token.");
1001 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1002 * 'token' and 'other'. Note that this function may return 'token' or
1003 * 'other' directly rather than allocating anything new.
1005 * Caution: Only very cursory error-checking is performed to see if
1006 * the final result is a valid single token. */
1008 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1010 token_t *combined = NULL;
1012 /* Pasting a placeholder onto anything makes no change. */
1013 if (other->type == PLACEHOLDER)
1016 /* When 'token' is a placeholder, just return 'other'. */
1017 if (token->type == PLACEHOLDER)
1020 /* A very few single-character punctuators can be combined
1021 * with another to form a multi-character punctuator. */
1022 switch (token->type) {
1024 if (other->type == '<')
1025 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1026 else if (other->type == '=')
1027 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1030 if (other->type == '>')
1031 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1032 else if (other->type == '=')
1033 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1036 if (other->type == '=')
1037 combined = _token_create_ival (token, EQUAL, EQUAL);
1040 if (other->type == '=')
1041 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1044 if (other->type == '&')
1045 combined = _token_create_ival (token, AND, AND);
1048 if (other->type == '|')
1049 combined = _token_create_ival (token, OR, OR);
1053 if (combined != NULL) {
1054 /* Inherit the location from the first token */
1055 combined->location = token->location;
1059 /* Two string-valued tokens can usually just be mashed
1062 * XXX: This isn't actually legitimate. Several things here
1063 * should result in a diagnostic since the result cannot be a
1064 * valid, single pre-processing token. For example, pasting
1065 * "123" and "abc" is not legal, but we don't catch that
1067 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1068 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1072 str = ralloc_asprintf (token, "%s%s", token->value.str,
1074 combined = _token_create_str (token, token->type, str);
1075 combined->location = token->location;
1079 glcpp_error (&token->location, parser, "");
1080 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1081 _token_print (&parser->info_log, &parser->info_log_length, token);
1082 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1083 _token_print (&parser->info_log, &parser->info_log_length, other);
1084 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1090 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1097 for (node = list->head; node; node = node->next)
1098 _token_print (&parser->output, &parser->output_length, node->token);
1102 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1104 glcpp_error(locp, parser, "%s", error);
1107 static void add_builtin_define(glcpp_parser_t *parser,
1108 const char *name, int value)
1113 tok = _token_create_ival (parser, INTEGER, value);
1115 list = _token_list_create(parser);
1116 _token_list_append(list, tok);
1117 _define_object_macro(parser, NULL, name, list);
1121 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1123 glcpp_parser_t *parser;
1124 int language_version;
1126 parser = ralloc (NULL, glcpp_parser_t);
1128 glcpp_lex_init_extra (parser, &parser->scanner);
1129 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1130 hash_table_string_compare);
1131 parser->active = NULL;
1132 parser->lexing_if = 0;
1133 parser->space_tokens = 1;
1134 parser->newline_as_space = 0;
1135 parser->in_control_line = 0;
1136 parser->paren_count = 0;
1138 parser->skip_stack = NULL;
1140 parser->lex_from_list = NULL;
1141 parser->lex_from_node = NULL;
1143 parser->output = ralloc_strdup(parser, "");
1144 parser->output_length = 0;
1145 parser->info_log = ralloc_strdup(parser, "");
1146 parser->info_log_length = 0;
1149 parser->has_new_line_number = 0;
1150 parser->new_line_number = 1;
1151 parser->has_new_source_number = 0;
1152 parser->new_source_number = 0;
1154 /* Add pre-defined macros. */
1155 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1156 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1158 if (api == API_OPENGLES2)
1159 add_builtin_define(parser, "GL_ES", 1);
1161 if (extensions != NULL) {
1162 if (extensions->EXT_texture_array) {
1163 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1166 if (extensions->ARB_fragment_coord_conventions)
1167 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1170 if (extensions->ARB_explicit_attrib_location)
1171 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1173 if (extensions->ARB_shader_texture_lod)
1174 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
1176 if (extensions->ARB_draw_instanced)
1177 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
1179 if (extensions->ARB_conservative_depth) {
1180 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1181 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
1184 if (extensions->OES_EGL_image_external)
1185 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
1187 if (extensions->ARB_shader_bit_encoding)
1188 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
1190 if (extensions->ARB_uniform_buffer_object)
1191 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
1194 language_version = 110;
1195 add_builtin_define(parser, "__VERSION__", language_version);
1201 glcpp_parser_parse (glcpp_parser_t *parser)
1203 return yyparse (parser);
1207 glcpp_parser_destroy (glcpp_parser_t *parser)
1209 glcpp_lex_destroy (parser->scanner);
1210 hash_table_dtor (parser->defines);
1211 ralloc_free (parser);
1214 typedef enum function_status
1216 FUNCTION_STATUS_SUCCESS,
1217 FUNCTION_NOT_A_FUNCTION,
1218 FUNCTION_UNBALANCED_PARENTHESES
1219 } function_status_t;
1221 /* Find a set of function-like macro arguments by looking for a
1222 * balanced set of parentheses.
1224 * When called, 'node' should be the opening-parenthesis token, (or
1225 * perhaps preceeding SPACE tokens). Upon successful return *last will
1226 * be the last consumed node, (corresponding to the closing right
1231 * FUNCTION_STATUS_SUCCESS:
1233 * Successfully parsed a set of function arguments.
1235 * FUNCTION_NOT_A_FUNCTION:
1237 * Macro name not followed by a '('. This is not an error, but
1238 * simply that the macro name should be treated as a non-macro.
1240 * FUNCTION_UNBALANCED_PARENTHESES
1242 * Macro name is not followed by a balanced set of parentheses.
1244 static function_status_t
1245 _arguments_parse (argument_list_t *arguments,
1247 token_node_t **last)
1249 token_list_t *argument;
1254 /* Ignore whitespace before first parenthesis. */
1255 while (node && node->token->type == SPACE)
1258 if (node == NULL || node->token->type != '(')
1259 return FUNCTION_NOT_A_FUNCTION;
1263 argument = _token_list_create (arguments);
1264 _argument_list_append (arguments, argument);
1266 for (paren_count = 1; node; node = node->next) {
1267 if (node->token->type == '(')
1271 else if (node->token->type == ')')
1274 if (paren_count == 0)
1278 if (node->token->type == ',' &&
1281 _token_list_trim_trailing_space (argument);
1282 argument = _token_list_create (arguments);
1283 _argument_list_append (arguments, argument);
1286 if (argument->head == NULL) {
1287 /* Don't treat initial whitespace as
1288 * part of the arguement. */
1289 if (node->token->type == SPACE)
1292 _token_list_append (argument, node->token);
1297 return FUNCTION_UNBALANCED_PARENTHESES;
1301 return FUNCTION_STATUS_SUCCESS;
1304 static token_list_t *
1305 _token_list_create_with_one_space (void *ctx)
1310 list = _token_list_create (ctx);
1311 space = _token_create_ival (list, SPACE, SPACE);
1312 _token_list_append (list, space);
1317 /* Perform macro expansion on 'list', placing the resulting tokens
1318 * into a new list which is initialized with a first token of type
1319 * 'head_token_type'. Then begin lexing from the resulting list,
1320 * (return to the current lexing source when this list is exhausted).
1323 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1324 int head_token_type,
1327 token_list_t *expanded;
1330 expanded = _token_list_create (parser);
1331 token = _token_create_ival (parser, head_token_type, head_token_type);
1332 _token_list_append (expanded, token);
1333 _glcpp_parser_expand_token_list (parser, list);
1334 _token_list_append_list (expanded, list);
1335 glcpp_parser_lex_from (parser, expanded);
1339 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1346 token_node_t *next_non_space;
1348 /* Look ahead for a PASTE token, skipping space. */
1349 next_non_space = node->next;
1350 while (next_non_space && next_non_space->token->type == SPACE)
1351 next_non_space = next_non_space->next;
1353 if (next_non_space == NULL)
1356 if (next_non_space->token->type != PASTE) {
1357 node = next_non_space;
1361 /* Now find the next non-space token after the PASTE. */
1362 next_non_space = next_non_space->next;
1363 while (next_non_space && next_non_space->token->type == SPACE)
1364 next_non_space = next_non_space->next;
1366 if (next_non_space == NULL) {
1367 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1371 node->token = _token_paste (parser, node->token, next_non_space->token);
1372 node->next = next_non_space->next;
1373 if (next_non_space == list->tail)
1377 list->non_space_tail = list->tail;
1380 /* This is a helper function that's essentially part of the
1381 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1382 * except for by that function.
1384 * Returns NULL if node is a simple token with no expansion, (that is,
1385 * although 'node' corresponds to an identifier defined as a
1386 * function-like macro, it is not followed with a parenthesized
1389 * Compute the complete expansion of node (which is a function-like
1390 * macro) and subsequent nodes which are arguments.
1392 * Returns the token list that results from the expansion and sets
1393 * *last to the last node in the list that was consumed by the
1394 * expansion. Specifically, *last will be set as follows: as the
1395 * token of the closing right parenthesis.
1397 static token_list_t *
1398 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1400 token_node_t **last)
1404 const char *identifier;
1405 argument_list_t *arguments;
1406 function_status_t status;
1407 token_list_t *substituted;
1408 int parameter_index;
1410 identifier = node->token->value.str;
1412 macro = hash_table_find (parser->defines, identifier);
1414 assert (macro->is_function);
1416 arguments = _argument_list_create (parser);
1417 status = _arguments_parse (arguments, node, last);
1420 case FUNCTION_STATUS_SUCCESS:
1422 case FUNCTION_NOT_A_FUNCTION:
1424 case FUNCTION_UNBALANCED_PARENTHESES:
1425 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1429 /* Replace a macro defined as empty with a SPACE token. */
1430 if (macro->replacements == NULL) {
1431 ralloc_free (arguments);
1432 return _token_list_create_with_one_space (parser);
1435 if (! ((_argument_list_length (arguments) ==
1436 _string_list_length (macro->parameters)) ||
1437 (_string_list_length (macro->parameters) == 0 &&
1438 _argument_list_length (arguments) == 1 &&
1439 arguments->head->argument->head == NULL)))
1441 glcpp_error (&node->token->location, parser,
1442 "Error: macro %s invoked with %d arguments (expected %d)\n",
1444 _argument_list_length (arguments),
1445 _string_list_length (macro->parameters));
1449 /* Perform argument substitution on the replacement list. */
1450 substituted = _token_list_create (arguments);
1452 for (node = macro->replacements->head; node; node = node->next)
1454 if (node->token->type == IDENTIFIER &&
1455 _string_list_contains (macro->parameters,
1456 node->token->value.str,
1459 token_list_t *argument;
1460 argument = _argument_list_member_at (arguments,
1462 /* Before substituting, we expand the argument
1463 * tokens, or append a placeholder token for
1464 * an empty argument. */
1465 if (argument->head) {
1466 token_list_t *expanded_argument;
1467 expanded_argument = _token_list_copy (parser,
1469 _glcpp_parser_expand_token_list (parser,
1471 _token_list_append_list (substituted,
1476 new_token = _token_create_ival (substituted,
1479 _token_list_append (substituted, new_token);
1482 _token_list_append (substituted, node->token);
1486 /* After argument substitution, and before further expansion
1487 * below, implement token pasting. */
1489 _token_list_trim_trailing_space (substituted);
1491 _glcpp_parser_apply_pastes (parser, substituted);
1496 /* Compute the complete expansion of node, (and subsequent nodes after
1497 * 'node' in the case that 'node' is a function-like macro and
1498 * subsequent nodes are arguments).
1500 * Returns NULL if node is a simple token with no expansion.
1502 * Otherwise, returns the token list that results from the expansion
1503 * and sets *last to the last node in the list that was consumed by
1504 * the expansion. Specifically, *last will be set as follows:
1506 * As 'node' in the case of object-like macro expansion.
1508 * As the token of the closing right parenthesis in the case of
1509 * function-like macro expansion.
1511 static token_list_t *
1512 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1514 token_node_t **last)
1516 token_t *token = node->token;
1517 const char *identifier;
1520 /* We only expand identifiers */
1521 if (token->type != IDENTIFIER) {
1522 /* We change any COMMA into a COMMA_FINAL to prevent
1523 * it being mistaken for an argument separator
1525 if (token->type == ',') {
1526 token->type = COMMA_FINAL;
1527 token->value.ival = COMMA_FINAL;
1533 /* Look up this identifier in the hash table. */
1534 identifier = token->value.str;
1535 macro = hash_table_find (parser->defines, identifier);
1537 /* Not a macro, so no expansion needed. */
1541 /* Finally, don't expand this macro if we're already actively
1542 * expanding it, (to avoid infinite recursion). */
1543 if (_parser_active_list_contains (parser, identifier)) {
1544 /* We change the token type here from IDENTIFIER to
1545 * OTHER to prevent any future expansion of this
1546 * unexpanded token. */
1548 token_list_t *expansion;
1551 str = ralloc_strdup (parser, token->value.str);
1552 final = _token_create_str (parser, OTHER, str);
1553 expansion = _token_list_create (parser);
1554 _token_list_append (expansion, final);
1559 if (! macro->is_function)
1561 token_list_t *replacement;
1564 /* Replace a macro defined as empty with a SPACE token. */
1565 if (macro->replacements == NULL)
1566 return _token_list_create_with_one_space (parser);
1568 replacement = _token_list_copy (parser, macro->replacements);
1569 _glcpp_parser_apply_pastes (parser, replacement);
1573 return _glcpp_parser_expand_function (parser, node, last);
1576 /* Push a new identifier onto the parser's active list.
1578 * Here, 'marker' is the token node that appears in the list after the
1579 * expansion of 'identifier'. That is, when the list iterator begins
1580 * examining 'marker', then it is time to pop this node from the
1584 _parser_active_list_push (glcpp_parser_t *parser,
1585 const char *identifier,
1586 token_node_t *marker)
1588 active_list_t *node;
1590 node = ralloc (parser->active, active_list_t);
1591 node->identifier = ralloc_strdup (node, identifier);
1592 node->marker = marker;
1593 node->next = parser->active;
1595 parser->active = node;
1599 _parser_active_list_pop (glcpp_parser_t *parser)
1601 active_list_t *node = parser->active;
1604 parser->active = NULL;
1608 node = parser->active->next;
1609 ralloc_free (parser->active);
1611 parser->active = node;
1615 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1617 active_list_t *node;
1619 if (parser->active == NULL)
1622 for (node = parser->active; node; node = node->next)
1623 if (strcmp (node->identifier, identifier) == 0)
1629 /* Walk over the token list replacing nodes with their expansion.
1630 * Whenever nodes are expanded the walking will walk over the new
1631 * nodes, continuing to expand as necessary. The results are placed in
1635 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1638 token_node_t *node_prev;
1639 token_node_t *node, *last = NULL;
1640 token_list_t *expansion;
1641 active_list_t *active_initial = parser->active;
1646 _token_list_trim_trailing_space (list);
1653 while (parser->active && parser->active->marker == node)
1654 _parser_active_list_pop (parser);
1656 expansion = _glcpp_parser_expand_node (parser, node, &last);
1660 for (n = node; n != last->next; n = n->next)
1661 while (parser->active &&
1662 parser->active->marker == n)
1664 _parser_active_list_pop (parser);
1667 _parser_active_list_push (parser,
1668 node->token->value.str,
1671 /* Splice expansion into list, supporting a
1672 * simple deletion if the expansion is
1674 if (expansion->head) {
1676 node_prev->next = expansion->head;
1678 list->head = expansion->head;
1679 expansion->tail->next = last->next;
1680 if (last == list->tail)
1681 list->tail = expansion->tail;
1684 node_prev->next = last->next;
1686 list->head = last->next;
1687 if (last == list->tail)
1693 node = node_prev ? node_prev->next : list->head;
1696 /* Remove any lingering effects of this invocation on the
1697 * active list. That is, pop until the list looks like it did
1698 * at the beginning of this function. */
1699 while (parser->active && parser->active != active_initial)
1700 _parser_active_list_pop (parser);
1702 list->non_space_tail = list->tail;
1706 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1712 _glcpp_parser_expand_token_list (parser, list);
1714 _token_list_trim_trailing_space (list);
1716 _token_list_print (parser, list);
1720 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1721 const char *identifier)
1723 /* According to the GLSL specification, macro names starting with "__"
1724 * or "GL_" are reserved for future use. So, don't allow them.
1726 if (strstr(identifier, "__")) {
1727 glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
1729 if (strncmp(identifier, "GL_", 3) == 0) {
1730 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1735 _macro_equal (macro_t *a, macro_t *b)
1737 if (a->is_function != b->is_function)
1740 if (a->is_function) {
1741 if (! _string_list_equal (a->parameters, b->parameters))
1745 return _token_list_equal_ignoring_space (a->replacements,
1750 _define_object_macro (glcpp_parser_t *parser,
1752 const char *identifier,
1753 token_list_t *replacements)
1755 macro_t *macro, *previous;
1758 _check_for_reserved_macro_name(parser, loc, identifier);
1760 macro = ralloc (parser, macro_t);
1762 macro->is_function = 0;
1763 macro->parameters = NULL;
1764 macro->identifier = ralloc_strdup (macro, identifier);
1765 macro->replacements = replacements;
1766 ralloc_steal (macro, replacements);
1768 previous = hash_table_find (parser->defines, identifier);
1770 if (_macro_equal (macro, previous)) {
1771 ralloc_free (macro);
1774 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1778 hash_table_insert (parser->defines, macro, identifier);
1782 _define_function_macro (glcpp_parser_t *parser,
1784 const char *identifier,
1785 string_list_t *parameters,
1786 token_list_t *replacements)
1788 macro_t *macro, *previous;
1790 _check_for_reserved_macro_name(parser, loc, identifier);
1792 macro = ralloc (parser, macro_t);
1793 ralloc_steal (macro, parameters);
1794 ralloc_steal (macro, replacements);
1796 macro->is_function = 1;
1797 macro->parameters = parameters;
1798 macro->identifier = ralloc_strdup (macro, identifier);
1799 macro->replacements = replacements;
1800 previous = hash_table_find (parser->defines, identifier);
1802 if (_macro_equal (macro, previous)) {
1803 ralloc_free (macro);
1806 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1810 hash_table_insert (parser->defines, macro, identifier);
1814 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1819 if (parser->lex_from_list == NULL) {
1820 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1822 /* XXX: This ugly block of code exists for the sole
1823 * purpose of converting a NEWLINE token into a SPACE
1824 * token, but only in the case where we have seen a
1825 * function-like macro name, but have not yet seen its
1826 * closing parenthesis.
1828 * There's perhaps a more compact way to do this with
1829 * mid-rule actions in the grammar.
1831 * I'm definitely not pleased with the complexity of
1834 if (parser->newline_as_space)
1837 parser->paren_count++;
1838 } else if (ret == ')') {
1839 parser->paren_count--;
1840 if (parser->paren_count == 0)
1841 parser->newline_as_space = 0;
1842 } else if (ret == NEWLINE) {
1844 } else if (ret != SPACE) {
1845 if (parser->paren_count == 0)
1846 parser->newline_as_space = 0;
1849 else if (parser->in_control_line)
1852 parser->in_control_line = 0;
1854 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1855 ret == HASH_UNDEF || ret == HASH_IF ||
1856 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1857 ret == HASH_ELIF || ret == HASH_ELSE ||
1858 ret == HASH_ENDIF || ret == HASH)
1860 parser->in_control_line = 1;
1862 else if (ret == IDENTIFIER)
1865 macro = hash_table_find (parser->defines,
1867 if (macro && macro->is_function) {
1868 parser->newline_as_space = 1;
1869 parser->paren_count = 0;
1876 node = parser->lex_from_node;
1879 ralloc_free (parser->lex_from_list);
1880 parser->lex_from_list = NULL;
1884 *yylval = node->token->value;
1885 ret = node->token->type;
1887 parser->lex_from_node = node->next;
1893 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1897 assert (parser->lex_from_list == NULL);
1899 /* Copy list, eliminating any space tokens. */
1900 parser->lex_from_list = _token_list_create (parser);
1902 for (node = list->head; node; node = node->next) {
1903 if (node->token->type == SPACE)
1905 _token_list_append (parser->lex_from_list, node->token);
1910 parser->lex_from_node = parser->lex_from_list->head;
1912 /* It's possible the list consisted of nothing but whitespace. */
1913 if (parser->lex_from_node == NULL) {
1914 ralloc_free (parser->lex_from_list);
1915 parser->lex_from_list = NULL;
1920 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1923 skip_type_t current = SKIP_NO_SKIP;
1926 if (parser->skip_stack)
1927 current = parser->skip_stack->type;
1929 node = ralloc (parser, skip_node_t);
1932 if (current == SKIP_NO_SKIP) {
1934 node->type = SKIP_NO_SKIP;
1936 node->type = SKIP_TO_ELSE;
1938 node->type = SKIP_TO_ENDIF;
1941 node->next = parser->skip_stack;
1942 parser->skip_stack = node;
1946 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1947 const char *type, int condition)
1949 if (parser->skip_stack == NULL) {
1950 glcpp_error (loc, parser, "%s without #if\n", type);
1954 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1956 parser->skip_stack->type = SKIP_NO_SKIP;
1958 parser->skip_stack->type = SKIP_TO_ENDIF;
1963 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1967 if (parser->skip_stack == NULL) {
1968 glcpp_error (loc, parser, "#endif without #if\n");
1972 node = parser->skip_stack;
1973 parser->skip_stack = node->next;