]>
Commit | Line | Data |
---|---|---|
9219021c | 1 | /* Helper routines for C++ support in GDB. |
38d518c9 | 2 | Copyright 2003, 2004 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 | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
24 | #include "cp-support.h" | |
25 | #include "gdb_obstack.h" | |
26 | #include "symtab.h" | |
27 | #include "symfile.h" | |
28 | #include "gdb_assert.h" | |
29 | #include "block.h" | |
5c4e30ca DC |
30 | #include "objfiles.h" |
31 | #include "gdbtypes.h" | |
32 | #include "dictionary.h" | |
33 | #include "command.h" | |
9219021c DC |
34 | |
35 | /* When set, the file that we're processing seems to have debugging | |
36 | info for C++ namespaces, so cp-namespace.c shouldn't try to guess | |
37 | namespace info itself. */ | |
38 | ||
39 | unsigned char processing_has_namespace_info; | |
40 | ||
38d518c9 EZ |
41 | /* This contains our best guess as to the name of the current |
42 | enclosing namespace(s)/class(es), if any. For example, if we're | |
43 | within the method foo() in the following code: | |
9219021c | 44 | |
38d518c9 EZ |
45 | namespace N { |
46 | class C { | |
47 | void foo () { | |
48 | } | |
49 | }; | |
50 | } | |
51 | ||
52 | then processing_current_prefix should be set to "N::C". If | |
53 | processing_has_namespace_info is false, then this variable might | |
54 | not be reliable. */ | |
0fc9922a | 55 | |
38d518c9 | 56 | const char *processing_current_prefix; |
9219021c DC |
57 | |
58 | /* List of using directives that are active in the current file. */ | |
59 | ||
60 | static struct using_direct *using_list; | |
61 | ||
62 | static struct using_direct *cp_add_using (const char *name, | |
63 | unsigned int inner_len, | |
64 | unsigned int outer_len, | |
65 | struct using_direct *next); | |
66 | ||
67 | static struct using_direct *cp_copy_usings (struct using_direct *using, | |
68 | struct obstack *obstack); | |
69 | ||
1fcb5155 DC |
70 | static struct symbol *lookup_namespace_scope (const char *name, |
71 | const char *linkage_name, | |
72 | const struct block *block, | |
73 | const domain_enum domain, | |
74 | struct symtab **symtab, | |
75 | const char *scope, | |
76 | int scope_len); | |
77 | ||
78 | static struct symbol *lookup_symbol_file (const char *name, | |
79 | const char *linkage_name, | |
80 | const struct block *block, | |
81 | const domain_enum domain, | |
82 | struct symtab **symtab, | |
83 | int anonymous_namespace); | |
84 | ||
5c4e30ca DC |
85 | static void initialize_namespace_symtab (struct objfile *objfile); |
86 | ||
87 | static struct block *get_possible_namespace_block (struct objfile *objfile); | |
88 | ||
89 | static void free_namespace_block (struct symtab *symtab); | |
90 | ||
91 | static int check_possible_namespace_symbols_loop (const char *name, | |
92 | int len, | |
93 | struct objfile *objfile); | |
94 | ||
95 | static int check_one_possible_namespace_symbol (const char *name, | |
96 | int len, | |
97 | struct objfile *objfile); | |
98 | ||
99 | static | |
100 | struct symbol *lookup_possible_namespace_symbol (const char *name, | |
101 | struct symtab **symtab); | |
102 | ||
103 | static void maintenance_cplus_namespace (char *args, int from_tty); | |
104 | ||
9219021c DC |
105 | /* Set up support for dealing with C++ namespace info in the current |
106 | symtab. */ | |
107 | ||
108 | void cp_initialize_namespace () | |
109 | { | |
110 | processing_has_namespace_info = 0; | |
111 | using_list = NULL; | |
112 | } | |
113 | ||
114 | /* Add all the using directives we've gathered to the current symtab. | |
115 | STATIC_BLOCK should be the symtab's static block; OBSTACK is used | |
116 | for allocation. */ | |
117 | ||
118 | void | |
119 | cp_finalize_namespace (struct block *static_block, | |
120 | struct obstack *obstack) | |
121 | { | |
122 | if (using_list != NULL) | |
123 | { | |
124 | block_set_using (static_block, | |
125 | cp_copy_usings (using_list, obstack), | |
126 | obstack); | |
127 | using_list = NULL; | |
128 | } | |
129 | } | |
130 | ||
131 | /* Check to see if SYMBOL refers to an object contained within an | |
132 | anonymous namespace; if so, add an appropriate using directive. */ | |
133 | ||
134 | /* Optimize away strlen ("(anonymous namespace)"). */ | |
135 | ||
136 | #define ANONYMOUS_NAMESPACE_LEN 21 | |
137 | ||
138 | void | |
139 | cp_scan_for_anonymous_namespaces (const struct symbol *symbol) | |
140 | { | |
141 | if (!processing_has_namespace_info | |
142 | && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) | |
143 | { | |
144 | const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol); | |
145 | unsigned int previous_component; | |
146 | unsigned int next_component; | |
147 | const char *len; | |
148 | ||
149 | /* Start with a quick-and-dirty check for mention of "(anonymous | |
150 | namespace)". */ | |
151 | ||
152 | if (!cp_is_anonymous (name)) | |
153 | return; | |
154 | ||
155 | previous_component = 0; | |
156 | next_component = cp_find_first_component (name + previous_component); | |
157 | ||
158 | while (name[next_component] == ':') | |
159 | { | |
160 | if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN | |
161 | && strncmp (name + previous_component, | |
162 | "(anonymous namespace)", | |
163 | ANONYMOUS_NAMESPACE_LEN) == 0) | |
164 | { | |
165 | /* We've found a component of the name that's an | |
166 | anonymous namespace. So add symbols in it to the | |
167 | namespace given by the previous component if there is | |
168 | one, or to the global namespace if there isn't. */ | |
169 | cp_add_using_directive (name, | |
170 | previous_component == 0 | |
171 | ? 0 : previous_component - 2, | |
172 | next_component); | |
173 | } | |
174 | /* The "+ 2" is for the "::". */ | |
175 | previous_component = next_component + 2; | |
176 | next_component = (previous_component | |
177 | + cp_find_first_component (name | |
178 | + previous_component)); | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | /* Add a using directive to using_list. NAME is the start of a string | |
184 | that should contain the namespaces we want to add as initial | |
185 | substrings, OUTER_LENGTH is the end of the outer namespace, and | |
186 | INNER_LENGTH is the end of the inner namespace. If the using | |
187 | directive in question has already been added, don't add it | |
188 | twice. */ | |
189 | ||
190 | void | |
191 | cp_add_using_directive (const char *name, unsigned int outer_length, | |
192 | unsigned int inner_length) | |
193 | { | |
194 | struct using_direct *current; | |
195 | struct using_direct *new; | |
196 | ||
197 | /* Has it already been added? */ | |
198 | ||
199 | for (current = using_list; current != NULL; current = current->next) | |
200 | { | |
201 | if ((strncmp (current->inner, name, inner_length) == 0) | |
202 | && (strlen (current->inner) == inner_length) | |
203 | && (strlen (current->outer) == outer_length)) | |
204 | return; | |
205 | } | |
206 | ||
207 | using_list = cp_add_using (name, inner_length, outer_length, | |
208 | using_list); | |
209 | } | |
210 | ||
211 | /* Record the namespace that the function defined by SYMBOL was | |
212 | defined in, if necessary. BLOCK is the associated block; use | |
213 | OBSTACK for allocation. */ | |
214 | ||
215 | void | |
216 | cp_set_block_scope (const struct symbol *symbol, | |
217 | struct block *block, | |
218 | struct obstack *obstack) | |
219 | { | |
220 | /* Make sure that the name was originally mangled: if not, there | |
221 | certainly isn't any namespace information to worry about! */ | |
222 | ||
223 | if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) | |
224 | { | |
0fc9922a DC |
225 | #if 0 |
226 | /* FIXME: carlton/2003-06-12: As mentioned above, | |
227 | 'processing_has_namespace_info' currently isn't entirely | |
228 | reliable, so let's always use demangled names to get this | |
229 | information for now. */ | |
230 | ||
9219021c DC |
231 | if (processing_has_namespace_info) |
232 | { | |
233 | block_set_scope | |
38d518c9 EZ |
234 | (block, obsavestring (processing_current_prefix, |
235 | strlen (processing_current_prefix), | |
9219021c DC |
236 | obstack), |
237 | obstack); | |
238 | } | |
239 | else | |
0fc9922a | 240 | #endif |
9219021c DC |
241 | { |
242 | /* Try to figure out the appropriate namespace from the | |
243 | demangled name. */ | |
244 | ||
245 | /* FIXME: carlton/2003-04-15: If the function in question is | |
246 | a method of a class, the name will actually include the | |
247 | name of the class as well. This should be harmless, but | |
248 | is a little unfortunate. */ | |
249 | ||
250 | const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol); | |
251 | unsigned int prefix_len = cp_entire_prefix_len (name); | |
252 | ||
253 | block_set_scope (block, | |
254 | obsavestring (name, prefix_len, obstack), | |
255 | obstack); | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
260 | /* Test whether or not NAMESPACE looks like it mentions an anonymous | |
261 | namespace; return nonzero if so. */ | |
262 | ||
263 | int | |
264 | cp_is_anonymous (const char *namespace) | |
265 | { | |
266 | return (strstr (namespace, "(anonymous namespace)") | |
267 | != NULL); | |
268 | } | |
269 | ||
270 | /* Create a new struct using direct whose inner namespace is the | |
271 | initial substring of NAME of leng INNER_LEN and whose outer | |
272 | namespace is the initial substring of NAME of length OUTER_LENGTH. | |
273 | Set its next member in the linked list to NEXT; allocate all memory | |
274 | using xmalloc. It copies the strings, so NAME can be a temporary | |
275 | string. */ | |
276 | ||
277 | static struct using_direct * | |
278 | cp_add_using (const char *name, | |
279 | unsigned int inner_len, | |
280 | unsigned int outer_len, | |
281 | struct using_direct *next) | |
282 | { | |
283 | struct using_direct *retval; | |
284 | ||
285 | gdb_assert (outer_len < inner_len); | |
286 | ||
287 | retval = xmalloc (sizeof (struct using_direct)); | |
288 | retval->inner = savestring (name, inner_len); | |
289 | retval->outer = savestring (name, outer_len); | |
290 | retval->next = next; | |
291 | ||
292 | return retval; | |
293 | } | |
294 | ||
295 | /* Make a copy of the using directives in the list pointed to by | |
296 | USING, using OBSTACK to allocate memory. Free all memory pointed | |
297 | to by USING via xfree. */ | |
298 | ||
299 | static struct using_direct * | |
300 | cp_copy_usings (struct using_direct *using, | |
301 | struct obstack *obstack) | |
302 | { | |
303 | if (using == NULL) | |
304 | { | |
305 | return NULL; | |
306 | } | |
307 | else | |
308 | { | |
309 | struct using_direct *retval | |
310 | = obstack_alloc (obstack, sizeof (struct using_direct)); | |
311 | retval->inner = obsavestring (using->inner, strlen (using->inner), | |
312 | obstack); | |
313 | retval->outer = obsavestring (using->outer, strlen (using->outer), | |
314 | obstack); | |
315 | retval->next = cp_copy_usings (using->next, obstack); | |
316 | ||
317 | xfree (using->inner); | |
318 | xfree (using->outer); | |
319 | xfree (using); | |
320 | ||
321 | return retval; | |
322 | } | |
323 | } | |
1fcb5155 DC |
324 | |
325 | /* The C++-specific version of name lookup for static and global | |
326 | names. This makes sure that names get looked for in all namespaces | |
327 | that are in scope. NAME is the natural name of the symbol that | |
328 | we're looking for, LINKAGE_NAME (which is optional) is its linkage | |
329 | name, BLOCK is the block that we're searching within, DOMAIN says | |
330 | what kind of symbols we're looking for, and if SYMTAB is non-NULL, | |
331 | we should store the symtab where we found the symbol in it. */ | |
332 | ||
333 | struct symbol * | |
334 | cp_lookup_symbol_nonlocal (const char *name, | |
335 | const char *linkage_name, | |
336 | const struct block *block, | |
337 | const domain_enum domain, | |
338 | struct symtab **symtab) | |
339 | { | |
340 | return lookup_namespace_scope (name, linkage_name, block, domain, | |
341 | symtab, block_scope (block), 0); | |
342 | } | |
343 | ||
344 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
345 | global variables). SCOPE is the namespace that the current | |
346 | function is defined within; only consider namespaces whose length | |
347 | is at least SCOPE_LEN. Other arguments are as in | |
348 | cp_lookup_symbol_nonlocal. | |
349 | ||
350 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 351 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
352 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
353 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
354 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
355 | "A::B::x"; if it doesn't find it, then the second call looks for | |
356 | "A::x", and if that call fails, then the first call looks for | |
357 | "x". */ | |
358 | ||
359 | static struct symbol * | |
360 | lookup_namespace_scope (const char *name, | |
361 | const char *linkage_name, | |
362 | const struct block *block, | |
363 | const domain_enum domain, | |
364 | struct symtab **symtab, | |
365 | const char *scope, | |
366 | int scope_len) | |
367 | { | |
368 | char *namespace; | |
369 | ||
370 | if (scope[scope_len] != '\0') | |
371 | { | |
372 | /* Recursively search for names in child namespaces first. */ | |
373 | ||
374 | struct symbol *sym; | |
375 | int new_scope_len = scope_len; | |
376 | ||
377 | /* If the current scope is followed by "::", skip past that. */ | |
378 | if (new_scope_len != 0) | |
379 | { | |
380 | gdb_assert (scope[new_scope_len] == ':'); | |
381 | new_scope_len += 2; | |
382 | } | |
383 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
384 | sym = lookup_namespace_scope (name, linkage_name, block, | |
385 | domain, symtab, | |
386 | scope, new_scope_len); | |
387 | if (sym != NULL) | |
388 | return sym; | |
389 | } | |
390 | ||
391 | /* Okay, we didn't find a match in our children, so look for the | |
392 | name in the current namespace. */ | |
393 | ||
394 | namespace = alloca (scope_len + 1); | |
395 | strncpy (namespace, scope, scope_len); | |
396 | namespace[scope_len] = '\0'; | |
397 | return cp_lookup_symbol_namespace (namespace, name, linkage_name, | |
398 | block, domain, symtab); | |
399 | } | |
400 | ||
401 | /* Look up NAME in the C++ namespace NAMESPACE, applying the using | |
402 | directives that are active in BLOCK. Other arguments are as in | |
403 | cp_lookup_symbol_nonlocal. */ | |
404 | ||
405 | struct symbol * | |
406 | cp_lookup_symbol_namespace (const char *namespace, | |
407 | const char *name, | |
408 | const char *linkage_name, | |
409 | const struct block *block, | |
410 | const domain_enum domain, | |
411 | struct symtab **symtab) | |
412 | { | |
413 | const struct using_direct *current; | |
414 | struct symbol *sym; | |
415 | ||
416 | /* First, go through the using directives. If any of them add new | |
417 | names to the namespace we're searching in, see if we can find a | |
418 | match by applying them. */ | |
419 | ||
420 | for (current = block_using (block); | |
421 | current != NULL; | |
422 | current = current->next) | |
423 | { | |
424 | if (strcmp (namespace, current->outer) == 0) | |
425 | { | |
426 | sym = cp_lookup_symbol_namespace (current->inner, | |
427 | name, | |
428 | linkage_name, | |
429 | block, | |
430 | domain, | |
431 | symtab); | |
432 | if (sym != NULL) | |
433 | return sym; | |
434 | } | |
435 | } | |
436 | ||
437 | /* We didn't find anything by applying any of the using directives | |
438 | that are still applicable; so let's see if we've got a match | |
439 | using the current namespace. */ | |
440 | ||
441 | if (namespace[0] == '\0') | |
442 | { | |
443 | return lookup_symbol_file (name, linkage_name, block, | |
444 | domain, symtab, 0); | |
445 | } | |
446 | else | |
447 | { | |
448 | char *concatenated_name | |
449 | = alloca (strlen (namespace) + 2 + strlen (name) + 1); | |
450 | strcpy (concatenated_name, namespace); | |
451 | strcat (concatenated_name, "::"); | |
452 | strcat (concatenated_name, name); | |
453 | sym = lookup_symbol_file (concatenated_name, linkage_name, | |
454 | block, domain, symtab, | |
455 | cp_is_anonymous (namespace)); | |
456 | return sym; | |
457 | } | |
458 | } | |
459 | ||
460 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
461 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
462 | within an anonymous namespace. Other arguments are as in | |
463 | cp_lookup_symbol_nonlocal. */ | |
464 | ||
465 | static struct symbol * | |
466 | lookup_symbol_file (const char *name, | |
467 | const char *linkage_name, | |
468 | const struct block *block, | |
469 | const domain_enum domain, | |
470 | struct symtab **symtab, | |
471 | int anonymous_namespace) | |
472 | { | |
473 | struct symbol *sym = NULL; | |
474 | ||
475 | sym = lookup_symbol_static (name, linkage_name, block, domain, symtab); | |
476 | if (sym != NULL) | |
477 | return sym; | |
478 | ||
479 | if (anonymous_namespace) | |
480 | { | |
481 | /* Symbols defined in anonymous namespaces have external linkage | |
482 | but should be treated as local to a single file nonetheless. | |
483 | So we only search the current file's global block. */ | |
484 | ||
485 | const struct block *global_block = block_global_block (block); | |
486 | ||
487 | if (global_block != NULL) | |
5c4e30ca DC |
488 | sym = lookup_symbol_aux_block (name, linkage_name, global_block, |
489 | domain, symtab); | |
1fcb5155 DC |
490 | } |
491 | else | |
492 | { | |
5c4e30ca DC |
493 | sym = lookup_symbol_global (name, linkage_name, domain, symtab); |
494 | } | |
495 | ||
496 | if (sym != NULL) | |
497 | return sym; | |
498 | ||
499 | /* Now call "lookup_possible_namespace_symbol". Symbols in here | |
500 | claim to be associated to namespaces, but this claim might be | |
501 | incorrect: the names in question might actually correspond to | |
502 | classes instead of namespaces. But if they correspond to | |
503 | classes, then we should have found a match for them above. So if | |
504 | we find them now, they should be genuine. */ | |
505 | ||
506 | /* FIXME: carlton/2003-06-12: This is a hack and should eventually | |
507 | be deleted: see comments below. */ | |
508 | ||
509 | if (domain == VAR_DOMAIN) | |
510 | { | |
511 | sym = lookup_possible_namespace_symbol (name, symtab); | |
512 | if (sym != NULL) | |
513 | return sym; | |
514 | } | |
515 | ||
516 | return NULL; | |
517 | } | |
518 | ||
79c2c32d DC |
519 | /* Look up a type named NESTED_NAME that is nested inside the C++ |
520 | class or namespace given by PARENT_TYPE, from within the context | |
521 | given by BLOCK. Return NULL if there is no such nested type. */ | |
522 | ||
523 | /* FIXME: carlton/2003-09-24: For now, this only works for nested | |
524 | namespaces; the patch to make this work on other sorts of nested | |
525 | types is next on my TODO list. */ | |
526 | ||
527 | struct type * | |
528 | cp_lookup_nested_type (struct type *parent_type, | |
529 | const char *nested_name, | |
530 | const struct block *block) | |
531 | { | |
532 | switch (TYPE_CODE (parent_type)) | |
533 | { | |
534 | case TYPE_CODE_NAMESPACE: | |
535 | { | |
536 | const char *parent_name = TYPE_TAG_NAME (parent_type); | |
537 | struct symbol *sym = cp_lookup_symbol_namespace (parent_name, | |
538 | nested_name, | |
539 | NULL, | |
540 | block, | |
541 | VAR_DOMAIN, | |
542 | NULL); | |
543 | if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF) | |
544 | return NULL; | |
545 | else | |
546 | return SYMBOL_TYPE (sym); | |
547 | } | |
548 | default: | |
549 | internal_error (__FILE__, __LINE__, | |
550 | "cp_lookup_nested_type called on a non-namespace."); | |
551 | } | |
552 | } | |
553 | ||
5c4e30ca DC |
554 | /* Now come functions for dealing with symbols associated to |
555 | namespaces. (They're used to store the namespaces themselves, not | |
556 | objects that live in the namespaces.) These symbols come in two | |
557 | varieties: if we run into a DW_TAG_namespace DIE, then we know that | |
558 | we have a namespace, so dwarf2read.c creates a symbol for it just | |
559 | like normal. But, unfortunately, versions of GCC through at least | |
560 | 3.3 don't generate those DIE's. Our solution is to try to guess | |
561 | their existence by looking at demangled names. This might cause us | |
562 | to misidentify classes as namespaces, however. So we put those | |
563 | symbols in a special block (one per objfile), and we only search | |
564 | that block as a last resort. */ | |
565 | ||
566 | /* FIXME: carlton/2003-06-12: Once versions of GCC that generate | |
567 | DW_TAG_namespace have been out for a year or two, we should get rid | |
568 | of all of this "possible namespace" nonsense. */ | |
569 | ||
570 | /* Allocate everything necessary for the possible namespace block | |
571 | associated to OBJFILE. */ | |
572 | ||
573 | static void | |
574 | initialize_namespace_symtab (struct objfile *objfile) | |
575 | { | |
576 | struct symtab *namespace_symtab; | |
577 | struct blockvector *bv; | |
578 | struct block *bl; | |
579 | ||
580 | namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile); | |
581 | namespace_symtab->language = language_cplus; | |
582 | namespace_symtab->free_code = free_nothing; | |
583 | namespace_symtab->dirname = NULL; | |
584 | ||
585 | bv = obstack_alloc (&objfile->symbol_obstack, | |
586 | sizeof (struct blockvector) | |
587 | + FIRST_LOCAL_BLOCK * sizeof (struct block *)); | |
588 | BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1; | |
589 | BLOCKVECTOR (namespace_symtab) = bv; | |
590 | ||
591 | /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */ | |
592 | ||
593 | bl = allocate_block (&objfile->symbol_obstack); | |
594 | BLOCK_DICT (bl) = dict_create_linear (&objfile->symbol_obstack, | |
595 | NULL); | |
596 | BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl; | |
597 | bl = allocate_block (&objfile->symbol_obstack); | |
598 | BLOCK_DICT (bl) = dict_create_linear (&objfile->symbol_obstack, | |
599 | NULL); | |
600 | BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl; | |
601 | ||
602 | /* Allocate the possible namespace block; we put it where the first | |
603 | local block will live, though I don't think there's any need to | |
604 | pretend that it's actually a local block (e.g. by setting | |
605 | BLOCK_SUPERBLOCK appropriately). We don't use the global or | |
606 | static block because we don't want it searched during the normal | |
607 | search of all global/static blocks in lookup_symbol: we only want | |
608 | it used as a last resort. */ | |
609 | ||
610 | /* NOTE: carlton/2003-09-11: I considered not associating the fake | |
611 | symbols to a block/symtab at all. But that would cause problems | |
612 | with lookup_symbol's SYMTAB argument and with block_found, so | |
613 | having a symtab/block for this purpose seems like the best | |
614 | solution for now. */ | |
615 | ||
616 | bl = allocate_block (&objfile->symbol_obstack); | |
617 | BLOCK_DICT (bl) = dict_create_hashed_expandable (); | |
618 | BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl; | |
619 | ||
620 | namespace_symtab->free_func = free_namespace_block; | |
621 | ||
622 | objfile->cp_namespace_symtab = namespace_symtab; | |
623 | } | |
624 | ||
625 | /* Locate the possible namespace block associated to OBJFILE, | |
626 | allocating it if necessary. */ | |
627 | ||
628 | static struct block * | |
629 | get_possible_namespace_block (struct objfile *objfile) | |
630 | { | |
631 | if (objfile->cp_namespace_symtab == NULL) | |
632 | initialize_namespace_symtab (objfile); | |
633 | ||
634 | return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab), | |
635 | FIRST_LOCAL_BLOCK); | |
636 | } | |
637 | ||
638 | /* Free the dictionary associated to the possible namespace block. */ | |
639 | ||
640 | static void | |
641 | free_namespace_block (struct symtab *symtab) | |
642 | { | |
643 | struct block *possible_namespace_block; | |
644 | ||
645 | possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), | |
646 | FIRST_LOCAL_BLOCK); | |
647 | gdb_assert (possible_namespace_block != NULL); | |
648 | dict_free (BLOCK_DICT (possible_namespace_block)); | |
649 | } | |
650 | ||
651 | /* Ensure that there are symbols in the possible namespace block | |
652 | associated to OBJFILE for all initial substrings of NAME that look | |
653 | like namespaces or classes. NAME should end in a member variable: | |
654 | it shouldn't consist solely of namespaces. */ | |
655 | ||
656 | void | |
657 | cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile) | |
658 | { | |
659 | check_possible_namespace_symbols_loop (name, | |
660 | cp_find_first_component (name), | |
661 | objfile); | |
662 | } | |
663 | ||
664 | /* This is a helper loop for cp_check_possible_namespace_symbols; it | |
665 | ensures that there are symbols in the possible namespace block | |
666 | associated to OBJFILE for all namespaces that are initial | |
667 | substrings of NAME of length at least LEN. It returns 1 if a | |
668 | previous loop had already created the shortest such symbol and 0 | |
669 | otherwise. | |
670 | ||
671 | This function assumes that if there is already a symbol associated | |
672 | to a substring of NAME of a given length, then there are already | |
673 | symbols associated to all substrings of NAME whose length is less | |
674 | than that length. So if cp_check_possible_namespace_symbols has | |
675 | been called once with argument "A::B::C::member", then that will | |
676 | create symbols "A", "A::B", and "A::B::C". If it is then later | |
677 | called with argument "A::B::D::member", then the new call will | |
678 | generate a new symbol for "A::B::D", but once it sees that "A::B" | |
679 | has already been created, it doesn't bother checking to see if "A" | |
680 | has also been created. */ | |
681 | ||
682 | static int | |
683 | check_possible_namespace_symbols_loop (const char *name, int len, | |
684 | struct objfile *objfile) | |
685 | { | |
686 | if (name[len] == ':') | |
687 | { | |
688 | int done; | |
689 | int next_len = len + 2; | |
690 | ||
691 | next_len += cp_find_first_component (name + next_len); | |
692 | done = check_possible_namespace_symbols_loop (name, next_len, | |
693 | objfile); | |
694 | ||
695 | if (!done) | |
696 | done = check_one_possible_namespace_symbol (name, len, objfile); | |
697 | ||
698 | return done; | |
1fcb5155 | 699 | } |
5c4e30ca DC |
700 | else |
701 | return 0; | |
702 | } | |
703 | ||
704 | /* Check to see if there's already a possible namespace symbol in | |
705 | OBJFILE whose name is the initial substring of NAME of length LEN. | |
706 | If not, create one and return 0; otherwise, return 1. */ | |
707 | ||
708 | static int | |
709 | check_one_possible_namespace_symbol (const char *name, int len, | |
710 | struct objfile *objfile) | |
711 | { | |
712 | struct block *block = get_possible_namespace_block (objfile); | |
713 | char *name_copy = obsavestring (name, len, &objfile->symbol_obstack); | |
714 | struct symbol *sym = lookup_block_symbol (block, name_copy, NULL, | |
715 | VAR_DOMAIN); | |
716 | ||
717 | if (sym == NULL) | |
718 | { | |
719 | struct type *type = init_type (TYPE_CODE_NAMESPACE, 0, 0, | |
720 | name_copy, objfile); | |
721 | TYPE_TAG_NAME (type) = TYPE_NAME (type); | |
722 | ||
723 | sym = obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol)); | |
724 | memset (sym, 0, sizeof (struct symbol)); | |
725 | SYMBOL_LANGUAGE (sym) = language_cplus; | |
726 | SYMBOL_SET_NAMES (sym, name_copy, len, objfile); | |
727 | SYMBOL_CLASS (sym) = LOC_TYPEDEF; | |
728 | SYMBOL_TYPE (sym) = type; | |
729 | SYMBOL_DOMAIN (sym) = VAR_DOMAIN; | |
730 | ||
731 | dict_add_symbol (BLOCK_DICT (block), sym); | |
732 | ||
733 | return 0; | |
734 | } | |
735 | else | |
736 | { | |
737 | obstack_free (&objfile->symbol_obstack, name_copy); | |
738 | ||
739 | return 1; | |
740 | } | |
741 | } | |
742 | ||
743 | /* Look for a symbol named NAME in all the possible namespace blocks. | |
744 | If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to | |
745 | equal the symtab where it was found. */ | |
746 | ||
747 | static struct symbol * | |
748 | lookup_possible_namespace_symbol (const char *name, struct symtab **symtab) | |
749 | { | |
750 | struct objfile *objfile; | |
751 | ||
752 | ALL_OBJFILES (objfile) | |
753 | { | |
754 | struct symbol *sym; | |
755 | ||
756 | sym = lookup_block_symbol (get_possible_namespace_block (objfile), | |
757 | name, NULL, VAR_DOMAIN); | |
758 | ||
759 | if (sym != NULL) | |
760 | { | |
761 | if (symtab != NULL) | |
762 | *symtab = objfile->cp_namespace_symtab; | |
763 | ||
764 | return sym; | |
765 | } | |
766 | } | |
767 | ||
768 | return NULL; | |
769 | } | |
770 | ||
771 | /* Print out all the possible namespace symbols. */ | |
772 | ||
773 | static void | |
774 | maintenance_cplus_namespace (char *args, int from_tty) | |
775 | { | |
776 | struct objfile *objfile; | |
777 | printf_unfiltered ("Possible namespaces:\n"); | |
778 | ALL_OBJFILES (objfile) | |
779 | { | |
780 | struct dict_iterator iter; | |
781 | struct symbol *sym; | |
782 | ||
783 | ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym) | |
784 | { | |
785 | printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym)); | |
786 | } | |
787 | } | |
788 | } | |
789 | ||
790 | void | |
791 | _initialize_cp_namespace (void) | |
792 | { | |
793 | add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace, | |
794 | "Print the list of possible C++ namespaces.", | |
795 | &maint_cplus_cmd_list); | |
1fcb5155 | 796 | } |