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