1 /* types.cc -- Lower D frontend types to GCC trees.
2 Copyright (C) 2006-2021 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
== 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
== 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 layout_decl (field
, 0);
274 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), field
);
277 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET.
278 This is done as the frontend puts fields into the outer struct, and so
279 their offset is from the beginning of the aggregate.
280 We want the offset to be from the beginning of the anonymous aggregate. */
283 fixup_anonymous_offset (tree fields
, tree offset
)
285 /* No adjustment in field offset required. */
286 if (integer_zerop (offset
))
289 while (fields
!= NULL_TREE
)
291 /* Traverse all nested anonymous aggregates to update the offset of their
292 fields. Note that the anonymous field itself is not adjusted, as it
293 already has an offset relative to its outer aggregate. */
294 tree ftype
= TREE_TYPE (fields
);
295 if (TYPE_NAME (ftype
) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype
)))
297 tree vfields
= TYPE_FIELDS (ftype
);
298 fixup_anonymous_offset (vfields
, offset
);
302 tree voffset
= DECL_FIELD_OFFSET (fields
);
303 DECL_FIELD_OFFSET (fields
) = size_binop (MINUS_EXPR
, voffset
, offset
);
306 fields
= DECL_CHAIN (fields
);
310 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT.
311 If INHERITED_P is true, then the members derive from a base class.
312 Returns the number of fields found. */
315 layout_aggregate_members (Dsymbols
*members
, tree context
, bool inherited_p
)
319 for (size_t i
= 0; i
< members
->length
; i
++)
321 Dsymbol
*sym
= (*members
)[i
];
322 VarDeclaration
*var
= sym
->isVarDeclaration ();
325 /* Skip fields that have already been added. */
326 if (!inherited_p
&& var
->csym
!= NULL
)
329 /* If this variable was really a tuple, add all tuple fields. */
332 TupleDeclaration
*td
= var
->aliassym
->isTupleDeclaration ();
334 /* No other way to coerce the underlying type out of the tuple.
335 Frontend should have already validated this. */
336 for (size_t j
= 0; j
< td
->objects
->length
; j
++)
338 RootObject
*ro
= (*td
->objects
)[j
];
339 gcc_assert (ro
->dyncast () == DYNCAST_EXPRESSION
);
340 Expression
*e
= (Expression
*) ro
;
341 gcc_assert (e
->op
== TOKdsymbol
);
342 DsymbolExp
*se
= e
->isDsymbolExp ();
344 tmembers
.push (se
->s
);
347 fields
+= layout_aggregate_members (&tmembers
, context
,
352 /* Insert the field declaration at its given offset. */
355 const char *ident
= var
->ident
? var
->ident
->toChars () : NULL
;
356 tree field
= create_field_decl (declaration_type (var
), ident
,
357 inherited_p
, inherited_p
);
358 apply_user_attributes (var
, field
);
359 insert_aggregate_field (context
, field
, var
->offset
);
361 /* Because the front-end shares field decls across classes, don't
362 create the corresponding back-end symbol unless we are adding
363 it to the aggregate it is defined in. */
366 DECL_LANG_SPECIFIC (field
) = build_lang_decl (var
);
375 /* Anonymous struct/union are flattened by the frontend. However, we
376 want to keep the record layout in-tact when building the type. */
377 AnonDeclaration
*ad
= sym
->isAnonDeclaration ();
380 tree ident
= make_anon_name ();
381 tree type
= make_node (ad
->isunion
? UNION_TYPE
: RECORD_TYPE
);
382 ANON_AGGR_TYPE_P (type
) = 1;
385 /* Build the type declaration. */
386 tree decl
= build_decl (make_location_t (ad
->loc
),
387 TYPE_DECL
, ident
, type
);
388 DECL_CONTEXT (decl
) = context
;
389 DECL_ARTIFICIAL (decl
) = 1;
391 TYPE_CONTEXT (type
) = context
;
392 TYPE_NAME (type
) = decl
;
393 TYPE_STUB_DECL (type
) = decl
;
395 /* Recursively iterator over the anonymous members. */
396 fields
+= layout_aggregate_members (ad
->decl
, type
, inherited_p
);
398 /* Remove from the anon fields the base offset of this anonymous
399 aggregate. Undoes what is set-up in setFieldOffset, but doesn't
400 affect field accesses. */
401 tree offset
= size_int (ad
->anonoffset
);
402 fixup_anonymous_offset (TYPE_FIELDS (type
), offset
);
404 finish_aggregate_type (ad
->anonstructsize
, ad
->anonalignsize
, type
);
406 /* And make the corresponding data member. */
407 tree field
= create_field_decl (type
, NULL
, 0, 0);
408 apply_user_attributes (ad
, field
);
409 insert_aggregate_field (context
, field
, ad
->anonoffset
);
413 /* Other kinds of attributes don't create a scope. */
414 AttribDeclaration
*attrib
= sym
->isAttribDeclaration ();
417 Dsymbols
*decls
= attrib
->include (NULL
);
420 fields
+= layout_aggregate_members (decls
, context
, inherited_p
);
425 /* Same with template mixins and namespaces. */
426 if (sym
->isTemplateMixin () || sym
->isNspace ())
428 ScopeDsymbol
*scopesym
= sym
->isScopeDsymbol ();
429 if (scopesym
->members
)
431 fields
+= layout_aggregate_members (scopesym
->members
, context
,
441 /* Write out all fields for aggregate BASE. For classes, write out all
442 interfaces first, then the base class fields. */
445 layout_aggregate_type (AggregateDeclaration
*decl
, tree type
,
446 AggregateDeclaration
*base
)
448 ClassDeclaration
*cd
= base
->isClassDeclaration ();
449 bool inherited_p
= (decl
!= base
);
454 layout_aggregate_type (decl
, type
, cd
->baseClass
);
457 /* This is the base class (Object) or interface. */
458 tree objtype
= TREE_TYPE (build_ctype (cd
->type
));
460 /* Add the vtable pointer, and optionally the monitor fields. */
461 InterfaceDeclaration
*id
= cd
->isInterfaceDeclaration ();
462 if (!id
|| id
->vtblInterfaces
->length
== 0)
464 tree field
= create_field_decl (vtbl_ptr_type_node
, "__vptr", 1,
466 DECL_VIRTUAL_P (field
) = 1;
467 TYPE_VFIELD (type
) = field
;
468 DECL_FCONTEXT (field
) = objtype
;
469 insert_aggregate_field (type
, field
, 0);
472 if (!id
&& cd
->hasMonitor ())
474 tree field
= create_field_decl (ptr_type_node
, "__monitor", 1,
476 insert_aggregate_field (type
, field
, target
.ptrsize
);
480 if (cd
->vtblInterfaces
)
482 for (size_t i
= 0; i
< cd
->vtblInterfaces
->length
; i
++)
484 BaseClass
*bc
= (*cd
->vtblInterfaces
)[i
];
485 tree field
= create_field_decl (vtbl_ptr_type_node
, NULL
, 1, 1);
486 insert_aggregate_field (type
, field
, bc
->offset
);
493 size_t fields
= layout_aggregate_members (base
->members
, type
,
495 gcc_assert (fields
== base
->fields
.length
);
497 /* Make sure that all fields have been created. */
500 for (size_t i
= 0; i
< base
->fields
.length
; i
++)
502 VarDeclaration
*var
= base
->fields
[i
];
503 gcc_assert (var
->csym
!= NULL
);
509 /* Given a record type TYPE, whose size and alignment are determined by
510 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
511 the finalized record mode. */
514 finish_aggregate_type (unsigned structsize
, unsigned alignsize
, tree type
)
516 /* Set size and alignment as requested by frontend. */
517 TYPE_SIZE (type
) = bitsize_int (structsize
* BITS_PER_UNIT
);
518 TYPE_SIZE_UNIT (type
) = size_int (structsize
);
519 SET_TYPE_ALIGN (type
, alignsize
* BITS_PER_UNIT
);
520 TYPE_PACKED (type
) = (alignsize
== 1);
522 /* Set the back-end type mode. */
523 compute_record_mode (type
);
525 /* Fix up all variants of this aggregate type. */
526 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
531 TYPE_FIELDS (t
) = TYPE_FIELDS (type
);
532 TYPE_LANG_SPECIFIC (t
) = TYPE_LANG_SPECIFIC (type
);
533 SET_TYPE_ALIGN (t
, TYPE_ALIGN (type
));
534 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (type
);
535 gcc_assert (TYPE_MODE (t
) == TYPE_MODE (type
));
539 /* Returns true if the class or struct type TYPE has already been layed out by
540 the lowering of another front-end AST type. In which case, there will either
541 be a reuse of the back-end type, or a multiple definition error.
542 DECO is the uniquely mangled decoration for the type. */
545 merge_aggregate_types (Type
*type
, tree deco
)
547 AggregateDeclaration
*sym
;
549 if (type
->ty
== Tstruct
)
550 sym
= type
->isTypeStruct ()->sym
;
551 else if (type
->ty
== Tclass
)
552 sym
= type
->isTypeClass ()->sym
;
556 if (IDENTIFIER_DAGGREGATE (deco
))
558 AggregateDeclaration
*ad
= IDENTIFIER_DAGGREGATE (deco
);
559 /* There should never be a class/struct mismatch in mangled names. */
560 gcc_assert ((sym
->isStructDeclaration () && ad
->isStructDeclaration ())
561 || (sym
->isClassDeclaration () && ad
->isClassDeclaration ()));
563 /* Non-templated variables shouldn't be defined twice. */
564 if (!sym
->isInstantiated ())
565 ScopeDsymbol::multiplyDefined (sym
->loc
, sym
, ad
);
567 type
->ctype
= build_ctype (ad
->type
);
574 /* Implements the visitor interface to build the GCC trees of all
575 Type AST classes emitted from the D Front-end, where CTYPE holds
576 the cached back-end representation to be returned. */
578 class TypeVisitor
: public Visitor
580 using Visitor::visit
;
587 /* This should be overridden by each type class. */
594 /* Type assigned to erroneous expressions or constructs that
595 failed during the semantic stage. */
597 void visit (TypeError
*t
)
599 t
->ctype
= error_mark_node
;
602 /* Type assigned to generic nullable types. */
604 void visit (TypeNull
*t
)
606 t
->ctype
= ptr_type_node
;
609 /* Bottom type used for functions that never return. */
611 void visit (TypeNoreturn
*t
)
613 t
->ctype
= void_type_node
;
616 /* Basic Data Types. */
618 void visit (TypeBasic
*t
)
620 /* [type/basic-data-types]
623 bool 8-bit boolean value.
624 byte 8-bit signed value.
625 ubyte 8-bit unsigned value.
626 short 16-bit signed value.
627 ushort 16-bit unsigned value.
628 int 32-bit signed value.
629 uint 32-bit unsigned value.
630 long 64-bit signed value.
631 ulong 64-bit unsigned value.
632 cent 128-bit signed value.
633 ucent 128-bit unsigned value.
634 float 32-bit IEEE 754 floating-point value.
635 double 64-bit IEEE 754 floating-point value.
636 real largest FP size implemented in hardware.
637 ifloat imaginary float.
638 idouble imaginary double.
639 ireal imaginary real.
640 cfloat complex float.
641 cdouble complex double.
643 char UTF-8 code unit.
644 wchar UTF-16 code unit.
645 dchar UTF-32 code unit. */
649 case Tvoid
: t
->ctype
= void_type_node
; break;
650 case Tbool
: t
->ctype
= d_bool_type
; break;
651 case Tint8
: t
->ctype
= d_byte_type
; break;
652 case Tuns8
: t
->ctype
= d_ubyte_type
; break;
653 case Tint16
: t
->ctype
= d_short_type
; break;
654 case Tuns16
: t
->ctype
= d_ushort_type
; break;
655 case Tint32
: t
->ctype
= d_int_type
; break;
656 case Tuns32
: t
->ctype
= d_uint_type
; break;
657 case Tint64
: t
->ctype
= d_long_type
; break;
658 case Tuns64
: t
->ctype
= d_ulong_type
; break;
659 case Tint128
: t
->ctype
= d_cent_type
; break;
660 case Tuns128
: t
->ctype
= d_ucent_type
; break;
661 case Tfloat32
: t
->ctype
= float_type_node
; break;
662 case Tfloat64
: t
->ctype
= double_type_node
; break;
663 case Tfloat80
: t
->ctype
= long_double_type_node
; break;
664 case Timaginary32
: t
->ctype
= ifloat_type_node
; break;
665 case Timaginary64
: t
->ctype
= idouble_type_node
; break;
666 case Timaginary80
: t
->ctype
= ireal_type_node
; break;
667 case Tcomplex32
: t
->ctype
= complex_float_type_node
; break;
668 case Tcomplex64
: t
->ctype
= complex_double_type_node
; break;
669 case Tcomplex80
: t
->ctype
= complex_long_double_type_node
; break;
670 case Tchar
: t
->ctype
= char8_type_node
; break;
671 case Twchar
: t
->ctype
= char16_type_node
; break;
672 case Tdchar
: t
->ctype
= char32_type_node
; break;
673 default: gcc_unreachable ();
676 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
680 /* Derived Data Types. */
682 /* Build a simple pointer to data type, analogous to C pointers. */
684 void visit (TypePointer
*t
)
686 t
->ctype
= build_pointer_type (build_ctype (t
->next
));
689 /* Build a dynamic array type, consisting of a length and a pointer
690 to the array data. */
692 void visit (TypeDArray
*t
)
694 /* In [abi/arrays], dynamic array layout is:
695 .length array dimension.
696 .ptr pointer to array data. */
697 t
->ctype
= make_struct_type (t
->toChars (), 2,
698 get_identifier ("length"),
699 build_ctype (Type::tsize_t
),
700 get_identifier ("ptr"),
701 build_pointer_type (build_ctype (t
->next
)));
702 TYPE_DYNAMIC_ARRAY (t
->ctype
) = 1;
703 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
707 /* Build a static array type, distinguished from dynamic arrays by
708 having a length fixed at compile-time, analogous to C arrays. */
710 void visit (TypeSArray
*t
)
712 if (t
->dim
->isConst () && t
->dim
->type
->isintegral ())
714 uinteger_t size
= t
->dim
->toUInteger ();
715 t
->ctype
= make_array_type (t
->next
, size
);
719 error ("invalid expression for static array dimension: %s",
725 /* Build a vector type, a fixed array of floating or integer types. */
727 void visit (TypeVector
*t
)
729 int nunits
= t
->basetype
->isTypeSArray ()->dim
->toUInteger ();
730 tree inner
= build_ctype (t
->elementType ());
732 /* Same rationale as void static arrays. */
733 if (inner
== void_type_node
)
734 inner
= build_ctype (Type::tuns8
);
736 t
->ctype
= build_vector_type (inner
, nunits
);
737 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
738 layout_type (t
->ctype
);
741 /* Build an associative array type, distinguished from arrays by having an
742 index that's not necessarily an integer, and can be sparsely populated. */
744 void visit (TypeAArray
*t
)
746 /* In [abi/associative-arrays], associative arrays are a struct that only
747 consist of a pointer to an opaque, implementation defined type. */
748 t
->ctype
= make_struct_type (t
->toChars (), 1,
749 get_identifier ("ptr"), ptr_type_node
);
750 TYPE_ASSOCIATIVE_ARRAY (t
->ctype
) = 1;
751 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
755 /* Build type for a function declaration, which consists of a return type,
756 and a list of parameter types, and a linkage attribute. */
758 void visit (TypeFunction
*t
)
760 tree fnparams
= NULL_TREE
;
763 /* [function/variadic]
765 Variadic functions with D linkage have an additional hidden argument
766 with the name _arguments passed to the function. */
767 if (t
->isDstyleVariadic ())
769 tree type
= build_ctype (Type::typeinfotypelist
->type
);
770 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
773 size_t n_args
= t
->parameterList
.length ();
775 for (size_t i
= 0; i
< n_args
; i
++)
777 tree type
= parameter_type (t
->parameterList
[i
]);
778 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
781 /* When the last parameter is void_list_node, that indicates a fixed length
782 parameter list, otherwise function is treated as variadic. */
783 if (t
->parameterList
.varargs
!= VARARGvariadic
)
784 fnparams
= chainon (fnparams
, void_list_node
);
788 fntype
= build_ctype (t
->next
);
790 fntype
= build_reference_type (fntype
);
793 fntype
= void_type_node
;
795 /* Could the function type be self referenced by parameters? */
796 t
->ctype
= build_function_type (fntype
, fnparams
);
797 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
800 /* Handle any special support for calling conventions. */
805 /* [attribute/linkage]
807 The Windows convention is distinct from the C convention only
808 on Win32, where it is equivalent to the stdcall convention. */
809 unsigned link_system
, link_windows
;
810 if (targetdm
.d_has_stdcall_convention (&link_system
, &link_windows
))
813 t
->ctype
= insert_type_attribute (t
->ctype
, "stdcall");
822 /* [abi/function-calling-conventions]
824 The extern (C) and extern (D) calling convention matches
825 the C calling convention used by the supported C compiler
826 on the host system. */
834 /* Build a delegate type, an aggregate of two pieces of data, an object
835 reference and a pointer to a non-static member function, or a pointer
836 to a closure and a pointer to a nested function. */
838 void visit (TypeDelegate
*t
)
840 /* In [abi/delegates], delegate layout is:
841 .ptr context pointer.
842 .funcptr pointer to function. */
843 tree fntype
= build_ctype (t
->next
);
844 tree dgtype
= build_vthis_function (void_type_node
, fntype
);
846 TYPE_ATTRIBUTES (dgtype
) = TYPE_ATTRIBUTES (fntype
);
847 TYPE_LANG_SPECIFIC (dgtype
) = TYPE_LANG_SPECIFIC (fntype
);
849 t
->ctype
= make_struct_type (t
->toChars (), 2,
850 get_identifier ("ptr"),
851 build_ctype (Type::tvoidptr
),
852 get_identifier ("funcptr"),
853 build_pointer_type (dgtype
));
854 TYPE_DELEGATE (t
->ctype
) = 1;
855 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
860 /* User Defined Types. */
862 /* Build a named enum type, a distinct value whose values are restrict to
863 a group of constants of the same underlying base type. */
865 void visit (TypeEnum
*t
)
867 tree basetype
= (t
->sym
->memtype
)
868 ? build_ctype (t
->sym
->memtype
) : void_type_node
;
870 if (t
->sym
->isSpecial ())
872 /* Special enums are opaque types that bind to C types. */
873 const char *ident
= t
->toChars ();
874 Type
*underlying
= NULL
;
876 /* Skip over the prefixing `__c_'. */
877 gcc_assert (startswith (ident
, "__c_"));
878 ident
= ident
+ strlen ("__c_");
880 /* To keep things compatible within the code generation we stick to
881 mapping to equivalent D types. However it should be OK to use the
882 GCC provided C types here as the front-end enforces that everything
883 must be explicitly cast from a D type to any of the opaque types. */
884 if (strcmp (ident
, "long") == 0)
885 underlying
= build_frontend_type (long_integer_type_node
);
886 else if (strcmp (ident
, "ulong") == 0)
887 underlying
= build_frontend_type (long_unsigned_type_node
);
888 else if (strcmp (ident
, "wchar_t") == 0)
889 underlying
= target
.c
.twchar_t
;
890 else if (strcmp (ident
, "longlong") == 0)
891 underlying
= build_frontend_type (long_long_integer_type_node
);
892 else if (strcmp (ident
, "ulonglong") == 0)
893 underlying
= build_frontend_type (long_long_unsigned_type_node
);
894 else if (strcmp (ident
, "long_double") == 0)
895 underlying
= build_frontend_type (long_double_type_node
);
896 else if (strcmp (ident
, "complex_real") == 0)
897 underlying
= build_frontend_type (complex_long_double_type_node
);
898 else if (strcmp (ident
, "complex_float") == 0)
899 underlying
= build_frontend_type (complex_float_type_node
);
900 else if (strcmp (ident
, "complex_double") == 0)
901 underlying
= build_frontend_type (complex_double_type_node
);
903 /* Conversion failed or there's an unhandled special type. */
904 gcc_assert (underlying
!= NULL
);
906 t
->ctype
= build_variant_type_copy (build_ctype (underlying
));
907 build_type_decl (t
->ctype
, t
->sym
);
909 else if (!INTEGRAL_TYPE_P (basetype
) || TREE_CODE (basetype
) == BOOLEAN_TYPE
)
911 /* Enums in D2 can have a base type that is not necessarily integral.
912 For these, we simplify this a little by using the base type directly
913 instead of building an ENUMERAL_TYPE. */
914 t
->ctype
= build_variant_type_copy (basetype
);
915 build_type_decl (t
->ctype
, t
->sym
);
919 t
->ctype
= make_node (ENUMERAL_TYPE
);
920 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
923 ENUM_IS_SCOPED (t
->ctype
) = 1;
924 TREE_TYPE (t
->ctype
) = basetype
;
926 if (flag_short_enums
)
927 TYPE_PACKED (t
->ctype
) = 1;
929 TYPE_PRECISION (t
->ctype
) = t
->size (t
->sym
->loc
) * 8;
930 TYPE_SIZE (t
->ctype
) = 0;
932 TYPE_MIN_VALUE (t
->ctype
) = TYPE_MIN_VALUE (basetype
);
933 TYPE_MAX_VALUE (t
->ctype
) = TYPE_MAX_VALUE (basetype
);
934 layout_type (t
->ctype
);
936 tree values
= NULL_TREE
;
939 for (size_t i
= 0; i
< t
->sym
->members
->length
; i
++)
941 EnumMember
*member
= (*t
->sym
->members
)[i
]->isEnumMember ();
942 /* Templated functions can seep through to the back-end
943 just ignore for now. */
947 tree ident
= get_identifier (member
->ident
->toChars ());
948 tree value
= build_integer_cst (member
->value ()->toInteger (),
951 /* Build an identifier for the enumeration constant. */
952 tree decl
= build_decl (make_location_t (member
->loc
),
953 CONST_DECL
, ident
, basetype
);
954 DECL_CONTEXT (decl
) = t
->ctype
;
955 TREE_CONSTANT (decl
) = 1;
956 TREE_READONLY (decl
) = 1;
957 DECL_INITIAL (decl
) = value
;
959 /* Add this enumeration constant to the list for this type. */
960 values
= chainon (values
, build_tree_list (ident
, decl
));
964 TYPE_VALUES (t
->ctype
) = values
;
965 TYPE_UNSIGNED (t
->ctype
) = TYPE_UNSIGNED (basetype
);
966 build_type_decl (t
->ctype
, t
->sym
);
969 apply_user_attributes (t
->sym
, t
->ctype
);
972 /* Build a struct or union type. Layout should be exactly represented
973 as an equivalent C struct, except for non-POD or nested structs. */
975 void visit (TypeStruct
*t
)
977 /* Merge types in the back-end if the front-end did not itself do so. */
978 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
979 if (merge_aggregate_types (t
, deco
))
982 /* Need to set this right away in case of self-references. */
983 t
->ctype
= make_node (t
->sym
->isUnionDeclaration ()
984 ? UNION_TYPE
: RECORD_TYPE
);
986 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
988 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
989 TYPE_CXX_ODR_P (t
->ctype
) = 1;
993 /* Must set up the overall size and alignment before determining
994 the context or laying out fields as those types may make
995 recursive references to this type. */
996 unsigned structsize
= t
->sym
->structsize
;
997 unsigned alignsize
= (t
->sym
->alignment
!= STRUCTALIGN_DEFAULT
)
998 ? t
->sym
->alignment
: t
->sym
->alignsize
;
1000 TYPE_SIZE (t
->ctype
) = bitsize_int (structsize
* BITS_PER_UNIT
);
1001 TYPE_SIZE_UNIT (t
->ctype
) = size_int (structsize
);
1002 SET_TYPE_ALIGN (t
->ctype
, alignsize
* BITS_PER_UNIT
);
1003 TYPE_PACKED (t
->ctype
) = (alignsize
== 1);
1004 compute_record_mode (t
->ctype
);
1006 /* Put out all fields. */
1007 layout_aggregate_type (t
->sym
, t
->ctype
, t
->sym
);
1008 apply_user_attributes (t
->sym
, t
->ctype
);
1009 finish_aggregate_type (structsize
, alignsize
, t
->ctype
);
1012 TYPE_CONTEXT (t
->ctype
) = d_decl_context (t
->sym
);
1013 build_type_decl (t
->ctype
, t
->sym
);
1015 /* For structs with a user defined postblit or a destructor,
1016 also set TREE_ADDRESSABLE on the type and all variants.
1017 This will make the struct be passed around by reference. */
1018 if (!t
->sym
->isPOD ())
1020 for (tree tv
= t
->ctype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
1022 TREE_ADDRESSABLE (tv
) = 1;
1023 SET_TYPE_MODE (tv
, BLKmode
);
1028 /* Build a class type. Whereas structs are value types, classes are
1029 reference types, with all the object-orientated features. */
1031 void visit (TypeClass
*t
)
1033 /* Merge types in the back-end if the front-end did not itself do so. */
1034 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
1035 if (merge_aggregate_types (t
, deco
))
1038 /* Need to set ctype right away in case of self-references to
1039 the type during this call. */
1040 tree basetype
= make_node (RECORD_TYPE
);
1041 t
->ctype
= build_pointer_type (basetype
);
1043 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
1045 /* Note that lang_specific data is assigned to both the reference
1046 and the underlying record type. */
1047 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
1048 TYPE_LANG_SPECIFIC (basetype
) = TYPE_LANG_SPECIFIC (t
->ctype
);
1049 CLASS_TYPE_P (basetype
) = 1;
1050 TYPE_CXX_ODR_P (basetype
) = 1;
1052 /* Put out all fields, including from each base class. */
1053 layout_aggregate_type (t
->sym
, basetype
, t
->sym
);
1054 apply_user_attributes (t
->sym
, basetype
);
1055 finish_aggregate_type (t
->sym
->structsize
, t
->sym
->alignsize
, basetype
);
1057 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
1058 for (tree tv
= basetype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
1060 TREE_ADDRESSABLE (tv
) = 1;
1061 SET_TYPE_MODE (tv
, BLKmode
);
1064 /* Type is final, there are no derivations. */
1065 if (t
->sym
->storage_class
& STCfinal
)
1066 TYPE_FINAL_P (basetype
) = 1;
1068 /* Create BINFO even if debugging is off. This is needed to keep
1069 references to inherited types. */
1070 if (!t
->sym
->isInterfaceDeclaration ())
1071 TYPE_BINFO (basetype
) = build_class_binfo (NULL_TREE
, t
->sym
);
1074 unsigned offset
= 0;
1076 TYPE_BINFO (basetype
) = build_interface_binfo (NULL_TREE
, t
->sym
,
1080 /* Associate all virtual methods with the class too. */
1081 for (size_t i
= 0; i
< t
->sym
->vtbl
.length
; i
++)
1083 FuncDeclaration
*fd
= t
->sym
->vtbl
[i
]->isFuncDeclaration ();
1084 tree method
= fd
? get_symbol_decl (fd
) : error_mark_node
;
1086 if (!error_operand_p (method
)
1087 && DECL_CONTEXT (method
) == basetype
1088 && !chain_member (method
, TYPE_FIELDS (basetype
)))
1089 TYPE_FIELDS (basetype
) = chainon (TYPE_FIELDS (basetype
), method
);
1092 TYPE_CONTEXT (basetype
) = d_decl_context (t
->sym
);
1093 build_type_decl (basetype
, t
->sym
);
1098 /* Build a tree from a frontend Type. */
1101 build_ctype (Type
*t
)
1107 /* Strip const modifiers from type before building. This is done
1108 to ensure that back-end treats e.g: const (T) as a variant of T,
1109 and not as two distinct types. */
1114 Type
*tb
= t
->castMod (0);
1117 t
->ctype
= insert_type_modifiers (tb
->ctype
, t
->mod
);