Working-with-declarations.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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>GNU Compiler Collection (GCC) Internals: Working with declarations</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Working with declarations">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Working with declarations">
  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="Declarations.html#Declarations" rel="up" title="Declarations">
  30. <link href="Internal-structure.html#Internal-structure" rel="next" title="Internal structure">
  31. <link href="Declarations.html#Declarations" rel="prev" title="Declarations">
  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="Working-with-declarations"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Internal-structure.html#Internal-structure" accesskey="n" rel="next">Internal structure</a>, Up: <a href="Declarations.html#Declarations" accesskey="u" rel="up">Declarations</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="Working-with-declarations-1"></a>
  69. <h4 class="subsection">10.4.1 Working with declarations</h4>
  70. <p>Some macros can be used with any kind of declaration. These include:
  71. </p><dl compact="compact">
  72. <dt><code>DECL_NAME</code>
  73. <a name="index-DECL_005fNAME"></a>
  74. </dt>
  75. <dd><p>This macro returns an <code>IDENTIFIER_NODE</code> giving the name of the
  76. entity.
  77. </p>
  78. </dd>
  79. <dt><code>TREE_TYPE</code>
  80. <a name="index-TREE_005fTYPE-2"></a>
  81. </dt>
  82. <dd><p>This macro returns the type of the entity declared.
  83. </p>
  84. </dd>
  85. <dt><code>EXPR_FILENAME</code>
  86. <a name="index-EXPR_005fFILENAME"></a>
  87. </dt>
  88. <dd><p>This macro returns the name of the file in which the entity was
  89. declared, as a <code>char*</code>. For an entity declared implicitly by the
  90. compiler (like <code>__builtin_memcpy</code>), this will be the string
  91. <code>&quot;&lt;internal&gt;&quot;</code>.
  92. </p>
  93. </dd>
  94. <dt><code>EXPR_LINENO</code>
  95. <a name="index-EXPR_005fLINENO"></a>
  96. </dt>
  97. <dd><p>This macro returns the line number at which the entity was declared, as
  98. an <code>int</code>.
  99. </p>
  100. </dd>
  101. <dt><code>DECL_ARTIFICIAL</code>
  102. <a name="index-DECL_005fARTIFICIAL"></a>
  103. </dt>
  104. <dd><p>This predicate holds if the declaration was implicitly generated by the
  105. compiler. For example, this predicate will hold of an implicitly
  106. declared member function, or of the <code>TYPE_DECL</code> implicitly
  107. generated for a class type. Recall that in C++ code like:
  108. </p><div class="smallexample">
  109. <pre class="smallexample">struct S {};
  110. </pre></div>
  111. <p>is roughly equivalent to C code like:
  112. </p><div class="smallexample">
  113. <pre class="smallexample">struct S {};
  114. typedef struct S S;
  115. </pre></div>
  116. <p>The implicitly generated <code>typedef</code> declaration is represented by a
  117. <code>TYPE_DECL</code> for which <code>DECL_ARTIFICIAL</code> holds.
  118. </p>
  119. </dd>
  120. </dl>
  121. <p>The various kinds of declarations include:
  122. </p><dl compact="compact">
  123. <dt><code>LABEL_DECL</code></dt>
  124. <dd><p>These nodes are used to represent labels in function bodies. For more
  125. information, see <a href="Functions.html#Functions">Functions</a>. These nodes only appear in block
  126. scopes.
  127. </p>
  128. </dd>
  129. <dt><code>CONST_DECL</code></dt>
  130. <dd><p>These nodes are used to represent enumeration constants. The value of
  131. the constant is given by <code>DECL_INITIAL</code> which will be an
  132. <code>INTEGER_CST</code> with the same type as the <code>TREE_TYPE</code> of the
  133. <code>CONST_DECL</code>, i.e., an <code>ENUMERAL_TYPE</code>.
  134. </p>
  135. </dd>
  136. <dt><code>RESULT_DECL</code></dt>
  137. <dd><p>These nodes represent the value returned by a function. When a value is
  138. assigned to a <code>RESULT_DECL</code>, that indicates that the value should
  139. be returned, via bitwise copy, by the function. You can use
  140. <code>DECL_SIZE</code> and <code>DECL_ALIGN</code> on a <code>RESULT_DECL</code>, just as
  141. with a <code>VAR_DECL</code>.
  142. </p>
  143. </dd>
  144. <dt><code>TYPE_DECL</code></dt>
  145. <dd><p>These nodes represent <code>typedef</code> declarations. The <code>TREE_TYPE</code>
  146. is the type declared to have the name given by <code>DECL_NAME</code>. In
  147. some cases, there is no associated name.
  148. </p>
  149. </dd>
  150. <dt><code>VAR_DECL</code></dt>
  151. <dd><p>These nodes represent variables with namespace or block scope, as well
  152. as static data members. The <code>DECL_SIZE</code> and <code>DECL_ALIGN</code> are
  153. analogous to <code>TYPE_SIZE</code> and <code>TYPE_ALIGN</code>. For a declaration,
  154. you should always use the <code>DECL_SIZE</code> and <code>DECL_ALIGN</code> rather
  155. than the <code>TYPE_SIZE</code> and <code>TYPE_ALIGN</code> given by the
  156. <code>TREE_TYPE</code>, since special attributes may have been applied to the
  157. variable to give it a particular size and alignment. You may use the
  158. predicates <code>DECL_THIS_STATIC</code> or <code>DECL_THIS_EXTERN</code> to test
  159. whether the storage class specifiers <code>static</code> or <code>extern</code> were
  160. used to declare a variable.
  161. </p>
  162. <p>If this variable is initialized (but does not require a constructor),
  163. the <code>DECL_INITIAL</code> will be an expression for the initializer. The
  164. initializer should be evaluated, and a bitwise copy into the variable
  165. performed. If the <code>DECL_INITIAL</code> is the <code>error_mark_node</code>,
  166. there is an initializer, but it is given by an explicit statement later
  167. in the code; no bitwise copy is required.
  168. </p>
  169. <p>GCC provides an extension that allows either automatic variables, or
  170. global variables, to be placed in particular registers. This extension
  171. is being used for a particular <code>VAR_DECL</code> if <code>DECL_REGISTER</code>
  172. holds for the <code>VAR_DECL</code>, and if <code>DECL_ASSEMBLER_NAME</code> is not
  173. equal to <code>DECL_NAME</code>. In that case, <code>DECL_ASSEMBLER_NAME</code> is
  174. the name of the register into which the variable will be placed.
  175. </p>
  176. </dd>
  177. <dt><code>PARM_DECL</code></dt>
  178. <dd><p>Used to represent a parameter to a function. Treat these nodes
  179. similarly to <code>VAR_DECL</code> nodes. These nodes only appear in the
  180. <code>DECL_ARGUMENTS</code> for a <code>FUNCTION_DECL</code>.
  181. </p>
  182. <p>The <code>DECL_ARG_TYPE</code> for a <code>PARM_DECL</code> is the type that will
  183. actually be used when a value is passed to this function. It may be a
  184. wider type than the <code>TREE_TYPE</code> of the parameter; for example, the
  185. ordinary type might be <code>short</code> while the <code>DECL_ARG_TYPE</code> is
  186. <code>int</code>.
  187. </p>
  188. </dd>
  189. <dt><code>DEBUG_EXPR_DECL</code></dt>
  190. <dd><p>Used to represent an anonymous debug-information temporary created to
  191. hold an expression as it is optimized away, so that its value can be
  192. referenced in debug bind statements.
  193. </p>
  194. </dd>
  195. <dt><code>FIELD_DECL</code></dt>
  196. <dd><p>These nodes represent non-static data members. The <code>DECL_SIZE</code> and
  197. <code>DECL_ALIGN</code> behave as for <code>VAR_DECL</code> nodes.
  198. The position of the field within the parent record is specified by a
  199. combination of three attributes. <code>DECL_FIELD_OFFSET</code> is the position,
  200. counting in bytes, of the <code>DECL_OFFSET_ALIGN</code>-bit sized word containing
  201. the bit of the field closest to the beginning of the structure.
  202. <code>DECL_FIELD_BIT_OFFSET</code> is the bit offset of the first bit of the field
  203. within this word; this may be nonzero even for fields that are not bit-fields,
  204. since <code>DECL_OFFSET_ALIGN</code> may be greater than the natural alignment
  205. of the field&rsquo;s type.
  206. </p>
  207. <p>If <code>DECL_C_BIT_FIELD</code> holds, this field is a bit-field. In a bit-field,
  208. <code>DECL_BIT_FIELD_TYPE</code> also contains the type that was originally
  209. specified for it, while DECL_TYPE may be a modified type with lesser precision,
  210. according to the size of the bit field.
  211. </p>
  212. </dd>
  213. <dt><code>NAMESPACE_DECL</code></dt>
  214. <dd><p>Namespaces provide a name hierarchy for other declarations. They
  215. appear in the <code>DECL_CONTEXT</code> of other <code>_DECL</code> nodes.
  216. </p>
  217. </dd>
  218. </dl>
  219. <hr>
  220. <div class="header">
  221. <p>
  222. Next: <a href="Internal-structure.html#Internal-structure" accesskey="n" rel="next">Internal structure</a>, Up: <a href="Declarations.html#Declarations" accesskey="u" rel="up">Declarations</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>
  223. </div>
  224. </body>
  225. </html>