1 /* C language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1993, 1994, 2000 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"
30 extern void _initialize_c_language (void);
31 static void c_emit_char (int c
, struct ui_file
* stream
, int quoter
);
33 /* Print the character C on STREAM as part of the contents of a literal
34 string whose delimiter is QUOTER. Note that that format for printing
35 characters and strings is language specific. */
38 c_emit_char (register int c
, struct ui_file
*stream
, int 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 (int c
, struct ui_file
*stream
)
85 fputc_filtered ('\'', stream
);
86 LA_EMIT_CHAR (c
, stream
, '\'');
87 fputc_filtered ('\'', stream
);
90 /* Print the character string STRING, printing at most LENGTH characters.
91 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
92 long. Printing stops early if the number hits print_max; repeat counts are
93 printed as appropriate. Print ellipses at the end if we had to stop before
94 printing LENGTH characters, or if FORCE_ELLIPSES. */
97 c_printstr (struct ui_file
*stream
, char *string
, unsigned int length
,
98 int width
, int force_ellipses
)
100 register unsigned int i
;
101 unsigned int things_printed
= 0;
104 extern int inspect_it
;
106 /* If the string was not truncated due to `set print elements', and
107 the last byte of it is a null, we don't print that, in traditional C
111 && extract_unsigned_integer (string
+ (length
- 1) * width
, width
) == '\0')
116 fputs_filtered ("\"\"", stream
);
120 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
122 /* Position of the character we are examining
123 to see whether it is repeated. */
125 /* Number of repetitions we have detected so far. */
127 unsigned long current_char
;
133 fputs_filtered (", ", stream
);
137 current_char
= extract_unsigned_integer (string
+ i
* width
, width
);
142 && extract_unsigned_integer (string
+ rep1
* width
, width
)
149 if (reps
> repeat_count_threshold
)
154 fputs_filtered ("\\\", ", stream
);
156 fputs_filtered ("\", ", stream
);
159 LA_PRINT_CHAR (current_char
, stream
);
160 fprintf_filtered (stream
, " <repeats %u times>", reps
);
162 things_printed
+= repeat_count_threshold
;
170 fputs_filtered ("\\\"", stream
);
172 fputs_filtered ("\"", stream
);
175 LA_EMIT_CHAR (current_char
, stream
, '"');
180 /* Terminate the quotes if necessary. */
184 fputs_filtered ("\\\"", stream
);
186 fputs_filtered ("\"", stream
);
189 if (force_ellipses
|| i
< length
)
190 fputs_filtered ("...", stream
);
193 /* Create a fundamental C type using default reasonable for the current
196 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
197 define fundamental types such as "int" or "double". Others (stabs or
198 DWARF version 2, etc) do define fundamental types. For the formats which
199 don't provide fundamental types, gdb can create such types using this
202 FIXME: Some compilers distinguish explicitly signed integral types
203 (signed short, signed int, signed long) from "regular" integral types
204 (short, int, long) in the debugging information. There is some dis-
205 agreement as to how useful this feature is. In particular, gcc does
206 not support this. Also, only some debugging formats allow the
207 distinction to be passed on to a debugger. For now, we always just
208 use "short", "int", or "long" as the type name, for both the implicit
209 and explicitly signed types. This also makes life easier for the
210 gdb test suite since we don't have to account for the differences
211 in output depending upon what the compiler and debugging format
212 support. We will probably have to re-examine the issue when gdb
213 starts taking it's fundamental type information directly from the
214 debugging information supplied by the compiler. fnf@cygnus.com */
217 c_create_fundamental_type (struct objfile
*objfile
, int typeid)
219 register struct type
*type
= NULL
;
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);
234 type
= init_type (TYPE_CODE_VOID
,
235 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
239 type
= init_type (TYPE_CODE_BOOL
,
240 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
245 type
= init_type (TYPE_CODE_INT
,
246 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
248 TYPE_FLAGS (type
) |= TYPE_FLAG_NOSIGN
;
251 type
= init_type (TYPE_CODE_INT
,
252 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
253 0, "signed char", objfile
);
255 case FT_UNSIGNED_CHAR
:
256 type
= init_type (TYPE_CODE_INT
,
257 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
258 TYPE_FLAG_UNSIGNED
, "unsigned char", objfile
);
261 type
= init_type (TYPE_CODE_INT
,
262 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
263 0, "short", objfile
);
265 case FT_SIGNED_SHORT
:
266 type
= init_type (TYPE_CODE_INT
,
267 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
268 0, "short", objfile
); /* FIXME-fnf */
270 case FT_UNSIGNED_SHORT
:
271 type
= init_type (TYPE_CODE_INT
,
272 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
273 TYPE_FLAG_UNSIGNED
, "unsigned short", objfile
);
276 type
= init_type (TYPE_CODE_INT
,
277 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
280 case FT_SIGNED_INTEGER
:
281 type
= init_type (TYPE_CODE_INT
,
282 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
283 0, "int", objfile
); /* FIXME -fnf */
285 case FT_UNSIGNED_INTEGER
:
286 type
= init_type (TYPE_CODE_INT
,
287 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
288 TYPE_FLAG_UNSIGNED
, "unsigned int", objfile
);
291 type
= init_type (TYPE_CODE_INT
,
292 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
296 type
= init_type (TYPE_CODE_INT
,
297 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
298 0, "long", objfile
); /* FIXME -fnf */
300 case FT_UNSIGNED_LONG
:
301 type
= init_type (TYPE_CODE_INT
,
302 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
303 TYPE_FLAG_UNSIGNED
, "unsigned long", objfile
);
306 type
= init_type (TYPE_CODE_INT
,
307 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
308 0, "long long", objfile
);
310 case FT_SIGNED_LONG_LONG
:
311 type
= init_type (TYPE_CODE_INT
,
312 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
313 0, "signed long long", objfile
);
315 case FT_UNSIGNED_LONG_LONG
:
316 type
= init_type (TYPE_CODE_INT
,
317 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
318 TYPE_FLAG_UNSIGNED
, "unsigned long long", objfile
);
321 type
= init_type (TYPE_CODE_FLT
,
322 TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
,
323 0, "float", objfile
);
325 case FT_DBL_PREC_FLOAT
:
326 type
= init_type (TYPE_CODE_FLT
,
327 TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
328 0, "double", objfile
);
330 case FT_EXT_PREC_FLOAT
:
331 type
= init_type (TYPE_CODE_FLT
,
332 TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
333 0, "long double", objfile
);
335 case FT_TEMPLATE_ARG
:
336 type
= init_type (TYPE_CODE_TEMPLATE_ARG
,
338 0, "<template arg>", objfile
);
346 /* Table mapping opcodes into strings for printing operators
347 and precedences of the operators. */
349 const struct op_print c_op_print_tab
[] =
351 {",", BINOP_COMMA
, PREC_COMMA
, 0},
352 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
353 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
354 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
355 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
356 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
357 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
358 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
359 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
360 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
361 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
362 {">", BINOP_GTR
, PREC_ORDER
, 0},
363 {"<", BINOP_LESS
, PREC_ORDER
, 0},
364 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
365 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
366 {"+", BINOP_ADD
, PREC_ADD
, 0},
367 {"-", BINOP_SUB
, PREC_ADD
, 0},
368 {"*", BINOP_MUL
, PREC_MUL
, 0},
369 {"/", BINOP_DIV
, PREC_MUL
, 0},
370 {"%", BINOP_REM
, PREC_MUL
, 0},
371 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
372 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
373 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
374 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
375 {"*", UNOP_IND
, PREC_PREFIX
, 0},
376 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
377 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
378 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
379 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
383 struct type
**CONST_PTR (c_builtin_types
[]) =
390 &builtin_type_double
,
392 &builtin_type_long_long
,
393 &builtin_type_signed_char
,
394 &builtin_type_unsigned_char
,
395 &builtin_type_unsigned_short
,
396 &builtin_type_unsigned_int
,
397 &builtin_type_unsigned_long
,
398 &builtin_type_unsigned_long_long
,
399 &builtin_type_long_double
,
400 &builtin_type_complex
,
401 &builtin_type_double_complex
,
405 const struct language_defn c_language_defn
=
407 "c", /* Language name */
415 evaluate_subexp_standard
,
416 c_printchar
, /* Print a character constant */
417 c_printstr
, /* Function to print string constant */
418 c_emit_char
, /* Print a single char */
419 c_create_fundamental_type
, /* Create fundamental type in this language */
420 c_print_type
, /* Print a type using appropriate syntax */
421 c_val_print
, /* Print a value using appropriate syntax */
422 c_value_print
, /* Print a top-level value */
423 {"", "", "", ""}, /* Binary format info */
424 {"0%lo", "0", "o", ""}, /* Octal format info */
425 {"%ld", "", "d", ""}, /* Decimal format info */
426 {"0x%lx", "0x", "x", ""}, /* Hex format info */
427 c_op_print_tab
, /* expression operators for printing */
428 1, /* c-style arrays */
429 0, /* String lower bound */
430 &builtin_type_char
, /* Type of string elements */
434 struct type
**const (cplus_builtin_types
[]) =
441 &builtin_type_double
,
443 &builtin_type_long_long
,
444 &builtin_type_signed_char
,
445 &builtin_type_unsigned_char
,
446 &builtin_type_unsigned_short
,
447 &builtin_type_unsigned_int
,
448 &builtin_type_unsigned_long
,
449 &builtin_type_unsigned_long_long
,
450 &builtin_type_long_double
,
451 &builtin_type_complex
,
452 &builtin_type_double_complex
,
457 const struct language_defn cplus_language_defn
=
459 "c++", /* Language name */
467 evaluate_subexp_standard
,
468 c_printchar
, /* Print a character constant */
469 c_printstr
, /* Function to print string constant */
470 c_emit_char
, /* Print a single char */
471 c_create_fundamental_type
, /* Create fundamental type in this language */
472 c_print_type
, /* Print a type using appropriate syntax */
473 c_val_print
, /* Print a value using appropriate syntax */
474 c_value_print
, /* Print a top-level value */
475 {"", "", "", ""}, /* Binary format info */
476 {"0%lo", "0", "o", ""}, /* Octal format info */
477 {"%ld", "", "d", ""}, /* Decimal format info */
478 {"0x%lx", "0x", "x", ""}, /* Hex format info */
479 c_op_print_tab
, /* expression operators for printing */
480 1, /* c-style arrays */
481 0, /* String lower bound */
482 &builtin_type_char
, /* Type of string elements */
486 const struct language_defn asm_language_defn
=
488 "asm", /* Language name */
496 evaluate_subexp_standard
,
497 c_printchar
, /* Print a character constant */
498 c_printstr
, /* Function to print string constant */
499 c_emit_char
, /* Print a single char */
500 c_create_fundamental_type
, /* Create fundamental type in this language */
501 c_print_type
, /* Print a type using appropriate syntax */
502 c_val_print
, /* Print a value using appropriate syntax */
503 c_value_print
, /* Print a top-level value */
504 {"", "", "", ""}, /* Binary format info */
505 {"0%lo", "0", "o", ""}, /* Octal format info */
506 {"%ld", "", "d", ""}, /* Decimal format info */
507 {"0x%lx", "0x", "x", ""}, /* Hex format info */
508 c_op_print_tab
, /* expression operators for printing */
509 1, /* c-style arrays */
510 0, /* String lower bound */
511 &builtin_type_char
, /* Type of string elements */
516 _initialize_c_language (void)
518 add_language (&c_language_defn
);
519 add_language (&cplus_language_defn
);
520 add_language (&asm_language_defn
);