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