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

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 6d4d1a6 was 66f8528, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into tuples

Conflicts:

src/ResolvExpr/CommonType.cc
src/tests/.expect/32/extension.txt
src/tests/.expect/32/gccExtensions.txt
src/tests/.expect/64/declarationSpecifier.txt
src/tests/.expect/64/extension.txt
src/tests/.expect/64/gccExtensions.txt
src/tests/.expect/castError.txt
src/tests/Makefile.am

  • 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() { return isVarArgs; }
207        void set_isVarArgs( bool newValue ) { isVarArgs = newValue; }
208
209        virtual FunctionType *clone() const { return new FunctionType( *this ); }
210        virtual void accept( Visitor &v ) { v.visit( this ); }
211        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
212        virtual void print( std::ostream &os, int indent = 0 ) const;
213  private:
214        std::list<DeclarationWithType*> returnVals;
215        std::list<DeclarationWithType*> parameters;
216
217        // Does the function accept a variable number of arguments following the arguments specified in the parameters list.
218        // This could be because of
219        // - an ellipsis in a prototype declaration
220        // - an unprototyped declaration
221        bool isVarArgs;
222};
223
224class ReferenceToType : public Type {
225  public:
226        ReferenceToType( const Type::Qualifiers &tq, const std::string &name );
227        ReferenceToType( const ReferenceToType &other );
228        virtual ~ReferenceToType();
229
230        const std::string &get_name() const { return name; }
231        void set_name( std::string newValue ) { name = newValue; }
232        std::list< Expression* >& get_parameters() { return parameters; }
233
234        virtual ReferenceToType *clone() const = 0;
235        virtual void accept( Visitor &v ) = 0;
236        virtual Type *acceptMutator( Mutator &m ) = 0;
237        virtual void print( std::ostream &os, int indent = 0 ) const;
238  protected:
239        virtual std::string typeString() const = 0;
240        std::list< Expression* > parameters;
241        std::string name;
242  private:
243};
244
245class StructInstType : public ReferenceToType {
246        typedef ReferenceToType Parent;
247  public:
248        StructInstType( const Type::Qualifiers &tq, const std::string &name ) : Parent( tq, name ), baseStruct( 0 ) {}
249        StructInstType( const Type::Qualifiers &tq, StructDecl * baseStruct );
250        StructInstType( const StructInstType &other ) : Parent( other ), baseStruct( other.baseStruct ) {}
251
252        StructDecl *get_baseStruct() const { return baseStruct; }
253        void set_baseStruct( StructDecl *newValue ) { baseStruct = newValue; }
254
255        /// Accesses generic parameters of base struct (NULL if none such)
256        std::list<TypeDecl*> * get_baseParameters();
257
258        /// Looks up the members of this struct named "name" and places them into "foundDecls".
259        /// Clones declarations into "foundDecls", caller responsible for freeing
260        void lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const;
261
262        virtual StructInstType *clone() const { return new StructInstType( *this ); }
263        virtual void accept( Visitor &v ) { v.visit( this ); }
264        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
265
266        virtual void print( std::ostream &os, int indent = 0 ) const;
267  private:
268        virtual std::string typeString() const;
269
270        // this decl is not "owned" by the struct inst; it is merely a pointer to elsewhere in the tree,
271        // where the structure used in this type is actually defined
272        StructDecl *baseStruct;
273};
274
275class UnionInstType : public ReferenceToType {
276        typedef ReferenceToType Parent;
277  public:
278        UnionInstType( const Type::Qualifiers &tq, const std::string &name ) : Parent( tq, name ), baseUnion( 0 ) {}
279        UnionInstType( const UnionInstType &other ) : Parent( other ), baseUnion( other.baseUnion ) {}
280
281        UnionDecl *get_baseUnion() const { return baseUnion; }
282        void set_baseUnion( UnionDecl *newValue ) { baseUnion = newValue; }
283
284        /// Accesses generic parameters of base union (NULL if none such)
285        std::list<TypeDecl*> * get_baseParameters();
286
287        /// looks up the members of this union named "name" and places them into "foundDecls"
288        /// Clones declarations into "foundDecls", caller responsible for freeing
289        void lookup( const std::string &name, std::list< Declaration* > &foundDecls ) const;
290
291        virtual UnionInstType *clone() const { return new UnionInstType( *this ); }
292        virtual void accept( Visitor &v ) { v.visit( this ); }
293        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
294
295        virtual void print( std::ostream &os, int indent = 0 ) const;
296  private:
297        virtual std::string typeString() const;
298
299        // this decl is not "owned" by the union inst; it is merely a pointer to elsewhere in the tree,
300        // where the union used in this type is actually defined
301        UnionDecl *baseUnion;
302};
303
304class EnumInstType : public ReferenceToType {
305        typedef ReferenceToType Parent;
306  public:
307        EnumInstType( const Type::Qualifiers &tq, const std::string &name ) : Parent( tq, name ) {}
308        EnumInstType( const EnumInstType &other ) : Parent( other ) {}
309
310        virtual EnumInstType *clone() const { return new EnumInstType( *this ); }
311        virtual void accept( Visitor &v ) { v.visit( this ); }
312        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
313  private:
314        virtual std::string typeString() const;
315};
316
317class TraitInstType : public ReferenceToType {
318        typedef ReferenceToType Parent;
319  public:
320        TraitInstType( const Type::Qualifiers &tq, const std::string &name ) : Parent( tq, name ) {}
321        TraitInstType( const TraitInstType &other );
322        ~TraitInstType();
323
324        std::list< Declaration* >& get_members() { return members; }
325
326        virtual TraitInstType *clone() const { return new TraitInstType( *this ); }
327        virtual void accept( Visitor &v ) { v.visit( this ); }
328        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
329  private:
330        virtual std::string typeString() const;
331
332        // this member is filled in by the validate pass, which instantiates the members of the correponding
333        // aggregate with the actual type parameters specified for this use of the context
334        std::list< Declaration* > members;
335};
336
337class TypeInstType : public ReferenceToType {
338        typedef ReferenceToType Parent;
339  public:
340        TypeInstType( const Type::Qualifiers &tq, const std::string &name, TypeDecl *baseType );
341        TypeInstType( const Type::Qualifiers &tq, const std::string &name, bool isFtype );
342        TypeInstType( const TypeInstType &other );
343        ~TypeInstType();
344
345        TypeDecl *get_baseType() const { return baseType; }
346        void set_baseType( TypeDecl *newValue );
347        bool get_isFtype() const { return isFtype; }
348        void set_isFtype( bool newValue ) { isFtype = newValue; }
349
350        virtual TypeInstType *clone() const { return new TypeInstType( *this ); }
351        virtual void accept( Visitor &v ) { v.visit( this ); }
352        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
353        virtual void print( std::ostream &os, int indent = 0 ) const;
354  private:
355        virtual std::string typeString() const;
356        // this decl is not "owned" by the type inst; it is merely a pointer to elsewhere in the tree,
357        // where the type used here is actually defined
358        TypeDecl *baseType;
359        bool isFtype;
360};
361
362class TupleType : public Type {
363  public:
364        TupleType( const Type::Qualifiers &tq, const std::list< Type * > & types = std::list< Type * >() );
365        TupleType( const TupleType& );
366        virtual ~TupleType();
367
368        typedef std::list<Type*> value_type;
369        typedef value_type::iterator iterator;
370
371        std::list<Type*>& get_types() { return types; }
372        virtual unsigned size() const { return types.size(); };
373
374        iterator begin() { return types.begin(); }
375        iterator end() { return types.end(); }
376
377        virtual Type * getComponent( unsigned i ) {
378                assertf( i < size(), "TupleType::getComponent: index %d must be less than size %d", i, size() );
379                return *(begin()+i);
380        }
381
382        virtual TupleType *clone() const { return new TupleType( *this ); }
383        virtual void accept( Visitor &v ) { v.visit( this ); }
384        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
385        virtual void print( std::ostream &os, int indent = 0 ) const;
386  private:
387        std::list<Type*> types;
388};
389
390class TypeofType : public Type {
391  public:
392        TypeofType( const Type::Qualifiers &tq, Expression *expr );
393        TypeofType( const TypeofType& );
394        virtual ~TypeofType();
395
396        Expression *get_expr() const { return expr; }
397        void set_expr( Expression *newValue ) { expr = newValue; }
398
399        virtual TypeofType *clone() const { return new TypeofType( *this ); }
400        virtual void accept( Visitor &v ) { v.visit( this ); }
401        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
402        virtual void print( std::ostream &os, int indent = 0 ) const;
403  private:
404        Expression *expr;
405};
406
407class AttrType : public Type {
408  public:
409        AttrType( const Type::Qualifiers &tq, const std::string &name, Expression *expr );
410        AttrType( const Type::Qualifiers &tq, const std::string &name, Type *type );
411        AttrType( const AttrType& );
412        virtual ~AttrType();
413
414        const std::string &get_name() const { return name; }
415        void set_name( const std::string &newValue ) { name = newValue; }
416        Expression *get_expr() const { return expr; }
417        void set_expr( Expression *newValue ) { expr = newValue; }
418        Type *get_type() const { return type; }
419        void set_type( Type *newValue ) { type = newValue; }
420        bool get_isType() const { return isType; }
421        void set_isType( bool newValue ) { isType = newValue; }
422
423        virtual AttrType *clone() const { return new AttrType( *this ); }
424        virtual void accept( Visitor &v ) { v.visit( this ); }
425        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
426        virtual void print( std::ostream &os, int indent = 0 ) const;
427  private:
428        std::string name;
429        Expression *expr;
430        Type *type;
431        bool isType;
432};
433
434/// Represents the GCC built-in varargs type
435class VarArgsType : public Type {
436  public:
437        VarArgsType();
438        VarArgsType( Type::Qualifiers tq );
439
440        virtual VarArgsType *clone() const { return new VarArgsType( *this ); }
441        virtual void accept( Visitor &v ) { v.visit( this ); }
442        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
443        virtual void print( std::ostream &os, int indent = 0 ) const;
444};
445
446/// Represents a zero constant
447class ZeroType : public Type {
448  public:
449        ZeroType();
450        ZeroType( Type::Qualifiers tq );
451
452        virtual ZeroType *clone() const { return new ZeroType( *this ); }
453        virtual void accept( Visitor &v ) { v.visit( this ); }
454        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
455        virtual void print( std::ostream &os, int indent = 0 ) const;
456};
457
458/// Represents a one constant
459class OneType : public Type {
460  public:
461        OneType();
462        OneType( Type::Qualifiers tq );
463
464        virtual OneType *clone() const { return new OneType( *this ); }
465        virtual void accept( Visitor &v ) { v.visit( this ); }
466        virtual Type *acceptMutator( Mutator &m ) { return m.mutate( this ); }
467        virtual void print( std::ostream &os, int indent = 0 ) const;
468};
469
470inline Type::Qualifiers &Type::Qualifiers::operator&=( const Type::Qualifiers &other ) {
471        isConst &= other.isConst;
472        isVolatile &= other.isVolatile;
473        isRestrict &= other.isRestrict;
474        isLvalue &= other.isLvalue;
475        isAtomic &= other.isAtomic;
476        return *this;
477}
478
479inline Type::Qualifiers &Type::Qualifiers::operator+=( const Type::Qualifiers &other ) {
480        isConst |= other.isConst;
481        isVolatile |= other.isVolatile;
482        isRestrict |= other.isRestrict;
483        isLvalue |= other.isLvalue;
484        isAtomic |= other.isAtomic;
485        return *this;
486}
487
488inline Type::Qualifiers &Type::Qualifiers::operator-=( const Type::Qualifiers &other ) {
489        if ( other.isConst ) isConst = 0;
490        if ( other.isVolatile ) isVolatile = 0;
491        if ( other.isRestrict ) isRestrict = 0;
492        if ( other.isAtomic ) isAtomic = 0;
493        return *this;
494}
495
496inline Type::Qualifiers Type::Qualifiers::operator+( const Type::Qualifiers &other ) {
497        Qualifiers q = other;
498        q += *this;
499        return q;
500}
501
502inline bool Type::Qualifiers::operator==( const Qualifiers &other ) {
503        return isConst == other.isConst
504                && isVolatile == other.isVolatile
505//              && isRestrict == other.isRestrict
506//              && isLvalue == other.isLvalue
507                && isAtomic == other.isAtomic;
508}
509
510inline bool Type::Qualifiers::operator!=( const Qualifiers &other ) {
511        return isConst != other.isConst
512                || isVolatile != other.isVolatile
513//              || isRestrict != other.isRestrict
514//              || isLvalue != other.isLvalue
515                || isAtomic != other.isAtomic;
516}
517
518inline bool Type::Qualifiers::operator<=( const Type::Qualifiers &other ) {
519        return isConst <= other.isConst
520                && isVolatile <= other.isVolatile
521//              && isRestrict <= other.isRestrict
522//              && isLvalue >= other.isLvalue
523                && isAtomic == other.isAtomic;
524}
525
526inline bool Type::Qualifiers::operator>=( const Type::Qualifiers &other ) {
527        return isConst >= other.isConst
528                && isVolatile >= other.isVolatile
529//              && isRestrict >= other.isRestrict
530//              && isLvalue <= other.isLvalue
531                && isAtomic == other.isAtomic;
532}
533
534inline bool Type::Qualifiers::operator<( const Type::Qualifiers &other ) {
535        return operator!=( other ) && operator<=( other );
536}
537
538inline bool Type::Qualifiers::operator>( const Type::Qualifiers &other ) {
539        return operator!=( other ) && operator>=( other );
540}
541
542std::ostream & operator<<( std::ostream & out, const Type * type );
543
544#endif // TYPE_H
545
546// Local Variables: //
547// tab-width: 4 //
548// mode: c++ //
549// compile-command: "make install" //
550// End: //
Note: See TracBrowser for help on using the repository browser.