blob: d756f3ec3bd050161b6189688de649749eb6bb88 [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 Moe740add2014-07-18 17:01:01 -070095TSymbol *TSymbolTable::find(const TString &name, int shaderVersion,
96 bool *builtIn, bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +000097{
98 int level = currentLevel();
99 TSymbol *symbol;
100
101 do
102 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700103 if (level == ESSL3_BUILTINS && shaderVersion != 300)
104 level--;
105 if (level == ESSL1_BUILTINS && shaderVersion != 100)
106 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000107
108 symbol = table[level]->find(name);
109 }
110 while (symbol == 0 && --level >= 0);
111
112 if (builtIn)
113 *builtIn = (level <= LAST_BUILTIN_LEVEL);
114 if (sameScope)
115 *sameScope = (level == currentLevel());
116
117 return symbol;
118}
119
Zhenyao Moe740add2014-07-18 17:01:01 -0700120TSymbol *TSymbolTable::findBuiltIn(
121 const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000122{
123 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
124 {
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700125 if (level == ESSL3_BUILTINS && shaderVersion != 300)
126 level--;
127 if (level == ESSL1_BUILTINS && shaderVersion != 100)
128 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000129
130 TSymbol *symbol = table[level]->find(name);
131
132 if (symbol)
133 return symbol;
134 }
135
136 return 0;
137}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400138
139TSymbolTable::~TSymbolTable()
140{
141 while (table.size() > 0)
142 pop();
143}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700144
145void TSymbolTable::insertBuiltIn(
146 ESymbolLevel level, TType *rvalue, const char *name,
147 TType *ptype1, TType *ptype2, TType *ptype3, TType *ptype4, TType *ptype5)
148{
149 if (ptype1->getBasicType() == EbtGSampler2D)
150 {
151 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
152 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
153 new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
154 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
155 new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
156 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
157 new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
158 return;
159 }
160 if (ptype1->getBasicType() == EbtGSampler3D)
161 {
162 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
163 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
164 new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
165 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
166 new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
167 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
168 new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
169 return;
170 }
171 if (ptype1->getBasicType() == EbtGSamplerCube)
172 {
173 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
174 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
175 new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
176 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
177 new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
178 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
179 new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
180 return;
181 }
182 if (ptype1->getBasicType() == EbtGSampler2DArray)
183 {
184 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
185 insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name,
186 new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
187 insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name,
188 new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
189 insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name,
190 new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
191 return;
192 }
193
194 TFunction *function = new TFunction(NewPoolTString(name), *rvalue);
195
196 TType *types[] = {ptype1, ptype2, ptype3, ptype4, ptype5};
197 for (size_t ii = 0; ii < sizeof(types) / sizeof(types[0]); ++ii)
198 {
199 if (types[ii])
200 {
201 TParameter param = {NULL, types[ii]};
202 function->addParameter(param);
203 }
204 }
205
Nicolas Capensadfffe42014-06-17 02:13:36 -0400206 insert(level, function);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700207}
208
Zhenyao Moe740add2014-07-18 17:01:01 -0700209TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700210{
211 if (!SupportsPrecision(type))
212 return EbpUndefined;
213
214 // unsigned integers use the same precision as signed
215 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
216
217 int level = static_cast<int>(precisionStack.size()) - 1;
218 assert(level >= 0); // Just to be safe. Should not happen.
219 // If we dont find anything we return this. Should we error check this?
220 TPrecision prec = EbpUndefined;
221 while (level >= 0)
222 {
223 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
224 if (it != precisionStack[level]->end())
225 {
226 prec = (*it).second;
227 break;
228 }
229 level--;
230 }
231 return prec;
232}