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