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;
903 parser = xtalloc (NULL, glcpp_parser_t);
905 glcpp_lex_init_extra (parser, &parser->scanner);
906 parser->defines = hash_table_ctor (32, hash_table_string_hash,
907 hash_table_string_compare);
908 parser->active = NULL;
909 parser->lexing_if = 0;
910 parser->space_tokens = 1;
911 parser->newline_as_space = 0;
912 parser->in_control_line = 0;
913 parser->paren_count = 0;
915 parser->skip_stack = NULL;
917 parser->lex_from_list = NULL;
918 parser->lex_from_node = NULL;
920 parser->output = talloc_strdup(parser, "");
921 parser->info_log = talloc_strdup(parser, "");
924 /* Add pre-defined macros. */
925 tok = _token_create_ival (parser, INTEGER, 1);
927 list = _token_list_create(parser);
928 _token_list_append(list, tok);
929 _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list);
931 list = _token_list_create(parser);
932 _token_list_append(list, tok);
933 _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list);
935 talloc_unlink(parser, tok);
941 glcpp_parser_parse (glcpp_parser_t *parser)
943 return yyparse (parser);
947 glcpp_parser_destroy (glcpp_parser_t *parser)
949 if (parser->skip_stack)
950 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
951 glcpp_lex_destroy (parser->scanner);
952 hash_table_dtor (parser->defines);
953 talloc_free (parser);
956 typedef enum function_status
958 FUNCTION_STATUS_SUCCESS,
959 FUNCTION_NOT_A_FUNCTION,
960 FUNCTION_UNBALANCED_PARENTHESES
963 /* Find a set of function-like macro arguments by looking for a
964 * balanced set of parentheses.
966 * When called, 'node' should be the opening-parenthesis token, (or
967 * perhaps preceeding SPACE tokens). Upon successful return *last will
968 * be the last consumed node, (corresponding to the closing right
973 * FUNCTION_STATUS_SUCCESS:
975 * Successfully parsed a set of function arguments.
977 * FUNCTION_NOT_A_FUNCTION:
979 * Macro name not followed by a '('. This is not an error, but
980 * simply that the macro name should be treated as a non-macro.
982 * FUNCTION_UNBALANCED_PARENTHESES
984 * Macro name is not followed by a balanced set of parentheses.
986 static function_status_t
987 _arguments_parse (argument_list_t *arguments,
991 token_list_t *argument;
996 /* Ignore whitespace before first parenthesis. */
997 while (node && node->token->type == SPACE)
1000 if (node == NULL || node->token->type != '(')
1001 return FUNCTION_NOT_A_FUNCTION;
1005 argument = _token_list_create (arguments);
1006 _argument_list_append (arguments, argument);
1008 for (paren_count = 1; node; node = node->next) {
1009 if (node->token->type == '(')
1013 else if (node->token->type == ')')
1016 if (paren_count == 0)
1020 if (node->token->type == ',' &&
1023 _token_list_trim_trailing_space (argument);
1024 argument = _token_list_create (arguments);
1025 _argument_list_append (arguments, argument);
1028 if (argument->head == NULL) {
1029 /* Don't treat initial whitespace as
1030 * part of the arguement. */
1031 if (node->token->type == SPACE)
1034 _token_list_append (argument, node->token);
1039 return FUNCTION_UNBALANCED_PARENTHESES;
1043 return FUNCTION_STATUS_SUCCESS;
1046 /* This is a helper function that's essentially part of the
1047 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1048 * except for by that function.
1050 * Returns NULL if node is a simple token with no expansion, (that is,
1051 * although 'node' corresponds to an identifier defined as a
1052 * function-like macro, it is not followed with a parenthesized
1055 * Compute the complete expansion of node (which is a function-like
1056 * macro) and subsequent nodes which are arguments.
1058 * Returns the token list that results from the expansion and sets
1059 * *last to the last node in the list that was consumed by the
1060 * expansion. Specificallty, *last will be set as follows: as the
1061 * token of the closing right parenthesis.
1063 static token_list_t *
1064 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1066 token_node_t **last)
1070 const char *identifier;
1071 argument_list_t *arguments;
1072 function_status_t status;
1073 token_list_t *substituted;
1074 int parameter_index;
1076 identifier = node->token->value.str;
1078 macro = hash_table_find (parser->defines, identifier);
1080 assert (macro->is_function);
1082 arguments = _argument_list_create (parser);
1083 status = _arguments_parse (arguments, node, last);
1086 case FUNCTION_STATUS_SUCCESS:
1088 case FUNCTION_NOT_A_FUNCTION:
1090 case FUNCTION_UNBALANCED_PARENTHESES:
1091 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1095 if (macro->replacements == NULL) {
1096 talloc_free (arguments);
1097 return _token_list_create (parser);
1100 if (! ((_argument_list_length (arguments) ==
1101 _string_list_length (macro->parameters)) ||
1102 (_string_list_length (macro->parameters) == 0 &&
1103 _argument_list_length (arguments) == 1 &&
1104 arguments->head->argument->head == NULL)))
1106 glcpp_error (&node->token->location, parser,
1107 "Error: macro %s invoked with %d arguments (expected %d)\n",
1109 _argument_list_length (arguments),
1110 _string_list_length (macro->parameters));
1114 /* Perform argument substitution on the replacement list. */
1115 substituted = _token_list_create (arguments);
1117 for (node = macro->replacements->head; node; node = node->next)
1119 if (node->token->type == IDENTIFIER &&
1120 _string_list_contains (macro->parameters,
1121 node->token->value.str,
1124 token_list_t *argument;
1125 argument = _argument_list_member_at (arguments,
1127 /* Before substituting, we expand the argument
1128 * tokens, or append a placeholder token for
1129 * an empty argument. */
1130 if (argument->head) {
1131 _glcpp_parser_expand_token_list (parser,
1133 _token_list_append_list (substituted, argument);
1137 new_token = _token_create_ival (substituted,
1140 _token_list_append (substituted, new_token);
1143 _token_list_append (substituted, node->token);
1147 /* After argument substitution, and before further expansion
1148 * below, implement token pasting. */
1150 _token_list_trim_trailing_space (substituted);
1152 node = substituted->head;
1155 token_node_t *next_non_space;
1157 /* Look ahead for a PASTE token, skipping space. */
1158 next_non_space = node->next;
1159 while (next_non_space && next_non_space->token->type == SPACE)
1160 next_non_space = next_non_space->next;
1162 if (next_non_space == NULL)
1165 if (next_non_space->token->type != PASTE) {
1166 node = next_non_space;
1170 /* Now find the next non-space token after the PASTE. */
1171 next_non_space = next_non_space->next;
1172 while (next_non_space && next_non_space->token->type == SPACE)
1173 next_non_space = next_non_space->next;
1175 if (next_non_space == NULL) {
1176 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1180 node->token = _token_paste (parser, node->token, next_non_space->token);
1181 node->next = next_non_space->next;
1182 if (next_non_space == substituted->tail)
1183 substituted->tail = node;
1188 substituted->non_space_tail = substituted->tail;
1193 /* Compute the complete expansion of node, (and subsequent nodes after
1194 * 'node' in the case that 'node' is a function-like macro and
1195 * subsequent nodes are arguments).
1197 * Returns NULL if node is a simple token with no expansion.
1199 * Otherwise, returns the token list that results from the expansion
1200 * and sets *last to the last node in the list that was consumed by
1201 * the expansion. Specificallty, *last will be set as follows:
1203 * As 'node' in the case of object-like macro expansion.
1205 * As the token of the closing right parenthesis in the case of
1206 * function-like macro expansion.
1208 static token_list_t *
1209 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1211 token_node_t **last)
1213 token_t *token = node->token;
1214 const char *identifier;
1217 /* We only expand identifiers */
1218 if (token->type != IDENTIFIER) {
1219 /* We change any COMMA into a COMMA_FINAL to prevent
1220 * it being mistaken for an argument separator
1222 if (token->type == ',') {
1223 token->type = COMMA_FINAL;
1224 token->value.ival = COMMA_FINAL;
1230 /* Look up this identifier in the hash table. */
1231 identifier = token->value.str;
1232 macro = hash_table_find (parser->defines, identifier);
1234 /* Not a macro, so no expansion needed. */
1238 /* Finally, don't expand this macro if we're already actively
1239 * expanding it, (to avoid infinite recursion). */
1240 if (_active_list_contains (parser->active, identifier)) {
1241 /* We change the token type here from IDENTIFIER to
1242 * OTHER to prevent any future expansion of this
1243 * unexpanded token. */
1245 token_list_t *expansion;
1248 str = xtalloc_strdup (parser, token->value.str);
1249 final = _token_create_str (parser, OTHER, str);
1250 expansion = _token_list_create (parser);
1251 _token_list_append (expansion, final);
1256 if (! macro->is_function)
1260 if (macro->replacements == NULL)
1261 return _token_list_create (parser);
1263 return _token_list_copy (parser, macro->replacements);
1266 return _glcpp_parser_expand_function (parser, node, last);
1269 /* Push a new identifier onto the active list, returning the new list.
1271 * Here, 'marker' is the token node that appears in the list after the
1272 * expansion of 'identifier'. That is, when the list iterator begins
1273 * examinging 'marker', then it is time to pop this node from the
1277 _active_list_push (active_list_t *list,
1278 const char *identifier,
1279 token_node_t *marker)
1281 active_list_t *node;
1283 node = xtalloc (list, active_list_t);
1284 node->identifier = xtalloc_strdup (node, identifier);
1285 node->marker = marker;
1292 _active_list_pop (active_list_t *list)
1294 active_list_t *node = list;
1306 _active_list_contains (active_list_t *list, const char *identifier)
1308 active_list_t *node;
1313 for (node = list; node; node = node->next)
1314 if (strcmp (node->identifier, identifier) == 0)
1320 /* Walk over the token list replacing nodes with their expansion.
1321 * Whenever nodes are expanded the walking will walk over the new
1322 * nodes, continuing to expand as necessary. The results are placed in
1326 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1329 token_node_t *node_prev;
1330 token_node_t *node, *last = NULL;
1331 token_list_t *expansion;
1336 _token_list_trim_trailing_space (list);
1343 while (parser->active && parser->active->marker == node)
1344 parser->active = _active_list_pop (parser->active);
1346 /* Find the expansion for node, which will replace all
1347 * nodes from node to last, inclusive. */
1348 expansion = _glcpp_parser_expand_node (parser, node, &last);
1352 for (n = node; n != last->next; n = n->next)
1353 while (parser->active &&
1354 parser->active->marker == n)
1356 parser->active = _active_list_pop (parser->active);
1359 parser->active = _active_list_push (parser->active,
1360 node->token->value.str,
1363 /* Splice expansion into list, supporting a
1364 * simple deletion if the expansion is
1366 if (expansion->head) {
1368 node_prev->next = expansion->head;
1370 list->head = expansion->head;
1371 expansion->tail->next = last->next;
1372 if (last == list->tail)
1373 list->tail = expansion->tail;
1376 node_prev->next = last->next;
1378 list->head = last->next;
1379 if (last == list->tail)
1385 node = node_prev ? node_prev->next : list->head;
1388 while (parser->active)
1389 parser->active = _active_list_pop (parser->active);
1391 list->non_space_tail = list->tail;
1395 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1401 _glcpp_parser_expand_token_list (parser, list);
1403 _token_list_trim_trailing_space (list);
1405 _token_list_print (parser, list);
1409 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1410 const char *identifier)
1412 /* According to the GLSL specification, macro names starting with "__"
1413 * or "GL_" are reserved for future use. So, don't allow them.
1415 if (strncmp(identifier, "__", 2) == 0) {
1416 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1418 if (strncmp(identifier, "GL_", 3) == 0) {
1419 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1424 _define_object_macro (glcpp_parser_t *parser,
1426 const char *identifier,
1427 token_list_t *replacements)
1432 _check_for_reserved_macro_name(parser, loc, identifier);
1434 macro = xtalloc (parser, macro_t);
1436 macro->is_function = 0;
1437 macro->parameters = NULL;
1438 macro->identifier = talloc_strdup (macro, identifier);
1439 macro->replacements = talloc_steal (macro, replacements);
1441 hash_table_insert (parser->defines, macro, identifier);
1445 _define_function_macro (glcpp_parser_t *parser,
1447 const char *identifier,
1448 string_list_t *parameters,
1449 token_list_t *replacements)
1453 _check_for_reserved_macro_name(parser, loc, identifier);
1455 macro = xtalloc (parser, macro_t);
1457 macro->is_function = 1;
1458 macro->parameters = talloc_steal (macro, parameters);
1459 macro->identifier = talloc_strdup (macro, identifier);
1460 macro->replacements = talloc_steal (macro, replacements);
1462 hash_table_insert (parser->defines, macro, identifier);
1466 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1471 if (parser->lex_from_list == NULL) {
1472 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1474 /* XXX: This ugly block of code exists for the sole
1475 * purpose of converting a NEWLINE token into a SPACE
1476 * token, but only in the case where we have seen a
1477 * function-like macro name, but have not yet seen its
1478 * closing parenthesis.
1480 * There's perhaps a more compact way to do this with
1481 * mid-rule actions in the grammar.
1483 * I'm definitely not pleased with the complexity of
1486 if (parser->newline_as_space)
1489 parser->paren_count++;
1490 } else if (ret == ')') {
1491 parser->paren_count--;
1492 if (parser->paren_count == 0)
1493 parser->newline_as_space = 0;
1494 } else if (ret == NEWLINE) {
1496 } else if (ret != SPACE) {
1497 if (parser->paren_count == 0)
1498 parser->newline_as_space = 0;
1501 else if (parser->in_control_line)
1504 parser->in_control_line = 0;
1506 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1507 ret == HASH_UNDEF || ret == HASH_IF ||
1508 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1509 ret == HASH_ELIF || ret == HASH_ELSE ||
1510 ret == HASH_ENDIF || ret == HASH)
1512 parser->in_control_line = 1;
1514 else if (ret == IDENTIFIER)
1517 macro = hash_table_find (parser->defines,
1519 if (macro && macro->is_function) {
1520 parser->newline_as_space = 1;
1521 parser->paren_count = 0;
1528 node = parser->lex_from_node;
1531 talloc_free (parser->lex_from_list);
1532 parser->lex_from_list = NULL;
1536 *yylval = node->token->value;
1537 ret = node->token->type;
1539 parser->lex_from_node = node->next;
1545 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1549 assert (parser->lex_from_list == NULL);
1551 /* Copy list, eliminating any space tokens. */
1552 parser->lex_from_list = _token_list_create (parser);
1554 for (node = list->head; node; node = node->next) {
1555 if (node->token->type == SPACE)
1557 _token_list_append (parser->lex_from_list, node->token);
1562 parser->lex_from_node = parser->lex_from_list->head;
1564 /* It's possible the list consisted of nothing but whitespace. */
1565 if (parser->lex_from_node == NULL) {
1566 talloc_free (parser->lex_from_list);
1567 parser->lex_from_list = NULL;
1572 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1575 skip_type_t current = SKIP_NO_SKIP;
1578 if (parser->skip_stack)
1579 current = parser->skip_stack->type;
1581 node = xtalloc (parser, skip_node_t);
1584 if (current == SKIP_NO_SKIP) {
1586 node->type = SKIP_NO_SKIP;
1588 node->type = SKIP_TO_ELSE;
1590 node->type = SKIP_TO_ENDIF;
1593 node->next = parser->skip_stack;
1594 parser->skip_stack = node;
1598 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1599 const char *type, int condition)
1601 if (parser->skip_stack == NULL) {
1602 glcpp_error (loc, parser, "%s without #if\n", type);
1606 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1608 parser->skip_stack->type = SKIP_NO_SKIP;
1610 parser->skip_stack->type = SKIP_TO_ENDIF;
1615 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1619 if (parser->skip_stack == NULL) {
1620 glcpp_error (loc, parser, "#endif without #if\n");
1624 node = parser->skip_stack;
1625 parser->skip_stack = node->next;