| 1 | /* Language independent support for printing types for GDB, the GNU debugger. |
| 2 | Copyright 1986, 1988, 1989, 1991-1993, 1998, 2000 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., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "obstack.h" |
| 23 | #include "bfd.h" /* Binary File Description */ |
| 24 | #include "symtab.h" |
| 25 | #include "gdbtypes.h" |
| 26 | #include "expression.h" |
| 27 | #include "value.h" |
| 28 | #include "gdbcore.h" |
| 29 | #include "command.h" |
| 30 | #include "gdbcmd.h" |
| 31 | #include "target.h" |
| 32 | #include "language.h" |
| 33 | #include "demangle.h" |
| 34 | |
| 35 | #include "gdb_string.h" |
| 36 | #include <errno.h> |
| 37 | |
| 38 | /* For real-type printing in whatis_exp() */ |
| 39 | extern int objectprint; /* Controls looking up an object's derived type |
| 40 | using what we find in its vtables. */ |
| 41 | |
| 42 | extern void _initialize_typeprint PARAMS ((void)); |
| 43 | |
| 44 | static void |
| 45 | ptype_command PARAMS ((char *, int)); |
| 46 | |
| 47 | static struct type * |
| 48 | ptype_eval PARAMS ((struct expression *)); |
| 49 | |
| 50 | static void |
| 51 | whatis_command PARAMS ((char *, int)); |
| 52 | |
| 53 | static void |
| 54 | whatis_exp PARAMS ((char *, int)); |
| 55 | |
| 56 | /* Print a description of a type TYPE in the form of a declaration of a |
| 57 | variable named VARSTRING. (VARSTRING is demangled if necessary.) |
| 58 | Output goes to STREAM (via stdio). |
| 59 | If SHOW is positive, we show the contents of the outermost level |
| 60 | of structure even if there is a type name that could be used instead. |
| 61 | If SHOW is negative, we never show the details of elements' types. */ |
| 62 | |
| 63 | void |
| 64 | type_print (type, varstring, stream, show) |
| 65 | struct type *type; |
| 66 | char *varstring; |
| 67 | struct ui_file *stream; |
| 68 | int show; |
| 69 | { |
| 70 | LA_PRINT_TYPE (type, varstring, stream, show, 0); |
| 71 | } |
| 72 | |
| 73 | /* Print type of EXP, or last thing in value history if EXP == NULL. |
| 74 | show is passed to type_print. */ |
| 75 | |
| 76 | static void |
| 77 | whatis_exp (exp, show) |
| 78 | char *exp; |
| 79 | int show; |
| 80 | { |
| 81 | struct expression *expr; |
| 82 | register value_ptr val; |
| 83 | register struct cleanup *old_chain = NULL; |
| 84 | struct type *real_type = NULL; |
| 85 | int full = 0; |
| 86 | int top = -1; |
| 87 | int using_enc = 0; |
| 88 | |
| 89 | if (exp) |
| 90 | { |
| 91 | expr = parse_expression (exp); |
| 92 | old_chain = make_cleanup ((make_cleanup_func) free_current_contents, |
| 93 | &expr); |
| 94 | val = evaluate_type (expr); |
| 95 | } |
| 96 | else |
| 97 | val = access_value_history (0); |
| 98 | |
| 99 | real_type = value_rtti_type (val, &full, &top, &using_enc); |
| 100 | |
| 101 | printf_filtered ("type = "); |
| 102 | |
| 103 | if (real_type && objectprint) |
| 104 | printf_filtered ("/* real type = %s%s */\n", |
| 105 | TYPE_NAME (real_type), |
| 106 | full ? "" : " (incomplete object)"); |
| 107 | /* FIXME: maybe better to use type_print (real_type, "", gdb_stdout, -1); */ |
| 108 | |
| 109 | type_print (VALUE_TYPE (val), "", gdb_stdout, show); |
| 110 | printf_filtered ("\n"); |
| 111 | |
| 112 | if (exp) |
| 113 | do_cleanups (old_chain); |
| 114 | } |
| 115 | |
| 116 | /* ARGSUSED */ |
| 117 | static void |
| 118 | whatis_command (exp, from_tty) |
| 119 | char *exp; |
| 120 | int from_tty; |
| 121 | { |
| 122 | /* Most of the time users do not want to see all the fields |
| 123 | in a structure. If they do they can use the "ptype" command. |
| 124 | Hence the "-1" below. */ |
| 125 | whatis_exp (exp, -1); |
| 126 | } |
| 127 | |
| 128 | /* Simple subroutine for ptype_command. */ |
| 129 | |
| 130 | static struct type * |
| 131 | ptype_eval (exp) |
| 132 | struct expression *exp; |
| 133 | { |
| 134 | if (exp->elts[0].opcode == OP_TYPE) |
| 135 | { |
| 136 | return (exp->elts[1].type); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | return (NULL); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* TYPENAME is either the name of a type, or an expression. */ |
| 145 | |
| 146 | /* ARGSUSED */ |
| 147 | static void |
| 148 | ptype_command (typename, from_tty) |
| 149 | char *typename; |
| 150 | int from_tty; |
| 151 | { |
| 152 | register struct type *type; |
| 153 | struct expression *expr; |
| 154 | register struct cleanup *old_chain; |
| 155 | |
| 156 | if (typename == NULL) |
| 157 | { |
| 158 | /* Print type of last thing in value history. */ |
| 159 | whatis_exp (typename, 1); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | expr = parse_expression (typename); |
| 164 | old_chain = make_cleanup ((make_cleanup_func) free_current_contents, |
| 165 | &expr); |
| 166 | type = ptype_eval (expr); |
| 167 | if (type != NULL) |
| 168 | { |
| 169 | /* User did "ptype <typename>" */ |
| 170 | printf_filtered ("type = "); |
| 171 | type_print (type, "", gdb_stdout, 1); |
| 172 | printf_filtered ("\n"); |
| 173 | do_cleanups (old_chain); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | /* User did "ptype <symbolname>" */ |
| 178 | do_cleanups (old_chain); |
| 179 | whatis_exp (typename, 1); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM. |
| 185 | Used to print data from type structures in a specified type. For example, |
| 186 | array bounds may be characters or booleans in some languages, and this |
| 187 | allows the ranges to be printed in their "natural" form rather than as |
| 188 | decimal integer values. |
| 189 | |
| 190 | FIXME: This is here simply because only the type printing routines |
| 191 | currently use it, and it wasn't clear if it really belonged somewhere |
| 192 | else (like printcmd.c). There are a lot of other gdb routines that do |
| 193 | something similar, but they are generally concerned with printing values |
| 194 | that come from the inferior in target byte order and target size. */ |
| 195 | |
| 196 | void |
| 197 | print_type_scalar (type, val, stream) |
| 198 | struct type *type; |
| 199 | LONGEST val; |
| 200 | struct ui_file *stream; |
| 201 | { |
| 202 | unsigned int i; |
| 203 | unsigned len; |
| 204 | |
| 205 | CHECK_TYPEDEF (type); |
| 206 | |
| 207 | switch (TYPE_CODE (type)) |
| 208 | { |
| 209 | |
| 210 | case TYPE_CODE_ENUM: |
| 211 | len = TYPE_NFIELDS (type); |
| 212 | for (i = 0; i < len; i++) |
| 213 | { |
| 214 | if (TYPE_FIELD_BITPOS (type, i) == val) |
| 215 | { |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | if (i < len) |
| 220 | { |
| 221 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | print_longest (stream, 'd', 0, val); |
| 226 | } |
| 227 | break; |
| 228 | |
| 229 | case TYPE_CODE_INT: |
| 230 | print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val); |
| 231 | break; |
| 232 | |
| 233 | case TYPE_CODE_CHAR: |
| 234 | LA_PRINT_CHAR ((unsigned char) val, stream); |
| 235 | break; |
| 236 | |
| 237 | case TYPE_CODE_BOOL: |
| 238 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); |
| 239 | break; |
| 240 | |
| 241 | case TYPE_CODE_RANGE: |
| 242 | print_type_scalar (TYPE_TARGET_TYPE (type), val, stream); |
| 243 | return; |
| 244 | |
| 245 | case TYPE_CODE_UNDEF: |
| 246 | case TYPE_CODE_PTR: |
| 247 | case TYPE_CODE_ARRAY: |
| 248 | case TYPE_CODE_STRUCT: |
| 249 | case TYPE_CODE_UNION: |
| 250 | case TYPE_CODE_FUNC: |
| 251 | case TYPE_CODE_FLT: |
| 252 | case TYPE_CODE_VOID: |
| 253 | case TYPE_CODE_SET: |
| 254 | case TYPE_CODE_STRING: |
| 255 | case TYPE_CODE_ERROR: |
| 256 | case TYPE_CODE_MEMBER: |
| 257 | case TYPE_CODE_METHOD: |
| 258 | case TYPE_CODE_REF: |
| 259 | error ("internal error: unhandled type in print_type_scalar"); |
| 260 | break; |
| 261 | |
| 262 | default: |
| 263 | error ("Invalid type code in symbol table."); |
| 264 | } |
| 265 | gdb_flush (stream); |
| 266 | } |
| 267 | |
| 268 | /* Dump details of a type specified either directly or indirectly. |
| 269 | Uses the same sort of type lookup mechanism as ptype_command() |
| 270 | and whatis_command(). */ |
| 271 | |
| 272 | void |
| 273 | maintenance_print_type (typename, from_tty) |
| 274 | char *typename; |
| 275 | int from_tty; |
| 276 | { |
| 277 | register value_ptr val; |
| 278 | register struct type *type; |
| 279 | register struct cleanup *old_chain; |
| 280 | struct expression *expr; |
| 281 | |
| 282 | if (typename != NULL) |
| 283 | { |
| 284 | expr = parse_expression (typename); |
| 285 | old_chain = make_cleanup ((make_cleanup_func) free_current_contents, &expr); |
| 286 | if (expr->elts[0].opcode == OP_TYPE) |
| 287 | { |
| 288 | /* The user expression names a type directly, just use that type. */ |
| 289 | type = expr->elts[1].type; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | /* The user expression may name a type indirectly by naming an |
| 294 | object of that type. Find that indirectly named type. */ |
| 295 | val = evaluate_type (expr); |
| 296 | type = VALUE_TYPE (val); |
| 297 | } |
| 298 | if (type != NULL) |
| 299 | { |
| 300 | recursive_dump_type (type, 0); |
| 301 | } |
| 302 | do_cleanups (old_chain); |
| 303 | } |
| 304 | } |
| 305 | \f |
| 306 | |
| 307 | void |
| 308 | _initialize_typeprint () |
| 309 | { |
| 310 | |
| 311 | add_com ("ptype", class_vars, ptype_command, |
| 312 | "Print definition of type TYPE.\n\ |
| 313 | Argument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\ |
| 314 | or \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\ |
| 315 | The selected stack frame's lexical context is used to look up the name."); |
| 316 | |
| 317 | add_com ("whatis", class_vars, whatis_command, |
| 318 | "Print data type of expression EXP."); |
| 319 | |
| 320 | } |