1 /* types.cc -- Lower D frontend types to GCC trees.
2 Copyright (C) 2006-2020 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"
45 /* Return TRUE if TYPE is a static array va_list. This is for compatibility
46 with the C ABI, where va_list static arrays are passed by reference.
47 However for every other case in D, static arrays are passed by value. */
50 valist_array_p (Type
*type
)
52 if (Type::tvalist
->ty
== Tsarray
)
54 Type
*tb
= type
->toBasetype ();
55 if (same_type_p (tb
, Type::tvalist
))
62 /* Returns true if TYPE contains no actual data, just various
63 possible combinations of empty aggregates. */
66 empty_aggregate_p (tree type
)
68 if (!AGGREGATE_TYPE_P (type
))
71 /* Want the element type for arrays. */
72 if (TREE_CODE (type
) == ARRAY_TYPE
)
73 return empty_aggregate_p (TREE_TYPE (type
));
75 /* Recursively check all fields. */
76 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
78 if (TREE_CODE (field
) == FIELD_DECL
79 && !empty_aggregate_p (TREE_TYPE (field
)))
86 /* Returns true if T1 and T2 are related to each other. */
89 same_type_p (Type
*t1
, Type
*t2
)
91 /* Types are equal. */
95 /* Types derive from the same base. */
96 Type
*tb1
= t1
->toBasetype ();
97 Type
*tb2
= t2
->toBasetype ();
101 /* Types are mutably the same type. */
102 if (tb1
->ty
== tb2
->ty
&& tb1
->equivalent (tb2
))
108 /* Returns 'Object' type which all D classes are derived from. */
111 get_object_type (void)
113 if (ClassDeclaration::object
)
114 return ClassDeclaration::object
->type
;
116 error ("missing or corrupt object.d");
121 /* Returns a static array of TYPE which has SIZE number of elements. */
124 make_array_type (Type
*type
, unsigned HOST_WIDE_INT size
)
126 /* In [arrays/void-arrays], void arrays can also be static, the length is
127 specified in bytes. */
128 if (type
->toBasetype ()->ty
== Tvoid
)
131 /* In [arrays/static-arrays], a static array with a dimension of 0 is allowed,
132 but no space is allocated for it. */
135 tree range
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (sizetype
),
136 TYPE_UNSIGNED (sizetype
));
137 tree index
= build_range_type (range
, size_zero_node
, NULL_TREE
);
139 tree t
= build_array_type (build_ctype (type
), index
);
140 TYPE_SIZE (t
) = bitsize_zero_node
;
141 TYPE_SIZE_UNIT (t
) = size_zero_node
;
145 return build_array_type (build_ctype (type
),
146 build_index_type (size_int (size
- 1)));
149 /* Builds a record type whose name is NAME. NFIELDS is the number of fields,
150 provided as field ident/type pairs. */
153 make_struct_type (const char *name
, int nfields
, ...)
155 tree fields
= NULL_TREE
;
158 va_start (ap
, nfields
);
160 for (int i
= 0; i
< nfields
; i
++)
162 tree ident
= va_arg (ap
, tree
);
163 tree type
= va_arg (ap
, tree
);
164 tree field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, ident
, type
);
165 DECL_CHAIN (field
) = fields
;
171 tree type
= make_node (RECORD_TYPE
);
172 finish_builtin_struct (type
, name
, fields
, NULL_TREE
);
177 /* Return qualified type variant of TYPE determined by modifier value MOD. */
180 insert_type_modifiers (tree type
, unsigned mod
)
190 case MODshared
| MODconst
:
191 case MODshared
| MODwild
:
192 case MODshared
| MODwildconst
:
193 quals
|= TYPE_QUAL_CONST
;
204 tree qualtype
= build_qualified_type (type
, quals
);
206 /* Mark whether the type is qualified 'shared'. */
208 TYPE_SHARED (qualtype
) = 1;
213 /* Adds FIELD into the aggregate TYPE at OFFSET. */
216 insert_aggregate_field (tree type
, tree field
, size_t offset
)
218 DECL_FIELD_CONTEXT (field
) = type
;
219 SET_DECL_OFFSET_ALIGN (field
, TYPE_ALIGN (TREE_TYPE (field
)));
220 DECL_FIELD_OFFSET (field
) = size_int (offset
);
221 DECL_FIELD_BIT_OFFSET (field
) = bitsize_zero_node
;
223 TREE_ADDRESSABLE (field
) = TYPE_SHARED (TREE_TYPE (field
));
225 layout_decl (field
, 0);
226 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), field
);
229 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET.
230 This is done as the frontend puts fields into the outer struct, and so
231 their offset is from the beginning of the aggregate.
232 We want the offset to be from the beginning of the anonymous aggregate. */
235 fixup_anonymous_offset (tree fields
, tree offset
)
237 while (fields
!= NULL_TREE
)
239 /* Traverse all nested anonymous aggregates to update their offset.
240 Set the anonymous decl offset to its first member. */
241 tree ftype
= TREE_TYPE (fields
);
242 if (TYPE_NAME (ftype
) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype
)))
244 tree vfields
= TYPE_FIELDS (ftype
);
245 fixup_anonymous_offset (vfields
, offset
);
246 DECL_FIELD_OFFSET (fields
) = DECL_FIELD_OFFSET (vfields
);
250 tree voffset
= DECL_FIELD_OFFSET (fields
);
251 DECL_FIELD_OFFSET (fields
) = size_binop (MINUS_EXPR
, voffset
, offset
);
254 fields
= DECL_CHAIN (fields
);
258 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT.
259 If INHERITED_P is true, then the members derive from a base class.
260 Returns the number of fields found. */
263 layout_aggregate_members (Dsymbols
*members
, tree context
, bool inherited_p
)
267 for (size_t i
= 0; i
< members
->dim
; i
++)
269 Dsymbol
*sym
= (*members
)[i
];
270 VarDeclaration
*var
= sym
->isVarDeclaration ();
273 /* Skip fields that have already been added. */
274 if (!inherited_p
&& var
->csym
!= NULL
)
277 /* If this variable was really a tuple, add all tuple fields. */
280 TupleDeclaration
*td
= var
->aliassym
->isTupleDeclaration ();
282 /* No other way to coerce the underlying type out of the tuple.
283 Frontend should have already validated this. */
284 for (size_t j
= 0; j
< td
->objects
->dim
; j
++)
286 RootObject
*ro
= (*td
->objects
)[j
];
287 gcc_assert (ro
->dyncast () == DYNCAST_EXPRESSION
);
288 Expression
*e
= (Expression
*) ro
;
289 gcc_assert (e
->op
== TOKdsymbol
);
290 DsymbolExp
*se
= (DsymbolExp
*) e
;
292 tmembers
.push (se
->s
);
295 fields
+= layout_aggregate_members (&tmembers
, context
,
300 /* Insert the field declaration at its given offset. */
303 const char *ident
= var
->ident
? var
->ident
->toChars () : NULL
;
304 tree field
= create_field_decl (declaration_type (var
), ident
,
305 inherited_p
, inherited_p
);
306 insert_aggregate_field (context
, field
, var
->offset
);
308 /* Because the front-end shares field decls across classes, don't
309 create the corresponding back-end symbol unless we are adding
310 it to the aggregate it is defined in. */
313 DECL_LANG_SPECIFIC (field
) = build_lang_decl (var
);
322 /* Anonymous struct/union are flattened by the frontend. However, we
323 want to keep the record layout in-tact when building the type. */
324 AnonDeclaration
*ad
= sym
->isAnonDeclaration ();
327 tree ident
= make_anon_name ();
328 tree type
= make_node (ad
->isunion
? UNION_TYPE
: RECORD_TYPE
);
329 ANON_AGGR_TYPE_P (type
) = 1;
332 /* Build the type declaration. */
333 tree decl
= build_decl (make_location_t (ad
->loc
),
334 TYPE_DECL
, ident
, type
);
335 DECL_CONTEXT (decl
) = context
;
336 DECL_ARTIFICIAL (decl
) = 1;
338 TYPE_CONTEXT (type
) = context
;
339 TYPE_NAME (type
) = decl
;
340 TYPE_STUB_DECL (type
) = decl
;
342 /* Recursively iterator over the anonymous members. */
343 fields
+= layout_aggregate_members (ad
->decl
, type
, inherited_p
);
345 /* Remove from the anon fields the base offset of this anonymous
346 aggregate. Undoes what is set-up in setFieldOffset, but doesn't
347 affect field accesses. */
348 tree offset
= size_int (ad
->anonoffset
);
349 fixup_anonymous_offset (TYPE_FIELDS (type
), offset
);
351 finish_aggregate_type (ad
->anonstructsize
, ad
->anonalignsize
,
354 /* And make the corresponding data member. */
355 tree field
= create_field_decl (type
, NULL
, 0, 0);
356 insert_aggregate_field (context
, field
, ad
->anonoffset
);
360 /* Other kinds of attributes don't create a scope. */
361 AttribDeclaration
*attrib
= sym
->isAttribDeclaration ();
364 Dsymbols
*decls
= attrib
->include (NULL
, NULL
);
367 fields
+= layout_aggregate_members (decls
, context
, inherited_p
);
372 /* Same with template mixins and namespaces. */
373 if (sym
->isTemplateMixin () || sym
->isNspace ())
375 ScopeDsymbol
*scopesym
= sym
->isScopeDsymbol ();
376 if (scopesym
->members
)
378 fields
+= layout_aggregate_members (scopesym
->members
, context
,
388 /* Write out all fields for aggregate BASE. For classes, write out all
389 interfaces first, then the base class fields. */
392 layout_aggregate_type (AggregateDeclaration
*decl
, tree type
,
393 AggregateDeclaration
*base
)
395 ClassDeclaration
*cd
= base
->isClassDeclaration ();
396 bool inherited_p
= (decl
!= base
);
401 layout_aggregate_type (decl
, type
, cd
->baseClass
);
404 /* This is the base class (Object) or interface. */
405 tree objtype
= TREE_TYPE (build_ctype (cd
->type
));
407 /* Add the vtable pointer, and optionally the monitor fields. */
408 InterfaceDeclaration
*id
= cd
->isInterfaceDeclaration ();
409 if (!id
|| id
->vtblInterfaces
->dim
== 0)
411 tree field
= create_field_decl (vtbl_ptr_type_node
, "__vptr", 1,
413 DECL_VIRTUAL_P (field
) = 1;
414 TYPE_VFIELD (type
) = field
;
415 DECL_FCONTEXT (field
) = objtype
;
416 insert_aggregate_field (type
, field
, 0);
419 if (!id
&& !cd
->isCPPclass ())
421 tree field
= create_field_decl (ptr_type_node
, "__monitor", 1,
423 insert_aggregate_field (type
, field
, Target::ptrsize
);
427 if (cd
->vtblInterfaces
)
429 for (size_t i
= 0; i
< cd
->vtblInterfaces
->dim
; i
++)
431 BaseClass
*bc
= (*cd
->vtblInterfaces
)[i
];
432 tree field
= create_field_decl (vtbl_ptr_type_node
, NULL
, 1, 1);
433 insert_aggregate_field (type
, field
, bc
->offset
);
440 size_t fields
= layout_aggregate_members (base
->members
, type
,
442 gcc_assert (fields
== base
->fields
.dim
);
444 /* Make sure that all fields have been created. */
447 for (size_t i
= 0; i
< base
->fields
.dim
; i
++)
449 VarDeclaration
*var
= base
->fields
[i
];
450 gcc_assert (var
->csym
!= NULL
);
456 /* Given a record type TYPE, whose size and alignment are determined by
457 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
458 the finalized record mode. */
461 finish_aggregate_type (unsigned structsize
, unsigned alignsize
,
462 tree type
, UserAttributeDeclaration
*attrs
)
464 TYPE_SIZE (type
) = NULL_TREE
;
466 /* Write out any GCC attributes that were applied to the type declaration. */
469 Expressions
*eattrs
= attrs
->getAttributes ();
470 decl_attributes (&type
, build_attributes (eattrs
),
471 ATTR_FLAG_TYPE_IN_PLACE
);
474 /* Set size and alignment as requested by frontend. */
475 TYPE_SIZE (type
) = bitsize_int (structsize
* BITS_PER_UNIT
);
476 TYPE_SIZE_UNIT (type
) = size_int (structsize
);
477 SET_TYPE_ALIGN (type
, alignsize
* BITS_PER_UNIT
);
478 TYPE_PACKED (type
) = (alignsize
== 1);
480 /* Set the back-end type mode. */
481 compute_record_mode (type
);
483 /* Fix up all variants of this aggregate type. */
484 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
489 TYPE_FIELDS (t
) = TYPE_FIELDS (type
);
490 TYPE_LANG_SPECIFIC (t
) = TYPE_LANG_SPECIFIC (type
);
491 SET_TYPE_ALIGN (t
, TYPE_ALIGN (type
));
492 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (type
);
493 gcc_assert (TYPE_MODE (t
) == TYPE_MODE (type
));
498 /* Implements the visitor interface to build the GCC trees of all
499 Type AST classes emitted from the D Front-end, where CTYPE holds
500 the cached back-end representation to be returned. */
502 class TypeVisitor
: public Visitor
504 using Visitor::visit
;
511 /* This should be overridden by each type class. */
518 /* Type assigned to erroneous expressions or constructs that
519 failed during the semantic stage. */
521 void visit (TypeError
*t
)
523 t
->ctype
= error_mark_node
;
526 /* Type assigned to generic nullable types. */
528 void visit (TypeNull
*t
)
530 t
->ctype
= ptr_type_node
;
534 /* Basic Data Types. */
536 void visit (TypeBasic
*t
)
538 /* [type/basic-data-types]
541 bool 8-bit boolean value.
542 byte 8-bit signed value.
543 ubyte 8-bit unsigned value.
544 short 16-bit signed value.
545 ushort 16-bit unsigned value.
546 int 32-bit signed value.
547 uint 32-bit unsigned value.
548 long 64-bit signed value.
549 ulong 64-bit unsigned value.
550 cent 128-bit signed value.
551 ucent 128-bit unsigned value.
552 float 32-bit IEEE 754 floating-point value.
553 double 64-bit IEEE 754 floating-point value.
554 real largest FP size implemented in hardware.
555 ifloat imaginary float.
556 idouble imaginary double.
557 ireal imaginary real.
558 cfloat complex float.
559 cdouble complex double.
561 char UTF-8 code unit.
562 wchar UTF-16 code unit.
563 dchar UTF-32 code unit. */
567 case Tvoid
: t
->ctype
= void_type_node
; break;
568 case Tbool
: t
->ctype
= d_bool_type
; break;
569 case Tint8
: t
->ctype
= d_byte_type
; break;
570 case Tuns8
: t
->ctype
= d_ubyte_type
; break;
571 case Tint16
: t
->ctype
= d_short_type
; break;
572 case Tuns16
: t
->ctype
= d_ushort_type
; break;
573 case Tint32
: t
->ctype
= d_int_type
; break;
574 case Tuns32
: t
->ctype
= d_uint_type
; break;
575 case Tint64
: t
->ctype
= d_long_type
; break;
576 case Tuns64
: t
->ctype
= d_ulong_type
; break;
577 case Tint128
: t
->ctype
= d_cent_type
; break;
578 case Tuns128
: t
->ctype
= d_ucent_type
; break;
579 case Tfloat32
: t
->ctype
= float_type_node
; break;
580 case Tfloat64
: t
->ctype
= double_type_node
; break;
581 case Tfloat80
: t
->ctype
= long_double_type_node
; break;
582 case Timaginary32
: t
->ctype
= ifloat_type_node
; break;
583 case Timaginary64
: t
->ctype
= idouble_type_node
; break;
584 case Timaginary80
: t
->ctype
= ireal_type_node
; break;
585 case Tcomplex32
: t
->ctype
= complex_float_type_node
; break;
586 case Tcomplex64
: t
->ctype
= complex_double_type_node
; break;
587 case Tcomplex80
: t
->ctype
= complex_long_double_type_node
; break;
588 case Tchar
: t
->ctype
= char8_type_node
; break;
589 case Twchar
: t
->ctype
= char16_type_node
; break;
590 case Tdchar
: t
->ctype
= char32_type_node
; break;
591 default: gcc_unreachable ();
594 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
598 /* Derived Data Types. */
600 /* Build a simple pointer to data type, analogous to C pointers. */
602 void visit (TypePointer
*t
)
604 t
->ctype
= build_pointer_type (build_ctype (t
->next
));
607 /* Build a dynamic array type, consisting of a length and a pointer
608 to the array data. */
610 void visit (TypeDArray
*t
)
612 /* In [abi/arrays], dynamic array layout is:
613 .length array dimension.
614 .ptr pointer to array data. */
615 t
->ctype
= make_struct_type (t
->toChars (), 2,
616 get_identifier ("length"),
617 build_ctype (Type::tsize_t
),
618 get_identifier ("ptr"),
619 build_pointer_type (build_ctype (t
->next
)));
620 TYPE_DYNAMIC_ARRAY (t
->ctype
) = 1;
621 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
625 /* Build a static array type, distinguished from dynamic arrays by
626 having a length fixed at compile-time, analogous to C arrays. */
628 void visit (TypeSArray
*t
)
630 if (t
->dim
->isConst () && t
->dim
->type
->isintegral ())
632 uinteger_t size
= t
->dim
->toUInteger ();
633 t
->ctype
= make_array_type (t
->next
, size
);
637 error ("invalid expression for static array dimension: %s",
643 /* Build a vector type, a fixed array of floating or integer types. */
645 void visit (TypeVector
*t
)
647 int nunits
= ((TypeSArray
*) t
->basetype
)->dim
->toUInteger ();
648 tree inner
= build_ctype (t
->elementType ());
650 /* Same rationale as void static arrays. */
651 if (inner
== void_type_node
)
652 inner
= build_ctype (Type::tuns8
);
654 t
->ctype
= build_vector_type (inner
, nunits
);
655 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
656 layout_type (t
->ctype
);
659 /* Build an associative array type, distinguished from arrays by having an
660 index that's not necessarily an integer, and can be sparsely populated. */
662 void visit (TypeAArray
*t
)
664 /* In [abi/associative-arrays], associative arrays are a struct that only
665 consist of a pointer to an opaque, implementation defined type. */
666 t
->ctype
= make_struct_type (t
->toChars (), 1,
667 get_identifier ("ptr"), ptr_type_node
);
668 TYPE_ASSOCIATIVE_ARRAY (t
->ctype
) = 1;
669 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
673 /* Build type for a function declaration, which consists of a return type,
674 and a list of parameter types, and a linkage attribute. */
676 void visit (TypeFunction
*t
)
678 tree fnparams
= NULL_TREE
;
681 /* [function/variadic]
683 Variadic functions with D linkage have an additional hidden argument
684 with the name _arguments passed to the function. */
685 if (t
->varargs
== 1 && t
->linkage
== LINKd
)
687 tree type
= build_ctype (Type::typeinfotypelist
->type
);
688 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
693 size_t n_args
= Parameter::dim (t
->parameters
);
695 for (size_t i
= 0; i
< n_args
; i
++)
697 tree type
= type_passed_as (Parameter::getNth (t
->parameters
, i
));
698 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
702 /* When the last parameter is void_list_node, that indicates a fixed length
703 parameter list, otherwise function is treated as variadic. */
705 fnparams
= chainon (fnparams
, void_list_node
);
709 fntype
= build_ctype (t
->next
);
711 fntype
= build_reference_type (fntype
);
714 fntype
= void_type_node
;
716 /* Could the function type be self referenced by parameters? */
717 t
->ctype
= build_function_type (fntype
, fnparams
);
718 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
721 /* Handle any special support for calling conventions. */
726 /* [attribute/linkage]
728 The Windows convention is distinct from the C convention only
729 on Win32, where it is equivalent to the stdcall convention. */
730 if (!global
.params
.is64bit
)
731 t
->ctype
= insert_type_attribute (t
->ctype
, "stdcall");
738 /* [abi/function-calling-conventions]
740 The extern (C) and extern (D) calling convention matches
741 the C calling convention used by the supported C compiler
742 on the host system. */
750 /* Build a delegate type, an aggregate of two pieces of data, an object
751 reference and a pointer to a non-static member function, or a pointer
752 to a closure and a pointer to a nested function. */
754 void visit (TypeDelegate
*t
)
756 /* In [abi/delegates], delegate layout is:
757 .ptr context pointer.
758 .funcptr pointer to function. */
759 tree fntype
= build_ctype (t
->next
);
760 tree dgtype
= build_vthis_function (void_type_node
, fntype
);
762 TYPE_ATTRIBUTES (dgtype
) = TYPE_ATTRIBUTES (fntype
);
763 TYPE_LANG_SPECIFIC (dgtype
) = TYPE_LANG_SPECIFIC (fntype
);
765 t
->ctype
= make_struct_type (t
->toChars (), 2,
766 get_identifier ("ptr"),
767 build_ctype (Type::tvoidptr
),
768 get_identifier ("funcptr"),
769 build_pointer_type (dgtype
));
770 TYPE_DELEGATE (t
->ctype
) = 1;
771 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
776 /* User Defined Types. */
778 /* Build a named enum type, a distinct value whose values are restrict to
779 a group of constants of the same underlying base type. */
781 void visit (TypeEnum
*t
)
783 tree basetype
= (t
->sym
->memtype
)
784 ? build_ctype (t
->sym
->memtype
) : void_type_node
;
786 if (!INTEGRAL_TYPE_P (basetype
) || TREE_CODE (basetype
) == BOOLEAN_TYPE
)
788 /* Enums in D2 can have a base type that is not necessarily integral.
789 For these, we simplify this a little by using the base type directly
790 instead of building an ENUMERAL_TYPE. */
791 t
->ctype
= build_variant_type_copy (basetype
);
795 t
->ctype
= make_node (ENUMERAL_TYPE
);
796 ENUM_IS_SCOPED (t
->ctype
) = 1;
797 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
800 if (flag_short_enums
)
801 TYPE_PACKED (t
->ctype
) = 1;
803 TYPE_PRECISION (t
->ctype
) = t
->size (t
->sym
->loc
) * 8;
804 TYPE_SIZE (t
->ctype
) = 0;
806 TYPE_MIN_VALUE (t
->ctype
) = TYPE_MIN_VALUE (basetype
);
807 TYPE_MAX_VALUE (t
->ctype
) = TYPE_MAX_VALUE (basetype
);
808 layout_type (t
->ctype
);
810 tree values
= NULL_TREE
;
813 for (size_t i
= 0; i
< t
->sym
->members
->dim
; i
++)
815 EnumMember
*member
= (*t
->sym
->members
)[i
]->isEnumMember ();
816 /* Templated functions can seep through to the back-end
817 just ignore for now. */
821 tree ident
= get_identifier (member
->ident
->toChars ());
822 tree value
= build_integer_cst (member
->value ()->toInteger (),
825 /* Build an identifier for the enumeration constant. */
826 tree decl
= build_decl (make_location_t (member
->loc
),
827 CONST_DECL
, ident
, basetype
);
828 DECL_CONTEXT (decl
) = t
->ctype
;
829 TREE_CONSTANT (decl
) = 1;
830 TREE_READONLY (decl
) = 1;
831 DECL_INITIAL (decl
) = value
;
833 /* Add this enumeration constant to the list for this type. */
834 values
= chainon (values
, build_tree_list (ident
, decl
));
838 TYPE_VALUES (t
->ctype
) = values
;
839 TYPE_UNSIGNED (t
->ctype
) = TYPE_UNSIGNED (basetype
);
840 build_type_decl (t
->ctype
, t
->sym
);
843 if (t
->sym
->userAttribDecl
)
845 Expressions
*eattrs
= t
->sym
->userAttribDecl
->getAttributes ();
846 decl_attributes (&t
->ctype
, build_attributes (eattrs
),
847 ATTR_FLAG_TYPE_IN_PLACE
);
851 /* Build a struct or union type. Layout should be exactly represented
852 as an equivalent C struct, except for non-POD or nested structs. */
854 void visit (TypeStruct
*t
)
856 /* Need to set this right away in case of self-references. */
857 t
->ctype
= make_node (t
->sym
->isUnionDeclaration ()
858 ? UNION_TYPE
: RECORD_TYPE
);
861 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
865 /* Must set up the overall size and alignment before determining
866 the context or laying out fields as those types may make
867 recursive references to this type. */
868 unsigned structsize
= t
->sym
->structsize
;
869 unsigned alignsize
= (t
->sym
->alignment
!= STRUCTALIGN_DEFAULT
)
870 ? t
->sym
->alignment
: t
->sym
->alignsize
;
872 TYPE_SIZE (t
->ctype
) = bitsize_int (structsize
* BITS_PER_UNIT
);
873 TYPE_SIZE_UNIT (t
->ctype
) = size_int (structsize
);
874 SET_TYPE_ALIGN (t
->ctype
, alignsize
* BITS_PER_UNIT
);
875 TYPE_PACKED (t
->ctype
) = (alignsize
== 1);
876 compute_record_mode (t
->ctype
);
878 /* Put out all fields. */
879 layout_aggregate_type (t
->sym
, t
->ctype
, t
->sym
);
880 finish_aggregate_type (structsize
, alignsize
, t
->ctype
,
881 t
->sym
->userAttribDecl
);
884 TYPE_CONTEXT (t
->ctype
) = d_decl_context (t
->sym
);
885 build_type_decl (t
->ctype
, t
->sym
);
887 /* For structs with a user defined postblit or a destructor,
888 also set TREE_ADDRESSABLE on the type and all variants.
889 This will make the struct be passed around by reference. */
890 if (t
->sym
->postblit
|| t
->sym
->dtor
)
892 for (tree tv
= t
->ctype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
893 TREE_ADDRESSABLE (tv
) = 1;
897 /* Build a class type. Whereas structs are value types, classes are
898 reference types, with all the object-orientated features. */
900 void visit (TypeClass
*t
)
902 /* Need to set ctype right away in case of self-references to
903 the type during this call. */
904 tree basetype
= make_node (RECORD_TYPE
);
905 t
->ctype
= build_pointer_type (basetype
);
908 /* Note that lang_specific data is assigned to both the reference
909 and the underlying record type. */
910 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
911 TYPE_LANG_SPECIFIC (basetype
) = TYPE_LANG_SPECIFIC (t
->ctype
);
912 CLASS_TYPE_P (basetype
) = 1;
914 /* Put out all fields, including from each base class. */
915 layout_aggregate_type (t
->sym
, basetype
, t
->sym
);
916 finish_aggregate_type (t
->sym
->structsize
, t
->sym
->alignsize
,
917 basetype
, t
->sym
->userAttribDecl
);
919 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
920 for (tree tv
= basetype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
921 TREE_ADDRESSABLE (tv
) = 1;
923 /* Type is final, there are no derivations. */
924 if (t
->sym
->storage_class
& STCfinal
)
925 TYPE_FINAL_P (basetype
) = 1;
927 /* Create BINFO even if debugging is off. This is needed to keep
928 references to inherited types. */
929 if (!t
->sym
->isInterfaceDeclaration ())
930 TYPE_BINFO (basetype
) = build_class_binfo (NULL_TREE
, t
->sym
);
935 TYPE_BINFO (basetype
) = build_interface_binfo (NULL_TREE
, t
->sym
,
939 /* Associate all virtual methods with the class too. */
940 for (size_t i
= 0; i
< t
->sym
->vtbl
.dim
; i
++)
942 FuncDeclaration
*fd
= t
->sym
->vtbl
[i
]->isFuncDeclaration ();
943 tree method
= fd
? get_symbol_decl (fd
) : error_mark_node
;
945 if (!error_operand_p (method
)
946 && DECL_CONTEXT (method
) == basetype
947 && !chain_member (method
, TYPE_FIELDS (basetype
)))
948 TYPE_FIELDS (basetype
) = chainon (TYPE_FIELDS (basetype
), method
);
951 TYPE_CONTEXT (basetype
) = d_decl_context (t
->sym
);
952 build_type_decl (basetype
, t
->sym
);
957 /* Build a tree from a frontend Type. */
960 build_ctype (Type
*t
)
966 /* Strip const modifiers from type before building. This is done
967 to ensure that back-end treats e.g: const (T) as a variant of T,
968 and not as two distinct types. */
973 Type
*tb
= t
->castMod (0);
976 t
->ctype
= insert_type_modifiers (tb
->ctype
, t
->mod
);