]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/c-lang.c
Fix regression on aarch64-linux gdbserver
[thirdparty/binutils-gdb.git] / gdb / c-lang.c
CommitLineData
c906108c 1/* C language support routines for GDB, the GNU debugger.
ce27fb25 2
1d506c26 3 Copyright (C) 1992-2024 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 19
ec452525 20#include "extract-store-integer.h"
4de283e4
TT
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"
c906108c 27#include "c-lang.h"
b1b60145 28#include "c-support.h"
4de283e4
TT
29#include "valprint.h"
30#include "macroscope.h"
234b45d4 31#include "charset.h"
4de283e4 32#include "demangle.h"
b18be20d 33#include "cp-abi.h"
1fcb5155 34#include "cp-support.h"
bf31fd38 35#include "gdbsupport/gdb_obstack.h"
4de283e4 36#include <ctype.h>
578d3588 37#include "gdbcore.h"
0d12e84c 38#include "gdbarch.h"
72d0a711 39#include "c-exp.h"
8e25bafe 40
6c7a06a3
TT
41/* Given a C string type, STR_TYPE, return the corresponding target
42 character set name. */
43
44static const char *
0c801b96 45charset_for_string_type (c_string_type str_type, struct gdbarch *gdbarch)
6c7a06a3
TT
46{
47 switch (str_type & ~C_CHAR)
48 {
49 case C_STRING:
f870a310 50 return target_charset (gdbarch);
6c7a06a3 51 case C_WIDE_STRING:
f870a310 52 return target_wide_charset (gdbarch);
6c7a06a3 53 case C_STRING_16:
b8899f2b 54 /* FIXME: UTF-16 is not always correct. */
f870a310 55 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 56 return "UTF-16BE";
6c7a06a3 57 else
b8899f2b 58 return "UTF-16LE";
6c7a06a3 59 case C_STRING_32:
b8899f2b 60 /* FIXME: UTF-32 is not always correct. */
f870a310 61 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 62 return "UTF-32BE";
6c7a06a3 63 else
b8899f2b 64 return "UTF-32LE";
6c7a06a3 65 }
f34652de 66 internal_error (_("unhandled c_string_type"));
6c7a06a3
TT
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
aff410f1
MS
72 characters of this type in target BYTE_ORDER to the host character
73 set. */
6c7a06a3 74
0c801b96 75static c_string_type
f870a310 76classify_type (struct type *elttype, struct gdbarch *gdbarch,
e17a4113 77 const char **encoding)
6c7a06a3 78{
0c801b96 79 c_string_type result;
6c7a06a3 80
85e306ed
TT
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)
6c7a06a3 87 {
7d93a1e0 88 const char *name = elttype->name ();
6c7a06a3 89
1c0e4363 90 if (name == nullptr)
6c7a06a3
TT
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
78134374 114 if (elttype->code () != TYPE_CODE_TYPEDEF)
85e306ed
TT
115 break;
116
117 /* Call for side effects. */
118 check_typedef (elttype);
119
27710edb
SM
120 if (elttype->target_type ())
121 elttype = elttype->target_type ();
85e306ed
TT
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++. */
f168693b 127 elttype = check_typedef (elttype);
85e306ed 128 }
6c7a06a3 129 }
6c7a06a3
TT
130
131 /* Punt. */
132 result = C_CHAR;
133
134 done:
e17a4113 135 if (encoding)
f870a310 136 *encoding = charset_for_string_type (result, gdbarch);
e17a4113 137
6c7a06a3
TT
138 return result;
139}
140
aff410f1
MS
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. */
c906108c 144
6aecb9c2 145void
c1c7fe59
TT
146language_defn::emitchar (int c, struct type *type,
147 struct ui_file *stream, int quoter) const
c906108c 148{
6c7a06a3 149 const char *encoding;
234b45d4 150
8ee511af 151 classify_type (type, type->arch (), &encoding);
3b2b8fea 152 generic_emit_char (c, type, stream, quoter, encoding);
c906108c
SS
153}
154
c5ee319e
AB
155/* See language.h. */
156
c906108c 157void
c5ee319e
AB
158language_defn::printchar (int c, struct type *type,
159 struct ui_file * stream) const
c906108c 160{
0c801b96 161 c_string_type str_type;
6c7a06a3 162
8ee511af 163 str_type = classify_type (type, type->arch (), NULL);
6c7a06a3
TT
164 switch (str_type)
165 {
166 case C_CHAR:
167 break;
168 case C_WIDE_CHAR:
a11ac3b3 169 gdb_putc ('L', stream);
6c7a06a3
TT
170 break;
171 case C_CHAR_16:
a11ac3b3 172 gdb_putc ('u', stream);
6c7a06a3
TT
173 break;
174 case C_CHAR_32:
a11ac3b3 175 gdb_putc ('U', stream);
6c7a06a3
TT
176 break;
177 }
178
a11ac3b3 179 gdb_putc ('\'', stream);
76ca72bc 180 emitchar (c, type, stream, '\'');
a11ac3b3 181 gdb_putc ('\'', stream);
c906108c
SS
182}
183
aff410f1
MS
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
76b58849
AB
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
aff410f1 189 characters, or if FORCE_ELLIPSES. */
c906108c
SS
190
191void
3a3bb6eb
TT
192language_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
c906108c 196{
0c801b96 197 c_string_type str_type;
3b2b8fea
TT
198 const char *type_encoding;
199 const char *encoding;
200
8ee511af 201 str_type = (classify_type (type, type->arch (), &type_encoding)
f870a310 202 & ~C_CHAR);
6c7a06a3
TT
203 switch (str_type)
204 {
205 case C_STRING:
206 break;
207 case C_WIDE_STRING:
0426ad51 208 gdb_puts ("L", stream);
6c7a06a3
TT
209 break;
210 case C_STRING_16:
0426ad51 211 gdb_puts ("u", stream);
6c7a06a3
TT
212 break;
213 case C_STRING_32:
0426ad51 214 gdb_puts ("U", stream);
6c7a06a3
TT
215 break;
216 }
217
3b2b8fea 218 encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;
6c7a06a3 219
3b2b8fea
TT
220 generic_printstr (stream, type, string, length, encoding, force_ellipses,
221 '"', 1, options);
c906108c 222}
ae6a3a4c
TJB
223
224/* Obtain a C string from the inferior storing it in a newly allocated
aff410f1
MS
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
fbb8f299 227 until a null character of the appropriate width is found, otherwise
aff410f1
MS
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
0987cf35
DE
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
fbb8f299
PM
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. */
ae6a3a4c
TJB
240
241void
b4be9fad 242c_get_string (struct value *value, gdb::unique_xmalloc_ptr<gdb_byte> *buffer,
aff410f1
MS
243 int *length, struct type **char_type,
244 const char **charset)
ae6a3a4c
TJB
245{
246 int err, width;
247 unsigned int fetchlimit;
d0c97917 248 struct type *type = check_typedef (value->type ());
27710edb 249 struct type *element_type = type->target_type ();
fbb8f299 250 int req_length = *length;
aff410f1 251 enum bfd_endian byte_order
34877895 252 = type_byte_order (type);
ae6a3a4c
TJB
253
254 if (element_type == NULL)
255 goto error;
256
78134374 257 if (type->code () == TYPE_CODE_ARRAY)
ae6a3a4c 258 {
aff410f1
MS
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. */
1f704f76 261 if (type->num_fields () == 1
940da03e 262 && type->field (0).type ()->code () == TYPE_CODE_RANGE)
ae6a3a4c
TJB
263 {
264 LONGEST low_bound, high_bound;
265
940da03e 266 get_discrete_bounds (type->field (0).type (),
ae6a3a4c
TJB
267 &low_bound, &high_bound);
268 fetchlimit = high_bound - low_bound + 1;
269 }
270 else
271 fetchlimit = UINT_MAX;
272 }
78134374 273 else if (type->code () == TYPE_CODE_PTR)
ae6a3a4c
TJB
274 fetchlimit = UINT_MAX;
275 else
276 /* We work only with arrays and pointers. */
277 goto error;
278
96c07c5b 279 if (! c_textual_element_type (element_type, 0))
ae6a3a4c 280 goto error;
8ee511af 281 classify_type (element_type, element_type->arch (), charset);
df86565b 282 width = element_type->length ();
ae6a3a4c 283
aff410f1
MS
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
80e55b13
TT
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. */
736355f2
TT
297 if ((value->lval () == not_lval
298 || value->lval () == lval_internalvar
78134374 299 || type->code () == TYPE_CODE_ARRAY)
80e55b13
TT
300 && fetchlimit != UINT_MAX
301 && (*length < 0 || *length <= fetchlimit))
ae6a3a4c
TJB
302 {
303 int i;
efaf1ae0 304 const gdb_byte *contents = value->contents ().data ();
ae6a3a4c 305
fbb8f299
PM
306 /* If a length is specified, use that. */
307 if (*length >= 0)
308 i = *length;
309 else
e623f035
SM
310 /* Otherwise, look for a null character. */
311 for (i = 0; i < fetchlimit; i++)
aff410f1
MS
312 if (extract_unsigned_integer (contents + i * width,
313 width, byte_order) == 0)
e623f035 314 break;
fbb8f299
PM
315
316 /* I is now either a user-defined length, the number of non-null
e623f035 317 characters, or FETCHLIMIT. */
ae6a3a4c 318 *length = i * width;
b4be9fad
TT
319 buffer->reset ((gdb_byte *) xmalloc (*length));
320 memcpy (buffer->get (), contents, *length);
ae6a3a4c
TJB
321 err = 0;
322 }
323 else
324 {
80e55b13
TT
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;
78134374 329 if (type->code () == TYPE_CODE_ARRAY)
80e55b13 330 {
736355f2 331 if (value->lval () != lval_memory)
80e55b13
TT
332 error (_("Attempt to take address of value "
333 "not located in memory."));
9feb2d07 334 addr = value->address ();
80e55b13
TT
335 }
336 else
337 addr = value_as_address (value);
621c8364 338
0987cf35
DE
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
3b1bdd53 354 err = target_read_string (addr, *length, width, fetchlimit,
9da74023 355 buffer, length);
d09f2c3f 356 if (err != 0)
b4be9fad 357 memory_error (TARGET_XFER_E_IO, addr);
ae6a3a4c
TJB
358 }
359
fbb8f299
PM
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
b4be9fad 367 && extract_unsigned_integer (buffer->get () + *length - width,
aff410f1 368 width, byte_order) == 0)
fbb8f299
PM
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;
ae6a3a4c 376
96c07c5b 377 *char_type = element_type;
ae6a3a4c
TJB
378
379 return;
380
381 error:
382 {
2f408ecb
PA
383 std::string type_str = type_to_string (type);
384 if (!type_str.empty ())
ae6a3a4c 385 {
ae6a3a4c 386 error (_("Trying to read string with inappropriate type `%s'."),
2f408ecb 387 type_str.c_str ());
ae6a3a4c
TJB
388 }
389 else
390 error (_("Trying to read string with inappropriate type."));
391 }
392}
393
c906108c 394\f
6c7a06a3
TT
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
e8b2f0d9
TT
403static const char *
404convert_ucn (const char *p, const char *limit, const char *dest_charset,
6c7a06a3
TT
405 struct obstack *output, int length)
406{
407 unsigned long result = 0;
408 gdb_byte data[4];
409 int i;
410
b1b60145 411 for (i = 0; i < length && p < limit && ISXDIGIT (*p); ++i, ++p)
2b531492 412 result = (result << 4) + fromhex (*p);
6c7a06a3
TT
413
414 for (i = 3; i >= 0; --i)
415 {
416 data[i] = result & 0xff;
417 result >>= 8;
418 }
419
aff410f1
MS
420 convert_between_encodings ("UTF-32BE", dest_charset, data,
421 4, 4, output, translit_none);
6c7a06a3
TT
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
429static void
430emit_numeric_character (struct type *type, unsigned long value,
431 struct obstack *output)
432{
433 gdb_byte *buffer;
434
df86565b 435 buffer = (gdb_byte *) alloca (type->length ());
6c7a06a3 436 pack_long (buffer, type, value);
df86565b 437 obstack_grow (output, buffer, type->length ());
6c7a06a3
TT
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
e8b2f0d9
TT
445static const char *
446convert_octal (struct type *type, const char *p,
447 const char *limit, struct obstack *output)
6c7a06a3 448{
30b66ecc 449 int i;
6c7a06a3
TT
450 unsigned long value = 0;
451
30b66ecc 452 for (i = 0;
b1b60145 453 i < 3 && p < limit && ISDIGIT (*p) && *p != '8' && *p != '9';
30b66ecc 454 ++i)
6c7a06a3 455 {
2b531492 456 value = 8 * value + fromhex (*p);
6c7a06a3
TT
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
e8b2f0d9
TT
470static const char *
471convert_hex (struct type *type, const char *p,
472 const char *limit, struct obstack *output)
6c7a06a3
TT
473{
474 unsigned long value = 0;
475
b1b60145 476 while (p < limit && ISXDIGIT (*p))
6c7a06a3 477 {
2b531492 478 value = 16 * value + fromhex (*p);
6c7a06a3
TT
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
e8b2f0d9 501static const char *
6c7a06a3 502convert_escape (struct type *type, const char *dest_charset,
e8b2f0d9 503 const char *p, const char *limit, struct obstack *output)
6c7a06a3
TT
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;
b1b60145 517 if (!ISXDIGIT (*p))
6c7a06a3
TT
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;
c5504eaf 537
6c7a06a3 538 ADVANCE;
b1b60145 539 if (!ISXDIGIT (*p))
6c7a06a3
TT
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
554static void
e8b2f0d9 555parse_one_string (struct obstack *output, const char *data, int len,
6c7a06a3
TT
556 const char *dest_charset, struct type *type)
557{
e8b2f0d9 558 const char *limit;
6c7a06a3
TT
559
560 limit = data + len;
561
562 while (data < limit)
563 {
e8b2f0d9 564 const char *p = data;
c5504eaf 565
6c7a06a3
TT
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,
e8b2f0d9 572 (const gdb_byte *) data, p - data, 1,
aff410f1 573 output, translit_none);
6c7a06a3
TT
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
72d0a711
TT
581namespace expr
582{
583
584value *
585c_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:
f34652de 615 internal_error (_("unhandled c_string_type"));
72d0a711
TT
616 }
617
72d0a711
TT
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
27710edb 624 = check_typedef (expect_type->target_type ());
72d0a711
TT
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
0b2b0b82
TT
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);
72d0a711
TT
639
640 if ((dest_type & C_CHAR) != 0)
641 {
642 LONGEST value;
643
df86565b 644 if (obstack_object_size (&output) != type->length ())
72d0a711
TT
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 {
baab3753 652 int element_size = type->length ();
72d0a711
TT
653
654 if (satisfy_expected)
655 {
656 LONGEST low_bound, high_bound;
72d0a711
TT
657
658 if (!get_discrete_bounds (expect_type->index_type (),
659 &low_bound, &high_bound))
660 {
661 low_bound = 0;
df86565b 662 high_bound = (expect_type->length () / element_size) - 1;
72d0a711
TT
663 }
664 if (obstack_object_size (&output) / element_size
665 > (high_bound - low_bound + 1))
666 error (_("Too many array elements"));
667
317c3ed9 668 result = value::allocate (expect_type);
bbe912ba 669 memcpy (result->contents_raw ().data (), obstack_base (&output),
72d0a711 670 obstack_object_size (&output));
baab3753
AB
671 /* Write the terminating character. */
672 memset (result->contents_raw ().data () + obstack_object_size (&output),
673 0, element_size);
72d0a711
TT
674 }
675 else
baab3753
AB
676 result = value_cstring ((const gdb_byte *) obstack_base (&output),
677 obstack_object_size (&output) / element_size,
72d0a711
TT
678 type);
679 }
680 return result;
681}
682
683} /* namespace expr */
684
43cc5389 685\f
4be290b2
AB
686/* See c-lang.h. */
687
688bool
689c_is_string_type_p (struct type *type)
690{
691 type = check_typedef (type);
78134374 692 while (type->code () == TYPE_CODE_REF)
4be290b2 693 {
27710edb 694 type = type->target_type ();
4be290b2
AB
695 type = check_typedef (type);
696 }
697
78134374 698 switch (type->code ())
4be290b2
AB
699 {
700 case TYPE_CODE_ARRAY:
701 {
702 /* See if target type looks like a string. */
27710edb 703 struct type *array_target_type = type->target_type ();
df86565b
SM
704 return (type->length () > 0
705 && array_target_type->length () > 0
4be290b2
AB
706 && c_textual_element_type (array_target_type, 0));
707 }
708 case TYPE_CODE_STRING:
709 return true;
710 case TYPE_CODE_PTR:
711 {
27710edb 712 struct type *element_type = type->target_type ();
4be290b2
AB
713 return c_textual_element_type (element_type, 0);
714 }
715 default:
716 break;
717 }
718
719 return false;
720}
721
c906108c 722\f
685419e2 723
55fc1623
TT
724/* See c-lang.h. */
725
726gdb::unique_xmalloc_ptr<char>
727c_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\f
737
e9667a65 738void
685419e2
AC
739c_language_arch_info (struct gdbarch *gdbarch,
740 struct language_arch_info *lai)
741{
742 const struct builtin_type *builtin = builtin_type (gdbarch);
c5504eaf 743
7bea47f0
AB
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);
cad351d1 773}
685419e2 774
0874fd07
AB
775/* Class representing the C language. */
776
777class c_language : public language_defn
778{
779public:
780 c_language ()
0e25e767 781 : language_defn (language_c)
0874fd07 782 { /* Nothing. */ }
1fb314aa 783
6f7664a9
AB
784 /* See language.h. */
785
786 const char *name () const override
787 { return "c"; }
788
789 /* See language.h. */
790
791 const char *natural_name () const override
792 { return "C"; }
793
e171d6f1
AB
794 /* See language.h. */
795
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
1fb314aa
AB
802 /* See language.h. */
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 }
8e25bafe
AB
808
809 /* See language.h. */
bdfea17e 810 std::unique_ptr<compile_instance> get_compile_instance () const override
8e25bafe
AB
811 {
812 return c_get_compile_context ();
813 }
fbfb0a46 814
9a49ad8c
AB
815 /* See language.h. */
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
fbfb0a46
AB
825 /* See language.h. */
826
97e20099
TT
827 bool can_print_type_offsets () const override
828 {
829 return true;
830 }
831
832 /* See language.h. */
833
fbfb0a46
AB
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 {
1c6fbf42 838 c_print_type (type, varstring, stream, show, level, la_language, flags);
fbfb0a46 839 }
d3355e4d
AB
840
841 /* See language.h. */
842
843 bool store_sym_names_in_linkage_form_p () const override
844 { return true; }
1ac14a04
AB
845
846 /* See language.h. */
847
848 enum macro_expansion macro_expansion () const override
849 { return macro_expansion_c; }
0874fd07
AB
850};
851
852/* Single instance of the C language class. */
853
854static c_language c_language_defn;
855
0874fd07
AB
856/* A class for the C++ language. */
857
858class cplus_language : public language_defn
859{
860public:
861 cplus_language ()
0e25e767 862 : language_defn (language_cplus)
0874fd07 863 { /* Nothing. */ }
48448202
AB
864
865 /* See language.h. */
866
6f7664a9
AB
867 const char *name () const override
868 { return "c++"; }
869
870 /* See language.h. */
871
872 const char *natural_name () const override
873 { return "C++"; }
874
21a527df
EL
875 /* See language.h */
876 const char *get_digit_separator () const override
877 { return "\'"; }
878
6f7664a9
AB
879 /* See language.h. */
880
e171d6f1
AB
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
48448202
AB
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 }
1fb314aa
AB
895
896 /* See language.h. */
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
7bea47f0
AB
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");
1fb314aa 935 }
54f4ca46
AB
936
937 /* See language.h. */
1ab9eefe
TT
938 struct type *lookup_transparent_type (const char *name,
939 domain_search_flags flags)
940 const override
54f4ca46 941 {
1ab9eefe 942 return cp_lookup_transparent_type (name, flags);
54f4ca46 943 }
8e25bafe
AB
944
945 /* See language.h. */
bdfea17e 946 std::unique_ptr<compile_instance> get_compile_instance () const override
8e25bafe
AB
947 {
948 return cplus_get_compile_context ();
949 }
fb8006fd 950
9a49ad8c
AB
951 /* See language.h. */
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
fb8006fd
AB
961 /* See language.h. */
962 unsigned int search_name_hash (const char *name) const override
963 {
964 return cp_search_name_hash (name);
965 }
6f827019
AB
966
967 /* See language.h. */
3456e70c
TT
968 bool sniff_from_mangled_name
969 (const char *mangled,
970 gdb::unique_xmalloc_ptr<char> *demangled) const override
6f827019
AB
971 {
972 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
973 return *demangled != NULL;
974 }
fbfb0a46
AB
975
976 /* See language.h. */
977
3456e70c
TT
978 gdb::unique_xmalloc_ptr<char> demangle_symbol (const char *mangled,
979 int options) const override
0a50df5d
AB
980 {
981 return gdb_demangle (mangled, options);
982 }
983
984 /* See language.h. */
985
97e20099
TT
986 bool can_print_type_offsets () const override
987 {
988 return true;
989 }
990
991 /* See language.h. */
992
fbfb0a46
AB
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 {
1c6fbf42 997 c_print_type (type, varstring, stream, show, level, la_language, flags);
fbfb0a46 998 }
f6eee2d0
AB
999
1000 /* See language.h. */
1001
9b0ccb1e 1002 CORE_ADDR skip_trampoline (const frame_info_ptr &fi,
f6eee2d0
AB
1003 CORE_ADDR pc) const override
1004 {
1005 return cplus_skip_trampoline (fi, pc);
1006 }
eff93b4d
AB
1007
1008 /* See language.h. */
1009
1010 char *class_name_from_physname (const char *physname) const override
1011 {
1012 return cp_class_name_from_physname (physname);
1013 }
c9debfb9 1014
a78a19b1
AB
1015 /* See language.h. */
1016
1017 struct block_symbol lookup_symbol_nonlocal
1018 (const char *name, const struct block *block,
ccf41c24 1019 const domain_search_flags domain) const override
a78a19b1
AB
1020 {
1021 return cp_lookup_symbol_nonlocal (this, name, block, domain);
1022 }
1023
5bae7c4e
AB
1024 /* See language.h. */
1025
1026 const char *name_of_this () const override
1027 { return "this"; }
1028
1ac14a04
AB
1029 /* See language.h. */
1030
1031 enum macro_expansion macro_expansion () const override
1032 { return macro_expansion_c; }
1033
b63a3f3f
AB
1034 /* See language.h. */
1035
1036 const struct lang_varobj_ops *varobj_ops () const override
1037 { return &cplus_varobj_ops; }
1038
c9debfb9
AB
1039protected:
1040
1041 /* See language.h. */
1042
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 }
0874fd07
AB
1048};
1049
1050/* The single instance of the C++ language class. */
1051
1052static cplus_language cplus_language_defn;
1053
0874fd07
AB
1054/* A class for the ASM language. */
1055
1056class asm_language : public language_defn
1057{
1058public:
1059 asm_language ()
0e25e767 1060 : language_defn (language_asm)
0874fd07 1061 { /* Nothing. */ }
1fb314aa 1062
6f7664a9
AB
1063 /* See language.h. */
1064
1065 const char *name () const override
1066 { return "asm"; }
1067
1068 /* See language.h. */
1069
1070 const char *natural_name () const override
1071 { return "Assembly"; }
1072
e171d6f1
AB
1073 /* See language.h. */
1074
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
1fb314aa
AB
1082 /* See language.h.
1083
1084 FIXME: Should this have its own arch info method? */
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 }
fbfb0a46
AB
1090
1091 /* See language.h. */
1092
97e20099
TT
1093 bool can_print_type_offsets () const override
1094 {
1095 return true;
1096 }
1097
1098 /* See language.h. */
1099
fbfb0a46
AB
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 {
1c6fbf42 1104 c_print_type (type, varstring, stream, show, level, la_language, flags);
fbfb0a46 1105 }
d3355e4d
AB
1106
1107 /* See language.h. */
1108
1109 bool store_sym_names_in_linkage_form_p () const override
1110 { return true; }
1ac14a04
AB
1111
1112 /* See language.h. */
1113
1114 enum macro_expansion macro_expansion () const override
1115 { return macro_expansion_c; }
0874fd07
AB
1116};
1117
1118/* The single instance of the ASM language class. */
1119static asm_language asm_language_defn;
1120
0e25e767
AB
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
20a0e81d
JB
1124 a language currently not supported by GDB. */
1125
0874fd07
AB
1126class minimal_language : public language_defn
1127{
1128public:
1129 minimal_language ()
0e25e767 1130 : language_defn (language_minimal)
0874fd07 1131 { /* Nothing. */ }
1fb314aa 1132
6f7664a9
AB
1133 /* See language.h. */
1134
1135 const char *name () const override
1136 { return "minimal"; }
1137
1138 /* See language.h. */
1139
1140 const char *natural_name () const override
1141 { return "Minimal"; }
1142
1fb314aa
AB
1143 /* See language.h. */
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 }
fbfb0a46
AB
1149
1150 /* See language.h. */
1151
97e20099
TT
1152 bool can_print_type_offsets () const override
1153 {
1154 return true;
1155 }
1156
1157 /* See language.h. */
1158
fbfb0a46
AB
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 {
1c6fbf42 1163 c_print_type (type, varstring, stream, show, level, la_language, flags);
fbfb0a46 1164 }
d3355e4d
AB
1165
1166 /* See language.h. */
1167
1168 bool store_sym_names_in_linkage_form_p () const override
1169 { return true; }
1ac14a04
AB
1170
1171 /* See language.h. */
1172
1173 enum macro_expansion macro_expansion () const override
1174 { return macro_expansion_c; }
0874fd07
AB
1175};
1176
1177/* The single instance of the minimal language class. */
1178static minimal_language minimal_language_defn;