d-namespace.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* Helper routines for D support in GDB.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "block.h"
  17. #include "language.h"
  18. #include "namespace.h"
  19. #include "d-lang.h"
  20. #include "gdbsupport/gdb_obstack.h"
  21. #include "gdbarch.h"
  22. /* This returns the length of first component of NAME, which should be
  23. the demangled name of a D variable/function/method/etc.
  24. Specifically, it returns the index of the first dot forming the
  25. boundary of the first component: so, given 'A.foo' or 'A.B.foo'
  26. it returns the 1, and given 'foo', it returns 0. */
  27. /* The character in NAME indexed by the return value is guaranteed to
  28. always be either '.' or '\0'. */
  29. static unsigned int
  30. d_find_first_component (const char *name)
  31. {
  32. unsigned int index = 0;
  33. for (;; ++index)
  34. {
  35. if (name[index] == '.' || name[index] == '\0')
  36. return index;
  37. }
  38. }
  39. /* If NAME is the fully-qualified name of a D function/variable/method,
  40. this returns the length of its entire prefix: all of the modules and
  41. classes that make up its name. Given 'A.foo', it returns 1, given
  42. 'A.B.foo', it returns 4, given 'foo', it returns 0. */
  43. static unsigned int
  44. d_entire_prefix_len (const char *name)
  45. {
  46. unsigned int current_len = d_find_first_component (name);
  47. unsigned int previous_len = 0;
  48. while (name[current_len] != '\0')
  49. {
  50. gdb_assert (name[current_len] == '.');
  51. previous_len = current_len;
  52. /* Skip the '.' */
  53. current_len++;
  54. current_len += d_find_first_component (name + current_len);
  55. }
  56. return previous_len;
  57. }
  58. /* Look up NAME in BLOCK's static block and in global blocks.
  59. If SEARCH is non-zero, search through base classes for a matching
  60. symbol. Other arguments are as in d_lookup_symbol_nonlocal. */
  61. static struct block_symbol
  62. d_lookup_symbol (const struct language_defn *langdef,
  63. const char *name, const struct block *block,
  64. const domain_enum domain, int search)
  65. {
  66. struct block_symbol sym;
  67. sym = lookup_symbol_in_static_block (name, block, domain);
  68. if (sym.symbol != NULL)
  69. return sym;
  70. /* If we didn't find a definition for a builtin type in the static block,
  71. such as "ucent" which is a specialist type, search for it now. */
  72. if (langdef != NULL && domain == VAR_DOMAIN)
  73. {
  74. struct gdbarch *gdbarch;
  75. if (block == NULL)
  76. gdbarch = target_gdbarch ();
  77. else
  78. gdbarch = block_gdbarch (block);
  79. sym.symbol
  80. = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
  81. sym.block = NULL;
  82. if (sym.symbol != NULL)
  83. return sym;
  84. }
  85. sym = lookup_global_symbol (name, block, domain);
  86. if (sym.symbol != NULL)
  87. return sym;
  88. if (search)
  89. {
  90. std::string classname, nested;
  91. unsigned int prefix_len;
  92. struct block_symbol class_sym;
  93. /* A simple lookup failed. Check if the symbol was defined in
  94. a base class. */
  95. /* Find the name of the class and the name of the method,
  96. variable, etc. */
  97. prefix_len = d_entire_prefix_len (name);
  98. /* If no prefix was found, search "this". */
  99. if (prefix_len == 0)
  100. {
  101. struct type *type;
  102. struct block_symbol lang_this;
  103. lang_this = lookup_language_this (language_def (language_d), block);
  104. if (lang_this.symbol == NULL)
  105. return {};
  106. type = check_typedef (TYPE_TARGET_TYPE (lang_this.symbol->type ()));
  107. classname = type->name ();
  108. nested = name;
  109. }
  110. else
  111. {
  112. /* The class name is everything up to and including PREFIX_LEN. */
  113. classname = std::string (name, prefix_len);
  114. /* The rest of the name is everything else past the initial scope
  115. operator. */
  116. nested = std::string (name + prefix_len + 1);
  117. }
  118. /* Lookup a class named CLASSNAME. If none is found, there is nothing
  119. more that can be done. */
  120. class_sym = lookup_global_symbol (classname.c_str (), block, domain);
  121. if (class_sym.symbol == NULL)
  122. return {};
  123. /* Look for a symbol named NESTED in this class. */
  124. sym = d_lookup_nested_symbol (class_sym.symbol->type (),
  125. nested.c_str (), block);
  126. }
  127. return sym;
  128. }
  129. /* Look up NAME in the D module MODULE. Other arguments are as in
  130. d_lookup_symbol_nonlocal. If SEARCH is non-zero, search through
  131. base classes for a matching symbol. */
  132. static struct block_symbol
  133. d_lookup_symbol_in_module (const char *module, const char *name,
  134. const struct block *block,
  135. const domain_enum domain, int search)
  136. {
  137. char *concatenated_name = NULL;
  138. if (module[0] != '\0')
  139. {
  140. concatenated_name
  141. = (char *) alloca (strlen (module) + strlen (name) + 2);
  142. strcpy (concatenated_name, module);
  143. strcat (concatenated_name, ".");
  144. strcat (concatenated_name, name);
  145. name = concatenated_name;
  146. }
  147. return d_lookup_symbol (NULL, name, block, domain, search);
  148. }
  149. /* Lookup NAME at module scope. SCOPE is the module that the current
  150. function is defined within; only consider modules whose length is at
  151. least SCOPE_LEN. Other arguments are as in d_lookup_symbol_nonlocal.
  152. For example, if we're within a function A.B.f and looking for a
  153. symbol x, this will get called with NAME = "x", SCOPE = "A.B", and
  154. SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
  155. but with SCOPE_LEN = 1. And then it calls itself with NAME and
  156. SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
  157. "A.B.x"; if it doesn't find it, then the second call looks for "A.x",
  158. and if that call fails, then the first call looks for "x". */
  159. static struct block_symbol
  160. lookup_module_scope (const struct language_defn *langdef,
  161. const char *name, const struct block *block,
  162. const domain_enum domain, const char *scope,
  163. int scope_len)
  164. {
  165. char *module;
  166. if (scope[scope_len] != '\0')
  167. {
  168. /* Recursively search for names in child modules first. */
  169. struct block_symbol sym;
  170. int new_scope_len = scope_len;
  171. /* If the current scope is followed by ".", skip past that. */
  172. if (new_scope_len != 0)
  173. {
  174. gdb_assert (scope[new_scope_len] == '.');
  175. new_scope_len++;
  176. }
  177. new_scope_len += d_find_first_component (scope + new_scope_len);
  178. sym = lookup_module_scope (langdef, name, block, domain,
  179. scope, new_scope_len);
  180. if (sym.symbol != NULL)
  181. return sym;
  182. }
  183. /* Okay, we didn't find a match in our children, so look for the
  184. name in the current module.
  185. If we there is no scope and we know we have a bare symbol, then short
  186. circuit everything and call d_lookup_symbol directly.
  187. This isn't an optimization, rather it allows us to pass LANGDEF which
  188. is needed for primitive type lookup. */
  189. if (scope_len == 0 && strchr (name, '.') == NULL)
  190. return d_lookup_symbol (langdef, name, block, domain, 1);
  191. module = (char *) alloca (scope_len + 1);
  192. strncpy (module, scope, scope_len);
  193. module[scope_len] = '\0';
  194. return d_lookup_symbol_in_module (module, name,
  195. block, domain, 1);
  196. }
  197. /* Search through the base classes of PARENT_TYPE for a symbol named
  198. NAME in block BLOCK. */
  199. static struct block_symbol
  200. find_symbol_in_baseclass (struct type *parent_type, const char *name,
  201. const struct block *block)
  202. {
  203. struct block_symbol sym = {};
  204. int i;
  205. for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
  206. {
  207. struct type *base_type = TYPE_BASECLASS (parent_type, i);
  208. const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
  209. if (base_name == NULL)
  210. continue;
  211. /* Search this particular base class. */
  212. sym = d_lookup_symbol_in_module (base_name, name, block,
  213. VAR_DOMAIN, 0);
  214. if (sym.symbol != NULL)
  215. break;
  216. /* Now search all static file-level symbols. We have to do this for
  217. things like typedefs in the class. First search in this symtab,
  218. what we want is possibly there. */
  219. std::string concatenated_name = std::string (base_name) + "." + name;
  220. sym = lookup_symbol_in_static_block (concatenated_name.c_str (), block,
  221. VAR_DOMAIN);
  222. if (sym.symbol != NULL)
  223. break;
  224. /* Nope. We now have to search all static blocks in all objfiles,
  225. even if block != NULL, because there's no guarantees as to which
  226. symtab the symbol we want is in. */
  227. sym = lookup_static_symbol (concatenated_name.c_str (), VAR_DOMAIN);
  228. if (sym.symbol != NULL)
  229. break;
  230. /* If this class has base classes, search them next. */
  231. base_type = check_typedef (base_type);
  232. if (TYPE_N_BASECLASSES (base_type) > 0)
  233. {
  234. sym = find_symbol_in_baseclass (base_type, name, block);
  235. if (sym.symbol != NULL)
  236. break;
  237. }
  238. }
  239. return sym;
  240. }
  241. /* Look up a symbol named NESTED_NAME that is nested inside the D
  242. class or module given by PARENT_TYPE, from within the context
  243. given by BLOCK. Return NULL if there is no such nested type. */
  244. struct block_symbol
  245. d_lookup_nested_symbol (struct type *parent_type,
  246. const char *nested_name,
  247. const struct block *block)
  248. {
  249. /* type_name_no_tag_required provides better error reporting using the
  250. original type. */
  251. struct type *saved_parent_type = parent_type;
  252. parent_type = check_typedef (parent_type);
  253. switch (parent_type->code ())
  254. {
  255. case TYPE_CODE_STRUCT:
  256. case TYPE_CODE_UNION:
  257. case TYPE_CODE_ENUM:
  258. case TYPE_CODE_MODULE:
  259. {
  260. int size;
  261. const char *parent_name = type_name_or_error (saved_parent_type);
  262. struct block_symbol sym
  263. = d_lookup_symbol_in_module (parent_name, nested_name,
  264. block, VAR_DOMAIN, 0);
  265. char *concatenated_name;
  266. if (sym.symbol != NULL)
  267. return sym;
  268. /* Now search all static file-level symbols. We have to do this
  269. for things like typedefs in the class. We do not try to
  270. guess any imported module as even the fully specified
  271. module search is already not D compliant and more assumptions
  272. could make it too magic. */
  273. size = strlen (parent_name) + strlen (nested_name) + 2;
  274. concatenated_name = (char *) alloca (size);
  275. xsnprintf (concatenated_name, size, "%s.%s",
  276. parent_name, nested_name);
  277. sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
  278. if (sym.symbol != NULL)
  279. return sym;
  280. /* If no matching symbols were found, try searching any
  281. base classes. */
  282. return find_symbol_in_baseclass (parent_type, nested_name, block);
  283. }
  284. case TYPE_CODE_FUNC:
  285. case TYPE_CODE_METHOD:
  286. return {};
  287. default:
  288. gdb_assert_not_reached ("called with non-aggregate type.");
  289. }
  290. }
  291. /* Search for NAME by applying all import statements belonging to
  292. BLOCK which are applicable in SCOPE. */
  293. static struct block_symbol
  294. d_lookup_symbol_imports (const char *scope, const char *name,
  295. const struct block *block,
  296. const domain_enum domain)
  297. {
  298. struct using_direct *current;
  299. struct block_symbol sym;
  300. /* First, try to find the symbol in the given module. */
  301. sym = d_lookup_symbol_in_module (scope, name, block, domain, 1);
  302. if (sym.symbol != NULL)
  303. return sym;
  304. /* Go through the using directives. If any of them add new names to
  305. the module we're searching in, see if we can find a match by
  306. applying them. */
  307. for (current = block_using (block);
  308. current != NULL;
  309. current = current->next)
  310. {
  311. const char **excludep;
  312. /* If the import destination is the current scope then search it. */
  313. if (!current->searched && strcmp (scope, current->import_dest) == 0)
  314. {
  315. /* Mark this import as searched so that the recursive call
  316. does not search it again. */
  317. scoped_restore restore_searched
  318. = make_scoped_restore (&current->searched, 1);
  319. /* If there is an import of a single declaration, compare the
  320. imported declaration (after optional renaming by its alias)
  321. with the sought out name. If there is a match pass
  322. current->import_src as MODULE to direct the search towards
  323. the imported module. */
  324. if (current->declaration
  325. && strcmp (name, current->alias
  326. ? current->alias : current->declaration) == 0)
  327. sym = d_lookup_symbol_in_module (current->import_src,
  328. current->declaration,
  329. block, domain, 1);
  330. /* If a symbol was found or this import statement was an import
  331. declaration, the search of this import is complete. */
  332. if (sym.symbol != NULL || current->declaration)
  333. {
  334. if (sym.symbol != NULL)
  335. return sym;
  336. continue;
  337. }
  338. /* Do not follow CURRENT if NAME matches its EXCLUDES. */
  339. for (excludep = current->excludes; *excludep; excludep++)
  340. if (strcmp (name, *excludep) == 0)
  341. break;
  342. if (*excludep)
  343. continue;
  344. /* If the import statement is creating an alias. */
  345. if (current->alias != NULL)
  346. {
  347. if (strcmp (name, current->alias) == 0)
  348. {
  349. /* If the alias matches the sought name. Pass
  350. current->import_src as the NAME to direct the
  351. search towards the aliased module. */
  352. sym = lookup_module_scope (NULL, current->import_src, block,
  353. domain, scope, 0);
  354. }
  355. else
  356. {
  357. /* If the alias matches the first component of the
  358. sought name, pass current->import_src as MODULE
  359. to direct the search, skipping over the aliased
  360. component in NAME. */
  361. int name_scope = d_find_first_component (name);
  362. if (name[name_scope] != '\0'
  363. && strncmp (name, current->alias, name_scope) == 0)
  364. {
  365. /* Skip the '.' */
  366. name_scope++;
  367. sym = d_lookup_symbol_in_module (current->import_src,
  368. name + name_scope,
  369. block, domain, 1);
  370. }
  371. }
  372. }
  373. else
  374. {
  375. /* If this import statement creates no alias, pass
  376. current->import_src as MODULE to direct the search
  377. towards the imported module. */
  378. sym = d_lookup_symbol_in_module (current->import_src,
  379. name, block, domain, 1);
  380. }
  381. if (sym.symbol != NULL)
  382. return sym;
  383. }
  384. }
  385. return {};
  386. }
  387. /* Searches for NAME in the current module, and by applying relevant
  388. import statements belonging to BLOCK and its parents. SCOPE is the
  389. module scope of the context in which the search is being evaluated. */
  390. static struct block_symbol
  391. d_lookup_symbol_module (const char *scope, const char *name,
  392. const struct block *block,
  393. const domain_enum domain)
  394. {
  395. struct block_symbol sym;
  396. /* First, try to find the symbol in the given module. */
  397. sym = d_lookup_symbol_in_module (scope, name,
  398. block, domain, 1);
  399. if (sym.symbol != NULL)
  400. return sym;
  401. /* Search for name in modules imported to this and parent
  402. blocks. */
  403. while (block != NULL)
  404. {
  405. sym = d_lookup_symbol_imports (scope, name, block, domain);
  406. if (sym.symbol != NULL)
  407. return sym;
  408. block = BLOCK_SUPERBLOCK (block);
  409. }
  410. return {};
  411. }
  412. /* The D-specific version of name lookup for static and global names
  413. This makes sure that names get looked for in all modules that are
  414. in scope. NAME is the natural name of the symbol that we're looking
  415. looking for, BLOCK is the block that we're searching within, DOMAIN
  416. says what kind of symbols we're looking for, and if SYMTAB is non-NULL,
  417. we should store the symtab where we found the symbol in it. */
  418. struct block_symbol
  419. d_lookup_symbol_nonlocal (const struct language_defn *langdef,
  420. const char *name,
  421. const struct block *block,
  422. const domain_enum domain)
  423. {
  424. struct block_symbol sym;
  425. const char *scope = block_scope (block);
  426. sym = lookup_module_scope (langdef, name, block, domain, scope, 0);
  427. if (sym.symbol != NULL)
  428. return sym;
  429. return d_lookup_symbol_module (scope, name, block, domain);
  430. }