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