blob: 873fda5e48d1d905fc0ded2a334b7ca13d7db5f0 [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>
19#include <algorithm>
20
21#if defined(_MSC_VER)
22#define snprintf _snprintf
23#endif
24
John Baumand4ae8632014-05-06 16:18:33 -040025int TSymbolTableLevel::uniqueId = 0;
26
John Bauman66b8ab22014-05-06 15:57:45 -040027TType::TType(const TPublicType &p) :
28 type(p.type), precision(p.precision), qualifier(p.qualifier), size(p.size), matrix(p.matrix), array(p.array), arraySize(p.arraySize),
29 maxArraySize(0), arrayInformationType(0), structure(0), structureSize(0), deepestStructNesting(0), fieldName(0), mangled(0), typeName(0)
30{
31 if (p.userDef)
32 {
33 structure = p.userDef->getStruct();
34 typeName = NewPoolTString(p.userDef->getTypeName().c_str());
35 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;
52 case EbtBool: mangledName += 'b'; break;
53 case EbtSampler2D: mangledName += "s2"; break;
54 case EbtSamplerCube: mangledName += "sC"; break;
Nicolas Capense9c5e4f2014-05-28 22:46:43 -040055 case EbtSamplerExternalOES: mangledName += "sE"; break;
Alexis Hetub027aa92015-01-19 15:56:12 -050056 case EbtSampler3D: mangledName += "s3"; break;
57 case EbtStruct:
John Bauman66b8ab22014-05-06 15:57:45 -040058 mangledName += "struct-";
59 if (typeName)
60 mangledName += *typeName;
61 {// support MSVC++6.0
62 for (unsigned int i = 0; i < structure->size(); ++i) {
63 mangledName += '-';
64 (*structure)[i].type->buildMangledName(mangledName);
65 }
66 }
67 default:
68 break;
69 }
70
71 mangledName += static_cast<char>('0' + getNominalSize());
72 if (isArray()) {
73 char buf[20];
74 snprintf(buf, sizeof(buf), "%d", arraySize);
75 mangledName += '[';
76 mangledName += buf;
77 mangledName += ']';
78 }
79}
80
81int TType::getStructSize() const
82{
83 if (!getStruct()) {
84 assert(false && "Not a struct");
85 return 0;
86 }
87
88 if (structureSize == 0)
89 for (TTypeList::const_iterator tl = getStruct()->begin(); tl != getStruct()->end(); tl++)
90 structureSize += ((*tl).type)->getObjectSize();
91
92 return structureSize;
93}
94
95void TType::computeDeepestStructNesting()
96{
97 if (!structure)
98 {
99 return;
100 }
101
102 int maxNesting = 0;
103 for (TTypeList::const_iterator tl = structure->begin(); tl != structure->end(); tl++)
104 {
105 maxNesting = std::max(maxNesting, ((*tl).type)->getDeepestStructNesting());
106 }
107
108 deepestStructNesting = 1 + maxNesting;
109}
110
111bool TType::isStructureContainingArrays() const
112{
113 if (!structure)
114 {
115 return false;
116 }
117
118 for (TTypeList::const_iterator member = structure->begin(); member != structure->end(); member++)
119 {
120 if (member->type->isArray() ||
121 member->type->isStructureContainingArrays())
122 {
123 return true;
124 }
125 }
126
127 return false;
128}
129
130//
John Bauman66b8ab22014-05-06 15:57:45 -0400131// Functions have buried pointers to delete.
132//
133TFunction::~TFunction()
134{
135 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
136 delete (*i).type;
137}
138
139//
140// Symbol table levels are a map of pointers to symbols that have to be deleted.
141//
142TSymbolTableLevel::~TSymbolTableLevel()
143{
144 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
145 delete (*it).second;
146}
147
148//
149// Change all function entries in the table with the non-mangled name
150// to be related to the provided built-in operation. This is a low
151// performance operation, and only intended for symbol tables that
152// live across a large number of compiles.
153//
154void TSymbolTableLevel::relateToOperator(const char* name, TOperator op)
155{
156 tLevel::iterator it;
157 for (it = level.begin(); it != level.end(); ++it) {
158 if ((*it).second->isFunction()) {
159 TFunction* function = static_cast<TFunction*>((*it).second);
160 if (function->getName() == name)
161 function->relateToOperator(op);
162 }
163 }
164}
165
166//
167// Change all function entries in the table with the non-mangled name
168// to be related to the provided built-in extension. This is a low
169// performance operation, and only intended for symbol tables that
170// live across a large number of compiles.
171//
172void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
173{
174 for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
175 if (it->second->isFunction()) {
176 TFunction* function = static_cast<TFunction*>(it->second);
177 if (function->getName() == name)
178 function->relateToExtension(ext);
179 }
180 }
181}
182
183TSymbol::TSymbol(const TSymbol& copyOf)
184{
185 name = NewPoolTString(copyOf.name->c_str());
John Bauman66b8ab22014-05-06 15:57:45 -0400186}