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>
This commit is contained in:
John Stiles 2021-12-28 13:50:14 -05:00 committed by SkCQ
parent 48432e133e
commit 106776364d
4 changed files with 27 additions and 16 deletions

View File

@ -212,6 +212,17 @@ bool DSLParser::expectIdentifier(Token* result) {
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 @@ SkTArray<dsl::DSLGlobalVar> DSLParser::structVarDeclaration(const DSLModifiers&
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 @@ bool DSLParser::interfaceBlock(const dsl::DSLModifiers& modifiers) {
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();

View File

@ -88,6 +88,13 @@ private:
*/
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:

View File

@ -1,7 +1,4 @@
### Compilation failed:
IB {
float f;
} float;
vec4 main() {
return vec4(float.f);
}
error: 3: expected ';', but found 'float'
1 error

View File

@ -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