2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file slang_preprocess.c
31 #include "main/imports.h"
32 #include "shader/grammar/grammar_mesa.h"
33 #include "slang_preprocess.h"
35 LONGSTRING
static const char *slang_pp_directives_syn
=
36 #include "library/slang_pp_directives_syn.h"
39 LONGSTRING
static const char *slang_pp_expression_syn
=
40 #include "library/slang_pp_expression_syn.h"
43 LONGSTRING
static const char *slang_pp_version_syn
=
44 #include "library/slang_pp_version_syn.h"
48 grammar_error_to_log (slang_info_log
*log
)
53 grammar_get_last_error ((byte
*) (buf
), sizeof (buf
), &pos
);
54 slang_info_log_error (log
, buf
);
58 _slang_preprocess_version (const char *text
, GLuint
*version
, GLuint
*eaten
, slang_info_log
*log
)
64 id
= grammar_load_from_text ((const byte
*) (slang_pp_version_syn
));
66 grammar_error_to_log (log
);
70 if (!grammar_fast_check (id
, (const byte
*) (text
), &prod
, &size
, 8)) {
71 grammar_error_to_log (log
);
76 /* there can be multiple #version directives - grab the last one */
78 *version
= (GLuint
) (I
[0]) + (GLuint
) (I
[1]) * 100;
79 *eaten
= (GLuint
) (I
[2]) + ((GLuint
) (I
[3]) << 8) + ((GLuint
) (I
[4]) << 16) + ((GLuint
) (I
[5]) << 24);
82 grammar_alloc_free (prod
);
87 * The preprocessor does the following work.
88 * 1. Remove comments. Each comment block is replaced with a single space and if the
89 * block contains new-lines, they are preserved. This ensures that line numbers
90 * stay the same and if a comment block delimits two tokens, the are delitmited
91 * by the space after comment removal.
92 * 2. Remove preprocessor directives from the source string, checking their syntax and
93 * executing them if appropriate. Again, new-lines are preserved.
95 * 4. Tokenize the source string by ensuring there is at least one space between every
96 * two adjacent tokens.
102 pp_annotate (slang_string
*output
, const char *fmt
, ...)
109 _mesa_vsprintf (buffer
, fmt
, va
);
111 slang_string_pushs (output
, buffer
, _mesa_strlen (buffer
));
119 * The expression is executed on a fixed-sized stack. The PUSH macro makes a runtime
120 * check if the stack is not overflown by too complex expressions. In that situation the
121 * GLSL preprocessor should report internal compiler error.
122 * The BINARYDIV makes a runtime check if the divider is not 0. If it is, it reports
126 #define EXECUTION_STACK_SIZE 1024
131 slang_info_log_error (elog, "internal compiler error: preprocessor execution stack overflow.");\
139 assert (sp < EXECUTION_STACK_SIZE);\
151 #define BINARYDIV(op)\
157 slang_info_log_error (elog, "division by zero in preprocessor expression.");\
172 #define OP_LOGICALOR 2
173 #define OP_LOGICALAND 3
178 #define OP_NOTEQUAL 8
179 #define OP_LESSEQUAL 9
180 #define OP_GREATEREQUAL 10
182 #define OP_GREATER 12
183 #define OP_LEFTSHIFT 13
184 #define OP_RIGHTSHIFT 14
186 #define OP_SUBTRACT 16
187 #define OP_MULTIPLY 17
189 #define OP_MODULUS 19
193 #define OP_COMPLEMENT 23
196 execute_expression (slang_string
*output
, const byte
*code
, GLuint
*pi
, GLint
*result
,
197 slang_info_log
*elog
)
200 GLint stack
[EXECUTION_STACK_SIZE
];
201 GLuint sp
= EXECUTION_STACK_SIZE
;
203 while (code
[i
] != OP_END
) {
207 PUSH(_mesa_atoi ((const char *) (&code
[i
])));
208 i
+= _mesa_strlen ((const char *) (&code
[i
])) + 1;
234 case OP_GREATEREQUAL
:
281 /* Write-back the index skipping the OP_END. */
284 /* There should be exactly one value left on the stack. This is our result. */
286 pp_annotate (output
, "%d ", *result
);
287 assert (sp
== EXECUTION_STACK_SIZE
);
292 * Function execute_expressions() executes up to 2 expressions. The second expression is there
293 * for the #line directive which takes 1 or 2 expressions that indicate line and file numbers.
294 * If it fails, it returns 0. If it succeeds, it returns the number of executed expressions.
298 #define EXP_EXPRESSION 1
301 execute_expressions (slang_string
*output
, grammar eid
, const byte
*expr
, GLint results
[2],
302 slang_info_log
*elog
)
306 GLuint size
, count
= 0;
308 success
= grammar_fast_check (eid
, expr
, &code
, &size
, 64);
312 while (code
[i
++] == EXP_EXPRESSION
) {
315 if (!execute_expression (output
, code
, &i
, &results
[count
], elog
)) {
321 grammar_alloc_free (code
);
324 slang_info_log_error (elog
, "syntax error in preprocessor expression.");\
330 * The pp_symbol structure is used to hold macro definitions and macro formal parameters. The
331 * pp_symbols strcture is a collection of pp_symbol. It is used both for storing macro formal
332 * parameters and all global macro definitions. Making this unification wastes some memory,
333 * becuse macro formal parameters don't need further lists of symbols. We lose 8 bytes per
334 * formal parameter here, but making this we can use the same code to substitute macro parameters
335 * as well as macros in the source string.
340 struct pp_symbol_
*symbols
;
345 pp_symbols_init (pp_symbols
*self
)
347 self
->symbols
= NULL
;
352 pp_symbols_free (pp_symbols
*);
354 typedef struct pp_symbol_
357 slang_string replacement
;
358 pp_symbols parameters
;
362 pp_symbol_init (pp_symbol
*self
)
364 slang_string_init (&self
->name
);
365 slang_string_init (&self
->replacement
);
366 pp_symbols_init (&self
->parameters
);
370 pp_symbol_free (pp_symbol
*self
)
372 slang_string_free (&self
->name
);
373 slang_string_free (&self
->replacement
);
374 pp_symbols_free (&self
->parameters
);
378 pp_symbol_reset (pp_symbol
*self
)
380 /* Leave symbol name intact. */
381 slang_string_reset (&self
->replacement
);
382 pp_symbols_free (&self
->parameters
);
383 pp_symbols_init (&self
->parameters
);
387 pp_symbols_free (pp_symbols
*self
)
391 for (i
= 0; i
< self
->count
; i
++)
392 pp_symbol_free (&self
->symbols
[i
]);
393 _mesa_free (self
->symbols
);
397 pp_symbols_push (pp_symbols
*self
)
399 self
->symbols
= (pp_symbol
*) (_mesa_realloc (self
->symbols
, self
->count
* sizeof (pp_symbol
),
400 (self
->count
+ 1) * sizeof (pp_symbol
)));
401 if (self
->symbols
== NULL
)
403 pp_symbol_init (&self
->symbols
[self
->count
]);
404 return &self
->symbols
[self
->count
++];
408 pp_symbols_erase (pp_symbols
*self
, pp_symbol
*symbol
)
410 assert (symbol
>= self
->symbols
&& symbol
< self
->symbols
+ self
->count
);
413 pp_symbol_free (symbol
);
414 if (symbol
< self
->symbols
+ self
->count
)
415 _mesa_memcpy (symbol
, symbol
+ 1, sizeof (pp_symbol
) * (self
->symbols
+ self
->count
- symbol
));
416 self
->symbols
= (pp_symbol
*) (_mesa_realloc (self
->symbols
, (self
->count
+ 1) * sizeof (pp_symbol
),
417 self
->count
* sizeof (pp_symbol
)));
418 return self
->symbols
!= NULL
;
422 pp_symbols_find (pp_symbols
*self
, const char *name
)
426 for (i
= 0; i
< self
->count
; i
++)
427 if (_mesa_strcmp (name
, slang_string_cstr (&self
->symbols
[i
].name
)) == 0)
428 return &self
->symbols
[i
];
433 * The condition context of a single #if/#else/#endif level. Those can be nested, so there
434 * is a stack of condition contexts.
435 * There is a special global context on the bottom of the stack. It is there to simplify
441 GLboolean current
; /* The condition value of this level. */
442 GLboolean effective
; /* The effective product of current condition, outer level conditions
443 * and position within #if-#else-#endif sections. */
444 GLboolean else_allowed
; /* TRUE if in #if-#else section, FALSE if in #else-#endif section
445 * and for global context. */
446 GLboolean endif_required
; /* FALSE for global context only. */
449 /* Should be enuff. */
450 #define CONDITION_STACK_SIZE 64
454 pp_cond_ctx stack
[CONDITION_STACK_SIZE
];
459 pp_cond_stack_push (pp_cond_stack
*self
, slang_info_log
*elog
)
461 if (self
->top
== self
->stack
) {
462 slang_info_log_error (elog
, "internal compiler error: preprocessor condition stack overflow.");
470 pp_cond_stack_reevaluate (pp_cond_stack
*self
)
472 /* There must be at least 2 conditions on the stack - one global and one being evaluated. */
473 assert (self
->top
<= &self
->stack
[CONDITION_STACK_SIZE
- 2]);
475 self
->top
->effective
= self
->top
->current
&& self
->top
[1].effective
;
479 * Extension enables through #extension directive.
480 * NOTE: Currently, only enable/disable state is stored.
485 GLboolean MESA_shader_debug
; /* GL_MESA_shader_debug enable */
486 GLboolean ARB_texture_rectangle
; /* GL_ARB_texture_rectangle enable */
490 * Disable all extensions. Called at startup and on #extension all: disable.
493 pp_ext_disable_all (pp_ext
*self
)
495 self
->MESA_shader_debug
= GL_FALSE
;
499 pp_ext_init (pp_ext
*self
)
501 pp_ext_disable_all (self
);
502 self
->ARB_texture_rectangle
= GL_TRUE
;
503 /* Other initialization code goes here. */
507 pp_ext_set (pp_ext
*self
, const char *name
, GLboolean enable
)
509 if (_mesa_strcmp (name
, "MESA_shader_debug") == 0)
510 self
->MESA_shader_debug
= enable
;
511 else if (_mesa_strcmp (name
, "GL_ARB_texture_rectangle") == 0)
512 self
->ARB_texture_rectangle
= enable
;
513 /* Next extension name tests go here. */
520 * The state of preprocessor: current line, file and version number, list of all defined macros
521 * and the #if/#endif context.
531 slang_info_log
*elog
;
536 pp_state_init (pp_state
*self
, slang_info_log
*elog
)
545 pp_symbols_init (&self
->symbols
);
546 pp_ext_init (&self
->ext
);
549 /* Initialize condition stack and create the global context. */
550 self
->cond
.top
= &self
->cond
.stack
[CONDITION_STACK_SIZE
- 1];
551 self
->cond
.top
->current
= GL_TRUE
;
552 self
->cond
.top
->effective
= GL_TRUE
;
553 self
->cond
.top
->else_allowed
= GL_FALSE
;
554 self
->cond
.top
->endif_required
= GL_FALSE
;
558 pp_state_free (pp_state
*self
)
560 pp_symbols_free (&self
->symbols
);
563 #define IS_FIRST_ID_CHAR(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') || (x) == '_')
564 #define IS_NEXT_ID_CHAR(x) (IS_FIRST_ID_CHAR(x) || ((x) >= '0' && (x) <= '9'))
565 #define IS_WHITE(x) ((x) == ' ' || (x) == '\n')
566 #define IS_NULL(x) ((x) == '\0')
568 #define SKIP_WHITE(x) do { while (IS_WHITE(*(x))) (x)++; } while (GL_FALSE)
572 slang_string
*output
;
578 expand_defined (expand_state
*e
, slang_string
*buffer
)
580 GLboolean in_paren
= GL_FALSE
;
583 /* Parse the optional opening parenthesis. */
584 SKIP_WHITE(e
->input
);
585 if (*e
->input
== '(') {
588 SKIP_WHITE(e
->input
);
592 if (!IS_FIRST_ID_CHAR(*e
->input
)) {
593 slang_info_log_error (e
->state
->elog
,
594 "preprocess error: identifier expected after operator 'defined'.");
597 slang_string_reset (buffer
);
598 slang_string_pushc (buffer
, *e
->input
++);
599 while (IS_NEXT_ID_CHAR(*e
->input
))
600 slang_string_pushc (buffer
, *e
->input
++);
601 id
= slang_string_cstr (buffer
);
603 /* Check if the operand is defined. Output 1 if it is defined, output 0 if not. */
604 if (pp_symbols_find (&e
->state
->symbols
, id
) == NULL
)
605 slang_string_pushs (e
->output
, " 0 ", 3);
607 slang_string_pushs (e
->output
, " 1 ", 3);
609 /* Parse the closing parentehesis if the opening one was there. */
611 SKIP_WHITE(e
->input
);
612 if (*e
->input
!= ')') {
613 slang_info_log_error (e
->state
->elog
, "preprocess error: ')' expected.");
617 SKIP_WHITE(e
->input
);
623 expand (expand_state
*, pp_symbols
*);
626 expand_symbol (expand_state
*e
, pp_symbol
*symbol
)
630 /* If the macro has some parameters, we need to parse them. */
631 if (symbol
->parameters
.count
!= 0) {
634 /* Parse the opening parenthesis. */
635 SKIP_WHITE(e
->input
);
636 if (*e
->input
!= '(') {
637 slang_info_log_error (e
->state
->elog
, "preprocess error: '(' expected.");
641 SKIP_WHITE(e
->input
);
643 /* Parse macro actual parameters. This can be anything, separated by a colon.
645 for (i
= 0; i
< symbol
->parameters
.count
; i
++) {
646 GLuint nested_paren_count
= 0; /* track number of nested parentheses */
648 if (*e
->input
== ')') {
649 slang_info_log_error (e
->state
->elog
, "preprocess error: unexpected ')'.");
653 /* Eat all characters up to the comma or closing parentheses. */
654 pp_symbol_reset (&symbol
->parameters
.symbols
[i
]);
655 while (!IS_NULL(*e
->input
)) {
656 /* Exit loop only when all nested parens have been eaten. */
657 if (nested_paren_count
== 0 && (*e
->input
== ',' || *e
->input
== ')'))
660 /* Actually count nested parens here. */
661 if (*e
->input
== '(')
662 nested_paren_count
++;
663 else if (*e
->input
== ')')
664 nested_paren_count
--;
666 slang_string_pushc (&symbol
->parameters
.symbols
[i
].replacement
, *e
->input
++);
669 /* If it was not the last paremeter, skip the comma. Otherwise, skip the
670 * closing parentheses. */
671 if (i
+ 1 == symbol
->parameters
.count
) {
672 /* This is the last paremeter - skip the closing parentheses. */
673 if (*e
->input
!= ')') {
674 slang_info_log_error (e
->state
->elog
, "preprocess error: ')' expected.");
678 SKIP_WHITE(e
->input
);
681 /* Skip the separating comma. */
682 if (*e
->input
!= ',') {
683 slang_info_log_error (e
->state
->elog
, "preprocess error: ',' expected.");
687 SKIP_WHITE(e
->input
);
692 /* Expand the macro. Use its parameters as a priority symbol list to expand
693 * macro parameters correctly. */
694 es
.output
= e
->output
;
695 es
.input
= slang_string_cstr (&symbol
->replacement
);
697 slang_string_pushc (e
->output
, ' ');
698 if (!expand (&es
, &symbol
->parameters
))
700 slang_string_pushc (e
->output
, ' ');
705 * Function expand() expands source text from <input> to <output>. The expansion is made using
706 * the list passed in <symbols> parameter. It allows us to expand macro formal parameters with
707 * actual parameters. The global list of symbols from pp state is used when doing a recursive
712 expand (expand_state
*e
, pp_symbols
*symbols
)
714 while (!IS_NULL(*e
->input
)) {
715 if (IS_FIRST_ID_CHAR(*e
->input
)) {
719 /* Parse the identifier. */
720 slang_string_init (&buffer
);
721 slang_string_pushc (&buffer
, *e
->input
++);
722 while (IS_NEXT_ID_CHAR(*e
->input
))
723 slang_string_pushc (&buffer
, *e
->input
++);
724 id
= slang_string_cstr (&buffer
);
726 /* Now check if the identifier is special in some way. The "defined" identifier is
727 * actually an operator that we must handle here and expand it either to " 0 " or " 1 ".
728 * The other identifiers start with "__" and we expand it to appropriate values
729 * taken from the preprocessor state. */
730 if (_mesa_strcmp (id
, "defined") == 0) {
731 if (!expand_defined (e
, &buffer
))
734 else if (_mesa_strcmp (id
, "__LINE__") == 0) {
735 slang_string_pushc (e
->output
, ' ');
736 slang_string_pushi (e
->output
, e
->state
->line
);
737 slang_string_pushc (e
->output
, ' ');
739 else if (_mesa_strcmp (id
, "__FILE__") == 0) {
740 slang_string_pushc (e
->output
, ' ');
741 slang_string_pushi (e
->output
, e
->state
->file
);
742 slang_string_pushc (e
->output
, ' ');
744 else if (_mesa_strcmp (id
, "__VERSION__") == 0) {
745 slang_string_pushc (e
->output
, ' ');
746 slang_string_pushi (e
->output
, e
->state
->version
);
747 slang_string_pushc (e
->output
, ' ');
750 else if (_mesa_strcmp (id
, "GL_ES") == 0 ||
751 _mesa_strcmp (id
, "GL_FRAGMENT_PRECISION_HIGH") == 0) {
752 slang_string_pushc (e
->output
, ' ');
753 slang_string_pushi (e
->output
, '1');
754 slang_string_pushc (e
->output
, ' ');
760 /* The list of symbols from <symbols> take precedence over the list from <state>.
761 * Note that in some cases this is the same list so avoid double look-up. */
762 symbol
= pp_symbols_find (symbols
, id
);
763 if (symbol
== NULL
&& symbols
!= &e
->state
->symbols
)
764 symbol
= pp_symbols_find (&e
->state
->symbols
, id
);
766 /* If the symbol was found, recursively expand its definition. */
767 if (symbol
!= NULL
) {
768 if (!expand_symbol (e
, symbol
)) {
769 slang_string_free (&buffer
);
774 slang_string_push (e
->output
, &buffer
);
777 slang_string_free (&buffer
);
779 else if (IS_WHITE(*e
->input
)) {
780 slang_string_pushc (e
->output
, *e
->input
++);
783 while (!IS_WHITE(*e
->input
) && !IS_NULL(*e
->input
) && !IS_FIRST_ID_CHAR(*e
->input
))
784 slang_string_pushc (e
->output
, *e
->input
++);
791 parse_if (slang_string
*output
, const byte
*prod
, GLuint
*pi
, GLint
*result
, pp_state
*state
,
797 text
= (const char *) (&prod
[*pi
]);
798 len
= _mesa_strlen (text
);
800 if (state
->cond
.top
->effective
) {
806 /* Expand the expression. */
807 slang_string_init (&expr
);
811 if (!expand (&es
, &state
->symbols
))
814 /* Execute the expression. */
815 count
= execute_expressions (output
, eid
, (const byte
*) (slang_string_cstr (&expr
)),
816 results
, state
->elog
);
817 slang_string_free (&expr
);
820 *result
= results
[0];
823 /* The directive is dead. */
831 #define ESCAPE_TOKEN 0
834 #define TOKEN_DEFINE 1
835 #define TOKEN_UNDEF 2
839 #define TOKEN_ENDIF 6
840 #define TOKEN_ERROR 7
841 #define TOKEN_PRAGMA 8
842 #define TOKEN_EXTENSION 9
843 #define TOKEN_LINE 10
846 #define PARAM_PARAMETER 1
848 #define BEHAVIOR_REQUIRE 1
849 #define BEHAVIOR_ENABLE 2
850 #define BEHAVIOR_WARN 3
851 #define BEHAVIOR_DISABLE 4
854 preprocess_source (slang_string
*output
, const char *source
, grammar pid
, grammar eid
,
855 slang_info_log
*elog
)
857 static const char *predefined
[] = {
863 "GL_FRAGMENT_PRECISION_HIGH",
871 if (!grammar_fast_check (pid
, (const byte
*) (source
), &prod
, &size
, 65536)) {
872 grammar_error_to_log (elog
);
876 pp_state_init (&state
, elog
);
878 /* add the predefined symbols to the symbol table */
879 for (i
= 0; predefined
[i
]; i
++) {
880 pp_symbol
*symbol
= NULL
;
881 symbol
= pp_symbols_push(&state
.symbols
);
883 slang_string_pushs(&symbol
->name
,
884 predefined
[i
], _mesa_strlen(predefined
[i
]));
889 if (prod
[i
] != ESCAPE_TOKEN
) {
890 if (state
.cond
.top
->effective
) {
894 /* Eat only one line of source code to expand it.
895 * FIXME: This approach has one drawback. If a macro with parameters spans across
896 * multiple lines, the preprocessor will raise an error. */
897 slang_string_init (&input
);
898 while (prod
[i
] != '\0' && prod
[i
] != '\n')
899 slang_string_pushc (&input
, prod
[i
++]);
901 slang_string_pushc (&input
, prod
[i
++]);
903 /* Increment line number. */
907 es
.input
= slang_string_cstr (&input
);
909 if (!expand (&es
, &state
.symbols
))
912 slang_string_free (&input
);
915 /* Condition stack is disabled - keep track on line numbers and output only newlines. */
916 if (prod
[i
] == '\n') {
918 /*pp_annotate (output, "%c", prod[i]);*/
921 /*pp_annotate (output, "%c", prod[i]);*/
934 /* End of source string.
935 * Check if all #ifs have been terminated by matching #endifs.
936 * On condition stack there should be only the global condition context. */
937 if (state
.cond
.top
->endif_required
) {
938 slang_info_log_error (elog
, "end of source without matching #endif.");
945 pp_symbol
*symbol
= NULL
;
947 /* Parse macro name. */
948 id
= (const char *) (&prod
[i
]);
949 idlen
= _mesa_strlen (id
);
950 if (state
.cond
.top
->effective
) {
951 pp_annotate (output
, "// #define %s(", id
);
953 /* If the symbol is already defined, override it. */
954 symbol
= pp_symbols_find (&state
.symbols
, id
);
955 if (symbol
== NULL
) {
956 symbol
= pp_symbols_push (&state
.symbols
);
959 slang_string_pushs (&symbol
->name
, id
, idlen
);
962 pp_symbol_reset (symbol
);
967 /* Parse optional macro parameters. */
968 while (prod
[i
++] != PARAM_END
) {
969 if (state
.cond
.top
->effective
) {
972 id
= (const char *) (&prod
[i
]);
973 idlen
= _mesa_strlen (id
);
974 pp_annotate (output
, "%s, ", id
);
975 param
= pp_symbols_push (&symbol
->parameters
);
978 slang_string_pushs (¶m
->name
, id
, idlen
);
983 /* Parse macro replacement. */
984 id
= (const char *) (&prod
[i
]);
985 idlen
= _mesa_strlen (id
);
986 if (state
.cond
.top
->effective
) {
987 pp_annotate (output
, ") %s", id
);
988 slang_string_pushs (&symbol
->replacement
, id
, idlen
);
995 id
= (const char *) (&prod
[i
]);
996 i
+= _mesa_strlen (id
) + 1;
997 if (state
.cond
.top
->effective
) {
1000 pp_annotate (output
, "// #undef %s", id
);
1001 /* Try to find symbol with given name and remove it. */
1002 symbol
= pp_symbols_find (&state
.symbols
, id
);
1004 if (!pp_symbols_erase (&state
.symbols
, symbol
))
1013 /* Parse #if expression end execute it. */
1014 pp_annotate (output
, "// #if ");
1015 if (!parse_if (output
, prod
, &i
, &result
, &state
, eid
))
1018 /* Push new condition on the stack. */
1019 if (!pp_cond_stack_push (&state
.cond
, state
.elog
))
1021 state
.cond
.top
->current
= result
? GL_TRUE
: GL_FALSE
;
1022 state
.cond
.top
->else_allowed
= GL_TRUE
;
1023 state
.cond
.top
->endif_required
= GL_TRUE
;
1024 pp_cond_stack_reevaluate (&state
.cond
);
1029 /* Check if #else is alloved here. */
1030 if (!state
.cond
.top
->else_allowed
) {
1031 slang_info_log_error (elog
, "#else without matching #if.");
1035 /* Negate current condition and reevaluate it. */
1036 state
.cond
.top
->current
= !state
.cond
.top
->current
;
1037 state
.cond
.top
->else_allowed
= GL_FALSE
;
1038 pp_cond_stack_reevaluate (&state
.cond
);
1039 if (state
.cond
.top
->effective
)
1040 pp_annotate (output
, "// #else");
1044 /* Check if #elif is alloved here. */
1045 if (!state
.cond
.top
->else_allowed
) {
1046 slang_info_log_error (elog
, "#elif without matching #if.");
1050 /* Negate current condition and reevaluate it. */
1051 state
.cond
.top
->current
= !state
.cond
.top
->current
;
1052 pp_cond_stack_reevaluate (&state
.cond
);
1054 if (state
.cond
.top
->effective
)
1055 pp_annotate (output
, "// #elif ");
1060 /* Parse #elif expression end execute it. */
1061 if (!parse_if (output
, prod
, &i
, &result
, &state
, eid
))
1064 /* Update current condition and reevaluate it. */
1065 state
.cond
.top
->current
= result
? GL_TRUE
: GL_FALSE
;
1066 pp_cond_stack_reevaluate (&state
.cond
);
1071 /* Check if #endif is alloved here. */
1072 if (!state
.cond
.top
->endif_required
) {
1073 slang_info_log_error (elog
, "#endif without matching #if.");
1077 /* Pop the condition off the stack. */
1079 if (state
.cond
.top
->effective
)
1080 pp_annotate (output
, "// #endif");
1083 case TOKEN_EXTENSION
:
1084 /* Parse the extension name. */
1085 id
= (const char *) (&prod
[i
]);
1086 i
+= _mesa_strlen (id
) + 1;
1087 if (state
.cond
.top
->effective
)
1088 pp_annotate (output
, "// #extension %s: ", id
);
1090 /* Parse and apply extension behavior. */
1091 if (state
.cond
.top
->effective
) {
1092 switch (prod
[i
++]) {
1094 case BEHAVIOR_REQUIRE
:
1095 pp_annotate (output
, "require");
1096 if (!pp_ext_set (&state
.ext
, id
, GL_TRUE
)) {
1097 if (_mesa_strcmp (id
, "all") == 0) {
1098 slang_info_log_error (elog
, "require: bad behavior for #extension all.");
1102 slang_info_log_error (elog
, "%s: required extension is not supported.", id
);
1108 case BEHAVIOR_ENABLE
:
1109 pp_annotate (output
, "enable");
1110 if (!pp_ext_set (&state
.ext
, id
, GL_TRUE
)) {
1111 if (_mesa_strcmp (id
, "all") == 0) {
1112 slang_info_log_error (elog
, "enable: bad behavior for #extension all.");
1116 slang_info_log_warning (elog
, "%s: enabled extension is not supported.", id
);
1122 pp_annotate (output
, "warn");
1123 if (!pp_ext_set (&state
.ext
, id
, GL_TRUE
)) {
1124 if (_mesa_strcmp (id
, "all") != 0) {
1125 slang_info_log_warning (elog
, "%s: enabled extension is not supported.", id
);
1130 case BEHAVIOR_DISABLE
:
1131 pp_annotate (output
, "disable");
1132 if (!pp_ext_set (&state
.ext
, id
, GL_FALSE
)) {
1133 if (_mesa_strcmp (id
, "all") == 0) {
1134 pp_ext_disable_all (&state
.ext
);
1137 slang_info_log_warning (elog
, "%s: disabled extension is not supported.", id
);
1149 id
= (const char *) (&prod
[i
]);
1150 i
+= _mesa_strlen (id
) + 1;
1152 if (state
.cond
.top
->effective
) {
1153 slang_string buffer
;
1158 slang_string_init (&buffer
);
1160 es
.output
= &buffer
;
1163 if (!expand (&es
, &state
.symbols
))
1166 pp_annotate (output
, "// #line ");
1167 count
= execute_expressions (output
, eid
,
1168 (const byte
*) (slang_string_cstr (&buffer
)),
1169 results
, state
.elog
);
1170 slang_string_free (&buffer
);
1174 state
.line
= results
[0] - 1;
1176 state
.file
= results
[1];
1183 /* Check for missing #endifs. */
1184 if (state
.cond
.top
->endif_required
) {
1185 slang_info_log_error (elog
, "#endif expected but end of source found.");
1189 grammar_alloc_free(prod
);
1190 pp_state_free (&state
);
1194 grammar_alloc_free(prod
);
1195 pp_state_free (&state
);
1200 _slang_preprocess_directives (slang_string
*output
, const char *input
, slang_info_log
*elog
)
1205 pid
= grammar_load_from_text ((const byte
*) (slang_pp_directives_syn
));
1207 grammar_error_to_log (elog
);
1210 eid
= grammar_load_from_text ((const byte
*) (slang_pp_expression_syn
));
1212 grammar_error_to_log (elog
);
1213 grammar_destroy (pid
);
1216 success
= preprocess_source (output
, input
, pid
, eid
, elog
);
1217 grammar_destroy (eid
);
1218 grammar_destroy (pid
);