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; }
486 _string_list_create (void *ctx)
490 list = xtalloc (ctx, string_list_t);
498 _string_list_append_item (string_list_t *list, const char *str)
502 node = xtalloc (list, string_node_t);
503 node->str = xtalloc_strdup (node, str);
507 if (list->head == NULL) {
510 list->tail->next = node;
517 _string_list_contains (string_list_t *list, const char *member, int *index)
525 for (i = 0, node = list->head; node; i++, node = node->next) {
526 if (strcmp (node->str, member) == 0) {
537 _string_list_length (string_list_t *list)
545 for (node = list->head; node; node = node->next)
552 _argument_list_create (void *ctx)
554 argument_list_t *list;
556 list = xtalloc (ctx, argument_list_t);
564 _argument_list_append (argument_list_t *list, token_list_t *argument)
566 argument_node_t *node;
568 node = xtalloc (list, argument_node_t);
569 node->argument = argument;
573 if (list->head == NULL) {
576 list->tail->next = node;
583 _argument_list_length (argument_list_t *list)
586 argument_node_t *node;
591 for (node = list->head; node; node = node->next)
598 _argument_list_member_at (argument_list_t *list, int index)
600 argument_node_t *node;
607 for (i = 0; i < index; i++) {
614 return node->argument;
619 /* Note: This function talloc_steal()s the str pointer. */
621 _token_create_str (void *ctx, int type, char *str)
625 token = xtalloc (ctx, token_t);
627 token->value.str = talloc_steal (token, str);
633 _token_create_ival (void *ctx, int type, int ival)
637 token = xtalloc (ctx, token_t);
639 token->value.ival = ival;
645 _token_list_create (void *ctx)
649 list = xtalloc (ctx, token_list_t);
652 list->non_space_tail = NULL;
658 _token_list_append (token_list_t *list, token_t *token)
662 node = xtalloc (list, token_node_t);
663 node->token = xtalloc_reference (list, token);
667 if (list->head == NULL) {
670 list->tail->next = node;
674 if (token->type != SPACE)
675 list->non_space_tail = node;
679 _token_list_append_list (token_list_t *list, token_list_t *tail)
681 if (tail == NULL || tail->head == NULL)
684 if (list->head == NULL) {
685 list->head = tail->head;
687 list->tail->next = tail->head;
690 list->tail = tail->tail;
691 list->non_space_tail = tail->non_space_tail;
694 static token_list_t *
695 _token_list_copy (void *ctx, token_list_t *other)
703 copy = _token_list_create (ctx);
704 for (node = other->head; node; node = node->next)
705 _token_list_append (copy, node->token);
711 _token_list_trim_trailing_space (token_list_t *list)
713 token_node_t *tail, *next;
715 if (list->non_space_tail) {
716 tail = list->non_space_tail->next;
717 list->non_space_tail->next = NULL;
718 list->tail = list->non_space_tail;
729 _token_print (char **out, token_t *token)
731 if (token->type < 256) {
732 glcpp_printf (*out, "%c", token->type);
736 switch (token->type) {
738 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
743 glcpp_print (*out, token->value.str);
746 glcpp_print (*out, " ");
749 glcpp_print (*out, "<<");
752 glcpp_print (*out, ">>");
755 glcpp_print (*out, "<=");
757 case GREATER_OR_EQUAL:
758 glcpp_print (*out, ">=");
761 glcpp_print (*out, "==");
764 glcpp_print (*out, "!=");
767 glcpp_print (*out, "&&");
770 glcpp_print (*out, "||");
773 glcpp_print (*out, "##");
776 glcpp_print (*out, ",");
779 /* Nothing to print. */
782 assert(!"Error: Don't know how to print token.");
787 /* Return a new token (talloc()ed off of 'token') formed by pasting
788 * 'token' and 'other'. Note that this function may return 'token' or
789 * 'other' directly rather than allocating anything new.
791 * Caution: Only very cursory error-checking is performed to see if
792 * the final result is a valid single token. */
794 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
796 token_t *combined = NULL;
798 /* Pasting a placeholder onto anything makes no change. */
799 if (other->type == PLACEHOLDER)
802 /* When 'token' is a placeholder, just return 'other'. */
803 if (token->type == PLACEHOLDER)
806 /* A very few single-character punctuators can be combined
807 * with another to form a multi-character punctuator. */
808 switch (token->type) {
810 if (other->type == '<')
811 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
812 else if (other->type == '=')
813 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
816 if (other->type == '>')
817 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
818 else if (other->type == '=')
819 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
822 if (other->type == '=')
823 combined = _token_create_ival (token, EQUAL, EQUAL);
826 if (other->type == '=')
827 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
830 if (other->type == '&')
831 combined = _token_create_ival (token, AND, AND);
834 if (other->type == '|')
835 combined = _token_create_ival (token, OR, OR);
839 if (combined != NULL) {
840 /* Inherit the location from the first token */
841 combined->location = token->location;
845 /* Two string-valued tokens can usually just be mashed
848 * XXX: This isn't actually legitimate. Several things here
849 * should result in a diagnostic since the result cannot be a
850 * valid, single pre-processing token. For example, pasting
851 * "123" and "abc" is not legal, but we don't catch that
853 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
854 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
858 str = xtalloc_asprintf (token, "%s%s",
859 token->value.str, other->value.str);
860 combined = _token_create_str (token, token->type, str);
861 combined->location = token->location;
865 glcpp_error (&token->location, parser, "");
866 glcpp_print (parser->info_log, "Pasting \"");
867 _token_print (&parser->info_log, token);
868 glcpp_print (parser->info_log, "\" and \"");
869 _token_print (&parser->info_log, other);
870 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
876 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
883 for (node = list->head; node; node = node->next)
884 _token_print (&parser->output, node->token);
888 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
890 glcpp_error(locp, parser, "%s", error);
894 glcpp_parser_create (const struct gl_extensions *extensions)
896 glcpp_parser_t *parser;
900 parser = xtalloc (NULL, glcpp_parser_t);
902 glcpp_lex_init_extra (parser, &parser->scanner);
903 parser->defines = hash_table_ctor (32, hash_table_string_hash,
904 hash_table_string_compare);
905 parser->active = NULL;
906 parser->lexing_if = 0;
907 parser->space_tokens = 1;
908 parser->newline_as_space = 0;
909 parser->in_control_line = 0;
910 parser->paren_count = 0;
912 parser->skip_stack = NULL;
914 parser->lex_from_list = NULL;
915 parser->lex_from_node = NULL;
917 parser->output = talloc_strdup(parser, "");
918 parser->info_log = talloc_strdup(parser, "");
921 /* Add pre-defined macros. */
922 tok = _token_create_ival (parser, INTEGER, 1);
924 list = _token_list_create(parser);
925 _token_list_append(list, tok);
926 _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list);
928 list = _token_list_create(parser);
929 _token_list_append(list, tok);
930 _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list);
932 if ((extensions != NULL) && extensions->EXT_texture_array) {
933 list = _token_list_create(parser);
934 _token_list_append(list, tok);
935 _define_object_macro(parser, NULL,
936 "GL_EXT_texture_array", list);
939 talloc_unlink(parser, tok);
945 glcpp_parser_parse (glcpp_parser_t *parser)
947 return yyparse (parser);
951 glcpp_parser_destroy (glcpp_parser_t *parser)
953 if (parser->skip_stack)
954 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
955 glcpp_lex_destroy (parser->scanner);
956 hash_table_dtor (parser->defines);
957 talloc_free (parser);
960 typedef enum function_status
962 FUNCTION_STATUS_SUCCESS,
963 FUNCTION_NOT_A_FUNCTION,
964 FUNCTION_UNBALANCED_PARENTHESES
967 /* Find a set of function-like macro arguments by looking for a
968 * balanced set of parentheses.
970 * When called, 'node' should be the opening-parenthesis token, (or
971 * perhaps preceeding SPACE tokens). Upon successful return *last will
972 * be the last consumed node, (corresponding to the closing right
977 * FUNCTION_STATUS_SUCCESS:
979 * Successfully parsed a set of function arguments.
981 * FUNCTION_NOT_A_FUNCTION:
983 * Macro name not followed by a '('. This is not an error, but
984 * simply that the macro name should be treated as a non-macro.
986 * FUNCTION_UNBALANCED_PARENTHESES
988 * Macro name is not followed by a balanced set of parentheses.
990 static function_status_t
991 _arguments_parse (argument_list_t *arguments,
995 token_list_t *argument;
1000 /* Ignore whitespace before first parenthesis. */
1001 while (node && node->token->type == SPACE)
1004 if (node == NULL || node->token->type != '(')
1005 return FUNCTION_NOT_A_FUNCTION;
1009 argument = _token_list_create (arguments);
1010 _argument_list_append (arguments, argument);
1012 for (paren_count = 1; node; node = node->next) {
1013 if (node->token->type == '(')
1017 else if (node->token->type == ')')
1020 if (paren_count == 0)
1024 if (node->token->type == ',' &&
1027 _token_list_trim_trailing_space (argument);
1028 argument = _token_list_create (arguments);
1029 _argument_list_append (arguments, argument);
1032 if (argument->head == NULL) {
1033 /* Don't treat initial whitespace as
1034 * part of the arguement. */
1035 if (node->token->type == SPACE)
1038 _token_list_append (argument, node->token);
1043 return FUNCTION_UNBALANCED_PARENTHESES;
1047 return FUNCTION_STATUS_SUCCESS;
1050 static token_list_t *
1051 _token_list_create_with_one_space (void *ctx)
1056 list = _token_list_create (ctx);
1057 space = _token_create_ival (list, SPACE, SPACE);
1058 _token_list_append (list, space);
1063 /* This is a helper function that's essentially part of the
1064 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1065 * except for by that function.
1067 * Returns NULL if node is a simple token with no expansion, (that is,
1068 * although 'node' corresponds to an identifier defined as a
1069 * function-like macro, it is not followed with a parenthesized
1072 * Compute the complete expansion of node (which is a function-like
1073 * macro) and subsequent nodes which are arguments.
1075 * Returns the token list that results from the expansion and sets
1076 * *last to the last node in the list that was consumed by the
1077 * expansion. Specifically, *last will be set as follows: as the
1078 * token of the closing right parenthesis.
1080 static token_list_t *
1081 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1083 token_node_t **last)
1087 const char *identifier;
1088 argument_list_t *arguments;
1089 function_status_t status;
1090 token_list_t *substituted;
1091 int parameter_index;
1093 identifier = node->token->value.str;
1095 macro = hash_table_find (parser->defines, identifier);
1097 assert (macro->is_function);
1099 arguments = _argument_list_create (parser);
1100 status = _arguments_parse (arguments, node, last);
1103 case FUNCTION_STATUS_SUCCESS:
1105 case FUNCTION_NOT_A_FUNCTION:
1107 case FUNCTION_UNBALANCED_PARENTHESES:
1108 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1112 /* Replace a macro defined as empty with a SPACE token. */
1113 if (macro->replacements == NULL) {
1114 talloc_free (arguments);
1115 return _token_list_create_with_one_space (parser);
1118 if (! ((_argument_list_length (arguments) ==
1119 _string_list_length (macro->parameters)) ||
1120 (_string_list_length (macro->parameters) == 0 &&
1121 _argument_list_length (arguments) == 1 &&
1122 arguments->head->argument->head == NULL)))
1124 glcpp_error (&node->token->location, parser,
1125 "Error: macro %s invoked with %d arguments (expected %d)\n",
1127 _argument_list_length (arguments),
1128 _string_list_length (macro->parameters));
1132 /* Perform argument substitution on the replacement list. */
1133 substituted = _token_list_create (arguments);
1135 for (node = macro->replacements->head; node; node = node->next)
1137 if (node->token->type == IDENTIFIER &&
1138 _string_list_contains (macro->parameters,
1139 node->token->value.str,
1142 token_list_t *argument;
1143 argument = _argument_list_member_at (arguments,
1145 /* Before substituting, we expand the argument
1146 * tokens, or append a placeholder token for
1147 * an empty argument. */
1148 if (argument->head) {
1149 token_list_t *expanded_argument;
1150 expanded_argument = _token_list_copy (parser,
1152 _glcpp_parser_expand_token_list (parser,
1154 _token_list_append_list (substituted,
1159 new_token = _token_create_ival (substituted,
1162 _token_list_append (substituted, new_token);
1165 _token_list_append (substituted, node->token);
1169 /* After argument substitution, and before further expansion
1170 * below, implement token pasting. */
1172 _token_list_trim_trailing_space (substituted);
1174 node = substituted->head;
1177 token_node_t *next_non_space;
1179 /* Look ahead for a PASTE token, skipping space. */
1180 next_non_space = node->next;
1181 while (next_non_space && next_non_space->token->type == SPACE)
1182 next_non_space = next_non_space->next;
1184 if (next_non_space == NULL)
1187 if (next_non_space->token->type != PASTE) {
1188 node = next_non_space;
1192 /* Now find the next non-space token after the PASTE. */
1193 next_non_space = next_non_space->next;
1194 while (next_non_space && next_non_space->token->type == SPACE)
1195 next_non_space = next_non_space->next;
1197 if (next_non_space == NULL) {
1198 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1202 node->token = _token_paste (parser, node->token, next_non_space->token);
1203 node->next = next_non_space->next;
1204 if (next_non_space == substituted->tail)
1205 substituted->tail = node;
1210 substituted->non_space_tail = substituted->tail;
1215 /* Compute the complete expansion of node, (and subsequent nodes after
1216 * 'node' in the case that 'node' is a function-like macro and
1217 * subsequent nodes are arguments).
1219 * Returns NULL if node is a simple token with no expansion.
1221 * Otherwise, returns the token list that results from the expansion
1222 * and sets *last to the last node in the list that was consumed by
1223 * the expansion. Specifically, *last will be set as follows:
1225 * As 'node' in the case of object-like macro expansion.
1227 * As the token of the closing right parenthesis in the case of
1228 * function-like macro expansion.
1230 static token_list_t *
1231 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1233 token_node_t **last)
1235 token_t *token = node->token;
1236 const char *identifier;
1239 /* We only expand identifiers */
1240 if (token->type != IDENTIFIER) {
1241 /* We change any COMMA into a COMMA_FINAL to prevent
1242 * it being mistaken for an argument separator
1244 if (token->type == ',') {
1245 token->type = COMMA_FINAL;
1246 token->value.ival = COMMA_FINAL;
1252 /* Look up this identifier in the hash table. */
1253 identifier = token->value.str;
1254 macro = hash_table_find (parser->defines, identifier);
1256 /* Not a macro, so no expansion needed. */
1260 /* Finally, don't expand this macro if we're already actively
1261 * expanding it, (to avoid infinite recursion). */
1262 if (_active_list_contains (parser->active, identifier)) {
1263 /* We change the token type here from IDENTIFIER to
1264 * OTHER to prevent any future expansion of this
1265 * unexpanded token. */
1267 token_list_t *expansion;
1270 str = xtalloc_strdup (parser, token->value.str);
1271 final = _token_create_str (parser, OTHER, str);
1272 expansion = _token_list_create (parser);
1273 _token_list_append (expansion, final);
1278 if (! macro->is_function)
1282 /* Replace a macro defined as empty with a SPACE token. */
1283 if (macro->replacements == NULL)
1284 return _token_list_create_with_one_space (parser);
1286 return _token_list_copy (parser, macro->replacements);
1289 return _glcpp_parser_expand_function (parser, node, last);
1292 /* Push a new identifier onto the active list, returning the new list.
1294 * Here, 'marker' is the token node that appears in the list after the
1295 * expansion of 'identifier'. That is, when the list iterator begins
1296 * examinging 'marker', then it is time to pop this node from the
1300 _active_list_push (active_list_t *list,
1301 const char *identifier,
1302 token_node_t *marker)
1304 active_list_t *node;
1306 node = xtalloc (list, active_list_t);
1307 node->identifier = xtalloc_strdup (node, identifier);
1308 node->marker = marker;
1315 _active_list_pop (active_list_t *list)
1317 active_list_t *node = list;
1329 _active_list_contains (active_list_t *list, const char *identifier)
1331 active_list_t *node;
1336 for (node = list; node; node = node->next)
1337 if (strcmp (node->identifier, identifier) == 0)
1343 /* Walk over the token list replacing nodes with their expansion.
1344 * Whenever nodes are expanded the walking will walk over the new
1345 * nodes, continuing to expand as necessary. The results are placed in
1349 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1352 token_node_t *node_prev;
1353 token_node_t *node, *last = NULL;
1354 token_list_t *expansion;
1359 _token_list_trim_trailing_space (list);
1366 while (parser->active && parser->active->marker == node)
1367 parser->active = _active_list_pop (parser->active);
1369 /* Find the expansion for node, which will replace all
1370 * nodes from node to last, inclusive. */
1371 expansion = _glcpp_parser_expand_node (parser, node, &last);
1375 for (n = node; n != last->next; n = n->next)
1376 while (parser->active &&
1377 parser->active->marker == n)
1379 parser->active = _active_list_pop (parser->active);
1382 parser->active = _active_list_push (parser->active,
1383 node->token->value.str,
1386 /* Splice expansion into list, supporting a
1387 * simple deletion if the expansion is
1389 if (expansion->head) {
1391 node_prev->next = expansion->head;
1393 list->head = expansion->head;
1394 expansion->tail->next = last->next;
1395 if (last == list->tail)
1396 list->tail = expansion->tail;
1399 node_prev->next = last->next;
1401 list->head = last->next;
1402 if (last == list->tail)
1408 node = node_prev ? node_prev->next : list->head;
1411 while (parser->active)
1412 parser->active = _active_list_pop (parser->active);
1414 list->non_space_tail = list->tail;
1418 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1424 _glcpp_parser_expand_token_list (parser, list);
1426 _token_list_trim_trailing_space (list);
1428 _token_list_print (parser, list);
1432 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1433 const char *identifier)
1435 /* According to the GLSL specification, macro names starting with "__"
1436 * or "GL_" are reserved for future use. So, don't allow them.
1438 if (strncmp(identifier, "__", 2) == 0) {
1439 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1441 if (strncmp(identifier, "GL_", 3) == 0) {
1442 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1447 _define_object_macro (glcpp_parser_t *parser,
1449 const char *identifier,
1450 token_list_t *replacements)
1455 _check_for_reserved_macro_name(parser, loc, identifier);
1457 macro = xtalloc (parser, macro_t);
1459 macro->is_function = 0;
1460 macro->parameters = NULL;
1461 macro->identifier = talloc_strdup (macro, identifier);
1462 macro->replacements = talloc_steal (macro, replacements);
1464 hash_table_insert (parser->defines, macro, identifier);
1468 _define_function_macro (glcpp_parser_t *parser,
1470 const char *identifier,
1471 string_list_t *parameters,
1472 token_list_t *replacements)
1476 _check_for_reserved_macro_name(parser, loc, identifier);
1478 macro = xtalloc (parser, macro_t);
1480 macro->is_function = 1;
1481 macro->parameters = talloc_steal (macro, parameters);
1482 macro->identifier = talloc_strdup (macro, identifier);
1483 macro->replacements = talloc_steal (macro, replacements);
1485 hash_table_insert (parser->defines, macro, identifier);
1489 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1494 if (parser->lex_from_list == NULL) {
1495 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1497 /* XXX: This ugly block of code exists for the sole
1498 * purpose of converting a NEWLINE token into a SPACE
1499 * token, but only in the case where we have seen a
1500 * function-like macro name, but have not yet seen its
1501 * closing parenthesis.
1503 * There's perhaps a more compact way to do this with
1504 * mid-rule actions in the grammar.
1506 * I'm definitely not pleased with the complexity of
1509 if (parser->newline_as_space)
1512 parser->paren_count++;
1513 } else if (ret == ')') {
1514 parser->paren_count--;
1515 if (parser->paren_count == 0)
1516 parser->newline_as_space = 0;
1517 } else if (ret == NEWLINE) {
1519 } else if (ret != SPACE) {
1520 if (parser->paren_count == 0)
1521 parser->newline_as_space = 0;
1524 else if (parser->in_control_line)
1527 parser->in_control_line = 0;
1529 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1530 ret == HASH_UNDEF || ret == HASH_IF ||
1531 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1532 ret == HASH_ELIF || ret == HASH_ELSE ||
1533 ret == HASH_ENDIF || ret == HASH)
1535 parser->in_control_line = 1;
1537 else if (ret == IDENTIFIER)
1540 macro = hash_table_find (parser->defines,
1542 if (macro && macro->is_function) {
1543 parser->newline_as_space = 1;
1544 parser->paren_count = 0;
1551 node = parser->lex_from_node;
1554 talloc_free (parser->lex_from_list);
1555 parser->lex_from_list = NULL;
1559 *yylval = node->token->value;
1560 ret = node->token->type;
1562 parser->lex_from_node = node->next;
1568 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1572 assert (parser->lex_from_list == NULL);
1574 /* Copy list, eliminating any space tokens. */
1575 parser->lex_from_list = _token_list_create (parser);
1577 for (node = list->head; node; node = node->next) {
1578 if (node->token->type == SPACE)
1580 _token_list_append (parser->lex_from_list, node->token);
1585 parser->lex_from_node = parser->lex_from_list->head;
1587 /* It's possible the list consisted of nothing but whitespace. */
1588 if (parser->lex_from_node == NULL) {
1589 talloc_free (parser->lex_from_list);
1590 parser->lex_from_list = NULL;
1595 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1598 skip_type_t current = SKIP_NO_SKIP;
1601 if (parser->skip_stack)
1602 current = parser->skip_stack->type;
1604 node = xtalloc (parser, skip_node_t);
1607 if (current == SKIP_NO_SKIP) {
1609 node->type = SKIP_NO_SKIP;
1611 node->type = SKIP_TO_ELSE;
1613 node->type = SKIP_TO_ENDIF;
1616 node->next = parser->skip_stack;
1617 parser->skip_stack = node;
1621 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1622 const char *type, int condition)
1624 if (parser->skip_stack == NULL) {
1625 glcpp_error (loc, parser, "%s without #if\n", type);
1629 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1631 parser->skip_stack->type = SKIP_NO_SKIP;
1633 parser->skip_stack->type = SKIP_TO_ENDIF;
1638 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1642 if (parser->skip_stack == NULL) {
1643 glcpp_error (loc, parser, "#endif without #if\n");
1647 node = parser->skip_stack;
1648 parser->skip_stack = node->next;