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