Added: trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor.html (0 => 247675)
--- trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor.html (rev 0)
+++ trunk/LayoutTests/webgpu/whlsl/enum-integer-constructor.html 2019-07-21 18:31:53 UTC (rev 247675)
@@ -0,0 +1,103 @@
+<!DOCTYPE html>
+<html>
+<meta charset=utf-8>
+<meta name="timeout" content="long">
+<title>Enums.</title>
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script>
+const whlslTests = {};
+
+whlslTests.simpleEnumFromInt = async () => {
+ let program = `
+ enum Foo {
+ A,
+ B,
+ C
+ }
+
+ int foo(int x) {
+ return int(Foo(x));
+ }
+ `;
+
+ assert_equals(await callIntFunction(program, "foo", [makeInt(42)]), 0);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(0)]), 0);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(1)]), 1);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(2)]), 2);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(-1)]), 0);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(2147483647)]), 0);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(-2147483648)]), 0);
+};
+
+whlslTests.simpleEnumFromUint = async () => {
+ let program = `
+ enum Foo : uint {
+ A,
+ B,
+ C
+ }
+
+ uint foo(uint x) {
+ return uint(Foo(x));
+ }
+ `;
+
+ assert_equals(await callUintFunction(program, "foo", [makeUint(0)]), 0);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(1)]), 1);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(2)]), 2);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(42)]), 0);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(1000000)]), 0);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(4294967295)]), 0);
+};
+
+whlslTests.weirdEnumFromInt = async () => {
+ let program = `
+ enum Foo : int {
+ A = 42,
+ B = -1000,
+ C = 101010,
+ D = 0
+ }
+
+ int foo(int x) {
+ return int(Foo(x));
+ }
+ `;
+
+ assert_equals(await callIntFunction(program, "foo", [makeInt(0)]), 0);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(42)]), 42);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(-1000)]), -1000);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(101010)]), 101010);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(-1)]), 0);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(2147483647)]), 0);
+ assert_equals(await callIntFunction(program, "foo", [makeInt(-2147483648)]), 0);
+};
+
+whlslTests.weirdEnumFromUint = async () => {
+ let program = `
+ enum Foo : uint {
+ A = 42,
+ C = 101010,
+ D = 0
+ }
+
+ uint foo(uint x) {
+ return uint(Foo(x));
+ }
+ `;
+
+ assert_equals(await callUintFunction(program, "foo", [makeUint(0)]), 0);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(42)]), 42);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(101010)]), 101010);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(4294967295)]), 0);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(1)]), 0);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(41)]), 0);
+ assert_equals(await callUintFunction(program, "foo", [makeUint(101011)]), 0);
+};
+
+runTests(whlslTests);
+</script>
+</html>
Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp (247674 => 247675)
--- trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp 2019-07-21 17:23:54 UTC (rev 247674)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/Metal/WHLSLNativeFunctionWriter.cpp 2019-07-21 18:31:53 UTC (rev 247675)
@@ -31,6 +31,7 @@
#include "NotImplemented.h"
#include "WHLSLAddressSpace.h"
#include "WHLSLArrayType.h"
+#include "WHLSLEnumerationDefinition.h"
#include "WHLSLInferTypes.h"
#include "WHLSLIntrinsics.h"
#include "WHLSLNamedType.h"
@@ -123,7 +124,8 @@
{
StringBuilder stringBuilder;
if (nativeFunctionDeclaration.isCast()) {
- auto metalReturnName = typeNamer.mangledNameForType(nativeFunctionDeclaration.type());
+ auto& returnType = nativeFunctionDeclaration.type();
+ auto metalReturnName = typeNamer.mangledNameForType(returnType);
if (!nativeFunctionDeclaration.parameters().size()) {
stringBuilder.append(makeString(metalReturnName, ' ', outputFunctionName, "() {\n"));
stringBuilder.append(makeString(" ", metalReturnName, " x;\n"));
@@ -134,8 +136,28 @@
}
ASSERT(nativeFunctionDeclaration.parameters().size() == 1);
- auto metalParameterName = typeNamer.mangledNameForType(*nativeFunctionDeclaration.parameters()[0]->type());
+ auto& parameterType = *nativeFunctionDeclaration.parameters()[0]->type();
+ auto metalParameterName = typeNamer.mangledNameForType(parameterType);
stringBuilder.append(makeString(metalReturnName, ' ', outputFunctionName, '(', metalParameterName, " x) {\n"));
+
+ {
+ auto isEnumerationDefinition = [] (auto& type) {
+ return is<AST::NamedType>(type) && is<AST::EnumerationDefinition>(downcast<AST::NamedType>(type));
+ };
+ auto& unifiedReturnType = returnType.unifyNode();
+ if (isEnumerationDefinition(unifiedReturnType) && !isEnumerationDefinition(parameterType.unifyNode())) {
+ auto& enumerationDefinition = downcast<AST::EnumerationDefinition>(downcast<AST::NamedType>(unifiedReturnType));
+ stringBuilder.append(" switch (x) {\n");
+ bool hasZeroCase = false;
+ for (auto& member : enumerationDefinition.enumerationMembers()) {
+ hasZeroCase |= !member.get().value();
+ stringBuilder.append(makeString(" case ", member.get().value(), ": break;\n"));
+ }
+ ASSERT_UNUSED(hasZeroCase, hasZeroCase);
+ stringBuilder.append(" default: x = 0; break; }\n");
+ }
+ }
+
stringBuilder.append(makeString(" return static_cast<", metalReturnName, ">(x);\n"));
stringBuilder.append("}\n");
return stringBuilder.toString();