]>
Commit | Line | Data |
---|---|---|
c63539ff ML |
1 | .. |
2 | Copyright 1988-2022 Free Software Foundation, Inc. | |
3 | This is part of the GCC manual. | |
4 | For copying conditions, see the copyright.rst file. | |
5 | ||
6 | .. index:: constructing calls, forwarding calls | |
7 | ||
8 | .. _constructing-calls: | |
9 | ||
10 | Constructing Function Calls | |
11 | *************************** | |
12 | ||
13 | Using the built-in functions described below, you can record | |
14 | the arguments a function received, and call another function | |
15 | with the same arguments, without knowing the number or types | |
16 | of the arguments. | |
17 | ||
18 | You can also record the return value of that function call, | |
19 | and later return that value, without knowing what data type | |
20 | the function tried to return (as long as your caller expects | |
21 | that data type). | |
22 | ||
23 | However, these built-in functions may interact badly with some | |
24 | sophisticated features or other extensions of the language. It | |
25 | is, therefore, not recommended to use them outside very simple | |
26 | functions acting as mere forwarders for their arguments. | |
27 | ||
28 | .. function:: void * __builtin_apply_args () | |
29 | ||
30 | This built-in function returns a pointer to data | |
31 | describing how to perform a call with the same arguments as are passed | |
32 | to the current function. | |
33 | ||
34 | The function saves the arg pointer register, structure value address, | |
35 | and all registers that might be used to pass arguments to a function | |
36 | into a block of memory allocated on the stack. Then it returns the | |
37 | address of that block. | |
38 | ||
39 | .. function:: void * __builtin_apply (void (*function)(), void *arguments, size_t size) | |
40 | ||
41 | This built-in function invokes :samp:`{function}` | |
42 | with a copy of the parameters described by :samp:`{arguments}` | |
43 | and :samp:`{size}`. | |
44 | ||
45 | The value of :samp:`{arguments}` should be the value returned by | |
46 | ``__builtin_apply_args``. The argument :samp:`{size}` specifies the size | |
47 | of the stack argument data, in bytes. | |
48 | ||
49 | This function returns a pointer to data describing | |
50 | how to return whatever value is returned by :samp:`{function}`. The data | |
51 | is saved in a block of memory allocated on the stack. | |
52 | ||
53 | It is not always simple to compute the proper value for :samp:`{size}`. The | |
54 | value is used by ``__builtin_apply`` to compute the amount of data | |
55 | that should be pushed on the stack and copied from the incoming argument | |
56 | area. | |
57 | ||
58 | .. function:: void __builtin_return (void *result) | |
59 | ||
60 | This built-in function returns the value described by :samp:`{result}` from | |
61 | the containing function. You should specify, for :samp:`{result}`, a value | |
62 | returned by ``__builtin_apply``. | |
63 | ||
64 | .. function:: __builtin_va_arg_pack () | |
65 | ||
66 | This built-in function represents all anonymous arguments of an inline | |
67 | function. It can be used only in inline functions that are always | |
68 | inlined, never compiled as a separate function, such as those using | |
69 | ``__attribute__ ((__always_inline__))`` or | |
70 | ``__attribute__ ((__gnu_inline__))`` extern inline functions. | |
71 | It must be only passed as last argument to some other function | |
72 | with variable arguments. This is useful for writing small wrapper | |
73 | inlines for variable argument functions, when using preprocessor | |
74 | macros is undesirable. For example: | |
75 | ||
76 | .. code-block:: c++ | |
77 | ||
78 | extern int myprintf (FILE *f, const char *format, ...); | |
79 | extern inline __attribute__ ((__gnu_inline__)) int | |
80 | myprintf (FILE *f, const char *format, ...) | |
81 | { | |
82 | int r = fprintf (f, "myprintf: "); | |
83 | if (r < 0) | |
84 | return r; | |
85 | int s = fprintf (f, format, __builtin_va_arg_pack ()); | |
86 | if (s < 0) | |
87 | return s; | |
88 | return r + s; | |
89 | } | |
90 | ||
91 | .. function:: size_t __builtin_va_arg_pack_len () | |
92 | ||
93 | This built-in function returns the number of anonymous arguments of | |
94 | an inline function. It can be used only in inline functions that | |
95 | are always inlined, never compiled as a separate function, such | |
96 | as those using ``__attribute__ ((__always_inline__))`` or | |
97 | ``__attribute__ ((__gnu_inline__))`` extern inline functions. | |
98 | For example following does link- or run-time checking of open | |
99 | arguments for optimized code: | |
100 | ||
101 | .. code-block:: c++ | |
102 | ||
103 | #ifdef __OPTIMIZE__ | |
104 | extern inline __attribute__((__gnu_inline__)) int | |
105 | myopen (const char *path, int oflag, ...) | |
106 | { | |
107 | if (__builtin_va_arg_pack_len () > 1) | |
108 | warn_open_too_many_arguments (); | |
109 | ||
110 | if (__builtin_constant_p (oflag)) | |
111 | { | |
112 | if ((oflag & O_CREAT) != 0 && __builtin_va_arg_pack_len () < 1) | |
113 | { | |
114 | warn_open_missing_mode (); | |
115 | return __open_2 (path, oflag); | |
116 | } | |
117 | return open (path, oflag, __builtin_va_arg_pack ()); | |
118 | } | |
119 | ||
120 | if (__builtin_va_arg_pack_len () < 1) | |
121 | return __open_2 (path, oflag); | |
122 | ||
123 | return open (path, oflag, __builtin_va_arg_pack ()); | |
124 | } | |
3ed1b4ce | 125 | #endif |