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