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