Debian Codegraph @

gcc/d/types.cc

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