Implement Type cloning for enums and structs.

As far as I know, there shouldn't be a way to introduce a struct or enum
other than at global scope; the keywords are not accepted inside a
function body. In fact, I wasn't able to find a way to exercise these
code paths in practice. But we now have concrete assurance that any
possible type can be cloned into a symbol table safely; all Types are
either built-in (available everywhere by design) or are clonable.

Change-Id: I4b006b6cab995b3e598b683736ab9689828629c9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/354664
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
This commit is contained in:
John Stiles 2021-01-15 15:34:10 -05:00 committed by Skia Commit-Bot
parent ddcc843e8d
commit 4f2bcff08e
6 changed files with 80 additions and 1 deletions

View File

@ -398,6 +398,7 @@ sksl_folding_tests = [
sksl_inliner_tests = [
"$_tests/sksl/inliner/DoWhileBodyMustBeInlinedIntoAScope.sksl",
"$_tests/sksl/inliner/DoWhileTestCannotBeInlined.sksl",
"$_tests/sksl/inliner/EnumsCanBeInlinedSafely.sksl",
"$_tests/sksl/inliner/ForBodyMustBeInlinedIntoAScope.sksl",
"$_tests/sksl/inliner/ForInitializerExpressionsCanBeInlined.sksl",
"$_tests/sksl/inliner/ForWithReturnInsideCannotBeInlined.sksl",
@ -423,6 +424,7 @@ sksl_inliner_tests = [
"$_tests/sksl/inliner/InlinerWrapsEarlyReturnsWithForLoop.sksl",
"$_tests/sksl/inliner/InlinerWrapsSwitchWithReturnInsideWithForLoop.sksl",
"$_tests/sksl/inliner/ShortCircuitEvaluationsCannotInlineRightHandSide.sksl",
"$_tests/sksl/inliner/StructsCanBeInlinedSafely.sksl",
"$_tests/sksl/inliner/SwitchWithCastCanBeInlined.sksl",
"$_tests/sksl/inliner/SwitchWithoutReturnInsideCanBeInlined.sksl",
"$_tests/sksl/inliner/SwizzleCanBeInlinedDirectly.sksl",

View File

@ -229,8 +229,12 @@ const Type* Type::clone(SymbolTable* symbolTable) const {
this->columns()));
case TypeKind::kStruct:
return symbolTable->add(Type::MakeStructType(this->fOffset, this->name(),
this->fields()));
case TypeKind::kEnum:
// TODO: implement Type cloning for structs and enums.
return symbolTable->add(Type::MakeEnumType(this->name()));
default:
SkDEBUGFAILF("don't know how to clone type '%s'", this->description().c_str());
return nullptr;

View File

@ -0,0 +1,21 @@
half4 helper();
void main() {
sk_FragColor = helper();
}
enum class E {
kBlack = 0,
kGray = 1,
kWhite = 2
};
half4 helper() {
E temp = E::kGray;
switch (temp) {
case E::kBlack: return half4(0, 0, 0, 1);
case E::kGray: return half4(0.5, 0.5, 0.5, 1);
case E::kWhite: return half4(1);
default: return half4(1, 0, 0, 1);
}
}

View File

@ -0,0 +1,21 @@
half4 helper();
void main() {
sk_FragColor = helper();
}
struct Color {
half red;
half green;
half blue;
half alpha;
};
half4 helper() {
Color c;
c.red = 0.25;
c.green = 0.5;
c.blue = 0.75;
c.alpha = 1.0;
return half4(c.red, c.green, c.blue, c.alpha);
}

View File

@ -0,0 +1,14 @@
out vec4 sk_FragColor;
vec4 helper();
void main() {
for (int _1_loop = 0;_1_loop < 1; _1_loop++) {
{
{
continue;
}
}
}
sk_FragColor = vec4(0.5, 0.5, 0.5, 1.0);
}

View File

@ -0,0 +1,17 @@
out vec4 sk_FragColor;
vec4 helper();
void main() {
struct Color {
float red;
float green;
float blue;
float alpha;
} _1_c;
_1_c.red = 0.25;
_1_c.green = 0.5;
_1_c.blue = 0.75;
_1_c.alpha = 1.0;
sk_FragColor = vec4(_1_c.red, _1_c.green, _1_c.blue, _1_c.alpha);
}