1 /* C language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "expression.h"
26 #include "parser-defs.h"
31 extern void _initialize_c_language (void);
32 static void c_emit_char (int c
, struct ui_file
* stream
, int quoter
);
34 /* Print the character C on STREAM as part of the contents of a literal
35 string whose delimiter is QUOTER. Note that that format for printing
36 characters and strings is language specific. */
39 c_emit_char (register int c
, struct ui_file
*stream
, int quoter
)
41 c
&= 0xFF; /* Avoid sign bit follies */
43 if (PRINT_LITERAL_FORM (c
))
45 if (c
== '\\' || c
== quoter
)
47 fputs_filtered ("\\", stream
);
49 fprintf_filtered (stream
, "%c", c
);
56 fputs_filtered ("\\n", stream
);
59 fputs_filtered ("\\b", stream
);
62 fputs_filtered ("\\t", stream
);
65 fputs_filtered ("\\f", stream
);
68 fputs_filtered ("\\r", stream
);
71 fputs_filtered ("\\e", stream
);
74 fputs_filtered ("\\a", stream
);
77 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
84 c_printchar (int c
, struct ui_file
*stream
)
86 fputc_filtered ('\'', stream
);
87 LA_EMIT_CHAR (c
, stream
, '\'');
88 fputc_filtered ('\'', stream
);
91 /* Print the character string STRING, printing at most LENGTH characters.
92 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
93 long. Printing stops early if the number hits print_max; repeat counts are
94 printed as appropriate. Print ellipses at the end if we had to stop before
95 printing LENGTH characters, or if FORCE_ELLIPSES. */
98 c_printstr (struct ui_file
*stream
, char *string
, unsigned int length
,
99 int width
, int force_ellipses
)
101 register unsigned int i
;
102 unsigned int things_printed
= 0;
105 extern int inspect_it
;
107 /* If the string was not truncated due to `set print elements', and
108 the last byte of it is a null, we don't print that, in traditional C
112 && extract_unsigned_integer (string
+ (length
- 1) * width
, width
) == '\0')
117 fputs_filtered ("\"\"", stream
);
121 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
123 /* Position of the character we are examining
124 to see whether it is repeated. */
126 /* Number of repetitions we have detected so far. */
128 unsigned long current_char
;
134 fputs_filtered (", ", stream
);
138 current_char
= extract_unsigned_integer (string
+ i
* width
, width
);
143 && extract_unsigned_integer (string
+ rep1
* width
, width
)
150 if (reps
> repeat_count_threshold
)
155 fputs_filtered ("\\\", ", stream
);
157 fputs_filtered ("\", ", stream
);
160 LA_PRINT_CHAR (current_char
, stream
);
161 fprintf_filtered (stream
, " <repeats %u times>", reps
);
163 things_printed
+= repeat_count_threshold
;
171 fputs_filtered ("\\\"", stream
);
173 fputs_filtered ("\"", stream
);
176 LA_EMIT_CHAR (current_char
, stream
, '"');
181 /* Terminate the quotes if necessary. */
185 fputs_filtered ("\\\"", stream
);
187 fputs_filtered ("\"", stream
);
190 if (force_ellipses
|| i
< length
)
191 fputs_filtered ("...", stream
);
194 /* Create a fundamental C type using default reasonable for the current
197 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
198 define fundamental types such as "int" or "double". Others (stabs or
199 DWARF version 2, etc) do define fundamental types. For the formats which
200 don't provide fundamental types, gdb can create such types using this
203 FIXME: Some compilers distinguish explicitly signed integral types
204 (signed short, signed int, signed long) from "regular" integral types
205 (short, int, long) in the debugging information. There is some dis-
206 agreement as to how useful this feature is. In particular, gcc does
207 not support this. Also, only some debugging formats allow the
208 distinction to be passed on to a debugger. For now, we always just
209 use "short", "int", or "long" as the type name, for both the implicit
210 and explicitly signed types. This also makes life easier for the
211 gdb test suite since we don't have to account for the differences
212 in output depending upon what the compiler and debugging format
213 support. We will probably have to re-examine the issue when gdb
214 starts taking it's fundamental type information directly from the
215 debugging information supplied by the compiler. fnf@cygnus.com */
218 c_create_fundamental_type (struct objfile
*objfile
, int typeid)
220 register struct type
*type
= NULL
;
225 /* FIXME: For now, if we are asked to produce a type not in this
226 language, create the equivalent of a C integer type with the
227 name "<?type?>". When all the dust settles from the type
228 reconstruction work, this should probably become an error. */
229 type
= init_type (TYPE_CODE_INT
,
230 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
231 0, "<?type?>", objfile
);
232 warning ("internal error: no C/C++ fundamental type %d", typeid);
235 type
= init_type (TYPE_CODE_VOID
,
236 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
240 type
= init_type (TYPE_CODE_BOOL
,
241 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
246 type
= init_type (TYPE_CODE_INT
,
247 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
249 TYPE_FLAGS (type
) |= TYPE_FLAG_NOSIGN
;
252 type
= init_type (TYPE_CODE_INT
,
253 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
254 0, "signed char", objfile
);
256 case FT_UNSIGNED_CHAR
:
257 type
= init_type (TYPE_CODE_INT
,
258 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
259 TYPE_FLAG_UNSIGNED
, "unsigned char", objfile
);
262 type
= init_type (TYPE_CODE_INT
,
263 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
264 0, "short", objfile
);
266 case FT_SIGNED_SHORT
:
267 type
= init_type (TYPE_CODE_INT
,
268 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
269 0, "short", objfile
); /* FIXME-fnf */
271 case FT_UNSIGNED_SHORT
:
272 type
= init_type (TYPE_CODE_INT
,
273 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
274 TYPE_FLAG_UNSIGNED
, "unsigned short", objfile
);
277 type
= init_type (TYPE_CODE_INT
,
278 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
281 case FT_SIGNED_INTEGER
:
282 type
= init_type (TYPE_CODE_INT
,
283 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
284 0, "int", objfile
); /* FIXME -fnf */
286 case FT_UNSIGNED_INTEGER
:
287 type
= init_type (TYPE_CODE_INT
,
288 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
289 TYPE_FLAG_UNSIGNED
, "unsigned int", objfile
);
292 type
= init_type (TYPE_CODE_INT
,
293 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
297 type
= init_type (TYPE_CODE_INT
,
298 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
299 0, "long", objfile
); /* FIXME -fnf */
301 case FT_UNSIGNED_LONG
:
302 type
= init_type (TYPE_CODE_INT
,
303 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
304 TYPE_FLAG_UNSIGNED
, "unsigned long", objfile
);
307 type
= init_type (TYPE_CODE_INT
,
308 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
309 0, "long long", objfile
);
311 case FT_SIGNED_LONG_LONG
:
312 type
= init_type (TYPE_CODE_INT
,
313 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
314 0, "signed long long", objfile
);
316 case FT_UNSIGNED_LONG_LONG
:
317 type
= init_type (TYPE_CODE_INT
,
318 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
319 TYPE_FLAG_UNSIGNED
, "unsigned long long", objfile
);
322 type
= init_type (TYPE_CODE_FLT
,
323 TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
,
324 0, "float", objfile
);
326 case FT_DBL_PREC_FLOAT
:
327 type
= init_type (TYPE_CODE_FLT
,
328 TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
329 0, "double", objfile
);
331 case FT_EXT_PREC_FLOAT
:
332 type
= init_type (TYPE_CODE_FLT
,
333 TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
334 0, "long double", objfile
);
336 case FT_TEMPLATE_ARG
:
337 type
= init_type (TYPE_CODE_TEMPLATE_ARG
,
339 0, "<template arg>", objfile
);
347 /* Table mapping opcodes into strings for printing operators
348 and precedences of the operators. */
350 const struct op_print c_op_print_tab
[] =
352 {",", BINOP_COMMA
, PREC_COMMA
, 0},
353 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
354 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
355 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
356 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
357 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
358 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
359 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
360 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
361 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
362 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
363 {">", BINOP_GTR
, PREC_ORDER
, 0},
364 {"<", BINOP_LESS
, PREC_ORDER
, 0},
365 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
366 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
367 {"+", BINOP_ADD
, PREC_ADD
, 0},
368 {"-", BINOP_SUB
, PREC_ADD
, 0},
369 {"*", BINOP_MUL
, PREC_MUL
, 0},
370 {"/", BINOP_DIV
, PREC_MUL
, 0},
371 {"%", BINOP_REM
, PREC_MUL
, 0},
372 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
373 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
374 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
375 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
376 {"*", UNOP_IND
, PREC_PREFIX
, 0},
377 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
378 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
379 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
380 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
384 struct type
**CONST_PTR (c_builtin_types
[]) =
391 &builtin_type_double
,
393 &builtin_type_long_long
,
394 &builtin_type_signed_char
,
395 &builtin_type_unsigned_char
,
396 &builtin_type_unsigned_short
,
397 &builtin_type_unsigned_int
,
398 &builtin_type_unsigned_long
,
399 &builtin_type_unsigned_long_long
,
400 &builtin_type_long_double
,
401 &builtin_type_complex
,
402 &builtin_type_double_complex
,
406 const struct language_defn c_language_defn
=
408 "c", /* Language name */
416 evaluate_subexp_standard
,
417 c_printchar
, /* Print a character constant */
418 c_printstr
, /* Function to print string constant */
419 c_emit_char
, /* Print a single char */
420 c_create_fundamental_type
, /* Create fundamental type in this language */
421 c_print_type
, /* Print a type using appropriate syntax */
422 c_val_print
, /* Print a value using appropriate syntax */
423 c_value_print
, /* Print a top-level value */
424 {"", "", "", ""}, /* Binary format info */
425 {"0%lo", "0", "o", ""}, /* Octal format info */
426 {"%ld", "", "d", ""}, /* Decimal format info */
427 {"0x%lx", "0x", "x", ""}, /* Hex format info */
428 c_op_print_tab
, /* expression operators for printing */
429 1, /* c-style arrays */
430 0, /* String lower bound */
431 &builtin_type_char
, /* Type of string elements */
435 struct type
**const (cplus_builtin_types
[]) =
442 &builtin_type_double
,
444 &builtin_type_long_long
,
445 &builtin_type_signed_char
,
446 &builtin_type_unsigned_char
,
447 &builtin_type_unsigned_short
,
448 &builtin_type_unsigned_int
,
449 &builtin_type_unsigned_long
,
450 &builtin_type_unsigned_long_long
,
451 &builtin_type_long_double
,
452 &builtin_type_complex
,
453 &builtin_type_double_complex
,
458 const struct language_defn cplus_language_defn
=
460 "c++", /* Language name */
468 evaluate_subexp_standard
,
469 c_printchar
, /* Print a character constant */
470 c_printstr
, /* Function to print string constant */
471 c_emit_char
, /* Print a single char */
472 c_create_fundamental_type
, /* Create fundamental type in this language */
473 c_print_type
, /* Print a type using appropriate syntax */
474 c_val_print
, /* Print a value using appropriate syntax */
475 c_value_print
, /* Print a top-level value */
476 {"", "", "", ""}, /* Binary format info */
477 {"0%lo", "0", "o", ""}, /* Octal format info */
478 {"%ld", "", "d", ""}, /* Decimal format info */
479 {"0x%lx", "0x", "x", ""}, /* Hex format info */
480 c_op_print_tab
, /* expression operators for printing */
481 1, /* c-style arrays */
482 0, /* String lower bound */
483 &builtin_type_char
, /* Type of string elements */
487 const struct language_defn asm_language_defn
=
489 "asm", /* Language name */
497 evaluate_subexp_standard
,
498 c_printchar
, /* Print a character constant */
499 c_printstr
, /* Function to print string constant */
500 c_emit_char
, /* Print a single char */
501 c_create_fundamental_type
, /* Create fundamental type in this language */
502 c_print_type
, /* Print a type using appropriate syntax */
503 c_val_print
, /* Print a value using appropriate syntax */
504 c_value_print
, /* Print a top-level value */
505 {"", "", "", ""}, /* Binary format info */
506 {"0%lo", "0", "o", ""}, /* Octal format info */
507 {"%ld", "", "d", ""}, /* Decimal format info */
508 {"0x%lx", "0x", "x", ""}, /* Hex format info */
509 c_op_print_tab
, /* expression operators for printing */
510 1, /* c-style arrays */
511 0, /* String lower bound */
512 &builtin_type_char
, /* Type of string elements */
517 _initialize_c_language (void)
519 add_language (&c_language_defn
);
520 add_language (&cplus_language_defn
);
521 add_language (&asm_language_defn
);