Title: [221549] trunk/Tools
Revision
221549
Author
[email protected]
Date
2017-09-03 10:43:25 -0700 (Sun, 03 Sep 2017)

Log Message

WSL NullType should be a special type variable so it can unify with any pointer type
https://bugs.webkit.org/show_bug.cgi?id=176235

Reviewed by Myles Maxfield.
        
Null is just a type variable that plays by weird rules:
        
- Other type variables don't recognize it, so if it's unified with T, it fails.
- It behaves like a type variable for pointer and array reference types.
- It arises only in argument (RHS) position.
- Once Checker finds it to be equals to something, we need to remember what that was.
        
I refactored some of the type system code to allow for the possibility of type variables being
unified during equals() tests. It turns out that we were playing fast and loose here; I needed
to beef up UnificationContext.prototype.verify. I added tests for this.
        
I added an equalsWithCommit() that triggers commitUnification(), which allows NullType to
remember what type it got unified with.
        
NullLiteral then just instantiates its own NullType, which is what Checker uses as the type of
the NullLiteral. Everything else just works.
        
This adds a bunch of tests to make sure we generate type errors in various weird cases while
also accepting a number of safe cases.
        
I also added a way to run the tests in-browser.

* WebGPUShadingLanguageRI/All.js:
* WebGPUShadingLanguageRI/ArrayRefType.js:
(ArrayRefType.prototype.get isArrayRef):
* WebGPUShadingLanguageRI/Checker.js:
(Checker.prototype.visitProtocolDecl.set throw):
* WebGPUShadingLanguageRI/ConstexprTypeParameter.js:
(ConstexprTypeParameter.prototype.typeVariableUnify):
* WebGPUShadingLanguageRI/Evaluator.js:
(Evaluator.prototype.visitDereferenceExpression):
(Evaluator.prototype.visitNullLiteral):
* WebGPUShadingLanguageRI/Func.js:
(Func.prototype.toString):
(Func):
* WebGPUShadingLanguageRI/Intrinsics.js:
(Intrinsics):
* WebGPUShadingLanguageRI/NativeFunc.js:
(NativeFunc.prototype.toDeclString):
(NativeFunc):
(NativeFunc.prototype.toString): Deleted.
* WebGPUShadingLanguageRI/NativeFuncInstance.js:
(NativeFuncInstance.prototype.toDeclString):
(NativeFuncInstance):
(NativeFuncInstance.prototype.toString): Deleted.
* WebGPUShadingLanguageRI/Node.js:
(Node.unify):
(Node.prototype._typeVariableUnifyImpl):
(Node.prototype.equals):
* WebGPUShadingLanguageRI/NullLiteral.js: Added.
(NullLiteral):
(NullLiteral.prototype.toString):
* WebGPUShadingLanguageRI/NullType.js:
(NullType):
(NullType.prototype.get isPrimitive):
(NullType.prototype.get isUnifiable):
(NullType.prototype.typeVariableUnify):
(NullType.prototype.unifyImpl):
(NullType.prototype.verifyAsArgument):
(NullType.prototype.verifyAsParameter):
(NullType.prototype.toString):
* WebGPUShadingLanguageRI/Parse.js:
(parseTerm):
(parseType.getAddressSpace):
(parseType):
* WebGPUShadingLanguageRI/Rewriter.js:
(Rewriter.prototype.visitNullLiteral):
* WebGPUShadingLanguageRI/Test.html: Added.
* WebGPUShadingLanguageRI/Test.js:
(this.window.this.print):
(TEST_doubleGenericCallsSingleGeneric):
(TEST_dereferenceDefaultNull):
(TEST_dereferenceDefaultNullArrayRef):
(doTest):
(let.s.in.this.s.startsWith.string_appeared_here.s.match): Deleted.
* WebGPUShadingLanguageRI/Type.js:
(Type.prototype.get isArrayRef):
* WebGPUShadingLanguageRI/TypeVariable.js:
(TypeVariable.prototype.typeVariableUnify):
* WebGPUShadingLanguageRI/UnificationContext.js:
(UnificationContext.prototype.verify):
(UnificationContext):
(UnificationContext.prototype.isUnunified): Deleted.
* WebGPUShadingLanguageRI/VariableRef.js:
(VariableRef.prototype.get unifyNode):
* WebGPUShadingLanguageRI/Visitor.js:
(Visitor.prototype.visitNullLiteral):

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (221548 => 221549)


