]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/c-lang.c
See gdb ChangeLog entry with header:
[thirdparty/binutils-gdb.git] / gdb / c-lang.c
CommitLineData
22e39759 1/* C language support routines for GDB, the GNU debugger.
cef0333e 2 Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
22e39759
FF
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
6c9638b4 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22e39759
FF
19
20#include "defs.h"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "expression.h"
24#include "parser-defs.h"
25#include "language.h"
26#include "c-lang.h"
27
b607efe7
FF
28static void emit_char PARAMS ((int, GDB_FILE *, int));
29
22e39759
FF
30/* Print the character C on STREAM as part of the contents of a literal
31 string whose delimiter is QUOTER. Note that that format for printing
32 characters and strings is language specific. */
33
34static void
35emit_char (c, stream, quoter)
36 register int c;
199b2450 37 GDB_FILE *stream;
22e39759
FF
38 int quoter;
39{
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;
70 case '\033':
71 fputs_filtered ("\\e", stream);
72 break;
73 case '\007':
74 fputs_filtered ("\\a", stream);
75 break;
76 default:
77 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
78 break;
79 }
80 }
81}
82
5e548861 83void
22e39759
FF
84c_printchar (c, stream)
85 int c;
199b2450 86 GDB_FILE *stream;
22e39759
FF
87{
88 fputs_filtered ("'", stream);
89 emit_char (c, stream, '\'');
90 fputs_filtered ("'", stream);
91}
92
93/* Print the character string STRING, printing at most LENGTH characters.
94 Printing stops early if the number hits print_max; repeat counts
95 are printed as appropriate. Print ellipses at the end if we
96 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
97
5e548861 98void
22e39759 99c_printstr (stream, string, length, force_ellipses)
199b2450 100 GDB_FILE *stream;
22e39759
FF
101 char *string;
102 unsigned int length;
103 int force_ellipses;
104{
105 register unsigned int i;
106 unsigned int things_printed = 0;
107 int in_quotes = 0;
108 int need_comma = 0;
109 extern int inspect_it;
110 extern int repeat_count_threshold;
111 extern int print_max;
112
c5c00171
JG
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. */
961ccde6 116 if ((!force_ellipses) && length > 0 && string[length-1] == '\0')
c5c00171
JG
117 length--;
118
22e39759
FF
119 if (length == 0)
120 {
55768580 121 fputs_filtered ("\"\"", stream);
22e39759
FF
122 return;
123 }
124
125 for (i = 0; i < length && things_printed < print_max; ++i)
126 {
127 /* Position of the character we are examining
128 to see whether it is repeated. */
129 unsigned int rep1;
130 /* Number of repetitions we have detected so far. */
131 unsigned int reps;
132
133 QUIT;
134
135 if (need_comma)
136 {
137 fputs_filtered (", ", stream);
138 need_comma = 0;
139 }
140
141 rep1 = i + 1;
142 reps = 1;
143 while (rep1 < length && string[rep1] == string[i])
144 {
145 ++rep1;
146 ++reps;
147 }
148
149 if (reps > repeat_count_threshold)
150 {
151 if (in_quotes)
152 {
153 if (inspect_it)
154 fputs_filtered ("\\\", ", stream);
155 else
156 fputs_filtered ("\", ", stream);
157 in_quotes = 0;
158 }
159 c_printchar (string[i], stream);
160 fprintf_filtered (stream, " <repeats %u times>", reps);
161 i = rep1 - 1;
162 things_printed += repeat_count_threshold;
163 need_comma = 1;
164 }
165 else
166 {
167 if (!in_quotes)
168 {
169 if (inspect_it)
170 fputs_filtered ("\\\"", stream);
171 else
172 fputs_filtered ("\"", stream);
173 in_quotes = 1;
174 }
175 emit_char (string[i], stream, '"');
176 ++things_printed;
177 }
178 }
179
180 /* Terminate the quotes if necessary. */
181 if (in_quotes)
182 {
183 if (inspect_it)
184 fputs_filtered ("\\\"", stream);
185 else
186 fputs_filtered ("\"", stream);
187 }
188
189 if (force_ellipses || i < length)
190 fputs_filtered ("...", stream);
191}
192
193/* Create a fundamental C type using default reasonable for the current
194 target machine.
195
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
200 function.
201
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 */
215
5e548861 216struct type *
22e39759
FF
217c_create_fundamental_type (objfile, typeid)
218 struct objfile *objfile;
219 int typeid;
220{
221 register struct type *type = NULL;
22e39759
FF
222
223 switch (typeid)
224 {
225 default:
226 /* FIXME: For now, if we are asked to produce a type not in this
227 language, create the equivalent of a C integer type with the
228 name "<?type?>". When all the dust settles from the type
229 reconstruction work, this should probably become an error. */
230 type = init_type (TYPE_CODE_INT,
231 TARGET_INT_BIT / TARGET_CHAR_BIT,
232 0, "<?type?>", objfile);
233 warning ("internal error: no C/C++ fundamental type %d", typeid);
234 break;
235 case FT_VOID:
236 type = init_type (TYPE_CODE_VOID,
237 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
238 0, "void", objfile);
239 break;
240 case FT_CHAR:
241 type = init_type (TYPE_CODE_INT,
242 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
243 0, "char", objfile);
244 break;
245 case FT_SIGNED_CHAR:
246 type = init_type (TYPE_CODE_INT,
247 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
dda398c3 248 0, "signed char", objfile);
22e39759
FF
249 break;
250 case FT_UNSIGNED_CHAR:
251 type = init_type (TYPE_CODE_INT,
252 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
253 TYPE_FLAG_UNSIGNED, "unsigned char", objfile);
254 break;
255 case FT_SHORT:
256 type = init_type (TYPE_CODE_INT,
257 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
258 0, "short", objfile);
259 break;
260 case FT_SIGNED_SHORT:
261 type = init_type (TYPE_CODE_INT,
262 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
dda398c3 263 0, "short", objfile); /* FIXME-fnf */
22e39759
FF
264 break;
265 case FT_UNSIGNED_SHORT:
266 type = init_type (TYPE_CODE_INT,
267 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
268 TYPE_FLAG_UNSIGNED, "unsigned short", objfile);
269 break;
270 case FT_INTEGER:
271 type = init_type (TYPE_CODE_INT,
272 TARGET_INT_BIT / TARGET_CHAR_BIT,
273 0, "int", objfile);
274 break;
275 case FT_SIGNED_INTEGER:
276 type = init_type (TYPE_CODE_INT,
277 TARGET_INT_BIT / TARGET_CHAR_BIT,
dda398c3 278 0, "int", objfile); /* FIXME -fnf */
22e39759
FF
279 break;
280 case FT_UNSIGNED_INTEGER:
281 type = init_type (TYPE_CODE_INT,
282 TARGET_INT_BIT / TARGET_CHAR_BIT,
283 TYPE_FLAG_UNSIGNED, "unsigned int", objfile);
284 break;
285 case FT_LONG:
286 type = init_type (TYPE_CODE_INT,
287 TARGET_LONG_BIT / TARGET_CHAR_BIT,
288 0, "long", objfile);
289 break;
290 case FT_SIGNED_LONG:
291 type = init_type (TYPE_CODE_INT,
292 TARGET_LONG_BIT / TARGET_CHAR_BIT,
dda398c3 293 0, "long", objfile); /* FIXME -fnf */
22e39759
FF
294 break;
295 case FT_UNSIGNED_LONG:
296 type = init_type (TYPE_CODE_INT,
297 TARGET_LONG_BIT / TARGET_CHAR_BIT,
298 TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
299 break;
300 case FT_LONG_LONG:
301 type = init_type (TYPE_CODE_INT,
302 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
303 0, "long long", objfile);
304 break;
305 case FT_SIGNED_LONG_LONG:
306 type = init_type (TYPE_CODE_INT,
307 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
dda398c3 308 0, "signed long long", objfile);
22e39759
FF
309 break;
310 case FT_UNSIGNED_LONG_LONG:
311 type = init_type (TYPE_CODE_INT,
312 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
313 TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
314 break;
315 case FT_FLOAT:
316 type = init_type (TYPE_CODE_FLT,
317 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
318 0, "float", objfile);
319 break;
320 case FT_DBL_PREC_FLOAT:
321 type = init_type (TYPE_CODE_FLT,
322 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
323 0, "double", objfile);
324 break;
325 case FT_EXT_PREC_FLOAT:
326 type = init_type (TYPE_CODE_FLT,
327 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
328 0, "long double", objfile);
329 break;
330 }
331 return (type);
332}
333
334\f
335/* Table mapping opcodes into strings for printing operators
336 and precedences of the operators. */
337
5e548861 338const struct op_print c_op_print_tab[] =
22e39759
FF
339 {
340 {",", BINOP_COMMA, PREC_COMMA, 0},
341 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
342 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
343 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
344 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
345 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
346 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
347 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
348 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
349 {"<=", BINOP_LEQ, PREC_ORDER, 0},
350 {">=", BINOP_GEQ, PREC_ORDER, 0},
351 {">", BINOP_GTR, PREC_ORDER, 0},
352 {"<", BINOP_LESS, PREC_ORDER, 0},
353 {">>", BINOP_RSH, PREC_SHIFT, 0},
354 {"<<", BINOP_LSH, PREC_SHIFT, 0},
355 {"+", BINOP_ADD, PREC_ADD, 0},
356 {"-", BINOP_SUB, PREC_ADD, 0},
357 {"*", BINOP_MUL, PREC_MUL, 0},
358 {"/", BINOP_DIV, PREC_MUL, 0},
359 {"%", BINOP_REM, PREC_MUL, 0},
360 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
361 {"-", UNOP_NEG, PREC_PREFIX, 0},
362 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
363 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
364 {"*", UNOP_IND, PREC_PREFIX, 0},
365 {"&", UNOP_ADDR, PREC_PREFIX, 0},
366 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
367 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
368 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
369 /* C++ */
370 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
371 {NULL, 0, 0, 0}
372};
373\f
22e39759
FF
374struct type ** const (c_builtin_types[]) =
375{
376 &builtin_type_int,
377 &builtin_type_long,
378 &builtin_type_short,
379 &builtin_type_char,
380 &builtin_type_float,
381 &builtin_type_double,
382 &builtin_type_void,
383 &builtin_type_long_long,
384 &builtin_type_signed_char,
385 &builtin_type_unsigned_char,
386 &builtin_type_unsigned_short,
387 &builtin_type_unsigned_int,
388 &builtin_type_unsigned_long,
389 &builtin_type_unsigned_long_long,
390 &builtin_type_long_double,
391 &builtin_type_complex,
392 &builtin_type_double_complex,
393 0
394};
395
396const struct language_defn c_language_defn = {
397 "c", /* Language name */
398 language_c,
399 c_builtin_types,
400 range_check_off,
401 type_check_off,
402 c_parse,
403 c_error,
7398958c 404 evaluate_subexp_standard,
22e39759
FF
405 c_printchar, /* Print a character constant */
406 c_printstr, /* Function to print string constant */
407 c_create_fundamental_type, /* Create fundamental type in this language */
a8a69e63
FF
408 c_print_type, /* Print a type using appropriate syntax */
409 c_val_print, /* Print a value using appropriate syntax */
e10cfcaa 410 c_value_print, /* Print a top-level value */
22e39759 411 {"", "", "", ""}, /* Binary format info */
5573d7d4
JK
412 {"0%lo", "0", "o", ""}, /* Octal format info */
413 {"%ld", "", "d", ""}, /* Decimal format info */
414 {"0x%lx", "0x", "x", ""}, /* Hex format info */
22e39759 415 c_op_print_tab, /* expression operators for printing */
acc4efde 416 1, /* c-style arrays */
ead95f8a
PB
417 0, /* String lower bound */
418 &builtin_type_char, /* Type of string elements */
22e39759
FF
419 LANG_MAGIC
420};
421
422const struct language_defn cplus_language_defn = {
423 "c++", /* Language name */
424 language_cplus,
425 c_builtin_types,
426 range_check_off,
427 type_check_off,
428 c_parse,
429 c_error,
7398958c 430 evaluate_subexp_standard,
22e39759
FF
431 c_printchar, /* Print a character constant */
432 c_printstr, /* Function to print string constant */
433 c_create_fundamental_type, /* Create fundamental type in this language */
a8a69e63
FF
434 c_print_type, /* Print a type using appropriate syntax */
435 c_val_print, /* Print a value using appropriate syntax */
e10cfcaa 436 c_value_print, /* Print a top-level value */
22e39759 437 {"", "", "", ""}, /* Binary format info */
5573d7d4
JK
438 {"0%lo", "0", "o", ""}, /* Octal format info */
439 {"%ld", "", "d", ""}, /* Decimal format info */
440 {"0x%lx", "0x", "x", ""}, /* Hex format info */
22e39759 441 c_op_print_tab, /* expression operators for printing */
acc4efde 442 1, /* c-style arrays */
ead95f8a
PB
443 0, /* String lower bound */
444 &builtin_type_char, /* Type of string elements */
22e39759
FF
445 LANG_MAGIC
446};
447
2c068010
PS
448const struct language_defn asm_language_defn = {
449 "asm", /* Language name */
450 language_asm,
451 c_builtin_types,
452 range_check_off,
453 type_check_off,
454 c_parse,
455 c_error,
7398958c 456 evaluate_subexp_standard,
2c068010
PS
457 c_printchar, /* Print a character constant */
458 c_printstr, /* Function to print string constant */
459 c_create_fundamental_type, /* Create fundamental type in this language */
460 c_print_type, /* Print a type using appropriate syntax */
461 c_val_print, /* Print a value using appropriate syntax */
462 c_value_print, /* Print a top-level value */
2c068010
PS
463 {"", "", "", ""}, /* Binary format info */
464 {"0%lo", "0", "o", ""}, /* Octal format info */
465 {"%ld", "", "d", ""}, /* Decimal format info */
466 {"0x%lx", "0x", "x", ""}, /* Hex format info */
467 c_op_print_tab, /* expression operators for printing */
acc4efde 468 1, /* c-style arrays */
ead95f8a
PB
469 0, /* String lower bound */
470 &builtin_type_char, /* Type of string elements */
2c068010
PS
471 LANG_MAGIC
472};
473
22e39759 474void
d62e7a20 475_initialize_c_language ()
22e39759 476{
22e39759
FF
477 add_language (&c_language_defn);
478 add_language (&cplus_language_defn);
2c068010 479 add_language (&asm_language_defn);
22e39759 480}