cp-namespace.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /* Helper routines for C++ support in GDB.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. Contributed by David Carlton and by Kealia, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "cp-support.h"
  17. #include "gdbsupport/gdb_obstack.h"
  18. #include "symtab.h"
  19. #include "symfile.h"
  20. #include "block.h"
  21. #include "objfiles.h"
  22. #include "gdbtypes.h"
  23. #include "dictionary.h"
  24. #include "command.h"
  25. #include "frame.h"
  26. #include "buildsym.h"
  27. #include "language.h"
  28. #include "namespace.h"
  29. #include <string>
  30. static struct block_symbol
  31. cp_lookup_nested_symbol_1 (struct type *container_type,
  32. const char *nested_name,
  33. const char *concatenated_name,
  34. const struct block *block,
  35. const domain_enum domain,
  36. int basic_lookup, int is_in_anonymous);
  37. static struct type *cp_lookup_transparent_type_loop (const char *name,
  38. const char *scope,
  39. int scope_len);
  40. /* Check to see if SYMBOL refers to an object contained within an
  41. anonymous namespace; if so, add an appropriate using directive. */
  42. void
  43. cp_scan_for_anonymous_namespaces (struct buildsym_compunit *compunit,
  44. const struct symbol *const symbol,
  45. struct objfile *const objfile)
  46. {
  47. if (symbol->demangled_name () != NULL)
  48. {
  49. const char *name = symbol->demangled_name ();
  50. unsigned int previous_component;
  51. unsigned int next_component;
  52. /* Start with a quick-and-dirty check for mention of "(anonymous
  53. namespace)". */
  54. if (!cp_is_in_anonymous (name))
  55. return;
  56. previous_component = 0;
  57. next_component = cp_find_first_component (name + previous_component);
  58. while (name[next_component] == ':')
  59. {
  60. if (((next_component - previous_component)
  61. == CP_ANONYMOUS_NAMESPACE_LEN)
  62. && strncmp (name + previous_component,
  63. CP_ANONYMOUS_NAMESPACE_STR,
  64. CP_ANONYMOUS_NAMESPACE_LEN) == 0)
  65. {
  66. int dest_len = (previous_component == 0
  67. ? 0 : previous_component - 2);
  68. int src_len = next_component;
  69. char *dest = (char *) alloca (dest_len + 1);
  70. char *src = (char *) alloca (src_len + 1);
  71. memcpy (dest, name, dest_len);
  72. memcpy (src, name, src_len);
  73. dest[dest_len] = '\0';
  74. src[src_len] = '\0';
  75. /* We've found a component of the name that's an
  76. anonymous namespace. So add symbols in it to the
  77. namespace given by the previous component if there is
  78. one, or to the global namespace if there isn't. */
  79. std::vector<const char *> excludes;
  80. add_using_directive (compunit->get_local_using_directives (),
  81. dest, src, NULL, NULL, excludes,
  82. 1, &objfile->objfile_obstack);
  83. }
  84. /* The "+ 2" is for the "::". */
  85. previous_component = next_component + 2;
  86. next_component = (previous_component
  87. + cp_find_first_component (name
  88. + previous_component));
  89. }
  90. }
  91. }
  92. /* Test whether or not NAMESPACE looks like it mentions an anonymous
  93. namespace; return nonzero if so. */
  94. int
  95. cp_is_in_anonymous (const char *symbol_name)
  96. {
  97. return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
  98. != NULL);
  99. }
  100. /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
  101. If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
  102. within an anonymous namespace. */
  103. static struct block_symbol
  104. cp_basic_lookup_symbol (const char *name, const struct block *block,
  105. const domain_enum domain, int is_in_anonymous)
  106. {
  107. struct block_symbol sym;
  108. sym = lookup_symbol_in_static_block (name, block, domain);
  109. if (sym.symbol != NULL)
  110. return sym;
  111. if (is_in_anonymous)
  112. {
  113. /* Symbols defined in anonymous namespaces have external linkage
  114. but should be treated as local to a single file nonetheless.
  115. So we only search the current file's global block. */
  116. const struct block *global_block = block_global_block (block);
  117. if (global_block != NULL)
  118. {
  119. sym.symbol = lookup_symbol_in_block (name,
  120. symbol_name_match_type::FULL,
  121. global_block, domain);
  122. sym.block = global_block;
  123. }
  124. }
  125. else
  126. sym = lookup_global_symbol (name, block, domain);
  127. return sym;
  128. }
  129. /* Search bare symbol NAME in DOMAIN in BLOCK.
  130. NAME is guaranteed to not have any scope (no "::") in its name, though
  131. if for example NAME is a template spec then "::" may appear in the
  132. argument list.
  133. If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
  134. that language. Normally we wouldn't need LANGDEF but fortran also uses
  135. this code.
  136. If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
  137. if so then also search for NAME in that class. */
  138. static struct block_symbol
  139. cp_lookup_bare_symbol (const struct language_defn *langdef,
  140. const char *name, const struct block *block,
  141. const domain_enum domain, int search)
  142. {
  143. struct block_symbol sym;
  144. /* Note: We can't do a simple assert for ':' not being in NAME because
  145. ':' may be in the args of a template spec. This isn't intended to be
  146. a complete test, just cheap and documentary. */
  147. if (strchr (name, '<') == NULL && strchr (name, '(') == NULL)
  148. gdb_assert (strstr (name, "::") == NULL);
  149. sym = lookup_symbol_in_static_block (name, block, domain);
  150. if (sym.symbol != NULL)
  151. return sym;
  152. /* If we didn't find a definition for a builtin type in the static block,
  153. search for it now. This is actually the right thing to do and can be
  154. a massive performance win. E.g., when debugging a program with lots of
  155. shared libraries we could search all of them only to find out the
  156. builtin type isn't defined in any of them. This is common for types
  157. like "void". */
  158. if (langdef != NULL && domain == VAR_DOMAIN)
  159. {
  160. struct gdbarch *gdbarch;
  161. if (block == NULL)
  162. gdbarch = target_gdbarch ();
  163. else
  164. gdbarch = block_gdbarch (block);
  165. sym.symbol
  166. = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
  167. sym.block = NULL;
  168. if (sym.symbol != NULL)
  169. return sym;
  170. }
  171. sym = lookup_global_symbol (name, block, domain);
  172. if (sym.symbol != NULL)
  173. return sym;
  174. if (search)
  175. {
  176. struct block_symbol lang_this;
  177. struct type *type;
  178. lang_this.symbol = NULL;
  179. if (langdef != NULL)
  180. lang_this = lookup_language_this (langdef, block);
  181. if (lang_this.symbol == NULL)
  182. return {};
  183. type = check_typedef (TYPE_TARGET_TYPE (lang_this.symbol->type ()));
  184. /* If TYPE_NAME is NULL, abandon trying to find this symbol.
  185. This can happen for lambda functions compiled with clang++,
  186. which outputs no name for the container class. */
  187. if (type->name () == NULL)
  188. return {};
  189. /* Look for symbol NAME in this class. */
  190. sym = cp_lookup_nested_symbol (type, name, block, domain);
  191. }
  192. return sym;
  193. }
  194. /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
  195. BLOCK specifies the context in which to perform the search.
  196. NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
  197. the length of the entire scope of NAME (up to, but not including, the last
  198. "::".
  199. Note: At least in the case of Fortran, which also uses this code, there
  200. may be no text after the last "::". */
  201. static struct block_symbol
  202. cp_search_static_and_baseclasses (const char *name,
  203. const struct block *block,
  204. const domain_enum domain,
  205. unsigned int prefix_len,
  206. int is_in_anonymous)
  207. {
  208. /* Check for malformed input. */
  209. if (prefix_len + 2 > strlen (name) || name[prefix_len + 1] != ':')
  210. return {};
  211. /* The class, namespace or function name is everything up to and
  212. including PREFIX_LEN. */
  213. std::string scope (name, prefix_len);
  214. /* The rest of the name is everything else past the initial scope
  215. operator. */
  216. const char *nested = name + prefix_len + 2;
  217. /* Lookup the scope symbol. If none is found, there is nothing more
  218. that can be done. SCOPE could be a namespace, so always look in
  219. VAR_DOMAIN. This works for classes too because of
  220. symbol_matches_domain (which should be replaced with something
  221. else, but it's what we have today). */
  222. block_symbol scope_sym = lookup_symbol_in_static_block (scope.c_str (),
  223. block, VAR_DOMAIN);
  224. if (scope_sym.symbol == NULL)
  225. scope_sym = lookup_global_symbol (scope.c_str (), block, VAR_DOMAIN);
  226. if (scope_sym.symbol == NULL)
  227. return {};
  228. struct type *scope_type = scope_sym.symbol->type ();
  229. /* If the scope is a function/method, then look up NESTED as a local
  230. static variable. E.g., "print 'function()::static_var'". */
  231. if ((scope_type->code () == TYPE_CODE_FUNC
  232. || scope_type->code () == TYPE_CODE_METHOD)
  233. && domain == VAR_DOMAIN)
  234. return lookup_symbol (nested, SYMBOL_BLOCK_VALUE (scope_sym.symbol),
  235. VAR_DOMAIN, NULL);
  236. /* Look for a symbol named NESTED in this class/namespace.
  237. The caller is assumed to have already have done a basic lookup of NAME.
  238. So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
  239. return cp_lookup_nested_symbol_1 (scope_type, nested, name,
  240. block, domain, 0, is_in_anonymous);
  241. }
  242. /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
  243. as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
  244. through base classes for a matching symbol.
  245. Note: Part of the complexity is because NAME may itself specify scope.
  246. Part of the complexity is also because this handles the case where
  247. there is no scoping in which case we also try looking in the class of
  248. "this" if we can compute it. */
  249. static struct block_symbol
  250. cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name,
  251. const struct block *block,
  252. const domain_enum domain, int search)
  253. {
  254. char *concatenated_name = NULL;
  255. int is_in_anonymous;
  256. unsigned int prefix_len;
  257. struct block_symbol sym;
  258. if (the_namespace[0] != '\0')
  259. {
  260. concatenated_name
  261. = (char *) alloca (strlen (the_namespace) + 2 + strlen (name) + 1);
  262. strcpy (concatenated_name, the_namespace);
  263. strcat (concatenated_name, "::");
  264. strcat (concatenated_name, name);
  265. name = concatenated_name;
  266. }
  267. prefix_len = cp_entire_prefix_len (name);
  268. if (prefix_len == 0)
  269. return cp_lookup_bare_symbol (NULL, name, block, domain, search);
  270. /* This would be simpler if we just called cp_lookup_nested_symbol
  271. at this point. But that would require first looking up the containing
  272. class/namespace. Since we're only searching static and global blocks
  273. there's often no need to first do that lookup. */
  274. is_in_anonymous
  275. = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace);
  276. sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
  277. if (sym.symbol != NULL)
  278. return sym;
  279. if (search)
  280. sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len,
  281. is_in_anonymous);
  282. return sym;
  283. }
  284. /* Search for NAME by applying all import statements belonging to
  285. BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
  286. search is restricted to using declarations.
  287. Example:
  288. namespace A {
  289. int x;
  290. }
  291. using A::x;
  292. If SEARCH_PARENTS the search will include imports which are
  293. applicable in parents of SCOPE.
  294. Example:
  295. namespace A {
  296. using namespace X;
  297. namespace B {
  298. using namespace Y;
  299. }
  300. }
  301. If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
  302. namespaces X and Y will be considered. If SEARCH_PARENTS is false
  303. only the import of Y is considered.
  304. SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
  305. pass 0 for it. Internally we pass 1 when recursing. */
  306. static struct block_symbol
  307. cp_lookup_symbol_via_imports (const char *scope,
  308. const char *name,
  309. const struct block *block,
  310. const domain_enum domain,
  311. const int search_scope_first,
  312. const int declaration_only,
  313. const int search_parents)
  314. {
  315. struct using_direct *current;
  316. struct block_symbol sym = {};
  317. int len;
  318. int directive_match;
  319. /* First, try to find the symbol in the given namespace if requested. */
  320. if (search_scope_first)
  321. sym = cp_lookup_symbol_in_namespace (scope, name,
  322. block, domain, 1);
  323. if (sym.symbol != NULL)
  324. return sym;
  325. /* Go through the using directives. If any of them add new names to
  326. the namespace we're searching in, see if we can find a match by
  327. applying them. */
  328. for (current = block_using (block);
  329. current != NULL;
  330. current = current->next)
  331. {
  332. const char **excludep;
  333. len = strlen (current->import_dest);
  334. directive_match = (search_parents
  335. ? (startswith (scope, current->import_dest)
  336. && (len == 0
  337. || scope[len] == ':'
  338. || scope[len] == '\0'))
  339. : strcmp (scope, current->import_dest) == 0);
  340. /* If the import destination is the current scope or one of its
  341. ancestors then it is applicable. */
  342. if (directive_match && !current->searched)
  343. {
  344. /* Mark this import as searched so that the recursive call
  345. does not search it again. */
  346. scoped_restore reset_directive_searched
  347. = make_scoped_restore (&current->searched, 1);
  348. /* If there is an import of a single declaration, compare the
  349. imported declaration (after optional renaming by its alias)
  350. with the sought out name. If there is a match pass
  351. current->import_src as NAMESPACE to direct the search
  352. towards the imported namespace. */
  353. if (current->declaration
  354. && strcmp (name, current->alias
  355. ? current->alias : current->declaration) == 0)
  356. sym = cp_lookup_symbol_in_namespace (current->import_src,
  357. current->declaration,
  358. block, domain, 1);
  359. /* If this is a DECLARATION_ONLY search or a symbol was found
  360. or this import statement was an import declaration, the
  361. search of this import is complete. */
  362. if (declaration_only || sym.symbol != NULL || current->declaration)
  363. {
  364. if (sym.symbol != NULL)
  365. return sym;
  366. continue;
  367. }
  368. /* Do not follow CURRENT if NAME matches its EXCLUDES. */
  369. for (excludep = current->excludes; *excludep; excludep++)
  370. if (strcmp (name, *excludep) == 0)
  371. break;
  372. if (*excludep)
  373. continue;
  374. if (current->alias != NULL
  375. && strcmp (name, current->alias) == 0)
  376. /* If the import is creating an alias and the alias matches
  377. the sought name. Pass current->import_src as the NAME to
  378. direct the search towards the aliased namespace. */
  379. {
  380. sym = cp_lookup_symbol_in_namespace (scope,
  381. current->import_src,
  382. block, domain, 1);
  383. }
  384. else if (current->alias == NULL)
  385. {
  386. /* If this import statement creates no alias, pass
  387. current->inner as NAMESPACE to direct the search
  388. towards the imported namespace. */
  389. sym = cp_lookup_symbol_via_imports (current->import_src,
  390. name, block,
  391. domain, 1, 0, 0);
  392. }
  393. if (sym.symbol != NULL)
  394. return sym;
  395. }
  396. }
  397. return {};
  398. }
  399. /* Helper function that searches an array of symbols for one named NAME. */
  400. static struct symbol *
  401. search_symbol_list (const char *name, int num,
  402. struct symbol **syms)
  403. {
  404. int i;
  405. /* Maybe we should store a dictionary in here instead. */
  406. for (i = 0; i < num; ++i)
  407. {
  408. if (strcmp (name, syms[i]->natural_name ()) == 0)
  409. return syms[i];
  410. }
  411. return NULL;
  412. }
  413. /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
  414. searches through the template parameters of the function and the
  415. function's type. */
  416. struct block_symbol
  417. cp_lookup_symbol_imports_or_template (const char *scope,
  418. const char *name,
  419. const struct block *block,
  420. const domain_enum domain)
  421. {
  422. struct symbol *function = BLOCK_FUNCTION (block);
  423. struct block_symbol result;
  424. if (symbol_lookup_debug)
  425. {
  426. gdb_printf (gdb_stdlog,
  427. "cp_lookup_symbol_imports_or_template"
  428. " (%s, %s, %s, %s)\n",
  429. scope, name, host_address_to_string (block),
  430. domain_name (domain));
  431. }
  432. if (function != NULL && function->language () == language_cplus)
  433. {
  434. /* Search the function's template parameters. */
  435. if (function->is_cplus_template_function ())
  436. {
  437. struct template_symbol *templ
  438. = (struct template_symbol *) function;
  439. struct symbol *sym = search_symbol_list (name,
  440. templ->n_template_arguments,
  441. templ->template_arguments);
  442. if (sym != NULL)
  443. {
  444. if (symbol_lookup_debug)
  445. {
  446. gdb_printf (gdb_stdlog,
  447. "cp_lookup_symbol_imports_or_template"
  448. " (...) = %s\n",
  449. host_address_to_string (sym));
  450. }
  451. return (struct block_symbol) {sym, block};
  452. }
  453. }
  454. /* Search the template parameters of the function's defining
  455. context. */
  456. if (function->natural_name ())
  457. {
  458. struct type *context;
  459. std::string name_copy (function->natural_name ());
  460. const struct language_defn *lang = language_def (language_cplus);
  461. const struct block *parent = BLOCK_SUPERBLOCK (block);
  462. struct symbol *sym;
  463. while (1)
  464. {
  465. unsigned int prefix_len
  466. = cp_entire_prefix_len (name_copy.c_str ());
  467. if (prefix_len == 0)
  468. context = NULL;
  469. else
  470. {
  471. name_copy.erase (prefix_len);
  472. context = lookup_typename (lang,
  473. name_copy.c_str (),
  474. parent, 1);
  475. }
  476. if (context == NULL)
  477. break;
  478. sym
  479. = search_symbol_list (name,
  480. TYPE_N_TEMPLATE_ARGUMENTS (context),
  481. TYPE_TEMPLATE_ARGUMENTS (context));
  482. if (sym != NULL)
  483. {
  484. if (symbol_lookup_debug)
  485. {
  486. gdb_printf
  487. (gdb_stdlog,
  488. "cp_lookup_symbol_imports_or_template (...) = %s\n",
  489. host_address_to_string (sym));
  490. }
  491. return (struct block_symbol) {sym, parent};
  492. }
  493. }
  494. }
  495. }
  496. result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1);
  497. if (symbol_lookup_debug)
  498. {
  499. gdb_printf (gdb_stdlog,
  500. "cp_lookup_symbol_imports_or_template (...) = %s\n",
  501. result.symbol != NULL
  502. ? host_address_to_string (result.symbol) : "NULL");
  503. }
  504. return result;
  505. }
  506. /* Search for NAME by applying relevant import statements belonging to BLOCK
  507. and its parents. SCOPE is the namespace scope of the context in which the
  508. search is being evaluated. */
  509. static struct block_symbol
  510. cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
  511. const struct block *block,
  512. const domain_enum domain)
  513. {
  514. struct block_symbol sym;
  515. while (block != NULL)
  516. {
  517. sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1);
  518. if (sym.symbol)
  519. return sym;
  520. block = BLOCK_SUPERBLOCK (block);
  521. }
  522. return {};
  523. }
  524. /* Searches for NAME in the current namespace, and by applying
  525. relevant import statements belonging to BLOCK and its parents.
  526. SCOPE is the namespace scope of the context in which the search is
  527. being evaluated. */
  528. struct block_symbol
  529. cp_lookup_symbol_namespace (const char *scope,
  530. const char *name,
  531. const struct block *block,
  532. const domain_enum domain)
  533. {
  534. struct block_symbol sym;
  535. if (symbol_lookup_debug)
  536. {
  537. gdb_printf (gdb_stdlog,
  538. "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
  539. scope, name, host_address_to_string (block),
  540. domain_name (domain));
  541. }
  542. /* First, try to find the symbol in the given namespace. */
  543. sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
  544. /* Search for name in namespaces imported to this and parent blocks. */
  545. if (sym.symbol == NULL)
  546. sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
  547. if (symbol_lookup_debug)
  548. {
  549. gdb_printf (gdb_stdlog,
  550. "cp_lookup_symbol_namespace (...) = %s\n",
  551. sym.symbol != NULL
  552. ? host_address_to_string (sym.symbol) : "NULL");
  553. }
  554. return sym;
  555. }
  556. /* Lookup NAME at namespace scope (or, in C terms, in static and
  557. global variables). SCOPE is the namespace that the current
  558. function is defined within; only consider namespaces whose length
  559. is at least SCOPE_LEN. Other arguments are as in
  560. cp_lookup_symbol_nonlocal.
  561. For example, if we're within a function A::B::f and looking for a
  562. symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
  563. SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
  564. but with SCOPE_LEN = 1. And then it calls itself with NAME and
  565. SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
  566. "A::B::x"; if it doesn't find it, then the second call looks for
  567. "A::x", and if that call fails, then the first call looks for
  568. "x". */
  569. static struct block_symbol
  570. lookup_namespace_scope (const struct language_defn *langdef,
  571. const char *name,
  572. const struct block *block,
  573. const domain_enum domain,
  574. const char *scope,
  575. int scope_len)
  576. {
  577. char *the_namespace;
  578. if (scope[scope_len] != '\0')
  579. {
  580. /* Recursively search for names in child namespaces first. */
  581. struct block_symbol sym;
  582. int new_scope_len = scope_len;
  583. /* If the current scope is followed by "::", skip past that. */
  584. if (new_scope_len != 0)
  585. {
  586. gdb_assert (scope[new_scope_len] == ':');
  587. new_scope_len += 2;
  588. }
  589. new_scope_len += cp_find_first_component (scope + new_scope_len);
  590. sym = lookup_namespace_scope (langdef, name, block, domain,
  591. scope, new_scope_len);
  592. if (sym.symbol != NULL)
  593. return sym;
  594. }
  595. /* Okay, we didn't find a match in our children, so look for the
  596. name in the current namespace.
  597. If we there is no scope and we know we have a bare symbol, then short
  598. circuit everything and call cp_lookup_bare_symbol directly.
  599. This isn't an optimization, rather it allows us to pass LANGDEF which
  600. is needed for primitive type lookup. The test doesn't have to be
  601. perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
  602. template symbol with "::" in the argument list) then
  603. cp_lookup_symbol_in_namespace will catch it. */
  604. if (scope_len == 0 && strchr (name, ':') == NULL)
  605. return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
  606. the_namespace = (char *) alloca (scope_len + 1);
  607. strncpy (the_namespace, scope, scope_len);
  608. the_namespace[scope_len] = '\0';
  609. return cp_lookup_symbol_in_namespace (the_namespace, name,
  610. block, domain, 1);
  611. }
  612. /* The C++-specific version of name lookup for static and global
  613. names. This makes sure that names get looked for in all namespaces
  614. that are in scope. NAME is the natural name of the symbol that
  615. we're looking for, BLOCK is the block that we're searching within,
  616. DOMAIN says what kind of symbols we're looking for. */
  617. struct block_symbol
  618. cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
  619. const char *name,
  620. const struct block *block,
  621. const domain_enum domain)
  622. {
  623. struct block_symbol sym;
  624. const char *scope = block_scope (block);
  625. if (symbol_lookup_debug)
  626. {
  627. gdb_printf (gdb_stdlog,
  628. "cp_lookup_symbol_non_local"
  629. " (%s, %s (scope %s), %s)\n",
  630. name, host_address_to_string (block), scope,
  631. domain_name (domain));
  632. }
  633. /* First, try to find the symbol in the given namespace, and all
  634. containing namespaces. */
  635. sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
  636. /* Search for name in namespaces imported to this and parent blocks. */
  637. if (sym.symbol == NULL)
  638. sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
  639. if (symbol_lookup_debug)
  640. {
  641. gdb_printf (gdb_stdlog,
  642. "cp_lookup_symbol_nonlocal (...) = %s\n",
  643. (sym.symbol != NULL
  644. ? host_address_to_string (sym.symbol)
  645. : "NULL"));
  646. }
  647. return sym;
  648. }
  649. /* Search through the base classes of PARENT_TYPE for a base class
  650. named NAME and return its type. If not found, return NULL. */
  651. struct type *
  652. cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
  653. {
  654. int i;
  655. parent_type = check_typedef (parent_type);
  656. for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
  657. {
  658. struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
  659. const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
  660. if (base_name == NULL)
  661. continue;
  662. if (streq (base_name, name))
  663. return type;
  664. type = cp_find_type_baseclass_by_name (type, name);
  665. if (type != NULL)
  666. return type;
  667. }
  668. return NULL;
  669. }
  670. /* Search through the base classes of PARENT_TYPE for a symbol named
  671. NAME in block BLOCK. */
  672. static struct block_symbol
  673. find_symbol_in_baseclass (struct type *parent_type, const char *name,
  674. const struct block *block, const domain_enum domain,
  675. int is_in_anonymous)
  676. {
  677. int i;
  678. struct block_symbol sym = {};
  679. for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
  680. {
  681. struct type *base_type = TYPE_BASECLASS (parent_type, i);
  682. const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
  683. if (base_name == NULL)
  684. continue;
  685. std::string concatenated_name = std::string (base_name) + "::" + name;
  686. sym = cp_lookup_nested_symbol_1 (base_type, name,
  687. concatenated_name.c_str (),
  688. block, domain, 1, is_in_anonymous);
  689. if (sym.symbol != NULL)
  690. break;
  691. }
  692. return sym;
  693. }
  694. /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
  695. and within the context of BLOCK.
  696. NESTED_NAME may have scope ("::").
  697. CONTAINER_TYPE needn't have been "check_typedef'd" yet.
  698. CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
  699. passed as an argument so that callers can control how space for it is
  700. allocated.
  701. If BASIC_LOOKUP is non-zero then perform a basic lookup of
  702. CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
  703. If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
  704. namespace. */
  705. static struct block_symbol
  706. cp_lookup_nested_symbol_1 (struct type *container_type,
  707. const char *nested_name,
  708. const char *concatenated_name,
  709. const struct block *block,
  710. const domain_enum domain,
  711. int basic_lookup, int is_in_anonymous)
  712. {
  713. struct block_symbol sym;
  714. /* NOTE: carlton/2003-11-10: We don't treat C++ class members
  715. of classes like, say, data or function members. Instead,
  716. they're just represented by symbols whose names are
  717. qualified by the name of the surrounding class. This is
  718. just like members of namespaces; in particular,
  719. cp_basic_lookup_symbol works when looking them up. */
  720. if (basic_lookup)
  721. {
  722. sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
  723. is_in_anonymous);
  724. if (sym.symbol != NULL)
  725. return sym;
  726. }
  727. /* Now search all static file-level symbols. We have to do this for things
  728. like typedefs in the class. We do not try to guess any imported
  729. namespace as even the fully specified namespace search is already not
  730. C++ compliant and more assumptions could make it too magic. */
  731. /* First search in this symtab, what we want is possibly there. */
  732. sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
  733. if (sym.symbol != NULL)
  734. return sym;
  735. /* Nope. We now have to search all static blocks in all objfiles,
  736. even if block != NULL, because there's no guarantees as to which
  737. symtab the symbol we want is in. Except for symbols defined in
  738. anonymous namespaces should be treated as local to a single file,
  739. which we just searched. */
  740. if (!is_in_anonymous)
  741. {
  742. sym = lookup_static_symbol (concatenated_name, domain);
  743. if (sym.symbol != NULL)
  744. return sym;
  745. }
  746. /* If this is a class with baseclasses, search them next. */
  747. container_type = check_typedef (container_type);
  748. if (TYPE_N_BASECLASSES (container_type) > 0)
  749. {
  750. sym = find_symbol_in_baseclass (container_type, nested_name, block,
  751. domain, is_in_anonymous);
  752. if (sym.symbol != NULL)
  753. return sym;
  754. }
  755. return {};
  756. }
  757. /* Look up a symbol named NESTED_NAME that is nested inside the C++
  758. class or namespace given by PARENT_TYPE, from within the context
  759. given by BLOCK, and in DOMAIN.
  760. Return NULL if there is no such nested symbol. */
  761. struct block_symbol
  762. cp_lookup_nested_symbol (struct type *parent_type,
  763. const char *nested_name,
  764. const struct block *block,
  765. const domain_enum domain)
  766. {
  767. /* type_name_or_error provides better error reporting using the
  768. original type. */
  769. struct type *saved_parent_type = parent_type;
  770. parent_type = check_typedef (parent_type);
  771. if (symbol_lookup_debug)
  772. {
  773. const char *type_name = saved_parent_type->name ();
  774. gdb_printf (gdb_stdlog,
  775. "cp_lookup_nested_symbol (%s, %s, %s, %s)\n",
  776. type_name != NULL ? type_name : "unnamed",
  777. nested_name, host_address_to_string (block),
  778. domain_name (domain));
  779. }
  780. switch (parent_type->code ())
  781. {
  782. case TYPE_CODE_STRUCT:
  783. case TYPE_CODE_NAMESPACE:
  784. case TYPE_CODE_UNION:
  785. case TYPE_CODE_ENUM:
  786. /* NOTE: Handle modules here as well, because Fortran is re-using the C++
  787. specific code to lookup nested symbols in modules, by calling the
  788. method lookup_symbol_nonlocal, which ends up here. */
  789. case TYPE_CODE_MODULE:
  790. {
  791. int size;
  792. const char *parent_name = type_name_or_error (saved_parent_type);
  793. struct block_symbol sym;
  794. char *concatenated_name;
  795. int is_in_anonymous;
  796. size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
  797. concatenated_name = (char *) alloca (size);
  798. xsnprintf (concatenated_name, size, "%s::%s",
  799. parent_name, nested_name);
  800. is_in_anonymous = cp_is_in_anonymous (concatenated_name);
  801. sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
  802. concatenated_name, block, domain,
  803. 1, is_in_anonymous);
  804. if (symbol_lookup_debug)
  805. {
  806. gdb_printf (gdb_stdlog,
  807. "cp_lookup_nested_symbol (...) = %s\n",
  808. (sym.symbol != NULL
  809. ? host_address_to_string (sym.symbol)
  810. : "NULL"));
  811. }
  812. return sym;
  813. }
  814. case TYPE_CODE_FUNC:
  815. case TYPE_CODE_METHOD:
  816. if (symbol_lookup_debug)
  817. {
  818. gdb_printf (gdb_stdlog,
  819. "cp_lookup_nested_symbol (...) = NULL"
  820. " (func/method)\n");
  821. }
  822. return {};
  823. default:
  824. internal_error (__FILE__, __LINE__,
  825. _("cp_lookup_nested_symbol called "
  826. "on a non-aggregate type."));
  827. }
  828. }
  829. /* The C++-version of lookup_transparent_type. */
  830. /* FIXME: carlton/2004-01-16: The problem that this is trying to
  831. address is that, unfortunately, sometimes NAME is wrong: it may not
  832. include the name of namespaces enclosing the type in question.
  833. lookup_transparent_type gets called when the type in question
  834. is a declaration, and we're trying to find its definition; but, for
  835. declarations, our type name deduction mechanism doesn't work.
  836. There's nothing we can do to fix this in general, I think, in the
  837. absence of debug information about namespaces (I've filed PR
  838. gdb/1511 about this); until such debug information becomes more
  839. prevalent, one heuristic which sometimes looks is to search for the
  840. definition in namespaces containing the current namespace.
  841. We should delete this functions once the appropriate debug
  842. information becomes more widespread. (GCC 3.4 will be the first
  843. released version of GCC with such information.) */
  844. struct type *
  845. cp_lookup_transparent_type (const char *name)
  846. {
  847. /* First, try the honest way of looking up the definition. */
  848. struct type *t = basic_lookup_transparent_type (name);
  849. const char *scope;
  850. if (t != NULL)
  851. return t;
  852. /* If that doesn't work and we're within a namespace, look there
  853. instead. */
  854. scope = block_scope (get_selected_block (0));
  855. if (scope[0] == '\0')
  856. return NULL;
  857. return cp_lookup_transparent_type_loop (name, scope, 0);
  858. }
  859. /* Lookup the type definition associated to NAME in namespaces/classes
  860. containing SCOPE whose name is strictly longer than LENGTH. LENGTH
  861. must be the index of the start of a component of SCOPE. */
  862. static struct type *
  863. cp_lookup_transparent_type_loop (const char *name,
  864. const char *scope,
  865. int length)
  866. {
  867. int scope_length = length + cp_find_first_component (scope + length);
  868. char *full_name;
  869. /* If the current scope is followed by "::", look in the next
  870. component. */
  871. if (scope[scope_length] == ':')
  872. {
  873. struct type *retval
  874. = cp_lookup_transparent_type_loop (name, scope,
  875. scope_length + 2);
  876. if (retval != NULL)
  877. return retval;
  878. }
  879. full_name = (char *) alloca (scope_length + 2 + strlen (name) + 1);
  880. strncpy (full_name, scope, scope_length);
  881. memcpy (full_name + scope_length, "::", 2);
  882. strcpy (full_name + scope_length + 2, name);
  883. return basic_lookup_transparent_type (full_name);
  884. }
  885. /* This used to do something but was removed when it became
  886. obsolete. */
  887. static void
  888. maintenance_cplus_namespace (const char *args, int from_tty)
  889. {
  890. gdb_printf (_("The `maint namespace' command was removed.\n"));
  891. }
  892. void _initialize_cp_namespace ();
  893. void
  894. _initialize_cp_namespace ()
  895. {
  896. struct cmd_list_element *cmd;
  897. cmd = add_cmd ("namespace", class_maintenance,
  898. maintenance_cplus_namespace,
  899. _("Deprecated placeholder for removed functionality."),
  900. &maint_cplus_cmd_list);
  901. deprecate_cmd (cmd, NULL);
  902. }