--- trunk/Tools/ChangeLog	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/ChangeLog	2017-09-03 17:43:25 UTC (rev 221549)
@@ -1,3 +1,98 @@
+2017-09-02  Filip Pizlo  <[email protected]>
+
+        WSL NullType should be a special type variable so it can unify with any pointer type
+        https://bugs.webkit.org/show_bug.cgi?id=176235
+
+        Reviewed by Myles Maxfield.
+        
+        Null is just a type variable that plays by weird rules:
+        
+        - Other type variables don't recognize it, so if it's unified with T, it fails.
+        - It behaves like a type variable for pointer and array reference types.
+        - It arises only in argument (RHS) position.
+        - Once Checker finds it to be equals to something, we need to remember what that was.
+        
+        I refactored some of the type system code to allow for the possibility of type variables being
+        unified during equals() tests. It turns out that we were playing fast and loose here; I needed
+        to beef up UnificationContext.prototype.verify. I added tests for this.
+        
+        I added an equalsWithCommit() that triggers commitUnification(), which allows NullType to
+        remember what type it got unified with.
+        
+        NullLiteral then just instantiates its own NullType, which is what Checker uses as the type of
+        the NullLiteral. Everything else just works.
+        
+        This adds a bunch of tests to make sure we generate type errors in various weird cases while
+        also accepting a number of safe cases.
+        
+        I also added a way to run the tests in-browser.
+
+        * WebGPUShadingLanguageRI/All.js:
+        * WebGPUShadingLanguageRI/ArrayRefType.js:
+        (ArrayRefType.prototype.get isArrayRef):
+        * WebGPUShadingLanguageRI/Checker.js:
+        (Checker.prototype.visitProtocolDecl.set throw):
+        * WebGPUShadingLanguageRI/ConstexprTypeParameter.js:
+        (ConstexprTypeParameter.prototype.typeVariableUnify):
+        * WebGPUShadingLanguageRI/Evaluator.js:
+        (Evaluator.prototype.visitDereferenceExpression):
+        (Evaluator.prototype.visitNullLiteral):
+        * WebGPUShadingLanguageRI/Func.js:
+        (Func.prototype.toString):
+        (Func):
+        * WebGPUShadingLanguageRI/Intrinsics.js:
+        (Intrinsics):
+        * WebGPUShadingLanguageRI/NativeFunc.js:
+        (NativeFunc.prototype.toDeclString):
+        (NativeFunc):
+        (NativeFunc.prototype.toString): Deleted.
+        * WebGPUShadingLanguageRI/NativeFuncInstance.js:
+        (NativeFuncInstance.prototype.toDeclString):
+        (NativeFuncInstance):
+        (NativeFuncInstance.prototype.toString): Deleted.
+        * WebGPUShadingLanguageRI/Node.js:
+        (Node.unify):
+        (Node.prototype._typeVariableUnifyImpl):
+        (Node.prototype.equals):
+        * WebGPUShadingLanguageRI/NullLiteral.js: Added.
+        (NullLiteral):
+        (NullLiteral.prototype.toString):
+        * WebGPUShadingLanguageRI/NullType.js:
+        (NullType):
+        (NullType.prototype.get isPrimitive):
+        (NullType.prototype.get isUnifiable):
+        (NullType.prototype.typeVariableUnify):
+        (NullType.prototype.unifyImpl):
+        (NullType.prototype.verifyAsArgument):
+        (NullType.prototype.verifyAsParameter):
+        (NullType.prototype.toString):
+        * WebGPUShadingLanguageRI/Parse.js:
+        (parseTerm):
+        (parseType.getAddressSpace):
+        (parseType):
+        * WebGPUShadingLanguageRI/Rewriter.js:
+        (Rewriter.prototype.visitNullLiteral):
+        * WebGPUShadingLanguageRI/Test.html: Added.
+        * WebGPUShadingLanguageRI/Test.js:
+        (this.window.this.print):
+        (TEST_doubleGenericCallsSingleGeneric):
+        (TEST_dereferenceDefaultNull):
+        (TEST_dereferenceDefaultNullArrayRef):
+        (doTest):
+        (let.s.in.this.s.startsWith.string_appeared_here.s.match): Deleted.
+        * WebGPUShadingLanguageRI/Type.js:
+        (Type.prototype.get isArrayRef):
+        * WebGPUShadingLanguageRI/TypeVariable.js:
+        (TypeVariable.prototype.typeVariableUnify):
+        * WebGPUShadingLanguageRI/UnificationContext.js:
+        (UnificationContext.prototype.verify):
+        (UnificationContext):
+        (UnificationContext.prototype.isUnunified): Deleted.
+        * WebGPUShadingLanguageRI/VariableRef.js:
+        (VariableRef.prototype.get unifyNode):
+        * WebGPUShadingLanguageRI/Visitor.js:
+        (Visitor.prototype.visitNullLiteral):
+
 2017-09-03  Sam Weinig  <[email protected]>
 
         Remove CanvasProxy

