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