[WHLSL] Standard library is too big to directly include in WebCore
[WebKit-https.git] / Source / WebCore / Modules / webgpu / WHLSL / WHLSLNameResolver.cpp
index 21ca18d..2c3a2d7 100644 (file)
@@ -39,6 +39,7 @@
 #include "WHLSLNameContext.h"
 #include "WHLSLProgram.h"
 #include "WHLSLPropertyAccessExpression.h"
+#include "WHLSLReplaceWith.h"
 #include "WHLSLResolveOverloadImpl.h"
 #include "WHLSLReturn.h"
 #include "WHLSLScopedSetAdder.h"
@@ -56,6 +57,19 @@ NameResolver::NameResolver(NameContext& nameContext)
 {
 }
 
+NameResolver::NameResolver(NameResolver& parentResolver, NameContext& nameContext)
+    : m_nameContext(nameContext)
+    , m_parentNameResolver(&parentResolver)
+{
+    setCurrentFunctionDefinition(parentResolver.m_currentFunction);
+}
+
+NameResolver::~NameResolver()
+{
+    if (error() && m_parentNameResolver)
+        m_parentNameResolver->setError();
+}
+
 void NameResolver::visit(AST::TypeReference& typeReference)
 {
     ScopedSetAdder<AST::TypeReference*> adder(m_typeReferences, &typeReference);
@@ -65,6 +79,8 @@ void NameResolver::visit(AST::TypeReference& typeReference)
     }
 
     Visitor::visit(typeReference);
+    if (error())
+        return;
     if (typeReference.maybeResolvedType()) // FIXME: https://bugs.webkit.org/show_bug.cgi?id=198161 Shouldn't we know by now whether the type has been resolved or not?
         return;
 
