]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/c-lang.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / c-lang.c
1 /* C language support routines for GDB, the GNU debugger.
2 Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "parser-defs.h"
26 #include "language.h"
27 #include "c-lang.h"
28
29 extern void _initialize_c_language PARAMS ((void));
30 static void c_emit_char PARAMS ((int c, GDB_FILE * stream, int quoter));
31
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. */
35
36 static void
37 c_emit_char (c, stream, quoter)
38 register int c;
39 GDB_FILE *stream;
40 int quoter;
41 {
42 c &= 0xFF; /* Avoid sign bit follies */
43
44 if (PRINT_LITERAL_FORM (c))
45 {
46 if (c == '\\' || c == quoter)
47 {
48 fputs_filtered ("\\", stream);
49 }
50 fprintf_filtered (stream, "%c", c);
51 }
52 else
53 {
54 switch (c)
55 {
56 case '\n':
57 fputs_filtered ("\\n", stream);
58 break;
59 case '\b':
60 fputs_filtered ("\\b", stream);
61 break;
62 case '\t':
63 fputs_filtered ("\\t", stream);
64 break;
65 case '\f':
66 fputs_filtered ("\\f", stream);
67 break;
68 case '\r':
69 fputs_filtered ("\\r", stream);
70 break;
71 case '\033':
72 fputs_filtered ("\\e", stream);
73 break;
74 case '\007':
75 fputs_filtered ("\\a", stream);
76 break;
77 default:
78 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
79 break;
80 }
81 }
82 }
83
84 void
85 c_printchar (c, stream)
86 int c;
87 GDB_FILE *stream;
88 {
89 fputc_filtered ('\'', stream);
90 LA_EMIT_CHAR (c, stream, '\'');
91 fputc_filtered ('\'', stream);
92 }
93
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. */
99
100 void
101 c_printstr (stream, string, length, width, force_ellipses)
102 GDB_FILE *stream;
103 char *string;
104 unsigned int length;
105 int width;
106 int force_ellipses;
107 {
108 register unsigned int i;
109 unsigned int things_printed = 0;
110 int in_quotes = 0;
111 int need_comma = 0;
112 extern int inspect_it;
113 extern int repeat_count_threshold;
114 extern int print_max;
115
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
118 style. */
119 if (!force_ellipses
120 && length > 0
121 && extract_unsigned_integer (string + (length - 1) * width, width) == '\0')
122 length--;
123
124 if (length == 0)
125 {
126 fputs_filtered ("\"\"", stream);
127 return;
128 }
129
130 for (i = 0; i < length && things_printed < print_max; ++i)
131 {
132 /* Position of the character we are examining
133 to see whether it is repeated. */
134 unsigned int rep1;
135 /* Number of repetitions we have detected so far. */
136 unsigned int reps;
137 unsigned long current_char;
138
139 QUIT;
140
141 if (need_comma)
142 {
143 fputs_filtered (", ", stream);
144 need_comma = 0;
145 }
146
147 current_char = extract_unsigned_integer (string + i * width, width);
148
149 rep1 = i + 1;
150 reps = 1;
151 while (rep1 < length
152 && extract_unsigned_integer (string + rep1 * width, width)
153 == current_char)
154 {
155 ++rep1;
156 ++reps;
157 }
158
159 if (reps > repeat_count_threshold)
160 {
161 if (in_quotes)
162 {
163 if (inspect_it)
164 fputs_filtered ("\\\", ", stream);
165 else
166 fputs_filtered ("\", ", stream);
167 in_quotes = 0;
168 }
169 LA_PRINT_CHAR (current_char, stream);
170 fprintf_filtered (stream, " <repeats %u times>", reps);
171 i = rep1 - 1;
172 things_printed += repeat_count_threshold;
173 need_comma = 1;
174 }
175 else
176 {
177 if (!in_quotes)
178 {
179 if (inspect_it)
180 fputs_filtered ("\\\"", stream);
181 else
182 fputs_filtered ("\"", stream);
183 in_quotes = 1;
184 }
185 LA_EMIT_CHAR (current_char, stream, '"');
186 ++things_printed;
187 }
188 }
189
190 /* Terminate the quotes if necessary. */
191 if (in_quotes)
192 {
193 if (inspect_it)
194 fputs_filtered ("\\\"", stream);
195 else
196 fputs_filtered ("\"", stream);
197 }
198
199 if (force_ellipses || i < length)
200 fputs_filtered ("...", stream);
201 }
202
203 /* Create a fundamental C type using default reasonable for the current
204 target machine.
205
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
210 function.
211
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 */
225
226 struct type *
227 c_create_fundamental_type (objfile, typeid)
228 struct objfile *objfile;
229 int typeid;
230 {
231 register struct type *type = NULL;
232
233 switch (typeid)
234 {
235 default:
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);
244 break;
245 case FT_VOID:
246 type = init_type (TYPE_CODE_VOID,
247 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
248 0, "void", objfile);
249 break;
250 case FT_BOOLEAN:
251 type = init_type (TYPE_CODE_BOOL,
252 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
253 0, "bool", objfile);
254
255 break;
256 case FT_CHAR:
257 type = init_type (TYPE_CODE_INT,
258 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
259 0, "char", objfile);
260 TYPE_FLAGS (type) |= TYPE_FLAG_NOSIGN;
261 break;
262 case FT_SIGNED_CHAR:
263 type = init_type (TYPE_CODE_INT,
264 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
265 0, "signed char", objfile);
266 break;
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);
271 break;
272 case FT_SHORT:
273 type = init_type (TYPE_CODE_INT,
274 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
275 0, "short", objfile);
276 break;
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 */
281 break;
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);
286 break;
287 case FT_INTEGER:
288 type = init_type (TYPE_CODE_INT,
289 TARGET_INT_BIT / TARGET_CHAR_BIT,
290 0, "int", objfile);
291 break;
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 */
296 break;
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);
301 break;
302 case FT_LONG:
303 type = init_type (TYPE_CODE_INT,
304 TARGET_LONG_BIT / TARGET_CHAR_BIT,
305 0, "long", objfile);
306 break;
307 case FT_SIGNED_LONG:
308 type = init_type (TYPE_CODE_INT,
309 TARGET_LONG_BIT / TARGET_CHAR_BIT,
310 0, "long", objfile); /* FIXME -fnf */
311 break;
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);
316 break;
317 case FT_LONG_LONG:
318 type = init_type (TYPE_CODE_INT,
319 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
320 0, "long long", objfile);
321 break;
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);
326 break;
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);
331 break;
332 case FT_FLOAT:
333 type = init_type (TYPE_CODE_FLT,
334 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
335 0, "float", objfile);
336 break;
337 case FT_DBL_PREC_FLOAT:
338 type = init_type (TYPE_CODE_FLT,
339 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
340 0, "double", objfile);
341 break;
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);
346 break;
347 case FT_TEMPLATE_ARG:
348 type = init_type (TYPE_CODE_TEMPLATE_ARG,
349 0,
350 0, "<template arg>", objfile);
351
352 break;
353 }
354 return (type);
355 }
356 \f
357
358 /* Table mapping opcodes into strings for printing operators
359 and precedences of the operators. */
360
361 const struct op_print c_op_print_tab[] =
362 {
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},
392 /* C++ */
393 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
394 {NULL, 0, 0, 0}
395 };
396 \f
397 struct type **CONST_PTR (c_builtin_types[]) =
398 {
399 &builtin_type_int,
400 &builtin_type_long,
401 &builtin_type_short,
402 &builtin_type_char,
403 &builtin_type_float,
404 &builtin_type_double,
405 &builtin_type_void,
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,
416 0
417 };
418
419 const struct language_defn c_language_defn =
420 {
421 "c", /* Language name */
422 language_c,
423 c_builtin_types,
424 range_check_off,
425 type_check_off,
426 c_parse,
427 c_error,
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 */
444 LANG_MAGIC
445 };
446
447 struct type **const (cplus_builtin_types[]) =
448 {
449 &builtin_type_int,
450 &builtin_type_long,
451 &builtin_type_short,
452 &builtin_type_char,
453 &builtin_type_float,
454 &builtin_type_double,
455 &builtin_type_void,
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,
466 &builtin_type_bool,
467 0
468 };
469
470 const struct language_defn cplus_language_defn =
471 {
472 "c++", /* Language name */
473 language_cplus,
474 cplus_builtin_types,
475 range_check_off,
476 type_check_off,
477 c_parse,
478 c_error,
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 */
495 LANG_MAGIC
496 };
497
498 const struct language_defn asm_language_defn =
499 {
500 "asm", /* Language name */
501 language_asm,
502 c_builtin_types,
503 range_check_off,
504 type_check_off,
505 c_parse,
506 c_error,
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 */
523 LANG_MAGIC
524 };
525
526 void
527 _initialize_c_language ()
528 {
529 add_language (&c_language_defn);
530 add_language (&cplus_language_defn);
531 add_language (&asm_language_defn);
532 }