]>
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 | 34 | |
63d06c5c DC |
35 | /* When set, the file that we're processing is known to have debugging |
36 | info for C++ namespaces. */ | |
37 | ||
38 | /* NOTE: carlton/2004-01-13: No currently released version of GCC (the | |
39 | latest of which is 3.3.x at the time of this writing) produces this | |
40 | debug info. GCC 3.4 should, however. */ | |
9219021c DC |
41 | |
42 | unsigned char processing_has_namespace_info; | |
43 | ||
38d518c9 EZ |
44 | /* This contains our best guess as to the name of the current |
45 | enclosing namespace(s)/class(es), if any. For example, if we're | |
46 | within the method foo() in the following code: | |
9219021c | 47 | |
38d518c9 EZ |
48 | namespace N { |
49 | class C { | |
50 | void foo () { | |
51 | } | |
52 | }; | |
53 | } | |
54 | ||
55 | then processing_current_prefix should be set to "N::C". If | |
56 | processing_has_namespace_info is false, then this variable might | |
57 | not be reliable. */ | |
0fc9922a | 58 | |
38d518c9 | 59 | const char *processing_current_prefix; |
9219021c DC |
60 | |
61 | /* List of using directives that are active in the current file. */ | |
62 | ||
63 | static struct using_direct *using_list; | |
64 | ||
65 | static struct using_direct *cp_add_using (const char *name, | |
66 | unsigned int inner_len, | |
67 | unsigned int outer_len, | |
68 | struct using_direct *next); | |
69 | ||
70 | static struct using_direct *cp_copy_usings (struct using_direct *using, | |
71 | struct obstack *obstack); | |
72 | ||
1fcb5155 DC |
73 | static struct symbol *lookup_namespace_scope (const char *name, |
74 | const char *linkage_name, | |
75 | const struct block *block, | |
76 | const domain_enum domain, | |
77 | struct symtab **symtab, | |
78 | const char *scope, | |
79 | int scope_len); | |
80 | ||
81 | static struct symbol *lookup_symbol_file (const char *name, | |
82 | const char *linkage_name, | |
83 | const struct block *block, | |
84 | const domain_enum domain, | |
85 | struct symtab **symtab, | |
86 | int anonymous_namespace); | |
87 | ||
5c4e30ca DC |
88 | static void initialize_namespace_symtab (struct objfile *objfile); |
89 | ||
90 | static struct block *get_possible_namespace_block (struct objfile *objfile); | |
91 | ||
92 | static void free_namespace_block (struct symtab *symtab); | |
93 | ||
94 | static int check_possible_namespace_symbols_loop (const char *name, | |
95 | int len, | |
96 | struct objfile *objfile); | |
97 | ||
98 | static int check_one_possible_namespace_symbol (const char *name, | |
99 | int len, | |
100 | struct objfile *objfile); | |
101 | ||
102 | static | |
103 | struct symbol *lookup_possible_namespace_symbol (const char *name, | |
104 | struct symtab **symtab); | |
105 | ||
106 | static void maintenance_cplus_namespace (char *args, int from_tty); | |
107 | ||
9219021c DC |
108 | /* Set up support for dealing with C++ namespace info in the current |
109 | symtab. */ | |
110 | ||
111 | void cp_initialize_namespace () | |
112 | { | |
113 | processing_has_namespace_info = 0; | |
114 | using_list = NULL; | |
115 | } | |
116 | ||
117 | /* Add all the using directives we've gathered to the current symtab. | |
118 | STATIC_BLOCK should be the symtab's static block; OBSTACK is used | |
119 | for allocation. */ | |
120 | ||
121 | void | |
122 | cp_finalize_namespace (struct block *static_block, | |
123 | struct obstack *obstack) | |
124 | { | |
125 | if (using_list != NULL) | |
126 | { | |
127 | block_set_using (static_block, | |
128 | cp_copy_usings (using_list, obstack), | |
129 | obstack); | |
130 | using_list = NULL; | |
131 | } | |
132 | } | |
133 | ||
134 | /* Check to see if SYMBOL refers to an object contained within an | |
135 | anonymous namespace; if so, add an appropriate using directive. */ | |
136 | ||
137 | /* Optimize away strlen ("(anonymous namespace)"). */ | |
138 | ||
139 | #define ANONYMOUS_NAMESPACE_LEN 21 | |
140 | ||
141 | void | |
142 | cp_scan_for_anonymous_namespaces (const struct symbol *symbol) | |
143 | { | |
144 | if (!processing_has_namespace_info | |
145 | && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) | |
146 | { | |
147 | const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol); | |
148 | unsigned int previous_component; | |
149 | unsigned int next_component; | |
150 | const char *len; | |
151 | ||
152 | /* Start with a quick-and-dirty check for mention of "(anonymous | |
153 | namespace)". */ | |
154 | ||
155 | if (!cp_is_anonymous (name)) | |
156 | return; | |
157 | ||
158 | previous_component = 0; | |
159 | next_component = cp_find_first_component (name + previous_component); | |
160 | ||
161 | while (name[next_component] == ':') | |
162 | { | |
163 | if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN | |
164 | && strncmp (name + previous_component, | |
165 | "(anonymous namespace)", | |
166 | ANONYMOUS_NAMESPACE_LEN) == 0) | |
167 | { | |
168 | /* We've found a component of the name that's an | |
169 | anonymous namespace. So add symbols in it to the | |
170 | namespace given by the previous component if there is | |
171 | one, or to the global namespace if there isn't. */ | |
172 | cp_add_using_directive (name, | |
173 | previous_component == 0 | |
174 | ? 0 : previous_component - 2, | |
175 | next_component); | |
176 | } | |
177 | /* The "+ 2" is for the "::". */ | |
178 | previous_component = next_component + 2; | |
179 | next_component = (previous_component | |
180 | + cp_find_first_component (name | |
181 | + previous_component)); | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | /* Add a using directive to using_list. NAME is the start of a string | |
187 | that should contain the namespaces we want to add as initial | |
188 | substrings, OUTER_LENGTH is the end of the outer namespace, and | |
189 | INNER_LENGTH is the end of the inner namespace. If the using | |
190 | directive in question has already been added, don't add it | |
191 | twice. */ | |
192 | ||
193 | void | |
194 | cp_add_using_directive (const char *name, unsigned int outer_length, | |
195 | unsigned int inner_length) | |
196 | { | |
197 | struct using_direct *current; | |
198 | struct using_direct *new; | |
199 | ||
200 | /* Has it already been added? */ | |
201 | ||
202 | for (current = using_list; current != NULL; current = current->next) | |
203 | { | |
204 | if ((strncmp (current->inner, name, inner_length) == 0) | |
205 | && (strlen (current->inner) == inner_length) | |
206 | && (strlen (current->outer) == outer_length)) | |
207 | return; | |
208 | } | |
209 | ||
210 | using_list = cp_add_using (name, inner_length, outer_length, | |
211 | using_list); | |
212 | } | |
213 | ||
214 | /* Record the namespace that the function defined by SYMBOL was | |
215 | defined in, if necessary. BLOCK is the associated block; use | |
216 | OBSTACK for allocation. */ | |
217 | ||
218 | void | |
219 | cp_set_block_scope (const struct symbol *symbol, | |
220 | struct block *block, | |
221 | struct obstack *obstack) | |
222 | { | |
223 | /* Make sure that the name was originally mangled: if not, there | |
224 | certainly isn't any namespace information to worry about! */ | |
225 | ||
226 | if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) | |
227 | { | |
228 | if (processing_has_namespace_info) | |
229 | { | |
230 | block_set_scope | |
38d518c9 EZ |
231 | (block, obsavestring (processing_current_prefix, |
232 | strlen (processing_current_prefix), | |
9219021c DC |
233 | obstack), |
234 | obstack); | |
235 | } | |
236 | else | |
237 | { | |
238 | /* Try to figure out the appropriate namespace from the | |
239 | demangled name. */ | |
240 | ||
241 | /* FIXME: carlton/2003-04-15: If the function in question is | |
242 | a method of a class, the name will actually include the | |
243 | name of the class as well. This should be harmless, but | |
244 | is a little unfortunate. */ | |
245 | ||
246 | const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol); | |
247 | unsigned int prefix_len = cp_entire_prefix_len (name); | |
248 | ||
249 | block_set_scope (block, | |
250 | obsavestring (name, prefix_len, obstack), | |
251 | obstack); | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
256 | /* Test whether or not NAMESPACE looks like it mentions an anonymous | |
257 | namespace; return nonzero if so. */ | |
258 | ||
259 | int | |
260 | cp_is_anonymous (const char *namespace) | |
261 | { | |
262 | return (strstr (namespace, "(anonymous namespace)") | |
263 | != NULL); | |
264 | } | |
265 | ||
266 | /* Create a new struct using direct whose inner namespace is the | |
267 | initial substring of NAME of leng INNER_LEN and whose outer | |
268 | namespace is the initial substring of NAME of length OUTER_LENGTH. | |
269 | Set its next member in the linked list to NEXT; allocate all memory | |
270 | using xmalloc. It copies the strings, so NAME can be a temporary | |
271 | string. */ | |
272 | ||
273 | static struct using_direct * | |
274 | cp_add_using (const char *name, | |
275 | unsigned int inner_len, | |
276 | unsigned int outer_len, | |
277 | struct using_direct *next) | |
278 | { | |
279 | struct using_direct *retval; | |
280 | ||
281 | gdb_assert (outer_len < inner_len); | |
282 | ||
283 | retval = xmalloc (sizeof (struct using_direct)); | |
284 | retval->inner = savestring (name, inner_len); | |
285 | retval->outer = savestring (name, outer_len); | |
286 | retval->next = next; | |
287 | ||
288 | return retval; | |
289 | } | |
290 | ||
291 | /* Make a copy of the using directives in the list pointed to by | |
292 | USING, using OBSTACK to allocate memory. Free all memory pointed | |
293 | to by USING via xfree. */ | |
294 | ||
295 | static struct using_direct * | |
296 | cp_copy_usings (struct using_direct *using, | |
297 | struct obstack *obstack) | |
298 | { | |
299 | if (using == NULL) | |
300 | { | |
301 | return NULL; | |
302 | } | |
303 | else | |
304 | { | |
305 | struct using_direct *retval | |
306 | = obstack_alloc (obstack, sizeof (struct using_direct)); | |
307 | retval->inner = obsavestring (using->inner, strlen (using->inner), | |
308 | obstack); | |
309 | retval->outer = obsavestring (using->outer, strlen (using->outer), | |
310 | obstack); | |
311 | retval->next = cp_copy_usings (using->next, obstack); | |
312 | ||
313 | xfree (using->inner); | |
314 | xfree (using->outer); | |
315 | xfree (using); | |
316 | ||
317 | return retval; | |
318 | } | |
319 | } | |
1fcb5155 DC |
320 | |
321 | /* The C++-specific version of name lookup for static and global | |
322 | names. This makes sure that names get looked for in all namespaces | |
323 | that are in scope. NAME is the natural name of the symbol that | |
324 | we're looking for, LINKAGE_NAME (which is optional) is its linkage | |
325 | name, BLOCK is the block that we're searching within, DOMAIN says | |
326 | what kind of symbols we're looking for, and if SYMTAB is non-NULL, | |
327 | we should store the symtab where we found the symbol in it. */ | |
328 | ||
329 | struct symbol * | |
330 | cp_lookup_symbol_nonlocal (const char *name, | |
331 | const char *linkage_name, | |
332 | const struct block *block, | |
333 | const domain_enum domain, | |
334 | struct symtab **symtab) | |
335 | { | |
336 | return lookup_namespace_scope (name, linkage_name, block, domain, | |
337 | symtab, block_scope (block), 0); | |
338 | } | |
339 | ||
340 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
341 | global variables). SCOPE is the namespace that the current | |
342 | function is defined within; only consider namespaces whose length | |
343 | is at least SCOPE_LEN. Other arguments are as in | |
344 | cp_lookup_symbol_nonlocal. | |
345 | ||
346 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 347 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
348 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
349 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
350 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
351 | "A::B::x"; if it doesn't find it, then the second call looks for | |
352 | "A::x", and if that call fails, then the first call looks for | |
353 | "x". */ | |
354 | ||
355 | static struct symbol * | |
356 | lookup_namespace_scope (const char *name, | |
357 | const char *linkage_name, | |
358 | const struct block *block, | |
359 | const domain_enum domain, | |
360 | struct symtab **symtab, | |
361 | const char *scope, | |
362 | int scope_len) | |
363 | { | |
364 | char *namespace; | |
365 | ||
366 | if (scope[scope_len] != '\0') | |
367 | { | |
368 | /* Recursively search for names in child namespaces first. */ | |
369 | ||
370 | struct symbol *sym; | |
371 | int new_scope_len = scope_len; | |
372 | ||
373 | /* If the current scope is followed by "::", skip past that. */ | |
374 | if (new_scope_len != 0) | |
375 | { | |
376 | gdb_assert (scope[new_scope_len] == ':'); | |
377 | new_scope_len += 2; | |
378 | } | |
379 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
380 | sym = lookup_namespace_scope (name, linkage_name, block, | |
381 | domain, symtab, | |
382 | scope, new_scope_len); | |
383 | if (sym != NULL) | |
384 | return sym; | |
385 | } | |
386 | ||
387 | /* Okay, we didn't find a match in our children, so look for the | |
388 | name in the current namespace. */ | |
389 | ||
390 | namespace = alloca (scope_len + 1); | |
391 | strncpy (namespace, scope, scope_len); | |
392 | namespace[scope_len] = '\0'; | |
393 | return cp_lookup_symbol_namespace (namespace, name, linkage_name, | |
394 | block, domain, symtab); | |
395 | } | |
396 | ||
397 | /* Look up NAME in the C++ namespace NAMESPACE, applying the using | |
398 | directives that are active in BLOCK. Other arguments are as in | |
399 | cp_lookup_symbol_nonlocal. */ | |
400 | ||
401 | struct symbol * | |
402 | cp_lookup_symbol_namespace (const char *namespace, | |
403 | const char *name, | |
404 | const char *linkage_name, | |
405 | const struct block *block, | |
406 | const domain_enum domain, | |
407 | struct symtab **symtab) | |
408 | { | |
409 | const struct using_direct *current; | |
410 | struct symbol *sym; | |
411 | ||
412 | /* First, go through the using directives. If any of them add new | |
413 | names to the namespace we're searching in, see if we can find a | |
414 | match by applying them. */ | |
415 | ||
416 | for (current = block_using (block); | |
417 | current != NULL; | |
418 | current = current->next) | |
419 | { | |
420 | if (strcmp (namespace, current->outer) == 0) | |
421 | { | |
422 | sym = cp_lookup_symbol_namespace (current->inner, | |
423 | name, | |
424 | linkage_name, | |
425 | block, | |
426 | domain, | |
427 | symtab); | |
428 | if (sym != NULL) | |
429 | return sym; | |
430 | } | |
431 | } | |
432 | ||
433 | /* We didn't find anything by applying any of the using directives | |
434 | that are still applicable; so let's see if we've got a match | |
435 | using the current namespace. */ | |
436 | ||
437 | if (namespace[0] == '\0') | |
438 | { | |
439 | return lookup_symbol_file (name, linkage_name, block, | |
440 | domain, symtab, 0); | |
441 | } | |
442 | else | |
443 | { | |
444 | char *concatenated_name | |
445 | = alloca (strlen (namespace) + 2 + strlen (name) + 1); | |
446 | strcpy (concatenated_name, namespace); | |
447 | strcat (concatenated_name, "::"); | |
448 | strcat (concatenated_name, name); | |
449 | sym = lookup_symbol_file (concatenated_name, linkage_name, | |
450 | block, domain, symtab, | |
451 | cp_is_anonymous (namespace)); | |
452 | return sym; | |
453 | } | |
454 | } | |
455 | ||
456 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
457 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
458 | within an anonymous namespace. Other arguments are as in | |
459 | cp_lookup_symbol_nonlocal. */ | |
460 | ||
461 | static struct symbol * | |
462 | lookup_symbol_file (const char *name, | |
463 | const char *linkage_name, | |
464 | const struct block *block, | |
465 | const domain_enum domain, | |
466 | struct symtab **symtab, | |
467 | int anonymous_namespace) | |
468 | { | |
469 | struct symbol *sym = NULL; | |
470 | ||
471 | sym = lookup_symbol_static (name, linkage_name, block, domain, symtab); | |
472 | if (sym != NULL) | |
473 | return sym; | |
474 | ||
475 | if (anonymous_namespace) | |
476 | { | |
477 | /* Symbols defined in anonymous namespaces have external linkage | |
478 | but should be treated as local to a single file nonetheless. | |
479 | So we only search the current file's global block. */ | |
480 | ||
481 | const struct block *global_block = block_global_block (block); | |
482 | ||
483 | if (global_block != NULL) | |
5c4e30ca DC |
484 | sym = lookup_symbol_aux_block (name, linkage_name, global_block, |
485 | domain, symtab); | |
1fcb5155 DC |
486 | } |
487 | else | |
488 | { | |
5c4e30ca DC |
489 | sym = lookup_symbol_global (name, linkage_name, domain, symtab); |
490 | } | |
491 | ||
492 | if (sym != NULL) | |
493 | return sym; | |
494 | ||
495 | /* Now call "lookup_possible_namespace_symbol". Symbols in here | |
496 | claim to be associated to namespaces, but this claim might be | |
497 | incorrect: the names in question might actually correspond to | |
498 | classes instead of namespaces. But if they correspond to | |
499 | classes, then we should have found a match for them above. So if | |
500 | we find them now, they should be genuine. */ | |
501 | ||
502 | /* FIXME: carlton/2003-06-12: This is a hack and should eventually | |
503 | be deleted: see comments below. */ | |
504 | ||
505 | if (domain == VAR_DOMAIN) | |
506 | { | |
507 | sym = lookup_possible_namespace_symbol (name, symtab); | |
508 | if (sym != NULL) | |
509 | return sym; | |
510 | } | |
511 | ||
512 | return NULL; | |
513 | } | |
514 | ||
79c2c32d DC |
515 | /* Look up a type named NESTED_NAME that is nested inside the C++ |
516 | class or namespace given by PARENT_TYPE, from within the context | |
517 | given by BLOCK. Return NULL if there is no such nested type. */ | |
518 | ||
79c2c32d DC |
519 | struct type * |
520 | cp_lookup_nested_type (struct type *parent_type, | |
521 | const char *nested_name, | |
522 | const struct block *block) | |
523 | { | |
524 | switch (TYPE_CODE (parent_type)) | |
525 | { | |
63d06c5c | 526 | case TYPE_CODE_STRUCT: |
79c2c32d DC |
527 | case TYPE_CODE_NAMESPACE: |
528 | { | |
63d06c5c DC |
529 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
530 | of classes like, say, data or function members. Instead, | |
531 | they're just represented by symbols whose names are | |
532 | qualified by the name of the surrounding class. This is | |
533 | just like members of namespaces; in particular, | |
534 | lookup_symbol_namespace works when looking them up. */ | |
535 | ||
79c2c32d DC |
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__, | |
63d06c5c | 550 | "cp_lookup_nested_type called on a non-aggregate type."); |
79c2c32d DC |
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 | } |