moved SkSL BoolLiteral data into IRNode
Change-Id: I177b6daf4d6cb024ba20264ab01d0aa68e768a6d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/319782
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/sksl/SkSLByteCodeGenerator.cpp b/src/sksl/SkSLByteCodeGenerator.cpp
index 8a63b2b..5907ade 100644
--- a/src/sksl/SkSLByteCodeGenerator.cpp
+++ b/src/sksl/SkSLByteCodeGenerator.cpp
@@ -901,7 +901,7 @@
void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
this->write(ByteCodeInstruction::kPushImmediate);
- this->write32(b.fValue ? ~0 : 0);
+ this->write32(b.value() ? ~0 : 0);
}
void ByteCodeGenerator::writeConstructor(const Constructor& c) {
diff --git a/src/sksl/SkSLCFGGenerator.cpp b/src/sksl/SkSLCFGGenerator.cpp
index 316b68d..377ad8e 100644
--- a/src/sksl/SkSLCFGGenerator.cpp
+++ b/src/sksl/SkSLCFGGenerator.cpp
@@ -479,7 +479,7 @@
}
static bool is_true(Expression& expr) {
- return expr.is<BoolLiteral>() && expr.as<BoolLiteral>().fValue;
+ return expr.is<BoolLiteral>() && expr.as<BoolLiteral>().value();
}
void CFGGenerator::addStatement(CFG& cfg, std::unique_ptr<Statement>* s) {
diff --git a/src/sksl/SkSLCompiler.cpp b/src/sksl/SkSLCompiler.cpp
index 8a50681..44ca904 100644
--- a/src/sksl/SkSLCompiler.cpp
+++ b/src/sksl/SkSLCompiler.cpp
@@ -881,7 +881,7 @@
if (t->fTest->kind() == Expression::Kind::kBoolLiteral) {
// ternary has a constant test, replace it with either the true or
// false branch
- if (t->fTest->as<BoolLiteral>().fValue) {
+ if (t->fTest->as<BoolLiteral>().value()) {
(*iter)->setExpression(std::move(t->fIfTrue));
} else {
(*iter)->setExpression(std::move(t->fIfFalse));
@@ -1292,7 +1292,7 @@
IfStatement& i = stmt->as<IfStatement>();
if (i.fTest->kind() == Expression::Kind::kBoolLiteral) {
// constant if, collapse down to a single branch
- if (i.fTest->as<BoolLiteral>().fValue) {
+ if (i.fTest->as<BoolLiteral>().value()) {
SkASSERT(i.fIfTrue);
(*iter)->setStatement(std::move(i.fIfTrue));
} else {
diff --git a/src/sksl/SkSLDehydrator.cpp b/src/sksl/SkSLDehydrator.cpp
index 683346d..a6883c1 100644
--- a/src/sksl/SkSLDehydrator.cpp
+++ b/src/sksl/SkSLDehydrator.cpp
@@ -267,7 +267,7 @@
case Expression::Kind::kBoolLiteral: {
const BoolLiteral& b = e->as<BoolLiteral>();
this->writeU8(Rehydrator::kBoolLiteral_Command);
- this->writeU8(b.fValue);
+ this->writeU8(b.value());
break;
}
case Expression::Kind::kConstructor: {
diff --git a/src/sksl/SkSLGLSLCodeGenerator.cpp b/src/sksl/SkSLGLSLCodeGenerator.cpp
index 81b9d5b..b6103fd 100644
--- a/src/sksl/SkSLGLSLCodeGenerator.cpp
+++ b/src/sksl/SkSLGLSLCodeGenerator.cpp
@@ -1008,7 +1008,7 @@
}
void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
- this->write(b.fValue ? "true" : "false");
+ this->write(b.value() ? "true" : "false");
}
void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) {
diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp
index 68802c8..f1f5ad7 100644
--- a/src/sksl/SkSLIRGenerator.cpp
+++ b/src/sksl/SkSLIRGenerator.cpp
@@ -532,7 +532,7 @@
}
if (test->kind() == Expression::Kind::kBoolLiteral) {
// static boolean value, fold down to a single branch
- if (test->as<BoolLiteral>().fValue) {
+ if (test->as<BoolLiteral>().value()) {
return ifTrue;
} else if (ifFalse) {
return ifFalse;
@@ -1710,7 +1710,7 @@
Token::Kind op,
const Expression& right) {
SkASSERT(left.kind() == Expression::Kind::kBoolLiteral);
- bool leftVal = left.as<BoolLiteral>().fValue;
+ bool leftVal = left.as<BoolLiteral>().value();
if (op == Token::Kind::TK_LOGICALAND) {
// (true && expr) -> (expr) and (false && expr) -> (false)
return leftVal ? right.clone()
@@ -1753,8 +1753,8 @@
// types, which will let us be more intelligent about this.
if (left.kind() == Expression::Kind::kBoolLiteral &&
right.kind() == Expression::Kind::kBoolLiteral) {
- bool leftVal = left.as<BoolLiteral>().fValue;
- bool rightVal = right.as<BoolLiteral>().fValue;
+ bool leftVal = left.as<BoolLiteral>().value();
+ bool rightVal = right.as<BoolLiteral>().value();
bool result;
switch (op) {
case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break;
@@ -2013,7 +2013,7 @@
}
if (test->kind() == Expression::Kind::kBoolLiteral) {
// static boolean test, just return one of the branches
- if (test->as<BoolLiteral>().fValue) {
+ if (test->as<BoolLiteral>().value()) {
return ifTrue;
} else {
return ifFalse;
@@ -2409,7 +2409,7 @@
}
if (base->kind() == Expression::Kind::kBoolLiteral) {
return std::make_unique<BoolLiteral>(fContext, base->fOffset,
- !base->as<BoolLiteral>().fValue);
+ !base->as<BoolLiteral>().value());
}
break;
case Token::Kind::TK_BITWISENOT:
diff --git a/src/sksl/SkSLMetalCodeGenerator.cpp b/src/sksl/SkSLMetalCodeGenerator.cpp
index db75ce7..335ff10 100644
--- a/src/sksl/SkSLMetalCodeGenerator.cpp
+++ b/src/sksl/SkSLMetalCodeGenerator.cpp
@@ -907,7 +907,7 @@
}
void MetalCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
- this->write(b.fValue ? "true" : "false");
+ this->write(b.value() ? "true" : "false");
}
void MetalCodeGenerator::writeIntLiteral(const IntLiteral& i) {
diff --git a/src/sksl/SkSLSPIRVCodeGenerator.cpp b/src/sksl/SkSLSPIRVCodeGenerator.cpp
index 6610b9f..c23fc8e 100644
--- a/src/sksl/SkSLSPIRVCodeGenerator.cpp
+++ b/src/sksl/SkSLSPIRVCodeGenerator.cpp
@@ -2503,7 +2503,7 @@
}
SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
- if (b.fValue) {
+ if (b.value()) {
if (fBoolTrue == 0) {
fBoolTrue = this->nextId();
this->writeInstruction(SpvOpConstantTrue, this->getType(b.type()), fBoolTrue,
diff --git a/src/sksl/SkSLTestRehydrator.cpp b/src/sksl/SkSLTestRehydrator.cpp
new file mode 100644
index 0000000..21c57bc
--- /dev/null
+++ b/src/sksl/SkSLTestRehydrator.cpp
@@ -0,0 +1,340 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <fstream>
+#include "src/sksl/SkSLCompiler.h"
+#include "src/sksl/SkSLDehydrator.h"
+#include "src/sksl/SkSLFileOutputStream.h"
+#include "src/sksl/SkSLIRGenerator.h"
+#include "src/sksl/SkSLStringStream.h"
+#include "src/sksl/SkSLUtil.h"
+#include "src/sksl/ir/SkSLEnum.h"
+#include "src/sksl/ir/SkSLUnresolvedFunction.h"
+
+// Given the path to a file (e.g. src/gpu/effects/GrFooFragmentProcessor.fp) and the expected
+// filename prefix and suffix (e.g. "Gr" and ".fp"), returns the "base name" of the
+// file (in this case, 'FooFragmentProcessor'). If no match, returns the empty string.
+static SkSL::String base_name(const char* fpPath, const char* prefix, const char* suffix) {
+ SkSL::String result;
+ const char* end = fpPath + strlen(fpPath);
+ const char* fileName = end;
+ // back up until we find a slash
+ while (fileName != fpPath && '/' != *(fileName - 1) && '\\' != *(fileName - 1)) {
+ --fileName;
+ }
+ if (!strncmp(fileName, prefix, strlen(prefix)) &&
+ !strncmp(end - strlen(suffix), suffix, strlen(suffix))) {
+ result.append(fileName + strlen(prefix), end - fileName - strlen(prefix) - strlen(suffix));
+ }
+ return result;
+}
+
+// Given a string containing an SkSL program, searches for a #pragma settings comment, like so:
+// /*#pragma settings Default Sharpen*/
+// The passed-in Settings object will be updated accordingly. Any number of options can be provided.
+static void detect_shader_settings(const SkSL::String& text, SkSL::Program::Settings* settings) {
+ using Factory = SkSL::ShaderCapsFactory;
+
+ // Find a matching comment and isolate the name portion.
+ static constexpr char kPragmaSettings[] = "/*#pragma settings ";
+ const char* settingsPtr = strstr(text.c_str(), kPragmaSettings);
+ if (settingsPtr != nullptr) {
+ // Subtract one here in order to preserve the leading space, which is necessary to allow
+ // consumeSuffix to find the first item.
+ settingsPtr += strlen(kPragmaSettings) - 1;
+
+ const char* settingsEnd = strstr(settingsPtr, "*/");
+ if (settingsEnd != nullptr) {
+ SkSL::String settingsText{settingsPtr, size_t(settingsEnd - settingsPtr)};
+
+ // Apply settings as requested. Since they can come in any order, repeat until we've
+ // consumed them all.
+ for (;;) {
+ const size_t startingLength = settingsText.length();
+
+ if (settingsText.consumeSuffix(" AddAndTrueToLoopCondition")) {
+ static auto s_addAndTrueCaps = Factory::AddAndTrueToLoopCondition();
+ settings->fCaps = s_addAndTrueCaps.get();
+ }
+ if (settingsText.consumeSuffix(" CannotUseFractForNegativeValues")) {
+ static auto s_negativeFractCaps = Factory::CannotUseFractForNegativeValues();
+ settings->fCaps = s_negativeFractCaps.get();
+ }
+ if (settingsText.consumeSuffix(" CannotUseFragCoord")) {
+ static auto s_noFragCoordCaps = Factory::CannotUseFragCoord();
+ settings->fCaps = s_noFragCoordCaps.get();
+ }
+ if (settingsText.consumeSuffix(" CannotUseMinAndAbsTogether")) {
+ static auto s_minAbsCaps = Factory::CannotUseMinAndAbsTogether();
+ settings->fCaps = s_minAbsCaps.get();
+ }
+ if (settingsText.consumeSuffix(" Default")) {
+ static auto s_defaultCaps = Factory::Default();
+ settings->fCaps = s_defaultCaps.get();
+ }
+ if (settingsText.consumeSuffix(" EmulateAbsIntFunction")) {
+ static auto s_emulateAbsIntCaps = Factory::EmulateAbsIntFunction();
+ settings->fCaps = s_emulateAbsIntCaps.get();
+ }
+ if (settingsText.consumeSuffix(" FragCoordsOld")) {
+ static auto s_fragCoordsOld = Factory::FragCoordsOld();
+ settings->fCaps = s_fragCoordsOld.get();
+ }
+ if (settingsText.consumeSuffix(" FragCoordsNew")) {
+ static auto s_fragCoordsNew = Factory::FragCoordsNew();
+ settings->fCaps = s_fragCoordsNew.get();
+ }
+ if (settingsText.consumeSuffix(" GeometryShaderExtensionString")) {
+ static auto s_geometryExtCaps = Factory::GeometryShaderExtensionString();
+ settings->fCaps = s_geometryExtCaps.get();
+ }
+ if (settingsText.consumeSuffix(" GeometryShaderSupport")) {
+ static auto s_geometryShaderCaps = Factory::GeometryShaderSupport();
+ settings->fCaps = s_geometryShaderCaps.get();
+ }
+ if (settingsText.consumeSuffix(" GSInvocationsExtensionString")) {
+ static auto s_gsInvocationCaps = Factory::GSInvocationsExtensionString();
+ settings->fCaps = s_gsInvocationCaps.get();
+ }
+ if (settingsText.consumeSuffix(" IncompleteShortIntPrecision")) {
+ static auto s_incompleteShortIntCaps = Factory::IncompleteShortIntPrecision();
+ settings->fCaps = s_incompleteShortIntCaps.get();
+ }
+ if (settingsText.consumeSuffix(" MustForceNegatedAtanParamToFloat")) {
+ static auto s_negativeAtanCaps = Factory::MustForceNegatedAtanParamToFloat();
+ settings->fCaps = s_negativeAtanCaps.get();
+ }
+ if (settingsText.consumeSuffix(" NoGSInvocationsSupport")) {
+ static auto s_noGSInvocations = Factory::NoGSInvocationsSupport();
+ settings->fCaps = s_noGSInvocations.get();
+ }
+ if (settingsText.consumeSuffix(" RemovePowWithConstantExponent")) {
+ static auto s_powCaps = Factory::RemovePowWithConstantExponent();
+ settings->fCaps = s_powCaps.get();
+ }
+ if (settingsText.consumeSuffix(" RewriteDoWhileLoops")) {
+ static auto s_rewriteLoopCaps = Factory::RewriteDoWhileLoops();
+ settings->fCaps = s_rewriteLoopCaps.get();
+ }
+ if (settingsText.consumeSuffix(" ShaderDerivativeExtensionString")) {
+ static auto s_derivativeCaps = Factory::ShaderDerivativeExtensionString();
+ settings->fCaps = s_derivativeCaps.get();
+ }
+ if (settingsText.consumeSuffix(" UnfoldShortCircuitAsTernary")) {
+ static auto s_ternaryCaps = Factory::UnfoldShortCircuitAsTernary();
+ settings->fCaps = s_ternaryCaps.get();
+ }
+ if (settingsText.consumeSuffix(" UsesPrecisionModifiers")) {
+ static auto s_precisionCaps = Factory::UsesPrecisionModifiers();
+ settings->fCaps = s_precisionCaps.get();
+ }
+ if (settingsText.consumeSuffix(" Version110")) {
+ static auto s_version110Caps = Factory::Version110();
+ settings->fCaps = s_version110Caps.get();
+ }
+ if (settingsText.consumeSuffix(" Version450Core")) {
+ static auto s_version450CoreCaps = Factory::Version450Core();
+ settings->fCaps = s_version450CoreCaps.get();
+ }
+ if (settingsText.consumeSuffix(" FlipY")) {
+ settings->fFlipY = true;
+ }
+ if (settingsText.consumeSuffix(" ForceHighPrecision")) {
+ settings->fForceHighPrecision = true;
+ }
+ if (settingsText.consumeSuffix(" Sharpen")) {
+ settings->fSharpenTextures = true;
+ }
+
+ if (settingsText.empty()) {
+ break;
+ }
+ if (settingsText.length() == startingLength) {
+ printf("Unrecognized #pragma settings: %s\n", settingsText.c_str());
+ exit(3);
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Very simple standalone executable to facilitate testing.
+ */
+int main(int argc, const char** argv) {
+ bool honorSettings = true;
+ if (argc == 4) {
+ if (0 == strcmp(argv[3], "--settings")) {
+ honorSettings = true;
+ } else if (0 == strcmp(argv[3], "--nosettings")) {
+ honorSettings = false;
+ } else {
+ printf("unrecognized flag: %s\n", argv[3]);
+ exit(1);
+ }
+ } else if (argc != 3) {
+ printf("usage: skslc <input> <output> <flags>\n"
+ "\n"
+ "Allowed flags:\n"
+ "--settings: honor embedded /*#pragma settings*/ comments.\n"
+ "--nosettings: ignore /*#pragma settings*/ comments\n");
+ exit(1);
+ }
+
+ SkSL::Program::Kind kind;
+ SkSL::String input(argv[1]);
+ if (input.endsWith(".vert")) {
+ kind = SkSL::Program::kVertex_Kind;
+ } else if (input.endsWith(".frag") || input.endsWith(".sksl")) {
+ kind = SkSL::Program::kFragment_Kind;
+ } else if (input.endsWith(".geom")) {
+ kind = SkSL::Program::kGeometry_Kind;
+ } else if (input.endsWith(".fp")) {
+ kind = SkSL::Program::kFragmentProcessor_Kind;
+ } else if (input.endsWith(".stage")) {
+ kind = SkSL::Program::kPipelineStage_Kind;
+ } else {
+ printf("input filename must end in '.vert', '.frag', '.geom', '.fp', '.stage', or "
+ "'.sksl'\n");
+ exit(1);
+ }
+
+ std::ifstream in(argv[1]);
+ SkSL::String text((std::istreambuf_iterator<char>(in)),
+ std::istreambuf_iterator<char>());
+ if (in.rdstate()) {
+ printf("error reading '%s'\n", argv[1]);
+ exit(2);
+ }
+
+ SkSL::Program::Settings settings;
+ if (honorSettings) {
+ detect_shader_settings(text, &settings);
+ }
+ SkSL::String name(argv[2]);
+ if (name.endsWith(".spirv")) {
+ SkSL::FileOutputStream out(argv[2]);
+ SkSL::Compiler compiler;
+ if (!out.isValid()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
+ if (!program || !compiler.toSPIRV(*program, out)) {
+ printf("%s", compiler.errorText().c_str());
+ exit(3);
+ }
+ if (!out.close()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ } else if (name.endsWith(".glsl")) {
+ SkSL::FileOutputStream out(argv[2]);
+ SkSL::Compiler compiler;
+ if (!out.isValid()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
+ if (!program || !compiler.toGLSL(*program, out)) {
+ printf("%s", compiler.errorText().c_str());
+ exit(3);
+ }
+ if (!out.close()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ } else if (name.endsWith(".metal")) {
+ SkSL::FileOutputStream out(argv[2]);
+ SkSL::Compiler compiler;
+ if (!out.isValid()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
+ if (!program || !compiler.toMetal(*program, out)) {
+ printf("%s", compiler.errorText().c_str());
+ exit(3);
+ }
+ if (!out.close()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ } else if (name.endsWith(".h")) {
+ SkSL::FileOutputStream out(argv[2]);
+ SkSL::Compiler compiler(SkSL::Compiler::kPermitInvalidStaticTests_Flag);
+ if (!out.isValid()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ settings.fReplaceSettings = false;
+ std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
+ if (!program || !compiler.toH(*program, base_name(argv[1], "Gr", ".fp"), out)) {
+ printf("%s", compiler.errorText().c_str());
+ exit(3);
+ }
+ if (!out.close()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ } else if (name.endsWith(".cpp")) {
+ SkSL::FileOutputStream out(argv[2]);
+ SkSL::Compiler compiler(SkSL::Compiler::kPermitInvalidStaticTests_Flag);
+ if (!out.isValid()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ settings.fReplaceSettings = false;
+ std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, text, settings);
+ if (!program || !compiler.toCPP(*program, base_name(argv[1], "Gr", ".fp"), out)) {
+ printf("%s", compiler.errorText().c_str());
+ exit(3);
+ }
+ if (!out.close()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ } else if (name.endsWith(".dehydrated.sksl")) {
+ SkSL::FileOutputStream out(argv[2]);
+ SkSL::Compiler compiler;
+ if (!out.isValid()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ std::shared_ptr<SkSL::SymbolTable> symbols;
+ std::vector<std::unique_ptr<SkSL::ProgramElement>> elements;
+ compiler.processIncludeFile(kind, argv[1], nullptr, &elements, &symbols);
+ SkSL::Dehydrator dehydrator;
+ for (int i = symbols->fParent->fOwnedSymbols.size() - 1; i >= 0; --i) {
+ symbols->fOwnedSymbols.insert(symbols->fOwnedSymbols.begin(),
+ std::move(symbols->fParent->fOwnedSymbols[i]));
+ }
+ for (const auto& p : *symbols->fParent) {
+ symbols->addWithoutOwnership(p.first, p.second);
+ }
+ dehydrator.write(*symbols);
+ dehydrator.write(elements);
+ SkSL::String baseName = base_name(argv[1], "", ".sksl");
+ SkSL::StringStream buffer;
+ dehydrator.finish(buffer);
+ const SkSL::String& data = buffer.str();
+ out.printf("static constexpr size_t SKSL_INCLUDE_%s_LENGTH = %d;\n", baseName.c_str(),
+ (int) data.length());
+ out.printf("static uint8_t SKSL_INCLUDE_%s[%d] = {", baseName.c_str(), (int) data.length());
+ for (size_t i = 0; i < data.length(); ++i) {
+ out.printf("%d,", (uint8_t) data[i]);
+ }
+ out.printf("};\n");
+ if (!out.close()) {
+ printf("error writing '%s'\n", argv[2]);
+ exit(4);
+ }
+ } else {
+ printf("expected output filename to end with '.spirv', '.glsl', '.cpp', '.h', or '.metal'");
+ exit(1);
+ }
+}
diff --git a/src/sksl/ir/SkSLBoolLiteral.h b/src/sksl/ir/SkSLBoolLiteral.h
index c82014e..437872e 100644
--- a/src/sksl/ir/SkSLBoolLiteral.h
+++ b/src/sksl/ir/SkSLBoolLiteral.h
@@ -16,15 +16,19 @@
/**
* Represents 'true' or 'false'.
*/
-struct BoolLiteral : public Expression {
+class BoolLiteral : public Expression {
+public:
static constexpr Kind kExpressionKind = Kind::kBoolLiteral;
BoolLiteral(const Context& context, int offset, bool value)
- : INHERITED(offset, kExpressionKind, context.fBool_Type.get())
- , fValue(value) {}
+ : INHERITED(offset, kExpressionKind, BoolLiteralData{context.fBool_Type.get(), value}) {}
+
+ bool value() const {
+ return this->boolLiteralData().fValue;
+ }
String description() const override {
- return String(fValue ? "true" : "false");
+ return String(this->value() ? "true" : "false");
}
bool hasProperty(Property property) const override {
@@ -36,22 +40,19 @@
}
bool compareConstant(const Context& context, const Expression& other) const override {
- BoolLiteral& b = (BoolLiteral&) other;
- return fValue == b.fValue;
+ const BoolLiteral& b = other.as<BoolLiteral>();
+ return this->value() == b.value();
}
std::unique_ptr<Expression> clone() const override {
- return std::unique_ptr<Expression>(new BoolLiteral(fOffset, fValue, &this->type()));
+ return std::unique_ptr<Expression>(new BoolLiteral(fOffset, this->value(), &this->type()));
}
- const bool fValue;
-
- using INHERITED = Expression;
-
private:
BoolLiteral(int offset, bool value, const Type* type)
- : INHERITED(offset, kExpressionKind, type)
- , fValue(value) {}
+ : INHERITED(offset, kExpressionKind, BoolLiteralData{type, value}) {}
+
+ using INHERITED = Expression;
};
} // namespace SkSL
diff --git a/src/sksl/ir/SkSLExpression.h b/src/sksl/ir/SkSLExpression.h
index fc5d506..8beb8ed 100644
--- a/src/sksl/ir/SkSLExpression.h
+++ b/src/sksl/ir/SkSLExpression.h
@@ -56,6 +56,11 @@
kContainsRTAdjust
};
+ Expression(int offset, Kind kind, BoolLiteralData data)
+ : INHERITED(offset, (int) kind, data) {
+ SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
+ }
+
Expression(int offset, Kind kind, const Type* type)
: INHERITED(offset, (int) kind, type) {
SkASSERT(kind >= Kind::kFirst && kind <= Kind::kLast);
diff --git a/src/sksl/ir/SkSLIRNode.cpp b/src/sksl/ir/SkSLIRNode.cpp
index e4de26f..38492da 100644
--- a/src/sksl/ir/SkSLIRNode.cpp
+++ b/src/sksl/ir/SkSLIRNode.cpp
@@ -11,18 +11,25 @@
namespace SkSL {
-IRNode::IRNode(int offset, int kind, BlockData data, std::vector<std::unique_ptr<Statement>> stmts)
+IRNode::IRNode(int offset, int kind, const BlockData& data,
+ std::vector<std::unique_ptr<Statement>> stmts)
: fOffset(offset)
, fKind(kind)
, fData(data)
, fStatementChildren(std::move(stmts)) {}
+IRNode::IRNode(int offset, int kind, const BoolLiteralData& data)
+: fOffset(offset)
+, fKind(kind)
+, fData(data) {}
+
+
IRNode::IRNode(int offset, int kind, const Type* data)
: fOffset(offset)
, fKind(kind)
, fData(data) {}
-IRNode::IRNode(int offset, int kind, TypeTokenData data)
+IRNode::IRNode(int offset, int kind, const TypeTokenData& data)
: fOffset(offset)
, fKind(kind)
, fData(data) {}
diff --git a/src/sksl/ir/SkSLIRNode.h b/src/sksl/ir/SkSLIRNode.h
index 4892463..c340fee 100644
--- a/src/sksl/ir/SkSLIRNode.h
+++ b/src/sksl/ir/SkSLIRNode.h
@@ -50,6 +50,8 @@
const Type& type() const {
switch (fData.fKind) {
+ case NodeData::Kind::kBoolLiteral:
+ return *this->boolLiteralData().fType;
case NodeData::Kind::kType:
return *this->typeData();
case NodeData::Kind::kTypeToken:
@@ -68,6 +70,11 @@
bool fIsScope;
};
+ struct BoolLiteralData {
+ const Type* fType;
+ bool fValue;
+ };
+
struct TypeTokenData {
const Type* fType;
Token::Kind fToken;
@@ -75,45 +82,64 @@
struct NodeData {
char fBytes[std::max({sizeof(BlockData),
+ sizeof(BoolLiteralData),
sizeof(Type*),
sizeof(TypeTokenData)})];
enum class Kind {
kBlock,
+ kBoolLiteral,
kType,
kTypeToken,
} fKind;
NodeData() = default;
- NodeData(BlockData data)
+ NodeData(const BlockData& data)
: fKind(Kind::kBlock) {
- new(reinterpret_cast<BlockData*>(fBytes)) BlockData{std::move(data.fSymbolTable),
- data.fIsScope};
+ *(new(fBytes) BlockData) = data;
+ }
+
+ NodeData(const BoolLiteralData& data)
+ : fKind(Kind::kBoolLiteral) {
+ *(new(fBytes) BoolLiteralData) = data;
}
NodeData(const Type* data)
: fKind(Kind::kType) {
- memcpy(fBytes, &data, sizeof(data));
+ *(new(fBytes) const Type*) = data;
}
- NodeData(TypeTokenData data)
+ NodeData(const TypeTokenData& data)
: fKind(Kind::kTypeToken) {
- memcpy(fBytes, &data, sizeof(data));
+ *(new(fBytes) TypeTokenData) = data;
}
~NodeData() {
- if (fKind == Kind::kBlock) {
- reinterpret_cast<BlockData*>(fBytes)->~BlockData();
+ switch (fKind) {
+ case Kind::kBlock:
+ reinterpret_cast<BlockData*>(fBytes)->~BlockData();
+ break;
+ case Kind::kBoolLiteral:
+ reinterpret_cast<BoolLiteralData*>(fBytes)->~BoolLiteralData();
+ break;
+ case Kind::kType:
+ break;
+ case Kind::kTypeToken:
+ reinterpret_cast<TypeTokenData*>(fBytes)->~TypeTokenData();
+ break;
}
}
};
- IRNode(int offset, int kind, BlockData data, std::vector<std::unique_ptr<Statement>> stmts);
+ IRNode(int offset, int kind, const BlockData& data,
+ std::vector<std::unique_ptr<Statement>> stmts);
+
+ IRNode(int offset, int kind, const BoolLiteralData& data);
IRNode(int offset, int kind, const Type* data = nullptr);
- IRNode(int offset, int kind, TypeTokenData data);
+ IRNode(int offset, int kind, const TypeTokenData& data);
IRNode(const IRNode& other);
@@ -166,6 +192,11 @@
return *reinterpret_cast<const BlockData*>(fData.fBytes);
}
+ const BoolLiteralData& boolLiteralData() const {
+ SkASSERT(fData.fKind == NodeData::Kind::kBoolLiteral);
+ return *reinterpret_cast<const BoolLiteralData*>(fData.fBytes);
+ }
+
const Type* typeData() const {
SkASSERT(fData.fKind == NodeData::Kind::kType);
return *reinterpret_cast<const Type* const*>(fData.fBytes);