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