Fix dw2-ifort-parameter.exp on PPC64
[binutils-gdb.git] / gdb / jv-typeprint.c
blobdf46debece361c71261da2eec00c4cc5330f76cb
1 /* Support for printing Java types for GDB, the GNU debugger.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
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
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "value.h"
24 #include "demangle.h"
25 #include "gdb-demangle.h"
26 #include "jv-lang.h"
27 #include <string.h>
28 #include "typeprint.h"
29 #include "c-lang.h"
30 #include "cp-abi.h"
31 #include "cp-support.h"
32 #include "gdb_assert.h"
34 /* Local functions */
36 static void java_type_print_base (struct type * type,
37 struct ui_file *stream, int show,
38 int level,
39 const struct type_print_options *flags);
41 static void
42 java_type_print_derivation_info (struct ui_file *stream, struct type *type)
44 const char *name;
45 int i;
46 int n_bases;
47 int prev;
49 n_bases = TYPE_N_BASECLASSES (type);
51 for (i = 0, prev = 0; i < n_bases; i++)
53 int kind;
55 kind = BASETYPE_VIA_VIRTUAL (type, i) ? 'I' : 'E';
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));
64 fprintf_filtered (stream, "%s", name ? name : "(null)");
67 if (i > 0)
68 fputs_filtered (" ", stream);
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.
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;}".
84 LEVEL is the number of spaces to indent by.
85 We increase it for some recursive calls. */
87 static void
88 java_type_print_base (struct type *type, struct ui_file *stream, int show,
89 int level, const struct type_print_options *flags)
91 int i;
92 int len;
93 char *mangled_name;
94 char *demangled_name;
96 QUIT;
97 wrap_here (" ");
99 if (type == NULL)
101 fputs_filtered ("<type unknown>", stream);
102 return;
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. */
108 if (show <= 0
109 && TYPE_NAME (type) != NULL)
111 fputs_filtered (TYPE_NAME (type), stream);
112 return;
115 CHECK_TYPEDEF (type);
117 switch (TYPE_CODE (type))
119 case TYPE_CODE_PTR:
120 java_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level,
121 flags);
122 break;
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));
129 fputs_filtered (name, stream);
130 xfree (name);
131 break;
134 if (show >= 0)
135 fprintf_filtered (stream, "class ");
137 if (TYPE_TAG_NAME (type) != NULL)
139 fputs_filtered (TYPE_TAG_NAME (type), stream);
140 if (show > 0)
141 fputs_filtered (" ", stream);
144 wrap_here (" ");
146 if (show < 0)
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, "{...}");
152 else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
154 java_type_print_derivation_info (stream, type);
156 fprintf_filtered (stream, "{\n");
157 if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
159 if (TYPE_STUB (type))
160 fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
161 else
162 fprintfi_filtered (level + 4, stream, "<no data fields>\n");
165 /* If there is a base class for this type,
166 do not print the field that it occupies. */
168 len = TYPE_NFIELDS (type);
169 for (i = TYPE_N_BASECLASSES (type); i < len; i++)
171 QUIT;
172 /* Don't print out virtual function table. */
173 if (strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5) == 0
174 && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
175 continue;
177 /* Don't print the dummy field "class". */
178 if (strncmp (TYPE_FIELD_NAME (type, i), "class", 5) == 0)
179 continue;
181 print_spaces_filtered (level + 4, stream);
183 if (HAVE_CPLUS_STRUCT (type))
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 ");
193 if (field_is_static (&TYPE_FIELD (type, i)))
194 fprintf_filtered (stream, "static ");
196 java_print_type (TYPE_FIELD_TYPE (type, i),
197 TYPE_FIELD_NAME (type, i),
198 stream, show - 1, level + 4, flags);
200 fprintf_filtered (stream, ";\n");
203 /* If there are both fields and methods, put a space between. */
204 len = TYPE_NFN_FIELDS (type);
205 if (len)
206 fprintf_filtered (stream, "\n");
208 /* Print out the methods. */
210 for (i = 0; i < len; i++)
212 struct fn_field *f;
213 int j;
214 const char *method_name;
215 const char *name;
216 int is_constructor;
217 int n_overloads;
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);
223 is_constructor = name && strcmp (method_name, name) == 0;
225 for (j = 0; j < n_overloads; j++)
227 const char *real_physname;
228 char *physname, *p;
229 int is_full_physname_constructor;
231 real_physname = TYPE_FN_FIELD_PHYSNAME (f, j);
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';
242 is_full_physname_constructor
243 = (TYPE_FN_FIELD_CONSTRUCTOR (f, j)
244 || is_constructor_name (physname)
245 || is_destructor_name (physname));
247 QUIT;
249 print_spaces_filtered (level + 4, stream);
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 ");
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 ");
269 if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
271 /* Keep GDB from crashing here. */
272 fprintf_filtered (stream, "<undefined type> %s;\n",
273 TYPE_FN_FIELD_PHYSNAME (f, j));
274 break;
276 else if (!is_constructor && !is_full_physname_constructor)
278 type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
279 "", stream, -1);
280 fputs_filtered (" ", stream);
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
287 mangled_name = physname;
289 demangled_name =
290 gdb_demangle (mangled_name,
291 DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
293 if (demangled_name == NULL)
294 demangled_name = xstrdup (mangled_name);
297 char *demangled_no_class;
298 char *ptr;
300 ptr = demangled_no_class = demangled_name;
302 while (1)
304 char c;
306 c = *ptr++;
308 if (c == 0 || c == '(')
309 break;
310 if (c == '.')
311 demangled_no_class = ptr;
314 fputs_filtered (demangled_no_class, stream);
315 xfree (demangled_name);
318 if (TYPE_FN_FIELD_STUB (f, j))
319 xfree (mangled_name);
321 fprintf_filtered (stream, ";\n");
325 fprintfi_filtered (level, stream, "}");
327 break;
329 default:
330 c_type_print_base (type, stream, show, level, flags);
334 /* LEVEL is the depth to indent lines by. */
336 void
337 java_print_type (struct type *type, const char *varstring,
338 struct ui_file *stream, int show, int level,
339 const struct type_print_options *flags)
341 int demangled_args;
343 java_type_print_base (type, stream, show, level, flags);
345 if (varstring != NULL && *varstring != '\0')
347 fputs_filtered (" ", stream);
348 fputs_filtered (varstring, stream);
351 /* For demangled function names, we have the arglist as part of the name,
352 so don't print an additional pair of ()'s. */
354 demangled_args = varstring != NULL && strchr (varstring, '(') != NULL;
355 c_type_print_varspec_suffix (type, stream, show, 0, demangled_args, flags);