Use gdb::def_vector in create_sals_line_offset
[deliverable/binutils-gdb.git] / gdb / cp-namespace.c
CommitLineData
9219021c 1/* Helper routines for C++ support in GDB.
61baf725 2 Copyright (C) 2003-2017 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"
05d49c37 35#include <string>
9219021c 36
d12307c1 37static struct block_symbol
6f27419a
DE
38 cp_lookup_nested_symbol_1 (struct type *container_type,
39 const char *nested_name,
40 const char *concatenated_name,
41 const struct block *block,
4dcabcc2 42 const domain_enum domain,
96553a0c 43 int basic_lookup, int is_in_anonymous);
6f27419a 44
b368761e
DC
45static struct type *cp_lookup_transparent_type_loop (const char *name,
46 const char *scope,
47 int scope_len);
48
9219021c
DC
49/* Check to see if SYMBOL refers to an object contained within an
50 anonymous namespace; if so, add an appropriate using directive. */
51
9219021c 52void
a10964d1
AR
53cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
54 struct objfile *const objfile)
9219021c 55{
df8a16a1 56 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
9219021c 57 {
df8a16a1 58 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
9219021c
DC
59 unsigned int previous_component;
60 unsigned int next_component;
9219021c
DC
61
62 /* Start with a quick-and-dirty check for mention of "(anonymous
63 namespace)". */
64
59da4d04 65 if (!cp_is_in_anonymous (name))
9219021c
DC
66 return;
67
68 previous_component = 0;
69 next_component = cp_find_first_component (name + previous_component);
70
71 while (name[next_component] == ':')
72 {
2b1dbab0
KS
73 if (((next_component - previous_component)
74 == CP_ANONYMOUS_NAMESPACE_LEN)
9219021c 75 && strncmp (name + previous_component,
2b1dbab0
KS
76 CP_ANONYMOUS_NAMESPACE_STR,
77 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
9219021c 78 {
aff410f1
MS
79 int dest_len = (previous_component == 0
80 ? 0 : previous_component - 2);
8c902bb1 81 int src_len = next_component;
794684b6 82
224c3ddb
SM
83 char *dest = (char *) alloca (dest_len + 1);
84 char *src = (char *) alloca (src_len + 1);
794684b6 85
8c902bb1
SW
86 memcpy (dest, name, dest_len);
87 memcpy (src, name, src_len);
794684b6 88
8c902bb1
SW
89 dest[dest_len] = '\0';
90 src[src_len] = '\0';
794684b6 91
9219021c
DC
92 /* We've found a component of the name that's an
93 anonymous namespace. So add symbols in it to the
94 namespace given by the previous component if there is
95 one, or to the global namespace if there isn't. */
22cee43f
PMR
96 add_using_directive (&local_using_directives,
97 dest, src, NULL, NULL, NULL, 1,
98 &objfile->objfile_obstack);
9219021c
DC
99 }
100 /* The "+ 2" is for the "::". */
101 previous_component = next_component + 2;
102 next_component = (previous_component
103 + cp_find_first_component (name
104 + previous_component));
105 }
106 }
107}
108
9219021c
DC
109/* Test whether or not NAMESPACE looks like it mentions an anonymous
110 namespace; return nonzero if so. */
111
112int
59da4d04 113cp_is_in_anonymous (const char *symbol_name)
9219021c 114{
59da4d04 115 return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
9219021c
DC
116 != NULL);
117}
118
6f27419a 119/* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
d5ff0482 120 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
6f27419a 121 within an anonymous namespace. */
34ef8452 122
d12307c1 123static struct block_symbol
6f27419a 124cp_basic_lookup_symbol (const char *name, const struct block *block,
d5ff0482 125 const domain_enum domain, int is_in_anonymous)
34ef8452 126{
d12307c1 127 struct block_symbol sym;
34ef8452
DE
128
129 sym = lookup_symbol_in_static_block (name, block, domain);
d12307c1 130 if (sym.symbol != NULL)
34ef8452
DE
131 return sym;
132
d5ff0482 133 if (is_in_anonymous)
34ef8452
DE
134 {
135 /* Symbols defined in anonymous namespaces have external linkage
136 but should be treated as local to a single file nonetheless.
137 So we only search the current file's global block. */
138
139 const struct block *global_block = block_global_block (block);
791244be 140
34ef8452 141 if (global_block != NULL)
d12307c1
PMR
142 {
143 sym.symbol = lookup_symbol_in_block (name, global_block, domain);
144 sym.block = global_block;
145 }
34ef8452
DE
146 }
147 else
d12307c1 148 sym = lookup_global_symbol (name, block, domain);
34ef8452 149
6f27419a
DE
150 return sym;
151}
152
153/* Search bare symbol NAME in DOMAIN in BLOCK.
1994afbf
DE
154 NAME is guaranteed to not have any scope (no "::") in its name, though
155 if for example NAME is a template spec then "::" may appear in the
156 argument list.
157 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
158 that language. Normally we wouldn't need LANGDEF but fortran also uses
159 this code.
6f27419a
DE
160 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
161 if so then also search for NAME in that class. */
162
d12307c1 163static struct block_symbol
1994afbf
DE
164cp_lookup_bare_symbol (const struct language_defn *langdef,
165 const char *name, const struct block *block,
6f27419a
DE
166 const domain_enum domain, int search)
167{
d12307c1 168 struct block_symbol sym;
6f27419a
DE
169
170 /* Note: We can't do a simple assert for ':' not being in NAME because
171 ':' may be in the args of a template spec. This isn't intended to be
172 a complete test, just cheap and documentary. */
173 if (strchr (name, '<') == NULL && strchr (name, '(') == NULL)
1cafadb4 174 gdb_assert (strstr (name, "::") == NULL);
6f27419a
DE
175
176 sym = lookup_symbol_in_static_block (name, block, domain);
d12307c1 177 if (sym.symbol != NULL)
6f27419a
DE
178 return sym;
179
1994afbf
DE
180 /* If we didn't find a definition for a builtin type in the static block,
181 search for it now. This is actually the right thing to do and can be
182 a massive performance win. E.g., when debugging a program with lots of
183 shared libraries we could search all of them only to find out the
184 builtin type isn't defined in any of them. This is common for types
185 like "void". */
186 if (langdef != NULL && domain == VAR_DOMAIN)
187 {
188 struct gdbarch *gdbarch;
189
190 if (block == NULL)
191 gdbarch = target_gdbarch ();
192 else
193 gdbarch = block_gdbarch (block);
d12307c1
PMR
194 sym.symbol
195 = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
196 sym.block = NULL;
197 if (sym.symbol != NULL)
1994afbf
DE
198 return sym;
199 }
200
6f27419a 201 sym = lookup_global_symbol (name, block, domain);
d12307c1 202 if (sym.symbol != NULL)
34ef8452
DE
203 return sym;
204
205 if (search)
206 {
d12307c1 207 struct block_symbol lang_this;
6f27419a 208 struct type *type;
34ef8452 209
41c977aa
WT
210 lang_this.symbol = NULL;
211
4f19a0e6
WT
212 if (langdef != NULL)
213 lang_this = lookup_language_this (langdef, block);
214
d12307c1 215 if (lang_this.symbol == NULL)
b6b80672 216 return null_block_symbol;
34ef8452 217
4f19a0e6 218
d12307c1 219 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol)));
6f27419a
DE
220 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
221 This can happen for lambda functions compiled with clang++,
222 which outputs no name for the container class. */
223 if (TYPE_NAME (type) == NULL)
b6b80672 224 return null_block_symbol;
34ef8452 225
d5ff0482 226 /* Look for symbol NAME in this class. */
4dcabcc2 227 sym = cp_lookup_nested_symbol (type, name, block, domain);
6f27419a 228 }
34ef8452 229
6f27419a
DE
230 return sym;
231}
34ef8452 232
6f27419a
DE
233/* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
234 BLOCK specifies the context in which to perform the search.
235 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
d5ff0482 236 the length of the entire scope of NAME (up to, but not including, the last
6f27419a 237 "::".
34ef8452 238
6f27419a
DE
239 Note: At least in the case of Fortran, which also uses this code, there
240 may be no text after the last "::". */
34ef8452 241
d12307c1 242static struct block_symbol
6f27419a
DE
243cp_search_static_and_baseclasses (const char *name,
244 const struct block *block,
245 const domain_enum domain,
96553a0c
DE
246 unsigned int prefix_len,
247 int is_in_anonymous)
6f27419a 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
e68cb8e0
PA
253 /* The class, namespace or function name is everything up to and
254 including PREFIX_LEN. */
255 std::string scope (name, prefix_len);
6f27419a
DE
256
257 /* The rest of the name is everything else past the initial scope
258 operator. */
e68cb8e0
PA
259 const char *nested = name + prefix_len + 2;
260
261 /* Lookup the scope symbol. If none is found, there is nothing more
262 that can be done. SCOPE could be a namespace, so always look in
263 VAR_DOMAIN. This works for classes too because of
264 symbol_matches_domain (which should be replaced with something
265 else, but it's what we have today). */
266 block_symbol scope_sym = lookup_symbol_in_static_block (scope.c_str (),
267 block, VAR_DOMAIN);
268 if (scope_sym.symbol == NULL)
269 scope_sym = lookup_global_symbol (scope.c_str (), block, VAR_DOMAIN);
270 if (scope_sym.symbol == NULL)
05d49c37 271 return null_block_symbol;
6f27419a 272
e68cb8e0
PA
273 struct type *scope_type = SYMBOL_TYPE (scope_sym.symbol);
274
275 /* If the scope is a function/method, then look up NESTED as a local
276 static variable. E.g., "print 'function()::static_var'". */
277 if (TYPE_CODE (scope_type) == TYPE_CODE_FUNC
278 || TYPE_CODE (scope_type) == TYPE_CODE_METHOD)
279 return lookup_symbol (nested, SYMBOL_BLOCK_VALUE (scope_sym.symbol),
280 VAR_DOMAIN, NULL);
281
282 /* Look for a symbol named NESTED in this class/namespace.
6f27419a
DE
283 The caller is assumed to have already have done a basic lookup of NAME.
284 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
e68cb8e0
PA
285 return cp_lookup_nested_symbol_1 (scope_type, nested, name,
286 block, domain, 0, is_in_anonymous);
34ef8452
DE
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;
05d49c37 565 std::string name_copy (SYMBOL_NATURAL_NAME (function));
34eaf542 566 const struct language_defn *lang = language_def (language_cplus);
08be3fe3 567 struct gdbarch *arch = symbol_arch (function);
34eaf542 568 const struct block *parent = BLOCK_SUPERBLOCK (block);
d12307c1 569 struct symbol *sym;
34eaf542
TT
570
571 while (1)
572 {
05d49c37
TT
573 unsigned int prefix_len
574 = cp_entire_prefix_len (name_copy.c_str ());
34eaf542
TT
575
576 if (prefix_len == 0)
577 context = NULL;
578 else
579 {
05d49c37 580 name_copy.erase (prefix_len);
aff410f1 581 context = lookup_typename (lang, arch,
05d49c37 582 name_copy.c_str (),
aff410f1 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 594 {
cc485e62
DE
595 if (symbol_lookup_debug)
596 {
d12307c1
PMR
597 fprintf_unfiltered
598 (gdb_stdlog,
599 "cp_lookup_symbol_imports_or_template (...) = %s\n",
600 host_address_to_string (sym));
cc485e62 601 }
d12307c1 602 return (struct block_symbol) {sym, parent};
c27e16e3 603 }
34eaf542 604 }
34eaf542
TT
605 }
606 }
607
4bd0864e 608 result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1);
cc485e62
DE
609 if (symbol_lookup_debug)
610 {
611 fprintf_unfiltered (gdb_stdlog,
612 "cp_lookup_symbol_imports_or_template (...) = %s\n",
d12307c1
PMR
613 result.symbol != NULL
614 ? host_address_to_string (result.symbol) : "NULL");
cc485e62
DE
615 }
616 return result;
34eaf542
TT
617}
618
f953163f
DE
619/* Search for NAME by applying relevant import statements belonging to BLOCK
620 and its parents. SCOPE is the namespace scope of the context in which the
621 search is being evaluated. */
622
d12307c1 623static struct block_symbol
f953163f
DE
624cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
625 const struct block *block,
626 const domain_enum domain)
627{
d12307c1 628 struct block_symbol sym;
f953163f
DE
629
630 while (block != NULL)
631 {
632 sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1);
d12307c1 633 if (sym.symbol)
f953163f
DE
634 return sym;
635
636 block = BLOCK_SUPERBLOCK (block);
637 }
638
b6b80672 639 return null_block_symbol;
f953163f
DE
640}
641
791244be
DE
642/* Searches for NAME in the current namespace, and by applying
643 relevant import statements belonging to BLOCK and its parents.
644 SCOPE is the namespace scope of the context in which the search is
645 being evaluated. */
8540c487 646
d12307c1 647struct block_symbol
8540c487
SW
648cp_lookup_symbol_namespace (const char *scope,
649 const char *name,
8540c487 650 const struct block *block,
13387711 651 const domain_enum domain)
8540c487 652{
d12307c1 653 struct block_symbol sym;
cc485e62
DE
654
655 if (symbol_lookup_debug)
656 {
657 fprintf_unfiltered (gdb_stdlog,
658 "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
659 scope, name, host_address_to_string (block),
660 domain_name (domain));
661 }
662
13387711 663 /* First, try to find the symbol in the given namespace. */
f953163f 664 sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
8540c487 665
f953163f 666 /* Search for name in namespaces imported to this and parent blocks. */
d12307c1 667 if (sym.symbol == NULL)
f953163f 668 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
8540c487 669
cc485e62
DE
670 if (symbol_lookup_debug)
671 {
672 fprintf_unfiltered (gdb_stdlog,
f953163f 673 "cp_lookup_symbol_namespace (...) = %s\n",
d12307c1
PMR
674 sym.symbol != NULL
675 ? host_address_to_string (sym.symbol) : "NULL");
cc485e62 676 }
f953163f 677 return sym;
1fcb5155
DC
678}
679
680/* Lookup NAME at namespace scope (or, in C terms, in static and
681 global variables). SCOPE is the namespace that the current
682 function is defined within; only consider namespaces whose length
683 is at least SCOPE_LEN. Other arguments are as in
684 cp_lookup_symbol_nonlocal.
685
686 For example, if we're within a function A::B::f and looking for a
3882f37a 687 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
1fcb5155
DC
688 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
689 but with SCOPE_LEN = 1. And then it calls itself with NAME and
690 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
691 "A::B::x"; if it doesn't find it, then the second call looks for
692 "A::x", and if that call fails, then the first call looks for
693 "x". */
694
d12307c1 695static struct block_symbol
1994afbf
DE
696lookup_namespace_scope (const struct language_defn *langdef,
697 const char *name,
1fcb5155
DC
698 const struct block *block,
699 const domain_enum domain,
1fcb5155
DC
700 const char *scope,
701 int scope_len)
702{
fe978cb0 703 char *the_namespace;
1fcb5155
DC
704
705 if (scope[scope_len] != '\0')
706 {
707 /* Recursively search for names in child namespaces first. */
708
d12307c1 709 struct block_symbol sym;
1fcb5155
DC
710 int new_scope_len = scope_len;
711
712 /* If the current scope is followed by "::", skip past that. */
713 if (new_scope_len != 0)
714 {
715 gdb_assert (scope[new_scope_len] == ':');
716 new_scope_len += 2;
717 }
718 new_scope_len += cp_find_first_component (scope + new_scope_len);
1994afbf 719 sym = lookup_namespace_scope (langdef, name, block, domain,
aff410f1 720 scope, new_scope_len);
d12307c1 721 if (sym.symbol != NULL)
1fcb5155
DC
722 return sym;
723 }
724
725 /* Okay, we didn't find a match in our children, so look for the
1994afbf
DE
726 name in the current namespace.
727
728 If we there is no scope and we know we have a bare symbol, then short
729 circuit everything and call cp_lookup_bare_symbol directly.
730 This isn't an optimization, rather it allows us to pass LANGDEF which
731 is needed for primitive type lookup. The test doesn't have to be
732 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
733 template symbol with "::" in the argument list) then
734 cp_lookup_symbol_in_namespace will catch it. */
735
736 if (scope_len == 0 && strchr (name, ':') == NULL)
737 return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
1fcb5155 738
224c3ddb 739 the_namespace = (char *) alloca (scope_len + 1);
fe978cb0
PA
740 strncpy (the_namespace, scope, scope_len);
741 the_namespace[scope_len] = '\0';
742 return cp_lookup_symbol_in_namespace (the_namespace, name,
8dea366b 743 block, domain, 1);
1fcb5155
DC
744}
745
56286edf
DE
746/* The C++-specific version of name lookup for static and global
747 names. This makes sure that names get looked for in all namespaces
748 that are in scope. NAME is the natural name of the symbol that
749 we're looking for, BLOCK is the block that we're searching within,
750 DOMAIN says what kind of symbols we're looking for. */
751
d12307c1 752struct block_symbol
f606139a
DE
753cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
754 const char *name,
56286edf
DE
755 const struct block *block,
756 const domain_enum domain)
757{
d12307c1 758 struct block_symbol sym;
56286edf
DE
759 const char *scope = block_scope (block);
760
cc485e62
DE
761 if (symbol_lookup_debug)
762 {
763 fprintf_unfiltered (gdb_stdlog,
764 "cp_lookup_symbol_non_local"
765 " (%s, %s (scope %s), %s)\n",
766 name, host_address_to_string (block), scope,
767 domain_name (domain));
768 }
769
f953163f
DE
770 /* First, try to find the symbol in the given namespace, and all
771 containing namespaces. */
1994afbf 772 sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
56286edf 773
f953163f 774 /* Search for name in namespaces imported to this and parent blocks. */
d12307c1 775 if (sym.symbol == NULL)
f953163f
DE
776 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
777
cc485e62
DE
778 if (symbol_lookup_debug)
779 {
780 fprintf_unfiltered (gdb_stdlog,
781 "cp_lookup_symbol_nonlocal (...) = %s\n",
d12307c1
PMR
782 (sym.symbol != NULL
783 ? host_address_to_string (sym.symbol)
784 : "NULL"));
cc485e62
DE
785 }
786 return sym;
56286edf
DE
787}
788
f7e3ecae
KS
789/* Search through the base classes of PARENT_TYPE for a base class
790 named NAME and return its type. If not found, return NULL. */
791
792struct type *
a07e3e18 793cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
f7e3ecae
KS
794{
795 int i;
796
f168693b 797 parent_type = check_typedef (parent_type);
f7e3ecae
KS
798 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
799 {
800 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
801 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
802
803 if (base_name == NULL)
804 continue;
805
806 if (streq (base_name, name))
807 return type;
808
a07e3e18 809 type = cp_find_type_baseclass_by_name (type, name);
f7e3ecae
KS
810 if (type != NULL)
811 return type;
812 }
813
814 return NULL;
815}
816
8dea366b
KS
817/* Search through the base classes of PARENT_TYPE for a symbol named
818 NAME in block BLOCK. */
819
d12307c1 820static struct block_symbol
8dea366b 821find_symbol_in_baseclass (struct type *parent_type, const char *name,
4dcabcc2
DE
822 const struct block *block, const domain_enum domain,
823 int is_in_anonymous)
8dea366b
KS
824{
825 int i;
d12307c1 826 struct block_symbol sym;
8dea366b 827
d12307c1
PMR
828 sym.symbol = NULL;
829 sym.block = NULL;
791244be 830
8dea366b
KS
831 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
832 {
69fc87c2 833 struct type *base_type = TYPE_BASECLASS (parent_type, i);
8dea366b
KS
834 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
835
836 if (base_name == NULL)
837 continue;
838
05d49c37 839 std::string concatenated_name = std::string (base_name) + "::" + name;
8dea366b 840
05d49c37
TT
841 sym = cp_lookup_nested_symbol_1 (base_type, name,
842 concatenated_name.c_str (),
4dcabcc2 843 block, domain, 1, is_in_anonymous);
d12307c1 844 if (sym.symbol != NULL)
69fc87c2 845 break;
8dea366b
KS
846 }
847
0c2e6019 848 return sym;
5c4e30ca
DC
849}
850
4dcabcc2
DE
851/* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
852 and within the context of BLOCK.
d5ff0482 853 NESTED_NAME may have scope ("::").
6f27419a
DE
854 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
855 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
856 passed as an argument so that callers can control how space for it is
857 allocated.
858 If BASIC_LOOKUP is non-zero then perform a basic lookup of
96553a0c
DE
859 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
860 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
861 namespace. */
6f27419a 862
d12307c1 863static struct block_symbol
6f27419a
DE
864cp_lookup_nested_symbol_1 (struct type *container_type,
865 const char *nested_name,
866 const char *concatenated_name,
867 const struct block *block,
4dcabcc2 868 const domain_enum domain,
96553a0c 869 int basic_lookup, int is_in_anonymous)
6f27419a 870{
d12307c1 871 struct block_symbol sym;
6f27419a
DE
872
873 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
874 of classes like, say, data or function members. Instead,
875 they're just represented by symbols whose names are
876 qualified by the name of the surrounding class. This is
877 just like members of namespaces; in particular,
878 cp_basic_lookup_symbol works when looking them up. */
879
880 if (basic_lookup)
881 {
4dcabcc2 882 sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
6f27419a 883 is_in_anonymous);
d12307c1 884 if (sym.symbol != NULL)
6f27419a
DE
885 return sym;
886 }
887
888 /* Now search all static file-level symbols. We have to do this for things
889 like typedefs in the class. We do not try to guess any imported
890 namespace as even the fully specified namespace search is already not
891 C++ compliant and more assumptions could make it too magic. */
892
893 /* First search in this symtab, what we want is possibly there. */
4dcabcc2 894 sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
d12307c1 895 if (sym.symbol != NULL)
6f27419a
DE
896 return sym;
897
898 /* Nope. We now have to search all static blocks in all objfiles,
899 even if block != NULL, because there's no guarantees as to which
96553a0c
DE
900 symtab the symbol we want is in. Except for symbols defined in
901 anonymous namespaces should be treated as local to a single file,
902 which we just searched. */
903 if (!is_in_anonymous)
904 {
4dcabcc2 905 sym = lookup_static_symbol (concatenated_name, domain);
d12307c1 906 if (sym.symbol != NULL)
96553a0c
DE
907 return sym;
908 }
6f27419a
DE
909
910 /* If this is a class with baseclasses, search them next. */
f168693b 911 container_type = check_typedef (container_type);
6f27419a
DE
912 if (TYPE_N_BASECLASSES (container_type) > 0)
913 {
96553a0c 914 sym = find_symbol_in_baseclass (container_type, nested_name, block,
4dcabcc2 915 domain, is_in_anonymous);
d12307c1 916 if (sym.symbol != NULL)
6f27419a
DE
917 return sym;
918 }
919
b6b80672 920 return null_block_symbol;
6f27419a
DE
921}
922
50af5481 923/* Look up a symbol named NESTED_NAME that is nested inside the C++
79c2c32d 924 class or namespace given by PARENT_TYPE, from within the context
4dcabcc2
DE
925 given by BLOCK, and in DOMAIN.
926 Return NULL if there is no such nested symbol. */
79c2c32d 927
d12307c1 928struct block_symbol
50af5481
JK
929cp_lookup_nested_symbol (struct type *parent_type,
930 const char *nested_name,
4dcabcc2
DE
931 const struct block *block,
932 const domain_enum domain)
79c2c32d 933{
05a6c3c8 934 /* type_name_no_tag_or_error provides better error reporting using the
d8228535
JK
935 original type. */
936 struct type *saved_parent_type = parent_type;
937
f168693b 938 parent_type = check_typedef (parent_type);
d8228535 939
cc485e62
DE
940 if (symbol_lookup_debug)
941 {
942 const char *type_name = type_name_no_tag (saved_parent_type);
943
944 fprintf_unfiltered (gdb_stdlog,
4dcabcc2 945 "cp_lookup_nested_symbol (%s, %s, %s, %s)\n",
cc485e62 946 type_name != NULL ? type_name : "unnamed",
4dcabcc2
DE
947 nested_name, host_address_to_string (block),
948 domain_name (domain));
cc485e62
DE
949 }
950
79c2c32d
DC
951 switch (TYPE_CODE (parent_type))
952 {
63d06c5c 953 case TYPE_CODE_STRUCT:
79c2c32d 954 case TYPE_CODE_NAMESPACE:
48e32051 955 case TYPE_CODE_UNION:
3d567982 956 case TYPE_CODE_ENUM:
530e8392
KB
957 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
958 specific code to lookup nested symbols in modules, by calling the
959 function pointer la_lookup_symbol_nonlocal, which ends up here. */
960 case TYPE_CODE_MODULE:
79c2c32d 961 {
08850b56 962 int size;
d8228535 963 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
d12307c1 964 struct block_symbol sym;
41f62f39 965 char *concatenated_name;
96553a0c 966 int is_in_anonymous;
c5504eaf 967
08850b56 968 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
224c3ddb 969 concatenated_name = (char *) alloca (size);
08850b56 970 xsnprintf (concatenated_name, size, "%s::%s",
6f27419a 971 parent_name, nested_name);
96553a0c 972 is_in_anonymous = cp_is_in_anonymous (concatenated_name);
6f27419a
DE
973
974 sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
4dcabcc2
DE
975 concatenated_name, block, domain,
976 1, is_in_anonymous);
41f62f39 977
cc485e62
DE
978 if (symbol_lookup_debug)
979 {
980 fprintf_unfiltered (gdb_stdlog,
981 "cp_lookup_nested_symbol (...) = %s\n",
d12307c1
PMR
982 (sym.symbol != NULL
983 ? host_address_to_string (sym.symbol)
984 : "NULL"));
cc485e62
DE
985 }
986 return sym;
79c2c32d 987 }
bb869963
SDJ
988
989 case TYPE_CODE_FUNC:
990 case TYPE_CODE_METHOD:
cc485e62
DE
991 if (symbol_lookup_debug)
992 {
993 fprintf_unfiltered (gdb_stdlog,
994 "cp_lookup_nested_symbol (...) = NULL"
995 " (func/method)\n");
996 }
b6b80672 997 return null_block_symbol;
bb869963 998
79c2c32d
DC
999 default:
1000 internal_error (__FILE__, __LINE__,
50af5481 1001 _("cp_lookup_nested_symbol called "
3e43a32a 1002 "on a non-aggregate type."));
79c2c32d
DC
1003 }
1004}
1005
b368761e
DC
1006/* The C++-version of lookup_transparent_type. */
1007
1008/* FIXME: carlton/2004-01-16: The problem that this is trying to
1009 address is that, unfortunately, sometimes NAME is wrong: it may not
1010 include the name of namespaces enclosing the type in question.
b021a221 1011 lookup_transparent_type gets called when the type in question
b368761e
DC
1012 is a declaration, and we're trying to find its definition; but, for
1013 declarations, our type name deduction mechanism doesn't work.
1014 There's nothing we can do to fix this in general, I think, in the
1015 absence of debug information about namespaces (I've filed PR
1016 gdb/1511 about this); until such debug information becomes more
1017 prevalent, one heuristic which sometimes looks is to search for the
1018 definition in namespaces containing the current namespace.
1019
1020 We should delete this functions once the appropriate debug
1021 information becomes more widespread. (GCC 3.4 will be the first
1022 released version of GCC with such information.) */
1023
1024struct type *
1025cp_lookup_transparent_type (const char *name)
1026{
1027 /* First, try the honest way of looking up the definition. */
1028 struct type *t = basic_lookup_transparent_type (name);
1029 const char *scope;
1030
1031 if (t != NULL)
1032 return t;
1033
1034 /* If that doesn't work and we're within a namespace, look there
1035 instead. */
1036 scope = block_scope (get_selected_block (0));
1037
1038 if (scope[0] == '\0')
1039 return NULL;
1040
1041 return cp_lookup_transparent_type_loop (name, scope, 0);
1042}
1043
b021a221
MS
1044/* Lookup the type definition associated to NAME in namespaces/classes
1045 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1046 must be the index of the start of a component of SCOPE. */
b368761e
DC
1047
1048static struct type *
aff410f1
MS
1049cp_lookup_transparent_type_loop (const char *name,
1050 const char *scope,
b368761e
DC
1051 int length)
1052{
1198ecbe 1053 int scope_length = length + cp_find_first_component (scope + length);
b368761e
DC
1054 char *full_name;
1055
1056 /* If the current scope is followed by "::", look in the next
1057 component. */
1058 if (scope[scope_length] == ':')
1059 {
1060 struct type *retval
aff410f1
MS
1061 = cp_lookup_transparent_type_loop (name, scope,
1062 scope_length + 2);
c5504eaf 1063
b368761e
DC
1064 if (retval != NULL)
1065 return retval;
1066 }
1067
224c3ddb 1068 full_name = (char *) alloca (scope_length + 2 + strlen (name) + 1);
b368761e
DC
1069 strncpy (full_name, scope, scope_length);
1070 strncpy (full_name + scope_length, "::", 2);
1071 strcpy (full_name + scope_length + 2, name);
1072
1073 return basic_lookup_transparent_type (full_name);
1074}
1075
0c2e6019
TT
1076/* This used to do something but was removed when it became
1077 obsolete. */
5c4e30ca
DC
1078
1079static void
1080maintenance_cplus_namespace (char *args, int from_tty)
1081{
0c2e6019 1082 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
5c4e30ca
DC
1083}
1084
1085void
1086_initialize_cp_namespace (void)
1087{
0c2e6019
TT
1088 struct cmd_list_element *cmd;
1089
1090 cmd = add_cmd ("namespace", class_maintenance,
1091 maintenance_cplus_namespace,
1092 _("Deprecated placeholder for removed functionality."),
1093 &maint_cplus_cmd_list);
1094 deprecate_cmd (cmd, NULL);
1fcb5155 1095}
This page took 1.077951 seconds and 4 git commands to generate.