| 1 | /* C language support routines for GDB, the GNU debugger. |
| 2 | Copyright 1992, 1993 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 "symtab.h" |
| 22 | #include "gdbtypes.h" |
| 23 | #include "expression.h" |
| 24 | #include "parser-defs.h" |
| 25 | #include "language.h" |
| 26 | #include "c-lang.h" |
| 27 | |
| 28 | /* Print the character C on STREAM as part of the contents of a literal |
| 29 | string whose delimiter is QUOTER. Note that that format for printing |
| 30 | characters and strings is language specific. */ |
| 31 | |
| 32 | static void |
| 33 | emit_char (c, stream, quoter) |
| 34 | register int c; |
| 35 | FILE *stream; |
| 36 | int quoter; |
| 37 | { |
| 38 | |
| 39 | c &= 0xFF; /* Avoid sign bit follies */ |
| 40 | |
| 41 | if (PRINT_LITERAL_FORM (c)) |
| 42 | { |
| 43 | if (c == '\\' || c == quoter) |
| 44 | { |
| 45 | fputs_filtered ("\\", stream); |
| 46 | } |
| 47 | fprintf_filtered (stream, "%c", c); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | switch (c) |
| 52 | { |
| 53 | case '\n': |
| 54 | fputs_filtered ("\\n", stream); |
| 55 | break; |
| 56 | case '\b': |
| 57 | fputs_filtered ("\\b", stream); |
| 58 | break; |
| 59 | case '\t': |
| 60 | fputs_filtered ("\\t", stream); |
| 61 | break; |
| 62 | case '\f': |
| 63 | fputs_filtered ("\\f", stream); |
| 64 | break; |
| 65 | case '\r': |
| 66 | fputs_filtered ("\\r", stream); |
| 67 | break; |
| 68 | case '\033': |
| 69 | fputs_filtered ("\\e", stream); |
| 70 | break; |
| 71 | case '\007': |
| 72 | fputs_filtered ("\\a", stream); |
| 73 | break; |
| 74 | default: |
| 75 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | c_printchar (c, stream) |
| 83 | int c; |
| 84 | FILE *stream; |
| 85 | { |
| 86 | fputs_filtered ("'", stream); |
| 87 | emit_char (c, stream, '\''); |
| 88 | fputs_filtered ("'", stream); |
| 89 | } |
| 90 | |
| 91 | /* Print the character string STRING, printing at most LENGTH characters. |
| 92 | Printing stops early if the number hits print_max; repeat counts |
| 93 | are printed as appropriate. Print ellipses at the end if we |
| 94 | had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */ |
| 95 | |
| 96 | static void |
| 97 | c_printstr (stream, string, length, force_ellipses) |
| 98 | FILE *stream; |
| 99 | char *string; |
| 100 | unsigned int length; |
| 101 | int force_ellipses; |
| 102 | { |
| 103 | register unsigned int i; |
| 104 | unsigned int things_printed = 0; |
| 105 | int in_quotes = 0; |
| 106 | int need_comma = 0; |
| 107 | extern int inspect_it; |
| 108 | extern int repeat_count_threshold; |
| 109 | extern int print_max; |
| 110 | |
| 111 | /* If the string was not truncated due to `set print elements', and |
| 112 | the last byte of it is a null, we don't print that, in traditional C |
| 113 | style. */ |
| 114 | if ((!force_ellipses) && length > 0 && string[length-1] == '\0') |
| 115 | length--; |
| 116 | |
| 117 | if (length == 0) |
| 118 | { |
| 119 | fputs_filtered ("\"\"", stdout); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | for (i = 0; i < length && things_printed < print_max; ++i) |
| 124 | { |
| 125 | /* Position of the character we are examining |
| 126 | to see whether it is repeated. */ |
| 127 | unsigned int rep1; |
| 128 | /* Number of repetitions we have detected so far. */ |
| 129 | unsigned int reps; |
| 130 | |
| 131 | QUIT; |
| 132 | |
| 133 | if (need_comma) |
| 134 | { |
| 135 | fputs_filtered (", ", stream); |
| 136 | need_comma = 0; |
| 137 | } |
| 138 | |
| 139 | rep1 = i + 1; |
| 140 | reps = 1; |
| 141 | while (rep1 < length && string[rep1] == string[i]) |
| 142 | { |
| 143 | ++rep1; |
| 144 | ++reps; |
| 145 | } |
| 146 | |
| 147 | if (reps > repeat_count_threshold) |
| 148 | { |
| 149 | if (in_quotes) |
| 150 | { |
| 151 | if (inspect_it) |
| 152 | fputs_filtered ("\\\", ", stream); |
| 153 | else |
| 154 | fputs_filtered ("\", ", stream); |
| 155 | in_quotes = 0; |
| 156 | } |
| 157 | c_printchar (string[i], stream); |
| 158 | fprintf_filtered (stream, " <repeats %u times>", reps); |
| 159 | i = rep1 - 1; |
| 160 | things_printed += repeat_count_threshold; |
| 161 | need_comma = 1; |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | if (!in_quotes) |
| 166 | { |
| 167 | if (inspect_it) |
| 168 | fputs_filtered ("\\\"", stream); |
| 169 | else |
| 170 | fputs_filtered ("\"", stream); |
| 171 | in_quotes = 1; |
| 172 | } |
| 173 | emit_char (string[i], stream, '"'); |
| 174 | ++things_printed; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* Terminate the quotes if necessary. */ |
| 179 | if (in_quotes) |
| 180 | { |
| 181 | if (inspect_it) |
| 182 | fputs_filtered ("\\\"", stream); |
| 183 | else |
| 184 | fputs_filtered ("\"", stream); |
| 185 | } |
| 186 | |
| 187 | if (force_ellipses || i < length) |
| 188 | fputs_filtered ("...", stream); |
| 189 | } |
| 190 | |
| 191 | /* Create a fundamental C type using default reasonable for the current |
| 192 | target machine. |
| 193 | |
| 194 | Some object/debugging file formats (DWARF version 1, COFF, etc) do not |
| 195 | define fundamental types such as "int" or "double". Others (stabs or |
| 196 | DWARF version 2, etc) do define fundamental types. For the formats which |
| 197 | don't provide fundamental types, gdb can create such types using this |
| 198 | function. |
| 199 | |
| 200 | FIXME: Some compilers distinguish explicitly signed integral types |
| 201 | (signed short, signed int, signed long) from "regular" integral types |
| 202 | (short, int, long) in the debugging information. There is some dis- |
| 203 | agreement as to how useful this feature is. In particular, gcc does |
| 204 | not support this. Also, only some debugging formats allow the |
| 205 | distinction to be passed on to a debugger. For now, we always just |
| 206 | use "short", "int", or "long" as the type name, for both the implicit |
| 207 | and explicitly signed types. This also makes life easier for the |
| 208 | gdb test suite since we don't have to account for the differences |
| 209 | in output depending upon what the compiler and debugging format |
| 210 | support. We will probably have to re-examine the issue when gdb |
| 211 | starts taking it's fundamental type information directly from the |
| 212 | debugging information supplied by the compiler. fnf@cygnus.com */ |
| 213 | |
| 214 | static struct type * |
| 215 | c_create_fundamental_type (objfile, typeid) |
| 216 | struct objfile *objfile; |
| 217 | int typeid; |
| 218 | { |
| 219 | register struct type *type = NULL; |
| 220 | |
| 221 | switch (typeid) |
| 222 | { |
| 223 | default: |
| 224 | /* FIXME: For now, if we are asked to produce a type not in this |
| 225 | language, create the equivalent of a C integer type with the |
| 226 | name "<?type?>". When all the dust settles from the type |
| 227 | reconstruction work, this should probably become an error. */ |
| 228 | type = init_type (TYPE_CODE_INT, |
| 229 | TARGET_INT_BIT / TARGET_CHAR_BIT, |
| 230 | 0, "<?type?>", objfile); |
| 231 | warning ("internal error: no C/C++ fundamental type %d", typeid); |
| 232 | break; |
| 233 | case FT_VOID: |
| 234 | type = init_type (TYPE_CODE_VOID, |
| 235 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, |
| 236 | 0, "void", objfile); |
| 237 | break; |
| 238 | case FT_CHAR: |
| 239 | type = init_type (TYPE_CODE_INT, |
| 240 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, |
| 241 | 0, "char", objfile); |
| 242 | break; |
| 243 | case FT_SIGNED_CHAR: |
| 244 | type = init_type (TYPE_CODE_INT, |
| 245 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, |
| 246 | TYPE_FLAG_SIGNED, "signed char", objfile); |
| 247 | break; |
| 248 | case FT_UNSIGNED_CHAR: |
| 249 | type = init_type (TYPE_CODE_INT, |
| 250 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, |
| 251 | TYPE_FLAG_UNSIGNED, "unsigned char", objfile); |
| 252 | break; |
| 253 | case FT_SHORT: |
| 254 | type = init_type (TYPE_CODE_INT, |
| 255 | TARGET_SHORT_BIT / TARGET_CHAR_BIT, |
| 256 | 0, "short", objfile); |
| 257 | break; |
| 258 | case FT_SIGNED_SHORT: |
| 259 | type = init_type (TYPE_CODE_INT, |
| 260 | TARGET_SHORT_BIT / TARGET_CHAR_BIT, |
| 261 | TYPE_FLAG_SIGNED, "short", objfile); /* FIXME-fnf */ |
| 262 | break; |
| 263 | case FT_UNSIGNED_SHORT: |
| 264 | type = init_type (TYPE_CODE_INT, |
| 265 | TARGET_SHORT_BIT / TARGET_CHAR_BIT, |
| 266 | TYPE_FLAG_UNSIGNED, "unsigned short", objfile); |
| 267 | break; |
| 268 | case FT_INTEGER: |
| 269 | type = init_type (TYPE_CODE_INT, |
| 270 | TARGET_INT_BIT / TARGET_CHAR_BIT, |
| 271 | 0, "int", objfile); |
| 272 | break; |
| 273 | case FT_SIGNED_INTEGER: |
| 274 | type = init_type (TYPE_CODE_INT, |
| 275 | TARGET_INT_BIT / TARGET_CHAR_BIT, |
| 276 | TYPE_FLAG_SIGNED, "int", objfile); /* FIXME -fnf */ |
| 277 | break; |
| 278 | case FT_UNSIGNED_INTEGER: |
| 279 | type = init_type (TYPE_CODE_INT, |
| 280 | TARGET_INT_BIT / TARGET_CHAR_BIT, |
| 281 | TYPE_FLAG_UNSIGNED, "unsigned int", objfile); |
| 282 | break; |
| 283 | case FT_LONG: |
| 284 | type = init_type (TYPE_CODE_INT, |
| 285 | TARGET_LONG_BIT / TARGET_CHAR_BIT, |
| 286 | 0, "long", objfile); |
| 287 | break; |
| 288 | case FT_SIGNED_LONG: |
| 289 | type = init_type (TYPE_CODE_INT, |
| 290 | TARGET_LONG_BIT / TARGET_CHAR_BIT, |
| 291 | TYPE_FLAG_SIGNED, "long", objfile); /* FIXME -fnf */ |
| 292 | break; |
| 293 | case FT_UNSIGNED_LONG: |
| 294 | type = init_type (TYPE_CODE_INT, |
| 295 | TARGET_LONG_BIT / TARGET_CHAR_BIT, |
| 296 | TYPE_FLAG_UNSIGNED, "unsigned long", objfile); |
| 297 | break; |
| 298 | case FT_LONG_LONG: |
| 299 | type = init_type (TYPE_CODE_INT, |
| 300 | TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, |
| 301 | 0, "long long", objfile); |
| 302 | break; |
| 303 | case FT_SIGNED_LONG_LONG: |
| 304 | type = init_type (TYPE_CODE_INT, |
| 305 | TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, |
| 306 | TYPE_FLAG_SIGNED, "signed long long", objfile); |
| 307 | break; |
| 308 | case FT_UNSIGNED_LONG_LONG: |
| 309 | type = init_type (TYPE_CODE_INT, |
| 310 | TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, |
| 311 | TYPE_FLAG_UNSIGNED, "unsigned long long", objfile); |
| 312 | break; |
| 313 | case FT_FLOAT: |
| 314 | type = init_type (TYPE_CODE_FLT, |
| 315 | TARGET_FLOAT_BIT / TARGET_CHAR_BIT, |
| 316 | 0, "float", objfile); |
| 317 | break; |
| 318 | case FT_DBL_PREC_FLOAT: |
| 319 | type = init_type (TYPE_CODE_FLT, |
| 320 | TARGET_DOUBLE_BIT / TARGET_CHAR_BIT, |
| 321 | 0, "double", objfile); |
| 322 | break; |
| 323 | case FT_EXT_PREC_FLOAT: |
| 324 | type = init_type (TYPE_CODE_FLT, |
| 325 | TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT, |
| 326 | 0, "long double", objfile); |
| 327 | break; |
| 328 | } |
| 329 | return (type); |
| 330 | } |
| 331 | |
| 332 | \f |
| 333 | /* Table mapping opcodes into strings for printing operators |
| 334 | and precedences of the operators. */ |
| 335 | |
| 336 | static const struct op_print c_op_print_tab[] = |
| 337 | { |
| 338 | {",", BINOP_COMMA, PREC_COMMA, 0}, |
| 339 | {"=", BINOP_ASSIGN, PREC_ASSIGN, 1}, |
| 340 | {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0}, |
| 341 | {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0}, |
| 342 | {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0}, |
| 343 | {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0}, |
| 344 | {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0}, |
| 345 | {"==", BINOP_EQUAL, PREC_EQUAL, 0}, |
| 346 | {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0}, |
| 347 | {"<=", BINOP_LEQ, PREC_ORDER, 0}, |
| 348 | {">=", BINOP_GEQ, PREC_ORDER, 0}, |
| 349 | {">", BINOP_GTR, PREC_ORDER, 0}, |
| 350 | {"<", BINOP_LESS, PREC_ORDER, 0}, |
| 351 | {">>", BINOP_RSH, PREC_SHIFT, 0}, |
| 352 | {"<<", BINOP_LSH, PREC_SHIFT, 0}, |
| 353 | {"+", BINOP_ADD, PREC_ADD, 0}, |
| 354 | {"-", BINOP_SUB, PREC_ADD, 0}, |
| 355 | {"*", BINOP_MUL, PREC_MUL, 0}, |
| 356 | {"/", BINOP_DIV, PREC_MUL, 0}, |
| 357 | {"%", BINOP_REM, PREC_MUL, 0}, |
| 358 | {"@", BINOP_REPEAT, PREC_REPEAT, 0}, |
| 359 | {"-", UNOP_NEG, PREC_PREFIX, 0}, |
| 360 | {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0}, |
| 361 | {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0}, |
| 362 | {"*", UNOP_IND, PREC_PREFIX, 0}, |
| 363 | {"&", UNOP_ADDR, PREC_PREFIX, 0}, |
| 364 | {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0}, |
| 365 | {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0}, |
| 366 | {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0}, |
| 367 | /* C++ */ |
| 368 | {"::", BINOP_SCOPE, PREC_PREFIX, 0}, |
| 369 | {NULL, 0, 0, 0} |
| 370 | }; |
| 371 | \f |
| 372 | struct type ** const (c_builtin_types[]) = |
| 373 | { |
| 374 | &builtin_type_int, |
| 375 | &builtin_type_long, |
| 376 | &builtin_type_short, |
| 377 | &builtin_type_char, |
| 378 | &builtin_type_float, |
| 379 | &builtin_type_double, |
| 380 | &builtin_type_void, |
| 381 | &builtin_type_long_long, |
| 382 | &builtin_type_signed_char, |
| 383 | &builtin_type_unsigned_char, |
| 384 | &builtin_type_unsigned_short, |
| 385 | &builtin_type_unsigned_int, |
| 386 | &builtin_type_unsigned_long, |
| 387 | &builtin_type_unsigned_long_long, |
| 388 | &builtin_type_long_double, |
| 389 | &builtin_type_complex, |
| 390 | &builtin_type_double_complex, |
| 391 | 0 |
| 392 | }; |
| 393 | |
| 394 | const struct language_defn c_language_defn = { |
| 395 | "c", /* Language name */ |
| 396 | language_c, |
| 397 | c_builtin_types, |
| 398 | range_check_off, |
| 399 | type_check_off, |
| 400 | c_parse, |
| 401 | c_error, |
| 402 | c_printchar, /* Print a character constant */ |
| 403 | c_printstr, /* Function to print string constant */ |
| 404 | c_create_fundamental_type, /* Create fundamental type in this language */ |
| 405 | c_print_type, /* Print a type using appropriate syntax */ |
| 406 | c_val_print, /* Print a value using appropriate syntax */ |
| 407 | &BUILTIN_TYPE_LONGEST, /* longest signed integral type */ |
| 408 | &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */ |
| 409 | &builtin_type_double, /* longest floating point type */ /*FIXME*/ |
| 410 | {"", "", "", ""}, /* Binary format info */ |
| 411 | {"0%o", "0", "o", ""}, /* Octal format info */ |
| 412 | {"%d", "", "d", ""}, /* Decimal format info */ |
| 413 | {"0x%x", "0x", "x", ""}, /* Hex format info */ |
| 414 | c_op_print_tab, /* expression operators for printing */ |
| 415 | LANG_MAGIC |
| 416 | }; |
| 417 | |
| 418 | const struct language_defn cplus_language_defn = { |
| 419 | "c++", /* Language name */ |
| 420 | language_cplus, |
| 421 | c_builtin_types, |
| 422 | range_check_off, |
| 423 | type_check_off, |
| 424 | c_parse, |
| 425 | c_error, |
| 426 | c_printchar, /* Print a character constant */ |
| 427 | c_printstr, /* Function to print string constant */ |
| 428 | c_create_fundamental_type, /* Create fundamental type in this language */ |
| 429 | c_print_type, /* Print a type using appropriate syntax */ |
| 430 | c_val_print, /* Print a value using appropriate syntax */ |
| 431 | &BUILTIN_TYPE_LONGEST, /* longest signed integral type */ |
| 432 | &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */ |
| 433 | &builtin_type_double, /* longest floating point type */ /*FIXME*/ |
| 434 | {"", "", "", ""}, /* Binary format info */ |
| 435 | {"0%o", "0", "o", ""}, /* Octal format info */ |
| 436 | {"%d", "", "d", ""}, /* Decimal format info */ |
| 437 | {"0x%x", "0x", "x", ""}, /* Hex format info */ |
| 438 | c_op_print_tab, /* expression operators for printing */ |
| 439 | LANG_MAGIC |
| 440 | }; |
| 441 | |
| 442 | void |
| 443 | _initialize_c_language () |
| 444 | { |
| 445 | add_language (&c_language_defn); |
| 446 | add_language (&cplus_language_defn); |
| 447 | } |