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 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 set. */
76 static enum c_string_type
77 classify_type (struct type *elttype, struct gdbarch *gdbarch,
78 const char **encoding)
80 enum c_string_type result;
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. */
89 char *name = TYPE_NAME (elttype);
91 if (TYPE_CODE (elttype) == TYPE_CODE_CHAR || !name)
97 if (!strcmp (name, "wchar_t"))
103 if (!strcmp (name, "char16_t"))
109 if (!strcmp (name, "char32_t"))
115 if (TYPE_CODE (elttype) != TYPE_CODE_TYPEDEF)
118 /* Call for side effects. */
119 check_typedef (elttype);
121 if (TYPE_TARGET_TYPE (elttype))
122 elttype = TYPE_TARGET_TYPE (elttype);
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);
137 *encoding = charset_for_string_type (result, gdbarch);
142 /* Return true if print_wchar can display W without resorting to a
143 numeric escape, false otherwise. */
146 wchar_printable (gdb_wchar_t w)
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'));
155 /* A helper function that converts the contents of STRING to wide
156 characters and then appends them to OUTPUT. */
159 append_string_as_wide (const char *string, struct obstack *output)
161 for (; *string; ++string)
163 gdb_wchar_t w = gdb_btowc (*string);
164 obstack_grow (output, &w, sizeof (gdb_wchar_t));
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. */
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)
182 int need_escape = *need_escapep;
185 if (gdb_iswprint (w) && (!need_escape || (!gdb_iswdigit (w)
187 && w != LCST ('9'))))
189 gdb_wchar_t wchar = w;
191 if (w == gdb_btowc (quoter) || w == LCST ('\\'))
192 obstack_grow_wstr (output, LCST ("\\"));
193 obstack_grow (output, &wchar, sizeof (gdb_wchar_t));
200 obstack_grow_wstr (output, LCST ("\\a"));
203 obstack_grow_wstr (output, LCST ("\\b"));
206 obstack_grow_wstr (output, LCST ("\\f"));
209 obstack_grow_wstr (output, LCST ("\\n"));
212 obstack_grow_wstr (output, LCST ("\\r"));
215 obstack_grow_wstr (output, LCST ("\\t"));
218 obstack_grow_wstr (output, LCST ("\\v"));
224 for (i = 0; i + width <= orig_len; i += width)
229 value = extract_unsigned_integer (&orig[i], width, byte_order);
230 /* If the value fits in 3 octal digits, print it that
231 way. Otherwise, print it as a hex escape. */
233 sprintf (octal, "\\%.3o", (int) (value & 0777));
235 sprintf (octal, "\\x%lx", (long) value);
236 append_string_as_wide (octal, output);
238 /* If we somehow have extra bytes, print them now. */
243 sprintf (octal, "\\%.3o", orig[i] & 0xff);
244 append_string_as_wide (octal, output);
255 /* Print the character C on STREAM as part of the contents of a literal
256 string whose delimiter is QUOTER. Note that that format for printing
257 characters and strings is language specific. */
260 c_emit_char (int c, struct type *type,
261 struct ui_file *stream, int quoter)
263 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
264 struct obstack wchar_buf, output;
265 struct cleanup *cleanups;
266 const char *encoding;
268 struct wchar_iterator *iter;
271 classify_type (type, get_type_arch (type), &encoding);
273 buf = alloca (TYPE_LENGTH (type));
274 pack_long (buf, type, c);
276 iter = make_wchar_iterator (buf, TYPE_LENGTH (type), encoding,
278 cleanups = make_cleanup_wchar_iterator (iter);
280 /* This holds the printable form of the wchar_t data. */
281 obstack_init (&wchar_buf);
282 make_cleanup_obstack_free (&wchar_buf);
290 int print_escape = 1;
291 enum wchar_iterate_result result;
293 num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
298 /* If all characters are printable, print them. Otherwise,
299 we're going to have to print an escape sequence. We
300 check all characters because we want to print the target
301 bytes in the escape sequence, and we don't know character
306 for (i = 0; i < num_chars; ++i)
307 if (!wchar_printable (chars[i]))
315 for (i = 0; i < num_chars; ++i)
316 print_wchar (chars[i], buf, buflen, TYPE_LENGTH (type),
317 byte_order, &wchar_buf, quoter, &need_escape);
321 /* This handles the NUM_CHARS == 0 case as well. */
323 print_wchar (gdb_WEOF, buf, buflen, TYPE_LENGTH (type), byte_order,
324 &wchar_buf, quoter, &need_escape);
327 /* The output in the host encoding. */
328 obstack_init (&output);
329 make_cleanup_obstack_free (&output);
331 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
332 obstack_base (&wchar_buf),
333 obstack_object_size (&wchar_buf),
334 1, &output, translit_char);
335 obstack_1grow (&output, '\0');
337 fputs_filtered (obstack_base (&output), stream);
339 do_cleanups (cleanups);
343 c_printchar (int c, struct type *type, struct ui_file *stream)
345 enum c_string_type str_type;
347 str_type = classify_type (type, get_type_arch (type), NULL);
353 fputc_filtered ('L', stream);
356 fputc_filtered ('u', stream);
359 fputc_filtered ('U', stream);
363 fputc_filtered ('\'', stream);
364 LA_EMIT_CHAR (c, type, stream, '\'');
365 fputc_filtered ('\'', stream);
368 /* Print the character string STRING, printing at most LENGTH characters.
369 LENGTH is -1 if the string is nul terminated. Each character is WIDTH bytes
370 long. Printing stops early if the number hits print_max; repeat counts are
371 printed as appropriate. Print ellipses at the end if we had to stop before
372 printing LENGTH characters, or if FORCE_ELLIPSES. */
375 c_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
376 unsigned int length, const char *user_encoding, int force_ellipses,
377 const struct value_print_options *options)
379 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
381 unsigned int things_printed = 0;
384 int width = TYPE_LENGTH (type);
385 struct obstack wchar_buf, output;
386 struct cleanup *cleanup;
387 enum c_string_type str_type;
388 const char *type_encoding;
389 const char *encoding;
390 struct wchar_iterator *iter;
396 unsigned long current_char = 1;
398 for (i = 0; current_char; ++i)
401 current_char = extract_unsigned_integer (string + i * width,
407 /* If the string was not truncated due to `set print elements', and
408 the last byte of it is a null, we don't print that, in traditional C
412 && (extract_unsigned_integer (string + (length - 1) * width,
413 width, byte_order) == 0))
416 str_type = (classify_type (type, get_type_arch (type), &type_encoding)
423 fputs_filtered ("L", stream);
426 fputs_filtered ("u", stream);
429 fputs_filtered ("U", stream);
433 encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;
437 fputs_filtered ("\"\"", stream);
441 /* Arrange to iterate over the characters, in wchar_t form. */
442 iter = make_wchar_iterator (string, length * width, encoding, width);
443 cleanup = make_cleanup_wchar_iterator (iter);
445 /* WCHAR_BUF is the obstack we use to represent the string in
447 obstack_init (&wchar_buf);
448 make_cleanup_obstack_free (&wchar_buf);
450 while (!finished && things_printed < options->print_max)
453 enum wchar_iterate_result result;
462 obstack_grow_wstr (&wchar_buf, LCST (", "));
466 num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
467 /* We only look at repetitions when we were able to convert a
468 single character in isolation. This makes the code simpler
469 and probably does the sensible thing in the majority of
471 while (num_chars == 1 && things_printed < options->print_max)
473 /* Count the number of repetitions. */
474 unsigned int reps = 0;
475 gdb_wchar_t current_char = chars[0];
476 const gdb_byte *orig_buf = buf;
477 int orig_len = buflen;
481 obstack_grow_wstr (&wchar_buf, LCST (", "));
485 while (num_chars == 1 && current_char == chars[0])
487 num_chars = wchar_iterate (iter, &result, &chars, &buf, &buflen);
491 /* Emit CURRENT_CHAR according to the repetition count and
493 if (reps > options->repeat_count_threshold)
497 if (options->inspect_it)
498 obstack_grow_wstr (&wchar_buf, LCST ("\\\", "));
500 obstack_grow_wstr (&wchar_buf, LCST ("\", "));
503 obstack_grow_wstr (&wchar_buf, LCST ("'"));
505 print_wchar (current_char, orig_buf, orig_len, width,
506 byte_order, &wchar_buf, '\'', &need_escape);
507 obstack_grow_wstr (&wchar_buf, LCST ("'"));
509 /* Painful gyrations. */
511 char *s = xstrprintf (_(" <repeats %u times>"), reps);
513 for (j = 0; s[j]; ++j)
515 gdb_wchar_t w = gdb_btowc (s[j]);
516 obstack_grow (&wchar_buf, &w, sizeof (gdb_wchar_t));
520 things_printed += options->repeat_count_threshold;
525 /* Saw the character one or more times, but fewer than
526 the repetition threshold. */
529 if (options->inspect_it)
530 obstack_grow_wstr (&wchar_buf, LCST ("\\\""));
532 obstack_grow_wstr (&wchar_buf, LCST ("\""));
539 print_wchar (current_char, orig_buf, orig_len, width,
540 byte_order, &wchar_buf, '"', &need_escape);
546 /* NUM_CHARS and the other outputs from wchar_iterate are valid
547 here regardless of which branch was taken above. */
557 case wchar_iterate_invalid:
560 if (options->inspect_it)
561 obstack_grow_wstr (&wchar_buf, LCST ("\\\""));
563 obstack_grow_wstr (&wchar_buf, LCST ("\""));
567 print_wchar (gdb_WEOF, buf, buflen, width, byte_order, &wchar_buf,
571 case wchar_iterate_incomplete:
574 if (options->inspect_it)
575 obstack_grow_wstr (&wchar_buf, LCST ("\\\","));
577 obstack_grow_wstr (&wchar_buf, LCST ("\","));
580 obstack_grow_wstr (&wchar_buf, LCST (" <incomplete sequence "));
581 print_wchar (gdb_WEOF, buf, buflen, width, byte_order, &wchar_buf,
583 obstack_grow_wstr (&wchar_buf, LCST (">"));
589 /* Terminate the quotes if necessary. */
592 if (options->inspect_it)
593 obstack_grow_wstr (&wchar_buf, LCST ("\\\""));
595 obstack_grow_wstr (&wchar_buf, LCST ("\""));
598 if (force_ellipses || !finished)
599 obstack_grow_wstr (&wchar_buf, LCST ("..."));
601 /* OUTPUT is where we collect `char's for printing. */
602 obstack_init (&output);
603 make_cleanup_obstack_free (&output);
605 convert_between_encodings (INTERMEDIATE_ENCODING, host_charset (),
606 obstack_base (&wchar_buf),
607 obstack_object_size (&wchar_buf),
608 1, &output, translit_char);
609 obstack_1grow (&output, '\0');
611 fputs_filtered (obstack_base (&output), stream);
613 do_cleanups (cleanup);
616 /* Obtain a C string from the inferior storing it in a newly allocated
617 buffer in BUFFER, which should be freed by the caller. If the
618 in- and out-parameter *LENGTH is specified at -1, the string is read
619 until a null character of the appropriate width is found, otherwise
620 the string is read to the length of characters specified.
621 The size of a character is determined by the length of the target
622 type of the pointer or array. If VALUE is an array with a known
623 length, the function will not read past the end of the array.
624 On completion, *LENGTH will be set to the size of the string read in
625 characters. (If a length of -1 is specified, the length returned
626 will not include the null character). CHARSET is always set to the
630 c_get_string (struct value *value, gdb_byte **buffer, int *length,
631 struct type **char_type, const char **charset)
634 unsigned int fetchlimit;
635 struct type *type = check_typedef (value_type (value));
636 struct type *element_type = TYPE_TARGET_TYPE (type);
637 int req_length = *length;
638 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
639 enum c_string_type kind;
641 if (element_type == NULL)
644 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
646 /* If we know the size of the array, we can use it as a limit on the
647 number of characters to be fetched. */
648 if (TYPE_NFIELDS (type) == 1
649 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_RANGE)
651 LONGEST low_bound, high_bound;
653 get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
654 &low_bound, &high_bound);
655 fetchlimit = high_bound - low_bound + 1;
658 fetchlimit = UINT_MAX;
660 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
661 fetchlimit = UINT_MAX;
663 /* We work only with arrays and pointers. */
666 if (! c_textual_element_type (element_type, 0))
668 kind = classify_type (element_type,
669 get_type_arch (element_type),
671 width = TYPE_LENGTH (element_type);
673 /* If the string lives in GDB's memory instead of the inferior's, then we
674 just need to copy it to BUFFER. Also, since such strings are arrays
675 with known size, FETCHLIMIT will hold the size of the array. */
676 if ((VALUE_LVAL (value) == not_lval
677 || VALUE_LVAL (value) == lval_internalvar)
678 && fetchlimit != UINT_MAX)
681 const gdb_byte *contents = value_contents (value);
683 /* If a length is specified, use that. */
687 /* Otherwise, look for a null character. */
688 for (i = 0; i < fetchlimit; i++)
689 if (extract_unsigned_integer (contents + i * width, width,
693 /* I is now either a user-defined length, the number of non-null
694 characters, or FETCHLIMIT. */
696 *buffer = xmalloc (*length);
697 memcpy (*buffer, contents, *length);
702 CORE_ADDR addr = value_as_address (value);
704 err = read_string (addr, *length, width, fetchlimit,
705 byte_order, buffer, length);
710 throw_error (MEMORY_ERROR, "Address %s out of bounds",
711 paddress (get_type_arch (type), addr));
713 error (_("Error reading string from inferior: %s"),
714 safe_strerror (err));
718 /* If the LENGTH is specified at -1, we want to return the string
719 length up to the terminating null character. If an actual length
720 was specified, we want to return the length of exactly what was
722 if (req_length == -1)
723 /* If the last character is null, subtract it from LENGTH. */
725 && extract_unsigned_integer (*buffer + *length - width, width,
729 /* The read_string function will return the number of bytes read.
730 If length returned from read_string was > 0, return the number of
731 characters read by dividing the number of bytes by width. */
733 *length = *length / width;
735 *char_type = element_type;
743 type_str = type_to_string (type);
746 make_cleanup (xfree, type_str);
747 error (_("Trying to read string with inappropriate type `%s'."),
751 error (_("Trying to read string with inappropriate type."));
756 /* Evaluating C and C++ expressions. */
758 /* Convert a UCN. The digits of the UCN start at P and extend no
759 farther than LIMIT. DEST_CHARSET is the name of the character set
760 into which the UCN should be converted. The results are written to
761 OUTPUT. LENGTH is the maximum length of the UCN, either 4 or 8.
762 Returns a pointer to just after the final digit of the UCN. */
765 convert_ucn (char *p, char *limit, const char *dest_charset,
766 struct obstack *output, int length)
768 unsigned long result = 0;
772 for (i = 0; i < length && p < limit && isxdigit (*p); ++i, ++p)
773 result = (result << 4) + host_hex_value (*p);
775 for (i = 3; i >= 0; --i)
777 data[i] = result & 0xff;
781 convert_between_encodings ("UTF-32BE", dest_charset, data, 4, 4, output,
787 /* Emit a character, VALUE, which was specified numerically, to
788 OUTPUT. TYPE is the target character type. */
791 emit_numeric_character (struct type *type, unsigned long value,
792 struct obstack *output)
796 buffer = alloca (TYPE_LENGTH (type));
797 pack_long (buffer, type, value);
798 obstack_grow (output, buffer, TYPE_LENGTH (type));
801 /* Convert an octal escape sequence. TYPE is the target character
802 type. The digits of the escape sequence begin at P and extend no
803 farther than LIMIT. The result is written to OUTPUT. Returns a
804 pointer to just after the final digit of the escape sequence. */
807 convert_octal (struct type *type, char *p, char *limit, struct obstack *output)
810 unsigned long value = 0;
813 i < 3 && p < limit && isdigit (*p) && *p != '8' && *p != '9';
816 value = 8 * value + host_hex_value (*p);
820 emit_numeric_character (type, value, output);
825 /* Convert a hex escape sequence. TYPE is the target character type.
826 The digits of the escape sequence begin at P and extend no farther
827 than LIMIT. The result is written to OUTPUT. Returns a pointer to
828 just after the final digit of the escape sequence. */
831 convert_hex (struct type *type, char *p, char *limit, struct obstack *output)
833 unsigned long value = 0;
835 while (p < limit && isxdigit (*p))
837 value = 16 * value + host_hex_value (*p);
841 emit_numeric_character (type, value, output);
850 error (_("Malformed escape sequence")); \
853 /* Convert an escape sequence to a target format. TYPE is the target
854 character type to use, and DEST_CHARSET is the name of the target
855 character set. The backslash of the escape sequence is at *P, and
856 the escape sequence will not extend past LIMIT. The results are
857 written to OUTPUT. Returns a pointer to just past the final
858 character of the escape sequence. */
861 convert_escape (struct type *type, const char *dest_charset,
862 char *p, char *limit, struct obstack *output)
864 /* Skip the backslash. */
870 obstack_1grow (output, '\\');
877 error (_("\\x used with no following hex digits."));
878 p = convert_hex (type, p, limit, output);
889 p = convert_octal (type, p, limit, output);
895 int length = *p == 'u' ? 4 : 8;
899 error (_("\\u used with no following hex digits"));
900 p = convert_ucn (p, limit, dest_charset, output, length);
907 /* Given a single string from a (C-specific) OP_STRING list, convert
908 it to a target string, handling escape sequences specially. The
909 output is written to OUTPUT. DATA is the input string, which has
910 length LEN. DEST_CHARSET is the name of the target character set,
911 and TYPE is the type of target character to use. */
914 parse_one_string (struct obstack *output, char *data, int len,
915 const char *dest_charset, struct type *type)
925 /* Look for next escape, or the end of the input. */
926 while (p < limit && *p != '\\')
928 /* If we saw a run of characters, convert them all. */
930 convert_between_encodings (host_charset (), dest_charset,
931 data, p - data, 1, output, translit_none);
932 /* If we saw an escape, convert it. */
934 p = convert_escape (type, dest_charset, p, limit, output);
939 /* Expression evaluator for the C language family. Most operations
940 are delegated to evaluate_subexp_standard; see that function for a
941 description of the arguments. */
944 evaluate_subexp_c (struct type *expect_type, struct expression *exp,
945 int *pos, enum noside noside)
947 enum exp_opcode op = exp->elts[*pos].opcode;
955 struct obstack output;
956 struct cleanup *cleanup;
957 struct value *result;
958 enum c_string_type dest_type;
959 const char *dest_charset;
961 obstack_init (&output);
962 cleanup = make_cleanup_obstack_free (&output);
965 oplen = longest_to_int (exp->elts[*pos].longconst);
968 limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1);
970 = (enum c_string_type) longest_to_int (exp->elts[*pos].longconst);
971 switch (dest_type & ~C_CHAR)
974 type = language_string_char_type (exp->language_defn,
978 type = lookup_typename (exp->language_defn, exp->gdbarch,
982 type = lookup_typename (exp->language_defn, exp->gdbarch,
983 "char16_t", NULL, 0);
986 type = lookup_typename (exp->language_defn, exp->gdbarch,
987 "char32_t", NULL, 0);
990 internal_error (__FILE__, __LINE__, "unhandled c_string_type");
993 /* Ensure TYPE_LENGTH is valid for TYPE. */
994 check_typedef (type);
996 dest_charset = charset_for_string_type (dest_type, exp->gdbarch);
1003 len = longest_to_int (exp->elts[*pos].longconst);
1006 if (noside != EVAL_SKIP)
1007 parse_one_string (&output, &exp->elts[*pos].string, len,
1008 dest_charset, type);
1009 *pos += BYTES_TO_EXP_ELEM (len);
1012 /* Skip the trailing length and opcode. */
1015 if (noside == EVAL_SKIP)
1017 /* Return a dummy value of the appropriate type. */
1018 if ((dest_type & C_CHAR) != 0)
1019 result = allocate_value (type);
1021 result = value_cstring ("", 0, type);
1022 do_cleanups (cleanup);
1026 if ((dest_type & C_CHAR) != 0)
1030 if (obstack_object_size (&output) != TYPE_LENGTH (type))
1031 error (_("Could not convert character constant to target character set"));
1032 value = unpack_long (type, obstack_base (&output));
1033 result = value_from_longest (type, value);
1039 /* Write the terminating character. */
1040 for (i = 0; i < TYPE_LENGTH (type); ++i)
1041 obstack_1grow (&output, 0);
1042 result = value_cstring (obstack_base (&output),
1043 obstack_object_size (&output),
1046 do_cleanups (cleanup);
1054 return evaluate_subexp_standard (expect_type, exp, pos, noside);
1059 /* Table mapping opcodes into strings for printing operators
1060 and precedences of the operators. */
1062 const struct op_print c_op_print_tab[] =
1064 {",", BINOP_COMMA, PREC_COMMA, 0},
1065 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1066 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1067 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1068 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1069 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1070 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1071 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1072 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1073 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1074 {">=", BINOP_GEQ, PREC_ORDER, 0},
1075 {">", BINOP_GTR, PREC_ORDER, 0},
1076 {"<", BINOP_LESS, PREC_ORDER, 0},
1077 {">>", BINOP_RSH, PREC_SHIFT, 0},
1078 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1079 {"+", BINOP_ADD, PREC_ADD, 0},
1080 {"-", BINOP_SUB, PREC_ADD, 0},
1081 {"*", BINOP_MUL, PREC_MUL, 0},
1082 {"/", BINOP_DIV, PREC_MUL, 0},
1083 {"%", BINOP_REM, PREC_MUL, 0},
1084 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1085 {"-", UNOP_NEG, PREC_PREFIX, 0},
1086 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1087 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1088 {"*", UNOP_IND, PREC_PREFIX, 0},
1089 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1090 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1091 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1092 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1096 enum c_primitive_types {
1097 c_primitive_type_int,
1098 c_primitive_type_long,
1099 c_primitive_type_short,
1100 c_primitive_type_char,
1101 c_primitive_type_float,
1102 c_primitive_type_double,
1103 c_primitive_type_void,
1104 c_primitive_type_long_long,
1105 c_primitive_type_signed_char,
1106 c_primitive_type_unsigned_char,
1107 c_primitive_type_unsigned_short,
1108 c_primitive_type_unsigned_int,
1109 c_primitive_type_unsigned_long,
1110 c_primitive_type_unsigned_long_long,
1111 c_primitive_type_long_double,
1112 c_primitive_type_complex,
1113 c_primitive_type_double_complex,
1114 c_primitive_type_decfloat,
1115 c_primitive_type_decdouble,
1116 c_primitive_type_declong,
1117 nr_c_primitive_types
1121 c_language_arch_info (struct gdbarch *gdbarch,
1122 struct language_arch_info *lai)
1124 const struct builtin_type *builtin = builtin_type (gdbarch);
1126 lai->string_char_type = builtin->builtin_char;
1127 lai->primitive_type_vector
1128 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
1130 lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
1131 lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
1132 lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
1133 lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
1134 lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
1135 lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
1136 lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
1137 lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
1138 lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
1139 lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
1140 lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
1141 lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
1142 lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
1143 lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
1144 lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
1145 lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
1146 lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
1147 lai->primitive_type_vector [c_primitive_type_decfloat] = builtin->builtin_decfloat;
1148 lai->primitive_type_vector [c_primitive_type_decdouble] = builtin->builtin_decdouble;
1149 lai->primitive_type_vector [c_primitive_type_declong] = builtin->builtin_declong;
1151 lai->bool_type_default = builtin->builtin_int;
1154 const struct exp_descriptor exp_descriptor_c =
1156 print_subexp_standard,
1157 operator_length_standard,
1158 operator_check_standard,
1160 dump_subexp_body_standard,
1164 const struct language_defn c_language_defn =
1166 "c", /* Language name */
1177 c_printchar, /* Print a character constant */
1178 c_printstr, /* Function to print string constant */
1179 c_emit_char, /* Print a single char */
1180 c_print_type, /* Print a type using appropriate syntax */
1181 c_print_typedef, /* Print a typedef using appropriate syntax */
1182 c_val_print, /* Print a value using appropriate syntax */
1183 c_value_print, /* Print a top-level value */
1184 NULL, /* Language specific skip_trampoline */
1185 NULL, /* name_of_this */
1186 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1187 basic_lookup_transparent_type,/* lookup_transparent_type */
1188 NULL, /* Language specific symbol demangler */
1189 NULL, /* Language specific class_name_from_physname */
1190 c_op_print_tab, /* expression operators for printing */
1191 1, /* c-style arrays */
1192 0, /* String lower bound */
1193 default_word_break_characters,
1194 default_make_symbol_completion_list,
1195 c_language_arch_info,
1196 default_print_array_index,
1197 default_pass_by_reference,
1202 enum cplus_primitive_types {
1203 cplus_primitive_type_int,
1204 cplus_primitive_type_long,
1205 cplus_primitive_type_short,
1206 cplus_primitive_type_char,
1207 cplus_primitive_type_float,
1208 cplus_primitive_type_double,
1209 cplus_primitive_type_void,
1210 cplus_primitive_type_long_long,
1211 cplus_primitive_type_signed_char,
1212 cplus_primitive_type_unsigned_char,
1213 cplus_primitive_type_unsigned_short,
1214 cplus_primitive_type_unsigned_int,
1215 cplus_primitive_type_unsigned_long,
1216 cplus_primitive_type_unsigned_long_long,
1217 cplus_primitive_type_long_double,
1218 cplus_primitive_type_complex,
1219 cplus_primitive_type_double_complex,
1220 cplus_primitive_type_bool,
1221 cplus_primitive_type_decfloat,
1222 cplus_primitive_type_decdouble,
1223 cplus_primitive_type_declong,
1224 nr_cplus_primitive_types
1228 cplus_language_arch_info (struct gdbarch *gdbarch,
1229 struct language_arch_info *lai)
1231 const struct builtin_type *builtin = builtin_type (gdbarch);
1233 lai->string_char_type = builtin->builtin_char;
1234 lai->primitive_type_vector
1235 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
1237 lai->primitive_type_vector [cplus_primitive_type_int]
1238 = builtin->builtin_int;
1239 lai->primitive_type_vector [cplus_primitive_type_long]
1240 = builtin->builtin_long;
1241 lai->primitive_type_vector [cplus_primitive_type_short]
1242 = builtin->builtin_short;
1243 lai->primitive_type_vector [cplus_primitive_type_char]
1244 = builtin->builtin_char;
1245 lai->primitive_type_vector [cplus_primitive_type_float]
1246 = builtin->builtin_float;
1247 lai->primitive_type_vector [cplus_primitive_type_double]
1248 = builtin->builtin_double;
1249 lai->primitive_type_vector [cplus_primitive_type_void]
1250 = builtin->builtin_void;
1251 lai->primitive_type_vector [cplus_primitive_type_long_long]
1252 = builtin->builtin_long_long;
1253 lai->primitive_type_vector [cplus_primitive_type_signed_char]
1254 = builtin->builtin_signed_char;
1255 lai->primitive_type_vector [cplus_primitive_type_unsigned_char]
1256 = builtin->builtin_unsigned_char;
1257 lai->primitive_type_vector [cplus_primitive_type_unsigned_short]
1258 = builtin->builtin_unsigned_short;
1259 lai->primitive_type_vector [cplus_primitive_type_unsigned_int]
1260 = builtin->builtin_unsigned_int;
1261 lai->primitive_type_vector [cplus_primitive_type_unsigned_long]
1262 = builtin->builtin_unsigned_long;
1263 lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long]
1264 = builtin->builtin_unsigned_long_long;
1265 lai->primitive_type_vector [cplus_primitive_type_long_double]
1266 = builtin->builtin_long_double;
1267 lai->primitive_type_vector [cplus_primitive_type_complex]
1268 = builtin->builtin_complex;
1269 lai->primitive_type_vector [cplus_primitive_type_double_complex]
1270 = builtin->builtin_double_complex;
1271 lai->primitive_type_vector [cplus_primitive_type_bool]
1272 = builtin->builtin_bool;
1273 lai->primitive_type_vector [cplus_primitive_type_decfloat]
1274 = builtin->builtin_decfloat;
1275 lai->primitive_type_vector [cplus_primitive_type_decdouble]
1276 = builtin->builtin_decdouble;
1277 lai->primitive_type_vector [cplus_primitive_type_declong]
1278 = builtin->builtin_declong;
1280 lai->bool_type_symbol = "bool";
1281 lai->bool_type_default = builtin->builtin_bool;
1284 const struct language_defn cplus_language_defn =
1286 "c++", /* Language name */
1297 c_printchar, /* Print a character constant */
1298 c_printstr, /* Function to print string constant */
1299 c_emit_char, /* Print a single char */
1300 c_print_type, /* Print a type using appropriate syntax */
1301 c_print_typedef, /* Print a typedef using appropriate syntax */
1302 c_val_print, /* Print a value using appropriate syntax */
1303 c_value_print, /* Print a top-level value */
1304 cplus_skip_trampoline, /* Language specific skip_trampoline */
1305 "this", /* name_of_this */
1306 cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1307 cp_lookup_transparent_type, /* lookup_transparent_type */
1308 cplus_demangle, /* Language specific symbol demangler */
1309 cp_class_name_from_physname, /* Language specific class_name_from_physname */
1310 c_op_print_tab, /* expression operators for printing */
1311 1, /* c-style arrays */
1312 0, /* String lower bound */
1313 default_word_break_characters,
1314 default_make_symbol_completion_list,
1315 cplus_language_arch_info,
1316 default_print_array_index,
1317 cp_pass_by_reference,
1322 const struct language_defn asm_language_defn =
1324 "asm", /* Language name */
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, /* FIXME: la_language_arch_info. */
1354 default_print_array_index,
1355 default_pass_by_reference,
1360 /* The following language_defn does not represent a real language.
1361 It just provides a minimal support a-la-C that should allow users
1362 to do some simple operations when debugging applications that use
1363 a language currently not supported by GDB. */
1365 const struct language_defn minimal_language_defn =
1367 "minimal", /* Language name */
1378 c_printchar, /* Print a character constant */
1379 c_printstr, /* Function to print string constant */
1380 c_emit_char, /* Print a single char */
1381 c_print_type, /* Print a type using appropriate syntax */
1382 c_print_typedef, /* Print a typedef using appropriate syntax */
1383 c_val_print, /* Print a value using appropriate syntax */
1384 c_value_print, /* Print a top-level value */
1385 NULL, /* Language specific skip_trampoline */
1386 NULL, /* name_of_this */
1387 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
1388 basic_lookup_transparent_type,/* lookup_transparent_type */
1389 NULL, /* Language specific symbol demangler */
1390 NULL, /* Language specific class_name_from_physname */
1391 c_op_print_tab, /* expression operators for printing */
1392 1, /* c-style arrays */
1393 0, /* String lower bound */
1394 default_word_break_characters,
1395 default_make_symbol_completion_list,
1396 c_language_arch_info,
1397 default_print_array_index,
1398 default_pass_by_reference,
1404 _initialize_c_language (void)
1406 add_language (&c_language_defn);
1407 add_language (&cplus_language_defn);
1408 add_language (&asm_language_defn);
1409 add_language (&minimal_language_defn);