@@ -86,9 +102,10 @@ void NameResolver::visit(AST::TypeReference& typeReference)
 void NameResolver::visit(AST::FunctionDefinition& functionDefinition)
 {
     NameContext newNameContext(&m_nameContext);
-    NameResolver newNameResolver(newNameContext);
-    newNameResolver.setCurrentFunctionDefinition(m_currentFunction);
+    NameResolver newNameResolver(*this, newNameContext);
     checkErrorAndVisit(functionDefinition.type());
+    if (error())
+        return;
     for (auto& parameter : functionDefinition.parameters())
         newNameResolver.checkErrorAndVisit(parameter);
     newNameResolver.checkErrorAndVisit(functionDefinition.block());
@@ -97,22 +114,27 @@ void NameResolver::visit(AST::FunctionDefinition& functionDefinition)
 void NameResolver::visit(AST::Block& block)
 {
     NameContext nameContext(&m_nameContext);
-    NameResolver newNameResolver(nameContext);
-    newNameResolver.setCurrentFunctionDefinition(m_currentFunction);
+    NameResolver newNameResolver(*this, nameContext);
     newNameResolver.Visitor::visit(block);
 }
 
 void NameResolver::visit(AST::IfStatement& ifStatement)
 {
     checkErrorAndVisit(ifStatement.conditional());
-    NameContext nameContext(&m_nameContext);
-    NameResolver newNameResolver(nameContext);
-    newNameResolver.setCurrentFunctionDefinition(m_currentFunction);
-    newNameResolver.checkErrorAndVisit(ifStatement.body());
+    if (error())
+        return;
+
+    {
+        NameContext nameContext(&m_nameContext);
+        NameResolver newNameResolver(*this, nameContext);
+        newNameResolver.checkErrorAndVisit(ifStatement.body());
+    }
+    if (error())
+        return;
+
     if (ifStatement.elseBody()) {
         NameContext nameContext(&m_nameContext);
-        NameResolver newNameResolver(nameContext);
-        newNameResolver.setCurrentFunctionDefinition(m_currentFunction);
+        NameResolver newNameResolver(*this, nameContext);
         newNameResolver.checkErrorAndVisit(*ifStatement.elseBody());
     }
 }
@@ -120,26 +142,29 @@ void NameResolver::visit(AST::IfStatement& ifStatement)
 void NameResolver::visit(AST::WhileLoop& whileLoop)
 {
     checkErrorAndVisit(whileLoop.conditional());
+    if (error())
+        return;
+
     NameContext nameContext(&m_nameContext);
-    NameResolver newNameResolver(nameContext);
-    newNameResolver.setCurrentFunctionDefinition(m_currentFunction);
+    NameResolver newNameResolver(*this, nameContext);
     newNameResolver.checkErrorAndVisit(whileLoop.body());
 }
 
 void NameResolver::visit(AST::DoWhileLoop& whileLoop)
 {
-    NameContext nameContext(&m_nameContext);
-    NameResolver newNameResolver(nameContext);
-    newNameResolver.setCurrentFunctionDefinition(m_currentFunction);
-    newNameResolver.checkErrorAndVisit(whileLoop.body());
+    {
+        NameContext nameContext(&m_nameContext);
+        NameResolver newNameResolver(*this, nameContext);
+        newNameResolver.checkErrorAndVisit(whileLoop.body());
+    }
+
     checkErrorAndVisit(whileLoop.conditional());
 }
 
 void NameResolver::visit(AST::ForLoop& forLoop)
 {
     NameContext nameContext(&m_nameContext);
-    NameResolver newNameResolver(nameContext);
-    newNameResolver.setCurrentFunctionDefinition(m_currentFunction);
+    NameResolver newNameResolver(*this, nameContext);
     newNameResolver.Visitor::visit(forLoop);
 }
 
@@ -174,12 +199,6 @@ void NameResolver::visit(AST::Return& returnStatement)
 
 void NameResolver::visit(AST::PropertyAccessExpression& propertyAccessExpression)
 {
-    if (auto* getterFunctions = m_nameContext.getFunctions(propertyAccessExpression.getterFunctionName()))
-        propertyAccessExpression.setPossibleGetterOverloads(*getterFunctions);
-    if (auto* setterFunctions = m_nameContext.getFunctions(propertyAccessExpression.setterFunctionName()))
-        propertyAccessExpression.setPossibleSetterOverloads(*setterFunctions);
-    if (auto* anderFunctions = m_nameContext.getFunctions(propertyAccessExpression.anderFunctionName()))
-        propertyAccessExpression.setPossibleAnderOverloads(*anderFunctions);
     Visitor::visit(propertyAccessExpression);
 }
 
@@ -210,24 +229,6 @@ void NameResolver::visit(AST::DotExpression& dotExpression)
 
 void NameResolver::visit(AST::CallExpression& callExpression)
 {
-    if (!callExpression.hasOverloads()) {
-        if (auto* functions = m_nameContext.getFunctions(callExpression.name()))
-            callExpression.setOverloads(*functions);
-        else {
-            if (auto* types = m_nameContext.getTypes(callExpression.name())) {
-                if (types->size() == 1) {
-                    if (auto* functions = m_nameContext.getFunctions("operator cast"_str)) {
-                        callExpression.setCastData((*types)[0].get());
-                        callExpression.setOverloads(*functions);
-                    }
-                }
-            }
-        }
-    }
-    if (!callExpression.hasOverloads()) {
-        setError();
-        return;
-    }
     Visitor::visit(callExpression);
 }
 
@@ -251,6 +252,13 @@ void NameResolver::visit(AST::EnumerationMemberLiteral& enumerationMemberLiteral
     setError();
 }
 
+void NameResolver::visit(AST::NativeFunctionDeclaration& nativeFunctionDeclaration)
+{
+    NameContext newNameContext(&m_nameContext);
+    NameResolver newNameResolver(newNameContext);
+    newNameResolver.Visitor::visit(nativeFunctionDeclaration);
+}
+
 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=198167 Make sure all the names have been resolved.
 
 bool resolveNamesInTypes(Program& program, NameResolver& nameResolver)
@@ -278,7 +286,7 @@ bool resolveNamesInTypes(Program& program, NameResolver& nameResolver)
     return true;
 }
 
-bool resolveNamesInFunctions(Program& program, NameResolver& nameResolver)
+bool resolveTypeNamesInFunctions(Program& program, NameResolver& nameResolver)
 {
     for (auto& functionDefinition : program.functionDefinitions()) {
         nameResolver.setCurrentFunctionDefinition(&functionDefinition);