Modified: trunk/Tools/WebGPUShadingLanguageRI/All.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/All.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/All.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -41,6 +41,7 @@
 load("CallExpression.js");
 load("CallFunction.js");
 load("Check.js");
+load("CheckLiteralTypes.js");
 load("CheckReturns.js");
 load("CheckUnreachableCode.js");
 load("Checker.js");
@@ -66,6 +67,7 @@
 load("Intrinsics.js");
 load("Lexer.js");
 load("LexerToken.js");
+load("LiteralTypeChecker.js");
 load("MakePtrExpression.js");
 load("NameContext.js");
 load("NameResolver.js");
@@ -73,6 +75,7 @@
 load("NativeFuncInstance.js");
 load("NativeType.js");
 load("NativeTypeInstance.js");
+load("NullLiteral.js");
 load("NullType.js");
 load("Parse.js");
 load("Prepare.js");

Modified: trunk/Tools/WebGPUShadingLanguageRI/ArrayRefType.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/ArrayRefType.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/ArrayRefType.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -37,6 +37,8 @@
         }
         return this.elementType.unify(unificationContext, other.elementType);
     }
+    
+    get isArrayRef() { return true; }
 
     toString()
     {

Added: trunk/Tools/WebGPUShadingLanguageRI/CheckLiteralTypes.js (0 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/CheckLiteralTypes.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/CheckLiteralTypes.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2017 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. ``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
+ * 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. 
+ */
+"use strict";
+
+function checkLiteralTypes(program)
+{
+    program.visit(new LiteralTypeChecker());
+}

Modified: trunk/Tools/WebGPUShadingLanguageRI/Checker.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Checker.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Checker.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -80,7 +80,7 @@
                 if (!typeArguments[i].inherits(typeArguments[i]))
                     throw new WTypeError(origin.originString, "Type argument does not inherit protocol");
             } else {
-                if (!result.equals(typeParameters[i].type))
+                if (!result.equalsWithCommit(typeParameters[i].type))
                     throw new WTypeError(origin.originString, "Wrong type for constexpr");
             }
         }
@@ -116,7 +116,7 @@
         if (node.initializer) {
             let lhsType = node.type;
             let rhsType = node.initializer.visit(this);
-            if (!lhsType.equals(rhsType))
+            if (!lhsType.equalsWithCommit(rhsType))
                 throw new WTypeError(node.origin.originString, "Type mismatch in variable initialization: " + lhsType + " versus " + rhsType);
         }
     }
@@ -127,7 +127,7 @@
             throw new WTypeError(node.origin.originString, "LHS of assignment is not an LValue: " + node.lhs);
         let lhsType = node.lhs.visit(this);
         let rhsType = node.rhs.visit(this);
-        if (!lhsType.equals(rhsType))
+        if (!lhsType.equalsWithCommit(rhsType))
             throw new WTypeError(node.origin.originString, "Type mismatch in assignment: " + lhsType + " versus " + rhsType);
         node.type = lhsType;
         return lhsType;
@@ -164,12 +164,12 @@
             let resultType = node.value.visit(this);
             if (!resultType)
                 throw new Error("Null result type from " + node.value);
-            if (!node.func.returnType.equals(resultType))
+            if (!node.func.returnType.equalsWithCommit(resultType))
                 throw new WTypeError(node.origin.originString, "Trying to return " + resultType + " in a function that returns " + node.func.returnType);
             return;
         }
         
-        if (!node.func.returnType.equals(this._program.intrinsics.void))
+        if (!node.func.returnType.equalsWithCommit(this._program.intrinsics.void))
             throw new WTypeError(node.origin.originString, "Non-void function must return a value");
     }
     
