d: Fix wrong code-gen when returning structs by value.
[official-gcc.git] / gcc / d / types.cc
blobef2d80e5bd4cb84da0594d6482d3868d5c111b66
1 /* types.cc -- Lower D frontend types to GCC trees.
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
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/>. */
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
22 #include "dmd/attrib.h"
23 #include "dmd/aggregate.h"
24 #include "dmd/enum.h"
25 #include "dmd/expression.h"
26 #include "dmd/identifier.h"
27 #include "dmd/mtype.h"
28 #include "dmd/target.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "diagnostic.h"
33 #include "langhooks.h"
34 #include "tm.h"
35 #include "function.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "stringpool.h"
39 #include "stor-layout.h"
40 #include "attribs.h"
42 #include "d-tree.h"
43 #include "d-target.h"
46 /* Return the signed or unsigned version of TYPE, an integral type, the
47 signedness being specified by UNSIGNEDP. */
49 static tree
50 d_signed_or_unsigned_type (int unsignedp, tree type)
52 if (VECTOR_TYPE_P (type) || !ANY_INTEGRAL_TYPE_P (type))
53 return signed_or_unsigned_type_for (unsignedp, type);
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. */
75 tree
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. */
83 tree
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. */
93 bool
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))
101 return true;
104 return false;
107 /* Returns true if TYPE contains no actual data, just various
108 possible combinations of empty aggregates. */
110 bool
111 empty_aggregate_p (tree type)
113 if (!AGGREGATE_TYPE_P (type))
114 return false;
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)))
125 return false;
128 return true;
131 /* Returns true if T1 and T2 are related to each other. */
133 bool
134 same_type_p (Type *t1, Type *t2)
136 /* Types are equal. */
137 if (t1 == t2)
138 return true;
140 /* Types derive from the same base. */
141 Type *tb1 = t1->toBasetype ();
142 Type *tb2 = t2->toBasetype ();
143 if (tb1 == tb2)
144 return true;
146 /* Types are mutably the same type. */
147 if (tb1->ty == tb2->ty && tb1->equivalent (tb2))
148 return true;
150 return false;
153 /* Returns `Object' type which all D classes are derived from. */
155 Type *
156 get_object_type (void)
158 if (ClassDeclaration::object)
159 return ClassDeclaration::object->type;
161 error ("missing or corrupt object.d");
162 return Type::terror;
166 /* Returns a static array of TYPE which has SIZE number of elements. */
168 tree
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)
174 type = Type::tuns8;
176 /* In [arrays/static-arrays], a static array with a dimension of 0 is allowed,
177 but no space is allocated for it. */
178 if (size == 0)
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;
187 return t;
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));
194 return t;
197 /* Builds a record type whose name is NAME. NFIELDS is the number of fields,
198 provided as field ident/type pairs. */
200 tree
201 make_struct_type (const char *name, int nfields, ...)
203 tree fields = NULL_TREE;
204 va_list ap;
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;
214 fields = field;
217 va_end (ap);
219 tree type = make_node (RECORD_TYPE);
220 finish_builtin_struct (type, name, fields, NULL_TREE);
222 return type;
225 /* Return qualified type variant of TYPE determined by modifier value MOD. */
227 tree
228 insert_type_modifiers (tree type, unsigned mod)
230 int quals = 0;
232 switch (mod)
234 case MODconst:
235 case MODwild:
236 case MODwildconst:
237 case MODimmutable:
238 case MODshared | MODconst:
239 case MODshared | MODwild:
240 case MODshared | MODwildconst:
241 quals |= TYPE_QUAL_CONST;
242 break;
244 case 0:
245 case MODshared:
246 break;
248 default:
249 gcc_unreachable ();
252 tree qualtype = build_qualified_type (type, quals);
254 /* Mark whether the type is qualified `shared'. */
255 if (mod & MODshared)
256 TYPE_SHARED (qualtype) = 1;
258 return qualtype;
261 /* Adds FIELD into the aggregate TYPE at OFFSET. */
263 void
264 insert_aggregate_field (tree type, tree field, size_t offset)
266 DECL_FIELD_CONTEXT (field) = type;
267 SET_DECL_OFFSET_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
268 DECL_FIELD_OFFSET (field) = size_int (offset);
269 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
271 TREE_ADDRESSABLE (field) = TYPE_SHARED (TREE_TYPE (field));
273 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), field);
276 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
278 static tree
279 d_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
281 /* Same as d_type_for_size, but uses exact match for size. */
282 if (width == TYPE_PRECISION (d_byte_type))
283 return unsignedp ? d_ubyte_type : d_byte_type;
285 if (width == TYPE_PRECISION (d_short_type))
286 return unsignedp ? d_ushort_type : d_short_type;
288 if (width == TYPE_PRECISION (d_int_type))
289 return unsignedp ? d_uint_type : d_int_type;
291 if (width == TYPE_PRECISION (d_long_type))
292 return unsignedp ? d_ulong_type : d_long_type;
294 if (width == TYPE_PRECISION (d_cent_type))
295 return unsignedp ? d_ucent_type : d_cent_type;
297 for (int i = 0; i < NUM_INT_N_ENTS; i ++)
299 if (int_n_enabled_p[i] && width == int_n_data[i].bitsize)
301 if (unsignedp)
302 return int_n_trees[i].unsigned_type;
303 else
304 return int_n_trees[i].signed_type;
308 return build_nonstandard_integer_type (width, unsignedp);
311 /* Adds BITFIELD into the aggregate TYPE at OFFSET+BITOFFSET. */
313 static void
314 insert_aggregate_bitfield (tree type, tree bitfield, size_t width,
315 size_t offset, size_t bitoffset)
317 DECL_FIELD_CONTEXT (bitfield) = type;
318 SET_DECL_OFFSET_ALIGN (bitfield, TYPE_ALIGN (TREE_TYPE (bitfield)));
319 DECL_SIZE (bitfield) = bitsize_int (width);
320 DECL_FIELD_OFFSET (bitfield) = size_int (offset);
321 DECL_FIELD_BIT_OFFSET (bitfield) = bitsize_int (bitoffset);
323 TREE_ADDRESSABLE (bitfield) = TYPE_SHARED (TREE_TYPE (bitfield));
325 DECL_BIT_FIELD (bitfield) = 1;
326 DECL_BIT_FIELD_TYPE (bitfield) = TREE_TYPE (bitfield);
328 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), bitfield);
331 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET.
332 This is done as the frontend puts fields into the outer struct, and so
333 their offset is from the beginning of the aggregate.
334 We want the offset to be from the beginning of the anonymous aggregate. */
336 static void
337 fixup_anonymous_offset (tree fields, tree offset)
339 /* No adjustment in field offset required. */
340 if (integer_zerop (offset))
341 return;
343 while (fields != NULL_TREE)
345 /* Traverse all nested anonymous aggregates to update the offset of their
346 fields. Note that the anonymous field itself is not adjusted, as it
347 already has an offset relative to its outer aggregate. */
348 tree ftype = TREE_TYPE (fields);
349 if (TYPE_NAME (ftype) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype)))
351 tree vfields = TYPE_FIELDS (ftype);
352 fixup_anonymous_offset (vfields, offset);
354 else
356 tree voffset = DECL_FIELD_OFFSET (fields);
357 DECL_FIELD_OFFSET (fields) = size_binop (MINUS_EXPR, voffset, offset);
360 fields = DECL_CHAIN (fields);
364 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT.
365 If INHERITED_P is true, then the members derive from a base class.
366 Returns the number of named fields found. */
368 static size_t
369 layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p)
371 size_t fields = 0;
373 for (size_t i = 0; i < members->length; i++)
375 Dsymbol *sym = (*members)[i];
376 VarDeclaration *var = sym->isVarDeclaration ();
377 if (var != NULL)
379 /* Skip fields that have already been added. */
380 if (!inherited_p && var->csym != NULL)
381 continue;
383 /* If this variable was really a tuple, add all tuple fields. */
384 if (var->aliasTuple)
386 TupleDeclaration *td = var->aliasTuple;
387 Dsymbols tmembers;
388 /* No other way to coerce the underlying type out of the tuple.
389 Frontend should have already validated this. */
390 for (size_t j = 0; j < td->objects->length; j++)
392 RootObject *ro = (*td->objects)[j];
393 gcc_assert (ro->dyncast () == DYNCAST_EXPRESSION);
394 Expression *e = (Expression *) ro;
395 gcc_assert (e->op == EXP::variable);
396 VarExp *ve = e->isVarExp ();
398 tmembers.push (ve->var);
401 fields += layout_aggregate_members (&tmembers, context,
402 inherited_p);
403 continue;
406 /* Insert the field declaration at its given offset. */
407 if (var->isField ())
409 const char *ident = (var->ident && !var->ident->isAnonymous ())
410 ? var->ident->toChars () : NULL;
411 tree field = create_field_decl (declaration_type (var), ident,
412 inherited_p, inherited_p);
413 apply_user_attributes (var, field);
415 if (BitFieldDeclaration *bf = var->isBitFieldDeclaration ())
417 /* Bit-fields come from an ImportC context, and require the
418 field be correctly adjusted. */
419 insert_aggregate_bitfield (context, field, bf->fieldWidth,
420 bf->offset, bf->bitOffset);
422 else
423 insert_aggregate_field (context, field, var->offset);
425 /* Because the front-end shares field decls across classes, don't
426 create the corresponding back-end symbol unless we are adding
427 it to the aggregate it is defined in. */
428 if (!inherited_p)
430 DECL_LANG_SPECIFIC (field) = build_lang_decl (var);
431 var->csym = field;
434 /* Only count the named fields in an aggregate. */
435 if (ident != NULL)
436 fields += 1;
438 continue;
442 /* Anonymous struct/union are flattened by the frontend. However, we
443 want to keep the record layout in-tact when building the type. */
444 AnonDeclaration *ad = sym->isAnonDeclaration ();
445 if (ad != NULL)
447 tree ident = make_anon_name ();
448 tree type = make_node (ad->isunion ? UNION_TYPE : RECORD_TYPE);
449 ANON_AGGR_TYPE_P (type) = 1;
450 d_keep (type);
452 /* Build the type declaration. */
453 tree decl = build_decl (make_location_t (ad->loc),
454 TYPE_DECL, ident, type);
455 DECL_CONTEXT (decl) = context;
456 DECL_ARTIFICIAL (decl) = 1;
458 TYPE_CONTEXT (type) = context;
459 TYPE_NAME (type) = decl;
460 TYPE_STUB_DECL (type) = decl;
462 /* Recursively iterator over the anonymous members. */
463 fields += layout_aggregate_members (ad->decl, type, inherited_p);
465 /* Remove from the anon fields the base offset of this anonymous
466 aggregate. Undoes what is set-up in setFieldOffset, but doesn't
467 affect field accesses. */
468 tree offset = size_int (ad->anonoffset);
469 fixup_anonymous_offset (TYPE_FIELDS (type), offset);
471 finish_aggregate_type (ad->anonstructsize, ad->anonalignsize, type);
473 /* And make the corresponding data member. */
474 tree field = create_field_decl (type, NULL, 0, 0);
475 apply_user_attributes (ad, field);
476 insert_aggregate_field (context, field, ad->anonoffset);
477 continue;
480 /* Other kinds of attributes don't create a scope. */
481 AttribDeclaration *attrib = sym->isAttribDeclaration ();
482 if (attrib != NULL)
484 Dsymbols *decls = attrib->include (NULL);
485 if (decls != NULL)
487 fields += layout_aggregate_members (decls, context, inherited_p);
488 continue;
492 /* Same with template mixins and namespaces. */
493 if (sym->isTemplateMixin () || sym->isNspace ())
495 ScopeDsymbol *scopesym = sym->isScopeDsymbol ();
496 if (scopesym->members)
498 fields += layout_aggregate_members (scopesym->members, context,
499 inherited_p);
500 continue;
505 return fields;
508 /* Write out all fields for aggregate BASE. For classes, write out all
509 interfaces first, then the base class fields. */
511 static void
512 layout_aggregate_type (AggregateDeclaration *decl, tree type,
513 AggregateDeclaration *base)
515 ClassDeclaration *cd = base->isClassDeclaration ();
516 bool inherited_p = (decl != base);
518 if (cd != NULL)
520 if (cd->baseClass)
521 layout_aggregate_type (decl, type, cd->baseClass);
522 else
524 /* This is the base class (Object) or interface. */
525 tree objtype = TREE_TYPE (build_ctype (cd->type));
527 /* Add the vtable pointer, and optionally the monitor fields. */
528 InterfaceDeclaration *id = cd->isInterfaceDeclaration ();
529 if (!id || id->vtblInterfaces->length == 0)
531 tree field = create_field_decl (vtbl_ptr_type_node, "__vptr", 1,
532 inherited_p);
533 DECL_VIRTUAL_P (field) = 1;
534 TYPE_VFIELD (type) = field;
535 DECL_FCONTEXT (field) = objtype;
536 insert_aggregate_field (type, field, 0);
539 if (!id && cd->hasMonitor ())
541 tree field = create_field_decl (ptr_type_node, "__monitor", 1,
542 inherited_p);
543 insert_aggregate_field (type, field, target.ptrsize);
547 if (cd->vtblInterfaces)
549 for (size_t i = 0; i < cd->vtblInterfaces->length; i++)
551 BaseClass *bc = (*cd->vtblInterfaces)[i];
552 tree field = create_field_decl (vtbl_ptr_type_node, NULL, 1, 1);
553 insert_aggregate_field (type, field, bc->offset);
558 if (base->members)
560 size_t fields = layout_aggregate_members (base->members, type,
561 inherited_p);
562 gcc_assert (fields == base->fields.length);
564 /* Make sure that all fields have been created. */
565 if (!inherited_p)
567 for (size_t i = 0; i < base->fields.length; i++)
569 VarDeclaration *var = base->fields[i];
570 gcc_assert (var->csym != NULL);
576 /* Given a record type TYPE compute the finalized record mode if all fields have
577 had their types resolved and sizes determined. */
579 void
580 finish_aggregate_mode (tree type)
582 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
584 /* Fields of type `typeof(*null)' have no size, so let them force the
585 record type mode to be computed as BLKmode. */
586 if (TYPE_MAIN_VARIANT (TREE_TYPE (field)) == noreturn_type_node)
587 break;
589 if (DECL_SIZE (field) == NULL_TREE)
590 return;
593 compute_record_mode (type);
595 /* Propagate computed mode to all variants of this aggregate type. */
596 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
598 if (t == type)
599 continue;
601 SET_TYPE_MODE (t, TYPE_MODE (type));
605 /* If the aggregate type TYPE completes the type of any previous field
606 declarations, lay them out now. */
608 static void
609 finish_incomplete_fields (tree type)
611 for (tree fwdref = TYPE_FORWARD_REFERENCES (type); fwdref != NULL_TREE;
612 fwdref = TREE_CHAIN (fwdref))
614 tree field = TREE_VALUE (fwdref);
615 tree basetype = TREE_TYPE (field);
617 /* Arrays of TYPE have layout_type() called from build_array_type(), but
618 would skip over setting TYPE_SIZE. Try completing the type again. */
619 if (TREE_CODE (basetype) == ARRAY_TYPE)
621 while (TREE_CODE (TREE_TYPE (basetype)) == ARRAY_TYPE)
622 basetype = TREE_TYPE (basetype);
624 layout_type (basetype);
627 relayout_decl (field);
629 /* Relayout of field may change the mode of its RECORD_TYPE. */
630 finish_aggregate_mode (DECL_FIELD_CONTEXT (field));
633 /* No more forward references to process. */
634 TYPE_FORWARD_REFERENCES (type) = NULL_TREE;
637 /* Given a record type TYPE, whose size and alignment are determined by
638 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
639 the finalized record mode. */
641 void
642 finish_aggregate_type (unsigned structsize, unsigned alignsize, tree type)
644 /* Set size and alignment as requested by frontend. */
645 TYPE_SIZE (type) = bitsize_int (structsize * BITS_PER_UNIT);
646 TYPE_SIZE_UNIT (type) = size_int (structsize);
647 SET_TYPE_ALIGN (type, alignsize * BITS_PER_UNIT);
648 TYPE_PACKED (type) = (alignsize == 1);
650 /* Layout all fields now the type is complete. */
651 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
653 /* If the field type is still being constructed because of recursive
654 references, attach it to that class/struct type, so we can go back
655 and complete the field later. */
656 if (!COMPLETE_TYPE_P (TREE_TYPE (field)))
658 tree basetype = TREE_TYPE (field);
659 while (TREE_CODE (basetype) == ARRAY_TYPE)
660 basetype = TREE_TYPE (basetype);
662 basetype = TYPE_MAIN_VARIANT (basetype);
663 if (RECORD_OR_UNION_TYPE_P (basetype)
664 || TREE_CODE (basetype) == ENUMERAL_TYPE)
666 gcc_assert (!COMPLETE_TYPE_P (basetype));
667 tree fwdrefs = tree_cons (NULL_TREE, field,
668 TYPE_FORWARD_REFERENCES (basetype));
669 TYPE_FORWARD_REFERENCES (basetype) = fwdrefs;
672 continue;
675 layout_decl (field, 0);
677 /* Give bit-field its proper type after layout_decl. */
678 if (DECL_BIT_FIELD (field))
680 tree orig_type = DECL_BIT_FIELD_TYPE (field);
681 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_SIZE (field));
683 if (width != TYPE_PRECISION (orig_type))
685 bool unsignedp = TYPE_UNSIGNED (orig_type);
687 TREE_TYPE (field)
688 = d_build_bitfield_integer_type (width, unsignedp);
689 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
694 /* Set the back-end type mode after all fields have had their size set. */
695 finish_aggregate_mode (type);
697 /* Fix up all forward-referenced variants of this aggregate type. */
698 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
700 if (t == type)
701 continue;
703 TYPE_FIELDS (t) = TYPE_FIELDS (type);
704 TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (type);
705 TYPE_SIZE (t) = TYPE_SIZE (type);
706 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
707 TYPE_PACKED (type) = TYPE_PACKED (type);
708 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
709 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
712 /* Finish debugging output for this type. */
713 rest_of_type_compilation (type, TYPE_FILE_SCOPE_P (type));
714 finish_incomplete_fields (type);
716 /* Finish processing of TYPE_DECL. */
717 rest_of_decl_compilation (TYPE_NAME (type),
718 DECL_FILE_SCOPE_P (TYPE_NAME (type)), 0);
721 /* Returns true if the class or struct type TYPE has already been layed out by
722 the lowering of another front-end AST type. In which case, there will either
723 be a reuse of the back-end type, or a multiple definition error.
724 DECO is the uniquely mangled decoration for the type. */
726 static bool
727 merge_aggregate_types (Type *type, tree deco)
729 AggregateDeclaration *sym;
731 if (type->ty == TY::Tstruct)
732 sym = type->isTypeStruct ()->sym;
733 else if (type->ty == TY::Tclass)
734 sym = type->isTypeClass ()->sym;
735 else
736 gcc_unreachable ();
738 if (IDENTIFIER_DAGGREGATE (deco))
740 AggregateDeclaration *ad = IDENTIFIER_DAGGREGATE (deco);
741 /* There should never be a class/struct mismatch in mangled names. */
742 gcc_assert ((sym->isStructDeclaration () && ad->isStructDeclaration ())
743 || (sym->isClassDeclaration () && ad->isClassDeclaration ()));
745 /* Non-templated variables shouldn't be defined twice. */
746 if (!sym->isInstantiated ())
747 ScopeDsymbol::multiplyDefined (sym->loc, sym, ad);
749 type->ctype = build_ctype (ad->type);
750 return true;
753 return false;
756 /* Implements the visitor interface to build the GCC trees of all
757 Type AST classes emitted from the D Front-end, where CTYPE holds
758 the cached back-end representation to be returned. */
760 class TypeVisitor : public Visitor
762 using Visitor::visit;
764 public:
765 TypeVisitor (void)
769 /* This should be overridden by each type class. */
771 void visit (Type *) final override
773 gcc_unreachable ();
776 /* Type assigned to erroneous expressions or constructs that
777 failed during the semantic stage. */
779 void visit (TypeError *t) final override
781 t->ctype = error_mark_node;
784 /* Type assigned to generic nullable types. */
786 void visit (TypeNull *t) final override
788 t->ctype = ptr_type_node;
791 /* Bottom type used for functions that never return. */
793 void visit (TypeNoreturn *t) final override
795 t->ctype = noreturn_type_node;
796 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
799 /* Basic Data Types. */
801 void visit (TypeBasic *t) final override
803 /* [type/basic-data-types]
805 void no type.
806 bool 8-bit boolean value.
807 byte 8-bit signed value.
808 ubyte 8-bit unsigned value.
809 short 16-bit signed value.
810 ushort 16-bit unsigned value.
811 int 32-bit signed value.
812 uint 32-bit unsigned value.
813 long 64-bit signed value.
814 ulong 64-bit unsigned value.
815 cent 128-bit signed value.
816 ucent 128-bit unsigned value.
817 float 32-bit IEEE 754 floating-point value.
818 double 64-bit IEEE 754 floating-point value.
819 real largest FP size implemented in hardware.
820 ifloat imaginary float.
821 idouble imaginary double.
822 ireal imaginary real.
823 cfloat complex float.
824 cdouble complex double.
825 creal complex real.
826 char UTF-8 code unit.
827 wchar UTF-16 code unit.
828 dchar UTF-32 code unit. */
830 switch (t->ty)
832 case TY::Tvoid: t->ctype = void_type_node; break;
833 case TY::Tbool: t->ctype = d_bool_type; break;
834 case TY::Tint8: t->ctype = d_byte_type; break;
835 case TY::Tuns8: t->ctype = d_ubyte_type; break;
836 case TY::Tint16: t->ctype = d_short_type; break;
837 case TY::Tuns16: t->ctype = d_ushort_type; break;
838 case TY::Tint32: t->ctype = d_int_type; break;
839 case TY::Tuns32: t->ctype = d_uint_type; break;
840 case TY::Tint64: t->ctype = d_long_type; break;
841 case TY::Tuns64: t->ctype = d_ulong_type; break;
842 case TY::Tint128: t->ctype = d_cent_type; break;
843 case TY::Tuns128: t->ctype = d_ucent_type; break;
844 case TY::Tfloat32: t->ctype = float_type_node; break;
845 case TY::Tfloat64: t->ctype = double_type_node; break;
846 case TY::Tfloat80: t->ctype = long_double_type_node; break;
847 case TY::Timaginary32: t->ctype = ifloat_type_node; break;
848 case TY::Timaginary64: t->ctype = idouble_type_node; break;
849 case TY::Timaginary80: t->ctype = ireal_type_node; break;
850 case TY::Tcomplex32: t->ctype = complex_float_type_node; break;
851 case TY::Tcomplex64: t->ctype = complex_double_type_node; break;
852 case TY::Tcomplex80: t->ctype = complex_long_double_type_node; break;
853 case TY::Tchar: t->ctype = char8_type_node; break;
854 case TY::Twchar: t->ctype = char16_type_node; break;
855 case TY::Tdchar: t->ctype = char32_type_node; break;
856 default: gcc_unreachable ();
859 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
863 /* Derived Data Types. */
865 /* Build a simple pointer to data type, analogous to C pointers. */
867 void visit (TypePointer *t) final override
869 t->ctype = build_pointer_type (build_ctype (t->next));
872 /* Build a dynamic array type, consisting of a length and a pointer
873 to the array data. */
875 void visit (TypeDArray *t) final override
877 /* In [abi/arrays], dynamic array layout is:
878 .length array dimension.
879 .ptr pointer to array data. */
880 t->ctype = make_struct_type (t->toChars (), 2,
881 get_identifier ("length"),
882 build_ctype (Type::tsize_t),
883 get_identifier ("ptr"),
884 build_pointer_type (build_ctype (t->next)));
885 TYPE_DYNAMIC_ARRAY (t->ctype) = 1;
886 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
887 d_keep (t->ctype);
890 /* Build a static array type, distinguished from dynamic arrays by
891 having a length fixed at compile-time, analogous to C arrays. */
893 void visit (TypeSArray *t) final override
895 if (t->dim->isConst () && t->dim->type->isintegral ())
897 uinteger_t size = t->dim->toUInteger ();
898 t->ctype = make_array_type (t->next, size);
900 else
902 error ("invalid expression for static array dimension: %s",
903 t->dim->toChars ());
904 gcc_unreachable ();
908 /* Build a vector type, a fixed array of floating or integer types. */
910 void visit (TypeVector *t) final override
912 int nunits = t->basetype->isTypeSArray ()->dim->toUInteger ();
913 tree inner = build_ctype (t->elementType ());
915 /* Same rationale as void static arrays. */
916 if (inner == void_type_node)
917 inner = build_ctype (Type::tuns8);
919 t->ctype = build_vector_type (inner, nunits);
920 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
921 layout_type (t->ctype);
924 /* Build an associative array type, distinguished from arrays by having an
925 index that's not necessarily an integer, and can be sparsely populated. */
927 void visit (TypeAArray *t) final override
929 /* In [abi/associative-arrays], associative arrays are a struct that only
930 consist of a pointer to an opaque, implementation defined type. */
931 t->ctype = make_struct_type (t->toChars (), 1,
932 get_identifier ("ptr"), ptr_type_node);
933 TYPE_ASSOCIATIVE_ARRAY (t->ctype) = 1;
934 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
935 d_keep (t->ctype);
938 /* Build type for a function declaration, which consists of a return type,
939 and a list of parameter types, and a linkage attribute. */
941 void visit (TypeFunction *t) final override
943 tree fnparams = NULL_TREE;
944 tree fntype;
946 /* [function/variadic]
948 Variadic functions with D linkage have an additional hidden argument
949 with the name _arguments passed to the function. */
950 if (t->isDstyleVariadic ())
952 tree type = build_ctype (Type::typeinfotypelist->type);
953 fnparams = chainon (fnparams, build_tree_list (0, type));
956 const size_t n_args = t->parameterList.length ();
958 for (size_t i = 0; i < n_args; i++)
960 tree type = parameter_type (t->parameterList[i]);
962 /* Type `noreturn` is a terminator, as no other arguments can possibly
963 be evaluated after it. */
964 if (type == noreturn_type_node)
965 break;
967 fnparams = chainon (fnparams, build_tree_list (0, type));
970 /* When the last parameter is void_list_node, that indicates a fixed length
971 parameter list, otherwise function is treated as variadic. */
972 if (t->parameterList.varargs != VARARGvariadic)
973 fnparams = chainon (fnparams, void_list_node);
975 if (t->next != NULL)
977 fntype = build_ctype (t->next);
978 if (t->isref ())
979 fntype = build_reference_type (fntype);
981 else
982 fntype = void_type_node;
984 /* Could the function type be self referenced by parameters? */
985 t->ctype = build_function_type (fntype, fnparams);
986 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
987 d_keep (t->ctype);
989 /* Qualify function types that have the type `noreturn` as volatile. */
990 if (fntype == noreturn_type_node)
991 t->ctype = build_qualified_type (t->ctype, TYPE_QUAL_VOLATILE);
993 /* Handle any special support for calling conventions. */
994 switch (t->linkage)
996 case LINK::windows:
998 /* [attribute/linkage]
1000 The Windows convention is distinct from the C convention only
1001 on Win32, where it is equivalent to the stdcall convention. */
1002 unsigned link_system, link_windows;
1003 if (targetdm.d_has_stdcall_convention (&link_system, &link_windows))
1005 if (link_windows)
1006 t->ctype = insert_type_attribute (t->ctype, "stdcall");
1008 break;
1011 case LINK::c:
1012 case LINK::cpp:
1013 case LINK::d:
1014 case LINK::objc:
1015 /* [abi/function-calling-conventions]
1017 The extern (C) and extern (D) calling convention matches
1018 the C calling convention used by the supported C compiler
1019 on the host system. */
1020 break;
1022 default:
1023 gcc_unreachable ();
1027 /* Build a delegate type, an aggregate of two pieces of data, an object
1028 reference and a pointer to a non-static member function, or a pointer
1029 to a closure and a pointer to a nested function. */
1031 void visit (TypeDelegate *t) final override
1033 /* In [abi/delegates], delegate layout is:
1034 .ptr context pointer.
1035 .funcptr pointer to function. */
1036 tree fntype = build_ctype (t->next);
1037 tree dgtype = build_vthis_function (void_type_node, fntype);
1039 TYPE_ATTRIBUTES (dgtype) = TYPE_ATTRIBUTES (fntype);
1040 TYPE_LANG_SPECIFIC (dgtype) = TYPE_LANG_SPECIFIC (fntype);
1042 t->ctype = make_struct_type (t->toChars (), 2,
1043 get_identifier ("ptr"),
1044 build_ctype (Type::tvoidptr),
1045 get_identifier ("funcptr"),
1046 build_pointer_type (dgtype));
1047 TYPE_DELEGATE (t->ctype) = 1;
1048 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1049 d_keep (t->ctype);
1053 /* User Defined Types. */
1055 /* Build a named enum type, a distinct value whose values are restrict to
1056 a group of constants of the same underlying base type. */
1058 void visit (TypeEnum *t) final override
1060 tree basetype = (t->sym->memtype)
1061 ? build_ctype (t->sym->memtype) : void_type_node;
1063 if (t->sym->isSpecial ())
1065 /* Special enums are opaque types that bind to C types. */
1066 const char *ident = t->toChars ();
1067 Type *underlying = NULL;
1069 /* Skip over the prefixing `__c_'. */
1070 gcc_assert (startswith (ident, "__c_"));
1071 ident = ident + strlen ("__c_");
1073 /* To keep things compatible within the code generation we stick to
1074 mapping to equivalent D types. However it should be OK to use the
1075 GCC provided C types here as the front-end enforces that everything
1076 must be explicitly cast from a D type to any of the opaque types. */
1077 if (strcmp (ident, "long") == 0)
1078 underlying = build_frontend_type (long_integer_type_node);
1079 else if (strcmp (ident, "ulong") == 0)
1080 underlying = build_frontend_type (long_unsigned_type_node);
1081 else if (strcmp (ident, "wchar_t") == 0)
1082 underlying =
1083 build_frontend_type (make_unsigned_type (WCHAR_TYPE_SIZE));
1084 else if (strcmp (ident, "longlong") == 0)
1085 underlying = build_frontend_type (long_long_integer_type_node);
1086 else if (strcmp (ident, "ulonglong") == 0)
1087 underlying = build_frontend_type (long_long_unsigned_type_node);
1088 else if (strcmp (ident, "long_double") == 0)
1089 underlying = build_frontend_type (long_double_type_node);
1090 else if (strcmp (ident, "complex_real") == 0)
1091 underlying = build_frontend_type (complex_long_double_type_node);
1092 else if (strcmp (ident, "complex_float") == 0)
1093 underlying = build_frontend_type (complex_float_type_node);
1094 else if (strcmp (ident, "complex_double") == 0)
1095 underlying = build_frontend_type (complex_double_type_node);
1097 /* Conversion failed or there's an unhandled special type. */
1098 gcc_assert (underlying != NULL);
1100 t->ctype = build_variant_type_copy (build_ctype (underlying));
1102 /* When the size of the declared enum base type doesn't match the target
1103 C type that this enum is being used as a placeholder for, we can't
1104 use the generated underlying type as it'll conflict with all sizes
1105 the front-end has computed during semantic. */
1106 if (TYPE_SIZE (t->ctype) != TYPE_SIZE (basetype))
1108 warning_at (make_location_t (t->sym->loc),
1109 OPT_Wmismatched_special_enum,
1110 "size of %qs (%wd) differ from its declared size (%wd)",
1111 t->sym->ident->toChars (), int_size_in_bytes (t->ctype),
1112 int_size_in_bytes (basetype));
1113 t->ctype = basetype;
1116 build_type_decl (t->ctype, t->sym);
1118 else if (t->sym->ident == NULL
1119 || !INTEGRAL_TYPE_P (basetype)
1120 || TREE_CODE (basetype) == BOOLEAN_TYPE)
1122 /* Enums in D2 can either be anonymous, or have a base type that is not
1123 necessarily integral. For these, we simplify this a little by using
1124 the base type directly instead of building an ENUMERAL_TYPE. */
1125 t->ctype = build_variant_type_copy (basetype);
1127 if (t->sym->ident != NULL)
1128 build_type_decl (t->ctype, t->sym);
1130 else
1132 t->ctype = make_node (ENUMERAL_TYPE);
1133 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1134 d_keep (t->ctype);
1136 ENUM_IS_SCOPED (t->ctype) = 1;
1137 TREE_TYPE (t->ctype) = basetype;
1139 if (flag_short_enums)
1140 TYPE_PACKED (t->ctype) = 1;
1142 tree values = NULL_TREE;
1143 if (t->sym->members)
1145 for (size_t i = 0; i < t->sym->members->length; i++)
1147 EnumMember *member = (*t->sym->members)[i]->isEnumMember ();
1148 /* Templated functions can seep through to the back-end
1149 just ignore for now. */
1150 if (member == NULL)
1151 continue;
1153 tree ident = get_identifier (member->ident->toChars ());
1154 tree value = build_integer_cst (member->value ()->toInteger (),
1155 basetype);
1157 /* Build an identifier for the enumeration constant. */
1158 tree decl = build_decl (make_location_t (member->loc),
1159 CONST_DECL, ident, basetype);
1160 DECL_CONTEXT (decl) = t->ctype;
1161 TREE_CONSTANT (decl) = 1;
1162 TREE_READONLY (decl) = 1;
1163 DECL_INITIAL (decl) = value;
1165 /* Add this enumeration constant to the list for this type. */
1166 values = chainon (values, build_tree_list (ident, decl));
1170 TYPE_VALUES (t->ctype) = values;
1171 build_type_decl (t->ctype, t->sym);
1174 apply_user_attributes (t->sym, t->ctype);
1176 /* Finish the enumeration type. */
1177 if (TREE_CODE (t->ctype) == ENUMERAL_TYPE)
1179 TYPE_MIN_VALUE (t->ctype) = TYPE_MIN_VALUE (basetype);
1180 TYPE_MAX_VALUE (t->ctype) = TYPE_MAX_VALUE (basetype);
1181 TYPE_UNSIGNED (t->ctype) = TYPE_UNSIGNED (basetype);
1182 SET_TYPE_ALIGN (t->ctype, TYPE_ALIGN (basetype));
1183 TYPE_SIZE (t->ctype) = NULL_TREE;
1184 TYPE_PRECISION (t->ctype) = t->size (t->sym->loc) * 8;
1186 layout_type (t->ctype);
1188 /* Finish debugging output for this type. */
1189 rest_of_type_compilation (t->ctype, TYPE_FILE_SCOPE_P (t->ctype));
1190 finish_incomplete_fields (t->ctype);
1192 /* Finish processing of TYPE_DECL. */
1193 rest_of_decl_compilation (TYPE_NAME (t->ctype),
1194 DECL_FILE_SCOPE_P (TYPE_NAME (t->ctype)), 0);
1198 /* Build a struct or union type. Layout should be exactly represented
1199 as an equivalent C struct, except for non-POD or nested structs. */
1201 void visit (TypeStruct *t) final override
1203 /* Merge types in the back-end if the front-end did not itself do so. */
1204 tree deco = get_identifier (d_mangle_decl (t->sym));
1205 if (merge_aggregate_types (t, deco))
1206 return;
1208 /* Need to set this right away in case of self-references. */
1209 t->ctype = make_node (t->sym->isUnionDeclaration ()
1210 ? UNION_TYPE : RECORD_TYPE);
1211 d_keep (t->ctype);
1212 IDENTIFIER_DAGGREGATE (deco) = t->sym;
1214 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1215 TYPE_CXX_ODR_P (t->ctype) = 1;
1217 if (t->sym->members)
1219 /* Must set up the overall size and alignment before determining
1220 the context or laying out fields as those types may make
1221 recursive references to this type. */
1222 unsigned structsize = t->sym->structsize;
1223 unsigned alignsize = t->sym->alignment.isDefault ()
1224 ? t->sym->alignsize : t->sym->alignment.get ();
1226 /* Put out all fields. */
1227 layout_aggregate_type (t->sym, t->ctype, t->sym);
1228 build_type_decl (t->ctype, t->sym);
1229 set_visibility_for_decl (t->ctype, t->sym);
1230 apply_user_attributes (t->sym, t->ctype);
1231 finish_aggregate_type (structsize, alignsize, t->ctype);
1234 /* For structs with a user defined postblit, copy constructor, or a
1235 destructor, also set TREE_ADDRESSABLE on the type and all variants.
1236 This will make the struct be passed around by reference. */
1237 if (!t->sym->isPOD ())
1239 for (tree tv = t->ctype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
1241 TREE_ADDRESSABLE (tv) = 1;
1242 SET_TYPE_MODE (tv, BLKmode);
1247 /* Build a class type. Whereas structs are value types, classes are
1248 reference types, with all the object-orientated features. */
1250 void visit (TypeClass *t) final override
1252 /* Merge types in the back-end if the front-end did not itself do so. */
1253 tree deco = get_identifier (d_mangle_decl (t->sym));
1254 if (merge_aggregate_types (t, deco))
1255 return;
1257 /* Need to set ctype right away in case of self-references to
1258 the type during this call. */
1259 tree basetype = make_node (RECORD_TYPE);
1260 t->ctype = build_pointer_type (basetype);
1261 d_keep (t->ctype);
1262 IDENTIFIER_DAGGREGATE (deco) = t->sym;
1264 /* Note that lang_specific data is assigned to both the reference
1265 and the underlying record type. */
1266 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1267 TYPE_LANG_SPECIFIC (basetype) = TYPE_LANG_SPECIFIC (t->ctype);
1268 CLASS_TYPE_P (basetype) = 1;
1269 TYPE_CXX_ODR_P (basetype) = 1;
1271 /* Put out all fields, including from each base class. */
1272 layout_aggregate_type (t->sym, basetype, t->sym);
1273 build_type_decl (basetype, t->sym);
1274 set_visibility_for_decl (basetype, t->sym);
1275 apply_user_attributes (t->sym, basetype);
1276 finish_aggregate_type (t->sym->structsize, t->sym->alignsize, basetype);
1278 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
1279 for (tree tv = basetype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
1281 TREE_ADDRESSABLE (tv) = 1;
1282 SET_TYPE_MODE (tv, BLKmode);
1285 /* Type is final, there are no derivations. */
1286 if (t->sym->storage_class & STCfinal)
1287 TYPE_FINAL_P (basetype) = 1;
1289 /* Create BINFO even if debugging is off. This is needed to keep
1290 references to inherited types. */
1291 if (!t->sym->isInterfaceDeclaration ())
1292 TYPE_BINFO (basetype) = build_class_binfo (NULL_TREE, t->sym);
1293 else
1295 unsigned offset = 0;
1297 TYPE_BINFO (basetype) = build_interface_binfo (NULL_TREE, t->sym,
1298 offset);
1301 /* Associate all virtual methods with the class too. */
1302 for (size_t i = 0; i < t->sym->vtbl.length; i++)
1304 FuncDeclaration *fd = t->sym->vtbl[i]->isFuncDeclaration ();
1305 tree method = fd ? get_symbol_decl (fd) : error_mark_node;
1307 if (!error_operand_p (method)
1308 && DECL_CONTEXT (method) == basetype
1309 && !chain_member (method, TYPE_FIELDS (basetype)))
1310 TYPE_FIELDS (basetype) = chainon (TYPE_FIELDS (basetype), method);
1316 /* Build a tree from a frontend Type. */
1318 tree
1319 build_ctype (Type *t)
1321 if (!t->ctype)
1323 TypeVisitor v;
1325 /* Strip const modifiers from type before building. This is done
1326 to ensure that back-end treats e.g: const (T) as a variant of T,
1327 and not as two distinct types. */
1328 if (t->isNaked ())
1329 t->accept (&v);
1330 else
1332 Type *tb = t->castMod (0);
1333 if (!tb->ctype)
1334 tb->accept (&v);
1335 t->ctype = insert_type_modifiers (tb->ctype, t->mod);
1339 return t->ctype;