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