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