Title: [245706] trunk/Source/WebCore
Revision
245706
Author
sbar...@apple.com
Date
2019-05-23 12:54:53 -0700 (Thu, 23 May 2019)

Log Message

[WHLSL] Add a helper for in-place AST mutation
https://bugs.webkit.org/show_bug.cgi?id=198175

Reviewed by Myles Maxfield.

This makes WHLSL AST mutation code a bit easier to read and write.

Code that looked like:
```
static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
void* location = &dotExpression;
dotExpression.~DotExpression();
auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
```

Can now be:
```
auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
```

* Modules/webgpu/WHLSL/AST/WHLSLNode.h:
(WebCore::WHLSL::AST::replaceWith):
* Modules/webgpu/WHLSL/WHLSLNameResolver.cpp:
(WebCore::WHLSL::NameResolver::visit):
* Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp:
(WebCore::WHLSL::PropertyResolver::visit):
(WebCore::WHLSL::PropertyResolver::simplifyRightValue):
(WebCore::WHLSL::LeftValueSimplifier::visit):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (245705 => 245706)


--- trunk/Source/WebCore/ChangeLog	2019-05-23 19:45:15 UTC (rev 245705)
+++ trunk/Source/WebCore/ChangeLog	2019-05-23 19:54:53 UTC (rev 245706)
@@ -1,3 +1,34 @@
+2019-05-23  Saam barati  <sbar...@apple.com>
+
+        [WHLSL] Add a helper for in-place AST mutation
+        https://bugs.webkit.org/show_bug.cgi?id=198175
+
+        Reviewed by Myles Maxfield.
+
+        This makes WHLSL AST mutation code a bit easier to read and write.
+        
+        Code that looked like:
+        ```
+        static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
+        void* location = &dotExpression;
+        dotExpression.~DotExpression();
+        auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
+        ```
+        
+        Can now be:
+        ```
+        auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
+        ```
+
+        * Modules/webgpu/WHLSL/AST/WHLSLNode.h:
+        (WebCore::WHLSL::AST::replaceWith):
+        * Modules/webgpu/WHLSL/WHLSLNameResolver.cpp:
+        (WebCore::WHLSL::NameResolver::visit):
+        * Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp:
+        (WebCore::WHLSL::PropertyResolver::visit):
+        (WebCore::WHLSL::PropertyResolver::simplifyRightValue):
+        (WebCore::WHLSL::LeftValueSimplifier::visit):
+
 2019-05-23  Eric Carlson  <eric.carl...@apple.com>
 
         [macOS,iOS] Add always-on logging for AVPlayerTimeControlStatus changes

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLNode.h (245705 => 245706)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLNode.h	2019-05-23 19:45:15 UTC (rev 245705)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/AST/WHLSLNode.h	2019-05-23 19:54:53 UTC (rev 245706)
@@ -48,6 +48,15 @@
 private:
 };
 
+template <typename New, typename Old, typename ...Args>
+ALWAYS_INLINE New* replaceWith(Old& old, Args&&... args)
+{
+    static_assert(sizeof(New) <= sizeof(Old), "This is needed for the placement new below to not overwrite unowned memory.");
+    void* location = &old;
+    old.~Old();
+    return new (location) New(std::forward<Args>(args)...);
+}
+
 } // namespace AST
 
 }

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLNameResolver.cpp (245705 => 245706)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLNameResolver.cpp	2019-05-23 19:45:15 UTC (rev 245705)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLNameResolver.cpp	2019-05-23 19:54:53 UTC (rev 245706)
@@ -194,12 +194,9 @@
                 AST::EnumerationDefinition& enumerationDefinition = downcast<AST::EnumerationDefinition>(type);
                 auto memberName = dotExpression.fieldName();
                 if (auto* member = enumerationDefinition.memberByName(memberName)) {
-                    static_assert(sizeof(AST::EnumerationMemberLiteral) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become EnumerationMemberLiterals without updating backreferences");
                     Lexer::Token origin = dotExpression.origin();
-                    void* location = &dotExpression;
-                    dotExpression.~DotExpression();
                     auto enumerationMemberLiteral = AST::EnumerationMemberLiteral::wrap(WTFMove(origin), WTFMove(baseName), WTFMove(memberName), enumerationDefinition, *member);
-                    new (location) AST::EnumerationMemberLiteral(WTFMove(enumerationMemberLiteral));
+                    AST::replaceWith<AST::EnumerationMemberLiteral>(dotExpression, WTFMove(enumerationMemberLiteral));
                     return;
                 }
                 setError();

Modified: trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp (245705 => 245706)


--- trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp	2019-05-23 19:45:15 UTC (rev 245705)
+++ trunk/Source/WebCore/Modules/webgpu/WHLSL/WHLSLPropertyResolver.cpp	2019-05-23 19:54:53 UTC (rev 245706)
@@ -404,11 +404,8 @@
     }
     simplifyLeftValue(modifyResult->innerLeftValue);
 
-    static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::AssignmentExpression), "Assignment expressions need to be able to become comma expressions without updating backreferences");
     Lexer::Token origin = assignmentExpression.origin();
-    void* location = &assignmentExpression;
-    assignmentExpression.~AssignmentExpression();
-    auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(modifyResult->expressions));
+    auto* commaExpression = AST::replaceWith<AST::CommaExpression>(assignmentExpression, WTFMove(origin), WTFMove(modifyResult->expressions));
     commaExpression->setType(WTFMove(type));
     commaExpression->setTypeAnnotation(AST::RightValue());
 
