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 */
35 #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
36 #define glcpp_printf(stream, fmt, args, ...) \
37 stream = talloc_asprintf_append(stream, fmt, args)
40 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
43 _define_object_macro (glcpp_parser_t *parser,
46 token_list_t *replacements);
49 _define_function_macro (glcpp_parser_t *parser,
52 string_list_t *parameters,
53 token_list_t *replacements);
55 static string_list_t *
56 _string_list_create (void *ctx);
59 _string_list_append_item (string_list_t *list, const char *str);
62 _string_list_contains (string_list_t *list, const char *member, int *index);
65 _string_list_length (string_list_t *list);
68 _string_list_equal (string_list_t *a, string_list_t *b);
70 static argument_list_t *
71 _argument_list_create (void *ctx);
74 _argument_list_append (argument_list_t *list, token_list_t *argument);
77 _argument_list_length (argument_list_t *list);
80 _argument_list_member_at (argument_list_t *list, int index);
82 /* Note: This function talloc_steal()s the str pointer. */
84 _token_create_str (void *ctx, int type, char *str);
87 _token_create_ival (void *ctx, int type, int ival);
90 _token_list_create (void *ctx);
92 /* Note: This function adds a talloc_reference() to token.
94 * You may want to talloc_unlink any current reference if you no
97 _token_list_append (token_list_t *list, token_t *token);
100 _token_list_append_list (token_list_t *list, token_list_t *tail);
103 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
105 static active_list_t *
106 _active_list_push (active_list_t *list,
107 const char *identifier,
108 token_node_t *marker);
110 static active_list_t *
111 _active_list_pop (active_list_t *list);
114 _active_list_contains (active_list_t *list, const char *identifier);
117 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
120 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
124 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
128 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
132 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
133 const char *type, int condition);
136 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
138 #define yylex glcpp_parser_lex
141 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
144 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
147 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
163 %parse-param {glcpp_parser_t *parser}
164 %lex-param {glcpp_parser_t *parser}
167 %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC 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
169 %type <ival> expression INTEGER operator SPACE integer_constant
170 %type <str> IDENTIFIER INTEGER_STRING OTHER
171 %type <string_list> identifier_list
172 %type <token> preprocessing_token conditional_token
173 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
179 %left EQUAL NOT_EQUAL
180 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
181 %left LEFT_SHIFT RIGHT_SHIFT
195 glcpp_print(parser->output, "\n");
198 _glcpp_parser_print_expanded_token_list (parser, $1);
199 glcpp_print(parser->output, "\n");
207 IF_EXPANDED expression NEWLINE {
208 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
210 | ELIF_EXPANDED expression NEWLINE {
211 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
216 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
217 _define_object_macro (parser, & @2, $2, $3);
219 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
220 _define_function_macro (parser, & @2, $2, NULL, $5);
222 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
223 _define_function_macro (parser, & @2, $2, $4, $6);
225 | HASH_UNDEF IDENTIFIER NEWLINE {
226 macro_t *macro = hash_table_find (parser->defines, $2);
228 hash_table_remove (parser->defines, $2);
233 | HASH_IF conditional_tokens NEWLINE {
234 /* Be careful to only evaluate the 'if' expression if
235 * we are not skipping. When we are skipping, we
236 * simply push a new 0-valued 'if' onto the skip
239 * This avoids generating diagnostics for invalid
240 * expressions that are being skipped. */
241 if (parser->skip_stack == NULL ||
242 parser->skip_stack->type == SKIP_NO_SKIP)
244 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
248 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
249 parser->skip_stack->type = SKIP_TO_ENDIF;
253 /* #if without an expression is only an error if we
254 * are not skipping */
255 if (parser->skip_stack == NULL ||
256 parser->skip_stack->type == SKIP_NO_SKIP)
258 glcpp_error(& @1, parser, "#if with no expression");
260 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
262 | HASH_IFDEF IDENTIFIER junk NEWLINE {
263 macro_t *macro = hash_table_find (parser->defines, $2);
265 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
267 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
268 macro_t *macro = hash_table_find (parser->defines, $2);
270 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
272 | HASH_ELIF conditional_tokens NEWLINE {
273 /* Be careful to only evaluate the 'elif' expression
274 * if we are not skipping. When we are skipping, we
275 * simply change to a 0-valued 'elif' on the skip
278 * This avoids generating diagnostics for invalid
279 * expressions that are being skipped. */
280 if (parser->skip_stack &&
281 parser->skip_stack->type == SKIP_TO_ELSE)
283 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
287 _glcpp_parser_skip_stack_change_if (parser, & @1,
291 | HASH_ELIF NEWLINE {
292 /* #elif without an expression is an error unless we
294 if (parser->skip_stack &&
295 parser->skip_stack->type == SKIP_TO_ELSE)
297 glcpp_error(& @1, parser, "#elif with no expression");
301 _glcpp_parser_skip_stack_change_if (parser, & @1,
303 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
306 | HASH_ELSE NEWLINE {
307 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
309 | HASH_ENDIF NEWLINE {
310 _glcpp_parser_skip_stack_pop (parser, & @1);
312 | HASH_VERSION integer_constant NEWLINE {
313 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
315 hash_table_remove (parser->defines, "__VERSION__");
318 add_builtin_define (parser, "__VERSION__", $2);
321 add_builtin_define (parser, "GL_ES", 1);
324 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
326 glcpp_printf(parser->output, "#version %" PRIiMAX, $2);
333 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
334 $$ = strtoll ($1 + 2, NULL, 16);
335 } else if ($1[0] == '0') {
336 $$ = strtoll ($1, NULL, 8);
338 $$ = 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 {
395 | expression '/' expression {
398 | expression '*' expression {
401 | '!' expression %prec UNARY {
404 | '~' expression %prec UNARY {
407 | '-' expression %prec UNARY {
410 | '+' expression %prec UNARY {
413 | '(' expression ')' {
420 $$ = _string_list_create (parser);
421 _string_list_append_item ($$, $1);
422 talloc_steal ($$, $1);
424 | identifier_list ',' IDENTIFIER {
426 _string_list_append_item ($$, $3);
427 talloc_steal ($$, $3);
432 NEWLINE { $$ = NULL; }
438 yyerror (& @1, parser, "Invalid tokens after #");
443 /* empty */ { $$ = NULL; }
450 glcpp_warning(&@1, parser, "extra tokens at end of directive");
455 /* Handle "defined" operator */
457 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
458 $$ = _token_create_ival (parser, INTEGER, v);
460 | DEFINED '(' IDENTIFIER ')' {
461 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
462 $$ = _token_create_ival (parser, INTEGER, v);
464 | preprocessing_token
468 /* Exactly the same as pp_tokens, but using conditional_token */
470 parser->space_tokens = 1;
471 $$ = _token_list_create (parser);
472 _token_list_append ($$, $1);
473 talloc_unlink (parser, $1);
475 | conditional_tokens conditional_token {
477 _token_list_append ($$, $2);
478 talloc_unlink (parser, $2);
483 preprocessing_token {
484 parser->space_tokens = 1;
485 $$ = _token_list_create (parser);
486 _token_list_append ($$, $1);
487 talloc_unlink (parser, $1);
489 | pp_tokens preprocessing_token {
491 _token_list_append ($$, $2);
492 talloc_unlink (parser, $2);
498 $$ = _token_create_str (parser, IDENTIFIER, $1);
499 $$->location = yylloc;
502 $$ = _token_create_str (parser, INTEGER_STRING, $1);
503 $$->location = yylloc;
506 $$ = _token_create_ival (parser, $1, $1);
507 $$->location = yylloc;
510 $$ = _token_create_str (parser, OTHER, $1);
511 $$->location = yylloc;
514 $$ = _token_create_ival (parser, SPACE, SPACE);
515 $$->location = yylloc;
535 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
536 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
539 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
540 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
541 | EQUAL { $$ = EQUAL; }
542 | NOT_EQUAL { $$ = NOT_EQUAL; }
550 | PASTE { $$ = PASTE; }
556 _string_list_create (void *ctx)
560 list = talloc (ctx, string_list_t);
568 _string_list_append_item (string_list_t *list, const char *str)
572 node = talloc (list, string_node_t);
573 node->str = talloc_strdup (node, str);
577 if (list->head == NULL) {
580 list->tail->next = node;
587 _string_list_contains (string_list_t *list, const char *member, int *index)
595 for (i = 0, node = list->head; node; i++, node = node->next) {
596 if (strcmp (node->str, member) == 0) {
607 _string_list_length (string_list_t *list)
615 for (node = list->head; node; node = node->next)
622 _string_list_equal (string_list_t *a, string_list_t *b)
624 string_node_t *node_a, *node_b;
626 if (a == NULL && b == NULL)
629 if (a == NULL || b == NULL)
632 for (node_a = a->head, node_b = b->head;
634 node_a = node_a->next, node_b = node_b->next)
636 if (strcmp (node_a->str, node_b->str))
640 /* Catch the case of lists being different lengths, (which
641 * would cause the loop above to terminate after the shorter
643 return node_a == node_b;
647 _argument_list_create (void *ctx)
649 argument_list_t *list;
651 list = talloc (ctx, argument_list_t);
659 _argument_list_append (argument_list_t *list, token_list_t *argument)
661 argument_node_t *node;
663 node = talloc (list, argument_node_t);
664 node->argument = argument;
668 if (list->head == NULL) {
671 list->tail->next = node;
678 _argument_list_length (argument_list_t *list)
681 argument_node_t *node;
686 for (node = list->head; node; node = node->next)
693 _argument_list_member_at (argument_list_t *list, int index)
695 argument_node_t *node;
702 for (i = 0; i < index; i++) {
709 return node->argument;
714 /* Note: This function talloc_steal()s the str pointer. */
716 _token_create_str (void *ctx, int type, char *str)
720 token = talloc (ctx, token_t);
722 token->value.str = talloc_steal (token, str);
728 _token_create_ival (void *ctx, int type, int ival)
732 token = talloc (ctx, token_t);
734 token->value.ival = ival;
740 _token_list_create (void *ctx)
744 list = talloc (ctx, token_list_t);
747 list->non_space_tail = NULL;
753 _token_list_append (token_list_t *list, token_t *token)
757 node = talloc (list, token_node_t);
758 node->token = talloc_reference (list, token);
762 if (list->head == NULL) {
765 list->tail->next = node;
769 if (token->type != SPACE)
770 list->non_space_tail = node;
774 _token_list_append_list (token_list_t *list, token_list_t *tail)
776 if (tail == NULL || tail->head == NULL)
779 if (list->head == NULL) {
780 list->head = tail->head;
782 list->tail->next = tail->head;
785 list->tail = tail->tail;
786 list->non_space_tail = tail->non_space_tail;
789 static token_list_t *
790 _token_list_copy (void *ctx, token_list_t *other)
798 copy = _token_list_create (ctx);
799 for (node = other->head; node; node = node->next)
800 _token_list_append (copy, node->token);
806 _token_list_trim_trailing_space (token_list_t *list)
808 token_node_t *tail, *next;
810 if (list->non_space_tail) {
811 tail = list->non_space_tail->next;
812 list->non_space_tail->next = NULL;
813 list->tail = list->non_space_tail;
824 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
826 token_node_t *node_a, *node_b;
833 if (node_a == NULL && node_b == NULL)
836 if (node_a == NULL || node_b == NULL)
839 if (node_a->token->type == SPACE) {
840 node_a = node_a->next;
844 if (node_b->token->type == SPACE) {
845 node_b = node_b->next;
849 if (node_a->token->type != node_b->token->type)
852 switch (node_a->token->type) {
854 if (node_a->token->value.ival !=
855 node_b->token->value.ival)
863 if (strcmp (node_a->token->value.str,
864 node_b->token->value.str))
871 node_a = node_a->next;
872 node_b = node_b->next;
879 _token_print (char **out, token_t *token)
881 if (token->type < 256) {
882 glcpp_printf (*out, "%c", token->type);
886 switch (token->type) {
888 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
893 glcpp_print (*out, token->value.str);
896 glcpp_print (*out, " ");
899 glcpp_print (*out, "<<");
902 glcpp_print (*out, ">>");
905 glcpp_print (*out, "<=");
907 case GREATER_OR_EQUAL:
908 glcpp_print (*out, ">=");
911 glcpp_print (*out, "==");
914 glcpp_print (*out, "!=");
917 glcpp_print (*out, "&&");
920 glcpp_print (*out, "||");
923 glcpp_print (*out, "##");
926 glcpp_print (*out, ",");
929 /* Nothing to print. */
932 assert(!"Error: Don't know how to print token.");
937 /* Return a new token (talloc()ed off of 'token') formed by pasting
938 * 'token' and 'other'. Note that this function may return 'token' or
939 * 'other' directly rather than allocating anything new.
941 * Caution: Only very cursory error-checking is performed to see if
942 * the final result is a valid single token. */
944 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
946 token_t *combined = NULL;
948 /* Pasting a placeholder onto anything makes no change. */
949 if (other->type == PLACEHOLDER)
952 /* When 'token' is a placeholder, just return 'other'. */
953 if (token->type == PLACEHOLDER)
956 /* A very few single-character punctuators can be combined
957 * with another to form a multi-character punctuator. */
958 switch (token->type) {
960 if (other->type == '<')
961 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
962 else if (other->type == '=')
963 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
966 if (other->type == '>')
967 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
968 else if (other->type == '=')
969 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
972 if (other->type == '=')
973 combined = _token_create_ival (token, EQUAL, EQUAL);
976 if (other->type == '=')
977 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
980 if (other->type == '&')
981 combined = _token_create_ival (token, AND, AND);
984 if (other->type == '|')
985 combined = _token_create_ival (token, OR, OR);
989 if (combined != NULL) {
990 /* Inherit the location from the first token */
991 combined->location = token->location;
995 /* Two string-valued tokens can usually just be mashed
998 * XXX: This isn't actually legitimate. Several things here
999 * should result in a diagnostic since the result cannot be a
1000 * valid, single pre-processing token. For example, pasting
1001 * "123" and "abc" is not legal, but we don't catch that
1003 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1004 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1008 str = talloc_asprintf (token, "%s%s", token->value.str,
1010 combined = _token_create_str (token, token->type, str);
1011 combined->location = token->location;
1015 glcpp_error (&token->location, parser, "");
1016 glcpp_print (parser->info_log, "Pasting \"");
1017 _token_print (&parser->info_log, token);
1018 glcpp_print (parser->info_log, "\" and \"");
1019 _token_print (&parser->info_log, other);
1020 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
1026 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1033 for (node = list->head; node; node = node->next)
1034 _token_print (&parser->output, node->token);
1038 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1040 glcpp_error(locp, parser, "%s", error);
1043 static void add_builtin_define(glcpp_parser_t *parser,
1044 const char *name, int value)
1049 tok = _token_create_ival (parser, INTEGER, value);
1051 list = _token_list_create(parser);
1052 _token_list_append(list, tok);
1053 _define_object_macro(parser, NULL, name, list);
1055 talloc_unlink(parser, tok);
1059 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1061 glcpp_parser_t *parser;
1062 int language_version;
1064 parser = talloc (NULL, glcpp_parser_t);
1066 glcpp_lex_init_extra (parser, &parser->scanner);
1067 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1068 hash_table_string_compare);
1069 parser->active = NULL;
1070 parser->lexing_if = 0;
1071 parser->space_tokens = 1;
1072 parser->newline_as_space = 0;
1073 parser->in_control_line = 0;
1074 parser->paren_count = 0;
1076 parser->skip_stack = NULL;
1078 parser->lex_from_list = NULL;
1079 parser->lex_from_node = NULL;
1081 parser->output = talloc_strdup(parser, "");
1082 parser->info_log = talloc_strdup(parser, "");
1085 /* Add pre-defined macros. */
1086 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1087 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1089 if (api == API_OPENGLES2)
1090 add_builtin_define(parser, "GL_ES", 1);
1092 if (extensions != NULL) {
1093 if (extensions->EXT_texture_array) {
1094 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1097 if (extensions->ARB_fragment_coord_conventions)
1098 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1101 if (extensions->ARB_explicit_attrib_location)
1102 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1105 language_version = 110;
1106 add_builtin_define(parser, "__VERSION__", language_version);
1112 glcpp_parser_parse (glcpp_parser_t *parser)
1114 return yyparse (parser);
1118 glcpp_parser_destroy (glcpp_parser_t *parser)
1120 glcpp_lex_destroy (parser->scanner);
1121 hash_table_dtor (parser->defines);
1122 talloc_free (parser);
1125 typedef enum function_status
1127 FUNCTION_STATUS_SUCCESS,
1128 FUNCTION_NOT_A_FUNCTION,
1129 FUNCTION_UNBALANCED_PARENTHESES
1130 } function_status_t;
1132 /* Find a set of function-like macro arguments by looking for a
1133 * balanced set of parentheses.
1135 * When called, 'node' should be the opening-parenthesis token, (or
1136 * perhaps preceeding SPACE tokens). Upon successful return *last will
1137 * be the last consumed node, (corresponding to the closing right
1142 * FUNCTION_STATUS_SUCCESS:
1144 * Successfully parsed a set of function arguments.
1146 * FUNCTION_NOT_A_FUNCTION:
1148 * Macro name not followed by a '('. This is not an error, but
1149 * simply that the macro name should be treated as a non-macro.
1151 * FUNCTION_UNBALANCED_PARENTHESES
1153 * Macro name is not followed by a balanced set of parentheses.
1155 static function_status_t
1156 _arguments_parse (argument_list_t *arguments,
1158 token_node_t **last)
1160 token_list_t *argument;
1165 /* Ignore whitespace before first parenthesis. */
1166 while (node && node->token->type == SPACE)
1169 if (node == NULL || node->token->type != '(')
1170 return FUNCTION_NOT_A_FUNCTION;
1174 argument = _token_list_create (arguments);
1175 _argument_list_append (arguments, argument);
1177 for (paren_count = 1; node; node = node->next) {
1178 if (node->token->type == '(')
1182 else if (node->token->type == ')')
1185 if (paren_count == 0)
1189 if (node->token->type == ',' &&
1192 _token_list_trim_trailing_space (argument);
1193 argument = _token_list_create (arguments);
1194 _argument_list_append (arguments, argument);
1197 if (argument->head == NULL) {
1198 /* Don't treat initial whitespace as
1199 * part of the arguement. */
1200 if (node->token->type == SPACE)
1203 _token_list_append (argument, node->token);
1208 return FUNCTION_UNBALANCED_PARENTHESES;
1212 return FUNCTION_STATUS_SUCCESS;
1215 static token_list_t *
1216 _token_list_create_with_one_space (void *ctx)
1221 list = _token_list_create (ctx);
1222 space = _token_create_ival (list, SPACE, SPACE);
1223 _token_list_append (list, space);
1229 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1231 token_list_t *expanded;
1234 expanded = _token_list_create (parser);
1235 token = _token_create_ival (parser, type, type);
1236 _token_list_append (expanded, token);
1237 _glcpp_parser_expand_token_list (parser, list);
1238 _token_list_append_list (expanded, list);
1239 glcpp_parser_lex_from (parser, expanded);
1242 /* This is a helper function that's essentially part of the
1243 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1244 * except for by that function.
1246 * Returns NULL if node is a simple token with no expansion, (that is,
1247 * although 'node' corresponds to an identifier defined as a
1248 * function-like macro, it is not followed with a parenthesized
1251 * Compute the complete expansion of node (which is a function-like
1252 * macro) and subsequent nodes which are arguments.
1254 * Returns the token list that results from the expansion and sets
1255 * *last to the last node in the list that was consumed by the
1256 * expansion. Specifically, *last will be set as follows: as the
1257 * token of the closing right parenthesis.
1259 static token_list_t *
1260 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1262 token_node_t **last)
1266 const char *identifier;
1267 argument_list_t *arguments;
1268 function_status_t status;
1269 token_list_t *substituted;
1270 int parameter_index;
1272 identifier = node->token->value.str;
1274 macro = hash_table_find (parser->defines, identifier);
1276 assert (macro->is_function);
1278 arguments = _argument_list_create (parser);
1279 status = _arguments_parse (arguments, node, last);
1282 case FUNCTION_STATUS_SUCCESS:
1284 case FUNCTION_NOT_A_FUNCTION:
1286 case FUNCTION_UNBALANCED_PARENTHESES:
1287 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1291 /* Replace a macro defined as empty with a SPACE token. */
1292 if (macro->replacements == NULL) {
1293 talloc_free (arguments);
1294 return _token_list_create_with_one_space (parser);
1297 if (! ((_argument_list_length (arguments) ==
1298 _string_list_length (macro->parameters)) ||
1299 (_string_list_length (macro->parameters) == 0 &&
1300 _argument_list_length (arguments) == 1 &&
1301 arguments->head->argument->head == NULL)))
1303 glcpp_error (&node->token->location, parser,
1304 "Error: macro %s invoked with %d arguments (expected %d)\n",
1306 _argument_list_length (arguments),
1307 _string_list_length (macro->parameters));
1311 /* Perform argument substitution on the replacement list. */
1312 substituted = _token_list_create (arguments);
1314 for (node = macro->replacements->head; node; node = node->next)
1316 if (node->token->type == IDENTIFIER &&
1317 _string_list_contains (macro->parameters,
1318 node->token->value.str,
1321 token_list_t *argument;
1322 argument = _argument_list_member_at (arguments,
1324 /* Before substituting, we expand the argument
1325 * tokens, or append a placeholder token for
1326 * an empty argument. */
1327 if (argument->head) {
1328 token_list_t *expanded_argument;
1329 expanded_argument = _token_list_copy (parser,
1331 _glcpp_parser_expand_token_list (parser,
1333 _token_list_append_list (substituted,
1338 new_token = _token_create_ival (substituted,
1341 _token_list_append (substituted, new_token);
1344 _token_list_append (substituted, node->token);
1348 /* After argument substitution, and before further expansion
1349 * below, implement token pasting. */
1351 _token_list_trim_trailing_space (substituted);
1353 node = substituted->head;
1356 token_node_t *next_non_space;
1358 /* Look ahead for a PASTE token, skipping space. */
1359 next_non_space = node->next;
1360 while (next_non_space && next_non_space->token->type == SPACE)
1361 next_non_space = next_non_space->next;
1363 if (next_non_space == NULL)
1366 if (next_non_space->token->type != PASTE) {
1367 node = next_non_space;
1371 /* Now find the next non-space token after the PASTE. */
1372 next_non_space = next_non_space->next;
1373 while (next_non_space && next_non_space->token->type == SPACE)
1374 next_non_space = next_non_space->next;
1376 if (next_non_space == NULL) {
1377 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1381 node->token = _token_paste (parser, node->token, next_non_space->token);
1382 node->next = next_non_space->next;
1383 if (next_non_space == substituted->tail)
1384 substituted->tail = node;
1389 substituted->non_space_tail = substituted->tail;
1394 /* Compute the complete expansion of node, (and subsequent nodes after
1395 * 'node' in the case that 'node' is a function-like macro and
1396 * subsequent nodes are arguments).
1398 * Returns NULL if node is a simple token with no expansion.
1400 * Otherwise, returns the token list that results from the expansion
1401 * and sets *last to the last node in the list that was consumed by
1402 * the expansion. Specifically, *last will be set as follows:
1404 * As 'node' in the case of object-like macro expansion.
1406 * As the token of the closing right parenthesis in the case of
1407 * function-like macro expansion.
1409 static token_list_t *
1410 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1412 token_node_t **last)
1414 token_t *token = node->token;
1415 const char *identifier;
1418 /* We only expand identifiers */
1419 if (token->type != IDENTIFIER) {
1420 /* We change any COMMA into a COMMA_FINAL to prevent
1421 * it being mistaken for an argument separator
1423 if (token->type == ',') {
1424 token->type = COMMA_FINAL;
1425 token->value.ival = COMMA_FINAL;
1431 /* Look up this identifier in the hash table. */
1432 identifier = token->value.str;
1433 macro = hash_table_find (parser->defines, identifier);
1435 /* Not a macro, so no expansion needed. */
1439 /* Finally, don't expand this macro if we're already actively
1440 * expanding it, (to avoid infinite recursion). */
1441 if (_active_list_contains (parser->active, identifier)) {
1442 /* We change the token type here from IDENTIFIER to
1443 * OTHER to prevent any future expansion of this
1444 * unexpanded token. */
1446 token_list_t *expansion;
1449 str = talloc_strdup (parser, token->value.str);
1450 final = _token_create_str (parser, OTHER, str);
1451 expansion = _token_list_create (parser);
1452 _token_list_append (expansion, final);
1457 if (! macro->is_function)
1461 /* Replace a macro defined as empty with a SPACE token. */
1462 if (macro->replacements == NULL)
1463 return _token_list_create_with_one_space (parser);
1465 return _token_list_copy (parser, macro->replacements);
1468 return _glcpp_parser_expand_function (parser, node, last);
1471 /* Push a new identifier onto the active list, returning the new list.
1473 * Here, 'marker' is the token node that appears in the list after the
1474 * expansion of 'identifier'. That is, when the list iterator begins
1475 * examinging 'marker', then it is time to pop this node from the
1479 _active_list_push (active_list_t *list,
1480 const char *identifier,
1481 token_node_t *marker)
1483 active_list_t *node;
1485 node = talloc (list, active_list_t);
1486 node->identifier = talloc_strdup (node, identifier);
1487 node->marker = marker;
1494 _active_list_pop (active_list_t *list)
1496 active_list_t *node = list;
1508 _active_list_contains (active_list_t *list, const char *identifier)
1510 active_list_t *node;
1515 for (node = list; node; node = node->next)
1516 if (strcmp (node->identifier, identifier) == 0)
1522 /* Walk over the token list replacing nodes with their expansion.
1523 * Whenever nodes are expanded the walking will walk over the new
1524 * nodes, continuing to expand as necessary. The results are placed in
1528 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1531 token_node_t *node_prev;
1532 token_node_t *node, *last = NULL;
1533 token_list_t *expansion;
1538 _token_list_trim_trailing_space (list);
1545 while (parser->active && parser->active->marker == node)
1546 parser->active = _active_list_pop (parser->active);
1548 /* Find the expansion for node, which will replace all
1549 * nodes from node to last, inclusive. */
1550 expansion = _glcpp_parser_expand_node (parser, node, &last);
1554 for (n = node; n != last->next; n = n->next)
1555 while (parser->active &&
1556 parser->active->marker == n)
1558 parser->active = _active_list_pop (parser->active);
1561 parser->active = _active_list_push (parser->active,
1562 node->token->value.str,
1565 /* Splice expansion into list, supporting a
1566 * simple deletion if the expansion is
1568 if (expansion->head) {
1570 node_prev->next = expansion->head;
1572 list->head = expansion->head;
1573 expansion->tail->next = last->next;
1574 if (last == list->tail)
1575 list->tail = expansion->tail;
1578 node_prev->next = last->next;
1580 list->head = last->next;
1581 if (last == list->tail)
1587 node = node_prev ? node_prev->next : list->head;
1590 while (parser->active)
1591 parser->active = _active_list_pop (parser->active);
1593 list->non_space_tail = list->tail;
1597 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1603 _glcpp_parser_expand_token_list (parser, list);
1605 _token_list_trim_trailing_space (list);
1607 _token_list_print (parser, list);
1611 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1612 const char *identifier)
1614 /* According to the GLSL specification, macro names starting with "__"
1615 * or "GL_" are reserved for future use. So, don't allow them.
1617 if (strncmp(identifier, "__", 2) == 0) {
1618 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1620 if (strncmp(identifier, "GL_", 3) == 0) {
1621 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1626 _macro_equal (macro_t *a, macro_t *b)
1628 if (a->is_function != b->is_function)
1631 if (a->is_function) {
1632 if (! _string_list_equal (a->parameters, b->parameters))
1636 return _token_list_equal_ignoring_space (a->replacements,
1641 _define_object_macro (glcpp_parser_t *parser,
1643 const char *identifier,
1644 token_list_t *replacements)
1646 macro_t *macro, *previous;
1649 _check_for_reserved_macro_name(parser, loc, identifier);
1651 macro = talloc (parser, macro_t);
1653 macro->is_function = 0;
1654 macro->parameters = NULL;
1655 macro->identifier = talloc_strdup (macro, identifier);
1656 macro->replacements = talloc_steal (macro, replacements);
1658 previous = hash_table_find (parser->defines, identifier);
1660 if (_macro_equal (macro, previous)) {
1661 talloc_free (macro);
1664 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1668 hash_table_insert (parser->defines, macro, identifier);
1672 _define_function_macro (glcpp_parser_t *parser,
1674 const char *identifier,
1675 string_list_t *parameters,
1676 token_list_t *replacements)
1678 macro_t *macro, *previous;
1680 _check_for_reserved_macro_name(parser, loc, identifier);
1682 macro = talloc (parser, macro_t);
1684 macro->is_function = 1;
1685 macro->parameters = talloc_steal (macro, parameters);
1686 macro->identifier = talloc_strdup (macro, identifier);
1687 macro->replacements = talloc_steal (macro, replacements);
1689 previous = hash_table_find (parser->defines, identifier);
1691 if (_macro_equal (macro, previous)) {
1692 talloc_free (macro);
1695 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1699 hash_table_insert (parser->defines, macro, identifier);
1703 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1708 if (parser->lex_from_list == NULL) {
1709 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1711 /* XXX: This ugly block of code exists for the sole
1712 * purpose of converting a NEWLINE token into a SPACE
1713 * token, but only in the case where we have seen a
1714 * function-like macro name, but have not yet seen its
1715 * closing parenthesis.
1717 * There's perhaps a more compact way to do this with
1718 * mid-rule actions in the grammar.
1720 * I'm definitely not pleased with the complexity of
1723 if (parser->newline_as_space)
1726 parser->paren_count++;
1727 } else if (ret == ')') {
1728 parser->paren_count--;
1729 if (parser->paren_count == 0)
1730 parser->newline_as_space = 0;
1731 } else if (ret == NEWLINE) {
1733 } else if (ret != SPACE) {
1734 if (parser->paren_count == 0)
1735 parser->newline_as_space = 0;
1738 else if (parser->in_control_line)
1741 parser->in_control_line = 0;
1743 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1744 ret == HASH_UNDEF || ret == HASH_IF ||
1745 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1746 ret == HASH_ELIF || ret == HASH_ELSE ||
1747 ret == HASH_ENDIF || ret == HASH)
1749 parser->in_control_line = 1;
1751 else if (ret == IDENTIFIER)
1754 macro = hash_table_find (parser->defines,
1756 if (macro && macro->is_function) {
1757 parser->newline_as_space = 1;
1758 parser->paren_count = 0;
1765 node = parser->lex_from_node;
1768 talloc_free (parser->lex_from_list);
1769 parser->lex_from_list = NULL;
1773 *yylval = node->token->value;
1774 ret = node->token->type;
1776 parser->lex_from_node = node->next;
1782 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1786 assert (parser->lex_from_list == NULL);
1788 /* Copy list, eliminating any space tokens. */
1789 parser->lex_from_list = _token_list_create (parser);
1791 for (node = list->head; node; node = node->next) {
1792 if (node->token->type == SPACE)
1794 _token_list_append (parser->lex_from_list, node->token);
1799 parser->lex_from_node = parser->lex_from_list->head;
1801 /* It's possible the list consisted of nothing but whitespace. */
1802 if (parser->lex_from_node == NULL) {
1803 talloc_free (parser->lex_from_list);
1804 parser->lex_from_list = NULL;
1809 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1812 skip_type_t current = SKIP_NO_SKIP;
1815 if (parser->skip_stack)
1816 current = parser->skip_stack->type;
1818 node = talloc (parser, skip_node_t);
1821 if (current == SKIP_NO_SKIP) {
1823 node->type = SKIP_NO_SKIP;
1825 node->type = SKIP_TO_ELSE;
1827 node->type = SKIP_TO_ENDIF;
1830 node->next = parser->skip_stack;
1831 parser->skip_stack = node;
1835 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1836 const char *type, int condition)
1838 if (parser->skip_stack == NULL) {
1839 glcpp_error (loc, parser, "%s without #if\n", type);
1843 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1845 parser->skip_stack->type = SKIP_NO_SKIP;
1847 parser->skip_stack->type = SKIP_TO_ENDIF;
1852 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1856 if (parser->skip_stack == NULL) {
1857 glcpp_error (loc, parser, "#endif without #if\n");
1861 node = parser->skip_stack;
1862 parser->skip_stack = node->next;