[0dd3a2f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[ae63a18] | 7 | // Type.h -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[65cdc1e] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed Aug 9 14:25:00 2017 |
---|
| 13 | // Update Count : 152 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[138e29e] | 18 | #include "BaseSyntaxNode.h" |
---|
| 19 | #include "Mutator.h" |
---|
[51b7345] | 20 | #include "SynTree.h" |
---|
| 21 | #include "Visitor.h" |
---|
[6f95000] | 22 | #include <strings.h> // ffs |
---|
[51b7345] | 23 | |
---|
[138e29e] | 24 | class Type : public BaseSyntaxNode { |
---|
[c8ffe20b] | 25 | public: |
---|
[6f95000] | 26 | // Simulate inheritance because union does not allow it. |
---|
[615a096] | 27 | // Bug in g++-4.9 prevents static field in union |
---|
| 28 | //static const char * Names[]; |
---|
[6f95000] | 29 | #define BFCommon( BFType, N ) \ |
---|
[d6d747d] | 30 | bool operator[]( unsigned int i ) const { return val & (1 << i); } \ |
---|
| 31 | bool any() const { return val != 0; } \ |
---|
[6f95000] | 32 | void reset() { val = 0; } \ |
---|
| 33 | int ffs() { return ::ffs( val ) - 1; } \ |
---|
| 34 | BFType operator&=( BFType other ) { \ |
---|
| 35 | val &= other.val; return *this; \ |
---|
| 36 | } \ |
---|
| 37 | BFType operator&( BFType other ) const { \ |
---|
| 38 | BFType q = other; \ |
---|
| 39 | q &= *this; \ |
---|
| 40 | return q; \ |
---|
| 41 | } \ |
---|
| 42 | BFType operator|=( BFType other ) { \ |
---|
| 43 | val |= other.val; return *this; \ |
---|
| 44 | } \ |
---|
| 45 | BFType operator|( BFType other ) const { \ |
---|
| 46 | BFType q = other; \ |
---|
| 47 | q |= *this; \ |
---|
| 48 | return q; \ |
---|
| 49 | } \ |
---|
| 50 | BFType operator-=( BFType other ) { \ |
---|
| 51 | val &= ~other.val; return *this; \ |
---|
| 52 | } \ |
---|
[d6d747d] | 53 | void print( std::ostream & os ) const { \ |
---|
| 54 | if ( (*this).any() ) { \ |
---|
| 55 | for ( unsigned int i = 0; i < N; i += 1 ) { \ |
---|
| 56 | if ( (*this)[i] ) { \ |
---|
[615a096] | 57 | os << BFType##Names[i] << ' '; \ |
---|
[d6d747d] | 58 | } \ |
---|
| 59 | } \ |
---|
| 60 | } \ |
---|
| 61 | } |
---|
| 62 | |
---|
[68fe077a] | 63 | // enum must remain in the same order as the corresponding bit fields. |
---|
| 64 | |
---|
[ddfd945] | 65 | enum { Inline = 1 << 0, Noreturn = 1 << 1, Fortran = 1 << 2, NumFuncSpecifier = 3 }; |
---|
[615a096] | 66 | static const char * FuncSpecifiersNames[]; |
---|
[ddfd945] | 67 | union FuncSpecifiers { |
---|
| 68 | unsigned int val; |
---|
| 69 | struct { |
---|
| 70 | bool is_inline : 1; |
---|
| 71 | bool is_noreturn : 1; |
---|
| 72 | bool is_fortran : 1; |
---|
| 73 | }; |
---|
| 74 | FuncSpecifiers() : val( 0 ) {} |
---|
| 75 | FuncSpecifiers( unsigned int val ) : val( val ) {} |
---|
[615a096] | 76 | // equality (==, !=) works implicitly on first field "val", relational operations are undefined. |
---|
[6f95000] | 77 | BFCommon( FuncSpecifiers, NumFuncSpecifier ) |
---|
[ddfd945] | 78 | }; // FuncSpecifiers |
---|
| 79 | |
---|
[68fe077a] | 80 | enum { Extern = 1 << 0, Static = 1 << 1, Auto = 1 << 2, Register = 1 << 3, Threadlocal = 1 << 4, NumStorageClass = 5 }; |
---|
[615a096] | 81 | static const char * StorageClassesNames[]; |
---|
[68fe077a] | 82 | union StorageClasses { |
---|
| 83 | unsigned int val; |
---|
| 84 | struct { |
---|
| 85 | bool is_extern : 1; |
---|
| 86 | bool is_static : 1; |
---|
| 87 | bool is_auto : 1; |
---|
| 88 | bool is_register : 1; |
---|
| 89 | bool is_threadlocal : 1; |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | StorageClasses() : val( 0 ) {} |
---|
| 93 | StorageClasses( unsigned int val ) : val( val ) {} |
---|
[615a096] | 94 | // equality (==, !=) works implicitly on first field "val", relational operations are undefined. |
---|
[6f95000] | 95 | BFCommon( StorageClasses, NumStorageClass ) |
---|
[68fe077a] | 96 | }; // StorageClasses |
---|
| 97 | |
---|
[bf4ac09] | 98 | enum { Const = 1 << 0, Restrict = 1 << 1, Volatile = 1 << 2, Lvalue = 1 << 3, Mutex = 1 << 4, Atomic = 1 << 5, NumTypeQualifier = 6 }; |
---|
[615a096] | 99 | static const char * QualifiersNames[]; |
---|
[bf4ac09] | 100 | union Qualifiers { |
---|
| 101 | enum { Mask = ~(Restrict | Lvalue) }; |
---|
| 102 | unsigned int val; |
---|
| 103 | struct { |
---|
[615a096] | 104 | bool is_const : 1; |
---|
| 105 | bool is_restrict : 1; |
---|
| 106 | bool is_volatile : 1; |
---|
| 107 | bool is_lvalue : 1; |
---|
| 108 | bool is_mutex : 1; |
---|
| 109 | bool is_atomic : 1; |
---|
[bf4ac09] | 110 | }; |
---|
[6e8bd43] | 111 | |
---|
[bf4ac09] | 112 | Qualifiers() : val( 0 ) {} |
---|
| 113 | Qualifiers( unsigned int val ) : val( val ) {} |
---|
[615a096] | 114 | // Complex comparisons provide implicit qualifier downcasting, e.g., T downcast to const T. |
---|
| 115 | bool operator==( Qualifiers other ) const { return (val & Mask) == (other.val & Mask); } |
---|
| 116 | bool operator!=( Qualifiers other ) const { return (val & Mask) != (other.val & Mask); } |
---|
[d6d747d] | 117 | bool operator<=( Qualifiers other ) const { |
---|
[e04b636] | 118 | return is_const <= other.is_const //Any non-const converts to const without cost |
---|
| 119 | && is_volatile <= other.is_volatile //Any non-volatile converts to volatile without cost |
---|
| 120 | && is_mutex >= other.is_mutex //Any mutex converts to non-mutex without cost |
---|
| 121 | && is_atomic == other.is_atomic; //No conversion from atomic to non atomic is free |
---|
[bf4ac09] | 122 | } |
---|
[6f95000] | 123 | bool operator<( Qualifiers other ) const { return *this != other && *this <= other; } |
---|
| 124 | bool operator>=( Qualifiers other ) const { return ! (*this < other); } |
---|
| 125 | bool operator>( Qualifiers other ) const { return *this != other && *this >= other; } |
---|
| 126 | BFCommon( Qualifiers, NumTypeQualifier ) |
---|
[bf4ac09] | 127 | }; // Qualifiers |
---|
[0dd3a2f] | 128 | |
---|
[65cdc1e] | 129 | typedef std::list<TypeDecl *> ForallList; |
---|
| 130 | |
---|
| 131 | Qualifiers tq; |
---|
| 132 | ForallList forall; |
---|
| 133 | std::list< Attribute * > attributes; |
---|
| 134 | |
---|
[f2e40a9f] | 135 | Type( const Qualifiers & tq, const std::list< Attribute * > & attributes ); |
---|
| 136 | Type( const Type & other ); |
---|
[0dd3a2f] | 137 | virtual ~Type(); |
---|
| 138 | |
---|
[f2e40a9f] | 139 | Qualifiers & get_qualifiers() { return tq; } |
---|
[615a096] | 140 | bool get_const() { return tq.is_const; } |
---|
| 141 | bool get_volatile() { return tq.is_volatile; } |
---|
| 142 | bool get_restrict() { return tq.is_restrict; } |
---|
| 143 | bool get_lvalue() { return tq.is_lvalue; } |
---|
| 144 | bool get_mutex() { return tq.is_mutex; } |
---|
| 145 | bool get_atomic() { return tq.is_atomic; } |
---|
| 146 | void set_const( bool newValue ) { tq.is_const = newValue; } |
---|
| 147 | void set_volatile( bool newValue ) { tq.is_volatile = newValue; } |
---|
| 148 | void set_restrict( bool newValue ) { tq.is_restrict = newValue; } |
---|
| 149 | void set_lvalue( bool newValue ) { tq.is_lvalue = newValue; } |
---|
| 150 | void set_mutex( bool newValue ) { tq.is_mutex = newValue; } |
---|
| 151 | void set_atomic( bool newValue ) { tq.is_atomic = newValue; } |
---|
[8c49c0e] | 152 | |
---|
| 153 | ForallList& get_forall() { return forall; } |
---|
[0dd3a2f] | 154 | |
---|
[c0aa336] | 155 | std::list< Attribute * >& get_attributes() { return attributes; } |
---|
| 156 | const std::list< Attribute * >& get_attributes() const { return attributes; } |
---|
| 157 | |
---|
[906e24d] | 158 | /// How many elemental types are represented by this type |
---|
| 159 | virtual unsigned size() const { return 1; }; |
---|
| 160 | virtual bool isVoid() const { return size() == 0; } |
---|
[7933351] | 161 | virtual Type * getComponent( unsigned i ) { assertf( size() == 1 && i == 0, "Type::getComponent was called with size %d and index %d\n", size(), i ); return this; } |
---|
[906e24d] | 162 | |
---|
[142cf5d] | 163 | /// return type without outer pointers and arrays |
---|
[0698aa1] | 164 | Type * stripDeclarator(); |
---|
| 165 | |
---|
| 166 | /// return type without outer references |
---|
| 167 | Type * stripReferences(); |
---|
[6f95000] | 168 | |
---|
[e6cee92] | 169 | /// return the number of references occuring consecutively on the outermost layer of this type (i.e. do not count references nested within other types) |
---|
| 170 | virtual int referenceDepth() const; |
---|
| 171 | |
---|
[4a9ccc3] | 172 | virtual bool isComplete() const { return true; } |
---|
| 173 | |
---|
[0dd3a2f] | 174 | virtual Type *clone() const = 0; |
---|
[f2e40a9f] | 175 | virtual void accept( Visitor & v ) = 0; |
---|
| 176 | virtual Type *acceptMutator( Mutator & m ) = 0; |
---|
| 177 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[51b7345] | 178 | }; |
---|
| 179 | |
---|
[65cdc1e] | 180 | extern const Type::FuncSpecifiers noFuncSpecifiers; |
---|
| 181 | extern const Type::StorageClasses noStorageClasses; |
---|
| 182 | extern const Type::Qualifiers noQualifiers; // no qualifiers on constants |
---|
[4cb935e] | 183 | |
---|
[c8ffe20b] | 184 | class VoidType : public Type { |
---|
| 185 | public: |
---|
[f2e40a9f] | 186 | VoidType( const Type::Qualifiers & tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[51b7345] | 187 | |
---|
[906e24d] | 188 | virtual unsigned size() const { return 0; }; |
---|
[4a9ccc3] | 189 | virtual bool isComplete() const { return false; } |
---|
[906e24d] | 190 | |
---|
[0dd3a2f] | 191 | virtual VoidType *clone() const { return new VoidType( *this ); } |
---|
[f2e40a9f] | 192 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 193 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 194 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[51b7345] | 195 | }; |
---|
| 196 | |
---|
[c8ffe20b] | 197 | class BasicType : public Type { |
---|
| 198 | public: |
---|
[ae63a18] | 199 | enum Kind { |
---|
[0dd3a2f] | 200 | Bool, |
---|
| 201 | Char, |
---|
| 202 | SignedChar, |
---|
| 203 | UnsignedChar, |
---|
| 204 | ShortSignedInt, |
---|
| 205 | ShortUnsignedInt, |
---|
| 206 | SignedInt, |
---|
| 207 | UnsignedInt, |
---|
| 208 | LongSignedInt, |
---|
| 209 | LongUnsignedInt, |
---|
| 210 | LongLongSignedInt, |
---|
| 211 | LongLongUnsignedInt, |
---|
| 212 | Float, |
---|
| 213 | Double, |
---|
| 214 | LongDouble, |
---|
| 215 | FloatComplex, |
---|
| 216 | DoubleComplex, |
---|
| 217 | LongDoubleComplex, |
---|
| 218 | FloatImaginary, |
---|
| 219 | DoubleImaginary, |
---|
| 220 | LongDoubleImaginary, |
---|
| 221 | NUMBER_OF_BASIC_TYPES |
---|
[65cdc1e] | 222 | } kind; |
---|
[0dd3a2f] | 223 | |
---|
[59db689] | 224 | static const char *typeNames[]; // string names for basic types, MUST MATCH with Kind |
---|
[0dd3a2f] | 225 | |
---|
[f2e40a9f] | 226 | BasicType( const Type::Qualifiers & tq, Kind bt, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[0dd3a2f] | 227 | |
---|
| 228 | Kind get_kind() { return kind; } |
---|
| 229 | void set_kind( Kind newValue ) { kind = newValue; } |
---|
| 230 | |
---|
| 231 | virtual BasicType *clone() const { return new BasicType( *this ); } |
---|
[f2e40a9f] | 232 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 233 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 234 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[0dd3a2f] | 235 | |
---|
| 236 | bool isInteger() const; |
---|
[51b7345] | 237 | }; |
---|
| 238 | |
---|
[c8ffe20b] | 239 | class PointerType : public Type { |
---|
| 240 | public: |
---|
[65cdc1e] | 241 | Type *base; |
---|
| 242 | |
---|
| 243 | // In C99, pointer types can be qualified in many ways e.g., int f( int a[ static 3 ] ) |
---|
| 244 | Expression *dimension; |
---|
| 245 | bool isVarLen; |
---|
| 246 | bool isStatic; |
---|
| 247 | |
---|
[f2e40a9f] | 248 | PointerType( const Type::Qualifiers & tq, Type *base, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 249 | PointerType( const Type::Qualifiers & tq, Type *base, Expression *dimension, bool isVarLen, bool isStatic, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[0dd3a2f] | 250 | PointerType( const PointerType& ); |
---|
| 251 | virtual ~PointerType(); |
---|
| 252 | |
---|
| 253 | Type *get_base() { return base; } |
---|
| 254 | void set_base( Type *newValue ) { base = newValue; } |
---|
| 255 | Expression *get_dimension() { return dimension; } |
---|
| 256 | void set_dimension( Expression *newValue ) { dimension = newValue; } |
---|
| 257 | bool get_isVarLen() { return isVarLen; } |
---|
| 258 | void set_isVarLen( bool newValue ) { isVarLen = newValue; } |
---|
| 259 | bool get_isStatic() { return isStatic; } |
---|
| 260 | void set_isStatic( bool newValue ) { isStatic = newValue; } |
---|
| 261 | |
---|
[ed8a0d2] | 262 | bool is_array() const { return isStatic || isVarLen || dimension; } |
---|
| 263 | |
---|
[ce8c12f] | 264 | virtual bool isComplete() const { return ! isVarLen; } |
---|
| 265 | |
---|
[0dd3a2f] | 266 | virtual PointerType *clone() const { return new PointerType( *this ); } |
---|
[f2e40a9f] | 267 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 268 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 269 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[65cdc1e] | 270 | }; |
---|
[ae63a18] | 271 | |
---|
[65cdc1e] | 272 | class ArrayType : public Type { |
---|
| 273 | public: |
---|
| 274 | Type *base; |
---|
[0dd3a2f] | 275 | Expression *dimension; |
---|
| 276 | bool isVarLen; |
---|
| 277 | bool isStatic; |
---|
[51b7345] | 278 | |
---|
[f2e40a9f] | 279 | ArrayType( const Type::Qualifiers & tq, Type *base, Expression *dimension, bool isVarLen, bool isStatic, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[0dd3a2f] | 280 | ArrayType( const ArrayType& ); |
---|
| 281 | virtual ~ArrayType(); |
---|
| 282 | |
---|
| 283 | Type *get_base() { return base; } |
---|
| 284 | void set_base( Type *newValue ) { base = newValue; } |
---|
| 285 | Expression *get_dimension() { return dimension; } |
---|
| 286 | void set_dimension( Expression *newValue ) { dimension = newValue; } |
---|
| 287 | bool get_isVarLen() { return isVarLen; } |
---|
| 288 | void set_isVarLen( bool newValue ) { isVarLen = newValue; } |
---|
| 289 | bool get_isStatic() { return isStatic; } |
---|
| 290 | void set_isStatic( bool newValue ) { isStatic = newValue; } |
---|
| 291 | |
---|
[4a9ccc3] | 292 | virtual bool isComplete() const { return ! isVarLen; } |
---|
| 293 | |
---|
[0dd3a2f] | 294 | virtual ArrayType *clone() const { return new ArrayType( *this ); } |
---|
[f2e40a9f] | 295 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 296 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 297 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[51b7345] | 298 | }; |
---|
| 299 | |
---|
[ce8c12f] | 300 | class ReferenceType : public Type { |
---|
| 301 | public: |
---|
[9236060] | 302 | Type *base; |
---|
| 303 | |
---|
[ce8c12f] | 304 | ReferenceType( const Type::Qualifiers & tq, Type *base, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 305 | ReferenceType( const ReferenceType & ); |
---|
| 306 | virtual ~ReferenceType(); |
---|
| 307 | |
---|
| 308 | Type *get_base() { return base; } |
---|
| 309 | void set_base( Type *newValue ) { base = newValue; } |
---|
| 310 | |
---|
[e6cee92] | 311 | virtual int referenceDepth() const; |
---|
| 312 | |
---|
[5ccb10d] | 313 | // Since reference types act like value types, their size is the size of the base. |
---|
| 314 | // This makes it simple to cast the empty tuple to a reference type, since casts that increase |
---|
| 315 | // the number of values are disallowed. |
---|
| 316 | virtual unsigned size() const { return base->size(); } |
---|
| 317 | |
---|
[ce8c12f] | 318 | virtual ReferenceType *clone() const { return new ReferenceType( *this ); } |
---|
| 319 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 320 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 321 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
| 322 | }; |
---|
| 323 | |
---|
[c8ffe20b] | 324 | class FunctionType : public Type { |
---|
| 325 | public: |
---|
[65cdc1e] | 326 | std::list<DeclarationWithType*> returnVals; |
---|
| 327 | std::list<DeclarationWithType*> parameters; |
---|
| 328 | |
---|
| 329 | // Does the function accept a variable number of arguments following the arguments specified in the parameters list. |
---|
| 330 | // This could be because of |
---|
| 331 | // - an ellipsis in a prototype declaration |
---|
| 332 | // - an unprototyped declaration |
---|
| 333 | bool isVarArgs; |
---|
| 334 | |
---|
[f2e40a9f] | 335 | FunctionType( const Type::Qualifiers & tq, bool isVarArgs, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[0dd3a2f] | 336 | FunctionType( const FunctionType& ); |
---|
| 337 | virtual ~FunctionType(); |
---|
| 338 | |
---|
[cf16f94] | 339 | std::list<DeclarationWithType*> & get_returnVals() { return returnVals; } |
---|
| 340 | std::list<DeclarationWithType*> & get_parameters() { return parameters; } |
---|
[8bf784a] | 341 | bool get_isVarArgs() const { return isVarArgs; } |
---|
[0dd3a2f] | 342 | void set_isVarArgs( bool newValue ) { isVarArgs = newValue; } |
---|
[8bf784a] | 343 | bool isTtype() const; |
---|
| 344 | |
---|
[0dd3a2f] | 345 | virtual FunctionType *clone() const { return new FunctionType( *this ); } |
---|
[f2e40a9f] | 346 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 347 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 348 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[51b7345] | 349 | }; |
---|
| 350 | |
---|
[c8ffe20b] | 351 | class ReferenceToType : public Type { |
---|
| 352 | public: |
---|
[65cdc1e] | 353 | std::list< Expression* > parameters; |
---|
| 354 | std::string name; |
---|
| 355 | bool hoistType; |
---|
| 356 | |
---|
[f2e40a9f] | 357 | ReferenceToType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes ); |
---|
| 358 | ReferenceToType( const ReferenceToType & other ); |
---|
[0dd3a2f] | 359 | virtual ~ReferenceToType(); |
---|
| 360 | |
---|
[f2e40a9f] | 361 | const std::string & get_name() const { return name; } |
---|
[0dd3a2f] | 362 | void set_name( std::string newValue ) { name = newValue; } |
---|
| 363 | std::list< Expression* >& get_parameters() { return parameters; } |
---|
[43c89a7] | 364 | bool get_hoistType() const { return hoistType; } |
---|
| 365 | void set_hoistType( bool newValue ) { hoistType = newValue; } |
---|
[ae63a18] | 366 | |
---|
[0dd3a2f] | 367 | virtual ReferenceToType *clone() const = 0; |
---|
[f2e40a9f] | 368 | virtual void accept( Visitor & v ) = 0; |
---|
| 369 | virtual Type *acceptMutator( Mutator & m ) = 0; |
---|
| 370 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[6013bd7] | 371 | |
---|
[b3c36f4] | 372 | virtual void lookup( __attribute__((unused)) const std::string & name, __attribute__((unused)) std::list< Declaration* > & foundDecls ) const {} |
---|
[c8ffe20b] | 373 | protected: |
---|
[0dd3a2f] | 374 | virtual std::string typeString() const = 0; |
---|
[51b7345] | 375 | }; |
---|
| 376 | |
---|
[c8ffe20b] | 377 | class StructInstType : public ReferenceToType { |
---|
[0dd3a2f] | 378 | typedef ReferenceToType Parent; |
---|
[c8ffe20b] | 379 | public: |
---|
[65cdc1e] | 380 | // this decl is not "owned" by the struct inst; it is merely a pointer to elsewhere in the tree, |
---|
| 381 | // where the structure used in this type is actually defined |
---|
| 382 | StructDecl *baseStruct; |
---|
| 383 | |
---|
[f2e40a9f] | 384 | StructInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ), baseStruct( 0 ) {} |
---|
| 385 | StructInstType( const Type::Qualifiers & tq, StructDecl * baseStruct, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 386 | StructInstType( const StructInstType & other ) : Parent( other ), baseStruct( other.baseStruct ) {} |
---|
[51b7345] | 387 | |
---|
[0dd3a2f] | 388 | StructDecl *get_baseStruct() const { return baseStruct; } |
---|
| 389 | void set_baseStruct( StructDecl *newValue ) { baseStruct = newValue; } |
---|
[37a3b8f9] | 390 | |
---|
[ed94eac] | 391 | /// Accesses generic parameters of base struct (NULL if none such) |
---|
[839ccbb] | 392 | std::list<TypeDecl*> * get_baseParameters(); |
---|
[ae63a18] | 393 | |
---|
[4a9ccc3] | 394 | virtual bool isComplete() const; |
---|
| 395 | |
---|
[37a3b8f9] | 396 | /// Looks up the members of this struct named "name" and places them into "foundDecls". |
---|
| 397 | /// Clones declarations into "foundDecls", caller responsible for freeing |
---|
[f2e40a9f] | 398 | void lookup( const std::string & name, std::list< Declaration* > & foundDecls ) const; |
---|
[51b7345] | 399 | |
---|
[0dd3a2f] | 400 | virtual StructInstType *clone() const { return new StructInstType( *this ); } |
---|
[f2e40a9f] | 401 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 402 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
[5d125e4] | 403 | |
---|
[f2e40a9f] | 404 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[c8ffe20b] | 405 | private: |
---|
[0dd3a2f] | 406 | virtual std::string typeString() const; |
---|
[51b7345] | 407 | }; |
---|
| 408 | |
---|
[c8ffe20b] | 409 | class UnionInstType : public ReferenceToType { |
---|
[0dd3a2f] | 410 | typedef ReferenceToType Parent; |
---|
[c8ffe20b] | 411 | public: |
---|
[65cdc1e] | 412 | // this decl is not "owned" by the union inst; it is merely a pointer to elsewhere in the tree, |
---|
| 413 | // where the union used in this type is actually defined |
---|
| 414 | UnionDecl *baseUnion; |
---|
| 415 | |
---|
[f2e40a9f] | 416 | UnionInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ), baseUnion( 0 ) {} |
---|
| 417 | UnionInstType( const Type::Qualifiers & tq, UnionDecl * baseUnion, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 418 | UnionInstType( const UnionInstType & other ) : Parent( other ), baseUnion( other.baseUnion ) {} |
---|
[0dd3a2f] | 419 | |
---|
| 420 | UnionDecl *get_baseUnion() const { return baseUnion; } |
---|
[c0aa336] | 421 | void set_baseUnion( UnionDecl * newValue ) { baseUnion = newValue; } |
---|
[37a3b8f9] | 422 | |
---|
[ed94eac] | 423 | /// Accesses generic parameters of base union (NULL if none such) |
---|
[c0aa336] | 424 | std::list< TypeDecl * > * get_baseParameters(); |
---|
[ae63a18] | 425 | |
---|
[4a9ccc3] | 426 | virtual bool isComplete() const; |
---|
| 427 | |
---|
[37a3b8f9] | 428 | /// looks up the members of this union named "name" and places them into "foundDecls" |
---|
| 429 | /// Clones declarations into "foundDecls", caller responsible for freeing |
---|
[f2e40a9f] | 430 | void lookup( const std::string & name, std::list< Declaration* > & foundDecls ) const; |
---|
[0dd3a2f] | 431 | |
---|
| 432 | virtual UnionInstType *clone() const { return new UnionInstType( *this ); } |
---|
[f2e40a9f] | 433 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 434 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
[5d125e4] | 435 | |
---|
[f2e40a9f] | 436 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[c8ffe20b] | 437 | private: |
---|
[0dd3a2f] | 438 | virtual std::string typeString() const; |
---|
[51b7345] | 439 | }; |
---|
| 440 | |
---|
[c8ffe20b] | 441 | class EnumInstType : public ReferenceToType { |
---|
[0dd3a2f] | 442 | typedef ReferenceToType Parent; |
---|
[c8ffe20b] | 443 | public: |
---|
[65cdc1e] | 444 | // this decl is not "owned" by the union inst; it is merely a pointer to elsewhere in the tree, |
---|
| 445 | // where the union used in this type is actually defined |
---|
| 446 | EnumDecl *baseEnum = nullptr; |
---|
| 447 | |
---|
[f2e40a9f] | 448 | EnumInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ) {} |
---|
| 449 | EnumInstType( const Type::Qualifiers & tq, EnumDecl * baseEnum, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 450 | EnumInstType( const EnumInstType & other ) : Parent( other ), baseEnum( other.baseEnum ) {} |
---|
[51b7345] | 451 | |
---|
[c0aa336] | 452 | EnumDecl *get_baseEnum() const { return baseEnum; } |
---|
| 453 | void set_baseEnum( EnumDecl *newValue ) { baseEnum = newValue; } |
---|
| 454 | |
---|
| 455 | virtual bool isComplete() const; |
---|
[4a9ccc3] | 456 | |
---|
[0dd3a2f] | 457 | virtual EnumInstType *clone() const { return new EnumInstType( *this ); } |
---|
[f2e40a9f] | 458 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 459 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
[c8ffe20b] | 460 | private: |
---|
[0dd3a2f] | 461 | virtual std::string typeString() const; |
---|
[51b7345] | 462 | }; |
---|
| 463 | |
---|
[4040425] | 464 | class TraitInstType : public ReferenceToType { |
---|
[0dd3a2f] | 465 | typedef ReferenceToType Parent; |
---|
[c8ffe20b] | 466 | public: |
---|
[65cdc1e] | 467 | // this member is filled in by the validate pass, which instantiates the members of the correponding |
---|
| 468 | // aggregate with the actual type parameters specified for this use of the context |
---|
| 469 | std::list< Declaration* > members; |
---|
| 470 | |
---|
[f2e40a9f] | 471 | TraitInstType( const Type::Qualifiers & tq, const std::string & name, const std::list< Attribute * > & attributes = std::list< Attribute * >() ) : Parent( tq, name, attributes ) {} |
---|
| 472 | TraitInstType( const TraitInstType & other ); |
---|
[4040425] | 473 | ~TraitInstType(); |
---|
[51b7345] | 474 | |
---|
[0dd3a2f] | 475 | std::list< Declaration* >& get_members() { return members; } |
---|
[51b7345] | 476 | |
---|
[4a9ccc3] | 477 | virtual bool isComplete() const; |
---|
| 478 | |
---|
[4040425] | 479 | virtual TraitInstType *clone() const { return new TraitInstType( *this ); } |
---|
[f2e40a9f] | 480 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 481 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
[c8ffe20b] | 482 | private: |
---|
[0dd3a2f] | 483 | virtual std::string typeString() const; |
---|
[51b7345] | 484 | }; |
---|
| 485 | |
---|
[c8ffe20b] | 486 | class TypeInstType : public ReferenceToType { |
---|
[0dd3a2f] | 487 | typedef ReferenceToType Parent; |
---|
[c8ffe20b] | 488 | public: |
---|
[65cdc1e] | 489 | // this decl is not "owned" by the type inst; it is merely a pointer to elsewhere in the tree, |
---|
| 490 | // where the type used here is actually defined |
---|
| 491 | TypeDecl *baseType; |
---|
| 492 | bool isFtype; |
---|
| 493 | |
---|
[f2e40a9f] | 494 | TypeInstType( const Type::Qualifiers & tq, const std::string & name, TypeDecl *baseType, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 495 | TypeInstType( const Type::Qualifiers & tq, const std::string & name, bool isFtype, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 496 | TypeInstType( const TypeInstType & other ); |
---|
[1e8b02f5] | 497 | ~TypeInstType(); |
---|
[0dd3a2f] | 498 | |
---|
| 499 | TypeDecl *get_baseType() const { return baseType; } |
---|
| 500 | void set_baseType( TypeDecl *newValue ); |
---|
| 501 | bool get_isFtype() const { return isFtype; } |
---|
| 502 | void set_isFtype( bool newValue ) { isFtype = newValue; } |
---|
[ae63a18] | 503 | |
---|
[4a9ccc3] | 504 | virtual bool isComplete() const; |
---|
| 505 | |
---|
[0dd3a2f] | 506 | virtual TypeInstType *clone() const { return new TypeInstType( *this ); } |
---|
[f2e40a9f] | 507 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 508 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 509 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[c8ffe20b] | 510 | private: |
---|
[0dd3a2f] | 511 | virtual std::string typeString() const; |
---|
[51b7345] | 512 | }; |
---|
| 513 | |
---|
[c8ffe20b] | 514 | class TupleType : public Type { |
---|
| 515 | public: |
---|
[65cdc1e] | 516 | std::list<Type *> types; |
---|
| 517 | std::list<Declaration *> members; |
---|
| 518 | |
---|
[62423350] | 519 | TupleType( const Type::Qualifiers & tq, const std::list< Type * > & types, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[0dd3a2f] | 520 | TupleType( const TupleType& ); |
---|
| 521 | virtual ~TupleType(); |
---|
[51b7345] | 522 | |
---|
[0362d42] | 523 | typedef std::list<Type*> value_type; |
---|
| 524 | typedef value_type::iterator iterator; |
---|
| 525 | |
---|
[62423350] | 526 | std::list<Type *> & get_types() { return types; } |
---|
[906e24d] | 527 | virtual unsigned size() const { return types.size(); }; |
---|
[51b7345] | 528 | |
---|
[62423350] | 529 | // For now, this is entirely synthetic -- tuple types always have unnamed members. |
---|
| 530 | // Eventually, we may allow named tuples, in which case members should subsume types |
---|
| 531 | std::list<Declaration *> & get_members() { return members; } |
---|
| 532 | |
---|
[0362d42] | 533 | iterator begin() { return types.begin(); } |
---|
| 534 | iterator end() { return types.end(); } |
---|
| 535 | |
---|
[7933351] | 536 | virtual Type * getComponent( unsigned i ) { |
---|
| 537 | assertf( i < size(), "TupleType::getComponent: index %d must be less than size %d", i, size() ); |
---|
| 538 | return *(begin()+i); |
---|
| 539 | } |
---|
| 540 | |
---|
[4a9ccc3] | 541 | // virtual bool isComplete() const { return true; } // xxx - not sure if this is right, might need to recursively check complete-ness |
---|
| 542 | |
---|
[0dd3a2f] | 543 | virtual TupleType *clone() const { return new TupleType( *this ); } |
---|
[f2e40a9f] | 544 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 545 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 546 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[51b7345] | 547 | }; |
---|
| 548 | |
---|
[c8ffe20b] | 549 | class TypeofType : public Type { |
---|
| 550 | public: |
---|
[65cdc1e] | 551 | Expression *expr; |
---|
| 552 | |
---|
[f2e40a9f] | 553 | TypeofType( const Type::Qualifiers & tq, Expression *expr, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[0dd3a2f] | 554 | TypeofType( const TypeofType& ); |
---|
| 555 | virtual ~TypeofType(); |
---|
[51b7345] | 556 | |
---|
[0dd3a2f] | 557 | Expression *get_expr() const { return expr; } |
---|
| 558 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
[51b7345] | 559 | |
---|
[4a9ccc3] | 560 | virtual bool isComplete() const { assert( false ); return false; } |
---|
| 561 | |
---|
[0dd3a2f] | 562 | virtual TypeofType *clone() const { return new TypeofType( *this ); } |
---|
[f2e40a9f] | 563 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 564 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 565 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[51b7345] | 566 | }; |
---|
| 567 | |
---|
[c8ffe20b] | 568 | class AttrType : public Type { |
---|
| 569 | public: |
---|
[65cdc1e] | 570 | std::string name; |
---|
| 571 | Expression *expr; |
---|
| 572 | Type *type; |
---|
| 573 | bool isType; |
---|
| 574 | |
---|
[f2e40a9f] | 575 | AttrType( const Type::Qualifiers & tq, const std::string & name, Expression *expr, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
| 576 | AttrType( const Type::Qualifiers & tq, const std::string & name, Type *type, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[0dd3a2f] | 577 | AttrType( const AttrType& ); |
---|
| 578 | virtual ~AttrType(); |
---|
| 579 | |
---|
[f2e40a9f] | 580 | const std::string & get_name() const { return name; } |
---|
| 581 | void set_name( const std::string & newValue ) { name = newValue; } |
---|
[0dd3a2f] | 582 | Expression *get_expr() const { return expr; } |
---|
| 583 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
| 584 | Type *get_type() const { return type; } |
---|
| 585 | void set_type( Type *newValue ) { type = newValue; } |
---|
| 586 | bool get_isType() const { return isType; } |
---|
| 587 | void set_isType( bool newValue ) { isType = newValue; } |
---|
| 588 | |
---|
[4a9ccc3] | 589 | virtual bool isComplete() const { assert( false ); } // xxx - not sure what to do here |
---|
| 590 | |
---|
[0dd3a2f] | 591 | virtual AttrType *clone() const { return new AttrType( *this ); } |
---|
[f2e40a9f] | 592 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 593 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 594 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[51b7345] | 595 | }; |
---|
| 596 | |
---|
[44b7088] | 597 | /// Represents the GCC built-in varargs type |
---|
| 598 | class VarArgsType : public Type { |
---|
[90c3b1c] | 599 | public: |
---|
[44b7088] | 600 | VarArgsType(); |
---|
[c0aa336] | 601 | VarArgsType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[44b7088] | 602 | |
---|
[4a9ccc3] | 603 | virtual bool isComplete() const{ return true; } // xxx - is this right? |
---|
| 604 | |
---|
[44b7088] | 605 | virtual VarArgsType *clone() const { return new VarArgsType( *this ); } |
---|
[f2e40a9f] | 606 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 607 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 608 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[44b7088] | 609 | }; |
---|
| 610 | |
---|
[89e6ffc] | 611 | /// Represents a zero constant |
---|
| 612 | class ZeroType : public Type { |
---|
| 613 | public: |
---|
| 614 | ZeroType(); |
---|
[c0aa336] | 615 | ZeroType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[89e6ffc] | 616 | |
---|
| 617 | virtual ZeroType *clone() const { return new ZeroType( *this ); } |
---|
[f2e40a9f] | 618 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 619 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 620 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[89e6ffc] | 621 | }; |
---|
| 622 | |
---|
| 623 | /// Represents a one constant |
---|
| 624 | class OneType : public Type { |
---|
| 625 | public: |
---|
| 626 | OneType(); |
---|
[c0aa336] | 627 | OneType( Type::Qualifiers tq, const std::list< Attribute * > & attributes = std::list< Attribute * >() ); |
---|
[89e6ffc] | 628 | |
---|
| 629 | virtual OneType *clone() const { return new OneType( *this ); } |
---|
[f2e40a9f] | 630 | virtual void accept( Visitor & v ) { v.visit( this ); } |
---|
| 631 | virtual Type *acceptMutator( Mutator & m ) { return m.mutate( this ); } |
---|
| 632 | virtual void print( std::ostream & os, int indent = 0 ) const; |
---|
[89e6ffc] | 633 | }; |
---|
| 634 | |
---|
[3906301] | 635 | std::ostream & operator<<( std::ostream & out, const Type * type ); |
---|
[baf7fee] | 636 | |
---|
[0dd3a2f] | 637 | // Local Variables: // |
---|
| 638 | // tab-width: 4 // |
---|
| 639 | // mode: c++ // |
---|
| 640 | // compile-command: "make install" // |
---|
| 641 | // End: // |
---|