glcpp: use the linear allocator for most objects
[mesa.git] / src / compiler / glsl / glcpp / glcpp-parse.y
1 %{
2 /*
3 * Copyright © 2010 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <inttypes.h>
30
31 #include "glcpp.h"
32 #include "main/core.h" /* for struct gl_extensions */
33 #include "main/mtypes.h" /* for gl_api enum */
34
35 static void
36 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
37
38 static void
39 _define_object_macro(glcpp_parser_t *parser,
40 YYLTYPE *loc,
41 const char *macro,
42 token_list_t *replacements);
43
44 static void
45 _define_function_macro(glcpp_parser_t *parser,
46 YYLTYPE *loc,
47 const char *macro,
48 string_list_t *parameters,
49 token_list_t *replacements);
50
51 static string_list_t *
52 _string_list_create(glcpp_parser_t *parser);
53
54 static void
55 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
56 const char *str);
57
58 static int
59 _string_list_contains(string_list_t *list, const char *member, int *index);
60
61 static const char *
62 _string_list_has_duplicate(string_list_t *list);
63
64 static int
65 _string_list_length(string_list_t *list);
66
67 static int
68 _string_list_equal(string_list_t *a, string_list_t *b);
69
70 static argument_list_t *
71 _argument_list_create(glcpp_parser_t *parser);
72
73 static void
74 _argument_list_append(glcpp_parser_t *parser, argument_list_t *list,
75 token_list_t *argument);
76
77 static int
78 _argument_list_length(argument_list_t *list);
79
80 static token_list_t *
81 _argument_list_member_at(argument_list_t *list, int index);
82
83 static token_t *
84 _token_create_str(glcpp_parser_t *parser, int type, char *str);
85
86 static token_t *
87 _token_create_ival(glcpp_parser_t *parser, int type, int ival);
88
89 static token_list_t *
90 _token_list_create(glcpp_parser_t *parser);
91
92 static void
93 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token);
94
95 static void
96 _token_list_append_list(token_list_t *list, token_list_t *tail);
97
98 static int
99 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b);
100
101 static void
102 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
103 token_node_t *marker);
104
105 static void
106 _parser_active_list_pop(glcpp_parser_t *parser);
107
108 static int
109 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier);
110
111 typedef enum {
112 EXPANSION_MODE_IGNORE_DEFINED,
113 EXPANSION_MODE_EVALUATE_DEFINED
114 } expansion_mode_t;
115
116 /* Expand list, and begin lexing from the result (after first
117 * prefixing a token of type 'head_token_type').
118 */
119 static void
120 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
121 token_list_t *list, expansion_mode_t mode);
122
123 /* Perform macro expansion in-place on the given list. */
124 static void
125 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
126 expansion_mode_t mode);
127
128 static void
129 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
130 token_list_t *list);
131
132 static void
133 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
134 int condition);
135
136 static void
137 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
138 const char *type, int condition);
139
140 static void
141 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc);
142
143 static void
144 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
145 const char *ident, bool explicitly_set);
146
147 static int
148 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
149
150 static void
151 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list);
152
153 static void
154 add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
155
156 %}
157
158 %pure-parser
159 %error-verbose
160
161 %locations
162 %initial-action {
163 @$.first_line = 1;
164 @$.first_column = 1;
165 @$.last_line = 1;
166 @$.last_column = 1;
167 @$.source = 0;
168 }
169
170 %parse-param {glcpp_parser_t *parser}
171 %lex-param {glcpp_parser_t *parser}
172
173 %expect 0
174
175 /* We use HASH_TOKEN, DEFINE_TOKEN and VERSION_TOKEN (as opposed to
176 * HASH, DEFINE, and VERSION) to avoid conflicts with other symbols,
177 * (such as the <HASH> and <DEFINE> start conditions in the lexer). */
178 %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 %token PASTE
180 %type <ival> INTEGER operator SPACE integer_constant
181 %type <expression_value> expression
182 %type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER ERROR_TOKEN PRAGMA
183 %type <string_list> identifier_list
184 %type <token> preprocessing_token
185 %type <token_list> pp_tokens replacement_list text_line
186 %left OR
187 %left AND
188 %left '|'
189 %left '^'
190 %left '&'
191 %left EQUAL NOT_EQUAL
192 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
193 %left LEFT_SHIFT RIGHT_SHIFT
194 %left '+' '-'
195 %left '*' '/' '%'
196 %right UNARY
197
198 %debug
199
200 %%
201
202 input:
203 /* empty */
204 | input line
205 ;
206
207 line:
208 control_line
209 | SPACE control_line
210 | text_line {
211 _glcpp_parser_print_expanded_token_list (parser, $1);
212 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
213 }
214 | expanded_line
215 ;
216
217 expanded_line:
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);
222 }
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);
227 }
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",
234 $2);
235 }
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",
244 $2, $3);
245 }
246 ;
247
248 define:
249 OBJ_IDENTIFIER replacement_list NEWLINE {
250 _define_object_macro (parser, & @1, $1, $2);
251 }
252 | FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
253 _define_function_macro (parser, & @1, $1, NULL, $4);
254 }
255 | FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
256 _define_function_macro (parser, & @1, $1, $3, $5);
257 }
258 ;
259
260 control_line:
261 control_line_success {
262 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "\n");
263 }
264 | control_line_error
265 | HASH_TOKEN LINE pp_tokens NEWLINE {
266
267 if (parser->skip_stack == NULL ||
268 parser->skip_stack->type == SKIP_NO_SKIP)
269 {
270 _glcpp_parser_expand_and_lex_from (parser,
271 LINE_EXPANDED, $3,
272 EXPANSION_MODE_IGNORE_DEFINED);
273 }
274 }
275 ;
276
277 control_line_success:
278 HASH_TOKEN DEFINE_TOKEN define
279 | HASH_TOKEN UNDEF IDENTIFIER NEWLINE {
280 struct hash_entry *entry;
281
282 /* Section 3.4 (Preprocessor) of the GLSL ES 3.00 spec says:
283 *
284 * It is an error to undefine or to redefine a built-in
285 * (pre-defined) macro name.
286 *
287 * The GLSL ES 1.00 spec does not contain this text.
288 *
289 * Section 3.3 (Preprocessor) of the GLSL 1.30 spec says:
290 *
291 * #define and #undef functionality are defined as is
292 * standard for C++ preprocessors for macro definitions
293 * both with and without macro parameters.
294 *
295 * At least as far as I can tell GCC allow '#undef __FILE__'.
296 * Furthermore, there are desktop OpenGL conformance tests
297 * that expect '#undef __VERSION__' and '#undef
298 * GL_core_profile' to work.
299 *
300 * Only disallow #undef of pre-defined macros on GLSL ES >=
301 * 3.00 shaders.
302 */
303 if (parser->is_gles &&
304 parser->version >= 300 &&
305 (strcmp("__LINE__", $3) == 0
306 || strcmp("__FILE__", $3) == 0
307 || strcmp("__VERSION__", $3) == 0
308 || strncmp("GL_", $3, 3) == 0))
309 glcpp_error(& @1, parser, "Built-in (pre-defined)"
310 " macro names cannot be undefined.");
311
312 entry = _mesa_hash_table_search (parser->defines, $3);
313 if (entry) {
314 _mesa_hash_table_remove (parser->defines, entry);
315 }
316 }
317 | HASH_TOKEN IF pp_tokens NEWLINE {
318 /* Be careful to only evaluate the 'if' expression if
319 * we are not skipping. When we are skipping, we
320 * simply push a new 0-valued 'if' onto the skip
321 * stack.
322 *
323 * This avoids generating diagnostics for invalid
324 * expressions that are being skipped. */
325 if (parser->skip_stack == NULL ||
326 parser->skip_stack->type == SKIP_NO_SKIP)
327 {
328 _glcpp_parser_expand_and_lex_from (parser,
329 IF_EXPANDED, $3,
330 EXPANSION_MODE_EVALUATE_DEFINED);
331 }
332 else
333 {
334 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
335 parser->skip_stack->type = SKIP_TO_ENDIF;
336 }
337 }
338 | HASH_TOKEN IF NEWLINE {
339 /* #if without an expression is only an error if we
340 * are not skipping */
341 if (parser->skip_stack == NULL ||
342 parser->skip_stack->type == SKIP_NO_SKIP)
343 {
344 glcpp_error(& @1, parser, "#if with no expression");
345 }
346 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
347 }
348 | HASH_TOKEN IFDEF IDENTIFIER junk NEWLINE {
349 struct hash_entry *entry =
350 _mesa_hash_table_search(parser->defines, $3);
351 macro_t *macro = entry ? entry->data : NULL;
352 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
353 }
354 | HASH_TOKEN IFNDEF IDENTIFIER junk NEWLINE {
355 struct hash_entry *entry =
356 _mesa_hash_table_search(parser->defines, $3);
357 macro_t *macro = entry ? entry->data : NULL;
358 _glcpp_parser_skip_stack_push_if (parser, & @3, macro == NULL);
359 }
360 | HASH_TOKEN ELIF pp_tokens NEWLINE {
361 /* Be careful to only evaluate the 'elif' expression
362 * if we are not skipping. When we are skipping, we
363 * simply change to a 0-valued 'elif' on the skip
364 * stack.
365 *
366 * This avoids generating diagnostics for invalid
367 * expressions that are being skipped. */
368 if (parser->skip_stack &&
369 parser->skip_stack->type == SKIP_TO_ELSE)
370 {
371 _glcpp_parser_expand_and_lex_from (parser,
372 ELIF_EXPANDED, $3,
373 EXPANSION_MODE_EVALUATE_DEFINED);
374 }
375 else if (parser->skip_stack &&
376 parser->skip_stack->has_else)
377 {
378 glcpp_error(& @1, parser, "#elif after #else");
379 }
380 else
381 {
382 _glcpp_parser_skip_stack_change_if (parser, & @1,
383 "elif", 0);
384 }
385 }
386 | HASH_TOKEN ELIF NEWLINE {
387 /* #elif without an expression is an error unless we
388 * are skipping. */
389 if (parser->skip_stack &&
390 parser->skip_stack->type == SKIP_TO_ELSE)
391 {
392 glcpp_error(& @1, parser, "#elif with no expression");
393 }
394 else if (parser->skip_stack &&
395 parser->skip_stack->has_else)
396 {
397 glcpp_error(& @1, parser, "#elif after #else");
398 }
399 else
400 {
401 _glcpp_parser_skip_stack_change_if (parser, & @1,
402 "elif", 0);
403 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
404 }
405 }
406 | HASH_TOKEN ELSE { parser->lexing_directive = 1; } NEWLINE {
407 if (parser->skip_stack &&
408 parser->skip_stack->has_else)
409 {
410 glcpp_error(& @1, parser, "multiple #else");
411 }
412 else
413 {
414 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
415 if (parser->skip_stack)
416 parser->skip_stack->has_else = true;
417 }
418 }
419 | HASH_TOKEN ENDIF {
420 _glcpp_parser_skip_stack_pop (parser, & @1);
421 } NEWLINE
422 | HASH_TOKEN VERSION_TOKEN integer_constant NEWLINE {
423 if (parser->version != 0) {
424 glcpp_error(& @1, parser, "#version must appear on the first line");
425 }
426 _glcpp_parser_handle_version_declaration(parser, $3, NULL, true);
427 }
428 | HASH_TOKEN VERSION_TOKEN integer_constant IDENTIFIER NEWLINE {
429 if (parser->version != 0) {
430 glcpp_error(& @1, parser, "#version must appear on the first line");
431 }
432 _glcpp_parser_handle_version_declaration(parser, $3, $4, true);
433 }
434 | HASH_TOKEN NEWLINE {
435 glcpp_parser_resolve_implicit_version(parser);
436 }
437 | HASH_TOKEN PRAGMA NEWLINE {
438 ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#%s", $2);
439 }
440 ;
441
442 control_line_error:
443 HASH_TOKEN ERROR_TOKEN NEWLINE {
444 glcpp_error(& @1, parser, "#%s", $2);
445 }
446 | HASH_TOKEN DEFINE_TOKEN NEWLINE {
447 glcpp_error (& @1, parser, "#define without macro name");
448 }
449 | HASH_TOKEN GARBAGE pp_tokens NEWLINE {
450 glcpp_error (& @1, parser, "Illegal non-directive after #");
451 }
452 ;
453
454 integer_constant:
455 INTEGER_STRING {
456 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
457 $$ = strtoll ($1 + 2, NULL, 16);
458 } else if ($1[0] == '0') {
459 $$ = strtoll ($1, NULL, 8);
460 } else {
461 $$ = strtoll ($1, NULL, 10);
462 }
463 }
464 | INTEGER {
465 $$ = $1;
466 }
467
468 expression:
469 integer_constant {
470 $$.value = $1;
471 $$.undefined_macro = NULL;
472 }
473 | IDENTIFIER {
474 $$.value = 0;
475 if (parser->is_gles)
476 $$.undefined_macro = linear_strdup(parser->linalloc, $1);
477 else
478 $$.undefined_macro = NULL;
479 }
480 | expression OR expression {
481 $$.value = $1.value || $3.value;
482
483 /* Short-circuit: Only flag undefined from right side
484 * if left side evaluates to false.
485 */
486 if ($1.undefined_macro)
487 $$.undefined_macro = $1.undefined_macro;
488 else if (! $1.value)
489 $$.undefined_macro = $3.undefined_macro;
490 }
491 | expression AND expression {
492 $$.value = $1.value && $3.value;
493
494 /* Short-circuit: Only flag undefined from right-side
495 * if left side evaluates to true.
496 */
497 if ($1.undefined_macro)
498 $$.undefined_macro = $1.undefined_macro;
499 else if ($1.value)
500 $$.undefined_macro = $3.undefined_macro;
501 }
502 | expression '|' expression {
503 $$.value = $1.value | $3.value;
504 if ($1.undefined_macro)
505 $$.undefined_macro = $1.undefined_macro;
506 else
507 $$.undefined_macro = $3.undefined_macro;
508 }
509 | expression '^' expression {
510 $$.value = $1.value ^ $3.value;
511 if ($1.undefined_macro)
512 $$.undefined_macro = $1.undefined_macro;
513 else
514 $$.undefined_macro = $3.undefined_macro;
515 }
516 | expression '&' expression {
517 $$.value = $1.value & $3.value;
518 if ($1.undefined_macro)
519 $$.undefined_macro = $1.undefined_macro;
520 else
521 $$.undefined_macro = $3.undefined_macro;
522 }
523 | expression NOT_EQUAL expression {
524 $$.value = $1.value != $3.value;
525 if ($1.undefined_macro)
526 $$.undefined_macro = $1.undefined_macro;
527 else
528 $$.undefined_macro = $3.undefined_macro;
529 }
530 | expression EQUAL expression {
531 $$.value = $1.value == $3.value;
532 if ($1.undefined_macro)
533 $$.undefined_macro = $1.undefined_macro;
534 else
535 $$.undefined_macro = $3.undefined_macro;
536 }
537 | expression GREATER_OR_EQUAL expression {
538 $$.value = $1.value >= $3.value;
539 if ($1.undefined_macro)
540 $$.undefined_macro = $1.undefined_macro;
541 else
542 $$.undefined_macro = $3.undefined_macro;
543 }
544 | expression LESS_OR_EQUAL expression {
545 $$.value = $1.value <= $3.value;
546 if ($1.undefined_macro)
547 $$.undefined_macro = $1.undefined_macro;
548 else
549 $$.undefined_macro = $3.undefined_macro;
550 }
551 | expression '>' expression {
552 $$.value = $1.value > $3.value;
553 if ($1.undefined_macro)
554 $$.undefined_macro = $1.undefined_macro;
555 else
556 $$.undefined_macro = $3.undefined_macro;
557 }
558 | expression '<' expression {
559 $$.value = $1.value < $3.value;
560 if ($1.undefined_macro)
561 $$.undefined_macro = $1.undefined_macro;
562 else
563 $$.undefined_macro = $3.undefined_macro;
564 }
565 | expression RIGHT_SHIFT expression {
566 $$.value = $1.value >> $3.value;
567 if ($1.undefined_macro)
568 $$.undefined_macro = $1.undefined_macro;
569 else
570 $$.undefined_macro = $3.undefined_macro;
571 }
572 | expression LEFT_SHIFT expression {
573 $$.value = $1.value << $3.value;
574 if ($1.undefined_macro)
575 $$.undefined_macro = $1.undefined_macro;
576 else
577 $$.undefined_macro = $3.undefined_macro;
578 }
579 | expression '-' expression {
580 $$.value = $1.value - $3.value;
581 if ($1.undefined_macro)
582 $$.undefined_macro = $1.undefined_macro;
583 else
584 $$.undefined_macro = $3.undefined_macro;
585 }
586 | expression '+' expression {
587 $$.value = $1.value + $3.value;
588 if ($1.undefined_macro)
589 $$.undefined_macro = $1.undefined_macro;
590 else
591 $$.undefined_macro = $3.undefined_macro;
592 }
593 | expression '%' expression {
594 if ($3.value == 0) {
595 yyerror (& @1, parser,
596 "zero modulus in preprocessor directive");
597 } else {
598 $$.value = $1.value % $3.value;
599 }
600 if ($1.undefined_macro)
601 $$.undefined_macro = $1.undefined_macro;
602 else
603 $$.undefined_macro = $3.undefined_macro;
604 }
605 | expression '/' expression {
606 if ($3.value == 0) {
607 yyerror (& @1, parser,
608 "division by 0 in preprocessor directive");
609 } else {
610 $$.value = $1.value / $3.value;
611 }
612 if ($1.undefined_macro)
613 $$.undefined_macro = $1.undefined_macro;
614 else
615 $$.undefined_macro = $3.undefined_macro;
616 }
617 | expression '*' expression {
618 $$.value = $1.value * $3.value;
619 if ($1.undefined_macro)
620 $$.undefined_macro = $1.undefined_macro;
621 else
622 $$.undefined_macro = $3.undefined_macro;
623 }
624 | '!' expression %prec UNARY {
625 $$.value = ! $2.value;
626 $$.undefined_macro = $2.undefined_macro;
627 }
628 | '~' expression %prec UNARY {
629 $$.value = ~ $2.value;
630 $$.undefined_macro = $2.undefined_macro;
631 }
632 | '-' expression %prec UNARY {
633 $$.value = - $2.value;
634 $$.undefined_macro = $2.undefined_macro;
635 }
636 | '+' expression %prec UNARY {
637 $$.value = + $2.value;
638 $$.undefined_macro = $2.undefined_macro;
639 }
640 | '(' expression ')' {
641 $$ = $2;
642 }
643 ;
644
645 identifier_list:
646 IDENTIFIER {
647 $$ = _string_list_create (parser);
648 _string_list_append_item (parser, $$, $1);
649 }
650 | identifier_list ',' IDENTIFIER {
651 $$ = $1;
652 _string_list_append_item (parser, $$, $3);
653 }
654 ;
655
656 text_line:
657 NEWLINE { $$ = NULL; }
658 | pp_tokens NEWLINE
659 ;
660
661 replacement_list:
662 /* empty */ { $$ = NULL; }
663 | pp_tokens
664 ;
665
666 junk:
667 /* empty */
668 | pp_tokens {
669 glcpp_error(&@1, parser, "extra tokens at end of directive");
670 }
671 ;
672
673 pp_tokens:
674 preprocessing_token {
675 parser->space_tokens = 1;
676 $$ = _token_list_create (parser);
677 _token_list_append (parser, $$, $1);
678 }
679 | pp_tokens preprocessing_token {
680 $$ = $1;
681 _token_list_append (parser, $$, $2);
682 }
683 ;
684
685 preprocessing_token:
686 IDENTIFIER {
687 $$ = _token_create_str (parser, IDENTIFIER, $1);
688 $$->location = yylloc;
689 }
690 | INTEGER_STRING {
691 $$ = _token_create_str (parser, INTEGER_STRING, $1);
692 $$->location = yylloc;
693 }
694 | operator {
695 $$ = _token_create_ival (parser, $1, $1);
696 $$->location = yylloc;
697 }
698 | DEFINED {
699 $$ = _token_create_ival (parser, DEFINED, DEFINED);
700 $$->location = yylloc;
701 }
702 | OTHER {
703 $$ = _token_create_str (parser, OTHER, $1);
704 $$->location = yylloc;
705 }
706 | SPACE {
707 $$ = _token_create_ival (parser, SPACE, SPACE);
708 $$->location = yylloc;
709 }
710 ;
711
712 operator:
713 '[' { $$ = '['; }
714 | ']' { $$ = ']'; }
715 | '(' { $$ = '('; }
716 | ')' { $$ = ')'; }
717 | '{' { $$ = '{'; }
718 | '}' { $$ = '}'; }
719 | '.' { $$ = '.'; }
720 | '&' { $$ = '&'; }
721 | '*' { $$ = '*'; }
722 | '+' { $$ = '+'; }
723 | '-' { $$ = '-'; }
724 | '~' { $$ = '~'; }
725 | '!' { $$ = '!'; }
726 | '/' { $$ = '/'; }
727 | '%' { $$ = '%'; }
728 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
729 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
730 | '<' { $$ = '<'; }
731 | '>' { $$ = '>'; }
732 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
733 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
734 | EQUAL { $$ = EQUAL; }
735 | NOT_EQUAL { $$ = NOT_EQUAL; }
736 | '^' { $$ = '^'; }
737 | '|' { $$ = '|'; }
738 | AND { $$ = AND; }
739 | OR { $$ = OR; }
740 | ';' { $$ = ';'; }
741 | ',' { $$ = ','; }
742 | '=' { $$ = '='; }
743 | PASTE { $$ = PASTE; }
744 | PLUS_PLUS { $$ = PLUS_PLUS; }
745 | MINUS_MINUS { $$ = MINUS_MINUS; }
746 ;
747
748 %%
749
750 string_list_t *
751 _string_list_create(glcpp_parser_t *parser)
752 {
753 string_list_t *list;
754
755 list = linear_alloc_child(parser->linalloc, sizeof(string_list_t));
756 list->head = NULL;
757 list->tail = NULL;
758
759 return list;
760 }
761
762 void
763 _string_list_append_item(glcpp_parser_t *parser, string_list_t *list,
764 const char *str)
765 {
766 string_node_t *node;
767
768 node = linear_alloc_child(parser->linalloc, sizeof(string_node_t));
769 node->str = linear_strdup(parser->linalloc, str);
770
771 node->next = NULL;
772
773 if (list->head == NULL) {
774 list->head = node;
775 } else {
776 list->tail->next = node;
777 }
778
779 list->tail = node;
780 }
781
782 int
783 _string_list_contains(string_list_t *list, const char *member, int *index)
784 {
785 string_node_t *node;
786 int i;
787
788 if (list == NULL)
789 return 0;
790
791 for (i = 0, node = list->head; node; i++, node = node->next) {
792 if (strcmp (node->str, member) == 0) {
793 if (index)
794 *index = i;
795 return 1;
796 }
797 }
798
799 return 0;
800 }
801
802 /* Return duplicate string in list (if any), NULL otherwise. */
803 const char *
804 _string_list_has_duplicate(string_list_t *list)
805 {
806 string_node_t *node, *dup;
807
808 if (list == NULL)
809 return NULL;
810
811 for (node = list->head; node; node = node->next) {
812 for (dup = node->next; dup; dup = dup->next) {
813 if (strcmp (node->str, dup->str) == 0)
814 return node->str;
815 }
816 }
817
818 return NULL;
819 }
820
821 int
822 _string_list_length(string_list_t *list)
823 {
824 int length = 0;
825 string_node_t *node;
826
827 if (list == NULL)
828 return 0;
829
830 for (node = list->head; node; node = node->next)
831 length++;
832
833 return length;
834 }
835
836 int
837 _string_list_equal(string_list_t *a, string_list_t *b)
838 {
839 string_node_t *node_a, *node_b;
840
841 if (a == NULL && b == NULL)
842 return 1;
843
844 if (a == NULL || b == NULL)
845 return 0;
846
847 for (node_a = a->head, node_b = b->head;
848 node_a && node_b;
849 node_a = node_a->next, node_b = node_b->next)
850 {
851 if (strcmp (node_a->str, node_b->str))
852 return 0;
853 }
854
855 /* Catch the case of lists being different lengths, (which
856 * would cause the loop above to terminate after the shorter
857 * list). */
858 return node_a == node_b;
859 }
860
861 argument_list_t *
862 _argument_list_create(glcpp_parser_t *parser)
863 {
864 argument_list_t *list;
865
866 list = linear_alloc_child(parser->linalloc, sizeof(argument_list_t));
867 list->head = NULL;
868 list->tail = NULL;
869
870 return list;
871 }
872
873 void
874 _argument_list_append(glcpp_parser_t *parser,
875 argument_list_t *list, token_list_t *argument)
876 {
877 argument_node_t *node;
878
879 node = linear_alloc_child(parser->linalloc, sizeof(argument_node_t));
880 node->argument = argument;
881
882 node->next = NULL;
883
884 if (list->head == NULL) {
885 list->head = node;
886 } else {
887 list->tail->next = node;
888 }
889
890 list->tail = node;
891 }
892
893 int
894 _argument_list_length(argument_list_t *list)
895 {
896 int length = 0;
897 argument_node_t *node;
898
899 if (list == NULL)
900 return 0;
901
902 for (node = list->head; node; node = node->next)
903 length++;
904
905 return length;
906 }
907
908 token_list_t *
909 _argument_list_member_at(argument_list_t *list, int index)
910 {
911 argument_node_t *node;
912 int i;
913
914 if (list == NULL)
915 return NULL;
916
917 node = list->head;
918 for (i = 0; i < index; i++) {
919 node = node->next;
920 if (node == NULL)
921 break;
922 }
923
924 if (node)
925 return node->argument;
926
927 return NULL;
928 }
929
930 token_t *
931 _token_create_str(glcpp_parser_t *parser, int type, char *str)
932 {
933 token_t *token;
934
935 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
936 token->type = type;
937 token->value.str = str;
938
939 return token;
940 }
941
942 token_t *
943 _token_create_ival(glcpp_parser_t *parser, int type, int ival)
944 {
945 token_t *token;
946
947 token = linear_alloc_child(parser->linalloc, sizeof(token_t));
948 token->type = type;
949 token->value.ival = ival;
950
951 return token;
952 }
953
954 token_list_t *
955 _token_list_create(glcpp_parser_t *parser)
956 {
957 token_list_t *list;
958
959 list = linear_alloc_child(parser->linalloc, sizeof(token_list_t));
960 list->head = NULL;
961 list->tail = NULL;
962 list->non_space_tail = NULL;
963
964 return list;
965 }
966
967 void
968 _token_list_append(glcpp_parser_t *parser, token_list_t *list, token_t *token)
969 {
970 token_node_t *node;
971
972 node = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
973 node->token = token;
974 node->next = NULL;
975
976 if (list->head == NULL) {
977 list->head = node;
978 } else {
979 list->tail->next = node;
980 }
981
982 list->tail = node;
983 if (token->type != SPACE)
984 list->non_space_tail = node;
985 }
986
987 void
988 _token_list_append_list(token_list_t *list, token_list_t *tail)
989 {
990 if (tail == NULL || tail->head == NULL)
991 return;
992
993 if (list->head == NULL) {
994 list->head = tail->head;
995 } else {
996 list->tail->next = tail->head;
997 }
998
999 list->tail = tail->tail;
1000 list->non_space_tail = tail->non_space_tail;
1001 }
1002
1003 static token_list_t *
1004 _token_list_copy(glcpp_parser_t *parser, token_list_t *other)
1005 {
1006 token_list_t *copy;
1007 token_node_t *node;
1008
1009 if (other == NULL)
1010 return NULL;
1011
1012 copy = _token_list_create (parser);
1013 for (node = other->head; node; node = node->next) {
1014 token_t *new_token = linear_alloc_child(parser->linalloc, sizeof(token_t));
1015 *new_token = *node->token;
1016 _token_list_append (parser, copy, new_token);
1017 }
1018
1019 return copy;
1020 }
1021
1022 static void
1023 _token_list_trim_trailing_space(token_list_t *list)
1024 {
1025 if (list->non_space_tail) {
1026 list->non_space_tail->next = NULL;
1027 list->tail = list->non_space_tail;
1028 }
1029 }
1030
1031 static int
1032 _token_list_is_empty_ignoring_space(token_list_t *l)
1033 {
1034 token_node_t *n;
1035
1036 if (l == NULL)
1037 return 1;
1038
1039 n = l->head;
1040 while (n != NULL && n->token->type == SPACE)
1041 n = n->next;
1042
1043 return n == NULL;
1044 }
1045
1046 int
1047 _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
1048 {
1049 token_node_t *node_a, *node_b;
1050
1051 if (a == NULL || b == NULL) {
1052 int a_empty = _token_list_is_empty_ignoring_space(a);
1053 int b_empty = _token_list_is_empty_ignoring_space(b);
1054 return a_empty == b_empty;
1055 }
1056
1057 node_a = a->head;
1058 node_b = b->head;
1059
1060 while (1)
1061 {
1062 if (node_a == NULL && node_b == NULL)
1063 break;
1064
1065 if (node_a == NULL || node_b == NULL)
1066 return 0;
1067 /* Make sure whitespace appears in the same places in both.
1068 * It need not be exactly the same amount of whitespace,
1069 * though.
1070 */
1071 if (node_a->token->type == SPACE && node_b->token->type == SPACE) {
1072 while (node_a && node_a->token->type == SPACE)
1073 node_a = node_a->next;
1074 while (node_b && node_b->token->type == SPACE)
1075 node_b = node_b->next;
1076 continue;
1077 }
1078
1079 if (node_a->token->type != node_b->token->type)
1080 return 0;
1081
1082 switch (node_a->token->type) {
1083 case INTEGER:
1084 if (node_a->token->value.ival != node_b->token->value.ival) {
1085 return 0;
1086 }
1087 break;
1088 case IDENTIFIER:
1089 case INTEGER_STRING:
1090 case OTHER:
1091 if (strcmp(node_a->token->value.str, node_b->token->value.str)) {
1092 return 0;
1093 }
1094 break;
1095 }
1096
1097 node_a = node_a->next;
1098 node_b = node_b->next;
1099 }
1100
1101 return 1;
1102 }
1103
1104 static void
1105 _token_print(char **out, size_t *len, token_t *token)
1106 {
1107 if (token->type < 256) {
1108 ralloc_asprintf_rewrite_tail (out, len, "%c", token->type);
1109 return;
1110 }
1111
1112 switch (token->type) {
1113 case INTEGER:
1114 ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
1115 break;
1116 case IDENTIFIER:
1117 case INTEGER_STRING:
1118 case OTHER:
1119 ralloc_asprintf_rewrite_tail (out, len, "%s", token->value.str);
1120 break;
1121 case SPACE:
1122 ralloc_asprintf_rewrite_tail (out, len, " ");
1123 break;
1124 case LEFT_SHIFT:
1125 ralloc_asprintf_rewrite_tail (out, len, "<<");
1126 break;
1127 case RIGHT_SHIFT:
1128 ralloc_asprintf_rewrite_tail (out, len, ">>");
1129 break;
1130 case LESS_OR_EQUAL:
1131 ralloc_asprintf_rewrite_tail (out, len, "<=");
1132 break;
1133 case GREATER_OR_EQUAL:
1134 ralloc_asprintf_rewrite_tail (out, len, ">=");
1135 break;
1136 case EQUAL:
1137 ralloc_asprintf_rewrite_tail (out, len, "==");
1138 break;
1139 case NOT_EQUAL:
1140 ralloc_asprintf_rewrite_tail (out, len, "!=");
1141 break;
1142 case AND:
1143 ralloc_asprintf_rewrite_tail (out, len, "&&");
1144 break;
1145 case OR:
1146 ralloc_asprintf_rewrite_tail (out, len, "||");
1147 break;
1148 case PASTE:
1149 ralloc_asprintf_rewrite_tail (out, len, "##");
1150 break;
1151 case PLUS_PLUS:
1152 ralloc_asprintf_rewrite_tail (out, len, "++");
1153 break;
1154 case MINUS_MINUS:
1155 ralloc_asprintf_rewrite_tail (out, len, "--");
1156 break;
1157 case DEFINED:
1158 ralloc_asprintf_rewrite_tail (out, len, "defined");
1159 break;
1160 case PLACEHOLDER:
1161 /* Nothing to print. */
1162 break;
1163 default:
1164 assert(!"Error: Don't know how to print token.");
1165
1166 break;
1167 }
1168 }
1169
1170 /* Return a new token formed by pasting 'token' and 'other'. Note that this
1171 * function may return 'token' or 'other' directly rather than allocating
1172 * anything new.
1173 *
1174 * Caution: Only very cursory error-checking is performed to see if
1175 * the final result is a valid single token. */
1176 static token_t *
1177 _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
1178 {
1179 token_t *combined = NULL;
1180
1181 /* Pasting a placeholder onto anything makes no change. */
1182 if (other->type == PLACEHOLDER)
1183 return token;
1184
1185 /* When 'token' is a placeholder, just return 'other'. */
1186 if (token->type == PLACEHOLDER)
1187 return other;
1188
1189 /* A very few single-character punctuators can be combined
1190 * with another to form a multi-character punctuator. */
1191 switch (token->type) {
1192 case '<':
1193 if (other->type == '<')
1194 combined = _token_create_ival (parser, LEFT_SHIFT, LEFT_SHIFT);
1195 else if (other->type == '=')
1196 combined = _token_create_ival (parser, LESS_OR_EQUAL, LESS_OR_EQUAL);
1197 break;
1198 case '>':
1199 if (other->type == '>')
1200 combined = _token_create_ival (parser, RIGHT_SHIFT, RIGHT_SHIFT);
1201 else if (other->type == '=')
1202 combined = _token_create_ival (parser, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
1203 break;
1204 case '=':
1205 if (other->type == '=')
1206 combined = _token_create_ival (parser, EQUAL, EQUAL);
1207 break;
1208 case '!':
1209 if (other->type == '=')
1210 combined = _token_create_ival (parser, NOT_EQUAL, NOT_EQUAL);
1211 break;
1212 case '&':
1213 if (other->type == '&')
1214 combined = _token_create_ival (parser, AND, AND);
1215 break;
1216 case '|':
1217 if (other->type == '|')
1218 combined = _token_create_ival (parser, OR, OR);
1219 break;
1220 }
1221
1222 if (combined != NULL) {
1223 /* Inherit the location from the first token */
1224 combined->location = token->location;
1225 return combined;
1226 }
1227
1228 /* Two string-valued (or integer) tokens can usually just be
1229 * mashed together. (We also handle a string followed by an
1230 * integer here as well.)
1231 *
1232 * There are some exceptions here. Notably, if the first token
1233 * is an integer (or a string representing an integer), then
1234 * the second token must also be an integer or must be a
1235 * string representing an integer that begins with a digit.
1236 */
1237 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING || token->type == INTEGER) &&
1238 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING || other->type == INTEGER))
1239 {
1240 char *str;
1241 int combined_type;
1242
1243 /* Check that pasting onto an integer doesn't create a
1244 * non-integer, (that is, only digits can be
1245 * pasted. */
1246 if (token->type == INTEGER_STRING || token->type == INTEGER) {
1247 switch (other->type) {
1248 case INTEGER_STRING:
1249 if (other->value.str[0] < '0' || other->value.str[0] > '9')
1250 goto FAIL;
1251 break;
1252 case INTEGER:
1253 if (other->value.ival < 0)
1254 goto FAIL;
1255 break;
1256 default:
1257 goto FAIL;
1258 }
1259 }
1260
1261 if (token->type == INTEGER)
1262 str = linear_asprintf(parser->linalloc, "%" PRIiMAX, token->value.ival);
1263 else
1264 str = linear_strdup(parser->linalloc, token->value.str);
1265
1266 if (other->type == INTEGER)
1267 linear_asprintf_append(parser->linalloc, &str, "%" PRIiMAX, other->value.ival);
1268 else
1269 linear_strcat(parser->linalloc, &str, other->value.str);
1270
1271 /* New token is same type as original token, unless we
1272 * started with an integer, in which case we will be
1273 * creating an integer-string. */
1274 combined_type = token->type;
1275 if (combined_type == INTEGER)
1276 combined_type = INTEGER_STRING;
1277
1278 combined = _token_create_str (parser, combined_type, str);
1279 combined->location = token->location;
1280 return combined;
1281 }
1282
1283 FAIL:
1284 glcpp_error (&token->location, parser, "");
1285 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
1286 _token_print (&parser->info_log, &parser->info_log_length, token);
1287 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
1288 _token_print (&parser->info_log, &parser->info_log_length, other);
1289 ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
1290
1291 return token;
1292 }
1293
1294 static void
1295 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
1296 {
1297 token_node_t *node;
1298
1299 if (list == NULL)
1300 return;
1301
1302 for (node = list->head; node; node = node->next)
1303 _token_print (&parser->output, &parser->output_length, node->token);
1304 }
1305
1306 void
1307 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
1308 {
1309 glcpp_error(locp, parser, "%s", error);
1310 }
1311
1312 static void
1313 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
1314 {
1315 token_t *tok;
1316 token_list_t *list;
1317
1318 tok = _token_create_ival (parser, INTEGER, value);
1319
1320 list = _token_list_create(parser);
1321 _token_list_append(parser, list, tok);
1322 _define_object_macro(parser, NULL, name, list);
1323 }
1324
1325 glcpp_parser_t *
1326 glcpp_parser_create(glcpp_extension_iterator extensions, void *state, gl_api api)
1327 {
1328 glcpp_parser_t *parser;
1329
1330 parser = ralloc (NULL, glcpp_parser_t);
1331
1332 glcpp_lex_init_extra (parser, &parser->scanner);
1333 parser->defines = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
1334 _mesa_key_string_equal);
1335 parser->linalloc = linear_alloc_parent(parser, 0);
1336 parser->active = NULL;
1337 parser->lexing_directive = 0;
1338 parser->lexing_version_directive = 0;
1339 parser->space_tokens = 1;
1340 parser->last_token_was_newline = 0;
1341 parser->last_token_was_space = 0;
1342 parser->first_non_space_token_this_line = 1;
1343 parser->newline_as_space = 0;
1344 parser->in_control_line = 0;
1345 parser->paren_count = 0;
1346 parser->commented_newlines = 0;
1347
1348 parser->skip_stack = NULL;
1349 parser->skipping = 0;
1350
1351 parser->lex_from_list = NULL;
1352 parser->lex_from_node = NULL;
1353
1354 parser->output = ralloc_strdup(parser, "");
1355 parser->output_length = 0;
1356 parser->info_log = ralloc_strdup(parser, "");
1357 parser->info_log_length = 0;
1358 parser->error = 0;
1359
1360 parser->extensions = extensions;
1361 parser->state = state;
1362 parser->api = api;
1363 parser->version = 0;
1364
1365 parser->has_new_line_number = 0;
1366 parser->new_line_number = 1;
1367 parser->has_new_source_number = 0;
1368 parser->new_source_number = 0;
1369
1370 parser->is_gles = false;
1371
1372 return parser;
1373 }
1374
1375 void
1376 glcpp_parser_destroy(glcpp_parser_t *parser)
1377 {
1378 glcpp_lex_destroy (parser->scanner);
1379 _mesa_hash_table_destroy(parser->defines, NULL);
1380 ralloc_free (parser);
1381 }
1382
1383 typedef enum function_status
1384 {
1385 FUNCTION_STATUS_SUCCESS,
1386 FUNCTION_NOT_A_FUNCTION,
1387 FUNCTION_UNBALANCED_PARENTHESES
1388 } function_status_t;
1389
1390 /* Find a set of function-like macro arguments by looking for a
1391 * balanced set of parentheses.
1392 *
1393 * When called, 'node' should be the opening-parenthesis token, (or
1394 * perhaps preceeding SPACE tokens). Upon successful return *last will
1395 * be the last consumed node, (corresponding to the closing right
1396 * parenthesis).
1397 *
1398 * Return values:
1399 *
1400 * FUNCTION_STATUS_SUCCESS:
1401 *
1402 * Successfully parsed a set of function arguments.
1403 *
1404 * FUNCTION_NOT_A_FUNCTION:
1405 *
1406 * Macro name not followed by a '('. This is not an error, but
1407 * simply that the macro name should be treated as a non-macro.
1408 *
1409 * FUNCTION_UNBALANCED_PARENTHESES
1410 *
1411 * Macro name is not followed by a balanced set of parentheses.
1412 */
1413 static function_status_t
1414 _arguments_parse(glcpp_parser_t *parser,
1415 argument_list_t *arguments, token_node_t *node,
1416 token_node_t **last)
1417 {
1418 token_list_t *argument;
1419 int paren_count;
1420
1421 node = node->next;
1422
1423 /* Ignore whitespace before first parenthesis. */
1424 while (node && node->token->type == SPACE)
1425 node = node->next;
1426
1427 if (node == NULL || node->token->type != '(')
1428 return FUNCTION_NOT_A_FUNCTION;
1429
1430 node = node->next;
1431
1432 argument = _token_list_create (parser);
1433 _argument_list_append (parser, arguments, argument);
1434
1435 for (paren_count = 1; node; node = node->next) {
1436 if (node->token->type == '(') {
1437 paren_count++;
1438 } else if (node->token->type == ')') {
1439 paren_count--;
1440 if (paren_count == 0)
1441 break;
1442 }
1443
1444 if (node->token->type == ',' && paren_count == 1) {
1445 _token_list_trim_trailing_space (argument);
1446 argument = _token_list_create (parser);
1447 _argument_list_append (parser, arguments, argument);
1448 } else {
1449 if (argument->head == NULL) {
1450 /* Don't treat initial whitespace as part of the argument. */
1451 if (node->token->type == SPACE)
1452 continue;
1453 }
1454 _token_list_append(parser, argument, node->token);
1455 }
1456 }
1457
1458 if (paren_count)
1459 return FUNCTION_UNBALANCED_PARENTHESES;
1460
1461 *last = node;
1462
1463 return FUNCTION_STATUS_SUCCESS;
1464 }
1465
1466 static token_list_t *
1467 _token_list_create_with_one_ival(glcpp_parser_t *parser, int type, int ival)
1468 {
1469 token_list_t *list;
1470 token_t *node;
1471
1472 list = _token_list_create(parser);
1473 node = _token_create_ival(parser, type, ival);
1474 _token_list_append(parser, list, node);
1475
1476 return list;
1477 }
1478
1479 static token_list_t *
1480 _token_list_create_with_one_space(glcpp_parser_t *parser)
1481 {
1482 return _token_list_create_with_one_ival(parser, SPACE, SPACE);
1483 }
1484
1485 static token_list_t *
1486 _token_list_create_with_one_integer(glcpp_parser_t *parser, int ival)
1487 {
1488 return _token_list_create_with_one_ival(parser, INTEGER, ival);
1489 }
1490
1491 /* Evaluate a DEFINED token node (based on subsequent tokens in the list).
1492 *
1493 * Note: This function must only be called when "node" is a DEFINED token,
1494 * (and will abort with an assertion failure otherwise).
1495 *
1496 * If "node" is followed, (ignoring any SPACE tokens), by an IDENTIFIER token
1497 * (optionally preceded and followed by '(' and ')' tokens) then the following
1498 * occurs:
1499 *
1500 * If the identifier is a defined macro, this function returns 1.
1501 *
1502 * If the identifier is not a defined macro, this function returns 0.
1503 *
1504 * In either case, *last will be updated to the last node in the list
1505 * consumed by the evaluation, (either the token of the identifier or the
1506 * token of the closing parenthesis).
1507 *
1508 * In all other cases, (such as "node is the final node of the list", or
1509 * "missing closing parenthesis", etc.), this function generates a
1510 * preprocessor error, returns -1 and *last will not be set.
1511 */
1512 static int
1513 _glcpp_parser_evaluate_defined(glcpp_parser_t *parser, token_node_t *node,
1514 token_node_t **last)
1515 {
1516 token_node_t *argument, *defined = node;
1517
1518 assert(node->token->type == DEFINED);
1519
1520 node = node->next;
1521
1522 /* Ignore whitespace after DEFINED token. */
1523 while (node && node->token->type == SPACE)
1524 node = node->next;
1525
1526 if (node == NULL)
1527 goto FAIL;
1528
1529 if (node->token->type == IDENTIFIER || node->token->type == OTHER) {
1530 argument = node;
1531 } else if (node->token->type == '(') {
1532 node = node->next;
1533
1534 /* Ignore whitespace after '(' token. */
1535 while (node && node->token->type == SPACE)
1536 node = node->next;
1537
1538 if (node == NULL || (node->token->type != IDENTIFIER &&
1539 node->token->type != OTHER)) {
1540 goto FAIL;
1541 }
1542
1543 argument = node;
1544
1545 node = node->next;
1546
1547 /* Ignore whitespace after identifier, before ')' token. */
1548 while (node && node->token->type == SPACE)
1549 node = node->next;
1550
1551 if (node == NULL || node->token->type != ')')
1552 goto FAIL;
1553 } else {
1554 goto FAIL;
1555 }
1556
1557 *last = node;
1558
1559 return _mesa_hash_table_search(parser->defines,
1560 argument->token->value.str) ? 1 : 0;
1561
1562 FAIL:
1563 glcpp_error (&defined->token->location, parser,
1564 "\"defined\" not followed by an identifier");
1565 return -1;
1566 }
1567
1568 /* Evaluate all DEFINED nodes in a given list, modifying the list in place.
1569 */
1570 static void
1571 _glcpp_parser_evaluate_defined_in_list(glcpp_parser_t *parser,
1572 token_list_t *list)
1573 {
1574 token_node_t *node, *node_prev, *replacement, *last = NULL;
1575 int value;
1576
1577 if (list == NULL)
1578 return;
1579
1580 node_prev = NULL;
1581 node = list->head;
1582
1583 while (node) {
1584
1585 if (node->token->type != DEFINED)
1586 goto NEXT;
1587
1588 value = _glcpp_parser_evaluate_defined (parser, node, &last);
1589 if (value == -1)
1590 goto NEXT;
1591
1592 replacement = linear_alloc_child(parser->linalloc, sizeof(token_node_t));
1593 replacement->token = _token_create_ival (parser, INTEGER, value);
1594
1595 /* Splice replacement node into list, replacing from "node"
1596 * through "last". */
1597 if (node_prev)
1598 node_prev->next = replacement;
1599 else
1600 list->head = replacement;
1601 replacement->next = last->next;
1602 if (last == list->tail)
1603 list->tail = replacement;
1604
1605 node = replacement;
1606
1607 NEXT:
1608 node_prev = node;
1609 node = node->next;
1610 }
1611 }
1612
1613 /* Perform macro expansion on 'list', placing the resulting tokens
1614 * into a new list which is initialized with a first token of type
1615 * 'head_token_type'. Then begin lexing from the resulting list,
1616 * (return to the current lexing source when this list is exhausted).
1617 *
1618 * See the documentation of _glcpp_parser_expand_token_list for a description
1619 * of the "mode" parameter.
1620 */
1621 static void
1622 _glcpp_parser_expand_and_lex_from(glcpp_parser_t *parser, int head_token_type,
1623 token_list_t *list, expansion_mode_t mode)
1624 {
1625 token_list_t *expanded;
1626 token_t *token;
1627
1628 expanded = _token_list_create (parser);
1629 token = _token_create_ival (parser, head_token_type, head_token_type);
1630 _token_list_append (parser, expanded, token);
1631 _glcpp_parser_expand_token_list (parser, list, mode);
1632 _token_list_append_list (expanded, list);
1633 glcpp_parser_lex_from (parser, expanded);
1634 }
1635
1636 static void
1637 _glcpp_parser_apply_pastes(glcpp_parser_t *parser, token_list_t *list)
1638 {
1639 token_node_t *node;
1640
1641 node = list->head;
1642 while (node) {
1643 token_node_t *next_non_space;
1644
1645 /* Look ahead for a PASTE token, skipping space. */
1646 next_non_space = node->next;
1647 while (next_non_space && next_non_space->token->type == SPACE)
1648 next_non_space = next_non_space->next;
1649
1650 if (next_non_space == NULL)
1651 break;
1652
1653 if (next_non_space->token->type != PASTE) {
1654 node = next_non_space;
1655 continue;
1656 }
1657
1658 /* Now find the next non-space token after the PASTE. */
1659 next_non_space = next_non_space->next;
1660 while (next_non_space && next_non_space->token->type == SPACE)
1661 next_non_space = next_non_space->next;
1662
1663 if (next_non_space == NULL) {
1664 yyerror(&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1665 return;
1666 }
1667
1668 node->token = _token_paste(parser, node->token, next_non_space->token);
1669 node->next = next_non_space->next;
1670 if (next_non_space == list->tail)
1671 list->tail = node;
1672 }
1673
1674 list->non_space_tail = list->tail;
1675 }
1676
1677 /* This is a helper function that's essentially part of the
1678 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1679 * except for by that function.
1680 *
1681 * Returns NULL if node is a simple token with no expansion, (that is,
1682 * although 'node' corresponds to an identifier defined as a
1683 * function-like macro, it is not followed with a parenthesized
1684 * argument list).
1685 *
1686 * Compute the complete expansion of node (which is a function-like
1687 * macro) and subsequent nodes which are arguments.
1688 *
1689 * Returns the token list that results from the expansion and sets
1690 * *last to the last node in the list that was consumed by the
1691 * expansion. Specifically, *last will be set as follows: as the
1692 * token of the closing right parenthesis.
1693 *
1694 * See the documentation of _glcpp_parser_expand_token_list for a description
1695 * of the "mode" parameter.
1696 */
1697 static token_list_t *
1698 _glcpp_parser_expand_function(glcpp_parser_t *parser, token_node_t *node,
1699 token_node_t **last, expansion_mode_t mode)
1700 {
1701 struct hash_entry *entry;
1702 macro_t *macro;
1703 const char *identifier;
1704 argument_list_t *arguments;
1705 function_status_t status;
1706 token_list_t *substituted;
1707 int parameter_index;
1708
1709 identifier = node->token->value.str;
1710
1711 entry = _mesa_hash_table_search(parser->defines, identifier);
1712 macro = entry ? entry->data : NULL;
1713
1714 assert(macro->is_function);
1715
1716 arguments = _argument_list_create(parser);
1717 status = _arguments_parse(parser, arguments, node, last);
1718
1719 switch (status) {
1720 case FUNCTION_STATUS_SUCCESS:
1721 break;
1722 case FUNCTION_NOT_A_FUNCTION:
1723 return NULL;
1724 case FUNCTION_UNBALANCED_PARENTHESES:
1725 glcpp_error(&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1726 return NULL;
1727 }
1728
1729 /* Replace a macro defined as empty with a SPACE token. */
1730 if (macro->replacements == NULL) {
1731 return _token_list_create_with_one_space(parser);
1732 }
1733
1734 if (!((_argument_list_length (arguments) ==
1735 _string_list_length (macro->parameters)) ||
1736 (_string_list_length (macro->parameters) == 0 &&
1737 _argument_list_length (arguments) == 1 &&
1738 arguments->head->argument->head == NULL))) {
1739 glcpp_error(&node->token->location, parser,
1740 "Error: macro %s invoked with %d arguments (expected %d)\n",
1741 identifier, _argument_list_length (arguments),
1742 _string_list_length(macro->parameters));
1743 return NULL;
1744 }
1745
1746 /* Perform argument substitution on the replacement list. */
1747 substituted = _token_list_create(parser);
1748
1749 for (node = macro->replacements->head; node; node = node->next) {
1750 if (node->token->type == IDENTIFIER &&
1751 _string_list_contains(macro->parameters, node->token->value.str,
1752 &parameter_index)) {
1753 token_list_t *argument;
1754 argument = _argument_list_member_at(arguments, parameter_index);
1755 /* Before substituting, we expand the argument tokens, or append a
1756 * placeholder token for an empty argument. */
1757 if (argument->head) {
1758 token_list_t *expanded_argument;
1759 expanded_argument = _token_list_copy(parser, argument);
1760 _glcpp_parser_expand_token_list(parser, expanded_argument, mode);
1761 _token_list_append_list(substituted, expanded_argument);
1762 } else {
1763 token_t *new_token;
1764
1765 new_token = _token_create_ival(parser, PLACEHOLDER,
1766 PLACEHOLDER);
1767 _token_list_append(parser, substituted, new_token);
1768 }
1769 } else {
1770 _token_list_append(parser, substituted, node->token);
1771 }
1772 }
1773
1774 /* After argument substitution, and before further expansion
1775 * below, implement token pasting. */
1776
1777 _token_list_trim_trailing_space(substituted);
1778
1779 _glcpp_parser_apply_pastes(parser, substituted);
1780
1781 return substituted;
1782 }
1783
1784 /* Compute the complete expansion of node, (and subsequent nodes after
1785 * 'node' in the case that 'node' is a function-like macro and
1786 * subsequent nodes are arguments).
1787 *
1788 * Returns NULL if node is a simple token with no expansion.
1789 *
1790 * Otherwise, returns the token list that results from the expansion
1791 * and sets *last to the last node in the list that was consumed by
1792 * the expansion. Specifically, *last will be set as follows:
1793 *
1794 * As 'node' in the case of object-like macro expansion.
1795 *
1796 * As the token of the closing right parenthesis in the case of
1797 * function-like macro expansion.
1798 *
1799 * See the documentation of _glcpp_parser_expand_token_list for a description
1800 * of the "mode" parameter.
1801 */
1802 static token_list_t *
1803 _glcpp_parser_expand_node(glcpp_parser_t *parser, token_node_t *node,
1804 token_node_t **last, expansion_mode_t mode)
1805 {
1806 token_t *token = node->token;
1807 const char *identifier;
1808 struct hash_entry *entry;
1809 macro_t *macro;
1810
1811 /* We only expand identifiers */
1812 if (token->type != IDENTIFIER) {
1813 return NULL;
1814 }
1815
1816 *last = node;
1817 identifier = token->value.str;
1818
1819 /* Special handling for __LINE__ and __FILE__, (not through
1820 * the hash table). */
1821 if (strcmp(identifier, "__LINE__") == 0)
1822 return _token_list_create_with_one_integer(parser, node->token->location.first_line);
1823
1824 if (strcmp(identifier, "__FILE__") == 0)
1825 return _token_list_create_with_one_integer(parser, node->token->location.source);
1826
1827 /* Look up this identifier in the hash table. */
1828 entry = _mesa_hash_table_search(parser->defines, identifier);
1829 macro = entry ? entry->data : NULL;
1830
1831 /* Not a macro, so no expansion needed. */
1832 if (macro == NULL)
1833 return NULL;
1834
1835 /* Finally, don't expand this macro if we're already actively
1836 * expanding it, (to avoid infinite recursion). */
1837 if (_parser_active_list_contains (parser, identifier)) {
1838 /* We change the token type here from IDENTIFIER to OTHER to prevent any
1839 * future expansion of this unexpanded token. */
1840 char *str;
1841 token_list_t *expansion;
1842 token_t *final;
1843
1844 str = linear_strdup(parser->linalloc, token->value.str);
1845 final = _token_create_str(parser, OTHER, str);
1846 expansion = _token_list_create(parser);
1847 _token_list_append(parser, expansion, final);
1848 return expansion;
1849 }
1850
1851 if (! macro->is_function) {
1852 token_list_t *replacement;
1853
1854 /* Replace a macro defined as empty with a SPACE token. */
1855 if (macro->replacements == NULL)
1856 return _token_list_create_with_one_space(parser);
1857
1858 replacement = _token_list_copy(parser, macro->replacements);
1859 _glcpp_parser_apply_pastes(parser, replacement);
1860 return replacement;
1861 }
1862
1863 return _glcpp_parser_expand_function(parser, node, last, mode);
1864 }
1865
1866 /* Push a new identifier onto the parser's active list.
1867 *
1868 * Here, 'marker' is the token node that appears in the list after the
1869 * expansion of 'identifier'. That is, when the list iterator begins
1870 * examining 'marker', then it is time to pop this node from the
1871 * active stack.
1872 */
1873 static void
1874 _parser_active_list_push(glcpp_parser_t *parser, const char *identifier,
1875 token_node_t *marker)
1876 {
1877 active_list_t *node;
1878
1879 node = linear_alloc_child(parser->linalloc, sizeof(active_list_t));
1880 node->identifier = linear_strdup(parser->linalloc, identifier);
1881 node->marker = marker;
1882 node->next = parser->active;
1883
1884 parser->active = node;
1885 }
1886
1887 static void
1888 _parser_active_list_pop(glcpp_parser_t *parser)
1889 {
1890 active_list_t *node = parser->active;
1891
1892 if (node == NULL) {
1893 parser->active = NULL;
1894 return;
1895 }
1896
1897 node = parser->active->next;
1898 parser->active = node;
1899 }
1900
1901 static int
1902 _parser_active_list_contains(glcpp_parser_t *parser, const char *identifier)
1903 {
1904 active_list_t *node;
1905
1906 if (parser->active == NULL)
1907 return 0;
1908
1909 for (node = parser->active; node; node = node->next)
1910 if (strcmp(node->identifier, identifier) == 0)
1911 return 1;
1912
1913 return 0;
1914 }
1915
1916 /* Walk over the token list replacing nodes with their expansion.
1917 * Whenever nodes are expanded the walking will walk over the new
1918 * nodes, continuing to expand as necessary. The results are placed in
1919 * 'list' itself.
1920 *
1921 * The "mode" argument controls the handling of any DEFINED tokens that
1922 * result from expansion as follows:
1923 *
1924 * EXPANSION_MODE_IGNORE_DEFINED: Any resulting DEFINED tokens will be
1925 * left in the final list, unevaluated. This is the correct mode
1926 * for expanding any list in any context other than a
1927 * preprocessor conditional, (#if or #elif).
1928 *
1929 * EXPANSION_MODE_EVALUATE_DEFINED: Any resulting DEFINED tokens will be
1930 * evaluated to 0 or 1 tokens depending on whether the following
1931 * token is the name of a defined macro. If the DEFINED token is
1932 * not followed by an (optionally parenthesized) identifier, then
1933 * an error will be generated. This the correct mode for
1934 * expanding any list in the context of a preprocessor
1935 * conditional, (#if or #elif).
1936 */
1937 static void
1938 _glcpp_parser_expand_token_list(glcpp_parser_t *parser, token_list_t *list,
1939 expansion_mode_t mode)
1940 {
1941 token_node_t *node_prev;
1942 token_node_t *node, *last = NULL;
1943 token_list_t *expansion;
1944 active_list_t *active_initial = parser->active;
1945
1946 if (list == NULL)
1947 return;
1948
1949 _token_list_trim_trailing_space (list);
1950
1951 node_prev = NULL;
1952 node = list->head;
1953
1954 if (mode == EXPANSION_MODE_EVALUATE_DEFINED)
1955 _glcpp_parser_evaluate_defined_in_list (parser, list);
1956
1957 while (node) {
1958
1959 while (parser->active && parser->active->marker == node)
1960 _parser_active_list_pop (parser);
1961
1962 expansion = _glcpp_parser_expand_node (parser, node, &last, mode);
1963 if (expansion) {
1964 token_node_t *n;
1965
1966 if (mode == EXPANSION_MODE_EVALUATE_DEFINED) {
1967 _glcpp_parser_evaluate_defined_in_list (parser, expansion);
1968 }
1969
1970 for (n = node; n != last->next; n = n->next)
1971 while (parser->active && parser->active->marker == n) {
1972 _parser_active_list_pop (parser);
1973 }
1974
1975 _parser_active_list_push(parser, node->token->value.str, last->next);
1976
1977 /* Splice expansion into list, supporting a simple deletion if the
1978 * expansion is empty.
1979 */
1980 if (expansion->head) {
1981 if (node_prev)
1982 node_prev->next = expansion->head;
1983 else
1984 list->head = expansion->head;
1985 expansion->tail->next = last->next;
1986 if (last == list->tail)
1987 list->tail = expansion->tail;
1988 } else {
1989 if (node_prev)
1990 node_prev->next = last->next;
1991 else
1992 list->head = last->next;
1993 if (last == list->tail)
1994 list->tail = NULL;
1995 }
1996 } else {
1997 node_prev = node;
1998 }
1999 node = node_prev ? node_prev->next : list->head;
2000 }
2001
2002 /* Remove any lingering effects of this invocation on the
2003 * active list. That is, pop until the list looks like it did
2004 * at the beginning of this function. */
2005 while (parser->active && parser->active != active_initial)
2006 _parser_active_list_pop (parser);
2007
2008 list->non_space_tail = list->tail;
2009 }
2010
2011 void
2012 _glcpp_parser_print_expanded_token_list(glcpp_parser_t *parser,
2013 token_list_t *list)
2014 {
2015 if (list == NULL)
2016 return;
2017
2018 _glcpp_parser_expand_token_list (parser, list, EXPANSION_MODE_IGNORE_DEFINED);
2019
2020 _token_list_trim_trailing_space (list);
2021
2022 _token_list_print (parser, list);
2023 }
2024
2025 static void
2026 _check_for_reserved_macro_name(glcpp_parser_t *parser, YYLTYPE *loc,
2027 const char *identifier)
2028 {
2029 /* Section 3.3 (Preprocessor) of the GLSL 1.30 spec (and later) and
2030 * the GLSL ES spec (all versions) say:
2031 *
2032 * "All macro names containing two consecutive underscores ( __ )
2033 * are reserved for future use as predefined macro names. All
2034 * macro names prefixed with "GL_" ("GL" followed by a single
2035 * underscore) are also reserved."
2036 *
2037 * The intention is that names containing __ are reserved for internal
2038 * use by the implementation, and names prefixed with GL_ are reserved
2039 * for use by Khronos. Since every extension adds a name prefixed
2040 * with GL_ (i.e., the name of the extension), that should be an
2041 * error. Names simply containing __ are dangerous to use, but should
2042 * be allowed.
2043 *
2044 * A future version of the GLSL specification will clarify this.
2045 */
2046 if (strstr(identifier, "__")) {
2047 glcpp_warning(loc, parser, "Macro names containing \"__\" are reserved "
2048 "for use by the implementation.\n");
2049 }
2050 if (strncmp(identifier, "GL_", 3) == 0) {
2051 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
2052 }
2053 if (strcmp(identifier, "defined") == 0) {
2054 glcpp_error (loc, parser, "\"defined\" cannot be used as a macro name");
2055 }
2056 }
2057
2058 static int
2059 _macro_equal(macro_t *a, macro_t *b)
2060 {
2061 if (a->is_function != b->is_function)
2062 return 0;
2063
2064 if (a->is_function) {
2065 if (! _string_list_equal (a->parameters, b->parameters))
2066 return 0;
2067 }
2068
2069 return _token_list_equal_ignoring_space(a->replacements, b->replacements);
2070 }
2071
2072 void
2073 _define_object_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2074 const char *identifier, token_list_t *replacements)
2075 {
2076 macro_t *macro, *previous;
2077 struct hash_entry *entry;
2078
2079 /* We define pre-defined macros before we've started parsing the actual
2080 * file. So if there's no location defined yet, that's what were doing and
2081 * we don't want to generate an error for using the reserved names. */
2082 if (loc != NULL)
2083 _check_for_reserved_macro_name(parser, loc, identifier);
2084
2085 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2086
2087 macro->is_function = 0;
2088 macro->parameters = NULL;
2089 macro->identifier = linear_strdup(parser->linalloc, identifier);
2090 macro->replacements = replacements;
2091
2092 entry = _mesa_hash_table_search(parser->defines, identifier);
2093 previous = entry ? entry->data : NULL;
2094 if (previous) {
2095 if (_macro_equal (macro, previous)) {
2096 return;
2097 }
2098 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2099 }
2100
2101 _mesa_hash_table_insert (parser->defines, identifier, macro);
2102 }
2103
2104 void
2105 _define_function_macro(glcpp_parser_t *parser, YYLTYPE *loc,
2106 const char *identifier, string_list_t *parameters,
2107 token_list_t *replacements)
2108 {
2109 macro_t *macro, *previous;
2110 struct hash_entry *entry;
2111 const char *dup;
2112
2113 _check_for_reserved_macro_name(parser, loc, identifier);
2114
2115 /* Check for any duplicate parameter names. */
2116 if ((dup = _string_list_has_duplicate (parameters)) != NULL) {
2117 glcpp_error (loc, parser, "Duplicate macro parameter \"%s\"", dup);
2118 }
2119
2120 macro = linear_alloc_child(parser->linalloc, sizeof(macro_t));
2121
2122 macro->is_function = 1;
2123 macro->parameters = parameters;
2124 macro->identifier = linear_strdup(parser->linalloc, identifier);
2125 macro->replacements = replacements;
2126
2127 entry = _mesa_hash_table_search(parser->defines, identifier);
2128 previous = entry ? entry->data : NULL;
2129 if (previous) {
2130 if (_macro_equal (macro, previous)) {
2131 return;
2132 }
2133 glcpp_error (loc, parser, "Redefinition of macro %s\n", identifier);
2134 }
2135
2136 _mesa_hash_table_insert(parser->defines, identifier, macro);
2137 }
2138
2139 static int
2140 glcpp_parser_lex(YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
2141 {
2142 token_node_t *node;
2143 int ret;
2144
2145 if (parser->lex_from_list == NULL) {
2146 ret = glcpp_lex(yylval, yylloc, parser->scanner);
2147
2148 /* XXX: This ugly block of code exists for the sole
2149 * purpose of converting a NEWLINE token into a SPACE
2150 * token, but only in the case where we have seen a
2151 * function-like macro name, but have not yet seen its
2152 * closing parenthesis.
2153 *
2154 * There's perhaps a more compact way to do this with
2155 * mid-rule actions in the grammar.
2156 *
2157 * I'm definitely not pleased with the complexity of
2158 * this code here.
2159 */
2160 if (parser->newline_as_space) {
2161 if (ret == '(') {
2162 parser->paren_count++;
2163 } else if (ret == ')') {
2164 parser->paren_count--;
2165 if (parser->paren_count == 0)
2166 parser->newline_as_space = 0;
2167 } else if (ret == NEWLINE) {
2168 ret = SPACE;
2169 } else if (ret != SPACE) {
2170 if (parser->paren_count == 0)
2171 parser->newline_as_space = 0;
2172 }
2173 } else if (parser->in_control_line) {
2174 if (ret == NEWLINE)
2175 parser->in_control_line = 0;
2176 }
2177 else if (ret == DEFINE_TOKEN || ret == UNDEF || ret == IF ||
2178 ret == IFDEF || ret == IFNDEF || ret == ELIF || ret == ELSE ||
2179 ret == ENDIF || ret == HASH_TOKEN) {
2180 parser->in_control_line = 1;
2181 } else if (ret == IDENTIFIER) {
2182 struct hash_entry *entry = _mesa_hash_table_search(parser->defines,
2183 yylval->str);
2184 macro_t *macro = entry ? entry->data : NULL;
2185 if (macro && macro->is_function) {
2186 parser->newline_as_space = 1;
2187 parser->paren_count = 0;
2188 }
2189 }
2190
2191 return ret;
2192 }
2193
2194 node = parser->lex_from_node;
2195
2196 if (node == NULL) {
2197 parser->lex_from_list = NULL;
2198 return NEWLINE;
2199 }
2200
2201 *yylval = node->token->value;
2202 ret = node->token->type;
2203
2204 parser->lex_from_node = node->next;
2205
2206 return ret;
2207 }
2208
2209 static void
2210 glcpp_parser_lex_from(glcpp_parser_t *parser, token_list_t *list)
2211 {
2212 token_node_t *node;
2213
2214 assert (parser->lex_from_list == NULL);
2215
2216 /* Copy list, eliminating any space tokens. */
2217 parser->lex_from_list = _token_list_create (parser);
2218
2219 for (node = list->head; node; node = node->next) {
2220 if (node->token->type == SPACE)
2221 continue;
2222 _token_list_append (parser, parser->lex_from_list, node->token);
2223 }
2224
2225 parser->lex_from_node = parser->lex_from_list->head;
2226
2227 /* It's possible the list consisted of nothing but whitespace. */
2228 if (parser->lex_from_node == NULL) {
2229 parser->lex_from_list = NULL;
2230 }
2231 }
2232
2233 static void
2234 _glcpp_parser_skip_stack_push_if(glcpp_parser_t *parser, YYLTYPE *loc,
2235 int condition)
2236 {
2237 skip_type_t current = SKIP_NO_SKIP;
2238 skip_node_t *node;
2239
2240 if (parser->skip_stack)
2241 current = parser->skip_stack->type;
2242
2243 node = linear_alloc_child(parser->linalloc, sizeof(skip_node_t));
2244 node->loc = *loc;
2245
2246 if (current == SKIP_NO_SKIP) {
2247 if (condition)
2248 node->type = SKIP_NO_SKIP;
2249 else
2250 node->type = SKIP_TO_ELSE;
2251 } else {
2252 node->type = SKIP_TO_ENDIF;
2253 }
2254
2255 node->has_else = false;
2256 node->next = parser->skip_stack;
2257 parser->skip_stack = node;
2258 }
2259
2260 static void
2261 _glcpp_parser_skip_stack_change_if(glcpp_parser_t *parser, YYLTYPE *loc,
2262 const char *type, int condition)
2263 {
2264 if (parser->skip_stack == NULL) {
2265 glcpp_error (loc, parser, "#%s without #if\n", type);
2266 return;
2267 }
2268
2269 if (parser->skip_stack->type == SKIP_TO_ELSE) {
2270 if (condition)
2271 parser->skip_stack->type = SKIP_NO_SKIP;
2272 } else {
2273 parser->skip_stack->type = SKIP_TO_ENDIF;
2274 }
2275 }
2276
2277 static void
2278 _glcpp_parser_skip_stack_pop(glcpp_parser_t *parser, YYLTYPE *loc)
2279 {
2280 skip_node_t *node;
2281
2282 if (parser->skip_stack == NULL) {
2283 glcpp_error (loc, parser, "#endif without #if\n");
2284 return;
2285 }
2286
2287 node = parser->skip_stack;
2288 parser->skip_stack = node->next;
2289 }
2290
2291 static void
2292 _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t version,
2293 const char *es_identifier,
2294 bool explicitly_set)
2295 {
2296 if (parser->version != 0)
2297 return;
2298
2299 parser->version = version;
2300
2301 add_builtin_define (parser, "__VERSION__", version);
2302
2303 parser->is_gles = (version == 100) ||
2304 (es_identifier && (strcmp(es_identifier, "es") == 0));
2305
2306 /* Add pre-defined macros. */
2307 if (parser->is_gles)
2308 add_builtin_define(parser, "GL_ES", 1);
2309 else if (version >= 150)
2310 add_builtin_define(parser, "GL_core_profile", 1);
2311
2312 /* Currently, all ES2/ES3 implementations support highp in the
2313 * fragment shader, so we always define this macro in ES2/ES3.
2314 * If we ever get a driver that doesn't support highp, we'll
2315 * need to add a flag to the gl_context and check that here.
2316 */
2317 if (version >= 130 || parser->is_gles)
2318 add_builtin_define (parser, "GL_FRAGMENT_PRECISION_HIGH", 1);
2319
2320 /* Add all the extension macros available in this context */
2321 if (parser->extensions)
2322 parser->extensions(parser->state, add_builtin_define, parser,
2323 version, parser->is_gles);
2324
2325 if (explicitly_set) {
2326 ralloc_asprintf_rewrite_tail(&parser->output, &parser->output_length,
2327 "#version %" PRIiMAX "%s%s", version,
2328 es_identifier ? " " : "",
2329 es_identifier ? es_identifier : "");
2330 }
2331 }
2332
2333 /* GLSL version if no version is explicitly specified. */
2334 #define IMPLICIT_GLSL_VERSION 110
2335
2336 /* GLSL ES version if no version is explicitly specified. */
2337 #define IMPLICIT_GLSL_ES_VERSION 100
2338
2339 void
2340 glcpp_parser_resolve_implicit_version(glcpp_parser_t *parser)
2341 {
2342 int language_version = parser->api == API_OPENGLES2 ?
2343 IMPLICIT_GLSL_ES_VERSION : IMPLICIT_GLSL_VERSION;
2344
2345 _glcpp_parser_handle_version_declaration(parser, language_version,
2346 NULL, false);
2347 }