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