zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SymbolTable.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
3 // 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 
16 #include "compiler/SymbolTable.h"
17 
18 #include <stdio.h>
19 #include <algorithm>
20 #include <climits>
21 
23  type(p.type), precision(p.precision), qualifier(p.qualifier), size(p.size), matrix(p.matrix), array(p.array), arraySize(p.arraySize), structure(0)
24 {
25  if (p.userDef)
26  structure = p.userDef->getStruct();
27 }
28 
29 //
30 // Recursively generate mangled names.
31 //
32 TString TType::buildMangledName() const
33 {
34  TString mangledName;
35  if (isMatrix())
36  mangledName += 'm';
37  else if (isVector())
38  mangledName += 'v';
39 
40  switch (type) {
41  case EbtFloat: mangledName += 'f'; break;
42  case EbtInt: mangledName += 'i'; break;
43  case EbtBool: mangledName += 'b'; break;
44  case EbtSampler2D: mangledName += "s2"; break;
45  case EbtSamplerCube: mangledName += "sC"; break;
46  case EbtStruct: mangledName += structure->mangledName(); break;
47  default: break;
48  }
49 
50  mangledName += static_cast<char>('0' + getNominalSize());
51  if (isArray()) {
52  char buf[20];
53  snprintf(buf, sizeof(buf), "%d", arraySize);
54  mangledName += '[';
55  mangledName += buf;
56  mangledName += ']';
57  }
58  return mangledName;
59 }
60 
61 size_t TType::getObjectSize() const
62 {
63  size_t totalSize = 0;
64 
65  if (getBasicType() == EbtStruct)
66  totalSize = structure->objectSize();
67  else if (matrix)
68  totalSize = size * size;
69  else
70  totalSize = size;
71 
72  if (isArray()) {
73  size_t arraySize = getArraySize();
74  if (arraySize > INT_MAX / totalSize)
75  totalSize = INT_MAX;
76  else
77  totalSize *= arraySize;
78  }
79 
80  return totalSize;
81 }
82 
84 {
85  for (size_t i = 0; i < mFields->size(); ++i) {
86  const TType* fieldType = (*mFields)[i]->type();
87  if (fieldType->isArray() || fieldType->isStructureContainingArrays())
88  return true;
89  }
90  return false;
91 }
92 
93 TString TStructure::buildMangledName() const
94 {
95  TString mangledName("struct-");
96  mangledName += *mName;
97  for (size_t i = 0; i < mFields->size(); ++i) {
98  mangledName += '-';
99  mangledName += (*mFields)[i]->type()->getMangledName();
100  }
101  return mangledName;
102 }
103 
104 size_t TStructure::calculateObjectSize() const
105 {
106  size_t size = 0;
107  for (size_t i = 0; i < mFields->size(); ++i) {
108  size_t fieldSize = (*mFields)[i]->type()->getObjectSize();
109  if (fieldSize > INT_MAX - size)
110  size = INT_MAX;
111  else
112  size += fieldSize;
113  }
114  return size;
115 }
116 
117 int TStructure::calculateDeepestNesting() const
118 {
119  int maxNesting = 0;
120  for (size_t i = 0; i < mFields->size(); ++i) {
121  maxNesting = std::max(maxNesting, (*mFields)[i]->type()->getDeepestStructNesting());
122  }
123  return 1 + maxNesting;
124 }
125 
126 //
127 // Dump functions.
128 //
129 
130 void TVariable::dump(TInfoSink& infoSink) const
131 {
132  infoSink.debug << getName().c_str() << ": " << type.getQualifierString() << " " << type.getPrecisionString() << " " << type.getBasicString();
133  if (type.isArray()) {
134  infoSink.debug << "[0]";
135  }
136  infoSink.debug << "\n";
137 }
138 
139 void TFunction::dump(TInfoSink &infoSink) const
140 {
141  infoSink.debug << getName().c_str() << ": " << returnType.getBasicString() << " " << getMangledName().c_str() << "\n";
142 }
143 
144 void TSymbolTableLevel::dump(TInfoSink &infoSink) const
145 {
146  tLevel::const_iterator it;
147  for (it = level.begin(); it != level.end(); ++it)
148  (*it).second->dump(infoSink);
149 }
150 
151 void TSymbolTable::dump(TInfoSink &infoSink) const
152 {
153  for (int level = currentLevel(); level >= 0; --level) {
154  infoSink.debug << "LEVEL " << level << "\n";
155  table[level]->dump(infoSink);
156  }
157 }
158 
159 //
160 // Functions have buried pointers to delete.
161 //
163 {
164  for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
165  delete (*i).type;
166 }
167 
168 //
169 // Symbol table levels are a map of pointers to symbols that have to be deleted.
170 //
172 {
173  for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
174  delete (*it).second;
175 }
176 
177 //
178 // Change all function entries in the table with the non-mangled name
179 // to be related to the provided built-in operation. This is a low
180 // performance operation, and only intended for symbol tables that
181 // live across a large number of compiles.
182 //
184 {
185  tLevel::iterator it;
186  for (it = level.begin(); it != level.end(); ++it) {
187  if ((*it).second->isFunction()) {
188  TFunction* function = static_cast<TFunction*>((*it).second);
189  if (function->getName() == name)
190  function->relateToOperator(op);
191  }
192  }
193 }
194 
195 //
196 // Change all function entries in the table with the non-mangled name
197 // to be related to the provided built-in extension. This is a low
198 // performance operation, and only intended for symbol tables that
199 // live across a large number of compiles.
200 //
201 void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
202 {
203  for (tLevel::iterator it = level.begin(); it != level.end(); ++it) {
204  TSymbol* symbol = it->second;
205  if (symbol->getName() == name)
206  symbol->relateToExtension(ext);
207  }
208 }
virtual void dump(TInfoSink &infoSink) const
TOperator
Definition: intermediate.h:29
GLenum GLsizei GLenum GLenum const GLvoid * table
Definition: glew.h:4422
void relateToExtension(const char *name, const TString &ext)
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: gl2ext.h:845
void relateToOperator(TOperator o)
Definition: SymbolTable.h:160
virtual void dump(TInfoSink &infoSink) const
virtual ~TFunction()
TBasicType getBasicType() const
Definition: Types.h:108
int getNominalSize() const
Definition: Types.h:118
void relateToExtension(const TString &ext)
Definition: SymbolTable.h:55
EGLImageKHR EGLint * name
Definition: eglext.h:284
Definition: Types.h:93
GLenum array
Definition: glew.h:8327
bool isArray() const
Definition: Types.h:162
const TString & getName() const
Definition: SymbolTable.h:48
TInfoSinkBase debug
Definition: InfoSink.h:111
GLfloat GLfloat p
Definition: glew.h:14938
TType()
Definition: Types.h:97
bool isVector() const
Definition: Types.h:167
void dump(TInfoSink &infoSink) const
GLuint GLenum matrix
Definition: glew.h:13408
std::basic_string< char, std::char_traits< char >, TStringAllocator > TString
Definition: Common.h:41
void relateToOperator(const char *name, TOperator op)
const TString & getMangledName() const
Definition: SymbolTable.h:157
void dump(TInfoSink &infoSink) const
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2539
int getArraySize() const
Definition: Types.h:163
int currentLevel() const
Definition: SymbolTable.h:382
GLint level
Definition: gl2ext.h:845
bool containsArrays() const
Definition: SymbolTable.cpp:83
bool isMatrix() const
Definition: Types.h:159
size_t getObjectSize() const
Definition: SymbolTable.cpp:61
const char * getBasicString() const
Definition: Types.h:209
TStructure * getStruct() const
Definition: Types.h:170
size_t objectSize() const
Definition: Types.h:64
bool isStructureContainingArrays() const
Definition: Types.h:230
int i
Definition: pngrutil.c:1377
GLenum GLint GLint * precision
Definition: glew.h:3391
#define max(x, y)
Definition: os.h:79
const TString & mangledName() const
Definition: Types.h:59
TType * userDef
Definition: Types.h:268
GLsizei size
Definition: gl2ext.h:1467