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 "WHLSLNameContext.h" |
28 | |
29 | #if ENABLE(WEBGPU) |
30 | |
31 | #include "WHLSLEnumerationDefinition.h" |
32 | #include "WHLSLFunctionDefinition.h" |
33 | #include "WHLSLNativeFunctionDeclaration.h" |
34 | #include "WHLSLNativeTypeDeclaration.h" |
35 | #include "WHLSLStructureDefinition.h" |
36 | #include "WHLSLTypeDefinition.h" |
37 | #include "WHLSLVariableDeclaration.h" |
38 | |
39 | namespace WebCore { |
40 | |
41 | namespace WHLSL { |
42 | |
43 | NameContext::NameContext(NameContext* parent) |
44 | : m_parent(parent) |
45 | { |
46 | } |
47 | |
48 | bool NameContext::add(AST::TypeDefinition& typeDefinition) |
49 | { |
50 | if (exists(typeDefinition.name())) |
51 | return false; |
52 | auto result = m_types.add(typeDefinition.name(), Vector<std::reference_wrapper<AST::NamedType>, 1>()); |
53 | if (!result.isNewEntry) |
54 | return false; |
55 | result.iterator->value.append(typeDefinition); |
56 | return true; |
57 | } |
58 | |
59 | bool NameContext::add(AST::StructureDefinition& structureDefinition) |
60 | { |
61 | if (exists(structureDefinition.name())) |
62 | return false; |
63 | auto result = m_types.add(structureDefinition.name(), Vector<std::reference_wrapper<AST::NamedType>, 1>()); |
64 | if (!result.isNewEntry) |
65 | return false; |
66 | result.iterator->value.append(structureDefinition); |
67 | return true; |
68 | } |
69 | |
70 | bool NameContext::add(AST::EnumerationDefinition& enumerationDefinition) |
71 | { |
72 | if (exists(enumerationDefinition.name())) |
73 | return false; |
74 | auto result = m_types.add(enumerationDefinition.name(), Vector<std::reference_wrapper<AST::NamedType>, 1>()); |
75 | if (!result.isNewEntry) |
76 | return false; |
77 | result.iterator->value.append(enumerationDefinition); |
78 | return true; |
79 | } |
80 | |
81 | bool NameContext::add(AST::FunctionDefinition& functionDefinition) |
82 | { |
83 | if (m_types.find(functionDefinition.name()) != m_types.end() |
84 | || m_variables.find(functionDefinition.name()) != m_variables.end()) |
85 | return false; |
86 | auto result = m_functions.add(functionDefinition.name(), Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>()); |
87 | result.iterator->value.append(functionDefinition); |
88 | return true; |
89 | } |
90 | |
91 | bool NameContext::add(AST::NativeFunctionDeclaration& nativeFunctionDeclaration) |
92 | { |
93 | if (m_types.find(nativeFunctionDeclaration.name()) != m_types.end() |
94 | || m_variables.find(nativeFunctionDeclaration.name()) != m_variables.end()) |
95 | return false; |
96 | auto result = m_functions.add(nativeFunctionDeclaration.name(), Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>()); |
97 | result.iterator->value.append(nativeFunctionDeclaration); |
98 | return true; |
99 | } |
100 | |
101 | bool NameContext::add(AST::NativeTypeDeclaration& nativeTypeDeclaration) |
102 | { |
103 | if (m_functions.find(nativeTypeDeclaration.name()) != m_functions.end() |
104 | || m_variables.find(nativeTypeDeclaration.name()) != m_variables.end()) |
105 | return false; |
106 | auto result = m_types.add(nativeTypeDeclaration.name(), Vector<std::reference_wrapper<AST::NamedType>, 1>()); |
107 | result.iterator->value.append(nativeTypeDeclaration); |
108 | return true; |
109 | } |
110 | |
111 | bool NameContext::add(AST::VariableDeclaration& variableDeclaration) |
112 | { |
113 | if (variableDeclaration.name().isNull()) |
114 | return true; |
115 | if (exists(variableDeclaration.name())) |
116 | return false; |
117 | auto result = m_variables.add(String(variableDeclaration.name()), &variableDeclaration); |
118 | return result.isNewEntry; |
119 | } |
120 | |
121 | Vector<std::reference_wrapper<AST::NamedType>, 1>* NameContext::getTypes(const String& name) |
122 | { |
123 | auto iterator = m_types.find(name); |
124 | if (iterator == m_types.end()) { |
125 | if (m_parent) |
126 | return m_parent->getTypes(name); |
127 | return nullptr; |
128 | } |
129 | return &iterator->value; |
130 | } |
131 | |
132 | Vector<std::reference_wrapper<AST::FunctionDeclaration>, 1>* NameContext::getFunctions(const String& name) |
133 | { |
134 | auto iterator = m_functions.find(name); |
135 | if (iterator == m_functions.end()) { |
136 | if (m_parent) |
137 | return m_parent->getFunctions(name); |
138 | return nullptr; |
139 | } |
140 | return &iterator->value; |
141 | } |
142 | |
143 | AST::VariableDeclaration* NameContext::getVariable(const String& name) |
144 | { |
145 | auto iterator = m_variables.find(name); |
146 | if (iterator == m_variables.end()) { |
147 | if (m_parent) |
148 | return m_parent->getVariable(name); |
149 | return nullptr; |
150 | } |
151 | return iterator->value; |
152 | } |
153 | |
154 | bool NameContext::exists(String& name) |
155 | { |
156 | return m_types.find(name) != m_types.end() |
157 | || m_functions.find(name) != m_functions.end() |
158 | || m_variables.find(name) != m_variables.end(); |
159 | } |
160 | |
161 | } // namespace WHLSL |
162 | |
163 | } // namespace WebCore |
164 | |
165 | #endif // ENABLE(WEBGPU) |
166 | |