1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // Type.h -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Sep 25 14:14:01 2017 |
---|
13 | // Update Count : 154 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <strings.h> // for ffs |
---|
19 | #include <cassert> // for assert, assertf |
---|
20 | #include <list> // for list, _List_iterator |
---|
21 | #include <ostream> // for ostream, operator<<, basic_ostream |
---|
22 | #include <string> // for string |
---|
23 | |
---|
24 | #include "BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
25 | #include "Common/utility.h" // for operator+ |
---|
26 | #include "Mutator.h" // for Mutator |
---|
27 | #include "SynTree.h" // for AST nodes |
---|
28 | #include "Visitor.h" // for Visitor |
---|
29 | |
---|
30 | class Type : public BaseSyntaxNode { |
---|
31 | public: |
---|
32 | // Simulate inheritance because union does not allow it. |
---|
33 | // Bug in g++-4.9 prevents static field in union |
---|
34 | //static const char * Names[]; |
---|
35 | #define BFCommon( BFType, N ) \ |
---|
36 | bool operator[]( unsigned int i ) const { return val & (1 << i); } \ |
---|
37 | bool any() const { return val != 0; } \ |
---|
38 | void reset() { val = 0; } \ |
---|
39 | int ffs() { return ::ffs( val ) - 1; } \ |
---|
40 | BFType operator&=( BFType other ) { \ |
---|
41 | val &= other.val; return *this; \ |
---|
42 | } \ |
---|
43 | BFType operator&( BFType other ) const { \ |
---|
44 | BFType q = other; \ |
---|
45 | q &= *this; \ |
---|
46 | return q; \ |
---|
47 | } \ |
---|
48 | BFType operator|=( BFType other ) { \ |
---|
49 | val |= other.val; return *this; \ |
---|
50 | } \ |
---|
51 | BFType operator|( BFType other ) const { \ |
---|
52 | BFType q = other; \ |
---|
53 | q |= *this; \ |
---|
54 | return q; \ |
---|
55 | } \ |
---|
56 | BFType operator-=( BFType other ) { \ |
---|
57 | val &= ~other.val; return *this; \ |
---|
58 | } \ |
---|
59 | void print( std::ostream & os ) const { \ |
---|
60 | if ( (*this).any() ) { \ |
---|
61 | for ( unsigned int i = 0; i < N; i += 1 ) { \ |
---|
62 | if ( (*this)[i] ) { \ |
---|
63 | os << BFType##Names[i] << ' '; \ |
---|
64 | } \ |
---|
65 | } \ |
---|
66 | } \ |
---|
67 | } |
---|
68 | |
---|
69 | // enum must remain in the same order as the corresponding bit fields. |
---|
70 | |
---|
71 | enum { Inline = 1 << 0, Noreturn = 1 << 1, Fortran = 1 << 2, NumFuncSpecifier = 3 }; |
---|
72 | static const char * FuncSpecifiersNames[]; |
---|
73 | union FuncSpecifiers { |
---|
74 | unsigned int val; |
---|
75 | struct { |
---|
76 | bool is_inline : 1; |
---|
77 | bool is_noreturn : 1; |
---|
78 | bool is_fortran : 1; |
---|
79 | }; |
---|
80 | FuncSpecifiers() : val( 0 ) {} |
---|
81 | FuncSpecifiers( unsigned int val ) : val( val ) {} |
---|
82 | // equality (==, !=) works implicitly on first field "val", relational operations are undefined. |
---|
83 | BFCommon( FuncSpecifiers, NumFuncSpecifier ) |
---|
84 | }; // FuncSpecifiers |
---|
85 | |
---|
86 | enum { Extern = 1 << 0, Static = 1 << 1, Auto = 1 << 2, Register = 1 << 3, Threadlocal = 1 << 4, NumStorageClass = 5 }; |
---|
87 | static const char * StorageClassesNames[]; |
---|
88 | union StorageClasses { |
---|
89 | unsigned int val; |
---|
90 | struct { |
---|
91 | bool is_extern : 1; |
---|
92 | bool is_static : 1; |
---|
93 | bool is_auto : 1; |
---|
94 | bool is_register : 1; |
---|
95 | bool is_threadlocal : 1; |
---|
96 | }; |
---|
97 | |
---|
98 | StorageClasses() : val( 0 ) {} |
---|
99 | StorageClasses( unsigned int val ) : val( val ) {} |
---|
100 | // equality (==, !=) works implicitly on first field "val", relational operations are undefined. |
---|
101 | BFCommon( StorageClasses, NumStorageClass ) |
---|
102 | }; // StorageClasses |
---|
103 | |
---|
104 | enum { Const = 1 << 0, Restrict = 1 << 1, Volatile = 1 << 2, Lvalue = 1 << 3, Mutex = 1 << 4, Atomic = 1 << 5, NumTypeQualifier = 6 }; |
---|
105 | static const char * QualifiersNames[]; |
---|
106 | union Qualifiers { |
---|
107 | enum { Mask = ~(Restrict | Lvalue) }; |
---|
108 | unsigned int val; |
---|
109 | struct { |
---|
110 | bool is_const : 1; |
---|
111 | bool is_restrict : 1; |
---|
112 | bool is_volatile : 1; |
---|
113 | bool is_lvalue : 1; |
---|
114 | bool is_mutex : 1; |
---|
115 | bool is_atomic : 1; |
---|
116 | }; |
---|
117 | |
---|
118 | Qualifiers() : val( 0 ) {} |
---|
119 | Qualifiers( unsigned int val ) : val( val ) {} |
---|
120 | // Complex comparisons provide implicit qualifier downcasting, e.g., T downcast to const T. |
---|
121 | bool operator==( Qualifiers other ) const { return (val & Mask) == (other.val & Mask); } |
---|
122 | bool operator!=( Qualifiers other ) const { return (val & Mask) != (other.val & Mask); } |
---|
123 | bool operator<=( Qualifiers other ) const { |
---|
124 | return is_const <= other.is_const //Any non-const converts to const without cost |
---|
125 | && is_volatile <= other.is_volatile //Any non-volatile converts to volatile without cost |
---|
126 | && is_mutex >= other.is_mutex //Any mutex converts to non-mutex without cost |
---|
127 | && is_atomic == other.is_atomic; //No conversion from atomic to non atomic is free |
---|
128 | } |
---|
129 | bool operator<( Qualifiers other ) const { return *this != other && *this <= other; } |
---|
130 | bool operator>=( Qualifiers other ) const { return ! (*this < other); } |
---|
131 | bool operator>( Qualifiers other ) const { return *this != other && *this >= other; } |
---|
132 | BFCommon( Qualifiers, NumTypeQualifier ) |
---|
133 | }; // Qualifiers |
---|
134 | |
---|
135 | typedef std::list<TypeDecl *> ForallList; |
---|
136 | |
---|
137 | Qualifiers tq; |
---|
138 | ForallList forall; |
---|
139 | std::list< Attribute * > attributes; |
---|
140 | |
---|
141 | Type( const Qualifiers & tq, const std::list< Attribute * > & attributes ); |
---|
142 | Type( const Type & other ); |
---|
143 | virtual ~Type(); |
---|
144 | |
---|
145 | Qualifiers & get_qualifiers() { return tq; } |
---|
146 | bool get_const() { return tq.is_const; } |
---|
147 | bool get_volatile() { return tq.is_volatile; } |
---|
148 | bool get_restrict() { return tq.is_restrict; } |
---|
149 | bool get_lvalue() { return tq.is_lvalue; } |
---|
150 | bool get_mutex() { return tq.is_mutex; } |
---|
151 | bool get_atomic() { return tq.is_atomic; } |
---|
152 | void set_const( bool newValue ) { tq.is_const = newValue; } |
---|
153 | void set_volatile( bool newValue ) { tq.is_volatile = newValue; } |
---|
154 | void set_restrict( bool newValue ) { tq.is_restrict = newValue; } |
---|
155 | void set_lvalue( bool newValue ) { tq.is_lvalue = newValue; } |
---|
156 | void set_mutex( bool newValue ) { tq.is_mutex = newValue; } |
---|
157 | void set_atomic( bool newValue ) { tq.is_atomic = newValue; } |
---|
158 | |
---|
159 | ForallList& get_forall() { return forall; } |
---|
160 | |
---|
161 | std::list< Attribute * >& get_attributes() { return attributes; } |
---|
162 | const std::list< Attribute * >& get_attributes() const { return attributes; } |
---|
163 | |
---|
164 | /// How many elemental types are represented by this type |
---|
165 | virtual unsigned size() const { return 1; }; |
---|
166 | virtual bool isVoid() const { return size() == 0; } |
---|
167 | virtual Type * getComponent( unsigned i ) { assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i ); return this; } |
---|
168 | |
---|
169 | /// return type without outer pointers and arrays |
---|
170 | Type * stripDeclarator(); |
---|
171 | |
---|
172 | /// return type without outer references |
---|
173 | Type * stripReferences(); |
---|
174 | |
---|
175 | /// return the number of references occuring consecutively on the outermost layer of this type (i.e. do not count references nested within other types) |
---|
176 | virtual int referenceDepth() const; |
---|
177 | |
---|
178 | virtual bool isComplete() const { return true; } |
---|
179 | |
---|
180 | virtual AggregateDecl * getAggr() { assertf( false, "Non-aggregate type: %s", toCString( this ) ); } |
---|
181 | |
---|
182 | virtual TypeSubstitution genericSubstitution() const; |
---|
183 | |
---|
184 | virtual Type *clone() const = 0; |
---|
185 | virtual void accept( Visitor & v ) = 0; |
---|
186 | virtual Type *acceptMutator( Mutator & m ) = 0; |
---|
187 | virtual void print( std::ostream & os, Indenter indent = {} ) const; |
---|
188 | }; |
---|
189 | |
---|
190 | extern const Type::FuncSpecifiers noFuncSpecifiers; |
---|
191 | extern const Type::StorageClasses noStorageClasses; |
---|
192 | extern const Type::Qualifiers noQualifiers; // no qualifiers on constants |
---|
193 | |
---|
194 | class VoidType : public Type { |
---|
195 | public: |
---|
196 | VoidType( const Type::Qualifiers & tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
197 | |
---|
198 | virtual unsigned size() const override { return 0; }; |
---|
199 | virtual bool isComplete() const override { return false; } |
---|
200 | |
---|
201 | virtual VoidType *clone() const override { return new VoidType( *this ); } |
---|
202 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
203 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
204 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
205 | }; |
---|
206 | |
---|
207 | class BasicType : public Type { |
---|
208 | public: |
---|
209 | enum Kind { |
---|
210 | Bool, |
---|
211 | Char, |
---|
212 | SignedChar, |
---|
213 | UnsignedChar, |
---|
214 | ShortSignedInt, |
---|
215 | ShortUnsignedInt, |
---|
216 | SignedInt, |
---|
217 | UnsignedInt, |
---|
218 | LongSignedInt, |
---|
219 | LongUnsignedInt, |
---|
220 | LongLongSignedInt, |
---|
221 | LongLongUnsignedInt, |
---|
222 | Float, |
---|
223 | Double, |
---|
224 | LongDouble, |
---|
225 | FloatComplex, |
---|
226 | DoubleComplex, |
---|
227 | LongDoubleComplex, |
---|
228 | FloatImaginary, |
---|
229 | DoubleImaginary, |
---|
230 | LongDoubleImaginary, |
---|
231 | SignedInt128, |
---|
232 | UnsignedInt128, |
---|
233 | Float80, |
---|
234 | Float128, |
---|
235 | NUMBER_OF_BASIC_TYPES |
---|
236 | } kind; |
---|
237 | |
---|
238 | static const char *typeNames[]; // string names for basic types, MUST MATCH with Kind |
---|
239 | |
---|
240 | BasicType( const Type::Qualifiers & tq, Kind bt, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
241 | |
---|
242 | Kind get_kind() { return kind; } |
---|
243 | void set_kind( Kind newValue ) { kind = newValue; } |
---|
244 | |
---|
245 | virtual BasicType *clone() const override { return new BasicType( *this ); } |
---|
246 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
247 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
248 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
249 | |
---|
250 | bool isInteger() const; |
---|
251 | }; |
---|
252 | |
---|
253 | class PointerType : public Type { |
---|
254 | public: |
---|
255 | Type *base; |
---|
256 | |
---|
257 | // In C99, pointer types can be qualified in many ways e.g., int f( int a[ static 3 ] ) |
---|
258 | Expression *dimension; |
---|
259 | bool isVarLen; |
---|
260 | bool isStatic; |
---|
261 | |
---|
262 | PointerType( const Type::Qualifiers & tq, Type *base, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
263 | PointerType( const Type::Qualifiers & tq, Type *base, Expression *dimension, bool isVarLen, bool isStatic, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
264 | PointerType( const PointerType& ); |
---|
265 | virtual ~PointerType(); |
---|
266 | |
---|
267 | Type *get_base() { return base; } |
---|
268 | void set_base( Type *newValue ) { base = newValue; } |
---|
269 | Expression *get_dimension() { return dimension; } |
---|
270 | void set_dimension( Expression *newValue ) { dimension = newValue; } |
---|
271 | bool get_isVarLen() { return isVarLen; } |
---|
272 | void set_isVarLen( bool newValue ) { isVarLen = newValue; } |
---|
273 | bool get_isStatic() { return isStatic; } |
---|
274 | void set_isStatic( bool newValue ) { isStatic = newValue; } |
---|
275 | |
---|
276 | bool is_array() const { return isStatic || isVarLen || dimension; } |
---|
277 | |
---|
278 | virtual bool isComplete() const override { return ! isVarLen; } |
---|
279 | |
---|
280 | virtual PointerType *clone() const override { return new PointerType( *this ); } |
---|
281 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
282 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
283 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
284 | }; |
---|
285 | |
---|
286 | class ArrayType : public Type { |
---|
287 | public: |
---|
288 | Type *base; |
---|
289 | Expression *dimension; |
---|
290 | bool isVarLen; |
---|
291 | bool isStatic; |
---|
292 | |
---|
293 | ArrayType( const Type::Qualifiers & tq, Type *base, Expression *dimension, bool isVarLen, bool isStatic, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
294 | ArrayType( const ArrayType& ); |
---|
295 | virtual ~ArrayType(); |
---|
296 | |
---|
297 | Type *get_base() { return base; } |
---|
298 | void set_base( Type *newValue ) { base = newValue; } |
---|
299 | Expression *get_dimension() { return dimension; } |
---|
300 | void set_dimension( Expression *newValue ) { dimension = newValue; } |
---|
301 | bool get_isVarLen() { return isVarLen; } |
---|
302 | void set_isVarLen( bool newValue ) { isVarLen = newValue; } |
---|
303 | bool get_isStatic() { return isStatic; } |
---|
304 | void set_isStatic( bool newValue ) { isStatic = newValue; } |
---|
305 | |
---|
306 | // array types are complete if they have a dimension expression or are |
---|
307 | // VLAs ('*' in parameter declaration), and incomplete otherwise. |
---|
308 | // See 6.7.6.2 |
---|
309 | virtual bool isComplete() const override { return dimension || isVarLen; } |
---|
310 | |
---|
311 | virtual ArrayType *clone() const override { return new ArrayType( *this ); } |
---|
312 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
313 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
314 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
315 | }; |
---|
316 | |
---|
317 | class QualifiedType : public Type { |
---|
318 | public: |
---|
319 | std::list<Type *> types; |
---|
320 | |
---|
321 | QualifiedType( const Type::Qualifiers & tq, const std::list< Type * > & types ); |
---|
322 | QualifiedType( const QualifiedType & tq ); |
---|
323 | virtual ~QualifiedType(); |
---|
324 | |
---|
325 | virtual QualifiedType *clone() const override { return new QualifiedType( *this ); } |
---|
326 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
327 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
328 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
329 | }; |
---|
330 | |
---|
331 | class ReferenceType : public Type { |
---|
332 | public: |
---|
333 | Type *base; |
---|
334 | |
---|
335 | ReferenceType( const Type::Qualifiers & tq, Type *base, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
336 | ReferenceType( const ReferenceType & ); |
---|
337 | virtual ~ReferenceType(); |
---|
338 | |
---|
339 | Type *get_base() { return base; } |
---|
340 | void set_base( Type *newValue ) { base = newValue; } |
---|
341 | |
---|
342 | virtual int referenceDepth() const override; |
---|
343 | |
---|
344 | // Since reference types act like value types, their size is the size of the base. |
---|
345 | // This makes it simple to cast the empty tuple to a reference type, since casts that increase |
---|
346 | // the number of values are disallowed. |
---|
347 | virtual unsigned size() const override { return base->size(); } |
---|
348 | |
---|
349 | virtual TypeSubstitution genericSubstitution() const override; |
---|
350 | |
---|
351 | virtual ReferenceType *clone() const override { return new ReferenceType( *this ); } |
---|
352 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
353 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
354 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
355 | }; |
---|
356 | |
---|
357 | class FunctionType : public Type { |
---|
358 | public: |
---|
359 | std::list<DeclarationWithType*> returnVals; |
---|
360 | std::list<DeclarationWithType*> parameters; |
---|
361 | |
---|
362 | // Does the function accept a variable number of arguments following the arguments specified in the parameters list. |
---|
363 | // This could be because of |
---|
364 | // - an ellipsis in a prototype declaration |
---|
365 | // - an unprototyped declaration |
---|
366 | bool isVarArgs; |
---|
367 | |
---|
368 | FunctionType( const Type::Qualifiers & tq, bool isVarArgs, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
369 | FunctionType( const FunctionType& ); |
---|
370 | virtual ~FunctionType(); |
---|
371 | |
---|
372 | std::list<DeclarationWithType*> & get_returnVals() { return returnVals; } |
---|
373 | std::list<DeclarationWithType*> & get_parameters() { return parameters; } |
---|
374 | bool get_isVarArgs() const { return isVarArgs; } |
---|
375 | void set_isVarArgs( bool newValue ) { isVarArgs = newValue; } |
---|
376 | bool isTtype() const; |
---|
377 | |
---|
378 | bool isUnprototyped() const { return isVarArgs && parameters.size() == 0; } |
---|
379 | |
---|
380 | virtual FunctionType *clone() const override { return new FunctionType( *this ); } |
---|
381 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
382 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
383 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
384 | }; |
---|
385 | |
---|
386 | class ReferenceToType : public Type { |
---|
387 | public: |
---|
388 | std::list< Expression* > parameters; |
---|
389 | std::string name; |
---|
390 | bool hoistType; |
---|
391 | |
---|
392 | ReferenceToType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes ); |
---|
393 | ReferenceToType( const ReferenceToType & other ); |
---|
394 | virtual ~ReferenceToType(); |
---|
395 | |
---|
396 | const std::string & get_name() const { return name; } |
---|
397 | void set_name( std::string newValue ) { name = newValue; } |
---|
398 | std::list< Expression* >& get_parameters() { return parameters; } |
---|
399 | bool get_hoistType() const { return hoistType; } |
---|
400 | void set_hoistType( bool newValue ) { hoistType = newValue; } |
---|
401 | |
---|
402 | virtual ReferenceToType *clone() const override = 0; |
---|
403 | virtual void accept( Visitor & v ) override = 0; |
---|
404 | virtual Type *acceptMutator( Mutator & m ) override = 0; |
---|
405 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
406 | |
---|
407 | virtual void lookup( __attribute__((unused)) const std::string & name, __attribute__((unused)) std::list< Declaration* > & foundDecls ) const {} |
---|
408 | protected: |
---|
409 | virtual std::string typeString() const = 0; |
---|
410 | }; |
---|
411 | |
---|
412 | class StructInstType : public ReferenceToType { |
---|
413 | typedef ReferenceToType Parent; |
---|
414 | public: |
---|
415 | // this decl is not "owned" by the struct inst; it is merely a pointer to elsewhere in the tree, |
---|
416 | // where the structure used in this type is actually defined |
---|
417 | StructDecl *baseStruct; |
---|
418 | |
---|
419 | StructInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ), baseStruct( 0 ) {} |
---|
420 | StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
421 | StructInstType( const StructInstType & other ) : Parent( other ), baseStruct( other.baseStruct ) {} |
---|
422 | |
---|
423 | StructDecl *get_baseStruct() const { return baseStruct; } |
---|
424 | void set_baseStruct( StructDecl *newValue ) { baseStruct = newValue; } |
---|
425 | |
---|
426 | /// Accesses generic parameters of base struct (NULL if none such) |
---|
427 | std::list<TypeDecl*> * get_baseParameters(); |
---|
428 | const std::list<TypeDecl*> * get_baseParameters() const; |
---|
429 | |
---|
430 | virtual bool isComplete() const override; |
---|
431 | |
---|
432 | virtual AggregateDecl * getAggr() override; |
---|
433 | |
---|
434 | virtual TypeSubstitution genericSubstitution() const override; |
---|
435 | |
---|
436 | /// Looks up the members of this struct named "name" and places them into "foundDecls". |
---|
437 | /// Clones declarations into "foundDecls", caller responsible for freeing |
---|
438 | void lookup( const std::string & name, std::list< Declaration* > & foundDecls ) const override; |
---|
439 | |
---|
440 | virtual StructInstType *clone() const override { return new StructInstType( *this ); } |
---|
441 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
442 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
443 | |
---|
444 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
445 | private: |
---|
446 | virtual std::string typeString() const override; |
---|
447 | }; |
---|
448 | |
---|
449 | class UnionInstType : public ReferenceToType { |
---|
450 | typedef ReferenceToType Parent; |
---|
451 | public: |
---|
452 | // this decl is not "owned" by the union inst; it is merely a pointer to elsewhere in the tree, |
---|
453 | // where the union used in this type is actually defined |
---|
454 | UnionDecl *baseUnion; |
---|
455 | |
---|
456 | UnionInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ), baseUnion( 0 ) {} |
---|
457 | UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
458 | UnionInstType( const UnionInstType & other ) : Parent( other ), baseUnion( other.baseUnion ) {} |
---|
459 | |
---|
460 | UnionDecl *get_baseUnion() const { return baseUnion; } |
---|
461 | void set_baseUnion( UnionDecl * newValue ) { baseUnion = newValue; } |
---|
462 | |
---|
463 | /// Accesses generic parameters of base union (NULL if none such) |
---|
464 | std::list<TypeDecl*> * get_baseParameters(); |
---|
465 | const std::list<TypeDecl*> * get_baseParameters() const; |
---|
466 | |
---|
467 | virtual bool isComplete() const override; |
---|
468 | |
---|
469 | virtual AggregateDecl * getAggr() override; |
---|
470 | |
---|
471 | virtual TypeSubstitution genericSubstitution() const override; |
---|
472 | |
---|
473 | /// looks up the members of this union named "name" and places them into "foundDecls" |
---|
474 | /// Clones declarations into "foundDecls", caller responsible for freeing |
---|
475 | void lookup( const std::string & name, std::list< Declaration* > & foundDecls ) const override; |
---|
476 | |
---|
477 | virtual UnionInstType *clone() const override { return new UnionInstType( *this ); } |
---|
478 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
479 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
480 | |
---|
481 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
482 | private: |
---|
483 | virtual std::string typeString() const override; |
---|
484 | }; |
---|
485 | |
---|
486 | class EnumInstType : public ReferenceToType { |
---|
487 | typedef ReferenceToType Parent; |
---|
488 | public: |
---|
489 | // this decl is not "owned" by the union inst; it is merely a pointer to elsewhere in the tree, |
---|
490 | // where the union used in this type is actually defined |
---|
491 | EnumDecl *baseEnum = nullptr; |
---|
492 | |
---|
493 | EnumInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ) {} |
---|
494 | EnumInstType( const Type::Qualifiers & tq, EnumDecl * baseEnum, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
495 | EnumInstType( const EnumInstType & other ) : Parent( other ), baseEnum( other.baseEnum ) {} |
---|
496 | |
---|
497 | EnumDecl *get_baseEnum() const { return baseEnum; } |
---|
498 | void set_baseEnum( EnumDecl *newValue ) { baseEnum = newValue; } |
---|
499 | |
---|
500 | virtual bool isComplete() const override; |
---|
501 | |
---|
502 | virtual EnumInstType *clone() const override { return new EnumInstType( *this ); } |
---|
503 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
504 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
505 | |
---|
506 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
507 | private: |
---|
508 | virtual std::string typeString() const override; |
---|
509 | }; |
---|
510 | |
---|
511 | class TraitInstType : public ReferenceToType { |
---|
512 | typedef ReferenceToType Parent; |
---|
513 | public: |
---|
514 | // this decl is not "owned" by the trait inst; it is merely a pointer to elsewhere in the tree, |
---|
515 | // where the trait used in this type is actually defined |
---|
516 | TraitDecl * baseTrait = nullptr; |
---|
517 | |
---|
518 | TraitInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ) {} |
---|
519 | TraitInstType( const Type::Qualifiers & tq, TraitDecl * baseTrait, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
520 | TraitInstType( const TraitInstType & other ); |
---|
521 | ~TraitInstType(); |
---|
522 | |
---|
523 | virtual bool isComplete() const override; |
---|
524 | |
---|
525 | virtual TraitInstType *clone() const override { return new TraitInstType( *this ); } |
---|
526 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
527 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
528 | private: |
---|
529 | virtual std::string typeString() const override; |
---|
530 | }; |
---|
531 | |
---|
532 | class TypeInstType : public ReferenceToType { |
---|
533 | typedef ReferenceToType Parent; |
---|
534 | public: |
---|
535 | // this decl is not "owned" by the type inst; it is merely a pointer to elsewhere in the tree, |
---|
536 | // where the type used here is actually defined |
---|
537 | TypeDecl *baseType; |
---|
538 | bool isFtype; |
---|
539 | |
---|
540 | TypeInstType( const Type::Qualifiers & tq, const std::string & name, TypeDecl *baseType, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
541 | TypeInstType( const Type::Qualifiers & tq, const std::string & name, bool isFtype, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
542 | TypeInstType( const TypeInstType & other ); |
---|
543 | ~TypeInstType(); |
---|
544 | |
---|
545 | TypeDecl *get_baseType() const { return baseType; } |
---|
546 | void set_baseType( TypeDecl *newValue ); |
---|
547 | bool get_isFtype() const { return isFtype; } |
---|
548 | void set_isFtype( bool newValue ) { isFtype = newValue; } |
---|
549 | |
---|
550 | virtual bool isComplete() const override; |
---|
551 | |
---|
552 | virtual TypeInstType *clone() const override { return new TypeInstType( *this ); } |
---|
553 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
554 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
555 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
556 | private: |
---|
557 | virtual std::string typeString() const override; |
---|
558 | }; |
---|
559 | |
---|
560 | class TupleType : public Type { |
---|
561 | public: |
---|
562 | std::list<Type *> types; |
---|
563 | std::list<Declaration *> members; |
---|
564 | |
---|
565 | TupleType( const Type::Qualifiers & tq, const std::list< Type * > & types, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
566 | TupleType( const TupleType& ); |
---|
567 | virtual ~TupleType(); |
---|
568 | |
---|
569 | typedef std::list<Type*> value_type; |
---|
570 | typedef value_type::iterator iterator; |
---|
571 | |
---|
572 | std::list<Type *> & get_types() { return types; } |
---|
573 | virtual unsigned size() const override { return types.size(); }; |
---|
574 | |
---|
575 | // For now, this is entirely synthetic -- tuple types always have unnamed members. |
---|
576 | // Eventually, we may allow named tuples, in which case members should subsume types |
---|
577 | std::list<Declaration *> & get_members() { return members; } |
---|
578 | |
---|
579 | iterator begin() { return types.begin(); } |
---|
580 | iterator end() { return types.end(); } |
---|
581 | |
---|
582 | virtual Type * getComponent( unsigned i ) override { |
---|
583 | assertf( i < size(), "TupleType::getComponent: index %d must be less than size %d", i, size() ); |
---|
584 | return *(begin()+i); |
---|
585 | } |
---|
586 | |
---|
587 | // virtual bool isComplete() const override { return true; } // xxx - not sure if this is right, might need to recursively check complete-ness |
---|
588 | |
---|
589 | virtual TupleType *clone() const override { return new TupleType( *this ); } |
---|
590 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
591 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
592 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
593 | }; |
---|
594 | |
---|
595 | class TypeofType : public Type { |
---|
596 | public: |
---|
597 | Expression *expr; |
---|
598 | |
---|
599 | TypeofType( const Type::Qualifiers & tq, Expression *expr, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
600 | TypeofType( const TypeofType& ); |
---|
601 | virtual ~TypeofType(); |
---|
602 | |
---|
603 | Expression *get_expr() const { return expr; } |
---|
604 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
605 | |
---|
606 | virtual bool isComplete() const override { assert( false ); return false; } |
---|
607 | |
---|
608 | virtual TypeofType *clone() const override { return new TypeofType( *this ); } |
---|
609 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
610 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
611 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
612 | }; |
---|
613 | |
---|
614 | class AttrType : public Type { |
---|
615 | public: |
---|
616 | std::string name; |
---|
617 | Expression *expr; |
---|
618 | Type *type; |
---|
619 | bool isType; |
---|
620 | |
---|
621 | AttrType( const Type::Qualifiers & tq, const std::string & name, Expression *expr, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
622 | AttrType( const Type::Qualifiers & tq, const std::string & name, Type *type, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
623 | AttrType( const AttrType& ); |
---|
624 | virtual ~AttrType(); |
---|
625 | |
---|
626 | const std::string & get_name() const { return name; } |
---|
627 | void set_name( const std::string & newValue ) { name = newValue; } |
---|
628 | Expression *get_expr() const { return expr; } |
---|
629 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
630 | Type *get_type() const { return type; } |
---|
631 | void set_type( Type *newValue ) { type = newValue; } |
---|
632 | bool get_isType() const { return isType; } |
---|
633 | void set_isType( bool newValue ) { isType = newValue; } |
---|
634 | |
---|
635 | virtual bool isComplete() const override { assert( false ); } // xxx - not sure what to do here |
---|
636 | |
---|
637 | virtual AttrType *clone() const override { return new AttrType( *this ); } |
---|
638 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
639 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
640 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
641 | }; |
---|
642 | |
---|
643 | /// Represents the GCC built-in varargs type |
---|
644 | class VarArgsType : public Type { |
---|
645 | public: |
---|
646 | VarArgsType(); |
---|
647 | VarArgsType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
648 | |
---|
649 | virtual bool isComplete() const override{ return true; } // xxx - is this right? |
---|
650 | |
---|
651 | virtual VarArgsType *clone() const override { return new VarArgsType( *this ); } |
---|
652 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
653 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
654 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
655 | }; |
---|
656 | |
---|
657 | /// Represents a zero constant |
---|
658 | class ZeroType : public Type { |
---|
659 | public: |
---|
660 | ZeroType(); |
---|
661 | ZeroType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
662 | |
---|
663 | virtual ZeroType *clone() const override { return new ZeroType( *this ); } |
---|
664 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
665 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
666 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
667 | }; |
---|
668 | |
---|
669 | /// Represents a one constant |
---|
670 | class OneType : public Type { |
---|
671 | public: |
---|
672 | OneType(); |
---|
673 | OneType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
674 | |
---|
675 | virtual OneType *clone() const override { return new OneType( *this ); } |
---|
676 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
677 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
678 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
679 | }; |
---|
680 | |
---|
681 | class GlobalScopeType : public Type { |
---|
682 | public: |
---|
683 | GlobalScopeType(); |
---|
684 | |
---|
685 | virtual GlobalScopeType *clone() const override { return new GlobalScopeType( *this ); } |
---|
686 | virtual void accept( Visitor & v ) override { v.visit( this ); } |
---|
687 | virtual Type *acceptMutator( Mutator & m ) override { return m.mutate( this ); } |
---|
688 | virtual void print( std::ostream & os, Indenter indent = {} ) const override; |
---|
689 | }; |
---|
690 | |
---|
691 | // Local Variables: // |
---|
692 | // tab-width: 4 // |
---|
693 | // mode: c++ // |
---|
694 | // compile-command: "make install" // |
---|
695 | // End: // |
---|