blob: ad3488e6f87443839d7bb179c38868837142a5d9 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Symbol table for parsing. Most functionaliy and main ideas
9// are documented in the header file.
10//
11
12#if defined(_MSC_VER)
13#pragma warning(disable: 4718)
14#endif
15
Nicolas Capenscc863da2015-01-21 15:50:55 -050016#include "SymbolTable.h"
John Bauman66b8ab22014-05-06 15:57:45 -040017
18#include <stdio.h>
Nicolas Capens94352d92015-06-11 08:53:40 -050019#include <limits.h>
John Bauman66b8ab22014-05-06 15:57:45 -040020#include <algorithm>
21
22#if defined(_MSC_VER)
23#define snprintf _snprintf
24#endif
25
John Baumand4ae8632014-05-06 16:18:33 -040026int TSymbolTableLevel::uniqueId = 0;
27
John Bauman66b8ab22014-05-06 15:57:45 -040028TType::TType(const TPublicType &p) :
Alexis Hetub14178b2015-04-13 13:23:20 -040029 type(p.type), precision(p.precision), primarySize(p.primarySize), secondarySize(p.secondarySize), qualifier(p.qualifier), array(p.array), arraySize(p.arraySize),
Alexis Hetua8b364b2015-06-10 11:48:40 -040030 maxArraySize(0), arrayInformationType(0), structure(0), deepestStructNesting(0), mangled(0)
John Bauman66b8ab22014-05-06 15:57:45 -040031{
32 if (p.userDef)
33 {
34 structure = p.userDef->getStruct();
John Bauman66b8ab22014-05-06 15:57:45 -040035 computeDeepestStructNesting();
36 }
37}
38
39//
40// Recursively generate mangled names.
41//
42void TType::buildMangledName(TString& mangledName)
43{
44 if (isMatrix())
45 mangledName += 'm';
46 else if (isVector())
47 mangledName += 'v';
48
49 switch (type) {
50 case EbtFloat: mangledName += 'f'; break;
51 case EbtInt: mangledName += 'i'; break;
Nicolas Capens3c20f802015-02-17 17:17:20 -050052 case EbtUInt: mangledName += 'u'; break;
John Bauman66b8ab22014-05-06 15:57:45 -040053 case EbtBool: mangledName += 'b'; break;
54 case EbtSampler2D: mangledName += "s2"; break;
Alexis Hetub027aa92015-01-19 15:56:12 -050055 case EbtSampler3D: mangledName += "s3"; break;
Alexis Hetua8b364b2015-06-10 11:48:40 -040056 case EbtSamplerCube: mangledName += "sC"; break;
57 case EbtSampler2DArray: mangledName += "s2a"; break;
58 case EbtSamplerExternalOES: mangledName += "sext"; break;
59 case EbtISampler2D: mangledName += "is2"; break;
60 case EbtISampler3D: mangledName += "is3"; break;
61 case EbtISamplerCube: mangledName += "isC"; break;
62 case EbtISampler2DArray: mangledName += "is2a"; break;
63 case EbtUSampler2D: mangledName += "us2"; break;
64 case EbtUSampler3D: mangledName += "us3"; break;
65 case EbtUSamplerCube: mangledName += "usC"; break;
66 case EbtUSampler2DArray: mangledName += "us2a"; break;
67 case EbtSampler2DShadow: mangledName += "s2s"; break;
68 case EbtSamplerCubeShadow: mangledName += "sCs"; break;
69 case EbtSampler2DArrayShadow: mangledName += "s2as"; break;
70 case EbtStruct: mangledName += structure->mangledName(); break;
71 case EbtInterfaceBlock: mangledName += interfaceBlock->mangledName(); break;
John Bauman66b8ab22014-05-06 15:57:45 -040072 default:
73 break;
74 }
75
76 mangledName += static_cast<char>('0' + getNominalSize());
Alexis Hetu39358652015-05-26 14:34:54 -040077 if(isMatrix()) {
78 mangledName += static_cast<char>('0' + getSecondarySize());
79 }
John Bauman66b8ab22014-05-06 15:57:45 -040080 if (isArray()) {
81 char buf[20];
82 snprintf(buf, sizeof(buf), "%d", arraySize);
83 mangledName += '[';
84 mangledName += buf;
85 mangledName += ']';
86 }
87}
88
89int TType::getStructSize() const
90{
91 if (!getStruct()) {
92 assert(false && "Not a struct");
93 return 0;
94 }
95
Alexis Hetua8b364b2015-06-10 11:48:40 -040096 return getStruct()->objectSize();
John Bauman66b8ab22014-05-06 15:57:45 -040097}
98
99void TType::computeDeepestStructNesting()
100{
Alexis Hetua8b364b2015-06-10 11:48:40 -0400101 deepestStructNesting = structure ? structure->deepestNesting() : 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400102}
103
Alexis Hetua8b364b2015-06-10 11:48:40 -0400104bool TStructure::containsArrays() const
John Bauman66b8ab22014-05-06 15:57:45 -0400105{
Alexis Hetua8b364b2015-06-10 11:48:40 -0400106 for(size_t i = 0; i < mFields->size(); ++i)
107 {
108 const TType *fieldType = (*mFields)[i]->type();
109 if(fieldType->isArray() || fieldType->isStructureContainingArrays())
110 return true;
111 }
112 return false;
113}
John Bauman66b8ab22014-05-06 15:57:45 -0400114
Alexis Hetua8b364b2015-06-10 11:48:40 -0400115bool TStructure::containsSamplers() const
116{
117 for(size_t i = 0; i < mFields->size(); ++i)
118 {
119 const TType *fieldType = (*mFields)[i]->type();
120 if(IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers())
121 return true;
122 }
123 return false;
124}
John Bauman66b8ab22014-05-06 15:57:45 -0400125
Alexis Hetua8b364b2015-06-10 11:48:40 -0400126TString TFieldListCollection::buildMangledName() const
127{
128 TString mangledName(mangledNamePrefix());
129 mangledName += *mName;
130 for(size_t i = 0; i < mFields->size(); ++i)
131 {
132 mangledName += '-';
133 mangledName += (*mFields)[i]->type()->getMangledName();
134 }
135 return mangledName;
136}
137
138size_t TFieldListCollection::calculateObjectSize() const
139{
140 size_t size = 0;
141 for(size_t i = 0; i < mFields->size(); ++i)
142 {
143 size_t fieldSize = (*mFields)[i]->type()->getObjectSize();
144 if(fieldSize > INT_MAX - size)
145 size = INT_MAX;
146 else
147 size += fieldSize;
148 }
149 return size;
150}
151
152int TStructure::calculateDeepestNesting() const
153{
154 int maxNesting = 0;
155 for(size_t i = 0; i < mFields->size(); ++i)
156 maxNesting = std::max(maxNesting, (*mFields)[i]->type()->getDeepestStructNesting());
157 return 1 + maxNesting;
John Bauman66b8ab22014-05-06 15:57:45 -0400158}
159
160//
John Bauman66b8ab22014-05-06 15:57:45 -0400161// Functions have buried pointers to delete.
162//
163TFunction::~TFunction()
164{
165 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
166 delete (*i).type;
167}
168
169//
170// Symbol table levels are a map of pointers to symbols that have to be deleted.
171//
172TSymbolTableLevel::~TSymbolTableLevel()
173{
174 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
175 delete (*it).second;
176}
177
Nicolas Capense2858652015-02-18 15:30:51 -0500178TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope) const
179{
180 int level = currentLevel();
181 TSymbol *symbol = nullptr;
182
183 do
184 {
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500185 while((level == ESSL3_BUILTINS && shaderVersion != 300) ||
186 (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels
187 {
188 --level;
189 }
Nicolas Capense2858652015-02-18 15:30:51 -0500190
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500191 symbol = table[level]->find(name);
192 }
193 while(!symbol && --level >= 0); // Doesn't decrement level when a symbol was found
Nicolas Capense2858652015-02-18 15:30:51 -0500194
195 if(builtIn)
196 {
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500197 *builtIn = (level <= LAST_BUILTIN_LEVEL);
Nicolas Capense2858652015-02-18 15:30:51 -0500198 }
199
200 if(sameScope)
201 {
202 *sameScope = (level == currentLevel());
203 }
204
205 return symbol;
206}
207
208TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const
209{
Nicolas Capens3d7f6ed2015-02-18 16:34:50 -0500210 for(int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
211 {
212 while((level == ESSL3_BUILTINS && shaderVersion != 300) ||
213 (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels
214 {
215 --level;
216 }
217
218 TSymbol *symbol = table[level]->find(name);
219
220 if(symbol)
221 {
222 return symbol;
223 }
224 }
225
226 return 0;
Nicolas Capense2858652015-02-18 15:30:51 -0500227}
228
John Bauman66b8ab22014-05-06 15:57:45 -0400229TSymbol::TSymbol(const TSymbol& copyOf)
230{
231 name = NewPoolTString(copyOf.name->c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400232}