]>
Commit | Line | Data |
---|---|---|
9219021c DC |
1 | /* Helper routines for C++ support in GDB. |
2 | Copyright 2003 Free Software Foundation, Inc. | |
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" | |
30 | ||
31 | /* When set, the file that we're processing seems to have debugging | |
32 | info for C++ namespaces, so cp-namespace.c shouldn't try to guess | |
33 | namespace info itself. */ | |
34 | ||
35 | unsigned char processing_has_namespace_info; | |
36 | ||
37 | /* If processing_has_namespace_info is nonzero, this string should | |
38 | contain the name of the current namespace. The string is | |
39 | temporary; copy it if you need it. */ | |
40 | ||
41 | const char *processing_current_namespace; | |
42 | ||
43 | /* List of using directives that are active in the current file. */ | |
44 | ||
45 | static struct using_direct *using_list; | |
46 | ||
47 | static struct using_direct *cp_add_using (const char *name, | |
48 | unsigned int inner_len, | |
49 | unsigned int outer_len, | |
50 | struct using_direct *next); | |
51 | ||
52 | static struct using_direct *cp_copy_usings (struct using_direct *using, | |
53 | struct obstack *obstack); | |
54 | ||
1fcb5155 DC |
55 | static struct symbol *lookup_namespace_scope (const char *name, |
56 | const char *linkage_name, | |
57 | const struct block *block, | |
58 | const domain_enum domain, | |
59 | struct symtab **symtab, | |
60 | const char *scope, | |
61 | int scope_len); | |
62 | ||
63 | static struct symbol *lookup_symbol_file (const char *name, | |
64 | const char *linkage_name, | |
65 | const struct block *block, | |
66 | const domain_enum domain, | |
67 | struct symtab **symtab, | |
68 | int anonymous_namespace); | |
69 | ||
9219021c DC |
70 | /* Set up support for dealing with C++ namespace info in the current |
71 | symtab. */ | |
72 | ||
73 | void cp_initialize_namespace () | |
74 | { | |
75 | processing_has_namespace_info = 0; | |
76 | using_list = NULL; | |
77 | } | |
78 | ||
79 | /* Add all the using directives we've gathered to the current symtab. | |
80 | STATIC_BLOCK should be the symtab's static block; OBSTACK is used | |
81 | for allocation. */ | |
82 | ||
83 | void | |
84 | cp_finalize_namespace (struct block *static_block, | |
85 | struct obstack *obstack) | |
86 | { | |
87 | if (using_list != NULL) | |
88 | { | |
89 | block_set_using (static_block, | |
90 | cp_copy_usings (using_list, obstack), | |
91 | obstack); | |
92 | using_list = NULL; | |
93 | } | |
94 | } | |
95 | ||
96 | /* Check to see if SYMBOL refers to an object contained within an | |
97 | anonymous namespace; if so, add an appropriate using directive. */ | |
98 | ||
99 | /* Optimize away strlen ("(anonymous namespace)"). */ | |
100 | ||
101 | #define ANONYMOUS_NAMESPACE_LEN 21 | |
102 | ||
103 | void | |
104 | cp_scan_for_anonymous_namespaces (const struct symbol *symbol) | |
105 | { | |
106 | if (!processing_has_namespace_info | |
107 | && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) | |
108 | { | |
109 | const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol); | |
110 | unsigned int previous_component; | |
111 | unsigned int next_component; | |
112 | const char *len; | |
113 | ||
114 | /* Start with a quick-and-dirty check for mention of "(anonymous | |
115 | namespace)". */ | |
116 | ||
117 | if (!cp_is_anonymous (name)) | |
118 | return; | |
119 | ||
120 | previous_component = 0; | |
121 | next_component = cp_find_first_component (name + previous_component); | |
122 | ||
123 | while (name[next_component] == ':') | |
124 | { | |
125 | if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN | |
126 | && strncmp (name + previous_component, | |
127 | "(anonymous namespace)", | |
128 | ANONYMOUS_NAMESPACE_LEN) == 0) | |
129 | { | |
130 | /* We've found a component of the name that's an | |
131 | anonymous namespace. So add symbols in it to the | |
132 | namespace given by the previous component if there is | |
133 | one, or to the global namespace if there isn't. */ | |
134 | cp_add_using_directive (name, | |
135 | previous_component == 0 | |
136 | ? 0 : previous_component - 2, | |
137 | next_component); | |
138 | } | |
139 | /* The "+ 2" is for the "::". */ | |
140 | previous_component = next_component + 2; | |
141 | next_component = (previous_component | |
142 | + cp_find_first_component (name | |
143 | + previous_component)); | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | /* Add a using directive to using_list. NAME is the start of a string | |
149 | that should contain the namespaces we want to add as initial | |
150 | substrings, OUTER_LENGTH is the end of the outer namespace, and | |
151 | INNER_LENGTH is the end of the inner namespace. If the using | |
152 | directive in question has already been added, don't add it | |
153 | twice. */ | |
154 | ||
155 | void | |
156 | cp_add_using_directive (const char *name, unsigned int outer_length, | |
157 | unsigned int inner_length) | |
158 | { | |
159 | struct using_direct *current; | |
160 | struct using_direct *new; | |
161 | ||
162 | /* Has it already been added? */ | |
163 | ||
164 | for (current = using_list; current != NULL; current = current->next) | |
165 | { | |
166 | if ((strncmp (current->inner, name, inner_length) == 0) | |
167 | && (strlen (current->inner) == inner_length) | |
168 | && (strlen (current->outer) == outer_length)) | |
169 | return; | |
170 | } | |
171 | ||
172 | using_list = cp_add_using (name, inner_length, outer_length, | |
173 | using_list); | |
174 | } | |
175 | ||
176 | /* Record the namespace that the function defined by SYMBOL was | |
177 | defined in, if necessary. BLOCK is the associated block; use | |
178 | OBSTACK for allocation. */ | |
179 | ||
180 | void | |
181 | cp_set_block_scope (const struct symbol *symbol, | |
182 | struct block *block, | |
183 | struct obstack *obstack) | |
184 | { | |
185 | /* Make sure that the name was originally mangled: if not, there | |
186 | certainly isn't any namespace information to worry about! */ | |
187 | ||
188 | if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL) | |
189 | { | |
190 | if (processing_has_namespace_info) | |
191 | { | |
192 | block_set_scope | |
193 | (block, obsavestring (processing_current_namespace, | |
194 | strlen (processing_current_namespace), | |
195 | obstack), | |
196 | obstack); | |
197 | } | |
198 | else | |
199 | { | |
200 | /* Try to figure out the appropriate namespace from the | |
201 | demangled name. */ | |
202 | ||
203 | /* FIXME: carlton/2003-04-15: If the function in question is | |
204 | a method of a class, the name will actually include the | |
205 | name of the class as well. This should be harmless, but | |
206 | is a little unfortunate. */ | |
207 | ||
208 | const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol); | |
209 | unsigned int prefix_len = cp_entire_prefix_len (name); | |
210 | ||
211 | block_set_scope (block, | |
212 | obsavestring (name, prefix_len, obstack), | |
213 | obstack); | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
218 | /* Test whether or not NAMESPACE looks like it mentions an anonymous | |
219 | namespace; return nonzero if so. */ | |
220 | ||
221 | int | |
222 | cp_is_anonymous (const char *namespace) | |
223 | { | |
224 | return (strstr (namespace, "(anonymous namespace)") | |
225 | != NULL); | |
226 | } | |
227 | ||
228 | /* Create a new struct using direct whose inner namespace is the | |
229 | initial substring of NAME of leng INNER_LEN and whose outer | |
230 | namespace is the initial substring of NAME of length OUTER_LENGTH. | |
231 | Set its next member in the linked list to NEXT; allocate all memory | |
232 | using xmalloc. It copies the strings, so NAME can be a temporary | |
233 | string. */ | |
234 | ||
235 | static struct using_direct * | |
236 | cp_add_using (const char *name, | |
237 | unsigned int inner_len, | |
238 | unsigned int outer_len, | |
239 | struct using_direct *next) | |
240 | { | |
241 | struct using_direct *retval; | |
242 | ||
243 | gdb_assert (outer_len < inner_len); | |
244 | ||
245 | retval = xmalloc (sizeof (struct using_direct)); | |
246 | retval->inner = savestring (name, inner_len); | |
247 | retval->outer = savestring (name, outer_len); | |
248 | retval->next = next; | |
249 | ||
250 | return retval; | |
251 | } | |
252 | ||
253 | /* Make a copy of the using directives in the list pointed to by | |
254 | USING, using OBSTACK to allocate memory. Free all memory pointed | |
255 | to by USING via xfree. */ | |
256 | ||
257 | static struct using_direct * | |
258 | cp_copy_usings (struct using_direct *using, | |
259 | struct obstack *obstack) | |
260 | { | |
261 | if (using == NULL) | |
262 | { | |
263 | return NULL; | |
264 | } | |
265 | else | |
266 | { | |
267 | struct using_direct *retval | |
268 | = obstack_alloc (obstack, sizeof (struct using_direct)); | |
269 | retval->inner = obsavestring (using->inner, strlen (using->inner), | |
270 | obstack); | |
271 | retval->outer = obsavestring (using->outer, strlen (using->outer), | |
272 | obstack); | |
273 | retval->next = cp_copy_usings (using->next, obstack); | |
274 | ||
275 | xfree (using->inner); | |
276 | xfree (using->outer); | |
277 | xfree (using); | |
278 | ||
279 | return retval; | |
280 | } | |
281 | } | |
1fcb5155 DC |
282 | |
283 | /* The C++-specific version of name lookup for static and global | |
284 | names. This makes sure that names get looked for in all namespaces | |
285 | that are in scope. NAME is the natural name of the symbol that | |
286 | we're looking for, LINKAGE_NAME (which is optional) is its linkage | |
287 | name, BLOCK is the block that we're searching within, DOMAIN says | |
288 | what kind of symbols we're looking for, and if SYMTAB is non-NULL, | |
289 | we should store the symtab where we found the symbol in it. */ | |
290 | ||
291 | struct symbol * | |
292 | cp_lookup_symbol_nonlocal (const char *name, | |
293 | const char *linkage_name, | |
294 | const struct block *block, | |
295 | const domain_enum domain, | |
296 | struct symtab **symtab) | |
297 | { | |
298 | return lookup_namespace_scope (name, linkage_name, block, domain, | |
299 | symtab, block_scope (block), 0); | |
300 | } | |
301 | ||
302 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
303 | global variables). SCOPE is the namespace that the current | |
304 | function is defined within; only consider namespaces whose length | |
305 | is at least SCOPE_LEN. Other arguments are as in | |
306 | cp_lookup_symbol_nonlocal. | |
307 | ||
308 | For example, if we're within a function A::B::f and looking for a | |
309 | symbol f, this will get called with NAME = "f", SCOPE = "A::B", and | |
310 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, | |
311 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
312 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
313 | "A::B::x"; if it doesn't find it, then the second call looks for | |
314 | "A::x", and if that call fails, then the first call looks for | |
315 | "x". */ | |
316 | ||
317 | static struct symbol * | |
318 | lookup_namespace_scope (const char *name, | |
319 | const char *linkage_name, | |
320 | const struct block *block, | |
321 | const domain_enum domain, | |
322 | struct symtab **symtab, | |
323 | const char *scope, | |
324 | int scope_len) | |
325 | { | |
326 | char *namespace; | |
327 | ||
328 | if (scope[scope_len] != '\0') | |
329 | { | |
330 | /* Recursively search for names in child namespaces first. */ | |
331 | ||
332 | struct symbol *sym; | |
333 | int new_scope_len = scope_len; | |
334 | ||
335 | /* If the current scope is followed by "::", skip past that. */ | |
336 | if (new_scope_len != 0) | |
337 | { | |
338 | gdb_assert (scope[new_scope_len] == ':'); | |
339 | new_scope_len += 2; | |
340 | } | |
341 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
342 | sym = lookup_namespace_scope (name, linkage_name, block, | |
343 | domain, symtab, | |
344 | scope, new_scope_len); | |
345 | if (sym != NULL) | |
346 | return sym; | |
347 | } | |
348 | ||
349 | /* Okay, we didn't find a match in our children, so look for the | |
350 | name in the current namespace. */ | |
351 | ||
352 | namespace = alloca (scope_len + 1); | |
353 | strncpy (namespace, scope, scope_len); | |
354 | namespace[scope_len] = '\0'; | |
355 | return cp_lookup_symbol_namespace (namespace, name, linkage_name, | |
356 | block, domain, symtab); | |
357 | } | |
358 | ||
359 | /* Look up NAME in the C++ namespace NAMESPACE, applying the using | |
360 | directives that are active in BLOCK. Other arguments are as in | |
361 | cp_lookup_symbol_nonlocal. */ | |
362 | ||
363 | struct symbol * | |
364 | cp_lookup_symbol_namespace (const char *namespace, | |
365 | const char *name, | |
366 | const char *linkage_name, | |
367 | const struct block *block, | |
368 | const domain_enum domain, | |
369 | struct symtab **symtab) | |
370 | { | |
371 | const struct using_direct *current; | |
372 | struct symbol *sym; | |
373 | ||
374 | /* First, go through the using directives. If any of them add new | |
375 | names to the namespace we're searching in, see if we can find a | |
376 | match by applying them. */ | |
377 | ||
378 | for (current = block_using (block); | |
379 | current != NULL; | |
380 | current = current->next) | |
381 | { | |
382 | if (strcmp (namespace, current->outer) == 0) | |
383 | { | |
384 | sym = cp_lookup_symbol_namespace (current->inner, | |
385 | name, | |
386 | linkage_name, | |
387 | block, | |
388 | domain, | |
389 | symtab); | |
390 | if (sym != NULL) | |
391 | return sym; | |
392 | } | |
393 | } | |
394 | ||
395 | /* We didn't find anything by applying any of the using directives | |
396 | that are still applicable; so let's see if we've got a match | |
397 | using the current namespace. */ | |
398 | ||
399 | if (namespace[0] == '\0') | |
400 | { | |
401 | return lookup_symbol_file (name, linkage_name, block, | |
402 | domain, symtab, 0); | |
403 | } | |
404 | else | |
405 | { | |
406 | char *concatenated_name | |
407 | = alloca (strlen (namespace) + 2 + strlen (name) + 1); | |
408 | strcpy (concatenated_name, namespace); | |
409 | strcat (concatenated_name, "::"); | |
410 | strcat (concatenated_name, name); | |
411 | sym = lookup_symbol_file (concatenated_name, linkage_name, | |
412 | block, domain, symtab, | |
413 | cp_is_anonymous (namespace)); | |
414 | return sym; | |
415 | } | |
416 | } | |
417 | ||
418 | /* Look up NAME in BLOCK's static block and in global blocks. If | |
419 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
420 | within an anonymous namespace. Other arguments are as in | |
421 | cp_lookup_symbol_nonlocal. */ | |
422 | ||
423 | static struct symbol * | |
424 | lookup_symbol_file (const char *name, | |
425 | const char *linkage_name, | |
426 | const struct block *block, | |
427 | const domain_enum domain, | |
428 | struct symtab **symtab, | |
429 | int anonymous_namespace) | |
430 | { | |
431 | struct symbol *sym = NULL; | |
432 | ||
433 | sym = lookup_symbol_static (name, linkage_name, block, domain, symtab); | |
434 | if (sym != NULL) | |
435 | return sym; | |
436 | ||
437 | if (anonymous_namespace) | |
438 | { | |
439 | /* Symbols defined in anonymous namespaces have external linkage | |
440 | but should be treated as local to a single file nonetheless. | |
441 | So we only search the current file's global block. */ | |
442 | ||
443 | const struct block *global_block = block_global_block (block); | |
444 | ||
445 | if (global_block != NULL) | |
446 | return lookup_symbol_aux_block (name, linkage_name, global_block, | |
447 | domain, symtab); | |
448 | else | |
449 | return NULL; | |
450 | } | |
451 | else | |
452 | { | |
453 | return lookup_symbol_global (name, linkage_name, domain, symtab); | |
454 | } | |
455 | } |