Rename AtomicString to AtomString
[WebKit.git] / Source / WebCore / Modules / webgpu / WHLSL / WHLSLSynthesizeConstructors.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 "WHLSLSynthesizeConstructors.h"
28
29 #if ENABLE(WEBGPU)
30
31 #include "WHLSLArrayType.h"
32 #include "WHLSLArrayReferenceType.h"
33 #include "WHLSLEnumerationDefinition.h"
34 #include "WHLSLInferTypes.h"
35 #include "WHLSLNativeFunctionDeclaration.h"
36 #include "WHLSLNativeTypeDeclaration.h"
37 #include "WHLSLPointerType.h"
38 #include "WHLSLProgram.h"
39 #include "WHLSLStructureDefinition.h"
40 #include "WHLSLTypeReference.h"
41 #include "WHLSLVariableDeclaration.h"
42 #include "WHLSLVisitor.h"
43
44 namespace WebCore {
45
46 namespace WHLSL {
47
48 class FindAllTypes : public Visitor {
49 public:
50     ~FindAllTypes() = default;
51
52     void visit(AST::PointerType& pointerType) override
53     {
54         m_unnamedTypes.append(pointerType);
55         Visitor::visit(pointerType);
56     }
57
58     void visit(AST::ArrayReferenceType& arrayReferenceType) override
59     {
60         m_unnamedTypes.append(arrayReferenceType);
61         Visitor::visit(arrayReferenceType);
62     }
63
64     void visit(AST::ArrayType& arrayType) override
65     {
66         m_unnamedTypes.append(arrayType);
67         Visitor::visit(arrayType);
68     }
69
70     void visit(AST::EnumerationDefinition& enumerationDefinition) override
71     {
72         m_namedTypes.append(enumerationDefinition);
73         Visitor::visit(enumerationDefinition);
74     }
75
76     void visit(AST::StructureDefinition& structureDefinition) override
77     {
78         m_namedTypes.append(structureDefinition);
79         Visitor::visit(structureDefinition);
80     }
81
82     void visit(AST::NativeTypeDeclaration& nativeTypeDeclaration) override
83     {
84         m_namedTypes.append(nativeTypeDeclaration);
85         Visitor::visit(nativeTypeDeclaration);
86     }
87
88     Vector<std::reference_wrapper<AST::UnnamedType>> takeUnnamedTypes()
89     {
90         return WTFMove(m_unnamedTypes);
91     }
92
93     Vector<std::reference_wrapper<AST::NamedType>> takeNamedTypes()
94     {
95         return WTFMove(m_namedTypes);
96     }
97
98 private:
99     Vector<std::reference_wrapper<AST::UnnamedType>> m_unnamedTypes;
100     Vector<std::reference_wrapper<AST::NamedType>> m_namedTypes;
101 };
102
103 bool synthesizeConstructors(Program& program)
104 {
105     FindAllTypes findAllTypes;
106     findAllTypes.checkErrorAndVisit(program);
107     auto unnamedTypes = findAllTypes.takeUnnamedTypes();
108     auto namedTypes = findAllTypes.takeNamedTypes();
109
110     bool isOperator = true;
111
112     for (auto& unnamedType : unnamedTypes) {
113         auto variableDeclaration = makeUniqueRef<AST::VariableDeclaration>(Lexer::Token(unnamedType.get().origin()), AST::Qualifiers(), unnamedType.get().clone(), String(), WTF::nullopt, WTF::nullopt);
114         AST::VariableDeclarations parameters;
115         parameters.append(WTFMove(variableDeclaration));
116         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));
117         program.append(WTFMove(copyConstructor));
118
119         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));
120         if (!program.append(WTFMove(defaultConstructor)))
121             return false;
122     }
123
124     for (auto& namedType : namedTypes) {
125         if (matches(namedType, program.intrinsics().voidType()))
126             continue;
127         if (is<AST::NativeTypeDeclaration>(static_cast<AST::NamedType&>(namedType)) && downcast<AST::NativeTypeDeclaration>(static_cast<AST::NamedType&>(namedType)).isAtom())
128             continue;
129
130         auto variableDeclaration = makeUniqueRef<AST::VariableDeclaration>(Lexer::Token(namedType.get().origin()), AST::Qualifiers(), UniqueRef<AST::UnnamedType>(AST::TypeReference::wrap(Lexer::Token(namedType.get().origin()), namedType.get())), String(), WTF::nullopt, WTF::nullopt);
131         AST::VariableDeclarations parameters;
132         parameters.append(WTFMove(variableDeclaration));
133         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));
134         program.append(WTFMove(copyConstructor));
135
136         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));
137         if (!program.append(WTFMove(defaultConstructor)))
138             return false;
139     }
140     return true;
141 }
142
143 } // namespace WHLSL
144
145 } // namespace WebCore
146
147 #endif // ENABLE(WEBGPU)