Replace various C-style casts in SkSLCompiler with as<T>.

This also shook out a few minor const-correctness issues which had been
masked by C-style casts.

Change-Id: I7680d43b1bb52c6e5d7c2e7e0f6d7e5ff7dea0d9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/311448
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
John Stiles 2020-08-18 11:19:07 -04:00 committed by Skia Commit-Bot
parent afbf899cf1
commit a5a97b4858

View File

@ -377,7 +377,7 @@ void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expressio
DefinitionMap* definitions) {
switch (lvalue->fKind) {
case Expression::kVariableReference_Kind: {
const Variable& var = ((VariableReference*) lvalue)->fVariable;
const Variable& var = lvalue->as<VariableReference>().fVariable;
if (var.fStorage == Variable::kLocal_Storage) {
(*definitions)[&var] = expr;
}
@ -390,19 +390,19 @@ void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expressio
// (we write to foo.x, and then pass foo to a function which happens to only read foo.x,
// but since we pass foo as a whole it is flagged as an error) unless we perform a much
// more complicated whole-program analysis. This is probably good enough.
this->addDefinition(((Swizzle*) lvalue)->fBase.get(),
this->addDefinition(lvalue->as<Swizzle>().fBase.get(),
(std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
definitions);
break;
case Expression::kIndex_Kind:
// see comments in Swizzle
this->addDefinition(((IndexExpression*) lvalue)->fBase.get(),
this->addDefinition(lvalue->as<IndexExpression>().fBase.get(),
(std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
definitions);
break;
case Expression::kFieldAccess_Kind:
// see comments in Swizzle
this->addDefinition(((FieldAccess*) lvalue)->fBase.get(),
this->addDefinition(lvalue->as<FieldAccess>().fBase.get(),
(std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
definitions);
break;
@ -410,10 +410,10 @@ void Compiler::addDefinition(const Expression* lvalue, std::unique_ptr<Expressio
// To simplify analysis, we just pretend that we write to both sides of the ternary.
// This allows for false positives (meaning we fail to detect that a variable might not
// have been assigned), but is preferable to false negatives.
this->addDefinition(((TernaryExpression*) lvalue)->fIfTrue.get(),
this->addDefinition(lvalue->as<TernaryExpression>().fIfTrue.get(),
(std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
definitions);
this->addDefinition(((TernaryExpression*) lvalue)->fIfFalse.get(),
this->addDefinition(lvalue->as<TernaryExpression>().fIfFalse.get(),
(std::unique_ptr<Expression>*) &fContext->fDefined_Expression,
definitions);
break;
@ -431,10 +431,10 @@ void Compiler::addDefinitions(const BasicBlock::Node& node,
switch (node.fKind) {
case BasicBlock::Node::kExpression_Kind: {
SkASSERT(node.expression());
const Expression* expr = (Expression*) node.expression()->get();
Expression* expr = node.expression()->get();
switch (expr->fKind) {
case Expression::kBinary_Kind: {
BinaryExpression* b = (BinaryExpression*) expr;
BinaryExpression* b = &expr->as<BinaryExpression>();
if (b->fOperator == Token::Kind::TK_EQ) {
this->addDefinition(b->fLeft.get(), &b->fRight, definitions);
} else if (Compiler::IsAssignment(b->fOperator)) {
@ -447,7 +447,7 @@ void Compiler::addDefinitions(const BasicBlock::Node& node,
break;
}
case Expression::kFunctionCall_Kind: {
const FunctionCall& c = (const FunctionCall&) *expr;
const FunctionCall& c = expr->as<FunctionCall>();
for (size_t i = 0; i < c.fFunction.fParameters.size(); ++i) {
if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
this->addDefinition(
@ -459,7 +459,7 @@ void Compiler::addDefinitions(const BasicBlock::Node& node,
break;
}
case Expression::kPrefix_Kind: {
const PrefixExpression* p = (PrefixExpression*) expr;
const PrefixExpression* p = &expr->as<PrefixExpression>();
if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
p->fOperator == Token::Kind::TK_PLUSPLUS) {
this->addDefinition(
@ -470,7 +470,7 @@ void Compiler::addDefinitions(const BasicBlock::Node& node,
break;
}
case Expression::kPostfix_Kind: {
const PostfixExpression* p = (PostfixExpression*) expr;
const PostfixExpression* p = &expr->as<PostfixExpression>();
if (p->fOperator == Token::Kind::TK_MINUSMINUS ||
p->fOperator == Token::Kind::TK_PLUSPLUS) {
this->addDefinition(
@ -481,7 +481,7 @@ void Compiler::addDefinitions(const BasicBlock::Node& node,
break;
}
case Expression::kVariableReference_Kind: {
const VariableReference* v = (VariableReference*) expr;
const VariableReference* v = &expr->as<VariableReference>();
if (v->fRefKind != VariableReference::kRead_RefKind) {
this->addDefinition(
v,
@ -496,9 +496,9 @@ void Compiler::addDefinitions(const BasicBlock::Node& node,
break;
}
case BasicBlock::Node::kStatement_Kind: {
const Statement* stmt = (Statement*) node.statement()->get();
Statement* stmt = node.statement()->get();
if (stmt->fKind == Statement::kVarDeclaration_Kind) {
VarDeclaration& vd = (VarDeclaration&) *stmt;
VarDeclaration& vd = stmt->as<VarDeclaration>();
if (vd.fValue) {
(*definitions)[vd.fVar] = &vd.fValue;
}
@ -558,10 +558,10 @@ static DefinitionMap compute_start_state(const CFG& cfg) {
SkASSERT(node.statement());
const Statement* s = node.statement()->get();
if (s->fKind == Statement::kVarDeclarations_Kind) {
const VarDeclarationsStatement* vd = (const VarDeclarationsStatement*) s;
const VarDeclarationsStatement* vd = &s->as<VarDeclarationsStatement>();
for (const auto& decl : vd->fDeclaration->fVars) {
if (decl->fKind == Statement::kVarDeclaration_Kind) {
result[((VarDeclaration&) *decl).fVar] = nullptr;
result[decl->as<VarDeclaration>().fVar] = nullptr;
}
}
}
@ -577,18 +577,18 @@ static DefinitionMap compute_start_state(const CFG& cfg) {
static bool is_dead(const Expression& lvalue) {
switch (lvalue.fKind) {
case Expression::kVariableReference_Kind:
return ((VariableReference&) lvalue).fVariable.dead();
return lvalue.as<VariableReference>().fVariable.dead();
case Expression::kSwizzle_Kind:
return is_dead(*((Swizzle&) lvalue).fBase);
return is_dead(*lvalue.as<Swizzle>().fBase);
case Expression::kFieldAccess_Kind:
return is_dead(*((FieldAccess&) lvalue).fBase);
return is_dead(*lvalue.as<FieldAccess>().fBase);
case Expression::kIndex_Kind: {
const IndexExpression& idx = (IndexExpression&) lvalue;
const IndexExpression& idx = lvalue.as<IndexExpression>();
return is_dead(*idx.fBase) &&
!idx.fIndex->hasProperty(Expression::Property::kSideEffects);
}
case Expression::kTernary_Kind: {
const TernaryExpression& t = (TernaryExpression&) lvalue;
const TernaryExpression& t = lvalue.as<TernaryExpression>();
return !t.fTest->hasSideEffects() && is_dead(*t.fIfTrue) && is_dead(*t.fIfFalse);
}
case Expression::kExternalValue_Kind:
@ -686,8 +686,7 @@ static void delete_left(BasicBlock* b,
bool* outNeedsRescan) {
*outUpdated = true;
std::unique_ptr<Expression>* target = (*iter)->expression();
SkASSERT((*target)->fKind == Expression::kBinary_Kind);
BinaryExpression& bin = (BinaryExpression&) **target;
BinaryExpression& bin = (*target)->as<BinaryExpression>();
SkASSERT(!bin.fLeft->hasSideEffects());
bool result;
if (bin.fOperator == Token::Kind::TK_EQ) {
@ -724,8 +723,7 @@ static void delete_right(BasicBlock* b,
bool* outNeedsRescan) {
*outUpdated = true;
std::unique_ptr<Expression>* target = (*iter)->expression();
SkASSERT((*target)->fKind == Expression::kBinary_Kind);
BinaryExpression& bin = (BinaryExpression&) **target;
BinaryExpression& bin = (*target)->as<BinaryExpression>();
SkASSERT(!bin.fRight->hasSideEffects());
if (!b->tryRemoveExpressionBefore(iter, bin.fRight.get())) {
*target = std::move(bin.fLeft);
@ -791,7 +789,7 @@ static void vectorize_left(BasicBlock* b,
std::vector<BasicBlock::Node>::iterator* iter,
bool* outUpdated,
bool* outNeedsRescan) {
BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
vectorize(b, iter, bin.fRight->fType, &bin.fLeft, outUpdated, outNeedsRescan);
}
@ -803,25 +801,25 @@ static void vectorize_right(BasicBlock* b,
std::vector<BasicBlock::Node>::iterator* iter,
bool* outUpdated,
bool* outNeedsRescan) {
BinaryExpression& bin = (BinaryExpression&) **(*iter)->expression();
BinaryExpression& bin = (*(*iter)->expression())->as<BinaryExpression>();
vectorize(b, iter, bin.fLeft->fType, &bin.fRight, outUpdated, outNeedsRescan);
}
// Mark that an expression which we were writing to is no longer being written to
static void clear_write(const Expression& expr) {
static void clear_write(Expression& expr) {
switch (expr.fKind) {
case Expression::kVariableReference_Kind: {
((VariableReference&) expr).setRefKind(VariableReference::kRead_RefKind);
expr.as<VariableReference>().setRefKind(VariableReference::kRead_RefKind);
break;
}
case Expression::kFieldAccess_Kind:
clear_write(*((FieldAccess&) expr).fBase);
clear_write(*expr.as<FieldAccess>().fBase);
break;
case Expression::kSwizzle_Kind:
clear_write(*((Swizzle&) expr).fBase);
clear_write(*expr.as<Swizzle>().fBase);
break;
case Expression::kIndex_Kind:
clear_write(*((IndexExpression&) expr).fBase);
clear_write(*expr.as<IndexExpression>().fBase);
break;
default:
ABORT("shouldn't be writing to this kind of expression\n");
@ -851,7 +849,7 @@ void Compiler::simplifyExpression(DefinitionMap& definitions,
}
switch (expr->fKind) {
case Expression::kVariableReference_Kind: {
const VariableReference& ref = (VariableReference&) *expr;
const VariableReference& ref = expr->as<VariableReference>();
const Variable& var = ref.fVariable;
if (ref.refKind() != VariableReference::kWrite_RefKind &&
ref.refKind() != VariableReference::kPointer_RefKind &&
@ -1067,7 +1065,7 @@ void Compiler::simplifyExpression(DefinitionMap& definitions,
}
// detect swizzles of swizzles, e.g. replace foo.argb.r000 with foo.a000
if (s.fBase->fKind == Expression::kSwizzle_Kind) {
Swizzle& base = (Swizzle&) *s.fBase;
Swizzle& base = s.fBase->as<Swizzle>();
std::vector<int> final;
for (int c : s.fComponents) {
if (c == SKSL_SWIZZLE_0 || c == SKSL_SWIZZLE_1) {
@ -1096,7 +1094,7 @@ void Compiler::simplifyExpression(DefinitionMap& definitions,
static bool contains_conditional_break_impl(Statement& s, bool inConditional) {
switch (s.fKind) {
case Statement::kBlock_Kind:
for (const std::unique_ptr<Statement>& sub : static_cast<Block&>(s).fStatements) {
for (const std::unique_ptr<Statement>& sub : s.as<Block>().fStatements) {
if (contains_conditional_break_impl(*sub, inConditional)) {
return true;
}
@ -1107,7 +1105,7 @@ static bool contains_conditional_break_impl(Statement& s, bool inConditional) {
return inConditional;
case Statement::kIf_Kind: {
const IfStatement& i = static_cast<IfStatement&>(s);
const IfStatement& i = s.as<IfStatement>();
return contains_conditional_break_impl(*i.fIfTrue, /*inConditional=*/true) ||
(i.fIfFalse &&
contains_conditional_break_impl(*i.fIfFalse, /*inConditional=*/true));
@ -1256,7 +1254,7 @@ void Compiler::simplifyStatement(DefinitionMap& definitions,
Statement* stmt = (*iter)->statement()->get();
switch (stmt->fKind) {
case Statement::kVarDeclaration_Kind: {
const auto& varDecl = (VarDeclaration&) *stmt;
const auto& varDecl = stmt->as<VarDeclaration>();
if (varDecl.fVar->dead() &&
(!varDecl.fValue ||
!varDecl.fValue->hasSideEffects())) {
@ -1272,10 +1270,10 @@ void Compiler::simplifyStatement(DefinitionMap& definitions,
break;
}
case Statement::kIf_Kind: {
IfStatement& i = (IfStatement&) *stmt;
IfStatement& i = stmt->as<IfStatement>();
if (i.fTest->fKind == Expression::kBoolLiteral_Kind) {
// constant if, collapse down to a single branch
if (((BoolLiteral&) *i.fTest).fValue) {
if (i.fTest->as<BoolLiteral>().fValue) {
SkASSERT(i.fIfTrue);
(*iter)->setStatement(std::move(i.fIfTrue));
} else {
@ -1312,7 +1310,7 @@ void Compiler::simplifyStatement(DefinitionMap& definitions,
break;
}
case Statement::kSwitch_Kind: {
SwitchStatement& s = (SwitchStatement&) *stmt;
SwitchStatement& s = stmt->as<SwitchStatement>();
if (s.fValue->isCompileTimeConstant()) {
// switch is constant, replace it with the case that matches
bool found = false;
@ -1363,7 +1361,7 @@ void Compiler::simplifyStatement(DefinitionMap& definitions,
break;
}
case Statement::kExpression_Kind: {
ExpressionStatement& e = (ExpressionStatement&) *stmt;
ExpressionStatement& e = stmt->as<ExpressionStatement>();
SkASSERT((*iter)->statement()->get() == &e);
if (!e.fExpression->hasSideEffects()) {
// Expression statement with no side effects, kill it
@ -1469,21 +1467,21 @@ void Compiler::scanCFG(FunctionDefinition& f) {
const Statement& s = **iter->statement();
switch (s.fKind) {
case Statement::kIf_Kind:
if (((const IfStatement&) s).fIsStatic &&
if (s.as<IfStatement>().fIsStatic &&
!(fFlags & kPermitInvalidStaticTests_Flag)) {
this->error(s.fOffset, "static if has non-static test");
}
++iter;
break;
case Statement::kSwitch_Kind:
if (((const SwitchStatement&) s).fIsStatic &&
if (s.as<SwitchStatement>().fIsStatic &&
!(fFlags & kPermitInvalidStaticTests_Flag)) {
this->error(s.fOffset, "static switch has non-static test");
}
++iter;
break;
case Statement::kVarDeclarations_Kind: {
VarDeclarations& decls = *((VarDeclarationsStatement&) s).fDeclaration;
VarDeclarations& decls = *s.as<VarDeclarationsStatement>().fDeclaration;
for (auto varIter = decls.fVars.begin(); varIter != decls.fVars.end();) {
if ((*varIter)->fKind == Statement::kNop_Kind) {
varIter = decls.fVars.erase(varIter);