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