Local-Labels.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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): Local Labels</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Local Labels">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Local Labels">
  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="Labels-as-Values.html#Labels-as-Values" rel="next" title="Labels as Values">
  31. <link href="Statement-Exprs.html#Statement-Exprs" rel="prev" title="Statement Exprs">
  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="Local-Labels"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Labels-as-Values.html#Labels-as-Values" accesskey="n" rel="next">Labels as Values</a>, Previous: <a href="Statement-Exprs.html#Statement-Exprs" accesskey="p" rel="prev">Statement Exprs</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="Locally-Declared-Labels"></a>
  69. <h3 class="section">6.2 Locally Declared Labels</h3>
  70. <a name="index-local-labels"></a>
  71. <a name="index-macros_002c-local-labels"></a>
  72. <p>GCC allows you to declare <em>local labels</em> in any nested block
  73. scope. A local label is just like an ordinary label, but you can
  74. only reference it (with a <code>goto</code> statement, or by taking its
  75. address) within the block in which it is declared.
  76. </p>
  77. <p>A local label declaration looks like this:
  78. </p>
  79. <div class="smallexample">
  80. <pre class="smallexample">__label__ <var>label</var>;
  81. </pre></div>
  82. <p>or
  83. </p>
  84. <div class="smallexample">
  85. <pre class="smallexample">__label__ <var>label1</var>, <var>label2</var>, /* <span class="roman">&hellip;</span> */;
  86. </pre></div>
  87. <p>Local label declarations must come at the beginning of the block,
  88. before any ordinary declarations or statements.
  89. </p>
  90. <p>The label declaration defines the label <em>name</em>, but does not define
  91. the label itself. You must do this in the usual way, with
  92. <code><var>label</var>:</code>, within the statements of the statement expression.
  93. </p>
  94. <p>The local label feature is useful for complex macros. If a macro
  95. contains nested loops, a <code>goto</code> can be useful for breaking out of
  96. them. However, an ordinary label whose scope is the whole function
  97. cannot be used: if the macro can be expanded several times in one
  98. function, the label is multiply defined in that function. A
  99. local label avoids this problem. For example:
  100. </p>
  101. <div class="smallexample">
  102. <pre class="smallexample">#define SEARCH(value, array, target) \
  103. do { \
  104. __label__ found; \
  105. typeof (target) _SEARCH_target = (target); \
  106. typeof (*(array)) *_SEARCH_array = (array); \
  107. int i, j; \
  108. int value; \
  109. for (i = 0; i &lt; max; i++) \
  110. for (j = 0; j &lt; max; j++) \
  111. if (_SEARCH_array[i][j] == _SEARCH_target) \
  112. { (value) = i; goto found; } \
  113. (value) = -1; \
  114. found:; \
  115. } while (0)
  116. </pre></div>
  117. <p>This could also be written using a statement expression:
  118. </p>
  119. <div class="smallexample">
  120. <pre class="smallexample">#define SEARCH(array, target) \
  121. ({ \
  122. __label__ found; \
  123. typeof (target) _SEARCH_target = (target); \
  124. typeof (*(array)) *_SEARCH_array = (array); \
  125. int i, j; \
  126. int value; \
  127. for (i = 0; i &lt; max; i++) \
  128. for (j = 0; j &lt; max; j++) \
  129. if (_SEARCH_array[i][j] == _SEARCH_target) \
  130. { value = i; goto found; } \
  131. value = -1; \
  132. found: \
  133. value; \
  134. })
  135. </pre></div>
  136. <p>Local label declarations also make the labels they declare visible to
  137. nested functions, if there are any. See <a href="Nested-Functions.html#Nested-Functions">Nested Functions</a>, for details.
  138. </p>
  139. <hr>
  140. <div class="header">
  141. <p>
  142. Next: <a href="Labels-as-Values.html#Labels-as-Values" accesskey="n" rel="next">Labels as Values</a>, Previous: <a href="Statement-Exprs.html#Statement-Exprs" accesskey="p" rel="prev">Statement Exprs</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>
  143. </div>
  144. </body>
  145. </html>