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