/* runtime.def -- Definitions for D runtime functions.
   Copyright (C) 2014-2019 Free Software Foundation, Inc.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

/* D runtime library functions.  */

/* DEF_D_RUNTIME (CODE, NAME, FLAGS)
   CODE	    The enum code used to refer to this function.
   NAME	    The name of this function as a string.
   FLAGS    ECF flags to describe attributes of the function.

   Used for declaring functions that are called by generated code.  Most are
   extern(C) - for those that are not, ensure to use correct mangling.  */

/* Helper macros for parameter building.  */
#define P0()		    0
#define P1(T1)		    1, LCT_ ## T1
#define P2(T1, T2)	    2, LCT_ ## T1, LCT_ ## T2
#define P3(T1, T2, T3)	    3, LCT_ ## T1, LCT_ ## T2, LCT_ ## T3
#define P4(T1, T2, T3, T4)  4, LCT_ ## T1, LCT_ ## T2, LCT_ ## T3, LCT_ ## T4
#define RT(T1)		    LCT_ ## T1

/* Used when an assert() contract fails.  */
DEF_D_RUNTIME (ASSERT, "_d_assert", RT(VOID), P2(STRING, UINT), ECF_NORETURN)
DEF_D_RUNTIME (ASSERT_MSG, "_d_assert_msg", RT(VOID), P3(STRING, STRING, UINT),
	       ECF_NORETURN)

/* Used when an assert() contract fails in a unittest function.  */
DEF_D_RUNTIME (UNITTEST, "_d_unittest", RT(VOID), P2(STRING, UINT),
	       ECF_NORETURN)
DEF_D_RUNTIME (UNITTEST_MSG, "_d_unittest_msg", RT(VOID),
	       P3(STRING, STRING, UINT), ECF_NORETURN)

/* Used when an array index outside the bounds of its range.  */
DEF_D_RUNTIME (ARRAY_BOUNDS, "_d_arraybounds", RT(VOID), P2(STRING, UINT),
	       ECF_NORETURN)

/* Used when calling new on a class.  */
DEF_D_RUNTIME (NEWCLASS, "_d_newclass", RT(OBJECT), P1(CONST_CLASSINFO), 0)

/* Used when calling delete on a class or interface.  */
DEF_D_RUNTIME (DELCLASS, "_d_delclass", RT(VOID), P1(VOIDPTR), 0)
DEF_D_RUNTIME (DELINTERFACE, "_d_delinterface", RT(VOID), P1(VOIDPTR), 0)

/* Same as deleting a class, but used for stack-allocated classes.  */
DEF_D_RUNTIME (CALLFINALIZER, "_d_callfinalizer", RT(VOID), P1(VOIDPTR), 0)
DEF_D_RUNTIME (CALLINTERFACEFINALIZER, "_d_callinterfacefinalizer", RT(VOID),
	       P1(VOIDPTR), 0)

/* Used for casting to a class or interface.  */
DEF_D_RUNTIME (DYNAMIC_CAST, "_d_dynamic_cast", RT(OBJECT),
	       P2(OBJECT, CLASSINFO), 0)
DEF_D_RUNTIME (INTERFACE_CAST, "_d_interface_cast", RT(OBJECT),
	       P2(OBJECT, CLASSINFO), 0)

/* Used when calling new on a pointer.  The `i' variant is for when the
   initializer is nonzero.  */
DEF_D_RUNTIME (NEWITEMT, "_d_newitemT", RT(VOIDPTR), P1(CONST_TYPEINFO), 0)
DEF_D_RUNTIME (NEWITEMIT, "_d_newitemiT", RT(VOIDPTR), P1(CONST_TYPEINFO), 0)

/* Used when calling delete on a pointer.  */
DEF_D_RUNTIME (DELMEMORY, "_d_delmemory", RT(VOID), P1(POINTER_VOIDPTR), 0)
DEF_D_RUNTIME (DELSTRUCT, "_d_delstruct", RT(VOID),
	       P2(POINTER_VOIDPTR, TYPEINFO), 0)

/* Used when calling new on an array.  The `i' variant is for when the
   initializer is nonzero, and the `m' variant is when initializing a
   multi-dimensional array.  */
DEF_D_RUNTIME (NEWARRAYT, "_d_newarrayT", RT(ARRAY_VOID),
	       P2(CONST_TYPEINFO, SIZE_T), 0)
DEF_D_RUNTIME (NEWARRAYIT, "_d_newarrayiT", RT(ARRAY_VOID),
	       P2(CONST_TYPEINFO, SIZE_T), 0)
DEF_D_RUNTIME (NEWARRAYMTX, "_d_newarraymTX", RT(ARRAY_VOID),
	       P2(CONST_TYPEINFO, ARRAY_SIZE_T), 0)