@@ -186,6 +186,12 @@
         return this._program.intrinsics.uint32;
     }
     
+    visitNullLiteral(node)
+    {
+        // Return the null literal's type variable.
+        return node.type;
+    }
+    
     visitCommaExpression(node)
     {
         let result = null;
@@ -231,12 +237,20 @@
                 let message = "Did not find function for call";
                 if (failures.length) {
                     let stringifyFailure =
-                        failure => failure.func + " did not match because: " + failure.reason;
+                        failure => failure.func.toDeclString() + " did not match because: " + failure.reason;
                     message += ", but considered:\n" + failures.map(stringifyFailure).join("\n")
                 }
                 throw new WTypeError(node.origin.originString, message);
             }
         }
+        for (let i = 0; i < argumentTypes.length; ++i) {
+            let argumentType = argumentTypes[i];
+            let parameterType = overload.func.parameters[i].type.substituteToUnification(
+                overload.func.typeParameters, overload.unificationContext);
+            let result = argumentType.equalsWithCommit(parameterType);
+            if (!result)
+                throw new Error("At " + node.origin.originString + " argument and parameter types not equal after type argument substitution: argument = " + argumentType + ", parameter = " + parameterType);
+        }
         return node.resolve(overload);
     }
 }

Modified: trunk/Tools/WebGPUShadingLanguageRI/ConstexprTypeParameter.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/ConstexprTypeParameter.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/ConstexprTypeParameter.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -45,16 +45,8 @@
 
         if (!this.type.unify(unificationContext, other.type))
             return false;
-
-        let realThis = unificationContext.find(this);
-        if (realThis != this)
-            return realThis.unify(unificationContext, other);
         
-        if (other instanceof VariableRef)
-            other = other.variable;
-        
-        unificationContext.unify(this, other);
-        return true;
+        return this._typeVariableUnifyImpl(unificationContext, other);
     }
     
     unifyImpl(unificationContext, other)

Modified: trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -94,7 +94,10 @@
     
     visitDereferenceExpression(node)
     {
-        return node.ptr.visit(this).loadValue();
+        let ptr = node.ptr.visit(this).loadValue();
+        if (!ptr)
+            throw new WTrapError(node.origin.originString, "Null dereference");
+        return ptr;
     }
     
     visitMakePtrExpression(node)
@@ -126,6 +129,11 @@
         return EPtr.box(node.value);
     }
     
+    visitNullLiteral(node)
+    {
+        return EPtr.box(null);
+    }
+    
     visitCallExpression(node)
     {
         // We evaluate inlined ASTs, so this can only be a native call.

Modified: trunk/Tools/WebGPUShadingLanguageRI/Func.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Func.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Func.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -44,9 +44,14 @@
     
     get kind() { return Func; }
     
-    toString()
+    toDeclString()
     {
         return this.returnType + " " + this.name + "<" + this.typeParameters + ">(" + this.parameters + ")";
     }
+    
+    toString()
+    {
+        return this.toDeclString();
+    }
 }
 

