| 1 | /* Support for printing Java types for GDB, the GNU debugger. |
| 2 | Copyright (C) 1997-2014 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 <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" |
| 33 | |
| 34 | /* Local functions */ |
| 35 | |
| 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); |
| 40 | |
| 41 | static void |
| 42 | java_type_print_derivation_info (struct ui_file *stream, struct type *type) |
| 43 | { |
| 44 | const char *name; |
| 45 | int i; |
| 46 | int n_bases; |
| 47 | int prev; |
| 48 | |
| 49 | n_bases = TYPE_N_BASECLASSES (type); |
| 50 | |
| 51 | for (i = 0, prev = 0; i < n_bases; i++) |
| 52 | { |
| 53 | int kind; |
| 54 | |
| 55 | kind = BASETYPE_VIA_VIRTUAL (type, i) ? 'I' : 'E'; |
| 56 | |
| 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)); |
| 63 | |
| 64 | fprintf_filtered (stream, "%s", name ? name : "(null)"); |
| 65 | } |
| 66 | |
| 67 | if (i > 0) |
| 68 | fputs_filtered (" ", stream); |
| 69 | } |
| 70 | |
| 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. |
| 74 | |
| 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;}". |
| 83 | |
| 84 | LEVEL is the number of spaces to indent by. |
| 85 | We increase it for some recursive calls. */ |
| 86 | |
| 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) |
| 90 | { |
| 91 | int i; |
| 92 | int len; |
| 93 | char *mangled_name; |
| 94 | char *demangled_name; |
| 95 | |
| 96 | QUIT; |
| 97 | wrap_here (" "); |
| 98 | |
| 99 | if (type == NULL) |
| 100 | { |
| 101 | fputs_filtered ("<type unknown>", stream); |
| 102 | return; |
| 103 | } |
| 104 | |
| 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. */ |
| 107 | |
| 108 | if (show <= 0 |
| 109 | && TYPE_NAME (type) != NULL) |
| 110 | { |
| 111 | fputs_filtered (TYPE_NAME (type), stream); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | CHECK_TYPEDEF (type); |
| 116 | |
| 117 | switch (TYPE_CODE (type)) |
| 118 | { |
| 119 | case TYPE_CODE_PTR: |
| 120 | java_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level, |
| 121 | flags); |
| 122 | break; |
| 123 | |
| 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)); |
| 128 | |
| 129 | fputs_filtered (name, stream); |
| 130 | xfree (name); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | if (show >= 0) |
| 135 | fprintf_filtered (stream, "class "); |
| 136 | |
| 137 | if (TYPE_TAG_NAME (type) != NULL) |
| 138 | { |
| 139 | fputs_filtered (TYPE_TAG_NAME (type), stream); |
| 140 | if (show > 0) |
| 141 | fputs_filtered (" ", stream); |
| 142 | } |
| 143 | |
| 144 | wrap_here (" "); |
| 145 | |
| 146 | if (show < 0) |
| 147 | { |
| 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, "{...}"); |
| 151 | } |
| 152 | else if (show > 0 || TYPE_TAG_NAME (type) == NULL) |
| 153 | { |
| 154 | java_type_print_derivation_info (stream, type); |
| 155 | |
| 156 | fprintf_filtered (stream, "{\n"); |
| 157 | if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0)) |
| 158 | { |
| 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"); |
| 163 | } |
| 164 | |
| 165 | /* If there is a base class for this type, |
| 166 | do not print the field that it occupies. */ |
| 167 | |
| 168 | len = TYPE_NFIELDS (type); |
| 169 | for (i = TYPE_N_BASECLASSES (type); i < len; i++) |
| 170 | { |
| 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; |
| 176 | |
| 177 | /* Don't print the dummy field "class". */ |
| 178 | if (strncmp (TYPE_FIELD_NAME (type, i), "class", 5) == 0) |
| 179 | continue; |
| 180 | |
| 181 | print_spaces_filtered (level + 4, stream); |
| 182 | |
| 183 | if (HAVE_CPLUS_STRUCT (type)) |
| 184 | { |
| 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 "); |
| 191 | } |
| 192 | |
| 193 | if (field_is_static (&TYPE_FIELD (type, i))) |
| 194 | fprintf_filtered (stream, "static "); |
| 195 | |
| 196 | java_print_type (TYPE_FIELD_TYPE (type, i), |
| 197 | TYPE_FIELD_NAME (type, i), |
| 198 | stream, show - 1, level + 4, flags); |
| 199 | |
| 200 | fprintf_filtered (stream, ";\n"); |
| 201 | } |
| 202 | |
| 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"); |
| 207 | |
| 208 | /* Print out the methods. */ |
| 209 | |
| 210 | for (i = 0; i < len; i++) |
| 211 | { |
| 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; |
| 218 | |
| 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; |
| 224 | |
| 225 | for (j = 0; j < n_overloads; j++) |
| 226 | { |
| 227 | const char *real_physname; |
| 228 | char *physname, *p; |
| 229 | int is_full_physname_constructor; |
| 230 | |
| 231 | real_physname = TYPE_FN_FIELD_PHYSNAME (f, j); |
| 232 | |
| 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'; |
| 241 | |
| 242 | is_full_physname_constructor |
| 243 | = (TYPE_FN_FIELD_CONSTRUCTOR (f, j) |
| 244 | || is_constructor_name (physname) |
| 245 | || is_destructor_name (physname)); |
| 246 | |
| 247 | QUIT; |
| 248 | |
| 249 | print_spaces_filtered (level + 4, stream); |
| 250 | |
| 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 "); |
| 257 | |
| 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 "); |
| 268 | |
| 269 | if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0) |
| 270 | { |
| 271 | /* Keep GDB from crashing here. */ |
| 272 | fprintf_filtered (stream, "<undefined type> %s;\n", |
| 273 | TYPE_FN_FIELD_PHYSNAME (f, j)); |
| 274 | break; |
| 275 | } |
| 276 | else if (!is_constructor && !is_full_physname_constructor) |
| 277 | { |
| 278 | type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), |
| 279 | "", stream, -1); |
| 280 | fputs_filtered (" ", stream); |
| 281 | } |
| 282 | |
| 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; |
| 288 | |
| 289 | demangled_name = |
| 290 | gdb_demangle (mangled_name, |
| 291 | DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA); |
| 292 | |
| 293 | if (demangled_name == NULL) |
| 294 | demangled_name = xstrdup (mangled_name); |
| 295 | |
| 296 | { |
| 297 | char *demangled_no_class; |
| 298 | char *ptr; |
| 299 | |
| 300 | ptr = demangled_no_class = demangled_name; |
| 301 | |
| 302 | while (1) |
| 303 | { |
| 304 | char c; |
| 305 | |
| 306 | c = *ptr++; |
| 307 | |
| 308 | if (c == 0 || c == '(') |
| 309 | break; |
| 310 | if (c == '.') |
| 311 | demangled_no_class = ptr; |
| 312 | } |
| 313 | |
| 314 | fputs_filtered (demangled_no_class, stream); |
| 315 | xfree (demangled_name); |
| 316 | } |
| 317 | |
| 318 | if (TYPE_FN_FIELD_STUB (f, j)) |
| 319 | xfree (mangled_name); |
| 320 | |
| 321 | fprintf_filtered (stream, ";\n"); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | fprintfi_filtered (level, stream, "}"); |
| 326 | } |
| 327 | break; |
| 328 | |
| 329 | default: |
| 330 | c_type_print_base (type, stream, show, level, flags); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* LEVEL is the depth to indent lines by. */ |
| 335 | |
| 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) |
| 340 | { |
| 341 | int demangled_args; |
| 342 | |
| 343 | java_type_print_base (type, stream, show, level, flags); |
| 344 | |
| 345 | if (varstring != NULL && *varstring != '\0') |
| 346 | { |
| 347 | fputs_filtered (" ", stream); |
| 348 | fputs_filtered (varstring, stream); |
| 349 | } |
| 350 | |
| 351 | /* For demangled function names, we have the arglist as part of the name, |
| 352 | so don't print an additional pair of ()'s. */ |
| 353 | |
| 354 | demangled_args = varstring != NULL && strchr (varstring, '(') != NULL; |
| 355 | c_type_print_varspec_suffix (type, stream, show, 0, demangled_args, flags); |
| 356 | } |