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