2 // Copyright (c) 2002-2013 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 "common/angleutils.h"
12 #include "compiler/translator/BaseTypes.h"
13 #include "compiler/translator/Common.h"
14 #include "compiler/translator/compilerdebug.h"
22 POOL_ALLOCATOR_NEW_DELETE();
23 TField(TType* type, TString* name) : mType(type), mName(name) {}
25 // TODO(alokp): We should only return const type.
26 // Fix it by tweaking grammar.
27 TType* type() { return mType; }
28 const TType* type() const { return mType; }
30 const TString& name() const { return *mName; }
33 DISALLOW_COPY_AND_ASSIGN(TField);
38 typedef TVector<TField*> TFieldList;
39 inline TFieldList* NewPoolTFieldList()
41 void* memory = GetGlobalPoolAllocator()->allocate(sizeof(TFieldList));
42 return new(memory) TFieldList;
48 POOL_ALLOCATOR_NEW_DELETE();
49 TStructure(TString* name, TFieldList* fields)
56 const TString& name() const { return *mName; }
57 const TFieldList& fields() const { return *mFields; }
59 const TString& mangledName() const {
60 if (mMangledName.empty())
61 mMangledName = buildMangledName();
64 size_t objectSize() const {
66 mObjectSize = calculateObjectSize();
69 int deepestNesting() const {
70 if (mDeepestNesting == 0)
71 mDeepestNesting = calculateDeepestNesting();
72 return mDeepestNesting;
74 bool containsArrays() const;
77 DISALLOW_COPY_AND_ASSIGN(TStructure);
78 TString buildMangledName() const;
79 size_t calculateObjectSize() const;
80 int calculateDeepestNesting() const;
85 mutable TString mMangledName;
86 mutable size_t mObjectSize;
87 mutable int mDeepestNesting;
91 // Base class for things that have a type.
96 POOL_ALLOCATOR_NEW_DELETE();
98 TType(TBasicType t, TPrecision p, TQualifier q = EvqTemporary, unsigned char s = 1, bool m = false, bool a = false) :
99 type(t), precision(p), qualifier(q), size(s), matrix(m), array(a), arraySize(0), structure(0)
102 explicit TType(const TPublicType &p);
103 TType(TStructure* userDef, TPrecision p = EbpUndefined) :
104 type(EbtStruct), precision(p), qualifier(EvqTemporary), size(1), matrix(false), array(false), arraySize(0), structure(userDef)
108 TBasicType getBasicType() const { return type; }
109 void setBasicType(TBasicType t) { type = t; }
111 TPrecision getPrecision() const { return precision; }
112 void setPrecision(TPrecision p) { precision = p; }
114 TQualifier getQualifier() const { return qualifier; }
115 void setQualifier(TQualifier q) { qualifier = q; }
117 // One-dimensional size of single instance type
118 int getNominalSize() const { return size; }
119 void setNominalSize(unsigned char s) { size = s; }
120 // Full size of single instance of type
121 size_t getObjectSize() const;
123 int elementRegisterCount() const
127 const TFieldList &fields = getStruct()->fields();
128 int registerCount = 0;
130 for (size_t i = 0; i < fields.size(); i++)
132 registerCount += fields[i]->type()->totalRegisterCount();
135 return registerCount;
139 return getNominalSize();
147 int totalRegisterCount() const
151 return arraySize * elementRegisterCount();
155 return elementRegisterCount();
159 bool isMatrix() const { return matrix ? true : false; }
160 void setMatrix(bool m) { matrix = m; }
162 bool isArray() const { return array ? true : false; }
163 int getArraySize() const { return arraySize; }
164 void setArraySize(int s) { array = true; arraySize = s; }
165 void clearArrayness() { array = false; arraySize = 0; }
167 bool isVector() const { return size > 1 && !matrix; }
168 bool isScalar() const { return size == 1 && !matrix && !structure; }
170 TStructure* getStruct() const { return structure; }
171 void setStruct(TStructure* s) { structure = s; }
173 const TString& getMangledName() const {
174 if (mangled.empty()) {
175 mangled = buildMangledName();
181 bool sameElementType(const TType& right) const {
182 return type == right.type &&
183 size == right.size &&
184 matrix == right.matrix &&
185 structure == right.structure;
187 bool operator==(const TType& right) const {
188 return type == right.type &&
189 size == right.size &&
190 matrix == right.matrix &&
191 array == right.array && (!array || arraySize == right.arraySize) &&
192 structure == right.structure;
193 // don't check the qualifier, it's not ever what's being sought after
195 bool operator!=(const TType& right) const {
196 return !operator==(right);
198 bool operator<(const TType& right) const {
199 if (type != right.type) return type < right.type;
200 if (size != right.size) return size < right.size;
201 if (matrix != right.matrix) return matrix < right.matrix;
202 if (array != right.array) return array < right.array;
203 if (arraySize != right.arraySize) return arraySize < right.arraySize;
204 if (structure != right.structure) return structure < right.structure;
209 const char* getBasicString() const { return ::getBasicString(type); }
210 const char* getPrecisionString() const { return ::getPrecisionString(precision); }
211 const char* getQualifierString() const { return ::getQualifierString(qualifier); }
212 TString getCompleteString() const;
214 // If this type is a struct, returns the deepest struct nesting of
215 // any field in the struct. For example:
223 // For type "nesting2", this method would return 2 -- the number
224 // of structures through which indirection must occur to reach the
225 // deepest field (nesting2.field1.position).
226 int getDeepestStructNesting() const {
227 return structure ? structure->deepestNesting() : 0;
230 bool isStructureContainingArrays() const {
231 return structure ? structure->containsArrays() : false;
235 TString buildMangledName() const;
238 TPrecision precision;
239 TQualifier qualifier;
245 TStructure* structure; // 0 unless this is a struct
247 mutable TString mangled;
251 // This is a workaround for a problem with the yacc stack, It can't have
252 // types that it thinks have non-trivial constructors. It should
253 // just be used while recognizing the grammar, not anything else. Pointers
254 // could be used, but also trying to avoid lots of memory management overhead.
256 // Not as bad as it looks, there is no actual assumption that the fields
257 // match up or are name the same or anything like that.
262 TQualifier qualifier;
263 TPrecision precision;
264 unsigned char size; // size of vector or matrix, not size of array
271 void setBasic(TBasicType bt, TQualifier q, const TSourceLoc& ln)
275 precision = EbpUndefined;
284 void setAggregate(unsigned char s, bool m = false)
290 void setArray(bool a, int s = 0)
296 bool isStructureContainingArrays() const
303 return userDef->isStructureContainingArrays();
307 #endif // _TYPES_INCLUDED_