2002-04-09 Daniel Jacobowitz <drow@mvista.com>
[deliverable/binutils-gdb.git] / gdb / c-lang.c
CommitLineData
c906108c 1/* C language support routines for GDB, the GNU debugger.
6c6ea35e 2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002
b6ba6518 3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "expression.h"
26#include "parser-defs.h"
27#include "language.h"
28#include "c-lang.h"
745b8ca0 29#include "valprint.h"
c906108c 30
a14ed312 31extern void _initialize_c_language (void);
d9fcf2fb 32static void c_emit_char (int c, struct ui_file * stream, int quoter);
c906108c
SS
33
34/* Print the character C on STREAM as part of the contents of a literal
35 string whose delimiter is QUOTER. Note that that format for printing
36 characters and strings is language specific. */
37
38static void
fba45db2 39c_emit_char (register int c, struct ui_file *stream, int quoter)
c906108c
SS
40{
41 c &= 0xFF; /* Avoid sign bit follies */
42
43 if (PRINT_LITERAL_FORM (c))
44 {
45 if (c == '\\' || c == quoter)
46 {
47 fputs_filtered ("\\", stream);
48 }
49 fprintf_filtered (stream, "%c", c);
50 }
51 else
52 {
53 switch (c)
54 {
55 case '\n':
56 fputs_filtered ("\\n", stream);
57 break;
58 case '\b':
59 fputs_filtered ("\\b", stream);
60 break;
61 case '\t':
62 fputs_filtered ("\\t", stream);
63 break;
64 case '\f':
65 fputs_filtered ("\\f", stream);
66 break;
67 case '\r':
68 fputs_filtered ("\\r", stream);
69 break;
ae23c7a3
JB
70 case '\013':
71 fputs_filtered ("\\v", stream);
72 break;
c906108c
SS
73 case '\033':
74 fputs_filtered ("\\e", stream);
75 break;
76 case '\007':
77 fputs_filtered ("\\a", stream);
78 break;
ae23c7a3
JB
79 case '\0':
80 fputs_filtered ("\\0", stream);
81 break;
c906108c
SS
82 default:
83 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
84 break;
85 }
86 }
87}
88
89void
fba45db2 90c_printchar (int c, struct ui_file *stream)
c906108c
SS
91{
92 fputc_filtered ('\'', stream);
93 LA_EMIT_CHAR (c, stream, '\'');
94 fputc_filtered ('\'', stream);
95}
96
97/* Print the character string STRING, printing at most LENGTH characters.
98 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
99 long. Printing stops early if the number hits print_max; repeat counts are
100 printed as appropriate. Print ellipses at the end if we had to stop before
101 printing LENGTH characters, or if FORCE_ELLIPSES. */
102
103void
fba45db2
KB
104c_printstr (struct ui_file *stream, char *string, unsigned int length,
105 int width, int force_ellipses)
c906108c
SS
106{
107 register unsigned int i;
108 unsigned int things_printed = 0;
109 int in_quotes = 0;
110 int need_comma = 0;
111 extern int inspect_it;
c906108c
SS
112
113 /* If the string was not truncated due to `set print elements', and
114 the last byte of it is a null, we don't print that, in traditional C
115 style. */
116 if (!force_ellipses
117 && length > 0
78a51202
JB
118 && (extract_unsigned_integer (string + (length - 1) * width, width)
119 == '\0'))
c906108c
SS
120 length--;
121
122 if (length == 0)
123 {
124 fputs_filtered ("\"\"", stream);
125 return;
126 }
127
128 for (i = 0; i < length && things_printed < print_max; ++i)
129 {
130 /* Position of the character we are examining
c5aa993b 131 to see whether it is repeated. */
c906108c
SS
132 unsigned int rep1;
133 /* Number of repetitions we have detected so far. */
134 unsigned int reps;
135 unsigned long current_char;
136
137 QUIT;
138
139 if (need_comma)
140 {
141 fputs_filtered (", ", stream);
142 need_comma = 0;
143 }
144
145 current_char = extract_unsigned_integer (string + i * width, width);
146
147 rep1 = i + 1;
148 reps = 1;
149 while (rep1 < length
150 && extract_unsigned_integer (string + rep1 * width, width)
c5aa993b 151 == current_char)
c906108c
SS
152 {
153 ++rep1;
154 ++reps;
155 }
156
157 if (reps > repeat_count_threshold)
158 {
159 if (in_quotes)
160 {
161 if (inspect_it)
162 fputs_filtered ("\\\", ", stream);
163 else
164 fputs_filtered ("\", ", stream);
165 in_quotes = 0;
166 }
167 LA_PRINT_CHAR (current_char, stream);
168 fprintf_filtered (stream, " <repeats %u times>", reps);
169 i = rep1 - 1;
170 things_printed += repeat_count_threshold;
171 need_comma = 1;
172 }
173 else
174 {
175 if (!in_quotes)
176 {
177 if (inspect_it)
178 fputs_filtered ("\\\"", stream);
179 else
180 fputs_filtered ("\"", stream);
181 in_quotes = 1;
182 }
183 LA_EMIT_CHAR (current_char, stream, '"');
184 ++things_printed;
185 }
186 }
187
188 /* Terminate the quotes if necessary. */
189 if (in_quotes)
190 {
191 if (inspect_it)
192 fputs_filtered ("\\\"", stream);
193 else
194 fputs_filtered ("\"", stream);
195 }
196
197 if (force_ellipses || i < length)
198 fputs_filtered ("...", stream);
199}
200
201/* Create a fundamental C type using default reasonable for the current
202 target machine.
203
204 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
205 define fundamental types such as "int" or "double". Others (stabs or
206 DWARF version 2, etc) do define fundamental types. For the formats which
207 don't provide fundamental types, gdb can create such types using this
208 function.
209
210 FIXME: Some compilers distinguish explicitly signed integral types
211 (signed short, signed int, signed long) from "regular" integral types
212 (short, int, long) in the debugging information. There is some dis-
213 agreement as to how useful this feature is. In particular, gcc does
214 not support this. Also, only some debugging formats allow the
215 distinction to be passed on to a debugger. For now, we always just
216 use "short", "int", or "long" as the type name, for both the implicit
217 and explicitly signed types. This also makes life easier for the
218 gdb test suite since we don't have to account for the differences
219 in output depending upon what the compiler and debugging format
220 support. We will probably have to re-examine the issue when gdb
221 starts taking it's fundamental type information directly from the
222 debugging information supplied by the compiler. fnf@cygnus.com */
223
224struct type *
fba45db2 225c_create_fundamental_type (struct objfile *objfile, int typeid)
c906108c
SS
226{
227 register struct type *type = NULL;
228
229 switch (typeid)
230 {
c5aa993b
JM
231 default:
232 /* FIXME: For now, if we are asked to produce a type not in this
233 language, create the equivalent of a C integer type with the
234 name "<?type?>". When all the dust settles from the type
235 reconstruction work, this should probably become an error. */
236 type = init_type (TYPE_CODE_INT,
237 TARGET_INT_BIT / TARGET_CHAR_BIT,
238 0, "<?type?>", objfile);
239 warning ("internal error: no C/C++ fundamental type %d", typeid);
240 break;
241 case FT_VOID:
242 type = init_type (TYPE_CODE_VOID,
243 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
244 0, "void", objfile);
245 break;
246 case FT_BOOLEAN:
247 type = init_type (TYPE_CODE_BOOL,
248 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
249 0, "bool", objfile);
c5aa993b
JM
250 break;
251 case FT_CHAR:
252 type = init_type (TYPE_CODE_INT,
253 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
6edc140f 254 TYPE_FLAG_NOSIGN, "char", objfile);
c5aa993b
JM
255 break;
256 case FT_SIGNED_CHAR:
257 type = init_type (TYPE_CODE_INT,
258 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
259 0, "signed char", objfile);
260 break;
261 case FT_UNSIGNED_CHAR:
262 type = init_type (TYPE_CODE_INT,
263 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
264 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
265 break;
266 case FT_SHORT:
267 type = init_type (TYPE_CODE_INT,
268 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
269 0, "short", objfile);
270 break;
271 case FT_SIGNED_SHORT:
272 type = init_type (TYPE_CODE_INT,
273 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
274 0, "short", objfile); /* FIXME-fnf */
275 break;
276 case FT_UNSIGNED_SHORT:
277 type = init_type (TYPE_CODE_INT,
278 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
279 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
280 break;
281 case FT_INTEGER:
282 type = init_type (TYPE_CODE_INT,
283 TARGET_INT_BIT / TARGET_CHAR_BIT,
284 0, "int", objfile);
285 break;
286 case FT_SIGNED_INTEGER:
287 type = init_type (TYPE_CODE_INT,
288 TARGET_INT_BIT / TARGET_CHAR_BIT,
289 0, "int", objfile); /* FIXME -fnf */
290 break;
291 case FT_UNSIGNED_INTEGER:
292 type = init_type (TYPE_CODE_INT,
293 TARGET_INT_BIT / TARGET_CHAR_BIT,
294 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
295 break;
296 case FT_LONG:
297 type = init_type (TYPE_CODE_INT,
298 TARGET_LONG_BIT / TARGET_CHAR_BIT,
299 0, "long", objfile);
300 break;
301 case FT_SIGNED_LONG:
302 type = init_type (TYPE_CODE_INT,
303 TARGET_LONG_BIT / TARGET_CHAR_BIT,
304 0, "long", objfile); /* FIXME -fnf */
305 break;
306 case FT_UNSIGNED_LONG:
307 type = init_type (TYPE_CODE_INT,
308 TARGET_LONG_BIT / TARGET_CHAR_BIT,
309 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
310 break;
311 case FT_LONG_LONG:
312 type = init_type (TYPE_CODE_INT,
313 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
314 0, "long long", objfile);
315 break;
316 case FT_SIGNED_LONG_LONG:
317 type = init_type (TYPE_CODE_INT,
318 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
319 0, "signed long long", objfile);
320 break;
321 case FT_UNSIGNED_LONG_LONG:
322 type = init_type (TYPE_CODE_INT,
323 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
324 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
325 break;
326 case FT_FLOAT:
327 type = init_type (TYPE_CODE_FLT,
328 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
329 0, "float", objfile);
330 break;
331 case FT_DBL_PREC_FLOAT:
332 type = init_type (TYPE_CODE_FLT,
333 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
334 0, "double", objfile);
335 break;
336 case FT_EXT_PREC_FLOAT:
337 type = init_type (TYPE_CODE_FLT,
338 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
339 0, "long double", objfile);
340 break;
f65ca430
DJ
341 case FT_COMPLEX:
342 type = init_type (TYPE_CODE_FLT,
343 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
344 0, "complex float", objfile);
345 TYPE_TARGET_TYPE (type)
346 = init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
347 0, "float", objfile);
348 break;
349 case FT_DBL_PREC_COMPLEX:
350 type = init_type (TYPE_CODE_FLT,
351 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
352 0, "complex double", objfile);
353 TYPE_TARGET_TYPE (type)
354 = init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
355 0, "double", objfile);
356 break;
357 case FT_EXT_PREC_COMPLEX:
358 type = init_type (TYPE_CODE_FLT,
359 2 * TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
360 0, "complex long double", objfile);
361 TYPE_TARGET_TYPE (type)
362 = init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
363 0, "long double", objfile);
364 break;
c5aa993b
JM
365 case FT_TEMPLATE_ARG:
366 type = init_type (TYPE_CODE_TEMPLATE_ARG,
367 0,
368 0, "<template arg>", objfile);
c5aa993b
JM
369 break;
370 }
c906108c
SS
371 return (type);
372}
c906108c 373\f
c5aa993b 374
c906108c
SS
375/* Table mapping opcodes into strings for printing operators
376 and precedences of the operators. */
377
378const struct op_print c_op_print_tab[] =
c5aa993b
JM
379{
380 {",", BINOP_COMMA, PREC_COMMA, 0},
381 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
382 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
383 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
384 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
385 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
386 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
387 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
388 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
389 {"<=", BINOP_LEQ, PREC_ORDER, 0},
390 {">=", BINOP_GEQ, PREC_ORDER, 0},
391 {">", BINOP_GTR, PREC_ORDER, 0},
392 {"<", BINOP_LESS, PREC_ORDER, 0},
393 {">>", BINOP_RSH, PREC_SHIFT, 0},
394 {"<<", BINOP_LSH, PREC_SHIFT, 0},
395 {"+", BINOP_ADD, PREC_ADD, 0},
396 {"-", BINOP_SUB, PREC_ADD, 0},
397 {"*", BINOP_MUL, PREC_MUL, 0},
398 {"/", BINOP_DIV, PREC_MUL, 0},
399 {"%", BINOP_REM, PREC_MUL, 0},
400 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
401 {"-", UNOP_NEG, PREC_PREFIX, 0},
402 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
403 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
404 {"*", UNOP_IND, PREC_PREFIX, 0},
405 {"&", UNOP_ADDR, PREC_PREFIX, 0},
406 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
407 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
408 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
c5aa993b 409 {NULL, 0, 0, 0}
c906108c
SS
410};
411\f
6c6ea35e 412struct type **const (c_builtin_types[]) =
c906108c
SS
413{
414 &builtin_type_int,
78a51202
JB
415 &builtin_type_long,
416 &builtin_type_short,
417 &builtin_type_char,
418 &builtin_type_float,
419 &builtin_type_double,
420 &builtin_type_void,
421 &builtin_type_long_long,
422 &builtin_type_signed_char,
423 &builtin_type_unsigned_char,
424 &builtin_type_unsigned_short,
425 &builtin_type_unsigned_int,
426 &builtin_type_unsigned_long,
427 &builtin_type_unsigned_long_long,
428 &builtin_type_long_double,
429 &builtin_type_complex,
430 &builtin_type_double_complex,
431 0
c906108c
SS
432};
433
c5aa993b
JM
434const struct language_defn c_language_defn =
435{
c906108c
SS
436 "c", /* Language name */
437 language_c,
438 c_builtin_types,
439 range_check_off,
440 type_check_off,
63872f9d 441 case_sensitive_on,
c906108c
SS
442 c_parse,
443 c_error,
444 evaluate_subexp_standard,
445 c_printchar, /* Print a character constant */
446 c_printstr, /* Function to print string constant */
447 c_emit_char, /* Print a single char */
448 c_create_fundamental_type, /* Create fundamental type in this language */
449 c_print_type, /* Print a type using appropriate syntax */
450 c_val_print, /* Print a value using appropriate syntax */
451 c_value_print, /* Print a top-level value */
c5aa993b
JM
452 {"", "", "", ""}, /* Binary format info */
453 {"0%lo", "0", "o", ""}, /* Octal format info */
454 {"%ld", "", "d", ""}, /* Decimal format info */
455 {"0x%lx", "0x", "x", ""}, /* Hex format info */
c906108c
SS
456 c_op_print_tab, /* expression operators for printing */
457 1, /* c-style arrays */
458 0, /* String lower bound */
c5aa993b 459 &builtin_type_char, /* Type of string elements */
c906108c
SS
460 LANG_MAGIC
461};
462
c5aa993b 463struct type **const (cplus_builtin_types[]) =
c906108c
SS
464{
465 &builtin_type_int,
78a51202
JB
466 &builtin_type_long,
467 &builtin_type_short,
468 &builtin_type_char,
469 &builtin_type_float,
470 &builtin_type_double,
471 &builtin_type_void,
472 &builtin_type_long_long,
473 &builtin_type_signed_char,
474 &builtin_type_unsigned_char,
475 &builtin_type_unsigned_short,
476 &builtin_type_unsigned_int,
477 &builtin_type_unsigned_long,
478 &builtin_type_unsigned_long_long,
479 &builtin_type_long_double,
480 &builtin_type_complex,
481 &builtin_type_double_complex,
482 &builtin_type_bool,
483 0
c906108c
SS
484};
485
c5aa993b
JM
486const struct language_defn cplus_language_defn =
487{
488 "c++", /* Language name */
c906108c
SS
489 language_cplus,
490 cplus_builtin_types,
491 range_check_off,
492 type_check_off,
63872f9d 493 case_sensitive_on,
c906108c
SS
494 c_parse,
495 c_error,
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 */
c5aa993b
JM
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 */
c906108c
SS
508 c_op_print_tab, /* expression operators for printing */
509 1, /* c-style arrays */
510 0, /* String lower bound */
c5aa993b 511 &builtin_type_char, /* Type of string elements */
c906108c
SS
512 LANG_MAGIC
513};
514
c5aa993b
JM
515const struct language_defn asm_language_defn =
516{
c906108c
SS
517 "asm", /* Language name */
518 language_asm,
519 c_builtin_types,
520 range_check_off,
521 type_check_off,
63872f9d 522 case_sensitive_on,
c906108c
SS
523 c_parse,
524 c_error,
525 evaluate_subexp_standard,
526 c_printchar, /* Print a character constant */
527 c_printstr, /* Function to print string constant */
528 c_emit_char, /* Print a single char */
529 c_create_fundamental_type, /* Create fundamental type in this language */
530 c_print_type, /* Print a type using appropriate syntax */
531 c_val_print, /* Print a value using appropriate syntax */
532 c_value_print, /* Print a top-level value */
c5aa993b
JM
533 {"", "", "", ""}, /* Binary format info */
534 {"0%lo", "0", "o", ""}, /* Octal format info */
535 {"%ld", "", "d", ""}, /* Decimal format info */
536 {"0x%lx", "0x", "x", ""}, /* Hex format info */
c906108c
SS
537 c_op_print_tab, /* expression operators for printing */
538 1, /* c-style arrays */
539 0, /* String lower bound */
c5aa993b 540 &builtin_type_char, /* Type of string elements */
c906108c
SS
541 LANG_MAGIC
542};
543
544void
fba45db2 545_initialize_c_language (void)
c906108c
SS
546{
547 add_language (&c_language_defn);
548 add_language (&cplus_language_defn);
549 add_language (&asm_language_defn);
550}
This page took 0.163134 seconds and 4 git commands to generate.