3 * Copyright © 2010 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
31 #include "main/mtypes.h"
33 #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
34 #define glcpp_printf(stream, fmt, args...) \
35 stream = talloc_asprintf_append(stream, fmt, args)
38 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
41 _define_object_macro (glcpp_parser_t *parser,
44 token_list_t *replacements);
47 _define_function_macro (glcpp_parser_t *parser,
50 string_list_t *parameters,
51 token_list_t *replacements);
53 static string_list_t *
54 _string_list_create (void *ctx);
57 _string_list_append_item (string_list_t *list, const char *str);
60 _string_list_contains (string_list_t *list, const char *member, int *index);
63 _string_list_length (string_list_t *list);
65 static argument_list_t *
66 _argument_list_create (void *ctx);
69 _argument_list_append (argument_list_t *list, token_list_t *argument);
72 _argument_list_length (argument_list_t *list);
75 _argument_list_member_at (argument_list_t *list, int index);
77 /* Note: This function talloc_steal()s the str pointer. */
79 _token_create_str (void *ctx, int type, char *str);
82 _token_create_ival (void *ctx, int type, int ival);
85 _token_list_create (void *ctx);
87 /* Note: This function adds a talloc_reference() to token.
89 * You may want to talloc_unlink any current reference if you no
92 _token_list_append (token_list_t *list, token_t *token);
95 _token_list_append_list (token_list_t *list, token_list_t *tail);
97 static active_list_t *
98 _active_list_push (active_list_t *list,
99 const char *identifier,
100 token_node_t *marker);
102 static active_list_t *
103 _active_list_pop (active_list_t *list);
106 _active_list_contains (active_list_t *list, const char *identifier);
109 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
113 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
117 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
121 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
122 const char *type, int condition);
125 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
127 #define yylex glcpp_parser_lex
130 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
133 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
136 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
144 %parse-param {glcpp_parser_t *parser}
145 %lex-param {glcpp_parser_t *parser}
148 %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 HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
150 %type <ival> expression INTEGER operator SPACE integer_constant
151 %type <str> IDENTIFIER INTEGER_STRING OTHER
152 %type <string_list> identifier_list
153 %type <token> preprocessing_token conditional_token
154 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
160 %left EQUAL NOT_EQUAL
161 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
162 %left LEFT_SHIFT RIGHT_SHIFT
176 glcpp_print(parser->output, "\n");
179 _glcpp_parser_print_expanded_token_list (parser, $1);
180 glcpp_print(parser->output, "\n");
188 IF_EXPANDED expression NEWLINE {
189 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
191 | ELIF_EXPANDED expression NEWLINE {
192 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
197 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
198 _define_object_macro (parser, & @2, $2, $3);
200 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
201 _define_function_macro (parser, & @2, $2, NULL, $5);
203 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
204 _define_function_macro (parser, & @2, $2, $4, $6);
206 | HASH_UNDEF IDENTIFIER NEWLINE {
207 macro_t *macro = hash_table_find (parser->defines, $2);
209 hash_table_remove (parser->defines, $2);
214 | HASH_IF conditional_tokens NEWLINE {
215 token_list_t *expanded;
218 expanded = _token_list_create (parser);
219 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
220 _token_list_append (expanded, token);
221 talloc_unlink (parser, token);
222 _glcpp_parser_expand_token_list (parser, $2);
223 _token_list_append_list (expanded, $2);
224 glcpp_parser_lex_from (parser, expanded);
226 | HASH_IFDEF IDENTIFIER junk NEWLINE {
227 macro_t *macro = hash_table_find (parser->defines, $2);
229 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
231 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
232 macro_t *macro = hash_table_find (parser->defines, $2);
234 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
236 | HASH_ELIF conditional_tokens NEWLINE {
237 token_list_t *expanded;
240 expanded = _token_list_create (parser);
241 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
242 _token_list_append (expanded, token);
243 talloc_unlink (parser, token);
244 _glcpp_parser_expand_token_list (parser, $2);
245 _token_list_append_list (expanded, $2);
246 glcpp_parser_lex_from (parser, expanded);
248 | HASH_ELIF NEWLINE {
249 /* #elif without an expression results in a warning if the
250 * condition doesn't matter (we just handled #if 1 or such)
251 * but an error otherwise. */
252 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
253 parser->skip_stack->type = SKIP_TO_ENDIF;
254 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
256 glcpp_error(& @1, parser, "#elif needs an expression");
259 | HASH_ELSE NEWLINE {
260 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
262 | HASH_ENDIF NEWLINE {
263 _glcpp_parser_skip_stack_pop (parser, & @1);
265 | HASH_VERSION integer_constant NEWLINE {
266 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
268 hash_table_remove (parser->defines, "__VERSION__");
271 add_builtin_define (parser, "__VERSION__", $2);
272 glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2);
279 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
280 $$ = strtoll ($1 + 2, NULL, 16);
281 } else if ($1[0] == '0') {
282 $$ = strtoll ($1, NULL, 8);
284 $$ = strtoll ($1, NULL, 10);
293 | expression OR expression {
296 | expression AND expression {
299 | expression '|' expression {
302 | expression '^' expression {
305 | expression '&' expression {
308 | expression NOT_EQUAL expression {
311 | expression EQUAL expression {
314 | expression GREATER_OR_EQUAL expression {
317 | expression LESS_OR_EQUAL expression {
320 | expression '>' expression {
323 | expression '<' expression {
326 | expression RIGHT_SHIFT expression {
329 | expression LEFT_SHIFT expression {
332 | expression '-' expression {
335 | expression '+' expression {
338 | expression '%' expression {
341 | expression '/' expression {
344 | expression '*' expression {
347 | '!' expression %prec UNARY {
350 | '~' expression %prec UNARY {
353 | '-' expression %prec UNARY {
356 | '+' expression %prec UNARY {
359 | '(' expression ')' {
366 $$ = _string_list_create (parser);
367 _string_list_append_item ($$, $1);
368 talloc_steal ($$, $1);
370 | identifier_list ',' IDENTIFIER {
372 _string_list_append_item ($$, $3);
373 talloc_steal ($$, $3);
378 NEWLINE { $$ = NULL; }
384 yyerror (& @1, parser, "Invalid tokens after #");
389 /* empty */ { $$ = NULL; }
396 glcpp_warning(&@1, parser, "extra tokens at end of directive");
401 /* Handle "defined" operator */
403 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
404 $$ = _token_create_ival (parser, INTEGER, v);
406 | DEFINED '(' IDENTIFIER ')' {
407 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
408 $$ = _token_create_ival (parser, INTEGER, v);
410 | preprocessing_token
414 /* Exactly the same as pp_tokens, but using conditional_token */
416 parser->space_tokens = 1;
417 $$ = _token_list_create (parser);
418 _token_list_append ($$, $1);
419 talloc_unlink (parser, $1);
421 | conditional_tokens conditional_token {
423 _token_list_append ($$, $2);
424 talloc_unlink (parser, $2);
429 preprocessing_token {
430 parser->space_tokens = 1;
431 $$ = _token_list_create (parser);
432 _token_list_append ($$, $1);
433 talloc_unlink (parser, $1);
435 | pp_tokens preprocessing_token {
437 _token_list_append ($$, $2);
438 talloc_unlink (parser, $2);
444 $$ = _token_create_str (parser, IDENTIFIER, $1);
445 $$->location = yylloc;
448 $$ = _token_create_str (parser, INTEGER_STRING, $1);
449 $$->location = yylloc;
452 $$ = _token_create_ival (parser, $1, $1);
453 $$->location = yylloc;
456 $$ = _token_create_str (parser, OTHER, $1);
457 $$->location = yylloc;
460 $$ = _token_create_ival (parser, SPACE, SPACE);
461 $$->location = yylloc;
481 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
482 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
485 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
486 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
487 | EQUAL { $$ = EQUAL; }
488 | NOT_EQUAL { $$ = NOT_EQUAL; }
496 | PASTE { $$ = PASTE; }
502 _string_list_create (void *ctx)
506 list = xtalloc (ctx, string_list_t);
514 _string_list_append_item (string_list_t *list, const char *str)
518 node = xtalloc (list, string_node_t);
519 node->str = xtalloc_strdup (node, str);
523 if (list->head == NULL) {
526 list->tail->next = node;
533 _string_list_contains (string_list_t *list, const char *member, int *index)
541 for (i = 0, node = list->head; node; i++, node = node->next) {
542 if (strcmp (node->str, member) == 0) {
553 _string_list_length (string_list_t *list)
561 for (node = list->head; node; node = node->next)
568 _argument_list_create (void *ctx)
570 argument_list_t *list;
572 list = xtalloc (ctx, argument_list_t);
580 _argument_list_append (argument_list_t *list, token_list_t *argument)
582 argument_node_t *node;
584 node = xtalloc (list, argument_node_t);
585 node->argument = argument;
589 if (list->head == NULL) {
592 list->tail->next = node;
599 _argument_list_length (argument_list_t *list)
602 argument_node_t *node;
607 for (node = list->head; node; node = node->next)
614 _argument_list_member_at (argument_list_t *list, int index)
616 argument_node_t *node;
623 for (i = 0; i < index; i++) {
630 return node->argument;
635 /* Note: This function talloc_steal()s the str pointer. */
637 _token_create_str (void *ctx, int type, char *str)
641 token = xtalloc (ctx, token_t);
643 token->value.str = talloc_steal (token, str);
649 _token_create_ival (void *ctx, int type, int ival)
653 token = xtalloc (ctx, token_t);
655 token->value.ival = ival;
661 _token_list_create (void *ctx)
665 list = xtalloc (ctx, token_list_t);
668 list->non_space_tail = NULL;
674 _token_list_append (token_list_t *list, token_t *token)
678 node = xtalloc (list, token_node_t);
679 node->token = xtalloc_reference (list, token);
683 if (list->head == NULL) {
686 list->tail->next = node;
690 if (token->type != SPACE)
691 list->non_space_tail = node;
695 _token_list_append_list (token_list_t *list, token_list_t *tail)
697 if (tail == NULL || tail->head == NULL)
700 if (list->head == NULL) {
701 list->head = tail->head;
703 list->tail->next = tail->head;
706 list->tail = tail->tail;
707 list->non_space_tail = tail->non_space_tail;
710 static token_list_t *
711 _token_list_copy (void *ctx, token_list_t *other)
719 copy = _token_list_create (ctx);
720 for (node = other->head; node; node = node->next)
721 _token_list_append (copy, node->token);
727 _token_list_trim_trailing_space (token_list_t *list)
729 token_node_t *tail, *next;
731 if (list->non_space_tail) {
732 tail = list->non_space_tail->next;
733 list->non_space_tail->next = NULL;
734 list->tail = list->non_space_tail;
745 _token_print (char **out, token_t *token)
747 if (token->type < 256) {
748 glcpp_printf (*out, "%c", token->type);
752 switch (token->type) {
754 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
759 glcpp_print (*out, token->value.str);
762 glcpp_print (*out, " ");
765 glcpp_print (*out, "<<");
768 glcpp_print (*out, ">>");
771 glcpp_print (*out, "<=");
773 case GREATER_OR_EQUAL:
774 glcpp_print (*out, ">=");
777 glcpp_print (*out, "==");
780 glcpp_print (*out, "!=");
783 glcpp_print (*out, "&&");
786 glcpp_print (*out, "||");
789 glcpp_print (*out, "##");
792 glcpp_print (*out, ",");
795 /* Nothing to print. */
798 assert(!"Error: Don't know how to print token.");
803 /* Return a new token (talloc()ed off of 'token') formed by pasting
804 * 'token' and 'other'. Note that this function may return 'token' or
805 * 'other' directly rather than allocating anything new.
807 * Caution: Only very cursory error-checking is performed to see if
808 * the final result is a valid single token. */
810 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
812 token_t *combined = NULL;
814 /* Pasting a placeholder onto anything makes no change. */
815 if (other->type == PLACEHOLDER)
818 /* When 'token' is a placeholder, just return 'other'. */
819 if (token->type == PLACEHOLDER)
822 /* A very few single-character punctuators can be combined
823 * with another to form a multi-character punctuator. */
824 switch (token->type) {
826 if (other->type == '<')
827 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
828 else if (other->type == '=')
829 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
832 if (other->type == '>')
833 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
834 else if (other->type == '=')
835 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
838 if (other->type == '=')
839 combined = _token_create_ival (token, EQUAL, EQUAL);
842 if (other->type == '=')
843 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
846 if (other->type == '&')
847 combined = _token_create_ival (token, AND, AND);
850 if (other->type == '|')
851 combined = _token_create_ival (token, OR, OR);
855 if (combined != NULL) {
856 /* Inherit the location from the first token */
857 combined->location = token->location;
861 /* Two string-valued tokens can usually just be mashed
864 * XXX: This isn't actually legitimate. Several things here
865 * should result in a diagnostic since the result cannot be a
866 * valid, single pre-processing token. For example, pasting
867 * "123" and "abc" is not legal, but we don't catch that
869 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
870 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
874 str = xtalloc_asprintf (token, "%s%s",
875 token->value.str, other->value.str);
876 combined = _token_create_str (token, token->type, str);
877 combined->location = token->location;
881 glcpp_error (&token->location, parser, "");
882 glcpp_print (parser->info_log, "Pasting \"");
883 _token_print (&parser->info_log, token);
884 glcpp_print (parser->info_log, "\" and \"");
885 _token_print (&parser->info_log, other);
886 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
892 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
899 for (node = list->head; node; node = node->next)
900 _token_print (&parser->output, node->token);
904 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
906 glcpp_error(locp, parser, "%s", error);
909 static void add_builtin_define(glcpp_parser_t *parser,
910 const char *name, int value)
915 tok = _token_create_ival (parser, INTEGER, value);
917 list = _token_list_create(parser);
918 _token_list_append(list, tok);
919 _define_object_macro(parser, NULL, name, list);
921 talloc_unlink(parser, tok);
925 glcpp_parser_create (const struct gl_extensions *extensions)
927 glcpp_parser_t *parser;
928 int language_version;
930 parser = xtalloc (NULL, glcpp_parser_t);
932 glcpp_lex_init_extra (parser, &parser->scanner);
933 parser->defines = hash_table_ctor (32, hash_table_string_hash,
934 hash_table_string_compare);
935 parser->active = NULL;
936 parser->lexing_if = 0;
937 parser->space_tokens = 1;
938 parser->newline_as_space = 0;
939 parser->in_control_line = 0;
940 parser->paren_count = 0;
942 parser->skip_stack = NULL;
944 parser->lex_from_list = NULL;
945 parser->lex_from_node = NULL;
947 parser->output = talloc_strdup(parser, "");
948 parser->info_log = talloc_strdup(parser, "");
951 /* Add pre-defined macros. */
952 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
953 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
955 if (extensions != NULL) {
956 if (extensions->EXT_texture_array) {
957 add_builtin_define(parser, "GL_EXT_texture_array", 1);
960 if (extensions->ARB_fragment_coord_conventions)
961 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
965 language_version = 110;
966 if (extensions && extensions->ARB_shading_language_120) {
967 language_version = 120;
969 add_builtin_define(parser, "__VERSION__", language_version);
975 glcpp_parser_parse (glcpp_parser_t *parser)
977 return yyparse (parser);
981 glcpp_parser_destroy (glcpp_parser_t *parser)
983 if (parser->skip_stack)
984 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
985 glcpp_lex_destroy (parser->scanner);
986 hash_table_dtor (parser->defines);
987 talloc_free (parser);
990 typedef enum function_status
992 FUNCTION_STATUS_SUCCESS,
993 FUNCTION_NOT_A_FUNCTION,
994 FUNCTION_UNBALANCED_PARENTHESES
997 /* Find a set of function-like macro arguments by looking for a
998 * balanced set of parentheses.
1000 * When called, 'node' should be the opening-parenthesis token, (or
1001 * perhaps preceeding SPACE tokens). Upon successful return *last will
1002 * be the last consumed node, (corresponding to the closing right
1007 * FUNCTION_STATUS_SUCCESS:
1009 * Successfully parsed a set of function arguments.
1011 * FUNCTION_NOT_A_FUNCTION:
1013 * Macro name not followed by a '('. This is not an error, but
1014 * simply that the macro name should be treated as a non-macro.
1016 * FUNCTION_UNBALANCED_PARENTHESES
1018 * Macro name is not followed by a balanced set of parentheses.
1020 static function_status_t
1021 _arguments_parse (argument_list_t *arguments,
1023 token_node_t **last)
1025 token_list_t *argument;
1030 /* Ignore whitespace before first parenthesis. */
1031 while (node && node->token->type == SPACE)
1034 if (node == NULL || node->token->type != '(')
1035 return FUNCTION_NOT_A_FUNCTION;
1039 argument = _token_list_create (arguments);
1040 _argument_list_append (arguments, argument);
1042 for (paren_count = 1; node; node = node->next) {
1043 if (node->token->type == '(')
1047 else if (node->token->type == ')')
1050 if (paren_count == 0)
1054 if (node->token->type == ',' &&
1057 _token_list_trim_trailing_space (argument);
1058 argument = _token_list_create (arguments);
1059 _argument_list_append (arguments, argument);
1062 if (argument->head == NULL) {
1063 /* Don't treat initial whitespace as
1064 * part of the arguement. */
1065 if (node->token->type == SPACE)
1068 _token_list_append (argument, node->token);
1073 return FUNCTION_UNBALANCED_PARENTHESES;
1077 return FUNCTION_STATUS_SUCCESS;
1080 static token_list_t *
1081 _token_list_create_with_one_space (void *ctx)
1086 list = _token_list_create (ctx);
1087 space = _token_create_ival (list, SPACE, SPACE);
1088 _token_list_append (list, space);
1093 /* This is a helper function that's essentially part of the
1094 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1095 * except for by that function.
1097 * Returns NULL if node is a simple token with no expansion, (that is,
1098 * although 'node' corresponds to an identifier defined as a
1099 * function-like macro, it is not followed with a parenthesized
1102 * Compute the complete expansion of node (which is a function-like
1103 * macro) and subsequent nodes which are arguments.
1105 * Returns the token list that results from the expansion and sets
1106 * *last to the last node in the list that was consumed by the
1107 * expansion. Specifically, *last will be set as follows: as the
1108 * token of the closing right parenthesis.
1110 static token_list_t *
1111 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1113 token_node_t **last)
1117 const char *identifier;
1118 argument_list_t *arguments;
1119 function_status_t status;
1120 token_list_t *substituted;
1121 int parameter_index;
1123 identifier = node->token->value.str;
1125 macro = hash_table_find (parser->defines, identifier);
1127 assert (macro->is_function);
1129 arguments = _argument_list_create (parser);
1130 status = _arguments_parse (arguments, node, last);
1133 case FUNCTION_STATUS_SUCCESS:
1135 case FUNCTION_NOT_A_FUNCTION:
1137 case FUNCTION_UNBALANCED_PARENTHESES:
1138 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1142 /* Replace a macro defined as empty with a SPACE token. */
1143 if (macro->replacements == NULL) {
1144 talloc_free (arguments);
1145 return _token_list_create_with_one_space (parser);
1148 if (! ((_argument_list_length (arguments) ==
1149 _string_list_length (macro->parameters)) ||
1150 (_string_list_length (macro->parameters) == 0 &&
1151 _argument_list_length (arguments) == 1 &&
1152 arguments->head->argument->head == NULL)))
1154 glcpp_error (&node->token->location, parser,
1155 "Error: macro %s invoked with %d arguments (expected %d)\n",
1157 _argument_list_length (arguments),
1158 _string_list_length (macro->parameters));
1162 /* Perform argument substitution on the replacement list. */
1163 substituted = _token_list_create (arguments);
1165 for (node = macro->replacements->head; node; node = node->next)
1167 if (node->token->type == IDENTIFIER &&
1168 _string_list_contains (macro->parameters,
1169 node->token->value.str,
1172 token_list_t *argument;
1173 argument = _argument_list_member_at (arguments,
1175 /* Before substituting, we expand the argument
1176 * tokens, or append a placeholder token for
1177 * an empty argument. */
1178 if (argument->head) {
1179 token_list_t *expanded_argument;
1180 expanded_argument = _token_list_copy (parser,
1182 _glcpp_parser_expand_token_list (parser,
1184 _token_list_append_list (substituted,
1189 new_token = _token_create_ival (substituted,
1192 _token_list_append (substituted, new_token);
1195 _token_list_append (substituted, node->token);
1199 /* After argument substitution, and before further expansion
1200 * below, implement token pasting. */
1202 _token_list_trim_trailing_space (substituted);
1204 node = substituted->head;
1207 token_node_t *next_non_space;
1209 /* Look ahead for a PASTE token, skipping space. */
1210 next_non_space = node->next;
1211 while (next_non_space && next_non_space->token->type == SPACE)
1212 next_non_space = next_non_space->next;
1214 if (next_non_space == NULL)
1217 if (next_non_space->token->type != PASTE) {
1218 node = next_non_space;
1222 /* Now find the next non-space token after the PASTE. */
1223 next_non_space = next_non_space->next;
1224 while (next_non_space && next_non_space->token->type == SPACE)
1225 next_non_space = next_non_space->next;
1227 if (next_non_space == NULL) {
1228 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1232 node->token = _token_paste (parser, node->token, next_non_space->token);
1233 node->next = next_non_space->next;
1234 if (next_non_space == substituted->tail)
1235 substituted->tail = node;
1240 substituted->non_space_tail = substituted->tail;
1245 /* Compute the complete expansion of node, (and subsequent nodes after
1246 * 'node' in the case that 'node' is a function-like macro and
1247 * subsequent nodes are arguments).
1249 * Returns NULL if node is a simple token with no expansion.
1251 * Otherwise, returns the token list that results from the expansion
1252 * and sets *last to the last node in the list that was consumed by
1253 * the expansion. Specifically, *last will be set as follows:
1255 * As 'node' in the case of object-like macro expansion.
1257 * As the token of the closing right parenthesis in the case of
1258 * function-like macro expansion.
1260 static token_list_t *
1261 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1263 token_node_t **last)
1265 token_t *token = node->token;
1266 const char *identifier;
1269 /* We only expand identifiers */
1270 if (token->type != IDENTIFIER) {
1271 /* We change any COMMA into a COMMA_FINAL to prevent
1272 * it being mistaken for an argument separator
1274 if (token->type == ',') {
1275 token->type = COMMA_FINAL;
1276 token->value.ival = COMMA_FINAL;
1282 /* Look up this identifier in the hash table. */
1283 identifier = token->value.str;
1284 macro = hash_table_find (parser->defines, identifier);
1286 /* Not a macro, so no expansion needed. */
1290 /* Finally, don't expand this macro if we're already actively
1291 * expanding it, (to avoid infinite recursion). */
1292 if (_active_list_contains (parser->active, identifier)) {
1293 /* We change the token type here from IDENTIFIER to
1294 * OTHER to prevent any future expansion of this
1295 * unexpanded token. */
1297 token_list_t *expansion;
1300 str = xtalloc_strdup (parser, token->value.str);
1301 final = _token_create_str (parser, OTHER, str);
1302 expansion = _token_list_create (parser);
1303 _token_list_append (expansion, final);
1308 if (! macro->is_function)
1312 /* Replace a macro defined as empty with a SPACE token. */
1313 if (macro->replacements == NULL)
1314 return _token_list_create_with_one_space (parser);
1316 return _token_list_copy (parser, macro->replacements);
1319 return _glcpp_parser_expand_function (parser, node, last);
1322 /* Push a new identifier onto the active list, returning the new list.
1324 * Here, 'marker' is the token node that appears in the list after the
1325 * expansion of 'identifier'. That is, when the list iterator begins
1326 * examinging 'marker', then it is time to pop this node from the
1330 _active_list_push (active_list_t *list,
1331 const char *identifier,
1332 token_node_t *marker)
1334 active_list_t *node;
1336 node = xtalloc (list, active_list_t);
1337 node->identifier = xtalloc_strdup (node, identifier);
1338 node->marker = marker;
1345 _active_list_pop (active_list_t *list)
1347 active_list_t *node = list;
1359 _active_list_contains (active_list_t *list, const char *identifier)
1361 active_list_t *node;
1366 for (node = list; node; node = node->next)
1367 if (strcmp (node->identifier, identifier) == 0)
1373 /* Walk over the token list replacing nodes with their expansion.
1374 * Whenever nodes are expanded the walking will walk over the new
1375 * nodes, continuing to expand as necessary. The results are placed in
1379 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1382 token_node_t *node_prev;
1383 token_node_t *node, *last = NULL;
1384 token_list_t *expansion;
1389 _token_list_trim_trailing_space (list);
1396 while (parser->active && parser->active->marker == node)
1397 parser->active = _active_list_pop (parser->active);
1399 /* Find the expansion for node, which will replace all
1400 * nodes from node to last, inclusive. */
1401 expansion = _glcpp_parser_expand_node (parser, node, &last);
1405 for (n = node; n != last->next; n = n->next)
1406 while (parser->active &&
1407 parser->active->marker == n)
1409 parser->active = _active_list_pop (parser->active);
1412 parser->active = _active_list_push (parser->active,
1413 node->token->value.str,
1416 /* Splice expansion into list, supporting a
1417 * simple deletion if the expansion is
1419 if (expansion->head) {
1421 node_prev->next = expansion->head;
1423 list->head = expansion->head;
1424 expansion->tail->next = last->next;
1425 if (last == list->tail)
1426 list->tail = expansion->tail;
1429 node_prev->next = last->next;
1431 list->head = last->next;
1432 if (last == list->tail)
1438 node = node_prev ? node_prev->next : list->head;
1441 while (parser->active)
1442 parser->active = _active_list_pop (parser->active);
1444 list->non_space_tail = list->tail;
1448 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1454 _glcpp_parser_expand_token_list (parser, list);
1456 _token_list_trim_trailing_space (list);
1458 _token_list_print (parser, list);
1462 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1463 const char *identifier)
1465 /* According to the GLSL specification, macro names starting with "__"
1466 * or "GL_" are reserved for future use. So, don't allow them.
1468 if (strncmp(identifier, "__", 2) == 0) {
1469 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1471 if (strncmp(identifier, "GL_", 3) == 0) {
1472 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1477 _define_object_macro (glcpp_parser_t *parser,
1479 const char *identifier,
1480 token_list_t *replacements)
1485 _check_for_reserved_macro_name(parser, loc, identifier);
1487 macro = xtalloc (parser, macro_t);
1489 macro->is_function = 0;
1490 macro->parameters = NULL;
1491 macro->identifier = talloc_strdup (macro, identifier);
1492 macro->replacements = talloc_steal (macro, replacements);
1494 hash_table_insert (parser->defines, macro, identifier);
1498 _define_function_macro (glcpp_parser_t *parser,
1500 const char *identifier,
1501 string_list_t *parameters,
1502 token_list_t *replacements)
1506 _check_for_reserved_macro_name(parser, loc, identifier);
1508 macro = xtalloc (parser, macro_t);
1510 macro->is_function = 1;
1511 macro->parameters = talloc_steal (macro, parameters);
1512 macro->identifier = talloc_strdup (macro, identifier);
1513 macro->replacements = talloc_steal (macro, replacements);
1515 hash_table_insert (parser->defines, macro, identifier);
1519 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1524 if (parser->lex_from_list == NULL) {
1525 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1527 /* XXX: This ugly block of code exists for the sole
1528 * purpose of converting a NEWLINE token into a SPACE
1529 * token, but only in the case where we have seen a
1530 * function-like macro name, but have not yet seen its
1531 * closing parenthesis.
1533 * There's perhaps a more compact way to do this with
1534 * mid-rule actions in the grammar.
1536 * I'm definitely not pleased with the complexity of
1539 if (parser->newline_as_space)
1542 parser->paren_count++;
1543 } else if (ret == ')') {
1544 parser->paren_count--;
1545 if (parser->paren_count == 0)
1546 parser->newline_as_space = 0;
1547 } else if (ret == NEWLINE) {
1549 } else if (ret != SPACE) {
1550 if (parser->paren_count == 0)
1551 parser->newline_as_space = 0;
1554 else if (parser->in_control_line)
1557 parser->in_control_line = 0;
1559 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1560 ret == HASH_UNDEF || ret == HASH_IF ||
1561 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1562 ret == HASH_ELIF || ret == HASH_ELSE ||
1563 ret == HASH_ENDIF || ret == HASH)
1565 parser->in_control_line = 1;
1567 else if (ret == IDENTIFIER)
1570 macro = hash_table_find (parser->defines,
1572 if (macro && macro->is_function) {
1573 parser->newline_as_space = 1;
1574 parser->paren_count = 0;
1581 node = parser->lex_from_node;
1584 talloc_free (parser->lex_from_list);
1585 parser->lex_from_list = NULL;
1589 *yylval = node->token->value;
1590 ret = node->token->type;
1592 parser->lex_from_node = node->next;
1598 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1602 assert (parser->lex_from_list == NULL);
1604 /* Copy list, eliminating any space tokens. */
1605 parser->lex_from_list = _token_list_create (parser);
1607 for (node = list->head; node; node = node->next) {
1608 if (node->token->type == SPACE)
1610 _token_list_append (parser->lex_from_list, node->token);
1615 parser->lex_from_node = parser->lex_from_list->head;
1617 /* It's possible the list consisted of nothing but whitespace. */
1618 if (parser->lex_from_node == NULL) {
1619 talloc_free (parser->lex_from_list);
1620 parser->lex_from_list = NULL;
1625 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1628 skip_type_t current = SKIP_NO_SKIP;
1631 if (parser->skip_stack)
1632 current = parser->skip_stack->type;
1634 node = xtalloc (parser, skip_node_t);
1637 if (current == SKIP_NO_SKIP) {
1639 node->type = SKIP_NO_SKIP;
1641 node->type = SKIP_TO_ELSE;
1643 node->type = SKIP_TO_ENDIF;
1646 node->next = parser->skip_stack;
1647 parser->skip_stack = node;
1651 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1652 const char *type, int condition)
1654 if (parser->skip_stack == NULL) {
1655 glcpp_error (loc, parser, "%s without #if\n", type);
1659 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1661 parser->skip_stack->type = SKIP_NO_SKIP;
1663 parser->skip_stack->type = SKIP_TO_ENDIF;
1668 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1672 if (parser->skip_stack == NULL) {
1673 glcpp_error (loc, parser, "#endif without #if\n");
1677 node = parser->skip_stack;
1678 parser->skip_stack = node->next;