@@ -513,11 +510,8 @@
         UniqueRef<AST::VariableDeclaration> oldVariableDeclaration = readModifyWriteExpression.takeOldValue();
         UniqueRef<AST::VariableDeclaration> newVariableDeclaration = readModifyWriteExpression.takeNewValue();
 
-        static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::ReadModifyWriteExpression), "ReadModifyWrite expressions need to be able to become comma expressions without updating backreferences");
         Lexer::Token origin = readModifyWriteExpression.origin();
-        void* location = &readModifyWriteExpression;
-        readModifyWriteExpression.~ReadModifyWriteExpression();
-        auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(expressions));
+        auto* commaExpression = AST::replaceWith<AST::CommaExpression>(readModifyWriteExpression, WTFMove(origin), WTFMove(expressions));
         commaExpression->setType(WTFMove(type));
         commaExpression->setTypeAnnotation(AST::RightValue());
 
@@ -580,11 +574,8 @@
     UniqueRef<AST::VariableDeclaration> oldVariableDeclaration = readModifyWriteExpression.takeOldValue();
     UniqueRef<AST::VariableDeclaration> newVariableDeclaration = readModifyWriteExpression.takeNewValue();
 
-    static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::ReadModifyWriteExpression), "ReadModifyWrite expressions need to be able to become comma expressions without updating backreferences");
     Lexer::Token origin = readModifyWriteExpression.origin();
-    void* location = &readModifyWriteExpression;
-    readModifyWriteExpression.~ReadModifyWriteExpression();
-    auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(modifyResult->expressions));
+    auto* commaExpression = AST::replaceWith<AST::CommaExpression>(readModifyWriteExpression, WTFMove(origin), WTFMove(modifyResult->expressions));
     commaExpression->setType(WTFMove(type));
     commaExpression->setTypeAnnotation(AST::RightValue());
 
@@ -612,10 +603,7 @@
             callExpression->setTypeAnnotation(AST::RightValue());
             callExpression->setFunction(*anderFunction);
 
-            static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
-            void* location = &dotExpression;
-            dotExpression.~DotExpression();
-            auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
+            auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
             dereferenceExpression->setType(downcast<AST::PointerType>(anderFunction->type()).elementType().clone());
             dereferenceExpression->setTypeAnnotation(AST::LeftValue { downcast<AST::PointerType>(anderFunction->type()).addressSpace() });
             return true;
@@ -655,13 +643,10 @@
         dereferenceExpression->setType(downcast<AST::PointerType>(anderFunction->type()).elementType().clone());
         dereferenceExpression->setTypeAnnotation(AST::LeftValue { AST::AddressSpace::Thread });
 
-        static_assert(sizeof(AST::CommaExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become comma expressions without updating backreferences");
-        void* location = &dotExpression;
-        dotExpression.~DotExpression();
         Vector<UniqueRef<AST::_expression_>> expressions;
         expressions.append(WTFMove(assignmentExpression));
         expressions.append(WTFMove(dereferenceExpression));
-        auto* commaExpression = new (location) AST::CommaExpression(WTFMove(origin), WTFMove(expressions));
+        auto* commaExpression = AST::replaceWith<AST::CommaExpression>(dotExpression, WTFMove(origin), WTFMove(expressions));
         commaExpression->setType(downcast<AST::PointerType>(anderFunction->type()).elementType().clone());
         commaExpression->setTypeAnnotation(AST::LeftValue { AST::AddressSpace::Thread });
 
@@ -669,14 +654,11 @@
         return true;
     }
 
-    static_assert(sizeof(AST::CallExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become call expressions without updating backreferences");
     ASSERT(dotExpression.getterFunction());
     auto& getterFunction = *dotExpression.getterFunction();
     Vector<UniqueRef<AST::_expression_>> arguments;
     arguments.append(dotExpression.takeBase());
-    void* location = &dotExpression;
-    dotExpression.~DotExpression();
-    auto* callExpression = new (location) AST::CallExpression(WTFMove(origin), String(getterFunction.name()), WTFMove(arguments));
+    auto* callExpression = AST::replaceWith<AST::CallExpression>(dotExpression, WTFMove(origin), String(getterFunction.name()), WTFMove(arguments));
     callExpression->setFunction(getterFunction);
     callExpression->setType(getterFunction.type().clone());
     callExpression->setTypeAnnotation(AST::RightValue());
@@ -712,10 +694,7 @@
     callExpression->setTypeAnnotation(AST::RightValue());
     callExpression->setFunction(*anderFunction);
 
-    static_assert(sizeof(AST::DereferenceExpression) <= sizeof(AST::DotExpression), "Dot expressions need to be able to become dereference expressions without updating backreferences");
-    void* location = &dotExpression;
-    dotExpression.~DotExpression();
-    auto* dereferenceExpression = new (location) AST::DereferenceExpression(WTFMove(origin), WTFMove(callExpression));
+    auto* dereferenceExpression = AST::replaceWith<AST::DereferenceExpression>(dotExpression, WTFMove(origin), WTFMove(callExpression));
     dereferenceExpression->setType(downcast<AST::PointerType>(anderFunction->type()).elementType().clone());
     dereferenceExpression->setTypeAnnotation(AST::LeftValue { downcast<AST::PointerType>(anderFunction->type()).addressSpace() });
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to