Modified: trunk/Tools/WebGPUShadingLanguageRI/Intrinsics.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Intrinsics.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Intrinsics.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -80,6 +80,8 @@
         let arrayElementPtr = func => {
             func.implementation = ([ref, index], node) => {
                 ref = ref.loadValue();
+                if (!ref)
+                    throw new WTrapError(node.origin.originString, "Null dereference");
                 index = index.loadValue();
                 if (index > ref.length)
                     throw new WTrapError(node.origin.originString, "Array index " + index + " is out of bounds of " + ref);

Added: trunk/Tools/WebGPUShadingLanguageRI/LiteralTypeChecker.js (0 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/LiteralTypeChecker.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/LiteralTypeChecker.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2017 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. ``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
+ * 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. 
+ */
+"use strict";
+
+// This phase is for asserting that the compiler didn't violate its internal constraints, so it
+// throws Error not WTypeError.
+//
+// The reason this phase exists is that in a compiler, we need literals like null and 0 to know their
+// exact type so that they know how many bytes they occupy and how to set their default value. For
+// example, null for ^ will probably take one pointer-sized word while null for [] will take two words
+// (one for base and one for length). The type checker is equipped to infer this, and this phase's job
+// is just to verify that the type checker succeeded.
+class LiteralTypeChecker extends Visitor {
+    visitNullType(node)
+    {
+        if (!node.type)
+            throw new Error("Null at " + node.origin.originString + " does not have type");
+    }
+}
+

Modified: trunk/Tools/WebGPUShadingLanguageRI/NativeFunc.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/NativeFunc.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/NativeFunc.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -32,9 +32,9 @@
     
     get isNative() { return true; }
 
-    toString()
+    toDeclString()
     {
-        return "native " + super.toString();
+        return "native " + super.toDeclString();
     }
 }
 

Modified: trunk/Tools/WebGPUShadingLanguageRI/NativeFuncInstance.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/NativeFuncInstance.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/NativeFuncInstance.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -33,9 +33,9 @@
     get func() { return this._func; }
     get isNative() { return true; }
 
-    toString()
+    toDeclString()
     {
-        return "native " + super.toString();
+        return "native " + super.toDeclString();
     }
 }
 

Modified: trunk/Tools/WebGPUShadingLanguageRI/Node.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Node.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Node.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -42,6 +42,8 @@
             throw new Error("Null other");
         let unifyThis = this.unifyNode;
         let unifyOther = other.unifyNode;
+        if (unifyThis == unifyOther)
+            return true;
         if (unifyOther.typeVariableUnify(unificationContext, unifyThis))
             return true;
         return unifyThis.unifyImpl(unificationContext, unifyOther);
@@ -59,6 +61,19 @@
         return false;
     }
     
+    _typeVariableUnifyImpl(unificationContext, other)
+    {
+        let realThis = unificationContext.find(this);
+        if (realThis != this)
+            return realThis.unify(unificationContext, other);
+        
+        unificationContext.union(this, other);
+        return true;
+    }
+    
+    // Most type variables don't care about this.
+    commitUnification(unificatoinContext) { }
+    
     get unifyNode() { return this; }
     get isUnifiable() { return false; }
     
@@ -67,9 +82,20 @@
     equals(other)
     {
         let unificationContext = new UnificationContext();
-        return this.unify(unificationContext, other) && unificationContext.isUnunified();
+        if (this.unify(unificationContext, other) && unificationContext.verify())
+            return unificationContext;
+        return false;
     }
     
