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 Type
*tvalist
= target
.va_listType (Loc (), NULL
);
53 if (tvalist
->ty
== Tsarray
)
55 Type
*tb
= type
->toBasetype ();
56 if (same_type_p (tb
, tvalist
))
63 /* Returns true if TYPE contains no actual data, just various
64 possible combinations of empty aggregates. */
67 empty_aggregate_p (tree type
)
69 if (!AGGREGATE_TYPE_P (type
))
72 /* Want the element type for arrays. */
73 if (TREE_CODE (type
) == ARRAY_TYPE
)
74 return empty_aggregate_p (TREE_TYPE (type
));
76 /* Recursively check all fields. */
77 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
79 if (TREE_CODE (field
) == FIELD_DECL
80 && !empty_aggregate_p (TREE_TYPE (field
)))
87 /* Returns true if T1 and T2 are related to each other. */
90 same_type_p (Type
*t1
, Type
*t2
)
92 /* Types are equal. */
96 /* Types derive from the same base. */
97 Type
*tb1
= t1
->toBasetype ();
98 Type
*tb2
= t2
->toBasetype ();
102 /* Types are mutably the same type. */
103 if (tb1
->ty
== tb2
->ty
&& tb1
->equivalent (tb2
))
109 /* Returns `Object' type which all D classes are derived from. */
112 get_object_type (void)
114 if (ClassDeclaration::object
)
115 return ClassDeclaration::object
->type
;
117 error ("missing or corrupt object.d");
122 /* Returns a static array of TYPE which has SIZE number of elements. */
125 make_array_type (Type
*type
, unsigned HOST_WIDE_INT size
)
127 /* In [arrays/void-arrays], void arrays can also be static, the length is
128 specified in bytes. */
129 if (type
->toBasetype ()->ty
== Tvoid
)
132 /* In [arrays/static-arrays], a static array with a dimension of 0 is allowed,
133 but no space is allocated for it. */
136 tree range
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (sizetype
),
137 TYPE_UNSIGNED (sizetype
));
138 tree index
= build_range_type (range
, size_zero_node
, NULL_TREE
);
140 tree t
= build_array_type (build_ctype (type
), index
);
141 TYPE_SIZE (t
) = bitsize_zero_node
;
142 TYPE_SIZE_UNIT (t
) = size_zero_node
;
146 return build_array_type (build_ctype (type
),
147 build_index_type (size_int (size
- 1)));
150 /* Builds a record type whose name is NAME. NFIELDS is the number of fields,
151 provided as field ident/type pairs. */
154 make_struct_type (const char *name
, int nfields
, ...)
156 tree fields
= NULL_TREE
;
159 va_start (ap
, nfields
);
161 for (int i
= 0; i
< nfields
; i
++)
163 tree ident
= va_arg (ap
, tree
);
164 tree type
= va_arg (ap
, tree
);
165 tree field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, ident
, type
);
166 DECL_CHAIN (field
) = fields
;
172 tree type
= make_node (RECORD_TYPE
);
173 finish_builtin_struct (type
, name
, fields
, NULL_TREE
);
178 /* Return qualified type variant of TYPE determined by modifier value MOD. */
181 insert_type_modifiers (tree type
, unsigned mod
)
191 case MODshared
| MODconst
:
192 case MODshared
| MODwild
:
193 case MODshared
| MODwildconst
:
194 quals
|= TYPE_QUAL_CONST
;
205 tree qualtype
= build_qualified_type (type
, quals
);
207 /* Mark whether the type is qualified `shared'. */
209 TYPE_SHARED (qualtype
) = 1;
214 /* Adds FIELD into the aggregate TYPE at OFFSET. */
217 insert_aggregate_field (tree type
, tree field
, size_t offset
)
219 DECL_FIELD_CONTEXT (field
) = type
;
220 SET_DECL_OFFSET_ALIGN (field
, TYPE_ALIGN (TREE_TYPE (field
)));
221 DECL_FIELD_OFFSET (field
) = size_int (offset
);
222 DECL_FIELD_BIT_OFFSET (field
) = bitsize_zero_node
;
224 TREE_ADDRESSABLE (field
) = TYPE_SHARED (TREE_TYPE (field
));
226 layout_decl (field
, 0);
227 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), field
);
230 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET.
231 This is done as the frontend puts fields into the outer struct, and so
232 their offset is from the beginning of the aggregate.
233 We want the offset to be from the beginning of the anonymous aggregate. */
236 fixup_anonymous_offset (tree fields
, tree offset
)
238 /* No adjustment in field offset required. */
239 if (integer_zerop (offset
))
242 while (fields
!= NULL_TREE
)
244 /* Traverse all nested anonymous aggregates to update the offset of their
245 fields. Note that the anonymous field itself is not adjusted, as it
246 already has an offset relative to its outer aggregate. */
247 tree ftype
= TREE_TYPE (fields
);
248 if (TYPE_NAME (ftype
) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype
)))
250 tree vfields
= TYPE_FIELDS (ftype
);
251 fixup_anonymous_offset (vfields
, offset
);
255 tree voffset
= DECL_FIELD_OFFSET (fields
);
256 DECL_FIELD_OFFSET (fields
) = size_binop (MINUS_EXPR
, voffset
, offset
);
259 fields
= DECL_CHAIN (fields
);
263 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT.
264 If INHERITED_P is true, then the members derive from a base class.
265 Returns the number of fields found. */
268 layout_aggregate_members (Dsymbols
*members
, tree context
, bool inherited_p
)
272 for (size_t i
= 0; i
< members
->length
; i
++)
274 Dsymbol
*sym
= (*members
)[i
];
275 VarDeclaration
*var
= sym
->isVarDeclaration ();
278 /* Skip fields that have already been added. */
279 if (!inherited_p
&& var
->csym
!= NULL
)
282 /* If this variable was really a tuple, add all tuple fields. */
285 TupleDeclaration
*td
= var
->aliassym
->isTupleDeclaration ();
287 /* No other way to coerce the underlying type out of the tuple.
288 Frontend should have already validated this. */
289 for (size_t j
= 0; j
< td
->objects
->length
; j
++)
291 RootObject
*ro
= (*td
->objects
)[j
];
292 gcc_assert (ro
->dyncast () == DYNCAST_EXPRESSION
);
293 Expression
*e
= (Expression
*) ro
;
294 gcc_assert (e
->op
== TOKdsymbol
);
295 DsymbolExp
*se
= e
->isDsymbolExp ();
297 tmembers
.push (se
->s
);
300 fields
+= layout_aggregate_members (&tmembers
, context
,
305 /* Insert the field declaration at its given offset. */
308 const char *ident
= var
->ident
? var
->ident
->toChars () : NULL
;
309 tree field
= create_field_decl (declaration_type (var
), ident
,
310 inherited_p
, inherited_p
);
311 insert_aggregate_field (context
, field
, var
->offset
);
313 /* Because the front-end shares field decls across classes, don't
314 create the corresponding back-end symbol unless we are adding
315 it to the aggregate it is defined in. */
318 DECL_LANG_SPECIFIC (field
) = build_lang_decl (var
);
327 /* Anonymous struct/union are flattened by the frontend. However, we
328 want to keep the record layout in-tact when building the type. */
329 AnonDeclaration
*ad
= sym
->isAnonDeclaration ();
332 tree ident
= make_anon_name ();
333 tree type
= make_node (ad
->isunion
? UNION_TYPE
: RECORD_TYPE
);
334 ANON_AGGR_TYPE_P (type
) = 1;
337 /* Build the type declaration. */
338 tree decl
= build_decl (make_location_t (ad
->loc
),
339 TYPE_DECL
, ident
, type
);
340 DECL_CONTEXT (decl
) = context
;
341 DECL_ARTIFICIAL (decl
) = 1;
343 TYPE_CONTEXT (type
) = context
;
344 TYPE_NAME (type
) = decl
;
345 TYPE_STUB_DECL (type
) = decl
;
347 /* Recursively iterator over the anonymous members. */
348 fields
+= layout_aggregate_members (ad
->decl
, type
, inherited_p
);
350 /* Remove from the anon fields the base offset of this anonymous
351 aggregate. Undoes what is set-up in setFieldOffset, but doesn't
352 affect field accesses. */
353 tree offset
= size_int (ad
->anonoffset
);
354 fixup_anonymous_offset (TYPE_FIELDS (type
), offset
);
356 finish_aggregate_type (ad
->anonstructsize
, ad
->anonalignsize
, type
);
358 /* And make the corresponding data member. */
359 tree field
= create_field_decl (type
, NULL
, 0, 0);
360 insert_aggregate_field (context
, field
, ad
->anonoffset
);
364 /* Other kinds of attributes don't create a scope. */
365 AttribDeclaration
*attrib
= sym
->isAttribDeclaration ();
368 Dsymbols
*decls
= attrib
->include (NULL
);
371 fields
+= layout_aggregate_members (decls
, context
, inherited_p
);
376 /* Same with template mixins and namespaces. */
377 if (sym
->isTemplateMixin () || sym
->isNspace ())
379 ScopeDsymbol
*scopesym
= sym
->isScopeDsymbol ();
380 if (scopesym
->members
)
382 fields
+= layout_aggregate_members (scopesym
->members
, context
,
392 /* Write out all fields for aggregate BASE. For classes, write out all
393 interfaces first, then the base class fields. */
396 layout_aggregate_type (AggregateDeclaration
*decl
, tree type
,
397 AggregateDeclaration
*base
)
399 ClassDeclaration
*cd
= base
->isClassDeclaration ();
400 bool inherited_p
= (decl
!= base
);
405 layout_aggregate_type (decl
, type
, cd
->baseClass
);
408 /* This is the base class (Object) or interface. */
409 tree objtype
= TREE_TYPE (build_ctype (cd
->type
));
411 /* Add the vtable pointer, and optionally the monitor fields. */
412 InterfaceDeclaration
*id
= cd
->isInterfaceDeclaration ();
413 if (!id
|| id
->vtblInterfaces
->length
== 0)
415 tree field
= create_field_decl (vtbl_ptr_type_node
, "__vptr", 1,
417 DECL_VIRTUAL_P (field
) = 1;
418 TYPE_VFIELD (type
) = field
;
419 DECL_FCONTEXT (field
) = objtype
;
420 insert_aggregate_field (type
, field
, 0);
423 if (!id
&& !cd
->isCPPclass ())
425 tree field
= create_field_decl (ptr_type_node
, "__monitor", 1,
427 insert_aggregate_field (type
, field
, target
.ptrsize
);
431 if (cd
->vtblInterfaces
)
433 for (size_t i
= 0; i
< cd
->vtblInterfaces
->length
; i
++)
435 BaseClass
*bc
= (*cd
->vtblInterfaces
)[i
];
436 tree field
= create_field_decl (vtbl_ptr_type_node
, NULL
, 1, 1);
437 insert_aggregate_field (type
, field
, bc
->offset
);
444 size_t fields
= layout_aggregate_members (base
->members
, type
,
446 gcc_assert (fields
== base
->fields
.length
);
448 /* Make sure that all fields have been created. */
451 for (size_t i
= 0; i
< base
->fields
.length
; i
++)
453 VarDeclaration
*var
= base
->fields
[i
];
454 gcc_assert (var
->csym
!= NULL
);
460 /* Given a record type TYPE, whose size and alignment are determined by
461 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
462 the finalized record mode. */
465 finish_aggregate_type (unsigned structsize
, unsigned alignsize
, tree type
)
467 /* Set size and alignment as requested by frontend. */
468 TYPE_SIZE (type
) = bitsize_int (structsize
* BITS_PER_UNIT
);
469 TYPE_SIZE_UNIT (type
) = size_int (structsize
);
470 SET_TYPE_ALIGN (type
, alignsize
* BITS_PER_UNIT
);
471 TYPE_PACKED (type
) = (alignsize
== 1);
473 /* Set the back-end type mode. */
474 compute_record_mode (type
);
476 /* Fix up all variants of this aggregate type. */
477 for (tree t
= TYPE_MAIN_VARIANT (type
); t
; t
= TYPE_NEXT_VARIANT (t
))
482 TYPE_FIELDS (t
) = TYPE_FIELDS (type
);
483 TYPE_LANG_SPECIFIC (t
) = TYPE_LANG_SPECIFIC (type
);
484 SET_TYPE_ALIGN (t
, TYPE_ALIGN (type
));
485 TYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (type
);
486 gcc_assert (TYPE_MODE (t
) == TYPE_MODE (type
));
490 /* Returns true if the class or struct type TYPE has already been layed out by
491 the lowering of another front-end AST type. In which case, there will either
492 be a reuse of the back-end type, or a multiple definition error.
493 DECO is the uniquely mangled decoration for the type. */
496 merge_aggregate_types (Type
*type
, tree deco
)
498 AggregateDeclaration
*sym
;
500 if (type
->ty
== Tstruct
)
501 sym
= type
->isTypeStruct ()->sym
;
502 else if (type
->ty
== Tclass
)
503 sym
= type
->isTypeClass ()->sym
;
507 if (IDENTIFIER_DAGGREGATE (deco
))
509 AggregateDeclaration
*ad
= IDENTIFIER_DAGGREGATE (deco
);
510 /* There should never be a class/struct mismatch in mangled names. */
511 gcc_assert ((sym
->isStructDeclaration () && ad
->isStructDeclaration ())
512 || (sym
->isClassDeclaration () && ad
->isClassDeclaration ()));
514 /* Non-templated variables shouldn't be defined twice. */
515 if (!sym
->isInstantiated ())
516 ScopeDsymbol::multiplyDefined (sym
->loc
, sym
, ad
);
518 type
->ctype
= build_ctype (ad
->type
);
525 /* Implements the visitor interface to build the GCC trees of all
526 Type AST classes emitted from the D Front-end, where CTYPE holds
527 the cached back-end representation to be returned. */
529 class TypeVisitor
: public Visitor
531 using Visitor::visit
;
538 /* This should be overridden by each type class. */
545 /* Type assigned to erroneous expressions or constructs that
546 failed during the semantic stage. */
548 void visit (TypeError
*t
)
550 t
->ctype
= error_mark_node
;
553 /* Type assigned to generic nullable types. */
555 void visit (TypeNull
*t
)
557 t
->ctype
= ptr_type_node
;
561 /* Basic Data Types. */
563 void visit (TypeBasic
*t
)
565 /* [type/basic-data-types]
568 bool 8-bit boolean value.
569 byte 8-bit signed value.
570 ubyte 8-bit unsigned value.
571 short 16-bit signed value.
572 ushort 16-bit unsigned value.
573 int 32-bit signed value.
574 uint 32-bit unsigned value.
575 long 64-bit signed value.
576 ulong 64-bit unsigned value.
577 cent 128-bit signed value.
578 ucent 128-bit unsigned value.
579 float 32-bit IEEE 754 floating-point value.
580 double 64-bit IEEE 754 floating-point value.
581 real largest FP size implemented in hardware.
582 ifloat imaginary float.
583 idouble imaginary double.
584 ireal imaginary real.
585 cfloat complex float.
586 cdouble complex double.
588 char UTF-8 code unit.
589 wchar UTF-16 code unit.
590 dchar UTF-32 code unit. */
594 case Tvoid
: t
->ctype
= void_type_node
; break;
595 case Tbool
: t
->ctype
= d_bool_type
; break;
596 case Tint8
: t
->ctype
= d_byte_type
; break;
597 case Tuns8
: t
->ctype
= d_ubyte_type
; break;
598 case Tint16
: t
->ctype
= d_short_type
; break;
599 case Tuns16
: t
->ctype
= d_ushort_type
; break;
600 case Tint32
: t
->ctype
= d_int_type
; break;
601 case Tuns32
: t
->ctype
= d_uint_type
; break;
602 case Tint64
: t
->ctype
= d_long_type
; break;
603 case Tuns64
: t
->ctype
= d_ulong_type
; break;
604 case Tint128
: t
->ctype
= d_cent_type
; break;
605 case Tuns128
: t
->ctype
= d_ucent_type
; break;
606 case Tfloat32
: t
->ctype
= float_type_node
; break;
607 case Tfloat64
: t
->ctype
= double_type_node
; break;
608 case Tfloat80
: t
->ctype
= long_double_type_node
; break;
609 case Timaginary32
: t
->ctype
= ifloat_type_node
; break;
610 case Timaginary64
: t
->ctype
= idouble_type_node
; break;
611 case Timaginary80
: t
->ctype
= ireal_type_node
; break;
612 case Tcomplex32
: t
->ctype
= complex_float_type_node
; break;
613 case Tcomplex64
: t
->ctype
= complex_double_type_node
; break;
614 case Tcomplex80
: t
->ctype
= complex_long_double_type_node
; break;
615 case Tchar
: t
->ctype
= char8_type_node
; break;
616 case Twchar
: t
->ctype
= char16_type_node
; break;
617 case Tdchar
: t
->ctype
= char32_type_node
; break;
618 default: gcc_unreachable ();
621 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
625 /* Derived Data Types. */
627 /* Build a simple pointer to data type, analogous to C pointers. */
629 void visit (TypePointer
*t
)
631 t
->ctype
= build_pointer_type (build_ctype (t
->next
));
634 /* Build a dynamic array type, consisting of a length and a pointer
635 to the array data. */
637 void visit (TypeDArray
*t
)
639 /* In [abi/arrays], dynamic array layout is:
640 .length array dimension.
641 .ptr pointer to array data. */
642 t
->ctype
= make_struct_type (t
->toChars (), 2,
643 get_identifier ("length"),
644 build_ctype (Type::tsize_t
),
645 get_identifier ("ptr"),
646 build_pointer_type (build_ctype (t
->next
)));
647 TYPE_DYNAMIC_ARRAY (t
->ctype
) = 1;
648 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
652 /* Build a static array type, distinguished from dynamic arrays by
653 having a length fixed at compile-time, analogous to C arrays. */
655 void visit (TypeSArray
*t
)
657 if (t
->dim
->isConst () && t
->dim
->type
->isintegral ())
659 uinteger_t size
= t
->dim
->toUInteger ();
660 t
->ctype
= make_array_type (t
->next
, size
);
664 error ("invalid expression for static array dimension: %s",
670 /* Build a vector type, a fixed array of floating or integer types. */
672 void visit (TypeVector
*t
)
674 int nunits
= t
->basetype
->isTypeSArray ()->dim
->toUInteger ();
675 tree inner
= build_ctype (t
->elementType ());
677 /* Same rationale as void static arrays. */
678 if (inner
== void_type_node
)
679 inner
= build_ctype (Type::tuns8
);
681 t
->ctype
= build_vector_type (inner
, nunits
);
682 TYPE_NAME (t
->ctype
) = get_identifier (t
->toChars ());
683 layout_type (t
->ctype
);
686 /* Build an associative array type, distinguished from arrays by having an
687 index that's not necessarily an integer, and can be sparsely populated. */
689 void visit (TypeAArray
*t
)
691 /* In [abi/associative-arrays], associative arrays are a struct that only
692 consist of a pointer to an opaque, implementation defined type. */
693 t
->ctype
= make_struct_type (t
->toChars (), 1,
694 get_identifier ("ptr"), ptr_type_node
);
695 TYPE_ASSOCIATIVE_ARRAY (t
->ctype
) = 1;
696 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
700 /* Build type for a function declaration, which consists of a return type,
701 and a list of parameter types, and a linkage attribute. */
703 void visit (TypeFunction
*t
)
705 tree fnparams
= NULL_TREE
;
708 /* [function/variadic]
710 Variadic functions with D linkage have an additional hidden argument
711 with the name _arguments passed to the function. */
712 if (t
->isDstyleVariadic ())
714 tree type
= build_ctype (Type::typeinfotypelist
->type
);
715 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
718 size_t n_args
= t
->parameterList
.length ();
720 for (size_t i
= 0; i
< n_args
; i
++)
722 tree type
= parameter_type (t
->parameterList
[i
]);
723 fnparams
= chainon (fnparams
, build_tree_list (0, type
));
726 /* When the last parameter is void_list_node, that indicates a fixed length
727 parameter list, otherwise function is treated as variadic. */
728 if (t
->parameterList
.varargs
!= VARARGvariadic
)
729 fnparams
= chainon (fnparams
, void_list_node
);
733 fntype
= build_ctype (t
->next
);
735 fntype
= build_reference_type (fntype
);
738 fntype
= void_type_node
;
740 /* Could the function type be self referenced by parameters? */
741 t
->ctype
= build_function_type (fntype
, fnparams
);
742 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
745 /* Handle any special support for calling conventions. */
750 /* [attribute/linkage]
752 The Windows convention is distinct from the C convention only
753 on Win32, where it is equivalent to the stdcall convention. */
754 if (!global
.params
.is64bit
)
755 t
->ctype
= insert_type_attribute (t
->ctype
, "stdcall");
762 /* [abi/function-calling-conventions]
764 The extern (C) and extern (D) calling convention matches
765 the C calling convention used by the supported C compiler
766 on the host system. */
774 /* Build a delegate type, an aggregate of two pieces of data, an object
775 reference and a pointer to a non-static member function, or a pointer
776 to a closure and a pointer to a nested function. */
778 void visit (TypeDelegate
*t
)
780 /* In [abi/delegates], delegate layout is:
781 .ptr context pointer.
782 .funcptr pointer to function. */
783 tree fntype
= build_ctype (t
->next
);
784 tree dgtype
= build_vthis_function (void_type_node
, fntype
);
786 TYPE_ATTRIBUTES (dgtype
) = TYPE_ATTRIBUTES (fntype
);
787 TYPE_LANG_SPECIFIC (dgtype
) = TYPE_LANG_SPECIFIC (fntype
);
789 t
->ctype
= make_struct_type (t
->toChars (), 2,
790 get_identifier ("ptr"),
791 build_ctype (Type::tvoidptr
),
792 get_identifier ("funcptr"),
793 build_pointer_type (dgtype
));
794 TYPE_DELEGATE (t
->ctype
) = 1;
795 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
800 /* User Defined Types. */
802 /* Build a named enum type, a distinct value whose values are restrict to
803 a group of constants of the same underlying base type. */
805 void visit (TypeEnum
*t
)
807 tree basetype
= (t
->sym
->memtype
)
808 ? build_ctype (t
->sym
->memtype
) : void_type_node
;
810 if (!INTEGRAL_TYPE_P (basetype
) || TREE_CODE (basetype
) == BOOLEAN_TYPE
)
812 /* Enums in D2 can have a base type that is not necessarily integral.
813 For these, we simplify this a little by using the base type directly
814 instead of building an ENUMERAL_TYPE. */
815 t
->ctype
= build_variant_type_copy (basetype
);
819 t
->ctype
= make_node (ENUMERAL_TYPE
);
820 ENUM_IS_SCOPED (t
->ctype
) = 1;
821 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
824 if (flag_short_enums
)
825 TYPE_PACKED (t
->ctype
) = 1;
827 TYPE_PRECISION (t
->ctype
) = t
->size (t
->sym
->loc
) * 8;
828 TYPE_SIZE (t
->ctype
) = 0;
830 TYPE_MIN_VALUE (t
->ctype
) = TYPE_MIN_VALUE (basetype
);
831 TYPE_MAX_VALUE (t
->ctype
) = TYPE_MAX_VALUE (basetype
);
832 layout_type (t
->ctype
);
834 tree values
= NULL_TREE
;
837 for (size_t i
= 0; i
< t
->sym
->members
->length
; i
++)
839 EnumMember
*member
= (*t
->sym
->members
)[i
]->isEnumMember ();
840 /* Templated functions can seep through to the back-end
841 just ignore for now. */
845 tree ident
= get_identifier (member
->ident
->toChars ());
846 tree value
= build_integer_cst (member
->value ()->toInteger (),
849 /* Build an identifier for the enumeration constant. */
850 tree decl
= build_decl (make_location_t (member
->loc
),
851 CONST_DECL
, ident
, basetype
);
852 DECL_CONTEXT (decl
) = t
->ctype
;
853 TREE_CONSTANT (decl
) = 1;
854 TREE_READONLY (decl
) = 1;
855 DECL_INITIAL (decl
) = value
;
857 /* Add this enumeration constant to the list for this type. */
858 values
= chainon (values
, build_tree_list (ident
, decl
));
862 TYPE_VALUES (t
->ctype
) = values
;
863 TYPE_UNSIGNED (t
->ctype
) = TYPE_UNSIGNED (basetype
);
864 build_type_decl (t
->ctype
, t
->sym
);
867 apply_user_attributes (t
->sym
, t
->ctype
);
870 /* Build a struct or union type. Layout should be exactly represented
871 as an equivalent C struct, except for non-POD or nested structs. */
873 void visit (TypeStruct
*t
)
875 /* Merge types in the back-end if the front-end did not itself do so. */
876 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
877 if (merge_aggregate_types (t
, deco
))
880 /* Need to set this right away in case of self-references. */
881 t
->ctype
= make_node (t
->sym
->isUnionDeclaration ()
882 ? UNION_TYPE
: RECORD_TYPE
);
884 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
886 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
887 TYPE_CXX_ODR_P (t
->ctype
) = 1;
891 /* Must set up the overall size and alignment before determining
892 the context or laying out fields as those types may make
893 recursive references to this type. */
894 unsigned structsize
= t
->sym
->structsize
;
895 unsigned alignsize
= (t
->sym
->alignment
!= STRUCTALIGN_DEFAULT
)
896 ? t
->sym
->alignment
: t
->sym
->alignsize
;
898 TYPE_SIZE (t
->ctype
) = bitsize_int (structsize
* BITS_PER_UNIT
);
899 TYPE_SIZE_UNIT (t
->ctype
) = size_int (structsize
);
900 SET_TYPE_ALIGN (t
->ctype
, alignsize
* BITS_PER_UNIT
);
901 TYPE_PACKED (t
->ctype
) = (alignsize
== 1);
902 compute_record_mode (t
->ctype
);
904 /* Put out all fields. */
905 layout_aggregate_type (t
->sym
, t
->ctype
, t
->sym
);
906 apply_user_attributes (t
->sym
, t
->ctype
);
907 finish_aggregate_type (structsize
, alignsize
, t
->ctype
);
910 TYPE_CONTEXT (t
->ctype
) = d_decl_context (t
->sym
);
911 build_type_decl (t
->ctype
, t
->sym
);
913 /* For structs with a user defined postblit or a destructor,
914 also set TREE_ADDRESSABLE on the type and all variants.
915 This will make the struct be passed around by reference. */
916 if (!t
->sym
->isPOD ())
918 for (tree tv
= t
->ctype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
919 TREE_ADDRESSABLE (tv
) = 1;
923 /* Build a class type. Whereas structs are value types, classes are
924 reference types, with all the object-orientated features. */
926 void visit (TypeClass
*t
)
928 /* Merge types in the back-end if the front-end did not itself do so. */
929 tree deco
= get_identifier (d_mangle_decl (t
->sym
));
930 if (merge_aggregate_types (t
, deco
))
933 /* Need to set ctype right away in case of self-references to
934 the type during this call. */
935 tree basetype
= make_node (RECORD_TYPE
);
936 t
->ctype
= build_pointer_type (basetype
);
938 IDENTIFIER_DAGGREGATE (deco
) = t
->sym
;
940 /* Note that lang_specific data is assigned to both the reference
941 and the underlying record type. */
942 TYPE_LANG_SPECIFIC (t
->ctype
) = build_lang_type (t
);
943 TYPE_LANG_SPECIFIC (basetype
) = TYPE_LANG_SPECIFIC (t
->ctype
);
944 CLASS_TYPE_P (basetype
) = 1;
945 TYPE_CXX_ODR_P (basetype
) = 1;
947 /* Put out all fields, including from each base class. */
948 layout_aggregate_type (t
->sym
, basetype
, t
->sym
);
949 apply_user_attributes (t
->sym
, basetype
);
950 finish_aggregate_type (t
->sym
->structsize
, t
->sym
->alignsize
, basetype
);
952 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
953 for (tree tv
= basetype
; tv
!= NULL_TREE
; tv
= TYPE_NEXT_VARIANT (tv
))
954 TREE_ADDRESSABLE (tv
) = 1;
956 /* Type is final, there are no derivations. */
957 if (t
->sym
->storage_class
& STCfinal
)
958 TYPE_FINAL_P (basetype
) = 1;
960 /* Create BINFO even if debugging is off. This is needed to keep
961 references to inherited types. */
962 if (!t
->sym
->isInterfaceDeclaration ())
963 TYPE_BINFO (basetype
) = build_class_binfo (NULL_TREE
, t
->sym
);
968 TYPE_BINFO (basetype
) = build_interface_binfo (NULL_TREE
, t
->sym
,
972 /* Associate all virtual methods with the class too. */
973 for (size_t i
= 0; i
< t
->sym
->vtbl
.length
; i
++)
975 FuncDeclaration
*fd
= t
->sym
->vtbl
[i
]->isFuncDeclaration ();
976 tree method
= fd
? get_symbol_decl (fd
) : error_mark_node
;
978 if (!error_operand_p (method
)
979 && DECL_CONTEXT (method
) == basetype
980 && !chain_member (method
, TYPE_FIELDS (basetype
)))
981 TYPE_FIELDS (basetype
) = chainon (TYPE_FIELDS (basetype
), method
);
984 TYPE_CONTEXT (basetype
) = d_decl_context (t
->sym
);
985 build_type_decl (basetype
, t
->sym
);
990 /* Build a tree from a frontend Type. */
993 build_ctype (Type
*t
)
999 /* Strip const modifiers from type before building. This is done
1000 to ensure that back-end treats e.g: const (T) as a variant of T,
1001 and not as two distinct types. */
1006 Type
*tb
= t
->castMod (0);
1009 t
->ctype
= insert_type_modifiers (tb
->ctype
, t
->mod
);