1 /* C language support routines for GDB, the GNU debugger.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2002, 2003,
4 2004, 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
24 #include "expression.h"
25 #include "parser-defs.h"
29 #include "macroscope.h"
30 #include "gdb_assert.h"
32 #include "gdb_string.h"
35 #include "cp-support.h"
36 #include "gdb_obstack.h"
38 #include "exceptions.h"
40 extern void _initialize_c_language (void);
42 /* Given a C string type, STR_TYPE, return the corresponding target
43 character set name. */
46 charset_for_string_type (enum c_string_type str_type
,
47 struct gdbarch
*gdbarch
)
49 switch (str_type
& ~C_CHAR
)
52 return target_charset (gdbarch
);
54 return target_wide_charset (gdbarch
);
56 /* FIXME: UTF-16 is not always correct. */
57 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
62 /* FIXME: UTF-32 is not always correct. */
63 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
68 internal_error (__FILE__
, __LINE__
, _("unhandled c_string_type"));
71 /* Classify ELTTYPE according to what kind of character it is. Return
72 the enum constant representing the character type. Also set
73 *ENCODING to the name of the character set to use when converting
74 characters of this type in target BYTE_ORDER to the host character
77 static enum c_string_type
78 classify_type (struct type
*elttype
, struct gdbarch
*gdbarch
,
79 const char **encoding
)
81 enum c_string_type result
;
83 /* We loop because ELTTYPE may be a typedef, and we want to
84 successively peel each typedef until we reach a type we
85 understand. We don't use CHECK_TYPEDEF because that will strip
86 all typedefs at once -- but in C, wchar_t is itself a typedef, so
87 that would do the wrong thing. */
90 char *name
= TYPE_NAME (elttype
);
92 if (TYPE_CODE (elttype
) == TYPE_CODE_CHAR
|| !name
)
98 if (!strcmp (name
, "wchar_t"))
100 result
= C_WIDE_CHAR
;
104 if (!strcmp (name
, "char16_t"))
110 if (!strcmp (name
, "char32_t"))
116 if (TYPE_CODE (elttype
) != TYPE_CODE_TYPEDEF
)
119 /* Call for side effects. */
120 check_typedef (elttype
);
122 if (TYPE_TARGET_TYPE (elttype
))
123 elttype
= TYPE_TARGET_TYPE (elttype
);
126 /* Perhaps check_typedef did not update the target type. In
127 this case, force the lookup again and hope it works out.
128 It never will for C, but it might for C++. */
129 CHECK_TYPEDEF (elttype
);
138 *encoding
= charset_for_string_type (result
, gdbarch
);
143 /* Return true if print_wchar can display W without resorting to a
144 numeric escape, false otherwise. */
147 wchar_printable (gdb_wchar_t w
)
149 return (gdb_iswprint (w
)
150 || w
== LCST ('\a') || w
== LCST ('\b')
151 || w
== LCST ('\f') || w
== LCST ('\n')
152 || w
== LCST ('\r') || w
== LCST ('\t')
153 || w
== LCST ('\v'));
156 /* A helper function that converts the contents of STRING to wide
157 characters and then appends them to OUTPUT. */
160 append_string_as_wide (const char *string
,
161 struct obstack
*output
)
163 for (; *string
; ++string
)
165 gdb_wchar_t w
= gdb_btowc (*string
);
166 obstack_grow (output
, &w
, sizeof (gdb_wchar_t
));
170 /* Print a wide character W to OUTPUT. ORIG is a pointer to the
171 original (target) bytes representing the character, ORIG_LEN is the
172 number of valid bytes. WIDTH is the number of bytes in a base
173 characters of the type. OUTPUT is an obstack to which wide
174 characters are emitted. QUOTER is a (narrow) character indicating
175 the style of quotes surrounding the character to be printed.
176 NEED_ESCAPE is an in/out flag which is used to track numeric
177 escapes across calls. */
180 print_wchar (gdb_wint_t w
, const gdb_byte
*orig
,
181 int orig_len
, int width
,
182 enum bfd_endian byte_order
,
183 struct obstack
*output
,
184 int quoter
, int *need_escapep
)
186 int need_escape
= *need_escapep
;
189 if (gdb_iswprint (w
) && (!need_escape
|| (!gdb_iswdigit (w
)
191 && w
!= LCST ('9'))))
193 gdb_wchar_t wchar
= w
;
195 if (w
== gdb_btowc (quoter
) || w
== LCST ('\\'))
196 obstack_grow_wstr (output
, LCST ("\\"));
197 obstack_grow (output
, &wchar
, sizeof (gdb_wchar_t
));
204 obstack_grow_wstr (output
, LCST ("\\a"));
207 obstack_grow_wstr (output
, LCST ("\\b"));
210 obstack_grow_wstr (output
, LCST ("\\f"));
213 obstack_grow_wstr (output
, LCST ("\\n"));
216 obstack_grow_wstr (output
, LCST ("\\r"));
219 obstack_grow_wstr (output
, LCST ("\\t"));
222 obstack_grow_wstr (output
, LCST ("\\v"));
228 for (i
= 0; i
+ width
<= orig_len
; i
+= width
)
233 value
= extract_unsigned_integer (&orig
[i
], width
,
235 /* If the value fits in 3 octal digits, print it that
236 way. Otherwise, print it as a hex escape. */
238 sprintf (octal
, "\\%.3o", (int) (value
& 0777));
240 sprintf (octal
, "\\x%lx", (long) value
);
241 append_string_as_wide (octal
, output
);
243 /* If we somehow have extra bytes, print them now. */
248 sprintf (octal
, "\\%.3o", orig
[i
] & 0xff);
249 append_string_as_wide (octal
, output
);
260 /* Print the character C on STREAM as part of the contents of a
261 literal string whose delimiter is QUOTER. Note that that format
262 for printing characters and strings is language specific. */
265 c_emit_char (int c
, struct type
*type
,
266 struct ui_file
*stream
, int quoter
)
268 enum bfd_endian byte_order
269 = gdbarch_byte_order (get_type_arch (type
));
270 struct obstack wchar_buf
, output
;
271 struct cleanup
*cleanups
;
272 const char *encoding
;
274 struct wchar_iterator
*iter
;
277 classify_type (type
, get_type_arch (type
), &encoding
);
279 buf
= alloca (TYPE_LENGTH (type
));
280 pack_long (buf
, type
, c
);
282 iter
= make_wchar_iterator (buf
, TYPE_LENGTH (type
),
283 encoding
, TYPE_LENGTH (type
));
284 cleanups
= make_cleanup_wchar_iterator (iter
);
286 /* This holds the printable form of the wchar_t data. */
287 obstack_init (&wchar_buf
);
288 make_cleanup_obstack_free (&wchar_buf
);
296 int print_escape
= 1;
297 enum wchar_iterate_result result
;
299 num_chars
= wchar_iterate (iter
, &result
, &chars
, &buf
, &buflen
);
304 /* If all characters are printable, print them. Otherwise,
305 we're going to have to print an escape sequence. We
306 check all characters because we want to print the target
307 bytes in the escape sequence, and we don't know character
312 for (i
= 0; i
< num_chars
; ++i
)
313 if (!wchar_printable (chars
[i
]))
321 for (i
= 0; i
< num_chars
; ++i
)
322 print_wchar (chars
[i
], buf
, buflen
,
323 TYPE_LENGTH (type
), byte_order
,
324 &wchar_buf
, quoter
, &need_escape
);
328 /* This handles the NUM_CHARS == 0 case as well. */
330 print_wchar (gdb_WEOF
, buf
, buflen
, TYPE_LENGTH (type
),
331 byte_order
, &wchar_buf
, quoter
, &need_escape
);
334 /* The output in the host encoding. */
335 obstack_init (&output
);
336 make_cleanup_obstack_free (&output
);
338 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
339 obstack_base (&wchar_buf
),
340 obstack_object_size (&wchar_buf
),
341 1, &output
, translit_char
);
342 obstack_1grow (&output
, '\0');
344 fputs_filtered (obstack_base (&output
), stream
);
346 do_cleanups (cleanups
);
350 c_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
352 enum c_string_type str_type
;
354 str_type
= classify_type (type
, get_type_arch (type
), NULL
);
360 fputc_filtered ('L', stream
);
363 fputc_filtered ('u', stream
);
366 fputc_filtered ('U', stream
);
370 fputc_filtered ('\'', stream
);
371 LA_EMIT_CHAR (c
, type
, stream
, '\'');
372 fputc_filtered ('\'', stream
);
375 /* Print the character string STRING, printing at most LENGTH
376 characters. LENGTH is -1 if the string is nul terminated. Each
377 character is WIDTH bytes long. Printing stops early if the number
378 hits print_max; repeat counts are printed as appropriate. Print
379 ellipses at the end if we had to stop before printing LENGTH
380 characters, or if FORCE_ELLIPSES. */
383 c_printstr (struct ui_file
*stream
, struct type
*type
,
384 const gdb_byte
*string
, unsigned int length
,
385 const char *user_encoding
, int force_ellipses
,
386 const struct value_print_options
*options
)
388 enum bfd_endian byte_order
= gdbarch_byte_order (get_type_arch (type
));
390 unsigned int things_printed
= 0;
393 int width
= TYPE_LENGTH (type
);
394 struct obstack wchar_buf
, output
;
395 struct cleanup
*cleanup
;
396 enum c_string_type str_type
;
397 const char *type_encoding
;
398 const char *encoding
;
399 struct wchar_iterator
*iter
;
405 unsigned long current_char
= 1;
407 for (i
= 0; current_char
; ++i
)
410 current_char
= extract_unsigned_integer (string
+ i
* width
,
416 /* If the string was not truncated due to `set print elements', and
417 the last byte of it is a null, we don't print that, in
418 traditional C style. */
421 && (extract_unsigned_integer (string
+ (length
- 1) * width
,
422 width
, byte_order
) == 0))
425 str_type
= (classify_type (type
, get_type_arch (type
), &type_encoding
)
432 fputs_filtered ("L", stream
);
435 fputs_filtered ("u", stream
);
438 fputs_filtered ("U", stream
);
442 encoding
= (user_encoding
&& *user_encoding
)
443 ? user_encoding
: type_encoding
;
447 fputs_filtered ("\"\"", stream
);
451 /* Arrange to iterate over the characters, in wchar_t form. */
452 iter
= make_wchar_iterator (string
, length
* width
, encoding
, width
);
453 cleanup
= make_cleanup_wchar_iterator (iter
);
455 /* WCHAR_BUF is the obstack we use to represent the string in
457 obstack_init (&wchar_buf
);
458 make_cleanup_obstack_free (&wchar_buf
);
460 while (!finished
&& things_printed
< options
->print_max
)
463 enum wchar_iterate_result result
;
472 obstack_grow_wstr (&wchar_buf
, LCST (", "));
476 num_chars
= wchar_iterate (iter
, &result
, &chars
, &buf
, &buflen
);
477 /* We only look at repetitions when we were able to convert a
478 single character in isolation. This makes the code simpler
479 and probably does the sensible thing in the majority of
481 while (num_chars
== 1 && things_printed
< options
->print_max
)
483 /* Count the number of repetitions. */
484 unsigned int reps
= 0;
485 gdb_wchar_t current_char
= chars
[0];
486 const gdb_byte
*orig_buf
= buf
;
487 int orig_len
= buflen
;
491 obstack_grow_wstr (&wchar_buf
, LCST (", "));
495 while (num_chars
== 1 && current_char
== chars
[0])
497 num_chars
= wchar_iterate (iter
, &result
, &chars
,
502 /* Emit CURRENT_CHAR according to the repetition count and
504 if (reps
> options
->repeat_count_threshold
)
508 if (options
->inspect_it
)
509 obstack_grow_wstr (&wchar_buf
, LCST ("\\\", "));
511 obstack_grow_wstr (&wchar_buf
, LCST ("\", "));
514 obstack_grow_wstr (&wchar_buf
, LCST ("'"));
516 print_wchar (current_char
, orig_buf
, orig_len
, width
,
517 byte_order
, &wchar_buf
, '\'', &need_escape
);
518 obstack_grow_wstr (&wchar_buf
, LCST ("'"));
520 /* Painful gyrations. */
522 char *s
= xstrprintf (_(" <repeats %u times>"), reps
);
524 for (j
= 0; s
[j
]; ++j
)
526 gdb_wchar_t w
= gdb_btowc (s
[j
]);
527 obstack_grow (&wchar_buf
, &w
, sizeof (gdb_wchar_t
));
531 things_printed
+= options
->repeat_count_threshold
;
536 /* Saw the character one or more times, but fewer than
537 the repetition threshold. */
540 if (options
->inspect_it
)
541 obstack_grow_wstr (&wchar_buf
, LCST ("\\\""));
543 obstack_grow_wstr (&wchar_buf
, LCST ("\""));
550 print_wchar (current_char
, orig_buf
,
552 byte_order
, &wchar_buf
,
559 /* NUM_CHARS and the other outputs from wchar_iterate are valid
560 here regardless of which branch was taken above. */
570 case wchar_iterate_invalid
:
573 if (options
->inspect_it
)
574 obstack_grow_wstr (&wchar_buf
, LCST ("\\\""));
576 obstack_grow_wstr (&wchar_buf
, LCST ("\""));
580 print_wchar (gdb_WEOF
, buf
, buflen
, width
, byte_order
,
581 &wchar_buf
, '"', &need_escape
);
584 case wchar_iterate_incomplete
:
587 if (options
->inspect_it
)
588 obstack_grow_wstr (&wchar_buf
, LCST ("\\\","));
590 obstack_grow_wstr (&wchar_buf
, LCST ("\","));
593 obstack_grow_wstr (&wchar_buf
,
594 LCST (" <incomplete sequence "));
595 print_wchar (gdb_WEOF
, buf
, buflen
, width
,
596 byte_order
, &wchar_buf
,
598 obstack_grow_wstr (&wchar_buf
, LCST (">"));
604 /* Terminate the quotes if necessary. */
607 if (options
->inspect_it
)
608 obstack_grow_wstr (&wchar_buf
, LCST ("\\\""));
610 obstack_grow_wstr (&wchar_buf
, LCST ("\""));
613 if (force_ellipses
|| !finished
)
614 obstack_grow_wstr (&wchar_buf
, LCST ("..."));
616 /* OUTPUT is where we collect `char's for printing. */
617 obstack_init (&output
);
618 make_cleanup_obstack_free (&output
);
620 convert_between_encodings (INTERMEDIATE_ENCODING
, host_charset (),
621 obstack_base (&wchar_buf
),
622 obstack_object_size (&wchar_buf
),
623 1, &output
, translit_char
);
624 obstack_1grow (&output
, '\0');
626 fputs_filtered (obstack_base (&output
), stream
);
628 do_cleanups (cleanup
);
631 /* Obtain a C string from the inferior storing it in a newly allocated
632 buffer in BUFFER, which should be freed by the caller. If the in-
633 and out-parameter *LENGTH is specified at -1, the string is read
634 until a null character of the appropriate width is found, otherwise
635 the string is read to the length of characters specified. The size
636 of a character is determined by the length of the target type of
637 the pointer or array. If VALUE is an array with a known length,
638 the function will not read past the end of the array. On
639 completion, *LENGTH will be set to the size of the string read in
640 characters. (If a length of -1 is specified, the length returned
641 will not include the null character). CHARSET is always set to the
645 c_get_string (struct value
*value
, gdb_byte
**buffer
,
646 int *length
, struct type
**char_type
,
647 const char **charset
)
650 unsigned int fetchlimit
;
651 struct type
*type
= check_typedef (value_type (value
));
652 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
653 int req_length
= *length
;
654 enum bfd_endian byte_order
655 = gdbarch_byte_order (get_type_arch (type
));
656 enum c_string_type kind
;
658 if (element_type
== NULL
)
661 if (TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
663 /* If we know the size of the array, we can use it as a limit on
664 the number of characters to be fetched. */
665 if (TYPE_NFIELDS (type
) == 1
666 && TYPE_CODE (TYPE_FIELD_TYPE (type
, 0)) == TYPE_CODE_RANGE
)
668 LONGEST low_bound
, high_bound
;
670 get_discrete_bounds (TYPE_FIELD_TYPE (type
, 0),
671 &low_bound
, &high_bound
);
672 fetchlimit
= high_bound
- low_bound
+ 1;
675 fetchlimit
= UINT_MAX
;
677 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
678 fetchlimit
= UINT_MAX
;
680 /* We work only with arrays and pointers. */
683 if (! c_textual_element_type (element_type
, 0))
685 kind
= classify_type (element_type
,
686 get_type_arch (element_type
),
688 width
= TYPE_LENGTH (element_type
);
690 /* If the string lives in GDB's memory instead of the inferior's,
691 then we just need to copy it to BUFFER. Also, since such strings
692 are arrays with known size, FETCHLIMIT will hold the size of the
694 if ((VALUE_LVAL (value
) == not_lval
695 || VALUE_LVAL (value
) == lval_internalvar
)
696 && fetchlimit
!= UINT_MAX
)
699 const gdb_byte
*contents
= value_contents (value
);
701 /* If a length is specified, use that. */
705 /* Otherwise, look for a null character. */
706 for (i
= 0; i
< fetchlimit
; i
++)
707 if (extract_unsigned_integer (contents
+ i
* width
,
708 width
, byte_order
) == 0)
711 /* I is now either a user-defined length, the number of non-null
712 characters, or FETCHLIMIT. */
714 *buffer
= xmalloc (*length
);
715 memcpy (*buffer
, contents
, *length
);
720 CORE_ADDR addr
= value_as_address (value
);
722 err
= read_string (addr
, *length
, width
, fetchlimit
,
723 byte_order
, buffer
, length
);
728 throw_error (MEMORY_ERROR
, "Address %s out of bounds",
729 paddress (get_type_arch (type
), addr
));
731 error (_("Error reading string from inferior: %s"),
732 safe_strerror (err
));
736 /* If the LENGTH is specified at -1, we want to return the string
737 length up to the terminating null character. If an actual length
738 was specified, we want to return the length of exactly what was
740 if (req_length
== -1)
741 /* If the last character is null, subtract it from LENGTH. */
743 && extract_unsigned_integer (*buffer
+ *length
- width
,
744 width
, byte_order
) == 0)
747 /* The read_string function will return the number of bytes read.
748 If length returned from read_string was > 0, return the number of
749 characters read by dividing the number of bytes by width. */
751 *length
= *length
/ width
;
753 *char_type
= element_type
;
761 type_str
= type_to_string (type
);
764 make_cleanup (xfree
, type_str
);
765 error (_("Trying to read string with inappropriate type `%s'."),
769 error (_("Trying to read string with inappropriate type."));
774 /* Evaluating C and C++ expressions. */
776 /* Convert a UCN. The digits of the UCN start at P and extend no
777 farther than LIMIT. DEST_CHARSET is the name of the character set
778 into which the UCN should be converted. The results are written to
779 OUTPUT. LENGTH is the maximum length of the UCN, either 4 or 8.
780 Returns a pointer to just after the final digit of the UCN. */
783 convert_ucn (char *p
, char *limit
, const char *dest_charset
,
784 struct obstack
*output
, int length
)
786 unsigned long result
= 0;
790 for (i
= 0; i
< length
&& p
< limit
&& isxdigit (*p
); ++i
, ++p
)
791 result
= (result
<< 4) + host_hex_value (*p
);
793 for (i
= 3; i
>= 0; --i
)
795 data
[i
] = result
& 0xff;
799 convert_between_encodings ("UTF-32BE", dest_charset
, data
,
800 4, 4, output
, translit_none
);
805 /* Emit a character, VALUE, which was specified numerically, to
806 OUTPUT. TYPE is the target character type. */
809 emit_numeric_character (struct type
*type
, unsigned long value
,
810 struct obstack
*output
)
814 buffer
= alloca (TYPE_LENGTH (type
));
815 pack_long (buffer
, type
, value
);
816 obstack_grow (output
, buffer
, TYPE_LENGTH (type
));
819 /* Convert an octal escape sequence. TYPE is the target character
820 type. The digits of the escape sequence begin at P and extend no
821 farther than LIMIT. The result is written to OUTPUT. Returns a
822 pointer to just after the final digit of the escape sequence. */
825 convert_octal (struct type
*type
, char *p
,
826 char *limit
, struct obstack
*output
)
829 unsigned long value
= 0;
832 i
< 3 && p
< limit
&& isdigit (*p
) && *p
!= '8' && *p
!= '9';
835 value
= 8 * value
+ host_hex_value (*p
);
839 emit_numeric_character (type
, value
, output
);
844 /* Convert a hex escape sequence. TYPE is the target character type.
845 The digits of the escape sequence begin at P and extend no farther
846 than LIMIT. The result is written to OUTPUT. Returns a pointer to
847 just after the final digit of the escape sequence. */
850 convert_hex (struct type
*type
, char *p
,
851 char *limit
, struct obstack
*output
)
853 unsigned long value
= 0;
855 while (p
< limit
&& isxdigit (*p
))
857 value
= 16 * value
+ host_hex_value (*p
);
861 emit_numeric_character (type
, value
, output
);
870 error (_("Malformed escape sequence")); \
873 /* Convert an escape sequence to a target format. TYPE is the target
874 character type to use, and DEST_CHARSET is the name of the target
875 character set. The backslash of the escape sequence is at *P, and
876 the escape sequence will not extend past LIMIT. The results are
877 written to OUTPUT. Returns a pointer to just past the final
878 character of the escape sequence. */
881 convert_escape (struct type
*type
, const char *dest_charset
,
882 char *p
, char *limit
, struct obstack
*output
)
884 /* Skip the backslash. */
890 obstack_1grow (output
, '\\');
897 error (_("\\x used with no following hex digits."));
898 p
= convert_hex (type
, p
, limit
, output
);
909 p
= convert_octal (type
, p
, limit
, output
);
915 int length
= *p
== 'u' ? 4 : 8;
919 error (_("\\u used with no following hex digits"));
920 p
= convert_ucn (p
, limit
, dest_charset
, output
, length
);
927 /* Given a single string from a (C-specific) OP_STRING list, convert
928 it to a target string, handling escape sequences specially. The
929 output is written to OUTPUT. DATA is the input string, which has
930 length LEN. DEST_CHARSET is the name of the target character set,
931 and TYPE is the type of target character to use. */
934 parse_one_string (struct obstack
*output
, char *data
, int len
,
935 const char *dest_charset
, struct type
*type
)
945 /* Look for next escape, or the end of the input. */
946 while (p
< limit
&& *p
!= '\\')
948 /* If we saw a run of characters, convert them all. */
950 convert_between_encodings (host_charset (), dest_charset
,
952 output
, translit_none
);
953 /* If we saw an escape, convert it. */
955 p
= convert_escape (type
, dest_charset
, p
, limit
, output
);
960 /* Expression evaluator for the C language family. Most operations
961 are delegated to evaluate_subexp_standard; see that function for a
962 description of the arguments. */
965 evaluate_subexp_c (struct type
*expect_type
, struct expression
*exp
,
966 int *pos
, enum noside noside
)
968 enum exp_opcode op
= exp
->elts
[*pos
].opcode
;
976 struct obstack output
;
977 struct cleanup
*cleanup
;
978 struct value
*result
;
979 enum c_string_type dest_type
;
980 const char *dest_charset
;
981 int satisfy_expected
= 0;
983 obstack_init (&output
);
984 cleanup
= make_cleanup_obstack_free (&output
);
987 oplen
= longest_to_int (exp
->elts
[*pos
].longconst
);
990 limit
= *pos
+ BYTES_TO_EXP_ELEM (oplen
+ 1);
992 = (enum c_string_type
) longest_to_int (exp
->elts
[*pos
].longconst
);
993 switch (dest_type
& ~C_CHAR
)
996 type
= language_string_char_type (exp
->language_defn
,
1000 type
= lookup_typename (exp
->language_defn
, exp
->gdbarch
,
1001 "wchar_t", NULL
, 0);
1004 type
= lookup_typename (exp
->language_defn
, exp
->gdbarch
,
1005 "char16_t", NULL
, 0);
1008 type
= lookup_typename (exp
->language_defn
, exp
->gdbarch
,
1009 "char32_t", NULL
, 0);
1012 internal_error (__FILE__
, __LINE__
, _("unhandled c_string_type"));
1015 /* Ensure TYPE_LENGTH is valid for TYPE. */
1016 check_typedef (type
);
1018 /* If the caller expects an array of some integral type,
1019 satisfy them. If something odder is expected, rely on the
1021 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_ARRAY
)
1023 struct type
*element_type
1024 = check_typedef (TYPE_TARGET_TYPE (expect_type
));
1026 if (TYPE_CODE (element_type
) == TYPE_CODE_INT
1027 || TYPE_CODE (element_type
) == TYPE_CODE_CHAR
)
1029 type
= element_type
;
1030 satisfy_expected
= 1;
1034 dest_charset
= charset_for_string_type (dest_type
, exp
->gdbarch
);
1037 while (*pos
< limit
)
1041 len
= longest_to_int (exp
->elts
[*pos
].longconst
);
1044 if (noside
!= EVAL_SKIP
)
1045 parse_one_string (&output
, &exp
->elts
[*pos
].string
, len
,
1046 dest_charset
, type
);
1047 *pos
+= BYTES_TO_EXP_ELEM (len
);
1050 /* Skip the trailing length and opcode. */
1053 if (noside
== EVAL_SKIP
)
1055 /* Return a dummy value of the appropriate type. */
1056 if (expect_type
!= NULL
)
1057 result
= allocate_value (expect_type
);
1058 else if ((dest_type
& C_CHAR
) != 0)
1059 result
= allocate_value (type
);
1061 result
= value_cstring ("", 0, type
);
1062 do_cleanups (cleanup
);
1066 if ((dest_type
& C_CHAR
) != 0)
1070 if (obstack_object_size (&output
) != TYPE_LENGTH (type
))
1071 error (_("Could not convert character "
1072 "constant to target character set"));
1073 value
= unpack_long (type
, obstack_base (&output
));
1074 result
= value_from_longest (type
, value
);
1080 /* Write the terminating character. */
1081 for (i
= 0; i
< TYPE_LENGTH (type
); ++i
)
1082 obstack_1grow (&output
, 0);
1084 if (satisfy_expected
)
1086 LONGEST low_bound
, high_bound
;
1087 int element_size
= TYPE_LENGTH (type
);
1089 if (get_discrete_bounds (TYPE_INDEX_TYPE (expect_type
),
1090 &low_bound
, &high_bound
) < 0)
1093 high_bound
= (TYPE_LENGTH (expect_type
) / element_size
) - 1;
1095 if (obstack_object_size (&output
) / element_size
1096 > (high_bound
- low_bound
+ 1))
1097 error (_("Too many array elements"));
1099 result
= allocate_value (expect_type
);
1100 memcpy (value_contents_raw (result
), obstack_base (&output
),
1101 obstack_object_size (&output
));
1104 result
= value_cstring (obstack_base (&output
),
1105 obstack_object_size (&output
),
1108 do_cleanups (cleanup
);
1116 return evaluate_subexp_standard (expect_type
, exp
, pos
, noside
);
1121 /* Table mapping opcodes into strings for printing operators
1122 and precedences of the operators. */
1124 const struct op_print c_op_print_tab
[] =
1126 {",", BINOP_COMMA
, PREC_COMMA
, 0},
1127 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
1128 {"||", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
1129 {"&&", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
1130 {"|", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
1131 {"^", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
1132 {"&", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
1133 {"==", BINOP_EQUAL
, PREC_EQUAL
, 0},
1134 {"!=", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
1135 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
1136 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
1137 {">", BINOP_GTR
, PREC_ORDER
, 0},
1138 {"<", BINOP_LESS
, PREC_ORDER
, 0},
1139 {">>", BINOP_RSH
, PREC_SHIFT
, 0},
1140 {"<<", BINOP_LSH
, PREC_SHIFT
, 0},
1141 {"+", BINOP_ADD
, PREC_ADD
, 0},
1142 {"-", BINOP_SUB
, PREC_ADD
, 0},
1143 {"*", BINOP_MUL
, PREC_MUL
, 0},
1144 {"/", BINOP_DIV
, PREC_MUL
, 0},
1145 {"%", BINOP_REM
, PREC_MUL
, 0},
1146 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
1147 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
1148 {"!", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
1149 {"~", UNOP_COMPLEMENT
, PREC_PREFIX
, 0},
1150 {"*", UNOP_IND
, PREC_PREFIX
, 0},
1151 {"&", UNOP_ADDR
, PREC_PREFIX
, 0},
1152 {"sizeof ", UNOP_SIZEOF
, PREC_PREFIX
, 0},
1153 {"++", UNOP_PREINCREMENT
, PREC_PREFIX
, 0},
1154 {"--", UNOP_PREDECREMENT
, PREC_PREFIX
, 0},
1158 enum c_primitive_types
{
1159 c_primitive_type_int
,
1160 c_primitive_type_long
,
1161 c_primitive_type_short
,
1162 c_primitive_type_char
,
1163 c_primitive_type_float
,
1164 c_primitive_type_double
,
1165 c_primitive_type_void
,
1166 c_primitive_type_long_long
,
1167 c_primitive_type_signed_char
,
1168 c_primitive_type_unsigned_char
,
1169 c_primitive_type_unsigned_short
,
1170 c_primitive_type_unsigned_int
,
1171 c_primitive_type_unsigned_long
,
1172 c_primitive_type_unsigned_long_long
,
1173 c_primitive_type_long_double
,
1174 c_primitive_type_complex
,
1175 c_primitive_type_double_complex
,
1176 c_primitive_type_decfloat
,
1177 c_primitive_type_decdouble
,
1178 c_primitive_type_declong
,
1179 nr_c_primitive_types
1183 c_language_arch_info (struct gdbarch
*gdbarch
,
1184 struct language_arch_info
*lai
)
1186 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1188 lai
->string_char_type
= builtin
->builtin_char
;
1189 lai
->primitive_type_vector
1190 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_c_primitive_types
+ 1,
1192 lai
->primitive_type_vector
[c_primitive_type_int
] = builtin
->builtin_int
;
1193 lai
->primitive_type_vector
[c_primitive_type_long
] = builtin
->builtin_long
;
1194 lai
->primitive_type_vector
[c_primitive_type_short
] = builtin
->builtin_short
;
1195 lai
->primitive_type_vector
[c_primitive_type_char
] = builtin
->builtin_char
;
1196 lai
->primitive_type_vector
[c_primitive_type_float
] = builtin
->builtin_float
;
1197 lai
->primitive_type_vector
[c_primitive_type_double
] = builtin
->builtin_double
;
1198 lai
->primitive_type_vector
[c_primitive_type_void
] = builtin
->builtin_void
;
1199 lai
->primitive_type_vector
[c_primitive_type_long_long
] = builtin
->builtin_long_long
;
1200 lai
->primitive_type_vector
[c_primitive_type_signed_char
] = builtin
->builtin_signed_char
;
1201 lai
->primitive_type_vector
[c_primitive_type_unsigned_char
] = builtin
->builtin_unsigned_char
;
1202 lai
->primitive_type_vector
[c_primitive_type_unsigned_short
] = builtin
->builtin_unsigned_short
;
1203 lai
->primitive_type_vector
[c_primitive_type_unsigned_int
] = builtin
->builtin_unsigned_int
;
1204 lai
->primitive_type_vector
[c_primitive_type_unsigned_long
] = builtin
->builtin_unsigned_long
;
1205 lai
->primitive_type_vector
[c_primitive_type_unsigned_long_long
] = builtin
->builtin_unsigned_long_long
;
1206 lai
->primitive_type_vector
[c_primitive_type_long_double
] = builtin
->builtin_long_double
;
1207 lai
->primitive_type_vector
[c_primitive_type_complex
] = builtin
->builtin_complex
;
1208 lai
->primitive_type_vector
[c_primitive_type_double_complex
] = builtin
->builtin_double_complex
;
1209 lai
->primitive_type_vector
[c_primitive_type_decfloat
] = builtin
->builtin_decfloat
;
1210 lai
->primitive_type_vector
[c_primitive_type_decdouble
] = builtin
->builtin_decdouble
;
1211 lai
->primitive_type_vector
[c_primitive_type_declong
] = builtin
->builtin_declong
;
1213 lai
->bool_type_default
= builtin
->builtin_int
;
1216 const struct exp_descriptor exp_descriptor_c
=
1218 print_subexp_standard
,
1219 operator_length_standard
,
1220 operator_check_standard
,
1222 dump_subexp_body_standard
,
1226 const struct language_defn c_language_defn
=
1228 "c", /* Language name */
1239 c_printchar
, /* Print a character constant */
1240 c_printstr
, /* Function to print string constant */
1241 c_emit_char
, /* Print a single char */
1242 c_print_type
, /* Print a type using appropriate syntax */
1243 c_print_typedef
, /* Print a typedef using appropriate syntax */
1244 c_val_print
, /* Print a value using appropriate syntax */
1245 c_value_print
, /* Print a top-level value */
1246 NULL
, /* Language specific skip_trampoline */
1247 NULL
, /* name_of_this */
1248 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1249 basic_lookup_transparent_type
,/* lookup_transparent_type */
1250 NULL
, /* Language specific symbol demangler */
1251 NULL
, /* Language specific
1252 class_name_from_physname */
1253 c_op_print_tab
, /* expression operators for printing */
1254 1, /* c-style arrays */
1255 0, /* String lower bound */
1256 default_word_break_characters
,
1257 default_make_symbol_completion_list
,
1258 c_language_arch_info
,
1259 default_print_array_index
,
1260 default_pass_by_reference
,
1265 enum cplus_primitive_types
{
1266 cplus_primitive_type_int
,
1267 cplus_primitive_type_long
,
1268 cplus_primitive_type_short
,
1269 cplus_primitive_type_char
,
1270 cplus_primitive_type_float
,
1271 cplus_primitive_type_double
,
1272 cplus_primitive_type_void
,
1273 cplus_primitive_type_long_long
,
1274 cplus_primitive_type_signed_char
,
1275 cplus_primitive_type_unsigned_char
,
1276 cplus_primitive_type_unsigned_short
,
1277 cplus_primitive_type_unsigned_int
,
1278 cplus_primitive_type_unsigned_long
,
1279 cplus_primitive_type_unsigned_long_long
,
1280 cplus_primitive_type_long_double
,
1281 cplus_primitive_type_complex
,
1282 cplus_primitive_type_double_complex
,
1283 cplus_primitive_type_bool
,
1284 cplus_primitive_type_decfloat
,
1285 cplus_primitive_type_decdouble
,
1286 cplus_primitive_type_declong
,
1287 nr_cplus_primitive_types
1291 cplus_language_arch_info (struct gdbarch
*gdbarch
,
1292 struct language_arch_info
*lai
)
1294 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
1296 lai
->string_char_type
= builtin
->builtin_char
;
1297 lai
->primitive_type_vector
1298 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_cplus_primitive_types
+ 1,
1300 lai
->primitive_type_vector
[cplus_primitive_type_int
]
1301 = builtin
->builtin_int
;
1302 lai
->primitive_type_vector
[cplus_primitive_type_long
]
1303 = builtin
->builtin_long
;
1304 lai
->primitive_type_vector
[cplus_primitive_type_short
]
1305 = builtin
->builtin_short
;
1306 lai
->primitive_type_vector
[cplus_primitive_type_char
]
1307 = builtin
->builtin_char
;
1308 lai
->primitive_type_vector
[cplus_primitive_type_float
]
1309 = builtin
->builtin_float
;
1310 lai
->primitive_type_vector
[cplus_primitive_type_double
]
1311 = builtin
->builtin_double
;
1312 lai
->primitive_type_vector
[cplus_primitive_type_void
]
1313 = builtin
->builtin_void
;
1314 lai
->primitive_type_vector
[cplus_primitive_type_long_long
]
1315 = builtin
->builtin_long_long
;
1316 lai
->primitive_type_vector
[cplus_primitive_type_signed_char
]
1317 = builtin
->builtin_signed_char
;
1318 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_char
]
1319 = builtin
->builtin_unsigned_char
;
1320 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_short
]
1321 = builtin
->builtin_unsigned_short
;
1322 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_int
]
1323 = builtin
->builtin_unsigned_int
;
1324 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_long
]
1325 = builtin
->builtin_unsigned_long
;
1326 lai
->primitive_type_vector
[cplus_primitive_type_unsigned_long_long
]
1327 = builtin
->builtin_unsigned_long_long
;
1328 lai
->primitive_type_vector
[cplus_primitive_type_long_double
]
1329 = builtin
->builtin_long_double
;
1330 lai
->primitive_type_vector
[cplus_primitive_type_complex
]
1331 = builtin
->builtin_complex
;
1332 lai
->primitive_type_vector
[cplus_primitive_type_double_complex
]
1333 = builtin
->builtin_double_complex
;
1334 lai
->primitive_type_vector
[cplus_primitive_type_bool
]
1335 = builtin
->builtin_bool
;
1336 lai
->primitive_type_vector
[cplus_primitive_type_decfloat
]
1337 = builtin
->builtin_decfloat
;
1338 lai
->primitive_type_vector
[cplus_primitive_type_decdouble
]
1339 = builtin
->builtin_decdouble
;
1340 lai
->primitive_type_vector
[cplus_primitive_type_declong
]
1341 = builtin
->builtin_declong
;
1343 lai
->bool_type_symbol
= "bool";
1344 lai
->bool_type_default
= builtin
->builtin_bool
;
1347 const struct language_defn cplus_language_defn
=
1349 "c++", /* Language name */
1360 c_printchar
, /* Print a character constant */
1361 c_printstr
, /* Function to print string constant */
1362 c_emit_char
, /* Print a single char */
1363 c_print_type
, /* Print a type using appropriate syntax */
1364 c_print_typedef
, /* Print a typedef using appropriate syntax */
1365 c_val_print
, /* Print a value using appropriate syntax */
1366 c_value_print
, /* Print a top-level value */
1367 cplus_skip_trampoline
, /* Language specific skip_trampoline */
1368 "this", /* name_of_this */
1369 cp_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1370 cp_lookup_transparent_type
, /* lookup_transparent_type */
1371 cplus_demangle
, /* Language specific symbol demangler */
1372 cp_class_name_from_physname
, /* Language specific
1373 class_name_from_physname */
1374 c_op_print_tab
, /* expression operators for printing */
1375 1, /* c-style arrays */
1376 0, /* String lower bound */
1377 default_word_break_characters
,
1378 default_make_symbol_completion_list
,
1379 cplus_language_arch_info
,
1380 default_print_array_index
,
1381 cp_pass_by_reference
,
1386 const struct language_defn asm_language_defn
=
1388 "asm", /* Language name */
1399 c_printchar
, /* Print a character constant */
1400 c_printstr
, /* Function to print string constant */
1401 c_emit_char
, /* Print a single char */
1402 c_print_type
, /* Print a type using appropriate syntax */
1403 c_print_typedef
, /* Print a typedef using appropriate syntax */
1404 c_val_print
, /* Print a value using appropriate syntax */
1405 c_value_print
, /* Print a top-level value */
1406 NULL
, /* Language specific skip_trampoline */
1407 NULL
, /* name_of_this */
1408 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1409 basic_lookup_transparent_type
,/* lookup_transparent_type */
1410 NULL
, /* Language specific symbol demangler */
1411 NULL
, /* Language specific
1412 class_name_from_physname */
1413 c_op_print_tab
, /* expression operators for printing */
1414 1, /* c-style arrays */
1415 0, /* String lower bound */
1416 default_word_break_characters
,
1417 default_make_symbol_completion_list
,
1418 c_language_arch_info
, /* FIXME: la_language_arch_info. */
1419 default_print_array_index
,
1420 default_pass_by_reference
,
1425 /* The following language_defn does not represent a real language.
1426 It just provides a minimal support a-la-C that should allow users
1427 to do some simple operations when debugging applications that use
1428 a language currently not supported by GDB. */
1430 const struct language_defn minimal_language_defn
=
1432 "minimal", /* Language name */
1443 c_printchar
, /* Print a character constant */
1444 c_printstr
, /* Function to print string constant */
1445 c_emit_char
, /* Print a single char */
1446 c_print_type
, /* Print a type using appropriate syntax */
1447 c_print_typedef
, /* Print a typedef using appropriate syntax */
1448 c_val_print
, /* Print a value using appropriate syntax */
1449 c_value_print
, /* Print a top-level value */
1450 NULL
, /* Language specific skip_trampoline */
1451 NULL
, /* name_of_this */
1452 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
1453 basic_lookup_transparent_type
,/* lookup_transparent_type */
1454 NULL
, /* Language specific symbol demangler */
1455 NULL
, /* Language specific
1456 class_name_from_physname */
1457 c_op_print_tab
, /* expression operators for printing */
1458 1, /* c-style arrays */
1459 0, /* String lower bound */
1460 default_word_break_characters
,
1461 default_make_symbol_completion_list
,
1462 c_language_arch_info
,
1463 default_print_array_index
,
1464 default_pass_by_reference
,
1470 _initialize_c_language (void)
1472 add_language (&c_language_defn
);
1473 add_language (&cplus_language_defn
);
1474 add_language (&asm_language_defn
);
1475 add_language (&minimal_language_defn
);