[WHLSL] Add native function synthesis passes
authormmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 12 Jan 2019 20:56:37 +0000 (20:56 +0000)
committermmaxfield@apple.com <mmaxfield@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 12 Jan 2019 20:56:37 +0000 (20:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193360

Reviewed by Dean Jackson.

This patch includes all the passes in prepare() that are between the name resolver and the
type checker. It involves a few small pieces:

- CheckDuplicateFunctions which makes sure the same function isn't defined twice
- Intrinsics, which remembers all of the native types so they can be referred to by the
  rest of the compiler
- RecursiveTypeChecker which makes sure types don't refer to themselves
- SynthesizeArrayOperatorLength which creates operator.length() functions for arrays
- SynthesizeConstructors which creates copy constructors and default constructors for all
  types
- SynthesizeEnumerationFunctions which provides cast operators between enum types and their
  base types
- SynthesizeStructureAccessors which provides getters, setters, and anders for each member
  of a struct

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/WHLSLBuiltInSemantic.cpp:
(WebCore::WHLSL::AST::BuiltInSemantic::isAcceptableType const):
* Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp:
(WebCore::WHLSL::AST::ResourceSemantic::isAcceptableType const):
* Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.cpp: Added.
(WebCore::WHLSL::checkDuplicateFunctions):
* Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
* Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp: Added.
(WebCore::WHLSL::Intrinsics::Intrinsics):
(WebCore::WHLSL::Intrinsics::add):
(WebCore::WHLSL::Intrinsics::addPrimitive):
(WebCore::WHLSL::Intrinsics::addVector):
(WebCore::WHLSL::Intrinsics::addMatrix):
(WebCore::WHLSL::Intrinsics::addFullTexture):
(WebCore::WHLSL::Intrinsics::addDepthTexture):
(WebCore::WHLSL::Intrinsics::addTexture):
* Modules/webgpu/WHLSL/WHLSLIntrinsics.h: Added.
(WebCore::WHLSL::Intrinsics::voidType const):
(WebCore::WHLSL::Intrinsics::boolType const):
(WebCore::WHLSL::Intrinsics::intType const):
(WebCore::WHLSL::Intrinsics::uintType const):
(WebCore::WHLSL::Intrinsics::samplerType const):
(WebCore::WHLSL::Intrinsics::floatType const):
(WebCore::WHLSL::Intrinsics::float3Type const):
(WebCore::WHLSL::Intrinsics::float4Type const):
* Modules/webgpu/WHLSL/WHLSLProgram.h:
(WebCore::WHLSL::Program::append):
(WebCore::WHLSL::Program::intrinsics):
* Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp: Added.
(WebCore::WHLSL::checkRecursiveTypes):
* Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
* Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.cpp: Added.
(WebCore::WHLSL::FindArrayTypes::takeArrayTypes):
(WebCore::WHLSL::synthesizeArrayOperatorLength):
* Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
* Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp: Added.
(WebCore::WHLSL::FindAllTypes::takeUnnamedTypes):
(WebCore::WHLSL::FindAllTypes::takeNamedTypes):
(WebCore::WHLSL::synthesizeConstructors):
* Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
* Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.cpp: Added.
(WebCore::WHLSL::synthesizeEnumerationFunctions):
* Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
* Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.cpp: Added.
(WebCore::WHLSL::synthesizeStructureAccessors):
* Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:

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

25 files changed:
Source/WebCore/ChangeLog
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLBuiltInSemantic.cpp
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLFloatLiteralType.cpp
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLIndexExpression.h
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLIntegerLiteralType.cpp
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp
Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLUnsignedIntegerLiteralType.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLIntrinsics.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLNameResolver.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLProgram.h
Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.h [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.cpp [new file with mode: 0644]
Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.h [new file with mode: 0644]
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj

index dfc5139..199ffd3 100644 (file)
@@ -1,3 +1,77 @@
+2019-01-12  Myles C. Maxfield  <mmaxfield@apple.com>
+
+        [WHLSL] Add native function synthesis passes
+        https://bugs.webkit.org/show_bug.cgi?id=193360
+
+        Reviewed by Dean Jackson.
+
+        This patch includes all the passes in prepare() that are between the name resolver and the
+        type checker. It involves a few small pieces:
+
+        - CheckDuplicateFunctions which makes sure the same function isn't defined twice
+        - Intrinsics, which remembers all of the native types so they can be referred to by the
+          rest of the compiler
+        - RecursiveTypeChecker which makes sure types don't refer to themselves
+        - SynthesizeArrayOperatorLength which creates operator.length() functions for arrays
+        - SynthesizeConstructors which creates copy constructors and default constructors for all
+          types
+        - SynthesizeEnumerationFunctions which provides cast operators between enum types and their
+          base types
+        - SynthesizeStructureAccessors which provides getters, setters, and anders for each member
+          of a struct
+
+        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/WHLSLBuiltInSemantic.cpp:
+        (WebCore::WHLSL::AST::BuiltInSemantic::isAcceptableType const):
+        * Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp:
+        (WebCore::WHLSL::AST::ResourceSemantic::isAcceptableType const):
+        * Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.cpp: Added.
+        (WebCore::WHLSL::checkDuplicateFunctions):
+        * Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
+        * Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp: Added.
+        (WebCore::WHLSL::Intrinsics::Intrinsics):
+        (WebCore::WHLSL::Intrinsics::add):
+        (WebCore::WHLSL::Intrinsics::addPrimitive):
+        (WebCore::WHLSL::Intrinsics::addVector):
+        (WebCore::WHLSL::Intrinsics::addMatrix):
+        (WebCore::WHLSL::Intrinsics::addFullTexture):
+        (WebCore::WHLSL::Intrinsics::addDepthTexture):
+        (WebCore::WHLSL::Intrinsics::addTexture):
+        * Modules/webgpu/WHLSL/WHLSLIntrinsics.h: Added.
+        (WebCore::WHLSL::Intrinsics::voidType const):
+        (WebCore::WHLSL::Intrinsics::boolType const):
+        (WebCore::WHLSL::Intrinsics::intType const):
+        (WebCore::WHLSL::Intrinsics::uintType const):
+        (WebCore::WHLSL::Intrinsics::samplerType const):
+        (WebCore::WHLSL::Intrinsics::floatType const):
+        (WebCore::WHLSL::Intrinsics::float3Type const):
+        (WebCore::WHLSL::Intrinsics::float4Type const):
+        * Modules/webgpu/WHLSL/WHLSLProgram.h:
+        (WebCore::WHLSL::Program::append):
+        (WebCore::WHLSL::Program::intrinsics):
+        * Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp: Added.
+        (WebCore::WHLSL::checkRecursiveTypes):
+        * Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.cpp: Added.
+        (WebCore::WHLSL::FindArrayTypes::takeArrayTypes):
+        (WebCore::WHLSL::synthesizeArrayOperatorLength):
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp: Added.
+        (WebCore::WHLSL::FindAllTypes::takeUnnamedTypes):
+        (WebCore::WHLSL::FindAllTypes::takeNamedTypes):
+        (WebCore::WHLSL::synthesizeConstructors):
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.cpp: Added.
+        (WebCore::WHLSL::synthesizeEnumerationFunctions):
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.cpp: Added.
+        (WebCore::WHLSL::synthesizeStructureAccessors):
+        * Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.h: Copied from Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLResourceSemantic.cpp.
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+
 2019-01-12  Dan Bernstein  <mitz@apple.com>
 
         [Cocoa] Avoid importing directly from subumbrella frameworks
index e42ee47..414bd45 100644 (file)
@@ -29,6 +29,8 @@
 #if ENABLE(WEBGPU)
 
 #include "WHLSLFunctionDefinition.h"
+#include "WHLSLInferTypes.h"
+#include "WHLSLIntrinsics.h"
 #include "WHLSLTypeReference.h"
 
 namespace WebCore {
@@ -37,10 +39,38 @@ namespace WHLSL {
 
 namespace AST {
 
-bool BuiltInSemantic::isAcceptableType(const UnnamedType&, const Intrinsics&) const
+bool BuiltInSemantic::isAcceptableType(const UnnamedType& unnamedType, const Intrinsics& intrinsics) const
 {
-    // FIXME: Implement this
-    return true;
+    switch (m_variable) {
+    case Variable::SVInstanceID:
+        return matches(unnamedType, intrinsics.uintType());
+    case Variable::SVVertexID:
+        return matches(unnamedType, intrinsics.uintType());
+    case Variable::PSize:
+        return matches(unnamedType, intrinsics.floatType());
+    case Variable::SVPosition:
+        return matches(unnamedType, intrinsics.float4Type());
+    case Variable::SVIsFrontFace:
+        return matches(unnamedType, intrinsics.boolType());
+    case Variable::SVSampleIndex:
+        return matches(unnamedType, intrinsics.uintType());
+    case Variable::SVInnerCoverage:
+        return matches(unnamedType, intrinsics.uintType());
+    case Variable::SVTarget:
+        return matches(unnamedType, intrinsics.float4Type());
+    case Variable::SVDepth:
+        return matches(unnamedType, intrinsics.floatType());
+    case Variable::SVCoverage:
+        return matches(unnamedType, intrinsics.uintType());
+    case Variable::SVDispatchThreadID:
+        return matches(unnamedType, intrinsics.float3Type());
+    case Variable::SVGroupID:
+        return matches(unnamedType, intrinsics.float3Type());
+    case Variable::SVGroupIndex:
+        return matches(unnamedType, intrinsics.uintType());
+    case Variable::SVGroupThreadID:
+        return matches(unnamedType, intrinsics.float3Type());
+    }
 }
 
 bool BuiltInSemantic::isAcceptableForShaderItemDirection(ShaderItemDirection direction, const FunctionDefinition& functionDefinition) const
index 3325cc5..81c9dde 100644 (file)
@@ -40,7 +40,7 @@ namespace AST {
 
 FloatLiteralType::FloatLiteralType(Lexer::Token&& origin, float value)
     : m_value(value)
-    , m_preferredType(makeUniqueRef<TypeReference>(WTFMove(origin), String("float", String::ConstructFromLiteral), TypeArguments()))
+    , m_preferredType(makeUniqueRef<TypeReference>(WTFMove(origin), "float"_str, TypeArguments()))
 {
 }
 
index ab89774..25ba6e7 100644 (file)
@@ -54,17 +54,17 @@ public:
 
     String getFunctionName() const override
     {
-        return String("operator[]", String::ConstructFromLiteral);
+        return "operator[]"_str;
     }
 
     String setFunctionName() const override
     {
-        return String("operator&[]", String::ConstructFromLiteral);
+        return "operator&[]"_str;
     }
 
     String andFunctionName() const override
     {
-        return String("operator[]=", String::ConstructFromLiteral);
+        return "operator[]="_str;
     }
 
     Expression& indexExpression() { return static_cast<Expression&>(m_index); }
index 3223641..e87acda 100644 (file)
@@ -41,7 +41,7 @@ namespace AST {
 
 IntegerLiteralType::IntegerLiteralType(Lexer::Token&& origin, int value)
     : m_value(value)
-    , m_preferredType(makeUniqueRef<TypeReference>(WTFMove(origin), String("int", String::ConstructFromLiteral), TypeArguments()))
+    , m_preferredType(makeUniqueRef<TypeReference>(WTFMove(origin), "int"_str, TypeArguments()))
 {
 }
 
index 65fbb33..8440960 100644 (file)
@@ -29,6 +29,8 @@
 #if ENABLE(WEBGPU)
 
 #include "WHLSLArrayType.h"
+#include "WHLSLInferTypes.h"
+#include "WHLSLIntrinsics.h"
 #include "WHLSLReferenceType.h"
 #include "WHLSLTypeReference.h"
 
@@ -38,10 +40,42 @@ namespace WHLSL {
 
 namespace AST {
 
-bool ResourceSemantic::isAcceptableType(const UnnamedType&, const Intrinsics&) const
+bool ResourceSemantic::isAcceptableType(const UnnamedType& unnamedType, const Intrinsics& intrinsics) const
 {
-    // FIXME: Implement this
-    return true;
+    switch (m_mode) {
+    case Mode::UnorderedAccessView:
+        if (is<ReferenceType>(unnamedType)) {
+            auto& referenceType = downcast<ReferenceType>(unnamedType);
+            return referenceType.addressSpace() == ReferenceType::AddressSpace::Constant || referenceType.addressSpace() == ReferenceType::AddressSpace::Device;
+        }
+        if (is<ArrayType>(unnamedType))
+            return true;
+        if (is<TypeReference>(unnamedType)) {
+            auto& typeReference = downcast<TypeReference>(unnamedType);
+            ASSERT(typeReference.resolvedType());
+            if (is<NativeTypeDeclaration>(*typeReference.resolvedType()))
+                return downcast<NativeTypeDeclaration>(*typeReference.resolvedType()).isTexture();
+        }
+        return false;
+    case Mode::Texture:
+        if (is<ReferenceType>(unnamedType))
+            return downcast<ReferenceType>(unnamedType).addressSpace() == ReferenceType::AddressSpace::Constant;
+        if (is<ArrayType>(unnamedType))
+            return true;
+        if (is<TypeReference>(unnamedType)) {
+            auto& typeReference = downcast<TypeReference>(unnamedType);
+            ASSERT(typeReference.resolvedType());
+            if (is<NativeTypeDeclaration>(*typeReference.resolvedType()))
+                return downcast<NativeTypeDeclaration>(*typeReference.resolvedType()).isTexture();
+        }
+        return false;
+    case Mode::Buffer:
+        if (is<ReferenceType>(unnamedType))
+            return downcast<ReferenceType>(unnamedType).addressSpace() == ReferenceType::AddressSpace::Constant;
+        return is<ArrayType>(unnamedType);
+    case Mode::Sampler:
+        return matches(unnamedType, intrinsics.samplerType());
+    }
 }
 
 bool ResourceSemantic::isAcceptableForShaderItemDirection(ShaderItemDirection direction, const FunctionDefinition&) const
index bf31a3c..01f3197 100644 (file)
@@ -42,7 +42,7 @@ namespace AST {
 
 UnsignedIntegerLiteralType::UnsignedIntegerLiteralType(Lexer::Token&& origin, unsigned value)
     : m_value(value)
-    , m_preferredType(makeUniqueRef<TypeReference>(WTFMove(origin), String("uint", String::ConstructFromLiteral), TypeArguments()))
+    , m_preferredType(makeUniqueRef<TypeReference>(WTFMove(origin), "uint"_str, TypeArguments()))
 {
 }
 
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.cpp
new file mode 100644 (file)
index 0000000..57b462e
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * 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 "WHLSLCheckDuplicateFunctions.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLArrayReferenceType.h"
+#include "WHLSLArrayType.h"
+#include "WHLSLInferTypes.h"
+#include "WHLSLTypeReference.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+bool checkDuplicateFunctions(const Program& program)
+{
+    Vector<std::reference_wrapper<const AST::FunctionDeclaration>> functions;
+    for (auto& functionDefinition : program.functionDefinitions())
+        functions.append(static_cast<const AST::FunctionDefinition&>(functionDefinition));
+    for (auto& nativeFunctionDeclaration : program.nativeFunctionDeclarations())
+        functions.append(static_cast<const AST::NativeFunctionDeclaration&>(nativeFunctionDeclaration));
+
+    std::sort(functions.begin(), functions.end(), [](const AST::FunctionDeclaration& a, const AST::FunctionDeclaration& b) -> bool {
+        if (a.name().length() < b.name().length())
+            return true;
+        if (a.name().length() > b.name().length())
+            return false;
+        for (unsigned i = 0; i < a.name().length(); ++i) {
+            if (a.name()[i] < b.name()[i])
+                return true;
+            if (a.name()[i] > b.name()[i])
+                return false;
+        }
+        return false;
+    });
+    for (size_t i = 0; i < functions.size(); ++i) {
+        for (size_t j = i + 1; j < functions.size(); ++i) {
+            if (functions[i].get().name() != functions[j].get().name())
+                break;
+            if (is<AST::NativeFunctionDeclaration>(functions[i].get()) && is<AST::NativeFunctionDeclaration>(functions[j].get()))
+                continue;
+            if (functions[i].get().parameters().size() != functions[j].get().parameters().size())
+                continue;
+            if (functions[i].get().isCast() && !matches(functions[i].get().type(), functions[j].get().type()))
+                continue;
+            bool same = true;
+            for (size_t k = 0; k < functions[i].get().parameters().size(); ++k) {
+                if (!matches(*functions[i].get().parameters()[k].type(), *functions[j].get().parameters()[k].type())) {
+                    same = false;
+                    break;
+                }
+            }
+            if (same)
+                return false;
+        }
+        
+        if (functions[i].get().name() == "operator&[]" && functions[i].get().parameters().size() == 2
+            && is<AST::ArrayReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type()))) {
+            auto& type = static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[1].type());
+            if (is<AST::TypeReference>(type)) {
+                if (auto* resolvedType = downcast<AST::TypeReference>(type).resolvedType()) {
+                    if (is<AST::NativeTypeDeclaration>(*resolvedType)) {
+                        auto& nativeTypeDeclaration = downcast<AST::NativeTypeDeclaration>(*resolvedType);
+                        if (nativeTypeDeclaration.name() == "uint")
+                            return false;
+                    }
+                }
+            }
+        } else if (functions[i].get().name() == "operator.length" && functions[i].get().parameters().size() == 1
+            && (is<AST::ArrayReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type()))
+            || is<AST::ArrayType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type()))))
+            return false;
+        else if (functions[i].get().name() == "operator=="
+            && functions[i].get().parameters().size() == 2
+            && is<AST::ReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type()))
+            && is<AST::ReferenceType>(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[1].type()))
+            && matches(static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[0].type()), static_cast<const AST::UnnamedType&>(*functions[i].get().parameters()[1].type())))
+            return false;
+    }
+    return true;
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.h
new file mode 100644 (file)
index 0000000..c1caf55
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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)
+
+#include "WHLSLProgram.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+bool checkDuplicateFunctions(const Program&);
+
+}
+
+}
+
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp
new file mode 100644 (file)
index 0000000..4768785
--- /dev/null
@@ -0,0 +1,424 @@
+/*
+ * 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 "WHLSLIntrinsics.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLConstantExpression.h"
+#include "WHLSLTypeArgument.h"
+#include "WHLSLTypeReference.h"
+#include <algorithm>
+#include <cstring>
+
+namespace WebCore {
+
+namespace WHLSL {
+
+constexpr const char* Intrinsics::m_textureTypeNames[];
+constexpr const char* Intrinsics::m_textureInnerTypeNames[];
+constexpr const char* Intrinsics::m_depthTextureInnerTypes[];
+
+Intrinsics::Intrinsics()
+{
+}
+
+void Intrinsics::add(AST::NativeFunctionDeclaration&)
+{
+    // FIXME: Populate this.
+}
+
+bool Intrinsics::addPrimitive(AST::NativeTypeDeclaration& nativeTypeDeclaration)
+{
+    if (nativeTypeDeclaration.typeArguments().size())
+        return false;
+
+    if (nativeTypeDeclaration.name() == "void")
+        m_voidType = &nativeTypeDeclaration;
+    else if (nativeTypeDeclaration.name() == "bool")
+        m_boolType = &nativeTypeDeclaration;
+    else if (nativeTypeDeclaration.name() == "uchar") {
+        nativeTypeDeclaration.setIsInt();
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setCanRepresentInteger([](int x) {
+            return x >= 0 && x <= 0xFF;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned x) {
+            return x <= 0xFF;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float x) {
+            return static_cast<float>(static_cast<uint8_t>(x)) == x;
+        });
+        nativeTypeDeclaration.setSuccessor([](int64_t x) -> int64_t {
+            return static_cast<uint8_t>(x + 1);
+        });
+        nativeTypeDeclaration.setFormatValueFromInteger([](int x) -> int64_t {
+            return static_cast<uint8_t>(x);
+        });
+        nativeTypeDeclaration.setFormatValueFromUnsignedInteger([](unsigned x) -> int64_t {
+            return static_cast<uint8_t>(x);
+        });
+        nativeTypeDeclaration.setIterateAllValues([](const std::function<bool(int64_t)>& callback) {
+            for (int64_t i = 0; i < 0x100; ++i) {
+                if (callback(i))
+                    break;
+            }
+        });
+        m_ucharType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "ushort") {
+        nativeTypeDeclaration.setIsInt();
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setCanRepresentInteger([](int x) {
+            return x >= 0 && x <= 0xFFFF;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned x) {
+            return x <= 0xFFFF;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float x) {
+            return static_cast<float>(static_cast<uint16_t>(x)) == x;
+        });
+        nativeTypeDeclaration.setSuccessor([](int64_t x) -> int64_t {
+            return static_cast<uint16_t>(x + 1);
+        });
+        nativeTypeDeclaration.setFormatValueFromInteger([](int x) -> int64_t {
+            return static_cast<uint16_t>(x);
+        });
+        nativeTypeDeclaration.setFormatValueFromUnsignedInteger([](unsigned x) -> int64_t {
+            return static_cast<uint16_t>(x);
+        });
+        nativeTypeDeclaration.setIterateAllValues([](const std::function<bool(int64_t)>& callback) {
+            for (int64_t i = 0; i < 0x10000; ++i) {
+                if (callback(i))
+                    break;
+            }
+        });
+        m_ushortType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "uint") {
+        nativeTypeDeclaration.setIsInt();
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setCanRepresentInteger([](int x) {
+            return x >= 0;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned) {
+            return true;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float x) {
+            return static_cast<float>(static_cast<uint32_t>(x)) == x;
+        });
+        nativeTypeDeclaration.setSuccessor([](int64_t x) -> int64_t {
+            return static_cast<uint32_t>(x + 1);
+        });
+        nativeTypeDeclaration.setFormatValueFromInteger([](int x) -> int64_t {
+            return static_cast<uint32_t>(x);
+        });
+        nativeTypeDeclaration.setFormatValueFromUnsignedInteger([](unsigned x) -> int64_t {
+            return static_cast<uint32_t>(x);
+        });
+        nativeTypeDeclaration.setIterateAllValues([](const std::function<bool(int64_t)>& callback) {
+            for (int64_t i = 0; i < 0x100000000; ++i) {
+                if (callback(i))
+                    break;
+            }
+        });
+        m_uintType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "char") {
+        nativeTypeDeclaration.setIsInt();
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setIsSigned();
+        nativeTypeDeclaration.setCanRepresentInteger([](int x) {
+            return x >= -128 && x <= 127;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned x) {
+            return x <= 127;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float x) {
+            return static_cast<float>(static_cast<int8_t>(x)) == x;
+        });
+        nativeTypeDeclaration.setSuccessor([](int64_t x) -> int64_t {
+            return static_cast<int8_t>(x + 1);
+        });
+        nativeTypeDeclaration.setFormatValueFromInteger([](int x) -> int64_t {
+            return static_cast<int8_t>(x);
+        });
+        nativeTypeDeclaration.setFormatValueFromUnsignedInteger([](unsigned x) -> int64_t {
+            return static_cast<int8_t>(x);
+        });
+        nativeTypeDeclaration.setIterateAllValues([](const std::function<bool(int64_t)>& callback) {
+            for (int64_t i = -128; i < 128; ++i) {
+                if (callback(i))
+                    break;
+            }
+        });
+        m_charType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "short") {
+        nativeTypeDeclaration.setIsInt();
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setIsSigned();
+        nativeTypeDeclaration.setCanRepresentInteger([](int x) {
+            return x >= -32768 && x <= 32767;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned x) {
+            return x <= 32767;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float x) {
+            return static_cast<float>(static_cast<int16_t>(x)) == x;
+        });
+        nativeTypeDeclaration.setSuccessor([](int64_t x) -> int64_t {
+            return static_cast<int16_t>(x + 1);
+        });
+        nativeTypeDeclaration.setFormatValueFromInteger([](int x) -> int64_t {
+            return static_cast<int16_t>(x);
+        });
+        nativeTypeDeclaration.setFormatValueFromUnsignedInteger([](unsigned x) -> int64_t {
+            return static_cast<int16_t>(x);
+        });
+        nativeTypeDeclaration.setIterateAllValues([](const std::function<bool(int64_t)>& callback) {
+            for (int64_t i = -32768; i < 32768; ++i) {
+                if (callback(i))
+                    break;
+            }
+        });
+        m_shortType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "int") {
+        nativeTypeDeclaration.setIsInt();
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setIsSigned();
+        nativeTypeDeclaration.setCanRepresentInteger([](int) {
+            return true;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned x) {
+            return x <= 2147483647;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float x) {
+            return static_cast<float>(static_cast<int32_t>(x)) == x;
+        });
+        nativeTypeDeclaration.setSuccessor([](int64_t x) -> int64_t {
+            return static_cast<int32_t>(x + 1);
+        });
+        nativeTypeDeclaration.setFormatValueFromInteger([](int x) -> int64_t {
+            return static_cast<int32_t>(x);
+        });
+        nativeTypeDeclaration.setFormatValueFromUnsignedInteger([](unsigned x) -> int64_t {
+            return static_cast<int32_t>(x);
+        });
+        nativeTypeDeclaration.setIterateAllValues([](const std::function<bool(int64_t)>& callback) {
+            for (int64_t i = -2147483647; i < 2147483648; ++i) {
+                if (callback(i))
+                    break;
+            }
+        });
+        m_intType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "half") {
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setIsFloating();
+        nativeTypeDeclaration.setCanRepresentInteger([](int) {
+            return true;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned) {
+            return true;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float) {
+            return true;
+        });
+        m_halfType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "float") {
+        nativeTypeDeclaration.setIsNumber();
+        nativeTypeDeclaration.setIsFloating();
+        nativeTypeDeclaration.setCanRepresentInteger([](int) {
+            return true;
+        });
+        nativeTypeDeclaration.setCanRepresentUnsignedInteger([](unsigned) {
+            return true;
+        });
+        nativeTypeDeclaration.setCanRepresentFloat([](float) {
+            return true;
+        });
+        m_floatType = &nativeTypeDeclaration;
+    } else if (nativeTypeDeclaration.name() == "atomic_int")
+        m_atomicIntType = &nativeTypeDeclaration;
+    else if (nativeTypeDeclaration.name() == "atomic_uint")
+        m_atomicUintType = &nativeTypeDeclaration;
+    else if (nativeTypeDeclaration.name() == "sampler")
+        m_samplerType = &nativeTypeDeclaration;
+    else
+        ASSERT_NOT_REACHED();
+    return true;
+}
+
+bool Intrinsics::addVector(AST::NativeTypeDeclaration& nativeTypeDeclaration)
+{
+    if (nativeTypeDeclaration.name() != "vector")
+        return false;
+
+    ASSERT(nativeTypeDeclaration.typeArguments().size() == 2);
+    ASSERT(WTF::holds_alternative<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
+    ASSERT(WTF::holds_alternative<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]));
+    auto& innerType = static_cast<AST::TypeReference&>(WTF::get<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
+    auto& lengthExpression = WTF::get<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]);
+    ASSERT(!innerType.typeArguments().size());
+    AST::NativeTypeDeclaration** array;
+    if (innerType.name() == "bool")
+        array = m_vectorBool;
+    else if (innerType.name() == "uchar")
+        array = m_vectorUchar;
+    else if (innerType.name() == "ushort")
+        array = m_vectorUshort;
+    else if (innerType.name() == "uint")
+        array = m_vectorUint;
+    else if (innerType.name() == "char")
+        array = m_vectorChar;
+    else if (innerType.name() == "short")
+        array = m_vectorShort;
+    else if (innerType.name() == "int")
+        array = m_vectorInt;
+    else if (innerType.name() == "half")
+        array = m_vectorHalf;
+    else {
+        ASSERT(innerType.name() == "float");
+        array = m_vectorFloat;
+    }
+    int length = lengthExpression.integerLiteral().value();
+    ASSERT(length >= 2 && length <= 4);
+    nativeTypeDeclaration.setIsVector();
+    array[length - 2] = &nativeTypeDeclaration;
+    return true;
+}
+
+bool Intrinsics::addMatrix(AST::NativeTypeDeclaration& nativeTypeDeclaration)
+{
+    if (nativeTypeDeclaration.name() != "matrix")
+        return false;
+
+    ASSERT(nativeTypeDeclaration.typeArguments().size() == 3);
+    ASSERT(WTF::holds_alternative<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
+    ASSERT(WTF::holds_alternative<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]));
+    ASSERT(WTF::holds_alternative<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[2]));
+    auto& innerType = static_cast<AST::TypeReference&>(WTF::get<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
+    auto& rowExpression = WTF::get<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[1]);
+    auto& columnExpression = WTF::get<AST::ConstantExpression>(nativeTypeDeclaration.typeArguments()[2]);
+    ASSERT(!innerType.typeArguments().size());
+    AST::NativeTypeDeclaration* (*array)[3];
+    if (innerType.name() == "half")
+        array = m_matrixHalf;
+    if (innerType.name() == "float")
+        array = m_matrixFloat;
+    int row = rowExpression.integerLiteral().value();
+    ASSERT(row >= 2 && row <= 4);
+    int column = columnExpression.integerLiteral().value();
+    ASSERT(column >= 2 && column <= 4);
+    nativeTypeDeclaration.setIsMatrix();
+    array[row - 2][column - 2] = &nativeTypeDeclaration;
+    return true;
+}
+
+bool Intrinsics::addFullTexture(AST::NativeTypeDeclaration& nativeTypeDeclaration, AST::TypeReference& innerType)
+{
+    unsigned textureTypeIndex = WTF_ARRAY_LENGTH(m_textureTypeNames);
+    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(m_textureTypeNames); ++i) {
+        if (nativeTypeDeclaration.name() == m_textureTypeNames[i])
+            textureTypeIndex = i;
+    }
+    if (textureTypeIndex == WTF_ARRAY_LENGTH(m_textureTypeNames))
+        return false;
+
+    unsigned innerTypeIndex = WTF_ARRAY_LENGTH(m_textureInnerTypeNames);
+    unsigned vectorLength;
+    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(m_textureInnerTypeNames); ++i) {
+        if (innerType.name().startsWith(m_textureInnerTypeNames[i])) {
+            textureTypeIndex = i;
+            if (innerType.name() == m_textureInnerTypeNames[i])
+                vectorLength = 1;
+            else {
+                ASSERT(innerType.name().length() == strlen(m_textureInnerTypeNames[i]) + 1);
+                ASSERT(innerType.name()[innerType.name().length() - 1] == '2'
+                    || innerType.name()[innerType.name().length() - 1] == '3'
+                    || innerType.name()[innerType.name().length() - 1] == '4');
+                vectorLength = innerType.name()[innerType.name().length() - 1] - '0';
+            }
+        }
+    }
+    ASSERT(innerTypeIndex != WTF_ARRAY_LENGTH(m_textureInnerTypeNames));
+    nativeTypeDeclaration.setIsTexture();
+    m_fullTextures[textureTypeIndex][innerTypeIndex][vectorLength - 1] = &nativeTypeDeclaration;
+    return true;
+}
+
+bool Intrinsics::addDepthTexture(AST::NativeTypeDeclaration& nativeTypeDeclaration, AST::TypeReference& innerType)
+{
+    AST::NativeTypeDeclaration** texture;
+    if (nativeTypeDeclaration.name() == "TextureDepth2D")
+        texture = m_textureDepth2D;
+    else if (nativeTypeDeclaration.name() == "RWTextureDepth2D")
+        texture = m_rwTextureDepth2D;
+    else if (nativeTypeDeclaration.name() == "TextureDepth2DArray")
+        texture = m_textureDepth2DArray;
+    else if (nativeTypeDeclaration.name() == "RWTextureDepth2DArray")
+        texture = m_rwTextureDepth2DArray;
+    else if (nativeTypeDeclaration.name() == "TextureDepthCube")
+        texture = m_textureDepthCube;
+    else
+        ASSERT_NOT_REACHED();
+    unsigned innerTypeIndex = WTF_ARRAY_LENGTH(m_depthTextureInnerTypes);
+    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(m_depthTextureInnerTypes); ++i) {
+        if (innerType.name() == m_depthTextureInnerTypes[i])
+            innerTypeIndex = i;
+    }
+    ASSERT(innerTypeIndex != WTF_ARRAY_LENGTH(m_depthTextureInnerTypes));
+    nativeTypeDeclaration.setIsTexture();
+    texture[innerTypeIndex] = &nativeTypeDeclaration;
+    return true;
+}
+
+void Intrinsics::addTexture(AST::NativeTypeDeclaration& nativeTypeDeclaration)
+{
+    ASSERT(nativeTypeDeclaration.typeArguments().size() == 1);
+    ASSERT(WTF::holds_alternative<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
+    auto& innerType = static_cast<AST::TypeReference&>(WTF::get<UniqueRef<AST::TypeReference>>(nativeTypeDeclaration.typeArguments()[0]));
+    ASSERT(!innerType.typeArguments().size());
+    if (addFullTexture(nativeTypeDeclaration, innerType)) {
+        m_textureSet.add(&nativeTypeDeclaration);
+        return;
+    }
+    if (addDepthTexture(nativeTypeDeclaration, innerType))
+        m_textureSet.add(&nativeTypeDeclaration);
+}
+
+void Intrinsics::add(AST::NativeTypeDeclaration& nativeTypeDeclaration)
+{
+    if (addPrimitive(nativeTypeDeclaration))
+        return;
+    if (addVector(nativeTypeDeclaration))
+        return;
+    if (addMatrix(nativeTypeDeclaration))
+        return;
+    addTexture(nativeTypeDeclaration);
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLIntrinsics.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLIntrinsics.h
new file mode 100644 (file)
index 0000000..eb2c045
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * 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)
+
+#include "WHLSLNativeFunctionDeclaration.h"
+#include "WHLSLNativeTypeDeclaration.h"
+#include <cstring>
+#include <wtf/HashSet.h>
+#include <wtf/StdLibExtras.h>
+#include <wtf/Vector.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class Intrinsics {
+public:
+    Intrinsics();
+
+    void add(AST::NativeFunctionDeclaration&);
+    void add(AST::NativeTypeDeclaration&);
+
+    AST::NativeTypeDeclaration& voidType() const
+    {
+        ASSERT(m_voidType);
+        return *m_voidType;
+    }
+
+    AST::NativeTypeDeclaration& boolType() const
+    {
+        ASSERT(m_boolType);
+        return *m_boolType;
+    }
+
+    AST::NativeTypeDeclaration& intType() const
+    {
+        ASSERT(m_intType);
+        return *m_intType;
+    }
+
+    AST::NativeTypeDeclaration& uintType() const
+    {
+        ASSERT(m_uintType);
+        return *m_uintType;
+    }
+
+    AST::NativeTypeDeclaration& samplerType() const
+    {
+        ASSERT(m_samplerType);
+        return *m_samplerType;
+    }
+
+    AST::NativeTypeDeclaration& floatType() const
+    {
+        ASSERT(m_floatType);
+        return *m_floatType;
+    }
+
+    AST::NativeTypeDeclaration& float3Type() const
+    {
+        ASSERT(m_vectorFloat[1]);
+        return *m_vectorFloat[1];
+    }
+
+    AST::NativeTypeDeclaration& float4Type() const
+    {
+        ASSERT(m_vectorFloat[2]);
+        return *m_vectorFloat[2];
+    }
+
+private:
+    bool addPrimitive(AST::NativeTypeDeclaration&);
+    bool addVector(AST::NativeTypeDeclaration&);
+    bool addMatrix(AST::NativeTypeDeclaration&);
+    bool addFullTexture(AST::NativeTypeDeclaration&, AST::TypeReference&);
+    bool addDepthTexture(AST::NativeTypeDeclaration&, AST::TypeReference&);
+    void addTexture(AST::NativeTypeDeclaration&);
+
+    HashSet<const AST::NativeTypeDeclaration*> m_textureSet;
+
+    AST::NativeTypeDeclaration* m_voidType;
+    AST::NativeTypeDeclaration* m_boolType;
+    AST::NativeTypeDeclaration* m_ucharType;
+    AST::NativeTypeDeclaration* m_ushortType;
+    AST::NativeTypeDeclaration* m_uintType;
+    AST::NativeTypeDeclaration* m_charType;
+    AST::NativeTypeDeclaration* m_shortType;
+    AST::NativeTypeDeclaration* m_intType;
+    AST::NativeTypeDeclaration* m_halfType;
+    AST::NativeTypeDeclaration* m_floatType;
+    AST::NativeTypeDeclaration* m_atomicIntType;
+    AST::NativeTypeDeclaration* m_atomicUintType;
+    AST::NativeTypeDeclaration* m_samplerType;
+
+    AST::NativeTypeDeclaration* m_vectorBool[3];
+    AST::NativeTypeDeclaration* m_vectorUchar[3];
+    AST::NativeTypeDeclaration* m_vectorUshort[3];
+    AST::NativeTypeDeclaration* m_vectorUint[3];
+    AST::NativeTypeDeclaration* m_vectorChar[3];
+    AST::NativeTypeDeclaration* m_vectorShort[3];
+    AST::NativeTypeDeclaration* m_vectorInt[3];
+    AST::NativeTypeDeclaration* m_vectorHalf[3];
+    AST::NativeTypeDeclaration* m_vectorFloat[3];
+
+    AST::NativeTypeDeclaration* m_matrixHalf[3][3];
+    AST::NativeTypeDeclaration* m_matrixFloat[3][3];
+
+    static constexpr const char* m_textureTypeNames[] = { "Texture1D", "RWTexture1D", "Texture1DArray", "RWTexture1DArray", "Texture2D", "RWTexture2D", "Texture2DArray", "RWTexture2DArray", "Texture3D", "RWTexture3D", "TextureCube" };
+
+    static constexpr const char* m_textureInnerTypeNames[] = { "uchar", "ushort",  "uint", "char", "short", "int", "half", "float" };
+
+    AST::NativeTypeDeclaration* m_fullTextures[WTF_ARRAY_LENGTH(m_textureTypeNames)][WTF_ARRAY_LENGTH(m_textureInnerTypeNames)][4];
+
+    static constexpr const char* m_depthTextureInnerTypes[] =  { "half", "float" };
+
+    AST::NativeTypeDeclaration* m_textureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
+    AST::NativeTypeDeclaration* m_rwTextureDepth2D[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
+    AST::NativeTypeDeclaration* m_textureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
+    AST::NativeTypeDeclaration* m_rwTextureDepth2DArray[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
+    AST::NativeTypeDeclaration* m_textureDepthCube[WTF_ARRAY_LENGTH(m_depthTextureInnerTypes)];
+};
+
+}
+
+}
+
+#endif
index 6bf18e6..48fa930 100644 (file)
@@ -197,7 +197,7 @@ void NameResolver::visit(AST::CallExpression& callExpression)
         else {
             if (auto* types = m_nameContext.getTypes(callExpression.name())) {
                 if (types->size() == 1) {
-                    if (auto* functions = m_nameContext.getFunctions(String("operator cast", String::ConstructFromLiteral))) {
+                    if (auto* functions = m_nameContext.getFunctions("operator cast"_str)) {
                         callExpression.setCastData((*types)[0].get());
                         callExpression.setOverloads(*functions);
                     }
index 0dcf3f5..c93a767 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "WHLSLEnumerationDefinition.h"
 #include "WHLSLFunctionDefinition.h"
+#include "WHLSLIntrinsics.h"
 #include "WHLSLNameContext.h"
 #include "WHLSLNativeFunctionDeclaration.h"
 #include "WHLSLNativeTypeDeclaration.h"
@@ -72,16 +73,19 @@ public:
     bool append(AST::NativeFunctionDeclaration&& nativeFunctionDeclaration)
     {
         m_nativeFunctionDeclarations.append(makeUniqueRef<AST::NativeFunctionDeclaration>(WTFMove(nativeFunctionDeclaration)));
+        m_intrinsics.add(static_cast<AST::NativeFunctionDeclaration&>(m_nativeFunctionDeclarations.last()));
         return m_nameContext.add(static_cast<AST::NativeFunctionDeclaration&>(m_nativeFunctionDeclarations.last()));
     }
 
     bool append(AST::NativeTypeDeclaration&& nativeTypeDeclaration)
     {
         m_nativeTypeDeclarations.append(makeUniqueRef<AST::NativeTypeDeclaration>(WTFMove(nativeTypeDeclaration)));
+        m_intrinsics.add(static_cast<AST::NativeTypeDeclaration&>(m_nativeTypeDeclarations.last()));
         return m_nameContext.add(static_cast<AST::NativeTypeDeclaration&>(m_nativeTypeDeclarations.last()));
     }
 
     NameContext& nameContext() { return m_nameContext; }
+    Intrinsics& intrinsics() { return m_intrinsics; }
     Vector<UniqueRef<AST::TypeDefinition>>& typeDefinitions() { return m_typeDefinitions; }
     Vector<UniqueRef<AST::StructureDefinition>>& structureDefinitions() { return m_structureDefinitions; }
     Vector<UniqueRef<AST::EnumerationDefinition>>& enumerationDefinitions() { return m_enumerationDefinitions; }
@@ -93,6 +97,7 @@ public:
 
 private:
     NameContext m_nameContext;
+    Intrinsics m_intrinsics;
     Vector<UniqueRef<AST::TypeDefinition>> m_typeDefinitions;
     Vector<UniqueRef<AST::StructureDefinition>> m_structureDefinitions;
     Vector<UniqueRef<AST::EnumerationDefinition>> m_enumerationDefinitions;
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp
new file mode 100644 (file)
index 0000000..d822f07
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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 "WHLSLRecursiveTypeChecker.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLVisitor.h"
+#include <wtf/HashSet.h>
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class RecursiveTypeChecker : public Visitor {
+public:
+    ~RecursiveTypeChecker() = default;
+
+    void visit(AST::TypeDefinition& typeDefinition) override
+    {
+        auto addResult = m_types.add(&typeDefinition);
+        if (!addResult.isNewEntry) {
+            setError();
+            return;
+        }
+
+        Visitor::visit(typeDefinition);
+
+        auto success = m_types.remove(&typeDefinition);
+        ASSERT_UNUSED(success, success);
+    }
+
+    void visit(AST::StructureDefinition& structureDefinition) override
+    {
+        auto addResult = m_types.add(&structureDefinition);
+        if (!addResult.isNewEntry) {
+            setError();
+            return;
+        }
+
+        Visitor::visit(structureDefinition);
+
+        auto success = m_types.remove(&structureDefinition);
+        ASSERT_UNUSED(success, success);
+    }
+
+    void visit(AST::TypeReference& typeReference) override
+    {
+        auto addResult = m_types.add(&typeReference);
+        if (!addResult.isNewEntry) {
+            setError();
+            return;
+        }
+
+        for (auto& typeArgument : typeReference.typeArguments())
+            checkErrorAndVisit(typeArgument);
+        checkErrorAndVisit(*typeReference.resolvedType());
+
+        auto success = m_types.remove(&typeReference);
+        ASSERT_UNUSED(success, success);
+    }
+
+    void visit(AST::ReferenceType&) override
+    {
+    }
+
+private:
+    HashSet<AST::Type*> m_types;
+};
+
+bool checkRecursiveTypes(Program& program)
+{
+    RecursiveTypeChecker recursiveTypeChecker;
+    recursiveTypeChecker.checkErrorAndVisit(program);
+    return recursiveTypeChecker.error();
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.h
new file mode 100644 (file)
index 0000000..fc970c7
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+bool checkRecursiveTypes(Program&);
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.cpp
new file mode 100644 (file)
index 0000000..9d620fa
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * 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 "WHLSLSynthesizeArrayOperatorLength.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLArrayType.h"
+#include "WHLSLProgram.h"
+#include "WHLSLTypeReference.h"
+#include "WHLSLVisitor.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class FindArrayTypes : public Visitor {
+public:
+    ~FindArrayTypes() = default;
+
+    void visit(AST::ArrayType& arrayType) override
+    {
+        m_arrayTypes.append(arrayType);
+        checkErrorAndVisit(arrayType);
+    }
+
+    Vector<std::reference_wrapper<AST::ArrayType>>&& takeArrayTypes()
+    {
+        return WTFMove(m_arrayTypes);
+    }
+
+private:
+    Vector<std::reference_wrapper<AST::ArrayType>> m_arrayTypes;
+};
+
+void synthesizeArrayOperatorLength(Program& program)
+{
+    FindArrayTypes findArrayTypes;
+    findArrayTypes.checkErrorAndVisit(program);
+    auto arrayTypes = findArrayTypes.takeArrayTypes();
+
+    bool isOperator = true;
+    bool isRestricted = false;
+
+    for (auto& arrayType : arrayTypes) {
+        AST::VariableDeclaration variableDeclaration(Lexer::Token(arrayType.get().origin()), AST::Qualifiers(), { arrayType.get().clone() }, String(), WTF::nullopt, WTF::nullopt);
+        AST::VariableDeclarations parameters;
+        parameters.append(WTFMove(variableDeclaration));
+        AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(arrayType.get().origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(arrayType.get().origin()), program.intrinsics().uintType()), "operator.length"_str, WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+        program.append(WTFMove(nativeFunctionDeclaration));
+    }
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.h
new file mode 100644 (file)
index 0000000..2012f2b
--- /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 synthesizeArrayOperatorLength(Program&);
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp
new file mode 100644 (file)
index 0000000..85e2b88
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * 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 "WHLSLSynthesizeConstructors.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLNativeFunctionDeclaration.h"
+#include "WHLSLProgram.h"
+#include "WHLSLTypeReference.h"
+#include "WHLSLVariableDeclaration.h"
+#include "WHLSLVisitor.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+class FindAllTypes : public Visitor {
+public:
+    ~FindAllTypes() = default;
+
+    void visit(AST::PointerType& pointerType) override
+    {
+        m_unnamedTypes.append(pointerType);
+        checkErrorAndVisit(pointerType);
+    }
+
+    void visit(AST::ArrayReferenceType& arrayReferenceType) override
+    {
+        m_unnamedTypes.append(arrayReferenceType);
+        checkErrorAndVisit(arrayReferenceType);
+    }
+
+    void visit(AST::ArrayType& arrayType) override
+    {
+        m_unnamedTypes.append(arrayType);
+        checkErrorAndVisit(arrayType);
+    }
+
+    void visit(AST::EnumerationDefinition& enumerationDefinition) override
+    {
+        m_namedTypes.append(enumerationDefinition);
+        checkErrorAndVisit(enumerationDefinition);
+    }
+
+    void visit(AST::StructureDefinition& structureDefinition) override
+    {
+        m_namedTypes.append(structureDefinition);
+        checkErrorAndVisit(structureDefinition);
+    }
+
+    void visit(AST::NativeTypeDeclaration& nativeTypeDeclaration) override
+    {
+        m_namedTypes.append(nativeTypeDeclaration);
+        checkErrorAndVisit(nativeTypeDeclaration);
+    }
+
+    Vector<std::reference_wrapper<AST::UnnamedType>>&& takeUnnamedTypes()
+    {
+        return WTFMove(m_unnamedTypes);
+    }
+
+    Vector<std::reference_wrapper<AST::NamedType>>&& takeNamedTypes()
+    {
+        return WTFMove(m_namedTypes);
+    }
+
+private:
+    Vector<std::reference_wrapper<AST::UnnamedType>> m_unnamedTypes;
+    Vector<std::reference_wrapper<AST::NamedType>> m_namedTypes;
+};
+
+void synthesizeConstructors(Program& program)
+{
+    FindAllTypes findAllTypes;
+    findAllTypes.checkErrorAndVisit(program);
+    auto m_unnamedTypes = findAllTypes.takeUnnamedTypes();
+    auto m_namedTypes = findAllTypes.takeNamedTypes();
+
+    bool isOperator = true;
+    bool isRestricted = false;
+
+    for (auto& unnamedType : m_unnamedTypes) {
+        AST::VariableDeclaration variableDeclaration(Lexer::Token(unnamedType.get().origin()), AST::Qualifiers(), { unnamedType.get().clone() }, String(), WTF::nullopt, WTF::nullopt);
+        AST::VariableDeclarations parameters;
+        parameters.append(WTFMove(variableDeclaration));
+        AST::NativeFunctionDeclaration copyConstructor(AST::FunctionDeclaration(Lexer::Token(unnamedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, unnamedType.get().clone(), "operator cast"_str, WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+        program.append(WTFMove(copyConstructor));
+
+        AST::NativeFunctionDeclaration defaultConstructor(AST::FunctionDeclaration(Lexer::Token(unnamedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, unnamedType.get().clone(), "operator cast"_str, AST::VariableDeclarations(), WTF::nullopt, isOperator), isRestricted);
+        program.append(WTFMove(defaultConstructor));
+    }
+
+    for (auto& namedType : m_namedTypes) {
+        AST::VariableDeclaration variableDeclaration(Lexer::Token(namedType.get().origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(namedType.get().origin()), namedType.get()) }, String(), WTF::nullopt, WTF::nullopt);
+        AST::VariableDeclarations parameters;
+        parameters.append(WTFMove(variableDeclaration));
+        AST::NativeFunctionDeclaration copyConstructor(AST::FunctionDeclaration(Lexer::Token(namedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(namedType.get().origin()), namedType.get()), "operator cast"_str, WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+        program.append(WTFMove(copyConstructor));
+
+        AST::NativeFunctionDeclaration defaultConstructor(AST::FunctionDeclaration(Lexer::Token(namedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(namedType.get().origin()), namedType.get()), "operator cast"_str, AST::VariableDeclarations(), WTF::nullopt, isOperator), isRestricted);
+        program.append(WTFMove(defaultConstructor));
+    }
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.h
new file mode 100644 (file)
index 0000000..19a05be
--- /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 synthesizeConstructors(Program&);
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.cpp
new file mode 100644 (file)
index 0000000..63fefb4
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * 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 "WHLSLSynthesizeEnumerationFunctions.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLProgram.h"
+#include "WHLSLTypeReference.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+void synthesizeEnumerationFunctions(Program& program)
+{
+    bool isOperator = true;
+    bool isRestricted = false;
+    for (auto& enumerationDefinition : program.enumerationDefinitions()) {
+        {
+            AST::VariableDeclaration variableDeclaration1(Lexer::Token(enumerationDefinition->origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(enumerationDefinition->origin()), static_cast<AST::EnumerationDefinition&>(enumerationDefinition)) }, String(), WTF::nullopt, WTF::nullopt);
+            AST::VariableDeclaration variableDeclaration2(Lexer::Token(enumerationDefinition->origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(enumerationDefinition->origin()), static_cast<AST::EnumerationDefinition&>(enumerationDefinition)) }, String(), WTF::nullopt, WTF::nullopt);
+            AST::VariableDeclarations parameters;
+            parameters.append(WTFMove(variableDeclaration1));
+            parameters.append(WTFMove(variableDeclaration2));
+            AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(enumerationDefinition->origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(enumerationDefinition->origin()), program.intrinsics().boolType()), "operator=="_str, WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+            program.append(WTFMove(nativeFunctionDeclaration));
+        }
+
+        if (enumerationDefinition->type()) {
+            // FIXME: Handle a null base type.
+            {
+                AST::VariableDeclaration variableDeclaration(Lexer::Token(enumerationDefinition->origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(enumerationDefinition->origin()), static_cast<AST::EnumerationDefinition&>(enumerationDefinition)) }, String(), WTF::nullopt, WTF::nullopt);
+                AST::VariableDeclarations parameters;
+                parameters.append(WTFMove(variableDeclaration));
+                AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(enumerationDefinition->origin()), AST::AttributeBlock(), WTF::nullopt, enumerationDefinition->type()->clone(), "operator.value"_str, WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+                program.append(WTFMove(nativeFunctionDeclaration));
+            }
+
+            {
+                AST::VariableDeclaration variableDeclaration(Lexer::Token(enumerationDefinition->origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(enumerationDefinition->origin()), static_cast<AST::EnumerationDefinition&>(enumerationDefinition)) }, String(), WTF::nullopt, WTF::nullopt);
+                AST::VariableDeclarations parameters;
+                parameters.append(WTFMove(variableDeclaration));
+                AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(enumerationDefinition->origin()), AST::AttributeBlock(), WTF::nullopt, enumerationDefinition->type()->clone(), "operator cast"_str, WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+                program.append(WTFMove(nativeFunctionDeclaration));
+            }
+
+            {
+                AST::VariableDeclaration variableDeclaration(Lexer::Token(enumerationDefinition->origin()), AST::Qualifiers(), enumerationDefinition->type()->clone(), String(), WTF::nullopt, WTF::nullopt);
+                AST::VariableDeclarations parameters;
+                parameters.append(WTFMove(variableDeclaration));
+                AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(enumerationDefinition->origin()), AST::AttributeBlock(), WTF::nullopt, { AST::TypeReference::wrap(Lexer::Token(enumerationDefinition->origin()), static_cast<AST::EnumerationDefinition&>(enumerationDefinition)) }, "operator cast"_str, WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+                program.append(WTFMove(nativeFunctionDeclaration));
+            }
+        }
+    }
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.h
new file mode 100644 (file)
index 0000000..2127fd1
--- /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 synthesizeEnumerationFunctions(Program&);
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.cpp b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.cpp
new file mode 100644 (file)
index 0000000..b3422dd
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * 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 "WHLSLSynthesizeStructureAccessors.h"
+
+#if ENABLE(WEBGPU)
+
+#include "WHLSLPointerType.h"
+#include "WHLSLProgram.h"
+#include "WHLSLReferenceType.h"
+#include "WHLSLTypeReference.h"
+#include "WHLSLVariableDeclaration.h"
+
+namespace WebCore {
+
+namespace WHLSL {
+
+void synthesizeStructureAccessors(Program& program)
+{
+    bool isOperator = true;
+    bool isRestricted = false;
+    for (auto& structureDefinition : program.structureDefinitions()) {
+        for (auto& structureElement : structureDefinition->structureElements()) {
+            {
+                // The getter: operator.field
+                AST::VariableDeclaration variableDeclaration(Lexer::Token(structureElement.origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(structureElement.origin()), static_cast<AST::StructureDefinition&>(structureDefinition)) }, String(), WTF::nullopt, WTF::nullopt);
+                AST::VariableDeclarations parameters;
+                parameters.append(WTFMove(variableDeclaration));
+                AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(structureElement.origin()), AST::AttributeBlock(), WTF::nullopt, structureElement.type().clone(), String::format("operator.%s", structureElement.name().utf8().data()), WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+                program.append(WTFMove(nativeFunctionDeclaration));
+            }
+
+            {
+                // The setter: operator.field=
+                AST::VariableDeclaration variableDeclaration1(Lexer::Token(structureElement.origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(structureElement.origin()), static_cast<AST::StructureDefinition&>(structureDefinition)) }, String(), WTF::nullopt, WTF::nullopt);
+                AST::VariableDeclaration variableDeclaration2(Lexer::Token(structureElement.origin()), AST::Qualifiers(), { structureElement.type().clone() }, String(), WTF::nullopt, WTF::nullopt);
+                AST::VariableDeclarations parameters;
+                parameters.append(WTFMove(variableDeclaration1));
+                parameters.append(WTFMove(variableDeclaration2));
+                AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(structureElement.origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(structureElement.origin()), static_cast<AST::StructureDefinition&>(structureDefinition)), String::format("operator.%s=", structureElement.name().utf8().data()), WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+                program.append(WTFMove(nativeFunctionDeclaration));
+            }
+
+            // The ander: operator&.field
+            auto createAnder = [&](AST::ReferenceType::AddressSpace addressSpace) -> AST::NativeFunctionDeclaration {
+                auto argumentType = makeUniqueRef<AST::PointerType>(Lexer::Token(structureElement.origin()), addressSpace, AST::TypeReference::wrap(Lexer::Token(structureElement.origin()), static_cast<AST::StructureDefinition&>(structureDefinition)));
+                AST::VariableDeclaration variableDeclaration(Lexer::Token(structureElement.origin()), AST::Qualifiers(), { WTFMove(argumentType) }, String(), WTF::nullopt, WTF::nullopt);
+                AST::VariableDeclarations parameters;
+                parameters.append(WTFMove(variableDeclaration));
+                auto returnType = makeUniqueRef<AST::PointerType>(Lexer::Token(structureElement.origin()), addressSpace, structureElement.type().clone());
+                AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(structureElement.origin()), AST::AttributeBlock(), WTF::nullopt, WTFMove(returnType), String::format("operator&.%s", structureElement.name().utf8().data()), WTFMove(parameters), WTF::nullopt, isOperator), isRestricted);
+                return nativeFunctionDeclaration;
+            };
+            program.append(createAnder(AST::ReferenceType::AddressSpace::Constant));
+            program.append(createAnder(AST::ReferenceType::AddressSpace::Device));
+            program.append(createAnder(AST::ReferenceType::AddressSpace::Threadgroup));
+            program.append(createAnder(AST::ReferenceType::AddressSpace::Thread));
+        }
+    }
+}
+
+}
+
+}
+
+#endif
diff --git a/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.h b/Source/WebCore/Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.h
new file mode 100644 (file)
index 0000000..bb9f2b9
--- /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 synthesizeStructureAccessors(Program&);
+
+}
+
+}
+
+#endif
index 06f32d1..0d25635 100644 (file)
@@ -309,6 +309,13 @@ Modules/webgpu/DOMWindowWebGPU.cpp
 Modules/webgpu/WHLSL/WHLSLInferTypes.cpp
 Modules/webgpu/WHLSL/WHLSLLexer.cpp
 Modules/webgpu/WHLSL/WHLSLParser.cpp
+Modules/webgpu/WHLSL/WHLSLCheckDuplicateFunctions.cpp
+Modules/webgpu/WHLSL/WHLSLRecursiveTypeChecker.cpp
+Modules/webgpu/WHLSL/WHLSLSynthesizeArrayOperatorLength.cpp
+Modules/webgpu/WHLSL/WHLSLSynthesizeConstructors.cpp
+Modules/webgpu/WHLSL/WHLSLSynthesizeEnumerationFunctions.cpp
+Modules/webgpu/WHLSL/WHLSLSynthesizeStructureAccessors.cpp
+Modules/webgpu/WHLSL/WHLSLIntrinsics.cpp
 Modules/webgpu/WHLSL/WHLSLNameContext.cpp
 Modules/webgpu/WHLSL/WHLSLNameResolver.cpp
 Modules/webgpu/WHLSL/WHLSLResolveOverloadImpl.cpp
index 7a9e1df..6493844 100644 (file)
                C234A99A21E90F56003C984D /* WHLSLInferTypes.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLInferTypes.cpp; sourceTree = "<group>"; };
                C234A99B21E90F57003C984D /* WHLSLInferTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLInferTypes.h; sourceTree = "<group>"; };
                C234A99D21E910BD003C984D /* WHLSLResolvingType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLResolvingType.h; sourceTree = "<group>"; };
+               C234A9A921E92C17003C984D /* WHLSLSynthesizeArrayOperatorLength.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLSynthesizeArrayOperatorLength.cpp; sourceTree = "<group>"; };
+               C234A9AB21E92C18003C984D /* WHLSLRecursiveTypeChecker.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLRecursiveTypeChecker.h; sourceTree = "<group>"; };
+               C234A9AC21E92C19003C984D /* WHLSLSynthesizeEnumerationFunctions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLSynthesizeEnumerationFunctions.h; sourceTree = "<group>"; };
+               C234A9AD21E92C19003C984D /* WHLSLRecursiveTypeChecker.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLRecursiveTypeChecker.cpp; sourceTree = "<group>"; };
+               C234A9AE21E92C1A003C984D /* WHLSLCheckDuplicateFunctions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLCheckDuplicateFunctions.h; sourceTree = "<group>"; };
+               C234A9AF21E92C1B003C984D /* WHLSLSynthesizeArrayOperatorLength.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLSynthesizeArrayOperatorLength.h; sourceTree = "<group>"; };
+               C234A9B021E92C1C003C984D /* WHLSLSynthesizeConstructors.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLSynthesizeConstructors.cpp; sourceTree = "<group>"; };
+               C234A9B121E92C1D003C984D /* WHLSLSynthesizeConstructors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLSynthesizeConstructors.h; sourceTree = "<group>"; };
+               C234A9B221E92C1F003C984D /* WHLSLCheckDuplicateFunctions.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLCheckDuplicateFunctions.cpp; sourceTree = "<group>"; };
+               C234A9B321E92C21003C984D /* WHLSLSynthesizeEnumerationFunctions.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLSynthesizeEnumerationFunctions.cpp; sourceTree = "<group>"; };
+               C234A9B421E92C22003C984D /* WHLSLSynthesizeStructureAccessors.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLSynthesizeStructureAccessors.cpp; sourceTree = "<group>"; };
+               C234A9B521E92C23003C984D /* WHLSLSynthesizeStructureAccessors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLSynthesizeStructureAccessors.h; sourceTree = "<group>"; };
+               C234A9B621E92CC0003C984D /* WHLSLIntrinsics.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WHLSLIntrinsics.h; sourceTree = "<group>"; };
+               C234A9B721E92CC1003C984D /* WHLSLIntrinsics.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLIntrinsics.cpp; sourceTree = "<group>"; };
                C21DF2E71D9E4E9900F5B24C /* CSSFontVariationValue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CSSFontVariationValue.cpp; sourceTree = "<group>"; };
                C21DF2E81D9E4E9900F5B24C /* CSSFontVariationValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSFontVariationValue.h; sourceTree = "<group>"; };
                C234A98521E886A9003C984D /* WHLSLVisitor.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLVisitor.cpp; sourceTree = "<group>"; };
                        isa = PBXGroup;
                        children = (
                                C21BF6F121CD898D00227979 /* AST */,
