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