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