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);
89 _token_list_append (token_list_t *list, token_t *token);
92 _token_list_append_list (token_list_t *list, token_list_t *tail);
95 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
98 _parser_active_list_push (glcpp_parser_t *parser,
99 const char *identifier,
100 token_node_t *marker);
103 _parser_active_list_pop (glcpp_parser_t *parser);
106 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier);
108 /* Expand list, and begin lexing from the result (after first
109 * prefixing a token of type 'head_token_type').
112 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
116 /* Perform macro expansion in-place on the given list. */
118 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
122 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
126 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
130 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
131 const char *type, int condition);
134 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
136 #define yylex glcpp_parser_lex
139 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
142 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
145 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
161 %parse-param {glcpp_parser_t *parser}
162 %lex-param {glcpp_parser_t *parser}
165 %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
167 %type <ival> expression INTEGER operator SPACE integer_constant
168 %type <str> IDENTIFIER INTEGER_STRING OTHER
169 %type <string_list> identifier_list
170 %type <token> preprocessing_token conditional_token
171 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
177 %left EQUAL NOT_EQUAL
178 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
179 %left LEFT_SHIFT RIGHT_SHIFT
193 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
196 _glcpp_parser_print_expanded_token_list (parser, $1);
197 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
205 IF_EXPANDED expression NEWLINE {
206 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
208 | ELIF_EXPANDED expression NEWLINE {
209 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
214 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
215 _define_object_macro (parser, & @2, $2, $3);
217 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
218 _define_function_macro (parser, & @2, $2, NULL, $5);
220 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
221 _define_function_macro (parser, & @2, $2, $4, $6);
223 | HASH_UNDEF IDENTIFIER NEWLINE {
224 macro_t *macro = hash_table_find (parser->defines, $2);
226 hash_table_remove (parser->defines, $2);
231 | HASH_IF conditional_tokens NEWLINE {
232 /* Be careful to only evaluate the 'if' expression if
233 * we are not skipping. When we are skipping, we
234 * simply push a new 0-valued 'if' onto the skip
237 * This avoids generating diagnostics for invalid
238 * expressions that are being skipped. */
239 if (parser->skip_stack == NULL ||
240 parser->skip_stack->type == SKIP_NO_SKIP)
242 _glcpp_parser_expand_and_lex_from (parser,
247 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
248 parser->skip_stack->type = SKIP_TO_ENDIF;
252 /* #if without an expression is only an error if we
253 * are not skipping */
254 if (parser->skip_stack == NULL ||
255 parser->skip_stack->type == SKIP_NO_SKIP)
257 glcpp_error(& @1, parser, "#if with no expression");
259 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
261 | HASH_IFDEF IDENTIFIER junk NEWLINE {
262 macro_t *macro = hash_table_find (parser->defines, $2);
264 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
266 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
267 macro_t *macro = hash_table_find (parser->defines, $2);
269 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
271 | HASH_ELIF conditional_tokens NEWLINE {
272 /* Be careful to only evaluate the 'elif' expression
273 * if we are not skipping. When we are skipping, we
274 * simply change to a 0-valued 'elif' on the skip
277 * This avoids generating diagnostics for invalid
278 * expressions that are being skipped. */
279 if (parser->skip_stack &&
280 parser->skip_stack->type == SKIP_TO_ELSE)
282 _glcpp_parser_expand_and_lex_from (parser,
287 _glcpp_parser_skip_stack_change_if (parser, & @1,
291 | HASH_ELIF NEWLINE {
292 /* #elif without an expression is an error unless we
294 if (parser->skip_stack &&
295 parser->skip_stack->type == SKIP_TO_ELSE)
297 glcpp_error(& @1, parser, "#elif with no expression");
301 _glcpp_parser_skip_stack_change_if (parser, & @1,
303 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
306 | HASH_ELSE NEWLINE {
307 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
309 | HASH_ENDIF NEWLINE {
310 _glcpp_parser_skip_stack_pop (parser, & @1);
312 | HASH_VERSION integer_constant NEWLINE {
313 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
315 hash_table_remove (parser->defines, "__VERSION__");
318 add_builtin_define (parser, "__VERSION__", $2);
321 add_builtin_define (parser, "GL_ES", 1);
323 /* Currently, all ES2 implementations support highp in the
324 * fragment shader, so we always define this macro in ES2.
325 * If we ever get a driver that doesn't support highp, we'll
326 * need to add a flag to the gl_context and check that here.
328 if ($2 >= 130 || $2 == 100)
329 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
331 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#version %" PRIiMAX, $2);
338 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
339 $$ = strtoll ($1 + 2, NULL, 16);
340 } else if ($1[0] == '0') {
341 $$ = strtoll ($1, NULL, 8);
343 $$ = strtoll ($1, NULL, 10);
355 | expression OR expression {
358 | expression AND expression {
361 | expression '|' expression {
364 | expression '^' expression {
367 | expression '&' expression {
370 | expression NOT_EQUAL expression {
373 | expression EQUAL expression {
376 | expression GREATER_OR_EQUAL expression {
379 | expression LESS_OR_EQUAL expression {
382 | expression '>' expression {
385 | expression '<' expression {
388 | expression RIGHT_SHIFT expression {
391 | expression LEFT_SHIFT expression {
394 | expression '-' expression {
397 | expression '+' expression {
400 | expression '%' expression {
402 yyerror (& @1, parser,
403 "zero modulus in preprocessor directive");
408 | expression '/' expression {
410 yyerror (& @1, parser,
411 "division by 0 in preprocessor directive");
416 | expression '*' expression {
419 | '!' expression %prec UNARY {
422 | '~' expression %prec UNARY {
425 | '-' expression %prec UNARY {
428 | '+' expression %prec UNARY {
431 | '(' expression ')' {
438 $$ = _string_list_create (parser);
439 _string_list_append_item ($$, $1);
440 ralloc_steal ($$, $1);
442 | identifier_list ',' IDENTIFIER {
444 _string_list_append_item ($$, $3);
445 ralloc_steal ($$, $3);
450 NEWLINE { $$ = NULL; }
456 yyerror (& @1, parser, "Invalid tokens after #");
461 /* empty */ { $$ = NULL; }
468 glcpp_warning(&@1, parser, "extra tokens at end of directive");
473 /* Handle "defined" operator */
475 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
476 $$ = _token_create_ival (parser, INTEGER, v);
478 | DEFINED '(' IDENTIFIER ')' {
479 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
480 $$ = _token_create_ival (parser, INTEGER, v);
482 | preprocessing_token
486 /* Exactly the same as pp_tokens, but using conditional_token */
488 $$ = _token_list_create (parser);
489 _token_list_append ($$, $1);
491 | conditional_tokens conditional_token {
493 _token_list_append ($$, $2);
498 preprocessing_token {
499 parser->space_tokens = 1;
500 $$ = _token_list_create (parser);
501 _token_list_append ($$, $1);
503 | pp_tokens preprocessing_token {
505 _token_list_append ($$, $2);
511 $$ = _token_create_str (parser, IDENTIFIER, $1);
512 $$->location = yylloc;
515 $$ = _token_create_str (parser, INTEGER_STRING, $1);
516 $$->location = yylloc;
519 $$ = _token_create_ival (parser, $1, $1);
520 $$->location = yylloc;
523 $$ = _token_create_str (parser, OTHER, $1);
524 $$->location = yylloc;
527 $$ = _token_create_ival (parser, SPACE, SPACE);
528 $$->location = yylloc;
548 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
549 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
552 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
553 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
554 | EQUAL { $$ = EQUAL; }
555 | NOT_EQUAL { $$ = NOT_EQUAL; }
563 | PASTE { $$ = PASTE; }
569 _string_list_create (void *ctx)
573 list = ralloc (ctx, string_list_t);
581 _string_list_append_item (string_list_t *list, const char *str)
585 node = ralloc (list, string_node_t);
586 node->str = ralloc_strdup (node, str);
590 if (list->head == NULL) {
593 list->tail->next = node;
600 _string_list_contains (string_list_t *list, const char *member, int *index)
608 for (i = 0, node = list->head; node; i++, node = node->next) {
609 if (strcmp (node->str, member) == 0) {
620 _string_list_length (string_list_t *list)
628 for (node = list->head; node; node = node->next)
635 _string_list_equal (string_list_t *a, string_list_t *b)
637 string_node_t *node_a, *node_b;
639 if (a == NULL && b == NULL)
642 if (a == NULL || b == NULL)
645 for (node_a = a->head, node_b = b->head;
647 node_a = node_a->next, node_b = node_b->next)
649 if (strcmp (node_a->str, node_b->str))
653 /* Catch the case of lists being different lengths, (which
654 * would cause the loop above to terminate after the shorter
656 return node_a == node_b;
660 _argument_list_create (void *ctx)
662 argument_list_t *list;
664 list = ralloc (ctx, argument_list_t);
672 _argument_list_append (argument_list_t *list, token_list_t *argument)
674 argument_node_t *node;
676 node = ralloc (list, argument_node_t);
677 node->argument = argument;
681 if (list->head == NULL) {
684 list->tail->next = node;
691 _argument_list_length (argument_list_t *list)
694 argument_node_t *node;
699 for (node = list->head; node; node = node->next)
706 _argument_list_member_at (argument_list_t *list, int index)
708 argument_node_t *node;
715 for (i = 0; i < index; i++) {
722 return node->argument;
727 /* Note: This function ralloc_steal()s the str pointer. */
729 _token_create_str (void *ctx, int type, char *str)
733 token = ralloc (ctx, token_t);
735 token->value.str = str;
737 ralloc_steal (token, str);
743 _token_create_ival (void *ctx, int type, int ival)
747 token = ralloc (ctx, token_t);
749 token->value.ival = ival;
755 _token_list_create (void *ctx)
759 list = ralloc (ctx, token_list_t);
762 list->non_space_tail = NULL;
768 _token_list_append (token_list_t *list, token_t *token)
772 node = ralloc (list, token_node_t);
776 if (list->head == NULL) {
779 list->tail->next = node;
783 if (token->type != SPACE)
784 list->non_space_tail = node;
788 _token_list_append_list (token_list_t *list, token_list_t *tail)
790 if (tail == NULL || tail->head == NULL)
793 if (list->head == NULL) {
794 list->head = tail->head;
796 list->tail->next = tail->head;
799 list->tail = tail->tail;
800 list->non_space_tail = tail->non_space_tail;
803 static token_list_t *
804 _token_list_copy (void *ctx, token_list_t *other)
812 copy = _token_list_create (ctx);
813 for (node = other->head; node; node = node->next) {
814 token_t *new_token = ralloc (copy, token_t);
815 *new_token = *node->token;
816 _token_list_append (copy, new_token);
823 _token_list_trim_trailing_space (token_list_t *list)
825 token_node_t *tail, *next;
827 if (list->non_space_tail) {
828 tail = list->non_space_tail->next;
829 list->non_space_tail->next = NULL;
830 list->tail = list->non_space_tail;
841 _token_list_is_empty_ignoring_space (token_list_t *l)
849 while (n != NULL && n->token->type == SPACE)
856 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
858 token_node_t *node_a, *node_b;
860 if (a == NULL || b == NULL) {
861 int a_empty = _token_list_is_empty_ignoring_space(a);
862 int b_empty = _token_list_is_empty_ignoring_space(b);
863 return a_empty == b_empty;
871 if (node_a == NULL && node_b == NULL)
874 if (node_a == NULL || node_b == NULL)
877 if (node_a->token->type == SPACE) {
878 node_a = node_a->next;
882 if (node_b->token->type == SPACE) {
883 node_b = node_b->next;
887 if (node_a->token->type != node_b->token->type)
890 switch (node_a->token->type) {
892 if (node_a->token->value.ival !=
893 node_b->token->value.ival)
901 if (strcmp (node_a->token->value.str,
902 node_b->token->value.str))
909 node_a = node_a->next;
910 node_b = node_b->next;
917 _token_print (char **out, size_t *len, token_t *token)
919 if (token->type < 256) {
920 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
924 switch (token->type) {
926 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
931 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
934 ralloc_asprintf_rewrite_tail (out, len, " ");
937 ralloc_asprintf_rewrite_tail (out, len, "<<");
940 ralloc_asprintf_rewrite_tail (out, len, ">>");
943 ralloc_asprintf_rewrite_tail (out, len, "<=");
945 case GREATER_OR_EQUAL:
946 ralloc_asprintf_rewrite_tail (out, len, ">=");
949 ralloc_asprintf_rewrite_tail (out, len, "==");
952 ralloc_asprintf_rewrite_tail (out, len, "!=");
955 ralloc_asprintf_rewrite_tail (out, len, "&&");
958 ralloc_asprintf_rewrite_tail (out, len, "||");
961 ralloc_asprintf_rewrite_tail (out, len, "##");
964 ralloc_asprintf_rewrite_tail (out, len, ",");
967 /* Nothing to print. */
970 assert(!"Error: Don't know how to print token.");
975 /* Return a new token (ralloc()ed off of 'token') formed by pasting
976 * 'token' and 'other'. Note that this function may return 'token' or
977 * 'other' directly rather than allocating anything new.
979 * Caution: Only very cursory error-checking is performed to see if
980 * the final result is a valid single token. */
982 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
984 token_t *combined = NULL;
986 /* Pasting a placeholder onto anything makes no change. */
987 if (other->type == PLACEHOLDER)
990 /* When 'token' is a placeholder, just return 'other'. */
991 if (token->type == PLACEHOLDER)
994 /* A very few single-character punctuators can be combined
995 * with another to form a multi-character punctuator. */
996 switch (token->type) {
998 if (other->type == '<')
999 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1000 else if (other->type == '=')
1001 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1004 if (other->type == '>')
1005 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1006 else if (other->type == '=')
1007 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1010 if (other->type == '=')
1011 combined = _token_create_ival (token, EQUAL, EQUAL);
1014 if (other->type == '=')
1015 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1018 if (other->type == '&')
1019 combined = _token_create_ival (token, AND, AND);
1022 if (other->type == '|')
1023 combined = _token_create_ival (token, OR, OR);
1027 if (combined != NULL) {
1028 /* Inherit the location from the first token */
1029 combined->location = token->location;
1033 /* Two string-valued tokens can usually just be mashed
1036 * XXX: This isn't actually legitimate. Several things here
1037 * should result in a diagnostic since the result cannot be a
1038 * valid, single pre-processing token. For example, pasting
1039 * "123" and "abc" is not legal, but we don't catch that
1041 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1042 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1046 str = ralloc_asprintf (token, "%s%s", token->value.str,
1048 combined = _token_create_str (token, token->type, str);
1049 combined->location = token->location;
1053 glcpp_error (&token->location, parser, "");
1054 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1055 _token_print (&parser->info_log, &parser->info_log_length, token);
1056 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1057 _token_print (&parser->info_log, &parser->info_log_length, other);
1058 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1064 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1071 for (node = list->head; node; node = node->next)
1072 _token_print (&parser->output, &parser->output_length, node->token);
1076 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1078 glcpp_error(locp, parser, "%s", error);
1081 static void add_builtin_define(glcpp_parser_t *parser,
1082 const char *name, int value)
1087 tok = _token_create_ival (parser, INTEGER, value);
1089 list = _token_list_create(parser);
1090 _token_list_append(list, tok);
1091 _define_object_macro(parser, NULL, name, list);
1095 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1097 glcpp_parser_t *parser;
1098 int language_version;
1100 parser = ralloc (NULL, glcpp_parser_t);
1102 glcpp_lex_init_extra (parser, &parser->scanner);
1103 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1104 hash_table_string_compare);
1105 parser->active = NULL;
1106 parser->lexing_if = 0;
1107 parser->space_tokens = 1;
1108 parser->newline_as_space = 0;
1109 parser->in_control_line = 0;
1110 parser->paren_count = 0;
1112 parser->skip_stack = NULL;
1114 parser->lex_from_list = NULL;
1115 parser->lex_from_node = NULL;
1117 parser->output = ralloc_strdup(parser, "");
1118 parser->output_length = 0;
1119 parser->info_log = ralloc_strdup(parser, "");
1120 parser->info_log_length = 0;
1123 /* Add pre-defined macros. */
1124 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1125 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1127 if (api == API_OPENGLES2)
1128 add_builtin_define(parser, "GL_ES", 1);
1130 if (extensions != NULL) {
1131 if (extensions->EXT_texture_array) {
1132 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1135 if (extensions->ARB_fragment_coord_conventions)
1136 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1139 if (extensions->ARB_explicit_attrib_location)
1140 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1142 if (extensions->ARB_shader_texture_lod)
1143 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
1145 if (extensions->ARB_draw_instanced)
1146 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
1148 if (extensions->ARB_conservative_depth) {
1149 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1150 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
1153 if (extensions->OES_EGL_image_external)
1154 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
1156 if (extensions->ARB_shader_bit_encoding)
1157 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
1160 language_version = 110;
1161 add_builtin_define(parser, "__VERSION__", language_version);
1167 glcpp_parser_parse (glcpp_parser_t *parser)
1169 return yyparse (parser);
1173 glcpp_parser_destroy (glcpp_parser_t *parser)
1175 glcpp_lex_destroy (parser->scanner);
1176 hash_table_dtor (parser->defines);
1177 ralloc_free (parser);
1180 typedef enum function_status
1182 FUNCTION_STATUS_SUCCESS,
1183 FUNCTION_NOT_A_FUNCTION,
1184 FUNCTION_UNBALANCED_PARENTHESES
1185 } function_status_t;
1187 /* Find a set of function-like macro arguments by looking for a
1188 * balanced set of parentheses.
1190 * When called, 'node' should be the opening-parenthesis token, (or
1191 * perhaps preceeding SPACE tokens). Upon successful return *last will
1192 * be the last consumed node, (corresponding to the closing right
1197 * FUNCTION_STATUS_SUCCESS:
1199 * Successfully parsed a set of function arguments.
1201 * FUNCTION_NOT_A_FUNCTION:
1203 * Macro name not followed by a '('. This is not an error, but
1204 * simply that the macro name should be treated as a non-macro.
1206 * FUNCTION_UNBALANCED_PARENTHESES
1208 * Macro name is not followed by a balanced set of parentheses.
1210 static function_status_t
1211 _arguments_parse (argument_list_t *arguments,
1213 token_node_t **last)
1215 token_list_t *argument;
1220 /* Ignore whitespace before first parenthesis. */
1221 while (node && node->token->type == SPACE)
1224 if (node == NULL || node->token->type != '(')
1225 return FUNCTION_NOT_A_FUNCTION;
1229 argument = _token_list_create (arguments);
1230 _argument_list_append (arguments, argument);
1232 for (paren_count = 1; node; node = node->next) {
1233 if (node->token->type == '(')
1237 else if (node->token->type == ')')
1240 if (paren_count == 0)
1244 if (node->token->type == ',' &&
1247 _token_list_trim_trailing_space (argument);
1248 argument = _token_list_create (arguments);
1249 _argument_list_append (arguments, argument);
1252 if (argument->head == NULL) {
1253 /* Don't treat initial whitespace as
1254 * part of the arguement. */
1255 if (node->token->type == SPACE)
1258 _token_list_append (argument, node->token);
1263 return FUNCTION_UNBALANCED_PARENTHESES;
1267 return FUNCTION_STATUS_SUCCESS;
1270 static token_list_t *
1271 _token_list_create_with_one_space (void *ctx)
1276 list = _token_list_create (ctx);
1277 space = _token_create_ival (list, SPACE, SPACE);
1278 _token_list_append (list, space);
1283 /* Perform macro expansion on 'list', placing the resulting tokens
1284 * into a new list which is initialized with a first token of type
1285 * 'head_token_type'. Then begin lexing from the resulting list,
1286 * (return to the current lexing source when this list is exhausted).
1289 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1290 int head_token_type,
1293 token_list_t *expanded;
1296 expanded = _token_list_create (parser);
1297 token = _token_create_ival (parser, head_token_type, head_token_type);
1298 _token_list_append (expanded, token);
1299 _glcpp_parser_expand_token_list (parser, list);
1300 _token_list_append_list (expanded, list);
1301 glcpp_parser_lex_from (parser, expanded);
1305 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1312 token_node_t *next_non_space;
1314 /* Look ahead for a PASTE token, skipping space. */
1315 next_non_space = node->next;
1316 while (next_non_space && next_non_space->token->type == SPACE)
1317 next_non_space = next_non_space->next;
1319 if (next_non_space == NULL)
1322 if (next_non_space->token->type != PASTE) {
1323 node = next_non_space;
1327 /* Now find the next non-space token after the PASTE. */
1328 next_non_space = next_non_space->next;
1329 while (next_non_space && next_non_space->token->type == SPACE)
1330 next_non_space = next_non_space->next;
1332 if (next_non_space == NULL) {
1333 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1337 node->token = _token_paste (parser, node->token, next_non_space->token);
1338 node->next = next_non_space->next;
1339 if (next_non_space == list->tail)
1343 list->non_space_tail = list->tail;
1346 /* This is a helper function that's essentially part of the
1347 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1348 * except for by that function.
1350 * Returns NULL if node is a simple token with no expansion, (that is,
1351 * although 'node' corresponds to an identifier defined as a
1352 * function-like macro, it is not followed with a parenthesized
1355 * Compute the complete expansion of node (which is a function-like
1356 * macro) and subsequent nodes which are arguments.
1358 * Returns the token list that results from the expansion and sets
1359 * *last to the last node in the list that was consumed by the
1360 * expansion. Specifically, *last will be set as follows: as the
1361 * token of the closing right parenthesis.
1363 static token_list_t *
1364 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1366 token_node_t **last)
1370 const char *identifier;
1371 argument_list_t *arguments;
1372 function_status_t status;
1373 token_list_t *substituted;
1374 int parameter_index;
1376 identifier = node->token->value.str;
1378 macro = hash_table_find (parser->defines, identifier);
1380 assert (macro->is_function);
1382 arguments = _argument_list_create (parser);
1383 status = _arguments_parse (arguments, node, last);
1386 case FUNCTION_STATUS_SUCCESS:
1388 case FUNCTION_NOT_A_FUNCTION:
1390 case FUNCTION_UNBALANCED_PARENTHESES:
1391 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1395 /* Replace a macro defined as empty with a SPACE token. */
1396 if (macro->replacements == NULL) {
1397 ralloc_free (arguments);
1398 return _token_list_create_with_one_space (parser);
1401 if (! ((_argument_list_length (arguments) ==
1402 _string_list_length (macro->parameters)) ||
1403 (_string_list_length (macro->parameters) == 0 &&
1404 _argument_list_length (arguments) == 1 &&
1405 arguments->head->argument->head == NULL)))
1407 glcpp_error (&node->token->location, parser,
1408 "Error: macro %s invoked with %d arguments (expected %d)\n",
1410 _argument_list_length (arguments),
1411 _string_list_length (macro->parameters));
1415 /* Perform argument substitution on the replacement list. */
1416 substituted = _token_list_create (arguments);
1418 for (node = macro->replacements->head; node; node = node->next)
1420 if (node->token->type == IDENTIFIER &&
1421 _string_list_contains (macro->parameters,
1422 node->token->value.str,
1425 token_list_t *argument;
1426 argument = _argument_list_member_at (arguments,
1428 /* Before substituting, we expand the argument
1429 * tokens, or append a placeholder token for
1430 * an empty argument. */
1431 if (argument->head) {
1432 token_list_t *expanded_argument;
1433 expanded_argument = _token_list_copy (parser,
1435 _glcpp_parser_expand_token_list (parser,
1437 _token_list_append_list (substituted,
1442 new_token = _token_create_ival (substituted,
1445 _token_list_append (substituted, new_token);
1448 _token_list_append (substituted, node->token);
1452 /* After argument substitution, and before further expansion
1453 * below, implement token pasting. */
1455 _token_list_trim_trailing_space (substituted);
1457 _glcpp_parser_apply_pastes (parser, substituted);
1462 /* Compute the complete expansion of node, (and subsequent nodes after
1463 * 'node' in the case that 'node' is a function-like macro and
1464 * subsequent nodes are arguments).
1466 * Returns NULL if node is a simple token with no expansion.
1468 * Otherwise, returns the token list that results from the expansion
1469 * and sets *last to the last node in the list that was consumed by
1470 * the expansion. Specifically, *last will be set as follows:
1472 * As 'node' in the case of object-like macro expansion.
1474 * As the token of the closing right parenthesis in the case of
1475 * function-like macro expansion.
1477 static token_list_t *
1478 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1480 token_node_t **last)
1482 token_t *token = node->token;
1483 const char *identifier;
1486 /* We only expand identifiers */
1487 if (token->type != IDENTIFIER) {
1488 /* We change any COMMA into a COMMA_FINAL to prevent
1489 * it being mistaken for an argument separator
1491 if (token->type == ',') {
1492 token->type = COMMA_FINAL;
1493 token->value.ival = COMMA_FINAL;
1499 /* Look up this identifier in the hash table. */
1500 identifier = token->value.str;
1501 macro = hash_table_find (parser->defines, identifier);
1503 /* Not a macro, so no expansion needed. */
1507 /* Finally, don't expand this macro if we're already actively
1508 * expanding it, (to avoid infinite recursion). */
1509 if (_parser_active_list_contains (parser, identifier)) {
1510 /* We change the token type here from IDENTIFIER to
1511 * OTHER to prevent any future expansion of this
1512 * unexpanded token. */
1514 token_list_t *expansion;
1517 str = ralloc_strdup (parser, token->value.str);
1518 final = _token_create_str (parser, OTHER, str);
1519 expansion = _token_list_create (parser);
1520 _token_list_append (expansion, final);
1525 if (! macro->is_function)
1527 token_list_t *replacement;
1530 /* Replace a macro defined as empty with a SPACE token. */
1531 if (macro->replacements == NULL)
1532 return _token_list_create_with_one_space (parser);
1534 replacement = _token_list_copy (parser, macro->replacements);
1535 _glcpp_parser_apply_pastes (parser, replacement);
1539 return _glcpp_parser_expand_function (parser, node, last);
1542 /* Push a new identifier onto the parser's active list.
1544 * Here, 'marker' is the token node that appears in the list after the
1545 * expansion of 'identifier'. That is, when the list iterator begins
1546 * examining 'marker', then it is time to pop this node from the
1550 _parser_active_list_push (glcpp_parser_t *parser,
1551 const char *identifier,
1552 token_node_t *marker)
1554 active_list_t *node;
1556 node = ralloc (parser->active, active_list_t);
1557 node->identifier = ralloc_strdup (node, identifier);
1558 node->marker = marker;
1559 node->next = parser->active;
1561 parser->active = node;
1565 _parser_active_list_pop (glcpp_parser_t *parser)
1567 active_list_t *node = parser->active;
1570 parser->active = NULL;
1574 node = parser->active->next;
1575 ralloc_free (parser->active);
1577 parser->active = node;
1581 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1583 active_list_t *node;
1585 if (parser->active == NULL)
1588 for (node = parser->active; node; node = node->next)
1589 if (strcmp (node->identifier, identifier) == 0)
1595 /* Walk over the token list replacing nodes with their expansion.
1596 * Whenever nodes are expanded the walking will walk over the new
1597 * nodes, continuing to expand as necessary. The results are placed in
1601 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1604 token_node_t *node_prev;
1605 token_node_t *node, *last = NULL;
1606 token_list_t *expansion;
1607 active_list_t *active_initial = parser->active;
1612 _token_list_trim_trailing_space (list);
1619 while (parser->active && parser->active->marker == node)
1620 _parser_active_list_pop (parser);
1622 expansion = _glcpp_parser_expand_node (parser, node, &last);
1626 for (n = node; n != last->next; n = n->next)
1627 while (parser->active &&
1628 parser->active->marker == n)
1630 _parser_active_list_pop (parser);
1633 _parser_active_list_push (parser,
1634 node->token->value.str,
1637 /* Splice expansion into list, supporting a
1638 * simple deletion if the expansion is
1640 if (expansion->head) {
1642 node_prev->next = expansion->head;
1644 list->head = expansion->head;
1645 expansion->tail->next = last->next;
1646 if (last == list->tail)
1647 list->tail = expansion->tail;
1650 node_prev->next = last->next;
1652 list->head = last->next;
1653 if (last == list->tail)
1659 node = node_prev ? node_prev->next : list->head;
1662 /* Remove any lingering effects of this invocation on the
1663 * active list. That is, pop until the list looks like it did
1664 * at the beginning of this function. */
1665 while (parser->active && parser->active != active_initial)
1666 _parser_active_list_pop (parser);
1668 list->non_space_tail = list->tail;
1672 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1678 _glcpp_parser_expand_token_list (parser, list);
1680 _token_list_trim_trailing_space (list);
1682 _token_list_print (parser, list);
1686 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1687 const char *identifier)
1689 /* According to the GLSL specification, macro names starting with "__"
1690 * or "GL_" are reserved for future use. So, don't allow them.
1692 if (strstr(identifier, "__")) {
1693 glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
1695 if (strncmp(identifier, "GL_", 3) == 0) {
1696 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1701 _macro_equal (macro_t *a, macro_t *b)
1703 if (a->is_function != b->is_function)
1706 if (a->is_function) {
1707 if (! _string_list_equal (a->parameters, b->parameters))
1711 return _token_list_equal_ignoring_space (a->replacements,
1716 _define_object_macro (glcpp_parser_t *parser,
1718 const char *identifier,
1719 token_list_t *replacements)
1721 macro_t *macro, *previous;
1724 _check_for_reserved_macro_name(parser, loc, identifier);
1726 macro = ralloc (parser, macro_t);
1728 macro->is_function = 0;
1729 macro->parameters = NULL;
1730 macro->identifier = ralloc_strdup (macro, identifier);
1731 macro->replacements = replacements;
1732 ralloc_steal (macro, replacements);
1734 previous = hash_table_find (parser->defines, identifier);
1736 if (_macro_equal (macro, previous)) {
1737 ralloc_free (macro);
1740 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1744 hash_table_insert (parser->defines, macro, identifier);
1748 _define_function_macro (glcpp_parser_t *parser,
1750 const char *identifier,
1751 string_list_t *parameters,
1752 token_list_t *replacements)
1754 macro_t *macro, *previous;
1756 _check_for_reserved_macro_name(parser, loc, identifier);
1758 macro = ralloc (parser, macro_t);
1759 ralloc_steal (macro, parameters);
1760 ralloc_steal (macro, replacements);
1762 macro->is_function = 1;
1763 macro->parameters = parameters;
1764 macro->identifier = ralloc_strdup (macro, identifier);
1765 macro->replacements = replacements;
1766 previous = hash_table_find (parser->defines, identifier);
1768 if (_macro_equal (macro, previous)) {
1769 ralloc_free (macro);
1772 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1776 hash_table_insert (parser->defines, macro, identifier);
1780 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1785 if (parser->lex_from_list == NULL) {
1786 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1788 /* XXX: This ugly block of code exists for the sole
1789 * purpose of converting a NEWLINE token into a SPACE
1790 * token, but only in the case where we have seen a
1791 * function-like macro name, but have not yet seen its
1792 * closing parenthesis.
1794 * There's perhaps a more compact way to do this with
1795 * mid-rule actions in the grammar.
1797 * I'm definitely not pleased with the complexity of
1800 if (parser->newline_as_space)
1803 parser->paren_count++;
1804 } else if (ret == ')') {
1805 parser->paren_count--;
1806 if (parser->paren_count == 0)
1807 parser->newline_as_space = 0;
1808 } else if (ret == NEWLINE) {
1810 } else if (ret != SPACE) {
1811 if (parser->paren_count == 0)
1812 parser->newline_as_space = 0;
1815 else if (parser->in_control_line)
1818 parser->in_control_line = 0;
1820 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1821 ret == HASH_UNDEF || ret == HASH_IF ||
1822 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1823 ret == HASH_ELIF || ret == HASH_ELSE ||
1824 ret == HASH_ENDIF || ret == HASH)
1826 parser->in_control_line = 1;
1828 else if (ret == IDENTIFIER)
1831 macro = hash_table_find (parser->defines,
1833 if (macro && macro->is_function) {
1834 parser->newline_as_space = 1;
1835 parser->paren_count = 0;
1842 node = parser->lex_from_node;
1845 ralloc_free (parser->lex_from_list);
1846 parser->lex_from_list = NULL;
1850 *yylval = node->token->value;
1851 ret = node->token->type;
1853 parser->lex_from_node = node->next;
1859 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1863 assert (parser->lex_from_list == NULL);
1865 /* Copy list, eliminating any space tokens. */
1866 parser->lex_from_list = _token_list_create (parser);
1868 for (node = list->head; node; node = node->next) {
1869 if (node->token->type == SPACE)
1871 _token_list_append (parser->lex_from_list, node->token);
1876 parser->lex_from_node = parser->lex_from_list->head;
1878 /* It's possible the list consisted of nothing but whitespace. */
1879 if (parser->lex_from_node == NULL) {
1880 ralloc_free (parser->lex_from_list);
1881 parser->lex_from_list = NULL;
1886 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1889 skip_type_t current = SKIP_NO_SKIP;
1892 if (parser->skip_stack)
1893 current = parser->skip_stack->type;
1895 node = ralloc (parser, skip_node_t);
1898 if (current == SKIP_NO_SKIP) {
1900 node->type = SKIP_NO_SKIP;
1902 node->type = SKIP_TO_ELSE;
1904 node->type = SKIP_TO_ENDIF;
1907 node->next = parser->skip_stack;
1908 parser->skip_stack = node;
1912 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1913 const char *type, int condition)
1915 if (parser->skip_stack == NULL) {
1916 glcpp_error (loc, parser, "%s without #if\n", type);
1920 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1922 parser->skip_stack->type = SKIP_NO_SKIP;
1924 parser->skip_stack->type = SKIP_TO_ENDIF;
1929 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1933 if (parser->skip_stack == NULL) {
1934 glcpp_error (loc, parser, "#endif without #if\n");
1938 node = parser->skip_stack;
1939 parser->skip_stack = node->next;