Prevent structs/interface-blocks from claiming builtin types.
Structs and interface blocks allow a trailing identifier which is added
to the symbol table. This identifier is now prohibited from
overlapping built-in types.
Change-Id: I33b9d6156a27ce017e6744a05979748c04a04767
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/489516
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLDSLParser.cpp b/src/sksl/SkSLDSLParser.cpp
index 1ff1ebf..f7c8763 100644
--- a/src/sksl/SkSLDSLParser.cpp
+++ b/src/sksl/SkSLDSLParser.cpp
@@ -212,6 +212,17 @@
return true;
}
+bool DSLParser::checkIdentifier(Token* result) {
+ if (!this->checkNext(Token::Kind::TK_IDENTIFIER, result)) {
+ return false;
+ }
+ if (IsBuiltinType(this->text(*result))) {
+ this->pushback(std::move(*result));
+ return false;
+ }
+ return true;
+}
+
skstd::string_view DSLParser::text(Token token) {
return skstd::string_view(fText->data() + token.fOffset, token.fLength);
}
@@ -645,9 +656,9 @@
return {};
}
Token name;
- if (this->checkNext(Token::Kind::TK_IDENTIFIER, &name)) {
+ if (this->checkIdentifier(&name)) {
this->globalVarDeclarationEnd(this->position(name), modifiers, std::move(*type),
- this->text(name));
+ this->text(name));
} else {
this->expect(Token::Kind::TK_SEMICOLON, "';'");
}
@@ -901,7 +912,7 @@
skstd::string_view instanceName;
Token instanceNameToken;
SKSL_INT arraySize = 0;
- if (this->checkNext(Token::Kind::TK_IDENTIFIER, &instanceNameToken)) {
+ if (this->checkIdentifier(&instanceNameToken)) {
instanceName = this->text(instanceNameToken);
if (this->checkNext(Token::Kind::TK_LBRACKET)) {
arraySize = this->arraySize();
diff --git a/src/sksl/SkSLDSLParser.h b/src/sksl/SkSLDSLParser.h
index 8e2a6cc..cdd4f7b 100644
--- a/src/sksl/SkSLDSLParser.h
+++ b/src/sksl/SkSLDSLParser.h
@@ -89,6 +89,13 @@
bool checkNext(Token::Kind kind, Token* result = nullptr);
/**
+ * Behaves like checkNext(TK_IDENTIFIER), but also verifies that identifier is not a builtin
+ * type. If the token was actually a builtin type, false is returned (the next token is not
+ * considered to be an identifier).
+ */
+ bool checkIdentifier(Token* result = nullptr);
+
+ /**
* Reads the next non-whitespace token and generates an error if it is not the expected type.
* The 'expected' string is part of the error message, which reads:
*
diff --git a/tests/sksl/errors/InterfaceBlockReservedName.glsl b/tests/sksl/errors/InterfaceBlockReservedName.glsl
index 284b23c..49164ae 100644
--- a/tests/sksl/errors/InterfaceBlockReservedName.glsl
+++ b/tests/sksl/errors/InterfaceBlockReservedName.glsl
@@ -1,7 +1,4 @@
+### Compilation failed:
-IB {
- float f;
-} float;
-vec4 main() {
- return vec4(float.f);
-}
+error: 3: expected ';', but found 'float'
+1 error
diff --git a/tests/sksl/errors/StructVariableReservedName.glsl b/tests/sksl/errors/StructVariableReservedName.glsl
index 60bd4be..49164ae 100644
--- a/tests/sksl/errors/StructVariableReservedName.glsl
+++ b/tests/sksl/errors/StructVariableReservedName.glsl
@@ -1,8 +1,4 @@
+### Compilation failed:
-struct S {
- float f;
-};
-S float;
-vec4 main() {
- return vec4(float.f);
-}
+error: 3: expected ';', but found 'float'
+1 error