+                               C234A9B221E92C1F003C984D /* WHLSLCheckDuplicateFunctions.cpp */,
+                               C234A9AE21E92C1A003C984D /* WHLSLCheckDuplicateFunctions.h */,
                                C234A99A21E90F56003C984D /* WHLSLInferTypes.cpp */,
                                C234A99B21E90F57003C984D /* WHLSLInferTypes.h */,
+                               C234A9B721E92CC1003C984D /* WHLSLIntrinsics.cpp */,
+                               C234A9B621E92CC0003C984D /* WHLSLIntrinsics.h */,
                                C210E91121B4BD1000B7F83D /* WHLSLLexer.cpp */,
                                C210E91221B4BD1000B7F83D /* WHLSLLexer.h */,
                                C234A98D21E88884003C984D /* WHLSLNameContext.cpp */,
                                C21BF73721CD8A0200227979 /* WHLSLParser.cpp */,
                                C21BF73821CD8A0300227979 /* WHLSLParser.h */,
                                C21BF73A21CD8D7000227979 /* WHLSLProgram.h */,
+                               C234A9AD21E92C19003C984D /* WHLSLRecursiveTypeChecker.cpp */,
+                               C234A9AB21E92C18003C984D /* WHLSLRecursiveTypeChecker.h */,
                                C234A99921E90F29003C984D /* WHLSLResolveOverloadImpl.cpp */,
                                C234A99721E90F28003C984D /* WHLSLResolveOverloadImpl.h */,
                                C234A99D21E910BD003C984D /* WHLSLResolvingType.h */,
                                C21BF74521CD969800227979 /* WHLSLStandardLibrary.txt */,
+                               C234A9A921E92C17003C984D /* WHLSLSynthesizeArrayOperatorLength.cpp */,
+                               C234A9AF21E92C1B003C984D /* WHLSLSynthesizeArrayOperatorLength.h */,
+                               C234A9B021E92C1C003C984D /* WHLSLSynthesizeConstructors.cpp */,
+                               C234A9B121E92C1D003C984D /* WHLSLSynthesizeConstructors.h */,
+                               C234A9B321E92C21003C984D /* WHLSLSynthesizeEnumerationFunctions.cpp */,
+                               C234A9AC21E92C19003C984D /* WHLSLSynthesizeEnumerationFunctions.h */,
+                               C234A9B421E92C22003C984D /* WHLSLSynthesizeStructureAccessors.cpp */,
+                               C234A9B521E92C23003C984D /* WHLSLSynthesizeStructureAccessors.h */,
                                C234A98521E886A9003C984D /* WHLSLVisitor.cpp */,
                                C234A98721E886AD003C984D /* WHLSLVisitor.h */,
                        );