DEF_D_RUNTIME (NEWARRAYMITX, "_d_newarraymiTX", RT(ARRAY_VOID),
	       P2(CONST_TYPEINFO, ARRAY_SIZE_T), 0)

/* Used for allocating an array literal on the GC heap.  */
DEF_D_RUNTIME (ARRAYLITERALTX, "_d_arrayliteralTX", RT(VOIDPTR),
	       P2(CONST_TYPEINFO, SIZE_T), 0)

/* Used when calling delete on an array.  */
DEF_D_RUNTIME (DELARRAYT, "_d_delarray_t", RT(VOID),
	       P2(ARRAYPTR_VOID, CONST_TYPEINFO), 0)

/* Used for value equality (x == y) and comparisons (x < y) of non-trivial
   arrays.  Such as an array of structs or classes.  */
DEF_D_RUNTIME (ADEQ2, "_adEq2", RT(INT),
	       P3(ARRAY_VOID, ARRAY_VOID, CONST_TYPEINFO), 0)
DEF_D_RUNTIME (ADCMP2, "_adCmp2", RT(INT),
	       P3(ARRAY_VOID, ARRAY_VOID, CONST_TYPEINFO), 0)

/* Used when casting from one array type to another where the index type
   sizes differ.  Such as from int[] to short[].  */
DEF_D_RUNTIME (ARRAYCAST, "_d_arraycast", RT(ARRAY_VOID),
	       P3(SIZE_T, SIZE_T, ARRAY_VOID), 0)

/* Used for (array.length = n) expressions.  The `i' variant is for when the
   initializer is nonzero.  */
DEF_D_RUNTIME (ARRAYSETLENGTHT, "_d_arraysetlengthT", RT(ARRAY_VOID),
	       P3(CONST_TYPEINFO, SIZE_T, ARRAYPTR_VOID), 0)
DEF_D_RUNTIME (ARRAYSETLENGTHIT, "_d_arraysetlengthiT", RT(ARRAY_VOID),
	       P3(CONST_TYPEINFO, SIZE_T, ARRAYPTR_VOID), 0)

/* Used for allocating closures on the GC heap.  */
DEF_D_RUNTIME (ALLOCMEMORY, "_d_allocmemory", RT(VOIDPTR), P1(SIZE_T),
	       ECF_MALLOC)

/* Used for copying an array into a slice, adds an enforcment that the source
   and destination are equal in size and do not overlap.  */
DEF_D_RUNTIME (ARRAYCOPY, "_d_arraycopy", RT(ARRAY_VOID),
	       P3(SIZE_T, ARRAY_VOID, ARRAY_VOID), 0)

/* Used for array assignments from an existing array.  The `set' variant is for
   when the assignment value is a single element.  */
DEF_D_RUNTIME (ARRAYASSIGN, "_d_arrayassign", RT(ARRAY_VOID),
	       P3(CONST_TYPEINFO, ARRAY_VOID, ARRAY_VOID), 0)
DEF_D_RUNTIME (ARRAYASSIGN_L, "_d_arrayassign_l", RT(ARRAY_VOID),
	       P4(CONST_TYPEINFO, ARRAY_VOID, ARRAY_VOID, VOIDPTR), 0)
DEF_D_RUNTIME (ARRAYASSIGN_R, "_d_arrayassign_r", RT(ARRAY_VOID),
	       P4(CONST_TYPEINFO, ARRAY_VOID, ARRAY_VOID, VOIDPTR), 0)
DEF_D_RUNTIME (ARRAYSETASSIGN, "_d_arraysetassign", RT(VOIDPTR),
	       P4(VOIDPTR, VOIDPTR, SIZE_T, CONST_TYPEINFO), 0)

/* Used for constructing a new array from an existing array.  The `set' variant
   is for when the constructor value is a single element.  */
DEF_D_RUNTIME (ARRAYCTOR, "_d_arrayctor", RT(ARRAY_VOID),
	       P3(CONST_TYPEINFO, ARRAY_VOID, ARRAY_VOID), 0)
DEF_D_RUNTIME (ARRAYSETCTOR, "_d_arraysetctor", RT(VOIDPTR),
	       P4(VOIDPTR, VOIDPTR, SIZE_T, CONST_TYPEINFO), 0)

/* Used for concatenating two or more arrays together.  Then `n' variant is
   for when there is more than two arrays to handle.  */
DEF_D_RUNTIME (ARRAYCATT, "_d_arraycatT", RT(ARRAY_BYTE),
	       P3(CONST_TYPEINFO, ARRAY_BYTE, ARRAY_BYTE), 0)
DEF_D_RUNTIME (ARRAYCATNTX, "_d_arraycatnTX", RT(ARRAY_VOID),
	       P2(CONST_TYPEINFO, ARRAYARRAY_BYTE), 0)

