]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/types.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / d / types.cc
1 /* types.cc -- Lower D frontend types to GCC trees.
2 Copyright (C) 2006-2020 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
44
45 /* Return TRUE if TYPE is a static array va_list. This is for compatibility
46 with the C ABI, where va_list static arrays are passed by reference.
47 However for every other case in D, static arrays are passed by value. */
48
49 bool
50 valist_array_p (Type *type)
51 {
52 if (Type::tvalist->ty == Tsarray)
53 {
54 Type *tb = type->toBasetype ();
55 if (same_type_p (tb, Type::tvalist))
56 return true;
57 }
58
59 return false;
60 }
61
62 /* Returns true if TYPE contains no actual data, just various
63 possible combinations of empty aggregates. */
64
65 bool
66 empty_aggregate_p (tree type)
67 {
68 if (!AGGREGATE_TYPE_P (type))
69 return false;
70
71 /* Want the element type for arrays. */
72 if (TREE_CODE (type) == ARRAY_TYPE)
73 return empty_aggregate_p (TREE_TYPE (type));
74
75 /* Recursively check all fields. */
76 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
77 {
78 if (TREE_CODE (field) == FIELD_DECL
79 && !empty_aggregate_p (TREE_TYPE (field)))
80 return false;
81 }
82
83 return true;
84 }
85
86 /* Returns true if T1 and T2 are related to each other. */
87
88 bool
89 same_type_p (Type *t1, Type *t2)
90 {
91 /* Types are equal. */
92 if (t1 == t2)
93 return true;
94
95 /* Types derive from the same base. */
96 Type *tb1 = t1->toBasetype ();
97 Type *tb2 = t2->toBasetype ();
98 if (tb1 == tb2)
99 return true;
100
101 /* Types are mutably the same type. */
102 if (tb1->ty == tb2->ty && tb1->equivalent (tb2))
103 return true;
104
105 return false;
106 }
107
108 /* Returns 'Object' type which all D classes are derived from. */
109
110 Type *
111 get_object_type (void)
112 {
113 if (ClassDeclaration::object)
114 return ClassDeclaration::object->type;
115
116 error ("missing or corrupt object.d");
117 return Type::terror;
118 }
119
120
121 /* Returns a static array of TYPE which has SIZE number of elements. */
122
123 tree
124 make_array_type (Type *type, unsigned HOST_WIDE_INT size)
125 {
126 /* In [arrays/void-arrays], void arrays can also be static, the length is
127 specified in bytes. */
128 if (type->toBasetype ()->ty == Tvoid)
129 type = Type::tuns8;
130
131 /* In [arrays/static-arrays], a static array with a dimension of 0 is allowed,
132 but no space is allocated for it. */
133 if (size == 0)
134 {
135 tree range = lang_hooks.types.type_for_size (TYPE_PRECISION (sizetype),
136 TYPE_UNSIGNED (sizetype));
137 tree index = build_range_type (range, size_zero_node, NULL_TREE);
138
139 tree t = build_array_type (build_ctype (type), index);
140 TYPE_SIZE (t) = bitsize_zero_node;
141 TYPE_SIZE_UNIT (t) = size_zero_node;
142 return t;
143 }
144
145 return build_array_type (build_ctype (type),
146 build_index_type (size_int (size - 1)));
147 }
148
149 /* Builds a record type whose name is NAME. NFIELDS is the number of fields,
150 provided as field ident/type pairs. */
151
152 tree
153 make_struct_type (const char *name, int nfields, ...)
154 {
155 tree fields = NULL_TREE;
156 va_list ap;
157
158 va_start (ap, nfields);
159
160 for (int i = 0; i < nfields; i++)
161 {
162 tree ident = va_arg (ap, tree);
163 tree type = va_arg (ap, tree);
164 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL, ident, type);
165 DECL_CHAIN (field) = fields;
166 fields = field;
167 }
168
169 va_end (ap);
170
171 tree type = make_node (RECORD_TYPE);
172 finish_builtin_struct (type, name, fields, NULL_TREE);
173
174 return type;
175 }
176
177 /* Return qualified type variant of TYPE determined by modifier value MOD. */
178
179 tree
180 insert_type_modifiers (tree type, unsigned mod)
181 {
182 int quals = 0;
183
184 switch (mod)
185 {
186 case MODconst:
187 case MODwild:
188 case MODwildconst:
189 case MODimmutable:
190 case MODshared | MODconst:
191 case MODshared | MODwild:
192 case MODshared | MODwildconst:
193 quals |= TYPE_QUAL_CONST;
194 break;
195
196 case 0:
197 case MODshared:
198 break;
199
200 default:
201 gcc_unreachable ();
202 }
203
204 tree qualtype = build_qualified_type (type, quals);
205
206 /* Mark whether the type is qualified 'shared'. */
207 if (mod & MODshared)
208 TYPE_SHARED (qualtype) = 1;
209
210 return qualtype;
211 }
212
213 /* Adds FIELD into the aggregate TYPE at OFFSET. */
214
215 void
216 insert_aggregate_field (tree type, tree field, size_t offset)
217 {
218 DECL_FIELD_CONTEXT (field) = type;
219 SET_DECL_OFFSET_ALIGN (field, TYPE_ALIGN (TREE_TYPE (field)));
220 DECL_FIELD_OFFSET (field) = size_int (offset);
221 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
222
223 TREE_ADDRESSABLE (field) = TYPE_SHARED (TREE_TYPE (field));
224
225 layout_decl (field, 0);
226 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), field);
227 }
228
229 /* For all decls in the FIELDS chain, adjust their field offset by OFFSET.
230 This is done as the frontend puts fields into the outer struct, and so
231 their offset is from the beginning of the aggregate.
232 We want the offset to be from the beginning of the anonymous aggregate. */
233
234 static void
235 fixup_anonymous_offset (tree fields, tree offset)
236 {
237 while (fields != NULL_TREE)
238 {
239 /* Traverse all nested anonymous aggregates to update their offset.
240 Set the anonymous decl offset to its first member. */
241 tree ftype = TREE_TYPE (fields);
242 if (TYPE_NAME (ftype) && IDENTIFIER_ANON_P (TYPE_IDENTIFIER (ftype)))
243 {
244 tree vfields = TYPE_FIELDS (ftype);
245 fixup_anonymous_offset (vfields, offset);
246 DECL_FIELD_OFFSET (fields) = DECL_FIELD_OFFSET (vfields);
247 }
248 else
249 {
250 tree voffset = DECL_FIELD_OFFSET (fields);
251 DECL_FIELD_OFFSET (fields) = size_binop (MINUS_EXPR, voffset, offset);
252 }
253
254 fields = DECL_CHAIN (fields);
255 }
256 }
257
258 /* Iterate over all MEMBERS of an aggregate, and add them as fields to CONTEXT.
259 If INHERITED_P is true, then the members derive from a base class.
260 Returns the number of fields found. */
261
262 static size_t
263 layout_aggregate_members (Dsymbols *members, tree context, bool inherited_p)
264 {
265 size_t fields = 0;
266
267 for (size_t i = 0; i < members->dim; i++)
268 {
269 Dsymbol *sym = (*members)[i];
270 VarDeclaration *var = sym->isVarDeclaration ();
271 if (var != NULL)
272 {
273 /* Skip fields that have already been added. */
274 if (!inherited_p && var->csym != NULL)
275 continue;
276
277 /* If this variable was really a tuple, add all tuple fields. */
278 if (var->aliassym)
279 {
280 TupleDeclaration *td = var->aliassym->isTupleDeclaration ();
281 Dsymbols tmembers;
282 /* No other way to coerce the underlying type out of the tuple.
283 Frontend should have already validated this. */
284 for (size_t j = 0; j < td->objects->dim; j++)
285 {
286 RootObject *ro = (*td->objects)[j];
287 gcc_assert (ro->dyncast () == DYNCAST_EXPRESSION);
288 Expression *e = (Expression *) ro;
289 gcc_assert (e->op == TOKdsymbol);
290 DsymbolExp *se = (DsymbolExp *) e;
291
292 tmembers.push (se->s);
293 }
294
295 fields += layout_aggregate_members (&tmembers, context,
296 inherited_p);
297 continue;
298 }
299
300 /* Insert the field declaration at its given offset. */
301 if (var->isField ())
302 {
303 const char *ident = var->ident ? var->ident->toChars () : NULL;
304 tree field = create_field_decl (declaration_type (var), ident,
305 inherited_p, inherited_p);
306 insert_aggregate_field (context, field, var->offset);
307
308 /* Because the front-end shares field decls across classes, don't
309 create the corresponding back-end symbol unless we are adding
310 it to the aggregate it is defined in. */
311 if (!inherited_p)
312 {
313 DECL_LANG_SPECIFIC (field) = build_lang_decl (var);
314 var->csym = field;
315 }
316
317 fields += 1;
318 continue;
319 }
320 }
321
322 /* Anonymous struct/union are flattened by the frontend. However, we
323 want to keep the record layout in-tact when building the type. */
324 AnonDeclaration *ad = sym->isAnonDeclaration ();
325 if (ad != NULL)
326 {
327 tree ident = make_anon_name ();
328 tree type = make_node (ad->isunion ? UNION_TYPE : RECORD_TYPE);
329 ANON_AGGR_TYPE_P (type) = 1;
330 d_keep (type);
331
332 /* Build the type declaration. */
333 tree decl = build_decl (make_location_t (ad->loc),
334 TYPE_DECL, ident, type);
335 DECL_CONTEXT (decl) = context;
336 DECL_ARTIFICIAL (decl) = 1;
337
338 TYPE_CONTEXT (type) = context;
339 TYPE_NAME (type) = decl;
340 TYPE_STUB_DECL (type) = decl;
341
342 /* Recursively iterator over the anonymous members. */
343 fields += layout_aggregate_members (ad->decl, type, inherited_p);
344
345 /* Remove from the anon fields the base offset of this anonymous
346 aggregate. Undoes what is set-up in setFieldOffset, but doesn't
347 affect field accesses. */
348 tree offset = size_int (ad->anonoffset);
349 fixup_anonymous_offset (TYPE_FIELDS (type), offset);
350
351 finish_aggregate_type (ad->anonstructsize, ad->anonalignsize,
352 type, NULL);
353
354 /* And make the corresponding data member. */
355 tree field = create_field_decl (type, NULL, 0, 0);
356 insert_aggregate_field (context, field, ad->anonoffset);
357 continue;
358 }
359
360 /* Other kinds of attributes don't create a scope. */
361 AttribDeclaration *attrib = sym->isAttribDeclaration ();
362 if (attrib != NULL)
363 {
364 Dsymbols *decls = attrib->include (NULL, NULL);
365 if (decls != NULL)
366 {
367 fields += layout_aggregate_members (decls, context, inherited_p);
368 continue;
369 }
370 }
371
372 /* Same with template mixins and namespaces. */
373 if (sym->isTemplateMixin () || sym->isNspace ())
374 {
375 ScopeDsymbol *scopesym = sym->isScopeDsymbol ();
376 if (scopesym->members)
377 {
378 fields += layout_aggregate_members (scopesym->members, context,
379 inherited_p);
380 continue;
381 }
382 }
383 }
384
385 return fields;
386 }
387
388 /* Write out all fields for aggregate BASE. For classes, write out all
389 interfaces first, then the base class fields. */
390
391 static void
392 layout_aggregate_type (AggregateDeclaration *decl, tree type,
393 AggregateDeclaration *base)
394 {
395 ClassDeclaration *cd = base->isClassDeclaration ();
396 bool inherited_p = (decl != base);
397
398 if (cd != NULL)
399 {
400 if (cd->baseClass)
401 layout_aggregate_type (decl, type, cd->baseClass);
402 else
403 {
404 /* This is the base class (Object) or interface. */
405 tree objtype = TREE_TYPE (build_ctype (cd->type));
406
407 /* Add the vtable pointer, and optionally the monitor fields. */
408 InterfaceDeclaration *id = cd->isInterfaceDeclaration ();
409 if (!id || id->vtblInterfaces->dim == 0)
410 {
411 tree field = create_field_decl (vtbl_ptr_type_node, "__vptr", 1,
412 inherited_p);
413 DECL_VIRTUAL_P (field) = 1;
414 TYPE_VFIELD (type) = field;
415 DECL_FCONTEXT (field) = objtype;
416 insert_aggregate_field (type, field, 0);
417 }
418
419 if (!id && !cd->isCPPclass ())
420 {
421 tree field = create_field_decl (ptr_type_node, "__monitor", 1,
422 inherited_p);
423 insert_aggregate_field (type, field, Target::ptrsize);
424 }
425 }
426
427 if (cd->vtblInterfaces)
428 {
429 for (size_t i = 0; i < cd->vtblInterfaces->dim; i++)
430 {
431 BaseClass *bc = (*cd->vtblInterfaces)[i];
432 tree field = create_field_decl (vtbl_ptr_type_node, NULL, 1, 1);
433 insert_aggregate_field (type, field, bc->offset);
434 }
435 }
436 }
437
438 if (base->members)
439 {
440 size_t fields = layout_aggregate_members (base->members, type,
441 inherited_p);
442 gcc_assert (fields == base->fields.dim);
443
444 /* Make sure that all fields have been created. */
445 if (!inherited_p)
446 {
447 for (size_t i = 0; i < base->fields.dim; i++)
448 {
449 VarDeclaration *var = base->fields[i];
450 gcc_assert (var->csym != NULL);
451 }
452 }
453 }
454 }
455
456 /* Given a record type TYPE, whose size and alignment are determined by
457 STRUCTSIZE and ALIGNSIZE. Apply any type attributes ATTRS and compute
458 the finalized record mode. */
459
460 void
461 finish_aggregate_type (unsigned structsize, unsigned alignsize,
462 tree type, UserAttributeDeclaration *attrs)
463 {
464 TYPE_SIZE (type) = NULL_TREE;
465
466 /* Write out any GCC attributes that were applied to the type declaration. */
467 if (attrs)
468 {
469 Expressions *eattrs = attrs->getAttributes ();
470 decl_attributes (&type, build_attributes (eattrs),
471 ATTR_FLAG_TYPE_IN_PLACE);
472 }
473
474 /* Set size and alignment as requested by frontend. */
475 TYPE_SIZE (type) = bitsize_int (structsize * BITS_PER_UNIT);
476 TYPE_SIZE_UNIT (type) = size_int (structsize);
477 SET_TYPE_ALIGN (type, alignsize * BITS_PER_UNIT);
478 TYPE_PACKED (type) = (alignsize == 1);
479
480 /* Set the back-end type mode. */
481 compute_record_mode (type);
482
483 /* Fix up all variants of this aggregate type. */
484 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
485 {
486 if (t == type)
487 continue;
488
489 TYPE_FIELDS (t) = TYPE_FIELDS (type);
490 TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (type);
491 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
492 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
493 gcc_assert (TYPE_MODE (t) == TYPE_MODE (type));
494 }
495 }
496
497
498 /* Implements the visitor interface to build the GCC trees of all
499 Type AST classes emitted from the D Front-end, where CTYPE holds
500 the cached back-end representation to be returned. */
501
502 class TypeVisitor : public Visitor
503 {
504 using Visitor::visit;
505
506 public:
507 TypeVisitor (void)
508 {
509 }
510
511 /* This should be overridden by each type class. */
512
513 void visit (Type *)
514 {
515 gcc_unreachable ();
516 }
517
518 /* Type assigned to erroneous expressions or constructs that
519 failed during the semantic stage. */
520
521 void visit (TypeError *t)
522 {
523 t->ctype = error_mark_node;
524 }
525
526 /* Type assigned to generic nullable types. */
527
528 void visit (TypeNull *t)
529 {
530 t->ctype = ptr_type_node;
531 }
532
533
534 /* Basic Data Types. */
535
536 void visit (TypeBasic *t)
537 {
538 /* [type/basic-data-types]
539
540 void no type.
541 bool 8-bit boolean value.
542 byte 8-bit signed value.
543 ubyte 8-bit unsigned value.
544 short 16-bit signed value.
545 ushort 16-bit unsigned value.
546 int 32-bit signed value.
547 uint 32-bit unsigned value.
548 long 64-bit signed value.
549 ulong 64-bit unsigned value.
550 cent 128-bit signed value.
551 ucent 128-bit unsigned value.
552 float 32-bit IEEE 754 floating-point value.
553 double 64-bit IEEE 754 floating-point value.
554 real largest FP size implemented in hardware.
555 ifloat imaginary float.
556 idouble imaginary double.
557 ireal imaginary real.
558 cfloat complex float.
559 cdouble complex double.
560 creal complex real.
561 char UTF-8 code unit.
562 wchar UTF-16 code unit.
563 dchar UTF-32 code unit. */
564
565 switch (t->ty)
566 {
567 case Tvoid: t->ctype = void_type_node; break;
568 case Tbool: t->ctype = d_bool_type; break;
569 case Tint8: t->ctype = d_byte_type; break;
570 case Tuns8: t->ctype = d_ubyte_type; break;
571 case Tint16: t->ctype = d_short_type; break;
572 case Tuns16: t->ctype = d_ushort_type; break;
573 case Tint32: t->ctype = d_int_type; break;
574 case Tuns32: t->ctype = d_uint_type; break;
575 case Tint64: t->ctype = d_long_type; break;
576 case Tuns64: t->ctype = d_ulong_type; break;
577 case Tint128: t->ctype = d_cent_type; break;
578 case Tuns128: t->ctype = d_ucent_type; break;
579 case Tfloat32: t->ctype = float_type_node; break;
580 case Tfloat64: t->ctype = double_type_node; break;
581 case Tfloat80: t->ctype = long_double_type_node; break;
582 case Timaginary32: t->ctype = ifloat_type_node; break;
583 case Timaginary64: t->ctype = idouble_type_node; break;
584 case Timaginary80: t->ctype = ireal_type_node; break;
585 case Tcomplex32: t->ctype = complex_float_type_node; break;
586 case Tcomplex64: t->ctype = complex_double_type_node; break;
587 case Tcomplex80: t->ctype = complex_long_double_type_node; break;
588 case Tchar: t->ctype = char8_type_node; break;
589 case Twchar: t->ctype = char16_type_node; break;
590 case Tdchar: t->ctype = char32_type_node; break;
591 default: gcc_unreachable ();
592 }
593
594 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
595 }
596
597
598 /* Derived Data Types. */
599
600 /* Build a simple pointer to data type, analogous to C pointers. */
601
602 void visit (TypePointer *t)
603 {
604 t->ctype = build_pointer_type (build_ctype (t->next));
605 }
606
607 /* Build a dynamic array type, consisting of a length and a pointer
608 to the array data. */
609
610 void visit (TypeDArray *t)
611 {
612 /* In [abi/arrays], dynamic array layout is:
613 .length array dimension.
614 .ptr pointer to array data. */
615 t->ctype = make_struct_type (t->toChars (), 2,
616 get_identifier ("length"),
617 build_ctype (Type::tsize_t),
618 get_identifier ("ptr"),
619 build_pointer_type (build_ctype (t->next)));
620 TYPE_DYNAMIC_ARRAY (t->ctype) = 1;
621 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
622 d_keep (t->ctype);
623 }
624
625 /* Build a static array type, distinguished from dynamic arrays by
626 having a length fixed at compile-time, analogous to C arrays. */
627
628 void visit (TypeSArray *t)
629 {
630 if (t->dim->isConst () && t->dim->type->isintegral ())
631 {
632 uinteger_t size = t->dim->toUInteger ();
633 t->ctype = make_array_type (t->next, size);
634 }
635 else
636 {
637 error ("invalid expression for static array dimension: %s",
638 t->dim->toChars ());
639 gcc_unreachable ();
640 }
641 }
642
643 /* Build a vector type, a fixed array of floating or integer types. */
644
645 void visit (TypeVector *t)
646 {
647 int nunits = ((TypeSArray *) t->basetype)->dim->toUInteger ();
648 tree inner = build_ctype (t->elementType ());
649
650 /* Same rationale as void static arrays. */
651 if (inner == void_type_node)
652 inner = build_ctype (Type::tuns8);
653
654 t->ctype = build_vector_type (inner, nunits);
655 TYPE_NAME (t->ctype) = get_identifier (t->toChars ());
656 layout_type (t->ctype);
657 }
658
659 /* Build an associative array type, distinguished from arrays by having an
660 index that's not necessarily an integer, and can be sparsely populated. */
661
662 void visit (TypeAArray *t)
663 {
664 /* In [abi/associative-arrays], associative arrays are a struct that only
665 consist of a pointer to an opaque, implementation defined type. */
666 t->ctype = make_struct_type (t->toChars (), 1,
667 get_identifier ("ptr"), ptr_type_node);
668 TYPE_ASSOCIATIVE_ARRAY (t->ctype) = 1;
669 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
670 d_keep (t->ctype);
671 }
672
673 /* Build type for a function declaration, which consists of a return type,
674 and a list of parameter types, and a linkage attribute. */
675
676 void visit (TypeFunction *t)
677 {
678 tree fnparams = NULL_TREE;
679 tree fntype;
680
681 /* [function/variadic]
682
683 Variadic functions with D linkage have an additional hidden argument
684 with the name _arguments passed to the function. */
685 if (t->varargs == 1 && t->linkage == LINKd)
686 {
687 tree type = build_ctype (Type::typeinfotypelist->type);
688 fnparams = chainon (fnparams, build_tree_list (0, type));
689 }
690
691 if (t->parameters)
692 {
693 size_t n_args = Parameter::dim (t->parameters);
694
695 for (size_t i = 0; i < n_args; i++)
696 {
697 tree type = type_passed_as (Parameter::getNth (t->parameters, i));
698 fnparams = chainon (fnparams, build_tree_list (0, type));
699 }
700 }
701
702 /* When the last parameter is void_list_node, that indicates a fixed length
703 parameter list, otherwise function is treated as variadic. */
704 if (t->varargs != 1)
705 fnparams = chainon (fnparams, void_list_node);
706
707 if (t->next != NULL)
708 {
709 fntype = build_ctype (t->next);
710 if (t->isref)
711 fntype = build_reference_type (fntype);
712 }
713 else
714 fntype = void_type_node;
715
716 /* Could the function type be self referenced by parameters? */
717 t->ctype = build_function_type (fntype, fnparams);
718 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
719 d_keep (t->ctype);
720
721 /* Handle any special support for calling conventions. */
722 switch (t->linkage)
723 {
724 case LINKpascal:
725 case LINKwindows:
726 /* [attribute/linkage]
727
728 The Windows convention is distinct from the C convention only
729 on Win32, where it is equivalent to the stdcall convention. */
730 if (!global.params.is64bit)
731 t->ctype = insert_type_attribute (t->ctype, "stdcall");
732 break;
733
734 case LINKc:
735 case LINKcpp:
736 case LINKd:
737 case LINKobjc:
738 /* [abi/function-calling-conventions]
739
740 The extern (C) and extern (D) calling convention matches
741 the C calling convention used by the supported C compiler
742 on the host system. */
743 break;
744
745 default:
746 gcc_unreachable ();
747 }
748 }
749
750 /* Build a delegate type, an aggregate of two pieces of data, an object
751 reference and a pointer to a non-static member function, or a pointer
752 to a closure and a pointer to a nested function. */
753
754 void visit (TypeDelegate *t)
755 {
756 /* In [abi/delegates], delegate layout is:
757 .ptr context pointer.
758 .funcptr pointer to function. */
759 tree fntype = build_ctype (t->next);
760 tree dgtype = build_vthis_function (void_type_node, fntype);
761
762 TYPE_ATTRIBUTES (dgtype) = TYPE_ATTRIBUTES (fntype);
763 TYPE_LANG_SPECIFIC (dgtype) = TYPE_LANG_SPECIFIC (fntype);
764
765 t->ctype = make_struct_type (t->toChars (), 2,
766 get_identifier ("ptr"),
767 build_ctype (Type::tvoidptr),
768 get_identifier ("funcptr"),
769 build_pointer_type (dgtype));
770 TYPE_DELEGATE (t->ctype) = 1;
771 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
772 d_keep (t->ctype);
773 }
774
775
776 /* User Defined Types. */
777
778 /* Build a named enum type, a distinct value whose values are restrict to
779 a group of constants of the same underlying base type. */
780
781 void visit (TypeEnum *t)
782 {
783 tree basetype = (t->sym->memtype)
784 ? build_ctype (t->sym->memtype) : void_type_node;
785
786 if (!INTEGRAL_TYPE_P (basetype) || TREE_CODE (basetype) == BOOLEAN_TYPE)
787 {
788 /* Enums in D2 can have a base type that is not necessarily integral.
789 For these, we simplify this a little by using the base type directly
790 instead of building an ENUMERAL_TYPE. */
791 t->ctype = build_variant_type_copy (basetype);
792 }
793 else
794 {
795 t->ctype = make_node (ENUMERAL_TYPE);
796 ENUM_IS_SCOPED (t->ctype) = 1;
797 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
798 d_keep (t->ctype);
799
800 if (flag_short_enums)
801 TYPE_PACKED (t->ctype) = 1;
802
803 TYPE_PRECISION (t->ctype) = t->size (t->sym->loc) * 8;
804 TYPE_SIZE (t->ctype) = 0;
805
806 TYPE_MIN_VALUE (t->ctype) = TYPE_MIN_VALUE (basetype);
807 TYPE_MAX_VALUE (t->ctype) = TYPE_MAX_VALUE (basetype);
808 layout_type (t->ctype);
809
810 tree values = NULL_TREE;
811 if (t->sym->members)
812 {
813 for (size_t i = 0; i < t->sym->members->dim; i++)
814 {
815 EnumMember *member = (*t->sym->members)[i]->isEnumMember ();
816 /* Templated functions can seep through to the back-end
817 just ignore for now. */
818 if (member == NULL)
819 continue;
820
821 tree ident = get_identifier (member->ident->toChars ());
822 tree value = build_integer_cst (member->value ()->toInteger (),
823 basetype);
824
825 /* Build an identifier for the enumeration constant. */
826 tree decl = build_decl (make_location_t (member->loc),
827 CONST_DECL, ident, basetype);
828 DECL_CONTEXT (decl) = t->ctype;
829 TREE_CONSTANT (decl) = 1;
830 TREE_READONLY (decl) = 1;
831 DECL_INITIAL (decl) = value;
832
833 /* Add this enumeration constant to the list for this type. */
834 values = chainon (values, build_tree_list (ident, decl));
835 }
836 }
837
838 TYPE_VALUES (t->ctype) = values;
839 TYPE_UNSIGNED (t->ctype) = TYPE_UNSIGNED (basetype);
840 build_type_decl (t->ctype, t->sym);
841 }
842
843 if (t->sym->userAttribDecl)
844 {
845 Expressions *eattrs = t->sym->userAttribDecl->getAttributes ();
846 decl_attributes (&t->ctype, build_attributes (eattrs),
847 ATTR_FLAG_TYPE_IN_PLACE);
848 }
849 }
850
851 /* Build a struct or union type. Layout should be exactly represented
852 as an equivalent C struct, except for non-POD or nested structs. */
853
854 void visit (TypeStruct *t)
855 {
856 /* Need to set this right away in case of self-references. */
857 t->ctype = make_node (t->sym->isUnionDeclaration ()
858 ? UNION_TYPE : RECORD_TYPE);
859 d_keep (t->ctype);
860
861 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
862
863 if (t->sym->members)
864 {
865 /* Must set up the overall size and alignment before determining
866 the context or laying out fields as those types may make
867 recursive references to this type. */
868 unsigned structsize = t->sym->structsize;
869 unsigned alignsize = (t->sym->alignment != STRUCTALIGN_DEFAULT)
870 ? t->sym->alignment : t->sym->alignsize;
871
872 TYPE_SIZE (t->ctype) = bitsize_int (structsize * BITS_PER_UNIT);
873 TYPE_SIZE_UNIT (t->ctype) = size_int (structsize);
874 SET_TYPE_ALIGN (t->ctype, alignsize * BITS_PER_UNIT);
875 TYPE_PACKED (t->ctype) = (alignsize == 1);
876 compute_record_mode (t->ctype);
877
878 /* Put out all fields. */
879 layout_aggregate_type (t->sym, t->ctype, t->sym);
880 finish_aggregate_type (structsize, alignsize, t->ctype,
881 t->sym->userAttribDecl);
882 }
883
884 TYPE_CONTEXT (t->ctype) = d_decl_context (t->sym);
885 build_type_decl (t->ctype, t->sym);
886
887 /* For structs with a user defined postblit or a destructor,
888 also set TREE_ADDRESSABLE on the type and all variants.
889 This will make the struct be passed around by reference. */
890 if (t->sym->postblit || t->sym->dtor)
891 {
892 for (tree tv = t->ctype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
893 TREE_ADDRESSABLE (tv) = 1;
894 }
895 }
896
897 /* Build a class type. Whereas structs are value types, classes are
898 reference types, with all the object-orientated features. */
899
900 void visit (TypeClass *t)
901 {
902 /* Need to set ctype right away in case of self-references to
903 the type during this call. */
904 tree basetype = make_node (RECORD_TYPE);
905 t->ctype = build_pointer_type (basetype);
906 d_keep (t->ctype);
907
908 /* Note that lang_specific data is assigned to both the reference
909 and the underlying record type. */
910 TYPE_LANG_SPECIFIC (t->ctype) = build_lang_type (t);
911 TYPE_LANG_SPECIFIC (basetype) = TYPE_LANG_SPECIFIC (t->ctype);
912 CLASS_TYPE_P (basetype) = 1;
913
914 /* Put out all fields, including from each base class. */
915 layout_aggregate_type (t->sym, basetype, t->sym);
916 finish_aggregate_type (t->sym->structsize, t->sym->alignsize,
917 basetype, t->sym->userAttribDecl);
918
919 /* Classes only live in memory, so always set the TREE_ADDRESSABLE bit. */
920 for (tree tv = basetype; tv != NULL_TREE; tv = TYPE_NEXT_VARIANT (tv))
921 TREE_ADDRESSABLE (tv) = 1;
922
923 /* Type is final, there are no derivations. */
924 if (t->sym->storage_class & STCfinal)
925 TYPE_FINAL_P (basetype) = 1;
926
927 /* Create BINFO even if debugging is off. This is needed to keep
928 references to inherited types. */
929 if (!t->sym->isInterfaceDeclaration ())
930 TYPE_BINFO (basetype) = build_class_binfo (NULL_TREE, t->sym);
931 else
932 {
933 unsigned offset = 0;
934
935 TYPE_BINFO (basetype) = build_interface_binfo (NULL_TREE, t->sym,
936 offset);
937 }
938
939 /* Associate all virtual methods with the class too. */
940 for (size_t i = 0; i < t->sym->vtbl.dim; i++)
941 {
942 FuncDeclaration *fd = t->sym->vtbl[i]->isFuncDeclaration ();
943 tree method = fd ? get_symbol_decl (fd) : error_mark_node;
944
945 if (!error_operand_p (method)
946 && DECL_CONTEXT (method) == basetype
947 && !chain_member (method, TYPE_FIELDS (basetype)))
948 TYPE_FIELDS (basetype) = chainon (TYPE_FIELDS (basetype), method);
949 }
950
951 TYPE_CONTEXT (basetype) = d_decl_context (t->sym);
952 build_type_decl (basetype, t->sym);
953 }
954 };
955
956
957 /* Build a tree from a frontend Type. */
958
959 tree
960 build_ctype (Type *t)
961 {
962 if (!t->ctype)
963 {
964 TypeVisitor v;
965
966 /* Strip const modifiers from type before building. This is done
967 to ensure that back-end treats e.g: const (T) as a variant of T,
968 and not as two distinct types. */
969 if (t->isNaked ())
970 t->accept (&v);
971 else
972 {
973 Type *tb = t->castMod (0);
974 if (!tb->ctype)
975 tb->accept (&v);
976 t->ctype = insert_type_modifiers (tb->ctype, t->mod);
977 }
978 }
979
980 return t->ctype;
981 }