Hash-Nodes.html 5.5 KB

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