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