source: translator/SynTree/Type.h @ 2c2242c

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

fixed restrict, fixed parameter copy, introduced name table for types, changed variable after to string

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