dictionary.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /* Routines for name->symbol lookups in GDB.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
  4. Inc.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include <ctype.h>
  18. #include "gdbsupport/gdb_obstack.h"
  19. #include "symtab.h"
  20. #include "buildsym.h"
  21. #include "dictionary.h"
  22. #include "safe-ctype.h"
  23. #include <unordered_map>
  24. #include "language.h"
  25. /* This file implements dictionaries, which are tables that associate
  26. symbols to names. They are represented by an opaque type 'struct
  27. dictionary'. That type has various internal implementations, which
  28. you can choose between depending on what properties you need
  29. (e.g. fast lookup, order-preserving, expandable).
  30. Each dictionary starts with a 'virtual function table' that
  31. contains the functions that actually implement the various
  32. operations that dictionaries provide. (Note, however, that, for
  33. the sake of client code, we also provide some functions that can be
  34. implemented generically in terms of the functions in the vtable.)
  35. To add a new dictionary implementation <impl>, what you should do
  36. is:
  37. * Add a new element DICT_<IMPL> to dict_type.
  38. * Create a new structure dictionary_<impl>. If your new
  39. implementation is a variant of an existing one, make sure that
  40. their structs have the same initial data members. Define accessor
  41. macros for your new data members.
  42. * Implement all the functions in dict_vector as static functions,
  43. whose name is the same as the corresponding member of dict_vector
  44. plus _<impl>. You don't have to do this for those members where
  45. you can reuse existing generic functions
  46. (e.g. add_symbol_nonexpandable, free_obstack) or in the case where
  47. your new implementation is a variant of an existing implementation
  48. and where the variant doesn't affect the member function in
  49. question.
  50. * Define a static const struct dict_vector dict_<impl>_vector.
  51. * Define a function dict_create_<impl> to create these
  52. gizmos. Add its declaration to dictionary.h.
  53. To add a new operation <op> on all existing implementations, what
  54. you should do is:
  55. * Add a new member <op> to struct dict_vector.
  56. * If there is useful generic behavior <op>, define a static
  57. function <op>_something_informative that implements that behavior.
  58. (E.g. add_symbol_nonexpandable, free_obstack.)
  59. * For every implementation <impl> that should have its own specific
  60. behavior for <op>, define a static function <op>_<impl>
  61. implementing it.
  62. * Modify all existing dict_vector_<impl>'s to include the appropriate
  63. member.
  64. * Define a function dict_<op> that looks up <op> in the dict_vector
  65. and calls the appropriate function. Add a declaration for
  66. dict_<op> to dictionary.h. */
  67. /* An enum representing the various implementations of dictionaries.
  68. Used only for debugging. */
  69. enum dict_type
  70. {
  71. /* Symbols are stored in a fixed-size hash table. */
  72. DICT_HASHED,
  73. /* Symbols are stored in an expandable hash table. */
  74. DICT_HASHED_EXPANDABLE,
  75. /* Symbols are stored in a fixed-size array. */
  76. DICT_LINEAR,
  77. /* Symbols are stored in an expandable array. */
  78. DICT_LINEAR_EXPANDABLE
  79. };
  80. /* The virtual function table. */
  81. struct dict_vector
  82. {
  83. /* The type of the dictionary. This is only here to make debugging
  84. a bit easier; it's not actually used. */
  85. enum dict_type type;
  86. /* The function to free a dictionary. */
  87. void (*free) (struct dictionary *dict);
  88. /* Add a symbol to a dictionary, if possible. */
  89. void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
  90. /* Iterator functions. */
  91. struct symbol *(*iterator_first) (const struct dictionary *dict,
  92. struct dict_iterator *iterator);
  93. struct symbol *(*iterator_next) (struct dict_iterator *iterator);
  94. /* Functions to iterate over symbols with a given name. */
  95. struct symbol *(*iter_match_first) (const struct dictionary *dict,
  96. const lookup_name_info &name,
  97. struct dict_iterator *iterator);
  98. struct symbol *(*iter_match_next) (const lookup_name_info &name,
  99. struct dict_iterator *iterator);
  100. /* A size function, for maint print symtabs. */
  101. int (*size) (const struct dictionary *dict);
  102. };
  103. /* Now comes the structs used to store the data for different
  104. implementations. If two implementations have data in common, put
  105. the common data at the top of their structs, ordered in the same
  106. way. */
  107. struct dictionary_hashed
  108. {
  109. int nbuckets;
  110. struct symbol **buckets;
  111. };
  112. struct dictionary_hashed_expandable
  113. {
  114. /* How many buckets we currently have. */
  115. int nbuckets;
  116. struct symbol **buckets;
  117. /* How many syms we currently have; we need this so we will know
  118. when to add more buckets. */
  119. int nsyms;
  120. };
  121. struct dictionary_linear
  122. {
  123. int nsyms;
  124. struct symbol **syms;
  125. };
  126. struct dictionary_linear_expandable
  127. {
  128. /* How many symbols we currently have. */
  129. int nsyms;
  130. struct symbol **syms;
  131. /* How many symbols we can store before needing to reallocate. */
  132. int capacity;
  133. };
  134. /* And now, the star of our show. */
  135. struct dictionary
  136. {
  137. const struct language_defn *language;
  138. const struct dict_vector *vector;
  139. union
  140. {
  141. struct dictionary_hashed hashed;
  142. struct dictionary_hashed_expandable hashed_expandable;
  143. struct dictionary_linear linear;
  144. struct dictionary_linear_expandable linear_expandable;
  145. }
  146. data;
  147. };
  148. /* Accessor macros. */
  149. #define DICT_VECTOR(d) (d)->vector
  150. #define DICT_LANGUAGE(d) (d)->language
  151. /* These can be used for DICT_HASHED_EXPANDABLE, too. */
  152. #define DICT_HASHED_NBUCKETS(d) (d)->data.hashed.nbuckets
  153. #define DICT_HASHED_BUCKETS(d) (d)->data.hashed.buckets
  154. #define DICT_HASHED_BUCKET(d,i) DICT_HASHED_BUCKETS (d) [i]
  155. #define DICT_HASHED_EXPANDABLE_NSYMS(d) (d)->data.hashed_expandable.nsyms
  156. /* These can be used for DICT_LINEAR_EXPANDABLEs, too. */
  157. #define DICT_LINEAR_NSYMS(d) (d)->data.linear.nsyms
  158. #define DICT_LINEAR_SYMS(d) (d)->data.linear.syms
  159. #define DICT_LINEAR_SYM(d,i) DICT_LINEAR_SYMS (d) [i]
  160. #define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
  161. (d)->data.linear_expandable.capacity
  162. /* The initial size of a DICT_*_EXPANDABLE dictionary. */
  163. #define DICT_EXPANDABLE_INITIAL_CAPACITY 10
  164. /* This calculates the number of buckets we'll use in a hashtable,
  165. given the number of symbols that it will contain. */
  166. #define DICT_HASHTABLE_SIZE(n) ((n)/5 + 1)
  167. /* Accessor macros for dict_iterators; they're here rather than
  168. dictionary.h because code elsewhere should treat dict_iterators as
  169. opaque. */
  170. /* The dictionary that the iterator is associated to. */
  171. #define DICT_ITERATOR_DICT(iter) (iter)->dict
  172. /* For linear dictionaries, the index of the last symbol returned; for
  173. hashed dictionaries, the bucket of the last symbol returned. */
  174. #define DICT_ITERATOR_INDEX(iter) (iter)->index
  175. /* For hashed dictionaries, this points to the last symbol returned;
  176. otherwise, this is unused. */
  177. #define DICT_ITERATOR_CURRENT(iter) (iter)->current
  178. /* Declarations of functions for vectors. */
  179. /* Functions that might work across a range of dictionary types. */
  180. static void add_symbol_nonexpandable (struct dictionary *dict,
  181. struct symbol *sym);
  182. static void free_obstack (struct dictionary *dict);
  183. /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
  184. dictionaries. */
  185. static struct symbol *iterator_first_hashed (const struct dictionary *dict,
  186. struct dict_iterator *iterator);
  187. static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);
  188. static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
  189. const lookup_name_info &name,
  190. struct dict_iterator *iterator);
  191. static struct symbol *iter_match_next_hashed (const lookup_name_info &name,
  192. struct dict_iterator *iterator);
  193. /* Functions only for DICT_HASHED. */
  194. static int size_hashed (const struct dictionary *dict);
  195. /* Functions only for DICT_HASHED_EXPANDABLE. */
  196. static void free_hashed_expandable (struct dictionary *dict);
  197. static void add_symbol_hashed_expandable (struct dictionary *dict,
  198. struct symbol *sym);
  199. static int size_hashed_expandable (const struct dictionary *dict);
  200. /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
  201. dictionaries. */
  202. static struct symbol *iterator_first_linear (const struct dictionary *dict,
  203. struct dict_iterator *iterator);
  204. static struct symbol *iterator_next_linear (struct dict_iterator *iterator);
  205. static struct symbol *iter_match_first_linear (const struct dictionary *dict,
  206. const lookup_name_info &name,
  207. struct dict_iterator *iterator);
  208. static struct symbol *iter_match_next_linear (const lookup_name_info &name,
  209. struct dict_iterator *iterator);
  210. static int size_linear (const struct dictionary *dict);
  211. /* Functions only for DICT_LINEAR_EXPANDABLE. */
  212. static void free_linear_expandable (struct dictionary *dict);
  213. static void add_symbol_linear_expandable (struct dictionary *dict,
  214. struct symbol *sym);
  215. /* Various vectors that we'll actually use. */
  216. static const struct dict_vector dict_hashed_vector =
  217. {
  218. DICT_HASHED, /* type */
  219. free_obstack, /* free */
  220. add_symbol_nonexpandable, /* add_symbol */
  221. iterator_first_hashed, /* iterator_first */
  222. iterator_next_hashed, /* iterator_next */
  223. iter_match_first_hashed, /* iter_name_first */
  224. iter_match_next_hashed, /* iter_name_next */
  225. size_hashed, /* size */
  226. };
  227. static const struct dict_vector dict_hashed_expandable_vector =
  228. {
  229. DICT_HASHED_EXPANDABLE, /* type */
  230. free_hashed_expandable, /* free */
  231. add_symbol_hashed_expandable, /* add_symbol */
  232. iterator_first_hashed, /* iterator_first */
  233. iterator_next_hashed, /* iterator_next */
  234. iter_match_first_hashed, /* iter_name_first */
  235. iter_match_next_hashed, /* iter_name_next */
  236. size_hashed_expandable, /* size */
  237. };
  238. static const struct dict_vector dict_linear_vector =
  239. {
  240. DICT_LINEAR, /* type */
  241. free_obstack, /* free */
  242. add_symbol_nonexpandable, /* add_symbol */
  243. iterator_first_linear, /* iterator_first */
  244. iterator_next_linear, /* iterator_next */
  245. iter_match_first_linear, /* iter_name_first */
  246. iter_match_next_linear, /* iter_name_next */
  247. size_linear, /* size */
  248. };
  249. static const struct dict_vector dict_linear_expandable_vector =
  250. {
  251. DICT_LINEAR_EXPANDABLE, /* type */
  252. free_linear_expandable, /* free */
  253. add_symbol_linear_expandable, /* add_symbol */
  254. iterator_first_linear, /* iterator_first */
  255. iterator_next_linear, /* iterator_next */
  256. iter_match_first_linear, /* iter_name_first */
  257. iter_match_next_linear, /* iter_name_next */
  258. size_linear, /* size */
  259. };
  260. /* Declarations of helper functions (i.e. ones that don't go into
  261. vectors). */
  262. static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
  263. static void insert_symbol_hashed (struct dictionary *dict,
  264. struct symbol *sym);
  265. static void expand_hashtable (struct dictionary *dict);
  266. /* The creation functions. */
  267. /* Create a hashed dictionary of a given language. */
  268. static struct dictionary *
  269. dict_create_hashed (struct obstack *obstack,
  270. enum language language,
  271. const std::vector<symbol *> &symbol_list)
  272. {
  273. /* Allocate the dictionary. */
  274. struct dictionary *retval = XOBNEW (obstack, struct dictionary);
  275. DICT_VECTOR (retval) = &dict_hashed_vector;
  276. DICT_LANGUAGE (retval) = language_def (language);
  277. /* Allocate space for symbols. */
  278. int nsyms = symbol_list.size ();
  279. int nbuckets = DICT_HASHTABLE_SIZE (nsyms);
  280. DICT_HASHED_NBUCKETS (retval) = nbuckets;
  281. struct symbol **buckets = XOBNEWVEC (obstack, struct symbol *, nbuckets);
  282. memset (buckets, 0, nbuckets * sizeof (struct symbol *));
  283. DICT_HASHED_BUCKETS (retval) = buckets;
  284. /* Now fill the buckets. */
  285. for (const auto &sym : symbol_list)
  286. insert_symbol_hashed (retval, sym);
  287. return retval;
  288. }
  289. /* Create an expandable hashed dictionary of a given language. */
  290. static struct dictionary *
  291. dict_create_hashed_expandable (enum language language)
  292. {
  293. struct dictionary *retval = XNEW (struct dictionary);
  294. DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
  295. DICT_LANGUAGE (retval) = language_def (language);
  296. DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
  297. DICT_HASHED_BUCKETS (retval) = XCNEWVEC (struct symbol *,
  298. DICT_EXPANDABLE_INITIAL_CAPACITY);
  299. DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
  300. return retval;
  301. }
  302. /* Create a linear dictionary of a given language. */
  303. static struct dictionary *
  304. dict_create_linear (struct obstack *obstack,
  305. enum language language,
  306. const std::vector<symbol *> &symbol_list)
  307. {
  308. struct dictionary *retval = XOBNEW (obstack, struct dictionary);
  309. DICT_VECTOR (retval) = &dict_linear_vector;
  310. DICT_LANGUAGE (retval) = language_def (language);
  311. /* Allocate space for symbols. */
  312. int nsyms = symbol_list.size ();
  313. DICT_LINEAR_NSYMS (retval) = nsyms;
  314. struct symbol **syms = XOBNEWVEC (obstack, struct symbol *, nsyms);
  315. DICT_LINEAR_SYMS (retval) = syms;
  316. /* Now fill in the symbols. */
  317. int idx = nsyms - 1;
  318. for (const auto &sym : symbol_list)
  319. syms[idx--] = sym;
  320. return retval;
  321. }
  322. /* Create an expandable linear dictionary of a given language. */
  323. static struct dictionary *
  324. dict_create_linear_expandable (enum language language)
  325. {
  326. struct dictionary *retval = XNEW (struct dictionary);
  327. DICT_VECTOR (retval) = &dict_linear_expandable_vector;
  328. DICT_LANGUAGE (retval) = language_def (language);
  329. DICT_LINEAR_NSYMS (retval) = 0;
  330. DICT_LINEAR_EXPANDABLE_CAPACITY (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
  331. DICT_LINEAR_SYMS (retval)
  332. = XNEWVEC (struct symbol *, DICT_LINEAR_EXPANDABLE_CAPACITY (retval));
  333. return retval;
  334. }
  335. /* The functions providing the dictionary interface. */
  336. /* Free the memory used by a dictionary that's not on an obstack. (If
  337. any.) */
  338. static void
  339. dict_free (struct dictionary *dict)
  340. {
  341. (DICT_VECTOR (dict))->free (dict);
  342. }
  343. /* Add SYM to DICT. DICT had better be expandable. */
  344. static void
  345. dict_add_symbol (struct dictionary *dict, struct symbol *sym)
  346. {
  347. (DICT_VECTOR (dict))->add_symbol (dict, sym);
  348. }
  349. /* Utility to add a list of symbols to a dictionary.
  350. DICT must be an expandable dictionary. */
  351. static void
  352. dict_add_pending (struct dictionary *dict,
  353. const std::vector<symbol *> &symbol_list)
  354. {
  355. /* Preserve ordering by reversing the list. */
  356. for (auto sym = symbol_list.rbegin (); sym != symbol_list.rend (); ++sym)
  357. dict_add_symbol (dict, *sym);
  358. }
  359. /* Initialize ITERATOR to point at the first symbol in DICT, and
  360. return that first symbol, or NULL if DICT is empty. */
  361. static struct symbol *
  362. dict_iterator_first (const struct dictionary *dict,
  363. struct dict_iterator *iterator)
  364. {
  365. return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
  366. }
  367. /* Advance ITERATOR, and return the next symbol, or NULL if there are
  368. no more symbols. */
  369. static struct symbol *
  370. dict_iterator_next (struct dict_iterator *iterator)
  371. {
  372. return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
  373. ->iterator_next (iterator);
  374. }
  375. static struct symbol *
  376. dict_iter_match_first (const struct dictionary *dict,
  377. const lookup_name_info &name,
  378. struct dict_iterator *iterator)
  379. {
  380. return (DICT_VECTOR (dict))->iter_match_first (dict, name, iterator);
  381. }
  382. static struct symbol *
  383. dict_iter_match_next (const lookup_name_info &name,
  384. struct dict_iterator *iterator)
  385. {
  386. return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
  387. ->iter_match_next (name, iterator);
  388. }
  389. static int
  390. dict_size (const struct dictionary *dict)
  391. {
  392. return (DICT_VECTOR (dict))->size (dict);
  393. }
  394. /* Now come functions (well, one function, currently) that are
  395. implemented generically by means of the vtable. Typically, they're
  396. rarely used. */
  397. /* The functions implementing the dictionary interface. */
  398. /* Generic functions, where appropriate. */
  399. static void
  400. free_obstack (struct dictionary *dict)
  401. {
  402. /* Do nothing! */
  403. }
  404. static void
  405. add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
  406. {
  407. internal_error (__FILE__, __LINE__,
  408. _("dict_add_symbol: non-expandable dictionary"));
  409. }
  410. /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
  411. static struct symbol *
  412. iterator_first_hashed (const struct dictionary *dict,
  413. struct dict_iterator *iterator)
  414. {
  415. DICT_ITERATOR_DICT (iterator) = dict;
  416. DICT_ITERATOR_INDEX (iterator) = -1;
  417. return iterator_hashed_advance (iterator);
  418. }
  419. static struct symbol *
  420. iterator_next_hashed (struct dict_iterator *iterator)
  421. {
  422. struct symbol *next;
  423. next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
  424. if (next == NULL)
  425. return iterator_hashed_advance (iterator);
  426. else
  427. {
  428. DICT_ITERATOR_CURRENT (iterator) = next;
  429. return next;
  430. }
  431. }
  432. static struct symbol *
  433. iterator_hashed_advance (struct dict_iterator *iterator)
  434. {
  435. const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
  436. int nbuckets = DICT_HASHED_NBUCKETS (dict);
  437. int i;
  438. for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
  439. {
  440. struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
  441. if (sym != NULL)
  442. {
  443. DICT_ITERATOR_INDEX (iterator) = i;
  444. DICT_ITERATOR_CURRENT (iterator) = sym;
  445. return sym;
  446. }
  447. }
  448. return NULL;
  449. }
  450. static struct symbol *
  451. iter_match_first_hashed (const struct dictionary *dict,
  452. const lookup_name_info &name,
  453. struct dict_iterator *iterator)
  454. {
  455. const language_defn *lang = DICT_LANGUAGE (dict);
  456. unsigned int hash_index = (name.search_name_hash (lang->la_language)
  457. % DICT_HASHED_NBUCKETS (dict));
  458. symbol_name_matcher_ftype *matches_name
  459. = lang->get_symbol_name_matcher (name);
  460. struct symbol *sym;
  461. DICT_ITERATOR_DICT (iterator) = dict;
  462. /* Loop through the symbols in the given bucket, breaking when SYM
  463. first matches. If SYM never matches, it will be set to NULL;
  464. either way, we have the right return value. */
  465. for (sym = DICT_HASHED_BUCKET (dict, hash_index);
  466. sym != NULL;
  467. sym = sym->hash_next)
  468. {
  469. /* Warning: the order of arguments to compare matters! */
  470. if (matches_name (sym->search_name (), name, NULL))
  471. break;
  472. }
  473. DICT_ITERATOR_CURRENT (iterator) = sym;
  474. return sym;
  475. }
  476. static struct symbol *
  477. iter_match_next_hashed (const lookup_name_info &name,
  478. struct dict_iterator *iterator)
  479. {
  480. const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
  481. symbol_name_matcher_ftype *matches_name
  482. = lang->get_symbol_name_matcher (name);
  483. struct symbol *next;
  484. for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
  485. next != NULL;
  486. next = next->hash_next)
  487. {
  488. if (matches_name (next->search_name (), name, NULL))
  489. break;
  490. }
  491. DICT_ITERATOR_CURRENT (iterator) = next;
  492. return next;
  493. }
  494. /* Insert SYM into DICT. */
  495. static void
  496. insert_symbol_hashed (struct dictionary *dict,
  497. struct symbol *sym)
  498. {
  499. unsigned int hash_index;
  500. unsigned int hash;
  501. struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
  502. /* We don't want to insert a symbol into a dictionary of a different
  503. language. The two may not use the same hashing algorithm. */
  504. gdb_assert (sym->language () == DICT_LANGUAGE (dict)->la_language);
  505. hash = search_name_hash (sym->language (), sym->search_name ());
  506. hash_index = hash % DICT_HASHED_NBUCKETS (dict);
  507. sym->hash_next = buckets[hash_index];
  508. buckets[hash_index] = sym;
  509. }
  510. static int
  511. size_hashed (const struct dictionary *dict)
  512. {
  513. return DICT_HASHED_NBUCKETS (dict);
  514. }
  515. /* Functions only for DICT_HASHED_EXPANDABLE. */
  516. static void
  517. free_hashed_expandable (struct dictionary *dict)
  518. {
  519. xfree (DICT_HASHED_BUCKETS (dict));
  520. xfree (dict);
  521. }
  522. static void
  523. add_symbol_hashed_expandable (struct dictionary *dict,
  524. struct symbol *sym)
  525. {
  526. int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
  527. if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
  528. expand_hashtable (dict);
  529. insert_symbol_hashed (dict, sym);
  530. DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
  531. }
  532. static int
  533. size_hashed_expandable (const struct dictionary *dict)
  534. {
  535. return DICT_HASHED_EXPANDABLE_NSYMS (dict);
  536. }
  537. static void
  538. expand_hashtable (struct dictionary *dict)
  539. {
  540. int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
  541. struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
  542. int new_nbuckets = 2 * old_nbuckets + 1;
  543. struct symbol **new_buckets = XCNEWVEC (struct symbol *, new_nbuckets);
  544. int i;
  545. DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
  546. DICT_HASHED_BUCKETS (dict) = new_buckets;
  547. for (i = 0; i < old_nbuckets; ++i)
  548. {
  549. struct symbol *sym, *next_sym;
  550. sym = old_buckets[i];
  551. if (sym != NULL)
  552. {
  553. for (next_sym = sym->hash_next;
  554. next_sym != NULL;
  555. next_sym = sym->hash_next)
  556. {
  557. insert_symbol_hashed (dict, sym);
  558. sym = next_sym;
  559. }
  560. insert_symbol_hashed (dict, sym);
  561. }
  562. }
  563. xfree (old_buckets);
  564. }
  565. /* See dictionary.h. */
  566. unsigned int
  567. language_defn::search_name_hash (const char *string0) const
  568. {
  569. /* The Ada-encoded version of a name P1.P2...Pn has either the form
  570. P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
  571. are lower-cased identifiers). The <suffix> (which can be empty)
  572. encodes additional information about the denoted entity. This
  573. routine hashes such names to msymbol_hash_iw(Pn). It actually
  574. does this for a superset of both valid Pi and of <suffix>, but
  575. in other cases it simply returns msymbol_hash_iw(STRING0). */
  576. const char *string;
  577. unsigned int hash;
  578. string = string0;
  579. if (*string == '_')
  580. {
  581. if (startswith (string, "_ada_"))
  582. string += 5;
  583. else
  584. return msymbol_hash_iw (string0);
  585. }
  586. hash = 0;
  587. while (*string)
  588. {
  589. switch (*string)
  590. {
  591. case '$':
  592. case '.':
  593. case 'X':
  594. if (string0 == string)
  595. return msymbol_hash_iw (string0);
  596. else
  597. return hash;
  598. case ' ':
  599. case '(':
  600. return msymbol_hash_iw (string0);
  601. case '_':
  602. if (string[1] == '_' && string != string0)
  603. {
  604. int c = string[2];
  605. if (c == 'B' && string[3] == '_')
  606. {
  607. for (string += 4; ISDIGIT (*string); ++string)
  608. ;
  609. continue;
  610. }
  611. if ((c < 'a' || c > 'z') && c != 'O')
  612. return hash;
  613. hash = 0;
  614. string += 2;
  615. continue;
  616. }
  617. break;
  618. case 'T':
  619. /* Ignore "TKB" suffixes.
  620. These are used by Ada for subprograms implementing a task body.
  621. For instance for a task T inside package Pck, the name of the
  622. subprogram implementing T's body is `pck__tTKB'. We need to
  623. ignore the "TKB" suffix because searches for this task body
  624. subprogram are going to be performed using `pck__t' (the encoded
  625. version of the natural name `pck.t'). */
  626. if (strcmp (string, "TKB") == 0)
  627. return hash;
  628. break;
  629. }
  630. hash = SYMBOL_HASH_NEXT (hash, *string);
  631. string += 1;
  632. }
  633. return hash;
  634. }
  635. /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
  636. static struct symbol *
  637. iterator_first_linear (const struct dictionary *dict,
  638. struct dict_iterator *iterator)
  639. {
  640. DICT_ITERATOR_DICT (iterator) = dict;
  641. DICT_ITERATOR_INDEX (iterator) = 0;
  642. return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
  643. }
  644. static struct symbol *
  645. iterator_next_linear (struct dict_iterator *iterator)
  646. {
  647. const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
  648. if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
  649. return NULL;
  650. else
  651. return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
  652. }
  653. static struct symbol *
  654. iter_match_first_linear (const struct dictionary *dict,
  655. const lookup_name_info &name,
  656. struct dict_iterator *iterator)
  657. {
  658. DICT_ITERATOR_DICT (iterator) = dict;
  659. DICT_ITERATOR_INDEX (iterator) = -1;
  660. return iter_match_next_linear (name, iterator);
  661. }
  662. static struct symbol *
  663. iter_match_next_linear (const lookup_name_info &name,
  664. struct dict_iterator *iterator)
  665. {
  666. const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
  667. const language_defn *lang = DICT_LANGUAGE (dict);
  668. symbol_name_matcher_ftype *matches_name
  669. = lang->get_symbol_name_matcher (name);
  670. int i, nsyms = DICT_LINEAR_NSYMS (dict);
  671. struct symbol *sym, *retval = NULL;
  672. for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
  673. {
  674. sym = DICT_LINEAR_SYM (dict, i);
  675. if (matches_name (sym->search_name (), name, NULL))
  676. {
  677. retval = sym;
  678. break;
  679. }
  680. }
  681. DICT_ITERATOR_INDEX (iterator) = i;
  682. return retval;
  683. }
  684. static int
  685. size_linear (const struct dictionary *dict)
  686. {
  687. return DICT_LINEAR_NSYMS (dict);
  688. }
  689. /* Functions only for DICT_LINEAR_EXPANDABLE. */
  690. static void
  691. free_linear_expandable (struct dictionary *dict)
  692. {
  693. xfree (DICT_LINEAR_SYMS (dict));
  694. xfree (dict);
  695. }
  696. static void
  697. add_symbol_linear_expandable (struct dictionary *dict,
  698. struct symbol *sym)
  699. {
  700. int nsyms = ++DICT_LINEAR_NSYMS (dict);
  701. /* Do we have enough room? If not, grow it. */
  702. if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
  703. {
  704. DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
  705. DICT_LINEAR_SYMS (dict)
  706. = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
  707. DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
  708. }
  709. DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
  710. }
  711. /* Multi-language dictionary support. */
  712. /* The structure describing a multi-language dictionary. */
  713. struct multidictionary
  714. {
  715. /* An array of dictionaries, one per language. All dictionaries
  716. must be of the same type. This should be free'd for expandable
  717. dictionary types. */
  718. struct dictionary **dictionaries;
  719. /* The number of language dictionaries currently allocated.
  720. Only used for expandable dictionaries. */
  721. unsigned short n_allocated_dictionaries;
  722. };
  723. /* A hasher for enum language. Injecting this into std is a convenience
  724. when using unordered_map with C++11. */
  725. namespace std
  726. {
  727. template<> struct hash<enum language>
  728. {
  729. typedef enum language argument_type;
  730. typedef std::size_t result_type;
  731. result_type operator() (const argument_type &l) const noexcept
  732. {
  733. return static_cast<result_type> (l);
  734. }
  735. };
  736. } /* namespace std */
  737. /* A helper function to collate symbols on the pending list by language. */
  738. static std::unordered_map<enum language, std::vector<symbol *>>
  739. collate_pending_symbols_by_language (const struct pending *symbol_list)
  740. {
  741. std::unordered_map<enum language, std::vector<symbol *>> nsyms;
  742. for (const pending *list_counter = symbol_list;
  743. list_counter != nullptr; list_counter = list_counter->next)
  744. {
  745. for (int i = list_counter->nsyms - 1; i >= 0; --i)
  746. {
  747. enum language language = list_counter->symbol[i]->language ();
  748. nsyms[language].push_back (list_counter->symbol[i]);
  749. }
  750. }
  751. return nsyms;
  752. }
  753. /* See dictionary.h. */
  754. struct multidictionary *
  755. mdict_create_hashed (struct obstack *obstack,
  756. const struct pending *symbol_list)
  757. {
  758. struct multidictionary *retval
  759. = XOBNEW (obstack, struct multidictionary);
  760. std::unordered_map<enum language, std::vector<symbol *>> nsyms
  761. = collate_pending_symbols_by_language (symbol_list);
  762. /* Loop over all languages and create/populate dictionaries. */
  763. retval->dictionaries
  764. = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
  765. retval->n_allocated_dictionaries = nsyms.size ();
  766. int idx = 0;
  767. for (const auto &pair : nsyms)
  768. {
  769. enum language language = pair.first;
  770. std::vector<symbol *> symlist = pair.second;
  771. retval->dictionaries[idx++]
  772. = dict_create_hashed (obstack, language, symlist);
  773. }
  774. return retval;
  775. }
  776. /* See dictionary.h. */
  777. struct multidictionary *
  778. mdict_create_hashed_expandable (enum language language)
  779. {
  780. struct multidictionary *retval = XNEW (struct multidictionary);
  781. /* We have no symbol list to populate, but we create an empty
  782. dictionary of the requested language to populate later. */
  783. retval->n_allocated_dictionaries = 1;
  784. retval->dictionaries = XNEW (struct dictionary *);
  785. retval->dictionaries[0] = dict_create_hashed_expandable (language);
  786. return retval;
  787. }
  788. /* See dictionary.h. */
  789. struct multidictionary *
  790. mdict_create_linear (struct obstack *obstack,
  791. const struct pending *symbol_list)
  792. {
  793. struct multidictionary *retval
  794. = XOBNEW (obstack, struct multidictionary);
  795. std::unordered_map<enum language, std::vector<symbol *>> nsyms
  796. = collate_pending_symbols_by_language (symbol_list);
  797. /* Loop over all languages and create/populate dictionaries. */
  798. retval->dictionaries
  799. = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
  800. retval->n_allocated_dictionaries = nsyms.size ();
  801. int idx = 0;
  802. for (const auto &pair : nsyms)
  803. {
  804. enum language language = pair.first;
  805. std::vector<symbol *> symlist = pair.second;
  806. retval->dictionaries[idx++]
  807. = dict_create_linear (obstack, language, symlist);
  808. }
  809. return retval;
  810. }
  811. /* See dictionary.h. */
  812. struct multidictionary *
  813. mdict_create_linear_expandable (enum language language)
  814. {
  815. struct multidictionary *retval = XNEW (struct multidictionary);
  816. /* We have no symbol list to populate, but we create an empty
  817. dictionary to populate later. */
  818. retval->n_allocated_dictionaries = 1;
  819. retval->dictionaries = XNEW (struct dictionary *);
  820. retval->dictionaries[0] = dict_create_linear_expandable (language);
  821. return retval;
  822. }
  823. /* See dictionary.h. */
  824. void
  825. mdict_free (struct multidictionary *mdict)
  826. {
  827. /* Grab the type of dictionary being used. */
  828. enum dict_type type = mdict->dictionaries[0]->vector->type;
  829. /* Loop over all dictionaries and free them. */
  830. for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
  831. dict_free (mdict->dictionaries[idx]);
  832. /* Free the dictionary list, if needed. */
  833. switch (type)
  834. {
  835. case DICT_HASHED:
  836. case DICT_LINEAR:
  837. /* Memory was allocated on an obstack when created. */
  838. break;
  839. case DICT_HASHED_EXPANDABLE:
  840. case DICT_LINEAR_EXPANDABLE:
  841. xfree (mdict->dictionaries);
  842. break;
  843. }
  844. }
  845. /* Helper function to find the dictionary associated with LANGUAGE
  846. or NULL if there is no dictionary of that language. */
  847. static struct dictionary *
  848. find_language_dictionary (const struct multidictionary *mdict,
  849. enum language language)
  850. {
  851. for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
  852. {
  853. if (DICT_LANGUAGE (mdict->dictionaries[idx])->la_language == language)
  854. return mdict->dictionaries[idx];
  855. }
  856. return nullptr;
  857. }
  858. /* Create a new language dictionary for LANGUAGE and add it to the
  859. multidictionary MDICT's list of dictionaries. If MDICT is not
  860. based on expandable dictionaries, this function throws an
  861. internal error. */
  862. static struct dictionary *
  863. create_new_language_dictionary (struct multidictionary *mdict,
  864. enum language language)
  865. {
  866. struct dictionary *retval = nullptr;
  867. /* We use the first dictionary entry to decide what create function
  868. to call. Not optimal but sufficient. */
  869. gdb_assert (mdict->dictionaries[0] != nullptr);
  870. switch (mdict->dictionaries[0]->vector->type)
  871. {
  872. case DICT_HASHED:
  873. case DICT_LINEAR:
  874. internal_error (__FILE__, __LINE__,
  875. _("create_new_language_dictionary: attempted to expand "
  876. "non-expandable multidictionary"));
  877. case DICT_HASHED_EXPANDABLE:
  878. retval = dict_create_hashed_expandable (language);
  879. break;
  880. case DICT_LINEAR_EXPANDABLE:
  881. retval = dict_create_linear_expandable (language);
  882. break;
  883. }
  884. /* Grow the dictionary vector and save the new dictionary. */
  885. mdict->dictionaries
  886. = (struct dictionary **) xrealloc (mdict->dictionaries,
  887. (++mdict->n_allocated_dictionaries
  888. * sizeof (struct dictionary *)));
  889. mdict->dictionaries[mdict->n_allocated_dictionaries - 1] = retval;
  890. return retval;
  891. }
  892. /* See dictionary.h. */
  893. void
  894. mdict_add_symbol (struct multidictionary *mdict, struct symbol *sym)
  895. {
  896. struct dictionary *dict
  897. = find_language_dictionary (mdict, sym->language ());
  898. if (dict == nullptr)
  899. {
  900. /* SYM is of a new language that we haven't previously seen.
  901. Create a new dictionary for it. */
  902. dict = create_new_language_dictionary (mdict, sym->language ());
  903. }
  904. dict_add_symbol (dict, sym);
  905. }
  906. /* See dictionary.h. */
  907. void
  908. mdict_add_pending (struct multidictionary *mdict,
  909. const struct pending *symbol_list)
  910. {
  911. std::unordered_map<enum language, std::vector<symbol *>> nsyms
  912. = collate_pending_symbols_by_language (symbol_list);
  913. for (const auto &pair : nsyms)
  914. {
  915. enum language language = pair.first;
  916. std::vector<symbol *> symlist = pair.second;
  917. struct dictionary *dict = find_language_dictionary (mdict, language);
  918. if (dict == nullptr)
  919. {
  920. /* The language was not previously seen. Create a new dictionary
  921. for it. */
  922. dict = create_new_language_dictionary (mdict, language);
  923. }
  924. dict_add_pending (dict, symlist);
  925. }
  926. }
  927. /* See dictionary.h. */
  928. struct symbol *
  929. mdict_iterator_first (const multidictionary *mdict,
  930. struct mdict_iterator *miterator)
  931. {
  932. miterator->mdict = mdict;
  933. miterator->current_idx = 0;
  934. for (unsigned short idx = miterator->current_idx;
  935. idx < mdict->n_allocated_dictionaries; ++idx)
  936. {
  937. struct symbol *result
  938. = dict_iterator_first (mdict->dictionaries[idx], &miterator->iterator);
  939. if (result != nullptr)
  940. {
  941. miterator->current_idx = idx;
  942. return result;
  943. }
  944. }
  945. return nullptr;
  946. }
  947. /* See dictionary.h. */
  948. struct symbol *
  949. mdict_iterator_next (struct mdict_iterator *miterator)
  950. {
  951. struct symbol *result = dict_iterator_next (&miterator->iterator);
  952. if (result != nullptr)
  953. return result;
  954. /* The current dictionary had no matches -- move to the next
  955. dictionary, if any. */
  956. for (unsigned short idx = ++miterator->current_idx;
  957. idx < miterator->mdict->n_allocated_dictionaries; ++idx)
  958. {
  959. result
  960. = dict_iterator_first (miterator->mdict->dictionaries[idx],
  961. &miterator->iterator);
  962. if (result != nullptr)
  963. {
  964. miterator->current_idx = idx;
  965. return result;
  966. }
  967. }
  968. return nullptr;
  969. }
  970. /* See dictionary.h. */
  971. struct symbol *
  972. mdict_iter_match_first (const struct multidictionary *mdict,
  973. const lookup_name_info &name,
  974. struct mdict_iterator *miterator)
  975. {
  976. miterator->mdict = mdict;
  977. miterator->current_idx = 0;
  978. for (unsigned short idx = miterator->current_idx;
  979. idx < mdict->n_allocated_dictionaries; ++idx)
  980. {
  981. struct symbol *result
  982. = dict_iter_match_first (mdict->dictionaries[idx], name,
  983. &miterator->iterator);
  984. if (result != nullptr)
  985. return result;
  986. }
  987. return nullptr;
  988. }
  989. /* See dictionary.h. */
  990. struct symbol *
  991. mdict_iter_match_next (const lookup_name_info &name,
  992. struct mdict_iterator *miterator)
  993. {
  994. /* Search the current dictionary. */
  995. struct symbol *result = dict_iter_match_next (name, &miterator->iterator);
  996. if (result != nullptr)
  997. return result;
  998. /* The current dictionary had no matches -- move to the next
  999. dictionary, if any. */
  1000. for (unsigned short idx = ++miterator->current_idx;
  1001. idx < miterator->mdict->n_allocated_dictionaries; ++idx)
  1002. {
  1003. result
  1004. = dict_iter_match_first (miterator->mdict->dictionaries[idx],
  1005. name, &miterator->iterator);
  1006. if (result != nullptr)
  1007. {
  1008. miterator->current_idx = idx;
  1009. return result;
  1010. }
  1011. }
  1012. return nullptr;
  1013. }
  1014. /* See dictionary.h. */
  1015. int
  1016. mdict_size (const struct multidictionary *mdict)
  1017. {
  1018. int size = 0;
  1019. for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
  1020. size += dict_size (mdict->dictionaries[idx]);
  1021. return size;
  1022. }