[2bb4a01] | 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 | // |
---|
| 7 | // Type.hpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Thu May 9 10:00:00 2019 |
---|
| 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Thu May 9 10:00:00 2019 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #pragma once |
---|
| 17 | |
---|
[9e1d485] | 18 | #include <cassert> |
---|
| 19 | #include <cstddef> // for nullptr_t |
---|
| 20 | #include <cstdint> // for uintptr_t |
---|
| 21 | #include <utility> // for move |
---|
| 22 | #include <vector> |
---|
| 23 | |
---|
| 24 | #include "CVQualifiers.hpp" |
---|
[9b4f329] | 25 | #include "Decl.hpp" // for AggregateDecl subclasses |
---|
[9e1d485] | 26 | #include "Fwd.hpp" |
---|
[d76c588] | 27 | #include "Node.hpp" // for Node, ptr, ptr_base |
---|
[9e1d485] | 28 | #include "TypeVar.hpp" |
---|
| 29 | #include "Visitor.hpp" |
---|
[2bb4a01] | 30 | |
---|
[f3cc5b6] | 31 | // Must be included in *all* AST classes; should be #undef'd at the end of the file |
---|
[acd80b4] | 32 | #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node); |
---|
[f3cc5b6] | 33 | |
---|
[2bb4a01] | 34 | namespace ast { |
---|
| 35 | |
---|
| 36 | class Type : public Node { |
---|
[9e1d485] | 37 | public: |
---|
[54e41b3] | 38 | CV::Qualifiers qualifiers; |
---|
[c829320] | 39 | std::vector<ptr<Attribute>> attributes; |
---|
[87701b6] | 40 | |
---|
[1ae47de] | 41 | Type( CV::Qualifiers q = {}, std::vector<ptr<Attribute>> && as = {} ) |
---|
[c829320] | 42 | : qualifiers(q), attributes(std::move(as)) {} |
---|
[54e41b3] | 43 | |
---|
| 44 | bool is_const() const { return qualifiers.is_const; } |
---|
| 45 | bool is_volatile() const { return qualifiers.is_volatile; } |
---|
| 46 | bool is_restrict() const { return qualifiers.is_restrict; } |
---|
| 47 | bool is_lvalue() const { return qualifiers.is_lvalue; } |
---|
| 48 | bool is_mutex() const { return qualifiers.is_mutex; } |
---|
| 49 | bool is_atomic() const { return qualifiers.is_atomic; } |
---|
| 50 | |
---|
[76ed81f] | 51 | Type * set_const( bool v ) { qualifiers.is_const = v; return this; } |
---|
[dafe9e1] | 52 | Type * set_volatile( bool v ) { qualifiers.is_volatile = v; return this; } |
---|
[76ed81f] | 53 | Type * set_restrict( bool v ) { qualifiers.is_restrict = v; return this; } |
---|
| 54 | Type * set_lvalue( bool v ) { qualifiers.is_lvalue = v; return this; } |
---|
| 55 | Type * set_mutex( bool v ) { qualifiers.is_mutex = v; return this; } |
---|
| 56 | Type * set_atomic( bool v ) { qualifiers.is_atomic = v; return this; } |
---|
[9e1d485] | 57 | |
---|
| 58 | /// How many elemental types are represented by this type |
---|
| 59 | virtual unsigned size() const { return 1; } |
---|
| 60 | /// Is this a void type? |
---|
| 61 | virtual bool isVoid() const { return size() == 0; } |
---|
| 62 | /// Get the i'th component of this type |
---|
[d76c588] | 63 | virtual const Type * getComponent( unsigned i ) const; |
---|
[9e1d485] | 64 | |
---|
| 65 | /// type without outer pointers and arrays |
---|
[d76c588] | 66 | const Type * stripDeclarator() const; |
---|
[9e1d485] | 67 | /// type without outer references |
---|
[d76c588] | 68 | const Type * stripReferences() const; |
---|
[9e1d485] | 69 | /// number of reference occuring consecutively on the outermost layer of this type |
---|
| 70 | /// (i.e. do not count references nested within other types) |
---|
| 71 | virtual unsigned referenceDepth() const { return 0; } |
---|
| 72 | /// true iff type is complete type (i.e. compiler knows the size, alignment, and layout) |
---|
| 73 | virtual bool isComplete() const { return true; } |
---|
| 74 | |
---|
[69bafd2] | 75 | virtual const Type * accept( Visitor & v ) const override = 0; |
---|
[9e1d485] | 76 | private: |
---|
| 77 | virtual Type * clone() const override = 0; |
---|
[f3cc5b6] | 78 | MUTATE_FRIEND |
---|
[9e1d485] | 79 | }; |
---|
| 80 | |
---|
[ee574a2] | 81 | /// Clear/reset the qualifiers on this type, cloning only if necessary |
---|
[d76c588] | 82 | template< enum Node::ref_type ref_t > |
---|
[ee574a2] | 83 | void reset_qualifiers( ptr_base< Type, ref_t > & p, CV::Qualifiers q = {} ) { |
---|
| 84 | if ( p->qualifiers.val != q.val ) p.get_and_mutate()->qualifiers = q; |
---|
[d76c588] | 85 | } |
---|
| 86 | |
---|
[ee574a2] | 87 | /// Add the specified qualifiers to this type, cloning only if necessary |
---|
[d76c588] | 88 | template< enum Node::ref_type ref_t > |
---|
[ee574a2] | 89 | void add_qualifiers( ptr_base< Type, ref_t > & p, CV::Qualifiers q ) { |
---|
| 90 | if ( ( p->qualifiers.val & q.val ) != q.val ) p.get_and_mutate()->qualifiers |= q; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /// Remove the specified qualifiers from this type, cloning only if necessary |
---|
| 94 | template< enum Node::ref_type ref_t > |
---|
| 95 | void remove_qualifiers( ptr_base< Type, ref_t > & p, CV::Qualifiers q ) { |
---|
| 96 | if ( ( p->qualifiers.val & q.val ) != 0 ) p.get_and_mutate()->qualifiers -= q; |
---|
[d76c588] | 97 | } |
---|
| 98 | |
---|
[9e1d485] | 99 | /// `void` |
---|
| 100 | class VoidType final : public Type { |
---|
| 101 | public: |
---|
| 102 | VoidType( CV::Qualifiers q = {} ) : Type( q ) {} |
---|
[87701b6] | 103 | |
---|
[9e1d485] | 104 | unsigned size() const override { return 0; } |
---|
| 105 | bool isVoid() const override { return true; } |
---|
| 106 | bool isComplete() const override { return false; } |
---|
| 107 | |
---|
[69bafd2] | 108 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 109 | private: |
---|
| 110 | VoidType * clone() const override { return new VoidType{ *this }; } |
---|
[f3cc5b6] | 111 | MUTATE_FRIEND |
---|
[9e1d485] | 112 | }; |
---|
| 113 | |
---|
| 114 | /// Built-in arithmetic type |
---|
| 115 | class BasicType final : public Type { |
---|
| 116 | public: |
---|
| 117 | // GENERATED START, DO NOT EDIT |
---|
| 118 | // GENERATED BY BasicTypes-gen.cc |
---|
| 119 | enum Kind { |
---|
| 120 | Bool, |
---|
| 121 | Char, |
---|
| 122 | SignedChar, |
---|
| 123 | UnsignedChar, |
---|
| 124 | ShortSignedInt, |
---|
| 125 | ShortUnsignedInt, |
---|
| 126 | SignedInt, |
---|
| 127 | UnsignedInt, |
---|
| 128 | LongSignedInt, |
---|
| 129 | LongUnsignedInt, |
---|
| 130 | LongLongSignedInt, |
---|
| 131 | LongLongUnsignedInt, |
---|
| 132 | SignedInt128, |
---|
| 133 | UnsignedInt128, |
---|
| 134 | uFloat16, |
---|
| 135 | uFloat16Complex, |
---|
| 136 | uFloat32, |
---|
| 137 | uFloat32Complex, |
---|
| 138 | Float, |
---|
| 139 | FloatComplex, |
---|
| 140 | uFloat32x, |
---|
| 141 | uFloat32xComplex, |
---|
| 142 | uFloat64, |
---|
| 143 | uFloat64Complex, |
---|
| 144 | Double, |
---|
| 145 | DoubleComplex, |
---|
| 146 | uFloat64x, |
---|
| 147 | uFloat64xComplex, |
---|
| 148 | uuFloat80, |
---|
| 149 | uFloat128, |
---|
| 150 | uFloat128Complex, |
---|
| 151 | uuFloat128, |
---|
| 152 | LongDouble, |
---|
| 153 | LongDoubleComplex, |
---|
| 154 | uFloat128x, |
---|
| 155 | uFloat128xComplex, |
---|
| 156 | NUMBER_OF_BASIC_TYPES |
---|
| 157 | } kind; |
---|
| 158 | // GENERATED END |
---|
| 159 | |
---|
| 160 | /// xxx -- MAX_INTEGER_TYPE should probably be in BasicTypes-gen.cc, rather than hardcoded here |
---|
| 161 | enum { MAX_INTEGER_TYPE = UnsignedInt128 }; |
---|
| 162 | |
---|
| 163 | /// string names of basic types; generated to match with Kind |
---|
| 164 | static const char *typeNames[]; |
---|
| 165 | |
---|
[0f6a7752] | 166 | BasicType( Kind k, CV::Qualifiers q = {}, std::vector<ptr<Attribute>> && as = {} ) |
---|
| 167 | : Type(q, std::move(as)), kind(k) {} |
---|
[9e1d485] | 168 | |
---|
| 169 | /// Check if this type represents an integer type |
---|
[3648d98] | 170 | bool isInteger() const { return (unsigned)kind <= (unsigned)MAX_INTEGER_TYPE; } |
---|
[9e1d485] | 171 | |
---|
[69bafd2] | 172 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 173 | private: |
---|
| 174 | BasicType * clone() const override { return new BasicType{ *this }; } |
---|
[f3cc5b6] | 175 | MUTATE_FRIEND |
---|
[9e1d485] | 176 | }; |
---|
| 177 | |
---|
| 178 | /// Pointer/array variable length? |
---|
| 179 | enum LengthFlag { FixedLen, VariableLen }; |
---|
| 180 | |
---|
| 181 | /// Pointer/array static dimension? |
---|
| 182 | enum DimensionFlag { DynamicDim, StaticDim }; |
---|
| 183 | |
---|
| 184 | /// Pointer type `T*` |
---|
| 185 | class PointerType final : public Type { |
---|
| 186 | public: |
---|
| 187 | ptr<Type> base; |
---|
| 188 | |
---|
| 189 | // In C99, pointer types can be qualified in many ways, e.g. `int a[ static 3 ]` |
---|
| 190 | ptr<Expr> dimension; |
---|
| 191 | LengthFlag isVarLen = FixedLen; |
---|
| 192 | DimensionFlag isStatic = DynamicDim; |
---|
| 193 | |
---|
| 194 | PointerType( const Type * b, CV::Qualifiers q = {} ) : Type(q), base(b), dimension() {} |
---|
[87701b6] | 195 | PointerType( const Type * b, const Expr * d, LengthFlag vl, DimensionFlag s, |
---|
[9e1d485] | 196 | CV::Qualifiers q = {} ) : Type(q), base(b), dimension(d), isVarLen(vl), isStatic(s) {} |
---|
| 197 | |
---|
| 198 | // true if this pointer is actually an array |
---|
| 199 | bool isArray() const { return isVarLen || isStatic || dimension; } |
---|
| 200 | |
---|
| 201 | bool isComplete() const override { return ! isVarLen; } |
---|
| 202 | |
---|
[69bafd2] | 203 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 204 | private: |
---|
| 205 | PointerType * clone() const override { return new PointerType{ *this }; } |
---|
[f3cc5b6] | 206 | MUTATE_FRIEND |
---|
[9e1d485] | 207 | }; |
---|
| 208 | |
---|
| 209 | /// Array type `T[]` |
---|
| 210 | class ArrayType final : public Type { |
---|
| 211 | public: |
---|
| 212 | ptr<Type> base; |
---|
| 213 | ptr<Expr> dimension; |
---|
| 214 | LengthFlag isVarLen; |
---|
| 215 | DimensionFlag isStatic; |
---|
[2bb4a01] | 216 | |
---|
[87701b6] | 217 | ArrayType( const Type * b, const Expr * d, LengthFlag vl, DimensionFlag s, |
---|
[9e1d485] | 218 | CV::Qualifiers q = {} ) : Type(q), base(b), dimension(d), isVarLen(vl), isStatic(s) {} |
---|
| 219 | |
---|
| 220 | // array types are complete if they have a dimension expression or are |
---|
| 221 | // VLAs ('*' in parameter declaration), and incomplete otherwise. |
---|
| 222 | // See 6.7.6.2 |
---|
| 223 | bool isComplete() const override { return dimension || isVarLen; } |
---|
| 224 | |
---|
[69bafd2] | 225 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 226 | private: |
---|
| 227 | ArrayType * clone() const override { return new ArrayType{ *this }; } |
---|
[f3cc5b6] | 228 | MUTATE_FRIEND |
---|
[9e1d485] | 229 | }; |
---|
| 230 | |
---|
| 231 | /// Reference type `T&` |
---|
| 232 | class ReferenceType final : public Type { |
---|
| 233 | public: |
---|
| 234 | ptr<Type> base; |
---|
| 235 | |
---|
| 236 | ReferenceType( const Type * b, CV::Qualifiers q = {} ) : Type(q), base(b) {} |
---|
| 237 | |
---|
| 238 | unsigned referenceDepth() const override { return base->referenceDepth() + 1; } |
---|
| 239 | |
---|
| 240 | // Since reference types act like value types, their size is the size of the base. |
---|
| 241 | // This makes it simple to cast the empty tuple to a reference type, since casts that increase |
---|
| 242 | // the number of values are disallowed. |
---|
| 243 | unsigned size() const override { return base->size(); } |
---|
| 244 | |
---|
[69bafd2] | 245 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 246 | private: |
---|
| 247 | ReferenceType * clone() const override { return new ReferenceType{ *this }; } |
---|
[f3cc5b6] | 248 | MUTATE_FRIEND |
---|
[2bb4a01] | 249 | }; |
---|
| 250 | |
---|
[9e1d485] | 251 | /// Qualified type `P.C` |
---|
| 252 | class QualifiedType final : public Type { |
---|
| 253 | public: |
---|
| 254 | ptr<Type> parent; |
---|
| 255 | ptr<Type> child; |
---|
| 256 | |
---|
[87701b6] | 257 | QualifiedType( const Type * p, const Type * c, CV::Qualifiers q = {} ) |
---|
[9e1d485] | 258 | : Type(q), parent(p), child(c) {} |
---|
| 259 | |
---|
[69bafd2] | 260 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 261 | private: |
---|
| 262 | QualifiedType * clone() const override { return new QualifiedType{ *this }; } |
---|
[f3cc5b6] | 263 | MUTATE_FRIEND |
---|
[9e1d485] | 264 | }; |
---|
| 265 | |
---|
| 266 | /// Base type for potentially forall-qualified types |
---|
| 267 | class ParameterizedType : public Type { |
---|
| 268 | public: |
---|
| 269 | using ForallList = std::vector<ptr<TypeDecl>>; |
---|
[92f5279] | 270 | |
---|
[9e1d485] | 271 | ForallList forall; |
---|
| 272 | |
---|
[1ae47de] | 273 | ParameterizedType( ForallList&& fs = {}, CV::Qualifiers q = {}, |
---|
[c829320] | 274 | std::vector<ptr<Attribute>> && as = {} ) |
---|
| 275 | : Type(q, std::move(as)), forall(std::move(fs)) {} |
---|
[1ae47de] | 276 | |
---|
| 277 | ParameterizedType( CV::Qualifiers q, std::vector<ptr<Attribute>> && as = {} ) |
---|
[c829320] | 278 | : Type(q, std::move(as)), forall() {} |
---|
[9e1d485] | 279 | |
---|
[92f5279] | 280 | ParameterizedType( const ParameterizedType & o ) : Type( o ), forall() { |
---|
| 281 | // one-level deep clone to avoid weak-reference errors |
---|
| 282 | forall.reserve( o.forall.size() ); |
---|
| 283 | for ( const TypeDecl * d : o.forall ) { forall.emplace_back( d->clone() ); } |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | ParameterizedType( ParameterizedType && ) = default; |
---|
| 287 | |
---|
| 288 | // no need to change destructor, and operator= deleted in Node |
---|
| 289 | |
---|
[9e1d485] | 290 | private: |
---|
| 291 | virtual ParameterizedType * clone() const override = 0; |
---|
[f3cc5b6] | 292 | MUTATE_FRIEND |
---|
[9e1d485] | 293 | }; |
---|
| 294 | |
---|
| 295 | /// Function variable arguments flag |
---|
| 296 | enum ArgumentFlag { FixedArgs, VariableArgs }; |
---|
| 297 | |
---|
| 298 | /// Type of a function `[R1, R2](*)(P1, P2, P3)` |
---|
| 299 | class FunctionType final : public ParameterizedType { |
---|
| 300 | public: |
---|
[54e41b3] | 301 | std::vector<ptr<DeclWithType>> returns; |
---|
| 302 | std::vector<ptr<DeclWithType>> params; |
---|
[9e1d485] | 303 | |
---|
[87701b6] | 304 | /// Does the function accept a variable number of arguments following the arguments specified |
---|
[9e1d485] | 305 | /// in the parameters list. |
---|
| 306 | /// This could be because of |
---|
| 307 | /// - an ellipsis in a prototype declaration |
---|
| 308 | /// - an unprototyped declaration |
---|
| 309 | ArgumentFlag isVarArgs; |
---|
| 310 | |
---|
| 311 | FunctionType( ArgumentFlag va = FixedArgs, CV::Qualifiers q = {} ) |
---|
[54e41b3] | 312 | : ParameterizedType(q), returns(), params(), isVarArgs(va) {} |
---|
[9e1d485] | 313 | |
---|
| 314 | /// true if either the parameters or return values contain a tttype |
---|
| 315 | bool isTtype() const; |
---|
| 316 | /// true if function parameters are unconstrained by prototype |
---|
[54e41b3] | 317 | bool isUnprototyped() const { return isVarArgs && params.size() == 0; } |
---|
[9e1d485] | 318 | |
---|
[69bafd2] | 319 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 320 | private: |
---|
| 321 | FunctionType * clone() const override { return new FunctionType{ *this }; } |
---|
[f3cc5b6] | 322 | MUTATE_FRIEND |
---|
[9e1d485] | 323 | }; |
---|
| 324 | |
---|
| 325 | /// base class for types that refer to types declared elsewhere (aggregates and typedefs) |
---|
| 326 | class ReferenceToType : public ParameterizedType { |
---|
| 327 | public: |
---|
[54e41b3] | 328 | std::vector<ptr<Expr>> params; |
---|
[9e1d485] | 329 | std::string name; |
---|
| 330 | bool hoistType = false; |
---|
| 331 | |
---|
[87701b6] | 332 | ReferenceToType( const std::string& n, CV::Qualifiers q = {}, |
---|
[9e1d485] | 333 | std::vector<ptr<Attribute>> && as = {} ) |
---|
[c829320] | 334 | : ParameterizedType(q, std::move(as)), params(), name(n) {} |
---|
[9e1d485] | 335 | |
---|
| 336 | /// Gets aggregate declaration this type refers to |
---|
| 337 | virtual const AggregateDecl * aggr() const = 0; |
---|
| 338 | /// Looks up member declarations with given name |
---|
| 339 | std::vector<readonly<Decl>> lookup( const std::string & name ) const; |
---|
| 340 | |
---|
| 341 | private: |
---|
| 342 | virtual ReferenceToType * clone() const override = 0; |
---|
[f3cc5b6] | 343 | MUTATE_FRIEND |
---|
[9e1d485] | 344 | }; |
---|
| 345 | |
---|
| 346 | /// instance of struct type |
---|
| 347 | class StructInstType final : public ReferenceToType { |
---|
| 348 | public: |
---|
| 349 | readonly<StructDecl> base; |
---|
| 350 | |
---|
[87701b6] | 351 | StructInstType( const std::string& n, CV::Qualifiers q = {}, |
---|
[9e1d485] | 352 | std::vector<ptr<Attribute>> && as = {} ) |
---|
| 353 | : ReferenceToType( n, q, std::move(as) ), base() {} |
---|
[87701b6] | 354 | StructInstType( const StructDecl * b, CV::Qualifiers q = {}, |
---|
[9e1d485] | 355 | std::vector<ptr<Attribute>> && as = {} ); |
---|
[87701b6] | 356 | |
---|
[9e1d485] | 357 | bool isComplete() const override; |
---|
| 358 | |
---|
| 359 | const StructDecl * aggr() const override { return base; } |
---|
| 360 | |
---|
[69bafd2] | 361 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 362 | private: |
---|
| 363 | StructInstType * clone() const override { return new StructInstType{ *this }; } |
---|
[f3cc5b6] | 364 | MUTATE_FRIEND |
---|
[9e1d485] | 365 | }; |
---|
| 366 | |
---|
| 367 | /// instance of union type |
---|
| 368 | class UnionInstType final : public ReferenceToType { |
---|
| 369 | public: |
---|
| 370 | readonly<UnionDecl> base; |
---|
| 371 | |
---|
[87701b6] | 372 | UnionInstType( const std::string& n, CV::Qualifiers q = {}, |
---|
[9e1d485] | 373 | std::vector<ptr<Attribute>> && as = {} ) |
---|
| 374 | : ReferenceToType( n, q, std::move(as) ), base() {} |
---|
[87701b6] | 375 | UnionInstType( const UnionDecl * b, CV::Qualifiers q = {}, |
---|
[9e1d485] | 376 | std::vector<ptr<Attribute>> && as = {} ); |
---|
[87701b6] | 377 | |
---|
[9e1d485] | 378 | bool isComplete() const override; |
---|
| 379 | |
---|
| 380 | const UnionDecl * aggr() const override { return base; } |
---|
| 381 | |
---|
[69bafd2] | 382 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 383 | private: |
---|
| 384 | UnionInstType * clone() const override { return new UnionInstType{ *this }; } |
---|
[f3cc5b6] | 385 | MUTATE_FRIEND |
---|
[9e1d485] | 386 | }; |
---|
| 387 | |
---|
| 388 | /// instance of enum type |
---|
| 389 | class EnumInstType final : public ReferenceToType { |
---|
| 390 | public: |
---|
| 391 | readonly<EnumDecl> base; |
---|
| 392 | |
---|
[87701b6] | 393 | EnumInstType( const std::string& n, CV::Qualifiers q = {}, |
---|
[9e1d485] | 394 | std::vector<ptr<Attribute>> && as = {} ) |
---|
| 395 | : ReferenceToType( n, q, std::move(as) ), base() {} |
---|
[87701b6] | 396 | EnumInstType( const EnumDecl * b, CV::Qualifiers q = {}, |
---|
[9e1d485] | 397 | std::vector<ptr<Attribute>> && as = {} ); |
---|
[87701b6] | 398 | |
---|
[9e1d485] | 399 | bool isComplete() const override; |
---|
| 400 | |
---|
| 401 | const EnumDecl * aggr() const override { return base; } |
---|
| 402 | |
---|
[69bafd2] | 403 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 404 | private: |
---|
| 405 | EnumInstType * clone() const override { return new EnumInstType{ *this }; } |
---|
[f3cc5b6] | 406 | MUTATE_FRIEND |
---|
[9e1d485] | 407 | }; |
---|
| 408 | |
---|
| 409 | /// instance of trait type |
---|
| 410 | class TraitInstType final : public ReferenceToType { |
---|
| 411 | public: |
---|
| 412 | readonly<TraitDecl> base; |
---|
| 413 | |
---|
[87701b6] | 414 | TraitInstType( const std::string& n, CV::Qualifiers q = {}, |
---|
[9e1d485] | 415 | std::vector<ptr<Attribute>> && as = {} ) |
---|
| 416 | : ReferenceToType( n, q, std::move(as) ), base() {} |
---|
[87701b6] | 417 | TraitInstType( const TraitDecl * b, CV::Qualifiers q = {}, |
---|
[9e1d485] | 418 | std::vector<ptr<Attribute>> && as = {} ); |
---|
[87701b6] | 419 | |
---|
[9e1d485] | 420 | // not meaningful for TraitInstType |
---|
| 421 | bool isComplete() const override { assert(false); } |
---|
| 422 | |
---|
| 423 | const TraitDecl * aggr() const override { return base; } |
---|
| 424 | |
---|
[69bafd2] | 425 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 426 | private: |
---|
| 427 | TraitInstType * clone() const override { return new TraitInstType{ *this }; } |
---|
[f3cc5b6] | 428 | MUTATE_FRIEND |
---|
[9e1d485] | 429 | }; |
---|
| 430 | |
---|
| 431 | /// instance of named type alias (typedef or variable) |
---|
| 432 | class TypeInstType final : public ReferenceToType { |
---|
| 433 | public: |
---|
| 434 | readonly<TypeDecl> base; |
---|
| 435 | TypeVar::Kind kind; |
---|
| 436 | |
---|
[87701b6] | 437 | TypeInstType( const std::string& n, const TypeDecl * b, CV::Qualifiers q = {}, |
---|
[9e1d485] | 438 | std::vector<ptr<Attribute>> && as = {} ) |
---|
| 439 | : ReferenceToType( n, q, std::move(as) ), base( b ), kind( b->kind ) {} |
---|
[87701b6] | 440 | TypeInstType( const std::string& n, TypeVar::Kind k, CV::Qualifiers q = {}, |
---|
[9e1d485] | 441 | std::vector<ptr<Attribute>> && as = {} ) |
---|
| 442 | : ReferenceToType( n, q, std::move(as) ), base(), kind( k ) {} |
---|
| 443 | |
---|
| 444 | /// sets `base`, updating `kind` correctly |
---|
| 445 | void set_base( const TypeDecl * ); |
---|
| 446 | |
---|
| 447 | bool isComplete() const override; |
---|
| 448 | |
---|
| 449 | // not meaningful for TypeInstType |
---|
| 450 | const AggregateDecl * aggr() const override { assert(false); } |
---|
| 451 | |
---|
[69bafd2] | 452 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 453 | private: |
---|
| 454 | TypeInstType * clone() const override { return new TypeInstType{ *this }; } |
---|
[f3cc5b6] | 455 | MUTATE_FRIEND |
---|
[9e1d485] | 456 | }; |
---|
| 457 | |
---|
| 458 | /// tuple type e.g. `[int, char]` |
---|
| 459 | class TupleType final : public Type { |
---|
| 460 | public: |
---|
| 461 | std::vector<ptr<Type>> types; |
---|
| 462 | std::vector<ptr<Decl>> members; |
---|
| 463 | |
---|
| 464 | TupleType( std::vector<ptr<Type>> && ts, CV::Qualifiers q = {} ); |
---|
| 465 | |
---|
| 466 | // collection simulation |
---|
| 467 | using iterator = std::vector<ptr<Type>>::const_iterator; |
---|
| 468 | iterator begin() const { return types.begin(); } |
---|
| 469 | iterator end() const { return types.end(); } |
---|
[87701b6] | 470 | |
---|
[9e1d485] | 471 | unsigned size() const override { return types.size(); } |
---|
| 472 | |
---|
[d76c588] | 473 | const Type * getComponent( unsigned i ) const override { |
---|
[87701b6] | 474 | assertf( i < size(), "TupleType::getComponent: index %d must be less than size %d", |
---|
[9e1d485] | 475 | i, size() ); |
---|
| 476 | return *(begin()+i); |
---|
| 477 | } |
---|
| 478 | |
---|
[69bafd2] | 479 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 480 | private: |
---|
| 481 | TupleType * clone() const override { return new TupleType{ *this }; } |
---|
[f3cc5b6] | 482 | MUTATE_FRIEND |
---|
[9e1d485] | 483 | }; |
---|
| 484 | |
---|
| 485 | /// Type of unresolved `typeof()` expression |
---|
| 486 | class TypeofType : public Type { |
---|
| 487 | public: |
---|
| 488 | ptr<Expr> expr; |
---|
| 489 | enum Kind { Typeof, Basetypeof } kind; |
---|
| 490 | |
---|
[87701b6] | 491 | TypeofType( const Expr * e, Kind k = Typeof, CV::Qualifiers q = {} ) |
---|
[9e1d485] | 492 | : Type(q), expr(e), kind(k) {} |
---|
| 493 | |
---|
[69bafd2] | 494 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 495 | private: |
---|
| 496 | TypeofType * clone() const override { return new TypeofType{ *this }; } |
---|
[f3cc5b6] | 497 | MUTATE_FRIEND |
---|
[9e1d485] | 498 | }; |
---|
| 499 | |
---|
| 500 | /// GCC built-in varargs type |
---|
| 501 | class VarArgsType final : public Type { |
---|
| 502 | public: |
---|
| 503 | VarArgsType( CV::Qualifiers q = {} ) : Type( q ) {} |
---|
[87701b6] | 504 | |
---|
[69bafd2] | 505 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 506 | private: |
---|
| 507 | VarArgsType * clone() const override { return new VarArgsType{ *this }; } |
---|
[f3cc5b6] | 508 | MUTATE_FRIEND |
---|
[9e1d485] | 509 | }; |
---|
| 510 | |
---|
| 511 | /// Type of zero constant `0` |
---|
| 512 | class ZeroType final : public Type { |
---|
| 513 | public: |
---|
| 514 | ZeroType( CV::Qualifiers q = {} ) : Type( q ) {} |
---|
[87701b6] | 515 | |
---|
[69bafd2] | 516 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 517 | private: |
---|
[f3cc5b6] | 518 | ZeroType * clone() const override { return new ZeroType{ *this }; } |
---|
| 519 | MUTATE_FRIEND |
---|
[9e1d485] | 520 | }; |
---|
| 521 | |
---|
| 522 | /// Type of one constant `1` |
---|
| 523 | class OneType final : public Type { |
---|
| 524 | public: |
---|
| 525 | OneType( CV::Qualifiers q = {} ) : Type( q ) {} |
---|
| 526 | |
---|
[69bafd2] | 527 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 528 | private: |
---|
| 529 | OneType * clone() const override { return new OneType{ *this }; } |
---|
[f3cc5b6] | 530 | MUTATE_FRIEND |
---|
[9e1d485] | 531 | }; |
---|
| 532 | |
---|
| 533 | /// Parent type for scope-qualified types at global scope |
---|
| 534 | class GlobalScopeType final : public Type { |
---|
| 535 | public: |
---|
[746ae82] | 536 | GlobalScopeType() : Type() {} |
---|
[9e1d485] | 537 | |
---|
[69bafd2] | 538 | const Type * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[9e1d485] | 539 | private: |
---|
| 540 | GlobalScopeType * clone() const override { return new GlobalScopeType{ *this }; } |
---|
[f3cc5b6] | 541 | MUTATE_FRIEND |
---|
[9e1d485] | 542 | }; |
---|
[e0115286] | 543 | |
---|
[2bb4a01] | 544 | } |
---|
| 545 | |
---|
[f3cc5b6] | 546 | #undef MUTATE_FRIEND |
---|
| 547 | |
---|
[2bb4a01] | 548 | // Local Variables: // |
---|
| 549 | // tab-width: 4 // |
---|
| 550 | // mode: c++ // |
---|
| 551 | // compile-command: "make install" // |
---|
| 552 | // End: // |
---|