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 */
35 #define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
36 #define glcpp_printf(stream, fmt, args, ...) \
37 stream = talloc_asprintf_append(stream, fmt, args)
40 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
43 _define_object_macro (glcpp_parser_t *parser,
46 token_list_t *replacements);
49 _define_function_macro (glcpp_parser_t *parser,
52 string_list_t *parameters,
53 token_list_t *replacements);
55 static string_list_t *
56 _string_list_create (void *ctx);
59 _string_list_append_item (string_list_t *list, const char *str);
62 _string_list_contains (string_list_t *list, const char *member, int *index);
65 _string_list_length (string_list_t *list);
68 _string_list_equal (string_list_t *a, string_list_t *b);
70 static argument_list_t *
71 _argument_list_create (void *ctx);
74 _argument_list_append (argument_list_t *list, token_list_t *argument);
77 _argument_list_length (argument_list_t *list);
80 _argument_list_member_at (argument_list_t *list, int index);
82 /* Note: This function talloc_steal()s the str pointer. */
84 _token_create_str (void *ctx, int type, char *str);
87 _token_create_ival (void *ctx, int type, int ival);
90 _token_list_create (void *ctx);
92 /* Note: This function calls talloc_steal on token. */
94 _token_list_append (token_list_t *list, token_t *token);
97 _token_list_append_list (token_list_t *list, token_list_t *tail);
100 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
102 static active_list_t *
103 _active_list_push (active_list_t *list,
104 const char *identifier,
105 token_node_t *marker);
107 static active_list_t *
108 _active_list_pop (active_list_t *list);
111 _active_list_contains (active_list_t *list, const char *identifier);
114 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
117 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
121 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
125 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
129 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
130 const char *type, int condition);
133 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
135 #define yylex glcpp_parser_lex
138 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
141 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
144 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
160 %parse-param {glcpp_parser_t *parser}
161 %lex-param {glcpp_parser_t *parser}
164 %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
166 %type <ival> expression INTEGER operator SPACE integer_constant
167 %type <str> IDENTIFIER INTEGER_STRING OTHER
168 %type <string_list> identifier_list
169 %type <token> preprocessing_token conditional_token
170 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
176 %left EQUAL NOT_EQUAL
177 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
178 %left LEFT_SHIFT RIGHT_SHIFT
192 glcpp_print(parser->output, "\n");
195 _glcpp_parser_print_expanded_token_list (parser, $1);
196 glcpp_print(parser->output, "\n");
204 IF_EXPANDED expression NEWLINE {
205 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
207 | ELIF_EXPANDED expression NEWLINE {
208 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
213 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
214 _define_object_macro (parser, & @2, $2, $3);
216 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
217 _define_function_macro (parser, & @2, $2, NULL, $5);
219 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
220 _define_function_macro (parser, & @2, $2, $4, $6);
222 | HASH_UNDEF IDENTIFIER NEWLINE {
223 macro_t *macro = hash_table_find (parser->defines, $2);
225 hash_table_remove (parser->defines, $2);
230 | HASH_IF conditional_tokens NEWLINE {
231 /* Be careful to only evaluate the 'if' expression if
232 * we are not skipping. When we are skipping, we
233 * simply push a new 0-valued 'if' onto the skip
236 * This avoids generating diagnostics for invalid
237 * expressions that are being skipped. */
238 if (parser->skip_stack == NULL ||
239 parser->skip_stack->type == SKIP_NO_SKIP)
241 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
245 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
246 parser->skip_stack->type = SKIP_TO_ENDIF;
250 /* #if without an expression is only an error if we
251 * are not skipping */
252 if (parser->skip_stack == NULL ||
253 parser->skip_stack->type == SKIP_NO_SKIP)
255 glcpp_error(& @1, parser, "#if with no expression");
257 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
259 | HASH_IFDEF IDENTIFIER junk NEWLINE {
260 macro_t *macro = hash_table_find (parser->defines, $2);
262 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
264 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
265 macro_t *macro = hash_table_find (parser->defines, $2);
267 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
269 | HASH_ELIF conditional_tokens NEWLINE {
270 /* Be careful to only evaluate the 'elif' expression
271 * if we are not skipping. When we are skipping, we
272 * simply change to a 0-valued 'elif' on the skip
275 * This avoids generating diagnostics for invalid
276 * expressions that are being skipped. */
277 if (parser->skip_stack &&
278 parser->skip_stack->type == SKIP_TO_ELSE)
280 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
284 _glcpp_parser_skip_stack_change_if (parser, & @1,
288 | HASH_ELIF NEWLINE {
289 /* #elif without an expression is an error unless we
291 if (parser->skip_stack &&
292 parser->skip_stack->type == SKIP_TO_ELSE)
294 glcpp_error(& @1, parser, "#elif with no expression");
298 _glcpp_parser_skip_stack_change_if (parser, & @1,
300 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
303 | HASH_ELSE NEWLINE {
304 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
306 | HASH_ENDIF NEWLINE {
307 _glcpp_parser_skip_stack_pop (parser, & @1);
309 | HASH_VERSION integer_constant NEWLINE {
310 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
312 hash_table_remove (parser->defines, "__VERSION__");
315 add_builtin_define (parser, "__VERSION__", $2);
318 add_builtin_define (parser, "GL_ES", 1);
320 /* Currently, all ES2 implementations support highp in the
321 * fragment shader, so we always define this macro in ES2.
322 * If we ever get a driver that doesn't support highp, we'll
323 * need to add a flag to the gl_context and check that here.
325 if ($2 >= 130 || $2 == 100)
326 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
328 glcpp_printf(parser->output, "#version %" PRIiMAX, $2);
335 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
336 $$ = strtoll ($1 + 2, NULL, 16);
337 } else if ($1[0] == '0') {
338 $$ = strtoll ($1, NULL, 8);
340 $$ = strtoll ($1, NULL, 10);
349 | expression OR expression {
352 | expression AND expression {
355 | expression '|' expression {
358 | expression '^' expression {
361 | expression '&' expression {
364 | expression NOT_EQUAL expression {
367 | expression EQUAL expression {
370 | expression GREATER_OR_EQUAL expression {
373 | expression LESS_OR_EQUAL expression {
376 | expression '>' expression {
379 | expression '<' expression {
382 | expression RIGHT_SHIFT expression {
385 | expression LEFT_SHIFT expression {
388 | expression '-' expression {
391 | expression '+' expression {
394 | expression '%' expression {
397 | expression '/' expression {
399 yyerror (& @1, parser,
400 "division by 0 in preprocessor directive");
405 | expression '*' expression {
408 | '!' expression %prec UNARY {
411 | '~' expression %prec UNARY {
414 | '-' expression %prec UNARY {
417 | '+' expression %prec UNARY {
420 | '(' expression ')' {
427 $$ = _string_list_create (parser);
428 _string_list_append_item ($$, $1);
429 talloc_steal ($$, $1);
431 | identifier_list ',' IDENTIFIER {
433 _string_list_append_item ($$, $3);
434 talloc_steal ($$, $3);
439 NEWLINE { $$ = NULL; }
445 yyerror (& @1, parser, "Invalid tokens after #");
450 /* empty */ { $$ = NULL; }
457 glcpp_warning(&@1, parser, "extra tokens at end of directive");
462 /* Handle "defined" operator */
464 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
465 $$ = _token_create_ival (parser, INTEGER, v);
467 | DEFINED '(' IDENTIFIER ')' {
468 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
469 $$ = _token_create_ival (parser, INTEGER, v);
471 | preprocessing_token
475 /* Exactly the same as pp_tokens, but using conditional_token */
477 $$ = _token_list_create (parser);
478 _token_list_append ($$, $1);
480 | conditional_tokens conditional_token {
482 _token_list_append ($$, $2);
487 preprocessing_token {
488 parser->space_tokens = 1;
489 $$ = _token_list_create (parser);
490 _token_list_append ($$, $1);
492 | pp_tokens preprocessing_token {
494 _token_list_append ($$, $2);
500 $$ = _token_create_str (parser, IDENTIFIER, $1);
501 $$->location = yylloc;
504 $$ = _token_create_str (parser, INTEGER_STRING, $1);
505 $$->location = yylloc;
508 $$ = _token_create_ival (parser, $1, $1);
509 $$->location = yylloc;
512 $$ = _token_create_str (parser, OTHER, $1);
513 $$->location = yylloc;
516 $$ = _token_create_ival (parser, SPACE, SPACE);
517 $$->location = yylloc;
537 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
538 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
541 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
542 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
543 | EQUAL { $$ = EQUAL; }
544 | NOT_EQUAL { $$ = NOT_EQUAL; }
552 | PASTE { $$ = PASTE; }
558 _string_list_create (void *ctx)
562 list = talloc (ctx, string_list_t);
570 _string_list_append_item (string_list_t *list, const char *str)
574 node = talloc (list, string_node_t);
575 node->str = talloc_strdup (node, str);
579 if (list->head == NULL) {
582 list->tail->next = node;
589 _string_list_contains (string_list_t *list, const char *member, int *index)
597 for (i = 0, node = list->head; node; i++, node = node->next) {
598 if (strcmp (node->str, member) == 0) {
609 _string_list_length (string_list_t *list)
617 for (node = list->head; node; node = node->next)
624 _string_list_equal (string_list_t *a, string_list_t *b)
626 string_node_t *node_a, *node_b;
628 if (a == NULL && b == NULL)
631 if (a == NULL || b == NULL)
634 for (node_a = a->head, node_b = b->head;
636 node_a = node_a->next, node_b = node_b->next)
638 if (strcmp (node_a->str, node_b->str))
642 /* Catch the case of lists being different lengths, (which
643 * would cause the loop above to terminate after the shorter
645 return node_a == node_b;
649 _argument_list_create (void *ctx)
651 argument_list_t *list;
653 list = talloc (ctx, argument_list_t);
661 _argument_list_append (argument_list_t *list, token_list_t *argument)
663 argument_node_t *node;
665 node = talloc (list, argument_node_t);
666 node->argument = argument;
670 if (list->head == NULL) {
673 list->tail->next = node;
680 _argument_list_length (argument_list_t *list)
683 argument_node_t *node;
688 for (node = list->head; node; node = node->next)
695 _argument_list_member_at (argument_list_t *list, int index)
697 argument_node_t *node;
704 for (i = 0; i < index; i++) {
711 return node->argument;
716 /* Note: This function talloc_steal()s the str pointer. */
718 _token_create_str (void *ctx, int type, char *str)
722 token = talloc (ctx, token_t);
724 token->value.str = talloc_steal (token, str);
730 _token_create_ival (void *ctx, int type, int ival)
734 token = talloc (ctx, token_t);
736 token->value.ival = ival;
742 _token_list_create (void *ctx)
746 list = talloc (ctx, token_list_t);
749 list->non_space_tail = NULL;
755 _token_list_append (token_list_t *list, token_t *token)
759 node = talloc (list, token_node_t);
760 node->token = talloc_steal (list, token);
764 if (list->head == NULL) {
767 list->tail->next = node;
771 if (token->type != SPACE)
772 list->non_space_tail = node;
776 _token_list_append_list (token_list_t *list, token_list_t *tail)
778 if (tail == NULL || tail->head == NULL)
781 if (list->head == NULL) {
782 list->head = tail->head;
784 list->tail->next = tail->head;
787 list->tail = tail->tail;
788 list->non_space_tail = tail->non_space_tail;
791 static token_list_t *
792 _token_list_copy (void *ctx, token_list_t *other)
800 copy = _token_list_create (ctx);
801 for (node = other->head; node; node = node->next) {
802 token_t *new_token = talloc (copy, token_t);
803 *new_token = *node->token;
804 _token_list_append (copy, new_token);
811 _token_list_trim_trailing_space (token_list_t *list)
813 token_node_t *tail, *next;
815 if (list->non_space_tail) {
816 tail = list->non_space_tail->next;
817 list->non_space_tail->next = NULL;
818 list->tail = list->non_space_tail;
829 _token_list_is_empty_ignoring_space (token_list_t *l)
837 while (n != NULL && n->token->type == SPACE)
844 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
846 token_node_t *node_a, *node_b;
848 if (a == NULL || b == NULL) {
849 int a_empty = _token_list_is_empty_ignoring_space(a);
850 int b_empty = _token_list_is_empty_ignoring_space(b);
851 return a_empty == b_empty;
859 if (node_a == NULL && node_b == NULL)
862 if (node_a == NULL || node_b == NULL)
865 if (node_a->token->type == SPACE) {
866 node_a = node_a->next;
870 if (node_b->token->type == SPACE) {
871 node_b = node_b->next;
875 if (node_a->token->type != node_b->token->type)
878 switch (node_a->token->type) {
880 if (node_a->token->value.ival !=
881 node_b->token->value.ival)
889 if (strcmp (node_a->token->value.str,
890 node_b->token->value.str))
897 node_a = node_a->next;
898 node_b = node_b->next;
905 _token_print (char **out, token_t *token)
907 if (token->type < 256) {
908 glcpp_printf (*out, "%c", token->type);
912 switch (token->type) {
914 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
919 glcpp_print (*out, token->value.str);
922 glcpp_print (*out, " ");
925 glcpp_print (*out, "<<");
928 glcpp_print (*out, ">>");
931 glcpp_print (*out, "<=");
933 case GREATER_OR_EQUAL:
934 glcpp_print (*out, ">=");
937 glcpp_print (*out, "==");
940 glcpp_print (*out, "!=");
943 glcpp_print (*out, "&&");
946 glcpp_print (*out, "||");
949 glcpp_print (*out, "##");
952 glcpp_print (*out, ",");
955 /* Nothing to print. */
958 assert(!"Error: Don't know how to print token.");
963 /* Return a new token (talloc()ed off of 'token') formed by pasting
964 * 'token' and 'other'. Note that this function may return 'token' or
965 * 'other' directly rather than allocating anything new.
967 * Caution: Only very cursory error-checking is performed to see if
968 * the final result is a valid single token. */
970 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
972 token_t *combined = NULL;
974 /* Pasting a placeholder onto anything makes no change. */
975 if (other->type == PLACEHOLDER)
978 /* When 'token' is a placeholder, just return 'other'. */
979 if (token->type == PLACEHOLDER)
982 /* A very few single-character punctuators can be combined
983 * with another to form a multi-character punctuator. */
984 switch (token->type) {
986 if (other->type == '<')
987 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
988 else if (other->type == '=')
989 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
992 if (other->type == '>')
993 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
994 else if (other->type == '=')
995 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
998 if (other->type == '=')
999 combined = _token_create_ival (token, EQUAL, EQUAL);
1002 if (other->type == '=')
1003 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1006 if (other->type == '&')
1007 combined = _token_create_ival (token, AND, AND);
1010 if (other->type == '|')
1011 combined = _token_create_ival (token, OR, OR);
1015 if (combined != NULL) {
1016 /* Inherit the location from the first token */
1017 combined->location = token->location;
1021 /* Two string-valued tokens can usually just be mashed
1024 * XXX: This isn't actually legitimate. Several things here
1025 * should result in a diagnostic since the result cannot be a
1026 * valid, single pre-processing token. For example, pasting
1027 * "123" and "abc" is not legal, but we don't catch that
1029 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1030 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1034 str = talloc_asprintf (token, "%s%s", token->value.str,
1036 combined = _token_create_str (token, token->type, str);
1037 combined->location = token->location;
1041 glcpp_error (&token->location, parser, "");
1042 glcpp_print (parser->info_log, "Pasting \"");
1043 _token_print (&parser->info_log, token);
1044 glcpp_print (parser->info_log, "\" and \"");
1045 _token_print (&parser->info_log, other);
1046 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
1052 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1059 for (node = list->head; node; node = node->next)
1060 _token_print (&parser->output, node->token);
1064 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1066 glcpp_error(locp, parser, "%s", error);
1069 static void add_builtin_define(glcpp_parser_t *parser,
1070 const char *name, int value)
1075 tok = _token_create_ival (parser, INTEGER, value);
1077 list = _token_list_create(parser);
1078 _token_list_append(list, tok);
1079 _define_object_macro(parser, NULL, name, list);
1083 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1085 glcpp_parser_t *parser;
1086 int language_version;
1088 parser = talloc (NULL, glcpp_parser_t);
1090 glcpp_lex_init_extra (parser, &parser->scanner);
1091 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1092 hash_table_string_compare);
1093 parser->active = NULL;
1094 parser->lexing_if = 0;
1095 parser->space_tokens = 1;
1096 parser->newline_as_space = 0;
1097 parser->in_control_line = 0;
1098 parser->paren_count = 0;
1100 parser->skip_stack = NULL;
1102 parser->lex_from_list = NULL;
1103 parser->lex_from_node = NULL;
1105 parser->output = talloc_strdup(parser, "");
1106 parser->info_log = talloc_strdup(parser, "");
1109 /* Add pre-defined macros. */
1110 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1111 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1113 if (api == API_OPENGLES2)
1114 add_builtin_define(parser, "GL_ES", 1);
1116 if (extensions != NULL) {
1117 if (extensions->EXT_texture_array) {
1118 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1121 if (extensions->ARB_fragment_coord_conventions)
1122 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1125 if (extensions->ARB_explicit_attrib_location)
1126 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1127 if (extensions->AMD_conservative_depth)
1128 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1131 language_version = 110;
1132 add_builtin_define(parser, "__VERSION__", language_version);
1138 glcpp_parser_parse (glcpp_parser_t *parser)
1140 return yyparse (parser);
1144 glcpp_parser_destroy (glcpp_parser_t *parser)
1146 glcpp_lex_destroy (parser->scanner);
1147 hash_table_dtor (parser->defines);
1148 talloc_free (parser);
1151 typedef enum function_status
1153 FUNCTION_STATUS_SUCCESS,
1154 FUNCTION_NOT_A_FUNCTION,
1155 FUNCTION_UNBALANCED_PARENTHESES
1156 } function_status_t;
1158 /* Find a set of function-like macro arguments by looking for a
1159 * balanced set of parentheses.
1161 * When called, 'node' should be the opening-parenthesis token, (or
1162 * perhaps preceeding SPACE tokens). Upon successful return *last will
1163 * be the last consumed node, (corresponding to the closing right
1168 * FUNCTION_STATUS_SUCCESS:
1170 * Successfully parsed a set of function arguments.
1172 * FUNCTION_NOT_A_FUNCTION:
1174 * Macro name not followed by a '('. This is not an error, but
1175 * simply that the macro name should be treated as a non-macro.
1177 * FUNCTION_UNBALANCED_PARENTHESES
1179 * Macro name is not followed by a balanced set of parentheses.
1181 static function_status_t
1182 _arguments_parse (argument_list_t *arguments,
1184 token_node_t **last)
1186 token_list_t *argument;
1191 /* Ignore whitespace before first parenthesis. */
1192 while (node && node->token->type == SPACE)
1195 if (node == NULL || node->token->type != '(')
1196 return FUNCTION_NOT_A_FUNCTION;
1200 argument = _token_list_create (arguments);
1201 _argument_list_append (arguments, argument);
1203 for (paren_count = 1; node; node = node->next) {
1204 if (node->token->type == '(')
1208 else if (node->token->type == ')')
1211 if (paren_count == 0)
1215 if (node->token->type == ',' &&
1218 _token_list_trim_trailing_space (argument);
1219 argument = _token_list_create (arguments);
1220 _argument_list_append (arguments, argument);
1223 if (argument->head == NULL) {
1224 /* Don't treat initial whitespace as
1225 * part of the arguement. */
1226 if (node->token->type == SPACE)
1229 _token_list_append (argument, node->token);
1234 return FUNCTION_UNBALANCED_PARENTHESES;
1238 return FUNCTION_STATUS_SUCCESS;
1241 static token_list_t *
1242 _token_list_create_with_one_space (void *ctx)
1247 list = _token_list_create (ctx);
1248 space = _token_create_ival (list, SPACE, SPACE);
1249 _token_list_append (list, space);
1255 _glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1257 token_list_t *expanded;
1260 expanded = _token_list_create (parser);
1261 token = _token_create_ival (parser, type, type);
1262 _token_list_append (expanded, token);
1263 _glcpp_parser_expand_token_list (parser, list);
1264 _token_list_append_list (expanded, list);
1265 glcpp_parser_lex_from (parser, expanded);
1268 /* This is a helper function that's essentially part of the
1269 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1270 * except for by that function.
1272 * Returns NULL if node is a simple token with no expansion, (that is,
1273 * although 'node' corresponds to an identifier defined as a
1274 * function-like macro, it is not followed with a parenthesized
1277 * Compute the complete expansion of node (which is a function-like
1278 * macro) and subsequent nodes which are arguments.
1280 * Returns the token list that results from the expansion and sets
1281 * *last to the last node in the list that was consumed by the
1282 * expansion. Specifically, *last will be set as follows: as the
1283 * token of the closing right parenthesis.
1285 static token_list_t *
1286 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1288 token_node_t **last)
1292 const char *identifier;
1293 argument_list_t *arguments;
1294 function_status_t status;
1295 token_list_t *substituted;
1296 int parameter_index;
1298 identifier = node->token->value.str;
1300 macro = hash_table_find (parser->defines, identifier);
1302 assert (macro->is_function);
1304 arguments = _argument_list_create (parser);
1305 status = _arguments_parse (arguments, node, last);
1308 case FUNCTION_STATUS_SUCCESS:
1310 case FUNCTION_NOT_A_FUNCTION:
1312 case FUNCTION_UNBALANCED_PARENTHESES:
1313 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1317 /* Replace a macro defined as empty with a SPACE token. */
1318 if (macro->replacements == NULL) {
1319 talloc_free (arguments);
1320 return _token_list_create_with_one_space (parser);
1323 if (! ((_argument_list_length (arguments) ==
1324 _string_list_length (macro->parameters)) ||
1325 (_string_list_length (macro->parameters) == 0 &&
1326 _argument_list_length (arguments) == 1 &&
1327 arguments->head->argument->head == NULL)))
1329 glcpp_error (&node->token->location, parser,
1330 "Error: macro %s invoked with %d arguments (expected %d)\n",
1332 _argument_list_length (arguments),
1333 _string_list_length (macro->parameters));
1337 /* Perform argument substitution on the replacement list. */
1338 substituted = _token_list_create (arguments);
1340 for (node = macro->replacements->head; node; node = node->next)
1342 if (node->token->type == IDENTIFIER &&
1343 _string_list_contains (macro->parameters,
1344 node->token->value.str,
1347 token_list_t *argument;
1348 argument = _argument_list_member_at (arguments,
1350 /* Before substituting, we expand the argument
1351 * tokens, or append a placeholder token for
1352 * an empty argument. */
1353 if (argument->head) {
1354 token_list_t *expanded_argument;
1355 expanded_argument = _token_list_copy (parser,
1357 _glcpp_parser_expand_token_list (parser,
1359 _token_list_append_list (substituted,
1364 new_token = _token_create_ival (substituted,
1367 _token_list_append (substituted, new_token);
1370 _token_list_append (substituted, node->token);
1374 /* After argument substitution, and before further expansion
1375 * below, implement token pasting. */
1377 _token_list_trim_trailing_space (substituted);
1379 node = substituted->head;
1382 token_node_t *next_non_space;
1384 /* Look ahead for a PASTE token, skipping space. */
1385 next_non_space = node->next;
1386 while (next_non_space && next_non_space->token->type == SPACE)
1387 next_non_space = next_non_space->next;
1389 if (next_non_space == NULL)
1392 if (next_non_space->token->type != PASTE) {
1393 node = next_non_space;
1397 /* Now find the next non-space token after the PASTE. */
1398 next_non_space = next_non_space->next;
1399 while (next_non_space && next_non_space->token->type == SPACE)
1400 next_non_space = next_non_space->next;
1402 if (next_non_space == NULL) {
1403 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1407 node->token = _token_paste (parser, node->token, next_non_space->token);
1408 node->next = next_non_space->next;
1409 if (next_non_space == substituted->tail)
1410 substituted->tail = node;
1415 substituted->non_space_tail = substituted->tail;
1420 /* Compute the complete expansion of node, (and subsequent nodes after
1421 * 'node' in the case that 'node' is a function-like macro and
1422 * subsequent nodes are arguments).
1424 * Returns NULL if node is a simple token with no expansion.
1426 * Otherwise, returns the token list that results from the expansion
1427 * and sets *last to the last node in the list that was consumed by
1428 * the expansion. Specifically, *last will be set as follows:
1430 * As 'node' in the case of object-like macro expansion.
1432 * As the token of the closing right parenthesis in the case of
1433 * function-like macro expansion.
1435 static token_list_t *
1436 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1438 token_node_t **last)
1440 token_t *token = node->token;
1441 const char *identifier;
1444 /* We only expand identifiers */
1445 if (token->type != IDENTIFIER) {
1446 /* We change any COMMA into a COMMA_FINAL to prevent
1447 * it being mistaken for an argument separator
1449 if (token->type == ',') {
1450 token->type = COMMA_FINAL;
1451 token->value.ival = COMMA_FINAL;
1457 /* Look up this identifier in the hash table. */
1458 identifier = token->value.str;
1459 macro = hash_table_find (parser->defines, identifier);
1461 /* Not a macro, so no expansion needed. */
1465 /* Finally, don't expand this macro if we're already actively
1466 * expanding it, (to avoid infinite recursion). */
1467 if (_active_list_contains (parser->active, identifier)) {
1468 /* We change the token type here from IDENTIFIER to
1469 * OTHER to prevent any future expansion of this
1470 * unexpanded token. */
1472 token_list_t *expansion;
1475 str = talloc_strdup (parser, token->value.str);
1476 final = _token_create_str (parser, OTHER, str);
1477 expansion = _token_list_create (parser);
1478 _token_list_append (expansion, final);
1483 if (! macro->is_function)
1487 /* Replace a macro defined as empty with a SPACE token. */
1488 if (macro->replacements == NULL)
1489 return _token_list_create_with_one_space (parser);
1491 return _token_list_copy (parser, macro->replacements);
1494 return _glcpp_parser_expand_function (parser, node, last);
1497 /* Push a new identifier onto the active list, returning the new list.
1499 * Here, 'marker' is the token node that appears in the list after the
1500 * expansion of 'identifier'. That is, when the list iterator begins
1501 * examinging 'marker', then it is time to pop this node from the
1505 _active_list_push (active_list_t *list,
1506 const char *identifier,
1507 token_node_t *marker)
1509 active_list_t *node;
1511 node = talloc (list, active_list_t);
1512 node->identifier = talloc_strdup (node, identifier);
1513 node->marker = marker;
1520 _active_list_pop (active_list_t *list)
1522 active_list_t *node = list;
1534 _active_list_contains (active_list_t *list, const char *identifier)
1536 active_list_t *node;
1541 for (node = list; node; node = node->next)
1542 if (strcmp (node->identifier, identifier) == 0)
1548 /* Walk over the token list replacing nodes with their expansion.
1549 * Whenever nodes are expanded the walking will walk over the new
1550 * nodes, continuing to expand as necessary. The results are placed in
1554 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1557 token_node_t *node_prev;
1558 token_node_t *node, *last = NULL;
1559 token_list_t *expansion;
1564 _token_list_trim_trailing_space (list);
1571 while (parser->active && parser->active->marker == node)
1572 parser->active = _active_list_pop (parser->active);
1574 /* Find the expansion for node, which will replace all
1575 * nodes from node to last, inclusive. */
1576 expansion = _glcpp_parser_expand_node (parser, node, &last);
1580 for (n = node; n != last->next; n = n->next)
1581 while (parser->active &&
1582 parser->active->marker == n)
1584 parser->active = _active_list_pop (parser->active);
1587 parser->active = _active_list_push (parser->active,
1588 node->token->value.str,
1591 /* Splice expansion into list, supporting a
1592 * simple deletion if the expansion is
1594 if (expansion->head) {
1596 node_prev->next = expansion->head;
1598 list->head = expansion->head;
1599 expansion->tail->next = last->next;
1600 if (last == list->tail)
1601 list->tail = expansion->tail;
1604 node_prev->next = last->next;
1606 list->head = last->next;
1607 if (last == list->tail)
1613 node = node_prev ? node_prev->next : list->head;
1616 while (parser->active)
1617 parser->active = _active_list_pop (parser->active);
1619 list->non_space_tail = list->tail;
1623 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1629 _glcpp_parser_expand_token_list (parser, list);
1631 _token_list_trim_trailing_space (list);
1633 _token_list_print (parser, list);
1637 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1638 const char *identifier)
1640 /* According to the GLSL specification, macro names starting with "__"
1641 * or "GL_" are reserved for future use. So, don't allow them.
1643 if (strncmp(identifier, "__", 2) == 0) {
1644 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1646 if (strncmp(identifier, "GL_", 3) == 0) {
1647 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1652 _macro_equal (macro_t *a, macro_t *b)
1654 if (a->is_function != b->is_function)
1657 if (a->is_function) {
1658 if (! _string_list_equal (a->parameters, b->parameters))
1662 return _token_list_equal_ignoring_space (a->replacements,
1667 _define_object_macro (glcpp_parser_t *parser,
1669 const char *identifier,
1670 token_list_t *replacements)
1672 macro_t *macro, *previous;
1675 _check_for_reserved_macro_name(parser, loc, identifier);
1677 macro = talloc (parser, macro_t);
1679 macro->is_function = 0;
1680 macro->parameters = NULL;
1681 macro->identifier = talloc_strdup (macro, identifier);
1682 macro->replacements = talloc_steal (macro, replacements);
1684 previous = hash_table_find (parser->defines, identifier);
1686 if (_macro_equal (macro, previous)) {
1687 talloc_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 = talloc (parser, macro_t);
1710 macro->is_function = 1;
1711 macro->parameters = talloc_steal (macro, parameters);
1712 macro->identifier = talloc_strdup (macro, identifier);
1713 macro->replacements = talloc_steal (macro, replacements);
1715 previous = hash_table_find (parser->defines, identifier);
1717 if (_macro_equal (macro, previous)) {
1718 talloc_free (macro);
1721 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1725 hash_table_insert (parser->defines, macro, identifier);
1729 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1734 if (parser->lex_from_list == NULL) {
1735 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1737 /* XXX: This ugly block of code exists for the sole
1738 * purpose of converting a NEWLINE token into a SPACE
1739 * token, but only in the case where we have seen a
1740 * function-like macro name, but have not yet seen its
1741 * closing parenthesis.
1743 * There's perhaps a more compact way to do this with
1744 * mid-rule actions in the grammar.
1746 * I'm definitely not pleased with the complexity of
1749 if (parser->newline_as_space)
1752 parser->paren_count++;
1753 } else if (ret == ')') {
1754 parser->paren_count--;
1755 if (parser->paren_count == 0)
1756 parser->newline_as_space = 0;
1757 } else if (ret == NEWLINE) {
1759 } else if (ret != SPACE) {
1760 if (parser->paren_count == 0)
1761 parser->newline_as_space = 0;
1764 else if (parser->in_control_line)
1767 parser->in_control_line = 0;
1769 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1770 ret == HASH_UNDEF || ret == HASH_IF ||
1771 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1772 ret == HASH_ELIF || ret == HASH_ELSE ||
1773 ret == HASH_ENDIF || ret == HASH)
1775 parser->in_control_line = 1;
1777 else if (ret == IDENTIFIER)
1780 macro = hash_table_find (parser->defines,
1782 if (macro && macro->is_function) {
1783 parser->newline_as_space = 1;
1784 parser->paren_count = 0;
1791 node = parser->lex_from_node;
1794 talloc_free (parser->lex_from_list);
1795 parser->lex_from_list = NULL;
1799 *yylval = node->token->value;
1800 ret = node->token->type;
1802 parser->lex_from_node = node->next;
1808 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1812 assert (parser->lex_from_list == NULL);
1814 /* Copy list, eliminating any space tokens. */
1815 parser->lex_from_list = _token_list_create (parser);
1817 for (node = list->head; node; node = node->next) {
1818 if (node->token->type == SPACE)
1820 _token_list_append (parser->lex_from_list, node->token);
1825 parser->lex_from_node = parser->lex_from_list->head;
1827 /* It's possible the list consisted of nothing but whitespace. */
1828 if (parser->lex_from_node == NULL) {
1829 talloc_free (parser->lex_from_list);
1830 parser->lex_from_list = NULL;
1835 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1838 skip_type_t current = SKIP_NO_SKIP;
1841 if (parser->skip_stack)
1842 current = parser->skip_stack->type;
1844 node = talloc (parser, skip_node_t);
1847 if (current == SKIP_NO_SKIP) {
1849 node->type = SKIP_NO_SKIP;
1851 node->type = SKIP_TO_ELSE;
1853 node->type = SKIP_TO_ENDIF;
1856 node->next = parser->skip_stack;
1857 parser->skip_stack = node;
1861 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1862 const char *type, int condition)
1864 if (parser->skip_stack == NULL) {
1865 glcpp_error (loc, parser, "%s without #if\n", type);
1869 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1871 parser->skip_stack->type = SKIP_NO_SKIP;
1873 parser->skip_stack->type = SKIP_TO_ENDIF;
1878 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1882 if (parser->skip_stack == NULL) {
1883 glcpp_error (loc, parser, "#endif without #if\n");
1887 node = parser->skip_stack;
1888 parser->skip_stack = node->next;