You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Statements-for-C_002b_002b.html 15KB

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Statements for C++ (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Statements for C++ (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Statements for C++ (GNU Compiler Collection (GCC) Internals)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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-and-C_002b_002b-Trees.html#C-and-C_002b_002b-Trees" rel="up" title="C and C++ Trees">
  30. <link href="C_002b_002b-Expressions.html#C_002b_002b-Expressions" rel="next" title="C++ Expressions">
  31. <link href="Functions-for-C_002b_002b.html#Functions-for-C_002b_002b" rel="prev" title="Functions for C++">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Statements-for-C_002b_002b"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="C_002b_002b-Expressions.html#C_002b_002b-Expressions" accesskey="n" rel="next">C++ Expressions</a>, Previous: <a href="Functions-for-C_002b_002b.html#Functions-for-C_002b_002b" accesskey="p" rel="prev">Functions for C++</a>, Up: <a href="C-and-C_002b_002b-Trees.html#C-and-C_002b_002b-Trees" accesskey="u" rel="up">C and C++ Trees</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>
  65. </div>
  66. <hr>
  67. <a name="Statements-for-C_002b_002b-1"></a>
  68. <h4 class="subsection">11.10.5 Statements for C++</h4>
  69. <a name="index-statements-1"></a>
  70. <a name="index-BREAK_005fSTMT"></a>
  71. <a name="index-CLEANUP_005fSTMT"></a>
  72. <a name="index-CLEANUP_005fDECL"></a>
  73. <a name="index-CLEANUP_005fEXPR"></a>
  74. <a name="index-CONTINUE_005fSTMT"></a>
  75. <a name="index-DECL_005fSTMT"></a>
  76. <a name="index-DECL_005fSTMT_005fDECL"></a>
  77. <a name="index-DO_005fSTMT"></a>
  78. <a name="index-DO_005fBODY"></a>
  79. <a name="index-DO_005fCOND"></a>
  80. <a name="index-EMPTY_005fCLASS_005fEXPR"></a>
  81. <a name="index-EXPR_005fSTMT"></a>
  82. <a name="index-EXPR_005fSTMT_005fEXPR"></a>
  83. <a name="index-FOR_005fSTMT"></a>
  84. <a name="index-FOR_005fINIT_005fSTMT"></a>
  85. <a name="index-FOR_005fCOND"></a>
  86. <a name="index-FOR_005fEXPR"></a>
  87. <a name="index-FOR_005fBODY"></a>
  88. <a name="index-HANDLER"></a>
  89. <a name="index-IF_005fSTMT"></a>
  90. <a name="index-IF_005fCOND"></a>
  91. <a name="index-THEN_005fCLAUSE"></a>
  92. <a name="index-ELSE_005fCLAUSE"></a>
  93. <a name="index-RETURN_005fSTMT"></a>
  94. <a name="index-RETURN_005fEXPR"></a>
  95. <a name="index-SUBOBJECT"></a>
  96. <a name="index-SUBOBJECT_005fCLEANUP"></a>
  97. <a name="index-SWITCH_005fSTMT"></a>
  98. <a name="index-SWITCH_005fCOND"></a>
  99. <a name="index-SWITCH_005fBODY"></a>
  100. <a name="index-TRY_005fBLOCK"></a>
  101. <a name="index-TRY_005fSTMTS"></a>
  102. <a name="index-TRY_005fHANDLERS"></a>
  103. <a name="index-HANDLER_005fPARMS"></a>
  104. <a name="index-HANDLER_005fBODY"></a>
  105. <a name="index-USING_005fSTMT"></a>
  106. <a name="index-WHILE_005fSTMT"></a>
  107. <a name="index-WHILE_005fBODY"></a>
  108. <a name="index-WHILE_005fCOND"></a>
  109. <p>A function that has a definition in the current translation unit will
  110. have a non-<code>NULL</code> <code>DECL_INITIAL</code>. However, back ends should not make
  111. use of the particular value given by <code>DECL_INITIAL</code>.
  112. </p>
  113. <p>The <code>DECL_SAVED_TREE</code> macro will give the complete body of the
  114. function.
  115. </p>
  116. <a name="Statements-2"></a>
  117. <h4 class="subsubsection">11.10.5.1 Statements</h4>
  118. <p>There are tree nodes corresponding to all of the source-level
  119. statement constructs, used within the C and C++ frontends. These are
  120. enumerated here, together with a list of the various macros that can
  121. be used to obtain information about them. There are a few macros that
  122. can be used with all statements:
  123. </p>
  124. <dl compact="compact">
  125. <dt><code>STMT_IS_FULL_EXPR_P</code>
  126. <a name="index-STMT_005fIS_005fFULL_005fEXPR_005fP"></a>
  127. </dt>
  128. <dd><p>In C++, statements normally constitute &ldquo;full expressions&rdquo;; temporaries
  129. created during a statement are destroyed when the statement is complete.
  130. However, G++ sometimes represents expressions by statements; these
  131. statements will not have <code>STMT_IS_FULL_EXPR_P</code> set. Temporaries
  132. created during such statements should be destroyed when the innermost
  133. enclosing statement with <code>STMT_IS_FULL_EXPR_P</code> set is exited.
  134. </p>
  135. </dd>
  136. </dl>
  137. <p>Here is the list of the various statement nodes, and the macros used to
  138. access them. This documentation describes the use of these nodes in
  139. non-template functions (including instantiations of template functions).
  140. In template functions, the same nodes are used, but sometimes in
  141. slightly different ways.
  142. </p>
  143. <p>Many of the statements have substatements. For example, a <code>while</code>
  144. loop will have a body, which is itself a statement. If the substatement
  145. is <code>NULL_TREE</code>, it is considered equivalent to a statement
  146. consisting of a single <code>;</code>, i.e., an expression statement in which
  147. the expression has been omitted. A substatement may in fact be a list
  148. of statements, connected via their <code>TREE_CHAIN</code>s. So, you should
  149. always process the statement tree by looping over substatements, like
  150. this:
  151. </p><div class="smallexample">
  152. <pre class="smallexample">void process_stmt (stmt)
  153. tree stmt;
  154. {
  155. while (stmt)
  156. {
  157. switch (TREE_CODE (stmt))
  158. {
  159. case IF_STMT:
  160. process_stmt (THEN_CLAUSE (stmt));
  161. /* <span class="roman">More processing here.</span> */
  162. break;
  163. &hellip;
  164. }
  165. stmt = TREE_CHAIN (stmt);
  166. }
  167. }
  168. </pre></div>
  169. <p>In other words, while the <code>then</code> clause of an <code>if</code> statement
  170. in C++ can be only one statement (although that one statement may be a
  171. compound statement), the intermediate representation will sometimes use
  172. several statements chained together.
  173. </p>
  174. <dl compact="compact">
  175. <dt><code>BREAK_STMT</code></dt>
  176. <dd>
  177. <p>Used to represent a <code>break</code> statement. There are no additional
  178. fields.
  179. </p>
  180. </dd>
  181. <dt><code>CLEANUP_STMT</code></dt>
  182. <dd>
  183. <p>Used to represent an action that should take place upon exit from the
  184. enclosing scope. Typically, these actions are calls to destructors for
  185. local objects, but back ends cannot rely on this fact. If these nodes
  186. are in fact representing such destructors, <code>CLEANUP_DECL</code> will be
  187. the <code>VAR_DECL</code> destroyed. Otherwise, <code>CLEANUP_DECL</code> will be
  188. <code>NULL_TREE</code>. In any case, the <code>CLEANUP_EXPR</code> is the
  189. expression to execute. The cleanups executed on exit from a scope
  190. should be run in the reverse order of the order in which the associated
  191. <code>CLEANUP_STMT</code>s were encountered.
  192. </p>
  193. </dd>
  194. <dt><code>CONTINUE_STMT</code></dt>
  195. <dd>
  196. <p>Used to represent a <code>continue</code> statement. There are no additional
  197. fields.
  198. </p>
  199. </dd>
  200. <dt><code>CTOR_STMT</code></dt>
  201. <dd>
  202. <p>Used to mark the beginning (if <code>CTOR_BEGIN_P</code> holds) or end (if
  203. <code>CTOR_END_P</code> holds of the main body of a constructor. See also
  204. <code>SUBOBJECT</code> for more information on how to use these nodes.
  205. </p>
  206. </dd>
  207. <dt><code>DO_STMT</code></dt>
  208. <dd>
  209. <p>Used to represent a <code>do</code> loop. The body of the loop is given by
  210. <code>DO_BODY</code> while the termination condition for the loop is given by
  211. <code>DO_COND</code>. The condition for a <code>do</code>-statement is always an
  212. expression.
  213. </p>
  214. </dd>
  215. <dt><code>EMPTY_CLASS_EXPR</code></dt>
  216. <dd>
  217. <p>Used to represent a temporary object of a class with no data whose
  218. address is never taken. (All such objects are interchangeable.) The
  219. <code>TREE_TYPE</code> represents the type of the object.
  220. </p>
  221. </dd>
  222. <dt><code>EXPR_STMT</code></dt>
  223. <dd>
  224. <p>Used to represent an expression statement. Use <code>EXPR_STMT_EXPR</code> to
  225. obtain the expression.
  226. </p>
  227. </dd>
  228. <dt><code>FOR_STMT</code></dt>
  229. <dd>
  230. <p>Used to represent a <code>for</code> statement. The <code>FOR_INIT_STMT</code> is
  231. the initialization statement for the loop. The <code>FOR_COND</code> is the
  232. termination condition. The <code>FOR_EXPR</code> is the expression executed
  233. right before the <code>FOR_COND</code> on each loop iteration; often, this
  234. expression increments a counter. The body of the loop is given by
  235. <code>FOR_BODY</code>. Note that <code>FOR_INIT_STMT</code> and <code>FOR_BODY</code>
  236. return statements, while <code>FOR_COND</code> and <code>FOR_EXPR</code> return
  237. expressions.
  238. </p>
  239. </dd>
  240. <dt><code>HANDLER</code></dt>
  241. <dd>
  242. <p>Used to represent a C++ <code>catch</code> block. The <code>HANDLER_TYPE</code>
  243. is the type of exception that will be caught by this handler; it is
  244. equal (by pointer equality) to <code>NULL</code> if this handler is for all
  245. types. <code>HANDLER_PARMS</code> is the <code>DECL_STMT</code> for the catch
  246. parameter, and <code>HANDLER_BODY</code> is the code for the block itself.
  247. </p>
  248. </dd>
  249. <dt><code>IF_STMT</code></dt>
  250. <dd>
  251. <p>Used to represent an <code>if</code> statement. The <code>IF_COND</code> is the
  252. expression.
  253. </p>
  254. <p>If the condition is a <code>TREE_LIST</code>, then the <code>TREE_PURPOSE</code> is
  255. a statement (usually a <code>DECL_STMT</code>). Each time the condition is
  256. evaluated, the statement should be executed. Then, the
  257. <code>TREE_VALUE</code> should be used as the conditional expression itself.
  258. This representation is used to handle C++ code like this:
  259. </p>
  260. <p>C++ distinguishes between this and <code>COND_EXPR</code> for handling templates.
  261. </p>
  262. <div class="smallexample">
  263. <pre class="smallexample">if (int i = 7) &hellip;
  264. </pre></div>
  265. <p>where there is a new local variable (or variables) declared within the
  266. condition.
  267. </p>
  268. <p>The <code>THEN_CLAUSE</code> represents the statement given by the <code>then</code>
  269. condition, while the <code>ELSE_CLAUSE</code> represents the statement given
  270. by the <code>else</code> condition.
  271. </p>
  272. </dd>
  273. <dt><code>SUBOBJECT</code></dt>
  274. <dd>
  275. <p>In a constructor, these nodes are used to mark the point at which a
  276. subobject of <code>this</code> is fully constructed. If, after this point, an
  277. exception is thrown before a <code>CTOR_STMT</code> with <code>CTOR_END_P</code> set
  278. is encountered, the <code>SUBOBJECT_CLEANUP</code> must be executed. The
  279. cleanups must be executed in the reverse order in which they appear.
  280. </p>
  281. </dd>
  282. <dt><code>SWITCH_STMT</code></dt>
  283. <dd>
  284. <p>Used to represent a <code>switch</code> statement. The <code>SWITCH_STMT_COND</code>
  285. is the expression on which the switch is occurring. See the documentation
  286. for an <code>IF_STMT</code> for more information on the representation used
  287. for the condition. The <code>SWITCH_STMT_BODY</code> is the body of the switch
  288. statement. The <code>SWITCH_STMT_TYPE</code> is the original type of switch
  289. expression as given in the source, before any compiler conversions.
  290. </p>
  291. </dd>
  292. <dt><code>TRY_BLOCK</code></dt>
  293. <dd><p>Used to represent a <code>try</code> block. The body of the try block is
  294. given by <code>TRY_STMTS</code>. Each of the catch blocks is a <code>HANDLER</code>
  295. node. The first handler is given by <code>TRY_HANDLERS</code>. Subsequent
  296. handlers are obtained by following the <code>TREE_CHAIN</code> link from one
  297. handler to the next. The body of the handler is given by
  298. <code>HANDLER_BODY</code>.
  299. </p>
  300. <p>If <code>CLEANUP_P</code> holds of the <code>TRY_BLOCK</code>, then the
  301. <code>TRY_HANDLERS</code> will not be a <code>HANDLER</code> node. Instead, it will
  302. be an expression that should be executed if an exception is thrown in
  303. the try block. It must rethrow the exception after executing that code.
  304. And, if an exception is thrown while the expression is executing,
  305. <code>terminate</code> must be called.
  306. </p>
  307. </dd>
  308. <dt><code>USING_STMT</code></dt>
  309. <dd><p>Used to represent a <code>using</code> directive. The namespace is given by
  310. <code>USING_STMT_NAMESPACE</code>, which will be a NAMESPACE_DECL. This node
  311. is needed inside template functions, to implement using directives
  312. during instantiation.
  313. </p>
  314. </dd>
  315. <dt><code>WHILE_STMT</code></dt>
  316. <dd>
  317. <p>Used to represent a <code>while</code> loop. The <code>WHILE_COND</code> is the
  318. termination condition for the loop. See the documentation for an
  319. <code>IF_STMT</code> for more information on the representation used for the
  320. condition.
  321. </p>
  322. <p>The <code>WHILE_BODY</code> is the body of the loop.
  323. </p>
  324. </dd>
  325. </dl>
  326. <hr>
  327. <div class="header">
  328. <p>
  329. Next: <a href="C_002b_002b-Expressions.html#C_002b_002b-Expressions" accesskey="n" rel="next">C++ Expressions</a>, Previous: <a href="Functions-for-C_002b_002b.html#Functions-for-C_002b_002b" accesskey="p" rel="prev">Functions for C++</a>, Up: <a href="C-and-C_002b_002b-Trees.html#C-and-C_002b_002b-Trees" accesskey="u" rel="up">C and C++ Trees</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>
  330. </div>
  331. </body>
  332. </html>