source: translator/SynTree/Type.h @ b1a6d6b

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 b1a6d6b was bdd516a, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

fixed sizeof type variable, find lowest cost alternative for sizeof expression, removed unused classes, added compiler flag, remove temporary file for -CFA, formatting

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