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