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

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 6c3a988f was 8bf784a, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

name mangling for ttype, fix SynTree? operator<< to work with nullptr, add isTtype check, ttype variables are automatically "sized"

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