Silence overactive sanity check with -fpartial-profile-training
[gcc.git] / gcc / d / d-builtins.cc
1 /* d-builtins.cc -- GCC builtins support for D.
2 Copyright (C) 2006-2019 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/cond.h"
25 #include "dmd/declaration.h"
26 #include "dmd/expression.h"
27 #include "dmd/identifier.h"
28 #include "dmd/module.h"
29 #include "dmd/mtype.h"
30
31 #include "tree.h"
32 #include "fold-const.h"
33 #include "diagnostic.h"
34 #include "langhooks.h"
35 #include "target.h"
36 #include "common/common-target.h"
37 #include "stringpool.h"
38 #include "stor-layout.h"
39
40 #include "d-tree.h"
41 #include "d-target.h"
42
43
44 static GTY(()) vec<tree, va_gc> *gcc_builtins_functions = NULL;
45 static GTY(()) vec<tree, va_gc> *gcc_builtins_libfuncs = NULL;
46 static GTY(()) vec<tree, va_gc> *gcc_builtins_types = NULL;
47
48 /* Record built-in types and their associated decls for re-use when
49 generating the `gcc.builtins' module. */
50
51 struct builtin_data
52 {
53 Type *dtype;
54 tree ctype;
55 Dsymbol *dsym;
56
57 builtin_data (Type *t, tree c, Dsymbol *d = NULL)
58 : dtype(t), ctype(c), dsym(d)
59 { }
60 };
61
62 static vec<builtin_data> builtin_converted_decls;
63
64 /* Build D frontend type from tree TYPE type given. This will set the
65 back-end type symbol directly for complex types to save build_ctype()
66 the work. For other types, it is not useful or will cause errors, such
67 as casting from `C char' to `D char', which also means that `char *`
68 needs to be specially handled. */
69
70 static Type *
71 build_frontend_type (tree type)
72 {
73 Type *dtype;
74 MOD mod = 0;
75
76 if (TYPE_READONLY (type))
77 mod |= MODconst;
78 if (TYPE_VOLATILE (type))
79 mod |= MODshared;
80
81 /* If we've seen the type before, re-use the converted decl. */
82 for (size_t i = 0; i < builtin_converted_decls.length (); ++i)
83 {
84 tree t = builtin_converted_decls[i].ctype;
85 if (TYPE_MAIN_VARIANT (t) == TYPE_MAIN_VARIANT (type))
86 return builtin_converted_decls[i].dtype;
87 }
88
89 switch (TREE_CODE (type))
90 {
91 case POINTER_TYPE:
92 dtype = build_frontend_type (TREE_TYPE (type));
93 if (dtype)
94 {
95 /* Check for char * first. Needs to be done for chars/string. */
96 if (TYPE_MAIN_VARIANT (TREE_TYPE (type)) == char_type_node)
97 return Type::tchar->addMod (dtype->mod)->pointerTo ()->addMod (mod);
98
99 if (dtype->ty == Tfunction)
100 return (TypePointer::create (dtype))->addMod (mod);
101
102 return dtype->pointerTo ()->addMod (mod);
103 }
104 break;
105
106 case REFERENCE_TYPE:
107 dtype = build_frontend_type (TREE_TYPE (type));
108 if (dtype)
109 {
110 /* Want to assign ctype directly so that the REFERENCE_TYPE code
111 can be turned into as an `inout' argument. Can't use pointerTo(),
112 because the returned Type is shared. */
113 dtype = (TypePointer::create (dtype))->addMod (mod);
114 dtype->ctype = type;
115 builtin_converted_decls.safe_push (builtin_data (dtype, type));
116 return dtype;
117 }
118 break;
119
120 case BOOLEAN_TYPE:
121 /* Should be no need for size checking. */
122 return Type::tbool->addMod (mod);
123
124 case INTEGER_TYPE:
125 {
126 unsigned size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
127 bool unsignedp = TYPE_UNSIGNED (type);
128
129 /* For now, skip support for cent/ucent until the frontend
130 has better support for handling it. */
131 for (size_t i = Tint8; i <= Tuns64; i++)
132 {
133 dtype = Type::basic[i];
134
135 /* Search for type matching size and signedness. */
136 if (unsignedp != dtype->isunsigned ()
137 || size != dtype->size ())
138 continue;
139
140 return dtype->addMod (mod);
141 }
142 break;
143 }
144
145 case REAL_TYPE:
146 {
147 unsigned size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
148
149 for (size_t i = Tfloat32; i <= Tfloat80; i++)
150 {
151 dtype = Type::basic[i];
152
153 /* Search for type matching size. */
154 if (dtype->size () != size)
155 continue;
156
157 return dtype->addMod (mod);
158 }
159 break;
160 }
161
162 case COMPLEX_TYPE:
163 {
164 unsigned size = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
165 for (size_t i = Tcomplex32; i <= Tcomplex80; i++)
166 {
167 dtype = Type::basic[i];
168
169 /* Search for type matching size. */
170 if (dtype->size () != size)
171 continue;
172
173 return dtype->addMod (mod);
174 }
175 break;
176 }
177
178 case VOID_TYPE:
179 return Type::tvoid->addMod (mod);
180
181 case ARRAY_TYPE:
182 dtype = build_frontend_type (TREE_TYPE (type));
183 if (dtype)
184 {
185 tree index = TYPE_DOMAIN (type);
186 tree ub = TYPE_MAX_VALUE (index);
187 tree lb = TYPE_MIN_VALUE (index);
188
189 tree length = fold_build2 (MINUS_EXPR, TREE_TYPE (lb), ub, lb);
190 length = size_binop (PLUS_EXPR, size_one_node,
191 convert (sizetype, length));
192
193 dtype = dtype->sarrayOf (TREE_INT_CST_LOW (length))->addMod (mod);
194 builtin_converted_decls.safe_push (builtin_data (dtype, type));
195 return dtype;
196 }
197 break;
198
199 case VECTOR_TYPE:
200 {
201 unsigned HOST_WIDE_INT nunits;
202 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nunits))
203 break;
204
205 dtype = build_frontend_type (TREE_TYPE (type));
206 if (!dtype)
207 break;
208
209 dtype = dtype->sarrayOf (nunits)->addMod (mod);
210 if (dtype->nextOf ()->isTypeBasic () == NULL)
211 break;
212
213 dtype = (TypeVector::create (Loc (), dtype))->addMod (mod);
214 builtin_converted_decls.safe_push (builtin_data (dtype, type));
215 return dtype;
216 }
217
218 case RECORD_TYPE:
219 {
220 Identifier *ident = TYPE_IDENTIFIER (type) ?
221 Identifier::idPool (IDENTIFIER_POINTER (TYPE_IDENTIFIER (type))) : NULL;
222
223 /* Neither the `object' and `gcc.builtins' modules will not exist when
224 this is called. Use a stub 'object' module parent in the meantime.
225 If `gcc.builtins' is later imported, the parent will be overridden
226 with the correct module symbol. */
227 static Identifier *object = Identifier::idPool ("object");
228 static Module *stubmod = Module::create ("object.d", object, 0, 0);
229
230 StructDeclaration *sdecl = StructDeclaration::create (Loc (), ident,
231 false);
232 sdecl->parent = stubmod;
233 sdecl->structsize = int_size_in_bytes (type);
234 sdecl->alignsize = TYPE_ALIGN_UNIT (type);
235 sdecl->alignment = STRUCTALIGN_DEFAULT;
236 sdecl->sizeok = SIZEOKdone;
237 sdecl->type = (TypeStruct::create (sdecl))->addMod (mod);
238 sdecl->type->ctype = type;
239 sdecl->type->merge2 ();
240
241 sdecl->members = new Dsymbols;
242
243 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
244 {
245 Type *ftype = build_frontend_type (TREE_TYPE (field));
246 if (!ftype)
247 {
248 delete sdecl->members;
249 return NULL;
250 }
251
252 Identifier *fident
253 = Identifier::idPool (IDENTIFIER_POINTER (DECL_NAME (field)));
254 VarDeclaration *vd = VarDeclaration::create (Loc (), ftype, fident,
255 NULL);
256 vd->offset = tree_to_uhwi (DECL_FIELD_OFFSET (field));
257 vd->semanticRun = PASSsemanticdone;
258 vd->csym = field;
259 sdecl->members->push (vd);
260 sdecl->fields.push (vd);
261 }
262
263 dtype = sdecl->type;
264 builtin_converted_decls.safe_push (builtin_data (dtype, type, sdecl));
265 return dtype;
266 }
267
268 case FUNCTION_TYPE:
269 dtype = build_frontend_type (TREE_TYPE (type));
270 if (dtype)
271 {
272 tree parms = TYPE_ARG_TYPES (type);
273 int varargs_p = 1;
274
275 Parameters *args = new Parameters;
276 args->reserve (list_length (parms));
277
278 /* Attempt to convert all parameter types. */
279 for (tree parm = parms; parm != NULL_TREE; parm = TREE_CHAIN (parm))
280 {
281 tree argtype = TREE_VALUE (parm);
282 if (argtype == void_type_node)
283 {
284 varargs_p = 0;
285 break;
286 }
287
288 StorageClass sc = STCundefined;
289 if (TREE_CODE (argtype) == REFERENCE_TYPE)
290 {
291 argtype = TREE_TYPE (argtype);
292 sc |= STCref;
293 }
294
295 Type *targ = build_frontend_type (argtype);
296 if (!targ)
297 {
298 delete args;
299 return NULL;
300 }
301
302 args->push (Parameter::create (sc, targ, NULL, NULL));
303 }
304
305 /* GCC generic and placeholder built-ins are marked as variadic, yet
306 have no named parameters, and so can't be represented in D. */
307 if (args->dim != 0 || !varargs_p)
308 {
309 dtype = TypeFunction::create (args, dtype, varargs_p, LINKc);
310 return dtype->addMod (mod);
311 }
312 }
313 break;
314
315 default:
316 break;
317 }
318
319 return NULL;
320 }
321
322 /* Attempt to convert GCC evaluated CST to a D Frontend Expression.
323 This is used for getting the CTFE value out of a const-folded builtin,
324 returns NULL if it cannot convert CST. */
325
326 Expression *
327 d_eval_constant_expression (tree cst)
328 {
329 STRIP_TYPE_NOPS (cst);
330 Type *type = build_frontend_type (TREE_TYPE (cst));
331
332 if (type)
333 {
334 /* Convert our GCC CST tree into a D Expression. This seems like we are
335 trying too hard, as these will only be converted back to a tree again
336 later in the codegen pass, but satisfies the need to have GCC built-ins
337 CTFE-able in the frontend. */
338 tree_code code = TREE_CODE (cst);
339 if (code == COMPLEX_CST)
340 {
341 real_value re = TREE_REAL_CST (TREE_REALPART (cst));
342 real_value im = TREE_REAL_CST (TREE_IMAGPART (cst));
343 complex_t value = complex_t (ldouble (re), ldouble (im));
344 return ComplexExp::create (Loc (), value, type);
345 }
346 else if (code == INTEGER_CST)
347 {
348 dinteger_t value = TREE_INT_CST_LOW (cst);
349 return IntegerExp::create (Loc (), value, type);
350 }
351 else if (code == REAL_CST)
352 {
353 real_value value = TREE_REAL_CST (cst);
354 return RealExp::create (Loc (), ldouble (value), type);
355 }
356 else if (code == STRING_CST)
357 {
358 const void *string = TREE_STRING_POINTER (cst);
359 size_t len = TREE_STRING_LENGTH (cst);
360 return StringExp::create (Loc (), CONST_CAST (void *, string), len);
361 }
362 else if (code == VECTOR_CST)
363 {
364 dinteger_t nunits = VECTOR_CST_NELTS (cst).to_constant ();
365 Expressions *elements = new Expressions;
366 elements->setDim (nunits);
367
368 for (size_t i = 0; i < nunits; i++)
369 {
370 Expression *elem
371 = d_eval_constant_expression (VECTOR_CST_ELT (cst, i));
372 if (elem == NULL)
373 return NULL;
374
375 (*elements)[i] = elem;
376 }
377
378 Expression *e = ArrayLiteralExp::create (Loc (), elements);
379 e->type = ((TypeVector *) type)->basetype;
380
381 return VectorExp::create (Loc (), e, type);
382 }
383 }
384
385 return NULL;
386 }
387
388 /* Callback for TARGET_D_CPU_VERSIONS and TARGET_D_OS_VERSIONS.
389 Adds IDENT to the list of predefined version identifiers. */
390
391 void
392 d_add_builtin_version (const char* ident)
393 {
394 /* For now, we need to tell the D frontend what platform is being targeted.
395 This should be removed once the frontend has been fixed. */
396 if (strcmp (ident, "linux") == 0)
397 global.params.isLinux = true;
398 else if (strcmp (ident, "OSX") == 0)
399 global.params.isOSX = true;
400 else if (strcmp (ident, "Windows") == 0)
401 global.params.isWindows = true;
402 else if (strcmp (ident, "FreeBSD") == 0)
403 global.params.isFreeBSD = true;
404 else if (strcmp (ident, "OpenBSD") == 0)
405 global.params.isOpenBSD = true;
406 else if (strcmp (ident, "Solaris") == 0)
407 global.params.isSolaris = true;
408 /* The is64bit field only refers to x86_64 target. */
409 else if (strcmp (ident, "X86_64") == 0)
410 global.params.is64bit = true;
411 /* No other fields are required to be set for the frontend. */
412
413 VersionCondition::addPredefinedGlobalIdent (ident);
414 }
415
416 /* Initialize the list of all the predefined version identifiers. */
417
418 void
419 d_init_versions (void)
420 {
421 VersionCondition::addPredefinedGlobalIdent ("GNU");
422 VersionCondition::addPredefinedGlobalIdent ("D_Version2");
423
424 if (BYTES_BIG_ENDIAN)
425 VersionCondition::addPredefinedGlobalIdent ("BigEndian");
426 else
427 VersionCondition::addPredefinedGlobalIdent ("LittleEndian");
428
429 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
430 VersionCondition::addPredefinedGlobalIdent ("GNU_SjLj_Exceptions");
431 else if (targetm_common.except_unwind_info (&global_options) == UI_SEH)
432 VersionCondition::addPredefinedGlobalIdent ("GNU_SEH_Exceptions");
433 else if (targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
434 VersionCondition::addPredefinedGlobalIdent ("GNU_DWARF2_Exceptions");
435
436 if (!targetm.have_tls)
437 VersionCondition::addPredefinedGlobalIdent ("GNU_EMUTLS");
438
439 if (STACK_GROWS_DOWNWARD)
440 VersionCondition::addPredefinedGlobalIdent ("GNU_StackGrowsDown");
441
442 /* Should define this anyway to set us apart from the competition. */
443 VersionCondition::addPredefinedGlobalIdent ("GNU_InlineAsm");
444
445 /* LP64 only means 64bit pointers in D. */
446 if (global.params.isLP64)
447 VersionCondition::addPredefinedGlobalIdent ("D_LP64");
448
449 /* Setting `global.params.cov' forces module info generation which is
450 not needed for the GCC coverage implementation. Instead, just
451 test flag_test_coverage while leaving `global.params.cov' unset. */
452 if (flag_test_coverage)
453 VersionCondition::addPredefinedGlobalIdent ("D_Coverage");
454 if (flag_pic)
455 VersionCondition::addPredefinedGlobalIdent ("D_PIC");
456
457 if (global.params.doDocComments)
458 VersionCondition::addPredefinedGlobalIdent ("D_Ddoc");
459
460 if (global.params.useUnitTests)
461 VersionCondition::addPredefinedGlobalIdent ("unittest");
462
463 if (global.params.useAssert)
464 VersionCondition::addPredefinedGlobalIdent ("assert");
465
466 if (global.params.useArrayBounds == BOUNDSCHECKoff)
467 VersionCondition::addPredefinedGlobalIdent ("D_NoBoundsChecks");
468
469 if (global.params.betterC)
470 VersionCondition::addPredefinedGlobalIdent ("D_BetterC");
471 else
472 {
473 VersionCondition::addPredefinedGlobalIdent ("D_ModuleInfo");
474 VersionCondition::addPredefinedGlobalIdent ("D_Exceptions");
475 VersionCondition::addPredefinedGlobalIdent ("D_TypeInfo");
476 }
477
478 VersionCondition::addPredefinedGlobalIdent ("all");
479
480 /* Emit all target-specific version identifiers. */
481 targetdm.d_cpu_versions ();
482 targetdm.d_os_versions ();
483
484 VersionCondition::addPredefinedGlobalIdent ("CppRuntime_Gcc");
485 }
486
487 /* A helper for d_build_builtins_module. Return a new ALIAS for TYPE.
488 Analogous to `alias ALIAS = TYPE' in D code. */
489
490 static AliasDeclaration *
491 build_alias_declaration (const char *alias, Type *type)
492 {
493 return AliasDeclaration::create (Loc (), Identifier::idPool (alias), type);
494 }
495
496 /* A helper function for Target::loadModule. Generates all code for the
497 `gcc.builtins' module, whose frontend symbol should be M. */
498
499 void
500 d_build_builtins_module (Module *m)
501 {
502 Dsymbols *members = new Dsymbols;
503 tree decl;
504
505 for (size_t i = 0; vec_safe_iterate (gcc_builtins_functions, i, &decl); ++i)
506 {
507 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
508 TypeFunction *tf
509 = (TypeFunction *) build_frontend_type (TREE_TYPE (decl));
510
511 /* Cannot create built-in function type for DECL. */
512 if (!tf)
513 continue;
514
515 /* A few notes on D2 attributes applied to builtin functions:
516 - It is assumed that built-ins solely provided by the compiler are
517 considered @safe and pure.
518 - Built-ins that correspond to `extern(C)' functions in the standard
519 library that have `__attribute__(nothrow)' are considered `@trusted'.
520 - The purity of a built-in can vary depending on compiler flags set
521 upon initialization, or by the `-foptions' passed, such as
522 flag_unsafe_math_optimizations.
523 - Built-ins never use the GC or raise a D exception, and so are always
524 marked as `nothrow' and `@nogc'. */
525 tf->purity = DECL_PURE_P (decl) ? PUREstrong
526 : TREE_READONLY (decl) ? PUREconst
527 : DECL_IS_NOVOPS (decl) ? PUREweak
528 : !DECL_ASSEMBLER_NAME_SET_P (decl) ? PUREweak
529 : PUREimpure;
530 tf->trust = !DECL_ASSEMBLER_NAME_SET_P (decl) ? TRUSTsafe
531 : TREE_NOTHROW (decl) ? TRUSTtrusted
532 : TRUSTsystem;
533 tf->isnothrow = true;
534 tf->isnogc = true;
535
536 FuncDeclaration *func
537 = FuncDeclaration::create (Loc (), Loc (),
538 Identifier::idPool (name),
539 STCextern, tf);
540 DECL_LANG_SPECIFIC (decl) = build_lang_decl (func);
541 func->csym = decl;
542 func->builtin = BUILTINyes;
543
544 members->push (func);
545 }
546
547 for (size_t i = 0; vec_safe_iterate (gcc_builtins_types, i, &decl); ++i)
548 {
549 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
550 Type *t = build_frontend_type (TREE_TYPE (decl));
551
552 /* Cannot create built-in type for DECL. */
553 if (!t)
554 continue;
555
556 members->push (build_alias_declaration (name, t));
557 }
558
559 /* Iterate through the target-specific builtin types for va_list. */
560 if (targetm.enum_va_list_p)
561 {
562 const char *name;
563 tree type;
564
565 for (int i = 0; targetm.enum_va_list_p (i, &name, &type); ++i)
566 {
567 Type *t = build_frontend_type (type);
568 /* Cannot create built-in type. */
569 if (!t)
570 continue;
571
572 members->push (build_alias_declaration (name, t));
573 }
574 }
575
576 /* Push out declarations for any RECORD_TYPE types encountered when building
577 all builtin functions and types. */
578 for (size_t i = 0; i < builtin_converted_decls.length (); ++i)
579 {
580 /* Currently, there is no need to run semantic, but we do want to output
581 initializers, typeinfo, and others on demand. */
582 Dsymbol *dsym = builtin_converted_decls[i].dsym;
583 if (dsym != NULL && !dsym->isAnonymous ())
584 {
585 dsym->parent = m;
586 members->push (dsym);
587 }
588 }
589
590 /* va_list should already be built, so no need to convert to D type again. */
591 StructDeclaration *sd = (Type::tvalist->ty == Tstruct)
592 ? ((TypeStruct *) Type::tvalist)->sym : NULL;
593 if (sd == NULL || !sd->isAnonymous ())
594 {
595 members->push (build_alias_declaration ("__builtin_va_list",
596 Type::tvalist));
597 }
598 else
599 {
600 sd->ident = Identifier::idPool ("__builtin_va_list");
601 members->push (sd);
602 }
603
604 /* Expose target-specific integer types to the builtins module. */
605 {
606 Type *t = build_frontend_type (long_integer_type_node);
607 members->push (build_alias_declaration ("__builtin_clong", t));
608
609 t = build_frontend_type (long_unsigned_type_node);
610 members->push (build_alias_declaration ("__builtin_culong", t));
611
612 t = build_frontend_type (long_long_integer_type_node);
613 members->push (build_alias_declaration ("__builtin_clonglong", t));
614
615 t = build_frontend_type (long_long_unsigned_type_node);
616 members->push (build_alias_declaration ("__builtin_culonglong", t));
617
618 t = build_frontend_type (lang_hooks.types.type_for_mode (byte_mode, 0));
619 members->push (build_alias_declaration ("__builtin_machine_byte", t));
620
621 t = build_frontend_type (lang_hooks.types.type_for_mode (byte_mode, 1));
622 members->push (build_alias_declaration ("__builtin_machine_ubyte", t));
623
624 t = build_frontend_type (lang_hooks.types.type_for_mode (word_mode, 0));
625 members->push (build_alias_declaration ("__builtin_machine_int", t));
626
627 t = build_frontend_type (lang_hooks.types.type_for_mode (word_mode, 1));
628 members->push (build_alias_declaration ("__builtin_machine_uint", t));
629
630 t = build_frontend_type (lang_hooks.types.type_for_mode (ptr_mode, 0));
631 members->push (build_alias_declaration ("__builtin_pointer_int", t));
632
633 t = build_frontend_type (lang_hooks.types.type_for_mode (ptr_mode, 1));
634 members->push (build_alias_declaration ("__builtin_pointer_uint", t));
635
636 /* _Unwind_Word has its own target specific mode. */
637 machine_mode mode = targetm.unwind_word_mode ();
638 t = build_frontend_type (lang_hooks.types.type_for_mode (mode, 0));
639 members->push (build_alias_declaration ("__builtin_unwind_int", t));
640
641 t = build_frontend_type (lang_hooks.types.type_for_mode (mode, 1));
642 members->push (build_alias_declaration ("__builtin_unwind_uint", t));
643 }
644
645 m->members->push (LinkDeclaration::create (LINKc, members));
646 }
647
648 /* Search for any `extern(C)' functions that match any known GCC library builtin
649 function in D and override its internal back-end symbol. */
650
651 static void
652 maybe_set_builtin_1 (Dsymbol *d)
653 {
654 AttribDeclaration *ad = d->isAttribDeclaration ();
655 FuncDeclaration *fd = d->isFuncDeclaration ();
656
657 if (ad != NULL)
658 {
659 /* Recursively search through attribute decls. */
660 Dsymbols *decls = ad->include (NULL, NULL);
661 if (decls && decls->dim)
662 {
663 for (size_t i = 0; i < decls->dim; i++)
664 {
665 Dsymbol *sym = (*decls)[i];
666 maybe_set_builtin_1 (sym);
667 }
668 }
669 }
670 else if (fd && !fd->fbody)
671 {
672 tree t;
673
674 for (size_t i = 0; vec_safe_iterate (gcc_builtins_libfuncs, i, &t); ++i)
675 {
676 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (t));
677
678 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t));
679 if (fd->ident != Identifier::idPool (name))
680 continue;
681
682 /* Found a match, tell the frontend this is a builtin. */
683 DECL_LANG_SPECIFIC (t) = build_lang_decl (fd);
684 fd->csym = t;
685 fd->builtin = BUILTINyes;
686 return;
687 }
688 }
689 }
690
691 /* A helper function for Target::loadModule. Traverse all members in module M
692 to search for any functions that can be mapped to any GCC builtin. */
693
694 void
695 d_maybe_set_builtin (Module *m)
696 {
697 if (!m || !m->members)
698 return;
699
700 for (size_t i = 0; i < m->members->dim; i++)
701 {
702 Dsymbol *sym = (*m->members)[i];
703 maybe_set_builtin_1 (sym);
704 }
705 }
706
707 /* Used to help initialize the builtin-types.def table. When a type of
708 the correct size doesn't exist, use error_mark_node instead of NULL.
709 The latter results in segfaults even when a decl using the type doesn't
710 get invoked. */
711
712 static tree
713 builtin_type_for_size (int size, bool unsignedp)
714 {
715 tree type = lang_hooks.types.type_for_size (size, unsignedp);
716 return type ? type : error_mark_node;
717 }
718
719 /* Support for DEF_BUILTIN. */
720
721 static void
722 do_build_builtin_fn (built_in_function fncode,
723 const char *name,
724 built_in_class fnclass,
725 tree fntype, bool both_p, bool fallback_p,
726 tree fnattrs, bool implicit_p)
727 {
728 tree decl;
729 const char *libname;
730
731 if (fntype == error_mark_node)
732 return;
733
734 gcc_assert ((!both_p && !fallback_p)
735 || !strncmp (name, "__builtin_",
736 strlen ("__builtin_")));
737
738 libname = name + strlen ("__builtin_");
739
740 decl = add_builtin_function (name, fntype, fncode, fnclass,
741 fallback_p ? libname : NULL, fnattrs);
742
743 set_builtin_decl (fncode, decl, implicit_p);
744 }
745
746 /* Standard data types to be used in builtin argument declarations. */
747
748 static GTY(()) tree string_type_node;
749 static GTY(()) tree const_string_type_node;
750 static GTY(()) tree wint_type_node;
751 static GTY(()) tree intmax_type_node;
752 static GTY(()) tree uintmax_type_node;
753 static GTY(()) tree signed_size_type_node;
754
755
756 /* Build nodes that would have been created by the C front-end; necessary
757 for including builtin-types.def and ultimately builtins.def. */
758
759 static void
760 d_build_c_type_nodes (void)
761 {
762 void_list_node = build_tree_list (NULL_TREE, void_type_node);
763 string_type_node = build_pointer_type (char_type_node);
764 const_string_type_node
765 = build_pointer_type (build_qualified_type (char_type_node,
766 TYPE_QUAL_CONST));
767
768 if (strcmp (UINTMAX_TYPE, "unsigned int") == 0)
769 {
770 intmax_type_node = integer_type_node;
771 uintmax_type_node = unsigned_type_node;
772 }
773 else if (strcmp (UINTMAX_TYPE, "long unsigned int") == 0)
774 {
775 intmax_type_node = long_integer_type_node;
776 uintmax_type_node = long_unsigned_type_node;
777 }
778 else if (strcmp (UINTMAX_TYPE, "long long unsigned int") == 0)
779 {
780 intmax_type_node = long_long_integer_type_node;
781 uintmax_type_node = long_long_unsigned_type_node;
782 }
783 else
784 gcc_unreachable ();
785
786 signed_size_type_node = signed_type_for (size_type_node);
787 wint_type_node = unsigned_type_node;
788 pid_type_node = integer_type_node;
789 }
790
791 /* Build nodes that are used by the D front-end.
792 These are distinct from C types. */
793
794 static void
795 d_build_d_type_nodes (void)
796 {
797 /* Integral types. */
798 d_byte_type = make_signed_type (8);
799 d_ubyte_type = make_unsigned_type (8);
800
801 d_short_type = make_signed_type (16);
802 d_ushort_type = make_unsigned_type (16);
803
804 d_int_type = make_signed_type (32);
805 d_uint_type = make_unsigned_type (32);
806
807 d_long_type = make_signed_type (64);
808 d_ulong_type = make_unsigned_type (64);
809
810 d_cent_type = make_signed_type (128);
811 d_ucent_type = make_unsigned_type (128);
812
813 {
814 /* Re-define size_t as a D type. */
815 machine_mode type_mode = TYPE_MODE (size_type_node);
816 size_type_node = lang_hooks.types.type_for_mode (type_mode, 1);
817 }
818
819 /* Bool and Character types. */
820 d_bool_type = make_unsigned_type (1);
821 TREE_SET_CODE (d_bool_type, BOOLEAN_TYPE);
822
823 char8_type_node = make_unsigned_type (8);
824 TYPE_STRING_FLAG (char8_type_node) = 1;
825
826 char16_type_node = make_unsigned_type (16);
827 TYPE_STRING_FLAG (char16_type_node) = 1;
828
829 char32_type_node = make_unsigned_type (32);
830 TYPE_STRING_FLAG (char32_type_node) = 1;
831
832 /* Imaginary types. */
833 ifloat_type_node = build_distinct_type_copy (float_type_node);
834 TYPE_IMAGINARY_FLOAT (ifloat_type_node) = 1;
835
836 idouble_type_node = build_distinct_type_copy (double_type_node);
837 TYPE_IMAGINARY_FLOAT (idouble_type_node) = 1;
838
839 ireal_type_node = build_distinct_type_copy (long_double_type_node);
840 TYPE_IMAGINARY_FLOAT (ireal_type_node) = 1;
841
842 /* Used for ModuleInfo, ClassInfo, and Interface decls. */
843 unknown_type_node = make_node (RECORD_TYPE);
844
845 /* Make sure we get a unique function type, so we can give
846 its pointer type a name. (This wins for gdb). */
847 {
848 tree vfunc_type = make_node (FUNCTION_TYPE);
849 TREE_TYPE (vfunc_type) = d_int_type;
850 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
851 layout_type (vfunc_type);
852
853 vtable_entry_type = build_pointer_type (vfunc_type);
854 }
855
856 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
857 layout_type (vtbl_ptr_type_node);
858
859 /* When an object is accessed via an interface, this type appears
860 as the first entry in its vtable. */
861 {
862 tree domain = build_index_type (size_int (3));
863 vtbl_interface_type_node = build_array_type (ptr_type_node, domain);
864 }
865
866 /* Use `void[]' as a generic dynamic array type. */
867 array_type_node = make_struct_type ("__builtin_void[]", 2,
868 get_identifier ("length"), size_type_node,
869 get_identifier ("ptr"), ptr_type_node);
870 TYPE_DYNAMIC_ARRAY (array_type_node) = 1;
871
872 null_array_node = d_array_value (array_type_node, size_zero_node,
873 null_pointer_node);
874 }
875
876 /* Handle default attributes. */
877
878 enum built_in_attribute
879 {
880 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
881 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
882 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
883 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
884 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
885 #include "builtin-attrs.def"
886 #undef DEF_ATTR_NULL_TREE
887 #undef DEF_ATTR_INT
888 #undef DEF_ATTR_STRING
889 #undef DEF_ATTR_IDENT
890 #undef DEF_ATTR_TREE_LIST
891 ATTR_LAST
892 };
893
894 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
895
896 /* Initialize the attribute table for all the supported builtins. */
897
898 static void
899 d_init_attributes (void)
900 {
901 /* Fill in the built_in_attributes array. */
902 #define DEF_ATTR_NULL_TREE(ENUM) \
903 built_in_attributes[(int) ENUM] = NULL_TREE;
904 # define DEF_ATTR_INT(ENUM, VALUE) \
905 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
906 #define DEF_ATTR_STRING(ENUM, VALUE) \
907 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
908 #define DEF_ATTR_IDENT(ENUM, STRING) \
909 built_in_attributes[(int) ENUM] = get_identifier (STRING);
910 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
911 built_in_attributes[(int) ENUM] \
912 = tree_cons (built_in_attributes[(int) PURPOSE], \
913 built_in_attributes[(int) VALUE], \
914 built_in_attributes[(int) CHAIN]);
915 #include "builtin-attrs.def"
916 #undef DEF_ATTR_NULL_TREE
917 #undef DEF_ATTR_INT
918 #undef DEF_ATTR_STRING
919 #undef DEF_ATTR_IDENT
920 #undef DEF_ATTR_TREE_LIST
921 }
922
923 /* Builtin types. */
924
925 enum d_builtin_type
926 {
927 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
928 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
929 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
930 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
931 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
932 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
933 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
934 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
935 ARG6) NAME,
936 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
937 ARG6, ARG7) NAME,
938 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
939 ARG6, ARG7, ARG8) NAME,
940 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
941 ARG6, ARG7, ARG8, ARG9) NAME,
942 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
943 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
944 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
945 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
946 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
947 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
948 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
949 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
950 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
951 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
952 NAME,
953 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
954 ARG6) NAME,
955 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
956 ARG6, ARG7) NAME,
957 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
958 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
959 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
960 #include "builtin-types.def"
961 #undef DEF_PRIMITIVE_TYPE
962 #undef DEF_FUNCTION_TYPE_0
963 #undef DEF_FUNCTION_TYPE_1
964 #undef DEF_FUNCTION_TYPE_2
965 #undef DEF_FUNCTION_TYPE_3
966 #undef DEF_FUNCTION_TYPE_4
967 #undef DEF_FUNCTION_TYPE_5
968 #undef DEF_FUNCTION_TYPE_6
969 #undef DEF_FUNCTION_TYPE_7
970 #undef DEF_FUNCTION_TYPE_8
971 #undef DEF_FUNCTION_TYPE_9
972 #undef DEF_FUNCTION_TYPE_10
973 #undef DEF_FUNCTION_TYPE_11
974 #undef DEF_FUNCTION_TYPE_VAR_0
975 #undef DEF_FUNCTION_TYPE_VAR_1
976 #undef DEF_FUNCTION_TYPE_VAR_2
977 #undef DEF_FUNCTION_TYPE_VAR_3
978 #undef DEF_FUNCTION_TYPE_VAR_4
979 #undef DEF_FUNCTION_TYPE_VAR_5
980 #undef DEF_FUNCTION_TYPE_VAR_6
981 #undef DEF_FUNCTION_TYPE_VAR_7
982 #undef DEF_FUNCTION_TYPE_VAR_11
983 #undef DEF_POINTER_TYPE
984 BT_LAST
985 };
986
987 typedef enum d_builtin_type builtin_type;
988
989 /* A temporary array used in communication with def_fn_type. */
990 static GTY(()) tree builtin_types[(int) BT_LAST + 1];
991
992 /* A helper function for d_init_builtins. Build function type for DEF with
993 return type RET and N arguments. If VAR is true, then the function should
994 be variadic after those N arguments.
995
996 Takes special care not to ICE if any of the types involved are
997 error_mark_node, which indicates that said type is not in fact available
998 (see builtin_type_for_size). In which case the function type as a whole
999 should be error_mark_node. */
1000
1001 static void
1002 def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
1003 {
1004 tree t;
1005 tree *args = XALLOCAVEC (tree, n);
1006 va_list list;
1007 int i;
1008
1009 va_start (list, n);
1010 for (i = 0; i < n; ++i)
1011 {
1012 builtin_type a = (builtin_type) va_arg (list, int);
1013 t = builtin_types[a];
1014 if (t == error_mark_node)
1015 goto egress;
1016 args[i] = t;
1017 }
1018
1019 t = builtin_types[ret];
1020 if (t == error_mark_node)
1021 goto egress;
1022 if (var)
1023 t = build_varargs_function_type_array (t, n, args);
1024 else
1025 t = build_function_type_array (t, n, args);
1026
1027 egress:
1028 builtin_types[def] = t;
1029 va_end (list);
1030 }
1031
1032 /* Create builtin types and functions. VA_LIST_REF_TYPE_NODE and
1033 VA_LIST_ARG_TYPE_NODE are used in builtin-types.def. */
1034
1035 static void
1036 d_define_builtins (tree va_list_ref_type_node ATTRIBUTE_UNUSED,
1037 tree va_list_arg_type_node ATTRIBUTE_UNUSED)
1038 {
1039 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
1040 builtin_types[(int) ENUM] = VALUE;
1041 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
1042 def_fn_type (ENUM, RETURN, 0, 0);
1043 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
1044 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
1045 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
1046 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
1047 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
1048 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
1049 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
1050 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
1051 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
1052 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
1053 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1054 ARG6) \
1055 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
1056 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1057 ARG6, ARG7) \
1058 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
1059 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1060 ARG6, ARG7, ARG8) \
1061 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1062 ARG7, ARG8);
1063 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1064 ARG6, ARG7, ARG8, ARG9) \
1065 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1066 ARG7, ARG8, ARG9);
1067 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1068 ARG6, ARG7, ARG8, ARG9, ARG10) \
1069 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1070 ARG7, ARG8, ARG9, ARG10);
1071 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1072 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
1073 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1074 ARG7, ARG8, ARG9, ARG10, ARG11);
1075 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
1076 def_fn_type (ENUM, RETURN, 1, 0);
1077 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
1078 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
1079 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
1080 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
1081 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
1082 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
1083 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
1084 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
1085 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
1086 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
1087 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1088 ARG6) \
1089 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
1090 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1091 ARG6, ARG7) \
1092 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
1093 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1094 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
1095 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1096 ARG7, ARG8, ARG9, ARG10, ARG11);
1097 #define DEF_POINTER_TYPE(ENUM, TYPE) \
1098 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
1099
1100 #include "builtin-types.def"
1101
1102 #undef DEF_PRIMITIVE_TYPE
1103 #undef DEF_FUNCTION_TYPE_1
1104 #undef DEF_FUNCTION_TYPE_2
1105 #undef DEF_FUNCTION_TYPE_3
1106 #undef DEF_FUNCTION_TYPE_4
1107 #undef DEF_FUNCTION_TYPE_5
1108 #undef DEF_FUNCTION_TYPE_6
1109 #undef DEF_FUNCTION_TYPE_7
1110 #undef DEF_FUNCTION_TYPE_8
1111 #undef DEF_FUNCTION_TYPE_9
1112 #undef DEF_FUNCTION_TYPE_10
1113 #undef DEF_FUNCTION_TYPE_11
1114 #undef DEF_FUNCTION_TYPE_VAR_0
1115 #undef DEF_FUNCTION_TYPE_VAR_1
1116 #undef DEF_FUNCTION_TYPE_VAR_2
1117 #undef DEF_FUNCTION_TYPE_VAR_3
1118 #undef DEF_FUNCTION_TYPE_VAR_4
1119 #undef DEF_FUNCTION_TYPE_VAR_5
1120 #undef DEF_FUNCTION_TYPE_VAR_6
1121 #undef DEF_FUNCTION_TYPE_VAR_7
1122 #undef DEF_FUNCTION_TYPE_VAR_11
1123 #undef DEF_POINTER_TYPE
1124 builtin_types[(int) BT_LAST] = NULL_TREE;
1125
1126 d_init_attributes ();
1127
1128 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
1129 NONANSI_P, ATTRS, IMPLICIT, COND) \
1130 if (NAME && COND) \
1131 do_build_builtin_fn (ENUM, NAME, CLASS, \
1132 builtin_types[(int) TYPE], \
1133 BOTH_P, FALLBACK_P, \
1134 built_in_attributes[(int) ATTRS], IMPLICIT);
1135 #include "builtins.def"
1136 #undef DEF_BUILTIN
1137 }
1138
1139 /* Build builtin functions and types for the D language frontend. */
1140
1141 void
1142 d_init_builtins (void)
1143 {
1144 /* Build the "standard" abi va_list. */
1145 Type::tvalist = build_frontend_type (va_list_type_node);
1146 if (!Type::tvalist)
1147 sorry ("cannot represent built-in %<va_list%> type in D");
1148
1149 /* Map the va_list type to the D frontend Type. This is to prevent both
1150 errors in gimplification or an ICE in targetm.canonical_va_list_type. */
1151 Type::tvalist->ctype = va_list_type_node;
1152 TYPE_LANG_SPECIFIC (va_list_type_node) = build_lang_type (Type::tvalist);
1153
1154 d_build_c_type_nodes ();
1155 d_build_d_type_nodes ();
1156
1157 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
1158 {
1159 /* It might seem natural to make the argument type a pointer, but there
1160 is no implicit casting from arrays to pointers in D. */
1161 d_define_builtins (va_list_type_node, va_list_type_node);
1162 }
1163 else
1164 {
1165 d_define_builtins (build_reference_type (va_list_type_node),
1166 va_list_type_node);
1167 }
1168
1169 targetm.init_builtins ();
1170 build_common_builtin_nodes ();
1171 }
1172
1173 /* Registration of machine- or os-specific builtin types.
1174 Add to builtin types list for maybe processing later
1175 if `gcc.builtins' was imported into the current module. */
1176
1177 void
1178 d_register_builtin_type (tree type, const char *name)
1179 {
1180 tree decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL,
1181 get_identifier (name), type);
1182 DECL_ARTIFICIAL (decl) = 1;
1183
1184 if (!TYPE_NAME (type))
1185 TYPE_NAME (type) = decl;
1186
1187 vec_safe_push (gcc_builtins_types, decl);
1188 }
1189
1190 /* Add DECL to builtin functions list for maybe processing later
1191 if `gcc.builtins' was imported into the current module. */
1192
1193 tree
1194 d_builtin_function (tree decl)
1195 {
1196 if (!flag_no_builtin && DECL_ASSEMBLER_NAME_SET_P (decl))
1197 vec_safe_push (gcc_builtins_libfuncs, decl);
1198
1199 vec_safe_push (gcc_builtins_functions, decl);
1200 return decl;
1201 }
1202
1203
1204 #include "gt-d-d-builtins.h"