1 /* types.cc -- Lower D frontend types to GCC trees. 2 Copyright (C) 2006-2022 Free Software Foundation, Inc. 3 4 GCC is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 3, or (at your option) 7 any later version. 8 9 GCC is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with GCC; see the file COPYING3. If not see 16 <http://www.gnu.org/licenses/>. */ 17 18 #include "config.h" 19 #include "system.h" 20 #include "coretypes.h" 21 22 #include "dmd/attrib.h" 23 #include "dmd/aggregate.h" 24 #include "dmd/enum.h" 25 #include "dmd/expression.h" 26 #include "dmd/identifier.h" 27 #include "dmd/mtype.h" 28 #include "dmd/target.h" 29 30 #include "tree.h" 31 #include "fold-const.h" 32 #include "diagnostic.h" 33 #include "langhooks.h" 34 #include "tm.h" 35 #include "function.h" 36 #include "toplev.h" 37 #include "target.h" 38 #include "stringpool.h" 39 #include "stor-layout.h" 40 #include "attribs.h" 41 42 #include "d-tree.h" 43 #include "d-target.h" 44 45 46 /* Return the signed or unsigned version of TYPE, an integral type, the 47 signedness being specified by UNSIGNEDP. */ 48 49 static tree 50 d_signed_or_unsigned_type (int unsignedp, tree type) 51 { 52 if (TYPE_UNSIGNED (type) == (unsigned) unsignedp) 53 return type; 54 55 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_cent_type)) 56 return unsignedp ? d_ucent_type : d_cent_type; 57 58 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_long_type)) 59 return unsignedp ? d_ulong_type : d_long_type; 60 61 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_int_type)) 62 return unsignedp ? d_uint_type : d_int_type; 63 64 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_short_type)) 65 return unsignedp ? d_ushort_type : d_short_type; 66 67 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_byte_type)) 68 return unsignedp ? d_ubyte_type : d_byte_type; 69 70 return signed_or_unsigned_type_for (unsignedp, type); 71 } 72 73 /* Return the unsigned version of TYPE, an integral type. */ 74 75 tree 76 d_unsigned_type (tree type) 77 { 78 return d_signed_or_unsigned_type (1, type); 79 } 80 81 /* Return the signed version of TYPE, an integral type. */ 82 83 tree 84 d_signed_type (tree type) 85 { 86 return d_signed_or_unsigned_type (0, type); 87 } 88 89 /* Return TRUE if TYPE is a static array va_list. This is for compatibility 90 with the C ABI, where va_list static arrays are passed by reference. 91 However for every other case in D, static arrays are passed by value. */ 92 93 bool 94 valist_array_p (Type *type) 95 { 96 Type *tvalist = target.va_listType (Loc (), NULL); 97 if (tvalist->ty == TY::Tsarray) 98 { 99 Type *tb = type->toBasetype (); 100 if (same_type_p (tb, tvalist)) 101 return true; 102 } 103 104 return false; 105 } 106 107 /* Returns true if TYPE contains no actual data, just various 108 possible combinations of empty aggregates. */ 109 110 bool 111 empty_aggregate_p (tree type) 112 { 113 if (!AGGREGATE_TYPE_P (type)) 114 return false; 115 116 /* Want the element type for arrays. */ 117 if (TREE_CODE (type) == ARRAY_TYPE) 118 return empty_aggregate_p (TREE_TYPE (type)); 119 120 /* Recursively check all fields. */ 121 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 122 { 123 if (TREE_CODE (field) == FIELD_DECL 124 && !empty_aggregate_p (TREE_TYPE (field))) 125 return false; 126 } 127 128 return true; 129 } 130 131 /* Returns true if T1 and T2 are related to each other. */ 132 133 bool 134 same_type_p (Type *t1, Type *t2) 135 { 136 /* Types are equal. */ 137 if (t1 == t2) 138 return true; 139 140 /* Types derive from the same base. */ 141 Type *tb1 = t1->toBasetype (); 142 Type *tb2 = t2->toBasetype (); 143 if (tb1 == tb2) 144 return true; 145 146 /* Types are mutably the same type. */ 147 if (tb1->ty == tb2->ty && tb1->equivalent (tb2)) 148 return true; 149 150 return false; 151 } 152 153 /* Returns `Object' type which all D classes are derived from. */ 154 155 Type * 156 get_object_type (void) 157 { 158 if (ClassDeclaration::object) 159 return ClassDeclaration::object->type; 160 161 error ("missing or corrupt object.d"); 162 return Type::terror; 163 } 164 165 166 /* Returns a static array of TYPE which has SIZE number of elements. */ 167 168 tree 169 make_array_type (Type *type, unsigned HOST_WIDE_INT size) 170 { 171 /* In [arrays/void-arrays], void arrays can also be static, the length is 172 specified in bytes. */ 173 if (type->toBasetype ()->ty == TY::Tvoid) 174 type = Type::tuns8; 175 176 /* In [arrays/static-arrays], a static array with a dimension of 0 is allowed, 177 but no space is allocated for it. */ 178 if (size == 0) 179 { 180 tree range = lang_hooks.types.type_for_size (TYPE_PRECISION (sizetype), 181 TYPE_UNSIGNED (sizetype)); 182 tree index = build_range_type (range, size_zero_node, NULL_TREE); 183 184 tree t = build_array_type (build_ctype (type), index); 185 TYPE_SIZE (t) = bitsize_zero_node; 186 TYPE_SIZE_UNIT (t) = size_zero_node; 187 return t; 188 } 189 190 tree t = build_array_type (build_ctype (type), 191 build_index_type (size_int (size - 1))); 192 /* Propagate TREE_ADDRESSABLE to the static array type. */ 193 TREE_ADDRESSABLE (t) = TREE_ADDRESSABLE (TREE_TYPE (t)); 194 return t; 195 } 196 197 /* Builds a record type whose name is NAME. NFIELDS is the number of fields, 198 provided as field ident/type pairs. */ 199 200 tree 201 make_struct_type (const char *name, int nfields, ...) 202 { 203 tree fields = NULL_TREE; 204 va_list ap; 205 206 va_start (ap, nfields); 207 208 for (int i = 0; i < nfields; i++) 209 { 210 tree ident = va_arg (ap, tree); 211 tree type = va_arg (ap, tree); 212 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL, ident, type); 213 DECL_CHAIN (field) = fields; 214 fields = field; 215 } 216 217 va_end (ap); 218 219 tree type = make_node (RECORD_TYPE); 220 finish_builtin_struct (type, name, fields, NULL_TREE); 221 222 return type; 223 } 224 225 /* Return qualified type variant of TYPE determined by modifier value MOD. */ 226 227 tree 228 insert_type_modifiers (tree type, unsigned mod) 229 { 230 int quals = 0; 231 232 switch (mod) 233 { 234 case MODconst: 235 case MODwild: 236 case MODwildconst: 237 case MODimmutable: 238 case MODshared | MODconst: 239 case MODshared | MODwild: 240 case MODshared | MODwildconst: 241 quals |= TYPE_QUAL_CONST; 242 break; 243 244 case 0: 245 case MODshared: 246 break; 247 248 default: 249 gcc_unreachable (); 250 } 251 252 tree qualtype = build_qualified_type (type, quals); 253 254 /* Mark whether the type is qualified `shared'. */ 255 if (mod & MODshared) 256 TYPE_SHARED (qualtype) = 1; 257 258 return qualtype; 259 } 260 261 /* Adds FIELD into the aggregate TYPE at OFFSET. */ 262 263 void 264 insert_aggregate_field (tree type, tree field, size_t offset) 265 { 266 DECL_FIELD_CONTEXT (field) = type; 267 SET_DECL_OFFSET_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field))); 268 DECL_FIELD_OFFSET (field) = size_int (offset); 269 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node; 270 271 TREE_ADDRESSABLE (field) = TYPE_SHARED (TREE_TYPE (field)); 272 273 layout_decl (field, 0); 274 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), field); 275 } 276 277 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */ 278 279 static tree 280 d_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp) 281 { 282 /* Same as d_type_for_size, but uses exact match for size. */ 283 if (width == TYPE_PRECISION (d_byte_type)) 284 return unsignedp ? d_ubyte_type : d_byte_type; 285 286 if (width == TYPE_PRECISION (d_short_type)) 287 return unsignedp ? d_ushort_type : d_short_type; 288 289 if (width == TYPE_PRECISION (d_int_type)) 290 return unsignedp ? d_uint_type : d_int_type; 291 292 if (width == TYPE_PRECISION (d_long_type)) 293 return unsignedp ? d_ulong_type : d_long_type; 294 295 if (width == TYPE_PRECISION (d_cent_type)) 296 return unsignedp ? d_ucent_type : d_cent_type; 297 298 for (int i = 0; i < NUM_INT_N_ENTS; i ++) 299 { 300 if (int_n_enabled_p[i] && width == int_n_data[i].bitsize) 301 { 302 if (unsignedp) 303 return int_n_trees[i].unsigned_type; 304 else 305 return int_n_trees[i].signed_type; 306 } 307 } 308 309 return build_nonstandard_integer_type (width, unsignedp); 310 } 311 312 /* Adds BITFIELD into the aggregate TYPE at OFFSET+BITOFFSET. */ 313 314 static void 315 insert_aggregate_bitfield (tree type, tree bitfield, size_t width, 316 size_t offset, size_t bitoffset) 317 { 318 DECL_FIELD_CONTEXT (bitfield) = type; 319 SET_DECL_OFFSET_ALIGN (bitfield, TYPE_ALIGN (TREE_TYPE (bitfield))); 320 DECL_SIZE (bitfield) = bitsize_int (width); 321 DECL_FIELD_OFFSET (bitfield) = size_int (offset); 322 DECL_FIELD_BIT_OFFSET (bitfield) = bitsize_int (bitoffset); 323 324 TREE_ADDRESSABLE (bitfield) = TYPE_SHARED (TREE_TYPE (bitfield)); 325 326 DECL_BIT_FIELD (bitfield) = 1; 327 DECL_BIT_FIELD_TYPE (bitfield) = TREE_TYPE (bitfield); 328 329 layout_decl (bitfield, 0); 330 331 /* Give bit-field its proper type after layout_decl. */ 332 tree orig_type = DECL_BIT_FIELD_TYPE (bitfield); 333 if (width != TYPE_PRECISION (orig_type)) 334 { 335 TREE_TYPE (bitfield) 336 = d_build_bitfield_integer_type (width, TYPE_UNSIGNED (orig_type)); 337 SET_DECL_MODE (bitfield, TYPE_MODE (TREE_TYPE (bitfield))); 338 } 339 340 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), bitfield); 341 } 342 343 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET. 344 This is done as the frontend puts fields into the outer struct, and so 345 their offset is from the beginning of the aggregate. 346 We want the offset to be from the beginning of the anonymous aggregate. */ 347 348 static void 349 fixup_anonymous_offset (tree fields, tree offset) 350 { 351 /* No adjustment in field offset required. */ 352 if (integer_zerop (offset)) 353 return; 354 355 while (fields != NULL_TREE) 356 { 357 /* Traverse all nested anonymous aggregates to update the offset of their 358 fields. Note that the anonymous field itself is not adjusted, as it 359 already has an offset relative to its outer aggregate. */ 360 tree ftype = TREE_TYPE (fields); 361 if (TYPE_NAME (ftype) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype))) 362 { 363 tree vfields = TYPE_FIELDS (ftype); 364 fixup_anonymous_offset (vfields, offset); 365 } 366 else 367 { 368 tree voffset = DECL_FIELD_OFFSET (fields); 369 DECL_FIELD_OFFSET (fields) = size_binop (MINUS_EXPR, voffset, offset); 370 } 371 372 fields = DECL_CHAIN (fields); 373 } 374 } 375 376 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT. 377 If INHERITED_P is true, then the members derive from a base class. 378 Returns the number of named fields found. */ 379 380 static size_t 381 layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p) 382 { 383 size_t fields = 0; 384 385 for (size_t i = 0; i < members->length; i++) 386 { 387 Dsymbol *sym = (*members)[i]; 388 VarDeclaration *var = sym->isVarDeclaration (); 389 if (var != NULL) 390 { 391 /* Skip fields that have already been added. */ 392 if (!inherited_p && var->csym != NULL) 393 continue; 394 395 /* If this variable was really a tuple, add all tuple fields. */ 396 if (var->aliassym) 397 { 398 TupleDeclaration *td = var->aliassym->isTupleDeclaration (); 399 Dsymbols tmembers; 400 /* No other way to coerce the underlying type out of the tuple. 401 Frontend should have already validated this. */ 402 for (size_t j = 0; j < td->objects->length; j++) 403 { 404 RootObject *ro = (*td->objects)[j]; 405 gcc_assert (ro->dyncast () == DYNCAST_EXPRESSION); 406 Expression *e = (Expression *) ro; 407 gcc_assert (e->op == EXP::dSymbol); 408 DsymbolExp *se = e->isDsymbolExp (); 409 410 tmembers.push (se->s); 411 } 412 413 fields += layout_aggregate_members (&tmembers, context, 414 inherited_p); 415 continue; 416 } 417 418 /* Insert the field declaration at its given offset. */ 419 if (var->isField ()) 420 { 421 const char *ident = (var->ident && !var->ident->isAnonymous ()) 422 ? var->ident->toChars () : NULL; 423 tree field = create_field_decl (declaration_type (var), ident, 424 inherited_p, inherited_p); 425 apply_user_attributes (var, field); 426 427 if (BitFieldDeclaration *bf = var->isBitFieldDeclaration ()) 428 { 429 /* Bit-fields come from an ImportC context, and require the 430 field be correctly adjusted. */ 431 insert_aggregate_bitfield (context, field, bf->fieldWidth, 432 bf->offset, bf->bitOffset); 433 } 434 else 435 insert_aggregate_field (context, field, var->offset); 436 437 /* Because the front-end shares field decls across classes, don't 438 create the corresponding back-end symbol unless we are adding 439 it to the aggregate it is defined in. */ 440 if (!inherited_p) 441 { 442 DECL_LANG_SPECIFIC (field) = build_lang_decl (var); 443 var->csym = field; 444 } 445 446 /* Only count the named fields in an aggregate. */ 447 if (ident != NULL) 448 fields += 1; 449 450 continue; 451 } 452 } 453 454 /* Anonymous struct/union are flattened by the frontend. However, we 455 want to keep the record layout in-tact when building the type. */ 456 AnonDeclaration *ad = sym->isAnonDeclaration (); 457 if (ad != NULL) 458 { 459 tree ident = make_anon_name (); 460 tree type = make_node (ad->isunion ? UNION_TYPE : RECORD_TYPE); 461 ANON_AGGR_TYPE_P (type) = 1; 462 d_keep (type); 463 464 /* Build the type declaration. */ 465 tree decl = build_decl (make_location_t (ad->loc), 466 TYPE_DECL, ident, type); 467 DECL_CONTEXT (decl) = context; 468 DECL_ARTIFICIAL (decl) = 1; 469 470 TYPE_CONTEXT (type) = context; 471 TYPE_NAME (type) = decl; 472 TYPE_STUB_DECL (type) = decl; 473 474 /* Recursively iterator over the anonymous members. */ 475 fields += layout_aggregate_members (ad->decl, type, inherited_p); 476 477 /* Remove from the anon fields the base offset of this anonymous 478 aggregate. Undoes what is set-up in setFieldOffset, but doesn't 479 affect field accesses. */ 480 tree offset = size_int (ad->anonoffset); 481 fixup_anonymous_offset (TYPE_FIELDS (type), offset); 482 483 finish_aggregate_type (ad->anonstructsize, ad->anonalignsize, type); 484 485 /* And make the corresponding data member. */ 486 tree field = create_field_decl (type, NULL, 0, 0); 487 apply_user_attributes (ad, field); 488 insert_aggregate_field (context, field, ad->anonoffset); 489 continue; 490 } 491 492 /* Other kinds of attributes don't create a scope. */ 493 AttribDeclaration *attrib = sym->isAttribDeclaration (); 494 if (attrib != NULL) 495 { 496 Dsymbols *decls = attrib->include (NULL); 497 if (decls != NULL) 498 { 499 fields += layout_aggregate_members (decls, context, inherited_p); 500 continue; 501 } 502 } 503 504 /* Same with template mixins and namespaces. */ 505 if (sym->isTemplateMixin () || sym->isNspace ()) 506 { 507 ScopeDsymbol *scopesym = sym->isScopeDsymbol (); 508 if (scopesym->members) 509 { 510 fields += layout_aggregate_members (scopesym->members, context, 511 inherited_p); 512 continue; 513 } 514 } 515 } 516 517 return fields; 518 } 519 520 /* Write out all fields for aggregate BASE. For classes, write out all 521 interfaces first, then the base class fields. */ 522 523 static void 524 layout_aggregate_type (AggregateDeclaration *decl, tree type, 525 AggregateDeclaration *base) 526 { 527 ClassDeclaration *cd = base->isClassDeclaration (); 528 bool inherited_p = (decl != base); 529 530 if (cd != NULL) 531 { 532 if (cd->baseClass) 533 layout_aggregate_type (decl, type, cd->baseClass); 534 else 535 { 536 /* This is the base class (Object) or interface. */ 537 tree objtype = TREE_TYPE (build_ctype (cd->type)); 538 539 /* Add the vtable pointer, and optionally the monitor fields. */ 540 InterfaceDeclaration *id = cd->isInterfaceDeclaration (); 541 if (!id || id->vtblInterfaces->length == 0) 542 { 543 tree field = create_field_decl (vtbl_ptr_type_node, "__vptr", 1, 544 inherited_p); 545 DECL_VIRTUAL_P (field) = 1; 546 TYPE_VFIELD (type) = field; 547 DECL_FCONTEXT (field) = objtype; 548 insert_aggregate_field (type, field, 0); 549 } 550 551 if (!id && cd->hasMonitor ()) 552 { 553 tree field = create_field_decl (ptr_type_node, "__monitor", 1, 554 inherited_p); 555 insert_aggregate_field (type, field, target.ptrsize); 556 } 557 } 558 559 if (cd->vtblInterfaces) 560 { 561 for (size_t i = 0; i < cd->vtblInterfaces->length; i++) 562 { 563 BaseClass *bc = (*cd->vtblInterfaces)[i]; 564 tree field = create_field_decl (vtbl_ptr_type_node, NULL, 1, 1); 565 insert_aggregate_field (type, field, bc->offset); 566 } 567 } 568 } 569 570 if (base->members) 571 { 572 size_t fields = layout_aggregate_members (base->members, type, 573 inherited_p); 574 gcc_assert (fields == base->fields.length); 575 576 /* Make sure that all fields have been created. */ 577 if (!inherited_p) 578 { 579 for (size_t i = 0; i < base->fields.length; i++) 580 { 581 VarDeclaration *var = base->fields[i]; 582 gcc_assert (var->csym != NULL); 583 } 584 } 585 } 586 } 587 588 /* Given a record type TYPE, whose size and alignment are determined by 589 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute 590 the finalized record mode. */ 591 592 void 593 finish_aggregate_type (unsigned structsize, unsigned alignsize, tree type) 594 { 595 /* Set size and alignment as requested by frontend. */ 596 TYPE_SIZE (type) = bitsize_int (structsize * BITS_PER_UNIT); 597 TYPE_SIZE_UNIT (type) = size_int (structsize); 598 SET_TYPE_ALIGN (type, alignsize * BITS_PER_UNIT); 599 TYPE_PACKED (type) = (alignsize == 1); 600 601 /* Set the back-end type mode. */ 602 compute_record_mode (type); 603 604 /* Fix up all variants of this aggregate type. */ 605 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 606 { 607 if (t == type) 608 continue; 609 610 TYPE_FIELDS (t) = TYPE_FIELDS (type); 611 TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (type); 612 SET_TYPE_ALIGN (t, TYPE_ALIGN (type)); 613 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type); 614 gcc_assert (TYPE_MODE (t) == TYPE_MODE (type)); 615 } 616 } 617 618 /* Returns true if the class or struct type TYPE has already been layed out by 619 the lowering of another front-end AST type. In which case, there will either 620 be a reuse of the back-end type, or a multiple definition error. 621 DECO is the uniquely mangled decoration for the type. */ 622 623 static bool 624 merge_aggregate_types (Type *type, tree deco) 625 { 626 AggregateDeclaration *sym; 627 628 if (type->ty == TY::Tstruct) 629 sym = type->isTypeStruct ()->sym; 630 else if (type->ty == TY::Tclass) 631 sym = type->isTypeClass ()->sym; 632 else 633 gcc_unreachable (); 634 635 if (IDENTIFIER_DAGGREGATE (deco)) 636 { 637 AggregateDeclaration *ad = IDENTIFIER_DAGGREGATE (deco); 638 /* There should never be a class/struct mismatch in mangled names. */ 639 gcc_assert ((sym->isStructDeclaration () && ad->isStructDeclaration ()) 640 || (sym->isClassDeclaration () && ad->isClassDeclaration ())); 641 642 /* Non-templated variables shouldn't be defined twice. */ 643 if (!sym->isInstantiated ()) 644 ScopeDsymbol::multiplyDefined (sym->loc, sym, ad); 645 646 type->ctype = build_ctype (ad->type); 647 return true; 648 } 649 650 return false; 651 } 652 653 /* Implements the visitor interface to build the GCC trees of all 654 Type AST classes emitted from the D Front-end, where CTYPE holds 655 the cached back-end representation to be returned. */ 656 657 class TypeVisitor : public Visitor 658 { 659 using Visitor::visit; 660 661 public: 662 TypeVisitor (void) 663 { 664 } 665 666 /* This should be overridden by each type class. */ 667 668 void visit (Type *) 669 { 670 gcc_unreachable (); 671 } 672 673 /* Type assigned to erroneous expressions or constructs that 674 failed during the semantic stage. */ 675 676 void visit (TypeError *t) 677 { 678 t->ctype = error_mark_node; 679 } 680 681 /* Type assigned to generic nullable types. */ 682 683 void visit (TypeNull *t) 684 { 685 t->ctype = ptr_type_node; 686 } 687 688 /* Bottom type used for functions that never return. */ 689 690 void visit (TypeNoreturn *t) 691 { 692 t->ctype = noreturn_type_node; 693 TYPE_NAME (t->ctype) = get_identifier (t->toChars ()); 694 } 695 696 /* Basic Data Types. */ 697 698 void visit (TypeBasic *t) 699 { 700 /* [type/basic-data-types] 701 702 void no type. 703 bool 8-bit boolean value. 704 byte 8-bit signed value. 705 ubyte 8-bit unsigned value. 706 short 16-bit signed value. 707 ushort 16-bit unsigned value. 708 int 32-bit signed value. 709 uint 32-bit unsigned value. 710 long 64-bit signed value. 711 ulong 64-bit unsigned value. 712 cent 128-bit signed value. 713 ucent 128-bit unsigned value. 714 float 32-bit IEEE 754 floating-point value. 715 double 64-bit IEEE 754 floating-point value. 716 real largest FP size implemented in hardware. 717 ifloat imaginary float. 718 idouble imaginary double. 719 ireal imaginary real. 720 cfloat complex float. 721 cdouble complex double. 722 creal complex real. 723 char UTF-8 code unit. 724 wchar UTF-16 code unit. 725 dchar UTF-32 code unit. */ 726 727 switch (t->ty) 728 { 729 case TY::Tvoid: t->ctype = void_type_node; break; 730 case TY::Tbool: t->ctype = d_bool_type; break; 731 case TY::Tint8: t->ctype = d_byte_type; break; 732 case TY::Tuns8: t->ctype = d_ubyte_type; break; 733 case TY::Tint16: t->ctype = d_short_type; break; 734 case TY::Tuns16: t->ctype = d_ushort_type; break; 735 case TY::Tint32: t->ctype = d_int_type; break; 736 case TY::Tuns32: t->ctype = d_uint_type; break; 737 case TY::Tint64: t->ctype = d_long_type; break; 738 case TY::Tuns64: t->ctype = d_ulong_type; break; 739 case TY::Tint128: t->ctype = d_cent_type; break; 740 case TY::Tuns128: t->ctype = d_ucent_type; break; 741 case TY::Tfloat32: t->ctype = float_type_node; break; 742 case TY::Tfloat64: t->ctype = double_type_node; break; 743 case TY::Tfloat80: t->ctype = long_double_type_node; break; 744 case TY::Timaginary32: t->ctype = ifloat_type_node; break; 745 case TY::Timaginary64: t->ctype = idouble_type_node; break; 746 case TY::Timaginary80: t->ctype = ireal_type_node; break; 747 case TY::Tcomplex32: t->ctype = complex_float_type_node; break; 748 case TY::Tcomplex64: t->ctype = complex_double_type_node; break; 749 case TY::Tcomplex80: t->ctype = complex_long_double_type_node; break; 750 case TY::Tchar: t->ctype = char8_type_node; break; 751 case TY::Twchar: t->ctype = char16_type_node; break; 752 case TY::Tdchar: t->ctype = char32_type_node; break; 753 default: gcc_unreachable (); 754 } 755 756 TYPE_NAME (t->ctype) = get_identifier (t->toChars ()); 757 } 758 759 760 /* Derived Data Types. */ 761 762 /* Build a simple pointer to data type, analogous to C pointers. */ 763 764 void visit (TypePointer *t) 765 { 766 t->ctype = build_pointer_type (build_ctype (t->next)); 767 } 768 769 /* Build a dynamic array type, consisting of a length and a pointer 770 to the array data. */ 771 772 void visit (TypeDArray *t) 773 { 774 /* In [abi/arrays], dynamic array layout is: 775 .length array dimension. 776 .ptr pointer to array data. */ 777 t->ctype = make_struct_type (t->toChars (), 2, 778 get_identifier ("length"), 779 build_ctype (Type::tsize_t), 780 get_identifier ("ptr"), 781 build_pointer_type (build_ctype (t->next))); 782 TYPE_DYNAMIC_ARRAY (t->ctype) = 1; 783 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t); 784 d_keep (t->ctype); 785 } 786 787 /* Build a static array type, distinguished from dynamic arrays by 788 having a length fixed at compile-time, analogous to C arrays. */ 789 790 void visit (TypeSArray *t) 791 { 792 if (t->dim->isConst () && t->dim->type->isintegral ()) 793 { 794 uinteger_t size = t->dim->toUInteger (); 795 t->ctype = make_array_type (t->next, size); 796 } 797 else 798 { 799 error ("invalid expression for static array dimension: %s", 800 t->dim->toChars ()); 801 gcc_unreachable (); 802 } 803 } 804 805 /* Build a vector type, a fixed array of floating or integer types. */ 806 807 void visit (TypeVector *t) 808 { 809 int nunits = t->basetype->isTypeSArray ()->dim->toUInteger (); 810 tree inner = build_ctype (t->elementType ()); 811 812 /* Same rationale as void static arrays. */ 813 if (inner == void_type_node) 814 inner = build_ctype (Type::tuns8); 815 816 t->ctype = build_vector_type (inner, nunits); 817 TYPE_NAME (t->ctype) = get_identifier (t->toChars ()); 818 layout_type (t->ctype); 819 } 820 821 /* Build an associative array type, distinguished from arrays by having an 822 index that's not necessarily an integer, and can be sparsely populated. */ 823 824 void visit (TypeAArray *t) 825 { 826 /* In [abi/associative-arrays], associative arrays are a struct that only 827 consist of a pointer to an opaque, implementation defined type. */ 828 t->ctype = make_struct_type (t->toChars (), 1, 829 get_identifier ("ptr"), ptr_type_node); 830 TYPE_ASSOCIATIVE_ARRAY (t->ctype) = 1; 831 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t); 832 d_keep (t->ctype); 833 } 834 835 /* Build type for a function declaration, which consists of a return type, 836 and a list of parameter types, and a linkage attribute. */ 837 838 void visit (TypeFunction *t) 839 { 840 tree fnparams = NULL_TREE; 841 tree fntype; 842 843 /* [function/variadic] 844 845 Variadic functions with D linkage have an additional hidden argument 846 with the name _arguments passed to the function. */ 847 if (t->isDstyleVariadic ()) 848 { 849 tree type = build_ctype (Type::typeinfotypelist->type); 850 fnparams = chainon (fnparams, build_tree_list (0, type)); 851 } 852 853 const size_t n_args = t->parameterList.length (); 854 855 for (size_t i = 0; i < n_args; i++) 856 { 857 tree type = parameter_type (t->parameterList[i]); 858 859 /* Type `noreturn` is a terminator, as no other arguments can possibly 860 be evaluated after it. */ 861 if (type == noreturn_type_node) 862 break; 863 864 fnparams = chainon (fnparams, build_tree_list (0, type)); 865 } 866 867 /* When the last parameter is void_list_node, that indicates a fixed length 868 parameter list, otherwise function is treated as variadic. */ 869 if (t->parameterList.varargs != VARARGvariadic) 870 fnparams = chainon (fnparams, void_list_node); 871 872 if (t->next != NULL) 873 { 874 fntype = build_ctype (t->next); 875 if (t->isref ()) 876 fntype = build_reference_type (fntype); 877 } 878 else 879 fntype = void_type_node; 880 881 /* Could the function type be self referenced by parameters? */ 882 t->ctype = build_function_type (fntype, fnparams); 883 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t); 884 d_keep (t->ctype); 885 886 /* Qualify function types that have the type `noreturn` as volatile. */ 887 if (fntype == noreturn_type_node) 888 t->ctype = build_qualified_type (t->ctype, TYPE_QUAL_VOLATILE); 889 890 /* Handle any special support for calling conventions. */ 891 switch (t->linkage) 892 { 893 case LINK::windows: 894 { 895 /* [attribute/linkage] 896 897 The Windows convention is distinct from the C convention only 898 on Win32, where it is equivalent to the stdcall convention. */ 899 unsigned link_system, link_windows; 900 if (targetdm.d_has_stdcall_convention (&link_system, &link_windows)) 901 { 902 if (link_windows) 903 t->ctype = insert_type_attribute (t->ctype, "stdcall"); 904 } 905 break; 906 } 907 908 case LINK::c: 909 case LINK::cpp: 910 case LINK::d: 911 case LINK::objc: 912 /* [abi/function-calling-conventions] 913 914 The extern (C) and extern (D) calling convention matches 915 the C calling convention used by the supported C compiler 916 on the host system. */ 917 break; 918 919 default: 920 gcc_unreachable (); 921 } 922 } 923 924 /* Build a delegate type, an aggregate of two pieces of data, an object 925 reference and a pointer to a non-static member function, or a pointer 926 to a closure and a pointer to a nested function. */ 927 928 void visit (TypeDelegate *t) 929 { 930 /* In [abi/delegates], delegate layout is: 931 .ptr context pointer. 932 .funcptr pointer to function. */ 933 tree fntype = build_ctype (t->next); 934 tree dgtype = build_vthis_function (void_type_node, fntype); 935 936 TYPE_ATTRIBUTES (dgtype) = TYPE_ATTRIBUTES (fntype); 937 TYPE_LANG_SPECIFIC (dgtype) = TYPE_LANG_SPECIFIC (fntype); 938 939 t->ctype = make_struct_type (t->toChars (), 2, 940 get_identifier ("ptr"), 941 build_ctype (Type::tvoidptr), 942 get_identifier ("funcptr"), 943 build_pointer_type (dgtype)); 944 TYPE_DELEGATE (t->ctype) = 1; 945 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t); 946 d_keep (t->ctype); 947 } 948 949 950 /* User Defined Types. */ 951 952 /* Build a named enum type, a distinct value whose values are restrict to 953 a group of constants of the same underlying base type. */ 954 955 void visit (TypeEnum *t) 956 { 957 tree basetype = (t->sym->memtype) 958 ? build_ctype (t->sym->memtype) : void_type_node; 959 960 if (t->sym->isSpecial ()) 961 { 962 /* Special enums are opaque types that bind to C types. */ 963 const char *ident = t->toChars (); 964 Type *underlying = NULL; 965 966 /* Skip over the prefixing `__c_'. */ 967 gcc_assert (startswith (ident, "__c_")); 968 ident = ident + strlen ("__c_"); 969 970 /* To keep things compatible within the code generation we stick to 971 mapping to equivalent D types. However it should be OK to use the 972 GCC provided C types here as the front-end enforces that everything 973 must be explicitly cast from a D type to any of the opaque types. */ 974 if (strcmp (ident, "long") == 0) 975 underlying = build_frontend_type (long_integer_type_node); 976 else if (strcmp (ident, "ulong") == 0) 977 underlying = build_frontend_type (long_unsigned_type_node); 978 else if (strcmp (ident, "wchar_t") == 0) 979 underlying = 980 build_frontend_type (make_unsigned_type (WCHAR_TYPE_SIZE)); 981 else if (strcmp (ident, "longlong") == 0) 982 underlying = build_frontend_type (long_long_integer_type_node); 983 else if (strcmp (ident, "ulonglong") == 0) 984 underlying = build_frontend_type (long_long_unsigned_type_node); 985 else if (strcmp (ident, "long_double") == 0) 986 underlying = build_frontend_type (long_double_type_node); 987 else if (strcmp (ident, "complex_real") == 0) 988 underlying = build_frontend_type (complex_long_double_type_node); 989 else if (strcmp (ident, "complex_float") == 0) 990 underlying = build_frontend_type (complex_float_type_node); 991 else if (strcmp (ident, "complex_double") == 0) 992 underlying = build_frontend_type (complex_double_type_node); 993 994 /* Conversion failed or there's an unhandled special type. */ 995 gcc_assert (underlying != NULL); 996 997 t->ctype = build_variant_type_copy (build_ctype (underlying)); 998 build_type_decl (t->ctype, t->sym); 999 } 1000 else if (t->sym->ident == NULL 1001 || !INTEGRAL_TYPE_P (basetype) 1002 || TREE_CODE (basetype) == BOOLEAN_TYPE) 1003 { 1004 /* Enums in D2 can either be anonymous, or have a base type that is not 1005 necessarily integral. For these, we simplify this a little by using 1006 the base type directly instead of building an ENUMERAL_TYPE. */ 1007 t->ctype = build_variant_type_copy (basetype); 1008 1009 if (t->sym->ident != NULL) 1010 build_type_decl (t->ctype, t->sym); 1011 } 1012 else 1013 { 1014 t->ctype = make_node (ENUMERAL_TYPE); 1015 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t); 1016 d_keep (t->ctype); 1017 1018 ENUM_IS_SCOPED (t->ctype) = 1; 1019 TREE_TYPE (t->ctype) = basetype; 1020 1021 if (flag_short_enums) 1022 TYPE_PACKED (t->ctype) = 1; 1023 1024 TYPE_PRECISION (t->ctype) = t->size (t->sym->loc) * 8; 1025 TYPE_SIZE (t->ctype) = 0; 1026 1027 TYPE_MIN_VALUE (t->ctype) = TYPE_MIN_VALUE (basetype); 1028 TYPE_MAX_VALUE (t->ctype) = TYPE_MAX_VALUE (basetype); 1029 layout_type (t->ctype); 1030 1031 tree values = NULL_TREE; 1032 if (t->sym->members) 1033 { 1034 for (size_t i = 0; i < t->sym->members->length; i++) 1035 { 1036 EnumMember *member = (*t->sym->members)[i]->isEnumMember (); 1037 /* Templated functions can seep through to the back-end 1038 just ignore for now. */ 1039 if (member == NULL) 1040 continue; 1041 1042 tree ident = get_identifier (member->ident->toChars ()); 1043 tree value = build_integer_cst (member->value ()->toInteger (), 1044 basetype); 1045 1046 /* Build an identifier for the enumeration constant. */ 1047 tree decl = build_decl (make_location_t (member->loc), 1048 CONST_DECL, ident, basetype); 1049 DECL_CONTEXT (decl) = t->ctype; 1050 TREE_CONSTANT (decl) = 1; 1051 TREE_READONLY (decl) = 1; 1052 DECL_INITIAL (decl) = value; 1053 1054 /* Add this enumeration constant to the list for this type. */ 1055 values = chainon (values, build_tree_list (ident, decl)); 1056 } 1057 } 1058 1059 TYPE_VALUES (t->ctype) = values; 1060 TYPE_UNSIGNED (t->ctype) = TYPE_UNSIGNED (basetype); 1061 build_type_decl (t->ctype, t->sym); 1062 } 1063 1064 apply_user_attributes (t->sym, t->ctype); 1065 } 1066 1067 /* Build a struct or union type. Layout should be exactly represented 1068 as an equivalent C struct, except for non-POD or nested structs. */ 1069 1070 void visit (TypeStruct *t) 1071 { 1072 /* Merge types in the back-end if the front-end did not itself do so. */ 1073 tree deco = get_identifier (d_mangle_decl (t->sym)); 1074 if (merge_aggregate_types (t, deco)) 1075 return; 1076 1077 /* Need to set this right away in case of self-references. */ 1078 t->ctype = make_node (t->sym->isUnionDeclaration () 1079 ? UNION_TYPE : RECORD_TYPE); 1080 d_keep (t->ctype); 1081 IDENTIFIER_DAGGREGATE (deco) = t->sym; 1082 1083 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t); 1084 TYPE_CXX_ODR_P (t->ctype) = 1; 1085 1086 if (t->sym->members) 1087 { 1088 /* Must set up the overall size and alignment before determining 1089 the context or laying out fields as those types may make 1090 recursive references to this type. */ 1091 unsigned structsize = t->sym->structsize; 1092 unsigned alignsize = t->sym->alignment.isDefault () 1093 ? t->sym->alignsize : t->sym->alignment.get (); 1094 1095 TYPE_SIZE (t->ctype) = bitsize_int (structsize * BITS_PER_UNIT); 1096 TYPE_SIZE_UNIT (t->ctype) = size_int (structsize); 1097 SET_TYPE_ALIGN (t->ctype, alignsize * BITS_PER_UNIT); 1098 TYPE_PACKED (t->ctype) = (alignsize == 1); 1099 compute_record_mode (t->ctype); 1100 1101 /* Put out all fields. */ 1102 layout_aggregate_type (t->sym, t->ctype, t->sym); 1103 apply_user_attributes (t->sym, t->ctype); 1104 finish_aggregate_type (structsize, alignsize, t->ctype); 1105 } 1106 else 1107 { 1108 build_type_decl (t->ctype, t->sym); 1109 apply_user_attributes (t->sym, t->ctype); 1110 } 1111 1112 TYPE_CONTEXT (t->ctype) = d_decl_context (t->sym); 1113 build_type_decl (t->ctype, t->sym); 1114 1115 /* For structs with a user defined postblit, copy constructor, or a 1116 destructor, also set TREE_ADDRESSABLE on the type and all variants. 1117 This will make the struct be passed around by reference. */ 1118 if (!t->sym->isPOD ()) 1119 { 1120 for (tree tv = t->ctype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv)) 1121 { 1122 TREE_ADDRESSABLE (tv) = 1; 1123 SET_TYPE_MODE (tv, BLKmode); 1124 } 1125 } 1126 } 1127 1128 /* Build a class type. Whereas structs are value types, classes are 1129 reference types, with all the object-orientated features. */ 1130 1131 void visit (TypeClass *t) 1132 { 1133 /* Merge types in the back-end if the front-end did not itself do so. */ 1134 tree deco = get_identifier (d_mangle_decl (t->sym)); 1135 if (merge_aggregate_types (t, deco)) 1136 return; 1137 1138 /* Need to set ctype right away in case of self-references to 1139 the type during this call. */ 1140 tree basetype = make_node (RECORD_TYPE); 1141 t->ctype = build_pointer_type (basetype); 1142 d_keep (t->ctype); 1143 IDENTIFIER_DAGGREGATE (deco) = t->sym; 1144 1145 /* Note that lang_specific data is assigned to both the reference 1146 and the underlying record type. */ 1147 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t); 1148 TYPE_LANG_SPECIFIC (basetype) = TYPE_LANG_SPECIFIC (t->ctype); 1149 CLASS_TYPE_P (basetype) = 1; 1150 TYPE_CXX_ODR_P (basetype) = 1; 1151 1152 /* Put out all fields, including from each base class. */ 1153 layout_aggregate_type (t->sym, basetype, t->sym); 1154 apply_user_attributes (t->sym, basetype); 1155 finish_aggregate_type (t->sym->structsize, t->sym->alignsize, basetype); 1156 1157 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */ 1158 for (tree tv = basetype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv)) 1159 { 1160 TREE_ADDRESSABLE (tv) = 1; 1161 SET_TYPE_MODE (tv, BLKmode); 1162 } 1163 1164 /* Type is final, there are no derivations. */ 1165 if (t->sym->storage_class & STCfinal) 1166 TYPE_FINAL_P (basetype) = 1; 1167 1168 /* Create BINFO even if debugging is off. This is needed to keep 1169 references to inherited types. */ 1170 if (!t->sym->isInterfaceDeclaration ()) 1171 TYPE_BINFO (basetype) = build_class_binfo (NULL_TREE, t->sym); 1172 else 1173 { 1174 unsigned offset = 0; 1175 1176 TYPE_BINFO (basetype) = build_interface_binfo (NULL_TREE, t->sym, 1177 offset); 1178 } 1179 1180 /* Associate all virtual methods with the class too. */ 1181 for (size_t i = 0; i < t->sym->vtbl.length; i++) 1182 { 1183 FuncDeclaration *fd = t->sym->vtbl[i]->isFuncDeclaration (); 1184 tree method = fd ? get_symbol_decl (fd) : error_mark_node; 1185 1186 if (!error_operand_p (method) 1187 && DECL_CONTEXT (method) == basetype 1188 && !chain_member (method, TYPE_FIELDS (basetype))) 1189 TYPE_FIELDS (basetype) = chainon (TYPE_FIELDS (basetype), method); 1190 } 1191 1192 TYPE_CONTEXT (basetype) = d_decl_context (t->sym); 1193 build_type_decl (basetype, t->sym); 1194 } 1195 }; 1196 1197 1198 /* Build a tree from a frontend Type. */ 1199 1200 tree 1201 build_ctype (Type *t) 1202 { 1203 if (!t->ctype) 1204 { 1205 TypeVisitor v; 1206 1207 /* Strip const modifiers from type before building. This is done 1208 to ensure that back-end treats e.g: const (T) as a variant of T, 1209 and not as two distinct types. */ 1210 if (t->isNaked ()) 1211 t->accept (&v); 1212 else 1213 { 1214 Type *tb = t->castMod (0); 1215 if (!tb->ctype) 1216 tb->accept (&v); 1217 t->ctype = insert_type_modifiers (tb->ctype, t->mod); 1218 } 1219 } 1220 1221 return t->ctype; 1222 } 1223