119370Spst/* Language independent support for printing types for GDB, the GNU debugger. 219370Spst 3130803Smarcel Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 4130803Smarcel 1999, 2000, 2001, 2003 Free Software Foundation, Inc. 5130803Smarcel 698944Sobrien This file is part of GDB. 719370Spst 898944Sobrien This program is free software; you can redistribute it and/or modify 998944Sobrien it under the terms of the GNU General Public License as published by 1098944Sobrien the Free Software Foundation; either version 2 of the License, or 1198944Sobrien (at your option) any later version. 1219370Spst 1398944Sobrien This program is distributed in the hope that it will be useful, 1498944Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1598944Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1698944Sobrien GNU General Public License for more details. 1719370Spst 1898944Sobrien You should have received a copy of the GNU General Public License 1998944Sobrien along with this program; if not, write to the Free Software 2098944Sobrien Foundation, Inc., 59 Temple Place - Suite 330, 2198944Sobrien Boston, MA 02111-1307, USA. */ 2219370Spst 2319370Spst#include "defs.h" 24130803Smarcel#include "gdb_obstack.h" 2519370Spst#include "bfd.h" /* Binary File Description */ 2619370Spst#include "symtab.h" 2719370Spst#include "gdbtypes.h" 2819370Spst#include "expression.h" 2919370Spst#include "value.h" 3019370Spst#include "gdbcore.h" 3119370Spst#include "command.h" 3219370Spst#include "gdbcmd.h" 3319370Spst#include "target.h" 3419370Spst#include "language.h" 3598944Sobrien#include "cp-abi.h" 36130803Smarcel#include "typeprint.h" 3719370Spst#include "gdb_string.h" 3819370Spst#include <errno.h> 3919370Spst 4046283Sdfr/* For real-type printing in whatis_exp() */ 4146283Sdfrextern int objectprint; /* Controls looking up an object's derived type 4246283Sdfr using what we find in its vtables. */ 4346283Sdfr 4498944Sobrienextern void _initialize_typeprint (void); 4519370Spst 4698944Sobrienstatic void ptype_command (char *, int); 4719370Spst 4898944Sobrienstatic struct type *ptype_eval (struct expression *); 4919370Spst 5098944Sobrienstatic void whatis_command (char *, int); 5119370Spst 5298944Sobrienstatic void whatis_exp (char *, int); 5398944Sobrien 5498944Sobrien/* Print a description of a type in the format of a 5598944Sobrien typedef for the current language. 5698944Sobrien NEW is the new name for a type TYPE. */ 5798944Sobrien 5898944Sobrienvoid 5998944Sobrientypedef_print (struct type *type, struct symbol *new, struct ui_file *stream) 6098944Sobrien{ 6198944Sobrien CHECK_TYPEDEF (type); 6298944Sobrien switch (current_language->la_language) 6398944Sobrien { 6498944Sobrien#ifdef _LANG_c 6598944Sobrien case language_c: 6698944Sobrien case language_cplus: 6798944Sobrien fprintf_filtered (stream, "typedef "); 6898944Sobrien type_print (type, "", stream, 0); 6998944Sobrien if (TYPE_NAME ((SYMBOL_TYPE (new))) == 0 70130803Smarcel || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0) 71130803Smarcel fprintf_filtered (stream, " %s", SYMBOL_PRINT_NAME (new)); 7298944Sobrien break; 7398944Sobrien#endif 7498944Sobrien#ifdef _LANG_m2 7598944Sobrien case language_m2: 7698944Sobrien fprintf_filtered (stream, "TYPE "); 77130803Smarcel if (!TYPE_NAME (SYMBOL_TYPE (new)) 78130803Smarcel || strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), DEPRECATED_SYMBOL_NAME (new)) != 0) 79130803Smarcel fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new)); 8098944Sobrien else 8198944Sobrien fprintf_filtered (stream, "<builtin> = "); 8298944Sobrien type_print (type, "", stream, 0); 8398944Sobrien break; 8498944Sobrien#endif 8598944Sobrien#ifdef _LANG_pascal 8698944Sobrien case language_pascal: 8798944Sobrien fprintf_filtered (stream, "type "); 88130803Smarcel fprintf_filtered (stream, "%s = ", SYMBOL_PRINT_NAME (new)); 8998944Sobrien type_print (type, "", stream, 0); 9098944Sobrien break; 9198944Sobrien#endif 9298944Sobrien default: 9398944Sobrien error ("Language not supported."); 9498944Sobrien } 9598944Sobrien fprintf_filtered (stream, ";\n"); 9698944Sobrien} 9798944Sobrien 9819370Spst/* Print a description of a type TYPE in the form of a declaration of a 9919370Spst variable named VARSTRING. (VARSTRING is demangled if necessary.) 10019370Spst Output goes to STREAM (via stdio). 10119370Spst If SHOW is positive, we show the contents of the outermost level 10219370Spst of structure even if there is a type name that could be used instead. 10319370Spst If SHOW is negative, we never show the details of elements' types. */ 10419370Spst 10519370Spstvoid 10698944Sobrientype_print (struct type *type, char *varstring, struct ui_file *stream, 10798944Sobrien int show) 10819370Spst{ 10919370Spst LA_PRINT_TYPE (type, varstring, stream, show, 0); 11019370Spst} 11119370Spst 11219370Spst/* Print type of EXP, or last thing in value history if EXP == NULL. 11319370Spst show is passed to type_print. */ 11419370Spst 11519370Spststatic void 11698944Sobrienwhatis_exp (char *exp, int show) 11719370Spst{ 11819370Spst struct expression *expr; 11998944Sobrien struct value *val; 120130803Smarcel struct cleanup *old_chain = NULL; 12198944Sobrien struct type *real_type = NULL; 12298944Sobrien struct type *type; 12346283Sdfr int full = 0; 12446283Sdfr int top = -1; 12546283Sdfr int using_enc = 0; 12619370Spst 12719370Spst if (exp) 12819370Spst { 12919370Spst expr = parse_expression (exp); 13098944Sobrien old_chain = make_cleanup (free_current_contents, &expr); 13119370Spst val = evaluate_type (expr); 13219370Spst } 13319370Spst else 13419370Spst val = access_value_history (0); 13519370Spst 13698944Sobrien type = VALUE_TYPE (val); 13798944Sobrien 13898944Sobrien if (objectprint) 13998944Sobrien { 14098944Sobrien if (((TYPE_CODE (type) == TYPE_CODE_PTR) || 14198944Sobrien (TYPE_CODE (type) == TYPE_CODE_REF)) 14298944Sobrien && 14398944Sobrien (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) 14498944Sobrien { 14598944Sobrien real_type = value_rtti_target_type (val, &full, &top, &using_enc); 14698944Sobrien if (real_type) 14798944Sobrien { 14898944Sobrien if (TYPE_CODE (type) == TYPE_CODE_PTR) 14998944Sobrien real_type = lookup_pointer_type (real_type); 15098944Sobrien else 15198944Sobrien real_type = lookup_reference_type (real_type); 15298944Sobrien } 15398944Sobrien } 15498944Sobrien else if (TYPE_CODE (type) == TYPE_CODE_CLASS) 15546283Sdfr real_type = value_rtti_type (val, &full, &top, &using_enc); 15698944Sobrien } 15746283Sdfr 15819370Spst printf_filtered ("type = "); 15946283Sdfr 16098944Sobrien if (real_type) 16198944Sobrien { 16298944Sobrien printf_filtered ("/* real type = "); 16398944Sobrien type_print (real_type, "", gdb_stdout, -1); 16498944Sobrien if (! full) 16598944Sobrien printf_filtered (" (incomplete object)"); 16698944Sobrien printf_filtered (" */\n"); 16798944Sobrien } 16846283Sdfr 16998944Sobrien type_print (type, "", gdb_stdout, show); 17019370Spst printf_filtered ("\n"); 17119370Spst 17219370Spst if (exp) 17319370Spst do_cleanups (old_chain); 17419370Spst} 17519370Spst 17619370Spststatic void 17798944Sobrienwhatis_command (char *exp, int from_tty) 17819370Spst{ 17919370Spst /* Most of the time users do not want to see all the fields 18019370Spst in a structure. If they do they can use the "ptype" command. 18119370Spst Hence the "-1" below. */ 18219370Spst whatis_exp (exp, -1); 18319370Spst} 18419370Spst 18519370Spst/* Simple subroutine for ptype_command. */ 18619370Spst 18719370Spststatic struct type * 18898944Sobrienptype_eval (struct expression *exp) 18919370Spst{ 19019370Spst if (exp->elts[0].opcode == OP_TYPE) 19119370Spst { 19219370Spst return (exp->elts[1].type); 19319370Spst } 19419370Spst else 19519370Spst { 19619370Spst return (NULL); 19719370Spst } 19819370Spst} 19919370Spst 20019370Spst/* TYPENAME is either the name of a type, or an expression. */ 20119370Spst 20219370Spststatic void 20398944Sobrienptype_command (char *typename, int from_tty) 20419370Spst{ 205130803Smarcel struct type *type; 20619370Spst struct expression *expr; 207130803Smarcel struct cleanup *old_chain; 20819370Spst 20919370Spst if (typename == NULL) 21019370Spst { 21119370Spst /* Print type of last thing in value history. */ 21219370Spst whatis_exp (typename, 1); 21319370Spst } 21419370Spst else 21519370Spst { 21619370Spst expr = parse_expression (typename); 21798944Sobrien old_chain = make_cleanup (free_current_contents, &expr); 21819370Spst type = ptype_eval (expr); 21919370Spst if (type != NULL) 22019370Spst { 22119370Spst /* User did "ptype <typename>" */ 22219370Spst printf_filtered ("type = "); 22319370Spst type_print (type, "", gdb_stdout, 1); 22419370Spst printf_filtered ("\n"); 22519370Spst do_cleanups (old_chain); 22619370Spst } 22719370Spst else 22819370Spst { 22919370Spst /* User did "ptype <symbolname>" */ 23019370Spst do_cleanups (old_chain); 23119370Spst whatis_exp (typename, 1); 23219370Spst } 23319370Spst } 23419370Spst} 23519370Spst 23619370Spst/* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM. 23719370Spst Used to print data from type structures in a specified type. For example, 23819370Spst array bounds may be characters or booleans in some languages, and this 23919370Spst allows the ranges to be printed in their "natural" form rather than as 24019370Spst decimal integer values. 24119370Spst 24219370Spst FIXME: This is here simply because only the type printing routines 24319370Spst currently use it, and it wasn't clear if it really belonged somewhere 24419370Spst else (like printcmd.c). There are a lot of other gdb routines that do 24519370Spst something similar, but they are generally concerned with printing values 24619370Spst that come from the inferior in target byte order and target size. */ 24719370Spst 24819370Spstvoid 24998944Sobrienprint_type_scalar (struct type *type, LONGEST val, struct ui_file *stream) 25019370Spst{ 25119370Spst unsigned int i; 25219370Spst unsigned len; 25319370Spst 25419370Spst CHECK_TYPEDEF (type); 25519370Spst 25619370Spst switch (TYPE_CODE (type)) 25719370Spst { 25819370Spst 25919370Spst case TYPE_CODE_ENUM: 26019370Spst len = TYPE_NFIELDS (type); 26119370Spst for (i = 0; i < len; i++) 26219370Spst { 26319370Spst if (TYPE_FIELD_BITPOS (type, i) == val) 26419370Spst { 26519370Spst break; 26619370Spst } 26719370Spst } 26819370Spst if (i < len) 26919370Spst { 27019370Spst fputs_filtered (TYPE_FIELD_NAME (type, i), stream); 27119370Spst } 27219370Spst else 27319370Spst { 27419370Spst print_longest (stream, 'd', 0, val); 27519370Spst } 27619370Spst break; 27719370Spst 27819370Spst case TYPE_CODE_INT: 27919370Spst print_longest (stream, TYPE_UNSIGNED (type) ? 'u' : 'd', 0, val); 28019370Spst break; 28119370Spst 28219370Spst case TYPE_CODE_CHAR: 28319370Spst LA_PRINT_CHAR ((unsigned char) val, stream); 28419370Spst break; 28519370Spst 28619370Spst case TYPE_CODE_BOOL: 28719370Spst fprintf_filtered (stream, val ? "TRUE" : "FALSE"); 28819370Spst break; 28919370Spst 29019370Spst case TYPE_CODE_RANGE: 29119370Spst print_type_scalar (TYPE_TARGET_TYPE (type), val, stream); 29219370Spst return; 29319370Spst 29419370Spst case TYPE_CODE_UNDEF: 29519370Spst case TYPE_CODE_PTR: 29619370Spst case TYPE_CODE_ARRAY: 29719370Spst case TYPE_CODE_STRUCT: 29819370Spst case TYPE_CODE_UNION: 29919370Spst case TYPE_CODE_FUNC: 30019370Spst case TYPE_CODE_FLT: 30119370Spst case TYPE_CODE_VOID: 30219370Spst case TYPE_CODE_SET: 30319370Spst case TYPE_CODE_STRING: 30419370Spst case TYPE_CODE_ERROR: 30519370Spst case TYPE_CODE_MEMBER: 30619370Spst case TYPE_CODE_METHOD: 30719370Spst case TYPE_CODE_REF: 308130803Smarcel case TYPE_CODE_NAMESPACE: 30919370Spst error ("internal error: unhandled type in print_type_scalar"); 31019370Spst break; 31119370Spst 31219370Spst default: 31319370Spst error ("Invalid type code in symbol table."); 31419370Spst } 31519370Spst gdb_flush (stream); 31619370Spst} 31719370Spst 31819370Spst/* Dump details of a type specified either directly or indirectly. 31919370Spst Uses the same sort of type lookup mechanism as ptype_command() 32019370Spst and whatis_command(). */ 32119370Spst 32219370Spstvoid 32398944Sobrienmaintenance_print_type (char *typename, int from_tty) 32419370Spst{ 32598944Sobrien struct value *val; 326130803Smarcel struct type *type; 327130803Smarcel struct cleanup *old_chain; 32819370Spst struct expression *expr; 32919370Spst 33019370Spst if (typename != NULL) 33198944Sobrien { 33298944Sobrien expr = parse_expression (typename); 33398944Sobrien old_chain = make_cleanup (free_current_contents, &expr); 33498944Sobrien if (expr->elts[0].opcode == OP_TYPE) 33598944Sobrien { 33698944Sobrien /* The user expression names a type directly, just use that type. */ 33798944Sobrien type = expr->elts[1].type; 33898944Sobrien } 33998944Sobrien else 34098944Sobrien { 34198944Sobrien /* The user expression may name a type indirectly by naming an 34298944Sobrien object of that type. Find that indirectly named type. */ 34398944Sobrien val = evaluate_type (expr); 34498944Sobrien type = VALUE_TYPE (val); 34598944Sobrien } 34698944Sobrien if (type != NULL) 34798944Sobrien { 34898944Sobrien recursive_dump_type (type, 0); 34998944Sobrien } 35098944Sobrien do_cleanups (old_chain); 35198944Sobrien } 35219370Spst} 35398944Sobrien 35419370Spst 35519370Spstvoid 35698944Sobrien_initialize_typeprint (void) 35719370Spst{ 35819370Spst 35919370Spst add_com ("ptype", class_vars, ptype_command, 36019370Spst "Print definition of type TYPE.\n\ 36119370SpstArgument may be a type name defined by typedef, or \"struct STRUCT-TAG\"\n\ 36219370Spstor \"class CLASS-NAME\" or \"union UNION-TAG\" or \"enum ENUM-TAG\".\n\ 36319370SpstThe selected stack frame's lexical context is used to look up the name."); 36419370Spst 36519370Spst add_com ("whatis", class_vars, whatis_command, 36619370Spst "Print data type of expression EXP."); 36719370Spst 36819370Spst} 369