]> git.ipfire.org Git - people/ms/gcc.git/blob - gcc/d/types.cc
483173d01fca3d2e5a22b3d798561cdcc989a531
[people/ms/gcc.git] / gcc / d / types.cc
1 /* types.cc -- Lower D frontend types to GCC trees.
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
17
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21
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"
29
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"
41
42 #include "d-tree.h"
43 #include "d-target.h"
44
45
46 /* Return the signed or unsigned version of TYPE, an integral type, the
47 signedness being specified by UNSIGNEDP. */
48
49 static tree
50 d_signed_or_unsigned_type (int unsignedp, tree type)
51 {
52 if (TYPE_UNSIGNED (type) == (unsigned) unsignedp)
53 return type;
54
55 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_cent_type))
56 return unsignedp ? d_ucent_type : d_cent_type;
57
58 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_long_type))
59 return unsignedp ? d_ulong_type : d_long_type;
60
61 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_int_type))
62 return unsignedp ? d_uint_type : d_int_type;
63
64 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_short_type))
65 return unsignedp ? d_ushort_type : d_short_type;
66
67 if (TYPE_PRECISION (type) == TYPE_PRECISION (d_byte_type))
68 return unsignedp ? d_ubyte_type : d_byte_type;
69
70 return signed_or_unsigned_type_for (unsignedp, type);
71 }
72
73 /* Return the unsigned version of TYPE, an integral type. */
74
75 tree
76 d_unsigned_type (tree type)
77 {
78 return d_signed_or_unsigned_type (1, type);
79 }
80
81 /* Return the signed version of TYPE, an integral type. */
82
83 tree
84 d_signed_type (tree type)
85 {
86 return d_signed_or_unsigned_type (0, type);
87 }
88
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. */
92
93 bool
94 valist_array_p (Type *type)
95 {
96 Type *tvalist = target.va_listType (Loc (), NULL);
97 if (tvalist->ty == TY::Tsarray)
98 {
99 Type *tb = type->toBasetype ();
100 if (same_type_p (tb, tvalist))
101 return true;
102 }
103
104 return false;
105 }
106
107 /* Returns true if TYPE contains no actual data, just various
108 possible combinations of empty aggregates. */
109
110 bool
111 empty_aggregate_p (tree type)
112 {
113 if (!AGGREGATE_TYPE_P (type))
114 return false;
115
116 /* Want the element type for arrays. */
117 if (TREE_CODE (type) == ARRAY_TYPE)
118 return empty_aggregate_p (TREE_TYPE (type));
119
120 /* Recursively check all fields. */
121 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
122 {
123 if (TREE_CODE (field) == FIELD_DECL
124 && !empty_aggregate_p (TREE_TYPE (field)))
125 return false;
126 }
127
128 return true;
129 }
130
131 /* Returns true if T1 and T2 are related to each other. */
132
133 bool
134 same_type_p (Type *t1, Type *t2)
135 {
136 /* Types are equal. */
137 if (t1 == t2)
138 return true;
139
140 /* Types derive from the same base. */
141 Type *tb1 = t1->toBasetype ();
142 Type *tb2 = t2->toBasetype ();
143 if (tb1 == tb2)
144 return true;
145
146 /* Types are mutably the same type. */
147 if (tb1->ty == tb2->ty && tb1->equivalent (tb2))
148 return true;
149
150 return false;
151 }
152
153 /* Returns `Object' type which all D classes are derived from. */
154
155 Type *
156 get_object_type (void)
157 {
158 if (ClassDeclaration::object)
159 return ClassDeclaration::object->type;
160
161 error ("missing or corrupt object.d");
162 return Type::terror;
163 }
164
165
166 /* Returns a static array of TYPE which has SIZE number of elements. */
167
168 tree
169 make_array_type (Type *type, unsigned HOST_WIDE_INT size)
170 {
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;
175
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)
179 {
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);
183
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;
188 }
189
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;
195 }
196
197 /* Builds a record type whose name is NAME. NFIELDS is the number of fields,
198 provided as field ident/type pairs. */
199
200 tree
201 make_struct_type (const char *name, int nfields, ...)
202 {
203 tree fields = NULL_TREE;
204 va_list ap;
205
206 va_start (ap, nfields);
207
208 for (int i = 0; i < nfields; i++)
209 {
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;
215 }
216
217 va_end (ap);
218
219 tree type = make_node (RECORD_TYPE);
220 finish_builtin_struct (type, name, fields, NULL_TREE);
221
222 return type;
223 }
224
225 /* Return qualified type variant of TYPE determined by modifier value MOD. */
226
227 tree
228 insert_type_modifiers (tree type, unsigned mod)
229 {
230 int quals = 0;
231
232 switch (mod)
233 {
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;
243
244 case 0:
245 case MODshared:
246 break;
247
248 default:
249 gcc_unreachable ();
250 }
251
252 tree qualtype = build_qualified_type (type, quals);
253
254 /* Mark whether the type is qualified `shared'. */
255 if (mod & MODshared)
256 TYPE_SHARED (qualtype) = 1;
257
258 return qualtype;
259 }
260
261 /* Adds FIELD into the aggregate TYPE at OFFSET. */
262
263 void
264 insert_aggregate_field (tree type, tree field, size_t offset)
265 {
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;
270
271 TREE_ADDRESSABLE (field) = TYPE_SHARED (TREE_TYPE (field));
272
273 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), field);
274 }
275
276 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
277
278 static tree
279 d_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
280 {
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;
284
285 if (width == TYPE_PRECISION (d_short_type))
286 return unsignedp ? d_ushort_type : d_short_type;
287
288 if (width == TYPE_PRECISION (d_int_type))
289 return unsignedp ? d_uint_type : d_int_type;
290
291 if (width == TYPE_PRECISION (d_long_type))
292 return unsignedp ? d_ulong_type : d_long_type;
293
294 if (width == TYPE_PRECISION (d_cent_type))
295 return unsignedp ? d_ucent_type : d_cent_type;
296
297 for (int i = 0; i < NUM_INT_N_ENTS; i ++)
298 {
299 if (int_n_enabled_p[i] && width == int_n_data[i].bitsize)
300 {
301 if (unsignedp)
302 return int_n_trees[i].unsigned_type;
303 else
304 return int_n_trees[i].signed_type;
305 }
306 }
307
308 return build_nonstandard_integer_type (width, unsignedp);
309 }
310
311 /* Adds BITFIELD into the aggregate TYPE at OFFSET+BITOFFSET. */
312
313 static void
314 insert_aggregate_bitfield (tree type, tree bitfield, size_t width,
315 size_t offset, size_t bitoffset)
316 {
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);
322
323 TREE_ADDRESSABLE (bitfield) = TYPE_SHARED (TREE_TYPE (bitfield));
324
325 DECL_BIT_FIELD (bitfield) = 1;
326 DECL_BIT_FIELD_TYPE (bitfield) = TREE_TYPE (bitfield);
327
328 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), bitfield);
329 }
330
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. */
335
336 static void
337 fixup_anonymous_offset (tree fields, tree offset)
338 {
339 /* No adjustment in field offset required. */
340 if (integer_zerop (offset))
341 return;
342
343 while (fields != NULL_TREE)
344 {
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)))
350 {
351 tree vfields = TYPE_FIELDS (ftype);
352 fixup_anonymous_offset (vfields, offset);
353 }
354 else
355 {
356 tree voffset = DECL_FIELD_OFFSET (fields);
357 DECL_FIELD_OFFSET (fields) = size_binop (MINUS_EXPR, voffset, offset);
358 }
359
360 fields = DECL_CHAIN (fields);
361 }
362 }
363
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. */
367
368 static size_t
369 layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p)
370 {
371 size_t fields = 0;
372
373 for (size_t i = 0; i < members->length; i++)
374 {
375 Dsymbol *sym = (*members)[i];
376 VarDeclaration *var = sym->isVarDeclaration ();
377 if (var != NULL)
378 {
379 /* Skip fields that have already been added. */
380 if (!inherited_p && var->csym != NULL)
381 continue;
382
383 /* If this variable was really a tuple, add all tuple fields. */
384 if (var->aliassym)
385 {
386 TupleDeclaration *td = var->aliassym->isTupleDeclaration ();
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++)
391 {
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 ();
397
398 tmembers.push (ve->var);
399 }
400
401 fields += layout_aggregate_members (&tmembers, context,
402 inherited_p);
403 continue;
404 }
405
406 /* Insert the field declaration at its given offset. */
407 if (var->isField ())
408 {
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);
414
415 if (BitFieldDeclaration *bf = var->isBitFieldDeclaration ())
416 {
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);
421 }
422 else
423 insert_aggregate_field (context, field, var->offset);
424
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)
429 {
430 DECL_LANG_SPECIFIC (field) = build_lang_decl (var);
431 var->csym = field;
432 }
433
434 /* Only count the named fields in an aggregate. */
435 if (ident != NULL)
436 fields += 1;
437
438 continue;
439 }
440 }
441
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)
446 {
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);
451
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;
457
458 TYPE_CONTEXT (type) = context;
459 TYPE_NAME (type) = decl;
460 TYPE_STUB_DECL (type) = decl;
461
462 /* Recursively iterator over the anonymous members. */
463 fields += layout_aggregate_members (ad->decl, type, inherited_p);
464
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);
470
471 finish_aggregate_type (ad->anonstructsize, ad->anonalignsize, type);
472
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;
478 }
479
480 /* Other kinds of attributes don't create a scope. */
481 AttribDeclaration *attrib = sym->isAttribDeclaration ();
482 if (attrib != NULL)
483 {
484 Dsymbols *decls = attrib->include (NULL);
485 if (decls != NULL)
486 {
487 fields += layout_aggregate_members (decls, context, inherited_p);
488 continue;
489 }
490 }
491
492 /* Same with template mixins and namespaces. */
493 if (sym->isTemplateMixin () || sym->isNspace ())
494 {
495 ScopeDsymbol *scopesym = sym->isScopeDsymbol ();
496 if (scopesym->members)
497 {
498 fields += layout_aggregate_members (scopesym->members, context,
499 inherited_p);
500 continue;
501 }
502 }
503 }
504
505 return fields;
506 }
507
508 /* Write out all fields for aggregate BASE. For classes, write out all
509 interfaces first, then the base class fields. */
510
511 static void
512 layout_aggregate_type (AggregateDeclaration *decl, tree type,
513 AggregateDeclaration *base)
514 {
515 ClassDeclaration *cd = base->isClassDeclaration ();
516 bool inherited_p = (decl != base);
517
518 if (cd != NULL)
519 {
520 if (cd->baseClass)
521 layout_aggregate_type (decl, type, cd->baseClass);
522 else
523 {
524 /* This is the base class (Object) or interface. */
525 tree objtype = TREE_TYPE (build_ctype (cd->type));
526
527 /* Add the vtable pointer, and optionally the monitor fields. */
528 InterfaceDeclaration *id = cd->isInterfaceDeclaration ();
529 if (!id || id->vtblInterfaces->length == 0)
530 {
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);
537 }
538
539 if (!id && cd->hasMonitor ())
540 {
541 tree field = create_field_decl (ptr_type_node, "__monitor", 1,
542 inherited_p);
543 insert_aggregate_field (type, field, target.ptrsize);
544 }
545 }
546
547 if (cd->vtblInterfaces)
548 {
549 for (size_t i = 0; i < cd->vtblInterfaces->length; i++)
550 {
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);
554 }
555 }
556 }
557
558 if (base->members)
559 {
560 size_t fields = layout_aggregate_members (base->members, type,
561 inherited_p);
562 gcc_assert (fields == base->fields.length);
563
564 /* Make sure that all fields have been created. */
565 if (!inherited_p)
566 {
567 for (size_t i = 0; i < base->fields.length; i++)
568 {
569 VarDeclaration *var = base->fields[i];
570 gcc_assert (var->csym != NULL);
571 }
572 }
573 }
574 }
575
576 /* If the aggregate type TYPE completes the type of any previous field
577 declarations, lay them out now. */
578
579 static void
580 finish_incomplete_fields (tree type)
581 {
582 for (tree fwdref = TYPE_FORWARD_REFERENCES (type); fwdref != NULL_TREE;
583 fwdref = TREE_CHAIN (fwdref))
584 {
585 tree field = TREE_VALUE (fwdref);
586 tree basetype = TREE_TYPE (field);
587
588 /* Arrays of TYPE have layout_type() called from build_array_type(), but
589 would skip over setting TYPE_SIZE. Try completing the type again. */
590 if (TREE_CODE (basetype) == ARRAY_TYPE)
591 {
592 while (TREE_CODE (TREE_TYPE (basetype)) == ARRAY_TYPE)
593 basetype = TREE_TYPE (basetype);
594
595 layout_type (basetype);
596 }
597
598 relayout_decl (field);
599 }
600
601 /* No more forward references to process. */
602 TYPE_FORWARD_REFERENCES (type) = NULL_TREE;
603 }
604
605 /* Given a record type TYPE, whose size and alignment are determined by
606 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
607 the finalized record mode. */
608
609 void
610 finish_aggregate_type (unsigned structsize, unsigned alignsize, tree type)
611 {
612 /* Set size and alignment as requested by frontend. */
613 TYPE_SIZE (type) = bitsize_int (structsize * BITS_PER_UNIT);
614 TYPE_SIZE_UNIT (type) = size_int (structsize);
615 SET_TYPE_ALIGN (type, alignsize * BITS_PER_UNIT);
616 TYPE_PACKED (type) = (alignsize == 1);
617
618 /* Set the back-end type mode. */
619 compute_record_mode (type);
620
621 /* Layout all fields now the type is complete. */
622 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
623 {
624 /* If the field type is still being constructed because of recursive
625 references, attach it to that class/struct type, so we can go back
626 and complete the field later. */
627 if (!COMPLETE_TYPE_P (TREE_TYPE (field)))
628 {
629 tree basetype = TREE_TYPE (field);
630 while (TREE_CODE (basetype) == ARRAY_TYPE)
631 basetype = TREE_TYPE (basetype);
632
633 basetype = TYPE_MAIN_VARIANT (basetype);
634 if (RECORD_OR_UNION_TYPE_P (basetype)
635 || TREE_CODE (basetype) == ENUMERAL_TYPE)
636 {
637 gcc_assert (!COMPLETE_TYPE_P (basetype));
638 tree fwdrefs = tree_cons (NULL_TREE, field,
639 TYPE_FORWARD_REFERENCES (basetype));
640 TYPE_FORWARD_REFERENCES (basetype) = fwdrefs;
641 }
642
643 continue;
644 }
645
646 layout_decl (field, 0);
647
648 /* Give bit-field its proper type after layout_decl. */
649 if (DECL_BIT_FIELD (field))
650 {
651 tree orig_type = DECL_BIT_FIELD_TYPE (field);
652 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_SIZE (field));
653
654 if (width != TYPE_PRECISION (orig_type))
655 {
656 bool unsignedp = TYPE_UNSIGNED (orig_type);
657
658 TREE_TYPE (field)
659 = d_build_bitfield_integer_type (width, unsignedp);
660 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
661 }
662 }
663 }
664
665 /* Fix up all forward-referenced variants of this aggregate type. */
666 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
667 {
668 if (t == type)
669 continue;
670
671 TYPE_FIELDS (t) = TYPE_FIELDS (type);
672 TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (type);
673 TYPE_SIZE (t) = TYPE_SIZE (type);
674 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (type);
675 TYPE_PACKED (type) = TYPE_PACKED (type);
676 SET_TYPE_MODE (t, TYPE_MODE (type));
677 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
678 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
679 }
680
681 /* Finish debugging output for this type. */
682 rest_of_type_compilation (type, TYPE_FILE_SCOPE_P (type));
683 finish_incomplete_fields (type);
684
685 /* Finish processing of TYPE_DECL. */
686 rest_of_decl_compilation (TYPE_NAME (type),
687 DECL_FILE_SCOPE_P (TYPE_NAME (type)), 0);
688 }
689
690 /* Returns true if the class or struct type TYPE has already been layed out by
691 the lowering of another front-end AST type. In which case, there will either
692 be a reuse of the back-end type, or a multiple definition error.
693 DECO is the uniquely mangled decoration for the type. */
694
695 static bool
696 merge_aggregate_types (Type *type, tree deco)
697 {
698 AggregateDeclaration *sym;
699
700 if (type->ty == TY::Tstruct)
701 sym = type->isTypeStruct ()->sym;
702 else if (type->ty == TY::Tclass)
703 sym = type->isTypeClass ()->sym;
704 else
705 gcc_unreachable ();
706
707 if (IDENTIFIER_DAGGREGATE (deco))
708 {
709 AggregateDeclaration *ad = IDENTIFIER_DAGGREGATE (deco);
710 /* There should never be a class/struct mismatch in mangled names. */
711 gcc_assert ((sym->isStructDeclaration () && ad->isStructDeclaration ())
712 || (sym->isClassDeclaration () && ad->isClassDeclaration ()));
713
714 /* Non-templated variables shouldn't be defined twice. */
715 if (!sym->isInstantiated ())
716 ScopeDsymbol::multiplyDefined (sym->loc, sym, ad);
717
718 type->ctype = build_ctype (ad->type);
719 return true;
720 }
721
722 return false;
723 }
724
725 /* Implements the visitor interface to build the GCC trees of all
726 Type AST classes emitted from the D Front-end, where CTYPE holds
727 the cached back-end representation to be returned. */
728
729 class TypeVisitor : public Visitor
730 {
731 using Visitor::visit;
732
733 public:
734 TypeVisitor (void)
735 {
736 }
737
738 /* This should be overridden by each type class. */
739
740 void visit (Type *) final override
741 {
742 gcc_unreachable ();
743 }
744
745 /* Type assigned to erroneous expressions or constructs that
746 failed during the semantic stage. */
747
748 void visit (TypeError *t) final override
749 {
750 t->ctype = error_mark_node;
751 }
752
753 /* Type assigned to generic nullable types. */
754
755 void visit (TypeNull *t) final override
756 {
757 t->ctype = ptr_type_node;
758 }
759
760 /* Bottom type used for functions that never return. */
761
762 void visit (TypeNoreturn *t) final override
763 {
764 t->ctype = noreturn_type_node;
765 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
766 }
767
768 /* Basic Data Types. */
769
770 void visit (TypeBasic *t) final override
771 {
772 /* [type/basic-data-types]
773
774 void no type.
775 bool 8-bit boolean value.
776 byte 8-bit signed value.
777 ubyte 8-bit unsigned value.
778 short 16-bit signed value.
779 ushort 16-bit unsigned value.
780 int 32-bit signed value.
781 uint 32-bit unsigned value.
782 long 64-bit signed value.
783 ulong 64-bit unsigned value.
784 cent 128-bit signed value.
785 ucent 128-bit unsigned value.
786 float 32-bit IEEE 754 floating-point value.
787 double 64-bit IEEE 754 floating-point value.
788 real largest FP size implemented in hardware.
789 ifloat imaginary float.
790 idouble imaginary double.
791 ireal imaginary real.
792 cfloat complex float.
793 cdouble complex double.
794 creal complex real.
795 char UTF-8 code unit.
796 wchar UTF-16 code unit.
797 dchar UTF-32 code unit. */
798
799 switch (t->ty)
800 {
801 case TY::Tvoid: t->ctype = void_type_node; break;
802 case TY::Tbool: t->ctype = d_bool_type; break;
803 case TY::Tint8: t->ctype = d_byte_type; break;
804 case TY::Tuns8: t->ctype = d_ubyte_type; break;
805 case TY::Tint16: t->ctype = d_short_type; break;
806 case TY::Tuns16: t->ctype = d_ushort_type; break;
807 case TY::Tint32: t->ctype = d_int_type; break;
808 case TY::Tuns32: t->ctype = d_uint_type; break;
809 case TY::Tint64: t->ctype = d_long_type; break;
810 case TY::Tuns64: t->ctype = d_ulong_type; break;
811 case TY::Tint128: t->ctype = d_cent_type; break;
812 case TY::Tuns128: t->ctype = d_ucent_type; break;
813 case TY::Tfloat32: t->ctype = float_type_node; break;
814 case TY::Tfloat64: t->ctype = double_type_node; break;
815 case TY::Tfloat80: t->ctype = long_double_type_node; break;
816 case TY::Timaginary32: t->ctype = ifloat_type_node; break;
817 case TY::Timaginary64: t->ctype = idouble_type_node; break;
818 case TY::Timaginary80: t->ctype = ireal_type_node; break;
819 case TY::Tcomplex32: t->ctype = complex_float_type_node; break;
820 case TY::Tcomplex64: t->ctype = complex_double_type_node; break;
821 case TY::Tcomplex80: t->ctype = complex_long_double_type_node; break;
822 case TY::Tchar: t->ctype = char8_type_node; break;
823 case TY::Twchar: t->ctype = char16_type_node; break;
824 case TY::Tdchar: t->ctype = char32_type_node; break;
825 default: gcc_unreachable ();
826 }
827
828 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
829 }
830
831
832 /* Derived Data Types. */
833
834 /* Build a simple pointer to data type, analogous to C pointers. */
835
836 void visit (TypePointer *t) final override
837 {
838 t->ctype = build_pointer_type (build_ctype (t->next));
839 }
840
841 /* Build a dynamic array type, consisting of a length and a pointer
842 to the array data. */
843
844 void visit (TypeDArray *t) final override
845 {
846 /* In [abi/arrays], dynamic array layout is:
847 .length array dimension.
848 .ptr pointer to array data. */
849 t->ctype = make_struct_type (t->toChars (), 2,
850 get_identifier ("length"),
851 build_ctype (Type::tsize_t),
852 get_identifier ("ptr"),
853 build_pointer_type (build_ctype (t->next)));
854 TYPE_DYNAMIC_ARRAY (t->ctype) = 1;
855 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
856 d_keep (t->ctype);
857 }
858
859 /* Build a static array type, distinguished from dynamic arrays by
860 having a length fixed at compile-time, analogous to C arrays. */
861
862 void visit (TypeSArray *t) final override
863 {
864 if (t->dim->isConst () && t->dim->type->isintegral ())
865 {
866 uinteger_t size = t->dim->toUInteger ();
867 t->ctype = make_array_type (t->next, size);
868 }
869 else
870 {
871 error ("invalid expression for static array dimension: %s",
872 t->dim->toChars ());
873 gcc_unreachable ();
874 }
875 }
876
877 /* Build a vector type, a fixed array of floating or integer types. */
878
879 void visit (TypeVector *t) final override
880 {
881 int nunits = t->basetype->isTypeSArray ()->dim->toUInteger ();
882 tree inner = build_ctype (t->elementType ());
883
884 /* Same rationale as void static arrays. */
885 if (inner == void_type_node)
886 inner = build_ctype (Type::tuns8);
887
888 t->ctype = build_vector_type (inner, nunits);
889 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
890 layout_type (t->ctype);
891 }
892
893 /* Build an associative array type, distinguished from arrays by having an
894 index that's not necessarily an integer, and can be sparsely populated. */
895
896 void visit (TypeAArray *t) final override
897 {
898 /* In [abi/associative-arrays], associative arrays are a struct that only
899 consist of a pointer to an opaque, implementation defined type. */
900 t->ctype = make_struct_type (t->toChars (), 1,
901 get_identifier ("ptr"), ptr_type_node);
902 TYPE_ASSOCIATIVE_ARRAY (t->ctype) = 1;
903 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
904 d_keep (t->ctype);
905 }
906
907 /* Build type for a function declaration, which consists of a return type,
908 and a list of parameter types, and a linkage attribute. */
909
910 void visit (TypeFunction *t) final override
911 {
912 tree fnparams = NULL_TREE;
913 tree fntype;
914
915 /* [function/variadic]
916
917 Variadic functions with D linkage have an additional hidden argument
918 with the name _arguments passed to the function. */
919 if (t->isDstyleVariadic ())
920 {
921 tree type = build_ctype (Type::typeinfotypelist->type);
922 fnparams = chainon (fnparams, build_tree_list (0, type));
923 }
924
925 const size_t n_args = t->parameterList.length ();
926
927 for (size_t i = 0; i < n_args; i++)
928 {
929 tree type = parameter_type (t->parameterList[i]);
930
931 /* Type `noreturn` is a terminator, as no other arguments can possibly
932 be evaluated after it. */
933 if (type == noreturn_type_node)
934 break;
935
936 fnparams = chainon (fnparams, build_tree_list (0, type));
937 }
938
939 /* When the last parameter is void_list_node, that indicates a fixed length
940 parameter list, otherwise function is treated as variadic. */
941 if (t->parameterList.varargs != VARARGvariadic)
942 fnparams = chainon (fnparams, void_list_node);
943
944 if (t->next != NULL)
945 {
946 fntype = build_ctype (t->next);
947 if (t->isref ())
948 fntype = build_reference_type (fntype);
949 }
950 else
951 fntype = void_type_node;
952
953 /* Could the function type be self referenced by parameters? */
954 t->ctype = build_function_type (fntype, fnparams);
955 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
956 d_keep (t->ctype);
957
958 /* Qualify function types that have the type `noreturn` as volatile. */
959 if (fntype == noreturn_type_node)
960 t->ctype = build_qualified_type (t->ctype, TYPE_QUAL_VOLATILE);
961
962 /* Handle any special support for calling conventions. */
963 switch (t->linkage)
964 {
965 case LINK::windows:
966 {
967 /* [attribute/linkage]
968
969 The Windows convention is distinct from the C convention only
970 on Win32, where it is equivalent to the stdcall convention. */
971 unsigned link_system, link_windows;
972 if (targetdm.d_has_stdcall_convention (&link_system, &link_windows))
973 {
974 if (link_windows)
975 t->ctype = insert_type_attribute (t->ctype, "stdcall");
976 }
977 break;
978 }
979
980 case LINK::c:
981 case LINK::cpp:
982 case LINK::d:
983 case LINK::objc:
984 /* [abi/function-calling-conventions]
985
986 The extern (C) and extern (D) calling convention matches
987 the C calling convention used by the supported C compiler
988 on the host system. */
989 break;
990
991 default:
992 gcc_unreachable ();
993 }
994 }
995
996 /* Build a delegate type, an aggregate of two pieces of data, an object
997 reference and a pointer to a non-static member function, or a pointer
998 to a closure and a pointer to a nested function. */
999
1000 void visit (TypeDelegate *t) final override
1001 {
1002 /* In [abi/delegates], delegate layout is:
1003 .ptr context pointer.
1004 .funcptr pointer to function. */
1005 tree fntype = build_ctype (t->next);
1006 tree dgtype = build_vthis_function (void_type_node, fntype);
1007
1008 TYPE_ATTRIBUTES (dgtype) = TYPE_ATTRIBUTES (fntype);
1009 TYPE_LANG_SPECIFIC (dgtype) = TYPE_LANG_SPECIFIC (fntype);
1010
1011 t->ctype = make_struct_type (t->toChars (), 2,
1012 get_identifier ("ptr"),
1013 build_ctype (Type::tvoidptr),
1014 get_identifier ("funcptr"),
1015 build_pointer_type (dgtype));
1016 TYPE_DELEGATE (t->ctype) = 1;
1017 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1018 d_keep (t->ctype);
1019 }
1020
1021
1022 /* User Defined Types. */
1023
1024 /* Build a named enum type, a distinct value whose values are restrict to
1025 a group of constants of the same underlying base type. */
1026
1027 void visit (TypeEnum *t) final override
1028 {
1029 tree basetype = (t->sym->memtype)
1030 ? build_ctype (t->sym->memtype) : void_type_node;
1031
1032 if (t->sym->isSpecial ())
1033 {
1034 /* Special enums are opaque types that bind to C types. */
1035 const char *ident = t->toChars ();
1036 Type *underlying = NULL;
1037
1038 /* Skip over the prefixing `__c_'. */
1039 gcc_assert (startswith (ident, "__c_"));
1040 ident = ident + strlen ("__c_");
1041
1042 /* To keep things compatible within the code generation we stick to
1043 mapping to equivalent D types. However it should be OK to use the
1044 GCC provided C types here as the front-end enforces that everything
1045 must be explicitly cast from a D type to any of the opaque types. */
1046 if (strcmp (ident, "long") == 0)
1047 underlying = build_frontend_type (long_integer_type_node);
1048 else if (strcmp (ident, "ulong") == 0)
1049 underlying = build_frontend_type (long_unsigned_type_node);
1050 else if (strcmp (ident, "wchar_t") == 0)
1051 underlying =
1052 build_frontend_type (make_unsigned_type (WCHAR_TYPE_SIZE));
1053 else if (strcmp (ident, "longlong") == 0)
1054 underlying = build_frontend_type (long_long_integer_type_node);
1055 else if (strcmp (ident, "ulonglong") == 0)
1056 underlying = build_frontend_type (long_long_unsigned_type_node);
1057 else if (strcmp (ident, "long_double") == 0)
1058 underlying = build_frontend_type (long_double_type_node);
1059 else if (strcmp (ident, "complex_real") == 0)
1060 underlying = build_frontend_type (complex_long_double_type_node);
1061 else if (strcmp (ident, "complex_float") == 0)
1062 underlying = build_frontend_type (complex_float_type_node);
1063 else if (strcmp (ident, "complex_double") == 0)
1064 underlying = build_frontend_type (complex_double_type_node);
1065
1066 /* Conversion failed or there's an unhandled special type. */
1067 gcc_assert (underlying != NULL);
1068
1069 t->ctype = build_variant_type_copy (build_ctype (underlying));
1070 build_type_decl (t->ctype, t->sym);
1071 }
1072 else if (t->sym->ident == NULL
1073 || !INTEGRAL_TYPE_P (basetype)
1074 || TREE_CODE (basetype) == BOOLEAN_TYPE)
1075 {
1076 /* Enums in D2 can either be anonymous, or have a base type that is not
1077 necessarily integral. For these, we simplify this a little by using
1078 the base type directly instead of building an ENUMERAL_TYPE. */
1079 t->ctype = build_variant_type_copy (basetype);
1080
1081 if (t->sym->ident != NULL)
1082 build_type_decl (t->ctype, t->sym);
1083 }
1084 else
1085 {
1086 t->ctype = make_node (ENUMERAL_TYPE);
1087 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1088 d_keep (t->ctype);
1089
1090 ENUM_IS_SCOPED (t->ctype) = 1;
1091 TREE_TYPE (t->ctype) = basetype;
1092
1093 if (flag_short_enums)
1094 TYPE_PACKED (t->ctype) = 1;
1095
1096 tree values = NULL_TREE;
1097 if (t->sym->members)
1098 {
1099 for (size_t i = 0; i < t->sym->members->length; i++)
1100 {
1101 EnumMember *member = (*t->sym->members)[i]->isEnumMember ();
1102 /* Templated functions can seep through to the back-end
1103 just ignore for now. */
1104 if (member == NULL)
1105 continue;
1106
1107 tree ident = get_identifier (member->ident->toChars ());
1108 tree value = build_integer_cst (member->value ()->toInteger (),
1109 basetype);
1110
1111 /* Build an identifier for the enumeration constant. */
1112 tree decl = build_decl (make_location_t (member->loc),
1113 CONST_DECL, ident, basetype);
1114 DECL_CONTEXT (decl) = t->ctype;
1115 TREE_CONSTANT (decl) = 1;
1116 TREE_READONLY (decl) = 1;
1117 DECL_INITIAL (decl) = value;
1118
1119 /* Add this enumeration constant to the list for this type. */
1120 values = chainon (values, build_tree_list (ident, decl));
1121 }
1122 }
1123
1124 TYPE_VALUES (t->ctype) = values;
1125 build_type_decl (t->ctype, t->sym);
1126 }
1127
1128 apply_user_attributes (t->sym, t->ctype);
1129
1130 /* Finish the enumeration type. */
1131 if (TREE_CODE (t->ctype) == ENUMERAL_TYPE)
1132 {
1133 TYPE_MIN_VALUE (t->ctype) = TYPE_MIN_VALUE (basetype);
1134 TYPE_MAX_VALUE (t->ctype) = TYPE_MAX_VALUE (basetype);
1135 TYPE_UNSIGNED (t->ctype) = TYPE_UNSIGNED (basetype);
1136 SET_TYPE_ALIGN (t->ctype, TYPE_ALIGN (basetype));
1137 TYPE_SIZE (t->ctype) = NULL_TREE;
1138 TYPE_PRECISION (t->ctype) = t->size (t->sym->loc) * 8;
1139
1140 layout_type (t->ctype);
1141
1142 /* Finish debugging output for this type. */
1143 rest_of_type_compilation (t->ctype, TYPE_FILE_SCOPE_P (t->ctype));
1144 finish_incomplete_fields (t->ctype);
1145
1146 /* Finish processing of TYPE_DECL. */
1147 rest_of_decl_compilation (TYPE_NAME (t->ctype),
1148 DECL_FILE_SCOPE_P (TYPE_NAME (t->ctype)), 0);
1149 }
1150 }
1151
1152 /* Build a struct or union type. Layout should be exactly represented
1153 as an equivalent C struct, except for non-POD or nested structs. */
1154
1155 void visit (TypeStruct *t) final override
1156 {
1157 /* Merge types in the back-end if the front-end did not itself do so. */
1158 tree deco = get_identifier (d_mangle_decl (t->sym));
1159 if (merge_aggregate_types (t, deco))
1160 return;
1161
1162 /* Need to set this right away in case of self-references. */
1163 t->ctype = make_node (t->sym->isUnionDeclaration ()
1164 ? UNION_TYPE : RECORD_TYPE);
1165 d_keep (t->ctype);
1166 IDENTIFIER_DAGGREGATE (deco) = t->sym;
1167
1168 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1169 TYPE_CXX_ODR_P (t->ctype) = 1;
1170
1171 if (t->sym->members)
1172 {
1173 /* Must set up the overall size and alignment before determining
1174 the context or laying out fields as those types may make
1175 recursive references to this type. */
1176 unsigned structsize = t->sym->structsize;
1177 unsigned alignsize = t->sym->alignment.isDefault ()
1178 ? t->sym->alignsize : t->sym->alignment.get ();
1179
1180 /* Put out all fields. */
1181 layout_aggregate_type (t->sym, t->ctype, t->sym);
1182 build_type_decl (t->ctype, t->sym);
1183 set_visibility_for_decl (t->ctype, t->sym);
1184 apply_user_attributes (t->sym, t->ctype);
1185 finish_aggregate_type (structsize, alignsize, t->ctype);
1186 }
1187
1188 /* For structs with a user defined postblit, copy constructor, or a
1189 destructor, also set TREE_ADDRESSABLE on the type and all variants.
1190 This will make the struct be passed around by reference. */
1191 if (!t->sym->isPOD ())
1192 {
1193 for (tree tv = t->ctype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
1194 {
1195 TREE_ADDRESSABLE (tv) = 1;
1196 SET_TYPE_MODE (tv, BLKmode);
1197 }
1198 }
1199 }
1200
1201 /* Build a class type. Whereas structs are value types, classes are
1202 reference types, with all the object-orientated features. */
1203
1204 void visit (TypeClass *t) final override
1205 {
1206 /* Merge types in the back-end if the front-end did not itself do so. */
1207 tree deco = get_identifier (d_mangle_decl (t->sym));
1208 if (merge_aggregate_types (t, deco))
1209 return;
1210
1211 /* Need to set ctype right away in case of self-references to
1212 the type during this call. */
1213 tree basetype = make_node (RECORD_TYPE);
1214 t->ctype = build_pointer_type (basetype);
1215 d_keep (t->ctype);
1216 IDENTIFIER_DAGGREGATE (deco) = t->sym;
1217
1218 /* Note that lang_specific data is assigned to both the reference
1219 and the underlying record type. */
1220 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
1221 TYPE_LANG_SPECIFIC (basetype) = TYPE_LANG_SPECIFIC (t->ctype);
1222 CLASS_TYPE_P (basetype) = 1;
1223 TYPE_CXX_ODR_P (basetype) = 1;
1224
1225 /* Put out all fields, including from each base class. */
1226 layout_aggregate_type (t->sym, basetype, t->sym);
1227 build_type_decl (basetype, t->sym);
1228 set_visibility_for_decl (basetype, t->sym);
1229 apply_user_attributes (t->sym, basetype);
1230 finish_aggregate_type (t->sym->structsize, t->sym->alignsize, basetype);
1231
1232 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
1233 for (tree tv = basetype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
1234 {
1235 TREE_ADDRESSABLE (tv) = 1;
1236 SET_TYPE_MODE (tv, BLKmode);
1237 }
1238
1239 /* Type is final, there are no derivations. */
1240 if (t->sym->storage_class & STCfinal)
1241 TYPE_FINAL_P (basetype) = 1;
1242
1243 /* Create BINFO even if debugging is off. This is needed to keep
1244 references to inherited types. */
1245 if (!t->sym->isInterfaceDeclaration ())
1246 TYPE_BINFO (basetype) = build_class_binfo (NULL_TREE, t->sym);
1247 else
1248 {
1249 unsigned offset = 0;
1250
1251 TYPE_BINFO (basetype) = build_interface_binfo (NULL_TREE, t->sym,
1252 offset);
1253 }
1254
1255 /* Associate all virtual methods with the class too. */
1256 for (size_t i = 0; i < t->sym->vtbl.length; i++)
1257 {
1258 FuncDeclaration *fd = t->sym->vtbl[i]->isFuncDeclaration ();
1259 tree method = fd ? get_symbol_decl (fd) : error_mark_node;
1260
1261 if (!error_operand_p (method)
1262 && DECL_CONTEXT (method) == basetype
1263 && !chain_member (method, TYPE_FIELDS (basetype)))
1264 TYPE_FIELDS (basetype) = chainon (TYPE_FIELDS (basetype), method);
1265 }
1266 }
1267 };
1268
1269
1270 /* Build a tree from a frontend Type. */
1271
1272 tree
1273 build_ctype (Type *t)
1274 {
1275 if (!t->ctype)
1276 {
1277 TypeVisitor v;
1278
1279 /* Strip const modifiers from type before building. This is done
1280 to ensure that back-end treats e.g: const (T) as a variant of T,
1281 and not as two distinct types. */
1282 if (t->isNaked ())
1283 t->accept (&v);
1284 else
1285 {
1286 Type *tb = t->castMod (0);
1287 if (!tb->ctype)
1288 tb->accept (&v);
1289 t->ctype = insert_type_modifiers (tb->ctype, t->mod);
1290 }
1291 }
1292
1293 return t->ctype;
1294 }