source: translator/SynTree/Type.h @ c8ffe20b

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

reformat files

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