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