blob: ae4fcaa6c3fdc513d2dd316ea28317ab6f0fc580 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000012#if defined(_MSC_VER)
13#pragma warning(disable: 4718)
14#endif
15
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/SymbolTable.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000018#include <stdio.h>
kbr@chromium.org476541f2011-10-27 21:14:51 +000019#include <algorithm>
20
Jamie Madillbfa91f42014-06-05 15:45:18 -040021int TSymbolTable::uniqueIdCounter = 0;
Nicolas Capensbd10cf52013-06-20 09:51:51 -040022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000023//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024// Functions have buried pointers to delete.
25//
26TFunction::~TFunction()
27{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000028 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
29 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030}
31
32//
33// Symbol table levels are a map of pointers to symbols that have to be deleted.
34//
35TSymbolTableLevel::~TSymbolTableLevel()
36{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000037 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
38 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039}
40
Nicolas Capensadfffe42014-06-17 02:13:36 -040041bool TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040042{
Nicolas Capensadfffe42014-06-17 02:13:36 -040043 symbol->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madillbfa91f42014-06-05 15:45:18 -040044
45 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040046 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040047
48 return result.second;
49}
50
Jamie Madillbfa91f42014-06-05 15:45:18 -040051TSymbol *TSymbolTableLevel::find(const TString &name) const
52{
53 tLevel::const_iterator it = level.find(name);
54 if (it == level.end())
55 return 0;
56 else
57 return (*it).second;
58}
59
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000060//
61// Change all function entries in the table with the non-mangled name
62// to be related to the provided built-in operation. This is a low
63// performance operation, and only intended for symbol tables that
64// live across a large number of compiles.
65//
Zhenyao Mo9eedea02014-05-12 16:02:35 -070066void TSymbolTableLevel::relateToOperator(const char *name, TOperator op)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000067{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070068 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
69 {
70 if ((*it).second->isFunction())
71 {
72 TFunction *function = static_cast<TFunction*>((*it).second);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000073 if (function->getName() == name)
74 function->relateToOperator(op);
75 }
76 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077}
78
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000079//
80// Change all function entries in the table with the non-mangled name
81// to be related to the provided built-in extension. This is a low
82// performance operation, and only intended for symbol tables that
83// live across a large number of compiles.
84//
Zhenyao Mo9eedea02014-05-12 16:02:35 -070085void TSymbolTableLevel::relateToExtension(const char *name, const TString &ext)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000086{
Zhenyao Mo9eedea02014-05-12 16:02:35 -070087 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
88 {
89 TSymbol *symbol = it->second;
90 if (symbol->getName() == name)
Jamie Madill2aeb26a2013-07-08 14:02:55 -040091 symbol->relateToExtension(ext);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +000092 }
93}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094
Zhenyao Mo9eedea02014-05-12 16:02:35 -070095TSymbol::TSymbol(const TSymbol &copyOf)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000097 name = NewPoolTString(copyOf.name->c_str());
98 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099}
100
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000101TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope)
102{
103 int level = currentLevel();
104 TSymbol *symbol;
105
106 do
107 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700108 if (level == ESSL3_BUILTINS && shaderVersion != 300)
109 level--;
110 if (level == ESSL1_BUILTINS && shaderVersion != 100)
111 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000112
113 symbol = table[level]->find(name);
114 }
115 while (symbol == 0 && --level >= 0);
116
117 if (builtIn)
118 *builtIn = (level <= LAST_BUILTIN_LEVEL);
119 if (sameScope)
120 *sameScope = (level == currentLevel());
121
122 return symbol;
123}
124
125TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion)
126{
127 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
128 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700129 if (level == ESSL3_BUILTINS && shaderVersion != 300)
130 level--;
131 if (level == ESSL1_BUILTINS && shaderVersion != 100)
132 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000133
134 TSymbol *symbol = table[level]->find(name);
135
136 if (symbol)
137 return symbol;
138 }
139
140 return 0;
141}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400142
143TSymbolTable::~TSymbolTable()
144{
145 while (table.size() > 0)
146 pop();
147}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700148
149void TSymbolTable::insertBuiltIn(
150 ESymbolLevel level, TType *rvalue, const char *name,
151 TType *ptype1, TType *ptype2, TType *ptype3, TType *ptype4, TType *ptype5)
152{
153 if (ptype1->getBasicType() == EbtGSampler2D)
154 {
155 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
156 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
157 new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
158 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
159 new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
160 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
161 new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
162 return;
163 }
164 if (ptype1->getBasicType() == EbtGSampler3D)
165 {
166 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
167 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
168 new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
169 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
170 new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
171 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
172 new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
173 return;
174 }
175 if (ptype1->getBasicType() == EbtGSamplerCube)
176 {
177 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
178 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
179 new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
180 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
181 new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
182 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
183 new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
184 return;
185 }
186 if (ptype1->getBasicType() == EbtGSampler2DArray)
187 {
188 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
189 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
190 new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
191 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
192 new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
193 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
194 new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
195 return;
196 }
197
198 TFunction *function = new TFunction(NewPoolTString(name), *rvalue);
199
200 TType *types[] = {ptype1, ptype2, ptype3, ptype4, ptype5};
201 for (size_t ii = 0; ii < sizeof(types) / sizeof(types[0]); ++ii)
202 {
203 if (types[ii])
204 {
205 TParameter param = {NULL, types[ii]};
206 function->addParameter(param);
207 }
208 }
209
Nicolas Capensadfffe42014-06-17 02:13:36 -0400210 insert(level, function);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700211}
212
213TPrecision TSymbolTable::getDefaultPrecision(TBasicType type)
214{
215 if (!SupportsPrecision(type))
216 return EbpUndefined;
217
218 // unsigned integers use the same precision as signed
219 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
220
221 int level = static_cast<int>(precisionStack.size()) - 1;
222 assert(level >= 0); // Just to be safe. Should not happen.
223 // If we dont find anything we return this. Should we error check this?
224 TPrecision prec = EbpUndefined;
225 while (level >= 0)
226 {
227 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
228 if (it != precisionStack[level]->end())
229 {
230 prec = (*it).second;
231 break;
232 }
233 level--;
234 }
235 return prec;
236}