1 /* Support for printing Java types for GDB, the GNU debugger.
2 Copyright (C) 1997, 1998, 1999, 2000, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "gdb_string.h"
28 #include "typeprint.h"
31 #include "gdb_assert.h"
35 static void java_type_print_base (struct type * type,
36 struct ui_file *stream, int show,
40 java_type_print_derivation_info (struct ui_file *stream, struct type *type)
47 n_bases = TYPE_N_BASECLASSES (type);
49 for (i = 0, prev = 0; i < n_bases; i++)
53 kind = BASETYPE_VIA_VIRTUAL (type, i) ? 'I' : 'E';
55 fputs_filtered (kind == prev ? ", "
56 : kind == 'I' ? " implements "
60 name = type_name_no_tag (TYPE_BASECLASS (type, i));
62 fprintf_filtered (stream, "%s", name ? name : "(null)");
66 fputs_filtered (" ", stream);
69 /* Print the name of the type (or the ultimate pointer target,
70 function value or array element), or the description of a
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
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;}".
82 LEVEL is the number of spaces to indent by.
83 We increase it for some recursive calls. */
86 java_type_print_base (struct type *type, struct ui_file *stream, int show,
99 fputs_filtered ("<type unknown>", stream);
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. */
107 && TYPE_NAME (type) != NULL)
109 fputs_filtered (TYPE_NAME (type), stream);
113 CHECK_TYPEDEF (type);
115 switch (TYPE_CODE (type))
118 java_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
121 case TYPE_CODE_STRUCT:
122 if (TYPE_TAG_NAME (type) != NULL && TYPE_TAG_NAME (type)[0] == '[')
124 char *name = java_demangle_type_signature (TYPE_TAG_NAME (type));
126 fputs_filtered (name, stream);
132 fprintf_filtered (stream, "class ");
134 if (TYPE_TAG_NAME (type) != NULL)
136 fputs_filtered (TYPE_TAG_NAME (type), stream);
138 fputs_filtered (" ", stream);
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, "{...}");
149 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
151 java_type_print_derivation_info (stream, type);
153 fprintf_filtered (stream, "{\n");
154 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
156 if (TYPE_STUB (type))
157 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
159 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
162 /* If there is a base class for this type,
163 do not print the field that it occupies. */
165 len = TYPE_NFIELDS (type);
166 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
169 /* Don't print out virtual function table. */
170 if (strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5) == 0
171 && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
174 /* Don't print the dummy field "class". */
175 if (strncmp (TYPE_FIELD_NAME (type, i), "class", 5) == 0)
178 print_spaces_filtered (level + 4, stream);
180 if (HAVE_CPLUS_STRUCT (type))
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 ");
187 fprintf_filtered (stream, "public ");
190 if (field_is_static (&TYPE_FIELD (type, i)))
191 fprintf_filtered (stream, "static ");
193 java_print_type (TYPE_FIELD_TYPE (type, i),
194 TYPE_FIELD_NAME (type, i),
195 stream, show - 1, level + 4);
197 fprintf_filtered (stream, ";\n");
200 /* If there are both fields and methods, put a space between. */
201 len = TYPE_NFN_FIELDS (type);
203 fprintf_filtered (stream, "\n");
205 /* Print out the methods */
207 for (i = 0; i < len; i++)
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);
220 is_constructor = name && strcmp (method_name, name) == 0;
222 for (j = 0; j < n_overloads; j++)
224 char *real_physname, *physname, *p;
225 int is_full_physname_constructor;
227 real_physname = TYPE_FN_FIELD_PHYSNAME (f, j);
229 /* The physname will contain the return type
230 after the final closing parenthesis. Strip it off. */
231 p = strrchr (real_physname, ')');
232 gdb_assert (p != NULL);
233 ++p; /* Keep the trailing ')'. */
234 physname = alloca (p - real_physname + 1);
235 memcpy (physname, real_physname, p - real_physname);
236 physname[p - real_physname] = '\0';
238 is_full_physname_constructor
239 = (is_constructor_name (physname)
240 || is_destructor_name (physname));
244 print_spaces_filtered (level + 4, stream);
246 if (TYPE_FN_FIELD_PROTECTED (f, j))
247 fprintf_filtered (stream, "protected ");
248 else if (TYPE_FN_FIELD_PRIVATE (f, j))
249 fprintf_filtered (stream, "private ");
250 else if (TYPE_FN_FIELD_PUBLIC (f, j))
251 fprintf_filtered (stream, "public ");
253 if (TYPE_FN_FIELD_ABSTRACT (f, j))
254 fprintf_filtered (stream, "abstract ");
255 if (TYPE_FN_FIELD_STATIC (f, j))
256 fprintf_filtered (stream, "static ");
257 if (TYPE_FN_FIELD_FINAL (f, j))
258 fprintf_filtered (stream, "final ");
259 if (TYPE_FN_FIELD_SYNCHRONIZED (f, j))
260 fprintf_filtered (stream, "synchronized ");
261 if (TYPE_FN_FIELD_NATIVE (f, j))
262 fprintf_filtered (stream, "native ");
264 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
266 /* Keep GDB from crashing here. */
267 fprintf_filtered (stream, "<undefined type> %s;\n",
268 TYPE_FN_FIELD_PHYSNAME (f, j));
271 else if (!is_constructor && !is_full_physname_constructor)
273 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
275 fputs_filtered (" ", stream);
278 if (TYPE_FN_FIELD_STUB (f, j))
279 /* Build something we can demangle. */
280 mangled_name = gdb_mangle_name (type, i, j);
282 mangled_name = physname;
285 cplus_demangle (mangled_name,
286 DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
288 if (demangled_name == NULL)
289 demangled_name = xstrdup (mangled_name);
292 char *demangled_no_class;
295 ptr = demangled_no_class = demangled_name;
303 if (c == 0 || c == '(')
306 demangled_no_class = ptr;
309 fputs_filtered (demangled_no_class, stream);
310 xfree (demangled_name);
313 if (TYPE_FN_FIELD_STUB (f, j))
314 xfree (mangled_name);
316 fprintf_filtered (stream, ";\n");
320 fprintfi_filtered (level, stream, "}");
325 c_type_print_base (type, stream, show, level);
329 /* LEVEL is the depth to indent lines by. */
331 extern void c_type_print_varspec_suffix (struct type *, struct ui_file *,
335 java_print_type (struct type *type, const char *varstring,
336 struct ui_file *stream, int show, int level)
340 java_type_print_base (type, stream, show, level);
342 if (varstring != NULL && *varstring != '\0')
344 fputs_filtered (" ", stream);
345 fputs_filtered (varstring, stream);
348 /* For demangled function names, we have the arglist as part of the name,
349 so don't print an additional pair of ()'s */
351 demangled_args = varstring != NULL && strchr (varstring, '(') != NULL;
352 c_type_print_varspec_suffix (type, stream, show, 0, demangled_args);