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,
19 Boston, MA 02111-1307, USA. */
24 #include "expression.h"
25 #include "parser-defs.h"
29 extern void _initialize_c_language
PARAMS ((void));
30 static void c_emit_char
PARAMS ((int c
, GDB_FILE
* stream
, int quoter
));
32 /* Print the character C on STREAM as part of the contents of a literal
33 string whose delimiter is QUOTER. Note that that format for printing
34 characters and strings is language specific. */
37 c_emit_char (c
, stream
, quoter
)
42 c
&= 0xFF; /* Avoid sign bit follies */
44 if (PRINT_LITERAL_FORM (c
))
46 if (c
== '\\' || c
== quoter
)
48 fputs_filtered ("\\", stream
);
50 fprintf_filtered (stream
, "%c", c
);
57 fputs_filtered ("\\n", stream
);
60 fputs_filtered ("\\b", stream
);
63 fputs_filtered ("\\t", stream
);
66 fputs_filtered ("\\f", stream
);
69 fputs_filtered ("\\r", stream
);
72 fputs_filtered ("\\e", stream
);
75 fputs_filtered ("\\a", stream
);
78 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
85 c_printchar (c
, stream
)
89 fputc_filtered ('\'', stream
);
90 LA_EMIT_CHAR (c
, stream
, '\'');
91 fputc_filtered ('\'', stream
);
94 /* Print the character string STRING, printing at most LENGTH characters.
95 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
96 long. Printing stops early if the number hits print_max; repeat counts are
97 printed as appropriate. Print ellipses at the end if we had to stop before
98 printing LENGTH characters, or if FORCE_ELLIPSES. */
101 c_printstr (stream
, string
, length
, width
, force_ellipses
)
108 register unsigned int i
;
109 unsigned int things_printed
= 0;
112 extern int inspect_it
;
113 extern int repeat_count_threshold
;
114 extern int print_max
;
116 /* If the string was not truncated due to `set print elements', and
117 the last byte of it is a null, we don't print that, in traditional C
121 && extract_unsigned_integer (string
+ (length
- 1) * width
, width
) == '\0')
126 fputs_filtered ("\"\"", stream
);
130 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
132 /* Position of the character we are examining
133 to see whether it is repeated. */
135 /* Number of repetitions we have detected so far. */
137 unsigned long current_char
;
143 fputs_filtered (", ", stream
);
147 current_char
= extract_unsigned_integer (string
+ i
* width
, width
);
152 && extract_unsigned_integer (string
+ rep1
* width
, width
)
159 if (reps
> repeat_count_threshold
)
164 fputs_filtered ("\\\", ", stream
);
166 fputs_filtered ("\", ", stream
);
169 LA_PRINT_CHAR (current_char
, stream
);
170 fprintf_filtered (stream
, " <repeats %u times>", reps
);
172 things_printed
+= repeat_count_threshold
;
180 fputs_filtered ("\\\"", stream
);
182 fputs_filtered ("\"", stream
);
185 LA_EMIT_CHAR (current_char
, stream
, '"');
190 /* Terminate the quotes if necessary. */
194 fputs_filtered ("\\\"", stream
);
196 fputs_filtered ("\"", stream
);
199 if (force_ellipses
|| i
< length
)
200 fputs_filtered ("...", stream
);
203 /* Create a fundamental C type using default reasonable for the current
206 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
207 define fundamental types such as "int" or "double". Others (stabs or
208 DWARF version 2, etc) do define fundamental types. For the formats which
209 don't provide fundamental types, gdb can create such types using this
212 FIXME: Some compilers distinguish explicitly signed integral types
213 (signed short, signed int, signed long) from "regular" integral types
214 (short, int, long) in the debugging information. There is some dis-
215 agreement as to how useful this feature is. In particular, gcc does
216 not support this. Also, only some debugging formats allow the
217 distinction to be passed on to a debugger. For now, we always just
218 use "short", "int", or "long" as the type name, for both the implicit
219 and explicitly signed types. This also makes life easier for the
220 gdb test suite since we don't have to account for the differences
221 in output depending upon what the compiler and debugging format
222 support. We will probably have to re-examine the issue when gdb
223 starts taking it's fundamental type information directly from the
224 debugging information supplied by the compiler. fnf@cygnus.com */
227 c_create_fundamental_type (objfile
, typeid)
228 struct objfile
*objfile
;
231 register struct type
*type
= NULL
;
236 /* FIXME: For now, if we are asked to produce a type not in this
237 language, create the equivalent of a C integer type with the
238 name "<?type?>". When all the dust settles from the type
239 reconstruction work, this should probably become an error. */
240 type
= init_type (TYPE_CODE_INT
,
241 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
242 0, "<?type?>", objfile
);
243 warning ("internal error: no C/C++ fundamental type %d", typeid);
246 type
= init_type (TYPE_CODE_VOID
,
247 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
251 type
= init_type (TYPE_CODE_BOOL
,
252 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
257 type
= init_type (TYPE_CODE_INT
,
258 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
260 TYPE_FLAGS (type
) |= TYPE_FLAG_NOSIGN
;
263 type
= init_type (TYPE_CODE_INT
,
264 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
265 0, "signed char", objfile
);
267 case FT_UNSIGNED_CHAR
:
268 type
= init_type (TYPE_CODE_INT
,
269 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
270 TYPE_FLAG_UNSIGNED
, "unsigned char", objfile
);
273 type
= init_type (TYPE_CODE_INT
,
274 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
275 0, "short", objfile
);
277 case FT_SIGNED_SHORT
:
278 type
= init_type (TYPE_CODE_INT
,
279 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
280 0, "short", objfile
); /* FIXME-fnf */
282 case FT_UNSIGNED_SHORT
:
283 type
= init_type (TYPE_CODE_INT
,
284 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
285 TYPE_FLAG_UNSIGNED
, "unsigned short", objfile
);
288 type
= init_type (TYPE_CODE_INT
,
289 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
292 case FT_SIGNED_INTEGER
:
293 type
= init_type (TYPE_CODE_INT
,
294 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
295 0, "int", objfile
); /* FIXME -fnf */
297 case FT_UNSIGNED_INTEGER
:
298 type
= init_type (TYPE_CODE_INT
,
299 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
300 TYPE_FLAG_UNSIGNED
, "unsigned int", objfile
);
303 type
= init_type (TYPE_CODE_INT
,
304 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
308 type
= init_type (TYPE_CODE_INT
,
309 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
310 0, "long", objfile
); /* FIXME -fnf */
312 case FT_UNSIGNED_LONG
:
313 type
= init_type (TYPE_CODE_INT
,
314 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
315 TYPE_FLAG_UNSIGNED
, "unsigned long", objfile
);
318 type
= init_type (TYPE_CODE_INT
,
319 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
320 0, "long long", objfile
);
322 case FT_SIGNED_LONG_LONG
:
323 type
= init_type (TYPE_CODE_INT
,
324 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
325 0, "signed long long", objfile
);
327 case FT_UNSIGNED_LONG_LONG
:
328 type
= init_type (TYPE_CODE_INT
,
329 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
330 TYPE_FLAG_UNSIGNED
, "unsigned long long", objfile
);
333 type
= init_type (TYPE_CODE_FLT
,
334 TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
,
335 0, "float", objfile
);
337 case FT_DBL_PREC_FLOAT
:
338 type
= init_type (TYPE_CODE_FLT
,
339 TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
340 0, "double", objfile
);
342 case FT_EXT_PREC_FLOAT
:
343 type
= init_type (TYPE_CODE_FLT
,
344 TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
345 0, "long double", objfile
);
347 case FT_TEMPLATE_ARG
:
348 type
= init_type (TYPE_CODE_TEMPLATE_ARG
,
350 0, "<template arg>", objfile
);
358 /* Table mapping opcodes into strings for printing operators
359 and precedences of the operators. */
361 const struct op_print c_op_print_tab
[] =
363 {",", BINOP_COMMA
, PREC_COMMA
, 0},
364 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
365 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
366 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
367 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
368 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
369 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
370 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
371 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
372 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
373 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
374 {">", BINOP_GTR
, PREC_ORDER
, 0},
375 {"<", BINOP_LESS
, PREC_ORDER
, 0},
376 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
377 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
378 {"+", BINOP_ADD
, PREC_ADD
, 0},
379 {"-", BINOP_SUB
, PREC_ADD
, 0},
380 {"*", BINOP_MUL
, PREC_MUL
, 0},
381 {"/", BINOP_DIV
, PREC_MUL
, 0},
382 {"%", BINOP_REM
, PREC_MUL
, 0},
383 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
384 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
385 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
386 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
387 {"*", UNOP_IND
, PREC_PREFIX
, 0},
388 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
389 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
390 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
391 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
393 {"::", BINOP_SCOPE
, PREC_PREFIX
, 0},
397 struct type
**CONST_PTR (c_builtin_types
[]) =
404 &builtin_type_double
,
406 &builtin_type_long_long
,
407 &builtin_type_signed_char
,
408 &builtin_type_unsigned_char
,
409 &builtin_type_unsigned_short
,
410 &builtin_type_unsigned_int
,
411 &builtin_type_unsigned_long
,
412 &builtin_type_unsigned_long_long
,
413 &builtin_type_long_double
,
414 &builtin_type_complex
,
415 &builtin_type_double_complex
,
419 const struct language_defn c_language_defn
=
421 "c", /* Language name */
428 evaluate_subexp_standard
,
429 c_printchar
, /* Print a character constant */
430 c_printstr
, /* Function to print string constant */
431 c_emit_char
, /* Print a single char */
432 c_create_fundamental_type
, /* Create fundamental type in this language */
433 c_print_type
, /* Print a type using appropriate syntax */
434 c_val_print
, /* Print a value using appropriate syntax */
435 c_value_print
, /* Print a top-level value */
436 {"", "", "", ""}, /* Binary format info */
437 {"0%lo", "0", "o", ""}, /* Octal format info */
438 {"%ld", "", "d", ""}, /* Decimal format info */
439 {"0x%lx", "0x", "x", ""}, /* Hex format info */
440 c_op_print_tab
, /* expression operators for printing */
441 1, /* c-style arrays */
442 0, /* String lower bound */
443 &builtin_type_char
, /* Type of string elements */
447 struct type
**const (cplus_builtin_types
[]) =
454 &builtin_type_double
,
456 &builtin_type_long_long
,
457 &builtin_type_signed_char
,
458 &builtin_type_unsigned_char
,
459 &builtin_type_unsigned_short
,
460 &builtin_type_unsigned_int
,
461 &builtin_type_unsigned_long
,
462 &builtin_type_unsigned_long_long
,
463 &builtin_type_long_double
,
464 &builtin_type_complex
,
465 &builtin_type_double_complex
,
470 const struct language_defn cplus_language_defn
=
472 "c++", /* Language name */
479 evaluate_subexp_standard
,
480 c_printchar
, /* Print a character constant */
481 c_printstr
, /* Function to print string constant */
482 c_emit_char
, /* Print a single char */
483 c_create_fundamental_type
, /* Create fundamental type in this language */
484 c_print_type
, /* Print a type using appropriate syntax */
485 c_val_print
, /* Print a value using appropriate syntax */
486 c_value_print
, /* Print a top-level value */
487 {"", "", "", ""}, /* Binary format info */
488 {"0%lo", "0", "o", ""}, /* Octal format info */
489 {"%ld", "", "d", ""}, /* Decimal format info */
490 {"0x%lx", "0x", "x", ""}, /* Hex format info */
491 c_op_print_tab
, /* expression operators for printing */
492 1, /* c-style arrays */
493 0, /* String lower bound */
494 &builtin_type_char
, /* Type of string elements */
498 const struct language_defn asm_language_defn
=
500 "asm", /* Language name */
507 evaluate_subexp_standard
,
508 c_printchar
, /* Print a character constant */
509 c_printstr
, /* Function to print string constant */
510 c_emit_char
, /* Print a single char */
511 c_create_fundamental_type
, /* Create fundamental type in this language */
512 c_print_type
, /* Print a type using appropriate syntax */
513 c_val_print
, /* Print a value using appropriate syntax */
514 c_value_print
, /* Print a top-level value */
515 {"", "", "", ""}, /* Binary format info */
516 {"0%lo", "0", "o", ""}, /* Octal format info */
517 {"%ld", "", "d", ""}, /* Decimal format info */
518 {"0x%lx", "0x", "x", ""}, /* Hex format info */
519 c_op_print_tab
, /* expression operators for printing */
520 1, /* c-style arrays */
521 0, /* String lower bound */
522 &builtin_type_char
, /* Type of string elements */
527 _initialize_c_language ()
529 add_language (&c_language_defn
);
530 add_language (&cplus_language_defn
);
531 add_language (&asm_language_defn
);