Commit | Line | Data |
---|---|---|
22e39759 | 1 | /* C language support routines for GDB, the GNU debugger. |
cef0333e | 2 | Copyright 1992, 1993, 1994 Free Software Foundation, Inc. |
22e39759 FF |
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 | |
6c9638b4 | 18 | Foundation, 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 | ||
242c0d81 | 28 | static void c_emit_char PARAMS ((int c, GDB_FILE *stream, int quoter)); |
b607efe7 | 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 | ||
34 | static void | |
242c0d81 | 35 | c_emit_char (c, stream, quoter) |
22e39759 | 36 | register int c; |
199b2450 | 37 | GDB_FILE *stream; |
22e39759 FF |
38 | int quoter; |
39 | { | |
22e39759 FF |
40 | c &= 0xFF; /* Avoid sign bit follies */ |
41 | ||
42 | if (PRINT_LITERAL_FORM (c)) | |
43 | { | |
44 | if (c == '\\' || c == quoter) | |
45 | { | |
46 | fputs_filtered ("\\", stream); | |
47 | } | |
48 | fprintf_filtered (stream, "%c", c); | |
49 | } | |
50 | else | |
51 | { | |
52 | switch (c) | |
53 | { | |
54 | case '\n': | |
55 | fputs_filtered ("\\n", stream); | |
56 | break; | |
57 | case '\b': | |
58 | fputs_filtered ("\\b", stream); | |
59 | break; | |
60 | case '\t': | |
61 | fputs_filtered ("\\t", stream); | |
62 | break; | |
63 | case '\f': | |
64 | fputs_filtered ("\\f", stream); | |
65 | break; | |
66 | case '\r': | |
67 | fputs_filtered ("\\r", stream); | |
68 | break; | |
69 | case '\033': | |
70 | fputs_filtered ("\\e", stream); | |
71 | break; | |
72 | case '\007': | |
73 | fputs_filtered ("\\a", stream); | |
74 | break; | |
75 | default: | |
76 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); | |
77 | break; | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
5e548861 | 82 | void |
22e39759 FF |
83 | c_printchar (c, stream) |
84 | int c; | |
199b2450 | 85 | GDB_FILE *stream; |
22e39759 | 86 | { |
242c0d81 SG |
87 | fputc_filtered ('\'', stream); |
88 | LA_EMIT_CHAR (c, stream, '\''); | |
89 | fputc_filtered ('\'', stream); | |
22e39759 FF |
90 | } |
91 | ||
92 | /* Print the character string STRING, printing at most LENGTH characters. | |
242c0d81 SG |
93 | LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes |
94 | long. Printing stops early if the number hits print_max; repeat counts are | |
95 | printed as appropriate. Print ellipses at the end if we had to stop before | |
96 | printing LENGTH characters, or if FORCE_ELLIPSES. */ | |
22e39759 | 97 | |
5e548861 | 98 | void |
242c0d81 | 99 | c_printstr (stream, string, length, width, force_ellipses) |
199b2450 | 100 | GDB_FILE *stream; |
22e39759 FF |
101 | char *string; |
102 | unsigned int length; | |
242c0d81 | 103 | int width; |
22e39759 FF |
104 | int force_ellipses; |
105 | { | |
106 | register unsigned int i; | |
107 | unsigned int things_printed = 0; | |
108 | int in_quotes = 0; | |
109 | int need_comma = 0; | |
110 | extern int inspect_it; | |
111 | extern int repeat_count_threshold; | |
112 | extern int print_max; | |
113 | ||
c5c00171 JG |
114 | /* If the string was not truncated due to `set print elements', and |
115 | the last byte of it is a null, we don't print that, in traditional C | |
116 | style. */ | |
242c0d81 SG |
117 | if (!force_ellipses |
118 | && length > 0 | |
119 | && extract_unsigned_integer (string + (length - 1) * width, width) == '\0') | |
c5c00171 JG |
120 | length--; |
121 | ||
22e39759 FF |
122 | if (length == 0) |
123 | { | |
55768580 | 124 | fputs_filtered ("\"\"", stream); |
22e39759 FF |
125 | return; |
126 | } | |
127 | ||
128 | for (i = 0; i < length && things_printed < print_max; ++i) | |
129 | { | |
130 | /* Position of the character we are examining | |
131 | to see whether it is repeated. */ | |
132 | unsigned int rep1; | |
133 | /* Number of repetitions we have detected so far. */ | |
134 | unsigned int reps; | |
242c0d81 | 135 | unsigned long current_char; |
22e39759 FF |
136 | |
137 | QUIT; | |
138 | ||
139 | if (need_comma) | |
140 | { | |
141 | fputs_filtered (", ", stream); | |
142 | need_comma = 0; | |
143 | } | |
144 | ||
242c0d81 SG |
145 | current_char = extract_unsigned_integer (string + i * width, width); |
146 | ||
22e39759 FF |
147 | rep1 = i + 1; |
148 | reps = 1; | |
242c0d81 SG |
149 | while (rep1 < length |
150 | && extract_unsigned_integer (string + rep1 * width, width) | |
151 | == current_char) | |
22e39759 FF |
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 | } | |
242c0d81 | 167 | LA_PRINT_CHAR (current_char, stream); |
22e39759 FF |
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 | } | |
242c0d81 | 183 | LA_EMIT_CHAR (current_char, stream, '"'); |
22e39759 FF |
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 | ||
5e548861 | 224 | struct type * |
22e39759 FF |
225 | c_create_fundamental_type (objfile, typeid) |
226 | struct objfile *objfile; | |
227 | int typeid; | |
228 | { | |
229 | register struct type *type = NULL; | |
22e39759 FF |
230 | |
231 | switch (typeid) | |
232 | { | |
233 | default: | |
234 | /* FIXME: For now, if we are asked to produce a type not in this | |
235 | language, create the equivalent of a C integer type with the | |
236 | name "<?type?>". When all the dust settles from the type | |
237 | reconstruction work, this should probably become an error. */ | |
238 | type = init_type (TYPE_CODE_INT, | |
239 | TARGET_INT_BIT / TARGET_CHAR_BIT, | |
240 | 0, "<?type?>", objfile); | |
241 | warning ("internal error: no C/C++ fundamental type %d", typeid); | |
242 | break; | |
243 | case FT_VOID: | |
244 | type = init_type (TYPE_CODE_VOID, | |
245 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
246 | 0, "void", objfile); | |
247 | break; | |
248 | case FT_CHAR: | |
249 | type = init_type (TYPE_CODE_INT, | |
250 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
251 | 0, "char", objfile); | |
252 | break; | |
253 | case FT_SIGNED_CHAR: | |
254 | type = init_type (TYPE_CODE_INT, | |
255 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
dda398c3 | 256 | 0, "signed char", objfile); |
22e39759 FF |
257 | break; |
258 | case FT_UNSIGNED_CHAR: | |
259 | type = init_type (TYPE_CODE_INT, | |
260 | TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
261 | TYPE_FLAG_UNSIGNED, "unsigned char", objfile); | |
262 | break; | |
263 | case FT_SHORT: | |
264 | type = init_type (TYPE_CODE_INT, | |
265 | TARGET_SHORT_BIT / TARGET_CHAR_BIT, | |
266 | 0, "short", objfile); | |
267 | break; | |
268 | case FT_SIGNED_SHORT: | |
269 | type = init_type (TYPE_CODE_INT, | |
270 | TARGET_SHORT_BIT / TARGET_CHAR_BIT, | |
dda398c3 | 271 | 0, "short", objfile); /* FIXME-fnf */ |
22e39759 FF |
272 | break; |
273 | case FT_UNSIGNED_SHORT: | |
274 | type = init_type (TYPE_CODE_INT, | |
275 | TARGET_SHORT_BIT / TARGET_CHAR_BIT, | |
276 | TYPE_FLAG_UNSIGNED, "unsigned short", objfile); | |
277 | break; | |
278 | case FT_INTEGER: | |
279 | type = init_type (TYPE_CODE_INT, | |
280 | TARGET_INT_BIT / TARGET_CHAR_BIT, | |
281 | 0, "int", objfile); | |
282 | break; | |
283 | case FT_SIGNED_INTEGER: | |
284 | type = init_type (TYPE_CODE_INT, | |
285 | TARGET_INT_BIT / TARGET_CHAR_BIT, | |
dda398c3 | 286 | 0, "int", objfile); /* FIXME -fnf */ |
22e39759 FF |
287 | break; |
288 | case FT_UNSIGNED_INTEGER: | |
289 | type = init_type (TYPE_CODE_INT, | |
290 | TARGET_INT_BIT / TARGET_CHAR_BIT, | |
291 | TYPE_FLAG_UNSIGNED, "unsigned int", objfile); | |
292 | break; | |
293 | case FT_LONG: | |
294 | type = init_type (TYPE_CODE_INT, | |
295 | TARGET_LONG_BIT / TARGET_CHAR_BIT, | |
296 | 0, "long", objfile); | |
297 | break; | |
298 | case FT_SIGNED_LONG: | |
299 | type = init_type (TYPE_CODE_INT, | |
300 | TARGET_LONG_BIT / TARGET_CHAR_BIT, | |
dda398c3 | 301 | 0, "long", objfile); /* FIXME -fnf */ |
22e39759 FF |
302 | break; |
303 | case FT_UNSIGNED_LONG: | |
304 | type = init_type (TYPE_CODE_INT, | |
305 | TARGET_LONG_BIT / TARGET_CHAR_BIT, | |
306 | TYPE_FLAG_UNSIGNED, "unsigned long", objfile); | |
307 | break; | |
308 | case FT_LONG_LONG: | |
309 | type = init_type (TYPE_CODE_INT, | |
310 | TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, | |
311 | 0, "long long", objfile); | |
312 | break; | |
313 | case FT_SIGNED_LONG_LONG: | |
314 | type = init_type (TYPE_CODE_INT, | |
315 | TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, | |
dda398c3 | 316 | 0, "signed long long", objfile); |
22e39759 FF |
317 | break; |
318 | case FT_UNSIGNED_LONG_LONG: | |
319 | type = init_type (TYPE_CODE_INT, | |
320 | TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, | |
321 | TYPE_FLAG_UNSIGNED, "unsigned long long", objfile); | |
322 | break; | |
323 | case FT_FLOAT: | |
324 | type = init_type (TYPE_CODE_FLT, | |
325 | TARGET_FLOAT_BIT / TARGET_CHAR_BIT, | |
326 | 0, "float", objfile); | |
327 | break; | |
328 | case FT_DBL_PREC_FLOAT: | |
329 | type = init_type (TYPE_CODE_FLT, | |
330 | TARGET_DOUBLE_BIT / TARGET_CHAR_BIT, | |
331 | 0, "double", objfile); | |
332 | break; | |
333 | case FT_EXT_PREC_FLOAT: | |
334 | type = init_type (TYPE_CODE_FLT, | |
335 | TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT, | |
336 | 0, "long double", objfile); | |
337 | break; | |
338 | } | |
339 | return (type); | |
340 | } | |
341 | ||
342 | \f | |
343 | /* Table mapping opcodes into strings for printing operators | |
344 | and precedences of the operators. */ | |
345 | ||
5e548861 | 346 | const struct op_print c_op_print_tab[] = |
22e39759 FF |
347 | { |
348 | {",", BINOP_COMMA, PREC_COMMA, 0}, | |
349 | {"=", BINOP_ASSIGN, PREC_ASSIGN, 1}, | |
350 | {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0}, | |
351 | {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0}, | |
352 | {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0}, | |
353 | {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0}, | |
354 | {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0}, | |
355 | {"==", BINOP_EQUAL, PREC_EQUAL, 0}, | |
356 | {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0}, | |
357 | {"<=", BINOP_LEQ, PREC_ORDER, 0}, | |
358 | {">=", BINOP_GEQ, PREC_ORDER, 0}, | |
359 | {">", BINOP_GTR, PREC_ORDER, 0}, | |
360 | {"<", BINOP_LESS, PREC_ORDER, 0}, | |
361 | {">>", BINOP_RSH, PREC_SHIFT, 0}, | |
362 | {"<<", BINOP_LSH, PREC_SHIFT, 0}, | |
363 | {"+", BINOP_ADD, PREC_ADD, 0}, | |
364 | {"-", BINOP_SUB, PREC_ADD, 0}, | |
365 | {"*", BINOP_MUL, PREC_MUL, 0}, | |
366 | {"/", BINOP_DIV, PREC_MUL, 0}, | |
367 | {"%", BINOP_REM, PREC_MUL, 0}, | |
368 | {"@", BINOP_REPEAT, PREC_REPEAT, 0}, | |
369 | {"-", UNOP_NEG, PREC_PREFIX, 0}, | |
370 | {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0}, | |
371 | {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0}, | |
372 | {"*", UNOP_IND, PREC_PREFIX, 0}, | |
373 | {"&", UNOP_ADDR, PREC_PREFIX, 0}, | |
374 | {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0}, | |
375 | {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0}, | |
376 | {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0}, | |
377 | /* C++ */ | |
378 | {"::", BINOP_SCOPE, PREC_PREFIX, 0}, | |
379 | {NULL, 0, 0, 0} | |
380 | }; | |
381 | \f | |
8501c742 | 382 | struct type ** CONST_PTR (c_builtin_types[]) = |
22e39759 FF |
383 | { |
384 | &builtin_type_int, | |
385 | &builtin_type_long, | |
386 | &builtin_type_short, | |
387 | &builtin_type_char, | |
388 | &builtin_type_float, | |
389 | &builtin_type_double, | |
390 | &builtin_type_void, | |
391 | &builtin_type_long_long, | |
392 | &builtin_type_signed_char, | |
393 | &builtin_type_unsigned_char, | |
394 | &builtin_type_unsigned_short, | |
395 | &builtin_type_unsigned_int, | |
396 | &builtin_type_unsigned_long, | |
397 | &builtin_type_unsigned_long_long, | |
398 | &builtin_type_long_double, | |
399 | &builtin_type_complex, | |
400 | &builtin_type_double_complex, | |
401 | 0 | |
402 | }; | |
403 | ||
404 | const struct language_defn c_language_defn = { | |
405 | "c", /* Language name */ | |
406 | language_c, | |
407 | c_builtin_types, | |
408 | range_check_off, | |
409 | type_check_off, | |
410 | c_parse, | |
411 | c_error, | |
7398958c | 412 | evaluate_subexp_standard, |
22e39759 FF |
413 | c_printchar, /* Print a character constant */ |
414 | c_printstr, /* Function to print string constant */ | |
242c0d81 | 415 | c_emit_char, /* Print a single char */ |
22e39759 | 416 | c_create_fundamental_type, /* Create fundamental type in this language */ |
a8a69e63 FF |
417 | c_print_type, /* Print a type using appropriate syntax */ |
418 | c_val_print, /* Print a value using appropriate syntax */ | |
e10cfcaa | 419 | c_value_print, /* Print a top-level value */ |
22e39759 | 420 | {"", "", "", ""}, /* Binary format info */ |
5573d7d4 JK |
421 | {"0%lo", "0", "o", ""}, /* Octal format info */ |
422 | {"%ld", "", "d", ""}, /* Decimal format info */ | |
423 | {"0x%lx", "0x", "x", ""}, /* Hex format info */ | |
22e39759 | 424 | c_op_print_tab, /* expression operators for printing */ |
acc4efde | 425 | 1, /* c-style arrays */ |
ead95f8a PB |
426 | 0, /* String lower bound */ |
427 | &builtin_type_char, /* Type of string elements */ | |
22e39759 FF |
428 | LANG_MAGIC |
429 | }; | |
430 | ||
431 | const struct language_defn cplus_language_defn = { | |
432 | "c++", /* Language name */ | |
433 | language_cplus, | |
434 | c_builtin_types, | |
435 | range_check_off, | |
436 | type_check_off, | |
437 | c_parse, | |
438 | c_error, | |
7398958c | 439 | evaluate_subexp_standard, |
22e39759 FF |
440 | c_printchar, /* Print a character constant */ |
441 | c_printstr, /* Function to print string constant */ | |
242c0d81 | 442 | c_emit_char, /* Print a single char */ |
22e39759 | 443 | c_create_fundamental_type, /* Create fundamental type in this language */ |
a8a69e63 FF |
444 | c_print_type, /* Print a type using appropriate syntax */ |
445 | c_val_print, /* Print a value using appropriate syntax */ | |
e10cfcaa | 446 | c_value_print, /* Print a top-level value */ |
22e39759 | 447 | {"", "", "", ""}, /* Binary format info */ |
5573d7d4 JK |
448 | {"0%lo", "0", "o", ""}, /* Octal format info */ |
449 | {"%ld", "", "d", ""}, /* Decimal format info */ | |
450 | {"0x%lx", "0x", "x", ""}, /* Hex format info */ | |
22e39759 | 451 | c_op_print_tab, /* expression operators for printing */ |
acc4efde | 452 | 1, /* c-style arrays */ |
ead95f8a PB |
453 | 0, /* String lower bound */ |
454 | &builtin_type_char, /* Type of string elements */ | |
22e39759 FF |
455 | LANG_MAGIC |
456 | }; | |
457 | ||
2c068010 PS |
458 | const struct language_defn asm_language_defn = { |
459 | "asm", /* Language name */ | |
460 | language_asm, | |
461 | c_builtin_types, | |
462 | range_check_off, | |
463 | type_check_off, | |
464 | c_parse, | |
465 | c_error, | |
7398958c | 466 | evaluate_subexp_standard, |
2c068010 PS |
467 | c_printchar, /* Print a character constant */ |
468 | c_printstr, /* Function to print string constant */ | |
242c0d81 | 469 | c_emit_char, /* Print a single char */ |
2c068010 PS |
470 | c_create_fundamental_type, /* Create fundamental type in this language */ |
471 | c_print_type, /* Print a type using appropriate syntax */ | |
472 | c_val_print, /* Print a value using appropriate syntax */ | |
473 | c_value_print, /* Print a top-level value */ | |
2c068010 PS |
474 | {"", "", "", ""}, /* Binary format info */ |
475 | {"0%lo", "0", "o", ""}, /* Octal format info */ | |
476 | {"%ld", "", "d", ""}, /* Decimal format info */ | |
477 | {"0x%lx", "0x", "x", ""}, /* Hex format info */ | |
478 | c_op_print_tab, /* expression operators for printing */ | |
acc4efde | 479 | 1, /* c-style arrays */ |
ead95f8a PB |
480 | 0, /* String lower bound */ |
481 | &builtin_type_char, /* Type of string elements */ | |
2c068010 PS |
482 | LANG_MAGIC |
483 | }; | |
484 | ||
22e39759 | 485 | void |
d62e7a20 | 486 | _initialize_c_language () |
22e39759 | 487 | { |
22e39759 FF |
488 | add_language (&c_language_defn); |
489 | add_language (&cplus_language_defn); | |
2c068010 | 490 | add_language (&asm_language_defn); |
22e39759 | 491 | } |