source: src/SynTree/Type.h @ 0c92c9f

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 0c92c9f was 4a9ccc3, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

propagate sized status through trait instances

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