Clone of mesa.
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.

symbol_table.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "main/imports.h"
  24. #include "symbol_table.h"
  25. #include "hash_table.h"
  26. struct symbol {
  27. /**
  28. * Link to the next symbol in the table with the same name
  29. *
  30. * The linked list of symbols with the same name is ordered by scope
  31. * from inner-most to outer-most.
  32. */
  33. struct symbol *next_with_same_name;
  34. /**
  35. * Link to the next symbol in the table with the same scope
  36. *
  37. * The linked list of symbols with the same scope is unordered. Symbols
  38. * in this list my have unique names.
  39. */
  40. struct symbol *next_with_same_scope;
  41. /**
  42. * Header information for the list of symbols with the same name.
  43. */
  44. struct symbol_header *hdr;
  45. /**
  46. * Name space of the symbol
  47. *
  48. * Name space are arbitrary user assigned integers. No two symbols can
  49. * exist in the same name space at the same scope level.
  50. */
  51. int name_space;
  52. /**
  53. * Arbitrary user supplied data.
  54. */
  55. void *data;
  56. /** Scope depth where this symbol was defined. */
  57. unsigned depth;
  58. };
  59. /**
  60. */
  61. struct symbol_header {
  62. /** Linkage in list of all headers in a given symbol table. */
  63. struct symbol_header *next;
  64. /** Symbol name. */
  65. const char *name;
  66. /** Linked list of symbols with the same name. */
  67. struct symbol *symbols;
  68. };
  69. /**
  70. * Element of the scope stack.
  71. */
  72. struct scope_level {
  73. /** Link to next (inner) scope level. */
  74. struct scope_level *next;
  75. /** Linked list of symbols with the same scope. */
  76. struct symbol *symbols;
  77. };
  78. /**
  79. *
  80. */
  81. struct _mesa_symbol_table {
  82. /** Hash table containing all symbols in the symbol table. */
  83. struct hash_table *ht;
  84. /** Top of scope stack. */
  85. struct scope_level *current_scope;
  86. /** List of all symbol headers in the table. */
  87. struct symbol_header *hdr;
  88. /** Current scope depth. */
  89. unsigned depth;
  90. };
  91. struct _mesa_symbol_table_iterator {
  92. /**
  93. * Name space of symbols returned by this iterator.
  94. */
  95. int name_space;
  96. /**
  97. * Currently iterated symbol
  98. *
  99. * The next call to \c _mesa_symbol_table_iterator_get will return this
  100. * value. It will also update this value to the value that should be
  101. * returned by the next call.
  102. */
  103. struct symbol *curr;
  104. };
  105. static void
  106. check_symbol_table(struct _mesa_symbol_table *table)
  107. {
  108. #if 1
  109. struct scope_level *scope;
  110. for (scope = table->current_scope; scope != NULL; scope = scope->next) {
  111. struct symbol *sym;
  112. for (sym = scope->symbols
  113. ; sym != NULL
  114. ; sym = sym->next_with_same_name) {
  115. const struct symbol_header *const hdr = sym->hdr;
  116. struct symbol *sym2;
  117. for (sym2 = hdr->symbols
  118. ; sym2 != NULL
  119. ; sym2 = sym2->next_with_same_name) {
  120. assert(sym2->hdr == hdr);
  121. }
  122. }
  123. }
  124. #endif
  125. }
  126. void
  127. _mesa_symbol_table_pop_scope(struct _mesa_symbol_table *table)
  128. {
  129. struct scope_level *const scope = table->current_scope;
  130. struct symbol *sym = scope->symbols;
  131. table->current_scope = scope->next;
  132. table->depth--;
  133. free(scope);
  134. while (sym != NULL) {
  135. struct symbol *const next = sym->next_with_same_scope;
  136. struct symbol_header *const hdr = sym->hdr;
  137. assert(hdr->symbols == sym);
  138. hdr->symbols = sym->next_with_same_name;
  139. free(sym);
  140. sym = next;
  141. }
  142. check_symbol_table(table);
  143. }
  144. void
  145. _mesa_symbol_table_push_scope(struct _mesa_symbol_table *table)
  146. {
  147. struct scope_level *const scope = calloc(1, sizeof(*scope));
  148. scope->next = table->current_scope;
  149. table->current_scope = scope;
  150. table->depth++;
  151. }
  152. static struct symbol_header *
  153. find_symbol(struct _mesa_symbol_table *table, const char *name)
  154. {
  155. return (struct symbol_header *) hash_table_find(table->ht, name);
  156. }
  157. struct _mesa_symbol_table_iterator *
  158. _mesa_symbol_table_iterator_ctor(struct _mesa_symbol_table *table,
  159. int name_space, const char *name)
  160. {
  161. struct _mesa_symbol_table_iterator *iter = calloc(1, sizeof(*iter));
  162. struct symbol_header *const hdr = find_symbol(table, name);
  163. iter->name_space = name_space;
  164. if (hdr != NULL) {
  165. struct symbol *sym;
  166. for (sym = hdr->symbols; sym != NULL; sym = sym->next_with_same_name) {
  167. assert(sym->hdr == hdr);
  168. if ((name_space == -1) || (sym->name_space == name_space)) {
  169. iter->curr = sym;
  170. break;
  171. }
  172. }
  173. }
  174. return iter;
  175. }
  176. void
  177. _mesa_symbol_table_iterator_dtor(struct _mesa_symbol_table_iterator *iter)
  178. {
  179. free(iter);
  180. }
  181. void *
  182. _mesa_symbol_table_iterator_get(struct _mesa_symbol_table_iterator *iter)
  183. {
  184. return (iter->curr == NULL) ? NULL : iter->curr->data;
  185. }
  186. int
  187. _mesa_symbol_table_iterator_next(struct _mesa_symbol_table_iterator *iter)
  188. {
  189. struct symbol_header *hdr;
  190. if (iter->curr == NULL) {
  191. return 0;
  192. }
  193. hdr = iter->curr->hdr;
  194. iter->curr = iter->curr->next_with_same_name;
  195. while (iter->curr != NULL) {
  196. assert(iter->curr->hdr == hdr);
  197. if ((iter->name_space == -1)
  198. || (iter->curr->name_space == iter->name_space)) {
  199. return 1;
  200. }
  201. iter->curr = iter->curr->next_with_same_name;
  202. }
  203. return 0;
  204. }
  205. void *
  206. _mesa_symbol_table_find_symbol(struct _mesa_symbol_table *table,
  207. int name_space, const char *name)
  208. {
  209. struct symbol_header *const hdr = find_symbol(table, name);
  210. if (hdr != NULL) {
  211. struct symbol *sym;
  212. for (sym = hdr->symbols; sym != NULL; sym = sym->next_with_same_name) {
  213. assert(sym->hdr == hdr);
  214. if ((name_space == -1) || (sym->name_space == name_space)) {
  215. return sym->data;
  216. }
  217. }
  218. }
  219. return NULL;
  220. }
  221. int
  222. _mesa_symbol_table_add_symbol(struct _mesa_symbol_table *table,
  223. int name_space, const char *name,
  224. void *declaration)
  225. {
  226. struct symbol_header *hdr;
  227. struct symbol *sym;
  228. check_symbol_table(table);
  229. hdr = find_symbol(table, name);
  230. check_symbol_table(table);
  231. if (hdr == NULL) {
  232. hdr = calloc(1, sizeof(*hdr));
  233. hdr->name = name;
  234. hash_table_insert(table->ht, hdr, name);
  235. hdr->next = table->hdr;
  236. table->hdr = hdr;
  237. }
  238. check_symbol_table(table);
  239. /* If the symbol already exists at this scope, it cannot be added to the
  240. * table.
  241. */
  242. if (hdr->symbols && (hdr->symbols->depth == table->depth))
  243. return -1;
  244. sym = calloc(1, sizeof(*sym));
  245. sym->next_with_same_name = hdr->symbols;
  246. sym->next_with_same_scope = table->current_scope->symbols;
  247. sym->hdr = hdr;
  248. sym->name_space = name_space;
  249. sym->data = declaration;
  250. sym->depth = table->depth;
  251. assert(sym->hdr == hdr);
  252. hdr->symbols = sym;
  253. table->current_scope->symbols = sym;
  254. check_symbol_table(table);
  255. return 0;
  256. }
  257. struct _mesa_symbol_table *
  258. _mesa_symbol_table_ctor(void)
  259. {
  260. struct _mesa_symbol_table *table = calloc(1, sizeof(*table));
  261. if (table != NULL) {
  262. table->ht = hash_table_ctor(32, hash_table_string_hash,
  263. hash_table_string_compare);
  264. _mesa_symbol_table_push_scope(table);
  265. }
  266. return table;
  267. }
  268. void
  269. _mesa_symbol_table_dtor(struct _mesa_symbol_table *table)
  270. {
  271. struct symbol_header *hdr;
  272. struct symbol_header *next;
  273. while (table->current_scope != NULL) {
  274. _mesa_symbol_table_pop_scope(table);
  275. }
  276. for (hdr = table->hdr; hdr != NULL; hdr = next) {
  277. next = hdr->next;
  278. _mesa_free(hdr);
  279. }
  280. hash_table_dtor(table->ht);
  281. free(table);
  282. }