]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* C language support routines for GDB, the GNU debugger. |
ce27fb25 | 2 | |
6aba47ca | 3 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003, |
7b6bb8da | 4 | 2004, 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 10 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 11 | (at your option) any later version. |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b | 18 | You should have received a copy of the GNU General Public License |
a9762ec7 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
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" | |
745b8ca0 | 28 | #include "valprint.h" |
84f0252a JB |
29 | #include "macroscope.h" |
30 | #include "gdb_assert.h" | |
234b45d4 | 31 | #include "charset.h" |
a15ef5f5 | 32 | #include "gdb_string.h" |
9a3d7dfd | 33 | #include "demangle.h" |
b18be20d | 34 | #include "cp-abi.h" |
1fcb5155 | 35 | #include "cp-support.h" |
6c7a06a3 TT |
36 | #include "gdb_obstack.h" |
37 | #include <ctype.h> | |
621c8364 | 38 | #include "exceptions.h" |
c906108c | 39 | |
a14ed312 | 40 | extern void _initialize_c_language (void); |
6c7a06a3 TT |
41 | |
42 | /* Given a C string type, STR_TYPE, return the corresponding target | |
43 | character set name. */ | |
44 | ||
45 | static const char * | |
e17a4113 | 46 | charset_for_string_type (enum c_string_type str_type, |
f870a310 | 47 | struct gdbarch *gdbarch) |
6c7a06a3 TT |
48 | { |
49 | switch (str_type & ~C_CHAR) | |
50 | { | |
51 | case C_STRING: | |
f870a310 | 52 | return target_charset (gdbarch); |
6c7a06a3 | 53 | case C_WIDE_STRING: |
f870a310 | 54 | return target_wide_charset (gdbarch); |
6c7a06a3 | 55 | case C_STRING_16: |
b8899f2b | 56 | /* FIXME: UTF-16 is not always correct. */ |
f870a310 | 57 | if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) |
b8899f2b | 58 | return "UTF-16BE"; |
6c7a06a3 | 59 | else |
b8899f2b | 60 | return "UTF-16LE"; |
6c7a06a3 | 61 | case C_STRING_32: |
b8899f2b | 62 | /* FIXME: UTF-32 is not always correct. */ |
f870a310 | 63 | if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) |
b8899f2b | 64 | return "UTF-32BE"; |
6c7a06a3 | 65 | else |
b8899f2b | 66 | return "UTF-32LE"; |
6c7a06a3 TT |
67 | } |
68 | internal_error (__FILE__, __LINE__, "unhandled c_string_type"); | |
69 | } | |
70 | ||
71 | /* Classify ELTTYPE according to what kind of character it is. Return | |
72 | the enum constant representing the character type. Also set | |
73 | *ENCODING to the name of the character set to use when converting | |
aff410f1 MS |
74 | characters of this type in target BYTE_ORDER to the host character |
75 | set. */ | |
6c7a06a3 TT |
76 | |
77 | static enum c_string_type | |
f870a310 | 78 | classify_type (struct type *elttype, struct gdbarch *gdbarch, |
e17a4113 | 79 | const char **encoding) |
6c7a06a3 | 80 | { |
6c7a06a3 TT |
81 | enum c_string_type result; |
82 | ||
85e306ed TT |
83 | /* We loop because ELTTYPE may be a typedef, and we want to |
84 | successively peel each typedef until we reach a type we | |
85 | understand. We don't use CHECK_TYPEDEF because that will strip | |
86 | all typedefs at once -- but in C, wchar_t is itself a typedef, so | |
87 | that would do the wrong thing. */ | |
88 | while (elttype) | |
6c7a06a3 TT |
89 | { |
90 | char *name = TYPE_NAME (elttype); | |
91 | ||
92 | if (TYPE_CODE (elttype) == TYPE_CODE_CHAR || !name) | |
93 | { | |
94 | result = C_CHAR; | |
95 | goto done; | |
96 | } | |
97 | ||
98 | if (!strcmp (name, "wchar_t")) | |
99 | { | |
100 | result = C_WIDE_CHAR; | |
101 | goto done; | |
102 | } | |
103 | ||
104 | if (!strcmp (name, "char16_t")) | |
105 | { | |
106 | result = C_CHAR_16; | |
107 | goto done; | |
108 | } | |
109 | ||
110 | if (!strcmp (name, "char32_t")) | |
111 | { | |
112 | result = C_CHAR_32; | |
113 | goto done; | |
114 | } | |
115 | ||
85e306ed TT |
116 | if (TYPE_CODE (elttype) != TYPE_CODE_TYPEDEF) |
117 | break; | |
118 | ||
119 | /* Call for side effects. */ | |
120 | check_typedef (elttype); | |
121 | ||
122 | if (TYPE_TARGET_TYPE (elttype)) | |
123 | elttype = TYPE_TARGET_TYPE (elttype); | |
124 | else | |
125 | { | |
126 | /* Perhaps check_typedef did not update the target type. In | |
127 | this case, force the lookup again and hope it works out. | |
128 | It never will for C, but it might for C++. */ | |
129 | CHECK_TYPEDEF (elttype); | |
130 | } | |
6c7a06a3 | 131 | } |
6c7a06a3 TT |
132 | |
133 | /* Punt. */ | |
134 | result = C_CHAR; | |
135 | ||
136 | done: | |
e17a4113 | 137 | if (encoding) |
f870a310 | 138 | *encoding = charset_for_string_type (result, gdbarch); |
e17a4113 | 139 | |
6c7a06a3 TT |
140 | return result; |
141 | } | |
142 | ||
143 | /* Return true if print_wchar can display W without resorting to a | |
144 | numeric escape, false otherwise. */ | |
145 | ||
146 | static int | |
147 | wchar_printable (gdb_wchar_t w) | |
148 | { | |
149 | return (gdb_iswprint (w) | |
150 | || w == LCST ('\a') || w == LCST ('\b') | |
151 | || w == LCST ('\f') || w == LCST ('\n') | |
152 | || w == LCST ('\r') || w == LCST ('\t') | |
153 | || w == LCST ('\v')); | |
154 | } | |
155 | ||
156 | /* A helper function that converts the contents of STRING to wide | |
157 | characters and then appends them to OUTPUT. */ | |
158 | ||
159 | static void | |
aff410f1 MS |
160 | append_string_as_wide (const char *string, |
161 | struct obstack *output) | |
6c7a06a3 TT |
162 | { |
163 | for (; *string; ++string) | |
164 | { | |
165 | gdb_wchar_t w = gdb_btowc (*string); | |
166 | obstack_grow (output, &w, sizeof (gdb_wchar_t)); | |
167 | } | |
168 | } | |
169 | ||
170 | /* Print a wide character W to OUTPUT. ORIG is a pointer to the | |
171 | original (target) bytes representing the character, ORIG_LEN is the | |
172 | number of valid bytes. WIDTH is the number of bytes in a base | |
173 | characters of the type. OUTPUT is an obstack to which wide | |
174 | characters are emitted. QUOTER is a (narrow) character indicating | |
175 | the style of quotes surrounding the character to be printed. | |
176 | NEED_ESCAPE is an in/out flag which is used to track numeric | |
177 | escapes across calls. */ | |
178 | ||
179 | static void | |
aff410f1 MS |
180 | print_wchar (gdb_wint_t w, const gdb_byte *orig, |
181 | int orig_len, int width, | |
182 | enum bfd_endian byte_order, | |
183 | struct obstack *output, | |
e17a4113 | 184 | int quoter, int *need_escapep) |
6c7a06a3 TT |
185 | { |
186 | int need_escape = *need_escapep; | |
c5504eaf | 187 | |
6c7a06a3 TT |
188 | *need_escapep = 0; |
189 | if (gdb_iswprint (w) && (!need_escape || (!gdb_iswdigit (w) | |
190 | && w != LCST ('8') | |
191 | && w != LCST ('9')))) | |
192 | { | |
3f7f5fe4 | 193 | gdb_wchar_t wchar = w; |
2d90c72a | 194 | |
6c7a06a3 TT |
195 | if (w == gdb_btowc (quoter) || w == LCST ('\\')) |
196 | obstack_grow_wstr (output, LCST ("\\")); | |
2d90c72a | 197 | obstack_grow (output, &wchar, sizeof (gdb_wchar_t)); |
6c7a06a3 TT |
198 | } |
199 | else | |
200 | { | |
201 | switch (w) | |
202 | { | |
203 | case LCST ('\a'): | |
204 | obstack_grow_wstr (output, LCST ("\\a")); | |
205 | break; | |
206 | case LCST ('\b'): | |
207 | obstack_grow_wstr (output, LCST ("\\b")); | |
208 | break; | |
209 | case LCST ('\f'): | |
210 | obstack_grow_wstr (output, LCST ("\\f")); | |
211 | break; | |
212 | case LCST ('\n'): | |
213 | obstack_grow_wstr (output, LCST ("\\n")); | |
214 | break; | |
215 | case LCST ('\r'): | |
216 | obstack_grow_wstr (output, LCST ("\\r")); | |
217 | break; | |
218 | case LCST ('\t'): | |
219 | obstack_grow_wstr (output, LCST ("\\t")); | |
220 | break; | |
221 | case LCST ('\v'): | |
222 | obstack_grow_wstr (output, LCST ("\\v")); | |
223 | break; | |
224 | default: | |
225 | { | |
226 | int i; | |
227 | ||
228 | for (i = 0; i + width <= orig_len; i += width) | |
229 | { | |
230 | char octal[30]; | |
e17a4113 | 231 | ULONGEST value; |
c5504eaf | 232 | |
aff410f1 MS |
233 | value = extract_unsigned_integer (&orig[i], width, |
234 | byte_order); | |
30b66ecc TT |
235 | /* If the value fits in 3 octal digits, print it that |
236 | way. Otherwise, print it as a hex escape. */ | |
237 | if (value <= 0777) | |
238 | sprintf (octal, "\\%.3o", (int) (value & 0777)); | |
239 | else | |
240 | sprintf (octal, "\\x%lx", (long) value); | |
6c7a06a3 TT |
241 | append_string_as_wide (octal, output); |
242 | } | |
243 | /* If we somehow have extra bytes, print them now. */ | |
244 | while (i < orig_len) | |
245 | { | |
246 | char octal[5]; | |
c5504eaf | 247 | |
6c7a06a3 TT |
248 | sprintf (octal, "\\%.3o", orig[i] & 0xff); |
249 | append_string_as_wide (octal, output); | |
250 | ++i; | |
251 | } | |
252 | ||
253 | *need_escapep = 1; | |
254 | } | |
255 | break; | |
256 | } | |
257 | } | |
258 | } | |
c906108c | 259 | |
aff410f1 MS |
260 | /* Print the character C on STREAM as part of the contents of a |
261 | literal string whose delimiter is QUOTER. Note that that format | |
262 | for printing characters and strings is language specific. */ | |
c906108c | 263 | |
6aecb9c2 JB |
264 | void |
265 | c_emit_char (int c, struct type *type, | |
266 | struct ui_file *stream, int quoter) | |
c906108c | 267 | { |
aff410f1 MS |
268 | enum bfd_endian byte_order |
269 | = gdbarch_byte_order (get_type_arch (type)); | |
6c7a06a3 TT |
270 | struct obstack wchar_buf, output; |
271 | struct cleanup *cleanups; | |
272 | const char *encoding; | |
273 | gdb_byte *buf; | |
274 | struct wchar_iterator *iter; | |
275 | int need_escape = 0; | |
234b45d4 | 276 | |
f870a310 | 277 | classify_type (type, get_type_arch (type), &encoding); |
c906108c | 278 | |
6c7a06a3 TT |
279 | buf = alloca (TYPE_LENGTH (type)); |
280 | pack_long (buf, type, c); | |
281 | ||
aff410f1 MS |
282 | iter = make_wchar_iterator (buf, TYPE_LENGTH (type), |
283 | encoding, TYPE_LENGTH (type)); | |
6c7a06a3 TT |
284 | cleanups = make_cleanup_wchar_iterator (iter); |
285 | ||
286 | /* This holds the printable form of the wchar_t data. */ | |
287 | obstack_init (&wchar_buf); | |
288 | make_cleanup_obstack_free (&wchar_buf); | |
289 | ||
290 | while (1) | |
c906108c | 291 | { |
6c7a06a3 TT |
292 | int num_chars; |
293 | gdb_wchar_t *chars; | |
294 | const gdb_byte *buf; | |
295 | size_t buflen; | |
296 | int print_escape = 1; | |
297 | enum wchar_iterate_result result; | |
298 | ||
299 | num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen); | |
300 | if (num_chars < 0) | |
301 | break; | |
302 | if (num_chars > 0) | |
303 | { | |
304 | /* If all characters are printable, print them. Otherwise, | |
305 | we're going to have to print an escape sequence. We | |
306 | check all characters because we want to print the target | |
307 | bytes in the escape sequence, and we don't know character | |
308 | boundaries there. */ | |
309 | int i; | |
310 | ||
311 | print_escape = 0; | |
312 | for (i = 0; i < num_chars; ++i) | |
313 | if (!wchar_printable (chars[i])) | |
314 | { | |
315 | print_escape = 1; | |
316 | break; | |
317 | } | |
318 | ||
319 | if (!print_escape) | |
320 | { | |
321 | for (i = 0; i < num_chars; ++i) | |
aff410f1 MS |
322 | print_wchar (chars[i], buf, buflen, |
323 | TYPE_LENGTH (type), byte_order, | |
324 | &wchar_buf, quoter, &need_escape); | |
6c7a06a3 TT |
325 | } |
326 | } | |
327 | ||
328 | /* This handles the NUM_CHARS == 0 case as well. */ | |
329 | if (print_escape) | |
aff410f1 MS |
330 | print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type), |
331 | byte_order, &wchar_buf, quoter, &need_escape); | |
c906108c | 332 | } |
6c7a06a3 TT |
333 | |
334 | /* The output in the host encoding. */ | |
335 | obstack_init (&output); | |
336 | make_cleanup_obstack_free (&output); | |
337 | ||
732f6a93 | 338 | convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), |
6c7a06a3 TT |
339 | obstack_base (&wchar_buf), |
340 | obstack_object_size (&wchar_buf), | |
341 | 1, &output, translit_char); | |
342 | obstack_1grow (&output, '\0'); | |
343 | ||
344 | fputs_filtered (obstack_base (&output), stream); | |
345 | ||
346 | do_cleanups (cleanups); | |
c906108c SS |
347 | } |
348 | ||
349 | void | |
6c7a06a3 | 350 | c_printchar (int c, struct type *type, struct ui_file *stream) |
c906108c | 351 | { |
6c7a06a3 | 352 | enum c_string_type str_type; |
6c7a06a3 | 353 | |
f870a310 | 354 | str_type = classify_type (type, get_type_arch (type), NULL); |
6c7a06a3 TT |
355 | switch (str_type) |
356 | { | |
357 | case C_CHAR: | |
358 | break; | |
359 | case C_WIDE_CHAR: | |
360 | fputc_filtered ('L', stream); | |
361 | break; | |
362 | case C_CHAR_16: | |
363 | fputc_filtered ('u', stream); | |
364 | break; | |
365 | case C_CHAR_32: | |
366 | fputc_filtered ('U', stream); | |
367 | break; | |
368 | } | |
369 | ||
c906108c | 370 | fputc_filtered ('\'', stream); |
6c7a06a3 | 371 | LA_EMIT_CHAR (c, type, stream, '\''); |
c906108c SS |
372 | fputc_filtered ('\'', stream); |
373 | } | |
374 | ||
aff410f1 MS |
375 | /* Print the character string STRING, printing at most LENGTH |
376 | characters. LENGTH is -1 if the string is nul terminated. Each | |
377 | character is WIDTH bytes long. Printing stops early if the number | |
378 | hits print_max; repeat counts are printed as appropriate. Print | |
379 | ellipses at the end if we had to stop before printing LENGTH | |
380 | characters, or if FORCE_ELLIPSES. */ | |
c906108c SS |
381 | |
382 | void | |
aff410f1 MS |
383 | c_printstr (struct ui_file *stream, struct type *type, |
384 | const gdb_byte *string, unsigned int length, | |
385 | const char *user_encoding, int force_ellipses, | |
79a45b7d | 386 | const struct value_print_options *options) |
c906108c | 387 | { |
e17a4113 | 388 | enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type)); |
f86f5ca3 | 389 | unsigned int i; |
c906108c SS |
390 | unsigned int things_printed = 0; |
391 | int in_quotes = 0; | |
392 | int need_comma = 0; | |
6c7a06a3 TT |
393 | int width = TYPE_LENGTH (type); |
394 | struct obstack wchar_buf, output; | |
395 | struct cleanup *cleanup; | |
396 | enum c_string_type str_type; | |
be759fcf | 397 | const char *type_encoding; |
6c7a06a3 TT |
398 | const char *encoding; |
399 | struct wchar_iterator *iter; | |
400 | int finished = 0; | |
401 | int need_escape = 0; | |
c906108c | 402 | |
3872d37d AS |
403 | if (length == -1) |
404 | { | |
405 | unsigned long current_char = 1; | |
406 | ||
407 | for (i = 0; current_char; ++i) | |
408 | { | |
409 | QUIT; | |
410 | current_char = extract_unsigned_integer (string + i * width, | |
411 | width, byte_order); | |
412 | } | |
413 | length = i; | |
414 | } | |
415 | ||
c906108c | 416 | /* If the string was not truncated due to `set print elements', and |
aff410f1 MS |
417 | the last byte of it is a null, we don't print that, in |
418 | traditional C style. */ | |
c906108c SS |
419 | if (!force_ellipses |
420 | && length > 0 | |
e17a4113 UW |
421 | && (extract_unsigned_integer (string + (length - 1) * width, |
422 | width, byte_order) == 0)) | |
c906108c SS |
423 | length--; |
424 | ||
f870a310 TT |
425 | str_type = (classify_type (type, get_type_arch (type), &type_encoding) |
426 | & ~C_CHAR); | |
6c7a06a3 TT |
427 | switch (str_type) |
428 | { | |
429 | case C_STRING: | |
430 | break; | |
431 | case C_WIDE_STRING: | |
432 | fputs_filtered ("L", stream); | |
433 | break; | |
434 | case C_STRING_16: | |
435 | fputs_filtered ("u", stream); | |
436 | break; | |
437 | case C_STRING_32: | |
438 | fputs_filtered ("U", stream); | |
439 | break; | |
440 | } | |
441 | ||
aff410f1 MS |
442 | encoding = (user_encoding && *user_encoding) |
443 | ? user_encoding : type_encoding; | |
be759fcf | 444 | |
c906108c SS |
445 | if (length == 0) |
446 | { | |
447 | fputs_filtered ("\"\"", stream); | |
448 | return; | |
449 | } | |
450 | ||
6c7a06a3 TT |
451 | /* Arrange to iterate over the characters, in wchar_t form. */ |
452 | iter = make_wchar_iterator (string, length * width, encoding, width); | |
453 | cleanup = make_cleanup_wchar_iterator (iter); | |
454 | ||
455 | /* WCHAR_BUF is the obstack we use to represent the string in | |
456 | wchar_t form. */ | |
457 | obstack_init (&wchar_buf); | |
458 | make_cleanup_obstack_free (&wchar_buf); | |
459 | ||
460 | while (!finished && things_printed < options->print_max) | |
c906108c | 461 | { |
6c7a06a3 TT |
462 | int num_chars; |
463 | enum wchar_iterate_result result; | |
464 | gdb_wchar_t *chars; | |
465 | const gdb_byte *buf; | |
466 | size_t buflen; | |
c906108c SS |
467 | |
468 | QUIT; | |
469 | ||
470 | if (need_comma) | |
471 | { | |
6c7a06a3 | 472 | obstack_grow_wstr (&wchar_buf, LCST (", ")); |
c906108c SS |
473 | need_comma = 0; |
474 | } | |
475 | ||
6c7a06a3 TT |
476 | num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen); |
477 | /* We only look at repetitions when we were able to convert a | |
478 | single character in isolation. This makes the code simpler | |
479 | and probably does the sensible thing in the majority of | |
480 | cases. */ | |
7a9fe101 | 481 | while (num_chars == 1 && things_printed < options->print_max) |
6c7a06a3 TT |
482 | { |
483 | /* Count the number of repetitions. */ | |
484 | unsigned int reps = 0; | |
485 | gdb_wchar_t current_char = chars[0]; | |
486 | const gdb_byte *orig_buf = buf; | |
487 | int orig_len = buflen; | |
c906108c | 488 | |
6c7a06a3 TT |
489 | if (need_comma) |
490 | { | |
491 | obstack_grow_wstr (&wchar_buf, LCST (", ")); | |
492 | need_comma = 0; | |
493 | } | |
494 | ||
495 | while (num_chars == 1 && current_char == chars[0]) | |
496 | { | |
aff410f1 MS |
497 | num_chars = wchar_iterate (iter, &result, &chars, |
498 | &buf, &buflen); | |
6c7a06a3 TT |
499 | ++reps; |
500 | } | |
501 | ||
502 | /* Emit CURRENT_CHAR according to the repetition count and | |
503 | options. */ | |
504 | if (reps > options->repeat_count_threshold) | |
505 | { | |
506 | if (in_quotes) | |
507 | { | |
508 | if (options->inspect_it) | |
509 | obstack_grow_wstr (&wchar_buf, LCST ("\\\", ")); | |
510 | else | |
511 | obstack_grow_wstr (&wchar_buf, LCST ("\", ")); | |
512 | in_quotes = 0; | |
513 | } | |
514 | obstack_grow_wstr (&wchar_buf, LCST ("'")); | |
515 | need_escape = 0; | |
516 | print_wchar (current_char, orig_buf, orig_len, width, | |
e17a4113 | 517 | byte_order, &wchar_buf, '\'', &need_escape); |
6c7a06a3 TT |
518 | obstack_grow_wstr (&wchar_buf, LCST ("'")); |
519 | { | |
520 | /* Painful gyrations. */ | |
521 | int j; | |
522 | char *s = xstrprintf (_(" <repeats %u times>"), reps); | |
c5504eaf | 523 | |
6c7a06a3 TT |
524 | for (j = 0; s[j]; ++j) |
525 | { | |
526 | gdb_wchar_t w = gdb_btowc (s[j]); | |
527 | obstack_grow (&wchar_buf, &w, sizeof (gdb_wchar_t)); | |
528 | } | |
529 | xfree (s); | |
530 | } | |
531 | things_printed += options->repeat_count_threshold; | |
532 | need_comma = 1; | |
533 | } | |
534 | else | |
535 | { | |
536 | /* Saw the character one or more times, but fewer than | |
537 | the repetition threshold. */ | |
538 | if (!in_quotes) | |
539 | { | |
540 | if (options->inspect_it) | |
541 | obstack_grow_wstr (&wchar_buf, LCST ("\\\"")); | |
542 | else | |
543 | obstack_grow_wstr (&wchar_buf, LCST ("\"")); | |
544 | in_quotes = 1; | |
545 | need_escape = 0; | |
546 | } | |
547 | ||
548 | while (reps-- > 0) | |
549 | { | |
aff410f1 MS |
550 | print_wchar (current_char, orig_buf, |
551 | orig_len, width, | |
552 | byte_order, &wchar_buf, | |
553 | '"', &need_escape); | |
6c7a06a3 TT |
554 | ++things_printed; |
555 | } | |
556 | } | |
557 | } | |
558 | ||
559 | /* NUM_CHARS and the other outputs from wchar_iterate are valid | |
560 | here regardless of which branch was taken above. */ | |
561 | if (num_chars < 0) | |
c906108c | 562 | { |
6c7a06a3 TT |
563 | /* Hit EOF. */ |
564 | finished = 1; | |
565 | break; | |
c906108c SS |
566 | } |
567 | ||
6c7a06a3 | 568 | switch (result) |
c906108c | 569 | { |
6c7a06a3 TT |
570 | case wchar_iterate_invalid: |
571 | if (!in_quotes) | |
c906108c | 572 | { |
79a45b7d | 573 | if (options->inspect_it) |
6c7a06a3 | 574 | obstack_grow_wstr (&wchar_buf, LCST ("\\\"")); |
c906108c | 575 | else |
6c7a06a3 TT |
576 | obstack_grow_wstr (&wchar_buf, LCST ("\"")); |
577 | in_quotes = 1; | |
c906108c | 578 | } |
6c7a06a3 | 579 | need_escape = 0; |
aff410f1 MS |
580 | print_wchar (gdb_WEOF, buf, buflen, width, byte_order, |
581 | &wchar_buf, '"', &need_escape); | |
6c7a06a3 TT |
582 | break; |
583 | ||
584 | case wchar_iterate_incomplete: | |
585 | if (in_quotes) | |
c906108c | 586 | { |
79a45b7d | 587 | if (options->inspect_it) |
6c7a06a3 | 588 | obstack_grow_wstr (&wchar_buf, LCST ("\\\",")); |
c906108c | 589 | else |
6c7a06a3 TT |
590 | obstack_grow_wstr (&wchar_buf, LCST ("\",")); |
591 | in_quotes = 0; | |
c906108c | 592 | } |
aff410f1 MS |
593 | obstack_grow_wstr (&wchar_buf, |
594 | LCST (" <incomplete sequence ")); | |
595 | print_wchar (gdb_WEOF, buf, buflen, width, | |
596 | byte_order, &wchar_buf, | |
6c7a06a3 TT |
597 | 0, &need_escape); |
598 | obstack_grow_wstr (&wchar_buf, LCST (">")); | |
599 | finished = 1; | |
600 | break; | |
c906108c SS |
601 | } |
602 | } | |
603 | ||
604 | /* Terminate the quotes if necessary. */ | |
605 | if (in_quotes) | |
606 | { | |
79a45b7d | 607 | if (options->inspect_it) |
6c7a06a3 | 608 | obstack_grow_wstr (&wchar_buf, LCST ("\\\"")); |
c906108c | 609 | else |
6c7a06a3 | 610 | obstack_grow_wstr (&wchar_buf, LCST ("\"")); |
c906108c SS |
611 | } |
612 | ||
6c7a06a3 TT |
613 | if (force_ellipses || !finished) |
614 | obstack_grow_wstr (&wchar_buf, LCST ("...")); | |
615 | ||
616 | /* OUTPUT is where we collect `char's for printing. */ | |
617 | obstack_init (&output); | |
618 | make_cleanup_obstack_free (&output); | |
619 | ||
732f6a93 | 620 | convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (), |
6c7a06a3 TT |
621 | obstack_base (&wchar_buf), |
622 | obstack_object_size (&wchar_buf), | |
623 | 1, &output, translit_char); | |
624 | obstack_1grow (&output, '\0'); | |
625 | ||
626 | fputs_filtered (obstack_base (&output), stream); | |
627 | ||
628 | do_cleanups (cleanup); | |
c906108c | 629 | } |
ae6a3a4c TJB |
630 | |
631 | /* Obtain a C string from the inferior storing it in a newly allocated | |
aff410f1 MS |
632 | buffer in BUFFER, which should be freed by the caller. If the in- |
633 | and out-parameter *LENGTH is specified at -1, the string is read | |
fbb8f299 | 634 | until a null character of the appropriate width is found, otherwise |
aff410f1 MS |
635 | the string is read to the length of characters specified. The size |
636 | of a character is determined by the length of the target type of | |
637 | the pointer or array. If VALUE is an array with a known length, | |
638 | the function will not read past the end of the array. On | |
639 | completion, *LENGTH will be set to the size of the string read in | |
fbb8f299 PM |
640 | characters. (If a length of -1 is specified, the length returned |
641 | will not include the null character). CHARSET is always set to the | |
642 | target charset. */ | |
ae6a3a4c TJB |
643 | |
644 | void | |
aff410f1 MS |
645 | c_get_string (struct value *value, gdb_byte **buffer, |
646 | int *length, struct type **char_type, | |
647 | const char **charset) | |
ae6a3a4c TJB |
648 | { |
649 | int err, width; | |
650 | unsigned int fetchlimit; | |
651 | struct type *type = check_typedef (value_type (value)); | |
652 | struct type *element_type = TYPE_TARGET_TYPE (type); | |
fbb8f299 | 653 | int req_length = *length; |
aff410f1 MS |
654 | enum bfd_endian byte_order |
655 | = gdbarch_byte_order (get_type_arch (type)); | |
96c07c5b | 656 | enum c_string_type kind; |
ae6a3a4c TJB |
657 | |
658 | if (element_type == NULL) | |
659 | goto error; | |
660 | ||
661 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
662 | { | |
aff410f1 MS |
663 | /* If we know the size of the array, we can use it as a limit on |
664 | the number of characters to be fetched. */ | |
ae6a3a4c TJB |
665 | if (TYPE_NFIELDS (type) == 1 |
666 | && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_RANGE) | |
667 | { | |
668 | LONGEST low_bound, high_bound; | |
669 | ||
670 | get_discrete_bounds (TYPE_FIELD_TYPE (type, 0), | |
671 | &low_bound, &high_bound); | |
672 | fetchlimit = high_bound - low_bound + 1; | |
673 | } | |
674 | else | |
675 | fetchlimit = UINT_MAX; | |
676 | } | |
677 | else if (TYPE_CODE (type) == TYPE_CODE_PTR) | |
678 | fetchlimit = UINT_MAX; | |
679 | else | |
680 | /* We work only with arrays and pointers. */ | |
681 | goto error; | |
682 | ||
96c07c5b | 683 | if (! c_textual_element_type (element_type, 0)) |
ae6a3a4c | 684 | goto error; |
96c07c5b | 685 | kind = classify_type (element_type, |
f870a310 | 686 | get_type_arch (element_type), |
96c07c5b | 687 | charset); |
ae6a3a4c TJB |
688 | width = TYPE_LENGTH (element_type); |
689 | ||
aff410f1 MS |
690 | /* If the string lives in GDB's memory instead of the inferior's, |
691 | then we just need to copy it to BUFFER. Also, since such strings | |
692 | are arrays with known size, FETCHLIMIT will hold the size of the | |
693 | array. */ | |
ae6a3a4c TJB |
694 | if ((VALUE_LVAL (value) == not_lval |
695 | || VALUE_LVAL (value) == lval_internalvar) | |
696 | && fetchlimit != UINT_MAX) | |
697 | { | |
698 | int i; | |
699 | const gdb_byte *contents = value_contents (value); | |
700 | ||
fbb8f299 PM |
701 | /* If a length is specified, use that. */ |
702 | if (*length >= 0) | |
703 | i = *length; | |
704 | else | |
705 | /* Otherwise, look for a null character. */ | |
706 | for (i = 0; i < fetchlimit; i++) | |
aff410f1 MS |
707 | if (extract_unsigned_integer (contents + i * width, |
708 | width, byte_order) == 0) | |
fbb8f299 PM |
709 | break; |
710 | ||
711 | /* I is now either a user-defined length, the number of non-null | |
712 | characters, or FETCHLIMIT. */ | |
ae6a3a4c TJB |
713 | *length = i * width; |
714 | *buffer = xmalloc (*length); | |
715 | memcpy (*buffer, contents, *length); | |
716 | err = 0; | |
717 | } | |
718 | else | |
719 | { | |
621c8364 TT |
720 | CORE_ADDR addr = value_as_address (value); |
721 | ||
722 | err = read_string (addr, *length, width, fetchlimit, | |
723 | byte_order, buffer, length); | |
ae6a3a4c TJB |
724 | if (err) |
725 | { | |
8ea5dfdf | 726 | xfree (*buffer); |
621c8364 TT |
727 | if (err == EIO) |
728 | throw_error (MEMORY_ERROR, "Address %s out of bounds", | |
729 | paddress (get_type_arch (type), addr)); | |
730 | else | |
731 | error (_("Error reading string from inferior: %s"), | |
732 | safe_strerror (err)); | |
ae6a3a4c TJB |
733 | } |
734 | } | |
735 | ||
fbb8f299 PM |
736 | /* If the LENGTH is specified at -1, we want to return the string |
737 | length up to the terminating null character. If an actual length | |
738 | was specified, we want to return the length of exactly what was | |
739 | read. */ | |
740 | if (req_length == -1) | |
741 | /* If the last character is null, subtract it from LENGTH. */ | |
742 | if (*length > 0 | |
aff410f1 MS |
743 | && extract_unsigned_integer (*buffer + *length - width, |
744 | width, byte_order) == 0) | |
fbb8f299 PM |
745 | *length -= width; |
746 | ||
747 | /* The read_string function will return the number of bytes read. | |
748 | If length returned from read_string was > 0, return the number of | |
749 | characters read by dividing the number of bytes by width. */ | |
750 | if (*length != 0) | |
751 | *length = *length / width; | |
ae6a3a4c | 752 | |
96c07c5b | 753 | *char_type = element_type; |
ae6a3a4c TJB |
754 | |
755 | return; | |
756 | ||
757 | error: | |
758 | { | |
759 | char *type_str; | |
760 | ||
761 | type_str = type_to_string (type); | |
762 | if (type_str) | |
763 | { | |
764 | make_cleanup (xfree, type_str); | |
765 | error (_("Trying to read string with inappropriate type `%s'."), | |
766 | type_str); | |
767 | } | |
768 | else | |
769 | error (_("Trying to read string with inappropriate type.")); | |
770 | } | |
771 | } | |
772 | ||
c906108c | 773 | \f |
6c7a06a3 TT |
774 | /* Evaluating C and C++ expressions. */ |
775 | ||
776 | /* Convert a UCN. The digits of the UCN start at P and extend no | |
777 | farther than LIMIT. DEST_CHARSET is the name of the character set | |
778 | into which the UCN should be converted. The results are written to | |
779 | OUTPUT. LENGTH is the maximum length of the UCN, either 4 or 8. | |
780 | Returns a pointer to just after the final digit of the UCN. */ | |
781 | ||
782 | static char * | |
783 | convert_ucn (char *p, char *limit, const char *dest_charset, | |
784 | struct obstack *output, int length) | |
785 | { | |
786 | unsigned long result = 0; | |
787 | gdb_byte data[4]; | |
788 | int i; | |
789 | ||
790 | for (i = 0; i < length && p < limit && isxdigit (*p); ++i, ++p) | |
791 | result = (result << 4) + host_hex_value (*p); | |
792 | ||
793 | for (i = 3; i >= 0; --i) | |
794 | { | |
795 | data[i] = result & 0xff; | |
796 | result >>= 8; | |
797 | } | |
798 | ||
aff410f1 MS |
799 | convert_between_encodings ("UTF-32BE", dest_charset, data, |
800 | 4, 4, output, translit_none); | |
6c7a06a3 TT |
801 | |
802 | return p; | |
803 | } | |
804 | ||
805 | /* Emit a character, VALUE, which was specified numerically, to | |
806 | OUTPUT. TYPE is the target character type. */ | |
807 | ||
808 | static void | |
809 | emit_numeric_character (struct type *type, unsigned long value, | |
810 | struct obstack *output) | |
811 | { | |
812 | gdb_byte *buffer; | |
813 | ||
814 | buffer = alloca (TYPE_LENGTH (type)); | |
815 | pack_long (buffer, type, value); | |
816 | obstack_grow (output, buffer, TYPE_LENGTH (type)); | |
817 | } | |
818 | ||
819 | /* Convert an octal escape sequence. TYPE is the target character | |
820 | type. The digits of the escape sequence begin at P and extend no | |
821 | farther than LIMIT. The result is written to OUTPUT. Returns a | |
822 | pointer to just after the final digit of the escape sequence. */ | |
823 | ||
824 | static char * | |
aff410f1 MS |
825 | convert_octal (struct type *type, char *p, |
826 | char *limit, struct obstack *output) | |
6c7a06a3 | 827 | { |
30b66ecc | 828 | int i; |
6c7a06a3 TT |
829 | unsigned long value = 0; |
830 | ||
30b66ecc TT |
831 | for (i = 0; |
832 | i < 3 && p < limit && isdigit (*p) && *p != '8' && *p != '9'; | |
833 | ++i) | |
6c7a06a3 TT |
834 | { |
835 | value = 8 * value + host_hex_value (*p); | |
836 | ++p; | |
837 | } | |
838 | ||
839 | emit_numeric_character (type, value, output); | |
840 | ||
841 | return p; | |
842 | } | |
843 | ||
844 | /* Convert a hex escape sequence. TYPE is the target character type. | |
845 | The digits of the escape sequence begin at P and extend no farther | |
846 | than LIMIT. The result is written to OUTPUT. Returns a pointer to | |
847 | just after the final digit of the escape sequence. */ | |
848 | ||
849 | static char * | |
aff410f1 MS |
850 | convert_hex (struct type *type, char *p, |
851 | char *limit, struct obstack *output) | |
6c7a06a3 TT |
852 | { |
853 | unsigned long value = 0; | |
854 | ||
855 | while (p < limit && isxdigit (*p)) | |
856 | { | |
857 | value = 16 * value + host_hex_value (*p); | |
858 | ++p; | |
859 | } | |
860 | ||
861 | emit_numeric_character (type, value, output); | |
862 | ||
863 | return p; | |
864 | } | |
865 | ||
866 | #define ADVANCE \ | |
867 | do { \ | |
868 | ++p; \ | |
869 | if (p == limit) \ | |
870 | error (_("Malformed escape sequence")); \ | |
871 | } while (0) | |
872 | ||
873 | /* Convert an escape sequence to a target format. TYPE is the target | |
874 | character type to use, and DEST_CHARSET is the name of the target | |
875 | character set. The backslash of the escape sequence is at *P, and | |
876 | the escape sequence will not extend past LIMIT. The results are | |
877 | written to OUTPUT. Returns a pointer to just past the final | |
878 | character of the escape sequence. */ | |
879 | ||
880 | static char * | |
881 | convert_escape (struct type *type, const char *dest_charset, | |
882 | char *p, char *limit, struct obstack *output) | |
883 | { | |
884 | /* Skip the backslash. */ | |
885 | ADVANCE; | |
886 | ||
887 | switch (*p) | |
888 | { | |
889 | case '\\': | |
890 | obstack_1grow (output, '\\'); | |
891 | ++p; | |
892 | break; | |
893 | ||
894 | case 'x': | |
895 | ADVANCE; | |
896 | if (!isxdigit (*p)) | |
897 | error (_("\\x used with no following hex digits.")); | |
898 | p = convert_hex (type, p, limit, output); | |
899 | break; | |
900 | ||
901 | case '0': | |
902 | case '1': | |
903 | case '2': | |
904 | case '3': | |
905 | case '4': | |
906 | case '5': | |
907 | case '6': | |
908 | case '7': | |
909 | p = convert_octal (type, p, limit, output); | |
910 | break; | |
911 | ||
912 | case 'u': | |
913 | case 'U': | |
914 | { | |
915 | int length = *p == 'u' ? 4 : 8; | |
c5504eaf | 916 | |
6c7a06a3 TT |
917 | ADVANCE; |
918 | if (!isxdigit (*p)) | |
919 | error (_("\\u used with no following hex digits")); | |
920 | p = convert_ucn (p, limit, dest_charset, output, length); | |
921 | } | |
922 | } | |
923 | ||
924 | return p; | |
925 | } | |
926 | ||
927 | /* Given a single string from a (C-specific) OP_STRING list, convert | |
928 | it to a target string, handling escape sequences specially. The | |
929 | output is written to OUTPUT. DATA is the input string, which has | |
930 | length LEN. DEST_CHARSET is the name of the target character set, | |
931 | and TYPE is the type of target character to use. */ | |
932 | ||
933 | static void | |
934 | parse_one_string (struct obstack *output, char *data, int len, | |
935 | const char *dest_charset, struct type *type) | |
936 | { | |
937 | char *limit; | |
938 | ||
939 | limit = data + len; | |
940 | ||
941 | while (data < limit) | |
942 | { | |
943 | char *p = data; | |
c5504eaf | 944 | |
6c7a06a3 TT |
945 | /* Look for next escape, or the end of the input. */ |
946 | while (p < limit && *p != '\\') | |
947 | ++p; | |
948 | /* If we saw a run of characters, convert them all. */ | |
949 | if (p > data) | |
950 | convert_between_encodings (host_charset (), dest_charset, | |
aff410f1 MS |
951 | data, p - data, 1, |
952 | output, translit_none); | |
6c7a06a3 TT |
953 | /* If we saw an escape, convert it. */ |
954 | if (p < limit) | |
955 | p = convert_escape (type, dest_charset, p, limit, output); | |
956 | data = p; | |
957 | } | |
958 | } | |
959 | ||
960 | /* Expression evaluator for the C language family. Most operations | |
961 | are delegated to evaluate_subexp_standard; see that function for a | |
962 | description of the arguments. */ | |
963 | ||
f4b8a18d | 964 | struct value * |
6c7a06a3 TT |
965 | evaluate_subexp_c (struct type *expect_type, struct expression *exp, |
966 | int *pos, enum noside noside) | |
967 | { | |
968 | enum exp_opcode op = exp->elts[*pos].opcode; | |
969 | ||
970 | switch (op) | |
971 | { | |
972 | case OP_STRING: | |
973 | { | |
974 | int oplen, limit; | |
975 | struct type *type; | |
976 | struct obstack output; | |
977 | struct cleanup *cleanup; | |
978 | struct value *result; | |
979 | enum c_string_type dest_type; | |
980 | const char *dest_charset; | |
981 | ||
982 | obstack_init (&output); | |
983 | cleanup = make_cleanup_obstack_free (&output); | |
984 | ||
985 | ++*pos; | |
986 | oplen = longest_to_int (exp->elts[*pos].longconst); | |
987 | ||
988 | ++*pos; | |
989 | limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1); | |
990 | dest_type | |
991 | = (enum c_string_type) longest_to_int (exp->elts[*pos].longconst); | |
992 | switch (dest_type & ~C_CHAR) | |
993 | { | |
994 | case C_STRING: | |
d80b854b UW |
995 | type = language_string_char_type (exp->language_defn, |
996 | exp->gdbarch); | |
6c7a06a3 TT |
997 | break; |
998 | case C_WIDE_STRING: | |
e6c014f2 UW |
999 | type = lookup_typename (exp->language_defn, exp->gdbarch, |
1000 | "wchar_t", NULL, 0); | |
6c7a06a3 TT |
1001 | break; |
1002 | case C_STRING_16: | |
e6c014f2 UW |
1003 | type = lookup_typename (exp->language_defn, exp->gdbarch, |
1004 | "char16_t", NULL, 0); | |
6c7a06a3 TT |
1005 | break; |
1006 | case C_STRING_32: | |
e6c014f2 UW |
1007 | type = lookup_typename (exp->language_defn, exp->gdbarch, |
1008 | "char32_t", NULL, 0); | |
6c7a06a3 TT |
1009 | break; |
1010 | default: | |
1011 | internal_error (__FILE__, __LINE__, "unhandled c_string_type"); | |
1012 | } | |
546e879e TT |
1013 | |
1014 | /* Ensure TYPE_LENGTH is valid for TYPE. */ | |
1015 | check_typedef (type); | |
1016 | ||
f870a310 | 1017 | dest_charset = charset_for_string_type (dest_type, exp->gdbarch); |
6c7a06a3 TT |
1018 | |
1019 | ++*pos; | |
1020 | while (*pos < limit) | |
1021 | { | |
1022 | int len; | |
1023 | ||
1024 | len = longest_to_int (exp->elts[*pos].longconst); | |
1025 | ||
1026 | ++*pos; | |
1027 | if (noside != EVAL_SKIP) | |
1028 | parse_one_string (&output, &exp->elts[*pos].string, len, | |
1029 | dest_charset, type); | |
1030 | *pos += BYTES_TO_EXP_ELEM (len); | |
1031 | } | |
1032 | ||
1033 | /* Skip the trailing length and opcode. */ | |
1034 | *pos += 2; | |
1035 | ||
1036 | if (noside == EVAL_SKIP) | |
334cc82d TT |
1037 | { |
1038 | /* Return a dummy value of the appropriate type. */ | |
1039 | if ((dest_type & C_CHAR) != 0) | |
1040 | result = allocate_value (type); | |
1041 | else | |
3b7538c0 | 1042 | result = value_cstring ("", 0, type); |
334cc82d TT |
1043 | do_cleanups (cleanup); |
1044 | return result; | |
1045 | } | |
6c7a06a3 TT |
1046 | |
1047 | if ((dest_type & C_CHAR) != 0) | |
1048 | { | |
1049 | LONGEST value; | |
1050 | ||
1051 | if (obstack_object_size (&output) != TYPE_LENGTH (type)) | |
3e43a32a MS |
1052 | error (_("Could not convert character " |
1053 | "constant to target character set")); | |
6c7a06a3 TT |
1054 | value = unpack_long (type, obstack_base (&output)); |
1055 | result = value_from_longest (type, value); | |
1056 | } | |
1057 | else | |
1058 | { | |
1059 | int i; | |
c5504eaf | 1060 | |
6c7a06a3 TT |
1061 | /* Write the terminating character. */ |
1062 | for (i = 0; i < TYPE_LENGTH (type); ++i) | |
1063 | obstack_1grow (&output, 0); | |
3b7538c0 UW |
1064 | result = value_cstring (obstack_base (&output), |
1065 | obstack_object_size (&output), | |
1066 | type); | |
6c7a06a3 TT |
1067 | } |
1068 | do_cleanups (cleanup); | |
1069 | return result; | |
1070 | } | |
1071 | break; | |
1072 | ||
1073 | default: | |
1074 | break; | |
1075 | } | |
1076 | return evaluate_subexp_standard (expect_type, exp, pos, noside); | |
1077 | } | |
c5aa993b | 1078 | |
84f0252a | 1079 | |
84f0252a | 1080 | \f |
c906108c SS |
1081 | /* Table mapping opcodes into strings for printing operators |
1082 | and precedences of the operators. */ | |
1083 | ||
1084 | const struct op_print c_op_print_tab[] = | |
c5aa993b JM |
1085 | { |
1086 | {",", BINOP_COMMA, PREC_COMMA, 0}, | |
1087 | {"=", BINOP_ASSIGN, PREC_ASSIGN, 1}, | |
1088 | {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0}, | |
1089 | {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0}, | |
1090 | {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0}, | |
1091 | {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0}, | |
1092 | {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0}, | |
1093 | {"==", BINOP_EQUAL, PREC_EQUAL, 0}, | |
1094 | {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0}, | |
1095 | {"<=", BINOP_LEQ, PREC_ORDER, 0}, | |
1096 | {">=", BINOP_GEQ, PREC_ORDER, 0}, | |
1097 | {">", BINOP_GTR, PREC_ORDER, 0}, | |
1098 | {"<", BINOP_LESS, PREC_ORDER, 0}, | |
1099 | {">>", BINOP_RSH, PREC_SHIFT, 0}, | |
1100 | {"<<", BINOP_LSH, PREC_SHIFT, 0}, | |
1101 | {"+", BINOP_ADD, PREC_ADD, 0}, | |
1102 | {"-", BINOP_SUB, PREC_ADD, 0}, | |
1103 | {"*", BINOP_MUL, PREC_MUL, 0}, | |
1104 | {"/", BINOP_DIV, PREC_MUL, 0}, | |
1105 | {"%", BINOP_REM, PREC_MUL, 0}, | |
1106 | {"@", BINOP_REPEAT, PREC_REPEAT, 0}, | |
1107 | {"-", UNOP_NEG, PREC_PREFIX, 0}, | |
1108 | {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0}, | |
1109 | {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0}, | |
1110 | {"*", UNOP_IND, PREC_PREFIX, 0}, | |
1111 | {"&", UNOP_ADDR, PREC_PREFIX, 0}, | |
1112 | {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0}, | |
1113 | {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0}, | |
1114 | {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0}, | |
c5aa993b | 1115 | {NULL, 0, 0, 0} |
c906108c SS |
1116 | }; |
1117 | \f | |
685419e2 AC |
1118 | enum c_primitive_types { |
1119 | c_primitive_type_int, | |
1120 | c_primitive_type_long, | |
1121 | c_primitive_type_short, | |
1122 | c_primitive_type_char, | |
1123 | c_primitive_type_float, | |
1124 | c_primitive_type_double, | |
1125 | c_primitive_type_void, | |
1126 | c_primitive_type_long_long, | |
1127 | c_primitive_type_signed_char, | |
1128 | c_primitive_type_unsigned_char, | |
1129 | c_primitive_type_unsigned_short, | |
1130 | c_primitive_type_unsigned_int, | |
1131 | c_primitive_type_unsigned_long, | |
1132 | c_primitive_type_unsigned_long_long, | |
1133 | c_primitive_type_long_double, | |
1134 | c_primitive_type_complex, | |
1135 | c_primitive_type_double_complex, | |
213e4dc2 TJB |
1136 | c_primitive_type_decfloat, |
1137 | c_primitive_type_decdouble, | |
1138 | c_primitive_type_declong, | |
685419e2 AC |
1139 | nr_c_primitive_types |
1140 | }; | |
1141 | ||
e9667a65 | 1142 | void |
685419e2 AC |
1143 | c_language_arch_info (struct gdbarch *gdbarch, |
1144 | struct language_arch_info *lai) | |
1145 | { | |
1146 | const struct builtin_type *builtin = builtin_type (gdbarch); | |
c5504eaf | 1147 | |
e9667a65 | 1148 | lai->string_char_type = builtin->builtin_char; |
685419e2 AC |
1149 | lai->primitive_type_vector |
1150 | = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1, | |
1151 | struct type *); | |
1152 | lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int; | |
1153 | lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long; | |
1154 | lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short; | |
1155 | lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char; | |
1156 | lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float; | |
1157 | lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double; | |
1158 | lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void; | |
1159 | lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long; | |
1160 | lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char; | |
1161 | lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char; | |
1162 | lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short; | |
1163 | lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int; | |
1164 | lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long; | |
1165 | lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long; | |
1166 | lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double; | |
1167 | lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex; | |
1168 | lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex; | |
213e4dc2 TJB |
1169 | lai->primitive_type_vector [c_primitive_type_decfloat] = builtin->builtin_decfloat; |
1170 | lai->primitive_type_vector [c_primitive_type_decdouble] = builtin->builtin_decdouble; | |
1171 | lai->primitive_type_vector [c_primitive_type_declong] = builtin->builtin_declong; | |
fbb06eb1 UW |
1172 | |
1173 | lai->bool_type_default = builtin->builtin_int; | |
cad351d1 | 1174 | } |
685419e2 | 1175 | |
6aecb9c2 | 1176 | const struct exp_descriptor exp_descriptor_c = |
6c7a06a3 TT |
1177 | { |
1178 | print_subexp_standard, | |
1179 | operator_length_standard, | |
c0201579 | 1180 | operator_check_standard, |
6c7a06a3 TT |
1181 | op_name_standard, |
1182 | dump_subexp_body_standard, | |
1183 | evaluate_subexp_c | |
1184 | }; | |
1185 | ||
c5aa993b JM |
1186 | const struct language_defn c_language_defn = |
1187 | { | |
c906108c SS |
1188 | "c", /* Language name */ |
1189 | language_c, | |
c906108c SS |
1190 | range_check_off, |
1191 | type_check_off, | |
63872f9d | 1192 | case_sensitive_on, |
7ca2d3a3 | 1193 | array_row_major, |
9a044a89 | 1194 | macro_expansion_c, |
6c7a06a3 | 1195 | &exp_descriptor_c, |
7c8adf68 | 1196 | c_parse, |
c906108c | 1197 | c_error, |
e85c3284 | 1198 | null_post_parser, |
c906108c SS |
1199 | c_printchar, /* Print a character constant */ |
1200 | c_printstr, /* Function to print string constant */ | |
1201 | c_emit_char, /* Print a single char */ | |
c906108c | 1202 | c_print_type, /* Print a type using appropriate syntax */ |
5c6ce71d | 1203 | c_print_typedef, /* Print a typedef using appropriate syntax */ |
c906108c SS |
1204 | c_val_print, /* Print a value using appropriate syntax */ |
1205 | c_value_print, /* Print a top-level value */ | |
f636b87d | 1206 | NULL, /* Language specific skip_trampoline */ |
2b2d9e11 | 1207 | NULL, /* name_of_this */ |
5f9a71c3 | 1208 | basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ |
b368761e | 1209 | basic_lookup_transparent_type,/* lookup_transparent_type */ |
9a3d7dfd | 1210 | NULL, /* Language specific symbol demangler */ |
aff410f1 MS |
1211 | NULL, /* Language specific |
1212 | class_name_from_physname */ | |
c906108c SS |
1213 | c_op_print_tab, /* expression operators for printing */ |
1214 | 1, /* c-style arrays */ | |
1215 | 0, /* String lower bound */ | |
6084f43a | 1216 | default_word_break_characters, |
41d27058 | 1217 | default_make_symbol_completion_list, |
685419e2 | 1218 | c_language_arch_info, |
e79af960 | 1219 | default_print_array_index, |
41f1b697 | 1220 | default_pass_by_reference, |
ae6a3a4c | 1221 | c_get_string, |
c906108c SS |
1222 | LANG_MAGIC |
1223 | }; | |
1224 | ||
cad351d1 UW |
1225 | enum cplus_primitive_types { |
1226 | cplus_primitive_type_int, | |
1227 | cplus_primitive_type_long, | |
1228 | cplus_primitive_type_short, | |
1229 | cplus_primitive_type_char, | |
1230 | cplus_primitive_type_float, | |
1231 | cplus_primitive_type_double, | |
1232 | cplus_primitive_type_void, | |
1233 | cplus_primitive_type_long_long, | |
1234 | cplus_primitive_type_signed_char, | |
1235 | cplus_primitive_type_unsigned_char, | |
1236 | cplus_primitive_type_unsigned_short, | |
1237 | cplus_primitive_type_unsigned_int, | |
1238 | cplus_primitive_type_unsigned_long, | |
1239 | cplus_primitive_type_unsigned_long_long, | |
1240 | cplus_primitive_type_long_double, | |
1241 | cplus_primitive_type_complex, | |
1242 | cplus_primitive_type_double_complex, | |
1243 | cplus_primitive_type_bool, | |
213e4dc2 TJB |
1244 | cplus_primitive_type_decfloat, |
1245 | cplus_primitive_type_decdouble, | |
1246 | cplus_primitive_type_declong, | |
cad351d1 | 1247 | nr_cplus_primitive_types |
c906108c SS |
1248 | }; |
1249 | ||
cad351d1 UW |
1250 | static void |
1251 | cplus_language_arch_info (struct gdbarch *gdbarch, | |
1252 | struct language_arch_info *lai) | |
1253 | { | |
1254 | const struct builtin_type *builtin = builtin_type (gdbarch); | |
c5504eaf | 1255 | |
cad351d1 UW |
1256 | lai->string_char_type = builtin->builtin_char; |
1257 | lai->primitive_type_vector | |
1258 | = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1, | |
1259 | struct type *); | |
1260 | lai->primitive_type_vector [cplus_primitive_type_int] | |
1261 | = builtin->builtin_int; | |
1262 | lai->primitive_type_vector [cplus_primitive_type_long] | |
1263 | = builtin->builtin_long; | |
1264 | lai->primitive_type_vector [cplus_primitive_type_short] | |
1265 | = builtin->builtin_short; | |
1266 | lai->primitive_type_vector [cplus_primitive_type_char] | |
1267 | = builtin->builtin_char; | |
1268 | lai->primitive_type_vector [cplus_primitive_type_float] | |
1269 | = builtin->builtin_float; | |
1270 | lai->primitive_type_vector [cplus_primitive_type_double] | |
1271 | = builtin->builtin_double; | |
1272 | lai->primitive_type_vector [cplus_primitive_type_void] | |
1273 | = builtin->builtin_void; | |
1274 | lai->primitive_type_vector [cplus_primitive_type_long_long] | |
1275 | = builtin->builtin_long_long; | |
1276 | lai->primitive_type_vector [cplus_primitive_type_signed_char] | |
1277 | = builtin->builtin_signed_char; | |
1278 | lai->primitive_type_vector [cplus_primitive_type_unsigned_char] | |
1279 | = builtin->builtin_unsigned_char; | |
1280 | lai->primitive_type_vector [cplus_primitive_type_unsigned_short] | |
1281 | = builtin->builtin_unsigned_short; | |
1282 | lai->primitive_type_vector [cplus_primitive_type_unsigned_int] | |
1283 | = builtin->builtin_unsigned_int; | |
1284 | lai->primitive_type_vector [cplus_primitive_type_unsigned_long] | |
1285 | = builtin->builtin_unsigned_long; | |
1286 | lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long] | |
1287 | = builtin->builtin_unsigned_long_long; | |
1288 | lai->primitive_type_vector [cplus_primitive_type_long_double] | |
1289 | = builtin->builtin_long_double; | |
1290 | lai->primitive_type_vector [cplus_primitive_type_complex] | |
1291 | = builtin->builtin_complex; | |
1292 | lai->primitive_type_vector [cplus_primitive_type_double_complex] | |
1293 | = builtin->builtin_double_complex; | |
1294 | lai->primitive_type_vector [cplus_primitive_type_bool] | |
1295 | = builtin->builtin_bool; | |
213e4dc2 TJB |
1296 | lai->primitive_type_vector [cplus_primitive_type_decfloat] |
1297 | = builtin->builtin_decfloat; | |
1298 | lai->primitive_type_vector [cplus_primitive_type_decdouble] | |
1299 | = builtin->builtin_decdouble; | |
1300 | lai->primitive_type_vector [cplus_primitive_type_declong] | |
1301 | = builtin->builtin_declong; | |
fbb06eb1 UW |
1302 | |
1303 | lai->bool_type_symbol = "bool"; | |
1304 | lai->bool_type_default = builtin->builtin_bool; | |
cad351d1 UW |
1305 | } |
1306 | ||
c5aa993b JM |
1307 | const struct language_defn cplus_language_defn = |
1308 | { | |
1309 | "c++", /* Language name */ | |
c906108c | 1310 | language_cplus, |
c906108c SS |
1311 | range_check_off, |
1312 | type_check_off, | |
63872f9d | 1313 | case_sensitive_on, |
7ca2d3a3 | 1314 | array_row_major, |
9a044a89 | 1315 | macro_expansion_c, |
6c7a06a3 | 1316 | &exp_descriptor_c, |
7c8adf68 | 1317 | c_parse, |
c906108c | 1318 | c_error, |
e85c3284 | 1319 | null_post_parser, |
c906108c SS |
1320 | c_printchar, /* Print a character constant */ |
1321 | c_printstr, /* Function to print string constant */ | |
1322 | c_emit_char, /* Print a single char */ | |
c906108c | 1323 | c_print_type, /* Print a type using appropriate syntax */ |
5c6ce71d | 1324 | c_print_typedef, /* Print a typedef using appropriate syntax */ |
c906108c SS |
1325 | c_val_print, /* Print a value using appropriate syntax */ |
1326 | c_value_print, /* Print a top-level value */ | |
b18be20d | 1327 | cplus_skip_trampoline, /* Language specific skip_trampoline */ |
2b2d9e11 | 1328 | "this", /* name_of_this */ |
1fcb5155 | 1329 | cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ |
b368761e | 1330 | cp_lookup_transparent_type, /* lookup_transparent_type */ |
9a3d7dfd | 1331 | cplus_demangle, /* Language specific symbol demangler */ |
aff410f1 MS |
1332 | cp_class_name_from_physname, /* Language specific |
1333 | class_name_from_physname */ | |
c906108c SS |
1334 | c_op_print_tab, /* expression operators for printing */ |
1335 | 1, /* c-style arrays */ | |
1336 | 0, /* String lower bound */ | |
6084f43a | 1337 | default_word_break_characters, |
41d27058 | 1338 | default_make_symbol_completion_list, |
cad351d1 | 1339 | cplus_language_arch_info, |
e79af960 | 1340 | default_print_array_index, |
41f1b697 | 1341 | cp_pass_by_reference, |
ae6a3a4c | 1342 | c_get_string, |
c906108c SS |
1343 | LANG_MAGIC |
1344 | }; | |
1345 | ||
c5aa993b JM |
1346 | const struct language_defn asm_language_defn = |
1347 | { | |
c906108c SS |
1348 | "asm", /* Language name */ |
1349 | language_asm, | |
c906108c SS |
1350 | range_check_off, |
1351 | type_check_off, | |
63872f9d | 1352 | case_sensitive_on, |
7ca2d3a3 | 1353 | array_row_major, |
9a044a89 | 1354 | macro_expansion_c, |
6c7a06a3 | 1355 | &exp_descriptor_c, |
7c8adf68 | 1356 | c_parse, |
c906108c | 1357 | c_error, |
e85c3284 | 1358 | null_post_parser, |
c906108c SS |
1359 | c_printchar, /* Print a character constant */ |
1360 | c_printstr, /* Function to print string constant */ | |
1361 | c_emit_char, /* Print a single char */ | |
c906108c | 1362 | c_print_type, /* Print a type using appropriate syntax */ |
5c6ce71d | 1363 | c_print_typedef, /* Print a typedef using appropriate syntax */ |
c906108c SS |
1364 | c_val_print, /* Print a value using appropriate syntax */ |
1365 | c_value_print, /* Print a top-level value */ | |
f636b87d | 1366 | NULL, /* Language specific skip_trampoline */ |
2b2d9e11 | 1367 | NULL, /* name_of_this */ |
5f9a71c3 | 1368 | basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ |
b368761e | 1369 | basic_lookup_transparent_type,/* lookup_transparent_type */ |
9a3d7dfd | 1370 | NULL, /* Language specific symbol demangler */ |
aff410f1 MS |
1371 | NULL, /* Language specific |
1372 | class_name_from_physname */ | |
c906108c SS |
1373 | c_op_print_tab, /* expression operators for printing */ |
1374 | 1, /* c-style arrays */ | |
1375 | 0, /* String lower bound */ | |
6084f43a | 1376 | default_word_break_characters, |
41d27058 | 1377 | default_make_symbol_completion_list, |
aff410f1 | 1378 | c_language_arch_info, /* FIXME: la_language_arch_info. */ |
e79af960 | 1379 | default_print_array_index, |
41f1b697 | 1380 | default_pass_by_reference, |
ae6a3a4c | 1381 | c_get_string, |
c906108c SS |
1382 | LANG_MAGIC |
1383 | }; | |
1384 | ||
20a0e81d JB |
1385 | /* The following language_defn does not represent a real language. |
1386 | It just provides a minimal support a-la-C that should allow users | |
1387 | to do some simple operations when debugging applications that use | |
1388 | a language currently not supported by GDB. */ | |
1389 | ||
1390 | const struct language_defn minimal_language_defn = | |
1391 | { | |
1392 | "minimal", /* Language name */ | |
1393 | language_minimal, | |
20a0e81d JB |
1394 | range_check_off, |
1395 | type_check_off, | |
1396 | case_sensitive_on, | |
7ca2d3a3 | 1397 | array_row_major, |
9a044a89 | 1398 | macro_expansion_c, |
6c7a06a3 | 1399 | &exp_descriptor_c, |
7c8adf68 | 1400 | c_parse, |
20a0e81d | 1401 | c_error, |
e85c3284 | 1402 | null_post_parser, |
20a0e81d JB |
1403 | c_printchar, /* Print a character constant */ |
1404 | c_printstr, /* Function to print string constant */ | |
1405 | c_emit_char, /* Print a single char */ | |
20a0e81d | 1406 | c_print_type, /* Print a type using appropriate syntax */ |
5c6ce71d | 1407 | c_print_typedef, /* Print a typedef using appropriate syntax */ |
20a0e81d JB |
1408 | c_val_print, /* Print a value using appropriate syntax */ |
1409 | c_value_print, /* Print a top-level value */ | |
1410 | NULL, /* Language specific skip_trampoline */ | |
2b2d9e11 | 1411 | NULL, /* name_of_this */ |
5f9a71c3 | 1412 | basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */ |
b368761e | 1413 | basic_lookup_transparent_type,/* lookup_transparent_type */ |
20a0e81d | 1414 | NULL, /* Language specific symbol demangler */ |
aff410f1 MS |
1415 | NULL, /* Language specific |
1416 | class_name_from_physname */ | |
20a0e81d JB |
1417 | c_op_print_tab, /* expression operators for printing */ |
1418 | 1, /* c-style arrays */ | |
1419 | 0, /* String lower bound */ | |
6084f43a | 1420 | default_word_break_characters, |
41d27058 | 1421 | default_make_symbol_completion_list, |
e9667a65 | 1422 | c_language_arch_info, |
e79af960 | 1423 | default_print_array_index, |
41f1b697 | 1424 | default_pass_by_reference, |
ae6a3a4c | 1425 | c_get_string, |
20a0e81d JB |
1426 | LANG_MAGIC |
1427 | }; | |
1428 | ||
c906108c | 1429 | void |
fba45db2 | 1430 | _initialize_c_language (void) |
c906108c SS |
1431 | { |
1432 | add_language (&c_language_defn); | |
1433 | add_language (&cplus_language_defn); | |
1434 | add_language (&asm_language_defn); | |
20a0e81d | 1435 | add_language (&minimal_language_defn); |
c906108c | 1436 | } |