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;
219 /* FIXME: For now, if we are asked to produce a type not in this
220 language, create the equivalent of a C integer type with the
221 name "<?type?>". When all the dust settles from the type
222 reconstruction work, this should probably become an error. */
223 type = init_type (TYPE_CODE_INT,
224 TARGET_INT_BIT / TARGET_CHAR_BIT,
225 0, "<?type?>", objfile);
226 warning ("internal error: no C/C++ fundamental type %d", typeid);
229 type = init_type (TYPE_CODE_VOID,
230 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
234 type = init_type (TYPE_CODE_INT,
235 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
239 type = init_type (TYPE_CODE_INT,
240 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
241 TYPE_FLAG_SIGNED, "signed char", objfile);
243 case FT_UNSIGNED_CHAR:
244 type = init_type (TYPE_CODE_INT,
245 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
246 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
249 type = init_type (TYPE_CODE_INT,
250 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
251 0, "short", objfile);
253 case FT_SIGNED_SHORT:
254 type = init_type (TYPE_CODE_INT,
255 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
256 TYPE_FLAG_SIGNED, "short", objfile); /* FIXME-fnf */
258 case FT_UNSIGNED_SHORT:
259 type = init_type (TYPE_CODE_INT,
260 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
261 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
264 type = init_type (TYPE_CODE_INT,
265 TARGET_INT_BIT / TARGET_CHAR_BIT,
268 case FT_SIGNED_INTEGER:
269 type = init_type (TYPE_CODE_INT,
270 TARGET_INT_BIT / TARGET_CHAR_BIT,
271 TYPE_FLAG_SIGNED, "int", objfile); /* FIXME -fnf */
273 case FT_UNSIGNED_INTEGER:
274 type = init_type (TYPE_CODE_INT,
275 TARGET_INT_BIT / TARGET_CHAR_BIT,
276 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
279 type = init_type (TYPE_CODE_INT,
280 TARGET_LONG_BIT / TARGET_CHAR_BIT,
284 type = init_type (TYPE_CODE_INT,
285 TARGET_LONG_BIT / TARGET_CHAR_BIT,
286 TYPE_FLAG_SIGNED, "long", objfile); /* FIXME -fnf */
288 case FT_UNSIGNED_LONG:
289 type = init_type (TYPE_CODE_INT,
290 TARGET_LONG_BIT / TARGET_CHAR_BIT,
291 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
294 type = init_type (TYPE_CODE_INT,
295 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
296 0, "long long", objfile);
298 case FT_SIGNED_LONG_LONG:
299 type = init_type (TYPE_CODE_INT,
300 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
301 TYPE_FLAG_SIGNED, "signed long long", objfile);
303 case FT_UNSIGNED_LONG_LONG:
304 type = init_type (TYPE_CODE_INT,
305 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
306 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
309 type = init_type (TYPE_CODE_FLT,
310 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
311 0, "float", objfile);
313 case FT_DBL_PREC_FLOAT:
314 type = init_type (TYPE_CODE_FLT,
315 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
316 0, "double", objfile);
318 case FT_EXT_PREC_FLOAT:
319 type = init_type (TYPE_CODE_FLT,
320 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
321 0, "long double", objfile);
328 /* Table mapping opcodes into strings for printing operators
329 and precedences of the operators. */
331 const static struct op_print c_op_print_tab[] =
333 {",", BINOP_COMMA, PREC_COMMA, 0},
334 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
335 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
336 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
337 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
338 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
339 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
340 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
341 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
342 {"<=", BINOP_LEQ, PREC_ORDER, 0},
343 {">=", BINOP_GEQ, PREC_ORDER, 0},
344 {">", BINOP_GTR, PREC_ORDER, 0},
345 {"<", BINOP_LESS, PREC_ORDER, 0},
346 {">>", BINOP_RSH, PREC_SHIFT, 0},
347 {"<<", BINOP_LSH, PREC_SHIFT, 0},
348 {"+", BINOP_ADD, PREC_ADD, 0},
349 {"-", BINOP_SUB, PREC_ADD, 0},
350 {"*", BINOP_MUL, PREC_MUL, 0},
351 {"/", BINOP_DIV, PREC_MUL, 0},
352 {"%", BINOP_REM, PREC_MUL, 0},
353 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
354 {"-", UNOP_NEG, PREC_PREFIX, 0},
355 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
356 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
357 {"*", UNOP_IND, PREC_PREFIX, 0},
358 {"&", UNOP_ADDR, PREC_PREFIX, 0},
359 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
360 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
361 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
363 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
367 /* These variables point to the objects
368 representing the predefined C data types. */
370 struct type *builtin_type_void;
371 struct type *builtin_type_char;
372 struct type *builtin_type_short;
373 struct type *builtin_type_int;
374 struct type *builtin_type_long;
375 struct type *builtin_type_long_long;
376 struct type *builtin_type_signed_char;
377 struct type *builtin_type_unsigned_char;
378 struct type *builtin_type_unsigned_short;
379 struct type *builtin_type_unsigned_int;
380 struct type *builtin_type_unsigned_long;
381 struct type *builtin_type_unsigned_long_long;
382 struct type *builtin_type_float;
383 struct type *builtin_type_double;
384 struct type *builtin_type_long_double;
385 struct type *builtin_type_complex;
386 struct type *builtin_type_double_complex;
388 struct type ** const (c_builtin_types[]) =
395 &builtin_type_double,
397 &builtin_type_long_long,
398 &builtin_type_signed_char,
399 &builtin_type_unsigned_char,
400 &builtin_type_unsigned_short,
401 &builtin_type_unsigned_int,
402 &builtin_type_unsigned_long,
403 &builtin_type_unsigned_long_long,
404 &builtin_type_long_double,
405 &builtin_type_complex,
406 &builtin_type_double_complex,
410 const struct language_defn c_language_defn = {
411 "c", /* Language name */
418 c_printchar, /* Print a character constant */
419 c_printstr, /* Function to print string constant */
420 c_create_fundamental_type, /* Create fundamental type in this language */
421 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
422 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
423 &builtin_type_double, /* longest floating point type */ /*FIXME*/
424 {"", "", "", ""}, /* Binary format info */
425 {"0%o", "0", "o", ""}, /* Octal format info */
426 {"%d", "", "d", ""}, /* Decimal format info */
427 {"0x%x", "0x", "x", ""}, /* Hex format info */
428 c_op_print_tab, /* expression operators for printing */
432 const struct language_defn cplus_language_defn = {
433 "c++", /* Language name */
440 c_printchar, /* Print a character constant */
441 c_printstr, /* Function to print string constant */
442 c_create_fundamental_type, /* Create fundamental type in this language */
443 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
444 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
445 &builtin_type_double, /* longest floating point type */ /*FIXME*/
446 {"", "", "", ""}, /* Binary format info */
447 {"0%o", "0", "o", ""}, /* Octal format info */
448 {"%d", "", "d", ""}, /* Decimal format info */
449 {"0x%x", "0x", "x", ""}, /* Hex format info */
450 c_op_print_tab, /* expression operators for printing */
458 init_type (TYPE_CODE_VOID, 1,
460 "void", (struct objfile *) NULL);
462 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
464 "char", (struct objfile *) NULL);
465 builtin_type_signed_char =
466 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
468 "signed char", (struct objfile *) NULL);
469 builtin_type_unsigned_char =
470 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
472 "unsigned char", (struct objfile *) NULL);
474 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
476 "short", (struct objfile *) NULL);
477 builtin_type_unsigned_short =
478 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
480 "unsigned short", (struct objfile *) NULL);
482 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
484 "int", (struct objfile *) NULL);
485 builtin_type_unsigned_int =
486 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
488 "unsigned int", (struct objfile *) NULL);
490 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
492 "long", (struct objfile *) NULL);
493 builtin_type_unsigned_long =
494 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
496 "unsigned long", (struct objfile *) NULL);
497 builtin_type_long_long =
498 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
500 "long long", (struct objfile *) NULL);
501 builtin_type_unsigned_long_long =
502 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
504 "unsigned long long", (struct objfile *) NULL);
506 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
508 "float", (struct objfile *) NULL);
509 builtin_type_double =
510 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
512 "double", (struct objfile *) NULL);
513 builtin_type_long_double =
514 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
516 "long double", (struct objfile *) NULL);
517 builtin_type_complex =
518 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
520 "complex", (struct objfile *) NULL);
521 builtin_type_double_complex =
522 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
524 "double complex", (struct objfile *) NULL);
526 add_language (&c_language_defn);
527 add_language (&cplus_language_defn);