Add Make factory method to VariableReference.

There aren't any optimization opportunities here, but it was the only
expression/statement type left without a Make or Convert factory.

Change-Id: I4c413a3b2619261f9e974528fa45211383e56fa7
Bug: skia:11342
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/402017
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
John Stiles 2021-04-27 17:29:59 -04:00 committed by Skia Commit-Bot
parent 4f0098227e
commit a94e0261ef
4 changed files with 23 additions and 19 deletions

View File

@ -702,12 +702,12 @@ std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<
const Variable* loopIdx = &(*fSymbolTable)["sk_InvocationID"]->as<Variable>();
auto test = BinaryExpression::Make(
fContext,
std::make_unique<VariableReference>(/*offset=*/-1, loopIdx),
VariableReference::Make(/*offset=*/-1, loopIdx),
Token::Kind::TK_LT,
IntLiteral::Make(fContext, /*offset=*/-1, fInvocations));
auto next = PostfixExpression::Make(
fContext,
std::make_unique<VariableReference>(/*offset=*/-1, loopIdx,VariableRefKind::kReadWrite),
VariableReference::Make(/*offset=*/-1, loopIdx, VariableRefKind::kReadWrite),
Token::Kind::TK_PLUSPLUS);
ASTNode endPrimitiveID(&fFile->fNodes, -1, ASTNode::Kind::kIdentifier, "EndPrimitive");
std::unique_ptr<Expression> endPrimitive = this->convertExpression(endPrimitiveID);
@ -723,7 +723,7 @@ std::unique_ptr<Block> IRGenerator::applyInvocationIDWorkaround(std::unique_ptr<
ExpressionArray{})));
auto assignment = BinaryExpression::Make(
fContext,
std::make_unique<VariableReference>(/*offset=*/-1, loopIdx, VariableRefKind::kWrite),
VariableReference::Make(/*offset=*/-1, loopIdx, VariableRefKind::kWrite),
Token::Kind::TK_EQ,
IntLiteral::Make(fContext, /*offset=*/-1, /*value=*/0));
auto initializer = ExpressionStatement::Make(fContext, std::move(assignment));
@ -750,12 +750,10 @@ std::unique_ptr<Statement> IRGenerator::getNormalizeSkPositionCode() {
// sk_Position.w);
SkASSERT(skPerVertex && fRTAdjust);
auto Ref = [](const Variable* var) -> std::unique_ptr<Expression> {
return std::make_unique<VariableReference>(/*offset=*/-1, var,
VariableReference::RefKind::kRead);
return VariableReference::Make(/*offset=*/-1, var, VariableReference::RefKind::kRead);
};
auto WRef = [](const Variable* var) -> std::unique_ptr<Expression> {
return std::make_unique<VariableReference>(/*offset=*/-1, var,
VariableReference::RefKind::kWrite);
return VariableReference::Make(/*offset=*/-1, var, VariableReference::RefKind::kWrite);
};
auto Field = [&](const Variable* var, int idx) -> std::unique_ptr<Expression> {
return FieldAccess::Make(fContext, Ref(var), idx,
@ -1590,13 +1588,11 @@ std::unique_ptr<Expression> IRGenerator::convertIdentifier(int offset, StringFra
}
}
// default to kRead_RefKind; this will be corrected later if the variable is written to
return std::make_unique<VariableReference>(offset,
var,
VariableReference::RefKind::kRead);
return VariableReference::Make(offset, var, VariableReference::RefKind::kRead);
}
case Symbol::Kind::kField: {
const Field* field = &result->as<Field>();
auto base = std::make_unique<VariableReference>(offset, &field->owner(),
auto base = VariableReference::Make(offset, &field->owner(),
VariableReference::RefKind::kRead);
return FieldAccess::Make(fContext, std::move(base), field->fieldIndex(),
FieldAccess::OwnerKind::kAnonymousInterfaceBlock);

View File

@ -550,7 +550,7 @@ std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
variable.type().clone(symbolTableForStatement),
isBuiltinCode,
variable.storage());
(*varMap)[&variable] = std::make_unique<VariableReference>(offset, clonedVar.get());
(*varMap)[&variable] = VariableReference::Make(offset, clonedVar.get());
auto result = VarDeclaration::Make(*fContext,
clonedVar.get(),
decl.baseType().clone(symbolTableForStatement),
@ -646,7 +646,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
symbolTable.get(), Modifiers{},
caller->isBuiltin(), &noInitialValue);
inlineStatements.push_back(std::move(var.fVarDecl));
resultExpr = std::make_unique<VariableReference>(/*offset=*/-1, var.fVarSymbol);
resultExpr = VariableReference::Make(/*offset=*/-1, var.fVarSymbol);
}
// Create variables in the extra statements to hold the arguments, and assign the arguments to
@ -670,7 +670,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
symbolTable.get(), param->modifiers(),
caller->isBuiltin(), &arguments[i]);
inlineStatements.push_back(std::move(var.fVarDecl));
varMap[param] = std::make_unique<VariableReference>(/*offset=*/-1, var.fVarSymbol);
varMap[param] = VariableReference::Make(/*offset=*/-1, var.fVarSymbol);
}
for (const std::unique_ptr<Statement>& stmt : body.children()) {

View File

@ -564,7 +564,7 @@ std::unique_ptr<Expression> Rehydrator::expression() {
case Rehydrator::kVariableReference_Command: {
const Variable* var = this->symbolRef<Variable>(Symbol::Kind::kVariable);
VariableReference::RefKind refKind = (VariableReference::RefKind) this->readU8();
return std::make_unique<VariableReference>(-1, var, refKind);
return VariableReference::Make(/*offset=*/-1, var, refKind);
}
case Rehydrator::kVoid_Command:
return nullptr;

View File

@ -37,7 +37,16 @@ public:
static constexpr Kind kExpressionKind = Kind::kVariableReference;
VariableReference(int offset, const Variable* variable, RefKind refKind = RefKind::kRead);
VariableReference(int offset, const Variable* variable, RefKind refKind);
// Creates a VariableReference. There isn't much in the way of error-checking or optimization
// opportunities here.
static std::unique_ptr<Expression> Make(int offset,
const Variable* variable,
RefKind refKind = RefKind::kRead) {
SkASSERT(variable);
return std::make_unique<VariableReference>(offset, variable, refKind);
}
VariableReference(const VariableReference&) = delete;
VariableReference& operator=(const VariableReference&) = delete;
@ -58,8 +67,7 @@ public:
bool isConstantOrUniform() const override;
std::unique_ptr<Expression> clone() const override {
return std::unique_ptr<Expression>(new VariableReference(fOffset, this->variable(),
this->refKind()));
return std::make_unique<VariableReference>(fOffset, this->variable(), this->refKind());
}
String description() const override;