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 DC |
217 | retval->next = next; |
218 | ||
219 | return retval; | |
220 | } | |
221 | ||
1fcb5155 DC |
222 | /* The C++-specific version of name lookup for static and global |
223 | names. This makes sure that names get looked for in all namespaces | |
224 | that are in scope. NAME is the natural name of the symbol that | |
225 | we're looking for, LINKAGE_NAME (which is optional) is its linkage | |
226 | name, BLOCK is the block that we're searching within, DOMAIN says | |
227 | what kind of symbols we're looking for, and if SYMTAB is non-NULL, | |
228 | we should store the symtab where we found the symbol in it. */ | |
229 | ||
230 | struct symbol * | |
231 | cp_lookup_symbol_nonlocal (const char *name, | |
232 | const char *linkage_name, | |
233 | const struct block *block, | |
21b556f4 | 234 | const domain_enum domain) |
1fcb5155 DC |
235 | { |
236 | return lookup_namespace_scope (name, linkage_name, block, domain, | |
21b556f4 | 237 | block_scope (block), 0); |
1fcb5155 DC |
238 | } |
239 | ||
240 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
241 | global variables). SCOPE is the namespace that the current | |
242 | function is defined within; only consider namespaces whose length | |
243 | is at least SCOPE_LEN. Other arguments are as in | |
244 | cp_lookup_symbol_nonlocal. | |
245 | ||
246 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 247 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
248 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
249 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
250 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
251 | "A::B::x"; if it doesn't find it, then the second call looks for | |
252 | "A::x", and if that call fails, then the first call looks for | |
253 | "x". */ | |
254 | ||
255 | static struct symbol * | |
256 | lookup_namespace_scope (const char *name, | |
257 | const char *linkage_name, | |
258 | const struct block *block, | |
259 | const domain_enum domain, | |
1fcb5155 DC |
260 | const char *scope, |
261 | int scope_len) | |
262 | { | |
263 | char *namespace; | |
264 | ||
265 | if (scope[scope_len] != '\0') | |
266 | { | |
267 | /* Recursively search for names in child namespaces first. */ | |
268 | ||
269 | struct symbol *sym; | |
270 | int new_scope_len = scope_len; | |
271 | ||
272 | /* If the current scope is followed by "::", skip past that. */ | |
273 | if (new_scope_len != 0) | |
274 | { | |
275 | gdb_assert (scope[new_scope_len] == ':'); | |
276 | new_scope_len += 2; | |
277 | } | |
278 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
279 | sym = lookup_namespace_scope (name, linkage_name, block, | |
21b556f4 | 280 | domain, scope, new_scope_len); |
1fcb5155 DC |
281 | if (sym != NULL) |
282 | return sym; | |
283 | } | |
284 | ||
285 | /* Okay, we didn't find a match in our children, so look for the | |
286 | name in the current namespace. */ | |
287 | ||
288 | namespace = alloca (scope_len + 1); | |
289 | strncpy (namespace, scope, scope_len); | |
290 | namespace[scope_len] = '\0'; | |
291 | return cp_lookup_symbol_namespace (namespace, name, linkage_name, | |
21b556f4 | 292 | block, domain); |
1fcb5155 DC |
293 | } |
294 | ||
295 | /* Look up NAME in the C++ namespace NAMESPACE, applying the using | |
296 | directives that are active in BLOCK. Other arguments are as in | |
297 | cp_lookup_symbol_nonlocal. */ | |
298 | ||
299 | struct symbol * | |
300 | cp_lookup_symbol_namespace (const char *namespace, | |
301 | const char *name, | |
302 | const char *linkage_name, | |
303 | const struct block *block, | |
21b556f4 | 304 | const domain_enum domain) |
1fcb5155 DC |
305 | { |
306 | const struct using_direct *current; | |
307 | struct symbol *sym; | |
308 | ||
309 | /* First, go through the using directives. If any of them add new | |
310 | names to the namespace we're searching in, see if we can find a | |
311 | match by applying them. */ | |
312 | ||
313 | for (current = block_using (block); | |
314 | current != NULL; | |
315 | current = current->next) | |
316 | { | |
8c902bb1 | 317 | if (strcmp (namespace, current->import_dest) == 0) |
1fcb5155 | 318 | { |
8c902bb1 | 319 | sym = cp_lookup_symbol_namespace (current->import_src, |
1fcb5155 DC |
320 | name, |
321 | linkage_name, | |
322 | block, | |
21b556f4 | 323 | domain); |
1fcb5155 DC |
324 | if (sym != NULL) |
325 | return sym; | |
326 | } | |
327 | } | |
328 | ||
329 | /* We didn't find anything by applying any of the using directives | |
330 | that are still applicable; so let's see if we've got a match | |
331 | using the current namespace. */ | |
332 | ||
333 | if (namespace[0] == '\0') | |
334 | { | |
335 | return lookup_symbol_file (name, linkage_name, block, | |
21b556f4 | 336 | domain, 0); |
1fcb5155 DC |
337 | } |
338 | else | |
339 | { | |
340 | char *concatenated_name | |
341 | = alloca (strlen (namespace) + 2 + strlen (name) + 1); | |
342 | strcpy (concatenated_name, namespace); | |
343 | strcat (concatenated_name, "::"); | |
344 | strcat (concatenated_name, name); | |
345 | sym = lookup_symbol_file (concatenated_name, linkage_name, | |
21b556f4 | 346 | block, domain, |
1fcb5155 DC |
347 | cp_is_anonymous (namespace)); |
348 | return sym; | |
349 | } | |
350 | } | |
351 | ||
352 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
353 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
354 | within an anonymous namespace. Other arguments are as in | |
355 | cp_lookup_symbol_nonlocal. */ | |
356 | ||
357 | static struct symbol * | |
358 | lookup_symbol_file (const char *name, | |
359 | const char *linkage_name, | |
360 | const struct block *block, | |
361 | const domain_enum domain, | |
1fcb5155 DC |
362 | int anonymous_namespace) |
363 | { | |
364 | struct symbol *sym = NULL; | |
365 | ||
21b556f4 | 366 | sym = lookup_symbol_static (name, linkage_name, block, domain); |
1fcb5155 DC |
367 | if (sym != NULL) |
368 | return sym; | |
369 | ||
370 | if (anonymous_namespace) | |
371 | { | |
372 | /* Symbols defined in anonymous namespaces have external linkage | |
373 | but should be treated as local to a single file nonetheless. | |
374 | So we only search the current file's global block. */ | |
375 | ||
376 | const struct block *global_block = block_global_block (block); | |
377 | ||
378 | if (global_block != NULL) | |
5c4e30ca | 379 | sym = lookup_symbol_aux_block (name, linkage_name, global_block, |
21b556f4 | 380 | domain); |
1fcb5155 DC |
381 | } |
382 | else | |
383 | { | |
21b556f4 | 384 | sym = lookup_symbol_global (name, linkage_name, block, domain); |
5c4e30ca DC |
385 | } |
386 | ||
387 | if (sym != NULL) | |
388 | return sym; | |
389 | ||
390 | /* Now call "lookup_possible_namespace_symbol". Symbols in here | |
391 | claim to be associated to namespaces, but this claim might be | |
392 | incorrect: the names in question might actually correspond to | |
393 | classes instead of namespaces. But if they correspond to | |
394 | classes, then we should have found a match for them above. So if | |
395 | we find them now, they should be genuine. */ | |
396 | ||
397 | /* FIXME: carlton/2003-06-12: This is a hack and should eventually | |
398 | be deleted: see comments below. */ | |
399 | ||
400 | if (domain == VAR_DOMAIN) | |
401 | { | |
21b556f4 | 402 | sym = lookup_possible_namespace_symbol (name); |
5c4e30ca DC |
403 | if (sym != NULL) |
404 | return sym; | |
405 | } | |
406 | ||
407 | return NULL; | |
408 | } | |
409 | ||
79c2c32d DC |
410 | /* Look up a type named NESTED_NAME that is nested inside the C++ |
411 | class or namespace given by PARENT_TYPE, from within the context | |
412 | given by BLOCK. Return NULL if there is no such nested type. */ | |
413 | ||
79c2c32d DC |
414 | struct type * |
415 | cp_lookup_nested_type (struct type *parent_type, | |
416 | const char *nested_name, | |
417 | const struct block *block) | |
418 | { | |
419 | switch (TYPE_CODE (parent_type)) | |
420 | { | |
63d06c5c | 421 | case TYPE_CODE_STRUCT: |
79c2c32d DC |
422 | case TYPE_CODE_NAMESPACE: |
423 | { | |
63d06c5c DC |
424 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
425 | of classes like, say, data or function members. Instead, | |
426 | they're just represented by symbols whose names are | |
427 | qualified by the name of the surrounding class. This is | |
428 | just like members of namespaces; in particular, | |
429 | lookup_symbol_namespace works when looking them up. */ | |
430 | ||
79c2c32d DC |
431 | const char *parent_name = TYPE_TAG_NAME (parent_type); |
432 | struct symbol *sym = cp_lookup_symbol_namespace (parent_name, | |
433 | nested_name, | |
434 | NULL, | |
435 | block, | |
21b556f4 | 436 | VAR_DOMAIN); |
79c2c32d DC |
437 | if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF) |
438 | return NULL; | |
439 | else | |
440 | return SYMBOL_TYPE (sym); | |
441 | } | |
442 | default: | |
443 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 444 | _("cp_lookup_nested_type called on a non-aggregate type.")); |
79c2c32d DC |
445 | } |
446 | } | |
447 | ||
b368761e DC |
448 | /* The C++-version of lookup_transparent_type. */ |
449 | ||
450 | /* FIXME: carlton/2004-01-16: The problem that this is trying to | |
451 | address is that, unfortunately, sometimes NAME is wrong: it may not | |
452 | include the name of namespaces enclosing the type in question. | |
453 | lookup_transparent_type gets called when the the type in question | |
454 | is a declaration, and we're trying to find its definition; but, for | |
455 | declarations, our type name deduction mechanism doesn't work. | |
456 | There's nothing we can do to fix this in general, I think, in the | |
457 | absence of debug information about namespaces (I've filed PR | |
458 | gdb/1511 about this); until such debug information becomes more | |
459 | prevalent, one heuristic which sometimes looks is to search for the | |
460 | definition in namespaces containing the current namespace. | |
461 | ||
462 | We should delete this functions once the appropriate debug | |
463 | information becomes more widespread. (GCC 3.4 will be the first | |
464 | released version of GCC with such information.) */ | |
465 | ||
466 | struct type * | |
467 | cp_lookup_transparent_type (const char *name) | |
468 | { | |
469 | /* First, try the honest way of looking up the definition. */ | |
470 | struct type *t = basic_lookup_transparent_type (name); | |
471 | const char *scope; | |
472 | ||
473 | if (t != NULL) | |
474 | return t; | |
475 | ||
476 | /* If that doesn't work and we're within a namespace, look there | |
477 | instead. */ | |
478 | scope = block_scope (get_selected_block (0)); | |
479 | ||
480 | if (scope[0] == '\0') | |
481 | return NULL; | |
482 | ||
483 | return cp_lookup_transparent_type_loop (name, scope, 0); | |
484 | } | |
485 | ||
486 | /* Lookup the the type definition associated to NAME in | |
487 | namespaces/classes containing SCOPE whose name is strictly longer | |
488 | than LENGTH. LENGTH must be the index of the start of a | |
489 | component of SCOPE. */ | |
490 | ||
491 | static struct type * | |
492 | cp_lookup_transparent_type_loop (const char *name, const char *scope, | |
493 | int length) | |
494 | { | |
1198ecbe | 495 | int scope_length = length + cp_find_first_component (scope + length); |
b368761e DC |
496 | char *full_name; |
497 | ||
498 | /* If the current scope is followed by "::", look in the next | |
499 | component. */ | |
500 | if (scope[scope_length] == ':') | |
501 | { | |
502 | struct type *retval | |
503 | = cp_lookup_transparent_type_loop (name, scope, scope_length + 2); | |
504 | if (retval != NULL) | |
505 | return retval; | |
506 | } | |
507 | ||
508 | full_name = alloca (scope_length + 2 + strlen (name) + 1); | |
509 | strncpy (full_name, scope, scope_length); | |
510 | strncpy (full_name + scope_length, "::", 2); | |
511 | strcpy (full_name + scope_length + 2, name); | |
512 | ||
513 | return basic_lookup_transparent_type (full_name); | |
514 | } | |
515 | ||
5c4e30ca DC |
516 | /* Now come functions for dealing with symbols associated to |
517 | namespaces. (They're used to store the namespaces themselves, not | |
518 | objects that live in the namespaces.) These symbols come in two | |
519 | varieties: if we run into a DW_TAG_namespace DIE, then we know that | |
520 | we have a namespace, so dwarf2read.c creates a symbol for it just | |
521 | like normal. But, unfortunately, versions of GCC through at least | |
522 | 3.3 don't generate those DIE's. Our solution is to try to guess | |
523 | their existence by looking at demangled names. This might cause us | |
524 | to misidentify classes as namespaces, however. So we put those | |
525 | symbols in a special block (one per objfile), and we only search | |
526 | that block as a last resort. */ | |
527 | ||
528 | /* FIXME: carlton/2003-06-12: Once versions of GCC that generate | |
529 | DW_TAG_namespace have been out for a year or two, we should get rid | |
530 | of all of this "possible namespace" nonsense. */ | |
531 | ||
532 | /* Allocate everything necessary for the possible namespace block | |
533 | associated to OBJFILE. */ | |
534 | ||
535 | static void | |
536 | initialize_namespace_symtab (struct objfile *objfile) | |
537 | { | |
538 | struct symtab *namespace_symtab; | |
539 | struct blockvector *bv; | |
540 | struct block *bl; | |
541 | ||
542 | namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile); | |
543 | namespace_symtab->language = language_cplus; | |
544 | namespace_symtab->free_code = free_nothing; | |
545 | namespace_symtab->dirname = NULL; | |
546 | ||
4a146b47 | 547 | bv = obstack_alloc (&objfile->objfile_obstack, |
5c4e30ca DC |
548 | sizeof (struct blockvector) |
549 | + FIRST_LOCAL_BLOCK * sizeof (struct block *)); | |
550 | BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1; | |
551 | BLOCKVECTOR (namespace_symtab) = bv; | |
552 | ||
553 | /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */ | |
554 | ||
4a146b47 EZ |
555 | bl = allocate_block (&objfile->objfile_obstack); |
556 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, | |
5c4e30ca DC |
557 | NULL); |
558 | BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl; | |
4a146b47 EZ |
559 | bl = allocate_block (&objfile->objfile_obstack); |
560 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, | |
5c4e30ca DC |
561 | NULL); |
562 | BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl; | |
563 | ||
564 | /* Allocate the possible namespace block; we put it where the first | |
565 | local block will live, though I don't think there's any need to | |
566 | pretend that it's actually a local block (e.g. by setting | |
567 | BLOCK_SUPERBLOCK appropriately). We don't use the global or | |
568 | static block because we don't want it searched during the normal | |
569 | search of all global/static blocks in lookup_symbol: we only want | |
570 | it used as a last resort. */ | |
571 | ||
572 | /* NOTE: carlton/2003-09-11: I considered not associating the fake | |
573 | symbols to a block/symtab at all. But that would cause problems | |
574 | with lookup_symbol's SYMTAB argument and with block_found, so | |
575 | having a symtab/block for this purpose seems like the best | |
576 | solution for now. */ | |
577 | ||
4a146b47 | 578 | bl = allocate_block (&objfile->objfile_obstack); |
5c4e30ca DC |
579 | BLOCK_DICT (bl) = dict_create_hashed_expandable (); |
580 | BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl; | |
581 | ||
582 | namespace_symtab->free_func = free_namespace_block; | |
583 | ||
584 | objfile->cp_namespace_symtab = namespace_symtab; | |
585 | } | |
586 | ||
587 | /* Locate the possible namespace block associated to OBJFILE, | |
588 | allocating it if necessary. */ | |
589 | ||
590 | static struct block * | |
591 | get_possible_namespace_block (struct objfile *objfile) | |
592 | { | |
593 | if (objfile->cp_namespace_symtab == NULL) | |
594 | initialize_namespace_symtab (objfile); | |
595 | ||
596 | return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab), | |
597 | FIRST_LOCAL_BLOCK); | |
598 | } | |
599 | ||
600 | /* Free the dictionary associated to the possible namespace block. */ | |
601 | ||
602 | static void | |
603 | free_namespace_block (struct symtab *symtab) | |
604 | { | |
605 | struct block *possible_namespace_block; | |
606 | ||
607 | possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), | |
608 | FIRST_LOCAL_BLOCK); | |
609 | gdb_assert (possible_namespace_block != NULL); | |
610 | dict_free (BLOCK_DICT (possible_namespace_block)); | |
611 | } | |
612 | ||
613 | /* Ensure that there are symbols in the possible namespace block | |
614 | associated to OBJFILE for all initial substrings of NAME that look | |
615 | like namespaces or classes. NAME should end in a member variable: | |
616 | it shouldn't consist solely of namespaces. */ | |
617 | ||
618 | void | |
619 | cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile) | |
620 | { | |
621 | check_possible_namespace_symbols_loop (name, | |
622 | cp_find_first_component (name), | |
623 | objfile); | |
624 | } | |
625 | ||
626 | /* This is a helper loop for cp_check_possible_namespace_symbols; it | |
627 | ensures that there are symbols in the possible namespace block | |
628 | associated to OBJFILE for all namespaces that are initial | |
629 | substrings of NAME of length at least LEN. It returns 1 if a | |
630 | previous loop had already created the shortest such symbol and 0 | |
631 | otherwise. | |
632 | ||
633 | This function assumes that if there is already a symbol associated | |
634 | to a substring of NAME of a given length, then there are already | |
635 | symbols associated to all substrings of NAME whose length is less | |
636 | than that length. So if cp_check_possible_namespace_symbols has | |
637 | been called once with argument "A::B::C::member", then that will | |
638 | create symbols "A", "A::B", and "A::B::C". If it is then later | |
639 | called with argument "A::B::D::member", then the new call will | |
640 | generate a new symbol for "A::B::D", but once it sees that "A::B" | |
641 | has already been created, it doesn't bother checking to see if "A" | |
642 | has also been created. */ | |
643 | ||
644 | static int | |
645 | check_possible_namespace_symbols_loop (const char *name, int len, | |
646 | struct objfile *objfile) | |
647 | { | |
648 | if (name[len] == ':') | |
649 | { | |
650 | int done; | |
651 | int next_len = len + 2; | |
652 | ||
653 | next_len += cp_find_first_component (name + next_len); | |
654 | done = check_possible_namespace_symbols_loop (name, next_len, | |
655 | objfile); | |
656 | ||
657 | if (!done) | |
658 | done = check_one_possible_namespace_symbol (name, len, objfile); | |
659 | ||
660 | return done; | |
1fcb5155 | 661 | } |
5c4e30ca DC |
662 | else |
663 | return 0; | |
664 | } | |
665 | ||
666 | /* Check to see if there's already a possible namespace symbol in | |
667 | OBJFILE whose name is the initial substring of NAME of length LEN. | |
668 | If not, create one and return 0; otherwise, return 1. */ | |
669 | ||
670 | static int | |
671 | check_one_possible_namespace_symbol (const char *name, int len, | |
672 | struct objfile *objfile) | |
673 | { | |
674 | struct block *block = get_possible_namespace_block (objfile); | |
ec5cdd75 DJ |
675 | char *name_copy = alloca (len + 1); |
676 | struct symbol *sym; | |
677 | ||
678 | memcpy (name_copy, name, len); | |
679 | name_copy[len] = '\0'; | |
680 | sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN); | |
5c4e30ca DC |
681 | |
682 | if (sym == NULL) | |
683 | { | |
ec5cdd75 | 684 | struct type *type; |
ec5cdd75 DJ |
685 | |
686 | type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile); | |
687 | ||
5c4e30ca DC |
688 | TYPE_TAG_NAME (type) = TYPE_NAME (type); |
689 | ||
4a146b47 | 690 | sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol)); |
5c4e30ca DC |
691 | memset (sym, 0, sizeof (struct symbol)); |
692 | SYMBOL_LANGUAGE (sym) = language_cplus; | |
04a679b8 TT |
693 | /* Note that init_type copied the name to the objfile's |
694 | obstack. */ | |
695 | SYMBOL_SET_NAMES (sym, TYPE_NAME (type), len, 0, objfile); | |
5c4e30ca DC |
696 | SYMBOL_CLASS (sym) = LOC_TYPEDEF; |
697 | SYMBOL_TYPE (sym) = type; | |
698 | SYMBOL_DOMAIN (sym) = VAR_DOMAIN; | |
699 | ||
700 | dict_add_symbol (BLOCK_DICT (block), sym); | |
701 | ||
702 | return 0; | |
703 | } | |
704 | else | |
ec5cdd75 | 705 | return 1; |
5c4e30ca DC |
706 | } |
707 | ||
708 | /* Look for a symbol named NAME in all the possible namespace blocks. | |
21b556f4 | 709 | If one is found, return it. */ |
5c4e30ca DC |
710 | |
711 | static struct symbol * | |
21b556f4 | 712 | lookup_possible_namespace_symbol (const char *name) |
5c4e30ca DC |
713 | { |
714 | struct objfile *objfile; | |
715 | ||
716 | ALL_OBJFILES (objfile) | |
717 | { | |
718 | struct symbol *sym; | |
719 | ||
720 | sym = lookup_block_symbol (get_possible_namespace_block (objfile), | |
721 | name, NULL, VAR_DOMAIN); | |
722 | ||
723 | if (sym != NULL) | |
21b556f4 | 724 | return sym; |
5c4e30ca DC |
725 | } |
726 | ||
727 | return NULL; | |
728 | } | |
729 | ||
730 | /* Print out all the possible namespace symbols. */ | |
731 | ||
732 | static void | |
733 | maintenance_cplus_namespace (char *args, int from_tty) | |
734 | { | |
735 | struct objfile *objfile; | |
a3f17187 | 736 | printf_unfiltered (_("Possible namespaces:\n")); |
5c4e30ca DC |
737 | ALL_OBJFILES (objfile) |
738 | { | |
739 | struct dict_iterator iter; | |
740 | struct symbol *sym; | |
741 | ||
742 | ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym) | |
743 | { | |
744 | printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym)); | |
745 | } | |
746 | } | |
747 | } | |
748 | ||
2c0b251b PA |
749 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
750 | extern initialize_file_ftype _initialize_cp_namespace; | |
751 | ||
5c4e30ca DC |
752 | void |
753 | _initialize_cp_namespace (void) | |
754 | { | |
755 | add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace, | |
1a966eab | 756 | _("Print the list of possible C++ namespaces."), |
5c4e30ca | 757 | &maint_cplus_cmd_list); |
1fcb5155 | 758 | } |