moved SkSL InlineMarker and UnresolvedFunction data into IRNode

Change-Id: I05b940c69b7756d41277626fc3eef06003d133c1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/324886
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
Ethan Nicholas 2020-10-09 16:51:18 -04:00 committed by Skia Commit-Bot
parent c67948d2c0
commit ceb6214a55
11 changed files with 110 additions and 30 deletions

View File

@ -170,8 +170,8 @@ void Dehydrator::write(const Symbol& s) {
const UnresolvedFunction& f = s.as<UnresolvedFunction>();
this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
this->writeId(&f);
this->writeU8(f.fFunctions.size());
for (const FunctionDeclaration* funcDecl : f.fFunctions) {
this->writeU8(f.functions().size());
for (const FunctionDeclaration* funcDecl : f.functions()) {
this->write(*funcDecl);
}
break;
@ -451,7 +451,7 @@ void Dehydrator::write(const Statement* s) {
case Statement::Kind::kInlineMarker: {
const InlineMarker& i = s->as<InlineMarker>();
this->writeCommand(Rehydrator::kInlineMarker_Command);
this->writeId(i.fFuncDecl);
this->writeId(&i.function());
break;
}
case Statement::Kind::kNop:

View File

@ -976,7 +976,7 @@ void IRGenerator::convertFunction(const ASTNode& f) {
std::vector<const FunctionDeclaration*> functions;
switch (entry->kind()) {
case Symbol::Kind::kUnresolvedFunction:
functions = entry->as<UnresolvedFunction>().fFunctions;
functions = entry->as<UnresolvedFunction>().functions();
break;
case Symbol::Kind::kFunctionDeclaration:
functions.push_back(&entry->as<FunctionDeclaration>());
@ -1347,7 +1347,8 @@ std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTNode& identi
}
case Symbol::Kind::kUnresolvedFunction: {
const UnresolvedFunction* f = &result->as<UnresolvedFunction>();
return std::make_unique<FunctionReference>(fContext, identifier.fOffset, f->fFunctions);
return std::make_unique<FunctionReference>(fContext, identifier.fOffset,
f->functions());
}
case Symbol::Kind::kVariable: {
const Variable* var = &result->as<Variable>();

View File

@ -178,7 +178,8 @@ static bool contains_recursive_call(const FunctionDeclaration& funcDecl) {
}
bool visitStatement(const Statement& stmt) override {
if (stmt.is<InlineMarker>() && stmt.as<InlineMarker>().fFuncDecl->matches(*fFuncDecl)) {
if (stmt.is<InlineMarker>() &&
stmt.as<InlineMarker>().function().matches(*fFuncDecl)) {
return true;
}
return INHERITED::visitStatement(stmt);
@ -609,7 +610,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
arguments.size() + // Function arguments (copy out-params back)
1); // Inlined code (Block or do-while loop)
inlinedBody.children().push_back(std::make_unique<InlineMarker>(call->function()));
inlinedBody.children().push_back(std::make_unique<InlineMarker>(&call->function()));
auto makeInlineVar =
[&](const String& baseName, const Type* type, Modifiers modifiers,

View File

@ -386,7 +386,7 @@ std::unique_ptr<Statement> Rehydrator::statement() {
case Rehydrator::kInlineMarker_Command: {
const FunctionDeclaration* funcDecl = this->symbolRef<FunctionDeclaration>(
Symbol::Kind::kFunctionDeclaration);
return std::make_unique<InlineMarker>(*funcDecl);
return std::make_unique<InlineMarker>(funcDecl);
}
case Rehydrator::kReturn_Command: {
std::unique_ptr<Expression> expr = this->expression();

View File

@ -68,6 +68,11 @@ IRNode::IRNode(int offset, int kind, const IfStatementData& data)
, fKind(kind)
, fData(data) {}
IRNode::IRNode(int offset, int kind, const InlineMarkerData& data)
: fOffset(offset)
, fKind(kind)
, fData(data) {}
IRNode::IRNode(int offset, int kind, const IntLiteralData& data)
: fOffset(offset)
, fKind(kind)
@ -103,6 +108,11 @@ IRNode::IRNode(int offset, int kind, const TypeTokenData& data)
, fKind(kind)
, fData(data) {}
IRNode::IRNode(int offset, int kind, const UnresolvedFunctionData& data)
: fOffset(offset)
, fKind(kind)
, fData(data) {}
IRNode::IRNode(int offset, int kind, const VariableData& data)
: fOffset(offset)
, fKind(kind)

View File

@ -143,6 +143,10 @@ protected:
int64_t fValue;
};
struct InlineMarkerData {
const FunctionDeclaration* fFunction;
};
struct SettingData {
String fName;
const Type* fType;
@ -163,6 +167,12 @@ protected:
Token::Kind fToken;
};
struct UnresolvedFunctionData {
// FIXME move this into the child vector after killing fExpressionChildren /
// fStatementChildren
std::vector<const FunctionDeclaration*> fFunctions;
};
struct VariableData {
StringFragment fName;
const Type* fType;
@ -196,6 +206,7 @@ protected:
kFunctionCall,
kFunctionDeclaration,
kIfStatement,
kInlineMarker,
kIntLiteral,
kSetting,
kString,
@ -203,6 +214,7 @@ protected:
kSymbolAlias,
kType,
kTypeToken,
kUnresolvedFunction,
kVariable,
kVariableReference,
} fKind = Kind::kType;
@ -220,6 +232,7 @@ protected:
FunctionCallData fFunctionCall;
FunctionDeclarationData fFunctionDeclaration;
IfStatementData fIfStatement;
InlineMarkerData fInlineMarker;
IntLiteralData fIntLiteral;
SettingData fSetting;
String fString;
@ -227,6 +240,7 @@ protected:
SymbolAliasData fSymbolAlias;
const Type* fType;
TypeTokenData fTypeToken;
UnresolvedFunctionData fUnresolvedFunction;
VariableData fVariable;
VariableReferenceData fVariableReference;
@ -290,6 +304,11 @@ protected:
*(new(&fContents) IfStatementData) = data;
}
NodeData(InlineMarkerData data)
: fKind(Kind::kInlineMarker) {
*(new(&fContents) InlineMarkerData) = data;
}
NodeData(IntLiteralData data)
: fKind(Kind::kIntLiteral) {
*(new(&fContents) IntLiteralData) = data;
@ -325,6 +344,11 @@ protected:
*(new(&fContents) TypeTokenData) = data;
}
NodeData(const UnresolvedFunctionData& data)
: fKind(Kind::kUnresolvedFunction) {
*(new(&fContents) UnresolvedFunctionData) = data;
}
NodeData(const VariableData& data)
: fKind(Kind::kVariable) {
*(new(&fContents) VariableData) = data;
@ -377,6 +401,9 @@ protected:
case Kind::kIfStatement:
*(new(&fContents) IfStatementData) = other.fContents.fIfStatement;
break;
case Kind::kInlineMarker:
*(new(&fContents) InlineMarkerData) = other.fContents.fInlineMarker;
break;
case Kind::kIntLiteral:
*(new(&fContents) IntLiteralData) = other.fContents.fIntLiteral;
break;
@ -398,6 +425,9 @@ protected:
case Kind::kTypeToken:
*(new(&fContents) TypeTokenData) = other.fContents.fTypeToken;
break;
case Kind::kUnresolvedFunction:
*(new(&fContents) UnresolvedFunctionData) = other.fContents.fUnresolvedFunction;
break;
case Kind::kVariable:
*(new(&fContents) VariableData) = other.fContents.fVariable;
break;
@ -442,11 +472,14 @@ protected:
case Kind::kFunctionCall:
fContents.fFunctionCall.~FunctionCallData();
break;
case Kind::kFunctionDeclaration:
fContents.fFunctionDeclaration.~FunctionDeclarationData();
break;
case Kind::kIfStatement:
fContents.fIfStatement.~IfStatementData();
break;
case Kind::kFunctionDeclaration:
fContents.fFunctionDeclaration.~FunctionDeclarationData();
case Kind::kInlineMarker:
fContents.fInlineMarker.~InlineMarkerData();
break;
case Kind::kIntLiteral:
fContents.fIntLiteral.~IntLiteralData();
@ -468,6 +501,9 @@ protected:
case Kind::kTypeToken:
fContents.fTypeToken.~TypeTokenData();
break;
case Kind::kUnresolvedFunction:
fContents.fUnresolvedFunction.~UnresolvedFunctionData();
break;
case Kind::kVariable:
fContents.fVariable.~VariableData();
break;
@ -497,9 +533,11 @@ protected:
IRNode(int offset, int kind, const FunctionCallData& data);
IRNode(int offset, int kind, const FunctionDeclarationData& data);
IRNode(int offset, int kind, const IfStatementData& data);
IRNode(int offset, int kind, const FunctionDeclarationData& data);
IRNode(int offset, int kind, const InlineMarkerData& data);
IRNode(int offset, int kind, const IntLiteralData& data);
@ -515,6 +553,8 @@ protected:
IRNode(int offset, int kind, const TypeTokenData& data);
IRNode(int offset, int kind, const UnresolvedFunctionData& data);
IRNode(int offset, int kind, const VariableData& data);
IRNode(int offset, int kind, const VariableReferenceData& data);
@ -613,14 +653,19 @@ protected:
return fData.fContents.fFunctionDeclaration;
}
const FunctionDeclarationData& functionDeclarationData() const {
SkASSERT(fData.fKind == NodeData::Kind::kFunctionDeclaration);
return fData.fContents.fFunctionDeclaration;
}
const IfStatementData& ifStatementData() const {
SkASSERT(fData.fKind == NodeData::Kind::kIfStatement);
return fData.fContents.fIfStatement;
}
const FunctionDeclarationData& functionDeclarationData() const {
SkASSERT(fData.fKind == NodeData::Kind::kFunctionDeclaration);
return fData.fContents.fFunctionDeclaration;
const InlineMarkerData& inlineMarkerData() const {
SkASSERT(fData.fKind == NodeData::Kind::kInlineMarker);
return fData.fContents.fInlineMarker;
}
const IntLiteralData& intLiteralData() const {
@ -663,6 +708,11 @@ protected:
return fData.fContents.fTypeToken;
}
const UnresolvedFunctionData& unresolvedFunctionData() const {
SkASSERT(fData.fKind == NodeData::Kind::kUnresolvedFunction);
return fData.fContents.fUnresolvedFunction;
}
VariableData& variableData() {
SkASSERT(fData.fKind == NodeData::Kind::kVariable);
return fData.fContents.fVariable;

View File

@ -18,25 +18,30 @@ namespace SkSL {
* A no-op statement that indicates that a function was inlined here. This is necessary to detect
* and prevent runaway infinite recursion. This node doesn't directly generate code.
*/
struct InlineMarker : public Statement {
class InlineMarker : public Statement {
public:
static constexpr Kind kStatementKind = Kind::kInlineMarker;
InlineMarker(const FunctionDeclaration& funcDecl)
: INHERITED(-1, kStatementKind), fFuncDecl(&funcDecl) {}
InlineMarker(const FunctionDeclaration* function)
: INHERITED(-1, InlineMarkerData{function}) {}
const FunctionDeclaration& function() const {
return *this->inlineMarkerData().fFunction;
}
bool isEmpty() const override {
return true;
}
String description() const override {
return String("/* inlined: ") + fFuncDecl->name() + String(" */");
return String("/* inlined: ") + this->function().name() + String(" */");
}
std::unique_ptr<Statement> clone() const override {
return std::make_unique<InlineMarker>(*fFuncDecl);
return std::make_unique<InlineMarker>(&this->function());
}
const FunctionDeclaration* fFuncDecl;
private:
using INHERITED = Statement;
};

View File

@ -49,11 +49,14 @@ public:
SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
}
Statement(int offset, const ForStatementData& data)
: INHERITED(offset, (int) Kind::kFor, data) {}
Statement(int offset, const IfStatementData& data)
: INHERITED(offset, (int) Kind::kIf, data) {}
Statement(int offset, const ForStatementData& data)
: INHERITED(offset, (int) Kind::kFor, data) {}
Statement(int offset, const InlineMarkerData& data)
: INHERITED(offset, (int) Kind::kInlineMarker, data) {}
Kind kind() const {
return (Kind) fKind;

View File

@ -45,6 +45,9 @@ public:
Symbol(int offset, const SymbolAliasData& data)
: INHERITED(offset, (int) Kind::kSymbolAlias, data) {}
Symbol(int offset, const UnresolvedFunctionData& data)
: INHERITED(offset, (int) Kind::kUnresolvedFunction, data) {}
Symbol(int offset, const VariableData& data)
: INHERITED(offset, (int) Kind::kVariable, data) {}

View File

@ -17,7 +17,7 @@ std::vector<const FunctionDeclaration*> SymbolTable::GetFunctions(const Symbol&
case Symbol::Kind::kFunctionDeclaration:
return { &s.as<FunctionDeclaration>() };
case Symbol::Kind::kUnresolvedFunction:
return s.as<UnresolvedFunction>().fFunctions;
return s.as<UnresolvedFunction>().functions();
default:
return std::vector<const FunctionDeclaration*>();
}
@ -103,7 +103,7 @@ void SymbolTable::addWithoutOwnership(Symbol* symbol) {
refInSymbolTable = this->takeOwnershipOfSymbol(
std::make_unique<UnresolvedFunction>(std::move(functions)));
} else if (refInSymbolTable->is<UnresolvedFunction>()) {
functions = refInSymbolTable->as<UnresolvedFunction>().fFunctions;
functions = refInSymbolTable->as<UnresolvedFunction>().functions();
functions.push_back(&symbol->as<FunctionDeclaration>());
refInSymbolTable = this->takeOwnershipOfSymbol(

View File

@ -15,25 +15,32 @@ namespace SkSL {
/**
* A symbol representing multiple functions with the same name.
*/
struct UnresolvedFunction : public Symbol {
class UnresolvedFunction : public Symbol {
public:
static constexpr Kind kSymbolKind = Kind::kUnresolvedFunction;
UnresolvedFunction(std::vector<const FunctionDeclaration*> funcs)
: INHERITED(-1, kSymbolKind, funcs[0]->name())
, fFunctions(std::move(funcs)) {
: INHERITED(-1, UnresolvedFunctionData{std::move(funcs)}) {
#ifdef SK_DEBUG
for (auto func : funcs) {
for (auto func : this->functions()) {
SkASSERT(func->name() == name());
}
#endif
}
StringFragment name() const override {
return this->functions()[0]->name();
}
const std::vector<const FunctionDeclaration*>& functions() const {
return this->unresolvedFunctionData().fFunctions;
}
String description() const override {
return this->name();
}
const std::vector<const FunctionDeclaration*> fFunctions;
private:
using INHERITED = Symbol;
};