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);
344 | expression OR expression {
347 | expression AND expression {
350 | expression '|' expression {
353 | expression '^' expression {
356 | expression '&' expression {
359 | expression NOT_EQUAL expression {
362 | expression EQUAL expression {
365 | expression GREATER_OR_EQUAL expression {
368 | expression LESS_OR_EQUAL expression {
371 | expression '>' expression {
374 | expression '<' expression {
377 | expression RIGHT_SHIFT expression {
380 | expression LEFT_SHIFT expression {
383 | expression '-' expression {
386 | expression '+' expression {
389 | expression '%' expression {
391 yyerror (& @1, parser,
392 "zero modulus in preprocessor directive");
397 | expression '/' expression {
399 yyerror (& @1, parser,
400 "division by 0 in preprocessor directive");
405 | expression '*' expression {
408 | '!' expression %prec UNARY {
411 | '~' expression %prec UNARY {
414 | '-' expression %prec UNARY {
417 | '+' expression %prec UNARY {
420 | '(' expression ')' {
427 $$ = _string_list_create (parser);
428 _string_list_append_item ($$, $1);
429 ralloc_steal ($$, $1);
431 | identifier_list ',' IDENTIFIER {
433 _string_list_append_item ($$, $3);
434 ralloc_steal ($$, $3);
439 NEWLINE { $$ = NULL; }
445 yyerror (& @1, parser, "Invalid tokens after #");
450 /* empty */ { $$ = NULL; }
457 glcpp_warning(&@1, parser, "extra tokens at end of directive");
462 /* Handle "defined" operator */
464 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
465 $$ = _token_create_ival (parser, INTEGER, v);
467 | DEFINED '(' IDENTIFIER ')' {
468 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
469 $$ = _token_create_ival (parser, INTEGER, v);
471 | preprocessing_token
475 /* Exactly the same as pp_tokens, but using conditional_token */
477 $$ = _token_list_create (parser);
478 _token_list_append ($$, $1);
480 | conditional_tokens conditional_token {
482 _token_list_append ($$, $2);
487 preprocessing_token {
488 parser->space_tokens = 1;
489 $$ = _token_list_create (parser);
490 _token_list_append ($$, $1);
492 | pp_tokens preprocessing_token {
494 _token_list_append ($$, $2);
500 $$ = _token_create_str (parser, IDENTIFIER, $1);
501 $$->location = yylloc;
504 $$ = _token_create_str (parser, INTEGER_STRING, $1);
505 $$->location = yylloc;
508 $$ = _token_create_ival (parser, $1, $1);
509 $$->location = yylloc;
512 $$ = _token_create_str (parser, OTHER, $1);
513 $$->location = yylloc;
516 $$ = _token_create_ival (parser, SPACE, SPACE);
517 $$->location = yylloc;
537 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
538 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
541 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
542 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
543 | EQUAL { $$ = EQUAL; }
544 | NOT_EQUAL { $$ = NOT_EQUAL; }
552 | PASTE { $$ = PASTE; }
558 _string_list_create (void *ctx)
562 list = ralloc (ctx, string_list_t);
570 _string_list_append_item (string_list_t *list, const char *str)
574 node = ralloc (list, string_node_t);
575 node->str = ralloc_strdup (node, str);
579 if (list->head == NULL) {
582 list->tail->next = node;
589 _string_list_contains (string_list_t *list, const char *member, int *index)
597 for (i = 0, node = list->head; node; i++, node = node->next) {
598 if (strcmp (node->str, member) == 0) {
609 _string_list_length (string_list_t *list)
617 for (node = list->head; node; node = node->next)
624 _string_list_equal (string_list_t *a, string_list_t *b)
626 string_node_t *node_a, *node_b;
628 if (a == NULL && b == NULL)
631 if (a == NULL || b == NULL)
634 for (node_a = a->head, node_b = b->head;
636 node_a = node_a->next, node_b = node_b->next)
638 if (strcmp (node_a->str, node_b->str))
642 /* Catch the case of lists being different lengths, (which
643 * would cause the loop above to terminate after the shorter
645 return node_a == node_b;
649 _argument_list_create (void *ctx)
651 argument_list_t *list;
653 list = ralloc (ctx, argument_list_t);
661 _argument_list_append (argument_list_t *list, token_list_t *argument)
663 argument_node_t *node;
665 node = ralloc (list, argument_node_t);
666 node->argument = argument;
670 if (list->head == NULL) {
673 list->tail->next = node;
680 _argument_list_length (argument_list_t *list)
683 argument_node_t *node;
688 for (node = list->head; node; node = node->next)
695 _argument_list_member_at (argument_list_t *list, int index)
697 argument_node_t *node;
704 for (i = 0; i < index; i++) {
711 return node->argument;
716 /* Note: This function ralloc_steal()s the str pointer. */
718 _token_create_str (void *ctx, int type, char *str)
722 token = ralloc (ctx, token_t);
724 token->value.str = str;
726 ralloc_steal (token, str);
732 _token_create_ival (void *ctx, int type, int ival)
736 token = ralloc (ctx, token_t);
738 token->value.ival = ival;
744 _token_list_create (void *ctx)
748 list = ralloc (ctx, token_list_t);
751 list->non_space_tail = NULL;
757 _token_list_append (token_list_t *list, token_t *token)
761 node = ralloc (list, token_node_t);
765 if (list->head == NULL) {
768 list->tail->next = node;
772 if (token->type != SPACE)
773 list->non_space_tail = node;
777 _token_list_append_list (token_list_t *list, token_list_t *tail)
779 if (tail == NULL || tail->head == NULL)
782 if (list->head == NULL) {
783 list->head = tail->head;
785 list->tail->next = tail->head;
788 list->tail = tail->tail;
789 list->non_space_tail = tail->non_space_tail;
792 static token_list_t *
793 _token_list_copy (void *ctx, token_list_t *other)
801 copy = _token_list_create (ctx);
802 for (node = other->head; node; node = node->next) {
803 token_t *new_token = ralloc (copy, token_t);
804 *new_token = *node->token;
805 _token_list_append (copy, new_token);
812 _token_list_trim_trailing_space (token_list_t *list)
814 token_node_t *tail, *next;
816 if (list->non_space_tail) {
817 tail = list->non_space_tail->next;
818 list->non_space_tail->next = NULL;
819 list->tail = list->non_space_tail;
830 _token_list_is_empty_ignoring_space (token_list_t *l)
838 while (n != NULL && n->token->type == SPACE)
845 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
847 token_node_t *node_a, *node_b;
849 if (a == NULL || b == NULL) {
850 int a_empty = _token_list_is_empty_ignoring_space(a);
851 int b_empty = _token_list_is_empty_ignoring_space(b);
852 return a_empty == b_empty;
860 if (node_a == NULL && node_b == NULL)
863 if (node_a == NULL || node_b == NULL)
866 if (node_a->token->type == SPACE) {
867 node_a = node_a->next;
871 if (node_b->token->type == SPACE) {
872 node_b = node_b->next;
876 if (node_a->token->type != node_b->token->type)
879 switch (node_a->token->type) {
881 if (node_a->token->value.ival !=
882 node_b->token->value.ival)
890 if (strcmp (node_a->token->value.str,
891 node_b->token->value.str))
898 node_a = node_a->next;
899 node_b = node_b->next;
906 _token_print (char **out, size_t *len, token_t *token)
908 if (token->type < 256) {
909 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
913 switch (token->type) {
915 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
920 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
923 ralloc_asprintf_rewrite_tail (out, len, " ");
926 ralloc_asprintf_rewrite_tail (out, len, "<<");
929 ralloc_asprintf_rewrite_tail (out, len, ">>");
932 ralloc_asprintf_rewrite_tail (out, len, "<=");
934 case GREATER_OR_EQUAL:
935 ralloc_asprintf_rewrite_tail (out, len, ">=");
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 /* Nothing to print. */
959 assert(!"Error: Don't know how to print token.");
964 /* Return a new token (ralloc()ed off of 'token') formed by pasting
965 * 'token' and 'other'. Note that this function may return 'token' or
966 * 'other' directly rather than allocating anything new.
968 * Caution: Only very cursory error-checking is performed to see if
969 * the final result is a valid single token. */
971 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
973 token_t *combined = NULL;
975 /* Pasting a placeholder onto anything makes no change. */
976 if (other->type == PLACEHOLDER)
979 /* When 'token' is a placeholder, just return 'other'. */
980 if (token->type == PLACEHOLDER)
983 /* A very few single-character punctuators can be combined
984 * with another to form a multi-character punctuator. */
985 switch (token->type) {
987 if (other->type == '<')
988 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
989 else if (other->type == '=')
990 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
993 if (other->type == '>')
994 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
995 else if (other->type == '=')
996 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
999 if (other->type == '=')
1000 combined = _token_create_ival (token, EQUAL, EQUAL);
1003 if (other->type == '=')
1004 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1007 if (other->type == '&')
1008 combined = _token_create_ival (token, AND, AND);
1011 if (other->type == '|')
1012 combined = _token_create_ival (token, OR, OR);
1016 if (combined != NULL) {
1017 /* Inherit the location from the first token */
1018 combined->location = token->location;
1022 /* Two string-valued tokens can usually just be mashed
1025 * XXX: This isn't actually legitimate. Several things here
1026 * should result in a diagnostic since the result cannot be a
1027 * valid, single pre-processing token. For example, pasting
1028 * "123" and "abc" is not legal, but we don't catch that
1030 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1031 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1035 str = ralloc_asprintf (token, "%s%s", token->value.str,
1037 combined = _token_create_str (token, token->type, str);
1038 combined->location = token->location;
1042 glcpp_error (&token->location, parser, "");
1043 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1044 _token_print (&parser->info_log, &parser->info_log_length, token);
1045 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1046 _token_print (&parser->info_log, &parser->info_log_length, other);
1047 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1053 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1060 for (node = list->head; node; node = node->next)
1061 _token_print (&parser->output, &parser->output_length, node->token);
1065 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1067 glcpp_error(locp, parser, "%s", error);
1070 static void add_builtin_define(glcpp_parser_t *parser,
1071 const char *name, int value)
1076 tok = _token_create_ival (parser, INTEGER, value);
1078 list = _token_list_create(parser);
1079 _token_list_append(list, tok);
1080 _define_object_macro(parser, NULL, name, list);
1084 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1086 glcpp_parser_t *parser;
1087 int language_version;
1089 parser = ralloc (NULL, glcpp_parser_t);
1091 glcpp_lex_init_extra (parser, &parser->scanner);
1092 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1093 hash_table_string_compare);
1094 parser->active = NULL;
1095 parser->lexing_if = 0;
1096 parser->space_tokens = 1;
1097 parser->newline_as_space = 0;
1098 parser->in_control_line = 0;
1099 parser->paren_count = 0;
1101 parser->skip_stack = NULL;
1103 parser->lex_from_list = NULL;
1104 parser->lex_from_node = NULL;
1106 parser->output = ralloc_strdup(parser, "");
1107 parser->output_length = 0;
1108 parser->info_log = ralloc_strdup(parser, "");
1109 parser->info_log_length = 0;
1112 /* Add pre-defined macros. */
1113 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1114 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1116 if (api == API_OPENGLES2)
1117 add_builtin_define(parser, "GL_ES", 1);
1119 if (extensions != NULL) {
1120 if (extensions->EXT_texture_array) {
1121 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1124 if (extensions->ARB_fragment_coord_conventions)
1125 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1128 if (extensions->ARB_explicit_attrib_location)
1129 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1131 if (extensions->ARB_shader_texture_lod)
1132 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
1134 if (extensions->ARB_draw_instanced)
1135 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
1137 if (extensions->ARB_conservative_depth) {
1138 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1139 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
1142 if (extensions->OES_EGL_image_external)
1143 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
1145 if (extensions->ARB_shader_bit_encoding)
1146 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
1149 language_version = 110;
1150 add_builtin_define(parser, "__VERSION__", language_version);
1156 glcpp_parser_parse (glcpp_parser_t *parser)
1158 return yyparse (parser);
1162 glcpp_parser_destroy (glcpp_parser_t *parser)
1164 glcpp_lex_destroy (parser->scanner);
1165 hash_table_dtor (parser->defines);
1166 ralloc_free (parser);
1169 typedef enum function_status
1171 FUNCTION_STATUS_SUCCESS,
1172 FUNCTION_NOT_A_FUNCTION,
1173 FUNCTION_UNBALANCED_PARENTHESES
1174 } function_status_t;
1176 /* Find a set of function-like macro arguments by looking for a
1177 * balanced set of parentheses.
1179 * When called, 'node' should be the opening-parenthesis token, (or
1180 * perhaps preceeding SPACE tokens). Upon successful return *last will
1181 * be the last consumed node, (corresponding to the closing right
1186 * FUNCTION_STATUS_SUCCESS:
1188 * Successfully parsed a set of function arguments.
1190 * FUNCTION_NOT_A_FUNCTION:
1192 * Macro name not followed by a '('. This is not an error, but
1193 * simply that the macro name should be treated as a non-macro.
1195 * FUNCTION_UNBALANCED_PARENTHESES
1197 * Macro name is not followed by a balanced set of parentheses.
1199 static function_status_t
1200 _arguments_parse (argument_list_t *arguments,
1202 token_node_t **last)
1204 token_list_t *argument;
1209 /* Ignore whitespace before first parenthesis. */
1210 while (node && node->token->type == SPACE)
1213 if (node == NULL || node->token->type != '(')
1214 return FUNCTION_NOT_A_FUNCTION;
1218 argument = _token_list_create (arguments);
1219 _argument_list_append (arguments, argument);
1221 for (paren_count = 1; node; node = node->next) {
1222 if (node->token->type == '(')
1226 else if (node->token->type == ')')
1229 if (paren_count == 0)
1233 if (node->token->type == ',' &&
1236 _token_list_trim_trailing_space (argument);
1237 argument = _token_list_create (arguments);
1238 _argument_list_append (arguments, argument);
1241 if (argument->head == NULL) {
1242 /* Don't treat initial whitespace as
1243 * part of the arguement. */
1244 if (node->token->type == SPACE)
1247 _token_list_append (argument, node->token);
1252 return FUNCTION_UNBALANCED_PARENTHESES;
1256 return FUNCTION_STATUS_SUCCESS;
1259 static token_list_t *
1260 _token_list_create_with_one_space (void *ctx)
1265 list = _token_list_create (ctx);
1266 space = _token_create_ival (list, SPACE, SPACE);
1267 _token_list_append (list, space);
1273 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1275 token_list_t *expanded;
1278 expanded = _token_list_create (parser);
1279 token = _token_create_ival (parser, type, type);
1280 _token_list_append (expanded, token);
1281 _glcpp_parser_expand_token_list (parser, list);
1282 _token_list_append_list (expanded, list);
1283 glcpp_parser_lex_from (parser, expanded);
1287 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1294 token_node_t *next_non_space;
1296 /* Look ahead for a PASTE token, skipping space. */
1297 next_non_space = node->next;
1298 while (next_non_space && next_non_space->token->type == SPACE)
1299 next_non_space = next_non_space->next;
1301 if (next_non_space == NULL)
1304 if (next_non_space->token->type != PASTE) {
1305 node = next_non_space;
1309 /* Now find the next non-space token after the PASTE. */
1310 next_non_space = next_non_space->next;
1311 while (next_non_space && next_non_space->token->type == SPACE)
1312 next_non_space = next_non_space->next;
1314 if (next_non_space == NULL) {
1315 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1319 node->token = _token_paste (parser, node->token, next_non_space->token);
1320 node->next = next_non_space->next;
1321 if (next_non_space == list->tail)
1325 list->non_space_tail = list->tail;
1328 /* This is a helper function that's essentially part of the
1329 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1330 * except for by that function.
1332 * Returns NULL if node is a simple token with no expansion, (that is,
1333 * although 'node' corresponds to an identifier defined as a
1334 * function-like macro, it is not followed with a parenthesized
1337 * Compute the complete expansion of node (which is a function-like
1338 * macro) and subsequent nodes which are arguments.
1340 * Returns the token list that results from the expansion and sets
1341 * *last to the last node in the list that was consumed by the
1342 * expansion. Specifically, *last will be set as follows: as the
1343 * token of the closing right parenthesis.
1345 static token_list_t *
1346 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1348 token_node_t **last)
1352 const char *identifier;
1353 argument_list_t *arguments;
1354 function_status_t status;
1355 token_list_t *substituted;
1356 int parameter_index;
1358 identifier = node->token->value.str;
1360 macro = hash_table_find (parser->defines, identifier);
1362 assert (macro->is_function);
1364 arguments = _argument_list_create (parser);
1365 status = _arguments_parse (arguments, node, last);
1368 case FUNCTION_STATUS_SUCCESS:
1370 case FUNCTION_NOT_A_FUNCTION:
1372 case FUNCTION_UNBALANCED_PARENTHESES:
1373 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1377 /* Replace a macro defined as empty with a SPACE token. */
1378 if (macro->replacements == NULL) {
1379 ralloc_free (arguments);
1380 return _token_list_create_with_one_space (parser);
1383 if (! ((_argument_list_length (arguments) ==
1384 _string_list_length (macro->parameters)) ||
1385 (_string_list_length (macro->parameters) == 0 &&
1386 _argument_list_length (arguments) == 1 &&
1387 arguments->head->argument->head == NULL)))
1389 glcpp_error (&node->token->location, parser,
1390 "Error: macro %s invoked with %d arguments (expected %d)\n",
1392 _argument_list_length (arguments),
1393 _string_list_length (macro->parameters));
1397 /* Perform argument substitution on the replacement list. */
1398 substituted = _token_list_create (arguments);
1400 for (node = macro->replacements->head; node; node = node->next)
1402 if (node->token->type == IDENTIFIER &&
1403 _string_list_contains (macro->parameters,
1404 node->token->value.str,
1407 token_list_t *argument;
1408 argument = _argument_list_member_at (arguments,
1410 /* Before substituting, we expand the argument
1411 * tokens, or append a placeholder token for
1412 * an empty argument. */
1413 if (argument->head) {
1414 token_list_t *expanded_argument;
1415 expanded_argument = _token_list_copy (parser,
1417 _glcpp_parser_expand_token_list (parser,
1419 _token_list_append_list (substituted,
1424 new_token = _token_create_ival (substituted,
1427 _token_list_append (substituted, new_token);
1430 _token_list_append (substituted, node->token);
1434 /* After argument substitution, and before further expansion
1435 * below, implement token pasting. */
1437 _token_list_trim_trailing_space (substituted);
1439 _glcpp_parser_apply_pastes (parser, substituted);
1444 /* Compute the complete expansion of node, (and subsequent nodes after
1445 * 'node' in the case that 'node' is a function-like macro and
1446 * subsequent nodes are arguments).
1448 * Returns NULL if node is a simple token with no expansion.
1450 * Otherwise, returns the token list that results from the expansion
1451 * and sets *last to the last node in the list that was consumed by
1452 * the expansion. Specifically, *last will be set as follows:
1454 * As 'node' in the case of object-like macro expansion.
1456 * As the token of the closing right parenthesis in the case of
1457 * function-like macro expansion.
1459 static token_list_t *
1460 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1462 token_node_t **last)
1464 token_t *token = node->token;
1465 const char *identifier;
1468 /* We only expand identifiers */
1469 if (token->type != IDENTIFIER) {
1470 /* We change any COMMA into a COMMA_FINAL to prevent
1471 * it being mistaken for an argument separator
1473 if (token->type == ',') {
1474 token->type = COMMA_FINAL;
1475 token->value.ival = COMMA_FINAL;
1481 /* Look up this identifier in the hash table. */
1482 identifier = token->value.str;
1483 macro = hash_table_find (parser->defines, identifier);
1485 /* Not a macro, so no expansion needed. */
1489 /* Finally, don't expand this macro if we're already actively
1490 * expanding it, (to avoid infinite recursion). */
1491 if (_parser_active_list_contains (parser, identifier)) {
1492 /* We change the token type here from IDENTIFIER to
1493 * OTHER to prevent any future expansion of this
1494 * unexpanded token. */
1496 token_list_t *expansion;
1499 str = ralloc_strdup (parser, token->value.str);
1500 final = _token_create_str (parser, OTHER, str);
1501 expansion = _token_list_create (parser);
1502 _token_list_append (expansion, final);
1507 if (! macro->is_function)
1509 token_list_t *replacement;
1512 /* Replace a macro defined as empty with a SPACE token. */
1513 if (macro->replacements == NULL)
1514 return _token_list_create_with_one_space (parser);
1516 replacement = _token_list_copy (parser, macro->replacements);
1517 _glcpp_parser_apply_pastes (parser, replacement);
1521 return _glcpp_parser_expand_function (parser, node, last);
1524 /* Push a new identifier onto the parser's active list.
1526 * Here, 'marker' is the token node that appears in the list after the
1527 * expansion of 'identifier'. That is, when the list iterator begins
1528 * examining 'marker', then it is time to pop this node from the
1532 _parser_active_list_push (glcpp_parser_t *parser,
1533 const char *identifier,
1534 token_node_t *marker)
1536 active_list_t *node;
1538 node = ralloc (parser->active, active_list_t);
1539 node->identifier = ralloc_strdup (node, identifier);
1540 node->marker = marker;
1541 node->next = parser->active;
1543 parser->active = node;
1547 _parser_active_list_pop (glcpp_parser_t *parser)
1549 active_list_t *node = parser->active;
1552 parser->active = NULL;
1556 node = parser->active->next;
1557 ralloc_free (parser->active);
1559 parser->active = node;
1563 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1565 active_list_t *node;
1567 if (parser->active == NULL)
1570 for (node = parser->active; node; node = node->next)
1571 if (strcmp (node->identifier, identifier) == 0)
1577 /* Walk over the token list replacing nodes with their expansion.
1578 * Whenever nodes are expanded the walking will walk over the new
1579 * nodes, continuing to expand as necessary. The results are placed in
1583 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1586 token_node_t *node_prev;
1587 token_node_t *node, *last = NULL;
1588 token_list_t *expansion;
1589 active_list_t *active_initial = parser->active;
1594 _token_list_trim_trailing_space (list);
1601 while (parser->active && parser->active->marker == node)
1602 _parser_active_list_pop (parser);
1604 expansion = _glcpp_parser_expand_node (parser, node, &last);
1608 for (n = node; n != last->next; n = n->next)
1609 while (parser->active &&
1610 parser->active->marker == n)
1612 _parser_active_list_pop (parser);
1615 _parser_active_list_push (parser,
1616 node->token->value.str,
1619 /* Splice expansion into list, supporting a
1620 * simple deletion if the expansion is
1622 if (expansion->head) {
1624 node_prev->next = expansion->head;
1626 list->head = expansion->head;
1627 expansion->tail->next = last->next;
1628 if (last == list->tail)
1629 list->tail = expansion->tail;
1632 node_prev->next = last->next;
1634 list->head = last->next;
1635 if (last == list->tail)
1641 node = node_prev ? node_prev->next : list->head;
1644 /* Remove any lingering effects of this invocation on the
1645 * active list. That is, pop until the list looks like it did
1646 * at the beginning of this function. */
1647 while (parser->active && parser->active != active_initial)
1648 _parser_active_list_pop (parser);
1650 list->non_space_tail = list->tail;
1654 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1660 _glcpp_parser_expand_token_list (parser, list);
1662 _token_list_trim_trailing_space (list);
1664 _token_list_print (parser, list);
1668 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1669 const char *identifier)
1671 /* According to the GLSL specification, macro names starting with "__"
1672 * or "GL_" are reserved for future use. So, don't allow them.
1674 if (strstr(identifier, "__")) {
1675 glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
1677 if (strncmp(identifier, "GL_", 3) == 0) {
1678 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1683 _macro_equal (macro_t *a, macro_t *b)
1685 if (a->is_function != b->is_function)
1688 if (a->is_function) {
1689 if (! _string_list_equal (a->parameters, b->parameters))
1693 return _token_list_equal_ignoring_space (a->replacements,
1698 _define_object_macro (glcpp_parser_t *parser,
1700 const char *identifier,
1701 token_list_t *replacements)
1703 macro_t *macro, *previous;
1706 _check_for_reserved_macro_name(parser, loc, identifier);
1708 macro = ralloc (parser, macro_t);
1710 macro->is_function = 0;
1711 macro->parameters = NULL;
1712 macro->identifier = ralloc_strdup (macro, identifier);
1713 macro->replacements = replacements;
1714 ralloc_steal (macro, replacements);
1716 previous = hash_table_find (parser->defines, identifier);
1718 if (_macro_equal (macro, previous)) {
1719 ralloc_free (macro);
1722 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1726 hash_table_insert (parser->defines, macro, identifier);
1730 _define_function_macro (glcpp_parser_t *parser,
1732 const char *identifier,
1733 string_list_t *parameters,
1734 token_list_t *replacements)
1736 macro_t *macro, *previous;
1738 _check_for_reserved_macro_name(parser, loc, identifier);
1740 macro = ralloc (parser, macro_t);
1741 ralloc_steal (macro, parameters);
1742 ralloc_steal (macro, replacements);
1744 macro->is_function = 1;
1745 macro->parameters = parameters;
1746 macro->identifier = ralloc_strdup (macro, identifier);
1747 macro->replacements = replacements;
1748 previous = hash_table_find (parser->defines, identifier);
1750 if (_macro_equal (macro, previous)) {
1751 ralloc_free (macro);
1754 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1758 hash_table_insert (parser->defines, macro, identifier);
1762 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1767 if (parser->lex_from_list == NULL) {
1768 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1770 /* XXX: This ugly block of code exists for the sole
1771 * purpose of converting a NEWLINE token into a SPACE
1772 * token, but only in the case where we have seen a
1773 * function-like macro name, but have not yet seen its
1774 * closing parenthesis.
1776 * There's perhaps a more compact way to do this with
1777 * mid-rule actions in the grammar.
1779 * I'm definitely not pleased with the complexity of
1782 if (parser->newline_as_space)
1785 parser->paren_count++;
1786 } else if (ret == ')') {
1787 parser->paren_count--;
1788 if (parser->paren_count == 0)
1789 parser->newline_as_space = 0;
1790 } else if (ret == NEWLINE) {
1792 } else if (ret != SPACE) {
1793 if (parser->paren_count == 0)
1794 parser->newline_as_space = 0;
1797 else if (parser->in_control_line)
1800 parser->in_control_line = 0;
1802 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1803 ret == HASH_UNDEF || ret == HASH_IF ||
1804 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1805 ret == HASH_ELIF || ret == HASH_ELSE ||
1806 ret == HASH_ENDIF || ret == HASH)
1808 parser->in_control_line = 1;
1810 else if (ret == IDENTIFIER)
1813 macro = hash_table_find (parser->defines,
1815 if (macro && macro->is_function) {
1816 parser->newline_as_space = 1;
1817 parser->paren_count = 0;
1824 node = parser->lex_from_node;
1827 ralloc_free (parser->lex_from_list);
1828 parser->lex_from_list = NULL;
1832 *yylval = node->token->value;
1833 ret = node->token->type;
1835 parser->lex_from_node = node->next;
1841 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1845 assert (parser->lex_from_list == NULL);
1847 /* Copy list, eliminating any space tokens. */
1848 parser->lex_from_list = _token_list_create (parser);
1850 for (node = list->head; node; node = node->next) {
1851 if (node->token->type == SPACE)
1853 _token_list_append (parser->lex_from_list, node->token);
1858 parser->lex_from_node = parser->lex_from_list->head;
1860 /* It's possible the list consisted of nothing but whitespace. */
1861 if (parser->lex_from_node == NULL) {
1862 ralloc_free (parser->lex_from_list);
1863 parser->lex_from_list = NULL;
1868 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1871 skip_type_t current = SKIP_NO_SKIP;
1874 if (parser->skip_stack)
1875 current = parser->skip_stack->type;
1877 node = ralloc (parser, skip_node_t);
1880 if (current == SKIP_NO_SKIP) {
1882 node->type = SKIP_NO_SKIP;
1884 node->type = SKIP_TO_ELSE;
1886 node->type = SKIP_TO_ENDIF;
1889 node->next = parser->skip_stack;
1890 parser->skip_stack = node;
1894 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1895 const char *type, int condition)
1897 if (parser->skip_stack == NULL) {
1898 glcpp_error (loc, parser, "%s without #if\n", type);
1902 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1904 parser->skip_stack->type = SKIP_NO_SKIP;
1906 parser->skip_stack->type = SKIP_TO_ENDIF;
1911 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1915 if (parser->skip_stack == NULL) {
1916 glcpp_error (loc, parser, "#endif without #if\n");
1920 node = parser->skip_stack;
1921 parser->skip_stack = node->next;