summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/constructing-calls.html
blob: 9cf68bec374c9746e5e8c1efc8100b38a4eb3e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<div class="section-level-extent" id="Constructing-Calls"> <div class="nav-panel"> <p> Next: <a href="typeof" accesskey="n" rel="next">Referring to a Type with <code class="code">typeof</code></a>, Previous: <a href="nonlocal-gotos" accesskey="p" rel="prev">Nonlocal Gotos</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div>  <h1 class="section" id="Constructing-Function-Calls"><span>6.6 Constructing Function Calls<a class="copiable-link" href="#Constructing-Function-Calls"> ¶</a></span></h1>   <p>Using the built-in functions described below, you can record the arguments a function received, and call another function with the same arguments, without knowing the number or types of the arguments. </p> <p>You can also record the return value of that function call, and later return that value, without knowing what data type the function tried to return (as long as your caller expects that data type). </p> <p>However, these built-in functions may interact badly with some sophisticated features or other extensions of the language. It is, therefore, not recommended to use them outside very simple functions acting as mere forwarders for their arguments. </p> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fapply_005fargs">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void *</code> <strong class="def-name">__builtin_apply_args</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fapply_005fargs"> ¶</a></span>
</dt> <dd>
<p>This built-in function returns a pointer to data describing how to perform a call with the same arguments as are passed to the current function. </p> <p>The function saves the arg pointer register, structure value address, and all registers that might be used to pass arguments to a function into a block of memory allocated on the stack. Then it returns the address of that block. </p>
</dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fapply">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void *</code> <strong class="def-name">__builtin_apply</strong> <code class="def-code-arguments">(void (*<var class="var">function</var>)(), void *<var class="var">arguments</var>, size_t <var class="var">size</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fapply"> ¶</a></span>
</dt> <dd>
<p>This built-in function invokes <var class="var">function</var> with a copy of the parameters described by <var class="var">arguments</var> and <var class="var">size</var>. </p> <p>The value of <var class="var">arguments</var> should be the value returned by <code class="code">__builtin_apply_args</code>. The argument <var class="var">size</var> specifies the size of the stack argument data, in bytes. </p> <p>This function returns a pointer to data describing how to return whatever value is returned by <var class="var">function</var>. The data is saved in a block of memory allocated on the stack. </p> <p>It is not always simple to compute the proper value for <var class="var">size</var>. The value is used by <code class="code">__builtin_apply</code> to compute the amount of data that should be pushed on the stack and copied from the incoming argument area. </p>
</dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005freturn">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void</code> <strong class="def-name">__builtin_return</strong> <code class="def-code-arguments">(void *<var class="var">result</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005freturn"> ¶</a></span>
</dt> <dd><p>This built-in function returns the value described by <var class="var">result</var> from the containing function. You should specify, for <var class="var">result</var>, a value returned by <code class="code">__builtin_apply</code>. </p></dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fva_005farg_005fpack">
<span class="category-def">Built-in Function: </span><span><strong class="def-name">__builtin_va_arg_pack</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fva_005farg_005fpack"> ¶</a></span>
</dt> <dd>
<p>This built-in function represents all anonymous arguments of an inline function. It can be used only in inline functions that are always inlined, never compiled as a separate function, such as those using <code class="code">__attribute__ ((__always_inline__))</code> or <code class="code">__attribute__ ((__gnu_inline__))</code> extern inline functions. It must be only passed as last argument to some other function with variable arguments. This is useful for writing small wrapper inlines for variable argument functions, when using preprocessor macros is undesirable. For example: </p>
<div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern int myprintf (FILE *f, const char *format, ...);
extern inline __attribute__ ((__gnu_inline__)) int
myprintf (FILE *f, const char *format, ...)
{
  int r = fprintf (f, "myprintf: ");
  if (r &lt; 0)
    return r;
  int s = fprintf (f, format, __builtin_va_arg_pack ());
  if (s &lt; 0)
    return s;
  return r + s;
}</pre>
</div> </dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fva_005farg_005fpack_005flen">
<span class="category-def">Built-in Function: </span><span><code class="def-type">int</code> <strong class="def-name">__builtin_va_arg_pack_len</strong> <code class="def-code-arguments">()</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fva_005farg_005fpack_005flen"> ¶</a></span>
</dt> <dd>
<p>This built-in function returns the number of anonymous arguments of an inline function. It can be used only in inline functions that are always inlined, never compiled as a separate function, such as those using <code class="code">__attribute__ ((__always_inline__))</code> or <code class="code">__attribute__ ((__gnu_inline__))</code> extern inline functions. For example following does link- or run-time checking of open arguments for optimized code: </p>
<div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#ifdef __OPTIMIZE__
extern inline __attribute__((__gnu_inline__)) int
myopen (const char *path, int oflag, ...)
{
  if (__builtin_va_arg_pack_len () &gt; 1)
    warn_open_too_many_arguments ();

  if (__builtin_constant_p (oflag))
    {
      if ((oflag &amp; O_CREAT) != 0 &amp;&amp; __builtin_va_arg_pack_len () &lt; 1)
        {
          warn_open_missing_mode ();
          return __open_2 (path, oflag);
        }
      return open (path, oflag, __builtin_va_arg_pack ());
    }

  if (__builtin_va_arg_pack_len () &lt; 1)
    return __open_2 (path, oflag);

  return open (path, oflag, __builtin_va_arg_pack ());
}
#endif</pre>
</div> </dd>
</dl> </div>  <div class="nav-panel"> <p> Next: <a href="typeof">Referring to a Type with <code class="code">typeof</code></a>, Previous: <a href="nonlocal-gotos">Nonlocal Gotos</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
  <p class="_attribution-p">
    &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
    <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Constructing-Calls.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Constructing-Calls.html</a>
  </p>
</div>