summaryrefslogtreecommitdiffstats
path: root/src/glsl/glcpp/glcpp-parse.y
diff options
context:
space:
mode:
authorCarl Worth <[email protected]>2014-06-13 14:54:46 -0700
committerCarl Worth <[email protected]>2014-07-09 12:05:13 -0700
commitec6222ef014ca336e540e13fda423fd9f927feb1 (patch)
treeaefbb036e60e8501788379868e0ff797dea6e650 /src/glsl/glcpp/glcpp-parse.y
parent9794f8f2452b796740a30abf6d6454452ff3f028 (diff)
glsl/glcpp: Add short-circuiting for || and && in #if/#elif for OpenGL ES.
The GLSL ES Specification 3.00.4 says: #if, #ifdef, #ifndef, #else, #elif, and #endif are defined to operate as for C++ except for the following: ... • Undefined identifiers not consumed by the defined operator do not default to '0'. Use of such identifiers causes an error. [Page 11 (page 127 of the PDF file)] as well as: The semantics of applying operators in the preprocessor match those standard in the C++ preprocessor with the following exceptions: • The 2nd operand in a logical and ('&&') operation is evaluated if and only if the 1st operand evaluates to non-zero. • The 2nd operand in a logical or ('||') operation is evaluated if and only if the 1st operand evaluates to zero. If an operand is not evaluated, the presence of undefined identifiers in the operand will not cause an error. (Note that neither of these deviations from C++ preprocessor behavior apply to non-ES GLSL, at least as of specfication version 4.30.6). The first portion of this, (generating an error for an undefined macro in an (short-circuiting to squelch errors), was not implemented previously, but is implemented in this commit. A test is added for "make check" to ensure this behavior. Note: The change as implemented does make the error message a bit less precise, (it just states that an undefined macro was encountered, but not the name of the macro). This commit fixes the following Khronos GLES3 conformance test: undefined_identifiers.valid_undefined_identifier_1_vertex undefined_identifiers.valid_undefined_identifier_1_fragment undefined_identifiers.valid_undefined_identifier_2_vertex undefined_identifiers.valid_undefined_identifier_2_fragment Reviewed-by: Kenneth Graunke <[email protected]>
Diffstat (limited to 'src/glsl/glcpp/glcpp-parse.y')
-rw-r--r--src/glsl/glcpp/glcpp-parse.y106
1 files changed, 76 insertions, 30 deletions
diff --git a/src/glsl/glcpp/glcpp-parse.y b/src/glsl/glcpp/glcpp-parse.y
index 82e310270d6..68e4ace012e 100644
--- a/src/glsl/glcpp/glcpp-parse.y
+++ b/src/glsl/glcpp/glcpp-parse.y
@@ -166,7 +166,8 @@ add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
%expect 0
%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE FUNC_IDENTIFIER OBJ_IDENTIFIER HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_LINE HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING LINE_EXPANDED NEWLINE OTHER PLACEHOLDER SPACE
%token PASTE
-%type <ival> expression INTEGER operator SPACE integer_constant
+%type <ival> INTEGER operator SPACE integer_constant
+%type <expression_value> expression
%type <str> IDENTIFIER FUNC_IDENTIFIER OBJ_IDENTIFIER INTEGER_STRING OTHER
%type <string_list> identifier_list
%type <token> preprocessing_token conditional_token
@@ -216,10 +217,14 @@ line:
expanded_line:
IF_EXPANDED expression NEWLINE {
- _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
+ if (parser->is_gles && $2.has_undefined)
+ glcpp_error(& @1, parser, "undefined macro in expression (illegal in GLES)");
+ _glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
}
| ELIF_EXPANDED expression NEWLINE {
- _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
+ if (parser->is_gles && $2.has_undefined)
+ glcpp_error(& @1, parser, "undefined macro in expression (illegal in GLES)");
+ _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
}
| LINE_EXPANDED integer_constant NEWLINE {
parser->has_new_line_number = 1;
@@ -412,87 +417,128 @@ integer_constant:
}
expression:
- integer_constant
+ integer_constant {
+ $$.value = $1;
+ $$.has_undefined = false;
+ }
| IDENTIFIER {
+ $$.value = 0;
if (parser->is_gles)
- glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $1);
- $$ = 0;
+ $$.has_undefined = true;
+ else
+ $$.has_undefined = false;
}
| expression OR expression {
- $$ = $1 || $3;
+ $$.value = $1.value || $3.value;
+
+ /* Short-circuit: Only flag undefined from right side
+ * if left side evaluates to false.
+ */
+ if ($1.value)
+ $$.has_undefined = $1.has_undefined;
+ else
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression AND expression {
- $$ = $1 && $3;
+ $$.value = $1.value && $3.value;
+
+ /* Short-circuit: Only flag undefined from right-side
+ * if left side evaluates to true.
+ */
+ if ($1.value)
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
+ else
+ $$.has_undefined = $1.has_undefined;
}
| expression '|' expression {
- $$ = $1 | $3;
+ $$.value = $1.value | $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '^' expression {
- $$ = $1 ^ $3;
+ $$.value = $1.value ^ $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '&' expression {
- $$ = $1 & $3;
+ $$.value = $1.value & $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression NOT_EQUAL expression {
- $$ = $1 != $3;
+ $$.value = $1.value != $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression EQUAL expression {
- $$ = $1 == $3;
+ $$.value = $1.value == $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression GREATER_OR_EQUAL expression {
- $$ = $1 >= $3;
+ $$.value = $1.value >= $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression LESS_OR_EQUAL expression {
- $$ = $1 <= $3;
+ $$.value = $1.value <= $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '>' expression {
- $$ = $1 > $3;
+ $$.value = $1.value > $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '<' expression {
- $$ = $1 < $3;
+ $$.value = $1.value < $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression RIGHT_SHIFT expression {
- $$ = $1 >> $3;
+ $$.value = $1.value >> $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression LEFT_SHIFT expression {
- $$ = $1 << $3;
+ $$.value = $1.value << $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '-' expression {
- $$ = $1 - $3;
+ $$.value = $1.value - $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '+' expression {
- $$ = $1 + $3;
+ $$.value = $1.value + $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '%' expression {
- if ($3 == 0) {
+ if ($3.value == 0) {
yyerror (& @1, parser,
"zero modulus in preprocessor directive");
} else {
- $$ = $1 % $3;
+ $$.value = $1.value % $3.value;
}
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '/' expression {
- if ($3 == 0) {
+ if ($3.value == 0) {
yyerror (& @1, parser,
"division by 0 in preprocessor directive");
} else {
- $$ = $1 / $3;
+ $$.value = $1.value / $3.value;
}
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| expression '*' expression {
- $$ = $1 * $3;
+ $$.value = $1.value * $3.value;
+ $$.has_undefined = $1.has_undefined || $3.has_undefined;
}
| '!' expression %prec UNARY {
- $$ = ! $2;
+ $$.value = ! $2.value;
+ $$.has_undefined = $2.has_undefined;
}
| '~' expression %prec UNARY {
- $$ = ~ $2;
+ $$.value = ~ $2.value;
+ $$.has_undefined = $2.has_undefined;
}
| '-' expression %prec UNARY {
- $$ = - $2;
+ $$.value = - $2.value;
+ $$.has_undefined = $2.has_undefined;
}
| '+' expression %prec UNARY {
- $$ = + $2;
+ $$.value = + $2.value;
+ $$.has_undefined = $2.has_undefined;
}
| '(' expression ')' {
$$ = $2;