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_has_duplicate(string_list_t *list);
64 _string_list_length(string_list_t *list);
67 _string_list_equal(string_list_t *a, string_list_t *b);
69 static argument_list_t *
70 _argument_list_create(void *ctx);
73 _argument_list_append(argument_list_t *list, token_list_t *argument);
76 _argument_list_length(argument_list_t *list);
79 _argument_list_member_at(argument_list_t *list, int index);
81 /* Note: This function ralloc_steal()s the str pointer. */
83 _token_create_str(void *ctx, int type, char *str);
86 _token_create_ival(void *ctx, int type, int ival);
89 _token_list_create(void *ctx);
92 _token_list_append(token_list_t *list, token_t *token);
95 _token_list_append_list(token_list_t *list, token_list_t *tail);
98 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b);
101 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
102 token_node_t *marker);
105 _parser_active_list_pop(glcpp_parser_t *parser);
108 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier);
111 EXPANSION_MODE_IGNORE_DEFINED,
112 EXPANSION_MODE_EVALUATE_DEFINED
115 /* Expand list, and begin lexing from the result (after first
116 * prefixing a token of type 'head_token_type').
119 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
120 token_list_t *list, expansion_mode_t mode);
122 /* Perform macro expansion in-place on the given list. */
124 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
125 expansion_mode_t mode);
128 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
132 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
136 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
137 const char *type, int condition);
140 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc);
143 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
144 const char *ident, bool explicitly_set);
147 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
150 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list);
153 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
169 %parse-param {glcpp_parser_t *parser}
170 %lex-param {glcpp_parser_t *parser}
174 /* We use HASH_TOKEN, DEFINE_TOKEN and VERSION_TOKEN (as opposed to
175 * HASH, DEFINE, and VERSION) to avoid conflicts with other symbols,
176 * (such as the <HASH> and <DEFINE> start conditions in the lexer). */
177 %token DEFINED ELIF_EXPANDED HASH_TOKEN DEFINE_TOKEN FUNC_IDENTIFIER OBJ_IDENTIFIER ELIF ELSE ENDIF ERROR_TOKEN IF IFDEF IFNDEF LINE PRAGMA UNDEF VERSION_TOKEN GARBAGE IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE PLUS_PLUS MINUS_MINUS
179 %type <ival> INTEGER operator SPACE integer_constant
180 %type <expression_value> expression
181 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA
182 %type <string_list> identifier_list
183 %type <token> preprocessing_token
184 %type <token_list> pp_tokens replacement_list text_line
190 %left EQUAL NOT_EQUAL
191 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
192 %left LEFT_SHIFT RIGHT_SHIFT
210 _glcpp_parser_print_expanded_token_list (parser, $1);
211 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
218 IF_EXPANDED expression NEWLINE {
219 if (parser->is_gles && $2.undefined_macro)
220 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
221 _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
223 | ELIF_EXPANDED expression NEWLINE {
224 if (parser->is_gles && $2.undefined_macro)
225 glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
226 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
228 | LINE_EXPANDED integer_constant NEWLINE {
229 parser->has_new_line_number = 1;
230 parser->new_line_number = $2;
231 ralloc_asprintf_rewrite_tail (&parser->output,
232 &parser->output_length,
233 "#line %" PRIiMAX "\n",
236 | LINE_EXPANDED integer_constant integer_constant NEWLINE {
237 parser->has_new_line_number = 1;
238 parser->new_line_number = $2;
239 parser->has_new_source_number = 1;
240 parser->new_source_number = $3;
241 ralloc_asprintf_rewrite_tail (&parser->output,
242 &parser->output_length,
243 "#line %" PRIiMAX " %" PRIiMAX "\n",
249 OBJ_IDENTIFIER replacement_list NEWLINE {
250 _define_object_macro (parser, & @1, $1, $2);
252 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
253 _define_function_macro (parser, & @1, $1, NULL, $4);
255 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
256 _define_function_macro (parser, & @1, $1, $3, $5);
261 control_line_success {
262 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
265 | HASH_TOKEN LINE pp_tokens NEWLINE {
267 if (parser->skip_stack == NULL ||
268 parser->skip_stack->type == SKIP_NO_SKIP)
270 _glcpp_parser_expand_and_lex_from (parser,
272 EXPANSION_MODE_IGNORE_DEFINED);
277 control_line_success:
278 HASH_TOKEN DEFINE_TOKEN define
279 | HASH_TOKEN UNDEF IDENTIFIER NEWLINE {
281 if (strcmp("__LINE__", $3) == 0
282 || strcmp("__FILE__", $3) == 0
283 || strcmp("__VERSION__", $3) == 0
284 || strncmp("GL_", $3, 3) == 0)
285 glcpp_error(& @1, parser, "Built-in (pre-defined)"
286 " macro names cannot be undefined.");
288 macro = hash_table_find (parser->defines, $3);
290 hash_table_remove (parser->defines, $3);
295 | HASH_TOKEN IF pp_tokens NEWLINE {
296 /* Be careful to only evaluate the 'if' expression if
297 * we are not skipping. When we are skipping, we
298 * simply push a new 0-valued 'if' onto the skip
301 * This avoids generating diagnostics for invalid
302 * expressions that are being skipped. */
303 if (parser->skip_stack == NULL ||
304 parser->skip_stack->type == SKIP_NO_SKIP)
306 _glcpp_parser_expand_and_lex_from (parser,
308 EXPANSION_MODE_EVALUATE_DEFINED);
312 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
313 parser->skip_stack->type = SKIP_TO_ENDIF;
316 | HASH_TOKEN IF NEWLINE {
317 /* #if without an expression is only an error if we
318 * are not skipping */
319 if (parser->skip_stack == NULL ||
320 parser->skip_stack->type == SKIP_NO_SKIP)
322 glcpp_error(& @1, parser, "#if with no expression");
324 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
326 | HASH_TOKEN IFDEF IDENTIFIER junk NEWLINE {
327 macro_t *macro = hash_table_find (parser->defines, $3);
329 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
331 | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE {
332 macro_t *macro = hash_table_find (parser->defines, $3);
334 _glcpp_parser_skip_stack_push_if (parser, & @3, macro == NULL);
336 | HASH_TOKEN ELIF pp_tokens NEWLINE {
337 /* Be careful to only evaluate the 'elif' expression
338 * if we are not skipping. When we are skipping, we
339 * simply change to a 0-valued 'elif' on the skip
342 * This avoids generating diagnostics for invalid
343 * expressions that are being skipped. */
344 if (parser->skip_stack &&
345 parser->skip_stack->type == SKIP_TO_ELSE)
347 _glcpp_parser_expand_and_lex_from (parser,
349 EXPANSION_MODE_EVALUATE_DEFINED);
351 else if (parser->skip_stack &&
352 parser->skip_stack->has_else)
354 glcpp_error(& @1, parser, "#elif after #else");
358 _glcpp_parser_skip_stack_change_if (parser, & @1,
362 | HASH_TOKEN ELIF NEWLINE {
363 /* #elif without an expression is an error unless we
365 if (parser->skip_stack &&
366 parser->skip_stack->type == SKIP_TO_ELSE)
368 glcpp_error(& @1, parser, "#elif with no expression");
370 else if (parser->skip_stack &&
371 parser->skip_stack->has_else)
373 glcpp_error(& @1, parser, "#elif after #else");
377 _glcpp_parser_skip_stack_change_if (parser, & @1,
379 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
382 | HASH_TOKEN ELSE { parser->lexing_directive = 1; } NEWLINE {
383 if (parser->skip_stack &&
384 parser->skip_stack->has_else)
386 glcpp_error(& @1, parser, "multiple #else");
390 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
391 if (parser->skip_stack)
392 parser->skip_stack->has_else = true;
396 _glcpp_parser_skip_stack_pop (parser, & @1);
398 | HASH_TOKEN VERSION_TOKEN integer_constant NEWLINE {
399 if (parser->version_resolved) {
400 glcpp_error(& @1, parser, "#version must appear on the first line");
402 _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
404 | HASH_TOKEN VERSION_TOKEN integer_constant IDENTIFIER NEWLINE {
405 if (parser->version_resolved) {
406 glcpp_error(& @1, parser, "#version must appear on the first line");
408 _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
410 | HASH_TOKEN NEWLINE {
411 glcpp_parser_resolve_implicit_version(parser);
413 | HASH_TOKEN PRAGMA NEWLINE {
414 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#%s", $2);
419 HASH_TOKEN ERROR_TOKEN NEWLINE {
420 glcpp_error(& @1, parser, "#%s", $2);
422 | HASH_TOKEN DEFINE_TOKEN NEWLINE {
423 glcpp_error (& @1, parser, "#define without macro name");
425 | HASH_TOKEN GARBAGE pp_tokens NEWLINE {
426 glcpp_error (& @1, parser, "Illegal non-directive after #");
432 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
433 $$ = strtoll ($1 + 2, NULL, 16);
434 } else if ($1[0] == '0') {
435 $$ = strtoll ($1, NULL, 8);
437 $$ = strtoll ($1, NULL, 10);
447 $$.undefined_macro = NULL;
452 $$.undefined_macro = ralloc_strdup (parser, $1);
454 $$.undefined_macro = NULL;
456 | expression OR expression {
457 $$.value = $1.value || $3.value;
459 /* Short-circuit: Only flag undefined from right side
460 * if left side evaluates to false.
462 if ($1.undefined_macro)
463 $$.undefined_macro = $1.undefined_macro;
465 $$.undefined_macro = $3.undefined_macro;
467 | expression AND expression {
468 $$.value = $1.value && $3.value;
470 /* Short-circuit: Only flag undefined from right-side
471 * if left side evaluates to true.
473 if ($1.undefined_macro)
474 $$.undefined_macro = $1.undefined_macro;
476 $$.undefined_macro = $3.undefined_macro;
478 | expression '|' expression {
479 $$.value = $1.value | $3.value;
480 if ($1.undefined_macro)
481 $$.undefined_macro = $1.undefined_macro;
483 $$.undefined_macro = $3.undefined_macro;
485 | expression '^' expression {
486 $$.value = $1.value ^ $3.value;
487 if ($1.undefined_macro)
488 $$.undefined_macro = $1.undefined_macro;
490 $$.undefined_macro = $3.undefined_macro;
492 | expression '&' expression {
493 $$.value = $1.value & $3.value;
494 if ($1.undefined_macro)
495 $$.undefined_macro = $1.undefined_macro;
497 $$.undefined_macro = $3.undefined_macro;
499 | expression NOT_EQUAL expression {
500 $$.value = $1.value != $3.value;
501 if ($1.undefined_macro)
502 $$.undefined_macro = $1.undefined_macro;
504 $$.undefined_macro = $3.undefined_macro;
506 | expression EQUAL expression {
507 $$.value = $1.value == $3.value;
508 if ($1.undefined_macro)
509 $$.undefined_macro = $1.undefined_macro;
511 $$.undefined_macro = $3.undefined_macro;
513 | expression GREATER_OR_EQUAL expression {
514 $$.value = $1.value >= $3.value;
515 if ($1.undefined_macro)
516 $$.undefined_macro = $1.undefined_macro;
518 $$.undefined_macro = $3.undefined_macro;
520 | expression LESS_OR_EQUAL expression {
521 $$.value = $1.value <= $3.value;
522 if ($1.undefined_macro)
523 $$.undefined_macro = $1.undefined_macro;
525 $$.undefined_macro = $3.undefined_macro;
527 | expression '>' expression {
528 $$.value = $1.value > $3.value;
529 if ($1.undefined_macro)
530 $$.undefined_macro = $1.undefined_macro;
532 $$.undefined_macro = $3.undefined_macro;
534 | expression '<' expression {
535 $$.value = $1.value < $3.value;
536 if ($1.undefined_macro)
537 $$.undefined_macro = $1.undefined_macro;
539 $$.undefined_macro = $3.undefined_macro;
541 | expression RIGHT_SHIFT expression {
542 $$.value = $1.value >> $3.value;
543 if ($1.undefined_macro)
544 $$.undefined_macro = $1.undefined_macro;
546 $$.undefined_macro = $3.undefined_macro;
548 | expression LEFT_SHIFT expression {
549 $$.value = $1.value << $3.value;
550 if ($1.undefined_macro)
551 $$.undefined_macro = $1.undefined_macro;
553 $$.undefined_macro = $3.undefined_macro;
555 | expression '-' expression {
556 $$.value = $1.value - $3.value;
557 if ($1.undefined_macro)
558 $$.undefined_macro = $1.undefined_macro;
560 $$.undefined_macro = $3.undefined_macro;
562 | expression '+' expression {
563 $$.value = $1.value + $3.value;
564 if ($1.undefined_macro)
565 $$.undefined_macro = $1.undefined_macro;
567 $$.undefined_macro = $3.undefined_macro;
569 | expression '%' expression {
571 yyerror (& @1, parser,
572 "zero modulus in preprocessor directive");
574 $$.value = $1.value % $3.value;
576 if ($1.undefined_macro)
577 $$.undefined_macro = $1.undefined_macro;
579 $$.undefined_macro = $3.undefined_macro;
581 | expression '/' expression {
583 yyerror (& @1, parser,
584 "division by 0 in preprocessor directive");
586 $$.value = $1.value / $3.value;
588 if ($1.undefined_macro)
589 $$.undefined_macro = $1.undefined_macro;
591 $$.undefined_macro = $3.undefined_macro;
593 | expression '*' expression {
594 $$.value = $1.value * $3.value;
595 if ($1.undefined_macro)
596 $$.undefined_macro = $1.undefined_macro;
598 $$.undefined_macro = $3.undefined_macro;
600 | '!' expression %prec UNARY {
601 $$.value = ! $2.value;
602 $$.undefined_macro = $2.undefined_macro;
604 | '~' expression %prec UNARY {
605 $$.value = ~ $2.value;
606 $$.undefined_macro = $2.undefined_macro;
608 | '-' expression %prec UNARY {
609 $$.value = - $2.value;
610 $$.undefined_macro = $2.undefined_macro;
612 | '+' expression %prec UNARY {
613 $$.value = + $2.value;
614 $$.undefined_macro = $2.undefined_macro;
616 | '(' expression ')' {
623 $$ = _string_list_create (parser);
624 _string_list_append_item ($$, $1);
625 ralloc_steal ($$, $1);
627 | identifier_list ',' IDENTIFIER {
629 _string_list_append_item ($$, $3);
630 ralloc_steal ($$, $3);
635 NEWLINE { $$ = NULL; }
640 /* empty */ { $$ = NULL; }
647 glcpp_error(&@1, parser, "extra tokens at end of directive");
652 preprocessing_token {
653 parser->space_tokens = 1;
654 $$ = _token_list_create (parser);
655 _token_list_append ($$, $1);
657 | pp_tokens preprocessing_token {
659 _token_list_append ($$, $2);
665 $$ = _token_create_str (parser, IDENTIFIER, $1);
666 $$->location = yylloc;
669 $$ = _token_create_str (parser, INTEGER_STRING, $1);
670 $$->location = yylloc;
673 $$ = _token_create_ival (parser, $1, $1);
674 $$->location = yylloc;
677 $$ = _token_create_ival (parser, DEFINED, DEFINED);
678 $$->location = yylloc;
681 $$ = _token_create_str (parser, OTHER, $1);
682 $$->location = yylloc;
685 $$ = _token_create_ival (parser, SPACE, SPACE);
686 $$->location = yylloc;
706 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
707 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
710 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
711 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
712 | EQUAL { $$ = EQUAL; }
713 | NOT_EQUAL { $$ = NOT_EQUAL; }
721 | PASTE { $$ = PASTE; }
722 | PLUS_PLUS { $$ = PLUS_PLUS; }
723 | MINUS_MINUS { $$ = MINUS_MINUS; }
729 _string_list_create(void *ctx)
733 list = ralloc (ctx, string_list_t);
741 _string_list_append_item(string_list_t *list, const char *str)
745 node = ralloc (list, string_node_t);
746 node->str = ralloc_strdup (node, str);
750 if (list->head == NULL) {
753 list->tail->next = node;
760 _string_list_contains(string_list_t *list, const char *member, int *index)
768 for (i = 0, node = list->head; node; i++, node = node->next) {
769 if (strcmp (node->str, member) == 0) {
779 /* Return duplicate string in list (if any), NULL otherwise. */
781 _string_list_has_duplicate(string_list_t *list)
783 string_node_t *node, *dup;
788 for (node = list->head; node; node = node->next) {
789 for (dup = node->next; dup; dup = dup->next) {
790 if (strcmp (node->str, dup->str) == 0)
799 _string_list_length(string_list_t *list)
807 for (node = list->head; node; node = node->next)
814 _string_list_equal(string_list_t *a, string_list_t *b)
816 string_node_t *node_a, *node_b;
818 if (a == NULL && b == NULL)
821 if (a == NULL || b == NULL)
824 for (node_a = a->head, node_b = b->head;
826 node_a = node_a->next, node_b = node_b->next)
828 if (strcmp (node_a->str, node_b->str))
832 /* Catch the case of lists being different lengths, (which
833 * would cause the loop above to terminate after the shorter
835 return node_a == node_b;
839 _argument_list_create(void *ctx)
841 argument_list_t *list;
843 list = ralloc (ctx, argument_list_t);
851 _argument_list_append(argument_list_t *list, token_list_t *argument)
853 argument_node_t *node;
855 node = ralloc (list, argument_node_t);
856 node->argument = argument;
860 if (list->head == NULL) {
863 list->tail->next = node;
870 _argument_list_length(argument_list_t *list)
873 argument_node_t *node;
878 for (node = list->head; node; node = node->next)
885 _argument_list_member_at(argument_list_t *list, int index)
887 argument_node_t *node;
894 for (i = 0; i < index; i++) {
901 return node->argument;
906 /* Note: This function ralloc_steal()s the str pointer. */
908 _token_create_str(void *ctx, int type, char *str)
912 token = ralloc (ctx, token_t);
914 token->value.str = str;
916 ralloc_steal (token, str);
922 _token_create_ival(void *ctx, int type, int ival)
926 token = ralloc (ctx, token_t);
928 token->value.ival = ival;
934 _token_list_create(void *ctx)
938 list = ralloc (ctx, token_list_t);
941 list->non_space_tail = NULL;
947 _token_list_append(token_list_t *list, token_t *token)
951 node = ralloc (list, token_node_t);
955 if (list->head == NULL) {
958 list->tail->next = node;
962 if (token->type != SPACE)
963 list->non_space_tail = node;
967 _token_list_append_list(token_list_t *list, token_list_t *tail)
969 if (tail == NULL || tail->head == NULL)
972 if (list->head == NULL) {
973 list->head = tail->head;
975 list->tail->next = tail->head;
978 list->tail = tail->tail;
979 list->non_space_tail = tail->non_space_tail;
982 static token_list_t *
983 _token_list_copy(void *ctx, token_list_t *other)
991 copy = _token_list_create (ctx);
992 for (node = other->head; node; node = node->next) {
993 token_t *new_token = ralloc (copy, token_t);
994 *new_token = *node->token;
995 _token_list_append (copy, new_token);
1002 _token_list_trim_trailing_space(token_list_t *list)
1004 token_node_t *tail, *next;
1006 if (list->non_space_tail) {
1007 tail = list->non_space_tail->next;
1008 list->non_space_tail->next = NULL;
1009 list->tail = list->non_space_tail;
1020 _token_list_is_empty_ignoring_space(token_list_t *l)
1028 while (n != NULL && n->token->type == SPACE)
1035 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1037 token_node_t *node_a, *node_b;
1039 if (a == NULL || b == NULL) {
1040 int a_empty = _token_list_is_empty_ignoring_space(a);
1041 int b_empty = _token_list_is_empty_ignoring_space(b);
1042 return a_empty == b_empty;
1050 if (node_a == NULL && node_b == NULL)
1053 if (node_a == NULL || node_b == NULL)
1055 /* Make sure whitespace appears in the same places in both.
1056 * It need not be exactly the same amount of whitespace,
1059 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1060 while (node_a && node_a->token->type == SPACE)
1061 node_a = node_a->next;
1062 while (node_b && node_b->token->type == SPACE)
1063 node_b = node_b->next;
1067 if (node_a->token->type != node_b->token->type)
1070 switch (node_a->token->type) {
1072 if (node_a->token->value.ival != node_b->token->value.ival) {
1077 case INTEGER_STRING:
1079 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1085 node_a = node_a->next;
1086 node_b = node_b->next;
1093 _token_print(char **out, size_t *len, token_t *token)
1095 if (token->type < 256) {
1096 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1100 switch (token->type) {
1102 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1105 case INTEGER_STRING:
1107 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1110 ralloc_asprintf_rewrite_tail (out, len, " ");
1113 ralloc_asprintf_rewrite_tail (out, len, "<<");
1116 ralloc_asprintf_rewrite_tail (out, len, ">>");
1119 ralloc_asprintf_rewrite_tail (out, len, "<=");
1121 case GREATER_OR_EQUAL:
1122 ralloc_asprintf_rewrite_tail (out, len, ">=");
1125 ralloc_asprintf_rewrite_tail (out, len, "==");
1128 ralloc_asprintf_rewrite_tail (out, len, "!=");
1131 ralloc_asprintf_rewrite_tail (out, len, "&&");
1134 ralloc_asprintf_rewrite_tail (out, len, "||");
1137 ralloc_asprintf_rewrite_tail (out, len, "##");
1140 ralloc_asprintf_rewrite_tail (out, len, "++");
1143 ralloc_asprintf_rewrite_tail (out, len, "--");
1146 ralloc_asprintf_rewrite_tail (out, len, "defined");
1149 /* Nothing to print. */
1152 assert(!"Error: Don't know how to print token.");
1158 /* Return a new token (ralloc()ed off of 'token') formed by pasting
1159 * 'token' and 'other'. Note that this function may return 'token' or
1160 * 'other' directly rather than allocating anything new.
1162 * Caution: Only very cursory error-checking is performed to see if
1163 * the final result is a valid single token. */
1165 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1167 token_t *combined = NULL;
1169 /* Pasting a placeholder onto anything makes no change. */
1170 if (other->type == PLACEHOLDER)
1173 /* When 'token' is a placeholder, just return 'other'. */
1174 if (token->type == PLACEHOLDER)
1177 /* A very few single-character punctuators can be combined
1178 * with another to form a multi-character punctuator. */
1179 switch (token->type) {
1181 if (other->type == '<')
1182 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
1183 else if (other->type == '=')
1184 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
1187 if (other->type == '>')
1188 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
1189 else if (other->type == '=')
1190 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1193 if (other->type == '=')
1194 combined = _token_create_ival (token, EQUAL, EQUAL);
1197 if (other->type == '=')
1198 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
1201 if (other->type == '&')
1202 combined = _token_create_ival (token, AND, AND);
1205 if (other->type == '|')
1206 combined = _token_create_ival (token, OR, OR);
1210 if (combined != NULL) {
1211 /* Inherit the location from the first token */
1212 combined->location = token->location;
1216 /* Two string-valued (or integer) tokens can usually just be
1217 * mashed together. (We also handle a string followed by an
1218 * integer here as well.)
1220 * There are some exceptions here. Notably, if the first token
1221 * is an integer (or a string representing an integer), then
1222 * the second token must also be an integer or must be a
1223 * string representing an integer that begins with a digit.
1225 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1226 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1231 /* Check that pasting onto an integer doesn't create a
1232 * non-integer, (that is, only digits can be
1234 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1235 switch (other->type) {
1236 case INTEGER_STRING:
1237 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1241 if (other->value.ival < 0)
1249 if (token->type == INTEGER)
1250 str = ralloc_asprintf (token, "%" PRIiMAX, token->value.ival);
1252 str = ralloc_strdup (token, token->value.str);
1254 if (other->type == INTEGER)
1255 ralloc_asprintf_append (&str, "%" PRIiMAX, other->value.ival);
1257 ralloc_strcat (&str, other->value.str);
1259 /* New token is same type as original token, unless we
1260 * started with an integer, in which case we will be
1261 * creating an integer-string. */
1262 combined_type = token->type;
1263 if (combined_type == INTEGER)
1264 combined_type = INTEGER_STRING;
1266 combined = _token_create_str (token, combined_type, str);
1267 combined->location = token->location;
1272 glcpp_error (&token->location, parser, "");
1273 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1274 _token_print (&parser->info_log, &parser->info_log_length, token);
1275 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1276 _token_print (&parser->info_log, &parser->info_log_length, other);
1277 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1283 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1290 for (node = list->head; node; node = node->next)
1291 _token_print (&parser->output, &parser->output_length, node->token);
1295 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1297 glcpp_error(locp, parser, "%s", error);
1301 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1306 tok = _token_create_ival (parser, INTEGER, value);
1308 list = _token_list_create(parser);
1309 _token_list_append(list, tok);
1310 _define_object_macro(parser, NULL, name, list);
1314 glcpp_parser_create(const struct gl_extensions *extensions, gl_api api)
1316 glcpp_parser_t *parser;
1318 parser = ralloc (NULL, glcpp_parser_t);
1320 glcpp_lex_init_extra (parser, &parser->scanner);
1321 parser->defines = hash_table_ctor(32, hash_table_string_hash,
1322 hash_table_string_compare);
1323 parser->active = NULL;
1324 parser->lexing_directive = 0;
1325 parser->space_tokens = 1;
1326 parser->last_token_was_newline = 0;
1327 parser->last_token_was_space = 0;
1328 parser->first_non_space_token_this_line = 1;
1329 parser->newline_as_space = 0;
1330 parser->in_control_line = 0;
1331 parser->paren_count = 0;
1332 parser->commented_newlines = 0;
1334 parser->skip_stack = NULL;
1335 parser->skipping = 0;
1337 parser->lex_from_list = NULL;
1338 parser->lex_from_node = NULL;
1340 parser->output = ralloc_strdup(parser, "");
1341 parser->output_length = 0;
1342 parser->info_log = ralloc_strdup(parser, "");
1343 parser->info_log_length = 0;
1346 parser->extensions = extensions;
1348 parser->version_resolved = false;
1350 parser->has_new_line_number = 0;
1351 parser->new_line_number = 1;
1352 parser->has_new_source_number = 0;
1353 parser->new_source_number = 0;
1359 glcpp_parser_destroy(glcpp_parser_t *parser)
1361 glcpp_lex_destroy (parser->scanner);
1362 hash_table_dtor (parser->defines);
1363 ralloc_free (parser);
1366 typedef enum function_status
1368 FUNCTION_STATUS_SUCCESS,
1369 FUNCTION_NOT_A_FUNCTION,
1370 FUNCTION_UNBALANCED_PARENTHESES
1371 } function_status_t;
1373 /* Find a set of function-like macro arguments by looking for a
1374 * balanced set of parentheses.
1376 * When called, 'node' should be the opening-parenthesis token, (or
1377 * perhaps preceeding SPACE tokens). Upon successful return *last will
1378 * be the last consumed node, (corresponding to the closing right
1383 * FUNCTION_STATUS_SUCCESS:
1385 * Successfully parsed a set of function arguments.
1387 * FUNCTION_NOT_A_FUNCTION:
1389 * Macro name not followed by a '('. This is not an error, but
1390 * simply that the macro name should be treated as a non-macro.
1392 * FUNCTION_UNBALANCED_PARENTHESES
1394 * Macro name is not followed by a balanced set of parentheses.
1396 static function_status_t
1397 _arguments_parse(argument_list_t *arguments, token_node_t *node,
1398 token_node_t **last)
1400 token_list_t *argument;
1405 /* Ignore whitespace before first parenthesis. */
1406 while (node && node->token->type == SPACE)
1409 if (node == NULL || node->token->type != '(')
1410 return FUNCTION_NOT_A_FUNCTION;
1414 argument = _token_list_create (arguments);
1415 _argument_list_append (arguments, argument);
1417 for (paren_count = 1; node; node = node->next) {
1418 if (node->token->type == '(') {
1420 } else if (node->token->type == ')') {
1422 if (paren_count == 0)
1426 if (node->token->type == ',' && paren_count == 1) {
1427 _token_list_trim_trailing_space (argument);
1428 argument = _token_list_create (arguments);
1429 _argument_list_append (arguments, argument);
1431 if (argument->head == NULL) {
1432 /* Don't treat initial whitespace as part of the argument. */
1433 if (node->token->type == SPACE)
1436 _token_list_append (argument, node->token);
1441 return FUNCTION_UNBALANCED_PARENTHESES;
1445 return FUNCTION_STATUS_SUCCESS;
1448 static token_list_t *
1449 _token_list_create_with_one_ival(void *ctx, int type, int ival)
1454 list = _token_list_create(ctx);
1455 node = _token_create_ival(list, type, ival);
1456 _token_list_append(list, node);
1461 static token_list_t *
1462 _token_list_create_with_one_space(void *ctx)
1464 return _token_list_create_with_one_ival(ctx, SPACE, SPACE);
1467 static token_list_t *
1468 _token_list_create_with_one_integer(void *ctx, int ival)
1470 return _token_list_create_with_one_ival(ctx, INTEGER, ival);
1473 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1475 * Note: This function must only be called when "node" is a DEFINED token,
1476 * (and will abort with an assertion failure otherwise).
1478 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1479 * (optionally preceded and followed by '(' and ')' tokens) then the following
1482 * If the identifier is a defined macro, this function returns 1.
1484 * If the identifier is not a defined macro, this function returns 0.
1486 * In either case, *last will be updated to the last node in the list
1487 * consumed by the evaluation, (either the token of the identifier or the
1488 * token of the closing parenthesis).
1490 * In all other cases, (such as "node is the final node of the list", or
1491 * "missing closing parenthesis", etc.), this function generates a
1492 * preprocessor error, returns -1 and *last will not be set.
1495 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1496 token_node_t **last)
1498 token_node_t *argument, *defined = node;
1500 assert(node->token->type == DEFINED);
1504 /* Ignore whitespace after DEFINED token. */
1505 while (node && node->token->type == SPACE)
1511 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1513 } else if (node->token->type == '(') {
1516 /* Ignore whitespace after '(' token. */
1517 while (node && node->token->type == SPACE)
1520 if (node == NULL || (node->token->type != IDENTIFIER &&
1521 node->token->type != OTHER)) {
1529 /* Ignore whitespace after identifier, before ')' token. */
1530 while (node && node->token->type == SPACE)
1533 if (node == NULL || node->token->type != ')')
1541 return hash_table_find(parser->defines,
1542 argument->token->value.str) ? 1 : 0;
1545 glcpp_error (&defined->token->location, parser,
1546 "\"defined\" not followed by an identifier");
1550 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1553 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1556 token_node_t *node, *node_prev, *replacement, *last = NULL;
1567 if (node->token->type != DEFINED)
1570 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1574 replacement = ralloc (list, token_node_t);
1575 replacement->token = _token_create_ival (list, INTEGER, value);
1577 /* Splice replacement node into list, replacing from "node"
1578 * through "last". */
1580 node_prev->next = replacement;
1582 list->head = replacement;
1583 replacement->next = last->next;
1584 if (last == list->tail)
1585 list->tail = replacement;
1595 /* Perform macro expansion on 'list', placing the resulting tokens
1596 * into a new list which is initialized with a first token of type
1597 * 'head_token_type'. Then begin lexing from the resulting list,
1598 * (return to the current lexing source when this list is exhausted).
1600 * See the documentation of _glcpp_parser_expand_token_list for a description
1601 * of the "mode" parameter.
1604 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1605 token_list_t *list, expansion_mode_t mode)
1607 token_list_t *expanded;
1610 expanded = _token_list_create (parser);
1611 token = _token_create_ival (parser, head_token_type, head_token_type);
1612 _token_list_append (expanded, token);
1613 _glcpp_parser_expand_token_list (parser, list, mode);
1614 _token_list_append_list (expanded, list);
1615 glcpp_parser_lex_from (parser, expanded);
1619 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1625 token_node_t *next_non_space;
1627 /* Look ahead for a PASTE token, skipping space. */
1628 next_non_space = node->next;
1629 while (next_non_space && next_non_space->token->type == SPACE)
1630 next_non_space = next_non_space->next;
1632 if (next_non_space == NULL)
1635 if (next_non_space->token->type != PASTE) {
1636 node = next_non_space;
1640 /* Now find the next non-space token after the PASTE. */
1641 next_non_space = next_non_space->next;
1642 while (next_non_space && next_non_space->token->type == SPACE)
1643 next_non_space = next_non_space->next;
1645 if (next_non_space == NULL) {
1646 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1650 node->token = _token_paste(parser, node->token, next_non_space->token);
1651 node->next = next_non_space->next;
1652 if (next_non_space == list->tail)
1656 list->non_space_tail = list->tail;
1659 /* This is a helper function that's essentially part of the
1660 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1661 * except for by that function.
1663 * Returns NULL if node is a simple token with no expansion, (that is,
1664 * although 'node' corresponds to an identifier defined as a
1665 * function-like macro, it is not followed with a parenthesized
1668 * Compute the complete expansion of node (which is a function-like
1669 * macro) and subsequent nodes which are arguments.
1671 * Returns the token list that results from the expansion and sets
1672 * *last to the last node in the list that was consumed by the
1673 * expansion. Specifically, *last will be set as follows: as the
1674 * token of the closing right parenthesis.
1676 * See the documentation of _glcpp_parser_expand_token_list for a description
1677 * of the "mode" parameter.
1679 static token_list_t *
1680 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1681 token_node_t **last, expansion_mode_t mode)
1684 const char *identifier;
1685 argument_list_t *arguments;
1686 function_status_t status;
1687 token_list_t *substituted;
1688 int parameter_index;
1690 identifier = node->token->value.str;
1692 macro = hash_table_find(parser->defines, identifier);
1694 assert(macro->is_function);
1696 arguments = _argument_list_create(parser);
1697 status = _arguments_parse(arguments, node, last);
1700 case FUNCTION_STATUS_SUCCESS:
1702 case FUNCTION_NOT_A_FUNCTION:
1704 case FUNCTION_UNBALANCED_PARENTHESES:
1705 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1709 /* Replace a macro defined as empty with a SPACE token. */
1710 if (macro->replacements == NULL) {
1711 ralloc_free(arguments);
1712 return _token_list_create_with_one_space(parser);
1715 if (!((_argument_list_length (arguments) ==
1716 _string_list_length (macro->parameters)) ||
1717 (_string_list_length (macro->parameters) == 0 &&
1718 _argument_list_length (arguments) == 1 &&
1719 arguments->head->argument->head == NULL))) {
1720 glcpp_error(&node->token->location, parser,
1721 "Error: macro %s invoked with %d arguments (expected %d)\n",
1722 identifier, _argument_list_length (arguments),
1723 _string_list_length(macro->parameters));
1727 /* Perform argument substitution on the replacement list. */
1728 substituted = _token_list_create(arguments);
1730 for (node = macro->replacements->head; node; node = node->next) {
1731 if (node->token->type == IDENTIFIER &&
1732 _string_list_contains(macro->parameters, node->token->value.str,
1733 ¶meter_index)) {
1734 token_list_t *argument;
1735 argument = _argument_list_member_at(arguments, parameter_index);
1736 /* Before substituting, we expand the argument tokens, or append a
1737 * placeholder token for an empty argument. */
1738 if (argument->head) {
1739 token_list_t *expanded_argument;
1740 expanded_argument = _token_list_copy(parser, argument);
1741 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1742 _token_list_append_list(substituted, expanded_argument);
1746 new_token = _token_create_ival(substituted, PLACEHOLDER,
1748 _token_list_append(substituted, new_token);
1751 _token_list_append(substituted, node->token);
1755 /* After argument substitution, and before further expansion
1756 * below, implement token pasting. */
1758 _token_list_trim_trailing_space(substituted);
1760 _glcpp_parser_apply_pastes(parser, substituted);
1765 /* Compute the complete expansion of node, (and subsequent nodes after
1766 * 'node' in the case that 'node' is a function-like macro and
1767 * subsequent nodes are arguments).
1769 * Returns NULL if node is a simple token with no expansion.
1771 * Otherwise, returns the token list that results from the expansion
1772 * and sets *last to the last node in the list that was consumed by
1773 * the expansion. Specifically, *last will be set as follows:
1775 * As 'node' in the case of object-like macro expansion.
1777 * As the token of the closing right parenthesis in the case of
1778 * function-like macro expansion.
1780 * See the documentation of _glcpp_parser_expand_token_list for a description
1781 * of the "mode" parameter.
1783 static token_list_t *
1784 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1785 token_node_t **last, expansion_mode_t mode)
1787 token_t *token = node->token;
1788 const char *identifier;
1791 /* We only expand identifiers */
1792 if (token->type != IDENTIFIER) {
1797 identifier = token->value.str;
1799 /* Special handling for __LINE__ and __FILE__, (not through
1800 * the hash table). */
1801 if (strcmp(identifier, "__LINE__") == 0)
1802 return _token_list_create_with_one_integer(parser, node->token->location.first_line);
1804 if (strcmp(identifier, "__FILE__") == 0)
1805 return _token_list_create_with_one_integer(parser, node->token->location.source);
1807 /* Look up this identifier in the hash table. */
1808 macro = hash_table_find(parser->defines, identifier);
1810 /* Not a macro, so no expansion needed. */
1814 /* Finally, don't expand this macro if we're already actively
1815 * expanding it, (to avoid infinite recursion). */
1816 if (_parser_active_list_contains (parser, identifier)) {
1817 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1818 * future expansion of this unexpanded token. */
1820 token_list_t *expansion;
1823 str = ralloc_strdup(parser, token->value.str);
1824 final = _token_create_str(parser, OTHER, str);
1825 expansion = _token_list_create(parser);
1826 _token_list_append(expansion, final);
1830 if (! macro->is_function) {
1831 token_list_t *replacement;
1833 /* Replace a macro defined as empty with a SPACE token. */
1834 if (macro->replacements == NULL)
1835 return _token_list_create_with_one_space(parser);
1837 replacement = _token_list_copy(parser, macro->replacements);
1838 _glcpp_parser_apply_pastes(parser, replacement);
1842 return _glcpp_parser_expand_function(parser, node, last, mode);
1845 /* Push a new identifier onto the parser's active list.
1847 * Here, 'marker' is the token node that appears in the list after the
1848 * expansion of 'identifier'. That is, when the list iterator begins
1849 * examining 'marker', then it is time to pop this node from the
1853 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
1854 token_node_t *marker)
1856 active_list_t *node;
1858 node = ralloc(parser->active, active_list_t);
1859 node->identifier = ralloc_strdup(node, identifier);
1860 node->marker = marker;
1861 node->next = parser->active;
1863 parser->active = node;
1867 _parser_active_list_pop(glcpp_parser_t *parser)
1869 active_list_t *node = parser->active;
1872 parser->active = NULL;
1876 node = parser->active->next;
1877 ralloc_free (parser->active);
1879 parser->active = node;
1883 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
1885 active_list_t *node;
1887 if (parser->active == NULL)
1890 for (node = parser->active; node; node = node->next)
1891 if (strcmp(node->identifier, identifier) == 0)
1897 /* Walk over the token list replacing nodes with their expansion.
1898 * Whenever nodes are expanded the walking will walk over the new
1899 * nodes, continuing to expand as necessary. The results are placed in
1902 * The "mode" argument controls the handling of any DEFINED tokens that
1903 * result from expansion as follows:
1905 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
1906 * left in the final list, unevaluated. This is the correct mode
1907 * for expanding any list in any context other than a
1908 * preprocessor conditional, (#if or #elif).
1910 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
1911 * evaluated to 0 or 1 tokens depending on whether the following
1912 * token is the name of a defined macro. If the DEFINED token is
1913 * not followed by an (optionally parenthesized) identifier, then
1914 * an error will be generated. This the correct mode for
1915 * expanding any list in the context of a preprocessor
1916 * conditional, (#if or #elif).
1919 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
1920 expansion_mode_t mode)
1922 token_node_t *node_prev;
1923 token_node_t *node, *last = NULL;
1924 token_list_t *expansion;
1925 active_list_t *active_initial = parser->active;
1930 _token_list_trim_trailing_space (list);
1935 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
1936 _glcpp_parser_evaluate_defined_in_list (parser, list);
1940 while (parser->active && parser->active->marker == node)
1941 _parser_active_list_pop (parser);
1943 expansion = _glcpp_parser_expand_node (parser, node, &last, mode);
1947 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
1948 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
1951 for (n = node; n != last->next; n = n->next)
1952 while (parser->active && parser->active->marker == n) {
1953 _parser_active_list_pop (parser);
1956 _parser_active_list_push(parser, node->token->value.str, last->next);
1958 /* Splice expansion into list, supporting a simple deletion if the
1959 * expansion is empty.
1961 if (expansion->head) {
1963 node_prev->next = expansion->head;
1965 list->head = expansion->head;
1966 expansion->tail->next = last->next;
1967 if (last == list->tail)
1968 list->tail = expansion->tail;
1971 node_prev->next = last->next;
1973 list->head = last->next;
1974 if (last == list->tail)
1980 node = node_prev ? node_prev->next : list->head;
1983 /* Remove any lingering effects of this invocation on the
1984 * active list. That is, pop until the list looks like it did
1985 * at the beginning of this function. */
1986 while (parser->active && parser->active != active_initial)
1987 _parser_active_list_pop (parser);
1989 list->non_space_tail = list->tail;
1993 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
1999 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2001 _token_list_trim_trailing_space (list);
2003 _token_list_print (parser, list);
2007 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2008 const char *identifier)
2010 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2011 * the GLSL ES spec (all versions) say:
2013 * "All macro names containing two consecutive underscores ( __ )
2014 * are reserved for future use as predefined macro names. All
2015 * macro names prefixed with "GL_" ("GL" followed by a single
2016 * underscore) are also reserved."
2018 * The intention is that names containing __ are reserved for internal
2019 * use by the implementation, and names prefixed with GL_ are reserved
2020 * for use by Khronos. Since every extension adds a name prefixed
2021 * with GL_ (i.e., the name of the extension), that should be an
2022 * error. Names simply containing __ are dangerous to use, but should
2025 * A future version of the GLSL specification will clarify this.
2027 if (strstr(identifier, "__")) {
2028 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2029 "for use by the implementation.\n");
2031 if (strncmp(identifier, "GL_", 3) == 0) {
2032 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2034 if (strcmp(identifier, "defined") == 0) {
2035 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2040 _macro_equal(macro_t *a, macro_t *b)
2042 if (a->is_function != b->is_function)
2045 if (a->is_function) {
2046 if (! _string_list_equal (a->parameters, b->parameters))
2050 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2054 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2055 const char *identifier, token_list_t *replacements)
2057 macro_t *macro, *previous;
2059 /* We define pre-defined macros before we've started parsing the actual
2060 * file. So if there's no location defined yet, that's what were doing and
2061 * we don't want to generate an error for using the reserved names. */
2063 _check_for_reserved_macro_name(parser, loc, identifier);
2065 macro = ralloc (parser, macro_t);
2067 macro->is_function = 0;
2068 macro->parameters = NULL;
2069 macro->identifier = ralloc_strdup (macro, identifier);
2070 macro->replacements = replacements;
2071 ralloc_steal (macro, replacements);
2073 previous = hash_table_find (parser->defines, identifier);
2075 if (_macro_equal (macro, previous)) {
2076 ralloc_free (macro);
2079 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2082 hash_table_insert (parser->defines, macro, identifier);
2086 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2087 const char *identifier, string_list_t *parameters,
2088 token_list_t *replacements)
2090 macro_t *macro, *previous;
2093 _check_for_reserved_macro_name(parser, loc, identifier);
2095 /* Check for any duplicate parameter names. */
2096 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2097 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2100 macro = ralloc (parser, macro_t);
2101 ralloc_steal (macro, parameters);
2102 ralloc_steal (macro, replacements);
2104 macro->is_function = 1;
2105 macro->parameters = parameters;
2106 macro->identifier = ralloc_strdup (macro, identifier);
2107 macro->replacements = replacements;
2108 previous = hash_table_find (parser->defines, identifier);
2110 if (_macro_equal (macro, previous)) {
2111 ralloc_free (macro);
2114 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2117 hash_table_insert(parser->defines, macro, identifier);
2121 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2126 if (parser->lex_from_list == NULL) {
2127 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2129 /* XXX: This ugly block of code exists for the sole
2130 * purpose of converting a NEWLINE token into a SPACE
2131 * token, but only in the case where we have seen a
2132 * function-like macro name, but have not yet seen its
2133 * closing parenthesis.
2135 * There's perhaps a more compact way to do this with
2136 * mid-rule actions in the grammar.
2138 * I'm definitely not pleased with the complexity of
2141 if (parser->newline_as_space) {
2143 parser->paren_count++;
2144 } else if (ret == ')') {
2145 parser->paren_count--;
2146 if (parser->paren_count == 0)
2147 parser->newline_as_space = 0;
2148 } else if (ret == NEWLINE) {
2150 } else if (ret != SPACE) {
2151 if (parser->paren_count == 0)
2152 parser->newline_as_space = 0;
2154 } else if (parser->in_control_line) {
2156 parser->in_control_line = 0;
2158 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2159 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2160 ret == ENDIF || ret == HASH_TOKEN) {
2161 parser->in_control_line = 1;
2162 } else if (ret == IDENTIFIER) {
2164 macro = hash_table_find (parser->defines,
2166 if (macro && macro->is_function) {
2167 parser->newline_as_space = 1;
2168 parser->paren_count = 0;
2175 node = parser->lex_from_node;
2178 ralloc_free (parser->lex_from_list);
2179 parser->lex_from_list = NULL;
2183 *yylval = node->token->value;
2184 ret = node->token->type;
2186 parser->lex_from_node = node->next;
2192 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2196 assert (parser->lex_from_list == NULL);
2198 /* Copy list, eliminating any space tokens. */
2199 parser->lex_from_list = _token_list_create (parser);
2201 for (node = list->head; node; node = node->next) {
2202 if (node->token->type == SPACE)
2204 _token_list_append (parser->lex_from_list, node->token);
2209 parser->lex_from_node = parser->lex_from_list->head;
2211 /* It's possible the list consisted of nothing but whitespace. */
2212 if (parser->lex_from_node == NULL) {
2213 ralloc_free (parser->lex_from_list);
2214 parser->lex_from_list = NULL;
2219 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2222 skip_type_t current = SKIP_NO_SKIP;
2225 if (parser->skip_stack)
2226 current = parser->skip_stack->type;
2228 node = ralloc (parser, skip_node_t);
2231 if (current == SKIP_NO_SKIP) {
2233 node->type = SKIP_NO_SKIP;
2235 node->type = SKIP_TO_ELSE;
2237 node->type = SKIP_TO_ENDIF;
2240 node->has_else = false;
2241 node->next = parser->skip_stack;
2242 parser->skip_stack = node;
2246 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2247 const char *type, int condition)
2249 if (parser->skip_stack == NULL) {
2250 glcpp_error (loc, parser, "#%s without #if\n", type);
2254 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2256 parser->skip_stack->type = SKIP_NO_SKIP;
2258 parser->skip_stack->type = SKIP_TO_ENDIF;
2263 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2267 if (parser->skip_stack == NULL) {
2268 glcpp_error (loc, parser, "#endif without #if\n");
2272 node = parser->skip_stack;
2273 parser->skip_stack = node->next;
2278 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2279 const char *es_identifier,
2280 bool explicitly_set)
2282 const struct gl_extensions *extensions = parser->extensions;
2284 if (parser->version_resolved)
2287 parser->version_resolved = true;
2289 add_builtin_define (parser, "__VERSION__", version);
2291 parser->is_gles = (version == 100) ||
2292 (es_identifier && (strcmp(es_identifier, "es") == 0));
2294 /* Add pre-defined macros. */
2295 if (parser->is_gles) {
2296 add_builtin_define(parser, "GL_ES", 1);
2297 add_builtin_define(parser, "GL_EXT_separate_shader_objects", 1);
2298 add_builtin_define(parser, "GL_EXT_draw_buffers", 1);
2300 if (extensions != NULL) {
2301 if (extensions->OES_EGL_image_external)
2302 add_builtin_define(parser, "GL_OES_EGL_image_external", 1);
2303 if (extensions->OES_sample_variables) {
2304 add_builtin_define(parser, "GL_OES_sample_variables", 1);
2305 add_builtin_define(parser, "GL_OES_shader_multisample_interpolation", 1);
2307 if (extensions->OES_standard_derivatives)
2308 add_builtin_define(parser, "GL_OES_standard_derivatives", 1);
2309 if (extensions->ARB_texture_multisample)
2310 add_builtin_define(parser, "GL_OES_texture_storage_multisample_2d_array", 1);
2311 if (extensions->ARB_blend_func_extended)
2312 add_builtin_define(parser, "GL_EXT_blend_func_extended", 1);
2314 if (version >= 310) {
2315 if (extensions->ARB_shader_image_load_store)
2316 add_builtin_define(parser, "GL_OES_shader_image_atomic", 1);
2318 if (extensions->OES_geometry_shader) {
2319 add_builtin_define(parser, "GL_OES_geometry_point_size", 1);
2320 add_builtin_define(parser, "GL_OES_geometry_shader", 1);
2322 if (extensions->ARB_gpu_shader5) {
2323 add_builtin_define(parser, "GL_EXT_gpu_shader5", 1);
2324 add_builtin_define(parser, "GL_OES_gpu_shader5", 1);
2326 if (extensions->OES_texture_buffer) {
2327 add_builtin_define(parser, "GL_EXT_texture_buffer", 1);
2328 add_builtin_define(parser, "GL_OES_texture_buffer", 1);
2333 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
2334 add_builtin_define(parser, "GL_ARB_enhanced_layouts", 1);
2335 add_builtin_define(parser, "GL_ARB_separate_shader_objects", 1);
2336 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
2337 add_builtin_define(parser, "GL_AMD_shader_trinary_minmax", 1);
2339 if (extensions != NULL) {
2340 if (extensions->EXT_texture_array)
2341 add_builtin_define(parser, "GL_EXT_texture_array", 1);
2343 if (extensions->ARB_ES3_1_compatibility)
2344 add_builtin_define(parser, "GL_ARB_ES3_1_compatibility", 1);
2346 if (extensions->ARB_arrays_of_arrays)
2347 add_builtin_define(parser, "GL_ARB_arrays_of_arrays", 1);
2349 if (extensions->ARB_fragment_coord_conventions) {
2350 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
2354 if (extensions->ARB_fragment_layer_viewport)
2355 add_builtin_define(parser, "GL_ARB_fragment_layer_viewport", 1);
2357 if (extensions->ARB_explicit_attrib_location)
2358 add_builtin_define(parser, "GL_ARB_explicit_attrib_location", 1);
2360 if (extensions->ARB_explicit_uniform_location)
2361 add_builtin_define(parser, "GL_ARB_explicit_uniform_location", 1);
2363 if (extensions->ARB_shader_texture_lod)
2364 add_builtin_define(parser, "GL_ARB_shader_texture_lod", 1);
2366 if (extensions->ARB_draw_instanced)
2367 add_builtin_define(parser, "GL_ARB_draw_instanced", 1);
2369 if (extensions->ARB_conservative_depth) {
2370 add_builtin_define(parser, "GL_AMD_conservative_depth", 1);
2371 add_builtin_define(parser, "GL_ARB_conservative_depth", 1);
2374 if (extensions->ARB_shader_bit_encoding)
2375 add_builtin_define(parser, "GL_ARB_shader_bit_encoding", 1);
2377 if (extensions->ARB_shader_clock)
2378 add_builtin_define(parser, "GL_ARB_shader_clock", 1);
2380 if (extensions->ARB_uniform_buffer_object)
2381 add_builtin_define(parser, "GL_ARB_uniform_buffer_object", 1);
2383 if (extensions->ARB_texture_cube_map_array)
2384 add_builtin_define(parser, "GL_ARB_texture_cube_map_array", 1);
2386 if (extensions->ARB_shading_language_packing)
2387 add_builtin_define(parser, "GL_ARB_shading_language_packing", 1);
2389 if (extensions->ARB_texture_multisample)
2390 add_builtin_define(parser, "GL_ARB_texture_multisample", 1);
2392 if (extensions->ARB_texture_query_levels)
2393 add_builtin_define(parser, "GL_ARB_texture_query_levels", 1);
2395 if (extensions->ARB_texture_query_lod)
2396 add_builtin_define(parser, "GL_ARB_texture_query_lod", 1);
2398 if (extensions->ARB_gpu_shader5)
2399 add_builtin_define(parser, "GL_ARB_gpu_shader5", 1);
2401 if (extensions->ARB_gpu_shader_fp64)
2402 add_builtin_define(parser, "GL_ARB_gpu_shader_fp64", 1);
2404 if (extensions->ARB_vertex_attrib_64bit)
2405 add_builtin_define(parser, "GL_ARB_vertex_attrib_64bit", 1);
2407 if (extensions->AMD_vertex_shader_layer)
2408 add_builtin_define(parser, "GL_AMD_vertex_shader_layer", 1);
2410 if (extensions->AMD_vertex_shader_viewport_index)
2411 add_builtin_define(parser, "GL_AMD_vertex_shader_viewport_index", 1);
2413 if (extensions->ARB_shading_language_420pack)
2414 add_builtin_define(parser, "GL_ARB_shading_language_420pack", 1);
2416 if (extensions->ARB_sample_shading)
2417 add_builtin_define(parser, "GL_ARB_sample_shading", 1);
2419 if (extensions->ARB_texture_gather)
2420 add_builtin_define(parser, "GL_ARB_texture_gather", 1);
2422 if (extensions->ARB_shader_atomic_counters)
2423 add_builtin_define(parser, "GL_ARB_shader_atomic_counters", 1);
2425 if (extensions->ARB_shader_atomic_counter_ops)
2426 add_builtin_define(parser, "GL_ARB_shader_atomic_counter_ops", 1);
2428 if (extensions->ARB_viewport_array)
2429 add_builtin_define(parser, "GL_ARB_viewport_array", 1);
2431 if (extensions->ARB_compute_shader)
2432 add_builtin_define(parser, "GL_ARB_compute_shader", 1);
2434 if (extensions->ARB_shader_image_load_store)
2435 add_builtin_define(parser, "GL_ARB_shader_image_load_store", 1);
2437 if (extensions->ARB_shader_image_size)
2438 add_builtin_define(parser, "GL_ARB_shader_image_size", 1);
2440 if (extensions->ARB_shader_texture_image_samples)
2441 add_builtin_define(parser, "GL_ARB_shader_texture_image_samples", 1);
2443 if (extensions->ARB_derivative_control)
2444 add_builtin_define(parser, "GL_ARB_derivative_control", 1);
2446 if (extensions->ARB_shader_precision)
2447 add_builtin_define(parser, "GL_ARB_shader_precision", 1);
2449 if (extensions->ARB_shader_storage_buffer_object)
2450 add_builtin_define(parser, "GL_ARB_shader_storage_buffer_object", 1);
2452 if (extensions->ARB_tessellation_shader)
2453 add_builtin_define(parser, "GL_ARB_tessellation_shader", 1);
2455 if (extensions->ARB_shader_subroutine)
2456 add_builtin_define(parser, "GL_ARB_shader_subroutine", 1);
2458 if (extensions->ARB_shader_draw_parameters)
2459 add_builtin_define(parser, "GL_ARB_shader_draw_parameters", 1);
2461 if (extensions->ARB_cull_distance)
2462 add_builtin_define(parser, "GL_ARB_cull_distance", 1);
2466 if (extensions != NULL) {
2467 if (extensions->EXT_shader_integer_mix)
2468 add_builtin_define(parser, "GL_EXT_shader_integer_mix", 1);
2470 if (extensions->EXT_shader_samples_identical)
2471 add_builtin_define(parser, "GL_EXT_shader_samples_identical", 1);
2475 add_builtin_define(parser, "GL_core_profile", 1);
2477 /* Currently, all ES2/ES3 implementations support highp in the
2478 * fragment shader, so we always define this macro in ES2/ES3.
2479 * If we ever get a driver that doesn't support highp, we'll
2480 * need to add a flag to the gl_context and check that here.
2482 if (version >= 130 || parser->is_gles)
2483 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2485 if (explicitly_set) {
2486 ralloc_asprintf_rewrite_tail(&parser->output, &parser->output_length,
2487 "#version %" PRIiMAX "%s%s", version,
2488 es_identifier ? " " : "",
2489 es_identifier ? es_identifier : "");
2493 /* GLSL version if no version is explicitly specified. */
2494 #define IMPLICIT_GLSL_VERSION 110
2496 /* GLSL ES version if no version is explicitly specified. */
2497 #define IMPLICIT_GLSL_ES_VERSION 100
2500 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2502 int language_version = parser->api == API_OPENGLES2 ?
2503 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2505 _glcpp_parser_handle_version_declaration(parser, language_version,