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_token_list (glcpp_parser_t *parser,
113 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
117 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
121 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
122 const char *type, int condition);
125 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
127 #define yylex glcpp_parser_lex
130 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
133 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
141 %parse-param {glcpp_parser_t *parser}
142 %lex-param {glcpp_parser_t *parser}
144 %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 IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
146 %type <ival> expression INTEGER operator SPACE
147 %type <str> IDENTIFIER INTEGER_STRING OTHER
148 %type <string_list> identifier_list
149 %type <token> preprocessing_token conditional_token
150 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
156 %left EQUAL NOT_EQUAL
157 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
158 %left LEFT_SHIFT RIGHT_SHIFT
172 glcpp_print(parser->output, "\n");
175 _glcpp_parser_print_expanded_token_list (parser, $1);
176 glcpp_print(parser->output, "\n");
184 IF_EXPANDED expression NEWLINE {
185 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
187 | ELIF_EXPANDED expression NEWLINE {
188 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
193 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
194 _define_object_macro (parser, & @2, $2, $3);
196 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
197 _define_function_macro (parser, & @2, $2, NULL, $5);
199 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
200 _define_function_macro (parser, & @2, $2, $4, $6);
202 | HASH_UNDEF IDENTIFIER NEWLINE {
203 macro_t *macro = hash_table_find (parser->defines, $2);
205 hash_table_remove (parser->defines, $2);
210 | HASH_IF conditional_tokens NEWLINE {
211 token_list_t *expanded;
214 expanded = _token_list_create (parser);
215 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
216 _token_list_append (expanded, token);
217 talloc_unlink (parser, token);
218 _glcpp_parser_expand_token_list (parser, $2);
219 _token_list_append_list (expanded, $2);
220 glcpp_parser_lex_from (parser, expanded);
222 | HASH_IFDEF IDENTIFIER junk NEWLINE {
223 macro_t *macro = hash_table_find (parser->defines, $2);
225 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
227 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
228 macro_t *macro = hash_table_find (parser->defines, $2);
230 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
232 | HASH_ELIF conditional_tokens NEWLINE {
233 token_list_t *expanded;
236 expanded = _token_list_create (parser);
237 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
238 _token_list_append (expanded, token);
239 talloc_unlink (parser, token);
240 _glcpp_parser_expand_token_list (parser, $2);
241 _token_list_append_list (expanded, $2);
242 glcpp_parser_lex_from (parser, expanded);
244 | HASH_ELIF NEWLINE {
245 /* #elif without an expression results in a warning if the
246 * condition doesn't matter (we just handled #if 1 or such)
247 * but an error otherwise. */
248 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
249 parser->skip_stack->type = SKIP_TO_ENDIF;
250 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
252 glcpp_error(& @1, parser, "#elif needs an expression");
255 | HASH_ELSE NEWLINE {
256 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
258 | HASH_ENDIF NEWLINE {
259 _glcpp_parser_skip_stack_pop (parser, & @1);
266 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
267 $$ = strtoll ($1 + 2, NULL, 16);
268 } else if ($1[0] == '0') {
269 $$ = strtoll ($1, NULL, 8);
271 $$ = strtoll ($1, NULL, 10);
277 | expression OR expression {
280 | expression AND expression {
283 | expression '|' expression {
286 | expression '^' expression {
289 | expression '&' expression {
292 | expression NOT_EQUAL expression {
295 | expression EQUAL expression {
298 | expression GREATER_OR_EQUAL expression {
301 | expression LESS_OR_EQUAL expression {
304 | expression '>' expression {
307 | expression '<' expression {
310 | expression RIGHT_SHIFT expression {
313 | expression LEFT_SHIFT expression {
316 | expression '-' expression {
319 | expression '+' expression {
322 | expression '%' expression {
325 | expression '/' expression {
328 | expression '*' expression {
331 | '!' expression %prec UNARY {
334 | '~' expression %prec UNARY {
337 | '-' expression %prec UNARY {
340 | '+' expression %prec UNARY {
343 | '(' expression ')' {
350 $$ = _string_list_create (parser);
351 _string_list_append_item ($$, $1);
352 talloc_steal ($$, $1);
354 | identifier_list ',' IDENTIFIER {
356 _string_list_append_item ($$, $3);
357 talloc_steal ($$, $3);
362 NEWLINE { $$ = NULL; }
368 yyerror (& @1, parser, "Invalid tokens after #");
373 /* empty */ { $$ = NULL; }
380 glcpp_warning(&@1, parser, "extra tokens at end of directive");
385 /* Handle "defined" operator */
387 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
388 $$ = _token_create_ival (parser, INTEGER, v);
390 | DEFINED '(' IDENTIFIER ')' {
391 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
392 $$ = _token_create_ival (parser, INTEGER, v);
394 | preprocessing_token
398 /* Exactly the same as pp_tokens, but using conditional_token */
400 parser->space_tokens = 1;
401 $$ = _token_list_create (parser);
402 _token_list_append ($$, $1);
403 talloc_unlink (parser, $1);
405 | conditional_tokens conditional_token {
407 _token_list_append ($$, $2);
408 talloc_unlink (parser, $2);
413 preprocessing_token {
414 parser->space_tokens = 1;
415 $$ = _token_list_create (parser);
416 _token_list_append ($$, $1);
417 talloc_unlink (parser, $1);
419 | pp_tokens preprocessing_token {
421 _token_list_append ($$, $2);
422 talloc_unlink (parser, $2);
428 $$ = _token_create_str (parser, IDENTIFIER, $1);
429 $$->location = yylloc;
432 $$ = _token_create_str (parser, INTEGER_STRING, $1);
433 $$->location = yylloc;
436 $$ = _token_create_ival (parser, $1, $1);
437 $$->location = yylloc;
440 $$ = _token_create_str (parser, OTHER, $1);
441 $$->location = yylloc;
444 $$ = _token_create_ival (parser, SPACE, SPACE);
445 $$->location = yylloc;
465 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
466 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
469 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
470 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
471 | EQUAL { $$ = EQUAL; }
472 | NOT_EQUAL { $$ = NOT_EQUAL; }
480 | PASTE { $$ = PASTE; }
481 | DEFINED { $$ = DEFINED; }
487 _string_list_create (void *ctx)
491 list = xtalloc (ctx, string_list_t);
499 _string_list_append_item (string_list_t *list, const char *str)
503 node = xtalloc (list, string_node_t);
504 node->str = xtalloc_strdup (node, str);
508 if (list->head == NULL) {
511 list->tail->next = node;
518 _string_list_contains (string_list_t *list, const char *member, int *index)
526 for (i = 0, node = list->head; node; i++, node = node->next) {
527 if (strcmp (node->str, member) == 0) {
538 _string_list_length (string_list_t *list)
546 for (node = list->head; node; node = node->next)
553 _argument_list_create (void *ctx)
555 argument_list_t *list;
557 list = xtalloc (ctx, argument_list_t);
565 _argument_list_append (argument_list_t *list, token_list_t *argument)
567 argument_node_t *node;
569 node = xtalloc (list, argument_node_t);
570 node->argument = argument;
574 if (list->head == NULL) {
577 list->tail->next = node;
584 _argument_list_length (argument_list_t *list)
587 argument_node_t *node;
592 for (node = list->head; node; node = node->next)
599 _argument_list_member_at (argument_list_t *list, int index)
601 argument_node_t *node;
608 for (i = 0; i < index; i++) {
615 return node->argument;
620 /* Note: This function talloc_steal()s the str pointer. */
622 _token_create_str (void *ctx, int type, char *str)
626 token = xtalloc (ctx, token_t);
628 token->value.str = talloc_steal (token, str);
634 _token_create_ival (void *ctx, int type, int ival)
638 token = xtalloc (ctx, token_t);
640 token->value.ival = ival;
646 _token_list_create (void *ctx)
650 list = xtalloc (ctx, token_list_t);
653 list->non_space_tail = NULL;
659 _token_list_append (token_list_t *list, token_t *token)
663 node = xtalloc (list, token_node_t);
664 node->token = xtalloc_reference (list, token);
668 if (list->head == NULL) {
671 list->tail->next = node;
675 if (token->type != SPACE)
676 list->non_space_tail = node;
680 _token_list_append_list (token_list_t *list, token_list_t *tail)
682 if (tail == NULL || tail->head == NULL)
685 if (list->head == NULL) {
686 list->head = tail->head;
688 list->tail->next = tail->head;
691 list->tail = tail->tail;
692 list->non_space_tail = tail->non_space_tail;
695 static token_list_t *
696 _token_list_copy (void *ctx, token_list_t *other)
704 copy = _token_list_create (ctx);
705 for (node = other->head; node; node = node->next)
706 _token_list_append (copy, node->token);
712 _token_list_trim_trailing_space (token_list_t *list)
714 token_node_t *tail, *next;
716 if (list->non_space_tail) {
717 tail = list->non_space_tail->next;
718 list->non_space_tail->next = NULL;
719 list->tail = list->non_space_tail;
730 _token_print (char **out, token_t *token)
732 if (token->type < 256) {
733 glcpp_printf (*out, "%c", token->type);
737 switch (token->type) {
739 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
744 glcpp_print (*out, token->value.str);
747 glcpp_print (*out, " ");
750 glcpp_print (*out, "<<");
753 glcpp_print (*out, ">>");
756 glcpp_print (*out, "<=");
758 case GREATER_OR_EQUAL:
759 glcpp_print (*out, ">=");
762 glcpp_print (*out, "==");
765 glcpp_print (*out, "!=");
768 glcpp_print (*out, "&&");
771 glcpp_print (*out, "||");
774 glcpp_print (*out, "##");
777 glcpp_print (*out, ",");
780 /* Nothing to print. */
783 assert(!"Error: Don't know how to print token.");
788 /* Return a new token (talloc()ed off of 'token') formed by pasting
789 * 'token' and 'other'. Note that this function may return 'token' or
790 * 'other' directly rather than allocating anything new.
792 * Caution: Only very cursory error-checking is performed to see if
793 * the final result is a valid single token. */
795 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
797 token_t *combined = NULL;
799 /* Pasting a placeholder onto anything makes no change. */
800 if (other->type == PLACEHOLDER)
803 /* When 'token' is a placeholder, just return 'other'. */
804 if (token->type == PLACEHOLDER)
807 /* A very few single-character punctuators can be combined
808 * with another to form a multi-character punctuator. */
809 switch (token->type) {
811 if (other->type == '<')
812 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
813 else if (other->type == '=')
814 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
817 if (other->type == '>')
818 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
819 else if (other->type == '=')
820 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
823 if (other->type == '=')
824 combined = _token_create_ival (token, EQUAL, EQUAL);
827 if (other->type == '=')
828 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
831 if (other->type == '&')
832 combined = _token_create_ival (token, AND, AND);
835 if (other->type == '|')
836 combined = _token_create_ival (token, OR, OR);
840 if (combined != NULL) {
841 /* Inherit the location from the first token */
842 combined->location = token->location;
846 /* Two string-valued tokens can usually just be mashed
849 * XXX: This isn't actually legitimate. Several things here
850 * should result in a diagnostic since the result cannot be a
851 * valid, single pre-processing token. For example, pasting
852 * "123" and "abc" is not legal, but we don't catch that
854 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
855 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
859 str = xtalloc_asprintf (token, "%s%s",
860 token->value.str, other->value.str);
861 combined = _token_create_str (token, token->type, str);
862 combined->location = token->location;
866 glcpp_error (&token->location, parser, "");
867 glcpp_print (parser->info_log, "Pasting \"");
868 _token_print (&parser->info_log, token);
869 glcpp_print (parser->info_log, "\" and \"");
870 _token_print (&parser->info_log, other);
871 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
877 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
884 for (node = list->head; node; node = node->next)
885 _token_print (&parser->output, node->token);
889 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
891 glcpp_error(locp, parser, "%s", error);
895 glcpp_parser_create (const struct gl_extensions *extensions)
897 glcpp_parser_t *parser;
901 parser = xtalloc (NULL, glcpp_parser_t);
903 glcpp_lex_init_extra (parser, &parser->scanner);
904 parser->defines = hash_table_ctor (32, hash_table_string_hash,
905 hash_table_string_compare);
906 parser->active = NULL;
907 parser->lexing_if = 0;
908 parser->space_tokens = 1;
909 parser->newline_as_space = 0;
910 parser->in_control_line = 0;
911 parser->paren_count = 0;
913 parser->skip_stack = NULL;
915 parser->lex_from_list = NULL;
916 parser->lex_from_node = NULL;
918 parser->output = talloc_strdup(parser, "");
919 parser->info_log = talloc_strdup(parser, "");
922 /* Add pre-defined macros. */
923 tok = _token_create_ival (parser, INTEGER, 1);
925 list = _token_list_create(parser);
926 _token_list_append(list, tok);
927 _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list);
929 list = _token_list_create(parser);
930 _token_list_append(list, tok);
931 _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list);
933 if ((extensions != NULL) && extensions->EXT_texture_array) {
934 list = _token_list_create(parser);
935 _token_list_append(list, tok);
936 _define_object_macro(parser, NULL,
937 "GL_EXT_texture_array", list);
940 talloc_unlink(parser, tok);
946 glcpp_parser_parse (glcpp_parser_t *parser)
948 return yyparse (parser);
952 glcpp_parser_destroy (glcpp_parser_t *parser)
954 if (parser->skip_stack)
955 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
956 glcpp_lex_destroy (parser->scanner);
957 hash_table_dtor (parser->defines);
958 talloc_free (parser);
961 typedef enum function_status
963 FUNCTION_STATUS_SUCCESS,
964 FUNCTION_NOT_A_FUNCTION,
965 FUNCTION_UNBALANCED_PARENTHESES
968 /* Find a set of function-like macro arguments by looking for a
969 * balanced set of parentheses.
971 * When called, 'node' should be the opening-parenthesis token, (or
972 * perhaps preceeding SPACE tokens). Upon successful return *last will
973 * be the last consumed node, (corresponding to the closing right
978 * FUNCTION_STATUS_SUCCESS:
980 * Successfully parsed a set of function arguments.
982 * FUNCTION_NOT_A_FUNCTION:
984 * Macro name not followed by a '('. This is not an error, but
985 * simply that the macro name should be treated as a non-macro.
987 * FUNCTION_UNBALANCED_PARENTHESES
989 * Macro name is not followed by a balanced set of parentheses.
991 static function_status_t
992 _arguments_parse (argument_list_t *arguments,
996 token_list_t *argument;
1001 /* Ignore whitespace before first parenthesis. */
1002 while (node && node->token->type == SPACE)
1005 if (node == NULL || node->token->type != '(')
1006 return FUNCTION_NOT_A_FUNCTION;
1010 argument = _token_list_create (arguments);
1011 _argument_list_append (arguments, argument);
1013 for (paren_count = 1; node; node = node->next) {
1014 if (node->token->type == '(')
1018 else if (node->token->type == ')')
1021 if (paren_count == 0)
1025 if (node->token->type == ',' &&
1028 _token_list_trim_trailing_space (argument);
1029 argument = _token_list_create (arguments);
1030 _argument_list_append (arguments, argument);
1033 if (argument->head == NULL) {
1034 /* Don't treat initial whitespace as
1035 * part of the arguement. */
1036 if (node->token->type == SPACE)
1039 _token_list_append (argument, node->token);
1044 return FUNCTION_UNBALANCED_PARENTHESES;
1048 return FUNCTION_STATUS_SUCCESS;
1051 static token_list_t *
1052 _token_list_create_with_one_space (void *ctx)
1057 list = _token_list_create (ctx);
1058 space = _token_create_ival (list, SPACE, SPACE);
1059 _token_list_append (list, space);
1064 /* This is a helper function that's essentially part of the
1065 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1066 * except for by that function.
1068 * Returns NULL if node is a simple token with no expansion, (that is,
1069 * although 'node' corresponds to an identifier defined as a
1070 * function-like macro, it is not followed with a parenthesized
1073 * Compute the complete expansion of node (which is a function-like
1074 * macro) and subsequent nodes which are arguments.
1076 * Returns the token list that results from the expansion and sets
1077 * *last to the last node in the list that was consumed by the
1078 * expansion. Specifically, *last will be set as follows: as the
1079 * token of the closing right parenthesis.
1081 static token_list_t *
1082 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1084 token_node_t **last)
1088 const char *identifier;
1089 argument_list_t *arguments;
1090 function_status_t status;
1091 token_list_t *substituted;
1092 int parameter_index;
1094 identifier = node->token->value.str;
1096 macro = hash_table_find (parser->defines, identifier);
1098 assert (macro->is_function);
1100 arguments = _argument_list_create (parser);
1101 status = _arguments_parse (arguments, node, last);
1104 case FUNCTION_STATUS_SUCCESS:
1106 case FUNCTION_NOT_A_FUNCTION:
1108 case FUNCTION_UNBALANCED_PARENTHESES:
1109 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1113 /* Replace a macro defined as empty with a SPACE token. */
1114 if (macro->replacements == NULL) {
1115 talloc_free (arguments);
1116 return _token_list_create_with_one_space (parser);
1119 if (! ((_argument_list_length (arguments) ==
1120 _string_list_length (macro->parameters)) ||
1121 (_string_list_length (macro->parameters) == 0 &&
1122 _argument_list_length (arguments) == 1 &&
1123 arguments->head->argument->head == NULL)))
1125 glcpp_error (&node->token->location, parser,
1126 "Error: macro %s invoked with %d arguments (expected %d)\n",
1128 _argument_list_length (arguments),
1129 _string_list_length (macro->parameters));
1133 /* Perform argument substitution on the replacement list. */
1134 substituted = _token_list_create (arguments);
1136 for (node = macro->replacements->head; node; node = node->next)
1138 if (node->token->type == IDENTIFIER &&
1139 _string_list_contains (macro->parameters,
1140 node->token->value.str,
1143 token_list_t *argument;
1144 argument = _argument_list_member_at (arguments,
1146 /* Before substituting, we expand the argument
1147 * tokens, or append a placeholder token for
1148 * an empty argument. */
1149 if (argument->head) {
1150 token_list_t *expanded_argument;
1151 expanded_argument = _token_list_copy (parser,
1153 _glcpp_parser_expand_token_list (parser,
1155 _token_list_append_list (substituted,
1160 new_token = _token_create_ival (substituted,
1163 _token_list_append (substituted, new_token);
1166 _token_list_append (substituted, node->token);
1170 /* After argument substitution, and before further expansion
1171 * below, implement token pasting. */
1173 _token_list_trim_trailing_space (substituted);
1175 node = substituted->head;
1178 token_node_t *next_non_space;
1180 /* Look ahead for a PASTE token, skipping space. */
1181 next_non_space = node->next;
1182 while (next_non_space && next_non_space->token->type == SPACE)
1183 next_non_space = next_non_space->next;
1185 if (next_non_space == NULL)
1188 if (next_non_space->token->type != PASTE) {
1189 node = next_non_space;
1193 /* Now find the next non-space token after the PASTE. */
1194 next_non_space = next_non_space->next;
1195 while (next_non_space && next_non_space->token->type == SPACE)
1196 next_non_space = next_non_space->next;
1198 if (next_non_space == NULL) {
1199 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1203 node->token = _token_paste (parser, node->token, next_non_space->token);
1204 node->next = next_non_space->next;
1205 if (next_non_space == substituted->tail)
1206 substituted->tail = node;
1211 substituted->non_space_tail = substituted->tail;
1216 /* Compute the complete expansion of node, (and subsequent nodes after
1217 * 'node' in the case that 'node' is a function-like macro and
1218 * subsequent nodes are arguments).
1220 * Returns NULL if node is a simple token with no expansion.
1222 * Otherwise, returns the token list that results from the expansion
1223 * and sets *last to the last node in the list that was consumed by
1224 * the expansion. Specifically, *last will be set as follows:
1226 * As 'node' in the case of object-like macro expansion.
1228 * As the token of the closing right parenthesis in the case of
1229 * function-like macro expansion.
1231 static token_list_t *
1232 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1234 token_node_t **last)
1236 token_t *token = node->token;
1237 const char *identifier;
1240 /* We only expand identifiers */
1241 if (token->type != IDENTIFIER) {
1242 /* We change any COMMA into a COMMA_FINAL to prevent
1243 * it being mistaken for an argument separator
1245 if (token->type == ',') {
1246 token->type = COMMA_FINAL;
1247 token->value.ival = COMMA_FINAL;
1253 /* Look up this identifier in the hash table. */
1254 identifier = token->value.str;
1255 macro = hash_table_find (parser->defines, identifier);
1257 /* Not a macro, so no expansion needed. */
1261 /* Finally, don't expand this macro if we're already actively
1262 * expanding it, (to avoid infinite recursion). */
1263 if (_active_list_contains (parser->active, identifier)) {
1264 /* We change the token type here from IDENTIFIER to
1265 * OTHER to prevent any future expansion of this
1266 * unexpanded token. */
1268 token_list_t *expansion;
1271 str = xtalloc_strdup (parser, token->value.str);
1272 final = _token_create_str (parser, OTHER, str);
1273 expansion = _token_list_create (parser);
1274 _token_list_append (expansion, final);
1279 if (! macro->is_function)
1283 /* Replace a macro defined as empty with a SPACE token. */
1284 if (macro->replacements == NULL)
1285 return _token_list_create_with_one_space (parser);
1287 return _token_list_copy (parser, macro->replacements);
1290 return _glcpp_parser_expand_function (parser, node, last);
1293 /* Push a new identifier onto the active list, returning the new list.
1295 * Here, 'marker' is the token node that appears in the list after the
1296 * expansion of 'identifier'. That is, when the list iterator begins
1297 * examinging 'marker', then it is time to pop this node from the
1301 _active_list_push (active_list_t *list,
1302 const char *identifier,
1303 token_node_t *marker)
1305 active_list_t *node;
1307 node = xtalloc (list, active_list_t);
1308 node->identifier = xtalloc_strdup (node, identifier);
1309 node->marker = marker;
1316 _active_list_pop (active_list_t *list)
1318 active_list_t *node = list;
1330 _active_list_contains (active_list_t *list, const char *identifier)
1332 active_list_t *node;
1337 for (node = list; node; node = node->next)
1338 if (strcmp (node->identifier, identifier) == 0)
1344 /* Walk over the token list replacing nodes with their expansion.
1345 * Whenever nodes are expanded the walking will walk over the new
1346 * nodes, continuing to expand as necessary. The results are placed in
1350 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1353 token_node_t *node_prev;
1354 token_node_t *node, *last = NULL;
1355 token_list_t *expansion;
1360 _token_list_trim_trailing_space (list);
1367 while (parser->active && parser->active->marker == node)
1368 parser->active = _active_list_pop (parser->active);
1370 /* Find the expansion for node, which will replace all
1371 * nodes from node to last, inclusive. */
1372 expansion = _glcpp_parser_expand_node (parser, node, &last);
1376 for (n = node; n != last->next; n = n->next)
1377 while (parser->active &&
1378 parser->active->marker == n)
1380 parser->active = _active_list_pop (parser->active);
1383 parser->active = _active_list_push (parser->active,
1384 node->token->value.str,
1387 /* Splice expansion into list, supporting a
1388 * simple deletion if the expansion is
1390 if (expansion->head) {
1392 node_prev->next = expansion->head;
1394 list->head = expansion->head;
1395 expansion->tail->next = last->next;
1396 if (last == list->tail)
1397 list->tail = expansion->tail;
1400 node_prev->next = last->next;
1402 list->head = last->next;
1403 if (last == list->tail)
1409 node = node_prev ? node_prev->next : list->head;
1412 while (parser->active)
1413 parser->active = _active_list_pop (parser->active);
1415 list->non_space_tail = list->tail;
1419 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1425 _glcpp_parser_expand_token_list (parser, list);
1427 _token_list_trim_trailing_space (list);
1429 _token_list_print (parser, list);
1433 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1434 const char *identifier)
1436 /* According to the GLSL specification, macro names starting with "__"
1437 * or "GL_" are reserved for future use. So, don't allow them.
1439 if (strncmp(identifier, "__", 2) == 0) {
1440 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1442 if (strncmp(identifier, "GL_", 3) == 0) {
1443 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1448 _define_object_macro (glcpp_parser_t *parser,
1450 const char *identifier,
1451 token_list_t *replacements)
1456 _check_for_reserved_macro_name(parser, loc, identifier);
1458 macro = xtalloc (parser, macro_t);
1460 macro->is_function = 0;
1461 macro->parameters = NULL;
1462 macro->identifier = talloc_strdup (macro, identifier);
1463 macro->replacements = talloc_steal (macro, replacements);
1465 hash_table_insert (parser->defines, macro, identifier);
1469 _define_function_macro (glcpp_parser_t *parser,
1471 const char *identifier,
1472 string_list_t *parameters,
1473 token_list_t *replacements)
1477 _check_for_reserved_macro_name(parser, loc, identifier);
1479 macro = xtalloc (parser, macro_t);
1481 macro->is_function = 1;
1482 macro->parameters = talloc_steal (macro, parameters);
1483 macro->identifier = talloc_strdup (macro, identifier);
1484 macro->replacements = talloc_steal (macro, replacements);
1486 hash_table_insert (parser->defines, macro, identifier);
1490 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1495 if (parser->lex_from_list == NULL) {
1496 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1498 /* XXX: This ugly block of code exists for the sole
1499 * purpose of converting a NEWLINE token into a SPACE
1500 * token, but only in the case where we have seen a
1501 * function-like macro name, but have not yet seen its
1502 * closing parenthesis.
1504 * There's perhaps a more compact way to do this with
1505 * mid-rule actions in the grammar.
1507 * I'm definitely not pleased with the complexity of
1510 if (parser->newline_as_space)
1513 parser->paren_count++;
1514 } else if (ret == ')') {
1515 parser->paren_count--;
1516 if (parser->paren_count == 0)
1517 parser->newline_as_space = 0;
1518 } else if (ret == NEWLINE) {
1520 } else if (ret != SPACE) {
1521 if (parser->paren_count == 0)
1522 parser->newline_as_space = 0;
1525 else if (parser->in_control_line)
1528 parser->in_control_line = 0;
1530 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1531 ret == HASH_UNDEF || ret == HASH_IF ||
1532 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1533 ret == HASH_ELIF || ret == HASH_ELSE ||
1534 ret == HASH_ENDIF || ret == HASH)
1536 parser->in_control_line = 1;
1538 else if (ret == IDENTIFIER)
1541 macro = hash_table_find (parser->defines,
1543 if (macro && macro->is_function) {
1544 parser->newline_as_space = 1;
1545 parser->paren_count = 0;
1552 node = parser->lex_from_node;
1555 talloc_free (parser->lex_from_list);
1556 parser->lex_from_list = NULL;
1560 *yylval = node->token->value;
1561 ret = node->token->type;
1563 parser->lex_from_node = node->next;
1569 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1573 assert (parser->lex_from_list == NULL);
1575 /* Copy list, eliminating any space tokens. */
1576 parser->lex_from_list = _token_list_create (parser);
1578 for (node = list->head; node; node = node->next) {
1579 if (node->token->type == SPACE)
1581 _token_list_append (parser->lex_from_list, node->token);
1586 parser->lex_from_node = parser->lex_from_list->head;
1588 /* It's possible the list consisted of nothing but whitespace. */
1589 if (parser->lex_from_node == NULL) {
1590 talloc_free (parser->lex_from_list);
1591 parser->lex_from_list = NULL;
1596 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1599 skip_type_t current = SKIP_NO_SKIP;
1602 if (parser->skip_stack)
1603 current = parser->skip_stack->type;
1605 node = xtalloc (parser, skip_node_t);
1608 if (current == SKIP_NO_SKIP) {
1610 node->type = SKIP_NO_SKIP;
1612 node->type = SKIP_TO_ELSE;
1614 node->type = SKIP_TO_ENDIF;
1617 node->next = parser->skip_stack;
1618 parser->skip_stack = node;
1622 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1623 const char *type, int condition)
1625 if (parser->skip_stack == NULL) {
1626 glcpp_error (loc, parser, "%s without #if\n", type);
1630 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1632 parser->skip_stack->type = SKIP_NO_SKIP;
1634 parser->skip_stack->type = SKIP_TO_ENDIF;
1639 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1643 if (parser->skip_stack == NULL) {
1644 glcpp_error (loc, parser, "#endif without #if\n");
1648 node = parser->skip_stack;
1649 parser->skip_stack = node->next;