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);
137 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
140 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
143 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
159 %parse-param {glcpp_parser_t *parser}
160 %lex-param {glcpp_parser_t *parser}
163 %token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE FUNC_IDENTIFIER OBJ_IDENTIFIER HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_LINE HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE
165 %type <ival> expression INTEGER operator SPACE integer_constant
166 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
167 %type <string_list> identifier_list
168 %type <token> preprocessing_token conditional_token
169 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
175 %left EQUAL NOT_EQUAL
176 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
177 %left LEFT_SHIFT RIGHT_SHIFT
191 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
193 | HASH_LINE pp_tokens NEWLINE {
194 if (parser->skip_stack == NULL ||
195 parser->skip_stack->type == SKIP_NO_SKIP)
197 _glcpp_parser_expand_and_lex_from (parser,
202 _glcpp_parser_print_expanded_token_list (parser, $1);
203 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
211 IF_EXPANDED expression NEWLINE {
212 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
214 | ELIF_EXPANDED expression NEWLINE {
215 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
217 | LINE_EXPANDED integer_constant NEWLINE {
218 parser->has_new_line_number = 1;
219 parser->new_line_number = $2;
220 ralloc_asprintf_rewrite_tail (&parser->output,
221 &parser->output_length,
222 "#line %" PRIiMAX "\n",
225 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
226 parser->has_new_line_number = 1;
227 parser->new_line_number = $2;
228 parser->has_new_source_number = 1;
229 parser->new_source_number = $3;
230 ralloc_asprintf_rewrite_tail (&parser->output,
231 &parser->output_length,
232 "#line %" PRIiMAX " %" PRIiMAX "\n",
238 HASH_DEFINE OBJ_IDENTIFIER replacement_list NEWLINE {
239 _define_object_macro (parser, & @2, $2, $3);
241 | HASH_DEFINE FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
242 _define_function_macro (parser, & @2, $2, NULL, $5);
244 | HASH_DEFINE FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
245 _define_function_macro (parser, & @2, $2, $4, $6);
247 | HASH_UNDEF IDENTIFIER NEWLINE {
248 macro_t *macro = hash_table_find (parser->defines, $2);
250 hash_table_remove (parser->defines, $2);
255 | HASH_IF conditional_tokens NEWLINE {
256 /* Be careful to only evaluate the 'if' expression if
257 * we are not skipping. When we are skipping, we
258 * simply push a new 0-valued 'if' onto the skip
261 * This avoids generating diagnostics for invalid
262 * expressions that are being skipped. */
263 if (parser->skip_stack == NULL ||
264 parser->skip_stack->type == SKIP_NO_SKIP)
266 _glcpp_parser_expand_and_lex_from (parser,
271 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
272 parser->skip_stack->type = SKIP_TO_ENDIF;
276 /* #if without an expression is only an error if we
277 * are not skipping */
278 if (parser->skip_stack == NULL ||
279 parser->skip_stack->type == SKIP_NO_SKIP)
281 glcpp_error(& @1, parser, "#if with no expression");
283 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
285 | HASH_IFDEF IDENTIFIER junk NEWLINE {
286 macro_t *macro = hash_table_find (parser->defines, $2);
288 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
290 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
291 macro_t *macro = hash_table_find (parser->defines, $2);
293 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
295 | HASH_ELIF conditional_tokens NEWLINE {
296 /* Be careful to only evaluate the 'elif' expression
297 * if we are not skipping. When we are skipping, we
298 * simply change to a 0-valued 'elif' on the skip
301 * This avoids generating diagnostics for invalid
302 * expressions that are being skipped. */
303 if (parser->skip_stack &&
304 parser->skip_stack->type == SKIP_TO_ELSE)
306 _glcpp_parser_expand_and_lex_from (parser,
311 _glcpp_parser_skip_stack_change_if (parser, & @1,
315 | HASH_ELIF NEWLINE {
316 /* #elif without an expression is an error unless we
318 if (parser->skip_stack &&
319 parser->skip_stack->type == SKIP_TO_ELSE)
321 glcpp_error(& @1, parser, "#elif with no expression");
325 _glcpp_parser_skip_stack_change_if (parser, & @1,
327 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
331 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
334 _glcpp_parser_skip_stack_pop (parser, & @1);
336 | HASH_VERSION integer_constant NEWLINE {
337 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
339 hash_table_remove (parser->defines, "__VERSION__");
342 add_builtin_define (parser, "__VERSION__", $2);
345 add_builtin_define (parser, "GL_ES", 1);
347 /* Currently, all ES2 implementations support highp in the
348 * fragment shader, so we always define this macro in ES2.
349 * If we ever get a driver that doesn't support highp, we'll
350 * need to add a flag to the gl_context and check that here.
352 if ($2 >= 130 || $2 == 100)
353 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
355 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#version %" PRIiMAX, $2);
362 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
363 $$ = strtoll ($1 + 2, NULL, 16);
364 } else if ($1[0] == '0') {
365 $$ = strtoll ($1, NULL, 8);
367 $$ = strtoll ($1, NULL, 10);
379 | expression OR expression {
382 | expression AND expression {
385 | expression '|' expression {
388 | expression '^' expression {
391 | expression '&' expression {
394 | expression NOT_EQUAL expression {
397 | expression EQUAL expression {
400 | expression GREATER_OR_EQUAL expression {
403 | expression LESS_OR_EQUAL expression {
406 | expression '>' expression {
409 | expression '<' expression {
412 | expression RIGHT_SHIFT expression {
415 | expression LEFT_SHIFT expression {
418 | expression '-' expression {
421 | expression '+' expression {
424 | expression '%' expression {
426 yyerror (& @1, parser,
427 "zero modulus in preprocessor directive");
432 | expression '/' expression {
434 yyerror (& @1, parser,
435 "division by 0 in preprocessor directive");
440 | expression '*' expression {
443 | '!' expression %prec UNARY {
446 | '~' expression %prec UNARY {
449 | '-' expression %prec UNARY {
452 | '+' expression %prec UNARY {
455 | '(' expression ')' {
462 $$ = _string_list_create (parser);
463 _string_list_append_item ($$, $1);
464 ralloc_steal ($$, $1);
466 | identifier_list ',' IDENTIFIER {
468 _string_list_append_item ($$, $3);
469 ralloc_steal ($$, $3);
474 NEWLINE { $$ = NULL; }
480 yyerror (& @1, parser, "Invalid tokens after #");
485 /* empty */ { $$ = NULL; }
492 glcpp_warning(&@1, parser, "extra tokens at end of directive");
497 /* Handle "defined" operator */
499 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
500 $$ = _token_create_ival (parser, INTEGER, v);
502 | DEFINED '(' IDENTIFIER ')' {
503 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
504 $$ = _token_create_ival (parser, INTEGER, v);
506 | preprocessing_token
510 /* Exactly the same as pp_tokens, but using conditional_token */
512 $$ = _token_list_create (parser);
513 _token_list_append ($$, $1);
515 | conditional_tokens conditional_token {
517 _token_list_append ($$, $2);
522 preprocessing_token {
523 parser->space_tokens = 1;
524 $$ = _token_list_create (parser);
525 _token_list_append ($$, $1);
527 | pp_tokens preprocessing_token {
529 _token_list_append ($$, $2);
535 $$ = _token_create_str (parser, IDENTIFIER, $1);
536 $$->location = yylloc;
539 $$ = _token_create_str (parser, INTEGER_STRING, $1);
540 $$->location = yylloc;
543 $$ = _token_create_ival (parser, $1, $1);
544 $$->location = yylloc;
547 $$ = _token_create_str (parser, OTHER, $1);
548 $$->location = yylloc;
551 $$ = _token_create_ival (parser, SPACE, SPACE);
552 $$->location = yylloc;
572 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
573 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
576 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
577 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
578 | EQUAL { $$ = EQUAL; }
579 | NOT_EQUAL { $$ = NOT_EQUAL; }
587 | PASTE { $$ = PASTE; }
593 _string_list_create (void *ctx)
597 list = ralloc (ctx, string_list_t);
605 _string_list_append_item (string_list_t *list, const char *str)
609 node = ralloc (list, string_node_t);
610 node->str = ralloc_strdup (node, str);
614 if (list->head == NULL) {
617 list->tail->next = node;
624 _string_list_contains (string_list_t *list, const char *member, int *index)
632 for (i = 0, node = list->head; node; i++, node = node->next) {
633 if (strcmp (node->str, member) == 0) {
644 _string_list_length (string_list_t *list)
652 for (node = list->head; node; node = node->next)
659 _string_list_equal (string_list_t *a, string_list_t *b)
661 string_node_t *node_a, *node_b;
663 if (a == NULL && b == NULL)
666 if (a == NULL || b == NULL)
669 for (node_a = a->head, node_b = b->head;
671 node_a = node_a->next, node_b = node_b->next)
673 if (strcmp (node_a->str, node_b->str))
677 /* Catch the case of lists being different lengths, (which
678 * would cause the loop above to terminate after the shorter
680 return node_a == node_b;
684 _argument_list_create (void *ctx)
686 argument_list_t *list;
688 list = ralloc (ctx, argument_list_t);
696 _argument_list_append (argument_list_t *list, token_list_t *argument)
698 argument_node_t *node;
700 node = ralloc (list, argument_node_t);
701 node->argument = argument;
705 if (list->head == NULL) {
708 list->tail->next = node;
715 _argument_list_length (argument_list_t *list)
718 argument_node_t *node;
723 for (node = list->head; node; node = node->next)
730 _argument_list_member_at (argument_list_t *list, int index)
732 argument_node_t *node;
739 for (i = 0; i < index; i++) {
746 return node->argument;
751 /* Note: This function ralloc_steal()s the str pointer. */
753 _token_create_str (void *ctx, int type, char *str)
757 token = ralloc (ctx, token_t);
759 token->value.str = str;
761 ralloc_steal (token, str);
767 _token_create_ival (void *ctx, int type, int ival)
771 token = ralloc (ctx, token_t);
773 token->value.ival = ival;
779 _token_list_create (void *ctx)
783 list = ralloc (ctx, token_list_t);
786 list->non_space_tail = NULL;
792 _token_list_append (token_list_t *list, token_t *token)
796 node = ralloc (list, token_node_t);
800 if (list->head == NULL) {
803 list->tail->next = node;
807 if (token->type != SPACE)
808 list->non_space_tail = node;
812 _token_list_append_list (token_list_t *list, token_list_t *tail)
814 if (tail == NULL || tail->head == NULL)
817 if (list->head == NULL) {
818 list->head = tail->head;
820 list->tail->next = tail->head;
823 list->tail = tail->tail;
824 list->non_space_tail = tail->non_space_tail;
827 static token_list_t *
828 _token_list_copy (void *ctx, token_list_t *other)
836 copy = _token_list_create (ctx);
837 for (node = other->head; node; node = node->next) {
838 token_t *new_token = ralloc (copy, token_t);
839 *new_token = *node->token;
840 _token_list_append (copy, new_token);
847 _token_list_trim_trailing_space (token_list_t *list)
849 token_node_t *tail, *next;
851 if (list->non_space_tail) {
852 tail = list->non_space_tail->next;
853 list->non_space_tail->next = NULL;
854 list->tail = list->non_space_tail;
865 _token_list_is_empty_ignoring_space (token_list_t *l)
873 while (n != NULL && n->token->type == SPACE)
880 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
882 token_node_t *node_a, *node_b;
884 if (a == NULL || b == NULL) {
885 int a_empty = _token_list_is_empty_ignoring_space(a);
886 int b_empty = _token_list_is_empty_ignoring_space(b);
887 return a_empty == b_empty;
895 if (node_a == NULL && node_b == NULL)
898 if (node_a == NULL || node_b == NULL)
901 if (node_a->token->type == SPACE) {
902 node_a = node_a->next;
906 if (node_b->token->type == SPACE) {
907 node_b = node_b->next;
911 if (node_a->token->type != node_b->token->type)
914 switch (node_a->token->type) {
916 if (node_a->token->value.ival !=
917 node_b->token->value.ival)
925 if (strcmp (node_a->token->value.str,
926 node_b->token->value.str))
933 node_a = node_a->next;
934 node_b = node_b->next;
941 _token_print (char **out, size_t *len, token_t *token)
943 if (token->type < 256) {
944 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
948 switch (token->type) {
950 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
955 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
958 ralloc_asprintf_rewrite_tail (out, len, " ");
961 ralloc_asprintf_rewrite_tail (out, len, "<<");
964 ralloc_asprintf_rewrite_tail (out, len, ">>");
967 ralloc_asprintf_rewrite_tail (out, len, "<=");
969 case GREATER_OR_EQUAL:
970 ralloc_asprintf_rewrite_tail (out, len, ">=");
973 ralloc_asprintf_rewrite_tail (out, len, "==");
976 ralloc_asprintf_rewrite_tail (out, len, "!=");
979 ralloc_asprintf_rewrite_tail (out, len, "&&");
982 ralloc_asprintf_rewrite_tail (out, len, "||");
985 ralloc_asprintf_rewrite_tail (out, len, "##");
988 ralloc_asprintf_rewrite_tail (out, len, ",");
991 /* Nothing to print. */
994 assert(!"Error: Don't know how to print token.");
999 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1000 * 'token' and 'other'. Note that this function may return 'token' or
1001 * 'other' directly rather than allocating anything new.
1003 * Caution: Only very cursory error-checking is performed to see if
1004 * the final result is a valid single token. */
1006 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
1008 token_t *combined = NULL;
1010 /* Pasting a placeholder onto anything makes no change. */
1011 if (other->type == PLACEHOLDER)
1014 /* When 'token' is a placeholder, just return 'other'. */
1015 if (token->type == PLACEHOLDER)
1018 /* A very few single-character punctuators can be combined
1019 * with another to form a multi-character punctuator. */
1020 switch (token->type) {
1022 if (other->type == '<')
1023 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1024 else if (other->type == '=')
1025 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1028 if (other->type == '>')
1029 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1030 else if (other->type == '=')
1031 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1034 if (other->type == '=')
1035 combined = _token_create_ival (token, EQUAL, EQUAL);
1038 if (other->type == '=')
1039 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1042 if (other->type == '&')
1043 combined = _token_create_ival (token, AND, AND);
1046 if (other->type == '|')
1047 combined = _token_create_ival (token, OR, OR);
1051 if (combined != NULL) {
1052 /* Inherit the location from the first token */
1053 combined->location = token->location;
1057 /* Two string-valued tokens can usually just be mashed
1060 * XXX: This isn't actually legitimate. Several things here
1061 * should result in a diagnostic since the result cannot be a
1062 * valid, single pre-processing token. For example, pasting
1063 * "123" and "abc" is not legal, but we don't catch that
1065 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
1066 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
1070 str = ralloc_asprintf (token, "%s%s", token->value.str,
1072 combined = _token_create_str (token, token->type, str);
1073 combined->location = token->location;
1077 glcpp_error (&token->location, parser, "");
1078 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1079 _token_print (&parser->info_log, &parser->info_log_length, token);
1080 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1081 _token_print (&parser->info_log, &parser->info_log_length, other);
1082 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1088 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1095 for (node = list->head; node; node = node->next)
1096 _token_print (&parser->output, &parser->output_length, node->token);
1100 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1102 glcpp_error(locp, parser, "%s", error);
1105 static void add_builtin_define(glcpp_parser_t *parser,
1106 const char *name, int value)
1111 tok = _token_create_ival (parser, INTEGER, value);
1113 list = _token_list_create(parser);
1114 _token_list_append(list, tok);
1115 _define_object_macro(parser, NULL, name, list);
1119 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1121 glcpp_parser_t *parser;
1122 int language_version;
1124 parser = ralloc (NULL, glcpp_parser_t);
1126 glcpp_lex_init_extra (parser, &parser->scanner);
1127 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1128 hash_table_string_compare);
1129 parser->active = NULL;
1130 parser->lexing_if = 0;
1131 parser->space_tokens = 1;
1132 parser->newline_as_space = 0;
1133 parser->in_control_line = 0;
1134 parser->paren_count = 0;
1136 parser->skip_stack = NULL;
1138 parser->lex_from_list = NULL;
1139 parser->lex_from_node = NULL;
1141 parser->output = ralloc_strdup(parser, "");
1142 parser->output_length = 0;
1143 parser->info_log = ralloc_strdup(parser, "");
1144 parser->info_log_length = 0;
1147 parser->has_new_line_number = 0;
1148 parser->new_line_number = 1;
1149 parser->has_new_source_number = 0;
1150 parser->new_source_number = 0;
1152 /* Add pre-defined macros. */
1153 if (extensions != NULL) {
1154 if (extensions->OES_EGL_image_external)
1155 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
1158 if (api == API_OPENGLES2)
1159 add_builtin_define(parser, "GL_ES", 1);
1161 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1162 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1164 if (extensions != NULL) {
1165 if (extensions->EXT_texture_array) {
1166 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1169 if (extensions->ARB_fragment_coord_conventions)
1170 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1173 if (extensions->ARB_explicit_attrib_location)
1174 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1176 if (extensions->ARB_shader_texture_lod)
1177 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
1179 if (extensions->ARB_draw_instanced)
1180 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
1182 if (extensions->ARB_conservative_depth) {
1183 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1184 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
1187 if (extensions->ARB_shader_bit_encoding)
1188 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
1190 if (extensions->ARB_uniform_buffer_object)
1191 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
1193 if (extensions->ARB_texture_cube_map_array)
1194 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
1198 language_version = 110;
1199 add_builtin_define(parser, "__VERSION__", language_version);
1205 glcpp_parser_destroy (glcpp_parser_t *parser)
1207 glcpp_lex_destroy (parser->scanner);
1208 hash_table_dtor (parser->defines);
1209 ralloc_free (parser);
1212 typedef enum function_status
1214 FUNCTION_STATUS_SUCCESS,
1215 FUNCTION_NOT_A_FUNCTION,
1216 FUNCTION_UNBALANCED_PARENTHESES
1217 } function_status_t;
1219 /* Find a set of function-like macro arguments by looking for a
1220 * balanced set of parentheses.
1222 * When called, 'node' should be the opening-parenthesis token, (or
1223 * perhaps preceeding SPACE tokens). Upon successful return *last will
1224 * be the last consumed node, (corresponding to the closing right
1229 * FUNCTION_STATUS_SUCCESS:
1231 * Successfully parsed a set of function arguments.
1233 * FUNCTION_NOT_A_FUNCTION:
1235 * Macro name not followed by a '('. This is not an error, but
1236 * simply that the macro name should be treated as a non-macro.
1238 * FUNCTION_UNBALANCED_PARENTHESES
1240 * Macro name is not followed by a balanced set of parentheses.
1242 static function_status_t
1243 _arguments_parse (argument_list_t *arguments,
1245 token_node_t **last)
1247 token_list_t *argument;
1252 /* Ignore whitespace before first parenthesis. */
1253 while (node && node->token->type == SPACE)
1256 if (node == NULL || node->token->type != '(')
1257 return FUNCTION_NOT_A_FUNCTION;
1261 argument = _token_list_create (arguments);
1262 _argument_list_append (arguments, argument);
1264 for (paren_count = 1; node; node = node->next) {
1265 if (node->token->type == '(')
1269 else if (node->token->type == ')')
1272 if (paren_count == 0)
1276 if (node->token->type == ',' &&
1279 _token_list_trim_trailing_space (argument);
1280 argument = _token_list_create (arguments);
1281 _argument_list_append (arguments, argument);
1284 if (argument->head == NULL) {
1285 /* Don't treat initial whitespace as
1286 * part of the arguement. */
1287 if (node->token->type == SPACE)
1290 _token_list_append (argument, node->token);
1295 return FUNCTION_UNBALANCED_PARENTHESES;
1299 return FUNCTION_STATUS_SUCCESS;
1302 static token_list_t *
1303 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1308 list = _token_list_create (ctx);
1309 node = _token_create_ival (list, type, ival);
1310 _token_list_append (list, node);
1315 static token_list_t *
1316 _token_list_create_with_one_space (void *ctx)
1318 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1321 static token_list_t *
1322 _token_list_create_with_one_integer (void *ctx, int ival)
1324 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1327 /* Perform macro expansion on 'list', placing the resulting tokens
1328 * into a new list which is initialized with a first token of type
1329 * 'head_token_type'. Then begin lexing from the resulting list,
1330 * (return to the current lexing source when this list is exhausted).
1333 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1334 int head_token_type,
1337 token_list_t *expanded;
1340 expanded = _token_list_create (parser);
1341 token = _token_create_ival (parser, head_token_type, head_token_type);
1342 _token_list_append (expanded, token);
1343 _glcpp_parser_expand_token_list (parser, list);
1344 _token_list_append_list (expanded, list);
1345 glcpp_parser_lex_from (parser, expanded);
1349 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1356 token_node_t *next_non_space;
1358 /* Look ahead for a PASTE token, skipping space. */
1359 next_non_space = node->next;
1360 while (next_non_space && next_non_space->token->type == SPACE)
1361 next_non_space = next_non_space->next;
1363 if (next_non_space == NULL)
1366 if (next_non_space->token->type != PASTE) {
1367 node = next_non_space;
1371 /* Now find the next non-space token after the PASTE. */
1372 next_non_space = next_non_space->next;
1373 while (next_non_space && next_non_space->token->type == SPACE)
1374 next_non_space = next_non_space->next;
1376 if (next_non_space == NULL) {
1377 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1381 node->token = _token_paste (parser, node->token, next_non_space->token);
1382 node->next = next_non_space->next;
1383 if (next_non_space == list->tail)
1387 list->non_space_tail = list->tail;
1390 /* This is a helper function that's essentially part of the
1391 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1392 * except for by that function.
1394 * Returns NULL if node is a simple token with no expansion, (that is,
1395 * although 'node' corresponds to an identifier defined as a
1396 * function-like macro, it is not followed with a parenthesized
1399 * Compute the complete expansion of node (which is a function-like
1400 * macro) and subsequent nodes which are arguments.
1402 * Returns the token list that results from the expansion and sets
1403 * *last to the last node in the list that was consumed by the
1404 * expansion. Specifically, *last will be set as follows: as the
1405 * token of the closing right parenthesis.
1407 static token_list_t *
1408 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1410 token_node_t **last)
1414 const char *identifier;
1415 argument_list_t *arguments;
1416 function_status_t status;
1417 token_list_t *substituted;
1418 int parameter_index;
1420 identifier = node->token->value.str;
1422 macro = hash_table_find (parser->defines, identifier);
1424 assert (macro->is_function);
1426 arguments = _argument_list_create (parser);
1427 status = _arguments_parse (arguments, node, last);
1430 case FUNCTION_STATUS_SUCCESS:
1432 case FUNCTION_NOT_A_FUNCTION:
1434 case FUNCTION_UNBALANCED_PARENTHESES:
1435 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1439 /* Replace a macro defined as empty with a SPACE token. */
1440 if (macro->replacements == NULL) {
1441 ralloc_free (arguments);
1442 return _token_list_create_with_one_space (parser);
1445 if (! ((_argument_list_length (arguments) ==
1446 _string_list_length (macro->parameters)) ||
1447 (_string_list_length (macro->parameters) == 0 &&
1448 _argument_list_length (arguments) == 1 &&
1449 arguments->head->argument->head == NULL)))
1451 glcpp_error (&node->token->location, parser,
1452 "Error: macro %s invoked with %d arguments (expected %d)\n",
1454 _argument_list_length (arguments),
1455 _string_list_length (macro->parameters));
1459 /* Perform argument substitution on the replacement list. */
1460 substituted = _token_list_create (arguments);
1462 for (node = macro->replacements->head; node; node = node->next)
1464 if (node->token->type == IDENTIFIER &&
1465 _string_list_contains (macro->parameters,
1466 node->token->value.str,
1469 token_list_t *argument;
1470 argument = _argument_list_member_at (arguments,
1472 /* Before substituting, we expand the argument
1473 * tokens, or append a placeholder token for
1474 * an empty argument. */
1475 if (argument->head) {
1476 token_list_t *expanded_argument;
1477 expanded_argument = _token_list_copy (parser,
1479 _glcpp_parser_expand_token_list (parser,
1481 _token_list_append_list (substituted,
1486 new_token = _token_create_ival (substituted,
1489 _token_list_append (substituted, new_token);
1492 _token_list_append (substituted, node->token);
1496 /* After argument substitution, and before further expansion
1497 * below, implement token pasting. */
1499 _token_list_trim_trailing_space (substituted);
1501 _glcpp_parser_apply_pastes (parser, substituted);
1506 /* Compute the complete expansion of node, (and subsequent nodes after
1507 * 'node' in the case that 'node' is a function-like macro and
1508 * subsequent nodes are arguments).
1510 * Returns NULL if node is a simple token with no expansion.
1512 * Otherwise, returns the token list that results from the expansion
1513 * and sets *last to the last node in the list that was consumed by
1514 * the expansion. Specifically, *last will be set as follows:
1516 * As 'node' in the case of object-like macro expansion.
1518 * As the token of the closing right parenthesis in the case of
1519 * function-like macro expansion.
1521 static token_list_t *
1522 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1524 token_node_t **last)
1526 token_t *token = node->token;
1527 const char *identifier;
1530 /* We only expand identifiers */
1531 if (token->type != IDENTIFIER) {
1532 /* We change any COMMA into a COMMA_FINAL to prevent
1533 * it being mistaken for an argument separator
1535 if (token->type == ',') {
1536 token->type = COMMA_FINAL;
1537 token->value.ival = COMMA_FINAL;
1544 identifier = token->value.str;
1546 /* Special handling for __LINE__ and __FILE__, (not through
1547 * the hash table). */
1548 if (strcmp(identifier, "__LINE__") == 0)
1549 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1551 if (strcmp(identifier, "__FILE__") == 0)
1552 return _token_list_create_with_one_integer (parser, node->token->location.source);
1554 /* Look up this identifier in the hash table. */
1555 macro = hash_table_find (parser->defines, identifier);
1557 /* Not a macro, so no expansion needed. */
1561 /* Finally, don't expand this macro if we're already actively
1562 * expanding it, (to avoid infinite recursion). */
1563 if (_parser_active_list_contains (parser, identifier)) {
1564 /* We change the token type here from IDENTIFIER to
1565 * OTHER to prevent any future expansion of this
1566 * unexpanded token. */
1568 token_list_t *expansion;
1571 str = ralloc_strdup (parser, token->value.str);
1572 final = _token_create_str (parser, OTHER, str);
1573 expansion = _token_list_create (parser);
1574 _token_list_append (expansion, final);
1578 if (! macro->is_function)
1580 token_list_t *replacement;
1582 /* Replace a macro defined as empty with a SPACE token. */
1583 if (macro->replacements == NULL)
1584 return _token_list_create_with_one_space (parser);
1586 replacement = _token_list_copy (parser, macro->replacements);
1587 _glcpp_parser_apply_pastes (parser, replacement);
1591 return _glcpp_parser_expand_function (parser, node, last);
1594 /* Push a new identifier onto the parser's active list.
1596 * Here, 'marker' is the token node that appears in the list after the
1597 * expansion of 'identifier'. That is, when the list iterator begins
1598 * examining 'marker', then it is time to pop this node from the
1602 _parser_active_list_push (glcpp_parser_t *parser,
1603 const char *identifier,
1604 token_node_t *marker)
1606 active_list_t *node;
1608 node = ralloc (parser->active, active_list_t);
1609 node->identifier = ralloc_strdup (node, identifier);
1610 node->marker = marker;
1611 node->next = parser->active;
1613 parser->active = node;
1617 _parser_active_list_pop (glcpp_parser_t *parser)
1619 active_list_t *node = parser->active;
1622 parser->active = NULL;
1626 node = parser->active->next;
1627 ralloc_free (parser->active);
1629 parser->active = node;
1633 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1635 active_list_t *node;
1637 if (parser->active == NULL)
1640 for (node = parser->active; node; node = node->next)
1641 if (strcmp (node->identifier, identifier) == 0)
1647 /* Walk over the token list replacing nodes with their expansion.
1648 * Whenever nodes are expanded the walking will walk over the new
1649 * nodes, continuing to expand as necessary. The results are placed in
1653 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1656 token_node_t *node_prev;
1657 token_node_t *node, *last = NULL;
1658 token_list_t *expansion;
1659 active_list_t *active_initial = parser->active;
1664 _token_list_trim_trailing_space (list);
1671 while (parser->active && parser->active->marker == node)
1672 _parser_active_list_pop (parser);
1674 expansion = _glcpp_parser_expand_node (parser, node, &last);
1678 for (n = node; n != last->next; n = n->next)
1679 while (parser->active &&
1680 parser->active->marker == n)
1682 _parser_active_list_pop (parser);
1685 _parser_active_list_push (parser,
1686 node->token->value.str,
1689 /* Splice expansion into list, supporting a
1690 * simple deletion if the expansion is
1692 if (expansion->head) {
1694 node_prev->next = expansion->head;
1696 list->head = expansion->head;
1697 expansion->tail->next = last->next;
1698 if (last == list->tail)
1699 list->tail = expansion->tail;
1702 node_prev->next = last->next;
1704 list->head = last->next;
1705 if (last == list->tail)
1711 node = node_prev ? node_prev->next : list->head;
1714 /* Remove any lingering effects of this invocation on the
1715 * active list. That is, pop until the list looks like it did
1716 * at the beginning of this function. */
1717 while (parser->active && parser->active != active_initial)
1718 _parser_active_list_pop (parser);
1720 list->non_space_tail = list->tail;
1724 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1730 _glcpp_parser_expand_token_list (parser, list);
1732 _token_list_trim_trailing_space (list);
1734 _token_list_print (parser, list);
1738 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1739 const char *identifier)
1741 /* According to the GLSL specification, macro names starting with "__"
1742 * or "GL_" are reserved for future use. So, don't allow them.
1744 if (strstr(identifier, "__")) {
1745 glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
1747 if (strncmp(identifier, "GL_", 3) == 0) {
1748 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1753 _macro_equal (macro_t *a, macro_t *b)
1755 if (a->is_function != b->is_function)
1758 if (a->is_function) {
1759 if (! _string_list_equal (a->parameters, b->parameters))
1763 return _token_list_equal_ignoring_space (a->replacements,
1768 _define_object_macro (glcpp_parser_t *parser,
1770 const char *identifier,
1771 token_list_t *replacements)
1773 macro_t *macro, *previous;
1776 _check_for_reserved_macro_name(parser, loc, identifier);
1778 macro = ralloc (parser, macro_t);
1780 macro->is_function = 0;
1781 macro->parameters = NULL;
1782 macro->identifier = ralloc_strdup (macro, identifier);
1783 macro->replacements = replacements;
1784 ralloc_steal (macro, replacements);
1786 previous = hash_table_find (parser->defines, identifier);
1788 if (_macro_equal (macro, previous)) {
1789 ralloc_free (macro);
1792 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1796 hash_table_insert (parser->defines, macro, identifier);
1800 _define_function_macro (glcpp_parser_t *parser,
1802 const char *identifier,
1803 string_list_t *parameters,
1804 token_list_t *replacements)
1806 macro_t *macro, *previous;
1808 _check_for_reserved_macro_name(parser, loc, identifier);
1810 macro = ralloc (parser, macro_t);
1811 ralloc_steal (macro, parameters);
1812 ralloc_steal (macro, replacements);
1814 macro->is_function = 1;
1815 macro->parameters = parameters;
1816 macro->identifier = ralloc_strdup (macro, identifier);
1817 macro->replacements = replacements;
1818 previous = hash_table_find (parser->defines, identifier);
1820 if (_macro_equal (macro, previous)) {
1821 ralloc_free (macro);
1824 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1828 hash_table_insert (parser->defines, macro, identifier);
1832 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1837 if (parser->lex_from_list == NULL) {
1838 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1840 /* XXX: This ugly block of code exists for the sole
1841 * purpose of converting a NEWLINE token into a SPACE
1842 * token, but only in the case where we have seen a
1843 * function-like macro name, but have not yet seen its
1844 * closing parenthesis.
1846 * There's perhaps a more compact way to do this with
1847 * mid-rule actions in the grammar.
1849 * I'm definitely not pleased with the complexity of
1852 if (parser->newline_as_space)
1855 parser->paren_count++;
1856 } else if (ret == ')') {
1857 parser->paren_count--;
1858 if (parser->paren_count == 0)
1859 parser->newline_as_space = 0;
1860 } else if (ret == NEWLINE) {
1862 } else if (ret != SPACE) {
1863 if (parser->paren_count == 0)
1864 parser->newline_as_space = 0;
1867 else if (parser->in_control_line)
1870 parser->in_control_line = 0;
1872 else if (ret == HASH_DEFINE ||
1873 ret == HASH_UNDEF || ret == HASH_IF ||
1874 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1875 ret == HASH_ELIF || ret == HASH_ELSE ||
1876 ret == HASH_ENDIF || ret == HASH)
1878 parser->in_control_line = 1;
1880 else if (ret == IDENTIFIER)
1883 macro = hash_table_find (parser->defines,
1885 if (macro && macro->is_function) {
1886 parser->newline_as_space = 1;
1887 parser->paren_count = 0;
1894 node = parser->lex_from_node;
1897 ralloc_free (parser->lex_from_list);
1898 parser->lex_from_list = NULL;
1902 *yylval = node->token->value;
1903 ret = node->token->type;
1905 parser->lex_from_node = node->next;
1911 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1915 assert (parser->lex_from_list == NULL);
1917 /* Copy list, eliminating any space tokens. */
1918 parser->lex_from_list = _token_list_create (parser);
1920 for (node = list->head; node; node = node->next) {
1921 if (node->token->type == SPACE)
1923 _token_list_append (parser->lex_from_list, node->token);
1928 parser->lex_from_node = parser->lex_from_list->head;
1930 /* It's possible the list consisted of nothing but whitespace. */
1931 if (parser->lex_from_node == NULL) {
1932 ralloc_free (parser->lex_from_list);
1933 parser->lex_from_list = NULL;
1938 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1941 skip_type_t current = SKIP_NO_SKIP;
1944 if (parser->skip_stack)
1945 current = parser->skip_stack->type;
1947 node = ralloc (parser, skip_node_t);
1950 if (current == SKIP_NO_SKIP) {
1952 node->type = SKIP_NO_SKIP;
1954 node->type = SKIP_TO_ELSE;
1956 node->type = SKIP_TO_ENDIF;
1959 node->next = parser->skip_stack;
1960 parser->skip_stack = node;
1964 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1965 const char *type, int condition)
1967 if (parser->skip_stack == NULL) {
1968 glcpp_error (loc, parser, "%s without #if\n", type);
1972 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1974 parser->skip_stack->type = SKIP_NO_SKIP;
1976 parser->skip_stack->type = SKIP_TO_ENDIF;
1981 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1985 if (parser->skip_stack == NULL) {
1986 glcpp_error (loc, parser, "#endif without #if\n");
1990 node = parser->skip_stack;
1991 parser->skip_stack = node->next;