unified ASTLayout/Layout and ASTModifiers/Modifiers
BUG=skia:
Change-Id: Ib4c2c94401e586e93e926776e13c0f7c12212f7e
Reviewed-on: https://skia-review.googlesource.com/5268
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/sksl/SkSLParser.cpp b/src/sksl/SkSLParser.cpp
index 80ef870..1b7cd9d 100644
--- a/src/sksl/SkSLParser.cpp
+++ b/src/sksl/SkSLParser.cpp
@@ -4,7 +4,7 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
-
+
#include "stdio.h"
#include "SkSLParser.h"
#include "SkSLToken.h"
@@ -69,6 +69,7 @@
#include "ast/SkSLASTVarDeclarationStatement.h"
#include "ast/SkSLASTWhileStatement.h"
#include "ir/SkSLSymbolTable.h"
+#include "ir/SkSLModifiers.h"
#include "ir/SkSLType.h"
namespace SkSL {
@@ -98,7 +99,7 @@
Parser* fParser;
};
-Parser::Parser(SkString text, SymbolTable& types, ErrorReporter& errors)
+Parser::Parser(SkString text, SymbolTable& types, ErrorReporter& errors)
: fPushback(Position(-1, -1), Token::INVALID_TOKEN, SkString())
, fTypes(types)
, fErrors(errors) {
@@ -229,7 +230,7 @@
result = Modifiers::kHighp_Flag;
break;
default:
- this->error(p.fPosition, "expected 'lowp', 'mediump', or 'highp', but found '" +
+ this->error(p.fPosition, "expected 'lowp', 'mediump', or 'highp', but found '" +
p.fText + "'");
return nullptr;
}
@@ -241,7 +242,7 @@
return std::unique_ptr<ASTDeclaration>(new ASTPrecision(p.fPosition, result));
}
-/* DIRECTIVE(#version) INT_LITERAL ("es" | "compatibility")? |
+/* DIRECTIVE(#version) INT_LITERAL ("es" | "compatibility")? |
DIRECTIVE(#extension) IDENTIFIER COLON IDENTIFIER */
std::unique_ptr<ASTDeclaration> Parser::directive() {
Token start;
@@ -269,7 +270,7 @@
if (!this->expect(Token::IDENTIFIER, "an identifier")) {
return nullptr;
}
- return std::unique_ptr<ASTDeclaration>(new ASTExtension(start.fPosition,
+ return std::unique_ptr<ASTDeclaration>(new ASTExtension(start.fPosition,
std::move(name.fText)));
} else {
this->error(start.fPosition, "unsupported directive '" + start.fText + "'");
@@ -277,10 +278,10 @@
}
}
-/* modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter
+/* modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter
(COMMA parameter)* RPAREN (block | SEMICOLON)) | SEMICOLON) | interfaceBlock) */
std::unique_ptr<ASTDeclaration> Parser::declaration() {
- ASTModifiers modifiers = this->modifiers();
+ Modifiers modifiers = this->modifiers();
Token lookahead = this->peek();
if (lookahead.fKind == Token::IDENTIFIER && !this->isType(lookahead.fText)) {
// we have an identifier that's not a type, could be the start of an interface block
@@ -330,9 +331,9 @@
return nullptr;
}
}
- return std::unique_ptr<ASTDeclaration>(new ASTFunction(name.fPosition, std::move(type),
- std::move(name.fText),
- std::move(parameters),
+ return std::unique_ptr<ASTDeclaration>(new ASTFunction(name.fPosition, std::move(type),
+ std::move(name.fText),
+ std::move(parameters),
std::move(body)));
} else {
return this->varDeclarationEnd(modifiers, std::move(type), name.fText);
@@ -341,7 +342,7 @@
/* modifiers type IDENTIFIER varDeclarationEnd */
std::unique_ptr<ASTVarDeclarations> Parser::varDeclarations() {
- ASTModifiers modifiers = this->modifiers();
+ Modifiers modifiers = this->modifiers();
std::unique_ptr<ASTType> type(this->type());
if (!type) {
return nullptr;
@@ -393,25 +394,25 @@
return nullptr;
}
fTypes.add(name.fText, std::unique_ptr<Type>(new Type(name.fText, fields)));
- return std::unique_ptr<ASTType>(new ASTType(name.fPosition, name.fText,
+ return std::unique_ptr<ASTType>(new ASTType(name.fPosition, name.fText,
ASTType::kStruct_Kind));
}
/* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */
-std::unique_ptr<ASTVarDeclarations> Parser::structVarDeclaration(ASTModifiers modifiers) {
+std::unique_ptr<ASTVarDeclarations> Parser::structVarDeclaration(Modifiers modifiers) {
std::unique_ptr<ASTType> type = this->structDeclaration();
if (!type) {
return nullptr;
}
if (peek().fKind == Token::IDENTIFIER) {
Token name = this->nextToken();
- std::unique_ptr<ASTVarDeclarations> result = this->varDeclarationEnd(modifiers,
- std::move(type),
+ std::unique_ptr<ASTVarDeclarations> result = this->varDeclarationEnd(modifiers,
+ std::move(type),
std::move(name.fText));
if (result) {
for (const auto& var : result->fVars) {
if (var.fValue) {
- this->error(var.fValue->fPosition,
+ this->error(var.fValue->fPosition,
"struct variables cannot be initialized");
}
}
@@ -422,9 +423,9 @@
return nullptr;
}
-/* (LBRACKET expression? RBRACKET)* (EQ expression)? (COMMA IDENTIFER
+/* (LBRACKET expression? RBRACKET)* (EQ expression)? (COMMA IDENTIFER
(LBRACKET expression? RBRACKET)* (EQ expression)?)* SEMICOLON */
-std::unique_ptr<ASTVarDeclarations> Parser::varDeclarationEnd(ASTModifiers mods,
+std::unique_ptr<ASTVarDeclarations> Parser::varDeclarationEnd(Modifiers mods,
std::unique_ptr<ASTType> type,
SkString name) {
std::vector<ASTVarDeclaration> vars;
@@ -497,7 +498,7 @@
/* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */
std::unique_ptr<ASTParameter> Parser::parameter() {
- ASTModifiers modifiers = this->modifiersWithDefaults(ASTModifiers::kIn_Flag);
+ Modifiers modifiers = this->modifiersWithDefaults(Modifiers::kIn_Flag);
std::unique_ptr<ASTType> type = this->type();
if (!type) {
return nullptr;
@@ -518,8 +519,8 @@
return nullptr;
}
}
- return std::unique_ptr<ASTParameter>(new ASTParameter(name.fPosition, modifiers,
- std::move(type), name.fText,
+ return std::unique_ptr<ASTParameter>(new ASTParameter(name.fPosition, modifiers,
+ std::move(type), name.fText,
std::move(sizes)));
}
@@ -536,7 +537,7 @@
}
/* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */
-ASTLayout Parser::layout() {
+Layout Parser::layout() {
int location = -1;
int binding = -1;
int index = -1;
@@ -546,14 +547,14 @@
bool originUpperLeft = false;
bool overrideCoverage = false;
bool blendSupportAllEquations = false;
- ASTLayout::Format format = ASTLayout::Format::kUnspecified;
+ Layout::Format format = Layout::Format::kUnspecified;
bool pushConstant = false;
if (this->peek().fKind == Token::LAYOUT) {
this->nextToken();
if (!this->expect(Token::LPAREN, "'('")) {
- return ASTLayout(location, binding, index, set, builtin, inputAttachmentIndex,
- originUpperLeft, overrideCoverage, blendSupportAllEquations, format,
- pushConstant);
+ return Layout(location, binding, index, set, builtin, inputAttachmentIndex,
+ originUpperLeft, overrideCoverage, blendSupportAllEquations, format,
+ pushConstant);
}
for (;;) {
Token t = this->nextToken();
@@ -575,7 +576,7 @@
overrideCoverage = true;
} else if (t.fText == "blend_support_all_equations") {
blendSupportAllEquations = true;
- } else if (ASTLayout::ReadFormat(t.fText, &format)) {
+ } else if (Layout::ReadFormat(t.fText, &format)) {
// AST::ReadFormat stored the result in 'format'.
} else if (t.fText == "push_constant") {
pushConstant = true;
@@ -592,68 +593,68 @@
}
}
}
- return ASTLayout(location, binding, index, set, builtin, inputAttachmentIndex, originUpperLeft,
+ return Layout(location, binding, index, set, builtin, inputAttachmentIndex, originUpperLeft,
overrideCoverage, blendSupportAllEquations, format, pushConstant);
}
/* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE)* */
-ASTModifiers Parser::modifiers() {
- ASTLayout layout = this->layout();
+Modifiers Parser::modifiers() {
+ Layout layout = this->layout();
int flags = 0;
for (;;) {
// TODO: handle duplicate / incompatible flags
switch (peek().fKind) {
case Token::UNIFORM:
this->nextToken();
- flags |= ASTModifiers::kUniform_Flag;
+ flags |= Modifiers::kUniform_Flag;
break;
case Token::CONST:
this->nextToken();
- flags |= ASTModifiers::kConst_Flag;
+ flags |= Modifiers::kConst_Flag;
break;
case Token::IN:
this->nextToken();
- flags |= ASTModifiers::kIn_Flag;
+ flags |= Modifiers::kIn_Flag;
break;
case Token::OUT:
this->nextToken();
- flags |= ASTModifiers::kOut_Flag;
+ flags |= Modifiers::kOut_Flag;
break;
case Token::INOUT:
this->nextToken();
- flags |= ASTModifiers::kIn_Flag;
- flags |= ASTModifiers::kOut_Flag;
+ flags |= Modifiers::kIn_Flag;
+ flags |= Modifiers::kOut_Flag;
break;
case Token::LOWP:
this->nextToken();
- flags |= ASTModifiers::kLowp_Flag;
+ flags |= Modifiers::kLowp_Flag;
break;
case Token::MEDIUMP:
this->nextToken();
- flags |= ASTModifiers::kMediump_Flag;
+ flags |= Modifiers::kMediump_Flag;
break;
case Token::HIGHP:
this->nextToken();
- flags |= ASTModifiers::kHighp_Flag;
+ flags |= Modifiers::kHighp_Flag;
break;
case Token::FLAT:
this->nextToken();
- flags |= ASTModifiers::kFlat_Flag;
+ flags |= Modifiers::kFlat_Flag;
break;
case Token::NOPERSPECTIVE:
this->nextToken();
- flags |= ASTModifiers::kNoPerspective_Flag;
+ flags |= Modifiers::kNoPerspective_Flag;
break;
default:
- return ASTModifiers(layout, flags);
+ return Modifiers(layout, flags);
}
}
}
-ASTModifiers Parser::modifiersWithDefaults(int defaultFlags) {
- ASTModifiers result = this->modifiers();
+Modifiers Parser::modifiersWithDefaults(int defaultFlags) {
+ Modifiers result = this->modifiers();
if (!result.fFlags) {
- return ASTModifiers(result.fLayout, defaultFlags);
+ return Modifiers(result.fLayout, defaultFlags);
}
return result;
}
@@ -681,8 +682,8 @@
case Token::LBRACE:
return this->block();
case Token::SEMICOLON:
- this->nextToken();
- return std::unique_ptr<ASTStatement>(new ASTBlock(start.fPosition,
+ this->nextToken();
+ return std::unique_ptr<ASTStatement>(new ASTBlock(start.fPosition,
std::vector<std::unique_ptr<ASTStatement>>()));
case Token::CONST: // fall through
case Token::HIGHP: // fall through
@@ -706,7 +707,7 @@
// fall through
default:
return this->expressionStatement();
- }
+ }
}
/* IDENTIFIER(type) */
@@ -719,25 +720,25 @@
this->error(type.fPosition, ("no type named '" + type.fText + "'").c_str());
return nullptr;
}
- return std::unique_ptr<ASTType>(new ASTType(type.fPosition, std::move(type.fText),
+ return std::unique_ptr<ASTType>(new ASTType(type.fPosition, std::move(type.fText),
ASTType::kIdentifier_Kind));
}
/* IDENTIFIER LBRACE varDeclaration* RBRACE */
-std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(ASTModifiers mods) {
+std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(Modifiers mods) {
Token name;
if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
return nullptr;
}
if (peek().fKind != Token::LBRACE) {
// we only get into interfaceBlock if we found a top-level identifier which was not a type.
- // 99% of the time, the user was not actually intending to create an interface block, so
+ // 99% of the time, the user was not actually intending to create an interface block, so
// it's better to report it as an unknown type
this->error(name.fPosition, "no type named '" + name.fText + "'");
return nullptr;
}
this->nextToken();
- std::vector<std::unique_ptr<ASTVarDeclarations>> decls;
+ std::vector<std::unique_ptr<ASTVarDeclarations>> decls;
while (this->peek().fKind != Token::RBRACE) {
std::unique_ptr<ASTVarDeclarations> decl = this->varDeclarations();
if (!decl) {
@@ -751,7 +752,7 @@
valueName = this->nextToken().fText;
}
this->expect(Token::SEMICOLON, "';'");
- return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fPosition, mods,
+ return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fPosition, mods,
name.fText, std::move(valueName),
std::move(decls)));
}
@@ -784,8 +785,8 @@
return nullptr;
}
}
- return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fPosition, std::move(test),
- std::move(ifTrue),
+ return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fPosition, std::move(test),
+ std::move(ifTrue),
std::move(ifFalse)));
}
@@ -815,7 +816,7 @@
if (!this->expect(Token::SEMICOLON, "';'")) {
return nullptr;
}
- return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fPosition,
+ return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fPosition,
std::move(statement),
std::move(test)));
}
@@ -840,12 +841,12 @@
if (!statement) {
return nullptr;
}
- return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fPosition,
- std::move(test),
+ return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fPosition,
+ std::move(test),
std::move(statement)));
}
-/* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN
+/* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN
STATEMENT */
std::unique_ptr<ASTForStatement> Parser::forStatement() {
Token start;
@@ -858,7 +859,7 @@
std::unique_ptr<ASTStatement> initializer;
Token nextToken = this->peek();
switch (nextToken.fKind) {
- case Token::SEMICOLON:
+ case Token::SEMICOLON:
this->nextToken();
break;
case Token::CONST: {
@@ -908,7 +909,7 @@
if (!statement) {
return nullptr;
}
- return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fPosition,
+ return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fPosition,
std::move(initializer),
std::move(test), std::move(next),
std::move(statement)));
@@ -930,7 +931,7 @@
if (!this->expect(Token::SEMICOLON, "';'")) {
return nullptr;
}
- return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fPosition,
+ return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fPosition,
std::move(expression)));
}
@@ -983,11 +984,11 @@
std::vector<std::unique_ptr<ASTStatement>> statements;
for (;;) {
switch (this->peek().fKind) {
- case Token::RBRACE:
+ case Token::RBRACE:
this->nextToken();
- return std::unique_ptr<ASTBlock>(new ASTBlock(start.fPosition,
+ return std::unique_ptr<ASTBlock>(new ASTBlock(start.fPosition,
std::move(statements)));
- case Token::END_OF_FILE:
+ case Token::END_OF_FILE:
this->error(this->peek().fPosition, "expected '}', but found end of file");
return nullptr;
default: {
@@ -1052,8 +1053,8 @@
if (!right) {
return nullptr;
}
- result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression(std::move(result),
- t,
+ result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression(std::move(result),
+ t,
std::move(right)));
}
default:
@@ -1076,8 +1077,8 @@
}
if (this->expect(Token::COLON, "':'")) {
std::unique_ptr<ASTExpression> falseExpr = this->assignmentExpression();
- return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std::move(result),
- std::move(trueExpr),
+ return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std::move(result),
+ std::move(trueExpr),
std::move(falseExpr)));
}
return nullptr;
@@ -1357,7 +1358,7 @@
}
}
-/* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN |
+/* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN |
PLUSPLUS | MINUSMINUS */
std::unique_ptr<ASTSuffix> Parser::suffix() {
Token next = this->nextToken();
@@ -1398,17 +1399,17 @@
}
}
this->expect(Token::RPAREN, "')' to complete function parameters");
- return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fPosition,
+ return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fPosition,
std::move(parameters)));
}
case Token::PLUSPLUS:
- return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition,
+ return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition,
ASTSuffix::kPostIncrement_Kind));
case Token::MINUSMINUS:
return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition,
ASTSuffix::kPostDecrement_Kind));
default: {
- this->error(next.fPosition, "expected expression suffix, but found '" + next.fText +
+ this->error(next.fPosition, "expected expression suffix, but found '" + next.fText +
"'\n");
return nullptr;
}
@@ -1505,7 +1506,7 @@
bool Parser::identifier(SkString* dest) {
Token t;
if (this->expect(Token::IDENTIFIER, "identifier", &t)) {
- *dest = t.fText;
+ *dest = t.fText;
return true;
}
return false;