1 /* C language support routines for GDB, the GNU debugger.
2 
3    Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "extract-store-integer.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "parser-defs.h"
25 #include "language.h"
26 #include "varobj.h"
27 #include "c-lang.h"
28 #include "c-support.h"
29 #include "valprint.h"
30 #include "macroscope.h"
31 #include "charset.h"
32 #include "demangle.h"
33 #include "cp-abi.h"
34 #include "cp-support.h"
35 #include "gdbsupport/gdb_obstack.h"
36 #include <ctype.h>
37 #include "gdbcore.h"
38 #include "gdbarch.h"
39 #include "c-exp.h"
40 
41 /* Given a C string type, STR_TYPE, return the corresponding target
42    character set name.  */
43 
44 static const char *
charset_for_string_type(c_string_type str_type,struct gdbarch * gdbarch)45 charset_for_string_type (c_string_type str_type, struct gdbarch *gdbarch)
46 {
47   switch (str_type & ~C_CHAR)
48     {
49     case C_STRING:
50       return target_charset (gdbarch);
51     case C_WIDE_STRING:
52       return target_wide_charset (gdbarch);
53     case C_STRING_16:
54       /* FIXME: UTF-16 is not always correct.  */
55       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
56           return "UTF-16BE";
57       else
58           return "UTF-16LE";
59     case C_STRING_32:
60       /* FIXME: UTF-32 is not always correct.  */
61       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
62           return "UTF-32BE";
63       else
64           return "UTF-32LE";
65     }
66   internal_error (_("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 in target BYTE_ORDER to the host character
73    set.  */
74 
75 static c_string_type
classify_type(struct type * elttype,struct gdbarch * gdbarch,const char ** encoding)76 classify_type (struct type *elttype, struct gdbarch *gdbarch,
77                  const char **encoding)
78 {
79   c_string_type result;
80 
81   /* We loop because ELTTYPE may be a typedef, and we want to
82      successively peel each typedef until we reach a type we
83      understand.  We don't use CHECK_TYPEDEF because that will strip
84      all typedefs at once -- but in C, wchar_t is itself a typedef, so
85      that would do the wrong thing.  */
86   while (elttype)
87     {
88       const char *name = elttype->name ();
89 
90       if (name == nullptr)
91           {
92             result = C_CHAR;
93             goto done;
94           }
95 
96       if (!strcmp (name, "wchar_t"))
97           {
98             result = C_WIDE_CHAR;
99             goto done;
100           }
101 
102       if (!strcmp (name, "char16_t"))
103           {
104             result = C_CHAR_16;
105             goto done;
106           }
107 
108       if (!strcmp (name, "char32_t"))
109           {
110             result = C_CHAR_32;
111             goto done;
112           }
113 
114       if (elttype->code () != TYPE_CODE_TYPEDEF)
115           break;
116 
117       /* Call for side effects.  */
118       check_typedef (elttype);
119 
120       if (elttype->target_type ())
121           elttype = elttype->target_type ();
122       else
123           {
124             /* Perhaps check_typedef did not update the target type.  In
125                this case, force the lookup again and hope it works out.
126                It never will for C, but it might for C++.  */
127             elttype = check_typedef (elttype);
128           }
129     }
130 
131   /* Punt.  */
132   result = C_CHAR;
133 
134  done:
135   if (encoding)
136     *encoding = charset_for_string_type (result, gdbarch);
137 
138   return result;
139 }
140 
141 /* Print the character C on STREAM as part of the contents of a
142    literal string whose delimiter is QUOTER.  Note that that format
143    for printing characters and strings is language specific.  */
144 
145 void
emitchar(int c,struct type * type,struct ui_file * stream,int quoter)146 language_defn::emitchar (int c, struct type *type,
147                                struct ui_file *stream, int quoter) const
148 {
149   const char *encoding;
150 
151   classify_type (type, type->arch (), &encoding);
152   generic_emit_char (c, type, stream, quoter, encoding);
153 }
154 
155 /* See language.h.  */
156 
157 void
printchar(int c,struct type * type,struct ui_file * stream)158 language_defn::printchar (int c, struct type *type,
159                                 struct ui_file * stream) const
160 {
161   c_string_type str_type;
162 
163   str_type = classify_type (type, type->arch (), NULL);
164   switch (str_type)
165     {
166     case C_CHAR:
167       break;
168     case C_WIDE_CHAR:
169       gdb_putc ('L', stream);
170       break;
171     case C_CHAR_16:
172       gdb_putc ('u', stream);
173       break;
174     case C_CHAR_32:
175       gdb_putc ('U', stream);
176       break;
177     }
178 
179   gdb_putc ('\'', stream);
180   emitchar (c, type, stream, '\'');
181   gdb_putc ('\'', stream);
182 }
183 
184 /* Print the character string STRING, printing at most LENGTH
185    characters.  LENGTH is -1 if the string is nul terminated.  Each
186    character is WIDTH bytes long.  Printing stops early if the number
187    hits print_max_chars; repeat counts are printed as appropriate.
188    Print ellipses at the end if we had to stop before printing LENGTH
189    characters, or if FORCE_ELLIPSES.  */
190 
191 void
printstr(struct ui_file * stream,struct type * type,const gdb_byte * string,unsigned int length,const char * user_encoding,int force_ellipses,const struct value_print_options * options)192 language_defn::printstr (struct ui_file *stream, struct type *type,
193                                const gdb_byte *string, unsigned int length,
194                                const char *user_encoding, int force_ellipses,
195                                const struct value_print_options *options) const
196 {
197   c_string_type str_type;
198   const char *type_encoding;
199   const char *encoding;
200 
201   str_type = (classify_type (type, type->arch (), &type_encoding)
202                 & ~C_CHAR);
203   switch (str_type)
204     {
205     case C_STRING:
206       break;
207     case C_WIDE_STRING:
208       gdb_puts ("L", stream);
209       break;
210     case C_STRING_16:
211       gdb_puts ("u", stream);
212       break;
213     case C_STRING_32:
214       gdb_puts ("U", stream);
215       break;
216     }
217 
218   encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;
219 
220   generic_printstr (stream, type, string, length, encoding, force_ellipses,
221                         '"', 1, options);
222 }
223 
224 /* Obtain a C string from the inferior storing it in a newly allocated
225    buffer in BUFFER, which should be freed by the caller.  If the in-
226    and out-parameter *LENGTH is specified at -1, the string is read
227    until a null character of the appropriate width is found, otherwise
228    the string is read to the length of characters specified.  The size
229    of a character is determined by the length of the target type of
230    the pointer or array.
231 
232    If VALUE is an array with a known length, and *LENGTH is -1,
233    the function will not read past the end of the array.  However, any
234    declared size of the array is ignored if *LENGTH > 0.
235 
236    On completion, *LENGTH will be set to the size of the string read in
237    characters.  (If a length of -1 is specified, the length returned
238    will not include the null character).  CHARSET is always set to the
239    target charset.  */
240 
241 void
c_get_string(struct value * value,gdb::unique_xmalloc_ptr<gdb_byte> * buffer,int * length,struct type ** char_type,const char ** charset)242 c_get_string (struct value *value, gdb::unique_xmalloc_ptr<gdb_byte> *buffer,
243                 int *length, struct type **char_type,
244                 const char **charset)
245 {
246   int err, width;
247   unsigned int fetchlimit;
248   struct type *type = check_typedef (value->type ());
249   struct type *element_type = type->target_type ();
250   int req_length = *length;
251   enum bfd_endian byte_order
252     = type_byte_order (type);
253 
254   if (element_type == NULL)
255     goto error;
256 
257   if (type->code () == TYPE_CODE_ARRAY)
258     {
259       /* If we know the size of the array, we can use it as a limit on
260            the number of characters to be fetched.  */
261       if (type->num_fields () == 1
262             && type->field (0).type ()->code () == TYPE_CODE_RANGE)
263           {
264             LONGEST low_bound, high_bound;
265 
266             get_discrete_bounds (type->field (0).type (),
267                                      &low_bound, &high_bound);
268             fetchlimit = high_bound - low_bound + 1;
269           }
270       else
271           fetchlimit = UINT_MAX;
272     }
273   else if (type->code () == TYPE_CODE_PTR)
274     fetchlimit = UINT_MAX;
275   else
276     /* We work only with arrays and pointers.  */
277     goto error;
278 
279   if (! c_textual_element_type (element_type, 0))
280     goto error;
281   classify_type (element_type, element_type->arch (), charset);
282   width = element_type->length ();
283 
284   /* If the string lives in GDB's memory instead of the inferior's,
285      then we just need to copy it to BUFFER.  Also, since such strings
286      are arrays with known size, FETCHLIMIT will hold the size of the
287      array.
288 
289      An array is assumed to live in GDB's memory, so we take this path
290      here.
291 
292      However, it's possible for the caller to request more array
293      elements than apparently exist -- this can happen when using the
294      C struct hack.  So, only do this if either no length was
295      specified, or the length is within the existing bounds.  This
296      avoids running off the end of the value's contents.  */
297   if ((value->lval () == not_lval
298        || value->lval () == lval_internalvar
299        || type->code () == TYPE_CODE_ARRAY)
300       && fetchlimit != UINT_MAX
301       && (*length < 0 || *length <= fetchlimit))
302     {
303       int i;
304       const gdb_byte *contents = value->contents ().data ();
305 
306       /* If a length is specified, use that.  */
307       if (*length >= 0)
308           i  = *length;
309       else
310           /* Otherwise, look for a null character.  */
311           for (i = 0; i < fetchlimit; i++)
312             if (extract_unsigned_integer (contents + i * width,
313                                                   width, byte_order) == 0)
314               break;
315 
316       /* I is now either a user-defined length, the number of non-null
317            characters, or FETCHLIMIT.  */
318       *length = i * width;
319       buffer->reset ((gdb_byte *) xmalloc (*length));
320       memcpy (buffer->get (), contents, *length);
321       err = 0;
322     }
323   else
324     {
325       /* value_as_address does not return an address for an array when
326            c_style_arrays is false, so we handle that specially
327            here.  */
328       CORE_ADDR addr;
329       if (type->code () == TYPE_CODE_ARRAY)
330           {
331             if (value->lval () != lval_memory)
332               error (_("Attempt to take address of value "
333                          "not located in memory."));
334             addr = value->address ();
335           }
336       else
337           addr = value_as_address (value);
338 
339       /* Prior to the fix for PR 16196 read_string would ignore fetchlimit
340            if length > 0.  The old "broken" behaviour is the behaviour we want:
341            The caller may want to fetch 100 bytes from a variable length array
342            implemented using the common idiom of having an array of length 1 at
343            the end of a struct.  In this case we want to ignore the declared
344            size of the array.  However, it's counterintuitive to implement that
345            behaviour in read_string: what does fetchlimit otherwise mean if
346            length > 0.  Therefore we implement the behaviour we want here:
347            If *length > 0, don't specify a fetchlimit.  This preserves the
348            previous behaviour.  We could move this check above where we know
349            whether the array is declared with a fixed size, but we only want
350            to apply this behaviour when calling read_string.  PR 16286.  */
351       if (*length > 0)
352           fetchlimit = UINT_MAX;
353 
354       err = target_read_string (addr, *length, width, fetchlimit,
355                                         buffer, length);
356       if (err != 0)
357           memory_error (TARGET_XFER_E_IO, addr);
358     }
359 
360   /* If the LENGTH is specified at -1, we want to return the string
361      length up to the terminating null character.  If an actual length
362      was specified, we want to return the length of exactly what was
363      read.  */
364   if (req_length == -1)
365     /* If the last character is null, subtract it from LENGTH.  */
366     if (*length > 0
367           && extract_unsigned_integer (buffer->get () + *length - width,
368                                              width, byte_order) == 0)
369       *length -= width;
370 
371   /* The read_string function will return the number of bytes read.
372      If length returned from read_string was > 0, return the number of
373      characters read by dividing the number of bytes by width.  */
374   if (*length != 0)
375      *length = *length / width;
376 
377   *char_type = element_type;
378 
379   return;
380 
381  error:
382   {
383     std::string type_str = type_to_string (type);
384     if (!type_str.empty ())
385       {
386           error (_("Trying to read string with inappropriate type `%s'."),
387                  type_str.c_str ());
388       }
389     else
390       error (_("Trying to read string with inappropriate type."));
391   }
392 }
393 
394 
395 /* Evaluating C and C++ expressions.  */
396 
397 /* Convert a UCN.  The digits of the UCN start at P and extend no
398    farther than LIMIT.  DEST_CHARSET is the name of the character set
399    into which the UCN should be converted.  The results are written to
400    OUTPUT.  LENGTH is the maximum length of the UCN, either 4 or 8.
401    Returns a pointer to just after the final digit of the UCN.  */
402 
403 static const char *
convert_ucn(const char * p,const char * limit,const char * dest_charset,struct obstack * output,int length)404 convert_ucn (const char *p, const char *limit, const char *dest_charset,
405                struct obstack *output, int length)
406 {
407   unsigned long result = 0;
408   gdb_byte data[4];
409   int i;
410 
411   for (i = 0; i < length && p < limit && ISXDIGIT (*p); ++i, ++p)
412     result = (result << 4) + fromhex (*p);
413 
414   for (i = 3; i >= 0; --i)
415     {
416       data[i] = result & 0xff;
417       result >>= 8;
418     }
419 
420   convert_between_encodings ("UTF-32BE", dest_charset, data,
421                                    4, 4, output, translit_none);
422 
423   return p;
424 }
425 
426 /* Emit a character, VALUE, which was specified numerically, to
427    OUTPUT.  TYPE is the target character type.  */
428 
429 static void
emit_numeric_character(struct type * type,unsigned long value,struct obstack * output)430 emit_numeric_character (struct type *type, unsigned long value,
431                               struct obstack *output)
432 {
433   gdb_byte *buffer;
434 
435   buffer = (gdb_byte *) alloca (type->length ());
436   pack_long (buffer, type, value);
437   obstack_grow (output, buffer, type->length ());
438 }
439 
440 /* Convert an octal escape sequence.  TYPE is the target character
441    type.  The digits of the escape sequence begin at P and extend no
442    farther than LIMIT.  The result is written to OUTPUT.  Returns a
443    pointer to just after the final digit of the escape sequence.  */
444 
445 static const char *
convert_octal(struct type * type,const char * p,const char * limit,struct obstack * output)446 convert_octal (struct type *type, const char *p,
447                  const char *limit, struct obstack *output)
448 {
449   int i;
450   unsigned long value = 0;
451 
452   for (i = 0;
453        i < 3 && p < limit && ISDIGIT (*p) && *p != '8' && *p != '9';
454        ++i)
455     {
456       value = 8 * value + fromhex (*p);
457       ++p;
458     }
459 
460   emit_numeric_character (type, value, output);
461 
462   return p;
463 }
464 
465 /* Convert a hex escape sequence.  TYPE is the target character type.
466    The digits of the escape sequence begin at P and extend no farther
467    than LIMIT.  The result is written to OUTPUT.  Returns a pointer to
468    just after the final digit of the escape sequence.  */
469 
470 static const char *
convert_hex(struct type * type,const char * p,const char * limit,struct obstack * output)471 convert_hex (struct type *type, const char *p,
472                const char *limit, struct obstack *output)
473 {
474   unsigned long value = 0;
475 
476   while (p < limit && ISXDIGIT (*p))
477     {
478       value = 16 * value + fromhex (*p);
479       ++p;
480     }
481 
482   emit_numeric_character (type, value, output);
483 
484   return p;
485 }
486 
487 #define ADVANCE                                             \
488   do {                                                      \
489     ++p;                                          \
490     if (p == limit)                               \
491       error (_("Malformed escape sequence"));     \
492   } while (0)
493 
494 /* Convert an escape sequence to a target format.  TYPE is the target
495    character type to use, and DEST_CHARSET is the name of the target
496    character set.  The backslash of the escape sequence is at *P, and
497    the escape sequence will not extend past LIMIT.  The results are
498    written to OUTPUT.  Returns a pointer to just past the final
499    character of the escape sequence.  */
500 
501 static const char *
convert_escape(struct type * type,const char * dest_charset,const char * p,const char * limit,struct obstack * output)502 convert_escape (struct type *type, const char *dest_charset,
503                     const char *p, const char *limit, struct obstack *output)
504 {
505   /* Skip the backslash.  */
506   ADVANCE;
507 
508   switch (*p)
509     {
510     case '\\':
511       obstack_1grow (output, '\\');
512       ++p;
513       break;
514 
515     case 'x':
516       ADVANCE;
517       if (!ISXDIGIT (*p))
518           error (_("\\x used with no following hex digits."));
519       p = convert_hex (type, p, limit, output);
520       break;
521 
522     case '0':
523     case '1':
524     case '2':
525     case '3':
526     case '4':
527     case '5':
528     case '6':
529     case '7':
530       p = convert_octal (type, p, limit, output);
531       break;
532 
533     case 'u':
534     case 'U':
535       {
536           int length = *p == 'u' ? 4 : 8;
537 
538           ADVANCE;
539           if (!ISXDIGIT (*p))
540             error (_("\\u used with no following hex digits"));
541           p = convert_ucn (p, limit, dest_charset, output, length);
542       }
543     }
544 
545   return p;
546 }
547 
548 /* Given a single string from a (C-specific) OP_STRING list, convert
549    it to a target string, handling escape sequences specially.  The
550    output is written to OUTPUT.  DATA is the input string, which has
551    length LEN.  DEST_CHARSET is the name of the target character set,
552    and TYPE is the type of target character to use.  */
553 
554 static void
parse_one_string(struct obstack * output,const char * data,int len,const char * dest_charset,struct type * type)555 parse_one_string (struct obstack *output, const char *data, int len,
556                       const char *dest_charset, struct type *type)
557 {
558   const char *limit;
559 
560   limit = data + len;
561 
562   while (data < limit)
563     {
564       const char *p = data;
565 
566       /* Look for next escape, or the end of the input.  */
567       while (p < limit && *p != '\\')
568           ++p;
569       /* If we saw a run of characters, convert them all.  */
570       if (p > data)
571           convert_between_encodings (host_charset (), dest_charset,
572                                            (const gdb_byte *) data, p - data, 1,
573                                            output, translit_none);
574       /* If we saw an escape, convert it.  */
575       if (p < limit)
576           p = convert_escape (type, dest_charset, p, limit, output);
577       data = p;
578     }
579 }
580 
581 namespace expr
582 {
583 
584 value *
evaluate(struct type * expect_type,struct expression * exp,enum noside noside)585 c_string_operation::evaluate (struct type *expect_type,
586                                     struct expression *exp,
587                                     enum noside noside)
588 {
589   struct type *type;
590   struct value *result;
591   c_string_type dest_type;
592   const char *dest_charset;
593   int satisfy_expected = 0;
594 
595   auto_obstack output;
596 
597   dest_type = std::get<0> (m_storage);
598 
599   switch (dest_type & ~C_CHAR)
600     {
601     case C_STRING:
602       type = language_string_char_type (exp->language_defn,
603                                                   exp->gdbarch);
604       break;
605     case C_WIDE_STRING:
606       type = lookup_typename (exp->language_defn, "wchar_t", NULL, 0);
607       break;
608     case C_STRING_16:
609       type = lookup_typename (exp->language_defn, "char16_t", NULL, 0);
610       break;
611     case C_STRING_32:
612       type = lookup_typename (exp->language_defn, "char32_t", NULL, 0);
613       break;
614     default:
615       internal_error (_("unhandled c_string_type"));
616     }
617 
618   /* If the caller expects an array of some integral type,
619      satisfy them.  If something odder is expected, rely on the
620      caller to cast.  */
621   if (expect_type && expect_type->code () == TYPE_CODE_ARRAY)
622     {
623       struct type *element_type
624           = check_typedef (expect_type->target_type ());
625 
626       if (element_type->code () == TYPE_CODE_INT
627             || element_type->code () == TYPE_CODE_CHAR)
628           {
629             type = element_type;
630             satisfy_expected = 1;
631           }
632     }
633 
634   dest_charset = charset_for_string_type (dest_type, exp->gdbarch);
635 
636   for (const std::string &item : std::get<1> (m_storage))
637     parse_one_string (&output, item.c_str (), item.size (),
638                           dest_charset, type);
639 
640   if ((dest_type & C_CHAR) != 0)
641     {
642       LONGEST value;
643 
644       if (obstack_object_size (&output) != type->length ())
645           error (_("Could not convert character "
646                      "constant to target character set"));
647       value = unpack_long (type, (gdb_byte *) obstack_base (&output));
648       result = value_from_longest (type, value);
649     }
650   else
651     {
652       int element_size = type->length ();
653 
654       if (satisfy_expected)
655           {
656             LONGEST low_bound, high_bound;
657 
658             if (!get_discrete_bounds (expect_type->index_type (),
659                                             &low_bound, &high_bound))
660               {
661                 low_bound = 0;
662                 high_bound = (expect_type->length () / element_size) - 1;
663               }
664             if (obstack_object_size (&output) / element_size
665                 > (high_bound - low_bound + 1))
666               error (_("Too many array elements"));
667 
668             result = value::allocate (expect_type);
669             memcpy (result->contents_raw ().data (), obstack_base (&output),
670                       obstack_object_size (&output));
671             /* Write the terminating character.  */
672             memset (result->contents_raw ().data () + obstack_object_size (&output),
673                       0, element_size);
674           }
675       else
676           result = value_cstring ((const gdb_byte *) obstack_base (&output),
677                                         obstack_object_size (&output) / element_size,
678                                         type);
679     }
680   return result;
681 }
682 
683 } /* namespace expr */
684 
685 
686 /* See c-lang.h.  */
687 
688 bool
c_is_string_type_p(struct type * type)689 c_is_string_type_p (struct type *type)
690 {
691   type = check_typedef (type);
692   while (type->code () == TYPE_CODE_REF)
693     {
694       type = type->target_type ();
695       type = check_typedef (type);
696     }
697 
698   switch (type->code ())
699     {
700     case TYPE_CODE_ARRAY:
701       {
702           /* See if target type looks like a string.  */
703           struct type *array_target_type = type->target_type ();
704           return (type->length () > 0
705                     && array_target_type->length () > 0
706                     && c_textual_element_type (array_target_type, 0));
707       }
708     case TYPE_CODE_STRING:
709       return true;
710     case TYPE_CODE_PTR:
711       {
712           struct type *element_type = type->target_type ();
713           return c_textual_element_type (element_type, 0);
714       }
715     default:
716       break;
717     }
718 
719   return false;
720 }
721 
722 
723 
724 /* See c-lang.h.  */
725 
726 gdb::unique_xmalloc_ptr<char>
c_canonicalize_name(const char * name)727 c_canonicalize_name (const char *name)
728 {
729   if (strchr (name, ' ') != nullptr
730       || streq (name, "signed")
731       || streq (name, "unsigned"))
732     return cp_canonicalize_string (name);
733   return nullptr;
734 }
735 
736 
737 
738 void
c_language_arch_info(struct gdbarch * gdbarch,struct language_arch_info * lai)739 c_language_arch_info (struct gdbarch *gdbarch,
740                           struct language_arch_info *lai)
741 {
742   const struct builtin_type *builtin = builtin_type (gdbarch);
743 
744   /* Helper function to allow shorter lines below.  */
745   auto add  = [&] (struct type * t)
746   {
747     lai->add_primitive_type (t);
748   };
749 
750   add (builtin->builtin_int);
751   add (builtin->builtin_long);
752   add (builtin->builtin_short);
753   add (builtin->builtin_char);
754   add (builtin->builtin_float);
755   add (builtin->builtin_double);
756   add (builtin->builtin_void);
757   add (builtin->builtin_long_long);
758   add (builtin->builtin_signed_char);
759   add (builtin->builtin_unsigned_char);
760   add (builtin->builtin_unsigned_short);
761   add (builtin->builtin_unsigned_int);
762   add (builtin->builtin_unsigned_long);
763   add (builtin->builtin_unsigned_long_long);
764   add (builtin->builtin_long_double);
765   add (builtin->builtin_complex);
766   add (builtin->builtin_double_complex);
767   add (builtin->builtin_decfloat);
768   add (builtin->builtin_decdouble);
769   add (builtin->builtin_declong);
770 
771   lai->set_string_char_type (builtin->builtin_char);
772   lai->set_bool_type (builtin->builtin_int);
773 }
774 
775 /* Class representing the C language.  */
776 
777 class c_language : public language_defn
778 {
779 public:
c_language()780   c_language ()
781     : language_defn (language_c)
782   { /* Nothing.  */ }
783 
784   /* See language.h.  */
785 
name()786   const char *name () const override
787   { return "c"; }
788 
789   /* See language.h.  */
790 
natural_name()791   const char *natural_name () const override
792   { return "C"; }
793 
794   /* See language.h.  */
795 
filename_extensions()796   const std::vector<const char *> &filename_extensions () const override
797   {
798     static const std::vector<const char *> extensions = { ".c" };
799     return extensions;
800   }
801 
802   /* See language.h.  */
language_arch_info(struct gdbarch * gdbarch,struct language_arch_info * lai)803   void language_arch_info (struct gdbarch *gdbarch,
804                                  struct language_arch_info *lai) const override
805   {
806     c_language_arch_info (gdbarch, lai);
807   }
808 
809   /* See language.h.  */
get_compile_instance()810   std::unique_ptr<compile_instance> get_compile_instance () const override
811   {
812     return c_get_compile_context ();
813   }
814 
815   /* See language.h.  */
compute_program(compile_instance * inst,const char * input,struct gdbarch * gdbarch,const struct block * expr_block,CORE_ADDR expr_pc)816   std::string compute_program (compile_instance *inst,
817                                      const char *input,
818                                      struct gdbarch *gdbarch,
819                                      const struct block *expr_block,
820                                      CORE_ADDR expr_pc) const override
821   {
822     return c_compute_program (inst, input, gdbarch, expr_block, expr_pc);
823   }
824 
825   /* See language.h.  */
826 
can_print_type_offsets()827   bool can_print_type_offsets () const override
828   {
829     return true;
830   }
831 
832   /* See language.h.  */
833 
print_type(struct type * type,const char * varstring,struct ui_file * stream,int show,int level,const struct type_print_options * flags)834   void print_type (struct type *type, const char *varstring,
835                        struct ui_file *stream, int show, int level,
836                        const struct type_print_options *flags) const override
837   {
838     c_print_type (type, varstring, stream, show, level, la_language, flags);
839   }
840 
841   /* See language.h.  */
842 
store_sym_names_in_linkage_form_p()843   bool store_sym_names_in_linkage_form_p () const override
844   { return true; }
845 
846   /* See language.h.  */
847 
macro_expansion()848   enum macro_expansion macro_expansion () const override
849   { return macro_expansion_c; }
850 };
851 
852 /* Single instance of the C language class.  */
853 
854 static c_language c_language_defn;
855 
856 /* A class for the C++ language.  */
857 
858 class cplus_language : public language_defn
859 {
860 public:
cplus_language()861   cplus_language ()
862     : language_defn (language_cplus)
863   { /* Nothing.  */ }
864 
865   /* See language.h.  */
866 
name()867   const char *name () const override
868   { return "c++"; }
869 
870   /* See language.h.  */
871 
natural_name()872   const char *natural_name () const override
873   { return "C++"; }
874 
875   /* See language.h  */
get_digit_separator()876   const char *get_digit_separator () const override
877   { return "\'"; }
878 
879   /* See language.h.  */
880 
filename_extensions()881   const std::vector<const char *> &filename_extensions () const override
882   {
883     static const std::vector<const char *> extensions
884       = { ".C", ".cc", ".cp", ".cpp", ".cxx", ".c++" };
885     return extensions;
886   }
887 
888   /* See language.h.  */
889 
pass_by_reference_info(struct type * type)890   struct language_pass_by_ref_info pass_by_reference_info
891           (struct type *type) const override
892   {
893     return cp_pass_by_reference (type);
894   }
895 
896   /* See language.h.  */
language_arch_info(struct gdbarch * gdbarch,struct language_arch_info * lai)897   void language_arch_info (struct gdbarch *gdbarch,
898                                  struct language_arch_info *lai) const override
899   {
900     const struct builtin_type *builtin = builtin_type (gdbarch);
901 
902     /* Helper function to allow shorter lines below.  */
903     auto add  = [&] (struct type * t)
904     {
905       lai->add_primitive_type (t);
906     };
907 
908     add (builtin->builtin_int);
909     add (builtin->builtin_long);
910     add (builtin->builtin_short);
911     add (builtin->builtin_char);
912     add (builtin->builtin_float);
913     add (builtin->builtin_double);
914     add (builtin->builtin_void);
915     add (builtin->builtin_long_long);
916     add (builtin->builtin_signed_char);
917     add (builtin->builtin_unsigned_char);
918     add (builtin->builtin_unsigned_short);
919     add (builtin->builtin_unsigned_int);
920     add (builtin->builtin_unsigned_long);
921     add (builtin->builtin_unsigned_long_long);
922     add (builtin->builtin_long_double);
923     add (builtin->builtin_complex);
924     add (builtin->builtin_double_complex);
925     add (builtin->builtin_bool);
926     add (builtin->builtin_decfloat);
927     add (builtin->builtin_decdouble);
928     add (builtin->builtin_declong);
929     add (builtin->builtin_char16);
930     add (builtin->builtin_char32);
931     add (builtin->builtin_wchar);
932 
933     lai->set_string_char_type (builtin->builtin_char);
934     lai->set_bool_type (builtin->builtin_bool, "bool");
935   }
936 
937   /* See language.h.  */
lookup_transparent_type(const char * name,domain_search_flags flags)938   struct type *lookup_transparent_type (const char *name,
939                                                   domain_search_flags flags)
940     const override
941   {
942     return cp_lookup_transparent_type (name, flags);
943   }
944 
945   /* See language.h.  */
get_compile_instance()946   std::unique_ptr<compile_instance> get_compile_instance () const override
947   {
948     return cplus_get_compile_context ();
949   }
950 
951   /* See language.h.  */
compute_program(compile_instance * inst,const char * input,struct gdbarch * gdbarch,const struct block * expr_block,CORE_ADDR expr_pc)952   std::string compute_program (compile_instance *inst,
953                                      const char *input,
954                                      struct gdbarch *gdbarch,
955                                      const struct block *expr_block,
956                                      CORE_ADDR expr_pc) const override
957   {
958     return cplus_compute_program (inst, input, gdbarch, expr_block, expr_pc);
959   }
960 
961   /* See language.h.  */
search_name_hash(const char * name)962   unsigned int search_name_hash (const char *name) const override
963   {
964     return cp_search_name_hash (name);
965   }
966 
967   /* See language.h.  */
sniff_from_mangled_name(const char * mangled,gdb::unique_xmalloc_ptr<char> * demangled)968   bool sniff_from_mangled_name
969        (const char *mangled,
970           gdb::unique_xmalloc_ptr<char> *demangled) const override
971   {
972     *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
973     return *demangled != NULL;
974   }
975 
976   /* See language.h.  */
977 
demangle_symbol(const char * mangled,int options)978   gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
979                                                              int options) const override
980   {
981     return gdb_demangle (mangled, options);
982   }
983 
984   /* See language.h.  */
985 
can_print_type_offsets()986   bool can_print_type_offsets () const override
987   {
988     return true;
989   }
990 
991   /* See language.h.  */
992 
print_type(struct type * type,const char * varstring,struct ui_file * stream,int show,int level,const struct type_print_options * flags)993   void print_type (struct type *type, const char *varstring,
994                        struct ui_file *stream, int show, int level,
995                        const struct type_print_options *flags) const override
996   {
997     c_print_type (type, varstring, stream, show, level, la_language, flags);
998   }
999 
1000   /* See language.h.  */
1001 
skip_trampoline(const frame_info_ptr & fi,CORE_ADDR pc)1002   CORE_ADDR skip_trampoline (const frame_info_ptr &fi,
1003                                    CORE_ADDR pc) const override
1004   {
1005     return cplus_skip_trampoline (fi, pc);
1006   }
1007 
1008   /* See language.h.  */
1009 
class_name_from_physname(const char * physname)1010   char *class_name_from_physname (const char *physname) const override
1011   {
1012     return cp_class_name_from_physname (physname);
1013   }
1014 
1015   /* See language.h.  */
1016 
lookup_symbol_nonlocal(const char * name,const struct block * block,const domain_search_flags domain)1017   struct block_symbol lookup_symbol_nonlocal
1018           (const char *name, const struct block *block,
1019            const domain_search_flags domain) const override
1020   {
1021     return cp_lookup_symbol_nonlocal (this, name, block, domain);
1022   }
1023 
1024   /* See language.h.  */
1025 
name_of_this()1026   const char *name_of_this () const override
1027   { return "this"; }
1028 
1029   /* See language.h.  */
1030 
macro_expansion()1031   enum macro_expansion macro_expansion () const override
1032   { return macro_expansion_c; }
1033 
1034   /* See language.h.  */
1035 
varobj_ops()1036   const struct lang_varobj_ops *varobj_ops () const override
1037   { return &cplus_varobj_ops; }
1038 
1039 protected:
1040 
1041   /* See language.h.  */
1042 
get_symbol_name_matcher_inner(const lookup_name_info & lookup_name)1043   symbol_name_matcher_ftype *get_symbol_name_matcher_inner
1044           (const lookup_name_info &lookup_name) const override
1045   {
1046     return cp_get_symbol_name_matcher (lookup_name);
1047   }
1048 };
1049 
1050 /* The single instance of the C++ language class.  */
1051 
1052 static cplus_language cplus_language_defn;
1053 
1054 /* A class for the ASM language.  */
1055 
1056 class asm_language : public language_defn
1057 {
1058 public:
asm_language()1059   asm_language ()
1060     : language_defn (language_asm)
1061   { /* Nothing.  */ }
1062 
1063   /* See language.h.  */
1064 
name()1065   const char *name () const override
1066   { return "asm"; }
1067 
1068   /* See language.h.  */
1069 
natural_name()1070   const char *natural_name () const override
1071   { return "Assembly"; }
1072 
1073   /* See language.h.  */
1074 
filename_extensions()1075   const std::vector<const char *> &filename_extensions () const override
1076   {
1077     static const std::vector<const char *> extensions
1078       = { ".s", ".sx", ".S" };
1079     return extensions;
1080   }
1081 
1082   /* See language.h.
1083 
1084      FIXME: Should this have its own arch info method?  */
language_arch_info(struct gdbarch * gdbarch,struct language_arch_info * lai)1085   void language_arch_info (struct gdbarch *gdbarch,
1086                                  struct language_arch_info *lai) const override
1087   {
1088     c_language_arch_info (gdbarch, lai);
1089   }
1090 
1091   /* See language.h.  */
1092 
can_print_type_offsets()1093   bool can_print_type_offsets () const override
1094   {
1095     return true;
1096   }
1097 
1098   /* See language.h.  */
1099 
print_type(struct type * type,const char * varstring,struct ui_file * stream,int show,int level,const struct type_print_options * flags)1100   void print_type (struct type *type, const char *varstring,
1101                        struct ui_file *stream, int show, int level,
1102                        const struct type_print_options *flags) const override
1103   {
1104     c_print_type (type, varstring, stream, show, level, la_language, flags);
1105   }
1106 
1107   /* See language.h.  */
1108 
store_sym_names_in_linkage_form_p()1109   bool store_sym_names_in_linkage_form_p () const override
1110   { return true; }
1111 
1112   /* See language.h.  */
1113 
macro_expansion()1114   enum macro_expansion macro_expansion () const override
1115   { return macro_expansion_c; }
1116 };
1117 
1118 /* The single instance of the ASM language class.  */
1119 static asm_language asm_language_defn;
1120 
1121 /* A class for the minimal language.  This does not represent a real
1122    language.  It just provides a minimal support a-la-C that should allow
1123    users to do some simple operations when debugging applications that use
1124    a language currently not supported by GDB.  */
1125 
1126 class minimal_language : public language_defn
1127 {
1128 public:
minimal_language()1129   minimal_language ()
1130     : language_defn (language_minimal)
1131   { /* Nothing.  */ }
1132 
1133   /* See language.h.  */
1134 
name()1135   const char *name () const override
1136   { return "minimal"; }
1137 
1138   /* See language.h.  */
1139 
natural_name()1140   const char *natural_name () const override
1141   { return "Minimal"; }
1142 
1143   /* See language.h.  */
language_arch_info(struct gdbarch * gdbarch,struct language_arch_info * lai)1144   void language_arch_info (struct gdbarch *gdbarch,
1145                                  struct language_arch_info *lai) const override
1146   {
1147     c_language_arch_info (gdbarch, lai);
1148   }
1149 
1150   /* See language.h.  */
1151 
can_print_type_offsets()1152   bool can_print_type_offsets () const override
1153   {
1154     return true;
1155   }
1156 
1157   /* See language.h.  */
1158 
print_type(struct type * type,const char * varstring,struct ui_file * stream,int show,int level,const struct type_print_options * flags)1159   void print_type (struct type *type, const char *varstring,
1160                        struct ui_file *stream, int show, int level,
1161                        const struct type_print_options *flags) const override
1162   {
1163     c_print_type (type, varstring, stream, show, level, la_language, flags);
1164   }
1165 
1166   /* See language.h.  */
1167 
store_sym_names_in_linkage_form_p()1168   bool store_sym_names_in_linkage_form_p () const override
1169   { return true; }
1170 
1171   /* See language.h.  */
1172 
macro_expansion()1173   enum macro_expansion macro_expansion () const override
1174   { return macro_expansion_c; }
1175 };
1176 
1177 /* The single instance of the minimal language class.  */
1178 static minimal_language minimal_language_defn;
1179