Debian Codegraph @

gcc/d/d-builtins.cc

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