source: src/SynTree/Type.h @ aa8f9df

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

Merge branch 'replace-results-list' into tuples

Conflicts:

src/ResolvExpr/AlternativeFinder.cc
src/SymTab/Indexer.cc
src/SynTree/Mutator.cc
src/SynTree/Visitor.cc
src/Tuples/TupleAssignment.cc
src/Tuples/TupleAssignment.h

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