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