/* Used for appending a single element to an array.  */
DEF_D_RUNTIME (ARRAYAPPENDCTX, "_d_arrayappendcTX", RT(ARRAY_BYTE),
	       P3(CONST_TYPEINFO, ARRAYPTR_BYTE, SIZE_T), 0)

/* Same as appending a single element to an array, but specific for when the
   source is a UTF-32 character, and the destination is a UTF-8 or 16 array.  */
DEF_D_RUNTIME (ARRAYAPPENDCD, "_d_arrayappendcd", RT(ARRAY_VOID),
	       P2(ARRAYPTR_BYTE, DCHAR), 0)
DEF_D_RUNTIME (ARRAYAPPENDWD, "_d_arrayappendwd", RT(ARRAY_VOID),
	       P2(ARRAYPTR_BYTE, DCHAR), 0)

/* Used for appending an existing array to another.  */
DEF_D_RUNTIME (ARRAYAPPENDT, "_d_arrayappendT", RT(ARRAY_VOID),
	       P3(TYPEINFO, ARRAYPTR_BYTE, ARRAY_BYTE), 0)

/* Used for allocating a new associative array.  */
DEF_D_RUNTIME (ASSOCARRAYLITERALTX, "_d_assocarrayliteralTX", RT(VOIDPTR),
	       P3(CONST_TYPEINFO, ARRAY_VOID, ARRAY_VOID), 0)

/* Used for value equality of two associative arrays.  */
DEF_D_RUNTIME (AAEQUAL, "_aaEqual", RT(INT),
	       P3(CONST_TYPEINFO, ASSOCARRAY, ASSOCARRAY), 0)

/* Used to determine is a key exists in an associative array.  */
DEF_D_RUNTIME (AAINX, "_aaInX", RT(VOIDPTR),
	       P3(ASSOCARRAY, CONST_TYPEINFO, VOIDPTR), 0)

/* Used to retrieve a value from an associative array index by a key.  The
   `Rvalue' variant returns null if the key is not found, where as aaGetY
   will create new key entry for assignment.  */
DEF_D_RUNTIME (AAGETY, "_aaGetY", RT(VOIDPTR),
	       P4(POINTER_ASSOCARRAY, CONST_TYPEINFO, SIZE_T, VOIDPTR), 0)
DEF_D_RUNTIME (AAGETRVALUEX, "_aaGetRvalueX", RT(VOIDPTR),
	       P4(ASSOCARRAY, CONST_TYPEINFO, SIZE_T, VOIDPTR), 0)

/* Used when calling delete on a key entry in an associative array.  */
DEF_D_RUNTIME (AADELX, "_aaDelX", RT(BOOL),
	       P3(ASSOCARRAY, CONST_TYPEINFO, VOIDPTR), 0)

/* Used for throw() expressions.  */
DEF_D_RUNTIME (THROW, "_d_throw", RT(VOID), P1(OBJECT), ECF_NORETURN)
DEF_D_RUNTIME (BEGIN_CATCH, "__gdc_begin_catch", RT(VOIDPTR), P1(VOIDPTR), 0)

/* C++ exception handlers.  */
DEF_D_RUNTIME (CXA_BEGIN_CATCH, "__cxa_begin_catch", RT(VOIDPTR), P1(VOIDPTR),
	       ECF_NOTHROW)
DEF_D_RUNTIME (CXA_END_CATCH, "__cxa_end_catch", RT(VOID), P0(), 0)

/* When invariant() contracts are turned on, used after testing whether a
   class != null for validating the state of a class.  */
DEF_D_RUNTIME (INVARIANT, "_D9invariant12_d_invariantFC6ObjectZv", RT(VOID),
	       P1(OBJECT), 0)

/* Used when performing a switch/cases on a string.  The `u' and `d' variants
   are for UTF-16 and UTF-32 strings respectively.  */
DEF_D_RUNTIME (SWITCH_STRING, "_d_switch_string", RT(INT),
	       P2(ARRAY_STRING, STRING), 0)
DEF_D_RUNTIME (SWITCH_USTRING, "_d_switch_ustring", RT(INT),
	       P2(ARRAY_WSTRING, WSTRING), 0)
DEF_D_RUNTIME (SWITCH_DSTRING, "_d_switch_dstring", RT(INT),
	       P2(ARRAY_DSTRING, DSTRING), 0)

/* Used when throwing an error that a switch statement has no default case,
   and yet none of the existing cases matched.  */
DEF_D_RUNTIME (SWITCH_ERROR, "_d_switch_error", RT(VOID), P2(STRING, UINT),
	       ECF_NORETURN)

#undef P0
#undef P1
#undef P2
#undef P3
#undef P4
#undef RT
