[WHLSL] Add the literal type checker
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Jan 2019 00:31:20 +0000 (00:31 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Jan 2019 00:31:20 +0000 (00:31 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193430

Reviewed by Dean Jackson.

This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/LiteralTypeChecker.mjs into C++.

No new tests because it isn't hooked up yet. Not enough of the compiler exists to have any meaningful sort
of test. When enough of the compiler is present, I'll port the reference implementation's test suite.

* Modules/webgpu/WHLSL/AST/WHLSLFloatLiteralType.h:
(WebCore::WHLSL::AST::FloatLiteralType::value const):
* Modules/webgpu/WHLSL/AST/WHLSLIntegerLiteralType.h:
(WebCore::WHLSL::AST::IntegerLiteralType::value const):
* Modules/webgpu/WHLSL/AST/WHLSLUnsignedIntegerLiteralType.h:
(WebCore::WHLSL::AST::UnsignedIntegerLiteralType::value const):
* Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp: Added.
(WebCore::WHLSL::getNativeTypeDeclaration):
(WebCore::WHLSL::LiteralTypeChecker::visit):
(WebCore::WHLSL::checkLiteralTypes):
* Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLFloatLiteralType.h.
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@240095 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLFloatLiteralType.h
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLIntegerLiteralType.h
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLUnsignedIntegerLiteralType.h
Source/WebCore/Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.cpp
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj

index fb20d47..30b5777 100644 (file)
@@ -1,3 +1,29 @@
+2019-01-16  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [WHLSL] Add the literal type checker
+        https://bugs.webkit.org/show_bug.cgi?id=193430
+
+        Reviewed by Dean Jackson.
+
+        This is a translation of https://github.com/gpuweb/WHLSL/blob/master/Source/LiteralTypeChecker.mjs into C++.
+
+        No new tests because it isn't hooked up yet. Not enough of the compiler exists to have any meaningful sort
+        of test. When enough of the compiler is present, I'll port the reference implementation's test suite.
+
+        * Modules/webgpu/WHLSL/AST/WHLSLFloatLiteralType.h:
+        (WebCore::WHLSL::AST::FloatLiteralType::value const):
+        * Modules/webgpu/WHLSL/AST/WHLSLIntegerLiteralType.h:
+        (WebCore::WHLSL::AST::IntegerLiteralType::value const):
+        * Modules/webgpu/WHLSL/AST/WHLSLUnsignedIntegerLiteralType.h:
+        (WebCore::WHLSL::AST::UnsignedIntegerLiteralType::value const):
+        * Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp: Added.
+        (WebCore::WHLSL::getNativeTypeDeclaration):
+        (WebCore::WHLSL::LiteralTypeChecker::visit):
+        (WebCore::WHLSL::checkLiteralTypes):
+        * Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLFloatLiteralType.h.
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+
 2019-01-16  Zalan Bujtas  <zalan@apple.com>
 
         [LFC][BFC] Inflow non-replaced used width should not be negative.
index bc9f037..64743c4 100644 (file)
@@ -53,6 +53,8 @@ public:
 
     bool isFloatLiteralType() const override { return true; }
 
+    float value() const { return m_value; }
+
     TypeReference& preferredType() { return m_preferredType; }
 
     bool canResolve(const Type&) const override;
index 55617b6..3d06b1d 100644 (file)
@@ -53,6 +53,8 @@ public:
 
     bool isIntegerLiteralType() const override { return true; }
 
+    int value() const { return m_value; }
+
     TypeReference& preferredType() { return m_preferredType; }
 
     bool canResolve(const Type&) const override;
index 95fbff6..ebc3106 100644 (file)
@@ -53,6 +53,8 @@ public:
 
     bool isUnsignedIntegerLiteralType() const override { return true; }
 
+    unsigned value() const { return m_value; }
+
     TypeReference& preferredType() { return m_preferredType; }
 
     bool canResolve(const Type&) const override;
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp
new file mode 100644 (file)
index 0000000..b4e5f43
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "WHLSLLiteralTypeChecker.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLIntegerLiteralType.h"
+#include "WHLSLNativeTypeDeclaration.h"
+#include "WHLSLNullLiteralType.h"
+#include "WHLSLProgram.h"
+#include "WHLSLTypeReference.h"
+#include "WHLSLVisitor.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+#if !ASSERT_DISABLED
+static AST::NativeTypeDeclaration* getNativeTypeDeclaration(AST::ResolvableType& resolvableType)
+{
+    if (!resolvableType.resolvedType())
+        return nullptr;
+    if (!is<AST::TypeReference>(*resolvableType.resolvedType()))
+        return nullptr;
+    auto& typeReference = downcast<AST::TypeReference>(*resolvableType.resolvedType());
+    ASSERT(typeReference.resolvedType());
+    if (!is<AST::NativeTypeDeclaration>(*typeReference.resolvedType()))
+        return nullptr;
+    return &downcast<AST::NativeTypeDeclaration>(*typeReference.resolvedType());
+}
+
+class LiteralTypeChecker : public Visitor {
+public:
+private:
+    void visit(AST::FloatLiteralType& floatLiteralType) override
+    {
+        auto* nativeTypeDeclaration = getNativeTypeDeclaration(floatLiteralType);
+        ASSERT(nativeTypeDeclaration);
+        ASSERT(nativeTypeDeclaration->canRepresentFloat()(floatLiteralType.value()));
+    }
+
+    void visit(AST::IntegerLiteralType& integerLiteralType) override
+    {
+        auto* nativeTypeDeclaration = getNativeTypeDeclaration(integerLiteralType);
+        ASSERT(nativeTypeDeclaration);
+        ASSERT(nativeTypeDeclaration->canRepresentInteger()(integerLiteralType.value()));
+    }
+
+    void visit(AST::UnsignedIntegerLiteralType& unsignedIntegerLiteralType) override
+    {
+        auto* nativeTypeDeclaration = getNativeTypeDeclaration(unsignedIntegerLiteralType);
+        ASSERT(nativeTypeDeclaration);
+        ASSERT(nativeTypeDeclaration->canRepresentUnsignedInteger()(unsignedIntegerLiteralType.value()));
+    }
+
+    void visit(AST::NullLiteralType& nullLiteralType) override
+    {
+        ASSERT(nullLiteralType.resolvedType());
+    }
+};
+#endif
+
+void checkLiteralTypes(Program& program)
+{
+#if ASSERT_DISABLED
+    UNUSED_PARAM(program);
+#else
+    LiteralTypeChecker().Visitor::visit(program);
+#endif
+}
+
+} // namespace WHLSL
+
+} // namespace WebCore
+
+#endif // ENABLE(WEBGPU)
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.h
new file mode 100644 (file)
index 0000000..71a46de
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if ENABLE(WEBGPU)
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class Program;
+
+void checkLiteralTypes(Program&);
+
+}
+
+}
+
+#endif
index 5b07f75..0f9c23d 100644 (file)
@@ -2586,8 +2586,8 @@ auto Parser::parseTerm() -> Expected<UniqueRef<AST::Expression>, Error>
     }
 }
 
