Fix issue for pointers to anonymous types with -fdump-ada-spec
[official-gcc.git] / gcc / d / types.cc
blobd897ec4c5e42d80c33b802b1d91faedee558b8bd
1 /* types.cc -- Lower D frontend types to GCC trees.
2 Copyright (C) 2006-2022 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 (TYPE_UNSIGNED (type) == (unsigned) unsignedp)
53 return 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 layout_decl (field, 0);
274 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), field);
277 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
279 static tree
280 d_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
282 /* Same as d_type_for_size, but uses exact match for size. */
283 if (width == TYPE_PRECISION (d_byte_type))
284 return unsignedp ? d_ubyte_type : d_byte_type;
286 if (width == TYPE_PRECISION (d_short_type))
287 return unsignedp ? d_ushort_type : d_short_type;
289 if (width == TYPE_PRECISION (d_int_type))
290 return unsignedp ? d_uint_type : d_int_type;
292 if (width == TYPE_PRECISION (d_long_type))
293 return unsignedp ? d_ulong_type : d_long_type;
295 if (width == TYPE_PRECISION (d_cent_type))
296 return unsignedp ? d_ucent_type : d_cent_type;
298 for (int i = 0; i < NUM_INT_N_ENTS; i ++)
300 if (int_n_enabled_p[i] && width == int_n_data[i].bitsize)
302 if (unsignedp)
303 return int_n_trees[i].unsigned_type;
304 else
305 return int_n_trees[i].signed_type;
309 return build_nonstandard_integer_type (width, unsignedp);
312 /* Adds BITFIELD into the aggregate TYPE at OFFSET+BITOFFSET. */
314 static void
315 insert_aggregate_bitfield (tree type, tree bitfield, size_t width,
316 size_t offset, size_t bitoffset)
318 DECL_FIELD_CONTEXT (bitfield) = type;
319 SET_DECL_OFFSET_ALIGN (bitfield, TYPE_ALIGN (TREE_TYPE (bitfield)));
320 DECL_SIZE (bitfield) = bitsize_int (width);
321 DECL_FIELD_OFFSET (bitfield) = size_int (offset);
322 DECL_FIELD_BIT_OFFSET (bitfield) = bitsize_int (bitoffset);
324 TREE_ADDRESSABLE (bitfield) = TYPE_SHARED (TREE_TYPE (bitfield));
326 DECL_BIT_FIELD (bitfield) = 1;
327 DECL_BIT_FIELD_TYPE (bitfield) = TREE_TYPE (bitfield);
329 layout_decl (bitfield, 0);
331 /* Give bit-field its proper type after layout_decl. */
332 tree orig_type = DECL_BIT_FIELD_TYPE (bitfield);
333 if (width != TYPE_PRECISION (orig_type))
335 TREE_TYPE (bitfield)
336 = d_build_bitfield_integer_type (width, TYPE_UNSIGNED (orig_type));
337 SET_DECL_MODE (bitfield, TYPE_MODE (TREE_TYPE (bitfield)));
340 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), bitfield);
343 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET.
344 This is done as the frontend puts fields into the outer struct, and so
345 their offset is from the beginning of the aggregate.
346 We want the offset to be from the beginning of the anonymous aggregate. */
348 static void
349 fixup_anonymous_offset (tree fields, tree offset)
351 /* No adjustment in field offset required. */
352 if (integer_zerop (offset))
353 return;
355 while (fields != NULL_TREE)
357 /* Traverse all nested anonymous aggregates to update the offset of their
358 fields. Note that the anonymous field itself is not adjusted, as it
359 already has an offset relative to its outer aggregate. */
360 tree ftype = TREE_TYPE (fields);
361 if (TYPE_NAME (ftype) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype)))
363 tree vfields = TYPE_FIELDS (ftype);
364 fixup_anonymous_offset (vfields, offset);
366 else
368 tree voffset = DECL_FIELD_OFFSET (fields);
369 DECL_FIELD_OFFSET (fields) = size_binop (MINUS_EXPR, voffset, offset);
372 fields = DECL_CHAIN (fields);
376 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT.
377 If INHERITED_P is true, then the members derive from a base class.
378 Returns the number of fields found. */
380 static size_t
381 layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p)
383 size_t fields = 0;
385 for (size_t i = 0; i < members->length; i++)
387 Dsymbol *sym = (*members)[i];
388 VarDeclaration *var = sym->isVarDeclaration ();
389 if (var != NULL)
391 /* Skip fields that have already been added. */
392 if (!inherited_p && var->csym != NULL)
393 continue;
395 /* If this variable was really a tuple, add all tuple fields. */
396 if (var->aliassym)
398 TupleDeclaration *td = var->aliassym->isTupleDeclaration ();
399 Dsymbols tmembers;
400 /* No other way to coerce the underlying type out of the tuple.
401 Frontend should have already validated this. */
402 for (size_t j = 0; j < td->objects->length; j++)
404 RootObject *ro = (*td->objects)[j];
405 gcc_assert (ro->dyncast () == DYNCAST_EXPRESSION);
406 Expression *e = (Expression *) ro;
407 gcc_assert (e->op == EXP::dSymbol);
408 DsymbolExp *se = e->isDsymbolExp ();
410 tmembers.push (se->s);
413 fields += layout_aggregate_members (&tmembers, context,
414 inherited_p);
415 continue;
418 /* Insert the field declaration at its given offset. */
419 if (var->isField ())
421 const char *ident = var->ident ? var->ident->toChars () : NULL;
422 tree field = create_field_decl (declaration_type (var), ident,
423 inherited_p, inherited_p);
424 apply_user_attributes (var, field);
426 if (BitFieldDeclaration *bf = var->isBitFieldDeclaration ())
428 /* Bit-fields come from an ImportC context, and require the
429 field be correctly adjusted. */
430 insert_aggregate_bitfield (context, field, bf->fieldWidth,
431 bf->offset, bf->bitOffset);
433 else
434 insert_aggregate_field (context, field, var->offset);
436 /* Because the front-end shares field decls across classes, don't
437 create the corresponding back-end symbol unless we are adding
438 it to the aggregate it is defined in. */
439 if (!inherited_p)
441 DECL_LANG_SPECIFIC (field) = build_lang_decl (var);
442 var->csym = field;
445 fields += 1;
446 continue;
450 /* Anonymous struct/union are flattened by the frontend. However, we
451 want to keep the record layout in-tact when building the type. */
452 AnonDeclaration *ad = sym->isAnonDeclaration ();
453 if (ad != NULL)
455 tree ident = make_anon_name ();
456 tree type = make_node (ad->isunion ? UNION_TYPE : RECORD_TYPE);
457 ANON_AGGR_TYPE_P (type) = 1;
458 d_keep (type);
460 /* Build the type declaration. */
461 tree decl = build_decl (make_location_t (ad->loc),
462 TYPE_DECL, ident, type);
463 DECL_CONTEXT (decl) = context;
464 DECL_ARTIFICIAL (decl) = 1;
466 TYPE_CONTEXT (type) = context;
467 TYPE_NAME (type) = decl;
468 TYPE_STUB_DECL (type) = decl;
470 /* Recursively iterator over the anonymous members. */
471 fields += layout_aggregate_members (ad->decl, type, inherited_p);
473 /* Remove from the anon fields the base offset of this anonymous
474 aggregate. Undoes what is set-up in setFieldOffset, but doesn't
475 affect field accesses. */
476 tree offset = size_int (ad->anonoffset);
477 fixup_anonymous_offset (TYPE_FIELDS (type), offset);
479 finish_aggregate_type (ad->anonstructsize, ad->anonalignsize, type);
481 /* And make the corresponding data member. */
482 tree field = create_field_decl (type, NULL, 0, 0);
483 apply_user_attributes (ad, field);
484 insert_aggregate_field (context, field, ad->anonoffset);
485 continue;
488 /* Other kinds of attributes don't create a scope. */
489 AttribDeclaration *attrib = sym->isAttribDeclaration ();
490 if (attrib != NULL)
492 Dsymbols *decls = attrib->include (NULL);
493 if (decls != NULL)
495 fields += layout_aggregate_members (decls, context, inherited_p);
496 continue;
500 /* Same with template mixins and namespaces. */
501 if (sym->isTemplateMixin () || sym->isNspace ())
503 ScopeDsymbol *scopesym = sym->isScopeDsymbol ();
504 if (scopesym->members)
506 fields += layout_aggregate_members (scopesym->members, context,
507 inherited_p);
508 continue;
513 return fields;
516 /* Write out all fields for aggregate BASE. For classes, write out all
517 interfaces first, then the base class fields. */
519 static void
520 layout_aggregate_type (AggregateDeclaration *decl, tree type,
521 AggregateDeclaration *base)
523 ClassDeclaration *cd = base->isClassDeclaration ();
524 bool inherited_p = (decl != base);
526 if (cd != NULL)
528 if (cd->baseClass)
529 layout_aggregate_type (decl, type, cd->baseClass);
530 else
532 /* This is the base class (Object) or interface. */
533 tree objtype = TREE_TYPE (build_ctype (cd->type));
535 /* Add the vtable pointer, and optionally the monitor fields. */
536 InterfaceDeclaration *id = cd->isInterfaceDeclaration ();
537 if (!id || id->vtblInterfaces->length == 0)
539 tree field = create_field_decl (vtbl_ptr_type_node, "__vptr", 1,
540 inherited_p);
541 DECL_VIRTUAL_P (field) = 1;
542 TYPE_VFIELD (type) = field;
543 DECL_FCONTEXT (field) = objtype;
544 insert_aggregate_field (type, field, 0);
547 if (!id && cd->hasMonitor ())
549 tree field = create_field_decl (ptr_type_node, "__monitor", 1,
550 inherited_p);
551 insert_aggregate_field (type, field, target.ptrsize);
555 if (cd->vtblInterfaces)
557 for (size_t i = 0; i < cd->vtblInterfaces->length; i++)
559 BaseClass *bc = (*cd->vtblInterfaces)[i];
560 tree field = create_field_decl (vtbl_ptr_type_node, NULL, 1, 1);
561 insert_aggregate_field (type, field, bc->offset);
566 if (base->members)
568 size_t fields = layout_aggregate_members (base->members, type,
569 inherited_p);
570 gcc_assert (fields == base->fields.length);
572 /* Make sure that all fields have been created. */
573 if (!inherited_p)
575 for (size_t i = 0; i < base->fields.length; i++)
577 VarDeclaration *var = base->fields[i];
578 gcc_assert (var->csym != NULL);
584 /* Given a record type TYPE, whose size and alignment are determined by
585 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
586 the finalized record mode. */
588 void
589 finish_aggregate_type (unsigned structsize, unsigned alignsize, tree type)
591 /* Set size and alignment as requested by frontend. */
592 TYPE_SIZE (type) = bitsize_int (structsize * BITS_PER_UNIT);
593 TYPE_SIZE_UNIT (type) = size_int (structsize);
594 SET_TYPE_ALIGN (type, alignsize * BITS_PER_UNIT);
595 TYPE_PACKED (type) = (alignsize == 1);
597 /* Set the back-end type mode. */
598 compute_record_mode (type);
600 /* Fix up all variants of this aggregate type. */
601 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
603 if (t == type)
604 continue;
606 TYPE_FIELDS (t) = TYPE_FIELDS (type);
607 TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (type);
608 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
609 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
610 gcc_assert (TYPE_MODE (t) == TYPE_MODE (type));
614 /* Returns true if the class or struct type TYPE has already been layed out by
615 the lowering of another front-end AST type. In which case, there will either
616 be a reuse of the back-end type, or a multiple definition error.
617 DECO is the uniquely mangled decoration for the type. */
619 static bool
620 merge_aggregate_types (Type *type, tree deco)
622 AggregateDeclaration *sym;
624 if (type->ty == TY::Tstruct)
625 sym = type->isTypeStruct ()->sym;
626 else if (type->ty == TY::Tclass)
627 sym = type->isTypeClass ()->sym;
628 else
629 gcc_unreachable ();
631 if (IDENTIFIER_DAGGREGATE (deco))
633 AggregateDeclaration *ad = IDENTIFIER_DAGGREGATE (deco);
634 /* There should never be a class/struct mismatch in mangled names. */
635 gcc_assert ((sym->isStructDeclaration () && ad->isStructDeclaration ())
636 || (sym->isClassDeclaration () && ad->isClassDeclaration ()));
638 /* Non-templated variables shouldn't be defined twice. */
639 if (!sym->isInstantiated ())
640 ScopeDsymbol::multiplyDefined (sym->loc, sym, ad);
642 type->ctype = build_ctype (ad->type);
643 return true;
646 return false;
649 /* Implements the visitor interface to build the GCC trees of all
650 Type AST classes emitted from the D Front-end, where CTYPE holds
651 the cached back-end representation to be returned. */
653 class TypeVisitor : public Visitor
655 using Visitor::visit;
657 public:
658 TypeVisitor (void)
662 /* This should be overridden by each type class. */
664 void visit (Type *)
666 gcc_unreachable ();
669 /* Type assigned to erroneous expressions or constructs that
670 failed during the semantic stage. */
672 void visit (TypeError *t)
674 t->ctype = error_mark_node;
677 /* Type assigned to generic nullable types. */
679 void visit (TypeNull *t)
681 t->ctype = ptr_type_node;
684 /* Bottom type used for functions that never return. */
686 void visit (TypeNoreturn *t)
688 t->ctype = noreturn_type_node;
689 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
692 /* Basic Data Types. */
694 void visit (TypeBasic *t)
696 /* [type/basic-data-types]
698 void no type.
699 bool 8-bit boolean value.
700 byte 8-bit signed value.
701 ubyte 8-bit unsigned value.
702 short 16-bit signed value.
703 ushort 16-bit unsigned value.
704 int 32-bit signed value.
705 uint 32-bit unsigned value.
706 long 64-bit signed value.
707 ulong 64-bit unsigned value.
708 cent 128-bit signed value.
709 ucent 128-bit unsigned value.
710 float 32-bit IEEE 754 floating-point value.
711 double 64-bit IEEE 754 floating-point value.
712 real largest FP size implemented in hardware.
713 ifloat imaginary float.
714 idouble imaginary double.
715 ireal imaginary real.
716 cfloat complex float.
717 cdouble complex double.
718 creal complex real.
719 char UTF-8 code unit.
720 wchar UTF-16 code unit.
721 dchar UTF-32 code unit. */
723 switch (t->ty)
725 case TY::Tvoid: t->ctype = void_type_node; break;
726 case TY::Tbool: t->ctype = d_bool_type; break;
727 case TY::Tint8: t->ctype = d_byte_type; break;
728 case TY::Tuns8: t->ctype = d_ubyte_type; break;
729 case TY::Tint16: t->ctype = d_short_type; break;
730 case TY::Tuns16: t->ctype = d_ushort_type; break;
731 case TY::Tint32: t->ctype = d_int_type; break;
732 case TY::Tuns32: t->ctype = d_uint_type; break;
733 case TY::Tint64: t->ctype = d_long_type; break;
734 case TY::Tuns64: t->ctype = d_ulong_type; break;
735 case TY::Tint128: t->ctype = d_cent_type; break;
736 case TY::Tuns128: t->ctype = d_ucent_type; break;
737 case TY::Tfloat32: t->ctype = float_type_node; break;
738 case TY::Tfloat64: t->ctype = double_type_node; break;
739 case TY::Tfloat80: t->ctype = long_double_type_node; break;
740 case TY::Timaginary32: t->ctype = ifloat_type_node; break;
741 case TY::Timaginary64: t->ctype = idouble_type_node; break;
742 case TY::Timaginary80: t->ctype = ireal_type_node; break;
743 case TY::Tcomplex32: t->ctype = complex_float_type_node; break;
744 case TY::Tcomplex64: t->ctype = complex_double_type_node; break;
745 case TY::Tcomplex80: t->ctype = complex_long_double_type_node; break;
746 case TY::Tchar: t->ctype = char8_type_node; break;
747 case TY::Twchar: t->ctype = char16_type_node; break;
748 case TY::Tdchar: t->ctype = char32_type_node; break;
749 default: gcc_unreachable ();
752 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
756 /* Derived Data Types. */
758 /* Build a simple pointer to data type, analogous to C pointers. */
760 void visit (TypePointer *t)
762 t->ctype = build_pointer_type (build_ctype (t->next));
765 /* Build a dynamic array type, consisting of a length and a pointer
766 to the array data. */
768 void visit (TypeDArray *t)
770 /* In [abi/arrays], dynamic array layout is:
771 .length array dimension.
772 .ptr pointer to array data. */
773 t->ctype = make_struct_type (t->toChars (), 2,
774 get_identifier ("length"),
775 build_ctype (Type::tsize_t),
776 get_identifier ("ptr"),
777 build_pointer_type (build_ctype (t->next)));
778 TYPE_DYNAMIC_ARRAY (t->ctype) = 1;
779 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
780 d_keep (t->ctype);
783 /* Build a static array type, distinguished from dynamic arrays by
784 having a length fixed at compile-time, analogous to C arrays. */
786 void visit (TypeSArray *t)
788 if (t->dim->isConst () && t->dim->type->isintegral ())
790 uinteger_t size = t->dim->toUInteger ();
791 t->ctype = make_array_type (t->next, size);
793 else
795 error ("invalid expression for static array dimension: %s",
796 t->dim->toChars ());
797 gcc_unreachable ();
801 /* Build a vector type, a fixed array of floating or integer types. */
803 void visit (TypeVector *t)
805 int nunits = t->basetype->isTypeSArray ()->dim->toUInteger ();
806 tree inner = build_ctype (t->elementType ());
808 /* Same rationale as void static arrays. */
809 if (inner == void_type_node)
810 inner = build_ctype (Type::tuns8);
812 t->ctype = build_vector_type (inner, nunits);
813 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
814 layout_type (t->ctype);
817 /* Build an associative array type, distinguished from arrays by having an
818 index that's not necessarily an integer, and can be sparsely populated. */
820 void visit (TypeAArray *t)
822 /* In [abi/associative-arrays], associative arrays are a struct that only
823 consist of a pointer to an opaque, implementation defined type. */
824 t->ctype = make_struct_type (t->toChars (), 1,
825 get_identifier ("ptr"), ptr_type_node);
826 TYPE_ASSOCIATIVE_ARRAY (t->ctype) = 1;
827 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
828 d_keep (t->ctype);
831 /* Build type for a function declaration, which consists of a return type,
832 and a list of parameter types, and a linkage attribute. */
834 void visit (TypeFunction *t)
836 tree fnparams = NULL_TREE;
837 tree fntype;
839 /* [function/variadic]
841 Variadic functions with D linkage have an additional hidden argument
842 with the name _arguments passed to the function. */
843 if (t->isDstyleVariadic ())
845 tree type = build_ctype (Type::typeinfotypelist->type);
846 fnparams = chainon (fnparams, build_tree_list (0, type));
849 const size_t n_args = t->parameterList.length ();
851 for (size_t i = 0; i < n_args; i++)
853 tree type = parameter_type (t->parameterList[i]);
855 /* Type `noreturn` is a terminator, as no other arguments can possibly
856 be evaluated after it. */
857 if (type == noreturn_type_node)
858 break;
860 fnparams = chainon (fnparams, build_tree_list (0, type));
863 /* When the last parameter is void_list_node, that indicates a fixed length
864 parameter list, otherwise function is treated as variadic. */
865 if (t->parameterList.varargs != VARARGvariadic)
866 fnparams = chainon (fnparams, void_list_node);
868 if (t->next != NULL)
870 fntype = build_ctype (t->next);
871 if (t->isref ())
872 fntype = build_reference_type (fntype);
874 else
875 fntype = void_type_node;
877 /* Could the function type be self referenced by parameters? */
878 t->ctype = build_function_type (fntype, fnparams);
879 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
880 d_keep (t->ctype);
882 /* Qualify function types that have the type `noreturn` as volatile. */
883 if (fntype == noreturn_type_node)
884 t->ctype = build_qualified_type (t->ctype, TYPE_QUAL_VOLATILE);
886 /* Handle any special support for calling conventions. */
887 switch (t->linkage)
889 case LINK::windows:
891 /* [attribute/linkage]
893 The Windows convention is distinct from the C convention only
894 on Win32, where it is equivalent to the stdcall convention. */
895 unsigned link_system, link_windows;
896 if (targetdm.d_has_stdcall_convention (&link_system, &link_windows))
898 if (link_windows)
899 t->ctype = insert_type_attribute (t->ctype, "stdcall");
901 break;
904 case LINK::c:
905 case LINK::cpp:
906 case LINK::d:
907 case LINK::objc:
908 /* [abi/function-calling-conventions]
910 The extern (C) and extern (D) calling convention matches
911 the C calling convention used by the supported C compiler
912 on the host system. */
913 break;
915 default:
916 gcc_unreachable ();
920 /* Build a delegate type, an aggregate of two pieces of data, an object
921 reference and a pointer to a non-static member function, or a pointer
922 to a closure and a pointer to a nested function. */
924 void visit (TypeDelegate *t)
926 /* In [abi/delegates], delegate layout is:
927 .ptr context pointer.
928 .funcptr pointer to function. */
929 tree fntype = build_ctype (t->next);
930 tree dgtype = build_vthis_function (void_type_node, fntype);
932 TYPE_ATTRIBUTES (dgtype) = TYPE_ATTRIBUTES (fntype);
933 TYPE_LANG_SPECIFIC (dgtype) = TYPE_LANG_SPECIFIC (fntype);
935 t->ctype = make_struct_type (t->toChars (), 2,
936 get_identifier ("ptr"),
937 build_ctype (Type::tvoidptr),
938 get_identifier ("funcptr"),
939 build_pointer_type (dgtype));
940 TYPE_DELEGATE (t->ctype) = 1;
941 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
942 d_keep (t->ctype);
946 /* User Defined Types. */
948 /* Build a named enum type, a distinct value whose values are restrict to
949 a group of constants of the same underlying base type. */
951 void visit (TypeEnum *t)
953 tree basetype = (t->sym->memtype)
954 ? build_ctype (t->sym->memtype) : void_type_node;
956 if (t->sym->isSpecial ())
958 /* Special enums are opaque types that bind to C types. */
959 const char *ident = t->toChars ();
960 Type *underlying = NULL;
962 /* Skip over the prefixing `__c_'. */
963 gcc_assert (startswith (ident, "__c_"));
964 ident = ident + strlen ("__c_");
966 /* To keep things compatible within the code generation we stick to
967 mapping to equivalent D types. However it should be OK to use the
968 GCC provided C types here as the front-end enforces that everything
969 must be explicitly cast from a D type to any of the opaque types. */
970 if (strcmp (ident, "long") == 0)
971 underlying = build_frontend_type (long_integer_type_node);
972 else if (strcmp (ident, "ulong") == 0)
973 underlying = build_frontend_type (long_unsigned_type_node);
974 else if (strcmp (ident, "wchar_t") == 0)
975 underlying =
976 build_frontend_type (make_unsigned_type (WCHAR_TYPE_SIZE));
977 else if (strcmp (ident, "longlong") == 0)
978 underlying = build_frontend_type (long_long_integer_type_node);
979 else if (strcmp (ident, "ulonglong") == 0)
980 underlying = build_frontend_type (long_long_unsigned_type_node);
981 else if (strcmp (ident, "long_double") == 0)
982 underlying = build_frontend_type (long_double_type_node);
983 else if (strcmp (ident, "complex_real") == 0)
984 underlying = build_frontend_type (complex_long_double_type_node);
985 else if (strcmp (ident, "complex_float") == 0)
986 underlying = build_frontend_type (complex_float_type_node);
987 else if (strcmp (ident, "complex_double") == 0)
988 underlying = build_frontend_type (complex_double_type_node);
990 /* Conversion failed or there's an unhandled special type. */
991 gcc_assert (underlying != NULL);
993 t->ctype = build_variant_type_copy (build_ctype (underlying));
994 build_type_decl (t->ctype, t->sym);
996 else if (t->sym->ident == NULL
997 || !INTEGRAL_TYPE_P (basetype)
998 || TREE_CODE (basetype) == BOOLEAN_TYPE)
1000 /* Enums in D2 can either be anonymous, or have a base type that is not
1001 necessarily integral. For these, we simplify this a little by using
1002 the base type directly instead of building an ENUMERAL_TYPE. */
1003 t->ctype = build_variant_type_copy (basetype);
1005 if (t->sym->ident != NULL)
1006 build_type_decl (t->ctype, t->sym);
1008 else
1010 t->ctype = make_node (ENUMERAL_TYPE);
1011 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1012 d_keep (t->ctype);
1014 ENUM_IS_SCOPED (t->ctype) = 1;
1015 TREE_TYPE (t->ctype) = basetype;
1017 if (flag_short_enums)
1018 TYPE_PACKED (t->ctype) = 1;
1020 TYPE_PRECISION (t->ctype) = t->size (t->sym->loc) * 8;
1021 TYPE_SIZE (t->ctype) = 0;
1023 TYPE_MIN_VALUE (t->ctype) = TYPE_MIN_VALUE (basetype);
1024 TYPE_MAX_VALUE (t->ctype) = TYPE_MAX_VALUE (basetype);
1025 layout_type (t->ctype);
1027 tree values = NULL_TREE;
1028 if (t->sym->members)
1030 for (size_t i = 0; i < t->sym->members->length; i++)
1032 EnumMember *member = (*t->sym->members)[i]->isEnumMember ();
1033 /* Templated functions can seep through to the back-end
1034 just ignore for now. */
1035 if (member == NULL)
1036 continue;
1038 tree ident = get_identifier (member->ident->toChars ());
1039 tree value = build_integer_cst (member->value ()->toInteger (),
1040 basetype);
1042 /* Build an identifier for the enumeration constant. */
1043 tree decl = build_decl (make_location_t (member->loc),
1044 CONST_DECL, ident, basetype);
1045 DECL_CONTEXT (decl) = t->ctype;
1046 TREE_CONSTANT (decl) = 1;
1047 TREE_READONLY (decl) = 1;
1048 DECL_INITIAL (decl) = value;
1050 /* Add this enumeration constant to the list for this type. */
1051 values = chainon (values, build_tree_list (ident, decl));
1055 TYPE_VALUES (t->ctype) = values;
1056 TYPE_UNSIGNED (t->ctype) = TYPE_UNSIGNED (basetype);
1057 build_type_decl (t->ctype, t->sym);
1060 apply_user_attributes (t->sym, t->ctype);
1063 /* Build a struct or union type. Layout should be exactly represented
1064 as an equivalent C struct, except for non-POD or nested structs. */
1066 void visit (TypeStruct *t)
1068 /* Merge types in the back-end if the front-end did not itself do so. */
1069 tree deco = get_identifier (d_mangle_decl (t->sym));
1070 if (merge_aggregate_types (t, deco))
1071 return;
1073 /* Need to set this right away in case of self-references. */
1074 t->ctype = make_node (t->sym->isUnionDeclaration ()
1075 ? UNION_TYPE : RECORD_TYPE);
1076 d_keep (t->ctype);
1077 IDENTIFIER_DAGGREGATE (deco) = t->sym;
1079 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1080 TYPE_CXX_ODR_P (t->ctype) = 1;
1082 if (t->sym->members)
1084 /* Must set up the overall size and alignment before determining
1085 the context or laying out fields as those types may make
1086 recursive references to this type. */
1087 unsigned structsize = t->sym->structsize;
1088 unsigned alignsize = t->sym->alignment.isDefault ()
1089 ? t->sym->alignsize : t->sym->alignment.get ();
1091 TYPE_SIZE (t->ctype) = bitsize_int (structsize * BITS_PER_UNIT);
1092 TYPE_SIZE_UNIT (t->ctype) = size_int (structsize);
1093 SET_TYPE_ALIGN (t->ctype, alignsize * BITS_PER_UNIT);
1094 TYPE_PACKED (t->ctype) = (alignsize == 1);
1095 compute_record_mode (t->ctype);
1097 /* Put out all fields. */
1098 layout_aggregate_type (t->sym, t->ctype, t->sym);
1099 apply_user_attributes (t->sym, t->ctype);
1100 finish_aggregate_type (structsize, alignsize, t->ctype);
1103 TYPE_CONTEXT (t->ctype) = d_decl_context (t->sym);
1104 build_type_decl (t->ctype, t->sym);
1106 /* For structs with a user defined postblit, copy constructor, or a
1107 destructor, also set TREE_ADDRESSABLE on the type and all variants.
1108 This will make the struct be passed around by reference. */
1109 if (!t->sym->isPOD ())
1111 for (tree tv = t->ctype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
1113 TREE_ADDRESSABLE (tv) = 1;
1114 SET_TYPE_MODE (tv, BLKmode);
1119 /* Build a class type. Whereas structs are value types, classes are
1120 reference types, with all the object-orientated features. */
1122 void visit (TypeClass *t)
1124 /* Merge types in the back-end if the front-end did not itself do so. */
1125 tree deco = get_identifier (d_mangle_decl (t->sym));
1126 if (merge_aggregate_types (t, deco))
1127 return;
1129 /* Need to set ctype right away in case of self-references to
1130 the type during this call. */
1131 tree basetype = make_node (RECORD_TYPE);
1132 t->ctype = build_pointer_type (basetype);
1133 d_keep (t->ctype);
1134 IDENTIFIER_DAGGREGATE (deco) = t->sym;
1136 /* Note that lang_specific data is assigned to both the reference
1137 and the underlying record type. */
1138 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1139 TYPE_LANG_SPECIFIC (basetype) = TYPE_LANG_SPECIFIC (t->ctype);
1140 CLASS_TYPE_P (basetype) = 1;
1141 TYPE_CXX_ODR_P (basetype) = 1;
1143 /* Put out all fields, including from each base class. */
1144 layout_aggregate_type (t->sym, basetype, t->sym);
1145 apply_user_attributes (t->sym, basetype);
1146 finish_aggregate_type (t->sym->structsize, t->sym->alignsize, basetype);
1148 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
1149 for (tree tv = basetype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
1151 TREE_ADDRESSABLE (tv) = 1;
1152 SET_TYPE_MODE (tv, BLKmode);
1155 /* Type is final, there are no derivations. */
1156 if (t->sym->storage_class & STCfinal)
1157 TYPE_FINAL_P (basetype) = 1;
1159 /* Create BINFO even if debugging is off. This is needed to keep
1160 references to inherited types. */
1161 if (!t->sym->isInterfaceDeclaration ())
1162 TYPE_BINFO (basetype) = build_class_binfo (NULL_TREE, t->sym);
1163 else
1165 unsigned offset = 0;
1167 TYPE_BINFO (basetype) = build_interface_binfo (NULL_TREE, t->sym,
1168 offset);
1171 /* Associate all virtual methods with the class too. */
1172 for (size_t i = 0; i < t->sym->vtbl.length; i++)
1174 FuncDeclaration *fd = t->sym->vtbl[i]->isFuncDeclaration ();
1175 tree method = fd ? get_symbol_decl (fd) : error_mark_node;
1177 if (!error_operand_p (method)
1178 && DECL_CONTEXT (method) == basetype
1179 && !chain_member (method, TYPE_FIELDS (basetype)))
1180 TYPE_FIELDS (basetype) = chainon (TYPE_FIELDS (basetype), method);
1183 TYPE_CONTEXT (basetype) = d_decl_context (t->sym);
1184 build_type_decl (basetype, t->sym);
1189 /* Build a tree from a frontend Type. */
1191 tree
1192 build_ctype (Type *t)
1194 if (!t->ctype)
1196 TypeVisitor v;
1198 /* Strip const modifiers from type before building. This is done
1199 to ensure that back-end treats e.g: const (T) as a variant of T,
1200 and not as two distinct types. */
1201 if (t->isNaked ())
1202 t->accept (&v);
1203 else
1205 Type *tb = t->castMod (0);
1206 if (!tb->ctype)
1207 tb->accept (&v);
1208 t->ctype = insert_type_modifiers (tb->ctype, t->mod);
1212 return t->ctype;