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