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