source: translator/SynTree/Type.h @ 5c7fb6c

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

add inline and attribute qualifiers, cfa.y comment formatting, fix error message in isIntegralType

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