gdb/python: don't allow the user to delete window title attributes
[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
41 /* Given a C string type, STR_TYPE, return the corresponding target
42 character set name. */
43
44 static const char *
45 charset_for_string_type (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 (__FILE__, __LINE__, _("unhandled c_string_type"));
67 }
68
69 /* Classify ELTTYPE according to what kind of character it is. Return
70 the enum constant representing the character type. Also set
71 *ENCODING to the name of the character set to use when converting
72 characters of this type in target BYTE_ORDER to the host character
73 set. */
74
75 static c_string_type
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 (elttype->code () == TYPE_CODE_CHAR || !name)
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 (TYPE_TARGET_TYPE (elttype))
121 elttype = TYPE_TARGET_TYPE (elttype);
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
146 c_emit_char (int c, struct type *type,
147 struct ui_file *stream, int quoter)
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
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 fputc_filtered ('L', stream);
170 break;
171 case C_CHAR_16:
172 fputc_filtered ('u', stream);
173 break;
174 case C_CHAR_32:
175 fputc_filtered ('U', stream);
176 break;
177 }
178
179 fputc_filtered ('\'', stream);
180 emitchar (c, type, stream, '\'');
181 fputc_filtered ('\'', 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; repeat counts are printed as appropriate. Print
188 ellipses at the end if we had to stop before printing LENGTH
189 characters, or if FORCE_ELLIPSES. */
190
191 void
192 c_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)
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 fputs_filtered ("L", stream);
209 break;
210 case C_STRING_16:
211 fputs_filtered ("u", stream);
212 break;
213 case C_STRING_32:
214 fputs_filtered ("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
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 (value));
249 struct type *element_type = TYPE_TARGET_TYPE (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 = TYPE_LENGTH (element_type);
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 (value) == not_lval
298 || VALUE_LVAL (value) == 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 (value);
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 (value) != lval_memory)
332 error (_("Attempt to take address of value "
333 "not located in memory."));
334 addr = value_address (value);
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 = read_string (addr, *length, width, fetchlimit,
355 byte_order, 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 \f
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 *
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) + host_hex_value (*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
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 (type));
436 pack_long (buffer, type, value);
437 obstack_grow (output, buffer, TYPE_LENGTH (type));
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 *
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 + host_hex_value (*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 *
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 + host_hex_value (*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 *
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
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 /* Expression evaluator for the C language family. Most operations
582 are delegated to evaluate_subexp_standard; see that function for a
583 description of the arguments. */
584
585 struct value *
586 evaluate_subexp_c (struct type *expect_type, struct expression *exp,
587 int *pos, enum noside noside)
588 {
589 enum exp_opcode op = exp->elts[*pos].opcode;
590
591 switch (op)
592 {
593 case OP_STRING:
594 {
595 int oplen, limit;
596 struct type *type;
597 struct value *result;
598 c_string_type dest_type;
599 const char *dest_charset;
600 int satisfy_expected = 0;
601
602 auto_obstack output;
603
604 ++*pos;
605 oplen = longest_to_int (exp->elts[*pos].longconst);
606
607 ++*pos;
608 limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1);
609 dest_type = ((enum c_string_type_values)
610 longest_to_int (exp->elts[*pos].longconst));
611 switch (dest_type & ~C_CHAR)
612 {
613 case C_STRING:
614 type = language_string_char_type (exp->language_defn,
615 exp->gdbarch);
616 break;
617 case C_WIDE_STRING:
618 type = lookup_typename (exp->language_defn, "wchar_t", NULL, 0);
619 break;
620 case C_STRING_16:
621 type = lookup_typename (exp->language_defn, "char16_t", NULL, 0);
622 break;
623 case C_STRING_32:
624 type = lookup_typename (exp->language_defn, "char32_t", NULL, 0);
625 break;
626 default:
627 internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
628 }
629
630 /* Ensure TYPE_LENGTH is valid for TYPE. */
631 check_typedef (type);
632
633 /* If the caller expects an array of some integral type,
634 satisfy them. If something odder is expected, rely on the
635 caller to cast. */
636 if (expect_type && expect_type->code () == TYPE_CODE_ARRAY)
637 {
638 struct type *element_type
639 = check_typedef (TYPE_TARGET_TYPE (expect_type));
640
641 if (element_type->code () == TYPE_CODE_INT
642 || element_type->code () == TYPE_CODE_CHAR)
643 {
644 type = element_type;
645 satisfy_expected = 1;
646 }
647 }
648
649 dest_charset = charset_for_string_type (dest_type, exp->gdbarch);
650
651 ++*pos;
652 while (*pos < limit)
653 {
654 int len;
655
656 len = longest_to_int (exp->elts[*pos].longconst);
657
658 ++*pos;
659 if (noside != EVAL_SKIP)
660 parse_one_string (&output, &exp->elts[*pos].string, len,
661 dest_charset, type);
662 *pos += BYTES_TO_EXP_ELEM (len);
663 }
664
665 /* Skip the trailing length and opcode. */
666 *pos += 2;
667
668 if (noside == EVAL_SKIP)
669 {
670 /* Return a dummy value of the appropriate type. */
671 if (expect_type != NULL)
672 result = allocate_value (expect_type);
673 else if ((dest_type & C_CHAR) != 0)
674 result = allocate_value (type);
675 else
676 result = value_cstring ("", 0, type);
677 return result;
678 }
679
680 if ((dest_type & C_CHAR) != 0)
681 {
682 LONGEST value;
683
684 if (obstack_object_size (&output) != TYPE_LENGTH (type))
685 error (_("Could not convert character "
686 "constant to target character set"));
687 value = unpack_long (type, (gdb_byte *) obstack_base (&output));
688 result = value_from_longest (type, value);
689 }
690 else
691 {
692 int i;
693
694 /* Write the terminating character. */
695 for (i = 0; i < TYPE_LENGTH (type); ++i)
696 obstack_1grow (&output, 0);
697
698 if (satisfy_expected)
699 {
700 LONGEST low_bound, high_bound;
701 int element_size = TYPE_LENGTH (type);
702
703 if (!get_discrete_bounds (expect_type->index_type (),
704 &low_bound, &high_bound))
705 {
706 low_bound = 0;
707 high_bound = (TYPE_LENGTH (expect_type) / element_size) - 1;
708 }
709 if (obstack_object_size (&output) / element_size
710 > (high_bound - low_bound + 1))
711 error (_("Too many array elements"));
712
713 result = allocate_value (expect_type);
714 memcpy (value_contents_raw (result), obstack_base (&output),
715 obstack_object_size (&output));
716 }
717 else
718 result = value_cstring ((const char *) obstack_base (&output),
719 obstack_object_size (&output),
720 type);
721 }
722 return result;
723 }
724 break;
725
726 default:
727 break;
728 }
729 return evaluate_subexp_standard (expect_type, exp, pos, noside);
730 }
731 \f
732 /* See c-lang.h. */
733
734 bool
735 c_is_string_type_p (struct type *type)
736 {
737 type = check_typedef (type);
738 while (type->code () == TYPE_CODE_REF)
739 {
740 type = TYPE_TARGET_TYPE (type);
741 type = check_typedef (type);
742 }
743
744 switch (type->code ())
745 {
746 case TYPE_CODE_ARRAY:
747 {
748 /* See if target type looks like a string. */
749 struct type *array_target_type = TYPE_TARGET_TYPE (type);
750 return (TYPE_LENGTH (type) > 0
751 && TYPE_LENGTH (array_target_type) > 0
752 && c_textual_element_type (array_target_type, 0));
753 }
754 case TYPE_CODE_STRING:
755 return true;
756 case TYPE_CODE_PTR:
757 {
758 struct type *element_type = TYPE_TARGET_TYPE (type);
759 return c_textual_element_type (element_type, 0);
760 }
761 default:
762 break;
763 }
764
765 return false;
766 }
767
768 \f
769 /* Table mapping opcodes into strings for printing operators
770 and precedences of the operators. */
771
772 const struct op_print c_op_print_tab[] =
773 {
774 {",", BINOP_COMMA, PREC_COMMA, 0},
775 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
776 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
777 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
778 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
779 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
780 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
781 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
782 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
783 {"<=", BINOP_LEQ, PREC_ORDER, 0},
784 {">=", BINOP_GEQ, PREC_ORDER, 0},
785 {">", BINOP_GTR, PREC_ORDER, 0},
786 {"<", BINOP_LESS, PREC_ORDER, 0},
787 {">>", BINOP_RSH, PREC_SHIFT, 0},
788 {"<<", BINOP_LSH, PREC_SHIFT, 0},
789 {"+", BINOP_ADD, PREC_ADD, 0},
790 {"-", BINOP_SUB, PREC_ADD, 0},
791 {"*", BINOP_MUL, PREC_MUL, 0},
792 {"/", BINOP_DIV, PREC_MUL, 0},
793 {"%", BINOP_REM, PREC_MUL, 0},
794 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
795 {"+", UNOP_PLUS, PREC_PREFIX, 0},
796 {"-", UNOP_NEG, PREC_PREFIX, 0},
797 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
798 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
799 {"*", UNOP_IND, PREC_PREFIX, 0},
800 {"&", UNOP_ADDR, PREC_PREFIX, 0},
801 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
802 {"alignof ", UNOP_ALIGNOF, PREC_PREFIX, 0},
803 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
804 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
805 {NULL, OP_NULL, PREC_PREFIX, 0}
806 };
807 \f
808
809 void
810 c_language_arch_info (struct gdbarch *gdbarch,
811 struct language_arch_info *lai)
812 {
813 const struct builtin_type *builtin = builtin_type (gdbarch);
814
815 /* Helper function to allow shorter lines below. */
816 auto add = [&] (struct type * t)
817 {
818 lai->add_primitive_type (t);
819 };
820
821 add (builtin->builtin_int);
822 add (builtin->builtin_long);
823 add (builtin->builtin_short);
824 add (builtin->builtin_char);
825 add (builtin->builtin_float);
826 add (builtin->builtin_double);
827 add (builtin->builtin_void);
828 add (builtin->builtin_long_long);
829 add (builtin->builtin_signed_char);
830 add (builtin->builtin_unsigned_char);
831 add (builtin->builtin_unsigned_short);
832 add (builtin->builtin_unsigned_int);
833 add (builtin->builtin_unsigned_long);
834 add (builtin->builtin_unsigned_long_long);
835 add (builtin->builtin_long_double);
836 add (builtin->builtin_complex);
837 add (builtin->builtin_double_complex);
838 add (builtin->builtin_decfloat);
839 add (builtin->builtin_decdouble);
840 add (builtin->builtin_declong);
841
842 lai->set_string_char_type (builtin->builtin_char);
843 lai->set_bool_type (builtin->builtin_int);
844 }
845
846 const struct exp_descriptor exp_descriptor_c =
847 {
848 print_subexp_standard,
849 operator_length_standard,
850 operator_check_standard,
851 dump_subexp_body_standard,
852 evaluate_subexp_c
853 };
854
855 /* Class representing the C language. */
856
857 class c_language : public language_defn
858 {
859 public:
860 c_language ()
861 : language_defn (language_c)
862 { /* Nothing. */ }
863
864 /* See language.h. */
865
866 const char *name () const override
867 { return "c"; }
868
869 /* See language.h. */
870
871 const char *natural_name () const override
872 { return "C"; }
873
874 /* See language.h. */
875
876 const std::vector<const char *> &filename_extensions () const override
877 {
878 static const std::vector<const char *> extensions = { ".c" };
879 return extensions;
880 }
881
882 /* See language.h. */
883 void language_arch_info (struct gdbarch *gdbarch,
884 struct language_arch_info *lai) const override
885 {
886 c_language_arch_info (gdbarch, lai);
887 }
888
889 /* See language.h. */
890 std::unique_ptr<compile_instance> get_compile_instance () const override
891 {
892 return c_get_compile_context ();
893 }
894
895 /* See language.h. */
896 std::string compute_program (compile_instance *inst,
897 const char *input,
898 struct gdbarch *gdbarch,
899 const struct block *expr_block,
900 CORE_ADDR expr_pc) const override
901 {
902 return c_compute_program (inst, input, gdbarch, expr_block, expr_pc);
903 }
904
905 /* See language.h. */
906
907 void print_type (struct type *type, const char *varstring,
908 struct ui_file *stream, int show, int level,
909 const struct type_print_options *flags) const override
910 {
911 c_print_type (type, varstring, stream, show, level, flags);
912 }
913
914 /* See language.h. */
915
916 bool store_sym_names_in_linkage_form_p () const override
917 { return true; }
918
919 /* See language.h. */
920
921 enum macro_expansion macro_expansion () const override
922 { return macro_expansion_c; }
923
924 /* See language.h. */
925
926 const struct exp_descriptor *expression_ops () const override
927 { return &exp_descriptor_c; }
928
929 /* See language.h. */
930
931 const struct op_print *opcode_print_table () const override
932 { return c_op_print_tab; }
933 };
934
935 /* Single instance of the C language class. */
936
937 static c_language c_language_defn;
938
939 /* A class for the C++ language. */
940
941 class cplus_language : public language_defn
942 {
943 public:
944 cplus_language ()
945 : language_defn (language_cplus)
946 { /* Nothing. */ }
947
948 /* See language.h. */
949
950 const char *name () const override
951 { return "c++"; }
952
953 /* See language.h. */
954
955 const char *natural_name () const override
956 { return "C++"; }
957
958 /* See language.h. */
959
960 const std::vector<const char *> &filename_extensions () const override
961 {
962 static const std::vector<const char *> extensions
963 = { ".C", ".cc", ".cp", ".cpp", ".cxx", ".c++" };
964 return extensions;
965 }
966
967 /* See language.h. */
968
969 struct language_pass_by_ref_info pass_by_reference_info
970 (struct type *type) const override
971 {
972 return cp_pass_by_reference (type);
973 }
974
975 /* See language.h. */
976 void language_arch_info (struct gdbarch *gdbarch,
977 struct language_arch_info *lai) const override
978 {
979 const struct builtin_type *builtin = builtin_type (gdbarch);
980
981 /* Helper function to allow shorter lines below. */
982 auto add = [&] (struct type * t)
983 {
984 lai->add_primitive_type (t);
985 };
986
987 add (builtin->builtin_int);
988 add (builtin->builtin_long);
989 add (builtin->builtin_short);
990 add (builtin->builtin_char);
991 add (builtin->builtin_float);
992 add (builtin->builtin_double);
993 add (builtin->builtin_void);
994 add (builtin->builtin_long_long);
995 add (builtin->builtin_signed_char);
996 add (builtin->builtin_unsigned_char);
997 add (builtin->builtin_unsigned_short);
998 add (builtin->builtin_unsigned_int);
999 add (builtin->builtin_unsigned_long);
1000 add (builtin->builtin_unsigned_long_long);
1001 add (builtin->builtin_long_double);
1002 add (builtin->builtin_complex);
1003 add (builtin->builtin_double_complex);
1004 add (builtin->builtin_bool);
1005 add (builtin->builtin_decfloat);
1006 add (builtin->builtin_decdouble);
1007 add (builtin->builtin_declong);
1008 add (builtin->builtin_char16);
1009 add (builtin->builtin_char32);
1010 add (builtin->builtin_wchar);
1011
1012 lai->set_string_char_type (builtin->builtin_char);
1013 lai->set_bool_type (builtin->builtin_bool, "bool");
1014 }
1015
1016 /* See language.h. */
1017 struct type *lookup_transparent_type (const char *name) const override
1018 {
1019 return cp_lookup_transparent_type (name);
1020 }
1021
1022 /* See language.h. */
1023 std::unique_ptr<compile_instance> get_compile_instance () const override
1024 {
1025 return cplus_get_compile_context ();
1026 }
1027
1028 /* See language.h. */
1029 std::string compute_program (compile_instance *inst,
1030 const char *input,
1031 struct gdbarch *gdbarch,
1032 const struct block *expr_block,
1033 CORE_ADDR expr_pc) const override
1034 {
1035 return cplus_compute_program (inst, input, gdbarch, expr_block, expr_pc);
1036 }
1037
1038 /* See language.h. */
1039 unsigned int search_name_hash (const char *name) const override
1040 {
1041 return cp_search_name_hash (name);
1042 }
1043
1044 /* See language.h. */
1045 bool sniff_from_mangled_name (const char *mangled,
1046 char **demangled) const override
1047 {
1048 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
1049 return *demangled != NULL;
1050 }
1051
1052 /* See language.h. */
1053
1054 char *demangle_symbol (const char *mangled, int options) const override
1055 {
1056 return gdb_demangle (mangled, options);
1057 }
1058
1059 /* See language.h. */
1060
1061 void print_type (struct type *type, const char *varstring,
1062 struct ui_file *stream, int show, int level,
1063 const struct type_print_options *flags) const override
1064 {
1065 c_print_type (type, varstring, stream, show, level, flags);
1066 }
1067
1068 /* See language.h. */
1069
1070 CORE_ADDR skip_trampoline (struct frame_info *fi,
1071 CORE_ADDR pc) const override
1072 {
1073 return cplus_skip_trampoline (fi, pc);
1074 }
1075
1076 /* See language.h. */
1077
1078 char *class_name_from_physname (const char *physname) const override
1079 {
1080 return cp_class_name_from_physname (physname);
1081 }
1082
1083 /* See language.h. */
1084
1085 struct block_symbol lookup_symbol_nonlocal
1086 (const char *name, const struct block *block,
1087 const domain_enum domain) const override
1088 {
1089 return cp_lookup_symbol_nonlocal (this, name, block, domain);
1090 }
1091
1092 /* See language.h. */
1093
1094 const char *name_of_this () const override
1095 { return "this"; }
1096
1097 /* See language.h. */
1098
1099 enum macro_expansion macro_expansion () const override
1100 { return macro_expansion_c; }
1101
1102 /* See language.h. */
1103
1104 const struct lang_varobj_ops *varobj_ops () const override
1105 { return &cplus_varobj_ops; }
1106
1107 /* See language.h. */
1108
1109 const struct exp_descriptor *expression_ops () const override
1110 { return &exp_descriptor_c; }
1111
1112 /* See language.h. */
1113
1114 const struct op_print *opcode_print_table () const override
1115 { return c_op_print_tab; }
1116
1117 protected:
1118
1119 /* See language.h. */
1120
1121 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
1122 (const lookup_name_info &lookup_name) const override
1123 {
1124 return cp_get_symbol_name_matcher (lookup_name);
1125 }
1126 };
1127
1128 /* The single instance of the C++ language class. */
1129
1130 static cplus_language cplus_language_defn;
1131
1132 /* A class for the ASM language. */
1133
1134 class asm_language : public language_defn
1135 {
1136 public:
1137 asm_language ()
1138 : language_defn (language_asm)
1139 { /* Nothing. */ }
1140
1141 /* See language.h. */
1142
1143 const char *name () const override
1144 { return "asm"; }
1145
1146 /* See language.h. */
1147
1148 const char *natural_name () const override
1149 { return "Assembly"; }
1150
1151 /* See language.h. */
1152
1153 const std::vector<const char *> &filename_extensions () const override
1154 {
1155 static const std::vector<const char *> extensions
1156 = { ".s", ".sx", ".S" };
1157 return extensions;
1158 }
1159
1160 /* See language.h.
1161
1162 FIXME: Should this have its own arch info method? */
1163 void language_arch_info (struct gdbarch *gdbarch,
1164 struct language_arch_info *lai) const override
1165 {
1166 c_language_arch_info (gdbarch, lai);
1167 }
1168
1169 /* See language.h. */
1170
1171 void print_type (struct type *type, const char *varstring,
1172 struct ui_file *stream, int show, int level,
1173 const struct type_print_options *flags) const override
1174 {
1175 c_print_type (type, varstring, stream, show, level, flags);
1176 }
1177
1178 /* See language.h. */
1179
1180 bool store_sym_names_in_linkage_form_p () const override
1181 { return true; }
1182
1183 /* See language.h. */
1184
1185 enum macro_expansion macro_expansion () const override
1186 { return macro_expansion_c; }
1187
1188 /* See language.h. */
1189
1190 const struct exp_descriptor *expression_ops () const override
1191 { return &exp_descriptor_c; }
1192
1193 /* See language.h. */
1194
1195 const struct op_print *opcode_print_table () const override
1196 { return c_op_print_tab; }
1197 };
1198
1199 /* The single instance of the ASM language class. */
1200 static asm_language asm_language_defn;
1201
1202 /* A class for the minimal language. This does not represent a real
1203 language. It just provides a minimal support a-la-C that should allow
1204 users to do some simple operations when debugging applications that use
1205 a language currently not supported by GDB. */
1206
1207 class minimal_language : public language_defn
1208 {
1209 public:
1210 minimal_language ()
1211 : language_defn (language_minimal)
1212 { /* Nothing. */ }
1213
1214 /* See language.h. */
1215
1216 const char *name () const override
1217 { return "minimal"; }
1218
1219 /* See language.h. */
1220
1221 const char *natural_name () const override
1222 { return "Minimal"; }
1223
1224 /* See language.h. */
1225 void language_arch_info (struct gdbarch *gdbarch,
1226 struct language_arch_info *lai) const override
1227 {
1228 c_language_arch_info (gdbarch, lai);
1229 }
1230
1231 /* See language.h. */
1232
1233 void print_type (struct type *type, const char *varstring,
1234 struct ui_file *stream, int show, int level,
1235 const struct type_print_options *flags) const override
1236 {
1237 c_print_type (type, varstring, stream, show, level, flags);
1238 }
1239
1240 /* See language.h. */
1241
1242 bool store_sym_names_in_linkage_form_p () const override
1243 { return true; }
1244
1245 /* See language.h. */
1246
1247 enum macro_expansion macro_expansion () const override
1248 { return macro_expansion_c; }
1249
1250 /* See language.h. */
1251
1252 const struct exp_descriptor *expression_ops () const override
1253 { return &exp_descriptor_c; }
1254
1255 /* See language.h. */
1256
1257 const struct op_print *opcode_print_table () const override
1258 { return c_op_print_tab; }
1259 };
1260
1261 /* The single instance of the minimal language class. */
1262 static minimal_language minimal_language_defn;