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