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