* main.c (gdb_datadir_provided): New static global.
[deliverable/binutils-gdb.git] / gdb / cp-namespace.c
CommitLineData
9219021c 1/* Helper routines for C++ support in GDB.
0b302171 2 Copyright (C) 2003-2004, 2007-2012 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"
26#include "gdb_assert.h"
27#include "block.h"
5c4e30ca
DC
28#include "objfiles.h"
29#include "gdbtypes.h"
30#include "dictionary.h"
31#include "command.h"
b368761e 32#include "frame.h"
27aa8d6a 33#include "buildsym.h"
34eaf542 34#include "language.h"
9219021c 35
1fcb5155 36static struct symbol *lookup_namespace_scope (const char *name,
1fcb5155
DC
37 const struct block *block,
38 const domain_enum domain,
1fcb5155
DC
39 const char *scope,
40 int scope_len);
41
42static struct symbol *lookup_symbol_file (const char *name,
1fcb5155
DC
43 const struct block *block,
44 const domain_enum domain,
1fcb5155
DC
45 int anonymous_namespace);
46
b368761e
DC
47static struct type *cp_lookup_transparent_type_loop (const char *name,
48 const char *scope,
49 int scope_len);
50
9219021c
DC
51/* Check to see if SYMBOL refers to an object contained within an
52 anonymous namespace; if so, add an appropriate using directive. */
53
9219021c 54void
a10964d1
AR
55cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
56 struct objfile *const objfile)
9219021c 57{
df8a16a1 58 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
9219021c 59 {
df8a16a1 60 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
9219021c
DC
61 unsigned int previous_component;
62 unsigned int next_component;
9219021c
DC
63
64 /* Start with a quick-and-dirty check for mention of "(anonymous
65 namespace)". */
66
67 if (!cp_is_anonymous (name))
68 return;
69
70 previous_component = 0;
71 next_component = cp_find_first_component (name + previous_component);
72
73 while (name[next_component] == ':')
74 {
2b1dbab0
KS
75 if (((next_component - previous_component)
76 == CP_ANONYMOUS_NAMESPACE_LEN)
9219021c 77 && strncmp (name + previous_component,
2b1dbab0
KS
78 CP_ANONYMOUS_NAMESPACE_STR,
79 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
9219021c 80 {
aff410f1
MS
81 int dest_len = (previous_component == 0
82 ? 0 : previous_component - 2);
8c902bb1 83 int src_len = next_component;
794684b6 84
8c902bb1
SW
85 char *dest = alloca (dest_len + 1);
86 char *src = alloca (src_len + 1);
794684b6 87
8c902bb1
SW
88 memcpy (dest, name, dest_len);
89 memcpy (src, name, src_len);
794684b6 90
8c902bb1
SW
91 dest[dest_len] = '\0';
92 src[src_len] = '\0';
794684b6 93
9219021c
DC
94 /* We've found a component of the name that's an
95 anonymous namespace. So add symbols in it to the
96 namespace given by the previous component if there is
97 one, or to the global namespace if there isn't. */
32019081 98 cp_add_using_directive (dest, src, NULL, NULL, NULL,
a10964d1 99 &objfile->objfile_obstack);
9219021c
DC
100 }
101 /* The "+ 2" is for the "::". */
102 previous_component = next_component + 2;
103 next_component = (previous_component
104 + cp_find_first_component (name
105 + previous_component));
106 }
107 }
108}
109
c0cc3a76 110
aff410f1
MS
111/* Add a using directive to using_directives. If the using directive
112 in question has already been added, don't add it twice.
113
114 Create a new struct using_direct which imports the namespace SRC
115 into the scope DEST. ALIAS is the name of the imported namespace
116 in the current scope. If ALIAS is NULL then the namespace is known
117 by its original name. DECLARATION is the name if the imported
118 varable if this is a declaration import (Eg. using A::x), otherwise
32019081
JK
119 it is NULL. EXCLUDES is a list of names not to import from an imported
120 module or NULL. The arguments are copied into newly allocated memory so
121 they can be temporaries. For EXCLUDES the VEC pointers are copied but the
122 pointed to characters are not copied. */
9219021c
DC
123
124void
13387711
SW
125cp_add_using_directive (const char *dest,
126 const char *src,
127 const char *alias,
128 const char *declaration,
32019081 129 VEC (const_char_ptr) *excludes,
c0cc3a76 130 struct obstack *obstack)
9219021c
DC
131{
132 struct using_direct *current;
133 struct using_direct *new;
13387711 134
9219021c
DC
135 /* Has it already been added? */
136
27aa8d6a 137 for (current = using_directives; current != NULL; current = current->next)
9219021c 138 {
32019081
JK
139 int ix;
140 const char *param;
141
70c622a3
JK
142 if (strcmp (current->import_src, src) != 0)
143 continue;
144 if (strcmp (current->import_dest, dest) != 0)
145 continue;
146 if ((alias == NULL && current->alias != NULL)
147 || (alias != NULL && current->alias == NULL)
148 || (alias != NULL && current->alias != NULL
149 && strcmp (alias, current->alias) != 0))
150 continue;
151 if ((declaration == NULL && current->declaration != NULL)
152 || (declaration != NULL && current->declaration == NULL)
153 || (declaration != NULL && current->declaration != NULL
154 && strcmp (declaration, current->declaration) != 0))
155 continue;
156
32019081
JK
157 /* Compare the contents of EXCLUDES. */
158 for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
159 if (current->excludes[ix] == NULL
160 || strcmp (param, current->excludes[ix]) != 0)
161 break;
162 if (ix < VEC_length (const_char_ptr, excludes)
163 || current->excludes[ix] != NULL)
164 continue;
165
70c622a3
JK
166 /* Parameters exactly match CURRENT. */
167 return;
9219021c
DC
168 }
169
32019081
JK
170 new = obstack_alloc (obstack, (sizeof (*new)
171 + (VEC_length (const_char_ptr, excludes)
172 * sizeof (*new->excludes))));
173 memset (new, 0, sizeof (*new));
c0cc3a76
SW
174
175 new->import_src = obsavestring (src, strlen (src), obstack);
176 new->import_dest = obsavestring (dest, strlen (dest), obstack);
794684b6 177
c0cc3a76
SW
178 if (alias != NULL)
179 new->alias = obsavestring (alias, strlen (alias), obstack);
180
13387711
SW
181 if (declaration != NULL)
182 new->declaration = obsavestring (declaration, strlen (declaration),
183 obstack);
184
32019081
JK
185 memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
186 VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
187 new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
188
c0cc3a76
SW
189 new->next = using_directives;
190 using_directives = new;
9219021c
DC
191}
192
193/* Record the namespace that the function defined by SYMBOL was
194 defined in, if necessary. BLOCK is the associated block; use
195 OBSTACK for allocation. */
196
197void
198cp_set_block_scope (const struct symbol *symbol,
199 struct block *block,
df8a16a1
DJ
200 struct obstack *obstack,
201 const char *processing_current_prefix,
202 int processing_has_namespace_info)
9219021c 203{
df8a16a1 204 if (processing_has_namespace_info)
9219021c 205 {
df8a16a1
DJ
206 block_set_scope
207 (block, obsavestring (processing_current_prefix,
208 strlen (processing_current_prefix),
209 obstack),
210 obstack);
211 }
212 else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
213 {
214 /* Try to figure out the appropriate namespace from the
215 demangled name. */
9219021c 216
df8a16a1
DJ
217 /* FIXME: carlton/2003-04-15: If the function in question is
218 a method of a class, the name will actually include the
219 name of the class as well. This should be harmless, but
220 is a little unfortunate. */
9219021c 221
df8a16a1
DJ
222 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
223 unsigned int prefix_len = cp_entire_prefix_len (name);
9219021c 224
df8a16a1
DJ
225 block_set_scope (block,
226 obsavestring (name, prefix_len, obstack),
227 obstack);
9219021c
DC
228 }
229}
230
231/* Test whether or not NAMESPACE looks like it mentions an anonymous
232 namespace; return nonzero if so. */
233
234int
235cp_is_anonymous (const char *namespace)
236{
2b1dbab0 237 return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
9219021c
DC
238 != NULL);
239}
240
1fcb5155
DC
241/* The C++-specific version of name lookup for static and global
242 names. This makes sure that names get looked for in all namespaces
243 that are in scope. NAME is the natural name of the symbol that
94af9270 244 we're looking for, BLOCK is the block that we're searching within,
aff410f1
MS
245 DOMAIN says what kind of symbols we're looking for, and if SYMTAB
246 is non-NULL, we should store the symtab where we found the symbol
247 in it. */
1fcb5155
DC
248
249struct symbol *
250cp_lookup_symbol_nonlocal (const char *name,
1fcb5155 251 const struct block *block,
21b556f4 252 const domain_enum domain)
1fcb5155 253{
8540c487
SW
254 struct symbol *sym;
255 const char *scope = block_scope (block);
256
aff410f1
MS
257 sym = lookup_namespace_scope (name, block,
258 domain, scope, 0);
8540c487
SW
259 if (sym != NULL)
260 return sym;
261
aff410f1
MS
262 return cp_lookup_symbol_namespace (scope, name,
263 block, domain);
8540c487
SW
264}
265
aff410f1
MS
266/* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
267 as in cp_lookup_symbol_nonlocal. */
8540c487
SW
268
269static struct symbol *
270cp_lookup_symbol_in_namespace (const char *namespace,
271 const char *name,
8540c487
SW
272 const struct block *block,
273 const domain_enum domain)
274{
275 if (namespace[0] == '\0')
276 {
94af9270 277 return lookup_symbol_file (name, block, domain, 0);
8540c487
SW
278 }
279 else
280 {
aff410f1
MS
281 char *concatenated_name = alloca (strlen (namespace) + 2
282 + strlen (name) + 1);
c5504eaf 283
8540c487
SW
284 strcpy (concatenated_name, namespace);
285 strcat (concatenated_name, "::");
286 strcat (concatenated_name, name);
aff410f1
MS
287 return lookup_symbol_file (concatenated_name, block, domain,
288 cp_is_anonymous (namespace));
8540c487
SW
289 }
290}
291
b14e635e
SW
292/* Used for cleanups to reset the "searched" flag incase
293 of an error. */
294
295static void
296reset_directive_searched (void *data)
297{
298 struct using_direct *direct = data;
299 direct->searched = 0;
300}
301
aff410f1
MS
302/* Search for NAME by applying all import statements belonging to
303 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
304 search is restricted to using declarations.
13387711
SW
305 Example:
306
aff410f1 307 namespace A {
13387711
SW
308 int x;
309 }
310 using A::x;
311
aff410f1
MS
312 If SEARCH_PARENTS the search will include imports which are
313 applicable in parents of SCOPE.
b14e635e
SW
314 Example:
315
aff410f1 316 namespace A {
b14e635e 317 using namespace X;
aff410f1 318 namespace B {
b14e635e
SW
319 using namespace Y;
320 }
321 }
322
aff410f1
MS
323 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
324 namespaces X and Y will be considered. If SEARCH_PARENTS is false
325 only the import of Y is considered. */
8540c487 326
13387711 327struct symbol *
8540c487
SW
328cp_lookup_symbol_imports (const char *scope,
329 const char *name,
8540c487 330 const struct block *block,
b14e635e 331 const domain_enum domain,
13387711 332 const int declaration_only,
b14e635e 333 const int search_parents)
8540c487 334{
b14e635e 335 struct using_direct *current;
13387711 336 struct symbol *sym = NULL;
8540c487 337 int len;
b14e635e
SW
338 int directive_match;
339 struct cleanup *searched_cleanup;
8540c487
SW
340
341 /* First, try to find the symbol in the given namespace. */
13387711 342 if (!declaration_only)
aff410f1
MS
343 sym = cp_lookup_symbol_in_namespace (scope, name,
344 block, domain);
13387711 345
8540c487
SW
346 if (sym != NULL)
347 return sym;
348
aff410f1
MS
349 /* Go through the using directives. If any of them add new names to
350 the namespace we're searching in, see if we can find a match by
351 applying them. */
8540c487
SW
352
353 for (current = block_using (block);
354 current != NULL;
355 current = current->next)
356 {
32019081
JK
357 const char **excludep;
358
b14e635e
SW
359 len = strlen (current->import_dest);
360 directive_match = (search_parents
361 ? (strncmp (scope, current->import_dest,
362 strlen (current->import_dest)) == 0
363 && (len == 0
aff410f1
MS
364 || scope[len] == ':'
365 || scope[len] == '\0'))
b14e635e 366 : strcmp (scope, current->import_dest) == 0);
8540c487 367
aff410f1
MS
368 /* If the import destination is the current scope or one of its
369 ancestors then it is applicable. */
b14e635e 370 if (directive_match && !current->searched)
8540c487 371 {
a1d705ee
TT
372 /* Mark this import as searched so that the recursive call
373 does not search it again. */
374 current->searched = 1;
375 searched_cleanup = make_cleanup (reset_directive_searched,
376 current);
377
378 /* If there is an import of a single declaration, compare the
379 imported declaration (after optional renaming by its alias)
380 with the sought out name. If there is a match pass
381 current->import_src as NAMESPACE to direct the search
382 towards the imported namespace. */
383 if (current->declaration
384 && strcmp (name, current->alias
385 ? current->alias : current->declaration) == 0)
386 sym = cp_lookup_symbol_in_namespace (current->import_src,
387 current->declaration,
aff410f1 388 block, domain);
a1d705ee
TT
389
390 /* If this is a DECLARATION_ONLY search or a symbol was found
391 or this import statement was an import declaration, the
392 search of this import is complete. */
393 if (declaration_only || sym != NULL || current->declaration)
394 {
395 current->searched = 0;
396 discard_cleanups (searched_cleanup);
397
398 if (sym != NULL)
399 return sym;
400
401 continue;
402 }
403
404 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
405 for (excludep = current->excludes; *excludep; excludep++)
406 if (strcmp (name, *excludep) == 0)
407 break;
408 if (*excludep)
409 {
410 discard_cleanups (searched_cleanup);
411 continue;
412 }
413
414 if (current->alias != NULL
415 && strcmp (name, current->alias) == 0)
416 /* If the import is creating an alias and the alias matches
417 the sought name. Pass current->import_src as the NAME to
418 direct the search towards the aliased namespace. */
419 {
420 sym = cp_lookup_symbol_in_namespace (scope,
421 current->import_src,
422 block, domain);
423 }
424 else if (current->alias == NULL)
425 {
426 /* If this import statement creates no alias, pass
427 current->inner as NAMESPACE to direct the search
428 towards the imported namespace. */
429 sym = cp_lookup_symbol_imports (current->import_src,
430 name, block,
431 domain, 0, 0);
432 }
433 current->searched = 0;
434 discard_cleanups (searched_cleanup);
435
436 if (sym != NULL)
437 return sym;
8540c487
SW
438 }
439 }
440
441 return NULL;
442}
443
34eaf542
TT
444/* Helper function that searches an array of symbols for one named
445 NAME. */
446
447static struct symbol *
aff410f1
MS
448search_symbol_list (const char *name, int num,
449 struct symbol **syms)
34eaf542
TT
450{
451 int i;
452
453 /* Maybe we should store a dictionary in here instead. */
454 for (i = 0; i < num; ++i)
455 {
456 if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
457 return syms[i];
458 }
459 return NULL;
460}
461
462/* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
463 searches through the template parameters of the function and the
464 function's type. */
465
466struct symbol *
467cp_lookup_symbol_imports_or_template (const char *scope,
468 const char *name,
469 const struct block *block,
470 const domain_enum domain)
471{
472 struct symbol *function = BLOCK_FUNCTION (block);
473
474 if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
475 {
476 int i;
477 struct cplus_specific *cps
478 = function->ginfo.language_specific.cplus_specific;
479
480 /* Search the function's template parameters. */
481 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
482 {
aff410f1
MS
483 struct template_symbol *templ
484 = (struct template_symbol *) function;
34eaf542
TT
485 struct symbol *result;
486
487 result = search_symbol_list (name,
488 templ->n_template_arguments,
489 templ->template_arguments);
490 if (result != NULL)
491 return result;
492 }
493
494 /* Search the template parameters of the function's defining
495 context. */
496 if (SYMBOL_NATURAL_NAME (function))
497 {
498 struct type *context;
499 char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
500 struct cleanup *cleanups = make_cleanup (xfree, name_copy);
501 const struct language_defn *lang = language_def (language_cplus);
502 struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch;
503 const struct block *parent = BLOCK_SUPERBLOCK (block);
504
505 while (1)
506 {
507 struct symbol *result;
508 unsigned int prefix_len = cp_entire_prefix_len (name_copy);
509
510 if (prefix_len == 0)
511 context = NULL;
512 else
513 {
514 name_copy[prefix_len] = '\0';
aff410f1
MS
515 context = lookup_typename (lang, arch,
516 name_copy,
517 parent, 1);
34eaf542
TT
518 }
519
520 if (context == NULL)
521 break;
522
aff410f1
MS
523 result
524 = search_symbol_list (name,
525 TYPE_N_TEMPLATE_ARGUMENTS (context),
526 TYPE_TEMPLATE_ARGUMENTS (context));
34eaf542
TT
527 if (result != NULL)
528 return result;
529 }
530
531 do_cleanups (cleanups);
532 }
533 }
534
535 return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
536}
537
aff410f1
MS
538 /* Searches for NAME in the current namespace, and by applying
539 relevant import statements belonging to BLOCK and its parents.
540 SCOPE is the namespace scope of the context in which the search is
541 being evaluated. */
8540c487
SW
542
543struct symbol*
544cp_lookup_symbol_namespace (const char *scope,
545 const char *name,
8540c487 546 const struct block *block,
13387711 547 const domain_enum domain)
8540c487
SW
548{
549 struct symbol *sym;
13387711
SW
550
551 /* First, try to find the symbol in the given namespace. */
aff410f1
MS
552 sym = cp_lookup_symbol_in_namespace (scope, name,
553 block, domain);
13387711
SW
554 if (sym != NULL)
555 return sym;
8540c487 556
aff410f1
MS
557 /* Search for name in namespaces imported to this and parent
558 blocks. */
8540c487
SW
559 while (block != NULL)
560 {
aff410f1
MS
561 sym = cp_lookup_symbol_imports (scope, name, block,
562 domain, 0, 1);
8540c487
SW
563
564 if (sym)
565 return sym;
566
567 block = BLOCK_SUPERBLOCK (block);
568 }
569
570 return NULL;
1fcb5155
DC
571}
572
573/* Lookup NAME at namespace scope (or, in C terms, in static and
574 global variables). SCOPE is the namespace that the current
575 function is defined within; only consider namespaces whose length
576 is at least SCOPE_LEN. Other arguments are as in
577 cp_lookup_symbol_nonlocal.
578
579 For example, if we're within a function A::B::f and looking for a
3882f37a 580 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
1fcb5155
DC
581 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
582 but with SCOPE_LEN = 1. And then it calls itself with NAME and
583 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
584 "A::B::x"; if it doesn't find it, then the second call looks for
585 "A::x", and if that call fails, then the first call looks for
586 "x". */
587
588static struct symbol *
589lookup_namespace_scope (const char *name,
1fcb5155
DC
590 const struct block *block,
591 const domain_enum domain,
1fcb5155
DC
592 const char *scope,
593 int scope_len)
594{
595 char *namespace;
596
597 if (scope[scope_len] != '\0')
598 {
599 /* Recursively search for names in child namespaces first. */
600
601 struct symbol *sym;
602 int new_scope_len = scope_len;
603
604 /* If the current scope is followed by "::", skip past that. */
605 if (new_scope_len != 0)
606 {
607 gdb_assert (scope[new_scope_len] == ':');
608 new_scope_len += 2;
609 }
610 new_scope_len += cp_find_first_component (scope + new_scope_len);
aff410f1
MS
611 sym = lookup_namespace_scope (name, block, domain,
612 scope, new_scope_len);
1fcb5155
DC
613 if (sym != NULL)
614 return sym;
615 }
616
617 /* Okay, we didn't find a match in our children, so look for the
618 name in the current namespace. */
619
620 namespace = alloca (scope_len + 1);
621 strncpy (namespace, scope, scope_len);
622 namespace[scope_len] = '\0';
aff410f1
MS
623 return cp_lookup_symbol_in_namespace (namespace, name,
624 block, domain);
1fcb5155
DC
625}
626
627/* Look up NAME in BLOCK's static block and in global blocks. If
628 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
629 within an anonymous namespace. Other arguments are as in
630 cp_lookup_symbol_nonlocal. */
631
632static struct symbol *
633lookup_symbol_file (const char *name,
1fcb5155
DC
634 const struct block *block,
635 const domain_enum domain,
1fcb5155
DC
636 int anonymous_namespace)
637{
638 struct symbol *sym = NULL;
639
94af9270 640 sym = lookup_symbol_static (name, block, domain);
1fcb5155
DC
641 if (sym != NULL)
642 return sym;
643
644 if (anonymous_namespace)
645 {
646 /* Symbols defined in anonymous namespaces have external linkage
647 but should be treated as local to a single file nonetheless.
648 So we only search the current file's global block. */
649
650 const struct block *global_block = block_global_block (block);
651
652 if (global_block != NULL)
94af9270 653 sym = lookup_symbol_aux_block (name, global_block, domain);
1fcb5155
DC
654 }
655 else
656 {
94af9270 657 sym = lookup_symbol_global (name, block, domain);
5c4e30ca
DC
658 }
659
0c2e6019 660 return sym;
5c4e30ca
DC
661}
662
50af5481 663/* Look up a symbol named NESTED_NAME that is nested inside the C++
79c2c32d
DC
664 class or namespace given by PARENT_TYPE, from within the context
665 given by BLOCK. Return NULL if there is no such nested type. */
666
50af5481
JK
667struct symbol *
668cp_lookup_nested_symbol (struct type *parent_type,
669 const char *nested_name,
670 const struct block *block)
79c2c32d 671{
d8228535
JK
672 /* type_name_no_tag_required provides better error reporting using the
673 original type. */
674 struct type *saved_parent_type = parent_type;
675
676 CHECK_TYPEDEF (parent_type);
677
79c2c32d
DC
678 switch (TYPE_CODE (parent_type))
679 {
63d06c5c 680 case TYPE_CODE_STRUCT:
79c2c32d 681 case TYPE_CODE_NAMESPACE:
48e32051 682 case TYPE_CODE_UNION:
79c2c32d 683 {
63d06c5c
DC
684 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
685 of classes like, say, data or function members. Instead,
686 they're just represented by symbols whose names are
687 qualified by the name of the surrounding class. This is
688 just like members of namespaces; in particular,
689 lookup_symbol_namespace works when looking them up. */
690
08850b56 691 int size;
d8228535 692 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
aff410f1
MS
693 struct symbol *sym
694 = cp_lookup_symbol_in_namespace (parent_name, nested_name,
695 block, VAR_DOMAIN);
41f62f39 696 char *concatenated_name;
c5504eaf 697
50af5481
JK
698 if (sym != NULL)
699 return sym;
41f62f39 700
aff410f1
MS
701 /* Now search all static file-level symbols. Not strictly
702 correct, but more useful than an error. We do not try to
703 guess any imported namespace as even the fully specified
704 namespace seach is is already not C++ compliant and more
705 assumptions could make it too magic. */
41f62f39 706
08850b56
PM
707 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
708 concatenated_name = alloca (size);
709 xsnprintf (concatenated_name, size, "%s::%s",
aff410f1 710 parent_name, nested_name);
50af5481
JK
711 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
712 if (sym != NULL)
713 return sym;
41f62f39
JK
714
715 return NULL;
79c2c32d
DC
716 }
717 default:
718 internal_error (__FILE__, __LINE__,
50af5481 719 _("cp_lookup_nested_symbol called "
3e43a32a 720 "on a non-aggregate type."));
79c2c32d
DC
721 }
722}
723
b368761e
DC
724/* The C++-version of lookup_transparent_type. */
725
726/* FIXME: carlton/2004-01-16: The problem that this is trying to
727 address is that, unfortunately, sometimes NAME is wrong: it may not
728 include the name of namespaces enclosing the type in question.
b021a221 729 lookup_transparent_type gets called when the type in question
b368761e
DC
730 is a declaration, and we're trying to find its definition; but, for
731 declarations, our type name deduction mechanism doesn't work.
732 There's nothing we can do to fix this in general, I think, in the
733 absence of debug information about namespaces (I've filed PR
734 gdb/1511 about this); until such debug information becomes more
735 prevalent, one heuristic which sometimes looks is to search for the
736 definition in namespaces containing the current namespace.
737
738 We should delete this functions once the appropriate debug
739 information becomes more widespread. (GCC 3.4 will be the first
740 released version of GCC with such information.) */
741
742struct type *
743cp_lookup_transparent_type (const char *name)
744{
745 /* First, try the honest way of looking up the definition. */
746 struct type *t = basic_lookup_transparent_type (name);
747 const char *scope;
748
749 if (t != NULL)
750 return t;
751
752 /* If that doesn't work and we're within a namespace, look there
753 instead. */
754 scope = block_scope (get_selected_block (0));
755
756 if (scope[0] == '\0')
757 return NULL;
758
759 return cp_lookup_transparent_type_loop (name, scope, 0);
760}
761
b021a221
MS
762/* Lookup the type definition associated to NAME in namespaces/classes
763 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
764 must be the index of the start of a component of SCOPE. */
b368761e
DC
765
766static struct type *
aff410f1
MS
767cp_lookup_transparent_type_loop (const char *name,
768 const char *scope,
b368761e
DC
769 int length)
770{
1198ecbe 771 int scope_length = length + cp_find_first_component (scope + length);
b368761e
DC
772 char *full_name;
773
774 /* If the current scope is followed by "::", look in the next
775 component. */
776 if (scope[scope_length] == ':')
777 {
778 struct type *retval
aff410f1
MS
779 = cp_lookup_transparent_type_loop (name, scope,
780 scope_length + 2);
c5504eaf 781
b368761e
DC
782 if (retval != NULL)
783 return retval;
784 }
785
786 full_name = alloca (scope_length + 2 + strlen (name) + 1);
787 strncpy (full_name, scope, scope_length);
788 strncpy (full_name + scope_length, "::", 2);
789 strcpy (full_name + scope_length + 2, name);
790
791 return basic_lookup_transparent_type (full_name);
792}
793
0c2e6019
TT
794/* This used to do something but was removed when it became
795 obsolete. */
5c4e30ca
DC
796
797static void
798maintenance_cplus_namespace (char *args, int from_tty)
799{
0c2e6019 800 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
5c4e30ca
DC
801}
802
2c0b251b
PA
803/* Provide a prototype to silence -Wmissing-prototypes. */
804extern initialize_file_ftype _initialize_cp_namespace;
805
5c4e30ca
DC
806void
807_initialize_cp_namespace (void)
808{
0c2e6019
TT
809 struct cmd_list_element *cmd;
810
811 cmd = add_cmd ("namespace", class_maintenance,
812 maintenance_cplus_namespace,
813 _("Deprecated placeholder for removed functionality."),
814 &maint_cplus_cmd_list);
815 deprecate_cmd (cmd, NULL);
1fcb5155 816}
This page took 0.652641 seconds and 4 git commands to generate.