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