1 /* d-builtins.cc -- GCC builtins support for D.
2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
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)
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.
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/>. */
20 #include "coretypes.h"
22 #include "dmd/attrib.h"
23 #include "dmd/aggregate.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"
33 #include "fold-const.h"
34 #include "diagnostic.h"
35 #include "langhooks.h"
37 #include "common/common-target.h"
38 #include "stringpool.h"
39 #include "stor-layout.h"
45 static GTY(()) vec
<tree
, va_gc
> *gcc_builtins_functions
= NULL
;
46 static GTY(()) vec
<tree
, va_gc
> *gcc_builtins_libfuncs
= NULL
;
47 static GTY(()) vec
<tree
, va_gc
> *gcc_builtins_types
= NULL
;
49 /* Record built-in types and their associated decls for re-use when
50 generating the `gcc.builtins' module. */
58 builtin_data (Type
*t
, tree c
, Dsymbol
*d
= NULL
)
59 : dtype(t
), ctype(c
), dsym(d
)
63 static vec
<builtin_data
> builtin_converted_decls
;
65 /* Build D frontend type from tree TYPE type given. This will set the
66 back-end type symbol directly for complex types to save build_ctype()
67 the work. For other types, it is not useful or will cause errors, such
68 as casting from `C char' to `D char', which also means that `char *`
69 needs to be specially handled. */
72 build_frontend_type (tree type
)
77 if (TYPE_READONLY (type
))
79 if (TYPE_VOLATILE (type
))
82 /* If we've seen the type before, re-use the converted decl. */
83 for (size_t i
= 0; i
< builtin_converted_decls
.length (); ++i
)
85 tree t
= builtin_converted_decls
[i
].ctype
;
86 if (TYPE_MAIN_VARIANT (t
) == TYPE_MAIN_VARIANT (type
))
87 return builtin_converted_decls
[i
].dtype
;
90 switch (TREE_CODE (type
))
93 dtype
= build_frontend_type (TREE_TYPE (type
));
96 /* Check for char * first. Needs to be done for chars/string. */
97 if (TYPE_MAIN_VARIANT (TREE_TYPE (type
)) == char_type_node
)
98 return Type::tchar
->addMod (dtype
->mod
)->pointerTo ()->addMod (mod
);
100 if (dtype
->ty
== Tfunction
)
101 return (TypePointer::create (dtype
))->addMod (mod
);
103 return dtype
->pointerTo ()->addMod (mod
);
108 dtype
= build_frontend_type (TREE_TYPE (type
));
111 /* Want to assign ctype directly so that the REFERENCE_TYPE code
112 can be turned into as an `inout' argument. Can't use pointerTo(),
113 because the returned Type is shared. */
114 dtype
= (TypePointer::create (dtype
))->addMod (mod
);
116 builtin_converted_decls
.safe_push (builtin_data (dtype
, type
));
122 /* Should be no need for size checking. */
123 return Type::tbool
->addMod (mod
);
127 unsigned size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
));
128 bool unsignedp
= TYPE_UNSIGNED (type
);
130 /* For now, skip support for cent/ucent until the frontend
131 has better support for handling it. */
132 for (size_t i
= Tint8
; i
<= Tuns64
; i
++)
134 dtype
= Type::basic
[i
];
136 /* Search for type matching size and signedness. */
137 if (unsignedp
!= dtype
->isunsigned ()
138 || size
!= dtype
->size ())
141 return dtype
->addMod (mod
);
148 unsigned size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
));
150 for (size_t i
= Tfloat32
; i
<= Tfloat80
; i
++)
152 dtype
= Type::basic
[i
];
154 /* Search for type matching size. */
155 if (dtype
->size () != size
)
158 return dtype
->addMod (mod
);
165 unsigned size
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type
));
166 for (size_t i
= Tcomplex32
; i
<= Tcomplex80
; i
++)
168 dtype
= Type::basic
[i
];
170 /* Search for type matching size. */
171 if (dtype
->size () != size
)
174 return dtype
->addMod (mod
);
180 return Type::tvoid
->addMod (mod
);
183 dtype
= build_frontend_type (TREE_TYPE (type
));
186 tree index
= TYPE_DOMAIN (type
);
187 tree ub
= TYPE_MAX_VALUE (index
);
188 tree lb
= TYPE_MIN_VALUE (index
);
190 tree length
= fold_build2 (MINUS_EXPR
, TREE_TYPE (lb
), ub
, lb
);
191 length
= size_binop (PLUS_EXPR
, size_one_node
,
192 convert (sizetype
, length
));
194 dtype
= dtype
->sarrayOf (TREE_INT_CST_LOW (length
))->addMod (mod
);
195 builtin_converted_decls
.safe_push (builtin_data (dtype
, type
));
202 unsigned HOST_WIDE_INT nunits
;
203 if (!TYPE_VECTOR_SUBPARTS (type
).is_constant (&nunits
))
206 dtype
= build_frontend_type (TREE_TYPE (type
));
210 dtype
= dtype
->sarrayOf (nunits
)->addMod (mod
);
211 if (target
.isVectorTypeSupported (dtype
->size (), dtype
->nextOf ()))
214 dtype
= (TypeVector::create (dtype
))->addMod (mod
);
215 builtin_converted_decls
.safe_push (builtin_data (dtype
, type
));
221 Identifier
*ident
= TYPE_IDENTIFIER (type
) ?
222 Identifier::idPool (IDENTIFIER_POINTER (TYPE_IDENTIFIER (type
))) : NULL
;
224 /* Neither the `object' and `gcc.builtins' modules will not exist when
225 this is called. Use a stub `object' module parent in the meantime.
226 If `gcc.builtins' is later imported, the parent will be overridden
227 with the correct module symbol. */
228 static Identifier
*object
= Identifier::idPool ("object");
229 static Module
*stubmod
= Module::create ("object.d", object
, 0, 0);
231 StructDeclaration
*sdecl
= StructDeclaration::create (Loc (), ident
,
233 sdecl
->parent
= stubmod
;
234 sdecl
->structsize
= int_size_in_bytes (type
);
235 sdecl
->alignsize
= TYPE_ALIGN_UNIT (type
);
236 sdecl
->alignment
= STRUCTALIGN_DEFAULT
;
237 sdecl
->sizeok
= SIZEOKdone
;
238 sdecl
->type
= (TypeStruct::create (sdecl
))->addMod (mod
);
239 sdecl
->type
->ctype
= type
;
240 sdecl
->type
->merge2 ();
242 /* Add both named and anonymous fields as members of the struct.
243 Anonymous fields still need a name in D, so call them "__pad%d". */
244 int anonfield_id
= 0;
245 sdecl
->members
= new Dsymbols
;
247 for (tree field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
249 Type
*ftype
= build_frontend_type (TREE_TYPE (field
));
252 delete sdecl
->members
;
257 if (DECL_NAME (field
) == NULL_TREE
)
258 fident
= Identifier::generateId ("__pad", anonfield_id
++);
261 const char *name
= IDENTIFIER_POINTER (DECL_NAME (field
));
262 fident
= Identifier::idPool (name
);
265 VarDeclaration
*vd
= VarDeclaration::create (Loc (), ftype
, fident
,
268 vd
->offset
= tree_to_uhwi (byte_position (field
));
269 vd
->semanticRun
= PASSsemanticdone
;
271 sdecl
->members
->push (vd
);
272 sdecl
->fields
.push (vd
);
276 builtin_converted_decls
.safe_push (builtin_data (dtype
, type
, sdecl
));
281 dtype
= build_frontend_type (TREE_TYPE (type
));
284 tree parms
= TYPE_ARG_TYPES (type
);
285 VarArg varargs_p
= VARARGvariadic
;
287 Parameters
*args
= new Parameters
;
288 args
->reserve (list_length (parms
));
290 /* Attempt to convert all parameter types. */
291 for (tree parm
= parms
; parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
293 tree argtype
= TREE_VALUE (parm
);
294 if (argtype
== void_type_node
)
296 varargs_p
= VARARGnone
;
300 StorageClass sc
= STCundefined
;
301 if (TREE_CODE (argtype
) == REFERENCE_TYPE
)
303 argtype
= TREE_TYPE (argtype
);
307 Type
*targ
= build_frontend_type (argtype
);
314 args
->push (Parameter::create (sc
, targ
, NULL
, NULL
));
317 /* GCC generic and placeholder built-ins are marked as variadic, yet
318 have no named parameters, and so can't be represented in D. */
319 if (args
->length
!= 0 || varargs_p
== VARARGnone
)
321 dtype
= TypeFunction::create (args
, dtype
, varargs_p
, LINKc
);
322 return dtype
->addMod (mod
);
334 /* Attempt to convert GCC evaluated CST to a D Frontend Expression.
335 This is used for getting the CTFE value out of a const-folded builtin,
336 returns NULL if it cannot convert CST. */
339 d_eval_constant_expression (tree cst
)
341 STRIP_TYPE_NOPS (cst
);
342 Type
*type
= build_frontend_type (TREE_TYPE (cst
));
346 /* Convert our GCC CST tree into a D Expression. This seems like we are
347 trying too hard, as these will only be converted back to a tree again
348 later in the codegen pass, but satisfies the need to have GCC built-ins
349 CTFE-able in the frontend. */
350 tree_code code
= TREE_CODE (cst
);
351 if (code
== COMPLEX_CST
)
353 real_value re
= TREE_REAL_CST (TREE_REALPART (cst
));
354 real_value im
= TREE_REAL_CST (TREE_IMAGPART (cst
));
355 complex_t value
= complex_t (ldouble (re
), ldouble (im
));
356 return ComplexExp::create (Loc (), value
, type
);
358 else if (code
== INTEGER_CST
)
360 dinteger_t value
= TREE_INT_CST_LOW (cst
);
361 return IntegerExp::create (Loc (), value
, type
);
363 else if (code
== REAL_CST
)
365 real_value value
= TREE_REAL_CST (cst
);
366 return RealExp::create (Loc (), ldouble (value
), type
);
368 else if (code
== STRING_CST
)
370 const void *string
= TREE_STRING_POINTER (cst
);
371 size_t len
= TREE_STRING_LENGTH (cst
);
372 return StringExp::create (Loc (), CONST_CAST (void *, string
), len
);
374 else if (code
== VECTOR_CST
)
376 dinteger_t nunits
= VECTOR_CST_NELTS (cst
).to_constant ();
377 Expressions
*elements
= new Expressions
;
378 elements
->setDim (nunits
);
380 for (size_t i
= 0; i
< nunits
; i
++)
383 = d_eval_constant_expression (VECTOR_CST_ELT (cst
, i
));
387 (*elements
)[i
] = elem
;
390 Expression
*e
= ArrayLiteralExp::create (Loc (), elements
);
391 e
->type
= type
->isTypeVector ()->basetype
;
393 return VectorExp::create (Loc (), e
, type
);
400 /* Callback for TARGET_D_CPU_VERSIONS and TARGET_D_OS_VERSIONS.
401 Adds IDENT to the list of predefined version identifiers. */
404 d_add_builtin_version (const char* ident
)
406 /* For now, we need to tell the D frontend what platform is being targeted.
407 This should be removed once the frontend has been fixed. */
408 if (strcmp (ident
, "linux") == 0)
409 global
.params
.isLinux
= true;
410 else if (strcmp (ident
, "OSX") == 0)
411 global
.params
.isOSX
= true;
412 else if (strcmp (ident
, "Windows") == 0)
413 global
.params
.isWindows
= true;
414 else if (strcmp (ident
, "FreeBSD") == 0)
415 global
.params
.isFreeBSD
= true;
416 else if (strcmp (ident
, "OpenBSD") == 0)
417 global
.params
.isOpenBSD
= true;
418 else if (strcmp (ident
, "Solaris") == 0)
419 global
.params
.isSolaris
= true;
420 /* The is64bit field only refers to x86_64 target. */
421 else if (strcmp (ident
, "X86_64") == 0)
422 global
.params
.is64bit
= true;
423 /* No other fields are required to be set for the frontend. */
425 VersionCondition::addPredefinedGlobalIdent (ident
);
428 /* Initialize the list of all the predefined version identifiers. */
431 d_init_versions (void)
433 VersionCondition::addPredefinedGlobalIdent ("GNU");
434 VersionCondition::addPredefinedGlobalIdent ("D_Version2");
436 if (BYTES_BIG_ENDIAN
)
437 VersionCondition::addPredefinedGlobalIdent ("BigEndian");
439 VersionCondition::addPredefinedGlobalIdent ("LittleEndian");
441 if (targetm_common
.except_unwind_info (&global_options
) == UI_SJLJ
)
442 VersionCondition::addPredefinedGlobalIdent ("GNU_SjLj_Exceptions");
443 else if (targetm_common
.except_unwind_info (&global_options
) == UI_SEH
)
444 VersionCondition::addPredefinedGlobalIdent ("GNU_SEH_Exceptions");
445 else if (targetm_common
.except_unwind_info (&global_options
) == UI_DWARF2
)
446 VersionCondition::addPredefinedGlobalIdent ("GNU_DWARF2_Exceptions");
448 if (!targetm
.have_tls
)
449 VersionCondition::addPredefinedGlobalIdent ("GNU_EMUTLS");
451 if (STACK_GROWS_DOWNWARD
)
452 VersionCondition::addPredefinedGlobalIdent ("GNU_StackGrowsDown");
454 /* Should define this anyway to set us apart from the competition. */
455 VersionCondition::addPredefinedGlobalIdent ("GNU_InlineAsm");
457 /* LP64 only means 64bit pointers in D. */
458 if (global
.params
.isLP64
)
459 VersionCondition::addPredefinedGlobalIdent ("D_LP64");
461 /* Setting `global.params.cov' forces module info generation which is
462 not needed for the GCC coverage implementation. Instead, just
463 test flag_test_coverage while leaving `global.params.cov' unset. */
464 if (flag_test_coverage
)
465 VersionCondition::addPredefinedGlobalIdent ("D_Coverage");
467 VersionCondition::addPredefinedGlobalIdent ("D_PIC");
469 if (global
.params
.doDocComments
)
470 VersionCondition::addPredefinedGlobalIdent ("D_Ddoc");
472 if (global
.params
.useUnitTests
)
473 VersionCondition::addPredefinedGlobalIdent ("unittest");
475 if (global
.params
.useAssert
== CHECKENABLEon
)
476 VersionCondition::addPredefinedGlobalIdent ("assert");
478 if (global
.params
.useArrayBounds
== CHECKENABLEoff
)
479 VersionCondition::addPredefinedGlobalIdent ("D_NoBoundsChecks");
481 if (global
.params
.betterC
)
482 VersionCondition::addPredefinedGlobalIdent ("D_BetterC");
485 VersionCondition::addPredefinedGlobalIdent ("D_ModuleInfo");
486 VersionCondition::addPredefinedGlobalIdent ("D_Exceptions");
487 VersionCondition::addPredefinedGlobalIdent ("D_TypeInfo");
490 VersionCondition::addPredefinedGlobalIdent ("all");
492 /* Emit all target-specific version identifiers. */
493 targetdm
.d_cpu_versions ();
494 targetdm
.d_os_versions ();
496 VersionCondition::addPredefinedGlobalIdent ("CppRuntime_Gcc");
499 /* A helper for d_build_builtins_module. Return a new ALIAS for TYPE.
500 Analogous to `alias ALIAS = TYPE' in D code. */
502 static AliasDeclaration
*
503 build_alias_declaration (const char *alias
, Type
*type
)
505 return AliasDeclaration::create (Loc (), Identifier::idPool (alias
), type
);
508 /* A helper function for Target::loadModule. Generates all code for the
509 `gcc.builtins' module, whose frontend symbol should be M. */
512 d_build_builtins_module (Module
*m
)
514 Dsymbols
*members
= new Dsymbols
;
517 for (size_t i
= 0; vec_safe_iterate (gcc_builtins_functions
, i
, &decl
); ++i
)
519 const char *name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
520 Type
*t
= build_frontend_type (TREE_TYPE (decl
));
521 TypeFunction
*tf
= t
? t
->isTypeFunction () : NULL
;
523 /* Cannot create built-in function type for DECL. */
527 /* A few notes on D2 attributes applied to builtin functions:
528 - It is assumed that built-ins solely provided by the compiler are
529 considered @safe and pure.
530 - Built-ins that correspond to `extern(C)' functions in the standard
531 library that have `__attribute__(nothrow)' are considered `@trusted'.
532 - The purity of a built-in can vary depending on compiler flags set
533 upon initialization, or by the `-foptions' passed, such as
534 flag_unsafe_math_optimizations.
535 - Built-ins never use the GC or raise a D exception, and so are always
536 marked as `nothrow' and `@nogc'. */
537 tf
->purity
= DECL_PURE_P (decl
) ? PUREstrong
538 : TREE_READONLY (decl
) ? PUREconst
539 : DECL_IS_NOVOPS (decl
) ? PUREweak
540 : !DECL_ASSEMBLER_NAME_SET_P (decl
) ? PUREweak
542 tf
->trust
= !DECL_ASSEMBLER_NAME_SET_P (decl
) ? TRUSTsafe
543 : TREE_NOTHROW (decl
) ? TRUSTtrusted
545 tf
->isnothrow
= true;
548 FuncDeclaration
*func
549 = FuncDeclaration::create (Loc (), Loc (),
550 Identifier::idPool (name
),
552 DECL_LANG_SPECIFIC (decl
) = build_lang_decl (func
);
554 func
->builtin
= BUILTINyes
;
556 members
->push (func
);
559 for (size_t i
= 0; vec_safe_iterate (gcc_builtins_types
, i
, &decl
); ++i
)
561 const char *name
= IDENTIFIER_POINTER (DECL_NAME (decl
));
562 Type
*t
= build_frontend_type (TREE_TYPE (decl
));
564 /* Cannot create built-in type for DECL. */
568 members
->push (build_alias_declaration (name
, t
));
571 /* Iterate through the target-specific builtin types for va_list. */
572 if (targetm
.enum_va_list_p
)
577 for (int i
= 0; targetm
.enum_va_list_p (i
, &name
, &type
); ++i
)
579 Type
*t
= build_frontend_type (type
);
580 /* Cannot create built-in type. */
584 members
->push (build_alias_declaration (name
, t
));
588 /* Push out declarations for any RECORD_TYPE types encountered when building
589 all builtin functions and types. */
590 for (size_t i
= 0; i
< builtin_converted_decls
.length (); ++i
)
592 /* Currently, there is no need to run semantic, but we do want to output
593 initializers, typeinfo, and others on demand. */
594 Dsymbol
*dsym
= builtin_converted_decls
[i
].dsym
;
595 if (dsym
!= NULL
&& !dsym
->isAnonymous ())
598 members
->push (dsym
);
602 /* Expose target-specific va_list type. */
603 Type
*tvalist
= target
.va_listType (Loc (), NULL
);
604 TypeStruct
*ts
= tvalist
->isTypeStruct ();
605 if (ts
== NULL
|| !ts
->sym
->isAnonymous ())
606 members
->push (build_alias_declaration ("__builtin_va_list", tvalist
));
609 ts
->sym
->ident
= Identifier::idPool ("__builtin_va_list");
610 members
->push (ts
->sym
);
613 /* Expose target-specific integer types to the builtins module. */
615 Type
*t
= build_frontend_type (long_integer_type_node
);
616 members
->push (build_alias_declaration ("__builtin_clong", t
));
618 t
= build_frontend_type (long_unsigned_type_node
);
619 members
->push (build_alias_declaration ("__builtin_culong", t
));
621 t
= build_frontend_type (long_long_integer_type_node
);
622 members
->push (build_alias_declaration ("__builtin_clonglong", t
));
624 t
= build_frontend_type (long_long_unsigned_type_node
);
625 members
->push (build_alias_declaration ("__builtin_culonglong", t
));
627 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (byte_mode
, 0));
628 members
->push (build_alias_declaration ("__builtin_machine_byte", t
));
630 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (byte_mode
, 1));
631 members
->push (build_alias_declaration ("__builtin_machine_ubyte", t
));
633 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (word_mode
, 0));
634 members
->push (build_alias_declaration ("__builtin_machine_int", t
));
636 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (word_mode
, 1));
637 members
->push (build_alias_declaration ("__builtin_machine_uint", t
));
639 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (ptr_mode
, 0));
640 members
->push (build_alias_declaration ("__builtin_pointer_int", t
));
642 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (ptr_mode
, 1));
643 members
->push (build_alias_declaration ("__builtin_pointer_uint", t
));
645 /* _Unwind_Word has its own target specific mode. */
646 machine_mode mode
= targetm
.unwind_word_mode ();
647 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (mode
, 0));
648 members
->push (build_alias_declaration ("__builtin_unwind_int", t
));
650 t
= build_frontend_type (lang_hooks
.types
.type_for_mode (mode
, 1));
651 members
->push (build_alias_declaration ("__builtin_unwind_uint", t
));
654 m
->members
->push (LinkDeclaration::create (LINKc
, members
));
657 /* Search for any `extern(C)' functions that match any known GCC library builtin
658 function in D and override its internal back-end symbol. */
661 maybe_set_builtin_1 (Dsymbol
*d
)
663 AttribDeclaration
*ad
= d
->isAttribDeclaration ();
664 FuncDeclaration
*fd
= d
->isFuncDeclaration ();
668 /* Recursively search through attribute decls. */
669 Dsymbols
*decls
= ad
->include (NULL
);
670 if (decls
&& decls
->length
)
672 for (size_t i
= 0; i
< decls
->length
; i
++)
674 Dsymbol
*sym
= (*decls
)[i
];
675 maybe_set_builtin_1 (sym
);
679 else if (fd
&& !fd
->fbody
)
683 for (size_t i
= 0; vec_safe_iterate (gcc_builtins_libfuncs
, i
, &t
); ++i
)
685 gcc_assert (DECL_ASSEMBLER_NAME_SET_P (t
));
687 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
));
688 if (fd
->ident
!= Identifier::idPool (name
))
691 /* Found a match, tell the frontend this is a builtin. */
692 DECL_LANG_SPECIFIC (t
) = build_lang_decl (fd
);
694 fd
->builtin
= BUILTINyes
;
700 /* A helper function for Target::loadModule. Traverse all members in module M
701 to search for any functions that can be mapped to any GCC builtin. */
704 d_maybe_set_builtin (Module
*m
)
706 if (!m
|| !m
->members
)
709 for (size_t i
= 0; i
< m
->members
->length
; i
++)
711 Dsymbol
*sym
= (*m
->members
)[i
];
712 maybe_set_builtin_1 (sym
);
716 /* Used to help initialize the builtin-types.def table. When a type of
717 the correct size doesn't exist, use error_mark_node instead of NULL.
718 The latter results in segfaults even when a decl using the type doesn't
722 builtin_type_for_size (int size
, bool unsignedp
)
724 tree type
= lang_hooks
.types
.type_for_size (size
, unsignedp
);
725 return type
? type
: error_mark_node
;
728 /* Support for DEF_BUILTIN. */
731 do_build_builtin_fn (built_in_function fncode
,
733 built_in_class fnclass
,
734 tree fntype
, bool both_p
, bool fallback_p
,
735 tree fnattrs
, bool implicit_p
)
740 if (fntype
== error_mark_node
)
743 gcc_assert ((!both_p
&& !fallback_p
)
744 || !strncmp (name
, "__builtin_",
745 strlen ("__builtin_")));
747 libname
= name
+ strlen ("__builtin_");
749 decl
= add_builtin_function (name
, fntype
, fncode
, fnclass
,
750 fallback_p
? libname
: NULL
, fnattrs
);
752 set_builtin_decl (fncode
, decl
, implicit_p
);
755 /* Standard data types to be used in builtin argument declarations. */
757 static GTY(()) tree string_type_node
;
758 static GTY(()) tree const_string_type_node
;
759 static GTY(()) tree wint_type_node
;
760 static GTY(()) tree intmax_type_node
;
761 static GTY(()) tree uintmax_type_node
;
762 static GTY(()) tree signed_size_type_node
;
765 /* Build nodes that would have been created by the C front-end; necessary
766 for including builtin-types.def and ultimately builtins.def. */
769 d_build_c_type_nodes (void)
771 void_list_node
= build_tree_list (NULL_TREE
, void_type_node
);
772 string_type_node
= build_pointer_type (char_type_node
);
773 const_string_type_node
774 = build_pointer_type (build_qualified_type (char_type_node
,
777 if (strcmp (UINTMAX_TYPE
, "unsigned int") == 0)
779 intmax_type_node
= integer_type_node
;
780 uintmax_type_node
= unsigned_type_node
;
782 else if (strcmp (UINTMAX_TYPE
, "long unsigned int") == 0)
784 intmax_type_node
= long_integer_type_node
;
785 uintmax_type_node
= long_unsigned_type_node
;
787 else if (strcmp (UINTMAX_TYPE
, "long long unsigned int") == 0)
789 intmax_type_node
= long_long_integer_type_node
;
790 uintmax_type_node
= long_long_unsigned_type_node
;
795 signed_size_type_node
= signed_type_for (size_type_node
);
796 wint_type_node
= unsigned_type_node
;
797 pid_type_node
= integer_type_node
;
800 /* Build nodes that are used by the D front-end.
801 These are distinct from C types. */
804 d_build_d_type_nodes (void)
806 /* Integral types. */
807 d_byte_type
= make_signed_type (8);
808 d_ubyte_type
= make_unsigned_type (8);
810 d_short_type
= make_signed_type (16);
811 d_ushort_type
= make_unsigned_type (16);
813 d_int_type
= make_signed_type (32);
814 d_uint_type
= make_unsigned_type (32);
816 d_long_type
= make_signed_type (64);
817 d_ulong_type
= make_unsigned_type (64);
819 d_cent_type
= make_signed_type (128);
820 d_ucent_type
= make_unsigned_type (128);
823 /* Re-define size_t as a D type. */
824 machine_mode type_mode
= TYPE_MODE (size_type_node
);
825 size_type_node
= lang_hooks
.types
.type_for_mode (type_mode
, 1);
828 /* Bool and Character types. */
829 d_bool_type
= make_unsigned_type (1);
830 TREE_SET_CODE (d_bool_type
, BOOLEAN_TYPE
);
832 char8_type_node
= make_unsigned_type (8);
833 TYPE_STRING_FLAG (char8_type_node
) = 1;
835 char16_type_node
= make_unsigned_type (16);
836 TYPE_STRING_FLAG (char16_type_node
) = 1;
838 char32_type_node
= make_unsigned_type (32);
839 TYPE_STRING_FLAG (char32_type_node
) = 1;
841 /* Imaginary types. */
842 ifloat_type_node
= build_distinct_type_copy (float_type_node
);
843 TYPE_IMAGINARY_FLOAT (ifloat_type_node
) = 1;
845 idouble_type_node
= build_distinct_type_copy (double_type_node
);
846 TYPE_IMAGINARY_FLOAT (idouble_type_node
) = 1;
848 ireal_type_node
= build_distinct_type_copy (long_double_type_node
);
849 TYPE_IMAGINARY_FLOAT (ireal_type_node
) = 1;
851 /* Calling build_ctype() links the front-end Type to the GCC node,
852 and sets the TYPE_NAME to the D language type. */
853 for (unsigned ty
= 0; ty
< TMAX
; ty
++)
855 if (Type::basic
[ty
] != NULL
)
856 build_ctype (Type::basic
[ty
]);
859 /* Used for ModuleInfo, ClassInfo, and Interface decls. */
860 unknown_type_node
= make_node (RECORD_TYPE
);
862 /* Make sure we get a unique function type, so we can give
863 its pointer type a name. (This wins for gdb). */
865 tree vfunc_type
= make_node (FUNCTION_TYPE
);
866 TREE_TYPE (vfunc_type
) = d_int_type
;
867 TYPE_ARG_TYPES (vfunc_type
) = NULL_TREE
;
868 layout_type (vfunc_type
);
870 vtable_entry_type
= build_pointer_type (vfunc_type
);
873 vtbl_ptr_type_node
= build_pointer_type (vtable_entry_type
);
874 layout_type (vtbl_ptr_type_node
);
876 /* When an object is accessed via an interface, this type appears
877 as the first entry in its vtable. */
879 tree domain
= build_index_type (size_int (3));
880 vtbl_interface_type_node
= build_array_type (ptr_type_node
, domain
);
883 /* Use `void[]' as a generic dynamic array type. */
884 array_type_node
= make_struct_type ("__builtin_void[]", 2,
885 get_identifier ("length"), size_type_node
,
886 get_identifier ("ptr"), ptr_type_node
);
887 TYPE_DYNAMIC_ARRAY (array_type_node
) = 1;
889 null_array_node
= d_array_value (array_type_node
, size_zero_node
,
893 /* Handle default attributes. */
895 enum built_in_attribute
897 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
898 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
899 #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
900 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
901 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
902 #include "builtin-attrs.def"
903 #undef DEF_ATTR_NULL_TREE
905 #undef DEF_ATTR_STRING
906 #undef DEF_ATTR_IDENT
907 #undef DEF_ATTR_TREE_LIST
911 static GTY(()) tree built_in_attributes
[(int) ATTR_LAST
];
913 /* Initialize the attribute table for all the supported builtins. */
916 d_init_attributes (void)
918 /* Fill in the built_in_attributes array. */
919 #define DEF_ATTR_NULL_TREE(ENUM) \
920 built_in_attributes[(int) ENUM] = NULL_TREE;
921 # define DEF_ATTR_INT(ENUM, VALUE) \
922 built_in_attributes[(int) ENUM] = build_int_cst (NULL_TREE, VALUE);
923 #define DEF_ATTR_STRING(ENUM, VALUE) \
924 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
925 #define DEF_ATTR_IDENT(ENUM, STRING) \
926 built_in_attributes[(int) ENUM] = get_identifier (STRING);
927 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
928 built_in_attributes[(int) ENUM] \
929 = tree_cons (built_in_attributes[(int) PURPOSE], \
930 built_in_attributes[(int) VALUE], \
931 built_in_attributes[(int) CHAIN]);
932 #include "builtin-attrs.def"
933 #undef DEF_ATTR_NULL_TREE
935 #undef DEF_ATTR_STRING
936 #undef DEF_ATTR_IDENT
937 #undef DEF_ATTR_TREE_LIST
944 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
945 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
946 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
947 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
948 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
949 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
950 #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
951 #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
953 #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
955 #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
956 ARG6, ARG7, ARG8) NAME,
957 #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
958 ARG6, ARG7, ARG8, ARG9) NAME,
959 #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
960 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
961 #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
962 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
963 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
964 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
965 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
966 #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
967 #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
968 #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
970 #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
972 #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
974 #define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
975 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
976 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
977 #include "builtin-types.def"
978 #undef DEF_PRIMITIVE_TYPE
979 #undef DEF_FUNCTION_TYPE_0
980 #undef DEF_FUNCTION_TYPE_1
981 #undef DEF_FUNCTION_TYPE_2
982 #undef DEF_FUNCTION_TYPE_3
983 #undef DEF_FUNCTION_TYPE_4
984 #undef DEF_FUNCTION_TYPE_5
985 #undef DEF_FUNCTION_TYPE_6
986 #undef DEF_FUNCTION_TYPE_7
987 #undef DEF_FUNCTION_TYPE_8
988 #undef DEF_FUNCTION_TYPE_9
989 #undef DEF_FUNCTION_TYPE_10
990 #undef DEF_FUNCTION_TYPE_11
991 #undef DEF_FUNCTION_TYPE_VAR_0
992 #undef DEF_FUNCTION_TYPE_VAR_1
993 #undef DEF_FUNCTION_TYPE_VAR_2
994 #undef DEF_FUNCTION_TYPE_VAR_3
995 #undef DEF_FUNCTION_TYPE_VAR_4
996 #undef DEF_FUNCTION_TYPE_VAR_5
997 #undef DEF_FUNCTION_TYPE_VAR_6
998 #undef DEF_FUNCTION_TYPE_VAR_7
999 #undef DEF_FUNCTION_TYPE_VAR_11
1000 #undef DEF_POINTER_TYPE
1004 typedef enum d_builtin_type builtin_type
;
1006 /* A temporary array used in communication with def_fn_type. */
1007 static GTY(()) tree builtin_types
[(int) BT_LAST
+ 1];
1009 /* A helper function for d_init_builtins. Build function type for DEF with
1010 return type RET and N arguments. If VAR is true, then the function should
1011 be variadic after those N arguments.
1013 Takes special care not to ICE if any of the types involved are
1014 error_mark_node, which indicates that said type is not in fact available
1015 (see builtin_type_for_size). In which case the function type as a whole
1016 should be error_mark_node. */
1019 def_fn_type (builtin_type def
, builtin_type ret
, bool var
, int n
, ...)
1022 tree
*args
= XALLOCAVEC (tree
, n
);
1027 for (i
= 0; i
< n
; ++i
)
1029 builtin_type a
= (builtin_type
) va_arg (list
, int);
1030 t
= builtin_types
[a
];
1031 if (t
== error_mark_node
)
1036 t
= builtin_types
[ret
];
1037 if (t
== error_mark_node
)
1040 t
= build_varargs_function_type_array (t
, n
, args
);
1042 t
= build_function_type_array (t
, n
, args
);
1045 builtin_types
[def
] = t
;
1049 /* Create builtin types and functions. VA_LIST_REF_TYPE_NODE and
1050 VA_LIST_ARG_TYPE_NODE are used in builtin-types.def. */
1053 d_define_builtins (tree va_list_ref_type_node ATTRIBUTE_UNUSED
,
1054 tree va_list_arg_type_node ATTRIBUTE_UNUSED
)
1056 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
1057 builtin_types[(int) ENUM] = VALUE;
1058 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
1059 def_fn_type (ENUM, RETURN, 0, 0);
1060 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
1061 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
1062 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
1063 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
1064 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
1065 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
1066 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
1067 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
1068 #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
1069 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
1070 #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1072 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
1073 #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1075 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
1076 #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1078 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1080 #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1081 ARG6, ARG7, ARG8, ARG9) \
1082 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1084 #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1085 ARG6, ARG7, ARG8, ARG9, ARG10) \
1086 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1087 ARG7, ARG8, ARG9, ARG10);
1088 #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1089 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
1090 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1091 ARG7, ARG8, ARG9, ARG10, ARG11);
1092 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
1093 def_fn_type (ENUM, RETURN, 1, 0);
1094 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
1095 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
1096 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
1097 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
1098 #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
1099 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
1100 #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
1101 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
1102 #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
1103 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
1104 #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1106 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
1107 #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1109 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
1110 #define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
1111 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
1112 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
1113 ARG7, ARG8, ARG9, ARG10, ARG11);
1114 #define DEF_POINTER_TYPE(ENUM, TYPE) \
1115 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
1117 #include "builtin-types.def"
1119 #undef DEF_PRIMITIVE_TYPE
1120 #undef DEF_FUNCTION_TYPE_1
1121 #undef DEF_FUNCTION_TYPE_2
1122 #undef DEF_FUNCTION_TYPE_3
1123 #undef DEF_FUNCTION_TYPE_4
1124 #undef DEF_FUNCTION_TYPE_5
1125 #undef DEF_FUNCTION_TYPE_6
1126 #undef DEF_FUNCTION_TYPE_7
1127 #undef DEF_FUNCTION_TYPE_8
1128 #undef DEF_FUNCTION_TYPE_9
1129 #undef DEF_FUNCTION_TYPE_10
1130 #undef DEF_FUNCTION_TYPE_11
1131 #undef DEF_FUNCTION_TYPE_VAR_0
1132 #undef DEF_FUNCTION_TYPE_VAR_1
1133 #undef DEF_FUNCTION_TYPE_VAR_2
1134 #undef DEF_FUNCTION_TYPE_VAR_3
1135 #undef DEF_FUNCTION_TYPE_VAR_4
1136 #undef DEF_FUNCTION_TYPE_VAR_5
1137 #undef DEF_FUNCTION_TYPE_VAR_6
1138 #undef DEF_FUNCTION_TYPE_VAR_7
1139 #undef DEF_FUNCTION_TYPE_VAR_11
1140 #undef DEF_POINTER_TYPE
1141 builtin_types
[(int) BT_LAST
] = NULL_TREE
;
1143 d_init_attributes ();
1145 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
1146 NONANSI_P, ATTRS, IMPLICIT, COND) \
1148 do_build_builtin_fn (ENUM, NAME, CLASS, \
1149 builtin_types[(int) TYPE], \
1150 BOTH_P, FALLBACK_P, \
1151 built_in_attributes[(int) ATTRS], IMPLICIT);
1152 #include "builtins.def"
1156 /* Build builtin functions and types for the D language frontend. */
1159 d_init_builtins (void)
1161 d_build_c_type_nodes ();
1162 d_build_d_type_nodes ();
1164 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
1166 /* It might seem natural to make the argument type a pointer, but there
1167 is no implicit casting from arrays to pointers in D. */
1168 d_define_builtins (va_list_type_node
, va_list_type_node
);
1172 d_define_builtins (build_reference_type (va_list_type_node
),
1176 targetm
.init_builtins ();
1177 build_common_builtin_nodes ();
1180 /* Registration of machine- or os-specific builtin types.
1181 Add to builtin types list for maybe processing later
1182 if `gcc.builtins' was imported into the current module. */
1185 d_register_builtin_type (tree type
, const char *name
)
1187 tree decl
= build_decl (UNKNOWN_LOCATION
, TYPE_DECL
,
1188 get_identifier (name
), type
);
1189 DECL_ARTIFICIAL (decl
) = 1;
1191 if (!TYPE_NAME (type
))
1192 TYPE_NAME (type
) = decl
;
1194 vec_safe_push (gcc_builtins_types
, decl
);
1197 /* Add DECL to builtin functions list for maybe processing later
1198 if `gcc.builtins' was imported into the current module. */
1201 d_builtin_function (tree decl
)
1203 if (!flag_no_builtin
&& DECL_ASSEMBLER_NAME_SET_P (decl
))
1204 vec_safe_push (gcc_builtins_libfuncs
, decl
);
1206 vec_safe_push (gcc_builtins_functions
, decl
);
1211 #include "gt-d-d-builtins.h"