2 // Copyright (c) 2002-2010 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.
7 #ifndef _TYPES_INCLUDED
8 #define _TYPES_INCLUDED
10 #include "compiler/BaseTypes.h"
11 #include "compiler/Common.h"
12 #include "compiler/debug.h"
15 // Need to have association of line numbers to types in a list for building structs.
22 typedef TVector<TTypeLine> TTypeList;
24 inline TTypeList* NewPoolTTypeList()
26 void* memory = GlobalPoolAllocator.allocate(sizeof(TTypeList));
27 return new(memory) TTypeList;
31 // This is a workaround for a problem with the yacc stack, It can't have
32 // types that it thinks have non-trivial constructors. It should
33 // just be used while recognizing the grammar, not anything else. Pointers
34 // could be used, but also trying to avoid lots of memory management overhead.
36 // Not as bad as it looks, there is no actual assumption that the fields
37 // match up or are name the same or anything like that.
44 int size; // size of vector or matrix, not size of array
51 void setBasic(TBasicType bt, TQualifier q, int ln = 0)
55 precision = EbpUndefined;
64 void setAggregate(int s, bool m = false)
70 void setArray(bool a, int s = 0)
77 typedef TMap<TTypeList*, TTypeList*> TStructureMap;
78 typedef TMap<TTypeList*, TTypeList*>::iterator TStructureMapIterator;
80 // Base class for things that have a type.
84 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
86 TType(TBasicType t, TPrecision p, TQualifier q = EvqTemporary, int s = 1, bool m = false, bool a = false) :
87 type(t), precision(p), qualifier(q), size(s), matrix(m), array(a), arraySize(0),
88 maxArraySize(0), arrayInformationType(0), structure(0), structureSize(0), fieldName(0), mangled(0), typeName(0)
91 explicit TType(const TPublicType &p) :
92 type(p.type), precision(p.precision), qualifier(p.qualifier), size(p.size), matrix(p.matrix), array(p.array), arraySize(p.arraySize),
93 maxArraySize(0), arrayInformationType(0), structure(0), structureSize(0), fieldName(0), mangled(0), typeName(0)
96 structure = p.userDef->getStruct();
97 typeName = NewPoolTString(p.userDef->getTypeName().c_str());
100 TType(TTypeList* userDef, const TString& n, TPrecision p = EbpUndefined) :
101 type(EbtStruct), precision(p), qualifier(EvqTemporary), size(1), matrix(false), array(false), arraySize(0),
102 maxArraySize(0), arrayInformationType(0), structure(userDef), structureSize(0), fieldName(0), mangled(0)
104 typeName = NewPoolTString(n.c_str());
107 void copyType(const TType& copyOf, TStructureMap& remapper)
110 precision = copyOf.precision;
111 qualifier = copyOf.qualifier;
113 matrix = copyOf.matrix;
114 array = copyOf.array;
115 arraySize = copyOf.arraySize;
117 TStructureMapIterator iter;
118 if (copyOf.structure) {
119 if ((iter = remapper.find(structure)) == remapper.end()) {
120 // create the new structure here
121 structure = NewPoolTTypeList();
122 for (unsigned int i = 0; i < copyOf.structure->size(); ++i) {
124 typeLine.line = (*copyOf.structure)[i].line;
125 typeLine.type = (*copyOf.structure)[i].type->clone(remapper);
126 structure->push_back(typeLine);
129 structure = iter->second;
135 if (copyOf.fieldName)
136 fieldName = NewPoolTString(copyOf.fieldName->c_str());
139 typeName = NewPoolTString(copyOf.typeName->c_str());
143 mangled = NewPoolTString(copyOf.mangled->c_str());
145 structureSize = copyOf.structureSize;
146 maxArraySize = copyOf.maxArraySize;
147 assert(copyOf.arrayInformationType == 0);
148 arrayInformationType = 0; // arrayInformationType should not be set for builtIn symbol table level
151 TType* clone(TStructureMap& remapper)
153 TType *newType = new TType();
154 newType->copyType(*this, remapper);
159 TBasicType getBasicType() const { return type; }
160 void setBasicType(TBasicType t) { type = t; }
162 TPrecision getPrecision() const { return precision; }
163 void setPrecision(TPrecision p) { precision = p; }
165 TQualifier getQualifier() const { return qualifier; }
166 void setQualifier(TQualifier q) { qualifier = q; }
168 // One-dimensional size of single instance type
169 int getNominalSize() const { return size; }
170 void setNominalSize(int s) { size = s; }
171 // Full size of single instance of type
172 int getObjectSize() const
176 if (getBasicType() == EbtStruct)
177 totalSize = getStructSize();
179 totalSize = size * size;
184 totalSize *= std::max(getArraySize(), getMaxArraySize());
189 bool isMatrix() const { return matrix ? true : false; }
190 void setMatrix(bool m) { matrix = m; }
192 bool isArray() const { return array ? true : false; }
193 int getArraySize() const { return arraySize; }
194 void setArraySize(int s) { array = true; arraySize = s; }
195 int getMaxArraySize () const { return maxArraySize; }
196 void setMaxArraySize (int s) { maxArraySize = s; }
197 void clearArrayness() { array = false; arraySize = 0; maxArraySize = 0; }
198 void setArrayInformationType(TType* t) { arrayInformationType = t; }
199 TType* getArrayInformationType() const { return arrayInformationType; }
201 bool isVector() const { return size > 1 && !matrix; }
202 bool isScalar() const { return size == 1 && !matrix && !structure; }
204 TTypeList* getStruct() const { return structure; }
205 void setStruct(TTypeList* s) { structure = s; }
207 const TString& getTypeName() const
212 void setTypeName(const TString& n)
214 typeName = NewPoolTString(n.c_str());
217 bool isField() const { return fieldName != 0; }
218 const TString& getFieldName() const
223 void setFieldName(const TString& n)
225 fieldName = NewPoolTString(n.c_str());
228 TString& getMangledName() {
230 mangled = NewPoolTString("");
231 buildMangledName(*mangled);
238 bool sameElementType(const TType& right) const {
239 return type == right.type &&
240 size == right.size &&
241 matrix == right.matrix &&
242 structure == right.structure;
244 bool operator==(const TType& right) const {
245 return type == right.type &&
246 size == right.size &&
247 matrix == right.matrix &&
248 array == right.array && (!array || arraySize == right.arraySize) &&
249 structure == right.structure;
250 // don't check the qualifier, it's not ever what's being sought after
252 bool operator!=(const TType& right) const {
253 return !operator==(right);
255 bool operator<(const TType& right) const {
256 if (type != right.type) return type < right.type;
257 if (size != right.size) return size < right.size;
258 if (matrix != right.matrix) return matrix < right.matrix;
259 if (array != right.array) return array < right.array;
260 if (arraySize != right.arraySize) return arraySize < right.arraySize;
261 if (structure != right.structure) return structure < right.structure;
266 const char* getBasicString() const { return ::getBasicString(type); }
267 const char* getPrecisionString() const { return ::getPrecisionString(precision); }
268 const char* getQualifierString() const { return ::getQualifierString(qualifier); }
269 TString getCompleteString() const;
272 void buildMangledName(TString&);
273 int getStructSize() const;
276 TPrecision precision;
277 TQualifier qualifier : 7;
278 int size : 8; // size of vector or matrix, not size of array
279 unsigned int matrix : 1;
280 unsigned int array : 1;
283 TType* arrayInformationType;
285 TTypeList* structure; // 0 unless this is a struct
286 mutable int structureSize;
288 TString *fieldName; // for structure field names
290 TString *typeName; // for structure field type name
293 #endif // _TYPES_INCLUDED_