Constructing-Calls.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Constructing Calls</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Constructing Calls">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Constructing Calls">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Typeof.html#Typeof" rel="next" title="Typeof">
  31. <link href="Nested-Functions.html#Nested-Functions" rel="prev" title="Nested Functions">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Constructing-Calls"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Typeof.html#Typeof" accesskey="n" rel="next">Typeof</a>, Previous: <a href="Nested-Functions.html#Nested-Functions" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Constructing-Function-Calls"></a>
  69. <h3 class="section">6.5 Constructing Function Calls</h3>
  70. <a name="index-constructing-calls"></a>
  71. <a name="index-forwarding-calls"></a>
  72. <p>Using the built-in functions described below, you can record
  73. the arguments a function received, and call another function
  74. with the same arguments, without knowing the number or types
  75. of the arguments.
  76. </p>
  77. <p>You can also record the return value of that function call,
  78. and later return that value, without knowing what data type
  79. the function tried to return (as long as your caller expects
  80. that data type).
  81. </p>
  82. <p>However, these built-in functions may interact badly with some
  83. sophisticated features or other extensions of the language. It
  84. is, therefore, not recommended to use them outside very simple
  85. functions acting as mere forwarders for their arguments.
  86. </p>
  87. <dl>
  88. <dt><a name="index-_005f_005fbuiltin_005fapply_005fargs"></a>Built-in Function: <em>void *</em> <strong>__builtin_apply_args</strong> <em>()</em></dt>
  89. <dd><p>This built-in function returns a pointer to data
  90. describing how to perform a call with the same arguments as are passed
  91. to the current function.
  92. </p>
  93. <p>The function saves the arg pointer register, structure value address,
  94. and all registers that might be used to pass arguments to a function
  95. into a block of memory allocated on the stack. Then it returns the
  96. address of that block.
  97. </p></dd></dl>
  98. <dl>
  99. <dt><a name="index-_005f_005fbuiltin_005fapply"></a>Built-in Function: <em>void *</em> <strong>__builtin_apply</strong> <em>(void (*<var>function</var>)(), void *<var>arguments</var>, size_t <var>size</var>)</em></dt>
  100. <dd><p>This built-in function invokes <var>function</var>
  101. with a copy of the parameters described by <var>arguments</var>
  102. and <var>size</var>.
  103. </p>
  104. <p>The value of <var>arguments</var> should be the value returned by
  105. <code>__builtin_apply_args</code>. The argument <var>size</var> specifies the size
  106. of the stack argument data, in bytes.
  107. </p>
  108. <p>This function returns a pointer to data describing
  109. how to return whatever value is returned by <var>function</var>. The data
  110. is saved in a block of memory allocated on the stack.
  111. </p>
  112. <p>It is not always simple to compute the proper value for <var>size</var>. The
  113. value is used by <code>__builtin_apply</code> to compute the amount of data
  114. that should be pushed on the stack and copied from the incoming argument
  115. area.
  116. </p></dd></dl>
  117. <dl>
  118. <dt><a name="index-_005f_005fbuiltin_005freturn"></a>Built-in Function: <em>void</em> <strong>__builtin_return</strong> <em>(void *<var>result</var>)</em></dt>
  119. <dd><p>This built-in function returns the value described by <var>result</var> from
  120. the containing function. You should specify, for <var>result</var>, a value
  121. returned by <code>__builtin_apply</code>.
  122. </p></dd></dl>
  123. <dl>
  124. <dt><a name="index-_005f_005fbuiltin_005fva_005farg_005fpack"></a>Built-in Function: <em></em> <strong>__builtin_va_arg_pack</strong> <em>()</em></dt>
  125. <dd><p>This built-in function represents all anonymous arguments of an inline
  126. function. It can be used only in inline functions that are always
  127. inlined, never compiled as a separate function, such as those using
  128. <code>__attribute__ ((__always_inline__))</code> or
  129. <code>__attribute__ ((__gnu_inline__))</code> extern inline functions.
  130. It must be only passed as last argument to some other function
  131. with variable arguments. This is useful for writing small wrapper
  132. inlines for variable argument functions, when using preprocessor
  133. macros is undesirable. For example:
  134. </p><div class="smallexample">
  135. <pre class="smallexample">extern int myprintf (FILE *f, const char *format, ...);
  136. extern inline __attribute__ ((__gnu_inline__)) int
  137. myprintf (FILE *f, const char *format, ...)
  138. {
  139. int r = fprintf (f, &quot;myprintf: &quot;);
  140. if (r &lt; 0)
  141. return r;
  142. int s = fprintf (f, format, __builtin_va_arg_pack ());
  143. if (s &lt; 0)
  144. return s;
  145. return r + s;
  146. }
  147. </pre></div>
  148. </dd></dl>
  149. <dl>
  150. <dt><a name="index-_005f_005fbuiltin_005fva_005farg_005fpack_005flen"></a>Built-in Function: <em>size_t</em> <strong>__builtin_va_arg_pack_len</strong> <em>()</em></dt>
  151. <dd><p>This built-in function returns the number of anonymous arguments of
  152. an inline function. It can be used only in inline functions that
  153. are always inlined, never compiled as a separate function, such
  154. as those using <code>__attribute__ ((__always_inline__))</code> or
  155. <code>__attribute__ ((__gnu_inline__))</code> extern inline functions.
  156. For example following does link- or run-time checking of open
  157. arguments for optimized code:
  158. </p><div class="smallexample">
  159. <pre class="smallexample">#ifdef __OPTIMIZE__
  160. extern inline __attribute__((__gnu_inline__)) int
  161. myopen (const char *path, int oflag, ...)
  162. {
  163. if (__builtin_va_arg_pack_len () &gt; 1)
  164. warn_open_too_many_arguments ();
  165. if (__builtin_constant_p (oflag))
  166. {
  167. if ((oflag &amp; O_CREAT) != 0 &amp;&amp; __builtin_va_arg_pack_len () &lt; 1)
  168. {
  169. warn_open_missing_mode ();
  170. return __open_2 (path, oflag);
  171. }
  172. return open (path, oflag, __builtin_va_arg_pack ());
  173. }
  174. if (__builtin_va_arg_pack_len () &lt; 1)
  175. return __open_2 (path, oflag);
  176. return open (path, oflag, __builtin_va_arg_pack ());
  177. }
  178. #endif
  179. </pre></div>
  180. </dd></dl>
  181. <hr>
  182. <div class="header">
  183. <p>
  184. Next: <a href="Typeof.html#Typeof" accesskey="n" rel="next">Typeof</a>, Previous: <a href="Nested-Functions.html#Nested-Functions" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  185. </div>
  186. </body>
  187. </html>