1 /* C language support routines for GDB, the GNU debugger.
2 Copyright 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "expression.h"
24 #include "parser-defs.h"
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. */
33 emit_char (c
, stream
, quoter
)
39 c
&= 0xFF; /* Avoid sign bit follies */
41 if (PRINT_LITERAL_FORM (c
))
43 if (c
== '\\' || c
== quoter
)
45 fputs_filtered ("\\", stream
);
47 fprintf_filtered (stream
, "%c", c
);
54 fputs_filtered ("\\n", stream
);
57 fputs_filtered ("\\b", stream
);
60 fputs_filtered ("\\t", stream
);
63 fputs_filtered ("\\f", stream
);
66 fputs_filtered ("\\r", stream
);
69 fputs_filtered ("\\e", stream
);
72 fputs_filtered ("\\a", stream
);
75 fprintf_filtered (stream
, "\\%.3o", (unsigned int) c
);
82 c_printchar (c
, stream
)
86 fputs_filtered ("'", stream
);
87 emit_char (c
, stream
, '\'');
88 fputs_filtered ("'", stream
);
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. */
97 c_printstr (stream
, string
, length
, force_ellipses
)
103 register unsigned int i
;
104 unsigned int things_printed
= 0;
107 extern int inspect_it
;
108 extern int repeat_count_threshold
;
109 extern int print_max
;
113 fputs_filtered ("\"\"", stdout
);
117 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
119 /* Position of the character we are examining
120 to see whether it is repeated. */
122 /* Number of repetitions we have detected so far. */
129 fputs_filtered (", ", stream
);
135 while (rep1
< length
&& string
[rep1
] == string
[i
])
141 if (reps
> repeat_count_threshold
)
146 fputs_filtered ("\\\", ", stream
);
148 fputs_filtered ("\", ", stream
);
151 c_printchar (string
[i
], stream
);
152 fprintf_filtered (stream
, " <repeats %u times>", reps
);
154 things_printed
+= repeat_count_threshold
;
162 fputs_filtered ("\\\"", stream
);
164 fputs_filtered ("\"", stream
);
167 emit_char (string
[i
], stream
, '"');
172 /* Terminate the quotes if necessary. */
176 fputs_filtered ("\\\"", stream
);
178 fputs_filtered ("\"", stream
);
181 if (force_ellipses
|| i
< length
)
182 fputs_filtered ("...", stream
);
185 /* Create a fundamental C type using default reasonable for the current
188 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
189 define fundamental types such as "int" or "double". Others (stabs or
190 DWARF version 2, etc) do define fundamental types. For the formats which
191 don't provide fundamental types, gdb can create such types using this
194 FIXME: Some compilers distinguish explicitly signed integral types
195 (signed short, signed int, signed long) from "regular" integral types
196 (short, int, long) in the debugging information. There is some dis-
197 agreement as to how useful this feature is. In particular, gcc does
198 not support this. Also, only some debugging formats allow the
199 distinction to be passed on to a debugger. For now, we always just
200 use "short", "int", or "long" as the type name, for both the implicit
201 and explicitly signed types. This also makes life easier for the
202 gdb test suite since we don't have to account for the differences
203 in output depending upon what the compiler and debugging format
204 support. We will probably have to re-examine the issue when gdb
205 starts taking it's fundamental type information directly from the
206 debugging information supplied by the compiler. fnf@cygnus.com */
209 c_create_fundamental_type (objfile
, typeid)
210 struct objfile
*objfile
;
213 register struct type
*type
= NULL
;
218 /* FIXME: For now, if we are asked to produce a type not in this
219 language, create the equivalent of a C integer type with the
220 name "<?type?>". When all the dust settles from the type
221 reconstruction work, this should probably become an error. */
222 type
= init_type (TYPE_CODE_INT
,
223 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
224 0, "<?type?>", objfile
);
225 warning ("internal error: no C/C++ fundamental type %d", typeid);
228 type
= init_type (TYPE_CODE_VOID
,
229 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
233 type
= init_type (TYPE_CODE_INT
,
234 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
238 type
= init_type (TYPE_CODE_INT
,
239 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
240 TYPE_FLAG_SIGNED
, "signed char", objfile
);
242 case FT_UNSIGNED_CHAR
:
243 type
= init_type (TYPE_CODE_INT
,
244 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
245 TYPE_FLAG_UNSIGNED
, "unsigned char", objfile
);
248 type
= init_type (TYPE_CODE_INT
,
249 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
250 0, "short", objfile
);
252 case FT_SIGNED_SHORT
:
253 type
= init_type (TYPE_CODE_INT
,
254 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
255 TYPE_FLAG_SIGNED
, "short", objfile
); /* FIXME-fnf */
257 case FT_UNSIGNED_SHORT
:
258 type
= init_type (TYPE_CODE_INT
,
259 TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
260 TYPE_FLAG_UNSIGNED
, "unsigned short", objfile
);
263 type
= init_type (TYPE_CODE_INT
,
264 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
267 case FT_SIGNED_INTEGER
:
268 type
= init_type (TYPE_CODE_INT
,
269 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
270 TYPE_FLAG_SIGNED
, "int", objfile
); /* FIXME -fnf */
272 case FT_UNSIGNED_INTEGER
:
273 type
= init_type (TYPE_CODE_INT
,
274 TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
275 TYPE_FLAG_UNSIGNED
, "unsigned int", objfile
);
278 type
= init_type (TYPE_CODE_INT
,
279 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
283 type
= init_type (TYPE_CODE_INT
,
284 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
285 TYPE_FLAG_SIGNED
, "long", objfile
); /* FIXME -fnf */
287 case FT_UNSIGNED_LONG
:
288 type
= init_type (TYPE_CODE_INT
,
289 TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
290 TYPE_FLAG_UNSIGNED
, "unsigned long", objfile
);
293 type
= init_type (TYPE_CODE_INT
,
294 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
295 0, "long long", objfile
);
297 case FT_SIGNED_LONG_LONG
:
298 type
= init_type (TYPE_CODE_INT
,
299 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
300 TYPE_FLAG_SIGNED
, "signed long long", objfile
);
302 case FT_UNSIGNED_LONG_LONG
:
303 type
= init_type (TYPE_CODE_INT
,
304 TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
305 TYPE_FLAG_UNSIGNED
, "unsigned long long", objfile
);
308 type
= init_type (TYPE_CODE_FLT
,
309 TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
,
310 0, "float", objfile
);
312 case FT_DBL_PREC_FLOAT
:
313 type
= init_type (TYPE_CODE_FLT
,
314 TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
315 0, "double", objfile
);
317 case FT_EXT_PREC_FLOAT
:
318 type
= init_type (TYPE_CODE_FLT
,
319 TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
320 0, "long double", objfile
);
327 /* Table mapping opcodes into strings for printing operators
328 and precedences of the operators. */
330 static const struct op_print c_op_print_tab
[] =
332 {",", BINOP_COMMA
, PREC_COMMA
, 0},
333 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
334 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
335 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
336 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
337 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
338 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
339 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
340 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
341 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
342 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
343 {">", BINOP_GTR
, PREC_ORDER
, 0},
344 {"<", BINOP_LESS
, PREC_ORDER
, 0},
345 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
346 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
347 {"+", BINOP_ADD
, PREC_ADD
, 0},
348 {"-", BINOP_SUB
, PREC_ADD
, 0},
349 {"*", BINOP_MUL
, PREC_MUL
, 0},
350 {"/", BINOP_DIV
, PREC_MUL
, 0},
351 {"%", BINOP_REM
, PREC_MUL
, 0},
352 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
353 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
354 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
355 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
356 {"*", UNOP_IND
, PREC_PREFIX
, 0},
357 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
358 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
359 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
360 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
362 {"::", BINOP_SCOPE
, PREC_PREFIX
, 0},
366 /* These variables point to the objects
367 representing the predefined C data types. */
369 struct type
*builtin_type_void
;
370 struct type
*builtin_type_char
;
371 struct type
*builtin_type_short
;
372 struct type
*builtin_type_int
;
373 struct type
*builtin_type_long
;
374 struct type
*builtin_type_long_long
;
375 struct type
*builtin_type_signed_char
;
376 struct type
*builtin_type_unsigned_char
;
377 struct type
*builtin_type_unsigned_short
;
378 struct type
*builtin_type_unsigned_int
;
379 struct type
*builtin_type_unsigned_long
;
380 struct type
*builtin_type_unsigned_long_long
;
381 struct type
*builtin_type_float
;
382 struct type
*builtin_type_double
;
383 struct type
*builtin_type_long_double
;
384 struct type
*builtin_type_complex
;
385 struct type
*builtin_type_double_complex
;
387 struct type
** const (c_builtin_types
[]) =
394 &builtin_type_double
,
396 &builtin_type_long_long
,
397 &builtin_type_signed_char
,
398 &builtin_type_unsigned_char
,
399 &builtin_type_unsigned_short
,
400 &builtin_type_unsigned_int
,
401 &builtin_type_unsigned_long
,
402 &builtin_type_unsigned_long_long
,
403 &builtin_type_long_double
,
404 &builtin_type_complex
,
405 &builtin_type_double_complex
,
409 const struct language_defn c_language_defn
= {
410 "c", /* Language name */
417 c_printchar
, /* Print a character constant */
418 c_printstr
, /* Function to print string constant */
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 &BUILTIN_TYPE_LONGEST
, /* longest signed integral type */
423 &BUILTIN_TYPE_UNSIGNED_LONGEST
,/* longest unsigned integral type */
424 &builtin_type_double
, /* longest floating point type */ /*FIXME*/
425 {"", "", "", ""}, /* Binary format info */
426 {"0%o", "0", "o", ""}, /* Octal format info */
427 {"%d", "", "d", ""}, /* Decimal format info */
428 {"0x%x", "0x", "x", ""}, /* Hex format info */
429 c_op_print_tab
, /* expression operators for printing */
433 const struct language_defn cplus_language_defn
= {
434 "c++", /* Language name */
441 c_printchar
, /* Print a character constant */
442 c_printstr
, /* Function to print string constant */
443 c_create_fundamental_type
, /* Create fundamental type in this language */
444 c_print_type
, /* Print a type using appropriate syntax */
445 c_val_print
, /* Print a value using appropriate syntax */
446 &BUILTIN_TYPE_LONGEST
, /* longest signed integral type */
447 &BUILTIN_TYPE_UNSIGNED_LONGEST
,/* longest unsigned integral type */
448 &builtin_type_double
, /* longest floating point type */ /*FIXME*/
449 {"", "", "", ""}, /* Binary format info */
450 {"0%o", "0", "o", ""}, /* Octal format info */
451 {"%d", "", "d", ""}, /* Decimal format info */
452 {"0x%x", "0x", "x", ""}, /* Hex format info */
453 c_op_print_tab
, /* expression operators for printing */
458 _initialize_c_language ()
461 init_type (TYPE_CODE_VOID
, 1,
463 "void", (struct objfile
*) NULL
);
465 init_type (TYPE_CODE_INT
, TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
467 "char", (struct objfile
*) NULL
);
468 builtin_type_signed_char
=
469 init_type (TYPE_CODE_INT
, TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
471 "signed char", (struct objfile
*) NULL
);
472 builtin_type_unsigned_char
=
473 init_type (TYPE_CODE_INT
, TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
475 "unsigned char", (struct objfile
*) NULL
);
477 init_type (TYPE_CODE_INT
, TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
479 "short", (struct objfile
*) NULL
);
480 builtin_type_unsigned_short
=
481 init_type (TYPE_CODE_INT
, TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
,
483 "unsigned short", (struct objfile
*) NULL
);
485 init_type (TYPE_CODE_INT
, TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
487 "int", (struct objfile
*) NULL
);
488 builtin_type_unsigned_int
=
489 init_type (TYPE_CODE_INT
, TARGET_INT_BIT
/ TARGET_CHAR_BIT
,
491 "unsigned int", (struct objfile
*) NULL
);
493 init_type (TYPE_CODE_INT
, TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
495 "long", (struct objfile
*) NULL
);
496 builtin_type_unsigned_long
=
497 init_type (TYPE_CODE_INT
, TARGET_LONG_BIT
/ TARGET_CHAR_BIT
,
499 "unsigned long", (struct objfile
*) NULL
);
500 builtin_type_long_long
=
501 init_type (TYPE_CODE_INT
, TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
503 "long long", (struct objfile
*) NULL
);
504 builtin_type_unsigned_long_long
=
505 init_type (TYPE_CODE_INT
, TARGET_LONG_LONG_BIT
/ TARGET_CHAR_BIT
,
507 "unsigned long long", (struct objfile
*) NULL
);
509 init_type (TYPE_CODE_FLT
, TARGET_FLOAT_BIT
/ TARGET_CHAR_BIT
,
511 "float", (struct objfile
*) NULL
);
512 builtin_type_double
=
513 init_type (TYPE_CODE_FLT
, TARGET_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
515 "double", (struct objfile
*) NULL
);
516 builtin_type_long_double
=
517 init_type (TYPE_CODE_FLT
, TARGET_LONG_DOUBLE_BIT
/ TARGET_CHAR_BIT
,
519 "long double", (struct objfile
*) NULL
);
520 builtin_type_complex
=
521 init_type (TYPE_CODE_FLT
, TARGET_COMPLEX_BIT
/ TARGET_CHAR_BIT
,
523 "complex", (struct objfile
*) NULL
);
524 builtin_type_double_complex
=
525 init_type (TYPE_CODE_FLT
, TARGET_DOUBLE_COMPLEX_BIT
/ TARGET_CHAR_BIT
,
527 "double complex", (struct objfile
*) NULL
);
529 add_language (&c_language_defn
);
530 add_language (&cplus_language_defn
);