-}
+} // namespace WHLSL
 
-}
+} // namespace WebCore
 
-#endif
+#endif // ENABLE(WEBGPU)
index e67b551..d4e31ea 100644 (file)
@@ -322,6 +322,7 @@ Modules/webgpu/WHLSL/WHLSLNameContext.cpp
 Modules/webgpu/WHLSL/WHLSLNameResolver.cpp
 Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp
 Modules/webgpu/WHLSL/WHLSLVisitor.cpp
+Modules/webgpu/WHLSL/WHLSLLiteralTypeChecker.cpp
 Modules/webgpu/WHLSL/WHLSLHighZombieFinder.cpp
 Modules/webgpu/WHLSL/WHLSLLoopChecker.cpp
 Modules/webgpu/WHLSL/AST/WHLSLTypeArgument.cpp
index ce49fdf..eb80596 100644 (file)
                1C840B9A21EC400900D0500D /* WHLSLGatherEntryPointItems.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLGatherEntryPointItems.h; sourceTree = "<group>"; };
                1C840B9B21EC400900D0500D /* WHLSLChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLChecker.cpp; sourceTree = "<group>"; };
                1C904DF90BA9D2C80081E9D0 /* Version.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Version.xcconfig; sourceTree = "<group>"; };
+               1CA0C2EA21EED6F500A11860 /* WHLSLLiteralTypeChecker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLLiteralTypeChecker.h; sourceTree = "<group>"; };
+               1CA0C2EC21EED6F600A11860 /* WHLSLLiteralTypeChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLLiteralTypeChecker.cpp; sourceTree = "<group>"; };
                1C9AE5CA21ED9DF50069D5F2 /* WHLSLHighZombieFinder.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLHighZombieFinder.cpp; sourceTree = "<group>"; };
                1C9AE5CB21ED9DF50069D5F2 /* WHLSLHighZombieFinder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLHighZombieFinder.h; sourceTree = "<group>"; };
                1CA0C2F421EEDAD000A11860 /* WHLSLLoopChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLLoopChecker.cpp; sourceTree = "<group>"; };
                C210E90D21B4BCA400B7F83D /* WHLSL */ = {
                        isa = PBXGroup;
                        children = (
-                               C21BF6F121CD898D00227979 /* AST */,
                                1CA0C2F621EEDAD200A11860 /* AST */,
                                C234A9B221E92C1F003C984D /* WHLSLCheckDuplicateFunctions.cpp */,
                                C234A9AE21E92C1A003C984D /* WHLSLCheckDuplicateFunctions.h */,
                                C234A9B621E92CC0003C984D /* WHLSLIntrinsics.h */,
                                C210E91121B4BD1000B7F83D /* WHLSLLexer.cpp */,
                                C210E91221B4BD1000B7F83D /* WHLSLLexer.h */,
+                               1CA0C2EC21EED6F600A11860 /* WHLSLLiteralTypeChecker.cpp */,
+                               1CA0C2EA21EED6F500A11860 /* WHLSLLiteralTypeChecker.h */,
                                1CA0C2F421EEDAD000A11860 /* WHLSLLoopChecker.cpp */,
                                1CA0C2F521EEDAD100A11860 /* WHLSLLoopChecker.h */,
                                C234A98D21E88884003C984D /* WHLSLNameContext.cpp */,
                        path = WHLSL;
                        sourceTree = "<group>";
                };
-               C21BF6F121CD898D00227979 /* AST */ = {
+               1CA0C2F621EEDAD200A11860 /* AST */ = {
                        isa = PBXGroup;
                        children = (
                                1C840B9021EC30F900D0500D /* WHLSLAddressSpace.h */,