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