| 1 | /* Support for printing C and C++ types for GDB, the GNU debugger. |
| 2 | Copyright 1986, 1988, 1989, 1991 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 2 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, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "obstack.h" |
| 22 | #include "bfd.h" /* Binary File Description */ |
| 23 | #include "symtab.h" |
| 24 | #include "gdbtypes.h" |
| 25 | #include "expression.h" |
| 26 | #include "value.h" |
| 27 | #include "gdbcore.h" |
| 28 | #include "target.h" |
| 29 | #include "command.h" |
| 30 | #include "gdbcmd.h" |
| 31 | #include "language.h" |
| 32 | #include "demangle.h" |
| 33 | #include "c-lang.h" |
| 34 | #include "typeprint.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | #include <errno.h> |
| 38 | |
| 39 | extern int demangle; /* whether to print C++ syms raw or source-form */ |
| 40 | |
| 41 | static void |
| 42 | c_type_print_args PARAMS ((struct type *, GDB_FILE *)); |
| 43 | |
| 44 | static void |
| 45 | c_type_print_varspec_suffix PARAMS ((struct type *, GDB_FILE *, int, int, int)); |
| 46 | |
| 47 | static void |
| 48 | cp_type_print_derivation_info PARAMS ((GDB_FILE *, struct type *)); |
| 49 | |
| 50 | void |
| 51 | c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int)); |
| 52 | |
| 53 | void |
| 54 | c_type_print_base PARAMS ((struct type *, GDB_FILE *, int, int)); |
| 55 | |
| 56 | \f |
| 57 | /* Print a description of a type in the format of a |
| 58 | typedef for the current language. |
| 59 | NEW is the new name for a type TYPE. */ |
| 60 | |
| 61 | void |
| 62 | c_typedef_print (type, new, stream) |
| 63 | struct type *type; |
| 64 | struct symbol *new; |
| 65 | GDB_FILE *stream; |
| 66 | { |
| 67 | switch (current_language->la_language) |
| 68 | { |
| 69 | #ifdef _LANG_c |
| 70 | case language_c: |
| 71 | case language_cplus: |
| 72 | fprintf_filtered(stream, "typedef "); |
| 73 | type_print(type,"",stream,0); |
| 74 | if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0 |
| 75 | || !STREQ (TYPE_NAME ((SYMBOL_TYPE (new))), SYMBOL_NAME (new))) |
| 76 | fprintf_filtered(stream, " %s", SYMBOL_SOURCE_NAME(new)); |
| 77 | break; |
| 78 | #endif |
| 79 | #ifdef _LANG_m2 |
| 80 | case language_m2: |
| 81 | fprintf_filtered(stream, "TYPE "); |
| 82 | if(!TYPE_NAME(SYMBOL_TYPE(new)) || |
| 83 | !STREQ (TYPE_NAME(SYMBOL_TYPE(new)), SYMBOL_NAME(new))) |
| 84 | fprintf_filtered(stream, "%s = ", SYMBOL_SOURCE_NAME(new)); |
| 85 | else |
| 86 | fprintf_filtered(stream, "<builtin> = "); |
| 87 | type_print(type,"",stream,0); |
| 88 | break; |
| 89 | #endif |
| 90 | #ifdef _LANG_chill |
| 91 | case language_chill: |
| 92 | error ("Missing Chill support in function c_typedef_print."); /*FIXME*/ |
| 93 | #endif |
| 94 | default: |
| 95 | error("Language not supported."); |
| 96 | } |
| 97 | fprintf_filtered(stream, ";\n"); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /* LEVEL is the depth to indent lines by. */ |
| 102 | |
| 103 | void |
| 104 | c_print_type (type, varstring, stream, show, level) |
| 105 | struct type *type; |
| 106 | char *varstring; |
| 107 | GDB_FILE *stream; |
| 108 | int show; |
| 109 | int level; |
| 110 | { |
| 111 | register enum type_code code; |
| 112 | int demangled_args; |
| 113 | |
| 114 | c_type_print_base (type, stream, show, level); |
| 115 | code = TYPE_CODE (type); |
| 116 | if ((varstring != NULL && *varstring != '\0') |
| 117 | || |
| 118 | /* Need a space if going to print stars or brackets; |
| 119 | but not if we will print just a type name. */ |
| 120 | ((show > 0 || TYPE_NAME (type) == 0) |
| 121 | && |
| 122 | (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC |
| 123 | || code == TYPE_CODE_METHOD |
| 124 | || code == TYPE_CODE_ARRAY |
| 125 | || code == TYPE_CODE_MEMBER |
| 126 | || code == TYPE_CODE_REF))) |
| 127 | fputs_filtered (" ", stream); |
| 128 | c_type_print_varspec_prefix (type, stream, show, 0); |
| 129 | |
| 130 | fputs_filtered (varstring, stream); |
| 131 | |
| 132 | /* For demangled function names, we have the arglist as part of the name, |
| 133 | so don't print an additional pair of ()'s */ |
| 134 | |
| 135 | demangled_args = varstring[strlen(varstring) - 1] == ')'; |
| 136 | c_type_print_varspec_suffix (type, stream, show, 0, demangled_args); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | /* Print the C++ method arguments ARGS to the file STREAM. */ |
| 141 | |
| 142 | void |
| 143 | cp_type_print_method_args (args, prefix, varstring, staticp, stream) |
| 144 | struct type **args; |
| 145 | char *prefix; |
| 146 | char *varstring; |
| 147 | int staticp; |
| 148 | GDB_FILE *stream; |
| 149 | { |
| 150 | int i; |
| 151 | |
| 152 | fprintf_symbol_filtered (stream, prefix, language_cplus, DMGL_ANSI); |
| 153 | fprintf_symbol_filtered (stream, varstring, language_cplus, DMGL_ANSI); |
| 154 | fputs_filtered (" (", stream); |
| 155 | if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID) |
| 156 | { |
| 157 | i = !staticp; /* skip the class variable */ |
| 158 | while (1) |
| 159 | { |
| 160 | type_print (args[i++], "", stream, 0); |
| 161 | if (!args[i]) |
| 162 | { |
| 163 | fprintf_filtered (stream, " ..."); |
| 164 | break; |
| 165 | } |
| 166 | else if (args[i]->code != TYPE_CODE_VOID) |
| 167 | { |
| 168 | fprintf_filtered (stream, ", "); |
| 169 | } |
| 170 | else break; |
| 171 | } |
| 172 | } |
| 173 | fprintf_filtered (stream, ")"); |
| 174 | } |
| 175 | |
| 176 | /* If TYPE is a derived type, then print out derivation information. |
| 177 | Print only the actual base classes of this type, not the base classes |
| 178 | of the base classes. I.E. for the derivation hierarchy: |
| 179 | |
| 180 | class A { int a; }; |
| 181 | class B : public A {int b; }; |
| 182 | class C : public B {int c; }; |
| 183 | |
| 184 | Print the type of class C as: |
| 185 | |
| 186 | class C : public B { |
| 187 | int c; |
| 188 | } |
| 189 | |
| 190 | Not as the following (like gdb used to), which is not legal C++ syntax for |
| 191 | derived types and may be confused with the multiple inheritance form: |
| 192 | |
| 193 | class C : public B : public A { |
| 194 | int c; |
| 195 | } |
| 196 | |
| 197 | In general, gdb should try to print the types as closely as possible to |
| 198 | the form that they appear in the source code. */ |
| 199 | |
| 200 | static void |
| 201 | cp_type_print_derivation_info (stream, type) |
| 202 | GDB_FILE *stream; |
| 203 | struct type *type; |
| 204 | { |
| 205 | char *name; |
| 206 | int i; |
| 207 | |
| 208 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) |
| 209 | { |
| 210 | fputs_filtered (i == 0 ? ": " : ", ", stream); |
| 211 | fprintf_filtered (stream, "%s%s ", |
| 212 | BASETYPE_VIA_PUBLIC (type, i) ? "public" : "private", |
| 213 | BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : ""); |
| 214 | name = type_name_no_tag (TYPE_BASECLASS (type, i)); |
| 215 | fprintf_filtered (stream, "%s", name ? name : "(null)"); |
| 216 | } |
| 217 | if (i > 0) |
| 218 | { |
| 219 | fputs_filtered (" ", stream); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /* Print any asterisks or open-parentheses needed before the |
| 224 | variable name (to describe its type). |
| 225 | |
| 226 | On outermost call, pass 0 for PASSED_A_PTR. |
| 227 | On outermost call, SHOW > 0 means should ignore |
| 228 | any typename for TYPE and show its details. |
| 229 | SHOW is always zero on recursive calls. */ |
| 230 | |
| 231 | void |
| 232 | c_type_print_varspec_prefix (type, stream, show, passed_a_ptr) |
| 233 | struct type *type; |
| 234 | GDB_FILE *stream; |
| 235 | int show; |
| 236 | int passed_a_ptr; |
| 237 | { |
| 238 | char *name; |
| 239 | if (type == 0) |
| 240 | return; |
| 241 | |
| 242 | if (TYPE_NAME (type) && show <= 0) |
| 243 | return; |
| 244 | |
| 245 | QUIT; |
| 246 | |
| 247 | switch (TYPE_CODE (type)) |
| 248 | { |
| 249 | case TYPE_CODE_PTR: |
| 250 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1); |
| 251 | fprintf_filtered (stream, "*"); |
| 252 | break; |
| 253 | |
| 254 | case TYPE_CODE_MEMBER: |
| 255 | if (passed_a_ptr) |
| 256 | fprintf_filtered (stream, "("); |
| 257 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); |
| 258 | fprintf_filtered (stream, " "); |
| 259 | name = type_name_no_tag (TYPE_DOMAIN_TYPE (type)); |
| 260 | if (name) |
| 261 | fputs_filtered (name, stream); |
| 262 | else |
| 263 | c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr); |
| 264 | fprintf_filtered (stream, "::"); |
| 265 | break; |
| 266 | |
| 267 | case TYPE_CODE_METHOD: |
| 268 | if (passed_a_ptr) |
| 269 | fprintf_unfiltered (stream, "("); |
| 270 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); |
| 271 | if (passed_a_ptr) |
| 272 | { |
| 273 | fprintf_filtered (stream, " "); |
| 274 | c_type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr); |
| 275 | fprintf_filtered (stream, "::"); |
| 276 | } |
| 277 | break; |
| 278 | |
| 279 | case TYPE_CODE_REF: |
| 280 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1); |
| 281 | fprintf_filtered (stream, "&"); |
| 282 | break; |
| 283 | |
| 284 | case TYPE_CODE_FUNC: |
| 285 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); |
| 286 | if (passed_a_ptr) |
| 287 | fprintf_filtered (stream, "("); |
| 288 | break; |
| 289 | |
| 290 | case TYPE_CODE_ARRAY: |
| 291 | c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0); |
| 292 | if (passed_a_ptr) |
| 293 | fprintf_filtered (stream, "("); |
| 294 | break; |
| 295 | |
| 296 | case TYPE_CODE_UNDEF: |
| 297 | case TYPE_CODE_STRUCT: |
| 298 | case TYPE_CODE_UNION: |
| 299 | case TYPE_CODE_ENUM: |
| 300 | case TYPE_CODE_INT: |
| 301 | case TYPE_CODE_FLT: |
| 302 | case TYPE_CODE_VOID: |
| 303 | case TYPE_CODE_ERROR: |
| 304 | case TYPE_CODE_CHAR: |
| 305 | case TYPE_CODE_BOOL: |
| 306 | case TYPE_CODE_SET: |
| 307 | case TYPE_CODE_RANGE: |
| 308 | case TYPE_CODE_STRING: |
| 309 | case TYPE_CODE_BITSTRING: |
| 310 | /* These types need no prefix. They are listed here so that |
| 311 | gcc -Wall will reveal any types that haven't been handled. */ |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | c_type_print_args (type, stream) |
| 318 | struct type *type; |
| 319 | GDB_FILE *stream; |
| 320 | { |
| 321 | int i; |
| 322 | struct type **args; |
| 323 | |
| 324 | fprintf_filtered (stream, "("); |
| 325 | args = TYPE_ARG_TYPES (type); |
| 326 | if (args != NULL) |
| 327 | { |
| 328 | if (args[1] == NULL) |
| 329 | { |
| 330 | fprintf_filtered (stream, "..."); |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | for (i = 1; |
| 335 | args[i] != NULL && args[i]->code != TYPE_CODE_VOID; |
| 336 | i++) |
| 337 | { |
| 338 | c_print_type (args[i], "", stream, -1, 0); |
| 339 | if (args[i+1] == NULL) |
| 340 | { |
| 341 | fprintf_filtered (stream, "..."); |
| 342 | } |
| 343 | else if (args[i+1]->code != TYPE_CODE_VOID) |
| 344 | { |
| 345 | fprintf_filtered (stream, ","); |
| 346 | wrap_here (" "); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | fprintf_filtered (stream, ")"); |
| 352 | } |
| 353 | |
| 354 | /* Print any array sizes, function arguments or close parentheses |
| 355 | needed after the variable name (to describe its type). |
| 356 | Args work like c_type_print_varspec_prefix. */ |
| 357 | |
| 358 | static void |
| 359 | c_type_print_varspec_suffix (type, stream, show, passed_a_ptr, demangled_args) |
| 360 | struct type *type; |
| 361 | GDB_FILE *stream; |
| 362 | int show; |
| 363 | int passed_a_ptr; |
| 364 | int demangled_args; |
| 365 | { |
| 366 | if (type == 0) |
| 367 | return; |
| 368 | |
| 369 | if (TYPE_NAME (type) && show <= 0) |
| 370 | return; |
| 371 | |
| 372 | QUIT; |
| 373 | |
| 374 | switch (TYPE_CODE (type)) |
| 375 | { |
| 376 | case TYPE_CODE_ARRAY: |
| 377 | if (passed_a_ptr) |
| 378 | fprintf_filtered (stream, ")"); |
| 379 | |
| 380 | fprintf_filtered (stream, "["); |
| 381 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) |
| 382 | fprintf_filtered (stream, "%d", |
| 383 | (TYPE_LENGTH (type) |
| 384 | / TYPE_LENGTH (TYPE_TARGET_TYPE (type)))); |
| 385 | fprintf_filtered (stream, "]"); |
| 386 | |
| 387 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); |
| 388 | break; |
| 389 | |
| 390 | case TYPE_CODE_MEMBER: |
| 391 | if (passed_a_ptr) |
| 392 | fprintf_filtered (stream, ")"); |
| 393 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); |
| 394 | break; |
| 395 | |
| 396 | case TYPE_CODE_METHOD: |
| 397 | if (passed_a_ptr) |
| 398 | fprintf_filtered (stream, ")"); |
| 399 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); |
| 400 | if (passed_a_ptr) |
| 401 | { |
| 402 | c_type_print_args (type, stream); |
| 403 | } |
| 404 | break; |
| 405 | |
| 406 | case TYPE_CODE_PTR: |
| 407 | case TYPE_CODE_REF: |
| 408 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0); |
| 409 | break; |
| 410 | |
| 411 | case TYPE_CODE_FUNC: |
| 412 | if (passed_a_ptr) |
| 413 | fprintf_filtered (stream, ")"); |
| 414 | if (!demangled_args) |
| 415 | fprintf_filtered (stream, "()"); |
| 416 | c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, |
| 417 | passed_a_ptr, 0); |
| 418 | break; |
| 419 | |
| 420 | case TYPE_CODE_UNDEF: |
| 421 | case TYPE_CODE_STRUCT: |
| 422 | case TYPE_CODE_UNION: |
| 423 | case TYPE_CODE_ENUM: |
| 424 | case TYPE_CODE_INT: |
| 425 | case TYPE_CODE_FLT: |
| 426 | case TYPE_CODE_VOID: |
| 427 | case TYPE_CODE_ERROR: |
| 428 | case TYPE_CODE_CHAR: |
| 429 | case TYPE_CODE_BOOL: |
| 430 | case TYPE_CODE_SET: |
| 431 | case TYPE_CODE_RANGE: |
| 432 | case TYPE_CODE_STRING: |
| 433 | case TYPE_CODE_BITSTRING: |
| 434 | /* These types do not need a suffix. They are listed so that |
| 435 | gcc -Wall will report types that may not have been considered. */ |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* Print the name of the type (or the ultimate pointer target, |
| 441 | function value or array element), or the description of a |
| 442 | structure or union. |
| 443 | |
| 444 | SHOW positive means print details about the type (e.g. enum values), |
| 445 | and print structure elements passing SHOW - 1 for show. |
| 446 | SHOW zero means just print the type name or struct tag if there is one. |
| 447 | If there is no name, print something sensible but concise like |
| 448 | "struct {...}". |
| 449 | SHOW negative means the same things as SHOW zero. The difference is that |
| 450 | zero is used for printing structure elements and -1 is used for the |
| 451 | "whatis" command. But I don't see any need to distinguish. |
| 452 | |
| 453 | LEVEL is the number of spaces to indent by. |
| 454 | We increase it for some recursive calls. */ |
| 455 | |
| 456 | void |
| 457 | c_type_print_base (type, stream, show, level) |
| 458 | struct type *type; |
| 459 | GDB_FILE *stream; |
| 460 | int show; |
| 461 | int level; |
| 462 | { |
| 463 | register int i; |
| 464 | register int len; |
| 465 | register int lastval; |
| 466 | char *mangled_name; |
| 467 | char *demangled_name; |
| 468 | enum {s_none, s_public, s_private, s_protected} section_type; |
| 469 | QUIT; |
| 470 | |
| 471 | wrap_here (" "); |
| 472 | if (type == NULL) |
| 473 | { |
| 474 | fputs_filtered ("<type unknown>", stream); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | /* When SHOW is zero or less, and there is a valid type name, then always |
| 479 | just print the type name directly from the type. */ |
| 480 | /* If we have "typedef struct foo {. . .} bar;" do we want to print it |
| 481 | as "struct foo" or as "bar"? Pick the latter, because C++ folk tend |
| 482 | to expect things like "class5 *foo" rather than "struct class5 *foo". */ |
| 483 | |
| 484 | if (show <= 0 |
| 485 | && TYPE_NAME (type) != NULL) |
| 486 | { |
| 487 | fputs_filtered (TYPE_NAME (type), stream); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | check_stub_type (type); |
| 492 | |
| 493 | switch (TYPE_CODE (type)) |
| 494 | { |
| 495 | case TYPE_CODE_ARRAY: |
| 496 | case TYPE_CODE_PTR: |
| 497 | case TYPE_CODE_MEMBER: |
| 498 | case TYPE_CODE_REF: |
| 499 | case TYPE_CODE_FUNC: |
| 500 | case TYPE_CODE_METHOD: |
| 501 | c_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level); |
| 502 | break; |
| 503 | |
| 504 | case TYPE_CODE_STRUCT: |
| 505 | if (HAVE_CPLUS_STRUCT (type)) |
| 506 | { |
| 507 | fprintf_filtered (stream, "class "); |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | fprintf_filtered (stream, "struct "); |
| 512 | } |
| 513 | goto struct_union; |
| 514 | |
| 515 | case TYPE_CODE_UNION: |
| 516 | fprintf_filtered (stream, "union "); |
| 517 | |
| 518 | struct_union: |
| 519 | if (TYPE_TAG_NAME (type) != NULL) |
| 520 | { |
| 521 | fputs_filtered (TYPE_TAG_NAME (type), stream); |
| 522 | if (show > 0) |
| 523 | fputs_filtered (" ", stream); |
| 524 | } |
| 525 | wrap_here (" "); |
| 526 | if (show <= 0) |
| 527 | { |
| 528 | /* If we just printed a tag name, no need to print anything else. */ |
| 529 | if (TYPE_TAG_NAME (type) == NULL) |
| 530 | fprintf_filtered (stream, "{...}"); |
| 531 | } |
| 532 | else if (show > 0) |
| 533 | { |
| 534 | cp_type_print_derivation_info (stream, type); |
| 535 | |
| 536 | fprintf_filtered (stream, "{\n"); |
| 537 | if ((TYPE_NFIELDS (type) == 0) && (TYPE_NFN_FIELDS (type) == 0)) |
| 538 | { |
| 539 | if (TYPE_FLAGS (type) & TYPE_FLAG_STUB) |
| 540 | fprintfi_filtered (level + 4, stream, "<incomplete type>\n"); |
| 541 | else |
| 542 | fprintfi_filtered (level + 4, stream, "<no data fields>\n"); |
| 543 | } |
| 544 | |
| 545 | /* Start off with no specific section type, so we can print |
| 546 | one for the first field we find, and use that section type |
| 547 | thereafter until we find another type. */ |
| 548 | |
| 549 | section_type = s_none; |
| 550 | |
| 551 | /* If there is a base class for this type, |
| 552 | do not print the field that it occupies. */ |
| 553 | |
| 554 | len = TYPE_NFIELDS (type); |
| 555 | for (i = TYPE_N_BASECLASSES (type); i < len; i++) |
| 556 | { |
| 557 | QUIT; |
| 558 | /* Don't print out virtual function table. */ |
| 559 | if ((TYPE_FIELD_NAME (type, i))[5] == CPLUS_MARKER && |
| 560 | !strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5)) |
| 561 | continue; |
| 562 | |
| 563 | /* If this is a C++ class we can print the various C++ section |
| 564 | labels. */ |
| 565 | |
| 566 | if (HAVE_CPLUS_STRUCT (type)) |
| 567 | { |
| 568 | if (TYPE_FIELD_PROTECTED (type, i)) |
| 569 | { |
| 570 | if (section_type != s_protected) |
| 571 | { |
| 572 | section_type = s_protected; |
| 573 | fprintfi_filtered (level + 2, stream, |
| 574 | "protected:\n"); |
| 575 | } |
| 576 | } |
| 577 | else if (TYPE_FIELD_PRIVATE (type, i)) |
| 578 | { |
| 579 | if (section_type != s_private) |
| 580 | { |
| 581 | section_type = s_private; |
| 582 | fprintfi_filtered (level + 2, stream, "private:\n"); |
| 583 | } |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | if (section_type != s_public) |
| 588 | { |
| 589 | section_type = s_public; |
| 590 | fprintfi_filtered (level + 2, stream, "public:\n"); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | print_spaces_filtered (level + 4, stream); |
| 596 | if (TYPE_FIELD_STATIC (type, i)) |
| 597 | { |
| 598 | fprintf_filtered (stream, "static "); |
| 599 | } |
| 600 | c_print_type (TYPE_FIELD_TYPE (type, i), |
| 601 | TYPE_FIELD_NAME (type, i), |
| 602 | stream, show - 1, level + 4); |
| 603 | if (!TYPE_FIELD_STATIC (type, i) |
| 604 | && TYPE_FIELD_PACKED (type, i)) |
| 605 | { |
| 606 | /* It is a bitfield. This code does not attempt |
| 607 | to look at the bitpos and reconstruct filler, |
| 608 | unnamed fields. This would lead to misleading |
| 609 | results if the compiler does not put out fields |
| 610 | for such things (I don't know what it does). */ |
| 611 | fprintf_filtered (stream, " : %d", |
| 612 | TYPE_FIELD_BITSIZE (type, i)); |
| 613 | } |
| 614 | fprintf_filtered (stream, ";\n"); |
| 615 | } |
| 616 | |
| 617 | /* If there are both fields and methods, put a space between. */ |
| 618 | len = TYPE_NFN_FIELDS (type); |
| 619 | if (len && section_type != s_none) |
| 620 | fprintf_filtered (stream, "\n"); |
| 621 | |
| 622 | /* C++: print out the methods */ |
| 623 | |
| 624 | for (i = 0; i < len; i++) |
| 625 | { |
| 626 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); |
| 627 | int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i); |
| 628 | char *method_name = TYPE_FN_FIELDLIST_NAME (type, i); |
| 629 | char *name = type_name_no_tag (type); |
| 630 | int is_constructor = name && STREQ(method_name, name); |
| 631 | for (j = 0; j < len2; j++) |
| 632 | { |
| 633 | QUIT; |
| 634 | if (TYPE_FN_FIELD_PROTECTED (f, j)) |
| 635 | { |
| 636 | if (section_type != s_protected) |
| 637 | { |
| 638 | section_type = s_protected; |
| 639 | fprintfi_filtered (level + 2, stream, |
| 640 | "protected:\n"); |
| 641 | } |
| 642 | } |
| 643 | else if (TYPE_FN_FIELD_PRIVATE (f, j)) |
| 644 | { |
| 645 | if (section_type != s_private) |
| 646 | { |
| 647 | section_type = s_private; |
| 648 | fprintfi_filtered (level + 2, stream, "private:\n"); |
| 649 | } |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | if (section_type != s_public) |
| 654 | { |
| 655 | section_type = s_public; |
| 656 | fprintfi_filtered (level + 2, stream, "public:\n"); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | print_spaces_filtered (level + 4, stream); |
| 661 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) |
| 662 | fprintf_filtered (stream, "virtual "); |
| 663 | else if (TYPE_FN_FIELD_STATIC_P (f, j)) |
| 664 | fprintf_filtered (stream, "static "); |
| 665 | if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0) |
| 666 | { |
| 667 | /* Keep GDB from crashing here. */ |
| 668 | fprintf_unfiltered (stream, "<undefined type> %s;\n", |
| 669 | TYPE_FN_FIELD_PHYSNAME (f, j)); |
| 670 | break; |
| 671 | } |
| 672 | else if (!is_constructor) |
| 673 | { |
| 674 | type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), |
| 675 | "", stream, 0); |
| 676 | fputs_filtered (" ", stream); |
| 677 | } |
| 678 | if (TYPE_FN_FIELD_STUB (f, j)) |
| 679 | { |
| 680 | /* Build something we can demangle. */ |
| 681 | mangled_name = gdb_mangle_name (type, i, j); |
| 682 | demangled_name = |
| 683 | cplus_demangle (mangled_name, |
| 684 | DMGL_ANSI | DMGL_PARAMS); |
| 685 | if (demangled_name == NULL) |
| 686 | fprintf_filtered (stream, "<badly mangled name %s>", |
| 687 | mangled_name); |
| 688 | else |
| 689 | { |
| 690 | char *demangled_no_class = |
| 691 | strchr (demangled_name, ':'); |
| 692 | |
| 693 | if (demangled_no_class == NULL) |
| 694 | demangled_no_class = demangled_name; |
| 695 | else |
| 696 | { |
| 697 | if (*++demangled_no_class == ':') |
| 698 | ++demangled_no_class; |
| 699 | } |
| 700 | fputs_filtered (demangled_no_class, stream); |
| 701 | free (demangled_name); |
| 702 | } |
| 703 | free (mangled_name); |
| 704 | } |
| 705 | else if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_' |
| 706 | && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER) |
| 707 | cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, |
| 708 | "~", method_name, 0, stream); |
| 709 | else |
| 710 | cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "", |
| 711 | method_name, |
| 712 | TYPE_FN_FIELD_STATIC_P (f, j), |
| 713 | stream); |
| 714 | |
| 715 | fprintf_filtered (stream, ";\n"); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | fprintfi_filtered (level, stream, "}"); |
| 720 | } |
| 721 | break; |
| 722 | |
| 723 | case TYPE_CODE_ENUM: |
| 724 | fprintf_filtered (stream, "enum "); |
| 725 | if (TYPE_TAG_NAME (type) != NULL) |
| 726 | { |
| 727 | fputs_filtered (TYPE_TAG_NAME (type), stream); |
| 728 | if (show > 0) |
| 729 | fputs_filtered (" ", stream); |
| 730 | } |
| 731 | |
| 732 | wrap_here (" "); |
| 733 | if (show <= 0) |
| 734 | { |
| 735 | /* If we just printed a tag name, no need to print anything else. */ |
| 736 | if (TYPE_TAG_NAME (type) == NULL) |
| 737 | fprintf_filtered (stream, "{...}"); |
| 738 | } |
| 739 | else if (show > 0) |
| 740 | { |
| 741 | fprintf_filtered (stream, "{"); |
| 742 | len = TYPE_NFIELDS (type); |
| 743 | lastval = 0; |
| 744 | for (i = 0; i < len; i++) |
| 745 | { |
| 746 | QUIT; |
| 747 | if (i) fprintf_filtered (stream, ", "); |
| 748 | wrap_here (" "); |
| 749 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
| 750 | if (lastval != TYPE_FIELD_BITPOS (type, i)) |
| 751 | { |
| 752 | fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i)); |
| 753 | lastval = TYPE_FIELD_BITPOS (type, i); |
| 754 | } |
| 755 | lastval++; |
| 756 | } |
| 757 | fprintf_filtered (stream, "}"); |
| 758 | } |
| 759 | break; |
| 760 | |
| 761 | case TYPE_CODE_VOID: |
| 762 | fprintf_filtered (stream, "void"); |
| 763 | break; |
| 764 | |
| 765 | case TYPE_CODE_UNDEF: |
| 766 | fprintf_filtered (stream, "struct <unknown>"); |
| 767 | break; |
| 768 | |
| 769 | case TYPE_CODE_ERROR: |
| 770 | fprintf_filtered (stream, "<unknown type>"); |
| 771 | break; |
| 772 | |
| 773 | case TYPE_CODE_RANGE: |
| 774 | /* This should not occur */ |
| 775 | fprintf_filtered (stream, "<range type>"); |
| 776 | break; |
| 777 | |
| 778 | default: |
| 779 | /* Handle types not explicitly handled by the other cases, |
| 780 | such as fundamental types. For these, just print whatever |
| 781 | the type name is, as recorded in the type itself. If there |
| 782 | is no type name, then complain. */ |
| 783 | if (TYPE_NAME (type) != NULL) |
| 784 | { |
| 785 | fputs_filtered (TYPE_NAME (type), stream); |
| 786 | } |
| 787 | else |
| 788 | { |
| 789 | /* At least for dump_symtab, it is important that this not be |
| 790 | an error (). */ |
| 791 | fprintf_filtered (stream, "<invalid type code %d>", |
| 792 | TYPE_CODE (type)); |
| 793 | } |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |