1 /* types.cc -- Lower D frontend types to GCC trees.
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
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)
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.
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/>. */
20 #include "coretypes.h"
22 #include "dmd/attrib.h"
23 #include "dmd/aggregate.h"
25 #include "dmd/expression.h"
26 #include "dmd/identifier.h"
27 #include "dmd/mtype.h"
28 #include "dmd/target.h"
31 #include "fold-const.h"
32 #include "diagnostic.h"
33 #include "langhooks.h"
38 #include "stringpool.h"
39 #include "stor-layout.h"
46 /* Return the signed or unsigned version of TYPE, an integral type, the
47 signedness being specified by UNSIGNEDP. */
50 d_signed_or_unsigned_type (int unsignedp
, tree type
)
52 if (TYPE_UNSIGNED (type
) == (unsigned) unsignedp
)
55 if (TYPE_PRECISION (type
) == TYPE_PRECISION (d_cent_type
))
56 return unsignedp
? d_ucent_type
: d_cent_type
;
58 if (TYPE_PRECISION (type
) == TYPE_PRECISION (d_long_type
))
59 return unsignedp
? d_ulong_type
: d_long_type
;
61 if (TYPE_PRECISION (type
) == TYPE_PRECISION (d_int_type
))
62 return unsignedp
? d_uint_type
: d_int_type
;
64 if (TYPE_PRECISION (type
) == TYPE_PRECISION (d_short_type
))
65 return unsignedp
? d_ushort_type
: d_short_type
;
67 if (TYPE_PRECISION (type
) == TYPE_PRECISION (d_byte_type
))
68 return unsignedp
? d_ubyte_type
: d_byte_type
;
70 return signed_or_unsigned_type_for (unsignedp
, type
);
73 /* Return the unsigned version of TYPE, an integral type. */
76 d_unsigned_type (tree type
)
78 return d_signed_or_unsigned_type (1, type
);
81 /* Return the signed version of TYPE, an integral type. */
84 d_signed_type (tree type
)
86 return d_signed_or_unsigned_type (0, type
);
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. */
94 valist_array_p (Type
*type
)
96 Type
*tvalist
= target
.va_listType (Loc (), NULL
);
97 if (tvalist
->ty
== TY::Tsarray
)
99 Type
*tb
= type
->toBasetype ();
100 if (same_type_p (tb
, tvalist
))
107 /* Returns true if TYPE contains no actual data, just various
108 possible combinations of empty aggregates. */
111 empty_aggregate_p (tree type
)
113 if (!AGGREGATE_TYPE_P (type
))
116 /* Want the element type for arrays. */
117 if (TREE_CODE (type
) == ARRAY_TYPE
)
118 return empty_aggregate_p (TREE_TYPE (type
));
120 /* Recursively check all fields. */
121 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
123 if (TREE_CODE (field
) == FIELD_DECL
124 && !empty_aggregate_p (TREE_TYPE (field
)))
131 /* Returns true if T1 and T2 are related to each other. */
134 same_type_p (Type
*t1
, Type
*t2
)
136 /* Types are equal. */
140 /* Types derive from the same base. */
141 Type
*tb1
= t1
->toBasetype ();
142 Type
*tb2
= t2
->toBasetype ();
146 /* Types are mutably the same type. */
147 if (tb1
->ty
== tb2
->ty
&& tb1
->equivalent (tb2
))
153 /* Returns `Object' type which all D classes are derived from. */
156 get_object_type (void)
158 if (ClassDeclaration::object
)
159 return ClassDeclaration::object
->type
;
161 error ("missing or corrupt object.d");
166 /* Returns a static array of TYPE which has SIZE number of elements. */
169 make_array_type (Type
*type
, unsigned HOST_WIDE_INT size
)
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
)
176 /* In [arrays/static-arrays], a static array with a dimension of 0 is allowed,
177 but no space is allocated for it. */
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
);
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
;
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
));
197 /* Builds a record type whose name is NAME. NFIELDS is the number of fields,
198 provided as field ident/type pairs. */
201 make_struct_type (const char *name
, int nfields
, ...)
203 tree fields
= NULL_TREE
;
206 va_start (ap
, nfields
);
208 for (int i
= 0; i
< nfields
; i
++)
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
;
219 tree type
= make_node (RECORD_TYPE
);
220 finish_builtin_struct (type
, name
, fields
, NULL_TREE
);
225 /* Return qualified type variant of TYPE determined by modifier value MOD. */
228 insert_type_modifiers (tree type
, unsigned mod
)
238 case MODshared
| MODconst
:
239 case MODshared
| MODwild
:
240 case MODshared
| MODwildconst
:
241 quals
|= TYPE_QUAL_CONST
;
252 tree qualtype
= build_qualified_type (type
, quals
);
254 /* Mark whether the type is qualified `shared'. */
256 TYPE_SHARED (qualtype
) = 1;
261 /* Adds FIELD into the aggregate TYPE at OFFSET. */
264 insert_aggregate_field (tree type
, tree field
, size_t offset
)
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
;
271 TREE_ADDRESSABLE (field
) = TYPE_SHARED (TREE_TYPE (field
));
273 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), field
);
276 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
279 d_build_bitfield_integer_type (unsigned HOST_WIDE_INT width
, int unsignedp
)
281 /* Same as d_type_for_size, but uses exact match for size. */
282 if (width
== TYPE_PRECISION (d_byte_type
))
283 return unsignedp
? d_ubyte_type
: d_byte_type
;
285 if (width
== TYPE_PRECISION (d_short_type
))
286 return unsignedp
? d_ushort_type
: d_short_type
;
288 if (width
== TYPE_PRECISION (d_int_type
))
289 return unsignedp
? d_uint_type
: d_int_type
;
291 if (width
== TYPE_PRECISION (d_long_type
))
292 return unsignedp
? d_ulong_type
: d_long_type
;
294 if (width
== TYPE_PRECISION (d_cent_type
))
295 return unsignedp
? d_ucent_type
: d_cent_type
;
297 for (int i
= 0; i
< NUM_INT_N_ENTS
; i
++)
299 if (int_n_enabled_p
[i
] && width
== int_n_data
[i
].bitsize
)
302 return int_n_trees
[i
].unsigned_type
;
304 return int_n_trees
[i
].signed_type
;
308 return build_nonstandard_integer_type (width
, unsignedp
);
311 /* Adds BITFIELD into the aggregate TYPE at OFFSET+BITOFFSET. */
314 insert_aggregate_bitfield (tree type
, tree bitfield
, size_t width
,
315 size_t offset
, size_t bitoffset
)
317 DECL_FIELD_CONTEXT (bitfield
) = type
;
318 SET_DECL_OFFSET_ALIGN (bitfield
, TYPE_ALIGN (TREE_TYPE (bitfield
)));
319 DECL_SIZE (bitfield
) = bitsize_int (width
);
320 DECL_FIELD_OFFSET (bitfield
) = size_int (offset
);
321 DECL_FIELD_BIT_OFFSET (bitfield
) = bitsize_int (bitoffset
);
323 TREE_ADDRESSABLE (bitfield
) = TYPE_SHARED (TREE_TYPE (bitfield
));
325 DECL_BIT_FIELD (bitfield
) = 1;
326 DECL_BIT_FIELD_TYPE (bitfield
) = TREE_TYPE (bitfield
);
328 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), bitfield
);
331 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET.
332 This is done as the frontend puts fields into the outer struct, and so
333 their offset is from the beginning of the aggregate.
334 We want the offset to be from the beginning of the anonymous aggregate. */
337 fixup_anonymous_offset (tree fields
, tree offset
)
339 /* No adjustment in field offset required. */
340 if (integer_zerop (offset
))
343 while (fields
!= NULL_TREE
)
345 /* Traverse all nested anonymous aggregates to update the offset of their
346 fields. Note that the anonymous field itself is not adjusted, as it
347 already has an offset relative to its outer aggregate. */
348 tree ftype
= TREE_TYPE (fields
);
349 if (TYPE_NAME (ftype
) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype
)))
351 tree vfields
= TYPE_FIELDS (ftype
);
352 fixup_anonymous_offset (vfields
, offset
);
356 tree voffset
= DECL_FIELD_OFFSET (fields
);
357 DECL_FIELD_OFFSET (fields
) = size_binop (MINUS_EXPR
, voffset
, offset
);
360 fields
= DECL_CHAIN (fields
);
364 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT.
365 If INHERITED_P is true, then the members derive from a base class.
366 Returns the number of named fields found. */
369 layout_aggregate_members (Dsymbols
*members
, tree context
, bool inherited_p
)
373 for (size_t i
= 0; i
< members
->length
; i
++)
375 Dsymbol
*sym
= (*members
)[i
];
376 VarDeclaration
*var
= sym
->isVarDeclaration ();
379 /* Skip fields that have already been added. */
380 if (!inherited_p
&& var
->csym
!= NULL
)
383 /* If this variable was really a tuple, add all tuple fields. */
386 TupleDeclaration
*td
= var
->aliassym
->isTupleDeclaration ();
388 /* No other way to coerce the underlying type out of the tuple.
389 Frontend should have already validated this. */
390 for (size_t j
= 0; j
< td
->objects
->length
; j
++)
392 RootObject
*ro
= (*td
->objects
)[j
];
393 gcc_assert (ro
->dyncast () == DYNCAST_EXPRESSION
);
394 Expression
*e
= (Expression
*) ro
;
395 gcc_assert (e
->op
== EXP::variable
);
396 VarExp
*ve
= e
->isVarExp ();
398 tmembers
.push (ve
->var
);
401 fields
+= layout_aggregate_members (&tmembers
, context
,
406 /* Insert the field declaration at its given offset. */
409 const char *ident
= (var
->ident
&& !var
->ident
->isAnonymous ())
410 ? var
->ident
->toChars () : NULL
;
411 tree field
= create_field_decl (declaration_type (var
), ident
,
412 inherited_p
, inherited_p
);
413 apply_user_attributes (var
, field
);
415 if (BitFieldDeclaration
*bf
= var
->isBitFieldDeclaration ())
417 /* Bit-fields come from an ImportC context, and require the
418 field be correctly adjusted. */
419 insert_aggregate_bitfield (context
, field
, bf
->fieldWidth
,
420 bf
->offset
, bf
->bitOffset
);
423 insert_aggregate_field (context
, field
, var
->offset
);
425 /* Because the front-end shares field decls across classes, don't
426 create the corresponding back-end symbol unless we are adding
427 it to the aggregate it is defined in. */
430 DECL_LANG_SPECIFIC (field
) = build_lang_decl (var
);
434 /* Only count the named fields in an aggregate. */
442 /* Anonymous struct/union are flattened by the frontend. However, we
443 want to keep the record layout in-tact when building the type. */
444 AnonDeclaration
*ad
= sym
->isAnonDeclaration ();
447 tree ident
= make_anon_name ();
448 tree type
= make_node (ad
->isunion
? UNION_TYPE
: RECORD_TYPE
);
449 ANON_AGGR_TYPE_P (type
) = 1;
452 /* Build the type declaration. */
453 tree decl
= build_decl (make_location_t (ad
->loc
),
454 TYPE_DECL
, ident
, type
);
455 DECL_CONTEXT (decl
) = context
;
456 DECL_ARTIFICIAL (decl
) = 1;
458 TYPE_CONTEXT (type
) = context
;
459 TYPE_NAME (type
) = decl
;
460 TYPE_STUB_DECL (type
) = decl
;
462 /* Recursively iterator over the anonymous members. */
463 fields
+= layout_aggregate_members (ad
->decl
, type
, inherited_p
);
465 /* Remove from the anon fields the base offset of this anonymous
466 aggregate. Undoes what is set-up in setFieldOffset, but doesn't
467 affect field accesses. */
468 tree offset
= size_int (ad
->anonoffset
);
469 fixup_anonymous_offset (TYPE_FIELDS (type
), offset
);
471 finish_aggregate_type (ad
->anonstructsize
, ad
->anonalignsize
, type
);
473 /* And make the corresponding data member. */
474 tree field
= create_field_decl (type
, NULL
, 0, 0);
475 apply_user_attributes (ad
, field
);
476 insert_aggregate_field (context
, field
, ad
->anonoffset
);
480 /* Other kinds of attributes don't create a scope. */
481 AttribDeclaration
*attrib
= sym
->isAttribDeclaration ();
484 Dsymbols
*decls
= attrib
->include (NULL
);
487 fields
+= layout_aggregate_members (decls
, context
, inherited_p
);
492 /* Same with template mixins and namespaces. */
493 if (sym
->isTemplateMixin () || sym
->isNspace ())
495 ScopeDsymbol
*scopesym
= sym
->isScopeDsymbol ();
496 if (scopesym
->members
)
498 fields
+= layout_aggregate_members (scopesym
->members
, context
,
508 /* Write out all fields for aggregate BASE. For classes, write out all
509 interfaces first, then the base class fields. */
512 layout_aggregate_type (AggregateDeclaration
*decl
, tree type
,
513 AggregateDeclaration
*base
)
515 ClassDeclaration
*cd
= base
->isClassDeclaration ();
516 bool inherited_p
= (decl
!= base
);
521 layout_aggregate_type (decl
, type
, cd
->baseClass
);
524 /* This is the base class (Object) or interface. */
525 tree objtype
= TREE_TYPE (build_ctype (cd
->type
));
527 /* Add the vtable pointer, and optionally the monitor fields. */
528 InterfaceDeclaration
*id
= cd
->isInterfaceDeclaration ();
529 if (!id
|| id
->vtblInterfaces
->length
== 0)
531 tree field
= create_field_decl (vtbl_ptr_type_node
, "__vptr", 1,
533 DECL_VIRTUAL_P (field
) = 1;
534 TYPE_VFIELD (type
) = field
;
535 DECL_FCONTEXT (field
) = objtype
;
536 insert_aggregate_field (type
, field
, 0);
539 if (!id
&& cd
->hasMonitor ())
541 tree field
= create_field_decl (ptr_type_node
, "__monitor", 1,
543 insert_aggregate_field (type
, field
, target
.ptrsize
);
547 if (cd
->vtblInterfaces
)
549 for (size_t i
= 0; i
< cd
->vtblInterfaces
->length
; i
++)
551 BaseClass
*bc
= (*cd
->vtblInterfaces
)[i
];
552 tree field
= create_field_decl (vtbl_ptr_type_node
, NULL
, 1, 1);
553 insert_aggregate_field (type
, field
, bc
->offset
);
560 size_t fields
= layout_aggregate_members (base
->members
, type
,
562 gcc_assert (fields
== base
->fields
.length
);
564 /* Make sure that all fields have been created. */
567 for (size_t i
= 0; i
< base
->fields
.length
; i
++)
569 VarDeclaration
*var
= base
->fields
[i
];
570 gcc_assert (var
->csym
!= NULL
);
576 /* If the aggregate type TYPE completes the type of any previous field
577 declarations, lay them out now. */
580 finish_incomplete_fields (tree type
)
582 for (tree fwdref
= TYPE_FORWARD_REFERENCES (type
); fwdref
!= NULL_TREE
;
583 fwdref
= TREE_CHAIN (fwdref
))
585 tree field
= TREE_VALUE (fwdref
);
586 tree basetype
= TREE_TYPE (field
);
588 /* Arrays of TYPE have layout_type() called from build_array_type(), but
589 would skip over setting TYPE_SIZE. Try completing the type again. */
590 if (TREE_CODE (basetype
) == ARRAY_TYPE
)
592 while (TREE_CODE (TREE_TYPE (basetype
)) == ARRAY_TYPE
)
593 basetype
= TREE_TYPE (basetype
);
595 layout_type (basetype
);
598 relayout_decl (field
);
601 /* No more forward references to process. */
602 TYPE_FORWARD_REFERENCES (type
) = NULL_TREE
;
605 /* Given a record type TYPE, whose size and alignment are determined by
606 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
607 the finalized record mode. */
610 finish_aggregate_type (unsigned structsize
, unsigned alignsize
, tree type
)
612 /* Set size and alignment as requested by frontend. */
613 TYPE_SIZE (type
) = bitsize_int (structsize
* BITS_PER_UNIT
);
614 TYPE_SIZE_UNIT (type
) = size_int (structsize
);
615 SET_TYPE_ALIGN (type
, alignsize
* BITS_PER_UNIT
);
616 TYPE_PACKED (type
) = (alignsize
== 1);
618 /* Set the back-end type mode. */
619 compute_record_mode (type
);
621 /* Layout all fields now the type is complete. */
622 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
624 /* If the field type is still being constructed because of recursive
625 references, attach it to that class/struct type, so we can go back
626 and complete the field later. */
627 if (!COMPLETE_TYPE_P (TREE_TYPE (field
)))
629 tree basetype
= TREE_TYPE (field
);
630 while (TREE_CODE (basetype
) == ARRAY_TYPE
)
631 basetype
= TREE_TYPE (basetype
);
633 basetype
= TYPE_MAIN_VARIANT (basetype
);
634 if (RECORD_OR_UNION_TYPE_P (basetype
)
635 || TREE_CODE (basetype
) == ENUMERAL_TYPE
)
637 gcc_assert (!COMPLETE_TYPE_P (basetype
));
638 tree fwdrefs
= tree_cons (NULL_TREE
, field
,
639 TYPE_FORWARD_REFERENCES (basetype
));
640 TYPE_FORWARD_REFERENCES (basetype
) = fwdrefs
;
646 layout_decl (field
, 0);
648 /* Give bit-field its proper type after layout_decl. */
649 if (DECL_BIT_FIELD (field
))
651 tree orig_type
= DECL_BIT_FIELD_TYPE (field
);
652 unsigned HOST_WIDE_INT width
= tree_to_uhwi (DECL_SIZE (field
));
654 if (width
!= TYPE_PRECISION (orig_type
))
656 bool unsignedp
= TYPE_UNSIGNED (orig_type
);
659 = d_build_bitfield_integer_type (width
, unsignedp
);
660 SET_DECL_MODE (field
, TYPE_MODE (TREE_TYPE (field
)));
665 /* Fix up all forward-referenced variants of this aggregate type. */
666 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
671 TYPE_FIELDS (t
) = TYPE_FIELDS (type
);
672 TYPE_LANG_SPECIFIC (t
) = TYPE_LANG_SPECIFIC (type
);
673 TYPE_SIZE (t
) = TYPE_SIZE (type
);
674 TYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (type
);
675 TYPE_PACKED (type
) = TYPE_PACKED (type
);
676 SET_TYPE_MODE (t
, TYPE_MODE (type
));
677 SET_TYPE_ALIGN (t
, TYPE_ALIGN (type
));
678 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (type
);
681 /* Finish debugging output for this type. */
682 rest_of_type_compilation (type
, TYPE_FILE_SCOPE_P (type
));
683 finish_incomplete_fields (type
);
685 /* Finish processing of TYPE_DECL. */
686 rest_of_decl_compilation (TYPE_NAME (type
),
687 DECL_FILE_SCOPE_P (TYPE_NAME (type
)), 0);
690 /* Returns true if the class or struct type TYPE has already been layed out by
691 the lowering of another front-end AST type. In which case, there will either
692 be a reuse of the back-end type, or a multiple definition error.
693 DECO is the uniquely mangled decoration for the type. */
696 merge_aggregate_types (Type
*type
, tree deco
)
698 AggregateDeclaration
*sym
;
700 if (type
->ty
== TY::Tstruct
)
701 sym
= type
->isTypeStruct ()->sym
;
702 else if (type
->ty
== TY::Tclass
)
703 sym
= type
->isTypeClass ()->sym
;
707 if (IDENTIFIER_DAGGREGATE (deco
))
709 AggregateDeclaration
*ad
= IDENTIFIER_DAGGREGATE (deco
);
710 /* There should never be a class/struct mismatch in mangled names. */
711 gcc_assert ((sym
->isStructDeclaration () && ad
->isStructDeclaration ())
712 || (sym
->isClassDeclaration () && ad
->isClassDeclaration ()));
714 /* Non-templated variables shouldn't be defined twice. */
715 if (!sym
->isInstantiated ())
716 ScopeDsymbol::multiplyDefined (sym
->loc
, sym
, ad
);
718 type
->ctype
= build_ctype (ad
->type
);
725 /* Implements the visitor interface to build the GCC trees of all
726 Type AST classes emitted from the D Front-end, where CTYPE holds
727 the cached back-end representation to be returned. */
729 class TypeVisitor
: public Visitor
731 using Visitor::visit
;
738 /* This should be overridden by each type class. */
740 void visit (Type
*) final override
745 /* Type assigned to erroneous expressions or constructs that
746 failed during the semantic stage. */
748 void visit (TypeError
*t
) final override
750 t
->ctype
= error_mark_node
;
753 /* Type assigned to generic nullable types. */
755 void visit (TypeNull
*t
) final override
757 t
->ctype
= ptr_type_node
;
760 /* Bottom type used for functions that never return. */
762 void visit (TypeNoreturn
*t
) final override
764 t
->ctype
= noreturn_type_node
;
765 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
768 /* Basic Data Types. */
770 void visit (TypeBasic
*t
) final override
772 /* [type/basic-data-types]
775 bool 8-bit boolean value.
776 byte 8-bit signed value.
777 ubyte 8-bit unsigned value.
778 short 16-bit signed value.
779 ushort 16-bit unsigned value.
780 int 32-bit signed value.
781 uint 32-bit unsigned value.
782 long 64-bit signed value.
783 ulong 64-bit unsigned value.
784 cent 128-bit signed value.
785 ucent 128-bit unsigned value.
786 float 32-bit IEEE 754 floating-point value.
787 double 64-bit IEEE 754 floating-point value.
788 real largest FP size implemented in hardware.
789 ifloat imaginary float.
790 idouble imaginary double.
791 ireal imaginary real.
792 cfloat complex float.
793 cdouble complex double.
795 char UTF-8 code unit.
796 wchar UTF-16 code unit.
797 dchar UTF-32 code unit. */
801 case TY::Tvoid
: t
->ctype
= void_type_node
; break;
802 case TY::Tbool
: t
->ctype
= d_bool_type
; break;
803 case TY::Tint8
: t
->ctype
= d_byte_type
; break;
804 case TY::Tuns8
: t
->ctype
= d_ubyte_type
; break;
805 case TY::Tint16
: t
->ctype
= d_short_type
; break;
806 case TY::Tuns16
: t
->ctype
= d_ushort_type
; break;
807 case TY::Tint32
: t
->ctype
= d_int_type
; break;
808 case TY::Tuns32
: t
->ctype
= d_uint_type
; break;
809 case TY::Tint64
: t
->ctype
= d_long_type
; break;
810 case TY::Tuns64
: t
->ctype
= d_ulong_type
; break;
811 case TY::Tint128
: t
->ctype
= d_cent_type
; break;
812 case TY::Tuns128
: t
->ctype
= d_ucent_type
; break;
813 case TY::Tfloat32
: t
->ctype
= float_type_node
; break;
814 case TY::Tfloat64
: t
->ctype
= double_type_node
; break;
815 case TY::Tfloat80
: t
->ctype
= long_double_type_node
; break;
816 case TY::Timaginary32
: t
->ctype
= ifloat_type_node
; break;
817 case TY::Timaginary64
: t
->ctype
= idouble_type_node
; break;
818 case TY::Timaginary80
: t
->ctype
= ireal_type_node
; break;
819 case TY::Tcomplex32
: t
->ctype
= complex_float_type_node
; break;
820 case TY::Tcomplex64
: t
->ctype
= complex_double_type_node
; break;
821 case TY::Tcomplex80
: t
->ctype
= complex_long_double_type_node
; break;
822 case TY::Tchar
: t
->ctype
= char8_type_node
; break;
823 case TY::Twchar
: t
->ctype
= char16_type_node
; break;
824 case TY::Tdchar
: t
->ctype
= char32_type_node
; break;
825 default: gcc_unreachable ();
828 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
832 /* Derived Data Types. */
834 /* Build a simple pointer to data type, analogous to C pointers. */
836 void visit (TypePointer
*t
) final override
838 t
->ctype
= build_pointer_type (build_ctype (t
->next
));
841 /* Build a dynamic array type, consisting of a length and a pointer
842 to the array data. */
844 void visit (TypeDArray
*t
) final override
846 /* In [abi/arrays], dynamic array layout is:
847 .length array dimension.
848 .ptr pointer to array data. */
849 t
->ctype
= make_struct_type (t
->toChars (), 2,
850 get_identifier ("length"),
851 build_ctype (Type::tsize_t
),
852 get_identifier ("ptr"),
853 build_pointer_type (build_ctype (t
->next
)));
854 TYPE_DYNAMIC_ARRAY (t
->ctype
) = 1;
855 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
859 /* Build a static array type, distinguished from dynamic arrays by
860 having a length fixed at compile-time, analogous to C arrays. */
862 void visit (TypeSArray
*t
) final override
864 if (t
->dim
->isConst () && t
->dim
->type
->isintegral ())
866 uinteger_t size
= t
->dim
->toUInteger ();
867 t
->ctype
= make_array_type (t
->next
, size
);
871 error ("invalid expression for static array dimension: %s",
877 /* Build a vector type, a fixed array of floating or integer types. */
879 void visit (TypeVector
*t
) final override
881 int nunits
= t
->basetype
->isTypeSArray ()->dim
->toUInteger ();
882 tree inner
= build_ctype (t
->elementType ());
884 /* Same rationale as void static arrays. */
885 if (inner
== void_type_node
)
886 inner
= build_ctype (Type::tuns8
);
888 t
->ctype
= build_vector_type (inner
, nunits
);
889 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
890 layout_type (t
->ctype
);
893 /* Build an associative array type, distinguished from arrays by having an
894 index that's not necessarily an integer, and can be sparsely populated. */
896 void visit (TypeAArray
*t
) final override
898 /* In [abi/associative-arrays], associative arrays are a struct that only
899 consist of a pointer to an opaque, implementation defined type. */
900 t
->ctype
= make_struct_type (t
->toChars (), 1,
901 get_identifier ("ptr"), ptr_type_node
);
902 TYPE_ASSOCIATIVE_ARRAY (t
->ctype
) = 1;
903 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
907 /* Build type for a function declaration, which consists of a return type,
908 and a list of parameter types, and a linkage attribute. */
910 void visit (TypeFunction
*t
) final override
912 tree fnparams
= NULL_TREE
;
915 /* [function/variadic]
917 Variadic functions with D linkage have an additional hidden argument
918 with the name _arguments passed to the function. */
919 if (t
->isDstyleVariadic ())
921 tree type
= build_ctype (Type::typeinfotypelist
->type
);
922 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
925 const size_t n_args
= t
->parameterList
.length ();
927 for (size_t i
= 0; i
< n_args
; i
++)
929 tree type
= parameter_type (t
->parameterList
[i
]);
931 /* Type `noreturn` is a terminator, as no other arguments can possibly
932 be evaluated after it. */
933 if (type
== noreturn_type_node
)
936 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
939 /* When the last parameter is void_list_node, that indicates a fixed length
940 parameter list, otherwise function is treated as variadic. */
941 if (t
->parameterList
.varargs
!= VARARGvariadic
)
942 fnparams
= chainon (fnparams
, void_list_node
);
946 fntype
= build_ctype (t
->next
);
948 fntype
= build_reference_type (fntype
);
951 fntype
= void_type_node
;
953 /* Could the function type be self referenced by parameters? */
954 t
->ctype
= build_function_type (fntype
, fnparams
);
955 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
958 /* Qualify function types that have the type `noreturn` as volatile. */
959 if (fntype
== noreturn_type_node
)
960 t
->ctype
= build_qualified_type (t
->ctype
, TYPE_QUAL_VOLATILE
);
962 /* Handle any special support for calling conventions. */
967 /* [attribute/linkage]
969 The Windows convention is distinct from the C convention only
970 on Win32, where it is equivalent to the stdcall convention. */
971 unsigned link_system
, link_windows
;
972 if (targetdm
.d_has_stdcall_convention (&link_system
, &link_windows
))
975 t
->ctype
= insert_type_attribute (t
->ctype
, "stdcall");
984 /* [abi/function-calling-conventions]
986 The extern (C) and extern (D) calling convention matches
987 the C calling convention used by the supported C compiler
988 on the host system. */
996 /* Build a delegate type, an aggregate of two pieces of data, an object
997 reference and a pointer to a non-static member function, or a pointer
998 to a closure and a pointer to a nested function. */
1000 void visit (TypeDelegate
*t
) final override
1002 /* In [abi/delegates], delegate layout is:
1003 .ptr context pointer.
1004 .funcptr pointer to function. */
1005 tree fntype
= build_ctype (t
->next
);
1006 tree dgtype
= build_vthis_function (void_type_node
, fntype
);
1008 TYPE_ATTRIBUTES (dgtype
) = TYPE_ATTRIBUTES (fntype
);
1009 TYPE_LANG_SPECIFIC (dgtype
) = TYPE_LANG_SPECIFIC (fntype
);
1011 t
->ctype
= make_struct_type (t
->toChars (), 2,
1012 get_identifier ("ptr"),
1013 build_ctype (Type::tvoidptr
),
1014 get_identifier ("funcptr"),
1015 build_pointer_type (dgtype
));
1016 TYPE_DELEGATE (t
->ctype
) = 1;
1017 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
1022 /* User Defined Types. */
1024 /* Build a named enum type, a distinct value whose values are restrict to
1025 a group of constants of the same underlying base type. */
1027 void visit (TypeEnum
*t
) final override
1029 tree basetype
= (t
->sym
->memtype
)
1030 ? build_ctype (t
->sym
->memtype
) : void_type_node
;
1032 if (t
->sym
->isSpecial ())
1034 /* Special enums are opaque types that bind to C types. */
1035 const char *ident
= t
->toChars ();
1036 Type
*underlying
= NULL
;
1038 /* Skip over the prefixing `__c_'. */
1039 gcc_assert (startswith (ident
, "__c_"));
1040 ident
= ident
+ strlen ("__c_");
1042 /* To keep things compatible within the code generation we stick to
1043 mapping to equivalent D types. However it should be OK to use the
1044 GCC provided C types here as the front-end enforces that everything
1045 must be explicitly cast from a D type to any of the opaque types. */
1046 if (strcmp (ident
, "long") == 0)
1047 underlying
= build_frontend_type (long_integer_type_node
);
1048 else if (strcmp (ident
, "ulong") == 0)
1049 underlying
= build_frontend_type (long_unsigned_type_node
);
1050 else if (strcmp (ident
, "wchar_t") == 0)
1052 build_frontend_type (make_unsigned_type (WCHAR_TYPE_SIZE
));
1053 else if (strcmp (ident
, "longlong") == 0)
1054 underlying
= build_frontend_type (long_long_integer_type_node
);
1055 else if (strcmp (ident
, "ulonglong") == 0)
1056 underlying
= build_frontend_type (long_long_unsigned_type_node
);
1057 else if (strcmp (ident
, "long_double") == 0)
1058 underlying
= build_frontend_type (long_double_type_node
);
1059 else if (strcmp (ident
, "complex_real") == 0)
1060 underlying
= build_frontend_type (complex_long_double_type_node
);
1061 else if (strcmp (ident
, "complex_float") == 0)
1062 underlying
= build_frontend_type (complex_float_type_node
);
1063 else if (strcmp (ident
, "complex_double") == 0)
1064 underlying
= build_frontend_type (complex_double_type_node
);
1066 /* Conversion failed or there's an unhandled special type. */
1067 gcc_assert (underlying
!= NULL
);
1069 t
->ctype
= build_variant_type_copy (build_ctype (underlying
));
1070 build_type_decl (t
->ctype
, t
->sym
);
1072 else if (t
->sym
->ident
== NULL
1073 || !INTEGRAL_TYPE_P (basetype
)
1074 || TREE_CODE (basetype
) == BOOLEAN_TYPE
)
1076 /* Enums in D2 can either be anonymous, or have a base type that is not
1077 necessarily integral. For these, we simplify this a little by using
1078 the base type directly instead of building an ENUMERAL_TYPE. */
1079 t
->ctype
= build_variant_type_copy (basetype
);
1081 if (t
->sym
->ident
!= NULL
)
1082 build_type_decl (t
->ctype
, t
->sym
);
1086 t
->ctype
= make_node (ENUMERAL_TYPE
);
1087 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
1090 ENUM_IS_SCOPED (t
->ctype
) = 1;
1091 TREE_TYPE (t
->ctype
) = basetype
;
1093 if (flag_short_enums
)
1094 TYPE_PACKED (t
->ctype
) = 1;
1096 tree values
= NULL_TREE
;
1097 if (t
->sym
->members
)
1099 for (size_t i
= 0; i
< t
->sym
->members
->length
; i
++)
1101 EnumMember
*member
= (*t
->sym
->members
)[i
]->isEnumMember ();
1102 /* Templated functions can seep through to the back-end
1103 just ignore for now. */
1107 tree ident
= get_identifier (member
->ident
->toChars ());
1108 tree value
= build_integer_cst (member
->value ()->toInteger (),
1111 /* Build an identifier for the enumeration constant. */
1112 tree decl
= build_decl (make_location_t (member
->loc
),
1113 CONST_DECL
, ident
, basetype
);
1114 DECL_CONTEXT (decl
) = t
->ctype
;
1115 TREE_CONSTANT (decl
) = 1;
1116 TREE_READONLY (decl
) = 1;
1117 DECL_INITIAL (decl
) = value
;
1119 /* Add this enumeration constant to the list for this type. */
1120 values
= chainon (values
, build_tree_list (ident
, decl
));
1124 TYPE_VALUES (t
->ctype
) = values
;
1125 build_type_decl (t
->ctype
, t
->sym
);
1128 apply_user_attributes (t
->sym
, t
->ctype
);
1130 /* Finish the enumeration type. */
1131 if (TREE_CODE (t
->ctype
) == ENUMERAL_TYPE
)
1133 TYPE_MIN_VALUE (t
->ctype
) = TYPE_MIN_VALUE (basetype
);
1134 TYPE_MAX_VALUE (t
->ctype
) = TYPE_MAX_VALUE (basetype
);
1135 TYPE_UNSIGNED (t
->ctype
) = TYPE_UNSIGNED (basetype
);
1136 SET_TYPE_ALIGN (t
->ctype
, TYPE_ALIGN (basetype
));
1137 TYPE_SIZE (t
->ctype
) = NULL_TREE
;
1138 TYPE_PRECISION (t
->ctype
) = t
->size (t
->sym
->loc
) * 8;
1140 layout_type (t
->ctype
);
1142 /* Finish debugging output for this type. */
1143 rest_of_type_compilation (t
->ctype
, TYPE_FILE_SCOPE_P (t
->ctype
));
1144 finish_incomplete_fields (t
->ctype
);
1146 /* Finish processing of TYPE_DECL. */
1147 rest_of_decl_compilation (TYPE_NAME (t
->ctype
),
1148 DECL_FILE_SCOPE_P (TYPE_NAME (t
->ctype
)), 0);
1152 /* Build a struct or union type. Layout should be exactly represented
1153 as an equivalent C struct, except for non-POD or nested structs. */
1155 void visit (TypeStruct
*t
) final override
1157 /* Merge types in the back-end if the front-end did not itself do so. */
1158 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
1159 if (merge_aggregate_types (t
, deco
))
1162 /* Need to set this right away in case of self-references. */
1163 t
->ctype
= make_node (t
->sym
->isUnionDeclaration ()
1164 ? UNION_TYPE
: RECORD_TYPE
);
1166 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
1168 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
1169 TYPE_CXX_ODR_P (t
->ctype
) = 1;
1171 if (t
->sym
->members
)
1173 /* Must set up the overall size and alignment before determining
1174 the context or laying out fields as those types may make
1175 recursive references to this type. */
1176 unsigned structsize
= t
->sym
->structsize
;
1177 unsigned alignsize
= t
->sym
->alignment
.isDefault ()
1178 ? t
->sym
->alignsize
: t
->sym
->alignment
.get ();
1180 /* Put out all fields. */
1181 layout_aggregate_type (t
->sym
, t
->ctype
, t
->sym
);
1182 build_type_decl (t
->ctype
, t
->sym
);
1183 set_visibility_for_decl (t
->ctype
, t
->sym
);
1184 apply_user_attributes (t
->sym
, t
->ctype
);
1185 finish_aggregate_type (structsize
, alignsize
, t
->ctype
);
1188 /* For structs with a user defined postblit, copy constructor, or a
1189 destructor, also set TREE_ADDRESSABLE on the type and all variants.
1190 This will make the struct be passed around by reference. */
1191 if (!t
->sym
->isPOD ())
1193 for (tree tv
= t
->ctype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
1195 TREE_ADDRESSABLE (tv
) = 1;
1196 SET_TYPE_MODE (tv
, BLKmode
);
1201 /* Build a class type. Whereas structs are value types, classes are
1202 reference types, with all the object-orientated features. */
1204 void visit (TypeClass
*t
) final override
1206 /* Merge types in the back-end if the front-end did not itself do so. */
1207 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
1208 if (merge_aggregate_types (t
, deco
))
1211 /* Need to set ctype right away in case of self-references to
1212 the type during this call. */
1213 tree basetype
= make_node (RECORD_TYPE
);
1214 t
->ctype
= build_pointer_type (basetype
);
1216 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
1218 /* Note that lang_specific data is assigned to both the reference
1219 and the underlying record type. */
1220 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
1221 TYPE_LANG_SPECIFIC (basetype
) = TYPE_LANG_SPECIFIC (t
->ctype
);
1222 CLASS_TYPE_P (basetype
) = 1;
1223 TYPE_CXX_ODR_P (basetype
) = 1;
1225 /* Put out all fields, including from each base class. */
1226 layout_aggregate_type (t
->sym
, basetype
, t
->sym
);
1227 build_type_decl (basetype
, t
->sym
);
1228 set_visibility_for_decl (basetype
, t
->sym
);
1229 apply_user_attributes (t
->sym
, basetype
);
1230 finish_aggregate_type (t
->sym
->structsize
, t
->sym
->alignsize
, basetype
);
1232 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
1233 for (tree tv
= basetype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
1235 TREE_ADDRESSABLE (tv
) = 1;
1236 SET_TYPE_MODE (tv
, BLKmode
);
1239 /* Type is final, there are no derivations. */
1240 if (t
->sym
->storage_class
& STCfinal
)
1241 TYPE_FINAL_P (basetype
) = 1;
1243 /* Create BINFO even if debugging is off. This is needed to keep
1244 references to inherited types. */
1245 if (!t
->sym
->isInterfaceDeclaration ())
1246 TYPE_BINFO (basetype
) = build_class_binfo (NULL_TREE
, t
->sym
);
1249 unsigned offset
= 0;
1251 TYPE_BINFO (basetype
) = build_interface_binfo (NULL_TREE
, t
->sym
,
1255 /* Associate all virtual methods with the class too. */
1256 for (size_t i
= 0; i
< t
->sym
->vtbl
.length
; i
++)
1258 FuncDeclaration
*fd
= t
->sym
->vtbl
[i
]->isFuncDeclaration ();
1259 tree method
= fd
? get_symbol_decl (fd
) : error_mark_node
;
1261 if (!error_operand_p (method
)
1262 && DECL_CONTEXT (method
) == basetype
1263 && !chain_member (method
, TYPE_FIELDS (basetype
)))
1264 TYPE_FIELDS (basetype
) = chainon (TYPE_FIELDS (basetype
), method
);
1270 /* Build a tree from a frontend Type. */
1273 build_ctype (Type
*t
)
1279 /* Strip const modifiers from type before building. This is done
1280 to ensure that back-end treats e.g: const (T) as a variant of T,
1281 and not as two distinct types. */
1286 Type
*tb
= t
->castMod (0);
1289 t
->ctype
= insert_type_modifiers (tb
->ctype
, t
->mod
);