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 add_builtin_define(parser, "__VERSION__", language_version);
972 glcpp_parser_parse (glcpp_parser_t *parser)
974 return yyparse (parser);
978 glcpp_parser_destroy (glcpp_parser_t *parser)
980 if (parser->skip_stack)
981 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
982 glcpp_lex_destroy (parser->scanner);
983 hash_table_dtor (parser->defines);
984 talloc_free (parser);
987 typedef enum function_status
989 FUNCTION_STATUS_SUCCESS,
990 FUNCTION_NOT_A_FUNCTION,
991 FUNCTION_UNBALANCED_PARENTHESES
994 /* Find a set of function-like macro arguments by looking for a
995 * balanced set of parentheses.
997 * When called, 'node' should be the opening-parenthesis token, (or
998 * perhaps preceeding SPACE tokens). Upon successful return *last will
999 * be the last consumed node, (corresponding to the closing right
1004 * FUNCTION_STATUS_SUCCESS:
1006 * Successfully parsed a set of function arguments.
1008 * FUNCTION_NOT_A_FUNCTION:
1010 * Macro name not followed by a '('. This is not an error, but
1011 * simply that the macro name should be treated as a non-macro.
1013 * FUNCTION_UNBALANCED_PARENTHESES
1015 * Macro name is not followed by a balanced set of parentheses.
1017 static function_status_t
1018 _arguments_parse (argument_list_t *arguments,
1020 token_node_t **last)
1022 token_list_t *argument;
1027 /* Ignore whitespace before first parenthesis. */
1028 while (node && node->token->type == SPACE)
1031 if (node == NULL || node->token->type != '(')
1032 return FUNCTION_NOT_A_FUNCTION;
1036 argument = _token_list_create (arguments);
1037 _argument_list_append (arguments, argument);
1039 for (paren_count = 1; node; node = node->next) {
1040 if (node->token->type == '(')
1044 else if (node->token->type == ')')
1047 if (paren_count == 0)
1051 if (node->token->type == ',' &&
1054 _token_list_trim_trailing_space (argument);
1055 argument = _token_list_create (arguments);
1056 _argument_list_append (arguments, argument);
1059 if (argument->head == NULL) {
1060 /* Don't treat initial whitespace as
1061 * part of the arguement. */
1062 if (node->token->type == SPACE)
1065 _token_list_append (argument, node->token);
1070 return FUNCTION_UNBALANCED_PARENTHESES;
1074 return FUNCTION_STATUS_SUCCESS;
1077 static token_list_t *
1078 _token_list_create_with_one_space (void *ctx)
1083 list = _token_list_create (ctx);
1084 space = _token_create_ival (list, SPACE, SPACE);
1085 _token_list_append (list, space);
1090 /* This is a helper function that's essentially part of the
1091 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1092 * except for by that function.
1094 * Returns NULL if node is a simple token with no expansion, (that is,
1095 * although 'node' corresponds to an identifier defined as a
1096 * function-like macro, it is not followed with a parenthesized
1099 * Compute the complete expansion of node (which is a function-like
1100 * macro) and subsequent nodes which are arguments.
1102 * Returns the token list that results from the expansion and sets
1103 * *last to the last node in the list that was consumed by the
1104 * expansion. Specifically, *last will be set as follows: as the
1105 * token of the closing right parenthesis.
1107 static token_list_t *
1108 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1110 token_node_t **last)
1114 const char *identifier;
1115 argument_list_t *arguments;
1116 function_status_t status;
1117 token_list_t *substituted;
1118 int parameter_index;
1120 identifier = node->token->value.str;
1122 macro = hash_table_find (parser->defines, identifier);
1124 assert (macro->is_function);
1126 arguments = _argument_list_create (parser);
1127 status = _arguments_parse (arguments, node, last);
1130 case FUNCTION_STATUS_SUCCESS:
1132 case FUNCTION_NOT_A_FUNCTION:
1134 case FUNCTION_UNBALANCED_PARENTHESES:
1135 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1139 /* Replace a macro defined as empty with a SPACE token. */
1140 if (macro->replacements == NULL) {
1141 talloc_free (arguments);
1142 return _token_list_create_with_one_space (parser);
1145 if (! ((_argument_list_length (arguments) ==
1146 _string_list_length (macro->parameters)) ||
1147 (_string_list_length (macro->parameters) == 0 &&
1148 _argument_list_length (arguments) == 1 &&
1149 arguments->head->argument->head == NULL)))
1151 glcpp_error (&node->token->location, parser,
1152 "Error: macro %s invoked with %d arguments (expected %d)\n",
1154 _argument_list_length (arguments),
1155 _string_list_length (macro->parameters));
1159 /* Perform argument substitution on the replacement list. */
1160 substituted = _token_list_create (arguments);
1162 for (node = macro->replacements->head; node; node = node->next)
1164 if (node->token->type == IDENTIFIER &&
1165 _string_list_contains (macro->parameters,
1166 node->token->value.str,
1169 token_list_t *argument;
1170 argument = _argument_list_member_at (arguments,
1172 /* Before substituting, we expand the argument
1173 * tokens, or append a placeholder token for
1174 * an empty argument. */
1175 if (argument->head) {
1176 token_list_t *expanded_argument;
1177 expanded_argument = _token_list_copy (parser,
1179 _glcpp_parser_expand_token_list (parser,
1181 _token_list_append_list (substituted,
1186 new_token = _token_create_ival (substituted,
1189 _token_list_append (substituted, new_token);
1192 _token_list_append (substituted, node->token);
1196 /* After argument substitution, and before further expansion
1197 * below, implement token pasting. */
1199 _token_list_trim_trailing_space (substituted);
1201 node = substituted->head;
1204 token_node_t *next_non_space;
1206 /* Look ahead for a PASTE token, skipping space. */
1207 next_non_space = node->next;
1208 while (next_non_space && next_non_space->token->type == SPACE)
1209 next_non_space = next_non_space->next;
1211 if (next_non_space == NULL)
1214 if (next_non_space->token->type != PASTE) {
1215 node = next_non_space;
1219 /* Now find the next non-space token after the PASTE. */
1220 next_non_space = next_non_space->next;
1221 while (next_non_space && next_non_space->token->type == SPACE)
1222 next_non_space = next_non_space->next;
1224 if (next_non_space == NULL) {
1225 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1229 node->token = _token_paste (parser, node->token, next_non_space->token);
1230 node->next = next_non_space->next;
1231 if (next_non_space == substituted->tail)
1232 substituted->tail = node;
1237 substituted->non_space_tail = substituted->tail;
1242 /* Compute the complete expansion of node, (and subsequent nodes after
1243 * 'node' in the case that 'node' is a function-like macro and
1244 * subsequent nodes are arguments).
1246 * Returns NULL if node is a simple token with no expansion.
1248 * Otherwise, returns the token list that results from the expansion
1249 * and sets *last to the last node in the list that was consumed by
1250 * the expansion. Specifically, *last will be set as follows:
1252 * As 'node' in the case of object-like macro expansion.
1254 * As the token of the closing right parenthesis in the case of
1255 * function-like macro expansion.
1257 static token_list_t *
1258 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1260 token_node_t **last)
1262 token_t *token = node->token;
1263 const char *identifier;
1266 /* We only expand identifiers */
1267 if (token->type != IDENTIFIER) {
1268 /* We change any COMMA into a COMMA_FINAL to prevent
1269 * it being mistaken for an argument separator
1271 if (token->type == ',') {
1272 token->type = COMMA_FINAL;
1273 token->value.ival = COMMA_FINAL;
1279 /* Look up this identifier in the hash table. */
1280 identifier = token->value.str;
1281 macro = hash_table_find (parser->defines, identifier);
1283 /* Not a macro, so no expansion needed. */
1287 /* Finally, don't expand this macro if we're already actively
1288 * expanding it, (to avoid infinite recursion). */
1289 if (_active_list_contains (parser->active, identifier)) {
1290 /* We change the token type here from IDENTIFIER to
1291 * OTHER to prevent any future expansion of this
1292 * unexpanded token. */
1294 token_list_t *expansion;
1297 str = xtalloc_strdup (parser, token->value.str);
1298 final = _token_create_str (parser, OTHER, str);
1299 expansion = _token_list_create (parser);
1300 _token_list_append (expansion, final);
1305 if (! macro->is_function)
1309 /* Replace a macro defined as empty with a SPACE token. */
1310 if (macro->replacements == NULL)
1311 return _token_list_create_with_one_space (parser);
1313 return _token_list_copy (parser, macro->replacements);
1316 return _glcpp_parser_expand_function (parser, node, last);
1319 /* Push a new identifier onto the active list, returning the new list.
1321 * Here, 'marker' is the token node that appears in the list after the
1322 * expansion of 'identifier'. That is, when the list iterator begins
1323 * examinging 'marker', then it is time to pop this node from the
1327 _active_list_push (active_list_t *list,
1328 const char *identifier,
1329 token_node_t *marker)
1331 active_list_t *node;
1333 node = xtalloc (list, active_list_t);
1334 node->identifier = xtalloc_strdup (node, identifier);
1335 node->marker = marker;
1342 _active_list_pop (active_list_t *list)
1344 active_list_t *node = list;
1356 _active_list_contains (active_list_t *list, const char *identifier)
1358 active_list_t *node;
1363 for (node = list; node; node = node->next)
1364 if (strcmp (node->identifier, identifier) == 0)
1370 /* Walk over the token list replacing nodes with their expansion.
1371 * Whenever nodes are expanded the walking will walk over the new
1372 * nodes, continuing to expand as necessary. The results are placed in
1376 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1379 token_node_t *node_prev;
1380 token_node_t *node, *last = NULL;
1381 token_list_t *expansion;
1386 _token_list_trim_trailing_space (list);
1393 while (parser->active && parser->active->marker == node)
1394 parser->active = _active_list_pop (parser->active);
1396 /* Find the expansion for node, which will replace all
1397 * nodes from node to last, inclusive. */
1398 expansion = _glcpp_parser_expand_node (parser, node, &last);
1402 for (n = node; n != last->next; n = n->next)
1403 while (parser->active &&
1404 parser->active->marker == n)
1406 parser->active = _active_list_pop (parser->active);
1409 parser->active = _active_list_push (parser->active,
1410 node->token->value.str,
1413 /* Splice expansion into list, supporting a
1414 * simple deletion if the expansion is
1416 if (expansion->head) {
1418 node_prev->next = expansion->head;
1420 list->head = expansion->head;
1421 expansion->tail->next = last->next;
1422 if (last == list->tail)
1423 list->tail = expansion->tail;
1426 node_prev->next = last->next;
1428 list->head = last->next;
1429 if (last == list->tail)
1435 node = node_prev ? node_prev->next : list->head;
1438 while (parser->active)
1439 parser->active = _active_list_pop (parser->active);
1441 list->non_space_tail = list->tail;
1445 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1451 _glcpp_parser_expand_token_list (parser, list);
1453 _token_list_trim_trailing_space (list);
1455 _token_list_print (parser, list);
1459 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1460 const char *identifier)
1462 /* According to the GLSL specification, macro names starting with "__"
1463 * or "GL_" are reserved for future use. So, don't allow them.
1465 if (strncmp(identifier, "__", 2) == 0) {
1466 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1468 if (strncmp(identifier, "GL_", 3) == 0) {
1469 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1474 _define_object_macro (glcpp_parser_t *parser,
1476 const char *identifier,
1477 token_list_t *replacements)
1482 _check_for_reserved_macro_name(parser, loc, identifier);
1484 macro = xtalloc (parser, macro_t);
1486 macro->is_function = 0;
1487 macro->parameters = NULL;
1488 macro->identifier = talloc_strdup (macro, identifier);
1489 macro->replacements = talloc_steal (macro, replacements);
1491 hash_table_insert (parser->defines, macro, identifier);
1495 _define_function_macro (glcpp_parser_t *parser,
1497 const char *identifier,
1498 string_list_t *parameters,
1499 token_list_t *replacements)
1503 _check_for_reserved_macro_name(parser, loc, identifier);
1505 macro = xtalloc (parser, macro_t);
1507 macro->is_function = 1;
1508 macro->parameters = talloc_steal (macro, parameters);
1509 macro->identifier = talloc_strdup (macro, identifier);
1510 macro->replacements = talloc_steal (macro, replacements);
1512 hash_table_insert (parser->defines, macro, identifier);
1516 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1521 if (parser->lex_from_list == NULL) {
1522 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1524 /* XXX: This ugly block of code exists for the sole
1525 * purpose of converting a NEWLINE token into a SPACE
1526 * token, but only in the case where we have seen a
1527 * function-like macro name, but have not yet seen its
1528 * closing parenthesis.
1530 * There's perhaps a more compact way to do this with
1531 * mid-rule actions in the grammar.
1533 * I'm definitely not pleased with the complexity of
1536 if (parser->newline_as_space)
1539 parser->paren_count++;
1540 } else if (ret == ')') {
1541 parser->paren_count--;
1542 if (parser->paren_count == 0)
1543 parser->newline_as_space = 0;
1544 } else if (ret == NEWLINE) {
1546 } else if (ret != SPACE) {
1547 if (parser->paren_count == 0)
1548 parser->newline_as_space = 0;
1551 else if (parser->in_control_line)
1554 parser->in_control_line = 0;
1556 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1557 ret == HASH_UNDEF || ret == HASH_IF ||
1558 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1559 ret == HASH_ELIF || ret == HASH_ELSE ||
1560 ret == HASH_ENDIF || ret == HASH)
1562 parser->in_control_line = 1;
1564 else if (ret == IDENTIFIER)
1567 macro = hash_table_find (parser->defines,
1569 if (macro && macro->is_function) {
1570 parser->newline_as_space = 1;
1571 parser->paren_count = 0;
1578 node = parser->lex_from_node;
1581 talloc_free (parser->lex_from_list);
1582 parser->lex_from_list = NULL;
1586 *yylval = node->token->value;
1587 ret = node->token->type;
1589 parser->lex_from_node = node->next;
1595 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1599 assert (parser->lex_from_list == NULL);
1601 /* Copy list, eliminating any space tokens. */
1602 parser->lex_from_list = _token_list_create (parser);
1604 for (node = list->head; node; node = node->next) {
1605 if (node->token->type == SPACE)
1607 _token_list_append (parser->lex_from_list, node->token);
1612 parser->lex_from_node = parser->lex_from_list->head;
1614 /* It's possible the list consisted of nothing but whitespace. */
1615 if (parser->lex_from_node == NULL) {
1616 talloc_free (parser->lex_from_list);
1617 parser->lex_from_list = NULL;
1622 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1625 skip_type_t current = SKIP_NO_SKIP;
1628 if (parser->skip_stack)
1629 current = parser->skip_stack->type;
1631 node = xtalloc (parser, skip_node_t);
1634 if (current == SKIP_NO_SKIP) {
1636 node->type = SKIP_NO_SKIP;
1638 node->type = SKIP_TO_ELSE;
1640 node->type = SKIP_TO_ENDIF;
1643 node->next = parser->skip_stack;
1644 parser->skip_stack = node;
1648 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1649 const char *type, int condition)
1651 if (parser->skip_stack == NULL) {
1652 glcpp_error (loc, parser, "%s without #if\n", type);
1656 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1658 parser->skip_stack->type = SKIP_NO_SKIP;
1660 parser->skip_stack->type = SKIP_TO_ENDIF;
1665 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1669 if (parser->skip_stack == NULL) {
1670 glcpp_error (loc, parser, "#endif without #if\n");
1674 node = parser->skip_stack;
1675 parser->skip_stack = node->next;