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 #include "main/core.h" /* for struct gl_extensions */
33 #include "main/mtypes.h" /* for gl_api enum */
36 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
39 _define_object_macro (glcpp_parser_t *parser,
42 token_list_t *replacements);
45 _define_function_macro (glcpp_parser_t *parser,
48 string_list_t *parameters,
49 token_list_t *replacements);
51 static string_list_t *
52 _string_list_create (void *ctx);
55 _string_list_append_item (string_list_t *list, const char *str);
58 _string_list_contains (string_list_t *list, const char *member, int *index);
61 _string_list_length (string_list_t *list);
64 _string_list_equal (string_list_t *a, string_list_t *b);
66 static argument_list_t *
67 _argument_list_create (void *ctx);
70 _argument_list_append (argument_list_t *list, token_list_t *argument);
73 _argument_list_length (argument_list_t *list);
76 _argument_list_member_at (argument_list_t *list, int index);
78 /* Note: This function ralloc_steal()s the str pointer. */
80 _token_create_str (void *ctx, int type, char *str);
83 _token_create_ival (void *ctx, int type, int ival);
86 _token_list_create (void *ctx);
88 /* Note: This function calls ralloc_steal on token. */
90 _token_list_append (token_list_t *list, token_t *token);
93 _token_list_append_list (token_list_t *list, token_list_t *tail);
96 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
98 static active_list_t *
99 _active_list_push (active_list_t *list,
100 const char *identifier,
101 token_node_t *marker);
103 static active_list_t *
104 _active_list_pop (active_list_t *list);
107 _active_list_contains (active_list_t *list, const char *identifier);
110 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
113 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
117 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
121 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
125 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
126 const char *type, int condition);
129 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
131 #define yylex glcpp_parser_lex
134 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
137 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
140 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
156 %parse-param {glcpp_parser_t *parser}
157 %lex-param {glcpp_parser_t *parser}
160 %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
162 %type <ival> expression INTEGER operator SPACE integer_constant
163 %type <str> IDENTIFIER INTEGER_STRING OTHER
164 %type <string_list> identifier_list
165 %type <token> preprocessing_token conditional_token
166 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
172 %left EQUAL NOT_EQUAL
173 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
174 %left LEFT_SHIFT RIGHT_SHIFT
188 ralloc_strcat (&parser->output, "\n");
191 _glcpp_parser_print_expanded_token_list (parser, $1);
192 ralloc_strcat (&parser->output, "\n");
200 IF_EXPANDED expression NEWLINE {
201 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
203 | ELIF_EXPANDED expression NEWLINE {
204 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
209 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
210 _define_object_macro (parser, & @2, $2, $3);
212 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
213 _define_function_macro (parser, & @2, $2, NULL, $5);
215 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
216 _define_function_macro (parser, & @2, $2, $4, $6);
218 | HASH_UNDEF IDENTIFIER NEWLINE {
219 macro_t *macro = hash_table_find (parser->defines, $2);
221 hash_table_remove (parser->defines, $2);
226 | HASH_IF conditional_tokens NEWLINE {
227 /* Be careful to only evaluate the 'if' expression if
228 * we are not skipping. When we are skipping, we
229 * simply push a new 0-valued 'if' onto the skip
232 * This avoids generating diagnostics for invalid
233 * expressions that are being skipped. */
234 if (parser->skip_stack == NULL ||
235 parser->skip_stack->type == SKIP_NO_SKIP)
237 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
241 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
242 parser->skip_stack->type = SKIP_TO_ENDIF;
246 /* #if without an expression is only an error if we
247 * are not skipping */
248 if (parser->skip_stack == NULL ||
249 parser->skip_stack->type == SKIP_NO_SKIP)
251 glcpp_error(& @1, parser, "#if with no expression");
253 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
255 | HASH_IFDEF IDENTIFIER junk NEWLINE {
256 macro_t *macro = hash_table_find (parser->defines, $2);
258 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
260 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
261 macro_t *macro = hash_table_find (parser->defines, $2);
263 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
265 | HASH_ELIF conditional_tokens NEWLINE {
266 /* Be careful to only evaluate the 'elif' expression
267 * if we are not skipping. When we are skipping, we
268 * simply change to a 0-valued 'elif' on the skip
271 * This avoids generating diagnostics for invalid
272 * expressions that are being skipped. */
273 if (parser->skip_stack &&
274 parser->skip_stack->type == SKIP_TO_ELSE)
276 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
280 _glcpp_parser_skip_stack_change_if (parser, & @1,
284 | HASH_ELIF NEWLINE {
285 /* #elif without an expression is an error unless we
287 if (parser->skip_stack &&
288 parser->skip_stack->type == SKIP_TO_ELSE)
290 glcpp_error(& @1, parser, "#elif with no expression");
294 _glcpp_parser_skip_stack_change_if (parser, & @1,
296 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
299 | HASH_ELSE NEWLINE {
300 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
302 | HASH_ENDIF NEWLINE {
303 _glcpp_parser_skip_stack_pop (parser, & @1);
305 | HASH_VERSION integer_constant NEWLINE {
306 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
308 hash_table_remove (parser->defines, "__VERSION__");
311 add_builtin_define (parser, "__VERSION__", $2);
314 add_builtin_define (parser, "GL_ES", 1);
316 /* Currently, all ES2 implementations support highp in the
317 * fragment shader, so we always define this macro in ES2.
318 * If we ever get a driver that doesn't support highp, we'll
319 * need to add a flag to the gl_context and check that here.
321 if ($2 >= 130 || $2 == 100)
322 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
324 ralloc_asprintf_append (&parser->output, "#version %" PRIiMAX, $2);
331 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
332 $$ = strtoll ($1 + 2, NULL, 16);
333 } else if ($1[0] == '0') {
334 $$ = strtoll ($1, NULL, 8);
336 $$ = strtoll ($1, NULL, 10);
345 | expression OR expression {
348 | expression AND expression {
351 | expression '|' expression {
354 | expression '^' expression {
357 | expression '&' expression {
360 | expression NOT_EQUAL expression {
363 | expression EQUAL expression {
366 | expression GREATER_OR_EQUAL expression {
369 | expression LESS_OR_EQUAL expression {
372 | expression '>' expression {
375 | expression '<' expression {
378 | expression RIGHT_SHIFT expression {
381 | expression LEFT_SHIFT expression {
384 | expression '-' expression {
387 | expression '+' expression {
390 | expression '%' expression {
393 | expression '/' expression {
395 yyerror (& @1, parser,
396 "division by 0 in preprocessor directive");
401 | expression '*' expression {
404 | '!' expression %prec UNARY {
407 | '~' expression %prec UNARY {
410 | '-' expression %prec UNARY {
413 | '+' expression %prec UNARY {
416 | '(' expression ')' {
423 $$ = _string_list_create (parser);
424 _string_list_append_item ($$, $1);
425 ralloc_steal ($$, $1);
427 | identifier_list ',' IDENTIFIER {
429 _string_list_append_item ($$, $3);
430 ralloc_steal ($$, $3);
435 NEWLINE { $$ = NULL; }
441 yyerror (& @1, parser, "Invalid tokens after #");
446 /* empty */ { $$ = NULL; }
453 glcpp_warning(&@1, parser, "extra tokens at end of directive");
458 /* Handle "defined" operator */
460 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
461 $$ = _token_create_ival (parser, INTEGER, v);
463 | DEFINED '(' IDENTIFIER ')' {
464 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
465 $$ = _token_create_ival (parser, INTEGER, v);
467 | preprocessing_token
471 /* Exactly the same as pp_tokens, but using conditional_token */
473 $$ = _token_list_create (parser);
474 _token_list_append ($$, $1);
476 | conditional_tokens conditional_token {
478 _token_list_append ($$, $2);
483 preprocessing_token {
484 parser->space_tokens = 1;
485 $$ = _token_list_create (parser);
486 _token_list_append ($$, $1);
488 | pp_tokens preprocessing_token {
490 _token_list_append ($$, $2);
496 $$ = _token_create_str (parser, IDENTIFIER, $1);
497 $$->location = yylloc;
500 $$ = _token_create_str (parser, INTEGER_STRING, $1);
501 $$->location = yylloc;
504 $$ = _token_create_ival (parser, $1, $1);
505 $$->location = yylloc;
508 $$ = _token_create_str (parser, OTHER, $1);
509 $$->location = yylloc;
512 $$ = _token_create_ival (parser, SPACE, SPACE);
513 $$->location = yylloc;
533 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
534 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
537 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
538 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
539 | EQUAL { $$ = EQUAL; }
540 | NOT_EQUAL { $$ = NOT_EQUAL; }
548 | PASTE { $$ = PASTE; }
554 _string_list_create (void *ctx)
558 list = ralloc (ctx, string_list_t);
566 _string_list_append_item (string_list_t *list, const char *str)
570 node = ralloc (list, string_node_t);
571 node->str = ralloc_strdup (node, str);
575 if (list->head == NULL) {
578 list->tail->next = node;
585 _string_list_contains (string_list_t *list, const char *member, int *index)
593 for (i = 0, node = list->head; node; i++, node = node->next) {
594 if (strcmp (node->str, member) == 0) {
605 _string_list_length (string_list_t *list)
613 for (node = list->head; node; node = node->next)
620 _string_list_equal (string_list_t *a, string_list_t *b)
622 string_node_t *node_a, *node_b;
624 if (a == NULL && b == NULL)
627 if (a == NULL || b == NULL)
630 for (node_a = a->head, node_b = b->head;
632 node_a = node_a->next, node_b = node_b->next)
634 if (strcmp (node_a->str, node_b->str))
638 /* Catch the case of lists being different lengths, (which
639 * would cause the loop above to terminate after the shorter
641 return node_a == node_b;
645 _argument_list_create (void *ctx)
647 argument_list_t *list;
649 list = ralloc (ctx, argument_list_t);
657 _argument_list_append (argument_list_t *list, token_list_t *argument)
659 argument_node_t *node;
661 node = ralloc (list, argument_node_t);
662 node->argument = argument;
666 if (list->head == NULL) {
669 list->tail->next = node;
676 _argument_list_length (argument_list_t *list)
679 argument_node_t *node;
684 for (node = list->head; node; node = node->next)
691 _argument_list_member_at (argument_list_t *list, int index)
693 argument_node_t *node;
700 for (i = 0; i < index; i++) {
707 return node->argument;
712 /* Note: This function ralloc_steal()s the str pointer. */
714 _token_create_str (void *ctx, int type, char *str)
718 token = ralloc (ctx, token_t);
720 token->value.str = str;
722 ralloc_steal (token, str);
728 _token_create_ival (void *ctx, int type, int ival)
732 token = ralloc (ctx, token_t);
734 token->value.ival = ival;
740 _token_list_create (void *ctx)
744 list = ralloc (ctx, token_list_t);
747 list->non_space_tail = NULL;
753 _token_list_append (token_list_t *list, token_t *token)
757 node = ralloc (list, token_node_t);
761 ralloc_steal (list, token);
763 if (list->head == NULL) {
766 list->tail->next = node;
770 if (token->type != SPACE)
771 list->non_space_tail = node;
775 _token_list_append_list (token_list_t *list, token_list_t *tail)
777 if (tail == NULL || tail->head == NULL)
780 if (list->head == NULL) {
781 list->head = tail->head;
783 list->tail->next = tail->head;
786 list->tail = tail->tail;
787 list->non_space_tail = tail->non_space_tail;
790 static token_list_t *
791 _token_list_copy (void *ctx, token_list_t *other)
799 copy = _token_list_create (ctx);
800 for (node = other->head; node; node = node->next) {
801 token_t *new_token = ralloc (copy, token_t);
802 *new_token = *node->token;
803 _token_list_append (copy, new_token);
810 _token_list_trim_trailing_space (token_list_t *list)
812 token_node_t *tail, *next;
814 if (list->non_space_tail) {
815 tail = list->non_space_tail->next;
816 list->non_space_tail->next = NULL;
817 list->tail = list->non_space_tail;
828 _token_list_is_empty_ignoring_space (token_list_t *l)
836 while (n != NULL && n->token->type == SPACE)
843 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
845 token_node_t *node_a, *node_b;
847 if (a == NULL || b == NULL) {
848 int a_empty = _token_list_is_empty_ignoring_space(a);
849 int b_empty = _token_list_is_empty_ignoring_space(b);
850 return a_empty == b_empty;
858 if (node_a == NULL && node_b == NULL)
861 if (node_a == NULL || node_b == NULL)
864 if (node_a->token->type == SPACE) {
865 node_a = node_a->next;
869 if (node_b->token->type == SPACE) {
870 node_b = node_b->next;
874 if (node_a->token->type != node_b->token->type)
877 switch (node_a->token->type) {
879 if (node_a->token->value.ival !=
880 node_b->token->value.ival)
888 if (strcmp (node_a->token->value.str,
889 node_b->token->value.str))
896 node_a = node_a->next;
897 node_b = node_b->next;
904 _token_print (char **out, token_t *token)
906 if (token->type < 256) {
907 ralloc_asprintf_append (out, "%c", token->type);
911 switch (token->type) {
913 ralloc_asprintf_append (out, "%" PRIiMAX, token->value.ival);
918 ralloc_strcat (out, token->value.str);
921 ralloc_strcat (out, " ");
924 ralloc_strcat (out, "<<");
927 ralloc_strcat (out, ">>");
930 ralloc_strcat (out, "<=");
932 case GREATER_OR_EQUAL:
933 ralloc_strcat (out, ">=");
936 ralloc_strcat (out, "==");
939 ralloc_strcat (out, "!=");
942 ralloc_strcat (out, "&&");
945 ralloc_strcat (out, "||");
948 ralloc_strcat (out, "##");
951 ralloc_strcat (out, ",");
954 /* Nothing to print. */
957 assert(!"Error: Don't know how to print token.");
962 /* Return a new token (ralloc()ed off of 'token') formed by pasting
963 * 'token' and 'other'. Note that this function may return 'token' or
964 * 'other' directly rather than allocating anything new.
966 * Caution: Only very cursory error-checking is performed to see if
967 * the final result is a valid single token. */
969 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
971 token_t *combined = NULL;
973 /* Pasting a placeholder onto anything makes no change. */
974 if (other->type == PLACEHOLDER)
977 /* When 'token' is a placeholder, just return 'other'. */
978 if (token->type == PLACEHOLDER)
981 /* A very few single-character punctuators can be combined
982 * with another to form a multi-character punctuator. */
983 switch (token->type) {
985 if (other->type == '<')
986 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
987 else if (other->type == '=')
988 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
991 if (other->type == '>')
992 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
993 else if (other->type == '=')
994 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
997 if (other->type == '=')
998 combined = _token_create_ival (token, EQUAL, EQUAL);
1001 if (other->type == '=')
1002 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1005 if (other->type == '&')
1006 combined = _token_create_ival (token, AND, AND);
1009 if (other->type == '|')
1010 combined = _token_create_ival (token, OR, OR);
1014 if (combined != NULL) {
1015 /* Inherit the location from the first token */
1016 combined->location = token->location;
1020 /* Two string-valued tokens can usually just be mashed
1023 * XXX: This isn't actually legitimate. Several things here
1024 * should result in a diagnostic since the result cannot be a
1025 * valid, single pre-processing token. For example, pasting
1026 * "123" and "abc" is not legal, but we don't catch that
1028 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1029 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1033 str = ralloc_asprintf (token, "%s%s", token->value.str,
1035 combined = _token_create_str (token, token->type, str);
1036 combined->location = token->location;
1040 glcpp_error (&token->location, parser, "");
1041 ralloc_strcat (&parser->info_log, "Pasting \"");
1042 _token_print (&parser->info_log, token);
1043 ralloc_strcat (&parser->info_log, "\" and \"");
1044 _token_print (&parser->info_log, other);
1045 ralloc_strcat (&parser->info_log, "\" does not give a valid preprocessing token.\n");
1051 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1058 for (node = list->head; node; node = node->next)
1059 _token_print (&parser->output, node->token);
1063 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1065 glcpp_error(locp, parser, "%s", error);
1068 static void add_builtin_define(glcpp_parser_t *parser,
1069 const char *name, int value)
1074 tok = _token_create_ival (parser, INTEGER, value);
1076 list = _token_list_create(parser);
1077 _token_list_append(list, tok);
1078 _define_object_macro(parser, NULL, name, list);
1082 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1084 glcpp_parser_t *parser;
1085 int language_version;
1087 parser = ralloc (NULL, glcpp_parser_t);
1089 glcpp_lex_init_extra (parser, &parser->scanner);
1090 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1091 hash_table_string_compare);
1092 parser->active = NULL;
1093 parser->lexing_if = 0;
1094 parser->space_tokens = 1;
1095 parser->newline_as_space = 0;
1096 parser->in_control_line = 0;
1097 parser->paren_count = 0;
1099 parser->skip_stack = NULL;
1101 parser->lex_from_list = NULL;
1102 parser->lex_from_node = NULL;
1104 parser->output = ralloc_strdup(parser, "");
1105 parser->info_log = ralloc_strdup(parser, "");
1108 /* Add pre-defined macros. */
1109 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1110 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1112 if (api == API_OPENGLES2)
1113 add_builtin_define(parser, "GL_ES", 1);
1115 if (extensions != NULL) {
1116 if (extensions->EXT_texture_array) {
1117 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1120 if (extensions->ARB_fragment_coord_conventions)
1121 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1124 if (extensions->ARB_explicit_attrib_location)
1125 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1126 if (extensions->AMD_conservative_depth)
1127 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1130 language_version = 110;
1131 add_builtin_define(parser, "__VERSION__", language_version);
1137 glcpp_parser_parse (glcpp_parser_t *parser)
1139 return yyparse (parser);
1143 glcpp_parser_destroy (glcpp_parser_t *parser)
1145 glcpp_lex_destroy (parser->scanner);
1146 hash_table_dtor (parser->defines);
1147 ralloc_free (parser);
1150 typedef enum function_status
1152 FUNCTION_STATUS_SUCCESS,
1153 FUNCTION_NOT_A_FUNCTION,
1154 FUNCTION_UNBALANCED_PARENTHESES
1155 } function_status_t;
1157 /* Find a set of function-like macro arguments by looking for a
1158 * balanced set of parentheses.
1160 * When called, 'node' should be the opening-parenthesis token, (or
1161 * perhaps preceeding SPACE tokens). Upon successful return *last will
1162 * be the last consumed node, (corresponding to the closing right
1167 * FUNCTION_STATUS_SUCCESS:
1169 * Successfully parsed a set of function arguments.
1171 * FUNCTION_NOT_A_FUNCTION:
1173 * Macro name not followed by a '('. This is not an error, but
1174 * simply that the macro name should be treated as a non-macro.
1176 * FUNCTION_UNBALANCED_PARENTHESES
1178 * Macro name is not followed by a balanced set of parentheses.
1180 static function_status_t
1181 _arguments_parse (argument_list_t *arguments,
1183 token_node_t **last)
1185 token_list_t *argument;
1190 /* Ignore whitespace before first parenthesis. */
1191 while (node && node->token->type == SPACE)
1194 if (node == NULL || node->token->type != '(')
1195 return FUNCTION_NOT_A_FUNCTION;
1199 argument = _token_list_create (arguments);
1200 _argument_list_append (arguments, argument);
1202 for (paren_count = 1; node; node = node->next) {
1203 if (node->token->type == '(')
1207 else if (node->token->type == ')')
1210 if (paren_count == 0)
1214 if (node->token->type == ',' &&
1217 _token_list_trim_trailing_space (argument);
1218 argument = _token_list_create (arguments);
1219 _argument_list_append (arguments, argument);
1222 if (argument->head == NULL) {
1223 /* Don't treat initial whitespace as
1224 * part of the arguement. */
1225 if (node->token->type == SPACE)
1228 _token_list_append (argument, node->token);
1233 return FUNCTION_UNBALANCED_PARENTHESES;
1237 return FUNCTION_STATUS_SUCCESS;
1240 static token_list_t *
1241 _token_list_create_with_one_space (void *ctx)
1246 list = _token_list_create (ctx);
1247 space = _token_create_ival (list, SPACE, SPACE);
1248 _token_list_append (list, space);
1254 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1256 token_list_t *expanded;
1259 expanded = _token_list_create (parser);
1260 token = _token_create_ival (parser, type, type);
1261 _token_list_append (expanded, token);
1262 _glcpp_parser_expand_token_list (parser, list);
1263 _token_list_append_list (expanded, list);
1264 glcpp_parser_lex_from (parser, expanded);
1267 /* This is a helper function that's essentially part of the
1268 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1269 * except for by that function.
1271 * Returns NULL if node is a simple token with no expansion, (that is,
1272 * although 'node' corresponds to an identifier defined as a
1273 * function-like macro, it is not followed with a parenthesized
1276 * Compute the complete expansion of node (which is a function-like
1277 * macro) and subsequent nodes which are arguments.
1279 * Returns the token list that results from the expansion and sets
1280 * *last to the last node in the list that was consumed by the
1281 * expansion. Specifically, *last will be set as follows: as the
1282 * token of the closing right parenthesis.
1284 static token_list_t *
1285 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1287 token_node_t **last)
1291 const char *identifier;
1292 argument_list_t *arguments;
1293 function_status_t status;
1294 token_list_t *substituted;
1295 int parameter_index;
1297 identifier = node->token->value.str;
1299 macro = hash_table_find (parser->defines, identifier);
1301 assert (macro->is_function);
1303 arguments = _argument_list_create (parser);
1304 status = _arguments_parse (arguments, node, last);
1307 case FUNCTION_STATUS_SUCCESS:
1309 case FUNCTION_NOT_A_FUNCTION:
1311 case FUNCTION_UNBALANCED_PARENTHESES:
1312 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1316 /* Replace a macro defined as empty with a SPACE token. */
1317 if (macro->replacements == NULL) {
1318 ralloc_free (arguments);
1319 return _token_list_create_with_one_space (parser);
1322 if (! ((_argument_list_length (arguments) ==
1323 _string_list_length (macro->parameters)) ||
1324 (_string_list_length (macro->parameters) == 0 &&
1325 _argument_list_length (arguments) == 1 &&
1326 arguments->head->argument->head == NULL)))
1328 glcpp_error (&node->token->location, parser,
1329 "Error: macro %s invoked with %d arguments (expected %d)\n",
1331 _argument_list_length (arguments),
1332 _string_list_length (macro->parameters));
1336 /* Perform argument substitution on the replacement list. */
1337 substituted = _token_list_create (arguments);
1339 for (node = macro->replacements->head; node; node = node->next)
1341 if (node->token->type == IDENTIFIER &&
1342 _string_list_contains (macro->parameters,
1343 node->token->value.str,
1346 token_list_t *argument;
1347 argument = _argument_list_member_at (arguments,
1349 /* Before substituting, we expand the argument
1350 * tokens, or append a placeholder token for
1351 * an empty argument. */
1352 if (argument->head) {
1353 token_list_t *expanded_argument;
1354 expanded_argument = _token_list_copy (parser,
1356 _glcpp_parser_expand_token_list (parser,
1358 _token_list_append_list (substituted,
1363 new_token = _token_create_ival (substituted,
1366 _token_list_append (substituted, new_token);
1369 _token_list_append (substituted, node->token);
1373 /* After argument substitution, and before further expansion
1374 * below, implement token pasting. */
1376 _token_list_trim_trailing_space (substituted);
1378 node = substituted->head;
1381 token_node_t *next_non_space;
1383 /* Look ahead for a PASTE token, skipping space. */
1384 next_non_space = node->next;
1385 while (next_non_space && next_non_space->token->type == SPACE)
1386 next_non_space = next_non_space->next;
1388 if (next_non_space == NULL)
1391 if (next_non_space->token->type != PASTE) {
1392 node = next_non_space;
1396 /* Now find the next non-space token after the PASTE. */
1397 next_non_space = next_non_space->next;
1398 while (next_non_space && next_non_space->token->type == SPACE)
1399 next_non_space = next_non_space->next;
1401 if (next_non_space == NULL) {
1402 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1406 node->token = _token_paste (parser, node->token, next_non_space->token);
1407 node->next = next_non_space->next;
1408 if (next_non_space == substituted->tail)
1409 substituted->tail = node;
1414 substituted->non_space_tail = substituted->tail;
1419 /* Compute the complete expansion of node, (and subsequent nodes after
1420 * 'node' in the case that 'node' is a function-like macro and
1421 * subsequent nodes are arguments).
1423 * Returns NULL if node is a simple token with no expansion.
1425 * Otherwise, returns the token list that results from the expansion
1426 * and sets *last to the last node in the list that was consumed by
1427 * the expansion. Specifically, *last will be set as follows:
1429 * As 'node' in the case of object-like macro expansion.
1431 * As the token of the closing right parenthesis in the case of
1432 * function-like macro expansion.
1434 static token_list_t *
1435 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1437 token_node_t **last)
1439 token_t *token = node->token;
1440 const char *identifier;
1443 /* We only expand identifiers */
1444 if (token->type != IDENTIFIER) {
1445 /* We change any COMMA into a COMMA_FINAL to prevent
1446 * it being mistaken for an argument separator
1448 if (token->type == ',') {
1449 token->type = COMMA_FINAL;
1450 token->value.ival = COMMA_FINAL;
1456 /* Look up this identifier in the hash table. */
1457 identifier = token->value.str;
1458 macro = hash_table_find (parser->defines, identifier);
1460 /* Not a macro, so no expansion needed. */
1464 /* Finally, don't expand this macro if we're already actively
1465 * expanding it, (to avoid infinite recursion). */
1466 if (_active_list_contains (parser->active, identifier)) {
1467 /* We change the token type here from IDENTIFIER to
1468 * OTHER to prevent any future expansion of this
1469 * unexpanded token. */
1471 token_list_t *expansion;
1474 str = ralloc_strdup (parser, token->value.str);
1475 final = _token_create_str (parser, OTHER, str);
1476 expansion = _token_list_create (parser);
1477 _token_list_append (expansion, final);
1482 if (! macro->is_function)
1486 /* Replace a macro defined as empty with a SPACE token. */
1487 if (macro->replacements == NULL)
1488 return _token_list_create_with_one_space (parser);
1490 return _token_list_copy (parser, macro->replacements);
1493 return _glcpp_parser_expand_function (parser, node, last);
1496 /* Push a new identifier onto the active list, returning the new list.
1498 * Here, 'marker' is the token node that appears in the list after the
1499 * expansion of 'identifier'. That is, when the list iterator begins
1500 * examinging 'marker', then it is time to pop this node from the
1504 _active_list_push (active_list_t *list,
1505 const char *identifier,
1506 token_node_t *marker)
1508 active_list_t *node;
1510 node = ralloc (list, active_list_t);
1511 node->identifier = ralloc_strdup (node, identifier);
1512 node->marker = marker;
1519 _active_list_pop (active_list_t *list)
1521 active_list_t *node = list;
1533 _active_list_contains (active_list_t *list, const char *identifier)
1535 active_list_t *node;
1540 for (node = list; node; node = node->next)
1541 if (strcmp (node->identifier, identifier) == 0)
1547 /* Walk over the token list replacing nodes with their expansion.
1548 * Whenever nodes are expanded the walking will walk over the new
1549 * nodes, continuing to expand as necessary. The results are placed in
1553 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1556 token_node_t *node_prev;
1557 token_node_t *node, *last = NULL;
1558 token_list_t *expansion;
1563 _token_list_trim_trailing_space (list);
1570 while (parser->active && parser->active->marker == node)
1571 parser->active = _active_list_pop (parser->active);
1573 /* Find the expansion for node, which will replace all
1574 * nodes from node to last, inclusive. */
1575 expansion = _glcpp_parser_expand_node (parser, node, &last);
1579 for (n = node; n != last->next; n = n->next)
1580 while (parser->active &&
1581 parser->active->marker == n)
1583 parser->active = _active_list_pop (parser->active);
1586 parser->active = _active_list_push (parser->active,
1587 node->token->value.str,
1590 /* Splice expansion into list, supporting a
1591 * simple deletion if the expansion is
1593 if (expansion->head) {
1595 node_prev->next = expansion->head;
1597 list->head = expansion->head;
1598 expansion->tail->next = last->next;
1599 if (last == list->tail)
1600 list->tail = expansion->tail;
1603 node_prev->next = last->next;
1605 list->head = last->next;
1606 if (last == list->tail)
1612 node = node_prev ? node_prev->next : list->head;
1615 while (parser->active)
1616 parser->active = _active_list_pop (parser->active);
1618 list->non_space_tail = list->tail;
1622 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1628 _glcpp_parser_expand_token_list (parser, list);
1630 _token_list_trim_trailing_space (list);
1632 _token_list_print (parser, list);
1636 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1637 const char *identifier)
1639 /* According to the GLSL specification, macro names starting with "__"
1640 * or "GL_" are reserved for future use. So, don't allow them.
1642 if (strncmp(identifier, "__", 2) == 0) {
1643 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1645 if (strncmp(identifier, "GL_", 3) == 0) {
1646 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1651 _macro_equal (macro_t *a, macro_t *b)
1653 if (a->is_function != b->is_function)
1656 if (a->is_function) {
1657 if (! _string_list_equal (a->parameters, b->parameters))
1661 return _token_list_equal_ignoring_space (a->replacements,
1666 _define_object_macro (glcpp_parser_t *parser,
1668 const char *identifier,
1669 token_list_t *replacements)
1671 macro_t *macro, *previous;
1674 _check_for_reserved_macro_name(parser, loc, identifier);
1676 macro = ralloc (parser, macro_t);
1678 macro->is_function = 0;
1679 macro->parameters = NULL;
1680 macro->identifier = ralloc_strdup (macro, identifier);
1681 macro->replacements = replacements;
1682 ralloc_steal (macro, replacements);
1684 previous = hash_table_find (parser->defines, identifier);
1686 if (_macro_equal (macro, previous)) {
1687 ralloc_free (macro);
1690 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1694 hash_table_insert (parser->defines, macro, identifier);
1698 _define_function_macro (glcpp_parser_t *parser,
1700 const char *identifier,
1701 string_list_t *parameters,
1702 token_list_t *replacements)
1704 macro_t *macro, *previous;
1706 _check_for_reserved_macro_name(parser, loc, identifier);
1708 macro = ralloc (parser, macro_t);
1709 ralloc_steal (macro, parameters);
1710 ralloc_steal (macro, replacements);
1712 macro->is_function = 1;
1713 macro->parameters = parameters;
1714 macro->identifier = ralloc_strdup (macro, identifier);
1715 macro->replacements = replacements;
1716 previous = hash_table_find (parser->defines, identifier);
1718 if (_macro_equal (macro, previous)) {
1719 ralloc_free (macro);
1722 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1726 hash_table_insert (parser->defines, macro, identifier);
1730 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1735 if (parser->lex_from_list == NULL) {
1736 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1738 /* XXX: This ugly block of code exists for the sole
1739 * purpose of converting a NEWLINE token into a SPACE
1740 * token, but only in the case where we have seen a
1741 * function-like macro name, but have not yet seen its
1742 * closing parenthesis.
1744 * There's perhaps a more compact way to do this with
1745 * mid-rule actions in the grammar.
1747 * I'm definitely not pleased with the complexity of
1750 if (parser->newline_as_space)
1753 parser->paren_count++;
1754 } else if (ret == ')') {
1755 parser->paren_count--;
1756 if (parser->paren_count == 0)
1757 parser->newline_as_space = 0;
1758 } else if (ret == NEWLINE) {
1760 } else if (ret != SPACE) {
1761 if (parser->paren_count == 0)
1762 parser->newline_as_space = 0;
1765 else if (parser->in_control_line)
1768 parser->in_control_line = 0;
1770 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1771 ret == HASH_UNDEF || ret == HASH_IF ||
1772 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1773 ret == HASH_ELIF || ret == HASH_ELSE ||
1774 ret == HASH_ENDIF || ret == HASH)
1776 parser->in_control_line = 1;
1778 else if (ret == IDENTIFIER)
1781 macro = hash_table_find (parser->defines,
1783 if (macro && macro->is_function) {
1784 parser->newline_as_space = 1;
1785 parser->paren_count = 0;
1792 node = parser->lex_from_node;
1795 ralloc_free (parser->lex_from_list);
1796 parser->lex_from_list = NULL;
1800 *yylval = node->token->value;
1801 ret = node->token->type;
1803 parser->lex_from_node = node->next;
1809 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1813 assert (parser->lex_from_list == NULL);
1815 /* Copy list, eliminating any space tokens. */
1816 parser->lex_from_list = _token_list_create (parser);
1818 for (node = list->head; node; node = node->next) {
1819 if (node->token->type == SPACE)
1821 _token_list_append (parser->lex_from_list, node->token);
1826 parser->lex_from_node = parser->lex_from_list->head;
1828 /* It's possible the list consisted of nothing but whitespace. */
1829 if (parser->lex_from_node == NULL) {
1830 ralloc_free (parser->lex_from_list);
1831 parser->lex_from_list = NULL;
1836 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1839 skip_type_t current = SKIP_NO_SKIP;
1842 if (parser->skip_stack)
1843 current = parser->skip_stack->type;
1845 node = ralloc (parser, skip_node_t);
1848 if (current == SKIP_NO_SKIP) {
1850 node->type = SKIP_NO_SKIP;
1852 node->type = SKIP_TO_ELSE;
1854 node->type = SKIP_TO_ENDIF;
1857 node->next = parser->skip_stack;
1858 parser->skip_stack = node;
1862 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1863 const char *type, int condition)
1865 if (parser->skip_stack == NULL) {
1866 glcpp_error (loc, parser, "%s without #if\n", type);
1870 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1872 parser->skip_stack->type = SKIP_NO_SKIP;
1874 parser->skip_stack->type = SKIP_TO_ENDIF;
1879 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1883 if (parser->skip_stack == NULL) {
1884 glcpp_error (loc, parser, "#endif without #if\n");
1888 node = parser->skip_stack;
1889 parser->skip_stack = node->next;