1 /* Helper routines for C++ support in GDB.
2 Copyright 2003 Free Software Foundation, Inc.
4 Contributed by David Carlton.
6 This file is part of GDB.
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.
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.
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. */
24 #include "cp-support.h"
25 #include "gdb_obstack.h"
28 #include "gdb_assert.h"
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. */
35 unsigned char processing_has_namespace_info
;
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. */
41 const char *processing_current_namespace
;
43 /* List of using directives that are active in the current file. */
45 static struct using_direct
*using_list
;
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
);
52 static struct using_direct
*cp_copy_usings (struct using_direct
*using,
53 struct obstack
*obstack
);
55 /* Set up support for dealing with C++ namespace info in the current
58 void cp_initialize_namespace ()
60 processing_has_namespace_info
= 0;
64 /* Add all the using directives we've gathered to the current symtab.
65 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
69 cp_finalize_namespace (struct block
*static_block
,
70 struct obstack
*obstack
)
72 if (using_list
!= NULL
)
74 block_set_using (static_block
,
75 cp_copy_usings (using_list
, obstack
),
81 /* Check to see if SYMBOL refers to an object contained within an
82 anonymous namespace; if so, add an appropriate using directive. */
84 /* Optimize away strlen ("(anonymous namespace)"). */
86 #define ANONYMOUS_NAMESPACE_LEN 21
89 cp_scan_for_anonymous_namespaces (const struct symbol
*symbol
)
91 if (!processing_has_namespace_info
92 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
94 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
95 unsigned int previous_component
;
96 unsigned int next_component
;
99 /* Start with a quick-and-dirty check for mention of "(anonymous
102 if (!cp_is_anonymous (name
))
105 previous_component
= 0;
106 next_component
= cp_find_first_component (name
+ previous_component
);
108 while (name
[next_component
] == ':')
110 if ((next_component
- previous_component
) == ANONYMOUS_NAMESPACE_LEN
111 && strncmp (name
+ previous_component
,
112 "(anonymous namespace)",
113 ANONYMOUS_NAMESPACE_LEN
) == 0)
115 /* We've found a component of the name that's an
116 anonymous namespace. So add symbols in it to the
117 namespace given by the previous component if there is
118 one, or to the global namespace if there isn't. */
119 cp_add_using_directive (name
,
120 previous_component
== 0
121 ? 0 : previous_component
- 2,
124 /* The "+ 2" is for the "::". */
125 previous_component
= next_component
+ 2;
126 next_component
= (previous_component
127 + cp_find_first_component (name
128 + previous_component
));
133 /* Add a using directive to using_list. NAME is the start of a string
134 that should contain the namespaces we want to add as initial
135 substrings, OUTER_LENGTH is the end of the outer namespace, and
136 INNER_LENGTH is the end of the inner namespace. If the using
137 directive in question has already been added, don't add it
141 cp_add_using_directive (const char *name
, unsigned int outer_length
,
142 unsigned int inner_length
)
144 struct using_direct
*current
;
145 struct using_direct
*new;
147 /* Has it already been added? */
149 for (current
= using_list
; current
!= NULL
; current
= current
->next
)
151 if ((strncmp (current
->inner
, name
, inner_length
) == 0)
152 && (strlen (current
->inner
) == inner_length
)
153 && (strlen (current
->outer
) == outer_length
))
157 using_list
= cp_add_using (name
, inner_length
, outer_length
,
161 /* Record the namespace that the function defined by SYMBOL was
162 defined in, if necessary. BLOCK is the associated block; use
163 OBSTACK for allocation. */
166 cp_set_block_scope (const struct symbol
*symbol
,
168 struct obstack
*obstack
)
170 /* Make sure that the name was originally mangled: if not, there
171 certainly isn't any namespace information to worry about! */
173 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
175 if (processing_has_namespace_info
)
178 (block
, obsavestring (processing_current_namespace
,
179 strlen (processing_current_namespace
),
185 /* Try to figure out the appropriate namespace from the
188 /* FIXME: carlton/2003-04-15: If the function in question is
189 a method of a class, the name will actually include the
190 name of the class as well. This should be harmless, but
191 is a little unfortunate. */
193 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
194 unsigned int prefix_len
= cp_entire_prefix_len (name
);
196 block_set_scope (block
,
197 obsavestring (name
, prefix_len
, obstack
),
203 /* Test whether or not NAMESPACE looks like it mentions an anonymous
204 namespace; return nonzero if so. */
207 cp_is_anonymous (const char *namespace)
209 return (strstr (namespace, "(anonymous namespace)")
213 /* Create a new struct using direct whose inner namespace is the
214 initial substring of NAME of leng INNER_LEN and whose outer
215 namespace is the initial substring of NAME of length OUTER_LENGTH.
216 Set its next member in the linked list to NEXT; allocate all memory
217 using xmalloc. It copies the strings, so NAME can be a temporary
220 static struct using_direct
*
221 cp_add_using (const char *name
,
222 unsigned int inner_len
,
223 unsigned int outer_len
,
224 struct using_direct
*next
)
226 struct using_direct
*retval
;
228 gdb_assert (outer_len
< inner_len
);
230 retval
= xmalloc (sizeof (struct using_direct
));
231 retval
->inner
= savestring (name
, inner_len
);
232 retval
->outer
= savestring (name
, outer_len
);
238 /* Make a copy of the using directives in the list pointed to by
239 USING, using OBSTACK to allocate memory. Free all memory pointed
240 to by USING via xfree. */
242 static struct using_direct
*
243 cp_copy_usings (struct using_direct
*using,
244 struct obstack
*obstack
)
252 struct using_direct
*retval
253 = obstack_alloc (obstack
, sizeof (struct using_direct
));
254 retval
->inner
= obsavestring (using->inner
, strlen (using->inner
),
256 retval
->outer
= obsavestring (using->outer
, strlen (using->outer
),
258 retval
->next
= cp_copy_usings (using->next
, obstack
);
260 xfree (using->inner
);
261 xfree (using->outer
);