1.. Copyright (C) 2017-2018 Free Software Foundation, Inc.
2   Originally contributed by David Malcolm <dmalcolm@redhat.com>
3
4   This is free software: you can redistribute it and/or modify it
5   under the terms of the GNU General Public License as published by
6   the Free Software Foundation, either version 3 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see
16   <http://www.gnu.org/licenses/>.
17
18.. default-domain:: c
19
20Function pointers
21=================
22
23You can generate calls that use a function pointer via
24:c:func:`gcc_jit_context_new_call_through_ptr`.
25
26To do requires a :c:type:`gcc_jit_rvalue` of the correct function pointer type.
27
28Function pointers for a :c:type:`gcc_jit_function` can be obtained
29via :c:func:`gcc_jit_function_get_address`.
30
31.. function:: gcc_jit_rvalue *\
32	      gcc_jit_function_get_address (gcc_jit_function *fn,\
33                                            gcc_jit_location *loc)
34
35   Get the address of a function as an rvalue, of function pointer
36   type.
37
38   This entrypoint was added in :ref:`LIBGCCJIT_ABI_9`; you can test
39   for its presence using
40
41   .. code-block:: c
42
43      #ifdef LIBGCCJIT_HAVE_gcc_jit_function_get_address
44
45Alternatively, given an existing function, you can obtain a pointer
46to it in :c:type:`gcc_jit_rvalue` form using
47:c:func:`gcc_jit_context_new_rvalue_from_ptr`, using a function pointer
48type obtained using :c:func:`gcc_jit_context_new_function_ptr_type`.
49
50Here's an example of creating a function pointer type corresponding to C's
51:c:type:`void (*) (int, int, int)`:
52
53.. code-block:: c
54
55  gcc_jit_type *void_type =
56    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
57  gcc_jit_type *int_type =
58    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
59
60  /* Build the function ptr type.  */
61  gcc_jit_type *param_types[3];
62  param_types[0] = int_type;
63  param_types[1] = int_type;
64  param_types[2] = int_type;
65
66  gcc_jit_type *fn_ptr_type =
67    gcc_jit_context_new_function_ptr_type (ctxt, NULL,
68					   void_type,
69					   3, param_types, 0);
70
71.. function:: gcc_jit_type *\
72	      gcc_jit_context_new_function_ptr_type (gcc_jit_context *ctxt,\
73				       gcc_jit_location *loc,\
74				       gcc_jit_type *return_type,\
75				       int num_params,\
76				       gcc_jit_type **param_types,\
77				       int is_variadic)
78
79   Generate a :c:type:`gcc_jit_type` for a function pointer with the
80   given return type and parameters.
81