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);
109 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
112 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
116 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
120 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
124 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
125 const char *type, int condition);
128 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
130 #define yylex glcpp_parser_lex
133 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
136 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
139 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
155 %parse-param {glcpp_parser_t *parser}
156 %lex-param {glcpp_parser_t *parser}
159 %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_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
161 %type <ival> expression INTEGER operator SPACE integer_constant
162 %type <str> IDENTIFIER INTEGER_STRING OTHER
163 %type <string_list> identifier_list
164 %type <token> preprocessing_token conditional_token
165 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
171 %left EQUAL NOT_EQUAL
172 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
173 %left LEFT_SHIFT RIGHT_SHIFT
187 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
190 _glcpp_parser_print_expanded_token_list (parser, $1);
191 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
199 IF_EXPANDED expression NEWLINE {
200 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
202 | ELIF_EXPANDED expression NEWLINE {
203 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
208 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
209 _define_object_macro (parser, & @2, $2, $3);
211 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
212 _define_function_macro (parser, & @2, $2, NULL, $5);
214 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
215 _define_function_macro (parser, & @2, $2, $4, $6);
217 | HASH_UNDEF IDENTIFIER NEWLINE {
218 macro_t *macro = hash_table_find (parser->defines, $2);
220 hash_table_remove (parser->defines, $2);
225 | HASH_IF conditional_tokens NEWLINE {
226 /* Be careful to only evaluate the 'if' expression if
227 * we are not skipping. When we are skipping, we
228 * simply push a new 0-valued 'if' onto the skip
231 * This avoids generating diagnostics for invalid
232 * expressions that are being skipped. */
233 if (parser->skip_stack == NULL ||
234 parser->skip_stack->type == SKIP_NO_SKIP)
236 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
240 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
241 parser->skip_stack->type = SKIP_TO_ENDIF;
245 /* #if without an expression is only an error if we
246 * are not skipping */
247 if (parser->skip_stack == NULL ||
248 parser->skip_stack->type == SKIP_NO_SKIP)
250 glcpp_error(& @1, parser, "#if with no expression");
252 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
254 | HASH_IFDEF IDENTIFIER junk NEWLINE {
255 macro_t *macro = hash_table_find (parser->defines, $2);
257 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
259 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
260 macro_t *macro = hash_table_find (parser->defines, $2);
262 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
264 | HASH_ELIF conditional_tokens NEWLINE {
265 /* Be careful to only evaluate the 'elif' expression
266 * if we are not skipping. When we are skipping, we
267 * simply change to a 0-valued 'elif' on the skip
270 * This avoids generating diagnostics for invalid
271 * expressions that are being skipped. */
272 if (parser->skip_stack &&
273 parser->skip_stack->type == SKIP_TO_ELSE)
275 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
279 _glcpp_parser_skip_stack_change_if (parser, & @1,
283 | HASH_ELIF NEWLINE {
284 /* #elif without an expression is an error unless we
286 if (parser->skip_stack &&
287 parser->skip_stack->type == SKIP_TO_ELSE)
289 glcpp_error(& @1, parser, "#elif with no expression");
293 _glcpp_parser_skip_stack_change_if (parser, & @1,
295 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
298 | HASH_ELSE NEWLINE {
299 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
301 | HASH_ENDIF NEWLINE {
302 _glcpp_parser_skip_stack_pop (parser, & @1);
304 | HASH_VERSION integer_constant NEWLINE {
305 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
307 hash_table_remove (parser->defines, "__VERSION__");
310 add_builtin_define (parser, "__VERSION__", $2);
313 add_builtin_define (parser, "GL_ES", 1);
315 /* Currently, all ES2 implementations support highp in the
316 * fragment shader, so we always define this macro in ES2.
317 * If we ever get a driver that doesn't support highp, we'll
318 * need to add a flag to the gl_context and check that here.
320 if ($2 >= 130 || $2 == 100)
321 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
323 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#version %" PRIiMAX, $2);
330 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
331 $$ = strtoll ($1 + 2, NULL, 16);
332 } else if ($1[0] == '0') {
333 $$ = strtoll ($1, NULL, 8);
335 $$ = strtoll ($1, NULL, 10);
347 | expression OR expression {
350 | expression AND expression {
353 | expression '|' expression {
356 | expression '^' expression {
359 | expression '&' expression {
362 | expression NOT_EQUAL expression {
365 | expression EQUAL expression {
368 | expression GREATER_OR_EQUAL expression {
371 | expression LESS_OR_EQUAL expression {
374 | expression '>' expression {
377 | expression '<' expression {
380 | expression RIGHT_SHIFT expression {
383 | expression LEFT_SHIFT expression {
386 | expression '-' expression {
389 | expression '+' expression {
392 | expression '%' expression {
394 yyerror (& @1, parser,
395 "zero modulus in preprocessor directive");
400 | expression '/' expression {
402 yyerror (& @1, parser,
403 "division by 0 in preprocessor directive");
408 | expression '*' expression {
411 | '!' expression %prec UNARY {
414 | '~' expression %prec UNARY {
417 | '-' expression %prec UNARY {
420 | '+' expression %prec UNARY {
423 | '(' expression ')' {
430 $$ = _string_list_create (parser);
431 _string_list_append_item ($$, $1);
432 ralloc_steal ($$, $1);
434 | identifier_list ',' IDENTIFIER {
436 _string_list_append_item ($$, $3);
437 ralloc_steal ($$, $3);
442 NEWLINE { $$ = NULL; }
448 yyerror (& @1, parser, "Invalid tokens after #");
453 /* empty */ { $$ = NULL; }
460 glcpp_warning(&@1, parser, "extra tokens at end of directive");
465 /* Handle "defined" operator */
467 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
468 $$ = _token_create_ival (parser, INTEGER, v);
470 | DEFINED '(' IDENTIFIER ')' {
471 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
472 $$ = _token_create_ival (parser, INTEGER, v);
474 | preprocessing_token
478 /* Exactly the same as pp_tokens, but using conditional_token */
480 $$ = _token_list_create (parser);
481 _token_list_append ($$, $1);
483 | conditional_tokens conditional_token {
485 _token_list_append ($$, $2);
490 preprocessing_token {
491 parser->space_tokens = 1;
492 $$ = _token_list_create (parser);
493 _token_list_append ($$, $1);
495 | pp_tokens preprocessing_token {
497 _token_list_append ($$, $2);
503 $$ = _token_create_str (parser, IDENTIFIER, $1);
504 $$->location = yylloc;
507 $$ = _token_create_str (parser, INTEGER_STRING, $1);
508 $$->location = yylloc;
511 $$ = _token_create_ival (parser, $1, $1);
512 $$->location = yylloc;
515 $$ = _token_create_str (parser, OTHER, $1);
516 $$->location = yylloc;
519 $$ = _token_create_ival (parser, SPACE, SPACE);
520 $$->location = yylloc;
540 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
541 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
544 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
545 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
546 | EQUAL { $$ = EQUAL; }
547 | NOT_EQUAL { $$ = NOT_EQUAL; }
555 | PASTE { $$ = PASTE; }
561 _string_list_create (void *ctx)
565 list = ralloc (ctx, string_list_t);
573 _string_list_append_item (string_list_t *list, const char *str)
577 node = ralloc (list, string_node_t);
578 node->str = ralloc_strdup (node, str);
582 if (list->head == NULL) {
585 list->tail->next = node;
592 _string_list_contains (string_list_t *list, const char *member, int *index)
600 for (i = 0, node = list->head; node; i++, node = node->next) {
601 if (strcmp (node->str, member) == 0) {
612 _string_list_length (string_list_t *list)
620 for (node = list->head; node; node = node->next)
627 _string_list_equal (string_list_t *a, string_list_t *b)
629 string_node_t *node_a, *node_b;
631 if (a == NULL && b == NULL)
634 if (a == NULL || b == NULL)
637 for (node_a = a->head, node_b = b->head;
639 node_a = node_a->next, node_b = node_b->next)
641 if (strcmp (node_a->str, node_b->str))
645 /* Catch the case of lists being different lengths, (which
646 * would cause the loop above to terminate after the shorter
648 return node_a == node_b;
652 _argument_list_create (void *ctx)
654 argument_list_t *list;
656 list = ralloc (ctx, argument_list_t);
664 _argument_list_append (argument_list_t *list, token_list_t *argument)
666 argument_node_t *node;
668 node = ralloc (list, argument_node_t);
669 node->argument = argument;
673 if (list->head == NULL) {
676 list->tail->next = node;
683 _argument_list_length (argument_list_t *list)
686 argument_node_t *node;
691 for (node = list->head; node; node = node->next)
698 _argument_list_member_at (argument_list_t *list, int index)
700 argument_node_t *node;
707 for (i = 0; i < index; i++) {
714 return node->argument;
719 /* Note: This function ralloc_steal()s the str pointer. */
721 _token_create_str (void *ctx, int type, char *str)
725 token = ralloc (ctx, token_t);
727 token->value.str = str;
729 ralloc_steal (token, str);
735 _token_create_ival (void *ctx, int type, int ival)
739 token = ralloc (ctx, token_t);
741 token->value.ival = ival;
747 _token_list_create (void *ctx)
751 list = ralloc (ctx, token_list_t);
754 list->non_space_tail = NULL;
760 _token_list_append (token_list_t *list, token_t *token)
764 node = ralloc (list, token_node_t);
768 if (list->head == NULL) {
771 list->tail->next = node;
775 if (token->type != SPACE)
776 list->non_space_tail = node;
780 _token_list_append_list (token_list_t *list, token_list_t *tail)
782 if (tail == NULL || tail->head == NULL)
785 if (list->head == NULL) {
786 list->head = tail->head;
788 list->tail->next = tail->head;
791 list->tail = tail->tail;
792 list->non_space_tail = tail->non_space_tail;
795 static token_list_t *
796 _token_list_copy (void *ctx, token_list_t *other)
804 copy = _token_list_create (ctx);
805 for (node = other->head; node; node = node->next) {
806 token_t *new_token = ralloc (copy, token_t);
807 *new_token = *node->token;
808 _token_list_append (copy, new_token);
815 _token_list_trim_trailing_space (token_list_t *list)
817 token_node_t *tail, *next;
819 if (list->non_space_tail) {
820 tail = list->non_space_tail->next;
821 list->non_space_tail->next = NULL;
822 list->tail = list->non_space_tail;
833 _token_list_is_empty_ignoring_space (token_list_t *l)
841 while (n != NULL && n->token->type == SPACE)
848 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
850 token_node_t *node_a, *node_b;
852 if (a == NULL || b == NULL) {
853 int a_empty = _token_list_is_empty_ignoring_space(a);
854 int b_empty = _token_list_is_empty_ignoring_space(b);
855 return a_empty == b_empty;
863 if (node_a == NULL && node_b == NULL)
866 if (node_a == NULL || node_b == NULL)
869 if (node_a->token->type == SPACE) {
870 node_a = node_a->next;
874 if (node_b->token->type == SPACE) {
875 node_b = node_b->next;
879 if (node_a->token->type != node_b->token->type)
882 switch (node_a->token->type) {
884 if (node_a->token->value.ival !=
885 node_b->token->value.ival)
893 if (strcmp (node_a->token->value.str,
894 node_b->token->value.str))
901 node_a = node_a->next;
902 node_b = node_b->next;
909 _token_print (char **out, size_t *len, token_t *token)
911 if (token->type < 256) {
912 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
916 switch (token->type) {
918 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
923 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
926 ralloc_asprintf_rewrite_tail (out, len, " ");
929 ralloc_asprintf_rewrite_tail (out, len, "<<");
932 ralloc_asprintf_rewrite_tail (out, len, ">>");
935 ralloc_asprintf_rewrite_tail (out, len, "<=");
937 case GREATER_OR_EQUAL:
938 ralloc_asprintf_rewrite_tail (out, len, ">=");
941 ralloc_asprintf_rewrite_tail (out, len, "==");
944 ralloc_asprintf_rewrite_tail (out, len, "!=");
947 ralloc_asprintf_rewrite_tail (out, len, "&&");
950 ralloc_asprintf_rewrite_tail (out, len, "||");
953 ralloc_asprintf_rewrite_tail (out, len, "##");
956 ralloc_asprintf_rewrite_tail (out, len, ",");
959 /* Nothing to print. */
962 assert(!"Error: Don't know how to print token.");
967 /* Return a new token (ralloc()ed off of 'token') formed by pasting
968 * 'token' and 'other'. Note that this function may return 'token' or
969 * 'other' directly rather than allocating anything new.
971 * Caution: Only very cursory error-checking is performed to see if
972 * the final result is a valid single token. */
974 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
976 token_t *combined = NULL;
978 /* Pasting a placeholder onto anything makes no change. */
979 if (other->type == PLACEHOLDER)
982 /* When 'token' is a placeholder, just return 'other'. */
983 if (token->type == PLACEHOLDER)
986 /* A very few single-character punctuators can be combined
987 * with another to form a multi-character punctuator. */
988 switch (token->type) {
990 if (other->type == '<')
991 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
992 else if (other->type == '=')
993 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
996 if (other->type == '>')
997 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
998 else if (other->type == '=')
999 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1002 if (other->type == '=')
1003 combined = _token_create_ival (token, EQUAL, EQUAL);
1006 if (other->type == '=')
1007 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1010 if (other->type == '&')
1011 combined = _token_create_ival (token, AND, AND);
1014 if (other->type == '|')
1015 combined = _token_create_ival (token, OR, OR);
1019 if (combined != NULL) {
1020 /* Inherit the location from the first token */
1021 combined->location = token->location;
1025 /* Two string-valued tokens can usually just be mashed
1028 * XXX: This isn't actually legitimate. Several things here
1029 * should result in a diagnostic since the result cannot be a
1030 * valid, single pre-processing token. For example, pasting
1031 * "123" and "abc" is not legal, but we don't catch that
1033 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1034 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1038 str = ralloc_asprintf (token, "%s%s", token->value.str,
1040 combined = _token_create_str (token, token->type, str);
1041 combined->location = token->location;
1045 glcpp_error (&token->location, parser, "");
1046 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1047 _token_print (&parser->info_log, &parser->info_log_length, token);
1048 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1049 _token_print (&parser->info_log, &parser->info_log_length, other);
1050 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1056 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1063 for (node = list->head; node; node = node->next)
1064 _token_print (&parser->output, &parser->output_length, node->token);
1068 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1070 glcpp_error(locp, parser, "%s", error);
1073 static void add_builtin_define(glcpp_parser_t *parser,
1074 const char *name, int value)
1079 tok = _token_create_ival (parser, INTEGER, value);
1081 list = _token_list_create(parser);
1082 _token_list_append(list, tok);
1083 _define_object_macro(parser, NULL, name, list);
1087 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1089 glcpp_parser_t *parser;
1090 int language_version;
1092 parser = ralloc (NULL, glcpp_parser_t);
1094 glcpp_lex_init_extra (parser, &parser->scanner);
1095 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1096 hash_table_string_compare);
1097 parser->active = NULL;
1098 parser->lexing_if = 0;
1099 parser->space_tokens = 1;
1100 parser->newline_as_space = 0;
1101 parser->in_control_line = 0;
1102 parser->paren_count = 0;
1104 parser->skip_stack = NULL;
1106 parser->lex_from_list = NULL;
1107 parser->lex_from_node = NULL;
1109 parser->output = ralloc_strdup(parser, "");
1110 parser->output_length = 0;
1111 parser->info_log = ralloc_strdup(parser, "");
1112 parser->info_log_length = 0;
1115 /* Add pre-defined macros. */
1116 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1117 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1119 if (api == API_OPENGLES2)
1120 add_builtin_define(parser, "GL_ES", 1);
1122 if (extensions != NULL) {
1123 if (extensions->EXT_texture_array) {
1124 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1127 if (extensions->ARB_fragment_coord_conventions)
1128 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1131 if (extensions->ARB_explicit_attrib_location)
1132 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1134 if (extensions->ARB_shader_texture_lod)
1135 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
1137 if (extensions->ARB_draw_instanced)
1138 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
1140 if (extensions->ARB_conservative_depth) {
1141 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1142 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
1145 if (extensions->OES_EGL_image_external)
1146 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
1148 if (extensions->ARB_shader_bit_encoding)
1149 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
1152 language_version = 110;
1153 add_builtin_define(parser, "__VERSION__", language_version);
1159 glcpp_parser_parse (glcpp_parser_t *parser)
1161 return yyparse (parser);
1165 glcpp_parser_destroy (glcpp_parser_t *parser)
1167 glcpp_lex_destroy (parser->scanner);
1168 hash_table_dtor (parser->defines);
1169 ralloc_free (parser);
1172 typedef enum function_status
1174 FUNCTION_STATUS_SUCCESS,
1175 FUNCTION_NOT_A_FUNCTION,
1176 FUNCTION_UNBALANCED_PARENTHESES
1177 } function_status_t;
1179 /* Find a set of function-like macro arguments by looking for a
1180 * balanced set of parentheses.
1182 * When called, 'node' should be the opening-parenthesis token, (or
1183 * perhaps preceeding SPACE tokens). Upon successful return *last will
1184 * be the last consumed node, (corresponding to the closing right
1189 * FUNCTION_STATUS_SUCCESS:
1191 * Successfully parsed a set of function arguments.
1193 * FUNCTION_NOT_A_FUNCTION:
1195 * Macro name not followed by a '('. This is not an error, but
1196 * simply that the macro name should be treated as a non-macro.
1198 * FUNCTION_UNBALANCED_PARENTHESES
1200 * Macro name is not followed by a balanced set of parentheses.
1202 static function_status_t
1203 _arguments_parse (argument_list_t *arguments,
1205 token_node_t **last)
1207 token_list_t *argument;
1212 /* Ignore whitespace before first parenthesis. */
1213 while (node && node->token->type == SPACE)
1216 if (node == NULL || node->token->type != '(')
1217 return FUNCTION_NOT_A_FUNCTION;
1221 argument = _token_list_create (arguments);
1222 _argument_list_append (arguments, argument);
1224 for (paren_count = 1; node; node = node->next) {
1225 if (node->token->type == '(')
1229 else if (node->token->type == ')')
1232 if (paren_count == 0)
1236 if (node->token->type == ',' &&
1239 _token_list_trim_trailing_space (argument);
1240 argument = _token_list_create (arguments);
1241 _argument_list_append (arguments, argument);
1244 if (argument->head == NULL) {
1245 /* Don't treat initial whitespace as
1246 * part of the arguement. */
1247 if (node->token->type == SPACE)
1250 _token_list_append (argument, node->token);
1255 return FUNCTION_UNBALANCED_PARENTHESES;
1259 return FUNCTION_STATUS_SUCCESS;
1262 static token_list_t *
1263 _token_list_create_with_one_space (void *ctx)
1268 list = _token_list_create (ctx);
1269 space = _token_create_ival (list, SPACE, SPACE);
1270 _token_list_append (list, space);
1276 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1278 token_list_t *expanded;
1281 expanded = _token_list_create (parser);
1282 token = _token_create_ival (parser, type, type);
1283 _token_list_append (expanded, token);
1284 _glcpp_parser_expand_token_list (parser, list);
1285 _token_list_append_list (expanded, list);
1286 glcpp_parser_lex_from (parser, expanded);
1290 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1297 token_node_t *next_non_space;
1299 /* Look ahead for a PASTE token, skipping space. */
1300 next_non_space = node->next;
1301 while (next_non_space && next_non_space->token->type == SPACE)
1302 next_non_space = next_non_space->next;
1304 if (next_non_space == NULL)
1307 if (next_non_space->token->type != PASTE) {
1308 node = next_non_space;
1312 /* Now find the next non-space token after the PASTE. */
1313 next_non_space = next_non_space->next;
1314 while (next_non_space && next_non_space->token->type == SPACE)
1315 next_non_space = next_non_space->next;
1317 if (next_non_space == NULL) {
1318 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1322 node->token = _token_paste (parser, node->token, next_non_space->token);
1323 node->next = next_non_space->next;
1324 if (next_non_space == list->tail)
1328 list->non_space_tail = list->tail;
1331 /* This is a helper function that's essentially part of the
1332 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1333 * except for by that function.
1335 * Returns NULL if node is a simple token with no expansion, (that is,
1336 * although 'node' corresponds to an identifier defined as a
1337 * function-like macro, it is not followed with a parenthesized
1340 * Compute the complete expansion of node (which is a function-like
1341 * macro) and subsequent nodes which are arguments.
1343 * Returns the token list that results from the expansion and sets
1344 * *last to the last node in the list that was consumed by the
1345 * expansion. Specifically, *last will be set as follows: as the
1346 * token of the closing right parenthesis.
1348 static token_list_t *
1349 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1351 token_node_t **last)
1355 const char *identifier;
1356 argument_list_t *arguments;
1357 function_status_t status;
1358 token_list_t *substituted;
1359 int parameter_index;
1361 identifier = node->token->value.str;
1363 macro = hash_table_find (parser->defines, identifier);
1365 assert (macro->is_function);
1367 arguments = _argument_list_create (parser);
1368 status = _arguments_parse (arguments, node, last);
1371 case FUNCTION_STATUS_SUCCESS:
1373 case FUNCTION_NOT_A_FUNCTION:
1375 case FUNCTION_UNBALANCED_PARENTHESES:
1376 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1380 /* Replace a macro defined as empty with a SPACE token. */
1381 if (macro->replacements == NULL) {
1382 ralloc_free (arguments);
1383 return _token_list_create_with_one_space (parser);
1386 if (! ((_argument_list_length (arguments) ==
1387 _string_list_length (macro->parameters)) ||
1388 (_string_list_length (macro->parameters) == 0 &&
1389 _argument_list_length (arguments) == 1 &&
1390 arguments->head->argument->head == NULL)))
1392 glcpp_error (&node->token->location, parser,
1393 "Error: macro %s invoked with %d arguments (expected %d)\n",
1395 _argument_list_length (arguments),
1396 _string_list_length (macro->parameters));
1400 /* Perform argument substitution on the replacement list. */
1401 substituted = _token_list_create (arguments);
1403 for (node = macro->replacements->head; node; node = node->next)
1405 if (node->token->type == IDENTIFIER &&
1406 _string_list_contains (macro->parameters,
1407 node->token->value.str,
1410 token_list_t *argument;
1411 argument = _argument_list_member_at (arguments,
1413 /* Before substituting, we expand the argument
1414 * tokens, or append a placeholder token for
1415 * an empty argument. */
1416 if (argument->head) {
1417 token_list_t *expanded_argument;
1418 expanded_argument = _token_list_copy (parser,
1420 _glcpp_parser_expand_token_list (parser,
1422 _token_list_append_list (substituted,
1427 new_token = _token_create_ival (substituted,
1430 _token_list_append (substituted, new_token);
1433 _token_list_append (substituted, node->token);
1437 /* After argument substitution, and before further expansion
1438 * below, implement token pasting. */
1440 _token_list_trim_trailing_space (substituted);
1442 _glcpp_parser_apply_pastes (parser, substituted);
1447 /* Compute the complete expansion of node, (and subsequent nodes after
1448 * 'node' in the case that 'node' is a function-like macro and
1449 * subsequent nodes are arguments).
1451 * Returns NULL if node is a simple token with no expansion.
1453 * Otherwise, returns the token list that results from the expansion
1454 * and sets *last to the last node in the list that was consumed by
1455 * the expansion. Specifically, *last will be set as follows:
1457 * As 'node' in the case of object-like macro expansion.
1459 * As the token of the closing right parenthesis in the case of
1460 * function-like macro expansion.
1462 static token_list_t *
1463 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1465 token_node_t **last)
1467 token_t *token = node->token;
1468 const char *identifier;
1471 /* We only expand identifiers */
1472 if (token->type != IDENTIFIER) {
1473 /* We change any COMMA into a COMMA_FINAL to prevent
1474 * it being mistaken for an argument separator
1476 if (token->type == ',') {
1477 token->type = COMMA_FINAL;
1478 token->value.ival = COMMA_FINAL;
1484 /* Look up this identifier in the hash table. */
1485 identifier = token->value.str;
1486 macro = hash_table_find (parser->defines, identifier);
1488 /* Not a macro, so no expansion needed. */
1492 /* Finally, don't expand this macro if we're already actively
1493 * expanding it, (to avoid infinite recursion). */
1494 if (_parser_active_list_contains (parser, identifier)) {
1495 /* We change the token type here from IDENTIFIER to
1496 * OTHER to prevent any future expansion of this
1497 * unexpanded token. */
1499 token_list_t *expansion;
1502 str = ralloc_strdup (parser, token->value.str);
1503 final = _token_create_str (parser, OTHER, str);
1504 expansion = _token_list_create (parser);
1505 _token_list_append (expansion, final);
1510 if (! macro->is_function)
1512 token_list_t *replacement;
1515 /* Replace a macro defined as empty with a SPACE token. */
1516 if (macro->replacements == NULL)
1517 return _token_list_create_with_one_space (parser);
1519 replacement = _token_list_copy (parser, macro->replacements);
1520 _glcpp_parser_apply_pastes (parser, replacement);
1524 return _glcpp_parser_expand_function (parser, node, last);
1527 /* Push a new identifier onto the parser's active list.
1529 * Here, 'marker' is the token node that appears in the list after the
1530 * expansion of 'identifier'. That is, when the list iterator begins
1531 * examining 'marker', then it is time to pop this node from the
1535 _parser_active_list_push (glcpp_parser_t *parser,
1536 const char *identifier,
1537 token_node_t *marker)
1539 active_list_t *node;
1541 node = ralloc (parser->active, active_list_t);
1542 node->identifier = ralloc_strdup (node, identifier);
1543 node->marker = marker;
1544 node->next = parser->active;
1546 parser->active = node;
1550 _parser_active_list_pop (glcpp_parser_t *parser)
1552 active_list_t *node = parser->active;
1555 parser->active = NULL;
1559 node = parser->active->next;
1560 ralloc_free (parser->active);
1562 parser->active = node;
1566 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1568 active_list_t *node;
1570 if (parser->active == NULL)
1573 for (node = parser->active; node; node = node->next)
1574 if (strcmp (node->identifier, identifier) == 0)
1580 /* Walk over the token list replacing nodes with their expansion.
1581 * Whenever nodes are expanded the walking will walk over the new
1582 * nodes, continuing to expand as necessary. The results are placed in
1586 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1589 token_node_t *node_prev;
1590 token_node_t *node, *last = NULL;
1591 token_list_t *expansion;
1592 active_list_t *active_initial = parser->active;
1597 _token_list_trim_trailing_space (list);
1604 while (parser->active && parser->active->marker == node)
1605 _parser_active_list_pop (parser);
1607 expansion = _glcpp_parser_expand_node (parser, node, &last);
1611 for (n = node; n != last->next; n = n->next)
1612 while (parser->active &&
1613 parser->active->marker == n)
1615 _parser_active_list_pop (parser);
1618 _parser_active_list_push (parser,
1619 node->token->value.str,
1622 /* Splice expansion into list, supporting a
1623 * simple deletion if the expansion is
1625 if (expansion->head) {
1627 node_prev->next = expansion->head;
1629 list->head = expansion->head;
1630 expansion->tail->next = last->next;
1631 if (last == list->tail)
1632 list->tail = expansion->tail;
1635 node_prev->next = last->next;
1637 list->head = last->next;
1638 if (last == list->tail)
1644 node = node_prev ? node_prev->next : list->head;
1647 /* Remove any lingering effects of this invocation on the
1648 * active list. That is, pop until the list looks like it did
1649 * at the beginning of this function. */
1650 while (parser->active && parser->active != active_initial)
1651 _parser_active_list_pop (parser);
1653 list->non_space_tail = list->tail;
1657 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1663 _glcpp_parser_expand_token_list (parser, list);
1665 _token_list_trim_trailing_space (list);
1667 _token_list_print (parser, list);
1671 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1672 const char *identifier)
1674 /* According to the GLSL specification, macro names starting with "__"
1675 * or "GL_" are reserved for future use. So, don't allow them.
1677 if (strstr(identifier, "__")) {
1678 glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
1680 if (strncmp(identifier, "GL_", 3) == 0) {
1681 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1686 _macro_equal (macro_t *a, macro_t *b)
1688 if (a->is_function != b->is_function)
1691 if (a->is_function) {
1692 if (! _string_list_equal (a->parameters, b->parameters))
1696 return _token_list_equal_ignoring_space (a->replacements,
1701 _define_object_macro (glcpp_parser_t *parser,
1703 const char *identifier,
1704 token_list_t *replacements)
1706 macro_t *macro, *previous;
1709 _check_for_reserved_macro_name(parser, loc, identifier);
1711 macro = ralloc (parser, macro_t);
1713 macro->is_function = 0;
1714 macro->parameters = NULL;
1715 macro->identifier = ralloc_strdup (macro, identifier);
1716 macro->replacements = replacements;
1717 ralloc_steal (macro, replacements);
1719 previous = hash_table_find (parser->defines, identifier);
1721 if (_macro_equal (macro, previous)) {
1722 ralloc_free (macro);
1725 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1729 hash_table_insert (parser->defines, macro, identifier);
1733 _define_function_macro (glcpp_parser_t *parser,
1735 const char *identifier,
1736 string_list_t *parameters,
1737 token_list_t *replacements)
1739 macro_t *macro, *previous;
1741 _check_for_reserved_macro_name(parser, loc, identifier);
1743 macro = ralloc (parser, macro_t);
1744 ralloc_steal (macro, parameters);
1745 ralloc_steal (macro, replacements);
1747 macro->is_function = 1;
1748 macro->parameters = parameters;
1749 macro->identifier = ralloc_strdup (macro, identifier);
1750 macro->replacements = replacements;
1751 previous = hash_table_find (parser->defines, identifier);
1753 if (_macro_equal (macro, previous)) {
1754 ralloc_free (macro);
1757 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1761 hash_table_insert (parser->defines, macro, identifier);
1765 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1770 if (parser->lex_from_list == NULL) {
1771 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1773 /* XXX: This ugly block of code exists for the sole
1774 * purpose of converting a NEWLINE token into a SPACE
1775 * token, but only in the case where we have seen a
1776 * function-like macro name, but have not yet seen its
1777 * closing parenthesis.
1779 * There's perhaps a more compact way to do this with
1780 * mid-rule actions in the grammar.
1782 * I'm definitely not pleased with the complexity of
1785 if (parser->newline_as_space)
1788 parser->paren_count++;
1789 } else if (ret == ')') {
1790 parser->paren_count--;
1791 if (parser->paren_count == 0)
1792 parser->newline_as_space = 0;
1793 } else if (ret == NEWLINE) {
1795 } else if (ret != SPACE) {
1796 if (parser->paren_count == 0)
1797 parser->newline_as_space = 0;
1800 else if (parser->in_control_line)
1803 parser->in_control_line = 0;
1805 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1806 ret == HASH_UNDEF || ret == HASH_IF ||
1807 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1808 ret == HASH_ELIF || ret == HASH_ELSE ||
1809 ret == HASH_ENDIF || ret == HASH)
1811 parser->in_control_line = 1;
1813 else if (ret == IDENTIFIER)
1816 macro = hash_table_find (parser->defines,
1818 if (macro && macro->is_function) {
1819 parser->newline_as_space = 1;
1820 parser->paren_count = 0;
1827 node = parser->lex_from_node;
1830 ralloc_free (parser->lex_from_list);
1831 parser->lex_from_list = NULL;
1835 *yylval = node->token->value;
1836 ret = node->token->type;
1838 parser->lex_from_node = node->next;
1844 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1848 assert (parser->lex_from_list == NULL);
1850 /* Copy list, eliminating any space tokens. */
1851 parser->lex_from_list = _token_list_create (parser);
1853 for (node = list->head; node; node = node->next) {
1854 if (node->token->type == SPACE)
1856 _token_list_append (parser->lex_from_list, node->token);
1861 parser->lex_from_node = parser->lex_from_list->head;
1863 /* It's possible the list consisted of nothing but whitespace. */
1864 if (parser->lex_from_node == NULL) {
1865 ralloc_free (parser->lex_from_list);
1866 parser->lex_from_list = NULL;
1871 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1874 skip_type_t current = SKIP_NO_SKIP;
1877 if (parser->skip_stack)
1878 current = parser->skip_stack->type;
1880 node = ralloc (parser, skip_node_t);
1883 if (current == SKIP_NO_SKIP) {
1885 node->type = SKIP_NO_SKIP;
1887 node->type = SKIP_TO_ELSE;
1889 node->type = SKIP_TO_ENDIF;
1892 node->next = parser->skip_stack;
1893 parser->skip_stack = node;
1897 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1898 const char *type, int condition)
1900 if (parser->skip_stack == NULL) {
1901 glcpp_error (loc, parser, "%s without #if\n", type);
1905 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1907 parser->skip_stack->type = SKIP_NO_SKIP;
1909 parser->skip_stack->type = SKIP_TO_ENDIF;
1914 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1918 if (parser->skip_stack == NULL) {
1919 glcpp_error (loc, parser, "#endif without #if\n");
1923 node = parser->skip_stack;
1924 parser->skip_stack = node->next;