069b93cc10d538f8ee270da676d34b0c402c5626
[WebKit-https.git] / Source / WebCore / Modules / webgpu / WHLSL / WHLSLNameResolver.cpp
1 /*
2  * Copyright (C) 2019 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WHLSLNameResolver.h"
28
29 #if ENABLE(WEBGPU)
30
31 #include "WHLSLCallExpression.h"
32 #include "WHLSLDoWhileLoop.h"
33 #include "WHLSLDotExpression.h"
34 #include "WHLSLEnumerationDefinition.h"
35 #include "WHLSLEnumerationMemberLiteral.h"
36 #include "WHLSLForLoop.h"
37 #include "WHLSLFunctionDefinition.h"
38 #include "WHLSLIfStatement.h"
39 #include "WHLSLNameContext.h"
40 #include "WHLSLProgram.h"
41 #include "WHLSLPropertyAccessExpression.h"
42 #include "WHLSLReplaceWith.h"
43 #include "WHLSLResolveOverloadImpl.h"
44 #include "WHLSLReturn.h"
45 #include "WHLSLScopedSetAdder.h"
46 #include "WHLSLTypeReference.h"
47 #include "WHLSLVariableDeclaration.h"
48 #include "WHLSLVariableReference.h"
49 #include "WHLSLWhileLoop.h"
50
51 namespace WebCore {
52
53 namespace WHLSL {
54
55 NameResolver::NameResolver(NameContext& nameContext)
56     : m_nameContext(nameContext)
57 {
58 }
59
60 NameResolver::NameResolver(NameResolver& parentResolver, NameContext& nameContext)
61     : m_nameContext(nameContext)
62     , m_parentNameResolver(&parentResolver)
63 {
64     setCurrentFunctionDefinition(parentResolver.m_currentFunction);
65 }
66
67 NameResolver::~NameResolver()
68 {
69     if (error() && m_parentNameResolver)
70         m_parentNameResolver->setError();
71 }
72
73 void NameResolver::visit(AST::TypeReference& typeReference)
74 {
75     ScopedSetAdder<AST::TypeReference*> adder(m_typeReferences, &typeReference);
76     if (!adder.isNewEntry()) {
77         setError();
78         return;
79     }
80
81     Visitor::visit(typeReference);
82     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?
83         return;
84
85     auto* candidates = m_nameContext.getTypes(typeReference.name());
86     if (candidates == nullptr) {
87         setError();
88         return;
89     }
90     for (auto& candidate : *candidates)
91         Visitor::visit(candidate);
92     if (auto result = resolveTypeOverloadImpl(*candidates, typeReference.typeArguments()))
93         typeReference.setResolvedType(*result);
94     else {
95         setError();
96         return;
97     }
98 }
99
100 void NameResolver::visit(AST::FunctionDefinition& functionDefinition)
101 {
102     NameContext newNameContext(&m_nameContext);
103     NameResolver newNameResolver(*this, newNameContext);
104     checkErrorAndVisit(functionDefinition.type());
105     for (auto& parameter : functionDefinition.parameters())
106         newNameResolver.checkErrorAndVisit(parameter);
107     newNameResolver.checkErrorAndVisit(functionDefinition.block());
108 }
109
110 void NameResolver::visit(AST::Block& block)
111 {
112     NameContext nameContext(&m_nameContext);
113     NameResolver newNameResolver(*this, nameContext);
114     newNameResolver.Visitor::visit(block);
115 }
116
117 void NameResolver::visit(AST::IfStatement& ifStatement)
118 {
119     checkErrorAndVisit(ifStatement.conditional());
120     if (error())
121         return;
122
123     {
124         NameContext nameContext(&m_nameContext);
125         NameResolver newNameResolver(*this, nameContext);
126         newNameResolver.checkErrorAndVisit(ifStatement.body());
127     }
128     if (error())
129         return;
130
131     if (ifStatement.elseBody()) {
132         NameContext nameContext(&m_nameContext);
133         NameResolver newNameResolver(*this, nameContext);
134         newNameResolver.checkErrorAndVisit(*ifStatement.elseBody());
135     }
136 }
137
138 void NameResolver::visit(AST::WhileLoop& whileLoop)
139 {
140     checkErrorAndVisit(whileLoop.conditional());
141     if (error())
142         return;
143
144     NameContext nameContext(&m_nameContext);
145     NameResolver newNameResolver(*this, nameContext);
146     newNameResolver.checkErrorAndVisit(whileLoop.body());
147 }
148
149 void NameResolver::visit(AST::DoWhileLoop& whileLoop)
150 {
151     {
152         NameContext nameContext(&m_nameContext);
153         NameResolver newNameResolver(*this, nameContext);
154         newNameResolver.checkErrorAndVisit(whileLoop.body());
155     }
156
157     checkErrorAndVisit(whileLoop.conditional());
158 }
159
160 void NameResolver::visit(AST::ForLoop& forLoop)
161 {
162     NameContext nameContext(&m_nameContext);
163     NameResolver newNameResolver(*this, nameContext);
164     newNameResolver.Visitor::visit(forLoop);
165 }
166
167 void NameResolver::visit(AST::VariableDeclaration& variableDeclaration)
168 {
169     if (!m_nameContext.add(variableDeclaration)) {
170         setError();
171         return;
172     }
173     Visitor::visit(variableDeclaration);
174 }
175
176 void NameResolver::visit(AST::VariableReference& variableReference)
177 {
178     if (variableReference.variable())
179         return;
180
181     if (auto* variable = m_nameContext.getVariable(variableReference.name()))
182         variableReference.setVariable(*variable);
183     else {
184         setError();
185         return;
186     }
187 }
188
189 void NameResolver::visit(AST::Return& returnStatement)
190 {
191     ASSERT(m_currentFunction);
192     returnStatement.setFunction(m_currentFunction);
193     Visitor::visit(returnStatement);
194 }
195
196 void NameResolver::visit(AST::PropertyAccessExpression& propertyAccessExpression)
197 {
198     Visitor::visit(propertyAccessExpression);
199 }
200
201 void NameResolver::visit(AST::DotExpression& dotExpression)
202 {
203     if (is<AST::VariableReference>(dotExpression.base())) {
204         auto baseName = downcast<AST::VariableReference>(dotExpression.base()).name();
205         if (auto enumerationTypes = m_nameContext.getTypes(baseName)) {
206             ASSERT(enumerationTypes->size() == 1);
207             AST::NamedType& type = (*enumerationTypes)[0];
208             if (is<AST::EnumerationDefinition>(type)) {
209                 AST::EnumerationDefinition& enumerationDefinition = downcast<AST::EnumerationDefinition>(type);
210                 auto memberName = dotExpression.fieldName();
211                 if (auto* member = enumerationDefinition.memberByName(memberName)) {
212                     Lexer::Token origin = dotExpression.origin();
213                     auto enumerationMemberLiteral = AST::EnumerationMemberLiteral::wrap(WTFMove(origin), WTFMove(baseName), WTFMove(memberName), enumerationDefinition, *member);
214                     AST::replaceWith<AST::EnumerationMemberLiteral>(dotExpression, WTFMove(enumerationMemberLiteral));
215                     return;
216                 }
217                 setError();
218                 return;
219             }
220         }
221     }
222
223     Visitor::visit(dotExpression);
224 }
225
226 void NameResolver::visit(AST::CallExpression& callExpression)
227 {
228     Visitor::visit(callExpression);
229 }
230
231 void NameResolver::visit(AST::EnumerationMemberLiteral& enumerationMemberLiteral)
232 {
233     if (enumerationMemberLiteral.enumerationMember())
234         return;
235
236     if (auto enumerationTypes = m_nameContext.getTypes(enumerationMemberLiteral.left())) {
237         ASSERT(enumerationTypes->size() == 1);
238         AST::NamedType& type = (*enumerationTypes)[0];
239         if (is<AST::EnumerationDefinition>(type)) {
240             AST::EnumerationDefinition& enumerationDefinition = downcast<AST::EnumerationDefinition>(type);
241             if (auto* member = enumerationDefinition.memberByName(enumerationMemberLiteral.right())) {
242                 enumerationMemberLiteral.setEnumerationMember(enumerationDefinition, *member);
243                 return;
244             }
245         }
246     }
247     
248     setError();
249 }
250
251 void NameResolver::visit(AST::NativeFunctionDeclaration& nativeFunctionDeclaration)
252 {
253     NameContext newNameContext(&m_nameContext);
254     NameResolver newNameResolver(newNameContext);
255     newNameResolver.Visitor::visit(nativeFunctionDeclaration);
256 }
257
258 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=198167 Make sure all the names have been resolved.
259
260 bool resolveNamesInTypes(Program& program, NameResolver& nameResolver)
261 {
262     for (auto& typeDefinition : program.typeDefinitions()) {
263         nameResolver.checkErrorAndVisit(typeDefinition);
264         if (nameResolver.error())
265             return false;
266     }
267     for (auto& structureDefinition : program.structureDefinitions()) {
268         nameResolver.checkErrorAndVisit(structureDefinition);
269         if (nameResolver.error())
270             return false;
271     }
272     for (auto& enumerationDefinition : program.enumerationDefinitions()) {
273         nameResolver.checkErrorAndVisit(enumerationDefinition);
274         if (nameResolver.error())
275             return false;
276     }
277     for (auto& nativeTypeDeclaration : program.nativeTypeDeclarations()) {
278         nameResolver.checkErrorAndVisit(nativeTypeDeclaration);
279         if (nameResolver.error())
280             return false;
281     }
282     return true;
283 }
284
285 bool resolveTypeNamesInFunctions(Program& program, NameResolver& nameResolver)
286 {
287     for (auto& functionDefinition : program.functionDefinitions()) {
288         nameResolver.setCurrentFunctionDefinition(&functionDefinition);
289         nameResolver.checkErrorAndVisit(functionDefinition);
290         if (nameResolver.error())
291             return false;
292     }
293     nameResolver.setCurrentFunctionDefinition(nullptr);
294     for (auto& nativeFunctionDeclaration : program.nativeFunctionDeclarations()) {
295         nameResolver.checkErrorAndVisit(nativeFunctionDeclaration);
296         if (nameResolver.error())
297             return false;
298     }
299     return true;
300 }
301
302 } // namespace WHLSL
303
304 } // namespace WebCore
305
306 #endif // ENABLE(WEBGPU)