blob: 2a285fc4136b4be1b1c945f11b68c1727c429dc4 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04002//
Nicolas Capens0bac2852016-05-07 06:09:58 -04003// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
John Bauman66b8ab22014-05-06 15:57:45 -04006//
Nicolas Capens0bac2852016-05-07 06:09:58 -04007// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
John Bauman66b8ab22014-05-06 15:57:45 -040014
15//
16// Symbol table for parsing. Most functionaliy and main ideas
17// are documented in the header file.
18//
19
20#if defined(_MSC_VER)
21#pragma warning(disable: 4718)
22#endif
23
Nicolas Capenscc863da2015-01-21 15:50:55 -050024#include "SymbolTable.h"
John Bauman66b8ab22014-05-06 15:57:45 -040025
26#include <stdio.h>
Nicolas Capens94352d92015-06-11 08:53:40 -050027#include <limits.h>
John Bauman66b8ab22014-05-06 15:57:45 -040028#include <algorithm>
29
Nicolas Capens0b7471a2018-01-09 21:39:21 -050030#if defined(_MSC_VER) && MSC_VER < 1900
31#define snprintf _snprintf
John Bauman66b8ab22014-05-06 15:57:45 -040032#endif
33
John Baumand4ae8632014-05-06 16:18:33 -040034int TSymbolTableLevel::uniqueId = 0;
35
John Bauman66b8ab22014-05-06 15:57:45 -040036TType::TType(const TPublicType &p) :
Nicolas Capensbb2bcae2018-02-05 11:12:10 -050037 type(p.type), precision(p.precision), qualifier(p.qualifier), layoutQualifier(p.layoutQualifier),
Nicolas Capens0bac2852016-05-07 06:09:58 -040038 primarySize(p.primarySize), secondarySize(p.secondarySize), array(p.array), arraySize(p.arraySize), maxArraySize(0),
Nicolas Capensbb2bcae2018-02-05 11:12:10 -050039 arrayInformationType(0), interfaceBlock(0), structure(0), mangled(0)
John Bauman66b8ab22014-05-06 15:57:45 -040040{
Nicolas Capens0bac2852016-05-07 06:09:58 -040041 if (p.userDef)
42 {
43 structure = p.userDef->getStruct();
Nicolas Capens0bac2852016-05-07 06:09:58 -040044 }
John Bauman66b8ab22014-05-06 15:57:45 -040045}
46
47//
48// Recursively generate mangled names.
49//
50void TType::buildMangledName(TString& mangledName)
51{
Nicolas Capens0bac2852016-05-07 06:09:58 -040052 if (isMatrix())
53 mangledName += 'm';
54 else if (isVector())
55 mangledName += 'v';
John Bauman66b8ab22014-05-06 15:57:45 -040056
Nicolas Capens0bac2852016-05-07 06:09:58 -040057 switch (type) {
58 case EbtFloat: mangledName += 'f'; break;
59 case EbtInt: mangledName += 'i'; break;
60 case EbtUInt: mangledName += 'u'; break;
61 case EbtBool: mangledName += 'b'; break;
62 case EbtSampler2D: mangledName += "s2"; break;
Alexis Hetub027aa92015-01-19 15:56:12 -050063 case EbtSampler3D: mangledName += "s3"; break;
Alexis Hetua8b364b2015-06-10 11:48:40 -040064 case EbtSamplerCube: mangledName += "sC"; break;
65 case EbtSampler2DArray: mangledName += "s2a"; break;
Alexis Hetu46768622018-01-16 22:09:28 -050066 case EbtSampler2DRect: mangledName += "s2r"; break;
Alexis Hetua8b364b2015-06-10 11:48:40 -040067 case EbtSamplerExternalOES: mangledName += "sext"; break;
68 case EbtISampler2D: mangledName += "is2"; break;
69 case EbtISampler3D: mangledName += "is3"; break;
70 case EbtISamplerCube: mangledName += "isC"; break;
71 case EbtISampler2DArray: mangledName += "is2a"; break;
72 case EbtUSampler2D: mangledName += "us2"; break;
73 case EbtUSampler3D: mangledName += "us3"; break;
74 case EbtUSamplerCube: mangledName += "usC"; break;
75 case EbtUSampler2DArray: mangledName += "us2a"; break;
76 case EbtSampler2DShadow: mangledName += "s2s"; break;
77 case EbtSamplerCubeShadow: mangledName += "sCs"; break;
78 case EbtSampler2DArrayShadow: mangledName += "s2as"; break;
79 case EbtStruct: mangledName += structure->mangledName(); break;
80 case EbtInterfaceBlock: mangledName += interfaceBlock->mangledName(); break;
Nicolas Capens0bac2852016-05-07 06:09:58 -040081 default:
82 break;
83 }
John Bauman66b8ab22014-05-06 15:57:45 -040084
Nicolas Capens0bac2852016-05-07 06:09:58 -040085 mangledName += static_cast<char>('0' + getNominalSize());
86 if(isMatrix()) {
87 mangledName += static_cast<char>('0' + getSecondarySize());
88 }
89 if (isArray()) {
90 char buf[20];
91 snprintf(buf, sizeof(buf), "%d", arraySize);
92 mangledName += '[';
93 mangledName += buf;
94 mangledName += ']';
95 }
John Bauman66b8ab22014-05-06 15:57:45 -040096}
97
Alexis Hetuab752792016-04-21 16:11:31 -040098size_t TType::getStructSize() const
John Bauman66b8ab22014-05-06 15:57:45 -040099{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400100 if (!getStruct()) {
101 assert(false && "Not a struct");
102 return 0;
103 }
John Bauman66b8ab22014-05-06 15:57:45 -0400104
Nicolas Capens0bac2852016-05-07 06:09:58 -0400105 return getStruct()->objectSize();
John Bauman66b8ab22014-05-06 15:57:45 -0400106}
107
Alexis Hetua8b364b2015-06-10 11:48:40 -0400108bool TStructure::containsArrays() const
John Bauman66b8ab22014-05-06 15:57:45 -0400109{
Alexis Hetud2742532018-01-23 16:53:41 -0500110 for(const auto& field : *mFields)
Alexis Hetua8b364b2015-06-10 11:48:40 -0400111 {
Alexis Hetud2742532018-01-23 16:53:41 -0500112 const TType *fieldType = field->type();
Alexis Hetua8b364b2015-06-10 11:48:40 -0400113 if(fieldType->isArray() || fieldType->isStructureContainingArrays())
114 return true;
115 }
116 return false;
117}
John Bauman66b8ab22014-05-06 15:57:45 -0400118
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500119bool TStructure::containsType(TBasicType type) const
120{
Alexis Hetud2742532018-01-23 16:53:41 -0500121 for(const auto& field : *mFields)
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500122 {
Alexis Hetud2742532018-01-23 16:53:41 -0500123 const TType *fieldType = field->type();
Alexis Hetuec93b1d2016-12-09 16:01:29 -0500124 if(fieldType->getBasicType() == type || fieldType->isStructureContainingType(type))
125 return true;
126 }
127 return false;
128}
129
Alexis Hetua8b364b2015-06-10 11:48:40 -0400130bool TStructure::containsSamplers() const
131{
Alexis Hetud2742532018-01-23 16:53:41 -0500132 for(const auto& field : *mFields)
Alexis Hetua8b364b2015-06-10 11:48:40 -0400133 {
Alexis Hetud2742532018-01-23 16:53:41 -0500134 const TType *fieldType = field->type();
Alexis Hetua8b364b2015-06-10 11:48:40 -0400135 if(IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
136 return true;
137 }
138 return false;
139}
John Bauman66b8ab22014-05-06 15:57:45 -0400140
Alexis Hetud2742532018-01-23 16:53:41 -0500141void TStructure::setMatrixPackingIfUnspecified(TLayoutMatrixPacking matrixPacking)
142{
143 for(auto& field : *mFields)
144 {
145 field->type()->setMatrixPackingIfUnspecified(matrixPacking);
146 }
147}
148
Alexis Hetua8b364b2015-06-10 11:48:40 -0400149TString TFieldListCollection::buildMangledName() const
150{
151 TString mangledName(mangledNamePrefix());
152 mangledName += *mName;
Alexis Hetud2742532018-01-23 16:53:41 -0500153 for(const auto& field : *mFields)
Alexis Hetua8b364b2015-06-10 11:48:40 -0400154 {
155 mangledName += '-';
Alexis Hetud2742532018-01-23 16:53:41 -0500156 mangledName += field->type()->getMangledName();
Alexis Hetua8b364b2015-06-10 11:48:40 -0400157 }
158 return mangledName;
159}
160
161size_t TFieldListCollection::calculateObjectSize() const
162{
163 size_t size = 0;
Alexis Hetud2742532018-01-23 16:53:41 -0500164 for(const auto& field : *mFields)
Alexis Hetua8b364b2015-06-10 11:48:40 -0400165 {
Alexis Hetud2742532018-01-23 16:53:41 -0500166 size_t fieldSize = field->type()->getObjectSize();
Alexis Hetua8b364b2015-06-10 11:48:40 -0400167 if(fieldSize > INT_MAX - size)
168 size = INT_MAX;
169 else
170 size += fieldSize;
171 }
172 return size;
173}
174
175int TStructure::calculateDeepestNesting() const
176{
177 int maxNesting = 0;
Alexis Hetud2742532018-01-23 16:53:41 -0500178 for(const auto& field : *mFields)
179 maxNesting = std::max(maxNesting, field->type()->getDeepestStructNesting());
Alexis Hetua8b364b2015-06-10 11:48:40 -0400180 return 1 + maxNesting;
John Bauman66b8ab22014-05-06 15:57:45 -0400181}
182
183//
John Bauman66b8ab22014-05-06 15:57:45 -0400184// Functions have buried pointers to delete.
185//
186TFunction::~TFunction()
187{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400188 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
189 delete (*i).type;
John Bauman66b8ab22014-05-06 15:57:45 -0400190}
191
192//
193// Symbol table levels are a map of pointers to symbols that have to be deleted.
194//
195TSymbolTableLevel::~TSymbolTableLevel()
196{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400197 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
198 delete (*it).second;
John Bauman66b8ab22014-05-06 15:57:45 -0400199}
200
Nicolas Capens0b7471a2018-01-09 21:39:21 -0500201bool TSymbolTableLevel::insert(TSymbol *symbol)
202{
203 symbol->setUniqueId(nextUniqueId());
204
205 // returning true means symbol was added to the table
206 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
207
208 return result.second;
209}
210
211bool TSymbolTableLevel::insertUnmangled(TFunction *function)
212{
213 function->setUniqueId(nextUniqueId());
214
215 // returning true means symbol was added to the table
216 tInsertResult result = level.insert(tLevelPair(function->getName(), function));
217
218 return result.second;
219}
220
221TSymbol *TSymbolTableLevel::find(const TString &name) const
222{
223 tLevel::const_iterator it = level.find(name);
224 if (it == level.end())
225 return 0;
226 else
227 return (*it).second;
228}
229
Nicolas Capense2858652015-02-18 15:30:51 -0500230TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope) const
231{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400232 int level = currentLevel();
233 TSymbol *symbol = nullptr;
Nicolas Capense2858652015-02-18 15:30:51 -0500234
Nicolas Capens0bac2852016-05-07 06:09:58 -0400235 do
236 {
237 while((level == ESSL3_BUILTINS && shaderVersion != 300) ||
238 (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels
239 {
240 --level;
241 }
Nicolas Capense2858652015-02-18 15:30:51 -0500242
Nicolas Capens0bac2852016-05-07 06:09:58 -0400243 symbol = table[level]->find(name);
244 }
245 while(!symbol && --level >= 0); // Doesn't decrement level when a symbol was found
Nicolas Capense2858652015-02-18 15:30:51 -0500246
Nicolas Capens0bac2852016-05-07 06:09:58 -0400247 if(builtIn)
248 {
249 *builtIn = (level <= LAST_BUILTIN_LEVEL);
250 }
Nicolas Capense2858652015-02-18 15:30:51 -0500251
Nicolas Capens0bac2852016-05-07 06:09:58 -0400252 if(sameScope)
253 {
254 *sameScope = (level == currentLevel());
255 }
Nicolas Capense2858652015-02-18 15:30:51 -0500256
Nicolas Capens0bac2852016-05-07 06:09:58 -0400257 return symbol;
Nicolas Capense2858652015-02-18 15:30:51 -0500258}
259
260TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const
261{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400262 for(int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
263 {
264 while((level == ESSL3_BUILTINS && shaderVersion != 300) ||
265 (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels
266 {
267 --level;
268 }
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500269
Nicolas Capens0bac2852016-05-07 06:09:58 -0400270 TSymbol *symbol = table[level]->find(name);
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500271
Nicolas Capens0bac2852016-05-07 06:09:58 -0400272 if(symbol)
273 {
274 return symbol;
275 }
276 }
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500277
Nicolas Capens0bac2852016-05-07 06:09:58 -0400278 return 0;
Nicolas Capense2858652015-02-18 15:30:51 -0500279}
280
John Bauman66b8ab22014-05-06 15:57:45 -0400281TSymbol::TSymbol(const TSymbol& copyOf)
282{
Nicolas Capens0bac2852016-05-07 06:09:58 -0400283 name = NewPoolTString(copyOf.name->c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400284}