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