]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing Java types for GDB, the GNU debugger. |
0b302171 | 2 | Copyright (C) 1997-2000, 2007-2012 Free Software Foundation, Inc. |
c906108c | 3 | |
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 9 | (at your option) any later version. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
c906108c | 15 | |
c5aa993b | 16 | You should have received a copy of the GNU General Public License |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
18 | |
19 | ||
20 | #include "defs.h" | |
21 | #include "symtab.h" | |
22 | #include "gdbtypes.h" | |
23 | #include "value.h" | |
24 | #include "demangle.h" | |
50f182aa | 25 | #include "gdb-demangle.h" |
c906108c SS |
26 | #include "jv-lang.h" |
27 | #include "gdb_string.h" | |
28 | #include "typeprint.h" | |
7a292a7a | 29 | #include "c-lang.h" |
015a42b4 | 30 | #include "cp-abi.h" |
86ffb506 | 31 | #include "gdb_assert.h" |
c906108c | 32 | |
392a587b JM |
33 | /* Local functions */ |
34 | ||
d9fcf2fb JM |
35 | static void java_type_print_base (struct type * type, |
36 | struct ui_file *stream, int show, | |
37 | int level); | |
392a587b | 38 | |
c906108c | 39 | static void |
fba45db2 | 40 | java_type_print_derivation_info (struct ui_file *stream, struct type *type) |
c906108c | 41 | { |
0d5cff50 | 42 | const char *name; |
c906108c SS |
43 | int i; |
44 | int n_bases; | |
45 | int prev; | |
46 | ||
47 | n_bases = TYPE_N_BASECLASSES (type); | |
48 | ||
49 | for (i = 0, prev = 0; i < n_bases; i++) | |
50 | { | |
51 | int kind; | |
52 | ||
c5aa993b | 53 | kind = BASETYPE_VIA_VIRTUAL (type, i) ? 'I' : 'E'; |
c906108c SS |
54 | |
55 | fputs_filtered (kind == prev ? ", " | |
56 | : kind == 'I' ? " implements " | |
57 | : " extends ", | |
58 | stream); | |
59 | prev = kind; | |
60 | name = type_name_no_tag (TYPE_BASECLASS (type, i)); | |
61 | ||
62 | fprintf_filtered (stream, "%s", name ? name : "(null)"); | |
63 | } | |
64 | ||
65 | if (i > 0) | |
66 | fputs_filtered (" ", stream); | |
67 | } | |
68 | ||
69 | /* Print the name of the type (or the ultimate pointer target, | |
70 | function value or array element), or the description of a | |
71 | structure or union. | |
72 | ||
73 | SHOW positive means print details about the type (e.g. enum values), | |
74 | and print structure elements passing SHOW - 1 for show. | |
75 | SHOW negative means just print the type name or struct tag if there is one. | |
76 | If there is no name, print something sensible but concise like | |
77 | "struct {...}". | |
78 | SHOW zero means just print the type name or struct tag if there is one. | |
79 | If there is no name, print something sensible but not as concise like | |
80 | "struct {int x; int y;}". | |
81 | ||
82 | LEVEL is the number of spaces to indent by. | |
83 | We increase it for some recursive calls. */ | |
84 | ||
392a587b | 85 | static void |
fba45db2 KB |
86 | java_type_print_base (struct type *type, struct ui_file *stream, int show, |
87 | int level) | |
c906108c | 88 | { |
52f0bd74 AC |
89 | int i; |
90 | int len; | |
c906108c SS |
91 | char *mangled_name; |
92 | char *demangled_name; | |
c906108c | 93 | |
e0881a8e | 94 | QUIT; |
c906108c SS |
95 | wrap_here (" "); |
96 | ||
97 | if (type == NULL) | |
98 | { | |
99 | fputs_filtered ("<type unknown>", stream); | |
100 | return; | |
101 | } | |
102 | ||
103 | /* When SHOW is zero or less, and there is a valid type name, then always | |
104 | just print the type name directly from the type. */ | |
105 | ||
106 | if (show <= 0 | |
107 | && TYPE_NAME (type) != NULL) | |
108 | { | |
109 | fputs_filtered (TYPE_NAME (type), stream); | |
110 | return; | |
111 | } | |
112 | ||
113 | CHECK_TYPEDEF (type); | |
114 | ||
115 | switch (TYPE_CODE (type)) | |
116 | { | |
117 | case TYPE_CODE_PTR: | |
118 | java_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level); | |
119 | break; | |
120 | ||
121 | case TYPE_CODE_STRUCT: | |
122 | if (TYPE_TAG_NAME (type) != NULL && TYPE_TAG_NAME (type)[0] == '[') | |
123 | { /* array type */ | |
124 | char *name = java_demangle_type_signature (TYPE_TAG_NAME (type)); | |
e0881a8e | 125 | |
c906108c | 126 | fputs_filtered (name, stream); |
b8c9b27d | 127 | xfree (name); |
c906108c SS |
128 | break; |
129 | } | |
130 | ||
131 | if (show >= 0) | |
132 | fprintf_filtered (stream, "class "); | |
133 | ||
134 | if (TYPE_TAG_NAME (type) != NULL) | |
135 | { | |
136 | fputs_filtered (TYPE_TAG_NAME (type), stream); | |
137 | if (show > 0) | |
138 | fputs_filtered (" ", stream); | |
139 | } | |
140 | ||
141 | wrap_here (" "); | |
142 | ||
143 | if (show < 0) | |
144 | { | |
145 | /* If we just printed a tag name, no need to print anything else. */ | |
146 | if (TYPE_TAG_NAME (type) == NULL) | |
147 | fprintf_filtered (stream, "{...}"); | |
148 | } | |
149 | else if (show > 0 || TYPE_TAG_NAME (type) == NULL) | |
150 | { | |
151 | java_type_print_derivation_info (stream, type); | |
c5aa993b | 152 | |
c906108c SS |
153 | fprintf_filtered (stream, "{\n"); |
154 | if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0)) | |
7a292a7a | 155 | { |
74a9bb82 | 156 | if (TYPE_STUB (type)) |
7a292a7a SS |
157 | fprintfi_filtered (level + 4, stream, "<incomplete type>\n"); |
158 | else | |
159 | fprintfi_filtered (level + 4, stream, "<no data fields>\n"); | |
160 | } | |
c906108c SS |
161 | |
162 | /* If there is a base class for this type, | |
163 | do not print the field that it occupies. */ | |
164 | ||
165 | len = TYPE_NFIELDS (type); | |
166 | for (i = TYPE_N_BASECLASSES (type); i < len; i++) | |
167 | { | |
168 | QUIT; | |
169 | /* Don't print out virtual function table. */ | |
5cb316ef | 170 | if (strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5) == 0 |
c906108c SS |
171 | && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5])) |
172 | continue; | |
173 | ||
1777feb0 | 174 | /* Don't print the dummy field "class". */ |
5cb316ef | 175 | if (strncmp (TYPE_FIELD_NAME (type, i), "class", 5) == 0) |
c906108c SS |
176 | continue; |
177 | ||
178 | print_spaces_filtered (level + 4, stream); | |
179 | ||
180 | if (HAVE_CPLUS_STRUCT (type)) | |
7a292a7a SS |
181 | { |
182 | if (TYPE_FIELD_PROTECTED (type, i)) | |
183 | fprintf_filtered (stream, "protected "); | |
184 | else if (TYPE_FIELD_PRIVATE (type, i)) | |
185 | fprintf_filtered (stream, "private "); | |
186 | else | |
187 | fprintf_filtered (stream, "public "); | |
188 | } | |
c906108c | 189 | |
d6a843b5 | 190 | if (field_is_static (&TYPE_FIELD (type, i))) |
c906108c SS |
191 | fprintf_filtered (stream, "static "); |
192 | ||
193 | java_print_type (TYPE_FIELD_TYPE (type, i), | |
194 | TYPE_FIELD_NAME (type, i), | |
195 | stream, show - 1, level + 4); | |
196 | ||
197 | fprintf_filtered (stream, ";\n"); | |
198 | } | |
199 | ||
1777feb0 | 200 | /* If there are both fields and methods, put a space between. */ |
c906108c SS |
201 | len = TYPE_NFN_FIELDS (type); |
202 | if (len) | |
c5aa993b | 203 | fprintf_filtered (stream, "\n"); |
c906108c | 204 | |
1777feb0 | 205 | /* Print out the methods. */ |
c906108c SS |
206 | |
207 | for (i = 0; i < len; i++) | |
208 | { | |
209 | struct fn_field *f; | |
210 | int j; | |
0d5cff50 DE |
211 | const char *method_name; |
212 | const char *name; | |
c906108c SS |
213 | int is_constructor; |
214 | int n_overloads; | |
215 | ||
216 | f = TYPE_FN_FIELDLIST1 (type, i); | |
217 | n_overloads = TYPE_FN_FIELDLIST_LENGTH (type, i); | |
218 | method_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
219 | name = type_name_no_tag (type); | |
762f08a3 | 220 | is_constructor = name && strcmp (method_name, name) == 0; |
c906108c SS |
221 | |
222 | for (j = 0; j < n_overloads; j++) | |
223 | { | |
1d06ead6 TT |
224 | const char *real_physname; |
225 | char *physname, *p; | |
c906108c SS |
226 | int is_full_physname_constructor; |
227 | ||
86ffb506 KS |
228 | real_physname = TYPE_FN_FIELD_PHYSNAME (f, j); |
229 | ||
230 | /* The physname will contain the return type | |
231 | after the final closing parenthesis. Strip it off. */ | |
232 | p = strrchr (real_physname, ')'); | |
233 | gdb_assert (p != NULL); | |
234 | ++p; /* Keep the trailing ')'. */ | |
235 | physname = alloca (p - real_physname + 1); | |
236 | memcpy (physname, real_physname, p - real_physname); | |
237 | physname[p - real_physname] = '\0'; | |
c906108c | 238 | |
015a42b4 JB |
239 | is_full_physname_constructor |
240 | = (is_constructor_name (physname) | |
241 | || is_destructor_name (physname)); | |
c906108c SS |
242 | |
243 | QUIT; | |
244 | ||
245 | print_spaces_filtered (level + 4, stream); | |
246 | ||
247 | if (TYPE_FN_FIELD_PROTECTED (f, j)) | |
248 | fprintf_filtered (stream, "protected "); | |
249 | else if (TYPE_FN_FIELD_PRIVATE (f, j)) | |
250 | fprintf_filtered (stream, "private "); | |
251 | else if (TYPE_FN_FIELD_PUBLIC (f, j)) | |
252 | fprintf_filtered (stream, "public "); | |
253 | ||
254 | if (TYPE_FN_FIELD_ABSTRACT (f, j)) | |
255 | fprintf_filtered (stream, "abstract "); | |
256 | if (TYPE_FN_FIELD_STATIC (f, j)) | |
257 | fprintf_filtered (stream, "static "); | |
258 | if (TYPE_FN_FIELD_FINAL (f, j)) | |
259 | fprintf_filtered (stream, "final "); | |
260 | if (TYPE_FN_FIELD_SYNCHRONIZED (f, j)) | |
261 | fprintf_filtered (stream, "synchronized "); | |
262 | if (TYPE_FN_FIELD_NATIVE (f, j)) | |
263 | fprintf_filtered (stream, "native "); | |
264 | ||
265 | if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0) | |
266 | { | |
267 | /* Keep GDB from crashing here. */ | |
268 | fprintf_filtered (stream, "<undefined type> %s;\n", | |
c5aa993b | 269 | TYPE_FN_FIELD_PHYSNAME (f, j)); |
c906108c SS |
270 | break; |
271 | } | |
272 | else if (!is_constructor && !is_full_physname_constructor) | |
273 | { | |
274 | type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), | |
275 | "", stream, -1); | |
276 | fputs_filtered (" ", stream); | |
277 | } | |
278 | ||
279 | if (TYPE_FN_FIELD_STUB (f, j)) | |
280 | /* Build something we can demangle. */ | |
281 | mangled_name = gdb_mangle_name (type, i, j); | |
282 | else | |
86ffb506 | 283 | mangled_name = physname; |
c906108c SS |
284 | |
285 | demangled_name = | |
286 | cplus_demangle (mangled_name, | |
287 | DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA); | |
288 | ||
289 | if (demangled_name == NULL) | |
c2d11a7d | 290 | demangled_name = xstrdup (mangled_name); |
c906108c SS |
291 | |
292 | { | |
293 | char *demangled_no_class; | |
294 | char *ptr; | |
295 | ||
296 | ptr = demangled_no_class = demangled_name; | |
297 | ||
298 | while (1) | |
299 | { | |
300 | char c; | |
301 | ||
302 | c = *ptr++; | |
303 | ||
304 | if (c == 0 || c == '(') | |
305 | break; | |
306 | if (c == '.') | |
307 | demangled_no_class = ptr; | |
308 | } | |
309 | ||
310 | fputs_filtered (demangled_no_class, stream); | |
b8c9b27d | 311 | xfree (demangled_name); |
c906108c SS |
312 | } |
313 | ||
314 | if (TYPE_FN_FIELD_STUB (f, j)) | |
b8c9b27d | 315 | xfree (mangled_name); |
c906108c SS |
316 | |
317 | fprintf_filtered (stream, ";\n"); | |
318 | } | |
319 | } | |
320 | ||
321 | fprintfi_filtered (level, stream, "}"); | |
322 | } | |
323 | break; | |
324 | ||
c5aa993b JM |
325 | default: |
326 | c_type_print_base (type, stream, show, level); | |
c906108c SS |
327 | } |
328 | } | |
329 | ||
330 | /* LEVEL is the depth to indent lines by. */ | |
331 | ||
d9fcf2fb JM |
332 | extern void c_type_print_varspec_suffix (struct type *, struct ui_file *, |
333 | int, int, int); | |
7a292a7a | 334 | |
c906108c | 335 | void |
25b524e8 JK |
336 | java_print_type (struct type *type, const char *varstring, |
337 | struct ui_file *stream, int show, int level) | |
c906108c SS |
338 | { |
339 | int demangled_args; | |
340 | ||
341 | java_type_print_base (type, stream, show, level); | |
342 | ||
343 | if (varstring != NULL && *varstring != '\0') | |
344 | { | |
345 | fputs_filtered (" ", stream); | |
346 | fputs_filtered (varstring, stream); | |
347 | } | |
348 | ||
349 | /* For demangled function names, we have the arglist as part of the name, | |
1777feb0 | 350 | so don't print an additional pair of ()'s. */ |
c906108c | 351 | |
dc5000e7 | 352 | demangled_args = varstring != NULL && strchr (varstring, '(') != NULL; |
c906108c SS |
353 | c_type_print_varspec_suffix (type, stream, show, 0, demangled_args); |
354 | } |