| 1 | /* Support for printing Java types for GDB, the GNU debugger. |
| 2 | Copyright (C) 1997-2015 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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/>. */ |
| 18 | |
| 19 | |
| 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 "typeprint.h" |
| 28 | #include "c-lang.h" |
| 29 | #include "cp-abi.h" |
| 30 | #include "cp-support.h" |
| 31 | |
| 32 | /* Local functions */ |
| 33 | |
| 34 | static void java_type_print_base (struct type * type, |
| 35 | struct ui_file *stream, int show, |
| 36 | int level, |
| 37 | const struct type_print_options *flags); |
| 38 | |
| 39 | static void |
| 40 | java_type_print_derivation_info (struct ui_file *stream, struct type *type) |
| 41 | { |
| 42 | const char *name; |
| 43 | int i; |
| 44 | int n_bases; |
| 45 | int prev; |
| 46 | |
| 47 | n_bases = TYPE_N_BASECLASSES (type); |
| 48 | |
| 49 | for (i = 0, prev = 0; i < n_bases; i++) |
| 50 | { |
| 51 | int kind; |
| 52 | |
| 53 | kind = BASETYPE_VIA_VIRTUAL (type, i) ? 'I' : 'E'; |
| 54 | |
| 55 | fputs_filtered (kind == prev ? ", " |
| 56 | : kind == 'I' ? " implements " |
| 57 | : " extends ", |
| 58 | stream); |
| 59 | prev = kind; |
| 60 | name = type_name_no_tag (TYPE_BASECLASS (type, i)); |
| 61 | |
| 62 | fprintf_filtered (stream, "%s", name ? name : "(null)"); |
| 63 | } |
| 64 | |
| 65 | if (i > 0) |
| 66 | fputs_filtered (" ", stream); |
| 67 | } |
| 68 | |
| 69 | /* Print the name of the type (or the ultimate pointer target, |
| 70 | function value or array element), or the description of a |
| 71 | structure or union. |
| 72 | |
| 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 |
| 77 | "struct {...}". |
| 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;}". |
| 81 | |
| 82 | LEVEL is the number of spaces to indent by. |
| 83 | We increase it for some recursive calls. */ |
| 84 | |
| 85 | static void |
| 86 | java_type_print_base (struct type *type, struct ui_file *stream, int show, |
| 87 | int level, const struct type_print_options *flags) |
| 88 | { |
| 89 | int i; |
| 90 | int len; |
| 91 | char *mangled_name; |
| 92 | char *demangled_name; |
| 93 | |
| 94 | QUIT; |
| 95 | wrap_here (" "); |
| 96 | |
| 97 | if (type == NULL) |
| 98 | { |
| 99 | fputs_filtered ("<type unknown>", stream); |
| 100 | return; |
| 101 | } |
| 102 | |
| 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. */ |
| 105 | |
| 106 | if (show <= 0 |
| 107 | && TYPE_NAME (type) != NULL) |
| 108 | { |
| 109 | fputs_filtered (TYPE_NAME (type), stream); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | type = check_typedef (type); |
| 114 | |
| 115 | switch (TYPE_CODE (type)) |
| 116 | { |
| 117 | case TYPE_CODE_PTR: |
| 118 | java_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level, |
| 119 | flags); |
| 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)); |
| 126 | |
| 127 | fputs_filtered (name, stream); |
| 128 | xfree (name); |
| 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); |
| 153 | |
| 154 | fprintf_filtered (stream, "{\n"); |
| 155 | if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0)) |
| 156 | { |
| 157 | if (TYPE_STUB (type)) |
| 158 | fprintfi_filtered (level + 4, stream, "<incomplete type>\n"); |
| 159 | else |
| 160 | fprintfi_filtered (level + 4, stream, "<no data fields>\n"); |
| 161 | } |
| 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. */ |
| 171 | if (startswith (TYPE_FIELD_NAME (type, i), "_vptr") |
| 172 | && is_cplus_marker ((TYPE_FIELD_NAME (type, i))[5])) |
| 173 | continue; |
| 174 | |
| 175 | /* Don't print the dummy field "class". */ |
| 176 | if (startswith (TYPE_FIELD_NAME (type, i), "class")) |
| 177 | continue; |
| 178 | |
| 179 | print_spaces_filtered (level + 4, stream); |
| 180 | |
| 181 | if (HAVE_CPLUS_STRUCT (type)) |
| 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 | } |
| 190 | |
| 191 | if (field_is_static (&TYPE_FIELD (type, i))) |
| 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, flags); |
| 197 | |
| 198 | fprintf_filtered (stream, ";\n"); |
| 199 | } |
| 200 | |
| 201 | /* If there are both fields and methods, put a space between. */ |
| 202 | len = TYPE_NFN_FIELDS (type); |
| 203 | if (len) |
| 204 | fprintf_filtered (stream, "\n"); |
| 205 | |
| 206 | /* Print out the methods. */ |
| 207 | |
| 208 | for (i = 0; i < len; i++) |
| 209 | { |
| 210 | struct fn_field *f; |
| 211 | int j; |
| 212 | const char *method_name; |
| 213 | const 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); |
| 221 | is_constructor = name && strcmp (method_name, name) == 0; |
| 222 | |
| 223 | for (j = 0; j < n_overloads; j++) |
| 224 | { |
| 225 | const char *real_physname; |
| 226 | char *physname, *p; |
| 227 | int is_full_physname_constructor; |
| 228 | |
| 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'; |
| 239 | |
| 240 | is_full_physname_constructor |
| 241 | = (TYPE_FN_FIELD_CONSTRUCTOR (f, j) |
| 242 | || is_constructor_name (physname) |
| 243 | || is_destructor_name (physname)); |
| 244 | |
| 245 | QUIT; |
| 246 | |
| 247 | print_spaces_filtered (level + 4, stream); |
| 248 | |
| 249 | if (TYPE_FN_FIELD_PROTECTED (f, j)) |
| 250 | fprintf_filtered (stream, "protected "); |
| 251 | else if (TYPE_FN_FIELD_PRIVATE (f, j)) |
| 252 | fprintf_filtered (stream, "private "); |
| 253 | else if (TYPE_FN_FIELD_PUBLIC (f, j)) |
| 254 | fprintf_filtered (stream, "public "); |
| 255 | |
| 256 | if (TYPE_FN_FIELD_ABSTRACT (f, j)) |
| 257 | fprintf_filtered (stream, "abstract "); |
| 258 | if (TYPE_FN_FIELD_STATIC (f, j)) |
| 259 | fprintf_filtered (stream, "static "); |
| 260 | if (TYPE_FN_FIELD_FINAL (f, j)) |
| 261 | fprintf_filtered (stream, "final "); |
| 262 | if (TYPE_FN_FIELD_SYNCHRONIZED (f, j)) |
| 263 | fprintf_filtered (stream, "synchronized "); |
| 264 | if (TYPE_FN_FIELD_NATIVE (f, j)) |
| 265 | fprintf_filtered (stream, "native "); |
| 266 | |
| 267 | if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0) |
| 268 | { |
| 269 | /* Keep GDB from crashing here. */ |
| 270 | fprintf_filtered (stream, "<undefined type> %s;\n", |
| 271 | TYPE_FN_FIELD_PHYSNAME (f, j)); |
| 272 | break; |
| 273 | } |
| 274 | else if (!is_constructor && !is_full_physname_constructor) |
| 275 | { |
| 276 | type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), |
| 277 | "", stream, -1); |
| 278 | fputs_filtered (" ", stream); |
| 279 | } |
| 280 | |
| 281 | if (TYPE_FN_FIELD_STUB (f, j)) |
| 282 | /* Build something we can demangle. */ |
| 283 | mangled_name = gdb_mangle_name (type, i, j); |
| 284 | else |
| 285 | mangled_name = physname; |
| 286 | |
| 287 | demangled_name = |
| 288 | gdb_demangle (mangled_name, |
| 289 | DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA); |
| 290 | |
| 291 | if (demangled_name == NULL) |
| 292 | demangled_name = xstrdup (mangled_name); |
| 293 | |
| 294 | { |
| 295 | char *demangled_no_class; |
| 296 | char *ptr; |
| 297 | |
| 298 | ptr = demangled_no_class = demangled_name; |
| 299 | |
| 300 | while (1) |
| 301 | { |
| 302 | char c; |
| 303 | |
| 304 | c = *ptr++; |
| 305 | |
| 306 | if (c == 0 || c == '(') |
| 307 | break; |
| 308 | if (c == '.') |
| 309 | demangled_no_class = ptr; |
| 310 | } |
| 311 | |
| 312 | fputs_filtered (demangled_no_class, stream); |
| 313 | xfree (demangled_name); |
| 314 | } |
| 315 | |
| 316 | if (TYPE_FN_FIELD_STUB (f, j)) |
| 317 | xfree (mangled_name); |
| 318 | |
| 319 | fprintf_filtered (stream, ";\n"); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | fprintfi_filtered (level, stream, "}"); |
| 324 | } |
| 325 | break; |
| 326 | |
| 327 | default: |
| 328 | c_type_print_base (type, stream, show, level, flags); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* LEVEL is the depth to indent lines by. */ |
| 333 | |
| 334 | void |
| 335 | java_print_type (struct type *type, const char *varstring, |
| 336 | struct ui_file *stream, int show, int level, |
| 337 | const struct type_print_options *flags) |
| 338 | { |
| 339 | int demangled_args; |
| 340 | |
| 341 | java_type_print_base (type, stream, show, level, flags); |
| 342 | |
| 343 | if (varstring != NULL && *varstring != '\0') |
| 344 | { |
| 345 | fputs_filtered (" ", stream); |
| 346 | fputs_filtered (varstring, stream); |
| 347 | } |
| 348 | |
| 349 | /* For demangled function names, we have the arglist as part of the name, |
| 350 | so don't print an additional pair of ()'s. */ |
| 351 | |
| 352 | demangled_args = varstring != NULL && strchr (varstring, '(') != NULL; |
| 353 | c_type_print_varspec_suffix (type, stream, show, 0, demangled_args, flags); |
| 354 | } |