Commit | Line | Data |
---|---|---|
9219021c | 1 | /* Helper routines for C++ support in GDB. |
4c38e0a4 JB |
2 | Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010 |
3 | Free Software Foundation, Inc. | |
9219021c | 4 | |
1fcb5155 | 5 | Contributed by David Carlton and by Kealia, Inc. |
9219021c DC |
6 | |
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
9219021c DC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
9219021c DC |
21 | |
22 | #include "defs.h" | |
23 | #include "cp-support.h" | |
24 | #include "gdb_obstack.h" | |
25 | #include "symtab.h" | |
26 | #include "symfile.h" | |
27 | #include "gdb_assert.h" | |
28 | #include "block.h" | |
5c4e30ca DC |
29 | #include "objfiles.h" |
30 | #include "gdbtypes.h" | |
31 | #include "dictionary.h" | |
32 | #include "command.h" | |
b368761e | 33 | #include "frame.h" |
27aa8d6a | 34 | #include "buildsym.h" |
9219021c | 35 | |
1fcb5155 DC |
36 | static struct symbol *lookup_namespace_scope (const char *name, |
37 | const char *linkage_name, | |
38 | const struct block *block, | |
39 | const domain_enum domain, | |
1fcb5155 DC |
40 | const char *scope, |
41 | int scope_len); | |
42 | ||
43 | static struct symbol *lookup_symbol_file (const char *name, | |
44 | const char *linkage_name, | |
45 | const struct block *block, | |
46 | const domain_enum domain, | |
1fcb5155 DC |
47 | int anonymous_namespace); |
48 | ||
b368761e DC |
49 | static struct type *cp_lookup_transparent_type_loop (const char *name, |
50 | const char *scope, | |
51 | int scope_len); | |
52 | ||
5c4e30ca DC |
53 | static void initialize_namespace_symtab (struct objfile *objfile); |
54 | ||
55 | static struct block *get_possible_namespace_block (struct objfile *objfile); | |
56 | ||
57 | static void free_namespace_block (struct symtab *symtab); | |
58 | ||
59 | static int check_possible_namespace_symbols_loop (const char *name, | |
60 | int len, | |
61 | struct objfile *objfile); | |
62 | ||
63 | static int check_one_possible_namespace_symbol (const char *name, | |
64 | int len, | |
65 | struct objfile *objfile); | |
66 | ||
21b556f4 | 67 | static struct symbol *lookup_possible_namespace_symbol (const char *name); |
5c4e30ca DC |
68 | |
69 | static void maintenance_cplus_namespace (char *args, int from_tty); | |
70 | ||
9219021c DC |
71 | /* Check to see if SYMBOL refers to an object contained within an |
72 | anonymous namespace; if so, add an appropriate using directive. */ | |
73 | ||
74 | /* Optimize away strlen ("(anonymous namespace)"). */ | |
75 | ||
76 | #define ANONYMOUS_NAMESPACE_LEN 21 | |
77 | ||
78 | void | |
79 | cp_scan_for_anonymous_namespaces (const struct symbol *symbol) | |
80 | { | |
df8a16a1 | 81 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
9219021c | 82 | { |
df8a16a1 | 83 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
9219021c DC |
84 | unsigned int previous_component; |
85 | unsigned int next_component; | |
86 | const char *len; | |
87 | ||
88 | /* Start with a quick-and-dirty check for mention of "(anonymous | |
89 | namespace)". */ | |
90 | ||
91 | if (!cp_is_anonymous (name)) | |
92 | return; | |
93 | ||
94 | previous_component = 0; | |
95 | next_component = cp_find_first_component (name + previous_component); | |
96 | ||
97 | while (name[next_component] == ':') | |
98 | { | |
99 | if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN | |
100 | && strncmp (name + previous_component, | |
101 | "(anonymous namespace)", | |
102 | ANONYMOUS_NAMESPACE_LEN) == 0) | |
103 | { | |
8c902bb1 SW |
104 | int dest_len = (previous_component == 0 ? 0 : previous_component - 2); |
105 | int src_len = next_component; | |
794684b6 | 106 | |
8c902bb1 SW |
107 | char *dest = alloca (dest_len + 1); |
108 | char *src = alloca (src_len + 1); | |
794684b6 | 109 | |
8c902bb1 SW |
110 | memcpy (dest, name, dest_len); |
111 | memcpy (src, name, src_len); | |
794684b6 | 112 | |
8c902bb1 SW |
113 | dest[dest_len] = '\0'; |
114 | src[src_len] = '\0'; | |
794684b6 | 115 | |
9219021c DC |
116 | /* We've found a component of the name that's an |
117 | anonymous namespace. So add symbols in it to the | |
118 | namespace given by the previous component if there is | |
119 | one, or to the global namespace if there isn't. */ | |
8c902bb1 | 120 | cp_add_using_directive (dest, src); |
9219021c DC |
121 | } |
122 | /* The "+ 2" is for the "::". */ | |
123 | previous_component = next_component + 2; | |
124 | next_component = (previous_component | |
125 | + cp_find_first_component (name | |
126 | + previous_component)); | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
794684b6 SW |
131 | /* Add a using directive to using_list. If the using directive in question |
132 | has already been added, don't add it twice. */ | |
9219021c DC |
133 | |
134 | void | |
8c902bb1 | 135 | cp_add_using_directive (const char *dest, const char *src) |
9219021c DC |
136 | { |
137 | struct using_direct *current; | |
138 | struct using_direct *new; | |
139 | ||
140 | /* Has it already been added? */ | |
141 | ||
27aa8d6a | 142 | for (current = using_directives; current != NULL; current = current->next) |
9219021c | 143 | { |
8c902bb1 SW |
144 | if (strcmp (current->import_src, src) == 0 |
145 | && strcmp (current->import_dest, dest) == 0) | |
9219021c DC |
146 | return; |
147 | } | |
148 | ||
8c902bb1 | 149 | using_directives = cp_add_using (dest, src, using_directives); |
794684b6 | 150 | |
9219021c DC |
151 | } |
152 | ||
153 | /* Record the namespace that the function defined by SYMBOL was | |
154 | defined in, if necessary. BLOCK is the associated block; use | |
155 | OBSTACK for allocation. */ | |
156 | ||
157 | void | |
158 | cp_set_block_scope (const struct symbol *symbol, | |
159 | struct block *block, | |
df8a16a1 DJ |
160 | struct obstack *obstack, |
161 | const char *processing_current_prefix, | |
162 | int processing_has_namespace_info) | |
9219021c | 163 | { |
df8a16a1 | 164 | if (processing_has_namespace_info) |
9219021c | 165 | { |
df8a16a1 DJ |
166 | block_set_scope |
167 | (block, obsavestring (processing_current_prefix, | |
168 | strlen (processing_current_prefix), | |
169 | obstack), | |
170 | obstack); | |
171 | } | |
172 | else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) | |
173 | { | |
174 | /* Try to figure out the appropriate namespace from the | |
175 | demangled name. */ | |
9219021c | 176 | |
df8a16a1 DJ |
177 | /* FIXME: carlton/2003-04-15: If the function in question is |
178 | a method of a class, the name will actually include the | |
179 | name of the class as well. This should be harmless, but | |
180 | is a little unfortunate. */ | |
9219021c | 181 | |
df8a16a1 DJ |
182 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
183 | unsigned int prefix_len = cp_entire_prefix_len (name); | |
9219021c | 184 | |
df8a16a1 DJ |
185 | block_set_scope (block, |
186 | obsavestring (name, prefix_len, obstack), | |
187 | obstack); | |
9219021c DC |
188 | } |
189 | } | |
190 | ||
191 | /* Test whether or not NAMESPACE looks like it mentions an anonymous | |
192 | namespace; return nonzero if so. */ | |
193 | ||
194 | int | |
195 | cp_is_anonymous (const char *namespace) | |
196 | { | |
197 | return (strstr (namespace, "(anonymous namespace)") | |
198 | != NULL); | |
199 | } | |
200 | ||
8c902bb1 SW |
201 | /* Create a new struct using direct which imports the namespace SRC |
202 | into the scope DEST. | |
9219021c DC |
203 | Set its next member in the linked list to NEXT; allocate all memory |
204 | using xmalloc. It copies the strings, so NAME can be a temporary | |
205 | string. */ | |
206 | ||
27aa8d6a | 207 | struct using_direct * |
8c902bb1 SW |
208 | cp_add_using (const char *dest, |
209 | const char *src, | |
9219021c DC |
210 | struct using_direct *next) |
211 | { | |
212 | struct using_direct *retval; | |
213 | ||
9219021c | 214 | retval = xmalloc (sizeof (struct using_direct)); |
8c902bb1 SW |
215 | retval->import_src = savestring (src, strlen(src)); |
216 | retval->import_dest = savestring (dest, strlen(dest)); | |
9219021c | 217 | retval->next = next; |
b14e635e | 218 | retval->searched = 0; |
9219021c DC |
219 | |
220 | return retval; | |
221 | } | |
222 | ||
1fcb5155 DC |
223 | /* The C++-specific version of name lookup for static and global |
224 | names. This makes sure that names get looked for in all namespaces | |
225 | that are in scope. NAME is the natural name of the symbol that | |
226 | we're looking for, LINKAGE_NAME (which is optional) is its linkage | |
227 | name, BLOCK is the block that we're searching within, DOMAIN says | |
228 | what kind of symbols we're looking for, and if SYMTAB is non-NULL, | |
229 | we should store the symtab where we found the symbol in it. */ | |
230 | ||
231 | struct symbol * | |
232 | cp_lookup_symbol_nonlocal (const char *name, | |
233 | const char *linkage_name, | |
234 | const struct block *block, | |
21b556f4 | 235 | const domain_enum domain) |
1fcb5155 | 236 | { |
8540c487 SW |
237 | struct symbol *sym; |
238 | const char *scope = block_scope (block); | |
239 | ||
240 | sym = lookup_namespace_scope (name, linkage_name, block, domain, scope, 0); | |
241 | if (sym != NULL) | |
242 | return sym; | |
243 | ||
b14e635e SW |
244 | return cp_lookup_symbol_namespace (scope, name, linkage_name, block, domain, |
245 | 1); | |
8540c487 SW |
246 | } |
247 | ||
248 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are as in | |
249 | cp_lookup_symbol_nonlocal. */ | |
250 | ||
251 | static struct symbol * | |
252 | cp_lookup_symbol_in_namespace (const char *namespace, | |
253 | const char *name, | |
254 | const char *linkage_name, | |
255 | const struct block *block, | |
256 | const domain_enum domain) | |
257 | { | |
258 | if (namespace[0] == '\0') | |
259 | { | |
260 | return lookup_symbol_file (name, linkage_name, block, | |
261 | domain, 0); | |
262 | } | |
263 | else | |
264 | { | |
265 | char *concatenated_name = alloca (strlen (namespace) + 2 + | |
266 | strlen (name+ 1)); | |
267 | strcpy (concatenated_name, namespace); | |
268 | strcat (concatenated_name, "::"); | |
269 | strcat (concatenated_name, name); | |
270 | return lookup_symbol_file (concatenated_name, linkage_name, | |
271 | block, domain,cp_is_anonymous (namespace)); | |
272 | } | |
273 | } | |
274 | ||
b14e635e SW |
275 | /* Used for cleanups to reset the "searched" flag incase |
276 | of an error. */ | |
277 | ||
278 | static void | |
279 | reset_directive_searched (void *data) | |
280 | { | |
281 | struct using_direct *direct = data; | |
282 | direct->searched = 0; | |
283 | } | |
284 | ||
8540c487 | 285 | /* Search for NAME by applying all import statements belonging |
b14e635e SW |
286 | to BLOCK which are applicable in SCOPE. |
287 | If SEARCH_PARENTS the search will include imports which are applicable in | |
288 | parents of SCOPE. | |
289 | Example: | |
290 | ||
291 | namespace A{ | |
292 | using namespace X; | |
293 | namespace B{ | |
294 | using namespace Y; | |
295 | } | |
296 | } | |
297 | ||
298 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of namespaces X | |
299 | and Y will be considered. If SEARCH_PARENTS is false only the import of Y | |
300 | is considered. */ | |
8540c487 SW |
301 | |
302 | static struct symbol * | |
303 | cp_lookup_symbol_imports (const char *scope, | |
304 | const char *name, | |
305 | const char *linkage_name, | |
306 | const struct block *block, | |
b14e635e SW |
307 | const domain_enum domain, |
308 | const int search_parents) | |
8540c487 | 309 | { |
b14e635e | 310 | struct using_direct *current; |
8540c487 SW |
311 | struct symbol *sym; |
312 | int len; | |
b14e635e SW |
313 | int directive_match; |
314 | struct cleanup *searched_cleanup; | |
8540c487 SW |
315 | |
316 | /* First, try to find the symbol in the given namespace. */ | |
317 | sym = cp_lookup_symbol_in_namespace (scope, name, linkage_name, block, | |
318 | domain); | |
319 | if (sym != NULL) | |
320 | return sym; | |
321 | ||
322 | /* Go through the using directives. If any of them add new | |
323 | names to the namespace we're searching in, see if we can find a | |
324 | match by applying them. */ | |
325 | ||
326 | for (current = block_using (block); | |
327 | current != NULL; | |
328 | current = current->next) | |
329 | { | |
b14e635e SW |
330 | len = strlen (current->import_dest); |
331 | directive_match = (search_parents | |
332 | ? (strncmp (scope, current->import_dest, | |
333 | strlen (current->import_dest)) == 0 | |
334 | && (len == 0 | |
335 | || scope[len] == ':' || scope[len] == '\0')) | |
336 | : strcmp (scope, current->import_dest) == 0); | |
8540c487 SW |
337 | |
338 | /* If the import destination is the current scope or one of its ancestors then | |
339 | it is applicable. */ | |
b14e635e | 340 | if (directive_match && !current->searched) |
8540c487 | 341 | { |
b14e635e SW |
342 | /* Mark this import as searched so that the recursive call does not |
343 | search it again. */ | |
344 | current->searched = 1; | |
345 | searched_cleanup = make_cleanup (reset_directive_searched, current); | |
346 | ||
347 | sym = cp_lookup_symbol_namespace (current->import_src, | |
348 | name, | |
349 | linkage_name, | |
350 | block, | |
351 | domain, | |
352 | 0); | |
353 | ||
354 | current->searched = 0; | |
355 | discard_cleanups (searched_cleanup); | |
356 | ||
357 | if (sym != NULL) | |
358 | return sym; | |
8540c487 SW |
359 | } |
360 | } | |
361 | ||
362 | return NULL; | |
363 | } | |
364 | ||
8540c487 SW |
365 | /* Searches for NAME in the current namespace, and by applying relevant import |
366 | statements belonging to BLOCK and its parents. SCOPE is the namespace scope | |
367 | of the context in which the search is being evaluated. */ | |
368 | ||
369 | struct symbol* | |
370 | cp_lookup_symbol_namespace (const char *scope, | |
371 | const char *name, | |
372 | const char *linkage_name, | |
373 | const struct block *block, | |
b14e635e SW |
374 | const domain_enum domain, |
375 | const int search_parents) | |
8540c487 SW |
376 | { |
377 | struct symbol *sym; | |
378 | ||
379 | /* Search for name in namespaces imported to this and parent blocks. */ | |
380 | while (block != NULL) | |
381 | { | |
b14e635e SW |
382 | sym = cp_lookup_symbol_imports (scope, name, linkage_name, block, domain, |
383 | search_parents); | |
8540c487 SW |
384 | |
385 | if (sym) | |
386 | return sym; | |
387 | ||
388 | block = BLOCK_SUPERBLOCK (block); | |
389 | } | |
390 | ||
391 | return NULL; | |
1fcb5155 DC |
392 | } |
393 | ||
394 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
395 | global variables). SCOPE is the namespace that the current | |
396 | function is defined within; only consider namespaces whose length | |
397 | is at least SCOPE_LEN. Other arguments are as in | |
398 | cp_lookup_symbol_nonlocal. | |
399 | ||
400 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 401 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
402 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
403 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
404 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
405 | "A::B::x"; if it doesn't find it, then the second call looks for | |
406 | "A::x", and if that call fails, then the first call looks for | |
407 | "x". */ | |
408 | ||
409 | static struct symbol * | |
410 | lookup_namespace_scope (const char *name, | |
411 | const char *linkage_name, | |
412 | const struct block *block, | |
413 | const domain_enum domain, | |
1fcb5155 DC |
414 | const char *scope, |
415 | int scope_len) | |
416 | { | |
417 | char *namespace; | |
418 | ||
419 | if (scope[scope_len] != '\0') | |
420 | { | |
421 | /* Recursively search for names in child namespaces first. */ | |
422 | ||
423 | struct symbol *sym; | |
424 | int new_scope_len = scope_len; | |
425 | ||
426 | /* If the current scope is followed by "::", skip past that. */ | |
427 | if (new_scope_len != 0) | |
428 | { | |
429 | gdb_assert (scope[new_scope_len] == ':'); | |
430 | new_scope_len += 2; | |
431 | } | |
432 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
433 | sym = lookup_namespace_scope (name, linkage_name, block, | |
21b556f4 | 434 | domain, scope, new_scope_len); |
1fcb5155 DC |
435 | if (sym != NULL) |
436 | return sym; | |
437 | } | |
438 | ||
439 | /* Okay, we didn't find a match in our children, so look for the | |
440 | name in the current namespace. */ | |
441 | ||
442 | namespace = alloca (scope_len + 1); | |
443 | strncpy (namespace, scope, scope_len); | |
444 | namespace[scope_len] = '\0'; | |
8540c487 SW |
445 | return cp_lookup_symbol_in_namespace (namespace, name, linkage_name, |
446 | block, domain); | |
1fcb5155 DC |
447 | } |
448 | ||
449 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
450 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
451 | within an anonymous namespace. Other arguments are as in | |
452 | cp_lookup_symbol_nonlocal. */ | |
453 | ||
454 | static struct symbol * | |
455 | lookup_symbol_file (const char *name, | |
456 | const char *linkage_name, | |
457 | const struct block *block, | |
458 | const domain_enum domain, | |
1fcb5155 DC |
459 | int anonymous_namespace) |
460 | { | |
461 | struct symbol *sym = NULL; | |
462 | ||
21b556f4 | 463 | sym = lookup_symbol_static (name, linkage_name, block, domain); |
1fcb5155 DC |
464 | if (sym != NULL) |
465 | return sym; | |
466 | ||
467 | if (anonymous_namespace) | |
468 | { | |
469 | /* Symbols defined in anonymous namespaces have external linkage | |
470 | but should be treated as local to a single file nonetheless. | |
471 | So we only search the current file's global block. */ | |
472 | ||
473 | const struct block *global_block = block_global_block (block); | |
474 | ||
475 | if (global_block != NULL) | |
5c4e30ca | 476 | sym = lookup_symbol_aux_block (name, linkage_name, global_block, |
21b556f4 | 477 | domain); |
1fcb5155 DC |
478 | } |
479 | else | |
480 | { | |
21b556f4 | 481 | sym = lookup_symbol_global (name, linkage_name, block, domain); |
5c4e30ca DC |
482 | } |
483 | ||
484 | if (sym != NULL) | |
485 | return sym; | |
486 | ||
487 | /* Now call "lookup_possible_namespace_symbol". Symbols in here | |
488 | claim to be associated to namespaces, but this claim might be | |
489 | incorrect: the names in question might actually correspond to | |
490 | classes instead of namespaces. But if they correspond to | |
491 | classes, then we should have found a match for them above. So if | |
492 | we find them now, they should be genuine. */ | |
493 | ||
494 | /* FIXME: carlton/2003-06-12: This is a hack and should eventually | |
495 | be deleted: see comments below. */ | |
496 | ||
497 | if (domain == VAR_DOMAIN) | |
498 | { | |
21b556f4 | 499 | sym = lookup_possible_namespace_symbol (name); |
5c4e30ca DC |
500 | if (sym != NULL) |
501 | return sym; | |
502 | } | |
503 | ||
504 | return NULL; | |
505 | } | |
506 | ||
79c2c32d DC |
507 | /* Look up a type named NESTED_NAME that is nested inside the C++ |
508 | class or namespace given by PARENT_TYPE, from within the context | |
509 | given by BLOCK. Return NULL if there is no such nested type. */ | |
510 | ||
79c2c32d DC |
511 | struct type * |
512 | cp_lookup_nested_type (struct type *parent_type, | |
513 | const char *nested_name, | |
514 | const struct block *block) | |
515 | { | |
516 | switch (TYPE_CODE (parent_type)) | |
517 | { | |
63d06c5c | 518 | case TYPE_CODE_STRUCT: |
79c2c32d DC |
519 | case TYPE_CODE_NAMESPACE: |
520 | { | |
63d06c5c DC |
521 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
522 | of classes like, say, data or function members. Instead, | |
523 | they're just represented by symbols whose names are | |
524 | qualified by the name of the surrounding class. This is | |
525 | just like members of namespaces; in particular, | |
526 | lookup_symbol_namespace works when looking them up. */ | |
527 | ||
79c2c32d | 528 | const char *parent_name = TYPE_TAG_NAME (parent_type); |
8540c487 SW |
529 | struct symbol *sym = cp_lookup_symbol_in_namespace (parent_name, |
530 | nested_name, | |
531 | NULL, | |
532 | block, | |
533 | VAR_DOMAIN); | |
79c2c32d DC |
534 | if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF) |
535 | return NULL; | |
536 | else | |
537 | return SYMBOL_TYPE (sym); | |
538 | } | |
539 | default: | |
540 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 541 | _("cp_lookup_nested_type called on a non-aggregate type.")); |
79c2c32d DC |
542 | } |
543 | } | |
544 | ||
b368761e DC |
545 | /* The C++-version of lookup_transparent_type. */ |
546 | ||
547 | /* FIXME: carlton/2004-01-16: The problem that this is trying to | |
548 | address is that, unfortunately, sometimes NAME is wrong: it may not | |
549 | include the name of namespaces enclosing the type in question. | |
550 | lookup_transparent_type gets called when the the type in question | |
551 | is a declaration, and we're trying to find its definition; but, for | |
552 | declarations, our type name deduction mechanism doesn't work. | |
553 | There's nothing we can do to fix this in general, I think, in the | |
554 | absence of debug information about namespaces (I've filed PR | |
555 | gdb/1511 about this); until such debug information becomes more | |
556 | prevalent, one heuristic which sometimes looks is to search for the | |
557 | definition in namespaces containing the current namespace. | |
558 | ||
559 | We should delete this functions once the appropriate debug | |
560 | information becomes more widespread. (GCC 3.4 will be the first | |
561 | released version of GCC with such information.) */ | |
562 | ||
563 | struct type * | |
564 | cp_lookup_transparent_type (const char *name) | |
565 | { | |
566 | /* First, try the honest way of looking up the definition. */ | |
567 | struct type *t = basic_lookup_transparent_type (name); | |
568 | const char *scope; | |
569 | ||
570 | if (t != NULL) | |
571 | return t; | |
572 | ||
573 | /* If that doesn't work and we're within a namespace, look there | |
574 | instead. */ | |
575 | scope = block_scope (get_selected_block (0)); | |
576 | ||
577 | if (scope[0] == '\0') | |
578 | return NULL; | |
579 | ||
580 | return cp_lookup_transparent_type_loop (name, scope, 0); | |
581 | } | |
582 | ||
583 | /* Lookup the the type definition associated to NAME in | |
584 | namespaces/classes containing SCOPE whose name is strictly longer | |
585 | than LENGTH. LENGTH must be the index of the start of a | |
586 | component of SCOPE. */ | |
587 | ||
588 | static struct type * | |
589 | cp_lookup_transparent_type_loop (const char *name, const char *scope, | |
590 | int length) | |
591 | { | |
1198ecbe | 592 | int scope_length = length + cp_find_first_component (scope + length); |
b368761e DC |
593 | char *full_name; |
594 | ||
595 | /* If the current scope is followed by "::", look in the next | |
596 | component. */ | |
597 | if (scope[scope_length] == ':') | |
598 | { | |
599 | struct type *retval | |
600 | = cp_lookup_transparent_type_loop (name, scope, scope_length + 2); | |
601 | if (retval != NULL) | |
602 | return retval; | |
603 | } | |
604 | ||
605 | full_name = alloca (scope_length + 2 + strlen (name) + 1); | |
606 | strncpy (full_name, scope, scope_length); | |
607 | strncpy (full_name + scope_length, "::", 2); | |
608 | strcpy (full_name + scope_length + 2, name); | |
609 | ||
610 | return basic_lookup_transparent_type (full_name); | |
611 | } | |
612 | ||
5c4e30ca DC |
613 | /* Now come functions for dealing with symbols associated to |
614 | namespaces. (They're used to store the namespaces themselves, not | |
615 | objects that live in the namespaces.) These symbols come in two | |
616 | varieties: if we run into a DW_TAG_namespace DIE, then we know that | |
617 | we have a namespace, so dwarf2read.c creates a symbol for it just | |
618 | like normal. But, unfortunately, versions of GCC through at least | |
619 | 3.3 don't generate those DIE's. Our solution is to try to guess | |
620 | their existence by looking at demangled names. This might cause us | |
621 | to misidentify classes as namespaces, however. So we put those | |
622 | symbols in a special block (one per objfile), and we only search | |
623 | that block as a last resort. */ | |
624 | ||
625 | /* FIXME: carlton/2003-06-12: Once versions of GCC that generate | |
626 | DW_TAG_namespace have been out for a year or two, we should get rid | |
627 | of all of this "possible namespace" nonsense. */ | |
628 | ||
629 | /* Allocate everything necessary for the possible namespace block | |
630 | associated to OBJFILE. */ | |
631 | ||
632 | static void | |
633 | initialize_namespace_symtab (struct objfile *objfile) | |
634 | { | |
635 | struct symtab *namespace_symtab; | |
636 | struct blockvector *bv; | |
637 | struct block *bl; | |
638 | ||
639 | namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile); | |
640 | namespace_symtab->language = language_cplus; | |
641 | namespace_symtab->free_code = free_nothing; | |
642 | namespace_symtab->dirname = NULL; | |
643 | ||
4a146b47 | 644 | bv = obstack_alloc (&objfile->objfile_obstack, |
5c4e30ca DC |
645 | sizeof (struct blockvector) |
646 | + FIRST_LOCAL_BLOCK * sizeof (struct block *)); | |
647 | BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1; | |
648 | BLOCKVECTOR (namespace_symtab) = bv; | |
649 | ||
650 | /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */ | |
651 | ||
4a146b47 EZ |
652 | bl = allocate_block (&objfile->objfile_obstack); |
653 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, | |
5c4e30ca DC |
654 | NULL); |
655 | BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl; | |
4a146b47 EZ |
656 | bl = allocate_block (&objfile->objfile_obstack); |
657 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, | |
5c4e30ca DC |
658 | NULL); |
659 | BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl; | |
660 | ||
661 | /* Allocate the possible namespace block; we put it where the first | |
662 | local block will live, though I don't think there's any need to | |
663 | pretend that it's actually a local block (e.g. by setting | |
664 | BLOCK_SUPERBLOCK appropriately). We don't use the global or | |
665 | static block because we don't want it searched during the normal | |
666 | search of all global/static blocks in lookup_symbol: we only want | |
667 | it used as a last resort. */ | |
668 | ||
669 | /* NOTE: carlton/2003-09-11: I considered not associating the fake | |
670 | symbols to a block/symtab at all. But that would cause problems | |
671 | with lookup_symbol's SYMTAB argument and with block_found, so | |
672 | having a symtab/block for this purpose seems like the best | |
673 | solution for now. */ | |
674 | ||
4a146b47 | 675 | bl = allocate_block (&objfile->objfile_obstack); |
5c4e30ca DC |
676 | BLOCK_DICT (bl) = dict_create_hashed_expandable (); |
677 | BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl; | |
678 | ||
679 | namespace_symtab->free_func = free_namespace_block; | |
680 | ||
681 | objfile->cp_namespace_symtab = namespace_symtab; | |
682 | } | |
683 | ||
684 | /* Locate the possible namespace block associated to OBJFILE, | |
685 | allocating it if necessary. */ | |
686 | ||
687 | static struct block * | |
688 | get_possible_namespace_block (struct objfile *objfile) | |
689 | { | |
690 | if (objfile->cp_namespace_symtab == NULL) | |
691 | initialize_namespace_symtab (objfile); | |
692 | ||
693 | return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab), | |
694 | FIRST_LOCAL_BLOCK); | |
695 | } | |
696 | ||
697 | /* Free the dictionary associated to the possible namespace block. */ | |
698 | ||
699 | static void | |
700 | free_namespace_block (struct symtab *symtab) | |
701 | { | |
702 | struct block *possible_namespace_block; | |
703 | ||
704 | possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), | |
705 | FIRST_LOCAL_BLOCK); | |
706 | gdb_assert (possible_namespace_block != NULL); | |
707 | dict_free (BLOCK_DICT (possible_namespace_block)); | |
708 | } | |
709 | ||
710 | /* Ensure that there are symbols in the possible namespace block | |
711 | associated to OBJFILE for all initial substrings of NAME that look | |
712 | like namespaces or classes. NAME should end in a member variable: | |
713 | it shouldn't consist solely of namespaces. */ | |
714 | ||
715 | void | |
716 | cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile) | |
717 | { | |
718 | check_possible_namespace_symbols_loop (name, | |
719 | cp_find_first_component (name), | |
720 | objfile); | |
721 | } | |
722 | ||
723 | /* This is a helper loop for cp_check_possible_namespace_symbols; it | |
724 | ensures that there are symbols in the possible namespace block | |
725 | associated to OBJFILE for all namespaces that are initial | |
726 | substrings of NAME of length at least LEN. It returns 1 if a | |
727 | previous loop had already created the shortest such symbol and 0 | |
728 | otherwise. | |
729 | ||
730 | This function assumes that if there is already a symbol associated | |
731 | to a substring of NAME of a given length, then there are already | |
732 | symbols associated to all substrings of NAME whose length is less | |
733 | than that length. So if cp_check_possible_namespace_symbols has | |
734 | been called once with argument "A::B::C::member", then that will | |
735 | create symbols "A", "A::B", and "A::B::C". If it is then later | |
736 | called with argument "A::B::D::member", then the new call will | |
737 | generate a new symbol for "A::B::D", but once it sees that "A::B" | |
738 | has already been created, it doesn't bother checking to see if "A" | |
739 | has also been created. */ | |
740 | ||
741 | static int | |
742 | check_possible_namespace_symbols_loop (const char *name, int len, | |
743 | struct objfile *objfile) | |
744 | { | |
745 | if (name[len] == ':') | |
746 | { | |
747 | int done; | |
748 | int next_len = len + 2; | |
749 | ||
750 | next_len += cp_find_first_component (name + next_len); | |
751 | done = check_possible_namespace_symbols_loop (name, next_len, | |
752 | objfile); | |
753 | ||
754 | if (!done) | |
755 | done = check_one_possible_namespace_symbol (name, len, objfile); | |
756 | ||
757 | return done; | |
1fcb5155 | 758 | } |
5c4e30ca DC |
759 | else |
760 | return 0; | |
761 | } | |
762 | ||
763 | /* Check to see if there's already a possible namespace symbol in | |
764 | OBJFILE whose name is the initial substring of NAME of length LEN. | |
765 | If not, create one and return 0; otherwise, return 1. */ | |
766 | ||
767 | static int | |
768 | check_one_possible_namespace_symbol (const char *name, int len, | |
769 | struct objfile *objfile) | |
770 | { | |
771 | struct block *block = get_possible_namespace_block (objfile); | |
ec5cdd75 DJ |
772 | char *name_copy = alloca (len + 1); |
773 | struct symbol *sym; | |
774 | ||
775 | memcpy (name_copy, name, len); | |
776 | name_copy[len] = '\0'; | |
777 | sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN); | |
5c4e30ca DC |
778 | |
779 | if (sym == NULL) | |
780 | { | |
ec5cdd75 | 781 | struct type *type; |
ec5cdd75 DJ |
782 | |
783 | type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile); | |
784 | ||
5c4e30ca DC |
785 | TYPE_TAG_NAME (type) = TYPE_NAME (type); |
786 | ||
4a146b47 | 787 | sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol)); |
5c4e30ca DC |
788 | memset (sym, 0, sizeof (struct symbol)); |
789 | SYMBOL_LANGUAGE (sym) = language_cplus; | |
04a679b8 TT |
790 | /* Note that init_type copied the name to the objfile's |
791 | obstack. */ | |
792 | SYMBOL_SET_NAMES (sym, TYPE_NAME (type), len, 0, objfile); | |
5c4e30ca DC |
793 | SYMBOL_CLASS (sym) = LOC_TYPEDEF; |
794 | SYMBOL_TYPE (sym) = type; | |
795 | SYMBOL_DOMAIN (sym) = VAR_DOMAIN; | |
796 | ||
797 | dict_add_symbol (BLOCK_DICT (block), sym); | |
798 | ||
799 | return 0; | |
800 | } | |
801 | else | |
ec5cdd75 | 802 | return 1; |
5c4e30ca DC |
803 | } |
804 | ||
805 | /* Look for a symbol named NAME in all the possible namespace blocks. | |
21b556f4 | 806 | If one is found, return it. */ |
5c4e30ca DC |
807 | |
808 | static struct symbol * | |
21b556f4 | 809 | lookup_possible_namespace_symbol (const char *name) |
5c4e30ca DC |
810 | { |
811 | struct objfile *objfile; | |
812 | ||
813 | ALL_OBJFILES (objfile) | |
814 | { | |
815 | struct symbol *sym; | |
816 | ||
817 | sym = lookup_block_symbol (get_possible_namespace_block (objfile), | |
818 | name, NULL, VAR_DOMAIN); | |
819 | ||
820 | if (sym != NULL) | |
21b556f4 | 821 | return sym; |
5c4e30ca DC |
822 | } |
823 | ||
824 | return NULL; | |
825 | } | |
826 | ||
827 | /* Print out all the possible namespace symbols. */ | |
828 | ||
829 | static void | |
830 | maintenance_cplus_namespace (char *args, int from_tty) | |
831 | { | |
832 | struct objfile *objfile; | |
a3f17187 | 833 | printf_unfiltered (_("Possible namespaces:\n")); |
5c4e30ca DC |
834 | ALL_OBJFILES (objfile) |
835 | { | |
836 | struct dict_iterator iter; | |
837 | struct symbol *sym; | |
838 | ||
839 | ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym) | |
840 | { | |
841 | printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym)); | |
842 | } | |
843 | } | |
844 | } | |
845 | ||
2c0b251b PA |
846 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
847 | extern initialize_file_ftype _initialize_cp_namespace; | |
848 | ||
5c4e30ca DC |
849 | void |
850 | _initialize_cp_namespace (void) | |
851 | { | |
852 | add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace, | |
1a966eab | 853 | _("Print the list of possible C++ namespaces."), |
5c4e30ca | 854 | &maint_cplus_cmd_list); |
1fcb5155 | 855 | } |