source: src/SynTree/Type.h @ d7bcbf5

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 d7bcbf5 was 4cb935e, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

0 and 1 now properly parse and resolve to zero_t and one_t respectively

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