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.
31 #include "main/mtypes.h"
33 #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
34 #define glcpp_printf(stream, fmt, args...) \
35 stream = talloc_asprintf_append(stream, fmt, args)
38 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
41 _define_object_macro (glcpp_parser_t *parser,
44 token_list_t *replacements);
47 _define_function_macro (glcpp_parser_t *parser,
50 string_list_t *parameters,
51 token_list_t *replacements);
53 static string_list_t *
54 _string_list_create (void *ctx);
57 _string_list_append_item (string_list_t *list, const char *str);
60 _string_list_contains (string_list_t *list, const char *member, int *index);
63 _string_list_length (string_list_t *list);
65 static argument_list_t *
66 _argument_list_create (void *ctx);
69 _argument_list_append (argument_list_t *list, token_list_t *argument);
72 _argument_list_length (argument_list_t *list);
75 _argument_list_member_at (argument_list_t *list, int index);
77 /* Note: This function talloc_steal()s the str pointer. */
79 _token_create_str (void *ctx, int type, char *str);
82 _token_create_ival (void *ctx, int type, int ival);
85 _token_list_create (void *ctx);
87 /* Note: This function adds a talloc_reference() to token.
89 * You may want to talloc_unlink any current reference if you no
92 _token_list_append (token_list_t *list, token_t *token);
95 _token_list_append_list (token_list_t *list, token_list_t *tail);
97 static active_list_t *
98 _active_list_push (active_list_t *list,
99 const char *identifier,
100 token_node_t *marker);
102 static active_list_t *
103 _active_list_pop (active_list_t *list);
106 _active_list_contains (active_list_t *list, 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 glcpp_print(parser->output, "\n");
190 _glcpp_parser_print_expanded_token_list (parser, $1);
191 glcpp_print(parser->output, "\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);
311 glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2);
318 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
319 $$ = strtoll ($1 + 2, NULL, 16);
320 } else if ($1[0] == '0') {
321 $$ = strtoll ($1, NULL, 8);
323 $$ = strtoll ($1, NULL, 10);
332 | expression OR expression {
335 | expression AND expression {
338 | expression '|' expression {
341 | expression '^' expression {
344 | expression '&' expression {
347 | expression NOT_EQUAL expression {
350 | expression EQUAL expression {
353 | expression GREATER_OR_EQUAL expression {
356 | expression LESS_OR_EQUAL expression {
359 | expression '>' expression {
362 | expression '<' expression {
365 | expression RIGHT_SHIFT expression {
368 | expression LEFT_SHIFT expression {
371 | expression '-' expression {
374 | expression '+' expression {
377 | expression '%' expression {
380 | expression '/' expression {
383 | expression '*' expression {
386 | '!' expression %prec UNARY {
389 | '~' expression %prec UNARY {
392 | '-' expression %prec UNARY {
395 | '+' expression %prec UNARY {
398 | '(' expression ')' {
405 $$ = _string_list_create (parser);
406 _string_list_append_item ($$, $1);
407 talloc_steal ($$, $1);
409 | identifier_list ',' IDENTIFIER {
411 _string_list_append_item ($$, $3);
412 talloc_steal ($$, $3);
417 NEWLINE { $$ = NULL; }
423 yyerror (& @1, parser, "Invalid tokens after #");
428 /* empty */ { $$ = NULL; }
435 glcpp_warning(&@1, parser, "extra tokens at end of directive");
440 /* Handle "defined" operator */
442 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
443 $$ = _token_create_ival (parser, INTEGER, v);
445 | DEFINED '(' IDENTIFIER ')' {
446 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
447 $$ = _token_create_ival (parser, INTEGER, v);
449 | preprocessing_token
453 /* Exactly the same as pp_tokens, but using conditional_token */
455 parser->space_tokens = 1;
456 $$ = _token_list_create (parser);
457 _token_list_append ($$, $1);
458 talloc_unlink (parser, $1);
460 | conditional_tokens conditional_token {
462 _token_list_append ($$, $2);
463 talloc_unlink (parser, $2);
468 preprocessing_token {
469 parser->space_tokens = 1;
470 $$ = _token_list_create (parser);
471 _token_list_append ($$, $1);
472 talloc_unlink (parser, $1);
474 | pp_tokens preprocessing_token {
476 _token_list_append ($$, $2);
477 talloc_unlink (parser, $2);
483 $$ = _token_create_str (parser, IDENTIFIER, $1);
484 $$->location = yylloc;
487 $$ = _token_create_str (parser, INTEGER_STRING, $1);
488 $$->location = yylloc;
491 $$ = _token_create_ival (parser, $1, $1);
492 $$->location = yylloc;
495 $$ = _token_create_str (parser, OTHER, $1);
496 $$->location = yylloc;
499 $$ = _token_create_ival (parser, SPACE, SPACE);
500 $$->location = yylloc;
520 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
521 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
524 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
525 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
526 | EQUAL { $$ = EQUAL; }
527 | NOT_EQUAL { $$ = NOT_EQUAL; }
535 | PASTE { $$ = PASTE; }
541 _string_list_create (void *ctx)
545 list = talloc (ctx, string_list_t);
553 _string_list_append_item (string_list_t *list, const char *str)
557 node = talloc (list, string_node_t);
558 node->str = talloc_strdup (node, str);
562 if (list->head == NULL) {
565 list->tail->next = node;
572 _string_list_contains (string_list_t *list, const char *member, int *index)
580 for (i = 0, node = list->head; node; i++, node = node->next) {
581 if (strcmp (node->str, member) == 0) {
592 _string_list_length (string_list_t *list)
600 for (node = list->head; node; node = node->next)
607 _argument_list_create (void *ctx)
609 argument_list_t *list;
611 list = talloc (ctx, argument_list_t);
619 _argument_list_append (argument_list_t *list, token_list_t *argument)
621 argument_node_t *node;
623 node = talloc (list, argument_node_t);
624 node->argument = argument;
628 if (list->head == NULL) {
631 list->tail->next = node;
638 _argument_list_length (argument_list_t *list)
641 argument_node_t *node;
646 for (node = list->head; node; node = node->next)
653 _argument_list_member_at (argument_list_t *list, int index)
655 argument_node_t *node;
662 for (i = 0; i < index; i++) {
669 return node->argument;
674 /* Note: This function talloc_steal()s the str pointer. */
676 _token_create_str (void *ctx, int type, char *str)
680 token = talloc (ctx, token_t);
682 token->value.str = talloc_steal (token, str);
688 _token_create_ival (void *ctx, int type, int ival)
692 token = talloc (ctx, token_t);
694 token->value.ival = ival;
700 _token_list_create (void *ctx)
704 list = talloc (ctx, token_list_t);
707 list->non_space_tail = NULL;
713 _token_list_append (token_list_t *list, token_t *token)
717 node = talloc (list, token_node_t);
718 node->token = talloc_reference (list, token);
722 if (list->head == NULL) {
725 list->tail->next = node;
729 if (token->type != SPACE)
730 list->non_space_tail = node;
734 _token_list_append_list (token_list_t *list, token_list_t *tail)
736 if (tail == NULL || tail->head == NULL)
739 if (list->head == NULL) {
740 list->head = tail->head;
742 list->tail->next = tail->head;
745 list->tail = tail->tail;
746 list->non_space_tail = tail->non_space_tail;
749 static token_list_t *
750 _token_list_copy (void *ctx, token_list_t *other)
758 copy = _token_list_create (ctx);
759 for (node = other->head; node; node = node->next)
760 _token_list_append (copy, node->token);
766 _token_list_trim_trailing_space (token_list_t *list)
768 token_node_t *tail, *next;
770 if (list->non_space_tail) {
771 tail = list->non_space_tail->next;
772 list->non_space_tail->next = NULL;
773 list->tail = list->non_space_tail;
784 _token_print (char **out, token_t *token)
786 if (token->type < 256) {
787 glcpp_printf (*out, "%c", token->type);
791 switch (token->type) {
793 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
798 glcpp_print (*out, token->value.str);
801 glcpp_print (*out, " ");
804 glcpp_print (*out, "<<");
807 glcpp_print (*out, ">>");
810 glcpp_print (*out, "<=");
812 case GREATER_OR_EQUAL:
813 glcpp_print (*out, ">=");
816 glcpp_print (*out, "==");
819 glcpp_print (*out, "!=");
822 glcpp_print (*out, "&&");
825 glcpp_print (*out, "||");
828 glcpp_print (*out, "##");
831 glcpp_print (*out, ",");
834 /* Nothing to print. */
837 assert(!"Error: Don't know how to print token.");
842 /* Return a new token (talloc()ed off of 'token') formed by pasting
843 * 'token' and 'other'. Note that this function may return 'token' or
844 * 'other' directly rather than allocating anything new.
846 * Caution: Only very cursory error-checking is performed to see if
847 * the final result is a valid single token. */
849 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
851 token_t *combined = NULL;
853 /* Pasting a placeholder onto anything makes no change. */
854 if (other->type == PLACEHOLDER)
857 /* When 'token' is a placeholder, just return 'other'. */
858 if (token->type == PLACEHOLDER)
861 /* A very few single-character punctuators can be combined
862 * with another to form a multi-character punctuator. */
863 switch (token->type) {
865 if (other->type == '<')
866 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
867 else if (other->type == '=')
868 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
871 if (other->type == '>')
872 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
873 else if (other->type == '=')
874 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
877 if (other->type == '=')
878 combined = _token_create_ival (token, EQUAL, EQUAL);
881 if (other->type == '=')
882 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
885 if (other->type == '&')
886 combined = _token_create_ival (token, AND, AND);
889 if (other->type == '|')
890 combined = _token_create_ival (token, OR, OR);
894 if (combined != NULL) {
895 /* Inherit the location from the first token */
896 combined->location = token->location;
900 /* Two string-valued tokens can usually just be mashed
903 * XXX: This isn't actually legitimate. Several things here
904 * should result in a diagnostic since the result cannot be a
905 * valid, single pre-processing token. For example, pasting
906 * "123" and "abc" is not legal, but we don't catch that
908 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
909 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
913 str = talloc_asprintf (token, "%s%s", token->value.str,
915 combined = _token_create_str (token, token->type, str);
916 combined->location = token->location;
920 glcpp_error (&token->location, parser, "");
921 glcpp_print (parser->info_log, "Pasting \"");
922 _token_print (&parser->info_log, token);
923 glcpp_print (parser->info_log, "\" and \"");
924 _token_print (&parser->info_log, other);
925 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
931 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
938 for (node = list->head; node; node = node->next)
939 _token_print (&parser->output, node->token);
943 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
945 glcpp_error(locp, parser, "%s", error);
948 static void add_builtin_define(glcpp_parser_t *parser,
949 const char *name, int value)
954 tok = _token_create_ival (parser, INTEGER, value);
956 list = _token_list_create(parser);
957 _token_list_append(list, tok);
958 _define_object_macro(parser, NULL, name, list);
960 talloc_unlink(parser, tok);
964 glcpp_parser_create (const struct gl_extensions *extensions)
966 glcpp_parser_t *parser;
967 int language_version;
969 parser = talloc (NULL, glcpp_parser_t);
971 glcpp_lex_init_extra (parser, &parser->scanner);
972 parser->defines = hash_table_ctor (32, hash_table_string_hash,
973 hash_table_string_compare);
974 parser->active = NULL;
975 parser->lexing_if = 0;
976 parser->space_tokens = 1;
977 parser->newline_as_space = 0;
978 parser->in_control_line = 0;
979 parser->paren_count = 0;
981 parser->skip_stack = NULL;
983 parser->lex_from_list = NULL;
984 parser->lex_from_node = NULL;
986 parser->output = talloc_strdup(parser, "");
987 parser->info_log = talloc_strdup(parser, "");
990 /* Add pre-defined macros. */
991 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
992 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
994 if (extensions != NULL) {
995 if (extensions->EXT_texture_array) {
996 add_builtin_define(parser, "GL_EXT_texture_array", 1);
999 if (extensions->ARB_fragment_coord_conventions)
1000 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1004 language_version = 110;
1005 add_builtin_define(parser, "__VERSION__", language_version);
1011 glcpp_parser_parse (glcpp_parser_t *parser)
1013 return yyparse (parser);
1017 glcpp_parser_destroy (glcpp_parser_t *parser)
1019 if (parser->skip_stack)
1020 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
1021 glcpp_lex_destroy (parser->scanner);
1022 hash_table_dtor (parser->defines);
1023 talloc_free (parser);
1026 typedef enum function_status
1028 FUNCTION_STATUS_SUCCESS,
1029 FUNCTION_NOT_A_FUNCTION,
1030 FUNCTION_UNBALANCED_PARENTHESES
1031 } function_status_t;
1033 /* Find a set of function-like macro arguments by looking for a
1034 * balanced set of parentheses.
1036 * When called, 'node' should be the opening-parenthesis token, (or
1037 * perhaps preceeding SPACE tokens). Upon successful return *last will
1038 * be the last consumed node, (corresponding to the closing right
1043 * FUNCTION_STATUS_SUCCESS:
1045 * Successfully parsed a set of function arguments.
1047 * FUNCTION_NOT_A_FUNCTION:
1049 * Macro name not followed by a '('. This is not an error, but
1050 * simply that the macro name should be treated as a non-macro.
1052 * FUNCTION_UNBALANCED_PARENTHESES
1054 * Macro name is not followed by a balanced set of parentheses.
1056 static function_status_t
1057 _arguments_parse (argument_list_t *arguments,
1059 token_node_t **last)
1061 token_list_t *argument;
1066 /* Ignore whitespace before first parenthesis. */
1067 while (node && node->token->type == SPACE)
1070 if (node == NULL || node->token->type != '(')
1071 return FUNCTION_NOT_A_FUNCTION;
1075 argument = _token_list_create (arguments);
1076 _argument_list_append (arguments, argument);
1078 for (paren_count = 1; node; node = node->next) {
1079 if (node->token->type == '(')
1083 else if (node->token->type == ')')
1086 if (paren_count == 0)
1090 if (node->token->type == ',' &&
1093 _token_list_trim_trailing_space (argument);
1094 argument = _token_list_create (arguments);
1095 _argument_list_append (arguments, argument);
1098 if (argument->head == NULL) {
1099 /* Don't treat initial whitespace as
1100 * part of the arguement. */
1101 if (node->token->type == SPACE)
1104 _token_list_append (argument, node->token);
1109 return FUNCTION_UNBALANCED_PARENTHESES;
1113 return FUNCTION_STATUS_SUCCESS;
1116 static token_list_t *
1117 _token_list_create_with_one_space (void *ctx)
1122 list = _token_list_create (ctx);
1123 space = _token_create_ival (list, SPACE, SPACE);
1124 _token_list_append (list, space);
1130 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1132 token_list_t *expanded;
1135 expanded = _token_list_create (parser);
1136 token = _token_create_ival (parser, type, type);
1137 _token_list_append (expanded, token);
1138 _glcpp_parser_expand_token_list (parser, list);
1139 _token_list_append_list (expanded, list);
1140 glcpp_parser_lex_from (parser, expanded);
1143 /* This is a helper function that's essentially part of the
1144 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1145 * except for by that function.
1147 * Returns NULL if node is a simple token with no expansion, (that is,
1148 * although 'node' corresponds to an identifier defined as a
1149 * function-like macro, it is not followed with a parenthesized
1152 * Compute the complete expansion of node (which is a function-like
1153 * macro) and subsequent nodes which are arguments.
1155 * Returns the token list that results from the expansion and sets
1156 * *last to the last node in the list that was consumed by the
1157 * expansion. Specifically, *last will be set as follows: as the
1158 * token of the closing right parenthesis.
1160 static token_list_t *
1161 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1163 token_node_t **last)
1167 const char *identifier;
1168 argument_list_t *arguments;
1169 function_status_t status;
1170 token_list_t *substituted;
1171 int parameter_index;
1173 identifier = node->token->value.str;
1175 macro = hash_table_find (parser->defines, identifier);
1177 assert (macro->is_function);
1179 arguments = _argument_list_create (parser);
1180 status = _arguments_parse (arguments, node, last);
1183 case FUNCTION_STATUS_SUCCESS:
1185 case FUNCTION_NOT_A_FUNCTION:
1187 case FUNCTION_UNBALANCED_PARENTHESES:
1188 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1192 /* Replace a macro defined as empty with a SPACE token. */
1193 if (macro->replacements == NULL) {
1194 talloc_free (arguments);
1195 return _token_list_create_with_one_space (parser);
1198 if (! ((_argument_list_length (arguments) ==
1199 _string_list_length (macro->parameters)) ||
1200 (_string_list_length (macro->parameters) == 0 &&
1201 _argument_list_length (arguments) == 1 &&
1202 arguments->head->argument->head == NULL)))
1204 glcpp_error (&node->token->location, parser,
1205 "Error: macro %s invoked with %d arguments (expected %d)\n",
1207 _argument_list_length (arguments),
1208 _string_list_length (macro->parameters));
1212 /* Perform argument substitution on the replacement list. */
1213 substituted = _token_list_create (arguments);
1215 for (node = macro->replacements->head; node; node = node->next)
1217 if (node->token->type == IDENTIFIER &&
1218 _string_list_contains (macro->parameters,
1219 node->token->value.str,
1222 token_list_t *argument;
1223 argument = _argument_list_member_at (arguments,
1225 /* Before substituting, we expand the argument
1226 * tokens, or append a placeholder token for
1227 * an empty argument. */
1228 if (argument->head) {
1229 token_list_t *expanded_argument;
1230 expanded_argument = _token_list_copy (parser,
1232 _glcpp_parser_expand_token_list (parser,
1234 _token_list_append_list (substituted,
1239 new_token = _token_create_ival (substituted,
1242 _token_list_append (substituted, new_token);
1245 _token_list_append (substituted, node->token);
1249 /* After argument substitution, and before further expansion
1250 * below, implement token pasting. */
1252 _token_list_trim_trailing_space (substituted);
1254 node = substituted->head;
1257 token_node_t *next_non_space;
1259 /* Look ahead for a PASTE token, skipping space. */
1260 next_non_space = node->next;
1261 while (next_non_space && next_non_space->token->type == SPACE)
1262 next_non_space = next_non_space->next;
1264 if (next_non_space == NULL)
1267 if (next_non_space->token->type != PASTE) {
1268 node = next_non_space;
1272 /* Now find the next non-space token after the PASTE. */
1273 next_non_space = next_non_space->next;
1274 while (next_non_space && next_non_space->token->type == SPACE)
1275 next_non_space = next_non_space->next;
1277 if (next_non_space == NULL) {
1278 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1282 node->token = _token_paste (parser, node->token, next_non_space->token);
1283 node->next = next_non_space->next;
1284 if (next_non_space == substituted->tail)
1285 substituted->tail = node;
1290 substituted->non_space_tail = substituted->tail;
1295 /* Compute the complete expansion of node, (and subsequent nodes after
1296 * 'node' in the case that 'node' is a function-like macro and
1297 * subsequent nodes are arguments).
1299 * Returns NULL if node is a simple token with no expansion.
1301 * Otherwise, returns the token list that results from the expansion
1302 * and sets *last to the last node in the list that was consumed by
1303 * the expansion. Specifically, *last will be set as follows:
1305 * As 'node' in the case of object-like macro expansion.
1307 * As the token of the closing right parenthesis in the case of
1308 * function-like macro expansion.
1310 static token_list_t *
1311 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1313 token_node_t **last)
1315 token_t *token = node->token;
1316 const char *identifier;
1319 /* We only expand identifiers */
1320 if (token->type != IDENTIFIER) {
1321 /* We change any COMMA into a COMMA_FINAL to prevent
1322 * it being mistaken for an argument separator
1324 if (token->type == ',') {
1325 token->type = COMMA_FINAL;
1326 token->value.ival = COMMA_FINAL;
1332 /* Look up this identifier in the hash table. */
1333 identifier = token->value.str;
1334 macro = hash_table_find (parser->defines, identifier);
1336 /* Not a macro, so no expansion needed. */
1340 /* Finally, don't expand this macro if we're already actively
1341 * expanding it, (to avoid infinite recursion). */
1342 if (_active_list_contains (parser->active, identifier)) {
1343 /* We change the token type here from IDENTIFIER to
1344 * OTHER to prevent any future expansion of this
1345 * unexpanded token. */
1347 token_list_t *expansion;
1350 str = talloc_strdup (parser, token->value.str);
1351 final = _token_create_str (parser, OTHER, str);
1352 expansion = _token_list_create (parser);
1353 _token_list_append (expansion, final);
1358 if (! macro->is_function)
1362 /* Replace a macro defined as empty with a SPACE token. */
1363 if (macro->replacements == NULL)
1364 return _token_list_create_with_one_space (parser);
1366 return _token_list_copy (parser, macro->replacements);
1369 return _glcpp_parser_expand_function (parser, node, last);
1372 /* Push a new identifier onto the active list, returning the new list.
1374 * Here, 'marker' is the token node that appears in the list after the
1375 * expansion of 'identifier'. That is, when the list iterator begins
1376 * examinging 'marker', then it is time to pop this node from the
1380 _active_list_push (active_list_t *list,
1381 const char *identifier,
1382 token_node_t *marker)
1384 active_list_t *node;
1386 node = talloc (list, active_list_t);
1387 node->identifier = talloc_strdup (node, identifier);
1388 node->marker = marker;
1395 _active_list_pop (active_list_t *list)
1397 active_list_t *node = list;
1409 _active_list_contains (active_list_t *list, const char *identifier)
1411 active_list_t *node;
1416 for (node = list; node; node = node->next)
1417 if (strcmp (node->identifier, identifier) == 0)
1423 /* Walk over the token list replacing nodes with their expansion.
1424 * Whenever nodes are expanded the walking will walk over the new
1425 * nodes, continuing to expand as necessary. The results are placed in
1429 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1432 token_node_t *node_prev;
1433 token_node_t *node, *last = NULL;
1434 token_list_t *expansion;
1439 _token_list_trim_trailing_space (list);
1446 while (parser->active && parser->active->marker == node)
1447 parser->active = _active_list_pop (parser->active);
1449 /* Find the expansion for node, which will replace all
1450 * nodes from node to last, inclusive. */
1451 expansion = _glcpp_parser_expand_node (parser, node, &last);
1455 for (n = node; n != last->next; n = n->next)
1456 while (parser->active &&
1457 parser->active->marker == n)
1459 parser->active = _active_list_pop (parser->active);
1462 parser->active = _active_list_push (parser->active,
1463 node->token->value.str,
1466 /* Splice expansion into list, supporting a
1467 * simple deletion if the expansion is
1469 if (expansion->head) {
1471 node_prev->next = expansion->head;
1473 list->head = expansion->head;
1474 expansion->tail->next = last->next;
1475 if (last == list->tail)
1476 list->tail = expansion->tail;
1479 node_prev->next = last->next;
1481 list->head = last->next;
1482 if (last == list->tail)
1488 node = node_prev ? node_prev->next : list->head;
1491 while (parser->active)
1492 parser->active = _active_list_pop (parser->active);
1494 list->non_space_tail = list->tail;
1498 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1504 _glcpp_parser_expand_token_list (parser, list);
1506 _token_list_trim_trailing_space (list);
1508 _token_list_print (parser, list);
1512 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1513 const char *identifier)
1515 /* According to the GLSL specification, macro names starting with "__"
1516 * or "GL_" are reserved for future use. So, don't allow them.
1518 if (strncmp(identifier, "__", 2) == 0) {
1519 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1521 if (strncmp(identifier, "GL_", 3) == 0) {
1522 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1527 _define_object_macro (glcpp_parser_t *parser,
1529 const char *identifier,
1530 token_list_t *replacements)
1535 _check_for_reserved_macro_name(parser, loc, identifier);
1537 macro = talloc (parser, macro_t);
1539 macro->is_function = 0;
1540 macro->parameters = NULL;
1541 macro->identifier = talloc_strdup (macro, identifier);
1542 macro->replacements = talloc_steal (macro, replacements);
1544 hash_table_insert (parser->defines, macro, identifier);
1548 _define_function_macro (glcpp_parser_t *parser,
1550 const char *identifier,
1551 string_list_t *parameters,
1552 token_list_t *replacements)
1556 _check_for_reserved_macro_name(parser, loc, identifier);
1558 macro = talloc (parser, macro_t);
1560 macro->is_function = 1;
1561 macro->parameters = talloc_steal (macro, parameters);
1562 macro->identifier = talloc_strdup (macro, identifier);
1563 macro->replacements = talloc_steal (macro, replacements);
1565 hash_table_insert (parser->defines, macro, identifier);
1569 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1574 if (parser->lex_from_list == NULL) {
1575 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1577 /* XXX: This ugly block of code exists for the sole
1578 * purpose of converting a NEWLINE token into a SPACE
1579 * token, but only in the case where we have seen a
1580 * function-like macro name, but have not yet seen its
1581 * closing parenthesis.
1583 * There's perhaps a more compact way to do this with
1584 * mid-rule actions in the grammar.
1586 * I'm definitely not pleased with the complexity of
1589 if (parser->newline_as_space)
1592 parser->paren_count++;
1593 } else if (ret == ')') {
1594 parser->paren_count--;
1595 if (parser->paren_count == 0)
1596 parser->newline_as_space = 0;
1597 } else if (ret == NEWLINE) {
1599 } else if (ret != SPACE) {
1600 if (parser->paren_count == 0)
1601 parser->newline_as_space = 0;
1604 else if (parser->in_control_line)
1607 parser->in_control_line = 0;
1609 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1610 ret == HASH_UNDEF || ret == HASH_IF ||
1611 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1612 ret == HASH_ELIF || ret == HASH_ELSE ||
1613 ret == HASH_ENDIF || ret == HASH)
1615 parser->in_control_line = 1;
1617 else if (ret == IDENTIFIER)
1620 macro = hash_table_find (parser->defines,
1622 if (macro && macro->is_function) {
1623 parser->newline_as_space = 1;
1624 parser->paren_count = 0;
1631 node = parser->lex_from_node;
1634 talloc_free (parser->lex_from_list);
1635 parser->lex_from_list = NULL;
1639 *yylval = node->token->value;
1640 ret = node->token->type;
1642 parser->lex_from_node = node->next;
1648 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1652 assert (parser->lex_from_list == NULL);
1654 /* Copy list, eliminating any space tokens. */
1655 parser->lex_from_list = _token_list_create (parser);
1657 for (node = list->head; node; node = node->next) {
1658 if (node->token->type == SPACE)
1660 _token_list_append (parser->lex_from_list, node->token);
1665 parser->lex_from_node = parser->lex_from_list->head;
1667 /* It's possible the list consisted of nothing but whitespace. */
1668 if (parser->lex_from_node == NULL) {
1669 talloc_free (parser->lex_from_list);
1670 parser->lex_from_list = NULL;
1675 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1678 skip_type_t current = SKIP_NO_SKIP;
1681 if (parser->skip_stack)
1682 current = parser->skip_stack->type;
1684 node = talloc (parser, skip_node_t);
1687 if (current == SKIP_NO_SKIP) {
1689 node->type = SKIP_NO_SKIP;
1691 node->type = SKIP_TO_ELSE;
1693 node->type = SKIP_TO_ENDIF;
1696 node->next = parser->skip_stack;
1697 parser->skip_stack = node;
1701 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1702 const char *type, int condition)
1704 if (parser->skip_stack == NULL) {
1705 glcpp_error (loc, parser, "%s without #if\n", type);
1709 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1711 parser->skip_stack->type = SKIP_NO_SKIP;
1713 parser->skip_stack->type = SKIP_TO_ENDIF;
1718 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1722 if (parser->skip_stack == NULL) {
1723 glcpp_error (loc, parser, "#endif without #if\n");
1727 node = parser->skip_stack;
1728 parser->skip_stack = node->next;