blob: d9f3ed31f132e588bb7a13944bd1820cb170e15b [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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000016#include "compiler/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
alokp@chromium.org79fb1012012-04-26 21:07:39 +000021#include "common/angleutils.h"
22
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +000023TType::TType(const TPublicType &p) :
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +000024 type(p.type), precision(p.precision), qualifier(p.qualifier), primarySize(p.primarySize), secondarySize(p.secondarySize), array(p.array), arraySize(p.arraySize),
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000025 maxArraySize(0), arrayInformationType(0), interfaceBlockType(0), structure(0), structureSize(0), deepestStructNesting(0), fieldName(0), mangled(0), typeName(0)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +000026{
27 if (p.userDef) {
28 structure = p.userDef->getStruct();
29 typeName = NewPoolTString(p.userDef->getTypeName().c_str());
30 computeDeepestStructNesting();
31 }
32}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033
34//
35// Recursively generate mangled names.
36//
37void TType::buildMangledName(TString& mangledName)
38{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000039 if (isMatrix())
40 mangledName += 'm';
41 else if (isVector())
42 mangledName += 'v';
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
daniel@transgaming.com0578f812010-05-17 09:58:39 +000044 switch (type) {
45 case EbtFloat: mangledName += 'f'; break;
46 case EbtInt: mangledName += 'i'; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +000047 case EbtUInt: mangledName += 'u'; break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000048 case EbtBool: mangledName += 'b'; break;
49 case EbtSampler2D: mangledName += "s2"; break;
50 case EbtSamplerCube: mangledName += "sC"; break;
51 case EbtStruct:
52 mangledName += "struct-";
53 if (typeName)
54 mangledName += *typeName;
55 {// support MSVC++6.0
56 for (unsigned int i = 0; i < structure->size(); ++i) {
57 mangledName += '-';
58 (*structure)[i].type->buildMangledName(mangledName);
59 }
60 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +000061 break;
62 case EbtInterfaceBlock:
63 {
64 mangledName += "interface-block-";
65 if (typeName)
66 {
67 mangledName += *typeName;
68 }
69 for (unsigned int i = 0; i < structure->size(); ++i)
70 {
71 mangledName += '-';
72 (*structure)[i].type->buildMangledName(mangledName);
73 }
74 }
75 break;
daniel@transgaming.com0578f812010-05-17 09:58:39 +000076 default:
77 break;
78 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +000080 if (isMatrix())
81 {
82 mangledName += static_cast<char>('0' + getCols());
83 mangledName += static_cast<char>('x');
84 mangledName += static_cast<char>('0' + getRows());
85 }
86 else
87 {
88 mangledName += static_cast<char>('0' + getNominalSize());
89 }
daniel@transgaming.com0578f812010-05-17 09:58:39 +000090 if (isArray()) {
91 char buf[20];
kbr@chromium.orgddb6e8e2012-04-25 00:48:13 +000092 snprintf(buf, sizeof(buf), "%d", arraySize);
daniel@transgaming.com0578f812010-05-17 09:58:39 +000093 mangledName += '[';
94 mangledName += buf;
95 mangledName += ']';
96 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097}
98
99int TType::getStructSize() const
100{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000101 if (!getStruct()) {
102 assert(false && "Not a struct");
103 return 0;
104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000106 if (structureSize == 0)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000107 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000108 structureSize += ((*tl).type)->getObjectSize();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000110 return structureSize;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111}
112
kbr@chromium.org476541f2011-10-27 21:14:51 +0000113void TType::computeDeepestStructNesting()
114{
115 if (!getStruct()) {
116 return;
117 }
118
119 int maxNesting = 0;
120 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); ++tl) {
121 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
122 }
123
124 deepestStructNesting = 1 + maxNesting;
125}
126
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000127bool TType::isStructureContainingArrays() const
128{
129 if (!structure)
130 {
131 return false;
132 }
133
134 for (TTypeList::const_iterator member = structure->begin(); member != structure->end(); member++)
135 {
136 if (member->type->isArray() ||
137 member->type->isStructureContainingArrays())
138 {
139 return true;
140 }
141 }
142
143 return false;
144}
145
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147// Functions have buried pointers to delete.
148//
149TFunction::~TFunction()
150{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000151 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
152 delete (*i).type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153}
154
155//
156// Symbol table levels are a map of pointers to symbols that have to be deleted.
157//
158TSymbolTableLevel::~TSymbolTableLevel()
159{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000160 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
161 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000162}
163
164//
165// Change all function entries in the table with the non-mangled name
166// to be related to the provided built-in operation. This is a low
167// performance operation, and only intended for symbol tables that
168// live across a large number of compiles.
169//
170void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
171{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000172 tLevel::iterator it;
173 for (it = level.begin(); it != level.end(); ++it) {
174 if ((*it).second->isFunction()) {
175 TFunction* function = static_cast<TFunction*>((*it).second);
176 if (function->getName() == name)
177 function->relateToOperator(op);
178 }
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180}
181
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000182//
183// Change all function entries in the table with the non-mangled name
184// to be related to the provided built-in extension. This is a low
185// performance operation, and only intended for symbol tables that
186// live across a large number of compiles.
187//
188void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
189{
190 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
191 if (it->second->isFunction()) {
192 TFunction* function = static_cast<TFunction*>(it->second);
193 if (function->getName() == name)
194 function->relateToExtension(ext);
195 }
196 }
197}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198
199TSymbol::TSymbol(const TSymbol& copyOf)
200{
daniel@transgaming.com0578f812010-05-17 09:58:39 +0000201 name = NewPoolTString(copyOf.name->c_str());
202 uniqueId = copyOf.uniqueId;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203}
204
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000205TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope)
206{
207 int level = currentLevel();
208 TSymbol *symbol;
209
210 do
211 {
212 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
213 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
214
215 symbol = table[level]->find(name);
216 }
217 while (symbol == 0 && --level >= 0);
218
219 if (builtIn)
220 *builtIn = (level <= LAST_BUILTIN_LEVEL);
221 if (sameScope)
222 *sameScope = (level == currentLevel());
223
224 return symbol;
225}
226
227TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion)
228{
229 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
230 {
231 if (level == ESSL3_BUILTINS && shaderVersion != 300) level--;
232 if (level == ESSL1_BUILTINS && shaderVersion != 100) level--;
233
234 TSymbol *symbol = table[level]->find(name);
235
236 if (symbol)
237 return symbol;
238 }
239
240 return 0;
241}