source: translator/SynTree/Type.h @ 134b86a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 134b86a was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

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