2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
5 //Redistribution and use in source and binary forms, with or without
6 //modification, are permitted provided that the following conditions
9 // Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
12 // Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
17 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //POSSIBILITY OF SUCH DAMAGE.
35 #ifndef _TYPES_INCLUDED
36 #define _TYPES_INCLUDED
38 #include "../Include/Common.h"
39 #include "../Include/BaseTypes.h"
42 // Need to have association of line numbers to types in a list for building structs.
49 typedef TVector
<TTypeLine
> TTypeList
;
51 inline TTypeList
* NewPoolTTypeList()
53 void* memory
= GlobalPoolAllocator
.allocate(sizeof(TTypeList
));
54 return new(memory
) TTypeList
;
58 // This is a workaround for a problem with the yacc stack, It can't have
59 // types that the compiler thinks non-trivial constructors. It should
60 // just be used while recognizing the grammar, not anything else. Pointers
61 // could be used, but also trying to avoid lots of memory management overhead.
63 // Not as bad as it looks, there is no actual assumption that the fields
64 // match up or are name the same or anything like that.
70 int size
; // size of vector or matrix, not size of array
77 typedef std::map
<TTypeList
*, TTypeList
*> TStructureMap
;
78 typedef std::map
<TTypeList
*, TTypeList
*>::iterator TStructureMapIterator
;
80 // Base class for things that have a type.
84 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator
)
85 explicit TType(TBasicType t
, TQualifier q
= EvqTemporary
, int s
= 1, bool m
= false, bool a
= false) :
86 type(t
), qualifier(q
), size(s
), matrix(m
), array(a
), arraySize(0), structure(0),
87 structureSize(0), maxArraySize(0), arrayInformationType(0), fieldName(0), typeName(0), mangled(0)
89 explicit TType(TPublicType p
) :
90 type(p
.type
), qualifier(p
.qualifier
), size(p
.size
), matrix(p
.matrix
), array(p
.array
), arraySize(0),
91 structure(0), structureSize(0), maxArraySize(0), arrayInformationType(0), fieldName(0), typeName(0), mangled(0)
94 structure
= p
.userDef
->getStruct();
95 structureSize
= setStructSize(p
.userDef
->getStruct());
96 typeName
= NewPoolTString(p
.userDef
->getTypeName().c_str());
99 explicit TType(TTypeList
* userDef
, TString n
) :
100 type(EbtStruct
), qualifier(EvqTemporary
), size(1), matrix(false), array(false), arraySize(0),
101 structure(userDef
), maxArraySize(0), arrayInformationType(0), fieldName(0), mangled(0) {
102 structureSize
= setStructSize(userDef
);
103 typeName
= NewPoolTString(n
.c_str());
108 TType (const TType
& type
) { *this = type
; }
110 void copyType(const TType
& copyOf
, TStructureMap
& remapper
)
113 qualifier
= copyOf
.qualifier
;
115 matrix
= copyOf
.matrix
;
116 array
= copyOf
.array
;
117 arraySize
= copyOf
.arraySize
;
119 TStructureMapIterator iter
;
120 if (copyOf
.structure
) {
121 if ((iter
= remapper
.find(structure
)) == remapper
.end()) {
122 // create the new structure here
123 structure
= NewPoolTTypeList();
124 for (unsigned int i
= 0; i
< copyOf
.structure
->size(); ++i
) {
126 typeLine
.line
= (*copyOf
.structure
)[i
].line
;
127 typeLine
.type
= (*copyOf
.structure
)[i
].type
->clone(remapper
);
128 structure
->push_back(typeLine
);
131 structure
= iter
->second
;
137 if (copyOf
.fieldName
)
138 fieldName
= NewPoolTString(copyOf
.fieldName
->c_str());
141 typeName
= NewPoolTString(copyOf
.typeName
->c_str());
145 mangled
= NewPoolTString(copyOf
.mangled
->c_str());
147 structureSize
= copyOf
.structureSize
;
148 maxArraySize
= copyOf
.maxArraySize
;
149 assert (copyOf
.arrayInformationType
== 0);
150 arrayInformationType
= 0; // arrayInformationType should not be set for builtIn symbol table level
153 TType
* clone(TStructureMap
& remapper
)
155 TType
*newType
= new TType();
156 newType
->copyType(*this, remapper
);
161 int setStructSize(TTypeList
* userDef
)
164 for (TTypeList::iterator tl
= userDef
->begin(); tl
!= userDef
->end(); tl
++) {
165 if (((*tl
).type
)->isArray()) {
166 if (((*tl
).type
)->getStruct()) {
167 int structSize
= setStructSize(((*tl
).type
)->getStruct());
168 stSize
+= structSize
* ((*tl
).type
)->getArraySize();
170 stSize
+= ((*tl
).type
)->getInstanceSize() * ((*tl
).type
)->getArraySize();
172 } else if (((*tl
).type
)->isMatrix() || ((*tl
).type
)->isVector()){
173 stSize
+= ((*tl
).type
)->getInstanceSize();
174 } else if (((*tl
).type
)->getStruct()) {
175 //?? We should actually be calling getStructSize() function and not setStructSize. This problem occurs in case
176 // of nested/embedded structs.
177 stSize
+= setStructSize(((*tl
).type
)->getStruct());
181 structureSize
= stSize
;
185 virtual void setType(TBasicType t
, int s
, bool m
, bool a
, int aS
= 0)
186 { type
= t
; size
= s
; matrix
= m
; array
= a
; arraySize
= aS
; }
187 virtual void setType(TBasicType t
, int s
, bool m
, TType
* userDef
= 0)
192 structure
= userDef
->getStruct();
193 // leave array information intact.
195 virtual void setTypeName(const TString
& n
) { typeName
= NewPoolTString(n
.c_str()); }
196 virtual void setFieldName(const TString
& n
) { fieldName
= NewPoolTString(n
.c_str()); }
197 virtual const TString
& getTypeName() const
203 virtual const TString
& getFieldName() const
209 virtual TBasicType
getBasicType() const { return type
; }
210 virtual TQualifier
getQualifier() const { return qualifier
; }
211 virtual void changeQualifier(TQualifier q
) { qualifier
= q
; }
213 // One-dimensional size of single instance type
214 virtual int getNominalSize() const { return size
; }
216 // Full-dimensional size of single instance of type
217 virtual int getInstanceSize() const
225 virtual bool isMatrix() const { return matrix
? true : false; }
226 virtual bool isArray() const { return array
? true : false; }
227 int getArraySize() const { return arraySize
; }
228 void setArraySize(int s
) { array
= true; arraySize
= s
; }
229 void setMaxArraySize (int s
) { maxArraySize
= s
; }
230 int getMaxArraySize () const { return maxArraySize
; }
231 void setArrayInformationType(TType
* t
) { arrayInformationType
= t
; }
232 TType
* getArrayInformationType() { return arrayInformationType
; }
233 virtual bool isVector() const { return size
> 1 && !matrix
; }
234 static char* getBasicString(TBasicType t
) {
236 case EbtVoid
: return "void"; break;
237 case EbtFloat
: return "float"; break;
238 case EbtInt
: return "int"; break;
239 case EbtBool
: return "bool"; break;
240 case EbtSampler1D
: return "sampler1D"; break;
241 case EbtSampler2D
: return "sampler2D"; break;
242 case EbtSampler3D
: return "sampler3D"; break;
243 case EbtSamplerCube
: return "samplerCube"; break;
244 case EbtSampler1DShadow
: return "sampler1DShadow"; break;
245 case EbtSampler2DShadow
: return "sampler2DShadow"; break;
246 case EbtStruct
: return "structure"; break;
247 default: return "unknown type";
250 const char* getBasicString() const { return TType::getBasicString(type
); }
251 const char* getQualifierString() const { return ::getQualifierString(qualifier
); }
252 TTypeList
* getStruct() { return structure
; }
253 int getStructSize() const { return structureSize
; }
254 TTypeList
* getStruct() const { return structure
; }
255 TString
& getMangledName() {
257 mangled
= NewPoolTString("");
258 buildMangledName(*mangled
);
264 bool operator==(const TType
& right
) const {
265 return type
== right
.type
&&
266 size
== right
.size
&&
267 matrix
== right
.matrix
&&
268 array
== right
.array
&&
269 structure
== right
.structure
;
270 // don't check the qualifier, it's not ever what's being sought after
272 bool operator!=(const TType
& right
) const {
273 return !operator==(right
);
275 TString
getCompleteString() const;
278 void buildMangledName(TString
&);
281 TQualifier qualifier
: 7;
282 int size
: 8; // size of vector or matrix, not size of array
283 unsigned int matrix
: 1;
284 unsigned int array
: 1;
287 TTypeList
* structure
; // 0 unless this is a struct
290 TType
* arrayInformationType
;
291 TString
*fieldName
; // for structure field names
292 TString
*typeName
; // for structure field type name
297 #endif // _TYPES_INCLUDED_