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