source: src/SynTree/Type.h @ 6e8bd43

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6e8bd43 was 6e8bd43, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

cleanup interface to qualifiers/specifiers

  • Property mode set to 100644
File size: 21.0 KB
Line 
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 : Wed Mar 15 23:28:33 2017
13// Update Count     : 89
14//
15
16#ifndef TYPE_H
17#define TYPE_H
18
19#include "BaseSyntaxNode.h"
20#include "Mutator.h"
21#include "SynTree.h"
22#include "Visitor.h"
23
24class Type : public BaseSyntaxNode {
25  public:
26        enum { Const = 1 << 0, Restrict = 1 << 1, Volatile = 1 << 2, Lvalue = 1 << 3, Mutex = 1 << 4, Atomic = 1 << 5, NumTypeQualifier = 6 };
27        union Qualifiers {
28                static const char * Names[];
29                enum { Mask = ~(Restrict | Lvalue) };
30                unsigned int val;
31                struct {
32                        bool isConst : 1;
33                        bool isRestrict : 1;
34                        bool isVolatile : 1;
35                        bool isLvalue : 1;
36                        bool isMutex : 1;
37                        bool isAtomic : 1;
38                };
39
40                Qualifiers() : val( 0 ) {}
41                Qualifiers( unsigned int val ) : val( val ) {}
42                bool operator[]( unsigned int i ) const { return val & (1 << i); }
43                bool any() const { return val != 0; }
44                bool operator==( const Qualifiers other ) const {
45                        return (val & Mask) == (other.val & Mask);
46                }
47                bool operator!=( const Qualifiers other ) const {
48                        return (val & Mask) != (other.val & Mask);
49                }
50                bool operator<=( const Qualifiers other ) const {
51                        return isConst <= other.isConst && isVolatile <= other.isVolatile &&
52                                isMutex == other.isMutex && isAtomic == other.isAtomic;
53                }
54                bool operator>=( const Qualifiers other ) const {
55                        return isConst >= other.isConst && isVolatile >= other.isVolatile &&
56                                isMutex == other.isMutex && isAtomic == other.isAtomic;
57                }
58                bool operator<( const Qualifiers other ) const {
59                        return *this != other && *this <= other;
60                }
61                bool operator>( const Qualifiers other ) const {
62                        return *this != other && *this >= other;
63                }
64                Qualifiers operator&=( const Type::Qualifiers other ) {
65                        val &= other.val; return *this;
66                }
67                Qualifiers operator+=( const Qualifiers other ) {
68                        val |= other.val; return *this;
69                }
70                Qualifiers operator-=( const Qualifiers other ) {
71                        val &= ~other.val; return *this;
72                }
73                Qualifiers operator+( const Qualifiers other ) const {
74                        Qualifiers q = other;
75                        q += *this;
76                        return q;
77                }
78                void print( std::ostream & os, int indent = 0 ) const {
79                        if ( (*this).any() ) {                                          // any type qualifiers ?
80                                for ( unsigned int i = 0; i < NumTypeQualifier; i += 1 ) {
81                                        if ( (*this)[i] ) {
82                                                os << Names[i] << ' ';
83                                        } // if
84                                } // for
85                        } // if
86                }
87        }; // Qualifiers
88
89        Type( const Qualifiers & tq, const std::list< Attribute * > & attributes );
90        Type( const Type & other );
91        virtual ~Type();
92
93        Qualifiers & get_qualifiers() { return tq; }
94        bool get_isConst() { return tq.isConst; }
95        bool get_isVolatile() { return tq.isVolatile; }
96        bool get_isRestrict() { return tq.isRestrict; }
97        bool get_isLvalue() { return tq.isLvalue; }
98        bool get_isAtomic() { return tq.isAtomic; }
99        void set_isConst( bool newValue ) { tq.isConst = newValue; }
100        void set_isVolatile( bool newValue ) { tq.isVolatile = newValue; }
101        void set_isRestrict( bool newValue ) { tq.isRestrict = newValue; }
102        void set_isLvalue( bool newValue ) { tq.isLvalue = newValue; }
103        void set_isAtomic( bool newValue ) { tq.isAtomic = newValue; }
104
105        typedef std::list<TypeDecl *> ForallList;
106        ForallList& get_forall() { return forall; }
107
108        std::list< Attribute * >& get_attributes() { return attributes; }
109        const std::list< Attribute * >& get_attributes() const { return attributes; }
110
111        /// How many elemental types are represented by this type
112        virtual unsigned size() const { return 1; };
113        virtual bool isVoid() const { return size() == 0; }
114        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; }
115
116        virtual bool isComplete() const { return true; }
117
118        virtual Type *clone() const = 0;
119        virtual void accept( Visitor & v ) = 0;
120        virtual Type *acceptMutator( Mutator & m ) = 0;
121        virtual void print( std::ostream & os, int indent = 0 ) const;
122  private:
123        Qualifiers tq;
124        ForallList forall;
125        std::list< Attribute * > attributes;
126};
127
128extern Type::Qualifiers emptyQualifiers;                                // no qualifiers on constants
129
130class VoidType : public Type {
131  public:
132        VoidType( const Type::Qualifiers & tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
133
134        virtual unsigned size() const { return 0; };
135        virtual bool isComplete() const { return false; }
136
137        virtual VoidType *clone() const { return new VoidType( *this ); }
138        virtual void accept( Visitor & v ) { v.visit( this ); }
139        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
140        virtual void print( std::ostream & os, int indent = 0 ) const;
141};
142
143class BasicType : public Type {
144  public:
145        enum Kind {
146                Bool,
147                Char,
148                SignedChar,
149                UnsignedChar,
150                ShortSignedInt,
151                ShortUnsignedInt,
152                SignedInt,
153                UnsignedInt,
154                LongSignedInt,
155                LongUnsignedInt,
156                LongLongSignedInt,
157                LongLongUnsignedInt,
158                Float,
159                Double,
160                LongDouble,
161                FloatComplex,
162                DoubleComplex,
163                LongDoubleComplex,
164                FloatImaginary,
165                DoubleImaginary,
166                LongDoubleImaginary,
167                NUMBER_OF_BASIC_TYPES
168        };
169
170        static const char *typeNames[];                                         // string names for basic types, MUST MATCH with Kind
171
172        BasicType( const Type::Qualifiers & tq, Kind bt, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
173
174        Kind get_kind() { return kind; }
175        void set_kind( Kind newValue ) { kind = newValue; }
176
177        virtual BasicType *clone() const { return new BasicType( *this ); }
178        virtual void accept( Visitor & v ) { v.visit( this ); }
179        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
180        virtual void print( std::ostream & os, int indent = 0 ) const;
181
182        bool isInteger() const;
183  private:
184        Kind kind;
185};
186
187class PointerType : public Type {
188  public:
189        PointerType( const Type::Qualifiers & tq, Type *base, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
190        PointerType( const Type::Qualifiers & tq, Type *base, Expression *dimension, bool isVarLen, bool isStatic, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
191        PointerType( const PointerType& );
192        virtual ~PointerType();
193
194        Type *get_base() { return base; }
195        void set_base( Type *newValue ) { base = newValue; }
196        Expression *get_dimension() { return dimension; }
197        void set_dimension( Expression *newValue ) { dimension = newValue; }
198        bool get_isVarLen() { return isVarLen; }
199        void set_isVarLen( bool newValue ) { isVarLen = newValue; }
200        bool get_isStatic() { return isStatic; }
201        void set_isStatic( bool newValue ) { isStatic = newValue; }
202
203        virtual PointerType *clone() const { return new PointerType( *this ); }
204        virtual void accept( Visitor & v ) { v.visit( this ); }
205        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
206        virtual void print( std::ostream & os, int indent = 0 ) const;
207  private:
208        Type *base;
209
210        // In C99, pointer types can be qualified in many ways e.g., int f( int a[ static 3 ] )
211        Expression *dimension;
212        bool isVarLen;
213        bool isStatic;
214};
215
216class ArrayType : public Type {
217  public:
218        ArrayType( const Type::Qualifiers & tq, Type *base, Expression *dimension, bool isVarLen, bool isStatic, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
219        ArrayType( const ArrayType& );
220        virtual ~ArrayType();
221
222        Type *get_base() { return base; }
223        void set_base( Type *newValue ) { base = newValue; }
224        Expression *get_dimension() { return dimension; }
225        void set_dimension( Expression *newValue ) { dimension = newValue; }
226        bool get_isVarLen() { return isVarLen; }
227        void set_isVarLen( bool newValue ) { isVarLen = newValue; }
228        bool get_isStatic() { return isStatic; }
229        void set_isStatic( bool newValue ) { isStatic = newValue; }
230
231        virtual bool isComplete() const { return ! isVarLen; }
232
233        virtual ArrayType *clone() const { return new ArrayType( *this ); }
234        virtual void accept( Visitor & v ) { v.visit( this ); }
235        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
236        virtual void print( std::ostream & os, int indent = 0 ) const;
237  private:
238        Type *base;
239        Expression *dimension;
240        bool isVarLen;
241        bool isStatic;
242};
243
244class FunctionType : public Type {
245  public:
246        FunctionType( const Type::Qualifiers & tq, bool isVarArgs, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
247        FunctionType( const FunctionType& );
248        virtual ~FunctionType();
249
250        std::list<DeclarationWithType*> & get_returnVals() { return returnVals; }
251        std::list<DeclarationWithType*> & get_parameters() { return parameters; }
252        bool get_isVarArgs() const { return isVarArgs; }
253        void set_isVarArgs( bool newValue ) { isVarArgs = newValue; }
254        bool isTtype() const;
255
256        virtual FunctionType *clone() const { return new FunctionType( *this ); }
257        virtual void accept( Visitor & v ) { v.visit( this ); }
258        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
259        virtual void print( std::ostream & os, int indent = 0 ) const;
260  private:
261        std::list<DeclarationWithType*> returnVals;
262        std::list<DeclarationWithType*> parameters;
263
264        // Does the function accept a variable number of arguments following the arguments specified in the parameters list.
265        // This could be because of
266        // - an ellipsis in a prototype declaration
267        // - an unprototyped declaration
268        bool isVarArgs;
269};
270
271class ReferenceToType : public Type {
272  public:
273        ReferenceToType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes );
274        ReferenceToType( const ReferenceToType & other );
275        virtual ~ReferenceToType();
276
277        const std::string & get_name() const { return name; }
278        void set_name( std::string newValue ) { name = newValue; }
279        std::list< Expression* >& get_parameters() { return parameters; }
280        bool get_hoistType() const { return hoistType; }
281        void set_hoistType( bool newValue ) { hoistType = newValue; }
282
283        virtual ReferenceToType *clone() const = 0;
284        virtual void accept( Visitor & v ) = 0;
285        virtual Type *acceptMutator( Mutator & m ) = 0;
286        virtual void print( std::ostream & os, int indent = 0 ) const;
287  protected:
288        virtual std::string typeString() const = 0;
289        std::list< Expression* > parameters;
290        std::string name;
291  private:
292        bool hoistType;
293};
294
295class StructInstType : public ReferenceToType {
296        typedef ReferenceToType Parent;
297  public:
298        StructInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >()  ) : Parent( tq, name, attributes ), baseStruct( 0 ) {}
299        StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
300        StructInstType( const StructInstType & other ) : Parent( other ), baseStruct( other.baseStruct ) {}
301
302        StructDecl *get_baseStruct() const { return baseStruct; }
303        void set_baseStruct( StructDecl *newValue ) { baseStruct = newValue; }
304
305        /// Accesses generic parameters of base struct (NULL if none such)
306        std::list<TypeDecl*> * get_baseParameters();
307
308        virtual bool isComplete() const;
309
310        /// Looks up the members of this struct named "name" and places them into "foundDecls".
311        /// Clones declarations into "foundDecls", caller responsible for freeing
312        void lookup( const std::string & name, std::list< Declaration* > & foundDecls ) const;
313
314        virtual StructInstType *clone() const { return new StructInstType( *this ); }
315        virtual void accept( Visitor & v ) { v.visit( this ); }
316        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
317
318        virtual void print( std::ostream & os, int indent = 0 ) const;
319  private:
320        virtual std::string typeString() const;
321
322        // this decl is not "owned" by the struct inst; it is merely a pointer to elsewhere in the tree,
323        // where the structure used in this type is actually defined
324        StructDecl *baseStruct;
325};
326
327class UnionInstType : public ReferenceToType {
328        typedef ReferenceToType Parent;
329  public:
330        UnionInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >()  ) : Parent( tq, name, attributes ), baseUnion( 0 ) {}
331        UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
332        UnionInstType( const UnionInstType & other ) : Parent( other ), baseUnion( other.baseUnion ) {}
333
334        UnionDecl *get_baseUnion() const { return baseUnion; }
335        void set_baseUnion( UnionDecl * newValue ) { baseUnion = newValue; }
336
337        /// Accesses generic parameters of base union (NULL if none such)
338        std::list< TypeDecl * > * get_baseParameters();
339
340        virtual bool isComplete() const;
341
342        /// looks up the members of this union named "name" and places them into "foundDecls"
343        /// Clones declarations into "foundDecls", caller responsible for freeing
344        void lookup( const std::string & name, std::list< Declaration* > & foundDecls ) const;
345
346        virtual UnionInstType *clone() const { return new UnionInstType( *this ); }
347        virtual void accept( Visitor & v ) { v.visit( this ); }
348        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
349
350        virtual void print( std::ostream & os, int indent = 0 ) const;
351  private:
352        virtual std::string typeString() const;
353
354        // this decl is not "owned" by the union inst; it is merely a pointer to elsewhere in the tree,
355        // where the union used in this type is actually defined
356        UnionDecl *baseUnion;
357};
358
359class EnumInstType : public ReferenceToType {
360        typedef ReferenceToType Parent;
361  public:
362        EnumInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >()  ) : Parent( tq, name, attributes ) {}
363        EnumInstType( const Type::Qualifiers & tq, EnumDecl * baseEnum, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
364        EnumInstType( const EnumInstType & other ) : Parent( other ), baseEnum( other.baseEnum ) {}
365
366        EnumDecl *get_baseEnum() const { return baseEnum; }
367        void set_baseEnum( EnumDecl *newValue ) { baseEnum = newValue; }
368
369        virtual bool isComplete() const;
370
371        virtual EnumInstType *clone() const { return new EnumInstType( *this ); }
372        virtual void accept( Visitor & v ) { v.visit( this ); }
373        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
374  private:
375        virtual std::string typeString() const;
376
377        // this decl is not "owned" by the union inst; it is merely a pointer to elsewhere in the tree,
378        // where the union used in this type is actually defined
379        EnumDecl *baseEnum = nullptr;
380};
381
382class TraitInstType : public ReferenceToType {
383        typedef ReferenceToType Parent;
384  public:
385        TraitInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >()  ) : Parent( tq, name, attributes ) {}
386        TraitInstType( const TraitInstType & other );
387        ~TraitInstType();
388
389        std::list< Declaration* >& get_members() { return members; }
390
391        virtual bool isComplete() const;
392
393        virtual TraitInstType *clone() const { return new TraitInstType( *this ); }
394        virtual void accept( Visitor & v ) { v.visit( this ); }
395        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
396  private:
397        virtual std::string typeString() const;
398
399        // this member is filled in by the validate pass, which instantiates the members of the correponding
400        // aggregate with the actual type parameters specified for this use of the context
401        std::list< Declaration* > members;
402};
403
404class TypeInstType : public ReferenceToType {
405        typedef ReferenceToType Parent;
406  public:
407        TypeInstType( const Type::Qualifiers & tq, const std::string & name, TypeDecl *baseType, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
408        TypeInstType( const Type::Qualifiers & tq, const std::string & name, bool isFtype, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
409        TypeInstType( const TypeInstType & other );
410        ~TypeInstType();
411
412        TypeDecl *get_baseType() const { return baseType; }
413        void set_baseType( TypeDecl *newValue );
414        bool get_isFtype() const { return isFtype; }
415        void set_isFtype( bool newValue ) { isFtype = newValue; }
416
417        virtual bool isComplete() const;
418
419        virtual TypeInstType *clone() const { return new TypeInstType( *this ); }
420        virtual void accept( Visitor & v ) { v.visit( this ); }
421        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
422        virtual void print( std::ostream & os, int indent = 0 ) const;
423  private:
424        virtual std::string typeString() const;
425        // this decl is not "owned" by the type inst; it is merely a pointer to elsewhere in the tree,
426        // where the type used here is actually defined
427        TypeDecl *baseType;
428        bool isFtype;
429};
430
431class TupleType : public Type {
432  public:
433        TupleType( const Type::Qualifiers & tq, const std::list< Type * > & types = std::list< Type * >(), const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
434        TupleType( const TupleType& );
435        virtual ~TupleType();
436
437        typedef std::list<Type*> value_type;
438        typedef value_type::iterator iterator;
439
440        std::list<Type*>& get_types() { return types; }
441        virtual unsigned size() const { return types.size(); };
442
443        iterator begin() { return types.begin(); }
444        iterator end() { return types.end(); }
445
446        virtual Type * getComponent( unsigned i ) {
447                assertf( i < size(), "TupleType::getComponent: index %d must be less than size %d", i, size() );
448                return *(begin()+i);
449        }
450
451        // virtual bool isComplete() const { return true; } // xxx - not sure if this is right, might need to recursively check complete-ness
452
453        virtual TupleType *clone() const { return new TupleType( *this ); }
454        virtual void accept( Visitor & v ) { v.visit( this ); }
455        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
456        virtual void print( std::ostream & os, int indent = 0 ) const;
457  private:
458        std::list<Type*> types;
459};
460
461class TypeofType : public Type {
462  public:
463        TypeofType( const Type::Qualifiers & tq, Expression *expr, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
464        TypeofType( const TypeofType& );
465        virtual ~TypeofType();
466
467        Expression *get_expr() const { return expr; }
468        void set_expr( Expression *newValue ) { expr = newValue; }
469
470        virtual bool isComplete() const { assert( false ); return false; }
471
472        virtual TypeofType *clone() const { return new TypeofType( *this ); }
473        virtual void accept( Visitor & v ) { v.visit( this ); }
474        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
475        virtual void print( std::ostream & os, int indent = 0 ) const;
476  private:
477        Expression *expr;
478};
479
480class AttrType : public Type {
481  public:
482        AttrType( const Type::Qualifiers & tq, const std::string & name, Expression *expr, const std::list< Attribute * > & attributes = std::list< Attribute * >() );
483        AttrType( const Type::Qualifiers & tq, const std::string & name, Type *type, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
484        AttrType( const AttrType& );
485        virtual ~AttrType();
486
487        const std::string & get_name() const { return name; }
488        void set_name( const std::string & newValue ) { name = newValue; }
489        Expression *get_expr() const { return expr; }
490        void set_expr( Expression *newValue ) { expr = newValue; }
491        Type *get_type() const { return type; }
492        void set_type( Type *newValue ) { type = newValue; }
493        bool get_isType() const { return isType; }
494        void set_isType( bool newValue ) { isType = newValue; }
495
496        virtual bool isComplete() const { assert( false ); } // xxx - not sure what to do here
497
498        virtual AttrType *clone() const { return new AttrType( *this ); }
499        virtual void accept( Visitor & v ) { v.visit( this ); }
500        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
501        virtual void print( std::ostream & os, int indent = 0 ) const;
502  private:
503        std::string name;
504        Expression *expr;
505        Type *type;
506        bool isType;
507};
508
509/// Represents the GCC built-in varargs type
510class VarArgsType : public Type {
511  public:
512        VarArgsType();
513        VarArgsType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
514
515        virtual bool isComplete() const{ return true; } // xxx - is this right?
516
517        virtual VarArgsType *clone() const { return new VarArgsType( *this ); }
518        virtual void accept( Visitor & v ) { v.visit( this ); }
519        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
520        virtual void print( std::ostream & os, int indent = 0 ) const;
521};
522
523/// Represents a zero constant
524class ZeroType : public Type {
525  public:
526        ZeroType();
527        ZeroType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
528
529        virtual ZeroType *clone() const { return new ZeroType( *this ); }
530        virtual void accept( Visitor & v ) { v.visit( this ); }
531        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
532        virtual void print( std::ostream & os, int indent = 0 ) const;
533};
534
535/// Represents a one constant
536class OneType : public Type {
537  public:
538        OneType();
539        OneType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >()  );
540
541        virtual OneType *clone() const { return new OneType( *this ); }
542        virtual void accept( Visitor & v ) { v.visit( this ); }
543        virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); }
544        virtual void print( std::ostream & os, int indent = 0 ) const;
545};
546
547std::ostream & operator<<( std::ostream & out, const Type * type );
548
549#endif // TYPE_H
550
551// Local Variables: //
552// tab-width: 4 //
553// mode: c++ //
554// compile-command: "make install" //
555// End: //
Note: See TracBrowser for help on using the repository browser.