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_append_list (string_list_t *list, string_list_t *tail);
62 _string_list_contains (string_list_t *list, const char *member, int *index);
65 _string_list_length (string_list_t *list);
67 static argument_list_t *
68 _argument_list_create (void *ctx);
71 _argument_list_append (argument_list_t *list, token_list_t *argument);
74 _argument_list_length (argument_list_t *list);
77 _argument_list_member_at (argument_list_t *list, int index);
79 /* Note: This function talloc_steal()s the str pointer. */
81 _token_create_str (void *ctx, int type, char *str);
84 _token_create_ival (void *ctx, int type, int ival);
87 _token_list_create (void *ctx);
89 /* Note: This function adds a talloc_reference() to token.
91 * You may want to talloc_unlink any current reference if you no
94 _token_list_append (token_list_t *list, token_t *token);
97 _token_list_append_list (token_list_t *list, token_list_t *tail);
100 _token_list_length (token_list_t *list);
102 static active_list_t *
103 _active_list_push (active_list_t *list,
104 const char *identifier,
105 token_node_t *marker);
107 static active_list_t *
108 _active_list_pop (active_list_t *list);
111 _active_list_contains (active_list_t *list, const char *identifier);
114 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
118 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
122 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
126 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
127 const char *type, int condition);
130 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
132 #define yylex glcpp_parser_lex
135 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
138 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
146 %parse-param {glcpp_parser_t *parser}
147 %lex-param {glcpp_parser_t *parser}
149 %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
151 %type <ival> expression INTEGER operator SPACE
152 %type <str> IDENTIFIER INTEGER_STRING OTHER
153 %type <string_list> identifier_list
154 %type <token> preprocessing_token conditional_token
155 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
161 %left EQUAL NOT_EQUAL
162 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
163 %left LEFT_SHIFT RIGHT_SHIFT
177 glcpp_print(parser->output, "\n");
180 _glcpp_parser_print_expanded_token_list (parser, $1);
181 glcpp_print(parser->output, "\n");
189 IF_EXPANDED expression NEWLINE {
190 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
192 | ELIF_EXPANDED expression NEWLINE {
193 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
198 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
199 _define_object_macro (parser, & @2, $2, $3);
201 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
202 _define_function_macro (parser, & @2, $2, NULL, $5);
204 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
205 _define_function_macro (parser, & @2, $2, $4, $6);
207 | HASH_UNDEF IDENTIFIER NEWLINE {
208 macro_t *macro = hash_table_find (parser->defines, $2);
210 /* XXX: Need hash table to support a real way
211 * to remove an element rather than prefixing
212 * a new node with data of NULL like this. */
213 hash_table_insert (parser->defines, NULL, $2);
218 | HASH_IF conditional_tokens NEWLINE {
219 token_list_t *expanded;
222 expanded = _token_list_create (parser);
223 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
224 _token_list_append (expanded, token);
225 talloc_unlink (parser, token);
226 _glcpp_parser_expand_token_list (parser, $2);
227 _token_list_append_list (expanded, $2);
228 glcpp_parser_lex_from (parser, expanded);
230 | HASH_IFDEF IDENTIFIER junk NEWLINE {
231 macro_t *macro = hash_table_find (parser->defines, $2);
233 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
235 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
236 macro_t *macro = hash_table_find (parser->defines, $2);
238 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
240 | HASH_ELIF conditional_tokens NEWLINE {
241 token_list_t *expanded;
244 expanded = _token_list_create (parser);
245 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
246 _token_list_append (expanded, token);
247 talloc_unlink (parser, token);
248 _glcpp_parser_expand_token_list (parser, $2);
249 _token_list_append_list (expanded, $2);
250 glcpp_parser_lex_from (parser, expanded);
252 | HASH_ELIF NEWLINE {
253 /* #elif without an expression results in a warning if the
254 * condition doesn't matter (we just handled #if 1 or such)
255 * but an error otherwise. */
256 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
257 parser->skip_stack->type = SKIP_TO_ENDIF;
258 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
260 glcpp_error(& @1, parser, "#elif needs an expression");
263 | HASH_ELSE NEWLINE {
264 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
266 | HASH_ENDIF NEWLINE {
267 _glcpp_parser_skip_stack_pop (parser, & @1);
274 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
275 $$ = strtoll ($1 + 2, NULL, 16);
276 } else if ($1[0] == '0') {
277 $$ = strtoll ($1, NULL, 8);
279 $$ = strtoll ($1, NULL, 10);
285 | expression OR expression {
288 | expression AND expression {
291 | expression '|' expression {
294 | expression '^' expression {
297 | expression '&' expression {
300 | expression NOT_EQUAL expression {
303 | expression EQUAL expression {
306 | expression GREATER_OR_EQUAL expression {
309 | expression LESS_OR_EQUAL expression {
312 | expression '>' expression {
315 | expression '<' expression {
318 | expression RIGHT_SHIFT expression {
321 | expression LEFT_SHIFT expression {
324 | expression '-' expression {
327 | expression '+' expression {
330 | expression '%' expression {
333 | expression '/' expression {
336 | expression '*' expression {
339 | '!' expression %prec UNARY {
342 | '~' expression %prec UNARY {
345 | '-' expression %prec UNARY {
348 | '+' expression %prec UNARY {
351 | '(' expression ')' {
358 $$ = _string_list_create (parser);
359 _string_list_append_item ($$, $1);
360 talloc_steal ($$, $1);
362 | identifier_list ',' IDENTIFIER {
364 _string_list_append_item ($$, $3);
365 talloc_steal ($$, $3);
370 NEWLINE { $$ = NULL; }
376 yyerror (& @1, parser, "Invalid tokens after #");
381 /* empty */ { $$ = NULL; }
388 glcpp_warning(&@1, parser, "extra tokens at end of directive");
393 /* Handle "defined" operator */
395 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
396 $$ = _token_create_ival (parser, INTEGER, v);
398 | DEFINED '(' IDENTIFIER ')' {
399 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
400 $$ = _token_create_ival (parser, INTEGER, v);
402 | preprocessing_token
406 /* Exactly the same as pp_tokens, but using conditional_token */
408 parser->space_tokens = 1;
409 $$ = _token_list_create (parser);
410 _token_list_append ($$, $1);
411 talloc_unlink (parser, $1);
413 | conditional_tokens conditional_token {
415 _token_list_append ($$, $2);
416 talloc_unlink (parser, $2);
421 preprocessing_token {
422 parser->space_tokens = 1;
423 $$ = _token_list_create (parser);
424 _token_list_append ($$, $1);
425 talloc_unlink (parser, $1);
427 | pp_tokens preprocessing_token {
429 _token_list_append ($$, $2);
430 talloc_unlink (parser, $2);
436 $$ = _token_create_str (parser, IDENTIFIER, $1);
437 $$->location = yylloc;
440 $$ = _token_create_str (parser, INTEGER_STRING, $1);
441 $$->location = yylloc;
444 $$ = _token_create_ival (parser, $1, $1);
445 $$->location = yylloc;
448 $$ = _token_create_str (parser, OTHER, $1);
449 $$->location = yylloc;
452 $$ = _token_create_ival (parser, SPACE, SPACE);
453 $$->location = yylloc;
473 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
474 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
477 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
478 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
479 | EQUAL { $$ = EQUAL; }
480 | NOT_EQUAL { $$ = NOT_EQUAL; }
488 | PASTE { $$ = PASTE; }
489 | DEFINED { $$ = DEFINED; }
495 _string_list_create (void *ctx)
499 list = xtalloc (ctx, string_list_t);
507 _string_list_append_list (string_list_t *list, string_list_t *tail)
509 if (list->head == NULL) {
510 list->head = tail->head;
512 list->tail->next = tail->head;
515 list->tail = tail->tail;
519 _string_list_append_item (string_list_t *list, const char *str)
523 node = xtalloc (list, string_node_t);
524 node->str = xtalloc_strdup (node, str);
528 if (list->head == NULL) {
531 list->tail->next = node;
538 _string_list_contains (string_list_t *list, const char *member, int *index)
546 for (i = 0, node = list->head; node; i++, node = node->next) {
547 if (strcmp (node->str, member) == 0) {
558 _string_list_length (string_list_t *list)
566 for (node = list->head; node; node = node->next)
573 _argument_list_create (void *ctx)
575 argument_list_t *list;
577 list = xtalloc (ctx, argument_list_t);
585 _argument_list_append (argument_list_t *list, token_list_t *argument)
587 argument_node_t *node;
589 node = xtalloc (list, argument_node_t);
590 node->argument = argument;
594 if (list->head == NULL) {
597 list->tail->next = node;
604 _argument_list_length (argument_list_t *list)
607 argument_node_t *node;
612 for (node = list->head; node; node = node->next)
619 _argument_list_member_at (argument_list_t *list, int index)
621 argument_node_t *node;
628 for (i = 0; i < index; i++) {
635 return node->argument;
640 /* Note: This function talloc_steal()s the str pointer. */
642 _token_create_str (void *ctx, int type, char *str)
646 token = xtalloc (ctx, token_t);
648 token->value.str = talloc_steal (token, str);
654 _token_create_ival (void *ctx, int type, int ival)
658 token = xtalloc (ctx, token_t);
660 token->value.ival = ival;
666 _token_list_create (void *ctx)
670 list = xtalloc (ctx, token_list_t);
673 list->non_space_tail = NULL;
679 _token_list_append (token_list_t *list, token_t *token)
683 node = xtalloc (list, token_node_t);
684 node->token = xtalloc_reference (list, token);
688 if (list->head == NULL) {
691 list->tail->next = node;
695 if (token->type != SPACE)
696 list->non_space_tail = node;
700 _token_list_append_list (token_list_t *list, token_list_t *tail)
702 if (tail == NULL || tail->head == NULL)
705 if (list->head == NULL) {
706 list->head = tail->head;
708 list->tail->next = tail->head;
711 list->tail = tail->tail;
712 list->non_space_tail = tail->non_space_tail;
716 _token_list_copy (void *ctx, token_list_t *other)
724 copy = _token_list_create (ctx);
725 for (node = other->head; node; node = node->next)
726 _token_list_append (copy, node->token);
732 _token_list_trim_trailing_space (token_list_t *list)
734 token_node_t *tail, *next;
736 if (list->non_space_tail) {
737 tail = list->non_space_tail->next;
738 list->non_space_tail->next = NULL;
739 list->tail = list->non_space_tail;
750 _token_list_length (token_list_t *list)
758 for (node = list->head; node; node = node->next)
765 _token_print (char **out, token_t *token)
767 if (token->type < 256) {
768 glcpp_printf (*out, "%c", token->type);
772 switch (token->type) {
774 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
779 glcpp_print (*out, token->value.str);
782 glcpp_print (*out, " ");
785 glcpp_print (*out, "<<");
788 glcpp_print (*out, ">>");
791 glcpp_print (*out, "<=");
793 case GREATER_OR_EQUAL:
794 glcpp_print (*out, ">=");
797 glcpp_print (*out, "==");
800 glcpp_print (*out, "!=");
803 glcpp_print (*out, "&&");
806 glcpp_print (*out, "||");
809 glcpp_print (*out, "##");
812 glcpp_print (*out, ",");
815 /* Nothing to print. */
818 assert(!"Error: Don't know how to print token.");
823 /* Return a new token (talloc()ed off of 'token') formed by pasting
824 * 'token' and 'other'. Note that this function may return 'token' or
825 * 'other' directly rather than allocating anything new.
827 * Caution: Only very cursory error-checking is performed to see if
828 * the final result is a valid single token. */
830 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
832 token_t *combined = NULL;
834 /* Pasting a placeholder onto anything makes no change. */
835 if (other->type == PLACEHOLDER)
838 /* When 'token' is a placeholder, just return 'other'. */
839 if (token->type == PLACEHOLDER)
842 /* A very few single-character punctuators can be combined
843 * with another to form a multi-character punctuator. */
844 switch (token->type) {
846 if (other->type == '<')
847 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
848 else if (other->type == '=')
849 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
852 if (other->type == '>')
853 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
854 else if (other->type == '=')
855 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
858 if (other->type == '=')
859 combined = _token_create_ival (token, EQUAL, EQUAL);
862 if (other->type == '=')
863 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
866 if (other->type == '&')
867 combined = _token_create_ival (token, AND, AND);
870 if (other->type == '|')
871 combined = _token_create_ival (token, OR, OR);
875 if (combined != NULL) {
876 /* Inherit the location from the first token */
877 combined->location = token->location;
881 /* Two string-valued tokens can usually just be mashed
884 * XXX: This isn't actually legitimate. Several things here
885 * should result in a diagnostic since the result cannot be a
886 * valid, single pre-processing token. For example, pasting
887 * "123" and "abc" is not legal, but we don't catch that
889 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
890 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
894 str = xtalloc_asprintf (token, "%s%s",
895 token->value.str, other->value.str);
896 combined = _token_create_str (token, token->type, str);
897 combined->location = token->location;
901 glcpp_error (&token->location, parser, "");
902 glcpp_print (parser->info_log, "Pasting \"");
903 _token_print (&parser->info_log, token);
904 glcpp_print (parser->info_log, "\" and \"");
905 _token_print (&parser->info_log, other);
906 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
912 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
919 for (node = list->head; node; node = node->next)
920 _token_print (&parser->output, node->token);
924 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
926 glcpp_error(locp, parser, "%s", error);
930 glcpp_parser_create (void)
932 glcpp_parser_t *parser;
934 parser = xtalloc (NULL, glcpp_parser_t);
936 glcpp_lex_init_extra (parser, &parser->scanner);
937 parser->defines = hash_table_ctor (32, hash_table_string_hash,
938 hash_table_string_compare);
939 parser->active = NULL;
940 parser->lexing_if = 0;
941 parser->space_tokens = 1;
942 parser->newline_as_space = 0;
943 parser->in_control_line = 0;
944 parser->paren_count = 0;
946 parser->skip_stack = NULL;
948 parser->lex_from_list = NULL;
949 parser->lex_from_node = NULL;
951 parser->output = talloc_strdup(parser, "");
952 parser->info_log = talloc_strdup(parser, "");
959 glcpp_parser_parse (glcpp_parser_t *parser)
961 return yyparse (parser);
965 glcpp_parser_destroy (glcpp_parser_t *parser)
967 if (parser->skip_stack)
968 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
969 glcpp_lex_destroy (parser->scanner);
970 hash_table_dtor (parser->defines);
971 talloc_free (parser);
974 typedef enum function_status
976 FUNCTION_STATUS_SUCCESS,
977 FUNCTION_NOT_A_FUNCTION,
978 FUNCTION_UNBALANCED_PARENTHESES
981 /* Find a set of function-like macro arguments by looking for a
982 * balanced set of parentheses.
984 * When called, 'node' should be the opening-parenthesis token, (or
985 * perhaps preceeding SPACE tokens). Upon successful return *last will
986 * be the last consumed node, (corresponding to the closing right
991 * FUNCTION_STATUS_SUCCESS:
993 * Successfully parsed a set of function arguments.
995 * FUNCTION_NOT_A_FUNCTION:
997 * Macro name not followed by a '('. This is not an error, but
998 * simply that the macro name should be treated as a non-macro.
1000 * FUNCTION_UNBALANCED_PARENTHESES
1002 * Macro name is not followed by a balanced set of parentheses.
1004 static function_status_t
1005 _arguments_parse (argument_list_t *arguments,
1007 token_node_t **last)
1009 token_list_t *argument;
1014 /* Ignore whitespace before first parenthesis. */
1015 while (node && node->token->type == SPACE)
1018 if (node == NULL || node->token->type != '(')
1019 return FUNCTION_NOT_A_FUNCTION;
1023 argument = _token_list_create (arguments);
1024 _argument_list_append (arguments, argument);
1026 for (paren_count = 1; node; node = node->next) {
1027 if (node->token->type == '(')
1031 else if (node->token->type == ')')
1034 if (paren_count == 0)
1038 if (node->token->type == ',' &&
1041 _token_list_trim_trailing_space (argument);
1042 argument = _token_list_create (arguments);
1043 _argument_list_append (arguments, argument);
1046 if (argument->head == NULL) {
1047 /* Don't treat initial whitespace as
1048 * part of the arguement. */
1049 if (node->token->type == SPACE)
1052 _token_list_append (argument, node->token);
1057 return FUNCTION_UNBALANCED_PARENTHESES;
1061 return FUNCTION_STATUS_SUCCESS;
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. Specificallty, *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 if (macro->replacements == NULL) {
1114 talloc_free (arguments);
1115 return _token_list_create (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 _glcpp_parser_expand_token_list (parser,
1151 _token_list_append_list (substituted, argument);
1155 new_token = _token_create_ival (substituted,
1158 _token_list_append (substituted, new_token);
1161 _token_list_append (substituted, node->token);
1165 /* After argument substitution, and before further expansion
1166 * below, implement token pasting. */
1168 _token_list_trim_trailing_space (substituted);
1170 node = substituted->head;
1173 token_node_t *next_non_space;
1175 /* Look ahead for a PASTE token, skipping space. */
1176 next_non_space = node->next;
1177 while (next_non_space && next_non_space->token->type == SPACE)
1178 next_non_space = next_non_space->next;
1180 if (next_non_space == NULL)
1183 if (next_non_space->token->type != PASTE) {
1184 node = next_non_space;
1188 /* Now find the next non-space token after the PASTE. */
1189 next_non_space = next_non_space->next;
1190 while (next_non_space && next_non_space->token->type == SPACE)
1191 next_non_space = next_non_space->next;
1193 if (next_non_space == NULL) {
1194 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1198 node->token = _token_paste (parser, node->token, next_non_space->token);
1199 node->next = next_non_space->next;
1200 if (next_non_space == substituted->tail)
1201 substituted->tail = node;
1206 substituted->non_space_tail = substituted->tail;
1211 /* Compute the complete expansion of node, (and subsequent nodes after
1212 * 'node' in the case that 'node' is a function-like macro and
1213 * subsequent nodes are arguments).
1215 * Returns NULL if node is a simple token with no expansion.
1217 * Otherwise, returns the token list that results from the expansion
1218 * and sets *last to the last node in the list that was consumed by
1219 * the expansion. Specificallty, *last will be set as follows:
1221 * As 'node' in the case of object-like macro expansion.
1223 * As the token of the closing right parenthesis in the case of
1224 * function-like macro expansion.
1226 static token_list_t *
1227 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1229 token_node_t **last)
1231 token_t *token = node->token;
1232 const char *identifier;
1235 /* We only expand identifiers */
1236 if (token->type != IDENTIFIER) {
1237 /* We change any COMMA into a COMMA_FINAL to prevent
1238 * it being mistaken for an argument separator
1240 if (token->type == ',') {
1241 token->type = COMMA_FINAL;
1242 token->value.ival = COMMA_FINAL;
1248 /* Look up this identifier in the hash table. */
1249 identifier = token->value.str;
1250 macro = hash_table_find (parser->defines, identifier);
1252 /* Not a macro, so no expansion needed. */
1256 /* Finally, don't expand this macro if we're already actively
1257 * expanding it, (to avoid infinite recursion). */
1258 if (_active_list_contains (parser->active, identifier)) {
1259 /* We change the token type here from IDENTIFIER to
1260 * OTHER to prevent any future expansion of this
1261 * unexpanded token. */
1263 token_list_t *expansion;
1266 str = xtalloc_strdup (parser, token->value.str);
1267 final = _token_create_str (parser, OTHER, str);
1268 expansion = _token_list_create (parser);
1269 _token_list_append (expansion, final);
1274 if (! macro->is_function)
1278 if (macro->replacements == NULL)
1279 return _token_list_create (parser);
1281 return _token_list_copy (parser, macro->replacements);
1284 return _glcpp_parser_expand_function (parser, node, last);
1287 /* Push a new identifier onto the active list, returning the new list.
1289 * Here, 'marker' is the token node that appears in the list after the
1290 * expansion of 'identifier'. That is, when the list iterator begins
1291 * examinging 'marker', then it is time to pop this node from the
1295 _active_list_push (active_list_t *list,
1296 const char *identifier,
1297 token_node_t *marker)
1299 active_list_t *node;
1301 node = xtalloc (list, active_list_t);
1302 node->identifier = xtalloc_strdup (node, identifier);
1303 node->marker = marker;
1310 _active_list_pop (active_list_t *list)
1312 active_list_t *node = list;
1324 _active_list_contains (active_list_t *list, const char *identifier)
1326 active_list_t *node;
1331 for (node = list; node; node = node->next)
1332 if (strcmp (node->identifier, identifier) == 0)
1338 /* Walk over the token list replacing nodes with their expansion.
1339 * Whenever nodes are expanded the walking will walk over the new
1340 * nodes, continuing to expand as necessary. The results are placed in
1344 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1347 token_node_t *node_prev;
1348 token_node_t *node, *last;
1349 token_list_t *expansion;
1354 _token_list_trim_trailing_space (list);
1361 while (parser->active && parser->active->marker == node)
1362 parser->active = _active_list_pop (parser->active);
1364 /* Find the expansion for node, which will replace all
1365 * nodes from node to last, inclusive. */
1366 expansion = _glcpp_parser_expand_node (parser, node, &last);
1370 for (n = node; n != last->next; n = n->next)
1371 while (parser->active &&
1372 parser->active->marker == n)
1374 parser->active = _active_list_pop (parser->active);
1377 parser->active = _active_list_push (parser->active,
1378 node->token->value.str,
1381 /* Splice expansion into list, supporting a
1382 * simple deletion if the expansion is
1384 if (expansion->head) {
1386 node_prev->next = expansion->head;
1388 list->head = expansion->head;
1389 expansion->tail->next = last->next;
1390 if (last == list->tail)
1391 list->tail = expansion->tail;
1394 node_prev->next = last->next;
1396 list->head = last->next;
1397 if (last == list->tail)
1403 node = node_prev ? node_prev->next : list->head;
1406 while (parser->active)
1407 parser->active = _active_list_pop (parser->active);
1409 list->non_space_tail = list->tail;
1413 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1419 _glcpp_parser_expand_token_list (parser, list);
1421 _token_list_trim_trailing_space (list);
1423 _token_list_print (parser, list);
1427 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1428 const char *identifier)
1430 /* According to the GLSL specification, macro names starting with "__"
1431 * or "GL_" are reserved for future use. So, don't allow them.
1433 if (strncmp(identifier, "__", 2) == 0) {
1434 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1436 if (strncmp(identifier, "GL_", 3) == 0) {
1437 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1442 _define_object_macro (glcpp_parser_t *parser,
1444 const char *identifier,
1445 token_list_t *replacements)
1449 _check_for_reserved_macro_name(parser, loc, identifier);
1451 macro = xtalloc (parser, macro_t);
1453 macro->is_function = 0;
1454 macro->parameters = NULL;
1455 macro->identifier = talloc_strdup (macro, identifier);
1456 macro->replacements = talloc_steal (macro, replacements);
1458 hash_table_insert (parser->defines, macro, identifier);
1462 _define_function_macro (glcpp_parser_t *parser,
1464 const char *identifier,
1465 string_list_t *parameters,
1466 token_list_t *replacements)
1470 _check_for_reserved_macro_name(parser, loc, identifier);
1472 macro = xtalloc (parser, macro_t);
1474 macro->is_function = 1;
1475 macro->parameters = talloc_steal (macro, parameters);
1476 macro->identifier = talloc_strdup (macro, identifier);
1477 macro->replacements = talloc_steal (macro, replacements);
1479 hash_table_insert (parser->defines, macro, identifier);
1483 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1488 if (parser->lex_from_list == NULL) {
1489 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1491 /* XXX: This ugly block of code exists for the sole
1492 * purpose of converting a NEWLINE token into a SPACE
1493 * token, but only in the case where we have seen a
1494 * function-like macro name, but have not yet seen its
1495 * closing parenthesis.
1497 * There's perhaps a more compact way to do this with
1498 * mid-rule actions in the grammar.
1500 * I'm definitely not pleased with the complexity of
1503 if (parser->newline_as_space)
1506 parser->paren_count++;
1507 } else if (ret == ')') {
1508 parser->paren_count--;
1509 if (parser->paren_count == 0)
1510 parser->newline_as_space = 0;
1511 } else if (ret == NEWLINE) {
1513 } else if (ret != SPACE) {
1514 if (parser->paren_count == 0)
1515 parser->newline_as_space = 0;
1518 else if (parser->in_control_line)
1521 parser->in_control_line = 0;
1523 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1524 ret == HASH_UNDEF || ret == HASH_IF ||
1525 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1526 ret == HASH_ELIF || ret == HASH_ELSE ||
1527 ret == HASH_ENDIF || ret == HASH)
1529 parser->in_control_line = 1;
1531 else if (ret == IDENTIFIER)
1534 macro = hash_table_find (parser->defines,
1536 if (macro && macro->is_function) {
1537 parser->newline_as_space = 1;
1538 parser->paren_count = 0;
1545 node = parser->lex_from_node;
1548 talloc_free (parser->lex_from_list);
1549 parser->lex_from_list = NULL;
1553 *yylval = node->token->value;
1554 ret = node->token->type;
1556 parser->lex_from_node = node->next;
1562 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1566 assert (parser->lex_from_list == NULL);
1568 /* Copy list, eliminating any space tokens. */
1569 parser->lex_from_list = _token_list_create (parser);
1571 for (node = list->head; node; node = node->next) {
1572 if (node->token->type == SPACE)
1574 _token_list_append (parser->lex_from_list, node->token);
1579 parser->lex_from_node = parser->lex_from_list->head;
1581 /* It's possible the list consisted of nothing but whitespace. */
1582 if (parser->lex_from_node == NULL) {
1583 talloc_free (parser->lex_from_list);
1584 parser->lex_from_list = NULL;
1589 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1592 skip_type_t current = SKIP_NO_SKIP;
1595 if (parser->skip_stack)
1596 current = parser->skip_stack->type;
1598 node = xtalloc (parser, skip_node_t);
1601 if (current == SKIP_NO_SKIP) {
1603 node->type = SKIP_NO_SKIP;
1605 node->type = SKIP_TO_ELSE;
1607 node->type = SKIP_TO_ENDIF;
1610 node->next = parser->skip_stack;
1611 parser->skip_stack = node;
1615 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1616 const char *type, int condition)
1618 if (parser->skip_stack == NULL) {
1619 glcpp_error (loc, parser, "%s without #if\n", type);
1623 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1625 parser->skip_stack->type = SKIP_NO_SKIP;
1627 parser->skip_stack->type = SKIP_TO_ENDIF;
1632 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1636 if (parser->skip_stack == NULL) {
1637 glcpp_error (loc, parser, "#endif without #if\n");
1641 node = parser->skip_stack;
1642 parser->skip_stack = node->next;