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