gdb/c-lang.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* C language support routines for GDB, the GNU debugger.

  2.    Copyright (C) 1992-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "parser-defs.h"
  19. #include "language.h"
  20. #include "varobj.h"
  21. #include "c-lang.h"
  22. #include "valprint.h"
  23. #include "macroscope.h"
  24. #include "charset.h"
  25. #include "demangle.h"
  26. #include "cp-abi.h"
  27. #include "cp-support.h"
  28. #include "gdb_obstack.h"
  29. #include <ctype.h>
  30. #include "gdbcore.h"

  31. extern void _initialize_c_language (void);

  32. /* Given a C string type, STR_TYPE, return the corresponding target
  33.    character set name.  */

  34. static const char *
  35. charset_for_string_type (enum c_string_type str_type,
  36.                          struct gdbarch *gdbarch)
  37. {
  38.   switch (str_type & ~C_CHAR)
  39.     {
  40.     case C_STRING:
  41.       return target_charset (gdbarch);
  42.     case C_WIDE_STRING:
  43.       return target_wide_charset (gdbarch);
  44.     case C_STRING_16:
  45.       /* FIXME: UTF-16 is not always correct.  */
  46.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  47.         return "UTF-16BE";
  48.       else
  49.         return "UTF-16LE";
  50.     case C_STRING_32:
  51.       /* FIXME: UTF-32 is not always correct.  */
  52.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  53.         return "UTF-32BE";
  54.       else
  55.         return "UTF-32LE";
  56.     }
  57.   internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
  58. }

  59. /* Classify ELTTYPE according to what kind of character it is.  Return
  60.    the enum constant representing the character type.  Also set
  61.    *ENCODING to the name of the character set to use when converting
  62.    characters of this type in target BYTE_ORDER to the host character
  63.    set.  */

  64. static enum c_string_type
  65. classify_type (struct type *elttype, struct gdbarch *gdbarch,
  66.                const char **encoding)
  67. {
  68.   enum c_string_type result;

  69.   /* We loop because ELTTYPE may be a typedef, and we want to
  70.      successively peel each typedef until we reach a type we
  71.      understand.  We don't use CHECK_TYPEDEF because that will strip
  72.      all typedefs at once -- but in C, wchar_t is itself a typedef, so
  73.      that would do the wrong thing.  */
  74.   while (elttype)
  75.     {
  76.       const char *name = TYPE_NAME (elttype);

  77.       if (TYPE_CODE (elttype) == TYPE_CODE_CHAR || !name)
  78.         {
  79.           result = C_CHAR;
  80.           goto done;
  81.         }

  82.       if (!strcmp (name, "wchar_t"))
  83.         {
  84.           result = C_WIDE_CHAR;
  85.           goto done;
  86.         }

  87.       if (!strcmp (name, "char16_t"))
  88.         {
  89.           result = C_CHAR_16;
  90.           goto done;
  91.         }

  92.       if (!strcmp (name, "char32_t"))
  93.         {
  94.           result = C_CHAR_32;
  95.           goto done;
  96.         }

  97.       if (TYPE_CODE (elttype) != TYPE_CODE_TYPEDEF)
  98.         break;

  99.       /* Call for side effects.  */
  100.       check_typedef (elttype);

  101.       if (TYPE_TARGET_TYPE (elttype))
  102.         elttype = TYPE_TARGET_TYPE (elttype);
  103.       else
  104.         {
  105.           /* Perhaps check_typedef did not update the target type.  In
  106.              this case, force the lookup again and hope it works out.
  107.              It never will for C, but it might for C++.  */
  108.           CHECK_TYPEDEF (elttype);
  109.         }
  110.     }

  111.   /* Punt.  */
  112.   result = C_CHAR;

  113. done:
  114.   if (encoding)
  115.     *encoding = charset_for_string_type (result, gdbarch);

  116.   return result;
  117. }

  118. /* Print the character C on STREAM as part of the contents of a
  119.    literal string whose delimiter is QUOTER.  Note that that format
  120.    for printing characters and strings is language specific.  */

  121. void
  122. c_emit_char (int c, struct type *type,
  123.              struct ui_file *stream, int quoter)
  124. {
  125.   const char *encoding;

  126.   classify_type (type, get_type_arch (type), &encoding);
  127.   generic_emit_char (c, type, stream, quoter, encoding);
  128. }

  129. void
  130. c_printchar (int c, struct type *type, struct ui_file *stream)
  131. {
  132.   enum c_string_type str_type;

  133.   str_type = classify_type (type, get_type_arch (type), NULL);
  134.   switch (str_type)
  135.     {
  136.     case C_CHAR:
  137.       break;
  138.     case C_WIDE_CHAR:
  139.       fputc_filtered ('L', stream);
  140.       break;
  141.     case C_CHAR_16:
  142.       fputc_filtered ('u', stream);
  143.       break;
  144.     case C_CHAR_32:
  145.       fputc_filtered ('U', stream);
  146.       break;
  147.     }

  148.   fputc_filtered ('\'', stream);
  149.   LA_EMIT_CHAR (c, type, stream, '\'');
  150.   fputc_filtered ('\'', stream);
  151. }

  152. /* Print the character string STRING, printing at most LENGTH
  153.    characters.  LENGTH is -1 if the string is nul terminated.  Each
  154.    character is WIDTH bytes long.  Printing stops early if the number
  155.    hits print_max; repeat counts are printed as appropriate.  Print
  156.    ellipses at the end if we had to stop before printing LENGTH
  157.    characters, or if FORCE_ELLIPSES.  */

  158. void
  159. c_printstr (struct ui_file *stream, struct type *type,
  160.             const gdb_byte *string, unsigned int length,
  161.             const char *user_encoding, int force_ellipses,
  162.             const struct value_print_options *options)
  163. {
  164.   enum c_string_type str_type;
  165.   const char *type_encoding;
  166.   const char *encoding;

  167.   str_type = (classify_type (type, get_type_arch (type), &type_encoding)
  168.               & ~C_CHAR);
  169.   switch (str_type)
  170.     {
  171.     case C_STRING:
  172.       break;
  173.     case C_WIDE_STRING:
  174.       fputs_filtered ("L", stream);
  175.       break;
  176.     case C_STRING_16:
  177.       fputs_filtered ("u", stream);
  178.       break;
  179.     case C_STRING_32:
  180.       fputs_filtered ("U", stream);
  181.       break;
  182.     }

  183.   encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;

  184.   generic_printstr (stream, type, string, length, encoding, force_ellipses,
  185.                     '"', 1, options);
  186. }

  187. /* Obtain a C string from the inferior storing it in a newly allocated
  188.    buffer in BUFFER, which should be freed by the caller.  If the in-
  189.    and out-parameter *LENGTH is specified at -1, the string is read
  190.    until a null character of the appropriate width is found, otherwise
  191.    the string is read to the length of characters specified.  The size
  192.    of a character is determined by the length of the target type of
  193.    the pointer or array.

  194.    If VALUE is an array with a known length, and *LENGTH is -1,
  195.    the function will not read past the end of the array.  However, any
  196.    declared size of the array is ignored if *LENGTH > 0.

  197.    On completion, *LENGTH will be set to the size of the string read in
  198.    characters.  (If a length of -1 is specified, the length returned
  199.    will not include the null character).  CHARSET is always set to the
  200.    target charset.  */

  201. void
  202. c_get_string (struct value *value, gdb_byte **buffer,
  203.               int *length, struct type **char_type,
  204.               const char **charset)
  205. {
  206.   int err, width;
  207.   unsigned int fetchlimit;
  208.   struct type *type = check_typedef (value_type (value));
  209.   struct type *element_type = TYPE_TARGET_TYPE (type);
  210.   int req_length = *length;
  211.   enum bfd_endian byte_order
  212.     = gdbarch_byte_order (get_type_arch (type));

  213.   if (element_type == NULL)
  214.     goto error;

  215.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
  216.     {
  217.       /* If we know the size of the array, we can use it as a limit on
  218.          the number of characters to be fetched.  */
  219.       if (TYPE_NFIELDS (type) == 1
  220.           && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_RANGE)
  221.         {
  222.           LONGEST low_bound, high_bound;

  223.           get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
  224.                                &low_bound, &high_bound);
  225.           fetchlimit = high_bound - low_bound + 1;
  226.         }
  227.       else
  228.         fetchlimit = UINT_MAX;
  229.     }
  230.   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
  231.     fetchlimit = UINT_MAX;
  232.   else
  233.     /* We work only with arrays and pointers.  */
  234.     goto error;

  235.   if (! c_textual_element_type (element_type, 0))
  236.     goto error;
  237.   classify_type (element_type, get_type_arch (element_type), charset);
  238.   width = TYPE_LENGTH (element_type);

  239.   /* If the string lives in GDB's memory instead of the inferior's,
  240.      then we just need to copy it to BUFFER.  Also, since such strings
  241.      are arrays with known size, FETCHLIMIT will hold the size of the
  242.      array.  */
  243.   if ((VALUE_LVAL (value) == not_lval
  244.        || VALUE_LVAL (value) == lval_internalvar)
  245.       && fetchlimit != UINT_MAX)
  246.     {
  247.       int i;
  248.       const gdb_byte *contents = value_contents (value);

  249.       /* If a length is specified, use that.  */
  250.       if (*length >= 0)
  251.         i  = *length;
  252.       else
  253.          /* Otherwise, look for a null character.  */
  254.          for (i = 0; i < fetchlimit; i++)
  255.           if (extract_unsigned_integer (contents + i * width,
  256.                                         width, byte_order) == 0)
  257.              break;

  258.       /* I is now either a user-defined length, the number of non-null
  259.           characters, or FETCHLIMIT.  */
  260.       *length = i * width;
  261.       *buffer = xmalloc (*length);
  262.       memcpy (*buffer, contents, *length);
  263.       err = 0;
  264.     }
  265.   else
  266.     {
  267.       CORE_ADDR addr = value_as_address (value);

  268.       /* Prior to the fix for PR 16196 read_string would ignore fetchlimit
  269.          if length > 0.  The old "broken" behaviour is the behaviour we want:
  270.          The caller may want to fetch 100 bytes from a variable length array
  271.          implemented using the common idiom of having an array of length 1 at
  272.          the end of a struct.  In this case we want to ignore the declared
  273.          size of the array.  However, it's counterintuitive to implement that
  274.          behaviour in read_string: what does fetchlimit otherwise mean if
  275.          length > 0.  Therefore we implement the behaviour we want here:
  276.          If *length > 0, don't specify a fetchlimit.  This preserves the
  277.          previous behaviour.  We could move this check above where we know
  278.          whether the array is declared with a fixed size, but we only want
  279.          to apply this behaviour when calling read_string.  PR 16286.  */
  280.       if (*length > 0)
  281.         fetchlimit = UINT_MAX;

  282.       err = read_string (addr, *length, width, fetchlimit,
  283.                          byte_order, buffer, length);
  284.       if (err)
  285.         {
  286.           xfree (*buffer);
  287.           memory_error (err, addr);
  288.         }
  289.     }

  290.   /* If the LENGTH is specified at -1, we want to return the string
  291.      length up to the terminating null character.  If an actual length
  292.      was specified, we want to return the length of exactly what was
  293.      read.  */
  294.   if (req_length == -1)
  295.     /* If the last character is null, subtract it from LENGTH.  */
  296.     if (*length > 0
  297.          && extract_unsigned_integer (*buffer + *length - width,
  298.                                      width, byte_order) == 0)
  299.       *length -= width;

  300.   /* The read_string function will return the number of bytes read.
  301.      If length returned from read_string was > 0, return the number of
  302.      characters read by dividing the number of bytes by width.  */
  303.   if (*length != 0)
  304.      *length = *length / width;

  305.   *char_type = element_type;

  306.   return;

  307. error:
  308.   {
  309.     char *type_str;

  310.     type_str = type_to_string (type);
  311.     if (type_str)
  312.       {
  313.         make_cleanup (xfree, type_str);
  314.         error (_("Trying to read string with inappropriate type `%s'."),
  315.                type_str);
  316.       }
  317.     else
  318.       error (_("Trying to read string with inappropriate type."));
  319.   }
  320. }


  321. /* Evaluating C and C++ expressions.  */

  322. /* Convert a UCN.  The digits of the UCN start at P and extend no
  323.    farther than LIMIT.  DEST_CHARSET is the name of the character set
  324.    into which the UCN should be converted.  The results are written to
  325.    OUTPUT.  LENGTH is the maximum length of the UCN, either 4 or 8.
  326.    Returns a pointer to just after the final digit of the UCN.  */

  327. static char *
  328. convert_ucn (char *p, char *limit, const char *dest_charset,
  329.              struct obstack *output, int length)
  330. {
  331.   unsigned long result = 0;
  332.   gdb_byte data[4];
  333.   int i;

  334.   for (i = 0; i < length && p < limit && isxdigit (*p); ++i, ++p)
  335.     result = (result << 4) + host_hex_value (*p);

  336.   for (i = 3; i >= 0; --i)
  337.     {
  338.       data[i] = result & 0xff;
  339.       result >>= 8;
  340.     }

  341.   convert_between_encodings ("UTF-32BE", dest_charset, data,
  342.                              4, 4, output, translit_none);

  343.   return p;
  344. }

  345. /* Emit a character, VALUE, which was specified numerically, to
  346.    OUTPUT.  TYPE is the target character type.  */

  347. static void
  348. emit_numeric_character (struct type *type, unsigned long value,
  349.                         struct obstack *output)
  350. {
  351.   gdb_byte *buffer;

  352.   buffer = alloca (TYPE_LENGTH (type));
  353.   pack_long (buffer, type, value);
  354.   obstack_grow (output, buffer, TYPE_LENGTH (type));
  355. }

  356. /* Convert an octal escape sequence.  TYPE is the target character
  357.    type.  The digits of the escape sequence begin at P and extend no
  358.    farther than LIMIT.  The result is written to OUTPUT.  Returns a
  359.    pointer to just after the final digit of the escape sequence.  */

  360. static char *
  361. convert_octal (struct type *type, char *p,
  362.                char *limit, struct obstack *output)
  363. {
  364.   int i;
  365.   unsigned long value = 0;

  366.   for (i = 0;
  367.        i < 3 && p < limit && isdigit (*p) && *p != '8' && *p != '9';
  368.        ++i)
  369.     {
  370.       value = 8 * value + host_hex_value (*p);
  371.       ++p;
  372.     }

  373.   emit_numeric_character (type, value, output);

  374.   return p;
  375. }

  376. /* Convert a hex escape sequence.  TYPE is the target character type.
  377.    The digits of the escape sequence begin at P and extend no farther
  378.    than LIMIT.  The result is written to OUTPUT.  Returns a pointer to
  379.    just after the final digit of the escape sequence.  */

  380. static char *
  381. convert_hex (struct type *type, char *p,
  382.              char *limit, struct obstack *output)
  383. {
  384.   unsigned long value = 0;

  385.   while (p < limit && isxdigit (*p))
  386.     {
  387.       value = 16 * value + host_hex_value (*p);
  388.       ++p;
  389.     }

  390.   emit_numeric_character (type, value, output);

  391.   return p;
  392. }

  393. #define ADVANCE                                        \
  394.   do {                                                \
  395.     ++p;                                        \
  396.     if (p == limit)                                \
  397.       error (_("Malformed escape sequence"));        \
  398.   } while (0)

  399. /* Convert an escape sequence to a target format.  TYPE is the target
  400.    character type to use, and DEST_CHARSET is the name of the target
  401.    character set.  The backslash of the escape sequence is at *P, and
  402.    the escape sequence will not extend past LIMIT.  The results are
  403.    written to OUTPUT.  Returns a pointer to just past the final
  404.    character of the escape sequence.  */

  405. static char *
  406. convert_escape (struct type *type, const char *dest_charset,
  407.                 char *p, char *limit, struct obstack *output)
  408. {
  409.   /* Skip the backslash.  */
  410.   ADVANCE;

  411.   switch (*p)
  412.     {
  413.     case '\\':
  414.       obstack_1grow (output, '\\');
  415.       ++p;
  416.       break;

  417.     case 'x':
  418.       ADVANCE;
  419.       if (!isxdigit (*p))
  420.         error (_("\\x used with no following hex digits."));
  421.       p = convert_hex (type, p, limit, output);
  422.       break;

  423.     case '0':
  424.     case '1':
  425.     case '2':
  426.     case '3':
  427.     case '4':
  428.     case '5':
  429.     case '6':
  430.     case '7':
  431.       p = convert_octal (type, p, limit, output);
  432.       break;

  433.     case 'u':
  434.     case 'U':
  435.       {
  436.         int length = *p == 'u' ? 4 : 8;

  437.         ADVANCE;
  438.         if (!isxdigit (*p))
  439.           error (_("\\u used with no following hex digits"));
  440.         p = convert_ucn (p, limit, dest_charset, output, length);
  441.       }
  442.     }

  443.   return p;
  444. }

  445. /* Given a single string from a (C-specific) OP_STRING list, convert
  446.    it to a target string, handling escape sequences specially.  The
  447.    output is written to OUTPUT.  DATA is the input string, which has
  448.    length LEN.  DEST_CHARSET is the name of the target character set,
  449.    and TYPE is the type of target character to use.  */

  450. static void
  451. parse_one_string (struct obstack *output, char *data, int len,
  452.                   const char *dest_charset, struct type *type)
  453. {
  454.   char *limit;

  455.   limit = data + len;

  456.   while (data < limit)
  457.     {
  458.       char *p = data;

  459.       /* Look for next escape, or the end of the input.  */
  460.       while (p < limit && *p != '\\')
  461.         ++p;
  462.       /* If we saw a run of characters, convert them all.  */
  463.       if (p > data)
  464.         convert_between_encodings (host_charset (), dest_charset,
  465.                                    (gdb_byte *) data, p - data, 1,
  466.                                    output, translit_none);
  467.       /* If we saw an escape, convert it.  */
  468.       if (p < limit)
  469.         p = convert_escape (type, dest_charset, p, limit, output);
  470.       data = p;
  471.     }
  472. }

  473. /* Expression evaluator for the C language family.  Most operations
  474.    are delegated to evaluate_subexp_standard; see that function for a
  475.    description of the arguments.  */

  476. struct value *
  477. evaluate_subexp_c (struct type *expect_type, struct expression *exp,
  478.                    int *pos, enum noside noside)
  479. {
  480.   enum exp_opcode op = exp->elts[*pos].opcode;

  481.   switch (op)
  482.     {
  483.     case OP_STRING:
  484.       {
  485.         int oplen, limit;
  486.         struct type *type;
  487.         struct obstack output;
  488.         struct cleanup *cleanup;
  489.         struct value *result;
  490.         enum c_string_type dest_type;
  491.         const char *dest_charset;
  492.         int satisfy_expected = 0;

  493.         obstack_init (&output);
  494.         cleanup = make_cleanup_obstack_free (&output);

  495.         ++*pos;
  496.         oplen = longest_to_int (exp->elts[*pos].longconst);

  497.         ++*pos;
  498.         limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1);
  499.         dest_type
  500.           = (enum c_string_type) longest_to_int (exp->elts[*pos].longconst);
  501.         switch (dest_type & ~C_CHAR)
  502.           {
  503.           case C_STRING:
  504.             type = language_string_char_type (exp->language_defn,
  505.                                               exp->gdbarch);
  506.             break;
  507.           case C_WIDE_STRING:
  508.             type = lookup_typename (exp->language_defn, exp->gdbarch,
  509.                                     "wchar_t", NULL, 0);
  510.             break;
  511.           case C_STRING_16:
  512.             type = lookup_typename (exp->language_defn, exp->gdbarch,
  513.                                     "char16_t", NULL, 0);
  514.             break;
  515.           case C_STRING_32:
  516.             type = lookup_typename (exp->language_defn, exp->gdbarch,
  517.                                     "char32_t", NULL, 0);
  518.             break;
  519.           default:
  520.             internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
  521.           }

  522.         /* Ensure TYPE_LENGTH is valid for TYPE.  */
  523.         check_typedef (type);

  524.         /* If the caller expects an array of some integral type,
  525.            satisfy them.  If something odder is expected, rely on the
  526.            caller to cast.  */
  527.         if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_ARRAY)
  528.           {
  529.             struct type *element_type
  530.               = check_typedef (TYPE_TARGET_TYPE (expect_type));

  531.             if (TYPE_CODE (element_type) == TYPE_CODE_INT
  532.                 || TYPE_CODE (element_type) == TYPE_CODE_CHAR)
  533.               {
  534.                 type = element_type;
  535.                 satisfy_expected = 1;
  536.               }
  537.           }

  538.         dest_charset = charset_for_string_type (dest_type, exp->gdbarch);

  539.         ++*pos;
  540.         while (*pos < limit)
  541.           {
  542.             int len;

  543.             len = longest_to_int (exp->elts[*pos].longconst);

  544.             ++*pos;
  545.             if (noside != EVAL_SKIP)
  546.               parse_one_string (&output, &exp->elts[*pos].string, len,
  547.                                 dest_charset, type);
  548.             *pos += BYTES_TO_EXP_ELEM (len);
  549.           }

  550.         /* Skip the trailing length and opcode.  */
  551.         *pos += 2;

  552.         if (noside == EVAL_SKIP)
  553.           {
  554.             /* Return a dummy value of the appropriate type.  */
  555.             if (expect_type != NULL)
  556.               result = allocate_value (expect_type);
  557.             else if ((dest_type & C_CHAR) != 0)
  558.               result = allocate_value (type);
  559.             else
  560.               result = value_cstring ("", 0, type);
  561.             do_cleanups (cleanup);
  562.             return result;
  563.           }

  564.         if ((dest_type & C_CHAR) != 0)
  565.           {
  566.             LONGEST value;

  567.             if (obstack_object_size (&output) != TYPE_LENGTH (type))
  568.               error (_("Could not convert character "
  569.                        "constant to target character set"));
  570.             value = unpack_long (type, (gdb_byte *) obstack_base (&output));
  571.             result = value_from_longest (type, value);
  572.           }
  573.         else
  574.           {
  575.             int i;

  576.             /* Write the terminating character.  */
  577.             for (i = 0; i < TYPE_LENGTH (type); ++i)
  578.               obstack_1grow (&output, 0);

  579.             if (satisfy_expected)
  580.               {
  581.                 LONGEST low_bound, high_bound;
  582.                 int element_size = TYPE_LENGTH (type);

  583.                 if (get_discrete_bounds (TYPE_INDEX_TYPE (expect_type),
  584.                                          &low_bound, &high_bound) < 0)
  585.                   {
  586.                     low_bound = 0;
  587.                     high_bound = (TYPE_LENGTH (expect_type) / element_size) - 1;
  588.                   }
  589.                 if (obstack_object_size (&output) / element_size
  590.                     > (high_bound - low_bound + 1))
  591.                   error (_("Too many array elements"));

  592.                 result = allocate_value (expect_type);
  593.                 memcpy (value_contents_raw (result), obstack_base (&output),
  594.                         obstack_object_size (&output));
  595.               }
  596.             else
  597.               result = value_cstring (obstack_base (&output),
  598.                                       obstack_object_size (&output),
  599.                                       type);
  600.           }
  601.         do_cleanups (cleanup);
  602.         return result;
  603.       }
  604.       break;

  605.     default:
  606.       break;
  607.     }
  608.   return evaluate_subexp_standard (expect_type, exp, pos, noside);
  609. }



  610. /* Table mapping opcodes into strings for printing operators
  611.    and precedences of the operators.  */

  612. const struct op_print c_op_print_tab[] =
  613. {
  614.   {",", BINOP_COMMA, PREC_COMMA, 0},
  615.   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
  616.   {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
  617.   {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
  618.   {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
  619.   {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
  620.   {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
  621.   {"==", BINOP_EQUAL, PREC_EQUAL, 0},
  622.   {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
  623.   {"<=", BINOP_LEQ, PREC_ORDER, 0},
  624.   {">=", BINOP_GEQ, PREC_ORDER, 0},
  625.   {">", BINOP_GTR, PREC_ORDER, 0},
  626.   {"<", BINOP_LESS, PREC_ORDER, 0},
  627.   {">>", BINOP_RSH, PREC_SHIFT, 0},
  628.   {"<<", BINOP_LSH, PREC_SHIFT, 0},
  629.   {"+", BINOP_ADD, PREC_ADD, 0},
  630.   {"-", BINOP_SUB, PREC_ADD, 0},
  631.   {"*", BINOP_MUL, PREC_MUL, 0},
  632.   {"/", BINOP_DIV, PREC_MUL, 0},
  633.   {"%", BINOP_REM, PREC_MUL, 0},
  634.   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
  635.   {"+", UNOP_PLUS, PREC_PREFIX, 0},
  636.   {"-", UNOP_NEG, PREC_PREFIX, 0},
  637.   {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
  638.   {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
  639.   {"*", UNOP_IND, PREC_PREFIX, 0},
  640.   {"&", UNOP_ADDR, PREC_PREFIX, 0},
  641.   {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
  642.   {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
  643.   {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
  644.   {NULL, 0, 0, 0}
  645. };

  646. enum c_primitive_types {
  647.   c_primitive_type_int,
  648.   c_primitive_type_long,
  649.   c_primitive_type_short,
  650.   c_primitive_type_char,
  651.   c_primitive_type_float,
  652.   c_primitive_type_double,
  653.   c_primitive_type_void,
  654.   c_primitive_type_long_long,
  655.   c_primitive_type_signed_char,
  656.   c_primitive_type_unsigned_char,
  657.   c_primitive_type_unsigned_short,
  658.   c_primitive_type_unsigned_int,
  659.   c_primitive_type_unsigned_long,
  660.   c_primitive_type_unsigned_long_long,
  661.   c_primitive_type_long_double,
  662.   c_primitive_type_complex,
  663.   c_primitive_type_double_complex,
  664.   c_primitive_type_decfloat,
  665.   c_primitive_type_decdouble,
  666.   c_primitive_type_declong,
  667.   nr_c_primitive_types
  668. };

  669. void
  670. c_language_arch_info (struct gdbarch *gdbarch,
  671.                       struct language_arch_info *lai)
  672. {
  673.   const struct builtin_type *builtin = builtin_type (gdbarch);

  674.   lai->string_char_type = builtin->builtin_char;
  675.   lai->primitive_type_vector
  676.     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
  677.                               struct type *);
  678.   lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
  679.   lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
  680.   lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
  681.   lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
  682.   lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
  683.   lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
  684.   lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
  685.   lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
  686.   lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
  687.   lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
  688.   lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
  689.   lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
  690.   lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
  691.   lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
  692.   lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
  693.   lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
  694.   lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
  695.   lai->primitive_type_vector [c_primitive_type_decfloat] = builtin->builtin_decfloat;
  696.   lai->primitive_type_vector [c_primitive_type_decdouble] = builtin->builtin_decdouble;
  697.   lai->primitive_type_vector [c_primitive_type_declong] = builtin->builtin_declong;

  698.   lai->bool_type_default = builtin->builtin_int;
  699. }

  700. const struct exp_descriptor exp_descriptor_c =
  701. {
  702.   print_subexp_standard,
  703.   operator_length_standard,
  704.   operator_check_standard,
  705.   op_name_standard,
  706.   dump_subexp_body_standard,
  707.   evaluate_subexp_c
  708. };

  709. const struct language_defn c_language_defn =
  710. {
  711.   "c",                                /* Language name */
  712.   "C",
  713.   language_c,
  714.   range_check_off,
  715.   case_sensitive_on,
  716.   array_row_major,
  717.   macro_expansion_c,
  718.   &exp_descriptor_c,
  719.   c_parse,
  720.   c_error,
  721.   null_post_parser,
  722.   c_printchar,                        /* Print a character constant */
  723.   c_printstr,                        /* Function to print string constant */
  724.   c_emit_char,                        /* Print a single char */
  725.   c_print_type,                        /* Print a type using appropriate syntax */
  726.   c_print_typedef,                /* Print a typedef using appropriate syntax */
  727.   c_val_print,                        /* Print a value using appropriate syntax */
  728.   c_value_print,                /* Print a top-level value */
  729.   default_read_var_value,        /* la_read_var_value */
  730.   NULL,                                /* Language specific skip_trampoline */
  731.   NULL,                                /* name_of_this */
  732.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  733.   basic_lookup_transparent_type,/* lookup_transparent_type */
  734.   NULL,                                /* Language specific symbol demangler */
  735.   NULL,                                /* Language specific
  736.                                    class_name_from_physname */
  737.   c_op_print_tab,                /* expression operators for printing */
  738.   1,                                /* c-style arrays */
  739.   0,                                /* String lower bound */
  740.   default_word_break_characters,
  741.   default_make_symbol_completion_list,
  742.   c_language_arch_info,
  743.   default_print_array_index,
  744.   default_pass_by_reference,
  745.   c_get_string,
  746.   NULL,                                /* la_get_symbol_name_cmp */
  747.   iterate_over_symbols,
  748.   &c_varobj_ops,
  749.   c_get_compile_context,
  750.   c_compute_program,
  751.   LANG_MAGIC
  752. };

  753. enum cplus_primitive_types {
  754.   cplus_primitive_type_int,
  755.   cplus_primitive_type_long,
  756.   cplus_primitive_type_short,
  757.   cplus_primitive_type_char,
  758.   cplus_primitive_type_float,
  759.   cplus_primitive_type_double,
  760.   cplus_primitive_type_void,
  761.   cplus_primitive_type_long_long,
  762.   cplus_primitive_type_signed_char,
  763.   cplus_primitive_type_unsigned_char,
  764.   cplus_primitive_type_unsigned_short,
  765.   cplus_primitive_type_unsigned_int,
  766.   cplus_primitive_type_unsigned_long,
  767.   cplus_primitive_type_unsigned_long_long,
  768.   cplus_primitive_type_long_double,
  769.   cplus_primitive_type_complex,
  770.   cplus_primitive_type_double_complex,
  771.   cplus_primitive_type_bool,
  772.   cplus_primitive_type_decfloat,
  773.   cplus_primitive_type_decdouble,
  774.   cplus_primitive_type_declong,
  775.   nr_cplus_primitive_types
  776. };

  777. static void
  778. cplus_language_arch_info (struct gdbarch *gdbarch,
  779.                           struct language_arch_info *lai)
  780. {
  781.   const struct builtin_type *builtin = builtin_type (gdbarch);

  782.   lai->string_char_type = builtin->builtin_char;
  783.   lai->primitive_type_vector
  784.     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
  785.                               struct type *);
  786.   lai->primitive_type_vector [cplus_primitive_type_int]
  787.     = builtin->builtin_int;
  788.   lai->primitive_type_vector [cplus_primitive_type_long]
  789.     = builtin->builtin_long;
  790.   lai->primitive_type_vector [cplus_primitive_type_short]
  791.     = builtin->builtin_short;
  792.   lai->primitive_type_vector [cplus_primitive_type_char]
  793.     = builtin->builtin_char;
  794.   lai->primitive_type_vector [cplus_primitive_type_float]
  795.     = builtin->builtin_float;
  796.   lai->primitive_type_vector [cplus_primitive_type_double]
  797.     = builtin->builtin_double;
  798.   lai->primitive_type_vector [cplus_primitive_type_void]
  799.     = builtin->builtin_void;
  800.   lai->primitive_type_vector [cplus_primitive_type_long_long]
  801.     = builtin->builtin_long_long;
  802.   lai->primitive_type_vector [cplus_primitive_type_signed_char]
  803.     = builtin->builtin_signed_char;
  804.   lai->primitive_type_vector [cplus_primitive_type_unsigned_char]
  805.     = builtin->builtin_unsigned_char;
  806.   lai->primitive_type_vector [cplus_primitive_type_unsigned_short]
  807.     = builtin->builtin_unsigned_short;
  808.   lai->primitive_type_vector [cplus_primitive_type_unsigned_int]
  809.     = builtin->builtin_unsigned_int;
  810.   lai->primitive_type_vector [cplus_primitive_type_unsigned_long]
  811.     = builtin->builtin_unsigned_long;
  812.   lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long]
  813.     = builtin->builtin_unsigned_long_long;
  814.   lai->primitive_type_vector [cplus_primitive_type_long_double]
  815.     = builtin->builtin_long_double;
  816.   lai->primitive_type_vector [cplus_primitive_type_complex]
  817.     = builtin->builtin_complex;
  818.   lai->primitive_type_vector [cplus_primitive_type_double_complex]
  819.     = builtin->builtin_double_complex;
  820.   lai->primitive_type_vector [cplus_primitive_type_bool]
  821.     = builtin->builtin_bool;
  822.   lai->primitive_type_vector [cplus_primitive_type_decfloat]
  823.     = builtin->builtin_decfloat;
  824.   lai->primitive_type_vector [cplus_primitive_type_decdouble]
  825.     = builtin->builtin_decdouble;
  826.   lai->primitive_type_vector [cplus_primitive_type_declong]
  827.     = builtin->builtin_declong;

  828.   lai->bool_type_symbol = "bool";
  829.   lai->bool_type_default = builtin->builtin_bool;
  830. }

  831. const struct language_defn cplus_language_defn =
  832. {
  833.   "c++",                        /* Language name */
  834.   "C++",
  835.   language_cplus,
  836.   range_check_off,
  837.   case_sensitive_on,
  838.   array_row_major,
  839.   macro_expansion_c,
  840.   &exp_descriptor_c,
  841.   c_parse,
  842.   c_error,
  843.   null_post_parser,
  844.   c_printchar,                        /* Print a character constant */
  845.   c_printstr,                        /* Function to print string constant */
  846.   c_emit_char,                        /* Print a single char */
  847.   c_print_type,                        /* Print a type using appropriate syntax */
  848.   c_print_typedef,                /* Print a typedef using appropriate syntax */
  849.   c_val_print,                        /* Print a value using appropriate syntax */
  850.   c_value_print,                /* Print a top-level value */
  851.   default_read_var_value,        /* la_read_var_value */
  852.   cplus_skip_trampoline,        /* Language specific skip_trampoline */
  853.   "this",                       /* name_of_this */
  854.   cp_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  855.   cp_lookup_transparent_type,   /* lookup_transparent_type */
  856.   gdb_demangle,                        /* Language specific symbol demangler */
  857.   cp_class_name_from_physname/* Language specific
  858.                                    class_name_from_physname */
  859.   c_op_print_tab,                /* expression operators for printing */
  860.   1,                                /* c-style arrays */
  861.   0,                                /* String lower bound */
  862.   default_word_break_characters,
  863.   default_make_symbol_completion_list,
  864.   cplus_language_arch_info,
  865.   default_print_array_index,
  866.   cp_pass_by_reference,
  867.   c_get_string,
  868.   NULL,                                /* la_get_symbol_name_cmp */
  869.   iterate_over_symbols,
  870.   &cplus_varobj_ops,
  871.   NULL,
  872.   NULL,
  873.   LANG_MAGIC
  874. };

  875. const struct language_defn asm_language_defn =
  876. {
  877.   "asm",                        /* Language name */
  878.   "assembly",
  879.   language_asm,
  880.   range_check_off,
  881.   case_sensitive_on,
  882.   array_row_major,
  883.   macro_expansion_c,
  884.   &exp_descriptor_c,
  885.   c_parse,
  886.   c_error,
  887.   null_post_parser,
  888.   c_printchar,                        /* Print a character constant */
  889.   c_printstr,                        /* Function to print string constant */
  890.   c_emit_char,                        /* Print a single char */
  891.   c_print_type,                        /* Print a type using appropriate syntax */
  892.   c_print_typedef,                /* Print a typedef using appropriate syntax */
  893.   c_val_print,                        /* Print a value using appropriate syntax */
  894.   c_value_print,                /* Print a top-level value */
  895.   default_read_var_value,        /* la_read_var_value */
  896.   NULL,                                /* Language specific skip_trampoline */
  897.   NULL,                                /* name_of_this */
  898.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  899.   basic_lookup_transparent_type,/* lookup_transparent_type */
  900.   NULL,                                /* Language specific symbol demangler */
  901.   NULL,                                /* Language specific
  902.                                    class_name_from_physname */
  903.   c_op_print_tab,                /* expression operators for printing */
  904.   1,                                /* c-style arrays */
  905.   0,                                /* String lower bound */
  906.   default_word_break_characters,
  907.   default_make_symbol_completion_list,
  908.   c_language_arch_info,         /* FIXME: la_language_arch_info.  */
  909.   default_print_array_index,
  910.   default_pass_by_reference,
  911.   c_get_string,
  912.   NULL,                                /* la_get_symbol_name_cmp */
  913.   iterate_over_symbols,
  914.   &default_varobj_ops,
  915.   NULL,
  916.   NULL,
  917.   LANG_MAGIC
  918. };

  919. /* The following language_defn does not represent a real language.
  920.    It just provides a minimal support a-la-C that should allow users
  921.    to do some simple operations when debugging applications that use
  922.    a language currently not supported by GDB.  */

  923. const struct language_defn minimal_language_defn =
  924. {
  925.   "minimal",                        /* Language name */
  926.   "Minimal",
  927.   language_minimal,
  928.   range_check_off,
  929.   case_sensitive_on,
  930.   array_row_major,
  931.   macro_expansion_c,
  932.   &exp_descriptor_c,
  933.   c_parse,
  934.   c_error,
  935.   null_post_parser,
  936.   c_printchar,                        /* Print a character constant */
  937.   c_printstr,                        /* Function to print string constant */
  938.   c_emit_char,                        /* Print a single char */
  939.   c_print_type,                        /* Print a type using appropriate syntax */
  940.   c_print_typedef,                /* Print a typedef using appropriate syntax */
  941.   c_val_print,                        /* Print a value using appropriate syntax */
  942.   c_value_print,                /* Print a top-level value */
  943.   default_read_var_value,        /* la_read_var_value */
  944.   NULL,                                /* Language specific skip_trampoline */
  945.   NULL,                                /* name_of_this */
  946.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  947.   basic_lookup_transparent_type,/* lookup_transparent_type */
  948.   NULL,                                /* Language specific symbol demangler */
  949.   NULL,                                /* Language specific
  950.                                    class_name_from_physname */
  951.   c_op_print_tab,                /* expression operators for printing */
  952.   1,                                /* c-style arrays */
  953.   0,                                /* String lower bound */
  954.   default_word_break_characters,
  955.   default_make_symbol_completion_list,
  956.   c_language_arch_info,
  957.   default_print_array_index,
  958.   default_pass_by_reference,
  959.   c_get_string,
  960.   NULL,                                /* la_get_symbol_name_cmp */
  961.   iterate_over_symbols,
  962.   &default_varobj_ops,
  963.   NULL,
  964.   NULL,
  965.   LANG_MAGIC
  966. };

  967. void
  968. _initialize_c_language (void)
  969. {
  970.   add_language (&c_language_defn);
  971.   add_language (&cplus_language_defn);
  972.   add_language (&asm_language_defn);
  973.   add_language (&minimal_language_defn);
  974. }