source: src/SynTree/Type.h @ 5802a4f

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 5802a4f was 7933351, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

add tuple cast resolution code

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