+    equalsWithCommit(other)
+    {
+        let unificationContext = this.equals(other);
+        if (!unificationContext)
+            return false;
+        unificationContext.commit();
+        return unificationContext;
+    }
+    
     substitute(parameters, argumentList)
     {
         return this.visit(new Substitution(parameters, argumentList));

Added: trunk/Tools/WebGPUShadingLanguageRI/NullLiteral.js (0 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/NullLiteral.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/NullLiteral.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 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. ``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
+ * 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. 
+ */
+"use strict";
+
+class NullLiteral extends _expression_ {
+    constructor(origin)
+    {
+        super(origin);
+        this.type = new NullType(origin);
+    }
+    
+    toString()
+    {
+        return "null";
+    }
+}
+

Modified: trunk/Tools/WebGPUShadingLanguageRI/NullType.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/NullType.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/NullType.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -25,9 +25,53 @@
 "use strict";
 
 class NullType extends Type {
-    // FIXME: This will have to behave like a type variable for the purposes of unification, so that
-    // it can be unified with any pointer type. Then, we can do a verification at the end to see if
-    // we got unified with a pointer type.
-    // https://bugs.webkit.org/show_bug.cgi?id=176235
+    constructor(origin)
+    {
+        super();
+        this._origin = origin;
+    }
+    
+    get origin() { return this._origin; }
+    
+    // For now we answer false for isPtr and isArrayRef because that happens to make all of the logic
+    // work. But, it's strange, since as a bottom type it really is the case that this is both isPtr and
+    // isArrayRef.
+    
+    get isPrimitive() { return true; }
+    get isUnifiable() { return true; }
+    
+    typeVariableUnify(unificationContext, other)
+    {
+        if (!(other instanceof Type))
+            return false;
+        
+        return this._typeVariableUnifyImpl(unificationContext, other);
+    }
+    
+    unifyImpl(unificationContext, other)
+    {
+        return this.typeVariableUnify(unificationContext, other);
+    }
+    
+    verifyAsArgument(unificationContext)
+    {
+        let realThis = unificationContext.find(this);
+        return realThis.isPtr || realThis.isArrayRef;
+    }
+    
+    verifyAsParameter(unificationContext)
+    {
+        throw new Error("NullType should never be used as a type parameter");
+    }
+    
+    commitUnification(unificationContext)
+    {
+        this.type = unificationContext.find(this);
+    }
+    
+    toString()
+    {
+        return "null";
+    }
 }
 

Modified: trunk/Tools/WebGPUShadingLanguageRI/Parse.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Parse.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Parse.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -168,7 +168,7 @@
     {
         let token;
         if (token = tryConsume("null"))
-            return new Null(token);
+            return new NullLiteral(token);
         if (token = tryConsumeKind("identifier"))
             return new VariableRef(token, token.text);
         if (token = tryConsumeKind("intLiteral")) {
@@ -260,7 +260,8 @@
             addressSpaceConsumed = true;
             if (addressSpace)
                 return addressSpace;
-            return tryConsume(...addressSpaces);
+            addressSpace = consume(...addressSpaces);
+            return addressSpace;
         }
         
         while (token = tryConsume("^", "[")) {

Modified: trunk/Tools/WebGPUShadingLanguageRI/Prepare.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Prepare.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Prepare.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -32,6 +32,7 @@
     resolveNames(program);
     resolveTypeDefs(program);
     check(program);
+    checkLiteralTypes(program);
     checkReturns(program);
     checkUnreachableCode(program);
     inline(program);

Modified: trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -194,7 +194,21 @@
     {
         return node;
     }
-
+    
+    visitNullLiteral(node)
+    {
+        let result = new NullLiteral(node.origin);
+        result.type = node.type.visit(this);
+        return result;
+    }
+    
+    visitNullType(node)
+    {
+        let result = new NullType(node.origin);
+        result.type = node.type ? node.type.visit(this) : null;
+        return result;
+    }
+    
     visitCallExpression(node)
     {
         let result = new CallExpression(

Added: trunk/Tools/WebGPUShadingLanguageRI/Test.html (0 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Test.html	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/Test.html	2017-09-03 17:43:25 UTC (rev 221549)
@@ -0,0 +1,93 @@
+<html>
+<head>
+<title>WSL Test</title>
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+<script src=""
+
+<script src=""
+</head>
+<body _onload_="doTest(this)">
+<div id="messages"></div>
+</body>
+</html>

Modified: trunk/Tools/WebGPUShadingLanguageRI/Test.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Test.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Test.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -24,7 +24,15 @@
  */
 "use strict";
 
-load("All.js");
+if (this.window) {
+    this.print = (text) => {
+        var span = document.createElement("span");
+        document.getElementById("messages").appendChild(span);
+        span.innerHTML = text + "<br>";
+    };
+    this.preciseTime = () => performance.now() / 1000;
+} else
+    load("All.js");
 
 function doPrep(code)
 {
@@ -376,8 +384,228 @@
         (e) => e instanceof WTypeError);
 }
 
-let before = preciseTime();
+function TEST_doubleGenericCallsDoubleGeneric()
+{
+    doPrep(`
+        void foo<T, U>(T, U) { }
+        void bar<V, W>(V x, W y) { foo(x, y); }
+    `);
+}
 
+function TEST_doubleGenericCallsSingleGeneric()
+{
+    checkFail(
+        () => doPrep(`
+            void foo<T>(T, T) { }
+            void bar<V, W>(V x, W y) { foo(x, y); }
+        `),
+        (e) => e instanceof WTypeError);
+}
+
+function TEST_loadNull()
+{
+    checkFail(
+        () => doPrep(`
+            void sink<T>(T) { }
+            void foo() { sink(^null); }
+        `),
+        (e) => e instanceof WTypeError && e.message.indexOf("Type passed to dereference is not a pointer: null") != -1);
+}
+
+function TEST_storeNull()
+{
+    checkFail(
+        () => doPrep(`
+            void foo() { ^null = 42; }
+        `),
+        (e) => e instanceof WTypeError && e.message.indexOf("Type passed to dereference is not a pointer: null") != -1);
+}
+
+function TEST_returnNull()
+{
+    let program = doPrep(`
+        thread int^ foo() { return null; }
+    `);
+    let result = callFunction(program, "foo", [], []);
+    if (!result.type.isPtr)
+        throw new Error("Return type is not a pointer: " + result.type);
+    if (!result.type.elementType.equals(program.intrinsics.int32))
+        throw new Error("Return type is not a pointer to an int: " + result.type);
+    if (result.value != null)
+        throw new Error("Return value is not null: " + result.value);
+}
+
+function TEST_dereferenceDefaultNull()
+{
+    let program = doPrep(`
+        int foo()
+        {
+            thread int^ p;
+            return ^p;
+        }
+    `);
+    checkFail(
+        () => callFunction(program, "foo", [], []),
+        (e) => e instanceof WTrapError);
+}
+
+function TEST_defaultInitializedNull()
+{
+    let program = doPrep(`
+        int foo()
+        {
+            thread int^ p = null;;
+            return ^p;
+        }
+    `);
+    checkFail(
+        () => callFunction(program, "foo", [], []),
+        (e) => e instanceof WTrapError);
+}
+
+function TEST_passNullToPtrMonomorphic()
+{
+    let program = doPrep(`
+        int foo(thread int^ ptr)
+        {
+            return ^ptr;
+        }
+        int bar()
+        {
+            return foo(null);
+        }
+    `);
+    checkFail(
+        () => callFunction(program, "bar", [], []),
+        (e) => e instanceof WTrapError);
+}
+
+function TEST_passNullToPtrPolymorphic()
+{
+    checkFail(
+        () => doPrep(`
+            T foo<T>(thread T^ ptr)
+            {
+                return ^ptr;
+            }
+            int bar()
+            {
+                return foo(null);
+            }
+        `),
+        (e) => e instanceof WTypeError);
+}
+
+function TEST_passNullToPolymorphic()
+{
+    checkFail(
+        () => doPrep(`
+            T foo<T>(T ptr)
+            {
+                return ptr;
+            }
+            int bar()
+            {
+                return foo(null);
+            }
+        `),
+        (e) => e instanceof WTypeError);
+}
+
+function TEST_loadNullArrayRef()
+{
+    checkFail(
+        () => doPrep(`
+            void sink<T>(T) { }
+            void foo() { sink(null[0u]); }
+        `),
+        (e) => e instanceof WTypeError && e.message.indexOf("Did not find function for call") != -1);
+}
+
+function TEST_storeNullArrayRef()
+{
+    checkFail(
+        () => doPrep(`
+            void foo() { null[0u] = 42; }
+        `),
+        (e) => e instanceof WTypeError && e.message.indexOf("Did not find function for call") != -1);
+}
+
+function TEST_returnNullArrayRef()
+{
+    let program = doPrep(`
+        thread int[] foo() { return null; }
+    `);
+    let result = callFunction(program, "foo", [], []);
+    if (!result.type.isArrayRef)
+        throw new Error("Return type is not an array reference: " + result.type);
+    if (!result.type.elementType.equals(program.intrinsics.int32))
+        throw new Error("Return type is not an int array reference: " + result.type);
+    if (result.value != null)
+        throw new Error("Return value is not null: " + result.value);
+}
+
+function TEST_dereferenceDefaultNullArrayRef()
+{
+    let program = doPrep(`
+        int foo()
+        {
+            thread int[] p;
+            return p[0u];
+        }
+    `);
+    checkFail(
+        () => callFunction(program, "foo", [], []),
+        (e) => e instanceof WTrapError);
+}
+
+function TEST_defaultInitializedNullArrayRef()
+{
+    let program = doPrep(`
+        int foo()
+        {
+            thread int[] p = null;
+            return p[0u];
+        }
+    `);
+    checkFail(
+        () => callFunction(program, "foo", [], []),
+        (e) => e instanceof WTrapError);
+}
+
+function TEST_passNullToPtrMonomorphicArrayRef()
+{
+    let program = doPrep(`
+        int foo(thread int[] ptr)
+        {
+            return ptr[0u];
+        }
+        int bar()
+        {
+            return foo(null);
+        }
+    `);
+    checkFail(
+        () => callFunction(program, "bar", [], []),
+        (e) => e instanceof WTrapError);
+}
+
+function TEST_passNullToPtrPolymorphicArrayRef()
+{
+    checkFail(
+        () => doPrep(`
+            T foo<T>(thread T[] ptr)
+            {
+                return ptr[0u];
+            }
+            int bar()
+            {
+                return foo(null);
+            }
+        `),
+        (e) => e instanceof WTypeError);
+}
+
 let filter = /.*/; // run everything by default
 if (this["arguments"]) {
     for (let i = 0; i < arguments.length; i++) {
@@ -391,15 +619,22 @@
     }
 }
 
+function doTest(object)
+{
+    let before = preciseTime();
 
-for (let s in this) {
-    if (s.startsWith("TEST_") && s.match(filter)) {
-        print(s + "...");
-        this[s]();
-        print("    OK!");
+    for (let s in object) {
+        if (s.startsWith("TEST_") && s.match(filter)) {
+            print(s + "...");
+            object[s]();
+            print("    OK!");
+        }
     }
+
+    let after = preciseTime();
+    
+    print("That took " + (after - before) * 1000 + " ms.");
 }
 
-let after = preciseTime();
-
-print("That took " + (after - before) * 1000 + " ms.");
+if (!this.window)
+    doTest(this);

Modified: trunk/Tools/WebGPUShadingLanguageRI/Type.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Type.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Type.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -28,6 +28,7 @@
     get typeParameters() { return []; }
     get kind() { return Type; }
     get isPtr() { return false; }
+    get isArrayRef() { return false; }
     
     inherits(protocol)
     {

Modified: trunk/Tools/WebGPUShadingLanguageRI/TypeVariable.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/TypeVariable.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/TypeVariable.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -56,12 +56,8 @@
     {
         if (!(other instanceof Type))
             return false;
-        let realThis = unificationContext.find(this);
-        if (realThis != this)
-            return realThis.unify(unificationContext, other);
         
-        unificationContext.union(this, other);
-        return true;
+        return this._typeVariableUnifyImpl(unificationContext, other);
     }
     
     unifyImpl(unificationContext, other)

Modified: trunk/Tools/WebGPUShadingLanguageRI/UnificationContext.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/UnificationContext.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/UnificationContext.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -77,6 +77,18 @@
         return result;
     }
     
+    typeParameters() { return this._typeParameters; }
+    *typeArguments()
+    {
+        for (let typeArgument of this.nodes) {
+            if (!typeArgument.isUnifiable)
+                continue;
+            if (this._typeParameters.has(typeArgument))
+                continue;
+            yield typeArgument;
+        }
+    }
+    
     verify()
     {
         for (let typeParameter of this._typeParameters) {
@@ -83,20 +95,21 @@
             if (!typeParameter.verifyAsParameter(this))
                 return false;
         }
-        for (let typeArgument of this.nodes) {
-            if (!typeArgument.isUnifiable)
-                continue;
-            if (this._typeParameters.has(typeArgument))
-                continue;
+        let numTypeVariableArguments = 0;
+        let argumentSet = new Set();
+        for (let typeArgument of this.typeArguments()) {
             if (!typeArgument.verifyAsArgument(this))
                 return false;
+            argumentSet.add(this.find(typeArgument));
+            numTypeVariableArguments++;
         }
-        return true;
+        return argumentSet.size == numTypeVariableArguments;
     }
     
-    isUnunified()
+    commit()
     {
-        return !this._nextMap.size;
+        for (let typeArgument of this.typeArguments())
+            typeArgument.commitUnification(this);
     }
 }
 

Modified: trunk/Tools/WebGPUShadingLanguageRI/VariableRef.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/VariableRef.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/VariableRef.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -37,6 +37,7 @@
     get unifyNode() { return this.variable.unifyNode; }
     get isLValue() { return true; }
     get addressSpace() { return "thread"; }
+    get unifyNode() { return this.variable; }
     
     toString()
     {

Modified: trunk/Tools/WebGPUShadingLanguageRI/Visitor.js (221548 => 221549)


--- trunk/Tools/WebGPUShadingLanguageRI/Visitor.js	2017-09-03 10:31:29 UTC (rev 221548)
+++ trunk/Tools/WebGPUShadingLanguageRI/Visitor.js	2017-09-03 17:43:25 UTC (rev 221549)
@@ -25,6 +25,10 @@
 "use strict";
 
 class Visitor {
+    constructor()
+    {
+    }
+    
     visitProgram(node)
     {
         for (let statement of node.topLevelStatements)
@@ -206,6 +210,17 @@
     {
     }
     
+    visitNullLiteral(node)
+    {
+        node.type.visit(this);
+    }
+    
+    visitNullType(node)
+    {
+        if (node.type)
+            node.type.visit(this);
+    }
+    
     visitCallExpression(node)
     {
         for (let typeArgument of node.typeArguments)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to