1<html lang="en"> 2<head> 3<title>Constructing Calls - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Nested-Functions.html#Nested-Functions" title="Nested Functions"> 10<link rel="next" href="Typeof.html#Typeof" title="Typeof"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Constructing-Calls"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Typeof.html#Typeof">Typeof</a>, 53Previous: <a rel="previous" accesskey="p" href="Nested-Functions.html#Nested-Functions">Nested Functions</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.5 Constructing Function Calls</h3> 59 60<p><a name="index-constructing-calls-2253"></a><a name="index-forwarding-calls-2254"></a> 61Using the built-in functions described below, you can record 62the arguments a function received, and call another function 63with the same arguments, without knowing the number or types 64of the arguments. 65 66 <p>You can also record the return value of that function call, 67and later return that value, without knowing what data type 68the function tried to return (as long as your caller expects 69that data type). 70 71 <p>However, these built-in functions may interact badly with some 72sophisticated features or other extensions of the language. It 73is, therefore, not recommended to use them outside very simple 74functions acting as mere forwarders for their arguments. 75 76<div class="defun"> 77— Built-in Function: void * <b>__builtin_apply_args</b> ()<var><a name="index-g_t_005f_005fbuiltin_005fapply_005fargs-2255"></a></var><br> 78<blockquote><p>This built-in function returns a pointer to data 79describing how to perform a call with the same arguments as were passed 80to the current function. 81 82 <p>The function saves the arg pointer register, structure value address, 83and all registers that might be used to pass arguments to a function 84into a block of memory allocated on the stack. Then it returns the 85address of that block. 86</p></blockquote></div> 87 88<div class="defun"> 89— Built-in Function: void * <b>__builtin_apply</b> (<var>void </var>(<var>*function</var>)()<var>, void *arguments, size_t size</var>)<var><a name="index-g_t_005f_005fbuiltin_005fapply-2256"></a></var><br> 90<blockquote><p>This built-in function invokes <var>function</var> 91with a copy of the parameters described by <var>arguments</var> 92and <var>size</var>. 93 94 <p>The value of <var>arguments</var> should be the value returned by 95<code>__builtin_apply_args</code>. The argument <var>size</var> specifies the size 96of the stack argument data, in bytes. 97 98 <p>This function returns a pointer to data describing 99how to return whatever value was returned by <var>function</var>. The data 100is saved in a block of memory allocated on the stack. 101 102 <p>It is not always simple to compute the proper value for <var>size</var>. The 103value is used by <code>__builtin_apply</code> to compute the amount of data 104that should be pushed on the stack and copied from the incoming argument 105area. 106</p></blockquote></div> 107 108<div class="defun"> 109— Built-in Function: void <b>__builtin_return</b> (<var>void *result</var>)<var><a name="index-g_t_005f_005fbuiltin_005freturn-2257"></a></var><br> 110<blockquote><p>This built-in function returns the value described by <var>result</var> from 111the containing function. You should specify, for <var>result</var>, a value 112returned by <code>__builtin_apply</code>. 113</p></blockquote></div> 114 115<div class="defun"> 116— Built-in Function: <b>__builtin_va_arg_pack</b> ()<var><a name="index-g_t_005f_005fbuiltin_005fva_005farg_005fpack-2258"></a></var><br> 117<blockquote><p>This built-in function represents all anonymous arguments of an inline 118function. It can be used only in inline functions which will be always 119inlined, never compiled as a separate function, such as those using 120<code>__attribute__ ((__always_inline__))</code> or 121<code>__attribute__ ((__gnu_inline__))</code> extern inline functions. 122It must be only passed as last argument to some other function 123with variable arguments. This is useful for writing small wrapper 124inlines for variable argument functions, when using preprocessor 125macros is undesirable. For example: 126 <pre class="smallexample"> extern int myprintf (FILE *f, const char *format, ...); 127 extern inline __attribute__ ((__gnu_inline__)) int 128 myprintf (FILE *f, const char *format, ...) 129 { 130 int r = fprintf (f, "myprintf: "); 131 if (r < 0) 132 return r; 133 int s = fprintf (f, format, __builtin_va_arg_pack ()); 134 if (s < 0) 135 return s; 136 return r + s; 137 } 138</pre> 139 </blockquote></div> 140 141<div class="defun"> 142— Built-in Function: size_t <b>__builtin_va_arg_pack_len</b> ()<var><a name="index-g_t_005f_005fbuiltin_005fva_005farg_005fpack_005flen-2259"></a></var><br> 143<blockquote><p>This built-in function returns the number of anonymous arguments of 144an inline function. It can be used only in inline functions which 145will be always inlined, never compiled as a separate function, such 146as those using <code>__attribute__ ((__always_inline__))</code> or 147<code>__attribute__ ((__gnu_inline__))</code> extern inline functions. 148For example following will do link or runtime checking of open 149arguments for optimized code: 150 <pre class="smallexample"> #ifdef __OPTIMIZE__ 151 extern inline __attribute__((__gnu_inline__)) int 152 myopen (const char *path, int oflag, ...) 153 { 154 if (__builtin_va_arg_pack_len () > 1) 155 warn_open_too_many_arguments (); 156 157 if (__builtin_constant_p (oflag)) 158 { 159 if ((oflag & O_CREAT) != 0 && __builtin_va_arg_pack_len () < 1) 160 { 161 warn_open_missing_mode (); 162 return __open_2 (path, oflag); 163 } 164 return open (path, oflag, __builtin_va_arg_pack ()); 165 } 166 167 if (__builtin_va_arg_pack_len () < 1) 168 return __open_2 (path, oflag); 169 170 return open (path, oflag, __builtin_va_arg_pack ()); 171 } 172 #endif 173</pre> 174 </blockquote></div> 175 176 </body></html> 177 178