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