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