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_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
141 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
144 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
147 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
163 %parse-param {glcpp_parser_t *parser}
164 %lex-param {glcpp_parser_t *parser}
167 %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
169 %type <ival> expression INTEGER operator SPACE integer_constant
170 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
171 %type <string_list> identifier_list
172 %type <token> preprocessing_token conditional_token
173 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
179 %left EQUAL NOT_EQUAL
180 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
181 %left LEFT_SHIFT RIGHT_SHIFT
195 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
197 | HASH_LINE pp_tokens NEWLINE {
198 if (parser->skip_stack == NULL ||
199 parser->skip_stack->type == SKIP_NO_SKIP)
201 _glcpp_parser_expand_and_lex_from (parser,
206 _glcpp_parser_print_expanded_token_list (parser, $1);
207 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
215 IF_EXPANDED expression NEWLINE {
216 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
218 | ELIF_EXPANDED expression NEWLINE {
219 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
221 | LINE_EXPANDED integer_constant NEWLINE {
222 parser->has_new_line_number = 1;
223 parser->new_line_number = $2;
224 ralloc_asprintf_rewrite_tail (&parser->output,
225 &parser->output_length,
226 "#line %" PRIiMAX "\n",
229 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
230 parser->has_new_line_number = 1;
231 parser->new_line_number = $2;
232 parser->has_new_source_number = 1;
233 parser->new_source_number = $3;
234 ralloc_asprintf_rewrite_tail (&parser->output,
235 &parser->output_length,
236 "#line %" PRIiMAX " %" PRIiMAX "\n",
242 HASH_DEFINE OBJ_IDENTIFIER replacement_list NEWLINE {
243 _define_object_macro (parser, & @2, $2, $3);
245 | HASH_DEFINE FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
246 _define_function_macro (parser, & @2, $2, NULL, $5);
248 | HASH_DEFINE FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
249 _define_function_macro (parser, & @2, $2, $4, $6);
251 | HASH_UNDEF IDENTIFIER NEWLINE {
252 macro_t *macro = hash_table_find (parser->defines, $2);
254 hash_table_remove (parser->defines, $2);
259 | HASH_IF conditional_tokens NEWLINE {
260 /* Be careful to only evaluate the 'if' expression if
261 * we are not skipping. When we are skipping, we
262 * simply push a new 0-valued 'if' onto the skip
265 * This avoids generating diagnostics for invalid
266 * expressions that are being skipped. */
267 if (parser->skip_stack == NULL ||
268 parser->skip_stack->type == SKIP_NO_SKIP)
270 _glcpp_parser_expand_and_lex_from (parser,
275 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
276 parser->skip_stack->type = SKIP_TO_ENDIF;
280 /* #if without an expression is only an error if we
281 * are not skipping */
282 if (parser->skip_stack == NULL ||
283 parser->skip_stack->type == SKIP_NO_SKIP)
285 glcpp_error(& @1, parser, "#if with no expression");
287 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
289 | HASH_IFDEF IDENTIFIER junk NEWLINE {
290 macro_t *macro = hash_table_find (parser->defines, $2);
292 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
294 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
295 macro_t *macro = hash_table_find (parser->defines, $2);
297 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
299 | HASH_ELIF conditional_tokens NEWLINE {
300 /* Be careful to only evaluate the 'elif' expression
301 * if we are not skipping. When we are skipping, we
302 * simply change to a 0-valued 'elif' on the skip
305 * This avoids generating diagnostics for invalid
306 * expressions that are being skipped. */
307 if (parser->skip_stack &&
308 parser->skip_stack->type == SKIP_TO_ELSE)
310 _glcpp_parser_expand_and_lex_from (parser,
315 _glcpp_parser_skip_stack_change_if (parser, & @1,
319 | HASH_ELIF NEWLINE {
320 /* #elif without an expression is an error unless we
322 if (parser->skip_stack &&
323 parser->skip_stack->type == SKIP_TO_ELSE)
325 glcpp_error(& @1, parser, "#elif with no expression");
329 _glcpp_parser_skip_stack_change_if (parser, & @1,
331 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
335 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
338 _glcpp_parser_skip_stack_pop (parser, & @1);
340 | HASH_VERSION integer_constant NEWLINE {
341 _glcpp_parser_handle_version_declaration(parser, $2, NULL);
343 | HASH_VERSION integer_constant IDENTIFIER NEWLINE {
344 _glcpp_parser_handle_version_declaration(parser, $2, $3);
351 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
352 $$ = strtoll ($1 + 2, NULL, 16);
353 } else if ($1[0] == '0') {
354 $$ = strtoll ($1, NULL, 8);
356 $$ = strtoll ($1, NULL, 10);
367 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $1);
370 | expression OR expression {
373 | expression AND expression {
376 | expression '|' expression {
379 | expression '^' expression {
382 | expression '&' expression {
385 | expression NOT_EQUAL expression {
388 | expression EQUAL expression {
391 | expression GREATER_OR_EQUAL expression {
394 | expression LESS_OR_EQUAL expression {
397 | expression '>' expression {
400 | expression '<' expression {
403 | expression RIGHT_SHIFT expression {
406 | expression LEFT_SHIFT expression {
409 | expression '-' expression {
412 | expression '+' expression {
415 | expression '%' expression {
417 yyerror (& @1, parser,
418 "zero modulus in preprocessor directive");
423 | expression '/' expression {
425 yyerror (& @1, parser,
426 "division by 0 in preprocessor directive");
431 | expression '*' expression {
434 | '!' expression %prec UNARY {
437 | '~' expression %prec UNARY {
440 | '-' expression %prec UNARY {
443 | '+' expression %prec UNARY {
446 | '(' expression ')' {
453 $$ = _string_list_create (parser);
454 _string_list_append_item ($$, $1);
455 ralloc_steal ($$, $1);
457 | identifier_list ',' IDENTIFIER {
459 _string_list_append_item ($$, $3);
460 ralloc_steal ($$, $3);
465 NEWLINE { $$ = NULL; }
471 yyerror (& @1, parser, "Invalid tokens after #");
476 /* empty */ { $$ = NULL; }
483 glcpp_warning(&@1, parser, "extra tokens at end of directive");
488 /* Handle "defined" operator */
490 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
491 $$ = _token_create_ival (parser, INTEGER, v);
493 | DEFINED '(' IDENTIFIER ')' {
494 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
495 $$ = _token_create_ival (parser, INTEGER, v);
497 | preprocessing_token
501 /* Exactly the same as pp_tokens, but using conditional_token */
503 $$ = _token_list_create (parser);
504 _token_list_append ($$, $1);
506 | conditional_tokens conditional_token {
508 _token_list_append ($$, $2);
513 preprocessing_token {
514 parser->space_tokens = 1;
515 $$ = _token_list_create (parser);
516 _token_list_append ($$, $1);
518 | pp_tokens preprocessing_token {
520 _token_list_append ($$, $2);
526 $$ = _token_create_str (parser, IDENTIFIER, $1);
527 $$->location = yylloc;
530 $$ = _token_create_str (parser, INTEGER_STRING, $1);
531 $$->location = yylloc;
534 $$ = _token_create_ival (parser, $1, $1);
535 $$->location = yylloc;
538 $$ = _token_create_str (parser, OTHER, $1);
539 $$->location = yylloc;
542 $$ = _token_create_ival (parser, SPACE, SPACE);
543 $$->location = yylloc;
563 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
564 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
567 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
568 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
569 | EQUAL { $$ = EQUAL; }
570 | NOT_EQUAL { $$ = NOT_EQUAL; }
578 | PASTE { $$ = PASTE; }
584 _string_list_create (void *ctx)
588 list = ralloc (ctx, string_list_t);
596 _string_list_append_item (string_list_t *list, const char *str)
600 node = ralloc (list, string_node_t);
601 node->str = ralloc_strdup (node, str);
605 if (list->head == NULL) {
608 list->tail->next = node;
615 _string_list_contains (string_list_t *list, const char *member, int *index)
623 for (i = 0, node = list->head; node; i++, node = node->next) {
624 if (strcmp (node->str, member) == 0) {
635 _string_list_length (string_list_t *list)
643 for (node = list->head; node; node = node->next)
650 _string_list_equal (string_list_t *a, string_list_t *b)
652 string_node_t *node_a, *node_b;
654 if (a == NULL && b == NULL)
657 if (a == NULL || b == NULL)
660 for (node_a = a->head, node_b = b->head;
662 node_a = node_a->next, node_b = node_b->next)
664 if (strcmp (node_a->str, node_b->str))
668 /* Catch the case of lists being different lengths, (which
669 * would cause the loop above to terminate after the shorter
671 return node_a == node_b;
675 _argument_list_create (void *ctx)
677 argument_list_t *list;
679 list = ralloc (ctx, argument_list_t);
687 _argument_list_append (argument_list_t *list, token_list_t *argument)
689 argument_node_t *node;
691 node = ralloc (list, argument_node_t);
692 node->argument = argument;
696 if (list->head == NULL) {
699 list->tail->next = node;
706 _argument_list_length (argument_list_t *list)
709 argument_node_t *node;
714 for (node = list->head; node; node = node->next)
721 _argument_list_member_at (argument_list_t *list, int index)
723 argument_node_t *node;
730 for (i = 0; i < index; i++) {
737 return node->argument;
742 /* Note: This function ralloc_steal()s the str pointer. */
744 _token_create_str (void *ctx, int type, char *str)
748 token = ralloc (ctx, token_t);
750 token->value.str = str;
752 ralloc_steal (token, str);
758 _token_create_ival (void *ctx, int type, int ival)
762 token = ralloc (ctx, token_t);
764 token->value.ival = ival;
770 _token_list_create (void *ctx)
774 list = ralloc (ctx, token_list_t);
777 list->non_space_tail = NULL;
783 _token_list_append (token_list_t *list, token_t *token)
787 node = ralloc (list, token_node_t);
791 if (list->head == NULL) {
794 list->tail->next = node;
798 if (token->type != SPACE)
799 list->non_space_tail = node;
803 _token_list_append_list (token_list_t *list, token_list_t *tail)
805 if (tail == NULL || tail->head == NULL)
808 if (list->head == NULL) {
809 list->head = tail->head;
811 list->tail->next = tail->head;
814 list->tail = tail->tail;
815 list->non_space_tail = tail->non_space_tail;
818 static token_list_t *
819 _token_list_copy (void *ctx, token_list_t *other)
827 copy = _token_list_create (ctx);
828 for (node = other->head; node; node = node->next) {
829 token_t *new_token = ralloc (copy, token_t);
830 *new_token = *node->token;
831 _token_list_append (copy, new_token);
838 _token_list_trim_trailing_space (token_list_t *list)
840 token_node_t *tail, *next;
842 if (list->non_space_tail) {
843 tail = list->non_space_tail->next;
844 list->non_space_tail->next = NULL;
845 list->tail = list->non_space_tail;
856 _token_list_is_empty_ignoring_space (token_list_t *l)
864 while (n != NULL && n->token->type == SPACE)
871 _token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
873 token_node_t *node_a, *node_b;
875 if (a == NULL || b == NULL) {
876 int a_empty = _token_list_is_empty_ignoring_space(a);
877 int b_empty = _token_list_is_empty_ignoring_space(b);
878 return a_empty == b_empty;
886 if (node_a == NULL && node_b == NULL)
889 if (node_a == NULL || node_b == NULL)
892 if (node_a->token->type == SPACE) {
893 node_a = node_a->next;
897 if (node_b->token->type == SPACE) {
898 node_b = node_b->next;
902 if (node_a->token->type != node_b->token->type)
905 switch (node_a->token->type) {
907 if (node_a->token->value.ival !=
908 node_b->token->value.ival)
916 if (strcmp (node_a->token->value.str,
917 node_b->token->value.str))
924 node_a = node_a->next;
925 node_b = node_b->next;
932 _token_print (char **out, size_t *len, token_t *token)
934 if (token->type < 256) {
935 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
939 switch (token->type) {
941 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
946 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
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, "<=");
960 case GREATER_OR_EQUAL:
961 ralloc_asprintf_rewrite_tail (out, len, ">=");
964 ralloc_asprintf_rewrite_tail (out, len, "==");
967 ralloc_asprintf_rewrite_tail (out, len, "!=");
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 /* Nothing to print. */
985 assert(!"Error: Don't know how to print token.");
990 /* Return a new token (ralloc()ed off of 'token') formed by pasting
991 * 'token' and 'other'. Note that this function may return 'token' or
992 * 'other' directly rather than allocating anything new.
994 * Caution: Only very cursory error-checking is performed to see if
995 * the final result is a valid single token. */
997 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
999 token_t *combined = NULL;
1001 /* Pasting a placeholder onto anything makes no change. */
1002 if (other->type == PLACEHOLDER)
1005 /* When 'token' is a placeholder, just return 'other'. */
1006 if (token->type == PLACEHOLDER)
1009 /* A very few single-character punctuators can be combined
1010 * with another to form a multi-character punctuator. */
1011 switch (token->type) {
1013 if (other->type == '<')
1014 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1015 else if (other->type == '=')
1016 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1019 if (other->type == '>')
1020 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1021 else if (other->type == '=')
1022 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1025 if (other->type == '=')
1026 combined = _token_create_ival (token, EQUAL, EQUAL);
1029 if (other->type == '=')
1030 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1033 if (other->type == '&')
1034 combined = _token_create_ival (token, AND, AND);
1037 if (other->type == '|')
1038 combined = _token_create_ival (token, OR, OR);
1042 if (combined != NULL) {
1043 /* Inherit the location from the first token */
1044 combined->location = token->location;
1048 /* Two string-valued (or integer) tokens can usually just be
1049 * mashed together. (We also handle a string followed by an
1050 * integer here as well.)
1052 * There are some exceptions here. Notably, if the first token
1053 * is an integer (or a string representing an integer), then
1054 * the second token must also be an integer or must be a
1055 * string representing an integer that begins with a digit.
1057 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1058 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1063 /* Check that pasting onto an integer doesn't create a
1064 * non-integer, (that is, only digits can be
1066 if (token->type == INTEGER_STRING || token->type == INTEGER)
1068 switch (other->type) {
1069 case INTEGER_STRING:
1070 if (other->value.str[0] < '0' ||
1071 other->value.str[0] > '9')
1075 if (other->value.ival < 0)
1083 if (token->type == INTEGER)
1084 str = ralloc_asprintf (token, "%" PRIiMAX,
1087 str = ralloc_strdup (token, token->value.str);
1090 if (other->type == INTEGER)
1091 ralloc_asprintf_append (&str, "%" PRIiMAX,
1094 ralloc_strcat (&str, other->value.str);
1096 /* New token is same type as original token, unless we
1097 * started with an integer, in which case we will be
1098 * creating an integer-string. */
1099 combined_type = token->type;
1100 if (combined_type == INTEGER)
1101 combined_type = INTEGER_STRING;
1103 combined = _token_create_str (token, combined_type, str);
1104 combined->location = token->location;
1109 glcpp_error (&token->location, parser, "");
1110 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1111 _token_print (&parser->info_log, &parser->info_log_length, token);
1112 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1113 _token_print (&parser->info_log, &parser->info_log_length, other);
1114 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1120 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
1127 for (node = list->head; node; node = node->next)
1128 _token_print (&parser->output, &parser->output_length, node->token);
1132 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1134 glcpp_error(locp, parser, "%s", error);
1137 static void add_builtin_define(glcpp_parser_t *parser,
1138 const char *name, int value)
1143 tok = _token_create_ival (parser, INTEGER, value);
1145 list = _token_list_create(parser);
1146 _token_list_append(list, tok);
1147 _define_object_macro(parser, NULL, name, list);
1151 glcpp_parser_create (const struct gl_extensions *extensions, int api)
1153 glcpp_parser_t *parser;
1154 int language_version;
1156 parser = ralloc (NULL, glcpp_parser_t);
1158 glcpp_lex_init_extra (parser, &parser->scanner);
1159 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1160 hash_table_string_compare);
1161 parser->active = NULL;
1162 parser->lexing_if = 0;
1163 parser->space_tokens = 1;
1164 parser->newline_as_space = 0;
1165 parser->in_control_line = 0;
1166 parser->paren_count = 0;
1168 parser->skip_stack = NULL;
1170 parser->lex_from_list = NULL;
1171 parser->lex_from_node = NULL;
1173 parser->output = ralloc_strdup(parser, "");
1174 parser->output_length = 0;
1175 parser->info_log = ralloc_strdup(parser, "");
1176 parser->info_log_length = 0;
1179 parser->has_new_line_number = 0;
1180 parser->new_line_number = 1;
1181 parser->has_new_source_number = 0;
1182 parser->new_source_number = 0;
1184 parser->is_gles = false;
1186 /* Add pre-defined macros. */
1187 if (extensions != NULL) {
1188 if (extensions->OES_EGL_image_external)
1189 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
1192 if (api == API_OPENGLES2) {
1193 parser->is_gles = true;
1194 add_builtin_define(parser, "GL_ES", 1);
1196 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1197 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
1199 if (extensions != NULL) {
1200 if (extensions->EXT_texture_array) {
1201 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1204 if (extensions->ARB_fragment_coord_conventions)
1205 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1208 if (extensions->ARB_explicit_attrib_location)
1209 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
1211 if (extensions->ARB_shader_texture_lod)
1212 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
1214 if (extensions->ARB_draw_instanced)
1215 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
1217 if (extensions->ARB_conservative_depth) {
1218 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
1219 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
1222 if (extensions->ARB_shader_bit_encoding)
1223 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
1225 if (extensions->ARB_uniform_buffer_object)
1226 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
1228 if (extensions->ARB_texture_cube_map_array)
1229 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
1231 if (extensions->ARB_shading_language_packing)
1232 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
1234 if (extensions->ARB_texture_multisample)
1235 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
1237 if (extensions->ARB_texture_query_lod)
1238 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
1240 if (extensions->ARB_gpu_shader5)
1241 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
1243 if (extensions->AMD_vertex_shader_layer)
1244 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
1248 language_version = 110;
1249 add_builtin_define(parser, "__VERSION__", language_version);
1255 glcpp_parser_destroy (glcpp_parser_t *parser)
1257 glcpp_lex_destroy (parser->scanner);
1258 hash_table_dtor (parser->defines);
1259 ralloc_free (parser);
1262 typedef enum function_status
1264 FUNCTION_STATUS_SUCCESS,
1265 FUNCTION_NOT_A_FUNCTION,
1266 FUNCTION_UNBALANCED_PARENTHESES
1267 } function_status_t;
1269 /* Find a set of function-like macro arguments by looking for a
1270 * balanced set of parentheses.
1272 * When called, 'node' should be the opening-parenthesis token, (or
1273 * perhaps preceeding SPACE tokens). Upon successful return *last will
1274 * be the last consumed node, (corresponding to the closing right
1279 * FUNCTION_STATUS_SUCCESS:
1281 * Successfully parsed a set of function arguments.
1283 * FUNCTION_NOT_A_FUNCTION:
1285 * Macro name not followed by a '('. This is not an error, but
1286 * simply that the macro name should be treated as a non-macro.
1288 * FUNCTION_UNBALANCED_PARENTHESES
1290 * Macro name is not followed by a balanced set of parentheses.
1292 static function_status_t
1293 _arguments_parse (argument_list_t *arguments,
1295 token_node_t **last)
1297 token_list_t *argument;
1302 /* Ignore whitespace before first parenthesis. */
1303 while (node && node->token->type == SPACE)
1306 if (node == NULL || node->token->type != '(')
1307 return FUNCTION_NOT_A_FUNCTION;
1311 argument = _token_list_create (arguments);
1312 _argument_list_append (arguments, argument);
1314 for (paren_count = 1; node; node = node->next) {
1315 if (node->token->type == '(')
1319 else if (node->token->type == ')')
1322 if (paren_count == 0)
1326 if (node->token->type == ',' &&
1329 _token_list_trim_trailing_space (argument);
1330 argument = _token_list_create (arguments);
1331 _argument_list_append (arguments, argument);
1334 if (argument->head == NULL) {
1335 /* Don't treat initial whitespace as
1336 * part of the arguement. */
1337 if (node->token->type == SPACE)
1340 _token_list_append (argument, node->token);
1345 return FUNCTION_UNBALANCED_PARENTHESES;
1349 return FUNCTION_STATUS_SUCCESS;
1352 static token_list_t *
1353 _token_list_create_with_one_ival (void *ctx, int type, int ival)
1358 list = _token_list_create (ctx);
1359 node = _token_create_ival (list, type, ival);
1360 _token_list_append (list, node);
1365 static token_list_t *
1366 _token_list_create_with_one_space (void *ctx)
1368 return _token_list_create_with_one_ival (ctx, SPACE, SPACE);
1371 static token_list_t *
1372 _token_list_create_with_one_integer (void *ctx, int ival)
1374 return _token_list_create_with_one_ival (ctx, INTEGER, ival);
1377 /* Perform macro expansion on 'list', placing the resulting tokens
1378 * into a new list which is initialized with a first token of type
1379 * 'head_token_type'. Then begin lexing from the resulting list,
1380 * (return to the current lexing source when this list is exhausted).
1383 _glcpp_parser_expand_and_lex_from (glcpp_parser_t *parser,
1384 int head_token_type,
1387 token_list_t *expanded;
1390 expanded = _token_list_create (parser);
1391 token = _token_create_ival (parser, head_token_type, head_token_type);
1392 _token_list_append (expanded, token);
1393 _glcpp_parser_expand_token_list (parser, list);
1394 _token_list_append_list (expanded, list);
1395 glcpp_parser_lex_from (parser, expanded);
1399 _glcpp_parser_apply_pastes (glcpp_parser_t *parser, token_list_t *list)
1406 token_node_t *next_non_space;
1408 /* Look ahead for a PASTE token, skipping space. */
1409 next_non_space = node->next;
1410 while (next_non_space && next_non_space->token->type == SPACE)
1411 next_non_space = next_non_space->next;
1413 if (next_non_space == NULL)
1416 if (next_non_space->token->type != PASTE) {
1417 node = next_non_space;
1421 /* Now find the next non-space token after the PASTE. */
1422 next_non_space = next_non_space->next;
1423 while (next_non_space && next_non_space->token->type == SPACE)
1424 next_non_space = next_non_space->next;
1426 if (next_non_space == NULL) {
1427 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1431 node->token = _token_paste (parser, node->token, next_non_space->token);
1432 node->next = next_non_space->next;
1433 if (next_non_space == list->tail)
1437 list->non_space_tail = list->tail;
1440 /* This is a helper function that's essentially part of the
1441 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1442 * except for by that function.
1444 * Returns NULL if node is a simple token with no expansion, (that is,
1445 * although 'node' corresponds to an identifier defined as a
1446 * function-like macro, it is not followed with a parenthesized
1449 * Compute the complete expansion of node (which is a function-like
1450 * macro) and subsequent nodes which are arguments.
1452 * Returns the token list that results from the expansion and sets
1453 * *last to the last node in the list that was consumed by the
1454 * expansion. Specifically, *last will be set as follows: as the
1455 * token of the closing right parenthesis.
1457 static token_list_t *
1458 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1460 token_node_t **last)
1464 const char *identifier;
1465 argument_list_t *arguments;
1466 function_status_t status;
1467 token_list_t *substituted;
1468 int parameter_index;
1470 identifier = node->token->value.str;
1472 macro = hash_table_find (parser->defines, identifier);
1474 assert (macro->is_function);
1476 arguments = _argument_list_create (parser);
1477 status = _arguments_parse (arguments, node, last);
1480 case FUNCTION_STATUS_SUCCESS:
1482 case FUNCTION_NOT_A_FUNCTION:
1484 case FUNCTION_UNBALANCED_PARENTHESES:
1485 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1489 /* Replace a macro defined as empty with a SPACE token. */
1490 if (macro->replacements == NULL) {
1491 ralloc_free (arguments);
1492 return _token_list_create_with_one_space (parser);
1495 if (! ((_argument_list_length (arguments) ==
1496 _string_list_length (macro->parameters)) ||
1497 (_string_list_length (macro->parameters) == 0 &&
1498 _argument_list_length (arguments) == 1 &&
1499 arguments->head->argument->head == NULL)))
1501 glcpp_error (&node->token->location, parser,
1502 "Error: macro %s invoked with %d arguments (expected %d)\n",
1504 _argument_list_length (arguments),
1505 _string_list_length (macro->parameters));
1509 /* Perform argument substitution on the replacement list. */
1510 substituted = _token_list_create (arguments);
1512 for (node = macro->replacements->head; node; node = node->next)
1514 if (node->token->type == IDENTIFIER &&
1515 _string_list_contains (macro->parameters,
1516 node->token->value.str,
1519 token_list_t *argument;
1520 argument = _argument_list_member_at (arguments,
1522 /* Before substituting, we expand the argument
1523 * tokens, or append a placeholder token for
1524 * an empty argument. */
1525 if (argument->head) {
1526 token_list_t *expanded_argument;
1527 expanded_argument = _token_list_copy (parser,
1529 _glcpp_parser_expand_token_list (parser,
1531 _token_list_append_list (substituted,
1536 new_token = _token_create_ival (substituted,
1539 _token_list_append (substituted, new_token);
1542 _token_list_append (substituted, node->token);
1546 /* After argument substitution, and before further expansion
1547 * below, implement token pasting. */
1549 _token_list_trim_trailing_space (substituted);
1551 _glcpp_parser_apply_pastes (parser, substituted);
1556 /* Compute the complete expansion of node, (and subsequent nodes after
1557 * 'node' in the case that 'node' is a function-like macro and
1558 * subsequent nodes are arguments).
1560 * Returns NULL if node is a simple token with no expansion.
1562 * Otherwise, returns the token list that results from the expansion
1563 * and sets *last to the last node in the list that was consumed by
1564 * the expansion. Specifically, *last will be set as follows:
1566 * As 'node' in the case of object-like macro expansion.
1568 * As the token of the closing right parenthesis in the case of
1569 * function-like macro expansion.
1571 static token_list_t *
1572 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1574 token_node_t **last)
1576 token_t *token = node->token;
1577 const char *identifier;
1580 /* We only expand identifiers */
1581 if (token->type != IDENTIFIER) {
1582 /* We change any COMMA into a COMMA_FINAL to prevent
1583 * it being mistaken for an argument separator
1585 if (token->type == ',') {
1586 token->type = COMMA_FINAL;
1587 token->value.ival = COMMA_FINAL;
1594 identifier = token->value.str;
1596 /* Special handling for __LINE__ and __FILE__, (not through
1597 * the hash table). */
1598 if (strcmp(identifier, "__LINE__") == 0)
1599 return _token_list_create_with_one_integer (parser, node->token->location.first_line);
1601 if (strcmp(identifier, "__FILE__") == 0)
1602 return _token_list_create_with_one_integer (parser, node->token->location.source);
1604 /* Look up this identifier in the hash table. */
1605 macro = hash_table_find (parser->defines, identifier);
1607 /* Not a macro, so no expansion needed. */
1611 /* Finally, don't expand this macro if we're already actively
1612 * expanding it, (to avoid infinite recursion). */
1613 if (_parser_active_list_contains (parser, identifier)) {
1614 /* We change the token type here from IDENTIFIER to
1615 * OTHER to prevent any future expansion of this
1616 * unexpanded token. */
1618 token_list_t *expansion;
1621 str = ralloc_strdup (parser, token->value.str);
1622 final = _token_create_str (parser, OTHER, str);
1623 expansion = _token_list_create (parser);
1624 _token_list_append (expansion, final);
1628 if (! macro->is_function)
1630 token_list_t *replacement;
1632 /* Replace a macro defined as empty with a SPACE token. */
1633 if (macro->replacements == NULL)
1634 return _token_list_create_with_one_space (parser);
1636 replacement = _token_list_copy (parser, macro->replacements);
1637 _glcpp_parser_apply_pastes (parser, replacement);
1641 return _glcpp_parser_expand_function (parser, node, last);
1644 /* Push a new identifier onto the parser's active list.
1646 * Here, 'marker' is the token node that appears in the list after the
1647 * expansion of 'identifier'. That is, when the list iterator begins
1648 * examining 'marker', then it is time to pop this node from the
1652 _parser_active_list_push (glcpp_parser_t *parser,
1653 const char *identifier,
1654 token_node_t *marker)
1656 active_list_t *node;
1658 node = ralloc (parser->active, active_list_t);
1659 node->identifier = ralloc_strdup (node, identifier);
1660 node->marker = marker;
1661 node->next = parser->active;
1663 parser->active = node;
1667 _parser_active_list_pop (glcpp_parser_t *parser)
1669 active_list_t *node = parser->active;
1672 parser->active = NULL;
1676 node = parser->active->next;
1677 ralloc_free (parser->active);
1679 parser->active = node;
1683 _parser_active_list_contains (glcpp_parser_t *parser, const char *identifier)
1685 active_list_t *node;
1687 if (parser->active == NULL)
1690 for (node = parser->active; node; node = node->next)
1691 if (strcmp (node->identifier, identifier) == 0)
1697 /* Walk over the token list replacing nodes with their expansion.
1698 * Whenever nodes are expanded the walking will walk over the new
1699 * nodes, continuing to expand as necessary. The results are placed in
1703 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1706 token_node_t *node_prev;
1707 token_node_t *node, *last = NULL;
1708 token_list_t *expansion;
1709 active_list_t *active_initial = parser->active;
1714 _token_list_trim_trailing_space (list);
1721 while (parser->active && parser->active->marker == node)
1722 _parser_active_list_pop (parser);
1724 expansion = _glcpp_parser_expand_node (parser, node, &last);
1728 for (n = node; n != last->next; n = n->next)
1729 while (parser->active &&
1730 parser->active->marker == n)
1732 _parser_active_list_pop (parser);
1735 _parser_active_list_push (parser,
1736 node->token->value.str,
1739 /* Splice expansion into list, supporting a
1740 * simple deletion if the expansion is
1742 if (expansion->head) {
1744 node_prev->next = expansion->head;
1746 list->head = expansion->head;
1747 expansion->tail->next = last->next;
1748 if (last == list->tail)
1749 list->tail = expansion->tail;
1752 node_prev->next = last->next;
1754 list->head = last->next;
1755 if (last == list->tail)
1761 node = node_prev ? node_prev->next : list->head;
1764 /* Remove any lingering effects of this invocation on the
1765 * active list. That is, pop until the list looks like it did
1766 * at the beginning of this function. */
1767 while (parser->active && parser->active != active_initial)
1768 _parser_active_list_pop (parser);
1770 list->non_space_tail = list->tail;
1774 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1780 _glcpp_parser_expand_token_list (parser, list);
1782 _token_list_trim_trailing_space (list);
1784 _token_list_print (parser, list);
1788 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1789 const char *identifier)
1791 /* According to the GLSL specification, macro names starting with "__"
1792 * or "GL_" are reserved for future use. So, don't allow them.
1794 if (strstr(identifier, "__")) {
1795 glcpp_error (loc, parser, "Macro names containing \"__\" are reserved.\n");
1797 if (strncmp(identifier, "GL_", 3) == 0) {
1798 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1803 _macro_equal (macro_t *a, macro_t *b)
1805 if (a->is_function != b->is_function)
1808 if (a->is_function) {
1809 if (! _string_list_equal (a->parameters, b->parameters))
1813 return _token_list_equal_ignoring_space (a->replacements,
1818 _define_object_macro (glcpp_parser_t *parser,
1820 const char *identifier,
1821 token_list_t *replacements)
1823 macro_t *macro, *previous;
1826 _check_for_reserved_macro_name(parser, loc, identifier);
1828 macro = ralloc (parser, macro_t);
1830 macro->is_function = 0;
1831 macro->parameters = NULL;
1832 macro->identifier = ralloc_strdup (macro, identifier);
1833 macro->replacements = replacements;
1834 ralloc_steal (macro, replacements);
1836 previous = hash_table_find (parser->defines, identifier);
1838 if (_macro_equal (macro, previous)) {
1839 ralloc_free (macro);
1842 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1846 hash_table_insert (parser->defines, macro, identifier);
1850 _define_function_macro (glcpp_parser_t *parser,
1852 const char *identifier,
1853 string_list_t *parameters,
1854 token_list_t *replacements)
1856 macro_t *macro, *previous;
1858 _check_for_reserved_macro_name(parser, loc, identifier);
1860 macro = ralloc (parser, macro_t);
1861 ralloc_steal (macro, parameters);
1862 ralloc_steal (macro, replacements);
1864 macro->is_function = 1;
1865 macro->parameters = parameters;
1866 macro->identifier = ralloc_strdup (macro, identifier);
1867 macro->replacements = replacements;
1868 previous = hash_table_find (parser->defines, identifier);
1870 if (_macro_equal (macro, previous)) {
1871 ralloc_free (macro);
1874 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1878 hash_table_insert (parser->defines, macro, identifier);
1882 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1887 if (parser->lex_from_list == NULL) {
1888 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1890 /* XXX: This ugly block of code exists for the sole
1891 * purpose of converting a NEWLINE token into a SPACE
1892 * token, but only in the case where we have seen a
1893 * function-like macro name, but have not yet seen its
1894 * closing parenthesis.
1896 * There's perhaps a more compact way to do this with
1897 * mid-rule actions in the grammar.
1899 * I'm definitely not pleased with the complexity of
1902 if (parser->newline_as_space)
1905 parser->paren_count++;
1906 } else if (ret == ')') {
1907 parser->paren_count--;
1908 if (parser->paren_count == 0)
1909 parser->newline_as_space = 0;
1910 } else if (ret == NEWLINE) {
1912 } else if (ret != SPACE) {
1913 if (parser->paren_count == 0)
1914 parser->newline_as_space = 0;
1917 else if (parser->in_control_line)
1920 parser->in_control_line = 0;
1922 else if (ret == HASH_DEFINE ||
1923 ret == HASH_UNDEF || ret == HASH_IF ||
1924 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1925 ret == HASH_ELIF || ret == HASH_ELSE ||
1926 ret == HASH_ENDIF || ret == HASH)
1928 parser->in_control_line = 1;
1930 else if (ret == IDENTIFIER)
1933 macro = hash_table_find (parser->defines,
1935 if (macro && macro->is_function) {
1936 parser->newline_as_space = 1;
1937 parser->paren_count = 0;
1944 node = parser->lex_from_node;
1947 ralloc_free (parser->lex_from_list);
1948 parser->lex_from_list = NULL;
1952 *yylval = node->token->value;
1953 ret = node->token->type;
1955 parser->lex_from_node = node->next;
1961 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1965 assert (parser->lex_from_list == NULL);
1967 /* Copy list, eliminating any space tokens. */
1968 parser->lex_from_list = _token_list_create (parser);
1970 for (node = list->head; node; node = node->next) {
1971 if (node->token->type == SPACE)
1973 _token_list_append (parser->lex_from_list, node->token);
1978 parser->lex_from_node = parser->lex_from_list->head;
1980 /* It's possible the list consisted of nothing but whitespace. */
1981 if (parser->lex_from_node == NULL) {
1982 ralloc_free (parser->lex_from_list);
1983 parser->lex_from_list = NULL;
1988 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1991 skip_type_t current = SKIP_NO_SKIP;
1994 if (parser->skip_stack)
1995 current = parser->skip_stack->type;
1997 node = ralloc (parser, skip_node_t);
2000 if (current == SKIP_NO_SKIP) {
2002 node->type = SKIP_NO_SKIP;
2004 node->type = SKIP_TO_ELSE;
2006 node->type = SKIP_TO_ENDIF;
2009 node->next = parser->skip_stack;
2010 parser->skip_stack = node;
2014 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
2015 const char *type, int condition)
2017 if (parser->skip_stack == NULL) {
2018 glcpp_error (loc, parser, "%s without #if\n", type);
2022 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2024 parser->skip_stack->type = SKIP_NO_SKIP;
2026 parser->skip_stack->type = SKIP_TO_ENDIF;
2031 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
2035 if (parser->skip_stack == NULL) {
2036 glcpp_error (loc, parser, "#endif without #if\n");
2040 node = parser->skip_stack;
2041 parser->skip_stack = node->next;
2046 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2047 const char *es_identifier)
2049 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
2051 hash_table_remove (parser->defines, "__VERSION__");
2052 ralloc_free (macro);
2054 add_builtin_define (parser, "__VERSION__", version);
2056 /* If we didn't have a GLES context to begin with, (indicated
2057 * by parser->api), then the version declaration here might
2059 if (! parser->is_gles &&
2061 (es_identifier && (strcmp(es_identifier, "es") == 0))))
2063 parser->is_gles = true;
2064 add_builtin_define (parser, "GL_ES", 1);
2068 add_builtin_define(parser, "GL_core_profile", 1);
2070 /* Currently, all ES2/ES3 implementations support highp in the
2071 * fragment shader, so we always define this macro in ES2/ES3.
2072 * If we ever get a driver that doesn't support highp, we'll
2073 * need to add a flag to the gl_context and check that here.
2075 if (version >= 130 || parser->is_gles)
2076 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2078 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length,
2079 "#version %" PRIiMAX "%s%s", version,
2080 es_identifier ? " " : "",
2081 es_identifier ? es_identifier : "");