1 /* C language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
23 #include "expression.h"
24 #include "parser-defs.h"
28 static void c_emit_char
PARAMS ((int c
, GDB_FILE
*stream
, int quoter
));
30 /* Print the character C on STREAM as part of the contents of a literal
31 string whose delimiter is QUOTER. Note that that format for printing
32 characters and strings is language specific. */
35 c_emit_char (c
, stream
, quoter
)
40 c
&= 0xFF; /* Avoid sign bit follies */
42 if (PRINT_LITERAL_FORM (c
))
44 if (c
== '\\' || c
== quoter
)
46 fputs_filtered ("\\", stream
);
48 fprintf_filtered (stream
, "%c", c
);
55 fputs_filtered ("\\n", stream
);
58 fputs_filtered ("\\b", stream
);
61 fputs_filtered ("\\t", stream
);
64 fputs_filtered ("\\f", stream
);
67 fputs_filtered ("\\r", stream
);
70 fputs_filtered ("\\e", stream
);
73 fputs_filtered ("\\a", stream
);
76 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
83 c_printchar (c
, stream
)
87 fputc_filtered ('\'', stream
);
88 LA_EMIT_CHAR (c
, stream
, '\'');
89 fputc_filtered ('\'', stream
);
92 /* Print the character string STRING, printing at most LENGTH characters.
93 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
94 long. Printing stops early if the number hits print_max; repeat counts are
95 printed as appropriate. Print ellipses at the end if we had to stop before
96 printing LENGTH characters, or if FORCE_ELLIPSES. */
99 c_printstr (stream
, string
, length
, width
, force_ellipses
)
106 register unsigned int i
;
107 unsigned int things_printed
= 0;
110 extern int inspect_it
;
111 extern int repeat_count_threshold
;
112 extern int print_max
;
114 /* If the string was not truncated due to `set print elements', and
115 the last byte of it is a null, we don't print that, in traditional C
119 && extract_unsigned_integer (string
+ (length
- 1) * width
, width
) == '\0')
124 fputs_filtered ("\"\"", stream
);
128 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
130 /* Position of the character we are examining
131 to see whether it is repeated. */
133 /* Number of repetitions we have detected so far. */
135 unsigned long current_char
;
141 fputs_filtered (", ", stream
);
145 current_char
= extract_unsigned_integer (string
+ i
* width
, width
);
150 && extract_unsigned_integer (string
+ rep1
* width
, width
)
157 if (reps
> repeat_count_threshold
)
162 fputs_filtered ("\\\", ", stream
);
164 fputs_filtered ("\", ", stream
);
167 LA_PRINT_CHAR (current_char
, stream
);
168 fprintf_filtered (stream
, " <repeats %u times>", reps
);
170 things_printed
+= repeat_count_threshold
;
178 fputs_filtered ("\\\"", stream
);
180 fputs_filtered ("\"", stream
);
183 LA_EMIT_CHAR (current_char
, stream
, '"');
188 /* Terminate the quotes if necessary. */
192 fputs_filtered ("\\\"", stream
);
194 fputs_filtered ("\"", stream
);
197 if (force_ellipses
|| i
< length
)
198 fputs_filtered ("...", stream
);
201 /* Create a fundamental C type using default reasonable for the current
204 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
205 define fundamental types such as "int" or "double". Others (stabs or
206 DWARF version 2, etc) do define fundamental types. For the formats which
207 don't provide fundamental types, gdb can create such types using this
210 FIXME: Some compilers distinguish explicitly signed integral types
211 (signed short, signed int, signed long) from "regular" integral types
212 (short, int, long) in the debugging information. There is some dis-
213 agreement as to how useful this feature is. In particular, gcc does
214 not support this. Also, only some debugging formats allow the
215 distinction to be passed on to a debugger. For now, we always just
216 use "short", "int", or "long" as the type name, for both the implicit
217 and explicitly signed types. This also makes life easier for the
218 gdb test suite since we don't have to account for the differences
219 in output depending upon what the compiler and debugging format
220 support. We will probably have to re-examine the issue when gdb
221 starts taking it's fundamental type information directly from the
222 debugging information supplied by the compiler. fnf@cygnus.com */
225 c_create_fundamental_type (objfile
, typeid)
226 struct objfile
*objfile
;
229 register struct type
*type
= NULL
;
234 /* FIXME: For now, if we are asked to produce a type not in this
235 language, create the equivalent of a C integer type with the
236 name "<?type?>". When all the dust settles from the type
237 reconstruction work, this should probably become an error. */
238 type
= init_type (TYPE_CODE_INT
,
239 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
240 0, "<?type?>", objfile
);
241 warning ("internal error: no C/C++ fundamental type %d", typeid);
244 type
= init_type (TYPE_CODE_VOID
,
245 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
249 type
= init_type (TYPE_CODE_BOOL
,
250 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
255 type
= init_type (TYPE_CODE_INT
,
256 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
258 TYPE_FLAGS (type
) |= TYPE_FLAG_NOSIGN
;
261 type
= init_type (TYPE_CODE_INT
,
262 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
263 0, "signed char", objfile
);
265 case FT_UNSIGNED_CHAR
:
266 type
= init_type (TYPE_CODE_INT
,
267 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
268 TYPE_FLAG_UNSIGNED
, "unsigned char", objfile
);
271 type
= init_type (TYPE_CODE_INT
,
272 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
273 0, "short", objfile
);
275 case FT_SIGNED_SHORT
:
276 type
= init_type (TYPE_CODE_INT
,
277 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
278 0, "short", objfile
); /* FIXME-fnf */
280 case FT_UNSIGNED_SHORT
:
281 type
= init_type (TYPE_CODE_INT
,
282 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
283 TYPE_FLAG_UNSIGNED
, "unsigned short", objfile
);
286 type
= init_type (TYPE_CODE_INT
,
287 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
290 case FT_SIGNED_INTEGER
:
291 type
= init_type (TYPE_CODE_INT
,
292 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
293 0, "int", objfile
); /* FIXME -fnf */
295 case FT_UNSIGNED_INTEGER
:
296 type
= init_type (TYPE_CODE_INT
,
297 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
298 TYPE_FLAG_UNSIGNED
, "unsigned int", objfile
);
301 type
= init_type (TYPE_CODE_INT
,
302 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
306 type
= init_type (TYPE_CODE_INT
,
307 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
308 0, "long", objfile
); /* FIXME -fnf */
310 case FT_UNSIGNED_LONG
:
311 type
= init_type (TYPE_CODE_INT
,
312 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
313 TYPE_FLAG_UNSIGNED
, "unsigned long", objfile
);
316 type
= init_type (TYPE_CODE_INT
,
317 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
318 0, "long long", objfile
);
320 case FT_SIGNED_LONG_LONG
:
321 type
= init_type (TYPE_CODE_INT
,
322 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
323 0, "signed long long", objfile
);
325 case FT_UNSIGNED_LONG_LONG
:
326 type
= init_type (TYPE_CODE_INT
,
327 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
328 TYPE_FLAG_UNSIGNED
, "unsigned long long", objfile
);
331 type
= init_type (TYPE_CODE_FLT
,
332 TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
,
333 0, "float", objfile
);
335 case FT_DBL_PREC_FLOAT
:
336 type
= init_type (TYPE_CODE_FLT
,
337 TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
338 0, "double", objfile
);
340 case FT_EXT_PREC_FLOAT
:
341 type
= init_type (TYPE_CODE_FLT
,
342 TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
343 0, "long double", objfile
);
345 case FT_TEMPLATE_ARG
:
346 type
= init_type (TYPE_CODE_TEMPLATE_ARG
,
348 0, "<template arg>", objfile
);
356 /* Table mapping opcodes into strings for printing operators
357 and precedences of the operators. */
359 const struct op_print c_op_print_tab
[] =
361 {",", BINOP_COMMA
, PREC_COMMA
, 0},
362 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
363 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
364 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
365 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
366 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
367 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
368 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
369 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
370 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
371 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
372 {">", BINOP_GTR
, PREC_ORDER
, 0},
373 {"<", BINOP_LESS
, PREC_ORDER
, 0},
374 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
375 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
376 {"+", BINOP_ADD
, PREC_ADD
, 0},
377 {"-", BINOP_SUB
, PREC_ADD
, 0},
378 {"*", BINOP_MUL
, PREC_MUL
, 0},
379 {"/", BINOP_DIV
, PREC_MUL
, 0},
380 {"%", BINOP_REM
, PREC_MUL
, 0},
381 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
382 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
383 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
384 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
385 {"*", UNOP_IND
, PREC_PREFIX
, 0},
386 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
387 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
388 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
389 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
391 {"::", BINOP_SCOPE
, PREC_PREFIX
, 0},
395 struct type
** CONST_PTR (c_builtin_types
[]) =
402 &builtin_type_double
,
404 &builtin_type_long_long
,
405 &builtin_type_signed_char
,
406 &builtin_type_unsigned_char
,
407 &builtin_type_unsigned_short
,
408 &builtin_type_unsigned_int
,
409 &builtin_type_unsigned_long
,
410 &builtin_type_unsigned_long_long
,
411 &builtin_type_long_double
,
412 &builtin_type_complex
,
413 &builtin_type_double_complex
,
417 const struct language_defn c_language_defn
= {
418 "c", /* Language name */
425 evaluate_subexp_standard
,
426 c_printchar
, /* Print a character constant */
427 c_printstr
, /* Function to print string constant */
428 c_emit_char
, /* Print a single char */
429 c_create_fundamental_type
, /* Create fundamental type in this language */
430 c_print_type
, /* Print a type using appropriate syntax */
431 c_val_print
, /* Print a value using appropriate syntax */
432 c_value_print
, /* Print a top-level value */
433 {"", "", "", ""}, /* Binary format info */
434 {"0%lo", "0", "o", ""}, /* Octal format info */
435 {"%ld", "", "d", ""}, /* Decimal format info */
436 {"0x%lx", "0x", "x", ""}, /* Hex format info */
437 c_op_print_tab
, /* expression operators for printing */
438 1, /* c-style arrays */
439 0, /* String lower bound */
440 &builtin_type_char
, /* Type of string elements */
444 struct type
** const (cplus_builtin_types
[]) =
451 &builtin_type_double
,
453 &builtin_type_long_long
,
454 &builtin_type_signed_char
,
455 &builtin_type_unsigned_char
,
456 &builtin_type_unsigned_short
,
457 &builtin_type_unsigned_int
,
458 &builtin_type_unsigned_long
,
459 &builtin_type_unsigned_long_long
,
460 &builtin_type_long_double
,
461 &builtin_type_complex
,
462 &builtin_type_double_complex
,
467 const struct language_defn cplus_language_defn
= {
468 "c++", /* Language name */
475 evaluate_subexp_standard
,
476 c_printchar
, /* Print a character constant */
477 c_printstr
, /* Function to print string constant */
478 c_emit_char
, /* Print a single char */
479 c_create_fundamental_type
, /* Create fundamental type in this language */
480 c_print_type
, /* Print a type using appropriate syntax */
481 c_val_print
, /* Print a value using appropriate syntax */
482 c_value_print
, /* Print a top-level value */
483 {"", "", "", ""}, /* Binary format info */
484 {"0%lo", "0", "o", ""}, /* Octal format info */
485 {"%ld", "", "d", ""}, /* Decimal format info */
486 {"0x%lx", "0x", "x", ""}, /* Hex format info */
487 c_op_print_tab
, /* expression operators for printing */
488 1, /* c-style arrays */
489 0, /* String lower bound */
490 &builtin_type_char
, /* Type of string elements */
494 const struct language_defn asm_language_defn
= {
495 "asm", /* Language name */
502 evaluate_subexp_standard
,
503 c_printchar
, /* Print a character constant */
504 c_printstr
, /* Function to print string constant */
505 c_emit_char
, /* Print a single char */
506 c_create_fundamental_type
, /* Create fundamental type in this language */
507 c_print_type
, /* Print a type using appropriate syntax */
508 c_val_print
, /* Print a value using appropriate syntax */
509 c_value_print
, /* Print a top-level value */
510 {"", "", "", ""}, /* Binary format info */
511 {"0%lo", "0", "o", ""}, /* Octal format info */
512 {"%ld", "", "d", ""}, /* Decimal format info */
513 {"0x%lx", "0x", "x", ""}, /* Hex format info */
514 c_op_print_tab
, /* expression operators for printing */
515 1, /* c-style arrays */
516 0, /* String lower bound */
517 &builtin_type_char
, /* Type of string elements */
522 _initialize_c_language ()
524 add_language (&c_language_defn
);
525 add_language (&cplus_language_defn
);
526 add_language (&asm_language_defn
);