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
== 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 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
== TY::Tstruct
)
550 sym
= type
->isTypeStruct ()->sym
;
551 else if (type
->ty
== 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
= noreturn_type_node
;
614 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
617 /* Basic Data Types. */
619 void visit (TypeBasic
*t
)
621 /* [type/basic-data-types]
624 bool 8-bit boolean value.
625 byte 8-bit signed value.
626 ubyte 8-bit unsigned value.
627 short 16-bit signed value.
628 ushort 16-bit unsigned value.
629 int 32-bit signed value.
630 uint 32-bit unsigned value.
631 long 64-bit signed value.
632 ulong 64-bit unsigned value.
633 cent 128-bit signed value.
634 ucent 128-bit unsigned value.
635 float 32-bit IEEE 754 floating-point value.
636 double 64-bit IEEE 754 floating-point value.
637 real largest FP size implemented in hardware.
638 ifloat imaginary float.
639 idouble imaginary double.
640 ireal imaginary real.
641 cfloat complex float.
642 cdouble complex double.
644 char UTF-8 code unit.
645 wchar UTF-16 code unit.
646 dchar UTF-32 code unit. */
650 case TY::Tvoid
: t
->ctype
= void_type_node
; break;
651 case TY::Tbool
: t
->ctype
= d_bool_type
; break;
652 case TY::Tint8
: t
->ctype
= d_byte_type
; break;
653 case TY::Tuns8
: t
->ctype
= d_ubyte_type
; break;
654 case TY::Tint16
: t
->ctype
= d_short_type
; break;
655 case TY::Tuns16
: t
->ctype
= d_ushort_type
; break;
656 case TY::Tint32
: t
->ctype
= d_int_type
; break;
657 case TY::Tuns32
: t
->ctype
= d_uint_type
; break;
658 case TY::Tint64
: t
->ctype
= d_long_type
; break;
659 case TY::Tuns64
: t
->ctype
= d_ulong_type
; break;
660 case TY::Tint128
: t
->ctype
= d_cent_type
; break;
661 case TY::Tuns128
: t
->ctype
= d_ucent_type
; break;
662 case TY::Tfloat32
: t
->ctype
= float_type_node
; break;
663 case TY::Tfloat64
: t
->ctype
= double_type_node
; break;
664 case TY::Tfloat80
: t
->ctype
= long_double_type_node
; break;
665 case TY::Timaginary32
: t
->ctype
= ifloat_type_node
; break;
666 case TY::Timaginary64
: t
->ctype
= idouble_type_node
; break;
667 case TY::Timaginary80
: t
->ctype
= ireal_type_node
; break;
668 case TY::Tcomplex32
: t
->ctype
= complex_float_type_node
; break;
669 case TY::Tcomplex64
: t
->ctype
= complex_double_type_node
; break;
670 case TY::Tcomplex80
: t
->ctype
= complex_long_double_type_node
; break;
671 case TY::Tchar
: t
->ctype
= char8_type_node
; break;
672 case TY::Twchar
: t
->ctype
= char16_type_node
; break;
673 case TY::Tdchar
: t
->ctype
= char32_type_node
; break;
674 default: gcc_unreachable ();
677 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
681 /* Derived Data Types. */
683 /* Build a simple pointer to data type, analogous to C pointers. */
685 void visit (TypePointer
*t
)
687 t
->ctype
= build_pointer_type (build_ctype (t
->next
));
690 /* Build a dynamic array type, consisting of a length and a pointer
691 to the array data. */
693 void visit (TypeDArray
*t
)
695 /* In [abi/arrays], dynamic array layout is:
696 .length array dimension.
697 .ptr pointer to array data. */
698 t
->ctype
= make_struct_type (t
->toChars (), 2,
699 get_identifier ("length"),
700 build_ctype (Type::tsize_t
),
701 get_identifier ("ptr"),
702 build_pointer_type (build_ctype (t
->next
)));
703 TYPE_DYNAMIC_ARRAY (t
->ctype
) = 1;
704 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
708 /* Build a static array type, distinguished from dynamic arrays by
709 having a length fixed at compile-time, analogous to C arrays. */
711 void visit (TypeSArray
*t
)
713 if (t
->dim
->isConst () && t
->dim
->type
->isintegral ())
715 uinteger_t size
= t
->dim
->toUInteger ();
716 t
->ctype
= make_array_type (t
->next
, size
);
720 error ("invalid expression for static array dimension: %s",
726 /* Build a vector type, a fixed array of floating or integer types. */
728 void visit (TypeVector
*t
)
730 int nunits
= t
->basetype
->isTypeSArray ()->dim
->toUInteger ();
731 tree inner
= build_ctype (t
->elementType ());
733 /* Same rationale as void static arrays. */
734 if (inner
== void_type_node
)
735 inner
= build_ctype (Type::tuns8
);
737 t
->ctype
= build_vector_type (inner
, nunits
);
738 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
739 layout_type (t
->ctype
);
742 /* Build an associative array type, distinguished from arrays by having an
743 index that's not necessarily an integer, and can be sparsely populated. */
745 void visit (TypeAArray
*t
)
747 /* In [abi/associative-arrays], associative arrays are a struct that only
748 consist of a pointer to an opaque, implementation defined type. */
749 t
->ctype
= make_struct_type (t
->toChars (), 1,
750 get_identifier ("ptr"), ptr_type_node
);
751 TYPE_ASSOCIATIVE_ARRAY (t
->ctype
) = 1;
752 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
756 /* Build type for a function declaration, which consists of a return type,
757 and a list of parameter types, and a linkage attribute. */
759 void visit (TypeFunction
*t
)
761 tree fnparams
= NULL_TREE
;
764 /* [function/variadic]
766 Variadic functions with D linkage have an additional hidden argument
767 with the name _arguments passed to the function. */
768 if (t
->isDstyleVariadic ())
770 tree type
= build_ctype (Type::typeinfotypelist
->type
);
771 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
774 const size_t n_args
= t
->parameterList
.length ();
776 for (size_t i
= 0; i
< n_args
; i
++)
778 tree type
= parameter_type (t
->parameterList
[i
]);
780 /* Type `noreturn` is a terminator, as no other arguments can possibly
781 be evaluated after it. */
782 if (type
== noreturn_type_node
)
785 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
788 /* When the last parameter is void_list_node, that indicates a fixed length
789 parameter list, otherwise function is treated as variadic. */
790 if (t
->parameterList
.varargs
!= VARARGvariadic
)
791 fnparams
= chainon (fnparams
, void_list_node
);
795 fntype
= build_ctype (t
->next
);
797 fntype
= build_reference_type (fntype
);
800 fntype
= void_type_node
;
802 /* Could the function type be self referenced by parameters? */
803 t
->ctype
= build_function_type (fntype
, fnparams
);
804 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
807 /* Qualify function types that have the type `noreturn` as volatile. */
808 if (fntype
== noreturn_type_node
)
809 t
->ctype
= build_qualified_type (t
->ctype
, TYPE_QUAL_VOLATILE
);
811 /* Handle any special support for calling conventions. */
816 /* [attribute/linkage]
818 The Windows convention is distinct from the C convention only
819 on Win32, where it is equivalent to the stdcall convention. */
820 unsigned link_system
, link_windows
;
821 if (targetdm
.d_has_stdcall_convention (&link_system
, &link_windows
))
824 t
->ctype
= insert_type_attribute (t
->ctype
, "stdcall");
833 /* [abi/function-calling-conventions]
835 The extern (C) and extern (D) calling convention matches
836 the C calling convention used by the supported C compiler
837 on the host system. */
845 /* Build a delegate type, an aggregate of two pieces of data, an object
846 reference and a pointer to a non-static member function, or a pointer
847 to a closure and a pointer to a nested function. */
849 void visit (TypeDelegate
*t
)
851 /* In [abi/delegates], delegate layout is:
852 .ptr context pointer.
853 .funcptr pointer to function. */
854 tree fntype
= build_ctype (t
->next
);
855 tree dgtype
= build_vthis_function (void_type_node
, fntype
);
857 TYPE_ATTRIBUTES (dgtype
) = TYPE_ATTRIBUTES (fntype
);
858 TYPE_LANG_SPECIFIC (dgtype
) = TYPE_LANG_SPECIFIC (fntype
);
860 t
->ctype
= make_struct_type (t
->toChars (), 2,
861 get_identifier ("ptr"),
862 build_ctype (Type::tvoidptr
),
863 get_identifier ("funcptr"),
864 build_pointer_type (dgtype
));
865 TYPE_DELEGATE (t
->ctype
) = 1;
866 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
871 /* User Defined Types. */
873 /* Build a named enum type, a distinct value whose values are restrict to
874 a group of constants of the same underlying base type. */
876 void visit (TypeEnum
*t
)
878 tree basetype
= (t
->sym
->memtype
)
879 ? build_ctype (t
->sym
->memtype
) : void_type_node
;
881 if (t
->sym
->isSpecial ())
883 /* Special enums are opaque types that bind to C types. */
884 const char *ident
= t
->toChars ();
885 Type
*underlying
= NULL
;
887 /* Skip over the prefixing `__c_'. */
888 gcc_assert (startswith (ident
, "__c_"));
889 ident
= ident
+ strlen ("__c_");
891 /* To keep things compatible within the code generation we stick to
892 mapping to equivalent D types. However it should be OK to use the
893 GCC provided C types here as the front-end enforces that everything
894 must be explicitly cast from a D type to any of the opaque types. */
895 if (strcmp (ident
, "long") == 0)
896 underlying
= build_frontend_type (long_integer_type_node
);
897 else if (strcmp (ident
, "ulong") == 0)
898 underlying
= build_frontend_type (long_unsigned_type_node
);
899 else if (strcmp (ident
, "wchar_t") == 0)
901 build_frontend_type (make_unsigned_type (WCHAR_TYPE_SIZE
));
902 else if (strcmp (ident
, "longlong") == 0)
903 underlying
= build_frontend_type (long_long_integer_type_node
);
904 else if (strcmp (ident
, "ulonglong") == 0)
905 underlying
= build_frontend_type (long_long_unsigned_type_node
);
906 else if (strcmp (ident
, "long_double") == 0)
907 underlying
= build_frontend_type (long_double_type_node
);
908 else if (strcmp (ident
, "complex_real") == 0)
909 underlying
= build_frontend_type (complex_long_double_type_node
);
910 else if (strcmp (ident
, "complex_float") == 0)
911 underlying
= build_frontend_type (complex_float_type_node
);
912 else if (strcmp (ident
, "complex_double") == 0)
913 underlying
= build_frontend_type (complex_double_type_node
);
915 /* Conversion failed or there's an unhandled special type. */
916 gcc_assert (underlying
!= NULL
);
918 t
->ctype
= build_variant_type_copy (build_ctype (underlying
));
919 build_type_decl (t
->ctype
, t
->sym
);
921 else if (!INTEGRAL_TYPE_P (basetype
) || TREE_CODE (basetype
) == BOOLEAN_TYPE
)
923 /* Enums in D2 can have a base type that is not necessarily integral.
924 For these, we simplify this a little by using the base type directly
925 instead of building an ENUMERAL_TYPE. */
926 t
->ctype
= build_variant_type_copy (basetype
);
927 build_type_decl (t
->ctype
, t
->sym
);
931 t
->ctype
= make_node (ENUMERAL_TYPE
);
932 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
935 ENUM_IS_SCOPED (t
->ctype
) = 1;
936 TREE_TYPE (t
->ctype
) = basetype
;
938 if (flag_short_enums
)
939 TYPE_PACKED (t
->ctype
) = 1;
941 TYPE_PRECISION (t
->ctype
) = t
->size (t
->sym
->loc
) * 8;
942 TYPE_SIZE (t
->ctype
) = 0;
944 TYPE_MIN_VALUE (t
->ctype
) = TYPE_MIN_VALUE (basetype
);
945 TYPE_MAX_VALUE (t
->ctype
) = TYPE_MAX_VALUE (basetype
);
946 layout_type (t
->ctype
);
948 tree values
= NULL_TREE
;
951 for (size_t i
= 0; i
< t
->sym
->members
->length
; i
++)
953 EnumMember
*member
= (*t
->sym
->members
)[i
]->isEnumMember ();
954 /* Templated functions can seep through to the back-end
955 just ignore for now. */
959 tree ident
= get_identifier (member
->ident
->toChars ());
960 tree value
= build_integer_cst (member
->value ()->toInteger (),
963 /* Build an identifier for the enumeration constant. */
964 tree decl
= build_decl (make_location_t (member
->loc
),
965 CONST_DECL
, ident
, basetype
);
966 DECL_CONTEXT (decl
) = t
->ctype
;
967 TREE_CONSTANT (decl
) = 1;
968 TREE_READONLY (decl
) = 1;
969 DECL_INITIAL (decl
) = value
;
971 /* Add this enumeration constant to the list for this type. */
972 values
= chainon (values
, build_tree_list (ident
, decl
));
976 TYPE_VALUES (t
->ctype
) = values
;
977 TYPE_UNSIGNED (t
->ctype
) = TYPE_UNSIGNED (basetype
);
978 build_type_decl (t
->ctype
, t
->sym
);
981 apply_user_attributes (t
->sym
, t
->ctype
);
984 /* Build a struct or union type. Layout should be exactly represented
985 as an equivalent C struct, except for non-POD or nested structs. */
987 void visit (TypeStruct
*t
)
989 /* Merge types in the back-end if the front-end did not itself do so. */
990 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
991 if (merge_aggregate_types (t
, deco
))
994 /* Need to set this right away in case of self-references. */
995 t
->ctype
= make_node (t
->sym
->isUnionDeclaration ()
996 ? UNION_TYPE
: RECORD_TYPE
);
998 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
1000 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
1001 TYPE_CXX_ODR_P (t
->ctype
) = 1;
1003 if (t
->sym
->members
)
1005 /* Must set up the overall size and alignment before determining
1006 the context or laying out fields as those types may make
1007 recursive references to this type. */
1008 unsigned structsize
= t
->sym
->structsize
;
1009 unsigned alignsize
= t
->sym
->alignment
.isDefault ()
1010 ? t
->sym
->alignsize
: t
->sym
->alignment
.get ();
1012 TYPE_SIZE (t
->ctype
) = bitsize_int (structsize
* BITS_PER_UNIT
);
1013 TYPE_SIZE_UNIT (t
->ctype
) = size_int (structsize
);
1014 SET_TYPE_ALIGN (t
->ctype
, alignsize
* BITS_PER_UNIT
);
1015 TYPE_PACKED (t
->ctype
) = (alignsize
== 1);
1016 compute_record_mode (t
->ctype
);
1018 /* Put out all fields. */
1019 layout_aggregate_type (t
->sym
, t
->ctype
, t
->sym
);
1020 apply_user_attributes (t
->sym
, t
->ctype
);
1021 finish_aggregate_type (structsize
, alignsize
, t
->ctype
);
1024 TYPE_CONTEXT (t
->ctype
) = d_decl_context (t
->sym
);
1025 build_type_decl (t
->ctype
, t
->sym
);
1027 /* For structs with a user defined postblit, copy constructor, or a
1028 destructor, also set TREE_ADDRESSABLE on the type and all variants.
1029 This will make the struct be passed around by reference. */
1030 if (!t
->sym
->isPOD ())
1032 for (tree tv
= t
->ctype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
1034 TREE_ADDRESSABLE (tv
) = 1;
1035 SET_TYPE_MODE (tv
, BLKmode
);
1040 /* Build a class type. Whereas structs are value types, classes are
1041 reference types, with all the object-orientated features. */
1043 void visit (TypeClass
*t
)
1045 /* Merge types in the back-end if the front-end did not itself do so. */
1046 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
1047 if (merge_aggregate_types (t
, deco
))
1050 /* Need to set ctype right away in case of self-references to
1051 the type during this call. */
1052 tree basetype
= make_node (RECORD_TYPE
);
1053 t
->ctype
= build_pointer_type (basetype
);
1055 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
1057 /* Note that lang_specific data is assigned to both the reference
1058 and the underlying record type. */
1059 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
1060 TYPE_LANG_SPECIFIC (basetype
) = TYPE_LANG_SPECIFIC (t
->ctype
);
1061 CLASS_TYPE_P (basetype
) = 1;
1062 TYPE_CXX_ODR_P (basetype
) = 1;
1064 /* Put out all fields, including from each base class. */
1065 layout_aggregate_type (t
->sym
, basetype
, t
->sym
);
1066 apply_user_attributes (t
->sym
, basetype
);
1067 finish_aggregate_type (t
->sym
->structsize
, t
->sym
->alignsize
, basetype
);
1069 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
1070 for (tree tv
= basetype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
1072 TREE_ADDRESSABLE (tv
) = 1;
1073 SET_TYPE_MODE (tv
, BLKmode
);
1076 /* Type is final, there are no derivations. */
1077 if (t
->sym
->storage_class
& STCfinal
)
1078 TYPE_FINAL_P (basetype
) = 1;
1080 /* Create BINFO even if debugging is off. This is needed to keep
1081 references to inherited types. */
1082 if (!t
->sym
->isInterfaceDeclaration ())
1083 TYPE_BINFO (basetype
) = build_class_binfo (NULL_TREE
, t
->sym
);
1086 unsigned offset
= 0;
1088 TYPE_BINFO (basetype
) = build_interface_binfo (NULL_TREE
, t
->sym
,
1092 /* Associate all virtual methods with the class too. */
1093 for (size_t i
= 0; i
< t
->sym
->vtbl
.length
; i
++)
1095 FuncDeclaration
*fd
= t
->sym
->vtbl
[i
]->isFuncDeclaration ();
1096 tree method
= fd
? get_symbol_decl (fd
) : error_mark_node
;
1098 if (!error_operand_p (method
)
1099 && DECL_CONTEXT (method
) == basetype
1100 && !chain_member (method
, TYPE_FIELDS (basetype
)))
1101 TYPE_FIELDS (basetype
) = chainon (TYPE_FIELDS (basetype
), method
);
1104 TYPE_CONTEXT (basetype
) = d_decl_context (t
->sym
);
1105 build_type_decl (basetype
, t
->sym
);
1110 /* Build a tree from a frontend Type. */
1113 build_ctype (Type
*t
)
1119 /* Strip const modifiers from type before building. This is done
1120 to ensure that back-end treats e.g: const (T) as a variant of T,
1121 and not as two distinct types. */
1126 Type
*tb
= t
->castMod (0);
1129 t
->ctype
= insert_type_modifiers (tb
->ctype
, t
->mod
);