3 * Copyright © 2010 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
32 #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
33 #define glcpp_printf(stream, fmt, args...) \
34 stream = talloc_asprintf_append(stream, fmt, args)
37 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
40 _define_object_macro (glcpp_parser_t *parser,
43 token_list_t *replacements);
46 _define_function_macro (glcpp_parser_t *parser,
49 string_list_t *parameters,
50 token_list_t *replacements);
52 static string_list_t *
53 _string_list_create (void *ctx);
56 _string_list_append_item (string_list_t *list, const char *str);
59 _string_list_contains (string_list_t *list, const char *member, int *index);
62 _string_list_length (string_list_t *list);
64 static argument_list_t *
65 _argument_list_create (void *ctx);
68 _argument_list_append (argument_list_t *list, token_list_t *argument);
71 _argument_list_length (argument_list_t *list);
74 _argument_list_member_at (argument_list_t *list, int index);
76 /* Note: This function talloc_steal()s the str pointer. */
78 _token_create_str (void *ctx, int type, char *str);
81 _token_create_ival (void *ctx, int type, int ival);
84 _token_list_create (void *ctx);
86 /* Note: This function adds a talloc_reference() to token.
88 * You may want to talloc_unlink any current reference if you no
91 _token_list_append (token_list_t *list, token_t *token);
94 _token_list_append_list (token_list_t *list, token_list_t *tail);
96 static active_list_t *
97 _active_list_push (active_list_t *list,
98 const char *identifier,
99 token_node_t *marker);
101 static active_list_t *
102 _active_list_pop (active_list_t *list);
105 _active_list_contains (active_list_t *list, const char *identifier);
108 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
112 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
116 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
120 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
121 const char *type, int condition);
124 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
126 #define yylex glcpp_parser_lex
129 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
132 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
140 %parse-param {glcpp_parser_t *parser}
141 %lex-param {glcpp_parser_t *parser}
143 %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
145 %type <ival> expression INTEGER operator SPACE
146 %type <str> IDENTIFIER INTEGER_STRING OTHER
147 %type <string_list> identifier_list
148 %type <token> preprocessing_token conditional_token
149 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
155 %left EQUAL NOT_EQUAL
156 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
157 %left LEFT_SHIFT RIGHT_SHIFT
171 glcpp_print(parser->output, "\n");
174 _glcpp_parser_print_expanded_token_list (parser, $1);
175 glcpp_print(parser->output, "\n");
183 IF_EXPANDED expression NEWLINE {
184 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
186 | ELIF_EXPANDED expression NEWLINE {
187 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
192 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
193 _define_object_macro (parser, & @2, $2, $3);
195 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
196 _define_function_macro (parser, & @2, $2, NULL, $5);
198 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
199 _define_function_macro (parser, & @2, $2, $4, $6);
201 | HASH_UNDEF IDENTIFIER NEWLINE {
202 macro_t *macro = hash_table_find (parser->defines, $2);
204 /* XXX: Need hash table to support a real way
205 * to remove an element rather than prefixing
206 * a new node with data of NULL like this. */
207 hash_table_insert (parser->defines, NULL, $2);
212 | HASH_IF conditional_tokens NEWLINE {
213 token_list_t *expanded;
216 expanded = _token_list_create (parser);
217 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
218 _token_list_append (expanded, token);
219 talloc_unlink (parser, token);
220 _glcpp_parser_expand_token_list (parser, $2);
221 _token_list_append_list (expanded, $2);
222 glcpp_parser_lex_from (parser, expanded);
224 | HASH_IFDEF IDENTIFIER junk NEWLINE {
225 macro_t *macro = hash_table_find (parser->defines, $2);
227 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
229 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
230 macro_t *macro = hash_table_find (parser->defines, $2);
232 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
234 | HASH_ELIF conditional_tokens NEWLINE {
235 token_list_t *expanded;
238 expanded = _token_list_create (parser);
239 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
240 _token_list_append (expanded, token);
241 talloc_unlink (parser, token);
242 _glcpp_parser_expand_token_list (parser, $2);
243 _token_list_append_list (expanded, $2);
244 glcpp_parser_lex_from (parser, expanded);
246 | HASH_ELIF NEWLINE {
247 /* #elif without an expression results in a warning if the
248 * condition doesn't matter (we just handled #if 1 or such)
249 * but an error otherwise. */
250 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
251 parser->skip_stack->type = SKIP_TO_ENDIF;
252 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
254 glcpp_error(& @1, parser, "#elif needs an expression");
257 | HASH_ELSE NEWLINE {
258 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
260 | HASH_ENDIF NEWLINE {
261 _glcpp_parser_skip_stack_pop (parser, & @1);
268 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
269 $$ = strtoll ($1 + 2, NULL, 16);
270 } else if ($1[0] == '0') {
271 $$ = strtoll ($1, NULL, 8);
273 $$ = strtoll ($1, NULL, 10);
279 | expression OR expression {
282 | expression AND expression {
285 | expression '|' expression {
288 | expression '^' expression {
291 | expression '&' expression {
294 | expression NOT_EQUAL expression {
297 | expression EQUAL expression {
300 | expression GREATER_OR_EQUAL expression {
303 | expression LESS_OR_EQUAL expression {
306 | expression '>' expression {
309 | expression '<' expression {
312 | expression RIGHT_SHIFT expression {
315 | expression LEFT_SHIFT expression {
318 | expression '-' expression {
321 | expression '+' expression {
324 | expression '%' expression {
327 | expression '/' expression {
330 | expression '*' expression {
333 | '!' expression %prec UNARY {
336 | '~' expression %prec UNARY {
339 | '-' expression %prec UNARY {
342 | '+' expression %prec UNARY {
345 | '(' expression ')' {
352 $$ = _string_list_create (parser);
353 _string_list_append_item ($$, $1);
354 talloc_steal ($$, $1);
356 | identifier_list ',' IDENTIFIER {
358 _string_list_append_item ($$, $3);
359 talloc_steal ($$, $3);
364 NEWLINE { $$ = NULL; }
370 yyerror (& @1, parser, "Invalid tokens after #");
375 /* empty */ { $$ = NULL; }
382 glcpp_warning(&@1, parser, "extra tokens at end of directive");
387 /* Handle "defined" operator */
389 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
390 $$ = _token_create_ival (parser, INTEGER, v);
392 | DEFINED '(' IDENTIFIER ')' {
393 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
394 $$ = _token_create_ival (parser, INTEGER, v);
396 | preprocessing_token
400 /* Exactly the same as pp_tokens, but using conditional_token */
402 parser->space_tokens = 1;
403 $$ = _token_list_create (parser);
404 _token_list_append ($$, $1);
405 talloc_unlink (parser, $1);
407 | conditional_tokens conditional_token {
409 _token_list_append ($$, $2);
410 talloc_unlink (parser, $2);
415 preprocessing_token {
416 parser->space_tokens = 1;
417 $$ = _token_list_create (parser);
418 _token_list_append ($$, $1);
419 talloc_unlink (parser, $1);
421 | pp_tokens preprocessing_token {
423 _token_list_append ($$, $2);
424 talloc_unlink (parser, $2);
430 $$ = _token_create_str (parser, IDENTIFIER, $1);
431 $$->location = yylloc;
434 $$ = _token_create_str (parser, INTEGER_STRING, $1);
435 $$->location = yylloc;
438 $$ = _token_create_ival (parser, $1, $1);
439 $$->location = yylloc;
442 $$ = _token_create_str (parser, OTHER, $1);
443 $$->location = yylloc;
446 $$ = _token_create_ival (parser, SPACE, SPACE);
447 $$->location = yylloc;
467 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
468 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
471 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
472 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
473 | EQUAL { $$ = EQUAL; }
474 | NOT_EQUAL { $$ = NOT_EQUAL; }
482 | PASTE { $$ = PASTE; }
483 | DEFINED { $$ = DEFINED; }
489 _string_list_create (void *ctx)
493 list = xtalloc (ctx, string_list_t);
501 _string_list_append_item (string_list_t *list, const char *str)
505 node = xtalloc (list, string_node_t);
506 node->str = xtalloc_strdup (node, str);
510 if (list->head == NULL) {
513 list->tail->next = node;
520 _string_list_contains (string_list_t *list, const char *member, int *index)
528 for (i = 0, node = list->head; node; i++, node = node->next) {
529 if (strcmp (node->str, member) == 0) {
540 _string_list_length (string_list_t *list)
548 for (node = list->head; node; node = node->next)
555 _argument_list_create (void *ctx)
557 argument_list_t *list;
559 list = xtalloc (ctx, argument_list_t);
567 _argument_list_append (argument_list_t *list, token_list_t *argument)
569 argument_node_t *node;
571 node = xtalloc (list, argument_node_t);
572 node->argument = argument;
576 if (list->head == NULL) {
579 list->tail->next = node;
586 _argument_list_length (argument_list_t *list)
589 argument_node_t *node;
594 for (node = list->head; node; node = node->next)
601 _argument_list_member_at (argument_list_t *list, int index)
603 argument_node_t *node;
610 for (i = 0; i < index; i++) {
617 return node->argument;
622 /* Note: This function talloc_steal()s the str pointer. */
624 _token_create_str (void *ctx, int type, char *str)
628 token = xtalloc (ctx, token_t);
630 token->value.str = talloc_steal (token, str);
636 _token_create_ival (void *ctx, int type, int ival)
640 token = xtalloc (ctx, token_t);
642 token->value.ival = ival;
648 _token_list_create (void *ctx)
652 list = xtalloc (ctx, token_list_t);
655 list->non_space_tail = NULL;
661 _token_list_append (token_list_t *list, token_t *token)
665 node = xtalloc (list, token_node_t);
666 node->token = xtalloc_reference (list, token);
670 if (list->head == NULL) {
673 list->tail->next = node;
677 if (token->type != SPACE)
678 list->non_space_tail = node;
682 _token_list_append_list (token_list_t *list, token_list_t *tail)
684 if (tail == NULL || tail->head == NULL)
687 if (list->head == NULL) {
688 list->head = tail->head;
690 list->tail->next = tail->head;
693 list->tail = tail->tail;
694 list->non_space_tail = tail->non_space_tail;
698 _token_list_copy (void *ctx, token_list_t *other)
706 copy = _token_list_create (ctx);
707 for (node = other->head; node; node = node->next)
708 _token_list_append (copy, node->token);
714 _token_list_trim_trailing_space (token_list_t *list)
716 token_node_t *tail, *next;
718 if (list->non_space_tail) {
719 tail = list->non_space_tail->next;
720 list->non_space_tail->next = NULL;
721 list->tail = list->non_space_tail;
732 _token_print (char **out, token_t *token)
734 if (token->type < 256) {
735 glcpp_printf (*out, "%c", token->type);
739 switch (token->type) {
741 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
746 glcpp_print (*out, token->value.str);
749 glcpp_print (*out, " ");
752 glcpp_print (*out, "<<");
755 glcpp_print (*out, ">>");
758 glcpp_print (*out, "<=");
760 case GREATER_OR_EQUAL:
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 glcpp_print (*out, ",");
782 /* Nothing to print. */
785 assert(!"Error: Don't know how to print token.");
790 /* Return a new token (talloc()ed off of 'token') formed by pasting
791 * 'token' and 'other'. Note that this function may return 'token' or
792 * 'other' directly rather than allocating anything new.
794 * Caution: Only very cursory error-checking is performed to see if
795 * the final result is a valid single token. */
797 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
799 token_t *combined = NULL;
801 /* Pasting a placeholder onto anything makes no change. */
802 if (other->type == PLACEHOLDER)
805 /* When 'token' is a placeholder, just return 'other'. */
806 if (token->type == PLACEHOLDER)
809 /* A very few single-character punctuators can be combined
810 * with another to form a multi-character punctuator. */
811 switch (token->type) {
813 if (other->type == '<')
814 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
815 else if (other->type == '=')
816 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
819 if (other->type == '>')
820 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
821 else if (other->type == '=')
822 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
825 if (other->type == '=')
826 combined = _token_create_ival (token, EQUAL, EQUAL);
829 if (other->type == '=')
830 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
833 if (other->type == '&')
834 combined = _token_create_ival (token, AND, AND);
837 if (other->type == '|')
838 combined = _token_create_ival (token, OR, OR);
842 if (combined != NULL) {
843 /* Inherit the location from the first token */
844 combined->location = token->location;
848 /* Two string-valued tokens can usually just be mashed
851 * XXX: This isn't actually legitimate. Several things here
852 * should result in a diagnostic since the result cannot be a
853 * valid, single pre-processing token. For example, pasting
854 * "123" and "abc" is not legal, but we don't catch that
856 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
857 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
861 str = xtalloc_asprintf (token, "%s%s",
862 token->value.str, other->value.str);
863 combined = _token_create_str (token, token->type, str);
864 combined->location = token->location;
868 glcpp_error (&token->location, parser, "");
869 glcpp_print (parser->info_log, "Pasting \"");
870 _token_print (&parser->info_log, token);
871 glcpp_print (parser->info_log, "\" and \"");
872 _token_print (&parser->info_log, other);
873 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
879 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
886 for (node = list->head; node; node = node->next)
887 _token_print (&parser->output, node->token);
891 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
893 glcpp_error(locp, parser, "%s", error);
897 glcpp_parser_create (void)
899 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, "");
926 glcpp_parser_parse (glcpp_parser_t *parser)
928 return yyparse (parser);
932 glcpp_parser_destroy (glcpp_parser_t *parser)
934 if (parser->skip_stack)
935 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
936 glcpp_lex_destroy (parser->scanner);
937 hash_table_dtor (parser->defines);
938 talloc_free (parser);
941 typedef enum function_status
943 FUNCTION_STATUS_SUCCESS,
944 FUNCTION_NOT_A_FUNCTION,
945 FUNCTION_UNBALANCED_PARENTHESES
948 /* Find a set of function-like macro arguments by looking for a
949 * balanced set of parentheses.
951 * When called, 'node' should be the opening-parenthesis token, (or
952 * perhaps preceeding SPACE tokens). Upon successful return *last will
953 * be the last consumed node, (corresponding to the closing right
958 * FUNCTION_STATUS_SUCCESS:
960 * Successfully parsed a set of function arguments.
962 * FUNCTION_NOT_A_FUNCTION:
964 * Macro name not followed by a '('. This is not an error, but
965 * simply that the macro name should be treated as a non-macro.
967 * FUNCTION_UNBALANCED_PARENTHESES
969 * Macro name is not followed by a balanced set of parentheses.
971 static function_status_t
972 _arguments_parse (argument_list_t *arguments,
976 token_list_t *argument;
981 /* Ignore whitespace before first parenthesis. */
982 while (node && node->token->type == SPACE)
985 if (node == NULL || node->token->type != '(')
986 return FUNCTION_NOT_A_FUNCTION;
990 argument = _token_list_create (arguments);
991 _argument_list_append (arguments, argument);
993 for (paren_count = 1; node; node = node->next) {
994 if (node->token->type == '(')
998 else if (node->token->type == ')')
1001 if (paren_count == 0)
1005 if (node->token->type == ',' &&
1008 _token_list_trim_trailing_space (argument);
1009 argument = _token_list_create (arguments);
1010 _argument_list_append (arguments, argument);
1013 if (argument->head == NULL) {
1014 /* Don't treat initial whitespace as
1015 * part of the arguement. */
1016 if (node->token->type == SPACE)
1019 _token_list_append (argument, node->token);
1024 return FUNCTION_UNBALANCED_PARENTHESES;
1028 return FUNCTION_STATUS_SUCCESS;
1031 /* This is a helper function that's essentially part of the
1032 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1033 * except for by that function.
1035 * Returns NULL if node is a simple token with no expansion, (that is,
1036 * although 'node' corresponds to an identifier defined as a
1037 * function-like macro, it is not followed with a parenthesized
1040 * Compute the complete expansion of node (which is a function-like
1041 * macro) and subsequent nodes which are arguments.
1043 * Returns the token list that results from the expansion and sets
1044 * *last to the last node in the list that was consumed by the
1045 * expansion. Specificallty, *last will be set as follows: as the
1046 * token of the closing right parenthesis.
1048 static token_list_t *
1049 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1051 token_node_t **last)
1055 const char *identifier;
1056 argument_list_t *arguments;
1057 function_status_t status;
1058 token_list_t *substituted;
1059 int parameter_index;
1061 identifier = node->token->value.str;
1063 macro = hash_table_find (parser->defines, identifier);
1065 assert (macro->is_function);
1067 arguments = _argument_list_create (parser);
1068 status = _arguments_parse (arguments, node, last);
1071 case FUNCTION_STATUS_SUCCESS:
1073 case FUNCTION_NOT_A_FUNCTION:
1075 case FUNCTION_UNBALANCED_PARENTHESES:
1076 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1080 if (macro->replacements == NULL) {
1081 talloc_free (arguments);
1082 return _token_list_create (parser);
1085 if (! ((_argument_list_length (arguments) ==
1086 _string_list_length (macro->parameters)) ||
1087 (_string_list_length (macro->parameters) == 0 &&
1088 _argument_list_length (arguments) == 1 &&
1089 arguments->head->argument->head == NULL)))
1091 glcpp_error (&node->token->location, parser,
1092 "Error: macro %s invoked with %d arguments (expected %d)\n",
1094 _argument_list_length (arguments),
1095 _string_list_length (macro->parameters));
1099 /* Perform argument substitution on the replacement list. */
1100 substituted = _token_list_create (arguments);
1102 for (node = macro->replacements->head; node; node = node->next)
1104 if (node->token->type == IDENTIFIER &&
1105 _string_list_contains (macro->parameters,
1106 node->token->value.str,
1109 token_list_t *argument;
1110 argument = _argument_list_member_at (arguments,
1112 /* Before substituting, we expand the argument
1113 * tokens, or append a placeholder token for
1114 * an empty argument. */
1115 if (argument->head) {
1116 _glcpp_parser_expand_token_list (parser,
1118 _token_list_append_list (substituted, argument);
1122 new_token = _token_create_ival (substituted,
1125 _token_list_append (substituted, new_token);
1128 _token_list_append (substituted, node->token);
1132 /* After argument substitution, and before further expansion
1133 * below, implement token pasting. */
1135 _token_list_trim_trailing_space (substituted);
1137 node = substituted->head;
1140 token_node_t *next_non_space;
1142 /* Look ahead for a PASTE token, skipping space. */
1143 next_non_space = node->next;
1144 while (next_non_space && next_non_space->token->type == SPACE)
1145 next_non_space = next_non_space->next;
1147 if (next_non_space == NULL)
1150 if (next_non_space->token->type != PASTE) {
1151 node = next_non_space;
1155 /* Now find the next non-space token after the PASTE. */
1156 next_non_space = next_non_space->next;
1157 while (next_non_space && next_non_space->token->type == SPACE)
1158 next_non_space = next_non_space->next;
1160 if (next_non_space == NULL) {
1161 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1165 node->token = _token_paste (parser, node->token, next_non_space->token);
1166 node->next = next_non_space->next;
1167 if (next_non_space == substituted->tail)
1168 substituted->tail = node;
1173 substituted->non_space_tail = substituted->tail;
1178 /* Compute the complete expansion of node, (and subsequent nodes after
1179 * 'node' in the case that 'node' is a function-like macro and
1180 * subsequent nodes are arguments).
1182 * Returns NULL if node is a simple token with no expansion.
1184 * Otherwise, returns the token list that results from the expansion
1185 * and sets *last to the last node in the list that was consumed by
1186 * the expansion. Specificallty, *last will be set as follows:
1188 * As 'node' in the case of object-like macro expansion.
1190 * As the token of the closing right parenthesis in the case of
1191 * function-like macro expansion.
1193 static token_list_t *
1194 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1196 token_node_t **last)
1198 token_t *token = node->token;
1199 const char *identifier;
1202 /* We only expand identifiers */
1203 if (token->type != IDENTIFIER) {
1204 /* We change any COMMA into a COMMA_FINAL to prevent
1205 * it being mistaken for an argument separator
1207 if (token->type == ',') {
1208 token->type = COMMA_FINAL;
1209 token->value.ival = COMMA_FINAL;
1215 /* Look up this identifier in the hash table. */
1216 identifier = token->value.str;
1217 macro = hash_table_find (parser->defines, identifier);
1219 /* Not a macro, so no expansion needed. */
1223 /* Finally, don't expand this macro if we're already actively
1224 * expanding it, (to avoid infinite recursion). */
1225 if (_active_list_contains (parser->active, identifier)) {
1226 /* We change the token type here from IDENTIFIER to
1227 * OTHER to prevent any future expansion of this
1228 * unexpanded token. */
1230 token_list_t *expansion;
1233 str = xtalloc_strdup (parser, token->value.str);
1234 final = _token_create_str (parser, OTHER, str);
1235 expansion = _token_list_create (parser);
1236 _token_list_append (expansion, final);
1241 if (! macro->is_function)
1245 if (macro->replacements == NULL)
1246 return _token_list_create (parser);
1248 return _token_list_copy (parser, macro->replacements);
1251 return _glcpp_parser_expand_function (parser, node, last);
1254 /* Push a new identifier onto the active list, returning the new list.
1256 * Here, 'marker' is the token node that appears in the list after the
1257 * expansion of 'identifier'. That is, when the list iterator begins
1258 * examinging 'marker', then it is time to pop this node from the
1262 _active_list_push (active_list_t *list,
1263 const char *identifier,
1264 token_node_t *marker)
1266 active_list_t *node;
1268 node = xtalloc (list, active_list_t);
1269 node->identifier = xtalloc_strdup (node, identifier);
1270 node->marker = marker;
1277 _active_list_pop (active_list_t *list)
1279 active_list_t *node = list;
1291 _active_list_contains (active_list_t *list, const char *identifier)
1293 active_list_t *node;
1298 for (node = list; node; node = node->next)
1299 if (strcmp (node->identifier, identifier) == 0)
1305 /* Walk over the token list replacing nodes with their expansion.
1306 * Whenever nodes are expanded the walking will walk over the new
1307 * nodes, continuing to expand as necessary. The results are placed in
1311 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1314 token_node_t *node_prev;
1315 token_node_t *node, *last = NULL;
1316 token_list_t *expansion;
1321 _token_list_trim_trailing_space (list);
1328 while (parser->active && parser->active->marker == node)
1329 parser->active = _active_list_pop (parser->active);
1331 /* Find the expansion for node, which will replace all
1332 * nodes from node to last, inclusive. */
1333 expansion = _glcpp_parser_expand_node (parser, node, &last);
1337 for (n = node; n != last->next; n = n->next)
1338 while (parser->active &&
1339 parser->active->marker == n)
1341 parser->active = _active_list_pop (parser->active);
1344 parser->active = _active_list_push (parser->active,
1345 node->token->value.str,
1348 /* Splice expansion into list, supporting a
1349 * simple deletion if the expansion is
1351 if (expansion->head) {
1353 node_prev->next = expansion->head;
1355 list->head = expansion->head;
1356 expansion->tail->next = last->next;
1357 if (last == list->tail)
1358 list->tail = expansion->tail;
1361 node_prev->next = last->next;
1363 list->head = last->next;
1364 if (last == list->tail)
1370 node = node_prev ? node_prev->next : list->head;
1373 while (parser->active)
1374 parser->active = _active_list_pop (parser->active);
1376 list->non_space_tail = list->tail;
1380 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1386 _glcpp_parser_expand_token_list (parser, list);
1388 _token_list_trim_trailing_space (list);
1390 _token_list_print (parser, list);
1394 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1395 const char *identifier)
1397 /* According to the GLSL specification, macro names starting with "__"
1398 * or "GL_" are reserved for future use. So, don't allow them.
1400 if (strncmp(identifier, "__", 2) == 0) {
1401 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1403 if (strncmp(identifier, "GL_", 3) == 0) {
1404 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1409 _define_object_macro (glcpp_parser_t *parser,
1411 const char *identifier,
1412 token_list_t *replacements)
1416 _check_for_reserved_macro_name(parser, loc, identifier);
1418 macro = xtalloc (parser, macro_t);
1420 macro->is_function = 0;
1421 macro->parameters = NULL;
1422 macro->identifier = talloc_strdup (macro, identifier);
1423 macro->replacements = talloc_steal (macro, replacements);
1425 hash_table_insert (parser->defines, macro, identifier);
1429 _define_function_macro (glcpp_parser_t *parser,
1431 const char *identifier,
1432 string_list_t *parameters,
1433 token_list_t *replacements)
1437 _check_for_reserved_macro_name(parser, loc, identifier);
1439 macro = xtalloc (parser, macro_t);
1441 macro->is_function = 1;
1442 macro->parameters = talloc_steal (macro, parameters);
1443 macro->identifier = talloc_strdup (macro, identifier);
1444 macro->replacements = talloc_steal (macro, replacements);
1446 hash_table_insert (parser->defines, macro, identifier);
1450 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1455 if (parser->lex_from_list == NULL) {
1456 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1458 /* XXX: This ugly block of code exists for the sole
1459 * purpose of converting a NEWLINE token into a SPACE
1460 * token, but only in the case where we have seen a
1461 * function-like macro name, but have not yet seen its
1462 * closing parenthesis.
1464 * There's perhaps a more compact way to do this with
1465 * mid-rule actions in the grammar.
1467 * I'm definitely not pleased with the complexity of
1470 if (parser->newline_as_space)
1473 parser->paren_count++;
1474 } else if (ret == ')') {
1475 parser->paren_count--;
1476 if (parser->paren_count == 0)
1477 parser->newline_as_space = 0;
1478 } else if (ret == NEWLINE) {
1480 } else if (ret != SPACE) {
1481 if (parser->paren_count == 0)
1482 parser->newline_as_space = 0;
1485 else if (parser->in_control_line)
1488 parser->in_control_line = 0;
1490 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1491 ret == HASH_UNDEF || ret == HASH_IF ||
1492 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1493 ret == HASH_ELIF || ret == HASH_ELSE ||
1494 ret == HASH_ENDIF || ret == HASH)
1496 parser->in_control_line = 1;
1498 else if (ret == IDENTIFIER)
1501 macro = hash_table_find (parser->defines,
1503 if (macro && macro->is_function) {
1504 parser->newline_as_space = 1;
1505 parser->paren_count = 0;
1512 node = parser->lex_from_node;
1515 talloc_free (parser->lex_from_list);
1516 parser->lex_from_list = NULL;
1520 *yylval = node->token->value;
1521 ret = node->token->type;
1523 parser->lex_from_node = node->next;
1529 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1533 assert (parser->lex_from_list == NULL);
1535 /* Copy list, eliminating any space tokens. */
1536 parser->lex_from_list = _token_list_create (parser);
1538 for (node = list->head; node; node = node->next) {
1539 if (node->token->type == SPACE)
1541 _token_list_append (parser->lex_from_list, node->token);
1546 parser->lex_from_node = parser->lex_from_list->head;
1548 /* It's possible the list consisted of nothing but whitespace. */
1549 if (parser->lex_from_node == NULL) {
1550 talloc_free (parser->lex_from_list);
1551 parser->lex_from_list = NULL;
1556 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1559 skip_type_t current = SKIP_NO_SKIP;
1562 if (parser->skip_stack)
1563 current = parser->skip_stack->type;
1565 node = xtalloc (parser, skip_node_t);
1568 if (current == SKIP_NO_SKIP) {
1570 node->type = SKIP_NO_SKIP;
1572 node->type = SKIP_TO_ELSE;
1574 node->type = SKIP_TO_ENDIF;
1577 node->next = parser->skip_stack;
1578 parser->skip_stack = node;
1582 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1583 const char *type, int condition)
1585 if (parser->skip_stack == NULL) {
1586 glcpp_error (loc, parser, "%s without #if\n", type);
1590 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1592 parser->skip_stack->type = SKIP_NO_SKIP;
1594 parser->skip_stack->type = SKIP_TO_ENDIF;
1599 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1603 if (parser->skip_stack == NULL) {
1604 glcpp_error (loc, parser, "#endif without #if\n");
1608 node = parser->skip_stack;
1609 parser->skip_stack = node->next;