Hash-Nodes.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <title>The GNU C Preprocessor Internals: Hash Nodes</title>
  6. <meta name="description" content="The GNU C Preprocessor Internals: Hash Nodes">
  7. <meta name="keywords" content="The GNU C Preprocessor Internals: Hash Nodes">
  8. <meta name="resource-type" content="document">
  9. <meta name="distribution" content="global">
  10. <meta name="Generator" content="makeinfo">
  11. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  14. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="index.html#Top" rel="up" title="Top">
  16. <link href="Macro-Expansion.html#Macro-Expansion" rel="next" title="Macro Expansion">
  17. <link href="Lexer.html#Lexer" rel="prev" title="Lexer">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.smallquotation {font-size: smaller}
  22. div.display {margin-left: 3.2em}
  23. div.example {margin-left: 3.2em}
  24. div.indentedblock {margin-left: 3.2em}
  25. div.lisp {margin-left: 3.2em}
  26. div.smalldisplay {margin-left: 3.2em}
  27. div.smallexample {margin-left: 3.2em}
  28. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style:oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nocodebreak {white-space:nowrap}
  40. span.nolinebreak {white-space:nowrap}
  41. span.roman {font-family:serif; font-weight:normal}
  42. span.sansserif {font-family:sans-serif; font-weight:normal}
  43. ul.no-bullet {list-style: none}
  44. -->
  45. </style>
  46. </head>
  47. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  48. <a name="Hash-Nodes"></a>
  49. <div class="header">
  50. <p>
  51. Next: <a href="Macro-Expansion.html#Macro-Expansion" accesskey="n" rel="next">Macro Expansion</a>, Previous: <a href="Lexer.html#Lexer" accesskey="p" rel="prev">Lexer</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  52. </div>
  53. <hr>
  54. <a name="Hash-Nodes-1"></a>
  55. <h2 class="unnumbered">Hash Nodes</h2>
  56. <a name="index-hash-table"></a>
  57. <a name="index-identifiers"></a>
  58. <a name="index-macros"></a>
  59. <a name="index-assertions"></a>
  60. <a name="index-named-operators"></a>
  61. <p>When cpplib encounters an &ldquo;identifier&rdquo;, it generates a hash code for
  62. it and stores it in the hash table. By &ldquo;identifier&rdquo; we mean tokens
  63. with type <code>CPP_NAME</code>; this includes identifiers in the usual C
  64. sense, as well as keywords, directive names, macro names and so on. For
  65. example, all of <code>pragma</code>, <code>int</code>, <code>foo</code> and
  66. <code>__GNUC__</code> are identifiers and hashed when lexed.
  67. </p>
  68. <p>Each node in the hash table contain various information about the
  69. identifier it represents. For example, its length and type. At any one
  70. time, each identifier falls into exactly one of three categories:
  71. </p>
  72. <ul>
  73. <li> Macros
  74. <p>These have been declared to be macros, either on the command line or
  75. with <code>#define</code>. A few, such as <code>__TIME__</code> are built-ins
  76. entered in the hash table during initialization. The hash node for a
  77. normal macro points to a structure with more information about the
  78. macro, such as whether it is function-like, how many arguments it takes,
  79. and its expansion. Built-in macros are flagged as special, and instead
  80. contain an enum indicating which of the various built-in macros it is.
  81. </p>
  82. </li><li> Assertions
  83. <p>Assertions are in a separate namespace to macros. To enforce this, cpp
  84. actually prepends a <code>#</code> character before hashing and entering it in
  85. the hash table. An assertion&rsquo;s node points to a chain of answers to
  86. that assertion.
  87. </p>
  88. </li><li> Void
  89. <p>Everything else falls into this category&mdash;an identifier that is not
  90. currently a macro, or a macro that has since been undefined with
  91. <code>#undef</code>.
  92. </p>
  93. <p>When preprocessing C++, this category also includes the named operators,
  94. such as <code>xor</code>. In expressions these behave like the operators they
  95. represent, but in contexts where the spelling of a token matters they
  96. are spelt differently. This spelling distinction is relevant when they
  97. are operands of the stringizing and pasting macro operators <code>#</code> and
  98. <code>##</code>. Named operator hash nodes are flagged, both to catch the
  99. spelling distinction and to prevent them from being defined as macros.
  100. </p></li></ul>
  101. <p>The same identifiers share the same hash node. Since each identifier
  102. token, after lexing, contains a pointer to its hash node, this is used
  103. to provide rapid lookup of various information. For example, when
  104. parsing a <code>#define</code> statement, CPP flags each argument&rsquo;s identifier
  105. hash node with the index of that argument. This makes duplicated
  106. argument checking an O(1) operation for each argument. Similarly, for
  107. each identifier in the macro&rsquo;s expansion, lookup to see if it is an
  108. argument, and which argument it is, is also an O(1) operation. Further,
  109. each directive name, such as <code>endif</code>, has an associated directive
  110. enum stored in its hash node, so that directive lookup is also O(1).
  111. </p>
  112. <hr>
  113. <div class="header">
  114. <p>
  115. Next: <a href="Macro-Expansion.html#Macro-Expansion" accesskey="n" rel="next">Macro Expansion</a>, Previous: <a href="Lexer.html#Lexer" accesskey="p" rel="prev">Lexer</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  116. </div>
  117. </body>
  118. </html>