1 2 /* Compiler implementation of the D programming language 3 * Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved 4 * written by Walter Bright 5 * http://www.digitalmars.com 6 * Distributed under the Boost Software License, Version 1.0. 7 * http://www.boost.org/LICENSE_1_0.txt 8 * https://github.com/dlang/dmd/blob/master/src/dmd/mtype.h 9 */ 10 11 #pragma once 12 13 #include "root/root.h" 14 #include "root/stringtable.h" 15 16 #include "arraytypes.h" 17 #include "ast_node.h" 18 #include "expression.h" 19 #include "visitor.h" 20 21 struct Scope; 22 class Identifier; 23 class Expression; 24 class StructDeclaration; 25 class ClassDeclaration; 26 class EnumDeclaration; 27 class TypeInfoDeclaration; 28 class Dsymbol; 29 class TemplateInstance; 30 class TemplateDeclaration; 31 enum LINK; 32 33 class TypeBasic; 34 class Parameter; 35 36 // Back end 37 #ifdef IN_GCC 38 typedef union tree_node type; 39 #else 40 typedef struct TYPE type; 41 #endif 42 43 Type *typeSemantic(Type *type, const Loc &loc, Scope *sc); 44 void semanticTypeInfo(Scope *sc, Type *t); 45 MATCH deduceType(RootObject *o, Scope *sc, Type *tparam, TemplateParameters *parameters, Objects *dedtypes, unsigned *wm = NULL, size_t inferStart = 0); 46 StorageClass ModToStc(unsigned mod); 47 48 enum ENUMTY 49 { 50 Tarray, // slice array, aka T[] 51 Tsarray, // static array, aka T[dimension] 52 Taarray, // associative array, aka T[type] 53 Tpointer, 54 Treference, 55 Tfunction, 56 Tident, 57 Tclass, 58 Tstruct, 59 Tenum, 60 61 Tdelegate, 62 Tnone, 63 Tvoid, 64 Tint8, 65 Tuns8, 66 Tint16, 67 Tuns16, 68 Tint32, 69 Tuns32, 70 Tint64, 71 72 Tuns64, 73 Tfloat32, 74 Tfloat64, 75 Tfloat80, 76 Timaginary32, 77 Timaginary64, 78 Timaginary80, 79 Tcomplex32, 80 Tcomplex64, 81 Tcomplex80, 82 83 Tbool, 84 Tchar, 85 Twchar, 86 Tdchar, 87 Terror, 88 Tinstance, 89 Ttypeof, 90 Ttuple, 91 Tslice, 92 Treturn, 93 94 Tnull, 95 Tvector, 96 Tint128, 97 Tuns128, 98 Ttraits, 99 Tmixin, 100 Tnoreturn, 101 TMAX 102 }; 103 typedef unsigned char TY; // ENUMTY 104 105 #define SIZE_INVALID (~(d_uns64)0) // error return from size() functions 106 107 108 /** 109 * type modifiers 110 * pick this order of numbers so switch statements work better 111 */ 112 enum MODFlags 113 { 114 MODconst = 1, // type is const 115 MODimmutable = 4, // type is immutable 116 MODshared = 2, // type is shared 117 MODwild = 8, // type is wild 118 MODwildconst = (MODwild | MODconst), // type is wild const 119 MODmutable = 0x10 // type is mutable (only used in wildcard matching) 120 }; 121 typedef unsigned char MOD; 122 123 // These tables are for implicit conversion of binary ops; 124 // the indices are the type of operand one, followed by operand two. 125 extern unsigned char impcnvResult[TMAX][TMAX]; 126 extern unsigned char impcnvType1[TMAX][TMAX]; 127 extern unsigned char impcnvType2[TMAX][TMAX]; 128 129 // If !=0, give warning on implicit conversion 130 extern unsigned char impcnvWarn[TMAX][TMAX]; 131 132 enum VarArg 133 { 134 VARARGnone = 0, /// fixed number of arguments 135 VARARGvariadic = 1, /// T t, ...) can be C-style (core.stdc.stdarg) or D-style (core.vararg) 136 VARARGtypesafe = 2 /// T t ...) typesafe https://dlang.org/spec/function.html#typesafe_variadic_functions 137 /// or https://dlang.org/spec/function.html#typesafe_variadic_functions 138 }; 139 140 class Type : public ASTNode 141 { 142 public: 143 TY ty; 144 MOD mod; // modifiers MODxxxx 145 char *deco; 146 147 /* These are cached values that are lazily evaluated by constOf(), immutableOf(), etc. 148 * They should not be referenced by anybody but mtype.c. 149 * They can be NULL if not lazily evaluated yet. 150 * Note that there is no "shared immutable", because that is just immutable 151 * Naked == no MOD bits 152 */ 153 154 Type *cto; // MODconst ? naked version of this type : const version 155 Type *ito; // MODimmutable ? naked version of this type : immutable version 156 Type *sto; // MODshared ? naked version of this type : shared mutable version 157 Type *scto; // MODshared | MODconst ? naked version of this type : shared const version 158 Type *wto; // MODwild ? naked version of this type : wild version 159 Type *wcto; // MODwildconst ? naked version of this type : wild const version 160 Type *swto; // MODshared | MODwild ? naked version of this type : shared wild version 161 Type *swcto; // MODshared | MODwildconst ? naked version of this type : shared wild const version 162 163 Type *pto; // merged pointer to this type 164 Type *rto; // reference to this type 165 Type *arrayof; // array of this type 166 TypeInfoDeclaration *vtinfo; // TypeInfo object for this Type 167 168 type *ctype; // for back end 169 170 static Type *tvoid; 171 static Type *tint8; 172 static Type *tuns8; 173 static Type *tint16; 174 static Type *tuns16; 175 static Type *tint32; 176 static Type *tuns32; 177 static Type *tint64; 178 static Type *tuns64; 179 static Type *tint128; 180 static Type *tuns128; 181 static Type *tfloat32; 182 static Type *tfloat64; 183 static Type *tfloat80; 184 185 static Type *timaginary32; 186 static Type *timaginary64; 187 static Type *timaginary80; 188 189 static Type *tcomplex32; 190 static Type *tcomplex64; 191 static Type *tcomplex80; 192 193 static Type *tbool; 194 static Type *tchar; 195 static Type *twchar; 196 static Type *tdchar; 197 198 // Some special types 199 static Type *tshiftcnt; 200 static Type *tvoidptr; // void* 201 static Type *tstring; // immutable(char)[] 202 static Type *twstring; // immutable(wchar)[] 203 static Type *tdstring; // immutable(dchar)[] 204 static Type *terror; // for error recovery 205 static Type *tnull; // for null type 206 static Type *tnoreturn; // for bottom type typeof(*null) 207 208 static Type *tsize_t; // matches size_t alias 209 static Type *tptrdiff_t; // matches ptrdiff_t alias 210 static Type *thash_t; // matches hash_t alias 211 212 static ClassDeclaration *dtypeinfo; 213 static ClassDeclaration *typeinfoclass; 214 static ClassDeclaration *typeinfointerface; 215 static ClassDeclaration *typeinfostruct; 216 static ClassDeclaration *typeinfopointer; 217 static ClassDeclaration *typeinfoarray; 218 static ClassDeclaration *typeinfostaticarray; 219 static ClassDeclaration *typeinfoassociativearray; 220 static ClassDeclaration *typeinfovector; 221 static ClassDeclaration *typeinfoenum; 222 static ClassDeclaration *typeinfofunction; 223 static ClassDeclaration *typeinfodelegate; 224 static ClassDeclaration *typeinfotypelist; 225 static ClassDeclaration *typeinfoconst; 226 static ClassDeclaration *typeinfoinvariant; 227 static ClassDeclaration *typeinfoshared; 228 static ClassDeclaration *typeinfowild; 229 230 static TemplateDeclaration *rtinfo; 231 232 static Type *basic[TMAX]; 233 static unsigned char sizeTy[TMAX]; 234 static StringTable stringtable; 235 236 Type(TY ty); 237 virtual const char *kind(); 238 Type *copy(); 239 virtual Type *syntaxCopy(); 240 bool equals(RootObject *o); 241 bool equivalent(Type *t); 242 // kludge for template.isType() dyncast()243 int dyncast() const { return DYNCAST_TYPE; } 244 int covariant(Type *t, StorageClass *pstc = NULL, bool fix17349 = true); 245 const char *toChars(); 246 char *toPrettyChars(bool QualifyTypes = false); 247 static void _init(); 248 249 d_uns64 size(); 250 virtual d_uns64 size(Loc loc); 251 virtual unsigned alignsize(); 252 Type *trySemantic(Loc loc, Scope *sc); 253 Type *merge(); 254 Type *merge2(); 255 void modToBuffer(OutBuffer *buf); 256 char *modToChars(); 257 258 /** For each active modifier (MODconst, MODimmutable, etc) call fp with a 259 void* for the work param and a string representation of the attribute. */ 260 int modifiersApply(void *param, int (*fp)(void *, const char *)); 261 262 virtual bool isintegral(); 263 virtual bool isfloating(); // real, imaginary, or complex 264 virtual bool isreal(); 265 virtual bool isimaginary(); 266 virtual bool iscomplex(); 267 virtual bool isscalar(); 268 virtual bool isunsigned(); 269 virtual bool isscope(); 270 virtual bool isString(); 271 virtual bool isAssignable(); 272 virtual bool isBoolean(); 273 virtual void checkDeprecated(Loc loc, Scope *sc); isConst()274 bool isConst() const { return (mod & MODconst) != 0; } isImmutable()275 bool isImmutable() const { return (mod & MODimmutable) != 0; } isMutable()276 bool isMutable() const { return (mod & (MODconst | MODimmutable | MODwild)) == 0; } isShared()277 bool isShared() const { return (mod & MODshared) != 0; } isSharedConst()278 bool isSharedConst() const { return (mod & (MODshared | MODconst)) == (MODshared | MODconst); } isWild()279 bool isWild() const { return (mod & MODwild) != 0; } isWildConst()280 bool isWildConst() const { return (mod & MODwildconst) == MODwildconst; } isSharedWild()281 bool isSharedWild() const { return (mod & (MODshared | MODwild)) == (MODshared | MODwild); } isNaked()282 bool isNaked() const { return mod == 0; } 283 Type *nullAttributes(); 284 Type *constOf(); 285 Type *immutableOf(); 286 Type *mutableOf(); 287 Type *sharedOf(); 288 Type *sharedConstOf(); 289 Type *unSharedOf(); 290 Type *wildOf(); 291 Type *wildConstOf(); 292 Type *sharedWildOf(); 293 Type *sharedWildConstOf(); 294 void fixTo(Type *t); 295 void check(); 296 Type *addSTC(StorageClass stc); 297 Type *castMod(MOD mod); 298 Type *addMod(MOD mod); 299 virtual Type *addStorageClass(StorageClass stc); 300 Type *pointerTo(); 301 Type *referenceTo(); 302 Type *arrayOf(); 303 Type *sarrayOf(dinteger_t dim); 304 Type *aliasthisOf(); 305 bool checkAliasThisRec(); 306 virtual Type *makeConst(); 307 virtual Type *makeImmutable(); 308 virtual Type *makeShared(); 309 virtual Type *makeSharedConst(); 310 virtual Type *makeWild(); 311 virtual Type *makeWildConst(); 312 virtual Type *makeSharedWild(); 313 virtual Type *makeSharedWildConst(); 314 virtual Type *makeMutable(); 315 virtual Dsymbol *toDsymbol(Scope *sc); 316 virtual Type *toBasetype(); 317 virtual bool isBaseOf(Type *t, int *poffset); 318 virtual MATCH implicitConvTo(Type *to); 319 virtual MATCH constConv(Type *to); 320 virtual unsigned char deduceWild(Type *t, bool isRef); 321 virtual Type *substWildTo(unsigned mod); 322 323 Type *unqualify(unsigned m); 324 325 virtual Type *toHeadMutable(); 326 virtual ClassDeclaration *isClassHandle(); 327 virtual Expression *getProperty(Loc loc, Identifier *ident, int flag); 328 virtual Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 329 virtual structalign_t alignment(); 330 Expression *noMember(Scope *sc, Expression *e, Identifier *ident, int flag); 331 virtual Expression *defaultInit(Loc loc = Loc()); 332 virtual Expression *defaultInitLiteral(Loc loc); 333 virtual bool isZeroInit(Loc loc = Loc()); // if initializer is 0 334 Identifier *getTypeInfoIdent(); 335 virtual void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 336 void resolveExp(Expression *e, Type **pt, Expression **pe, Dsymbol **ps); 337 virtual int hasWild() const; 338 virtual bool hasPointers(); 339 virtual bool hasVoidInitPointers(); 340 virtual Type *nextOf(); 341 Type *baseElemOf(); 342 uinteger_t sizemask(); 343 unsigned numberOfElems(const Loc &loc); 344 virtual bool needsDestruction(); 345 virtual bool needsNested(); 346 void checkComplexTransition(Loc loc); 347 TypeFunction *toTypeFunction(); 348 349 static void error(Loc loc, const char *format, ...); 350 static void warning(Loc loc, const char *format, ...); 351 352 // For eliminating dynamic_cast 353 virtual TypeBasic *isTypeBasic(); 354 TypeError *isTypeError(); 355 TypeVector *isTypeVector(); 356 TypeSArray *isTypeSArray(); 357 TypeDArray *isTypeDArray(); 358 TypeAArray *isTypeAArray(); 359 TypePointer *isTypePointer(); 360 TypeReference *isTypeReference(); 361 TypeFunction *isTypeFunction(); 362 TypeDelegate *isTypeDelegate(); 363 TypeIdentifier *isTypeIdentifier(); 364 TypeInstance *isTypeInstance(); 365 TypeTypeof *isTypeTypeof(); 366 TypeReturn *isTypeReturn(); 367 TypeStruct *isTypeStruct(); 368 TypeEnum *isTypeEnum(); 369 TypeClass *isTypeClass(); 370 TypeTuple *isTypeTuple(); 371 TypeSlice *isTypeSlice(); 372 TypeNull *isTypeNull(); 373 TypeMixin *isTypeMixin(); 374 TypeTraits *isTypeTraits(); 375 TypeNoreturn *isTypeNoreturn(); 376 accept(Visitor * v)377 void accept(Visitor *v) { v->visit(this); } 378 }; 379 380 class TypeError : public Type 381 { 382 public: 383 TypeError(); 384 Type *syntaxCopy(); 385 386 d_uns64 size(Loc loc); 387 Expression *getProperty(Loc loc, Identifier *ident, int flag); 388 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 389 Expression *defaultInit(Loc loc); 390 Expression *defaultInitLiteral(Loc loc); accept(Visitor * v)391 void accept(Visitor *v) { v->visit(this); } 392 }; 393 394 class TypeNext : public Type 395 { 396 public: 397 Type *next; 398 399 TypeNext(TY ty, Type *next); 400 void checkDeprecated(Loc loc, Scope *sc); 401 int hasWild() const; 402 Type *nextOf(); 403 Type *makeConst(); 404 Type *makeImmutable(); 405 Type *makeShared(); 406 Type *makeSharedConst(); 407 Type *makeWild(); 408 Type *makeWildConst(); 409 Type *makeSharedWild(); 410 Type *makeSharedWildConst(); 411 Type *makeMutable(); 412 MATCH constConv(Type *to); 413 unsigned char deduceWild(Type *t, bool isRef); 414 void transitive(); accept(Visitor * v)415 void accept(Visitor *v) { v->visit(this); } 416 }; 417 418 class TypeBasic : public Type 419 { 420 public: 421 const char *dstring; 422 unsigned flags; 423 424 TypeBasic(TY ty); 425 const char *kind(); 426 Type *syntaxCopy(); 427 d_uns64 size(Loc loc) /*const*/; 428 unsigned alignsize(); 429 Expression *getProperty(Loc loc, Identifier *ident, int flag); 430 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 431 bool isintegral(); 432 bool isfloating() /*const*/; 433 bool isreal() /*const*/; 434 bool isimaginary() /*const*/; 435 bool iscomplex() /*const*/; 436 bool isscalar() /*const*/; 437 bool isunsigned() /*const*/; 438 MATCH implicitConvTo(Type *to); 439 Expression *defaultInit(Loc loc); 440 bool isZeroInit(Loc loc) /*const*/; 441 442 // For eliminating dynamic_cast 443 TypeBasic *isTypeBasic(); accept(Visitor * v)444 void accept(Visitor *v) { v->visit(this); } 445 }; 446 447 class TypeVector : public Type 448 { 449 public: 450 Type *basetype; 451 452 TypeVector(Type *basetype); 453 static TypeVector *create(Type *basetype); 454 const char *kind(); 455 Type *syntaxCopy(); 456 d_uns64 size(Loc loc); 457 unsigned alignsize(); 458 Expression *getProperty(Loc loc, Identifier *ident, int flag); 459 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 460 bool isintegral(); 461 bool isfloating(); 462 bool isscalar(); 463 bool isunsigned(); 464 bool isBoolean() /*const*/; 465 MATCH implicitConvTo(Type *to); 466 Expression *defaultInit(Loc loc); 467 Expression *defaultInitLiteral(Loc loc); 468 TypeBasic *elementType(); 469 bool isZeroInit(Loc loc); 470 accept(Visitor * v)471 void accept(Visitor *v) { v->visit(this); } 472 }; 473 474 class TypeArray : public TypeNext 475 { 476 public: 477 TypeArray(TY ty, Type *next); 478 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); accept(Visitor * v)479 void accept(Visitor *v) { v->visit(this); } 480 }; 481 482 // Static array, one with a fixed dimension 483 class TypeSArray : public TypeArray 484 { 485 public: 486 Expression *dim; 487 488 TypeSArray(Type *t, Expression *dim); 489 const char *kind(); 490 Type *syntaxCopy(); 491 d_uns64 size(Loc loc); 492 unsigned alignsize(); 493 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 494 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 495 bool isString(); 496 bool isZeroInit(Loc loc); 497 structalign_t alignment(); 498 MATCH constConv(Type *to); 499 MATCH implicitConvTo(Type *to); 500 Expression *defaultInit(Loc loc); 501 Expression *defaultInitLiteral(Loc loc); 502 bool hasPointers(); 503 bool needsDestruction(); 504 bool needsNested(); 505 accept(Visitor * v)506 void accept(Visitor *v) { v->visit(this); } 507 }; 508 509 // Dynamic array, no dimension 510 class TypeDArray : public TypeArray 511 { 512 public: 513 TypeDArray(Type *t); 514 const char *kind(); 515 Type *syntaxCopy(); 516 d_uns64 size(Loc loc) /*const*/; 517 unsigned alignsize() /*const*/; 518 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 519 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 520 bool isString(); 521 bool isZeroInit(Loc loc) /*const*/; 522 bool isBoolean() /*const*/; 523 MATCH implicitConvTo(Type *to); 524 Expression *defaultInit(Loc loc); 525 bool hasPointers() /*const*/; 526 accept(Visitor * v)527 void accept(Visitor *v) { v->visit(this); } 528 }; 529 530 class TypeAArray : public TypeArray 531 { 532 public: 533 Type *index; // key type 534 Loc loc; 535 Scope *sc; 536 537 TypeAArray(Type *t, Type *index); 538 static TypeAArray *create(Type *t, Type *index); 539 const char *kind(); 540 Type *syntaxCopy(); 541 d_uns64 size(Loc loc); 542 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 543 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 544 Expression *defaultInit(Loc loc); 545 bool isZeroInit(Loc loc) /*const*/; 546 bool isBoolean() /*const*/; 547 bool hasPointers() /*const*/; 548 MATCH implicitConvTo(Type *to); 549 MATCH constConv(Type *to); 550 accept(Visitor * v)551 void accept(Visitor *v) { v->visit(this); } 552 }; 553 554 class TypePointer : public TypeNext 555 { 556 public: 557 TypePointer(Type *t); 558 static TypePointer *create(Type *t); 559 const char *kind(); 560 Type *syntaxCopy(); 561 d_uns64 size(Loc loc) /*const*/; 562 MATCH implicitConvTo(Type *to); 563 MATCH constConv(Type *to); 564 bool isscalar() /*const*/; 565 Expression *defaultInit(Loc loc); 566 bool isZeroInit(Loc loc) /*const*/; 567 bool hasPointers() /*const*/; 568 accept(Visitor * v)569 void accept(Visitor *v) { v->visit(this); } 570 }; 571 572 class TypeReference : public TypeNext 573 { 574 public: 575 TypeReference(Type *t); 576 const char *kind(); 577 Type *syntaxCopy(); 578 d_uns64 size(Loc loc) /*const*/; 579 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 580 Expression *defaultInit(Loc loc); 581 bool isZeroInit(Loc loc) /*const*/; accept(Visitor * v)582 void accept(Visitor *v) { v->visit(this); } 583 }; 584 585 enum RET 586 { 587 RETregs = 1, // returned in registers 588 RETstack = 2 // returned on stack 589 }; 590 591 enum TRUST 592 { 593 TRUSTdefault = 0, 594 TRUSTsystem = 1, // @system (same as TRUSTdefault) 595 TRUSTtrusted = 2, // @trusted 596 TRUSTsafe = 3 // @safe 597 }; 598 599 // in hdrgen.c 600 void trustToBuffer(OutBuffer *buf, TRUST trust); 601 const char *trustToChars(TRUST trust); 602 603 enum TRUSTformat 604 { 605 TRUSTformatDefault, // do not emit @system when trust == TRUSTdefault 606 TRUSTformatSystem // emit @system when trust == TRUSTdefault 607 }; 608 609 enum PURE 610 { 611 PUREimpure = 0, // not pure at all 612 PUREfwdref = 1, // it's pure, but not known which level yet 613 PUREweak = 2, // no mutable globals are read or written 614 PUREconst = 3, // parameters are values or const 615 PUREstrong = 4 // parameters are values or immutable 616 }; 617 618 class Parameter : public ASTNode 619 { 620 public: 621 StorageClass storageClass; 622 Type *type; 623 Identifier *ident; 624 Expression *defaultArg; 625 UserAttributeDeclaration *userAttribDecl; // user defined attributes 626 627 Parameter(StorageClass storageClass, Type *type, Identifier *ident, 628 Expression *defaultArg, UserAttributeDeclaration *userAttribDecl); 629 static Parameter *create(StorageClass storageClass, Type *type, Identifier *ident, 630 Expression *defaultArg, UserAttributeDeclaration *userAttribDecl); 631 Parameter *syntaxCopy(); 632 Type *isLazyArray(); 633 // kludge for template.isType() dyncast()634 int dyncast() const { return DYNCAST_PARAMETER; } accept(Visitor * v)635 void accept(Visitor *v) { v->visit(this); } 636 637 static Parameters *arraySyntaxCopy(Parameters *parameters); 638 static size_t dim(Parameters *parameters); 639 static Parameter *getNth(Parameters *parameters, size_t nth, size_t *pn = NULL); 640 const char *toChars(); 641 bool isCovariant(bool returnByRef, const Parameter *p) const; 642 static bool isCovariantScope(bool returnByRef, StorageClass from, StorageClass to); 643 }; 644 645 struct ParameterList 646 { 647 Parameters *parameters; 648 VarArg varargs; 649 650 ParameterList(Parameters *parameters = NULL, VarArg varargs = VARARGnone); 651 652 size_t length(); 653 Parameter *operator[](size_t i) { return Parameter::getNth(parameters, i); } 654 }; 655 656 class TypeFunction : public TypeNext 657 { 658 public: 659 // .next is the return type 660 661 ParameterList parameterList; // function parameters 662 663 bool isnothrow; // true: nothrow 664 bool isnogc; // true: is @nogc 665 bool isproperty; // can be called without parentheses 666 bool isref; // true: returns a reference 667 bool isreturn; // true: 'this' is returned by ref 668 bool isscope; // true: 'this' is scope 669 bool isscopeinferred; // true: 'this' is scope from inference 670 LINK linkage; // calling convention 671 TRUST trust; // level of trust 672 PURE purity; // PURExxxx 673 unsigned char iswild; // bit0: inout on params, bit1: inout on qualifier 674 Expressions *fargs; // function arguments 675 676 int inuse; 677 678 TypeFunction(const ParameterList &pl, Type *treturn, LINK linkage, StorageClass stc = 0); 679 static TypeFunction *create(Parameters *parameters, Type *treturn, VarArg varargs, LINK linkage, StorageClass stc = 0); 680 const char *kind(); 681 Type *syntaxCopy(); 682 void purityLevel(); 683 bool hasLazyParameters(); 684 bool isDstyleVariadic() const; 685 bool parameterEscapes(Parameter *p); 686 StorageClass parameterStorageClass(Parameter *p); 687 Type *addStorageClass(StorageClass stc); 688 689 /** For each active attribute (ref/const/nogc/etc) call fp with a void* for the 690 work param and a string representation of the attribute. */ 691 int attributesApply(void *param, int (*fp)(void *, const char *), TRUSTformat trustFormat = TRUSTformatDefault); 692 693 Type *substWildTo(unsigned mod); 694 MATCH callMatch(Type *tthis, Expressions *toargs, int flag = 0, const char **pMessage = NULL); 695 bool checkRetType(Loc loc); 696 697 Expression *defaultInit(Loc loc) /*const*/; accept(Visitor * v)698 void accept(Visitor *v) { v->visit(this); } 699 }; 700 701 class TypeDelegate : public TypeNext 702 { 703 public: 704 // .next is a TypeFunction 705 706 TypeDelegate(Type *t); 707 static TypeDelegate *create(Type *t); 708 const char *kind(); 709 Type *syntaxCopy(); 710 Type *addStorageClass(StorageClass stc); 711 d_uns64 size(Loc loc) /*const*/; 712 unsigned alignsize() /*const*/; 713 MATCH implicitConvTo(Type *to); 714 Expression *defaultInit(Loc loc); 715 bool isZeroInit(Loc loc) /*const*/; 716 bool isBoolean() /*const*/; 717 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 718 bool hasPointers() /*const*/; 719 accept(Visitor * v)720 void accept(Visitor *v) { v->visit(this); } 721 }; 722 723 class TypeTraits : public Type 724 { 725 public: 726 Loc loc; 727 /// The expression to resolve as type or symbol. 728 TraitsExp *exp; 729 /// The symbol when exp doesn't represent a type. 730 Dsymbol *sym; 731 732 TypeTraits(const Loc &loc, TraitsExp *exp); 733 Type *syntaxCopy(); 734 Dsymbol *toDsymbol(Scope *sc); 735 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 736 d_uns64 size(Loc loc); accept(Visitor * v)737 void accept(Visitor *v) { v->visit(this); } 738 }; 739 740 class TypeMixin : public Type 741 { 742 public: 743 Loc loc; 744 Expressions *exps; 745 RootObject *obj; 746 747 TypeMixin(const Loc &loc, Expressions *exps); 748 const char *kind(); 749 Type *syntaxCopy(); 750 Dsymbol *toDsymbol(Scope *sc); 751 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); accept(Visitor * v)752 void accept(Visitor *v) { v->visit(this); } 753 }; 754 755 class TypeQualified : public Type 756 { 757 public: 758 Loc loc; 759 // array of Identifier and TypeInstance, 760 // representing ident.ident!tiargs.ident. ... etc. 761 Objects idents; 762 763 TypeQualified(TY ty, Loc loc); 764 void syntaxCopyHelper(TypeQualified *t); 765 void addIdent(Identifier *ident); 766 void addInst(TemplateInstance *inst); 767 void addIndex(RootObject *expr); 768 d_uns64 size(Loc loc); 769 770 void resolveTupleIndex(Loc loc, Scope *sc, Dsymbol *s, 771 Expression **pe, Type **pt, Dsymbol **ps, RootObject *oindex); 772 void resolveHelper(Loc loc, Scope *sc, Dsymbol *s, Dsymbol *scopesym, 773 Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 774 accept(Visitor * v)775 void accept(Visitor *v) { v->visit(this); } 776 }; 777 778 class TypeIdentifier : public TypeQualified 779 { 780 public: 781 Identifier *ident; 782 Dsymbol *originalSymbol; // The symbol representing this identifier, before alias resolution 783 784 TypeIdentifier(Loc loc, Identifier *ident); 785 const char *kind(); 786 Type *syntaxCopy(); 787 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 788 Dsymbol *toDsymbol(Scope *sc); accept(Visitor * v)789 void accept(Visitor *v) { v->visit(this); } 790 }; 791 792 /* Similar to TypeIdentifier, but with a TemplateInstance as the root 793 */ 794 class TypeInstance : public TypeQualified 795 { 796 public: 797 TemplateInstance *tempinst; 798 799 TypeInstance(Loc loc, TemplateInstance *tempinst); 800 const char *kind(); 801 Type *syntaxCopy(); 802 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 803 Dsymbol *toDsymbol(Scope *sc); accept(Visitor * v)804 void accept(Visitor *v) { v->visit(this); } 805 }; 806 807 class TypeTypeof : public TypeQualified 808 { 809 public: 810 Expression *exp; 811 int inuse; 812 813 TypeTypeof(Loc loc, Expression *exp); 814 const char *kind(); 815 Type *syntaxCopy(); 816 Dsymbol *toDsymbol(Scope *sc); 817 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); 818 d_uns64 size(Loc loc); accept(Visitor * v)819 void accept(Visitor *v) { v->visit(this); } 820 }; 821 822 class TypeReturn : public TypeQualified 823 { 824 public: 825 TypeReturn(Loc loc); 826 const char *kind(); 827 Type *syntaxCopy(); 828 Dsymbol *toDsymbol(Scope *sc); 829 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); accept(Visitor * v)830 void accept(Visitor *v) { v->visit(this); } 831 }; 832 833 // Whether alias this dependency is recursive or not. 834 enum AliasThisRec 835 { 836 RECno = 0, // no alias this recursion 837 RECyes = 1, // alias this has recursive dependency 838 RECfwdref = 2, // not yet known 839 RECtypeMask = 3,// mask to read no/yes/fwdref 840 841 RECtracing = 0x4, // mark in progress of implicitConvTo/deduceWild 842 RECtracingDT = 0x8 // mark in progress of deduceType 843 }; 844 845 class TypeStruct : public Type 846 { 847 public: 848 StructDeclaration *sym; 849 AliasThisRec att; 850 CPPMANGLE cppmangle; 851 852 TypeStruct(StructDeclaration *sym); 853 static TypeStruct *create(StructDeclaration *sym); 854 const char *kind(); 855 d_uns64 size(Loc loc); 856 unsigned alignsize(); 857 Type *syntaxCopy(); 858 Dsymbol *toDsymbol(Scope *sc); 859 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 860 structalign_t alignment(); 861 Expression *defaultInit(Loc loc); 862 Expression *defaultInitLiteral(Loc loc); 863 bool isZeroInit(Loc loc) /*const*/; 864 bool isAssignable(); 865 bool isBoolean() /*const*/; 866 bool needsDestruction() /*const*/; 867 bool needsNested(); 868 bool hasPointers(); 869 bool hasVoidInitPointers(); 870 MATCH implicitConvTo(Type *to); 871 MATCH constConv(Type *to); 872 unsigned char deduceWild(Type *t, bool isRef); 873 Type *toHeadMutable(); 874 accept(Visitor * v)875 void accept(Visitor *v) { v->visit(this); } 876 }; 877 878 class TypeEnum : public Type 879 { 880 public: 881 EnumDeclaration *sym; 882 883 TypeEnum(EnumDeclaration *sym); 884 const char *kind(); 885 Type *syntaxCopy(); 886 d_uns64 size(Loc loc); 887 unsigned alignsize(); 888 Dsymbol *toDsymbol(Scope *sc); 889 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 890 Expression *getProperty(Loc loc, Identifier *ident, int flag); 891 bool isintegral(); 892 bool isfloating(); 893 bool isreal(); 894 bool isimaginary(); 895 bool iscomplex(); 896 bool isscalar(); 897 bool isunsigned(); 898 bool isBoolean(); 899 bool isString(); 900 bool isAssignable(); 901 bool needsDestruction(); 902 bool needsNested(); 903 MATCH implicitConvTo(Type *to); 904 MATCH constConv(Type *to); 905 Type *toBasetype(); 906 Expression *defaultInit(Loc loc); 907 bool isZeroInit(Loc loc); 908 bool hasPointers(); 909 bool hasVoidInitPointers(); 910 Type *nextOf(); 911 accept(Visitor * v)912 void accept(Visitor *v) { v->visit(this); } 913 }; 914 915 class TypeClass : public Type 916 { 917 public: 918 ClassDeclaration *sym; 919 AliasThisRec att; 920 CPPMANGLE cppmangle; 921 922 TypeClass(ClassDeclaration *sym); 923 const char *kind(); 924 d_uns64 size(Loc loc) /*const*/; 925 Type *syntaxCopy(); 926 Dsymbol *toDsymbol(Scope *sc); 927 Expression *dotExp(Scope *sc, Expression *e, Identifier *ident, int flag); 928 ClassDeclaration *isClassHandle(); 929 bool isBaseOf(Type *t, int *poffset); 930 MATCH implicitConvTo(Type *to); 931 MATCH constConv(Type *to); 932 unsigned char deduceWild(Type *t, bool isRef); 933 Type *toHeadMutable(); 934 Expression *defaultInit(Loc loc); 935 bool isZeroInit(Loc loc) /*const*/; 936 bool isscope() /*const*/; 937 bool isBoolean() /*const*/; 938 bool hasPointers() /*const*/; 939 accept(Visitor * v)940 void accept(Visitor *v) { v->visit(this); } 941 }; 942 943 class TypeTuple : public Type 944 { 945 public: 946 Parameters *arguments; // types making up the tuple 947 948 TypeTuple(Parameters *arguments); 949 TypeTuple(Expressions *exps); 950 static TypeTuple *create(Parameters *arguments); 951 TypeTuple(); 952 TypeTuple(Type *t1); 953 TypeTuple(Type *t1, Type *t2); 954 const char *kind(); 955 Type *syntaxCopy(); 956 bool equals(RootObject *o); 957 Expression *getProperty(Loc loc, Identifier *ident, int flag); 958 Expression *defaultInit(Loc loc); accept(Visitor * v)959 void accept(Visitor *v) { v->visit(this); } 960 }; 961 962 class TypeSlice : public TypeNext 963 { 964 public: 965 Expression *lwr; 966 Expression *upr; 967 968 TypeSlice(Type *next, Expression *lwr, Expression *upr); 969 const char *kind(); 970 Type *syntaxCopy(); 971 void resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid = false); accept(Visitor * v)972 void accept(Visitor *v) { v->visit(this); } 973 }; 974 975 class TypeNull : public Type 976 { 977 public: 978 TypeNull(); 979 const char *kind(); 980 981 Type *syntaxCopy(); 982 MATCH implicitConvTo(Type *to); 983 bool isBoolean() /*const*/; 984 985 d_uns64 size(Loc loc) /*const*/; 986 Expression *defaultInit(Loc loc) /*const*/; accept(Visitor * v)987 void accept(Visitor *v) { v->visit(this); } 988 }; 989 990 class TypeNoreturn : public Type 991 { 992 public: 993 TypeNoreturn(); 994 const char *kind(); 995 996 Type *syntaxCopy(); 997 MATCH implicitConvTo(Type *to); 998 bool isBoolean() /*const*/; 999 1000 d_uns64 size(Loc loc) /*const*/; 1001 unsigned alignsize(); accept(Visitor * v)1002 void accept(Visitor *v) { v->visit(this); } 1003 }; 1004 1005 /**************************************************************/ 1006 1007 bool arrayTypeCompatible(Loc loc, Type *t1, Type *t2); 1008 bool arrayTypeCompatibleWithoutCasting(Type *t1, Type *t2); 1009