15796c8dcSSimon Schubert /* Support for printing Java types for GDB, the GNU debugger.
2*ef5ccd6cSJohn Marino Copyright (C) 1997-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert
45796c8dcSSimon Schubert This file is part of GDB.
55796c8dcSSimon Schubert
65796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
75796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
85796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
95796c8dcSSimon Schubert (at your option) any later version.
105796c8dcSSimon Schubert
115796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
125796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
135796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
145796c8dcSSimon Schubert GNU General Public License for more details.
155796c8dcSSimon Schubert
165796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
175796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
185796c8dcSSimon Schubert
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "symtab.h"
225796c8dcSSimon Schubert #include "gdbtypes.h"
235796c8dcSSimon Schubert #include "value.h"
245796c8dcSSimon Schubert #include "demangle.h"
25a45ae5f8SJohn Marino #include "gdb-demangle.h"
265796c8dcSSimon Schubert #include "jv-lang.h"
275796c8dcSSimon Schubert #include "gdb_string.h"
285796c8dcSSimon Schubert #include "typeprint.h"
295796c8dcSSimon Schubert #include "c-lang.h"
305796c8dcSSimon Schubert #include "cp-abi.h"
31cf7f2e2dSJohn Marino #include "gdb_assert.h"
325796c8dcSSimon Schubert
335796c8dcSSimon Schubert /* Local functions */
345796c8dcSSimon Schubert
355796c8dcSSimon Schubert static void java_type_print_base (struct type * type,
365796c8dcSSimon Schubert struct ui_file *stream, int show,
37*ef5ccd6cSJohn Marino int level,
38*ef5ccd6cSJohn Marino const struct type_print_options *flags);
395796c8dcSSimon Schubert
405796c8dcSSimon Schubert static void
java_type_print_derivation_info(struct ui_file * stream,struct type * type)415796c8dcSSimon Schubert java_type_print_derivation_info (struct ui_file *stream, struct type *type)
425796c8dcSSimon Schubert {
43*ef5ccd6cSJohn Marino const char *name;
445796c8dcSSimon Schubert int i;
455796c8dcSSimon Schubert int n_bases;
465796c8dcSSimon Schubert int prev;
475796c8dcSSimon Schubert
485796c8dcSSimon Schubert n_bases = TYPE_N_BASECLASSES (type);
495796c8dcSSimon Schubert
505796c8dcSSimon Schubert for (i = 0, prev = 0; i < n_bases; i++)
515796c8dcSSimon Schubert {
525796c8dcSSimon Schubert int kind;
535796c8dcSSimon Schubert
545796c8dcSSimon Schubert kind = BASETYPE_VIA_VIRTUAL (type, i) ? 'I' : 'E';
555796c8dcSSimon Schubert
565796c8dcSSimon Schubert fputs_filtered (kind == prev ? ", "
575796c8dcSSimon Schubert : kind == 'I' ? " implements "
585796c8dcSSimon Schubert : " extends ",
595796c8dcSSimon Schubert stream);
605796c8dcSSimon Schubert prev = kind;
615796c8dcSSimon Schubert name = type_name_no_tag (TYPE_BASECLASS (type, i));
625796c8dcSSimon Schubert
635796c8dcSSimon Schubert fprintf_filtered (stream, "%s", name ? name : "(null)");
645796c8dcSSimon Schubert }
655796c8dcSSimon Schubert
665796c8dcSSimon Schubert if (i > 0)
675796c8dcSSimon Schubert fputs_filtered (" ", stream);
685796c8dcSSimon Schubert }
695796c8dcSSimon Schubert
705796c8dcSSimon Schubert /* Print the name of the type (or the ultimate pointer target,
715796c8dcSSimon Schubert function value or array element), or the description of a
725796c8dcSSimon Schubert structure or union.
735796c8dcSSimon Schubert
745796c8dcSSimon Schubert SHOW positive means print details about the type (e.g. enum values),
755796c8dcSSimon Schubert and print structure elements passing SHOW - 1 for show.
765796c8dcSSimon Schubert SHOW negative means just print the type name or struct tag if there is one.
775796c8dcSSimon Schubert If there is no name, print something sensible but concise like
785796c8dcSSimon Schubert "struct {...}".
795796c8dcSSimon Schubert SHOW zero means just print the type name or struct tag if there is one.
805796c8dcSSimon Schubert If there is no name, print something sensible but not as concise like
815796c8dcSSimon Schubert "struct {int x; int y;}".
825796c8dcSSimon Schubert
835796c8dcSSimon Schubert LEVEL is the number of spaces to indent by.
845796c8dcSSimon Schubert We increase it for some recursive calls. */
855796c8dcSSimon Schubert
865796c8dcSSimon Schubert static void
java_type_print_base(struct type * type,struct ui_file * stream,int show,int level,const struct type_print_options * flags)875796c8dcSSimon Schubert java_type_print_base (struct type *type, struct ui_file *stream, int show,
88*ef5ccd6cSJohn Marino int level, const struct type_print_options *flags)
895796c8dcSSimon Schubert {
905796c8dcSSimon Schubert int i;
915796c8dcSSimon Schubert int len;
925796c8dcSSimon Schubert char *mangled_name;
935796c8dcSSimon Schubert char *demangled_name;
945796c8dcSSimon Schubert
95cf7f2e2dSJohn Marino QUIT;
965796c8dcSSimon Schubert wrap_here (" ");
975796c8dcSSimon Schubert
985796c8dcSSimon Schubert if (type == NULL)
995796c8dcSSimon Schubert {
1005796c8dcSSimon Schubert fputs_filtered ("<type unknown>", stream);
1015796c8dcSSimon Schubert return;
1025796c8dcSSimon Schubert }
1035796c8dcSSimon Schubert
1045796c8dcSSimon Schubert /* When SHOW is zero or less, and there is a valid type name, then always
1055796c8dcSSimon Schubert just print the type name directly from the type. */
1065796c8dcSSimon Schubert
1075796c8dcSSimon Schubert if (show <= 0
1085796c8dcSSimon Schubert && TYPE_NAME (type) != NULL)
1095796c8dcSSimon Schubert {
1105796c8dcSSimon Schubert fputs_filtered (TYPE_NAME (type), stream);
1115796c8dcSSimon Schubert return;
1125796c8dcSSimon Schubert }
1135796c8dcSSimon Schubert
1145796c8dcSSimon Schubert CHECK_TYPEDEF (type);
1155796c8dcSSimon Schubert
1165796c8dcSSimon Schubert switch (TYPE_CODE (type))
1175796c8dcSSimon Schubert {
1185796c8dcSSimon Schubert case TYPE_CODE_PTR:
119*ef5ccd6cSJohn Marino java_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level,
120*ef5ccd6cSJohn Marino flags);
1215796c8dcSSimon Schubert break;
1225796c8dcSSimon Schubert
1235796c8dcSSimon Schubert case TYPE_CODE_STRUCT:
1245796c8dcSSimon Schubert if (TYPE_TAG_NAME (type) != NULL && TYPE_TAG_NAME (type)[0] == '[')
1255796c8dcSSimon Schubert { /* array type */
1265796c8dcSSimon Schubert char *name = java_demangle_type_signature (TYPE_TAG_NAME (type));
127cf7f2e2dSJohn Marino
1285796c8dcSSimon Schubert fputs_filtered (name, stream);
1295796c8dcSSimon Schubert xfree (name);
1305796c8dcSSimon Schubert break;
1315796c8dcSSimon Schubert }
1325796c8dcSSimon Schubert
1335796c8dcSSimon Schubert if (show >= 0)
1345796c8dcSSimon Schubert fprintf_filtered (stream, "class ");
1355796c8dcSSimon Schubert
1365796c8dcSSimon Schubert if (TYPE_TAG_NAME (type) != NULL)
1375796c8dcSSimon Schubert {
1385796c8dcSSimon Schubert fputs_filtered (TYPE_TAG_NAME (type), stream);
1395796c8dcSSimon Schubert if (show > 0)
1405796c8dcSSimon Schubert fputs_filtered (" ", stream);
1415796c8dcSSimon Schubert }
1425796c8dcSSimon Schubert
1435796c8dcSSimon Schubert wrap_here (" ");
1445796c8dcSSimon Schubert
1455796c8dcSSimon Schubert if (show < 0)
1465796c8dcSSimon Schubert {
1475796c8dcSSimon Schubert /* If we just printed a tag name, no need to print anything else. */
1485796c8dcSSimon Schubert if (TYPE_TAG_NAME (type) == NULL)
1495796c8dcSSimon Schubert fprintf_filtered (stream, "{...}");
1505796c8dcSSimon Schubert }
1515796c8dcSSimon Schubert else if (show > 0 || TYPE_TAG_NAME (type) == NULL)
1525796c8dcSSimon Schubert {
1535796c8dcSSimon Schubert java_type_print_derivation_info (stream, type);
1545796c8dcSSimon Schubert
1555796c8dcSSimon Schubert fprintf_filtered (stream, "{\n");
1565796c8dcSSimon Schubert if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0))
1575796c8dcSSimon Schubert {
1585796c8dcSSimon Schubert if (TYPE_STUB (type))
1595796c8dcSSimon Schubert fprintfi_filtered (level + 4, stream, "<incomplete type>\n");
1605796c8dcSSimon Schubert else
1615796c8dcSSimon Schubert fprintfi_filtered (level + 4, stream, "<no data fields>\n");
1625796c8dcSSimon Schubert }
1635796c8dcSSimon Schubert
1645796c8dcSSimon Schubert /* If there is a base class for this type,
1655796c8dcSSimon Schubert do not print the field that it occupies. */
1665796c8dcSSimon Schubert
1675796c8dcSSimon Schubert len = TYPE_NFIELDS (type);
1685796c8dcSSimon Schubert for (i = TYPE_N_BASECLASSES (type); i < len; i++)
1695796c8dcSSimon Schubert {
1705796c8dcSSimon Schubert QUIT;
1715796c8dcSSimon Schubert /* Don't print out virtual function table. */
1725796c8dcSSimon Schubert if (strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5) == 0
1735796c8dcSSimon Schubert && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5]))
1745796c8dcSSimon Schubert continue;
1755796c8dcSSimon Schubert
1765796c8dcSSimon Schubert /* Don't print the dummy field "class". */
1775796c8dcSSimon Schubert if (strncmp (TYPE_FIELD_NAME (type, i), "class", 5) == 0)
1785796c8dcSSimon Schubert continue;
1795796c8dcSSimon Schubert
1805796c8dcSSimon Schubert print_spaces_filtered (level + 4, stream);
1815796c8dcSSimon Schubert
1825796c8dcSSimon Schubert if (HAVE_CPLUS_STRUCT (type))
1835796c8dcSSimon Schubert {
1845796c8dcSSimon Schubert if (TYPE_FIELD_PROTECTED (type, i))
1855796c8dcSSimon Schubert fprintf_filtered (stream, "protected ");
1865796c8dcSSimon Schubert else if (TYPE_FIELD_PRIVATE (type, i))
1875796c8dcSSimon Schubert fprintf_filtered (stream, "private ");
1885796c8dcSSimon Schubert else
1895796c8dcSSimon Schubert fprintf_filtered (stream, "public ");
1905796c8dcSSimon Schubert }
1915796c8dcSSimon Schubert
1925796c8dcSSimon Schubert if (field_is_static (&TYPE_FIELD (type, i)))
1935796c8dcSSimon Schubert fprintf_filtered (stream, "static ");
1945796c8dcSSimon Schubert
1955796c8dcSSimon Schubert java_print_type (TYPE_FIELD_TYPE (type, i),
1965796c8dcSSimon Schubert TYPE_FIELD_NAME (type, i),
197*ef5ccd6cSJohn Marino stream, show - 1, level + 4, flags);
1985796c8dcSSimon Schubert
1995796c8dcSSimon Schubert fprintf_filtered (stream, ";\n");
2005796c8dcSSimon Schubert }
2015796c8dcSSimon Schubert
2025796c8dcSSimon Schubert /* If there are both fields and methods, put a space between. */
2035796c8dcSSimon Schubert len = TYPE_NFN_FIELDS (type);
2045796c8dcSSimon Schubert if (len)
2055796c8dcSSimon Schubert fprintf_filtered (stream, "\n");
2065796c8dcSSimon Schubert
207c50c785cSJohn Marino /* Print out the methods. */
2085796c8dcSSimon Schubert
2095796c8dcSSimon Schubert for (i = 0; i < len; i++)
2105796c8dcSSimon Schubert {
2115796c8dcSSimon Schubert struct fn_field *f;
2125796c8dcSSimon Schubert int j;
213*ef5ccd6cSJohn Marino const char *method_name;
214*ef5ccd6cSJohn Marino const char *name;
2155796c8dcSSimon Schubert int is_constructor;
2165796c8dcSSimon Schubert int n_overloads;
2175796c8dcSSimon Schubert
2185796c8dcSSimon Schubert f = TYPE_FN_FIELDLIST1 (type, i);
2195796c8dcSSimon Schubert n_overloads = TYPE_FN_FIELDLIST_LENGTH (type, i);
2205796c8dcSSimon Schubert method_name = TYPE_FN_FIELDLIST_NAME (type, i);
2215796c8dcSSimon Schubert name = type_name_no_tag (type);
2225796c8dcSSimon Schubert is_constructor = name && strcmp (method_name, name) == 0;
2235796c8dcSSimon Schubert
2245796c8dcSSimon Schubert for (j = 0; j < n_overloads; j++)
2255796c8dcSSimon Schubert {
226a45ae5f8SJohn Marino const char *real_physname;
227a45ae5f8SJohn Marino char *physname, *p;
2285796c8dcSSimon Schubert int is_full_physname_constructor;
2295796c8dcSSimon Schubert
230cf7f2e2dSJohn Marino real_physname = TYPE_FN_FIELD_PHYSNAME (f, j);
231cf7f2e2dSJohn Marino
232cf7f2e2dSJohn Marino /* The physname will contain the return type
233cf7f2e2dSJohn Marino after the final closing parenthesis. Strip it off. */
234cf7f2e2dSJohn Marino p = strrchr (real_physname, ')');
235cf7f2e2dSJohn Marino gdb_assert (p != NULL);
236cf7f2e2dSJohn Marino ++p; /* Keep the trailing ')'. */
237cf7f2e2dSJohn Marino physname = alloca (p - real_physname + 1);
238cf7f2e2dSJohn Marino memcpy (physname, real_physname, p - real_physname);
239cf7f2e2dSJohn Marino physname[p - real_physname] = '\0';
2405796c8dcSSimon Schubert
2415796c8dcSSimon Schubert is_full_physname_constructor
242*ef5ccd6cSJohn Marino = (TYPE_FN_FIELD_CONSTRUCTOR (f, j)
243*ef5ccd6cSJohn Marino || is_constructor_name (physname)
2445796c8dcSSimon Schubert || is_destructor_name (physname));
2455796c8dcSSimon Schubert
2465796c8dcSSimon Schubert QUIT;
2475796c8dcSSimon Schubert
2485796c8dcSSimon Schubert print_spaces_filtered (level + 4, stream);
2495796c8dcSSimon Schubert
2505796c8dcSSimon Schubert if (TYPE_FN_FIELD_PROTECTED (f, j))
2515796c8dcSSimon Schubert fprintf_filtered (stream, "protected ");
2525796c8dcSSimon Schubert else if (TYPE_FN_FIELD_PRIVATE (f, j))
2535796c8dcSSimon Schubert fprintf_filtered (stream, "private ");
2545796c8dcSSimon Schubert else if (TYPE_FN_FIELD_PUBLIC (f, j))
2555796c8dcSSimon Schubert fprintf_filtered (stream, "public ");
2565796c8dcSSimon Schubert
2575796c8dcSSimon Schubert if (TYPE_FN_FIELD_ABSTRACT (f, j))
2585796c8dcSSimon Schubert fprintf_filtered (stream, "abstract ");
2595796c8dcSSimon Schubert if (TYPE_FN_FIELD_STATIC (f, j))
2605796c8dcSSimon Schubert fprintf_filtered (stream, "static ");
2615796c8dcSSimon Schubert if (TYPE_FN_FIELD_FINAL (f, j))
2625796c8dcSSimon Schubert fprintf_filtered (stream, "final ");
2635796c8dcSSimon Schubert if (TYPE_FN_FIELD_SYNCHRONIZED (f, j))
2645796c8dcSSimon Schubert fprintf_filtered (stream, "synchronized ");
2655796c8dcSSimon Schubert if (TYPE_FN_FIELD_NATIVE (f, j))
2665796c8dcSSimon Schubert fprintf_filtered (stream, "native ");
2675796c8dcSSimon Schubert
2685796c8dcSSimon Schubert if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
2695796c8dcSSimon Schubert {
2705796c8dcSSimon Schubert /* Keep GDB from crashing here. */
2715796c8dcSSimon Schubert fprintf_filtered (stream, "<undefined type> %s;\n",
2725796c8dcSSimon Schubert TYPE_FN_FIELD_PHYSNAME (f, j));
2735796c8dcSSimon Schubert break;
2745796c8dcSSimon Schubert }
2755796c8dcSSimon Schubert else if (!is_constructor && !is_full_physname_constructor)
2765796c8dcSSimon Schubert {
2775796c8dcSSimon Schubert type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
2785796c8dcSSimon Schubert "", stream, -1);
2795796c8dcSSimon Schubert fputs_filtered (" ", stream);
2805796c8dcSSimon Schubert }
2815796c8dcSSimon Schubert
2825796c8dcSSimon Schubert if (TYPE_FN_FIELD_STUB (f, j))
2835796c8dcSSimon Schubert /* Build something we can demangle. */
2845796c8dcSSimon Schubert mangled_name = gdb_mangle_name (type, i, j);
2855796c8dcSSimon Schubert else
286cf7f2e2dSJohn Marino mangled_name = physname;
2875796c8dcSSimon Schubert
2885796c8dcSSimon Schubert demangled_name =
2895796c8dcSSimon Schubert cplus_demangle (mangled_name,
2905796c8dcSSimon Schubert DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
2915796c8dcSSimon Schubert
2925796c8dcSSimon Schubert if (demangled_name == NULL)
2935796c8dcSSimon Schubert demangled_name = xstrdup (mangled_name);
2945796c8dcSSimon Schubert
2955796c8dcSSimon Schubert {
2965796c8dcSSimon Schubert char *demangled_no_class;
2975796c8dcSSimon Schubert char *ptr;
2985796c8dcSSimon Schubert
2995796c8dcSSimon Schubert ptr = demangled_no_class = demangled_name;
3005796c8dcSSimon Schubert
3015796c8dcSSimon Schubert while (1)
3025796c8dcSSimon Schubert {
3035796c8dcSSimon Schubert char c;
3045796c8dcSSimon Schubert
3055796c8dcSSimon Schubert c = *ptr++;
3065796c8dcSSimon Schubert
3075796c8dcSSimon Schubert if (c == 0 || c == '(')
3085796c8dcSSimon Schubert break;
3095796c8dcSSimon Schubert if (c == '.')
3105796c8dcSSimon Schubert demangled_no_class = ptr;
3115796c8dcSSimon Schubert }
3125796c8dcSSimon Schubert
3135796c8dcSSimon Schubert fputs_filtered (demangled_no_class, stream);
3145796c8dcSSimon Schubert xfree (demangled_name);
3155796c8dcSSimon Schubert }
3165796c8dcSSimon Schubert
3175796c8dcSSimon Schubert if (TYPE_FN_FIELD_STUB (f, j))
3185796c8dcSSimon Schubert xfree (mangled_name);
3195796c8dcSSimon Schubert
3205796c8dcSSimon Schubert fprintf_filtered (stream, ";\n");
3215796c8dcSSimon Schubert }
3225796c8dcSSimon Schubert }
3235796c8dcSSimon Schubert
3245796c8dcSSimon Schubert fprintfi_filtered (level, stream, "}");
3255796c8dcSSimon Schubert }
3265796c8dcSSimon Schubert break;
3275796c8dcSSimon Schubert
3285796c8dcSSimon Schubert default:
329*ef5ccd6cSJohn Marino c_type_print_base (type, stream, show, level, flags);
3305796c8dcSSimon Schubert }
3315796c8dcSSimon Schubert }
3325796c8dcSSimon Schubert
3335796c8dcSSimon Schubert /* LEVEL is the depth to indent lines by. */
3345796c8dcSSimon Schubert
3355796c8dcSSimon Schubert void
java_print_type(struct type * type,const char * varstring,struct ui_file * stream,int show,int level,const struct type_print_options * flags)336cf7f2e2dSJohn Marino java_print_type (struct type *type, const char *varstring,
337*ef5ccd6cSJohn Marino struct ui_file *stream, int show, int level,
338*ef5ccd6cSJohn Marino const struct type_print_options *flags)
3395796c8dcSSimon Schubert {
3405796c8dcSSimon Schubert int demangled_args;
3415796c8dcSSimon Schubert
342*ef5ccd6cSJohn Marino java_type_print_base (type, stream, show, level, flags);
3435796c8dcSSimon Schubert
3445796c8dcSSimon Schubert if (varstring != NULL && *varstring != '\0')
3455796c8dcSSimon Schubert {
3465796c8dcSSimon Schubert fputs_filtered (" ", stream);
3475796c8dcSSimon Schubert fputs_filtered (varstring, stream);
3485796c8dcSSimon Schubert }
3495796c8dcSSimon Schubert
3505796c8dcSSimon Schubert /* For demangled function names, we have the arglist as part of the name,
351c50c785cSJohn Marino so don't print an additional pair of ()'s. */
3525796c8dcSSimon Schubert
3535796c8dcSSimon Schubert demangled_args = varstring != NULL && strchr (varstring, '(') != NULL;
354*ef5ccd6cSJohn Marino c_type_print_varspec_suffix (type, stream, show, 0, demangled_args, flags);
3555796c8dcSSimon Schubert }
356