gdb: move go_language class declaration into header file
[deliverable/binutils-gdb.git] / gdb / c-lang.c
CommitLineData
c906108c 1/* C language support routines for GDB, the GNU debugger.
ce27fb25 2
b811d2c2 3 Copyright (C) 1992-2020 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
SS
19
20#include "defs.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"
6c7a06a3 35#include "gdb_obstack.h"
4de283e4 36#include <ctype.h>
578d3588 37#include "gdbcore.h"
0d12e84c 38#include "gdbarch.h"
c906108c 39
8e25bafe
AB
40class compile_instance;
41
6c7a06a3
TT
42/* Given a C string type, STR_TYPE, return the corresponding target
43 character set name. */
44
45static const char *
0c801b96 46charset_for_string_type (c_string_type str_type, struct gdbarch *gdbarch)
6c7a06a3
TT
47{
48 switch (str_type & ~C_CHAR)
49 {
50 case C_STRING:
f870a310 51 return target_charset (gdbarch);
6c7a06a3 52 case C_WIDE_STRING:
f870a310 53 return target_wide_charset (gdbarch);
6c7a06a3 54 case C_STRING_16:
b8899f2b 55 /* FIXME: UTF-16 is not always correct. */
f870a310 56 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 57 return "UTF-16BE";
6c7a06a3 58 else
b8899f2b 59 return "UTF-16LE";
6c7a06a3 60 case C_STRING_32:
b8899f2b 61 /* FIXME: UTF-32 is not always correct. */
f870a310 62 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 63 return "UTF-32BE";
6c7a06a3 64 else
b8899f2b 65 return "UTF-32LE";
6c7a06a3 66 }
9b20d036 67 internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
6c7a06a3
TT
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
aff410f1
MS
73 characters of this type in target BYTE_ORDER to the host character
74 set. */
6c7a06a3 75
0c801b96 76static c_string_type
f870a310 77classify_type (struct type *elttype, struct gdbarch *gdbarch,
e17a4113 78 const char **encoding)
6c7a06a3 79{
0c801b96 80 c_string_type result;
6c7a06a3 81
85e306ed
TT
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)
6c7a06a3 88 {
7d93a1e0 89 const char *name = elttype->name ();
6c7a06a3 90
78134374 91 if (elttype->code () == TYPE_CODE_CHAR || !name)
6c7a06a3
TT
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
78134374 115 if (elttype->code () != TYPE_CODE_TYPEDEF)
85e306ed
TT
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++. */
f168693b 128 elttype = check_typedef (elttype);
85e306ed 129 }
6c7a06a3 130 }
6c7a06a3
TT
131
132 /* Punt. */
133 result = C_CHAR;
134
135 done:
e17a4113 136 if (encoding)
f870a310 137 *encoding = charset_for_string_type (result, gdbarch);
e17a4113 138
6c7a06a3
TT
139 return result;
140}
141
aff410f1
MS
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. */
c906108c 145
6aecb9c2
JB
146void
147c_emit_char (int c, struct type *type,
148 struct ui_file *stream, int quoter)
c906108c 149{
6c7a06a3 150 const char *encoding;
234b45d4 151
f870a310 152 classify_type (type, get_type_arch (type), &encoding);
3b2b8fea 153 generic_emit_char (c, type, stream, quoter, encoding);
c906108c
SS
154}
155
c5ee319e
AB
156/* See language.h. */
157
c906108c 158void
c5ee319e
AB
159language_defn::printchar (int c, struct type *type,
160 struct ui_file * stream) const
c906108c 161{
0c801b96 162 c_string_type str_type;
6c7a06a3 163
f870a310 164 str_type = classify_type (type, get_type_arch (type), NULL);
6c7a06a3
TT
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
c906108c 180 fputc_filtered ('\'', stream);
76ca72bc 181 emitchar (c, type, stream, '\'');
c906108c
SS
182 fputc_filtered ('\'', stream);
183}
184
aff410f1
MS
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. */
c906108c
SS
191
192void
aff410f1
MS
193c_printstr (struct ui_file *stream, struct type *type,
194 const gdb_byte *string, unsigned int length,
195 const char *user_encoding, int force_ellipses,
79a45b7d 196 const struct value_print_options *options)
c906108c 197{
0c801b96 198 c_string_type str_type;
3b2b8fea
TT
199 const char *type_encoding;
200 const char *encoding;
201
f870a310
TT
202 str_type = (classify_type (type, get_type_arch (type), &type_encoding)
203 & ~C_CHAR);
6c7a06a3
TT
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
3b2b8fea 219 encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;
6c7a06a3 220
3b2b8fea
TT
221 generic_printstr (stream, type, string, length, encoding, force_ellipses,
222 '"', 1, options);
c906108c 223}
ae6a3a4c
TJB
224
225/* Obtain a C string from the inferior storing it in a newly allocated
aff410f1
MS
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
fbb8f299 228 until a null character of the appropriate width is found, otherwise
aff410f1
MS
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
0987cf35
DE
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
fbb8f299
PM
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. */
ae6a3a4c
TJB
241
242void
b4be9fad 243c_get_string (struct value *value, gdb::unique_xmalloc_ptr<gdb_byte> *buffer,
aff410f1
MS
244 int *length, struct type **char_type,
245 const char **charset)
ae6a3a4c
TJB
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);
fbb8f299 251 int req_length = *length;
aff410f1 252 enum bfd_endian byte_order
34877895 253 = type_byte_order (type);
ae6a3a4c
TJB
254
255 if (element_type == NULL)
256 goto error;
257
78134374 258 if (type->code () == TYPE_CODE_ARRAY)
ae6a3a4c 259 {
aff410f1
MS
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. */
1f704f76 262 if (type->num_fields () == 1
940da03e 263 && type->field (0).type ()->code () == TYPE_CODE_RANGE)
ae6a3a4c
TJB
264 {
265 LONGEST low_bound, high_bound;
266
940da03e 267 get_discrete_bounds (type->field (0).type (),
ae6a3a4c
TJB
268 &low_bound, &high_bound);
269 fetchlimit = high_bound - low_bound + 1;
270 }
271 else
272 fetchlimit = UINT_MAX;
273 }
78134374 274 else if (type->code () == TYPE_CODE_PTR)
ae6a3a4c
TJB
275 fetchlimit = UINT_MAX;
276 else
277 /* We work only with arrays and pointers. */
278 goto error;
279
96c07c5b 280 if (! c_textual_element_type (element_type, 0))
ae6a3a4c 281 goto error;
df54f8eb 282 classify_type (element_type, get_type_arch (element_type), charset);
ae6a3a4c
TJB
283 width = TYPE_LENGTH (element_type);
284
aff410f1
MS
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
80e55b13
TT
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. */
ae6a3a4c 298 if ((VALUE_LVAL (value) == not_lval
80e55b13 299 || VALUE_LVAL (value) == lval_internalvar
78134374 300 || type->code () == TYPE_CODE_ARRAY)
80e55b13
TT
301 && fetchlimit != UINT_MAX
302 && (*length < 0 || *length <= fetchlimit))
ae6a3a4c
TJB
303 {
304 int i;
305 const gdb_byte *contents = value_contents (value);
306
fbb8f299
PM
307 /* If a length is specified, use that. */
308 if (*length >= 0)
309 i = *length;
310 else
e623f035
SM
311 /* Otherwise, look for a null character. */
312 for (i = 0; i < fetchlimit; i++)
aff410f1
MS
313 if (extract_unsigned_integer (contents + i * width,
314 width, byte_order) == 0)
e623f035 315 break;
fbb8f299
PM
316
317 /* I is now either a user-defined length, the number of non-null
e623f035 318 characters, or FETCHLIMIT. */
ae6a3a4c 319 *length = i * width;
b4be9fad
TT
320 buffer->reset ((gdb_byte *) xmalloc (*length));
321 memcpy (buffer->get (), contents, *length);
ae6a3a4c
TJB
322 err = 0;
323 }
324 else
325 {
80e55b13
TT
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;
78134374 330 if (type->code () == TYPE_CODE_ARRAY)
80e55b13
TT
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);
621c8364 339
0987cf35
DE
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
621c8364
TT
355 err = read_string (addr, *length, width, fetchlimit,
356 byte_order, buffer, length);
d09f2c3f 357 if (err != 0)
b4be9fad 358 memory_error (TARGET_XFER_E_IO, addr);
ae6a3a4c
TJB
359 }
360
fbb8f299
PM
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
b4be9fad 368 && extract_unsigned_integer (buffer->get () + *length - width,
aff410f1 369 width, byte_order) == 0)
fbb8f299
PM
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;
ae6a3a4c 377
96c07c5b 378 *char_type = element_type;
ae6a3a4c
TJB
379
380 return;
381
382 error:
383 {
2f408ecb
PA
384 std::string type_str = type_to_string (type);
385 if (!type_str.empty ())
ae6a3a4c 386 {
ae6a3a4c 387 error (_("Trying to read string with inappropriate type `%s'."),
2f408ecb 388 type_str.c_str ());
ae6a3a4c
TJB
389 }
390 else
391 error (_("Trying to read string with inappropriate type."));
392 }
393}
394
c906108c 395\f
6c7a06a3
TT
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
e8b2f0d9
TT
404static const char *
405convert_ucn (const char *p, const char *limit, const char *dest_charset,
6c7a06a3
TT
406 struct obstack *output, int length)
407{
408 unsigned long result = 0;
409 gdb_byte data[4];
410 int i;
411
b1b60145 412 for (i = 0; i < length && p < limit && ISXDIGIT (*p); ++i, ++p)
6c7a06a3
TT
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
aff410f1
MS
421 convert_between_encodings ("UTF-32BE", dest_charset, data,
422 4, 4, output, translit_none);
6c7a06a3
TT
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
430static void
431emit_numeric_character (struct type *type, unsigned long value,
432 struct obstack *output)
433{
434 gdb_byte *buffer;
435
224c3ddb 436 buffer = (gdb_byte *) alloca (TYPE_LENGTH (type));
6c7a06a3
TT
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
e8b2f0d9
TT
446static const char *
447convert_octal (struct type *type, const char *p,
448 const char *limit, struct obstack *output)
6c7a06a3 449{
30b66ecc 450 int i;
6c7a06a3
TT
451 unsigned long value = 0;
452
30b66ecc 453 for (i = 0;
b1b60145 454 i < 3 && p < limit && ISDIGIT (*p) && *p != '8' && *p != '9';
30b66ecc 455 ++i)
6c7a06a3
TT
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
e8b2f0d9
TT
471static const char *
472convert_hex (struct type *type, const char *p,
473 const char *limit, struct obstack *output)
6c7a06a3
TT
474{
475 unsigned long value = 0;
476
b1b60145 477 while (p < limit && ISXDIGIT (*p))
6c7a06a3
TT
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
e8b2f0d9 502static const char *
6c7a06a3 503convert_escape (struct type *type, const char *dest_charset,
e8b2f0d9 504 const char *p, const char *limit, struct obstack *output)
6c7a06a3
TT
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;
b1b60145 518 if (!ISXDIGIT (*p))
6c7a06a3
TT
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;
c5504eaf 538
6c7a06a3 539 ADVANCE;
b1b60145 540 if (!ISXDIGIT (*p))
6c7a06a3
TT
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
555static void
e8b2f0d9 556parse_one_string (struct obstack *output, const char *data, int len,
6c7a06a3
TT
557 const char *dest_charset, struct type *type)
558{
e8b2f0d9 559 const char *limit;
6c7a06a3
TT
560
561 limit = data + len;
562
563 while (data < limit)
564 {
e8b2f0d9 565 const char *p = data;
c5504eaf 566
6c7a06a3
TT
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,
e8b2f0d9 573 (const gdb_byte *) data, p - data, 1,
aff410f1 574 output, translit_none);
6c7a06a3
TT
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
f4b8a18d 586struct value *
6c7a06a3
TT
587evaluate_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;
6c7a06a3 598 struct value *result;
0c801b96 599 c_string_type dest_type;
6c7a06a3 600 const char *dest_charset;
c50491a7 601 int satisfy_expected = 0;
6c7a06a3 602
8268c778 603 auto_obstack output;
6c7a06a3
TT
604
605 ++*pos;
606 oplen = longest_to_int (exp->elts[*pos].longconst);
607
608 ++*pos;
609 limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1);
0c801b96
SM
610 dest_type = ((enum c_string_type_values)
611 longest_to_int (exp->elts[*pos].longconst));
6c7a06a3
TT
612 switch (dest_type & ~C_CHAR)
613 {
614 case C_STRING:
d80b854b
UW
615 type = language_string_char_type (exp->language_defn,
616 exp->gdbarch);
6c7a06a3
TT
617 break;
618 case C_WIDE_STRING:
b858499d 619 type = lookup_typename (exp->language_defn, "wchar_t", NULL, 0);
6c7a06a3
TT
620 break;
621 case C_STRING_16:
b858499d 622 type = lookup_typename (exp->language_defn, "char16_t", NULL, 0);
6c7a06a3
TT
623 break;
624 case C_STRING_32:
b858499d 625 type = lookup_typename (exp->language_defn, "char32_t", NULL, 0);
6c7a06a3
TT
626 break;
627 default:
9b20d036 628 internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
6c7a06a3 629 }
546e879e
TT
630
631 /* Ensure TYPE_LENGTH is valid for TYPE. */
632 check_typedef (type);
633
c50491a7
TT
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. */
78134374 637 if (expect_type && expect_type->code () == TYPE_CODE_ARRAY)
c50491a7
TT
638 {
639 struct type *element_type
640 = check_typedef (TYPE_TARGET_TYPE (expect_type));
641
78134374
SM
642 if (element_type->code () == TYPE_CODE_INT
643 || element_type->code () == TYPE_CODE_CHAR)
c50491a7
TT
644 {
645 type = element_type;
646 satisfy_expected = 1;
647 }
648 }
649
f870a310 650 dest_charset = charset_for_string_type (dest_type, exp->gdbarch);
6c7a06a3
TT
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)
334cc82d
TT
670 {
671 /* Return a dummy value of the appropriate type. */
c50491a7
TT
672 if (expect_type != NULL)
673 result = allocate_value (expect_type);
674 else if ((dest_type & C_CHAR) != 0)
334cc82d
TT
675 result = allocate_value (type);
676 else
3b7538c0 677 result = value_cstring ("", 0, type);
334cc82d
TT
678 return result;
679 }
6c7a06a3
TT
680
681 if ((dest_type & C_CHAR) != 0)
682 {
683 LONGEST value;
684
685 if (obstack_object_size (&output) != TYPE_LENGTH (type))
3e43a32a
MS
686 error (_("Could not convert character "
687 "constant to target character set"));
51a5cd90 688 value = unpack_long (type, (gdb_byte *) obstack_base (&output));
6c7a06a3
TT
689 result = value_from_longest (type, value);
690 }
691 else
692 {
693 int i;
c5504eaf 694
6c7a06a3
TT
695 /* Write the terminating character. */
696 for (i = 0; i < TYPE_LENGTH (type); ++i)
697 obstack_1grow (&output, 0);
c50491a7
TT
698
699 if (satisfy_expected)
700 {
701 LONGEST low_bound, high_bound;
702 int element_size = TYPE_LENGTH (type);
703
1f8d2881
SM
704 if (!get_discrete_bounds (expect_type->index_type (),
705 &low_bound, &high_bound))
c50491a7
TT
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
79f33898 719 result = value_cstring ((const char *) obstack_base (&output),
c50491a7
TT
720 obstack_object_size (&output),
721 type);
6c7a06a3 722 }
6c7a06a3
TT
723 return result;
724 }
725 break;
726
727 default:
728 break;
729 }
730 return evaluate_subexp_standard (expect_type, exp, pos, noside);
731}
43cc5389 732\f
4be290b2
AB
733/* See c-lang.h. */
734
735bool
736c_is_string_type_p (struct type *type)
737{
738 type = check_typedef (type);
78134374 739 while (type->code () == TYPE_CODE_REF)
4be290b2
AB
740 {
741 type = TYPE_TARGET_TYPE (type);
742 type = check_typedef (type);
743 }
744
78134374 745 switch (type->code ())
4be290b2
AB
746 {
747 case TYPE_CODE_ARRAY:
748 {
749 /* See if target type looks like a string. */
750 struct type *array_target_type = TYPE_TARGET_TYPE (type);
751 return (TYPE_LENGTH (type) > 0
752 && TYPE_LENGTH (array_target_type) > 0
753 && c_textual_element_type (array_target_type, 0));
754 }
755 case TYPE_CODE_STRING:
756 return true;
757 case TYPE_CODE_PTR:
758 {
759 struct type *element_type = TYPE_TARGET_TYPE (type);
760 return c_textual_element_type (element_type, 0);
761 }
762 default:
763 break;
764 }
765
766 return false;
767}
768
84f0252a 769\f
c906108c
SS
770/* Table mapping opcodes into strings for printing operators
771 and precedences of the operators. */
772
773const struct op_print c_op_print_tab[] =
c5aa993b
JM
774{
775 {",", BINOP_COMMA, PREC_COMMA, 0},
776 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
777 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
778 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
779 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
780 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
781 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
782 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
783 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
784 {"<=", BINOP_LEQ, PREC_ORDER, 0},
785 {">=", BINOP_GEQ, PREC_ORDER, 0},
786 {">", BINOP_GTR, PREC_ORDER, 0},
787 {"<", BINOP_LESS, PREC_ORDER, 0},
788 {">>", BINOP_RSH, PREC_SHIFT, 0},
789 {"<<", BINOP_LSH, PREC_SHIFT, 0},
790 {"+", BINOP_ADD, PREC_ADD, 0},
791 {"-", BINOP_SUB, PREC_ADD, 0},
792 {"*", BINOP_MUL, PREC_MUL, 0},
793 {"/", BINOP_DIV, PREC_MUL, 0},
794 {"%", BINOP_REM, PREC_MUL, 0},
795 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
a016fc87 796 {"+", UNOP_PLUS, PREC_PREFIX, 0},
c5aa993b
JM
797 {"-", UNOP_NEG, PREC_PREFIX, 0},
798 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
799 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
800 {"*", UNOP_IND, PREC_PREFIX, 0},
801 {"&", UNOP_ADDR, PREC_PREFIX, 0},
802 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
007e1530 803 {"alignof ", UNOP_ALIGNOF, PREC_PREFIX, 0},
c5aa993b
JM
804 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
805 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
f486487f 806 {NULL, OP_NULL, PREC_PREFIX, 0}
c906108c
SS
807};
808\f
685419e2 809
e9667a65 810void
685419e2
AC
811c_language_arch_info (struct gdbarch *gdbarch,
812 struct language_arch_info *lai)
813{
814 const struct builtin_type *builtin = builtin_type (gdbarch);
c5504eaf 815
7bea47f0
AB
816 /* Helper function to allow shorter lines below. */
817 auto add = [&] (struct type * t)
818 {
819 lai->add_primitive_type (t);
820 };
821
822 add (builtin->builtin_int);
823 add (builtin->builtin_long);
824 add (builtin->builtin_short);
825 add (builtin->builtin_char);
826 add (builtin->builtin_float);
827 add (builtin->builtin_double);
828 add (builtin->builtin_void);
829 add (builtin->builtin_long_long);
830 add (builtin->builtin_signed_char);
831 add (builtin->builtin_unsigned_char);
832 add (builtin->builtin_unsigned_short);
833 add (builtin->builtin_unsigned_int);
834 add (builtin->builtin_unsigned_long);
835 add (builtin->builtin_unsigned_long_long);
836 add (builtin->builtin_long_double);
837 add (builtin->builtin_complex);
838 add (builtin->builtin_double_complex);
839 add (builtin->builtin_decfloat);
840 add (builtin->builtin_decdouble);
841 add (builtin->builtin_declong);
842
843 lai->set_string_char_type (builtin->builtin_char);
844 lai->set_bool_type (builtin->builtin_int);
cad351d1 845}
685419e2 846
6aecb9c2 847const struct exp_descriptor exp_descriptor_c =
6c7a06a3
TT
848{
849 print_subexp_standard,
850 operator_length_standard,
c0201579 851 operator_check_standard,
6c7a06a3
TT
852 dump_subexp_body_standard,
853 evaluate_subexp_c
854};
855
0874fd07
AB
856/* Class representing the C language. */
857
858class c_language : public language_defn
859{
860public:
861 c_language ()
0e25e767 862 : language_defn (language_c)
0874fd07 863 { /* Nothing. */ }
1fb314aa 864
6f7664a9
AB
865 /* See language.h. */
866
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
e171d6f1
AB
875 /* See language.h. */
876
877 const std::vector<const char *> &filename_extensions () const override
878 {
879 static const std::vector<const char *> extensions = { ".c" };
880 return extensions;
881 }
882
1fb314aa
AB
883 /* See language.h. */
884 void language_arch_info (struct gdbarch *gdbarch,
885 struct language_arch_info *lai) const override
886 {
887 c_language_arch_info (gdbarch, lai);
888 }
8e25bafe
AB
889
890 /* See language.h. */
891 compile_instance *get_compile_instance () const override
892 {
893 return c_get_compile_context ();
894 }
fbfb0a46 895
9a49ad8c
AB
896 /* See language.h. */
897 std::string compute_program (compile_instance *inst,
898 const char *input,
899 struct gdbarch *gdbarch,
900 const struct block *expr_block,
901 CORE_ADDR expr_pc) const override
902 {
903 return c_compute_program (inst, input, gdbarch, expr_block, expr_pc);
904 }
905
fbfb0a46
AB
906 /* See language.h. */
907
908 void print_type (struct type *type, const char *varstring,
909 struct ui_file *stream, int show, int level,
910 const struct type_print_options *flags) const override
911 {
912 c_print_type (type, varstring, stream, show, level, flags);
913 }
d3355e4d
AB
914
915 /* See language.h. */
916
917 bool store_sym_names_in_linkage_form_p () const override
918 { return true; }
1ac14a04
AB
919
920 /* See language.h. */
921
922 enum macro_expansion macro_expansion () const override
923 { return macro_expansion_c; }
5aba6ebe
AB
924
925 /* See language.h. */
926
927 const struct exp_descriptor *expression_ops () const override
928 { return &exp_descriptor_c; }
b7c6e27d
AB
929
930 /* See language.h. */
931
932 const struct op_print *opcode_print_table () const override
933 { return c_op_print_tab; }
0874fd07
AB
934};
935
936/* Single instance of the C language class. */
937
938static c_language c_language_defn;
939
0874fd07
AB
940/* A class for the C++ language. */
941
942class cplus_language : public language_defn
943{
944public:
945 cplus_language ()
0e25e767 946 : language_defn (language_cplus)
0874fd07 947 { /* Nothing. */ }
48448202
AB
948
949 /* See language.h. */
950
6f7664a9
AB
951 const char *name () const override
952 { return "c++"; }
953
954 /* See language.h. */
955
956 const char *natural_name () const override
957 { return "C++"; }
958
959 /* See language.h. */
960
e171d6f1
AB
961 const std::vector<const char *> &filename_extensions () const override
962 {
963 static const std::vector<const char *> extensions
964 = { ".C", ".cc", ".cp", ".cpp", ".cxx", ".c++" };
965 return extensions;
966 }
967
968 /* See language.h. */
969
48448202
AB
970 struct language_pass_by_ref_info pass_by_reference_info
971 (struct type *type) const override
972 {
973 return cp_pass_by_reference (type);
974 }
1fb314aa
AB
975
976 /* See language.h. */
977 void language_arch_info (struct gdbarch *gdbarch,
978 struct language_arch_info *lai) const override
979 {
980 const struct builtin_type *builtin = builtin_type (gdbarch);
981
7bea47f0
AB
982 /* Helper function to allow shorter lines below. */
983 auto add = [&] (struct type * t)
984 {
985 lai->add_primitive_type (t);
986 };
987
988 add (builtin->builtin_int);
989 add (builtin->builtin_long);
990 add (builtin->builtin_short);
991 add (builtin->builtin_char);
992 add (builtin->builtin_float);
993 add (builtin->builtin_double);
994 add (builtin->builtin_void);
995 add (builtin->builtin_long_long);
996 add (builtin->builtin_signed_char);
997 add (builtin->builtin_unsigned_char);
998 add (builtin->builtin_unsigned_short);
999 add (builtin->builtin_unsigned_int);
1000 add (builtin->builtin_unsigned_long);
1001 add (builtin->builtin_unsigned_long_long);
1002 add (builtin->builtin_long_double);
1003 add (builtin->builtin_complex);
1004 add (builtin->builtin_double_complex);
1005 add (builtin->builtin_bool);
1006 add (builtin->builtin_decfloat);
1007 add (builtin->builtin_decdouble);
1008 add (builtin->builtin_declong);
1009 add (builtin->builtin_char16);
1010 add (builtin->builtin_char32);
1011 add (builtin->builtin_wchar);
1012
1013 lai->set_string_char_type (builtin->builtin_char);
1014 lai->set_bool_type (builtin->builtin_bool, "bool");
1fb314aa 1015 }
54f4ca46
AB
1016
1017 /* See language.h. */
1018 struct type *lookup_transparent_type (const char *name) const override
1019 {
1020 return cp_lookup_transparent_type (name);
1021 }
8e25bafe
AB
1022
1023 /* See language.h. */
1024 compile_instance *get_compile_instance () const override
1025 {
1026 return cplus_get_compile_context ();
1027 }
fb8006fd 1028
9a49ad8c
AB
1029 /* See language.h. */
1030 std::string compute_program (compile_instance *inst,
1031 const char *input,
1032 struct gdbarch *gdbarch,
1033 const struct block *expr_block,
1034 CORE_ADDR expr_pc) const override
1035 {
1036 return cplus_compute_program (inst, input, gdbarch, expr_block, expr_pc);
1037 }
1038
fb8006fd
AB
1039 /* See language.h. */
1040 unsigned int search_name_hash (const char *name) const override
1041 {
1042 return cp_search_name_hash (name);
1043 }
6f827019
AB
1044
1045 /* See language.h. */
1046 bool sniff_from_mangled_name (const char *mangled,
1047 char **demangled) const override
1048 {
1049 *demangled = gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
1050 return *demangled != NULL;
1051 }
fbfb0a46
AB
1052
1053 /* See language.h. */
1054
5399db93 1055 char *demangle_symbol (const char *mangled, int options) const override
0a50df5d
AB
1056 {
1057 return gdb_demangle (mangled, options);
1058 }
1059
1060 /* See language.h. */
1061
fbfb0a46
AB
1062 void print_type (struct type *type, const char *varstring,
1063 struct ui_file *stream, int show, int level,
1064 const struct type_print_options *flags) const override
1065 {
1066 c_print_type (type, varstring, stream, show, level, flags);
1067 }
f6eee2d0
AB
1068
1069 /* See language.h. */
1070
1071 CORE_ADDR skip_trampoline (struct frame_info *fi,
1072 CORE_ADDR pc) const override
1073 {
1074 return cplus_skip_trampoline (fi, pc);
1075 }
eff93b4d
AB
1076
1077 /* See language.h. */
1078
1079 char *class_name_from_physname (const char *physname) const override
1080 {
1081 return cp_class_name_from_physname (physname);
1082 }
c9debfb9 1083
a78a19b1
AB
1084 /* See language.h. */
1085
1086 struct block_symbol lookup_symbol_nonlocal
1087 (const char *name, const struct block *block,
1088 const domain_enum domain) const override
1089 {
1090 return cp_lookup_symbol_nonlocal (this, name, block, domain);
1091 }
1092
5bae7c4e
AB
1093 /* See language.h. */
1094
1095 const char *name_of_this () const override
1096 { return "this"; }
1097
1ac14a04
AB
1098 /* See language.h. */
1099
1100 enum macro_expansion macro_expansion () const override
1101 { return macro_expansion_c; }
1102
b63a3f3f
AB
1103 /* See language.h. */
1104
1105 const struct lang_varobj_ops *varobj_ops () const override
1106 { return &cplus_varobj_ops; }
1107
5aba6ebe
AB
1108 /* See language.h. */
1109
1110 const struct exp_descriptor *expression_ops () const override
1111 { return &exp_descriptor_c; }
1112
b7c6e27d
AB
1113 /* See language.h. */
1114
1115 const struct op_print *opcode_print_table () const override
1116 { return c_op_print_tab; }
1117
c9debfb9
AB
1118protected:
1119
1120 /* See language.h. */
1121
1122 symbol_name_matcher_ftype *get_symbol_name_matcher_inner
1123 (const lookup_name_info &lookup_name) const override
1124 {
1125 return cp_get_symbol_name_matcher (lookup_name);
1126 }
0874fd07
AB
1127};
1128
1129/* The single instance of the C++ language class. */
1130
1131static cplus_language cplus_language_defn;
1132
0874fd07
AB
1133/* A class for the ASM language. */
1134
1135class asm_language : public language_defn
1136{
1137public:
1138 asm_language ()
0e25e767 1139 : language_defn (language_asm)
0874fd07 1140 { /* Nothing. */ }
1fb314aa 1141
6f7664a9
AB
1142 /* See language.h. */
1143
1144 const char *name () const override
1145 { return "asm"; }
1146
1147 /* See language.h. */
1148
1149 const char *natural_name () const override
1150 { return "Assembly"; }
1151
e171d6f1
AB
1152 /* See language.h. */
1153
1154 const std::vector<const char *> &filename_extensions () const override
1155 {
1156 static const std::vector<const char *> extensions
1157 = { ".s", ".sx", ".S" };
1158 return extensions;
1159 }
1160
1fb314aa
AB
1161 /* See language.h.
1162
1163 FIXME: Should this have its own arch info method? */
1164 void language_arch_info (struct gdbarch *gdbarch,
1165 struct language_arch_info *lai) const override
1166 {
1167 c_language_arch_info (gdbarch, lai);
1168 }
fbfb0a46
AB
1169
1170 /* See language.h. */
1171
1172 void print_type (struct type *type, const char *varstring,
1173 struct ui_file *stream, int show, int level,
1174 const struct type_print_options *flags) const override
1175 {
1176 c_print_type (type, varstring, stream, show, level, flags);
1177 }
d3355e4d
AB
1178
1179 /* See language.h. */
1180
1181 bool store_sym_names_in_linkage_form_p () const override
1182 { return true; }
1ac14a04
AB
1183
1184 /* See language.h. */
1185
1186 enum macro_expansion macro_expansion () const override
1187 { return macro_expansion_c; }
5aba6ebe
AB
1188
1189 /* See language.h. */
1190
1191 const struct exp_descriptor *expression_ops () const override
1192 { return &exp_descriptor_c; }
b7c6e27d
AB
1193
1194 /* See language.h. */
1195
1196 const struct op_print *opcode_print_table () const override
1197 { return c_op_print_tab; }
0874fd07
AB
1198};
1199
1200/* The single instance of the ASM language class. */
1201static asm_language asm_language_defn;
1202
0e25e767
AB
1203/* A class for the minimal language. This does not represent a real
1204 language. It just provides a minimal support a-la-C that should allow
1205 users to do some simple operations when debugging applications that use
20a0e81d
JB
1206 a language currently not supported by GDB. */
1207
0874fd07
AB
1208class minimal_language : public language_defn
1209{
1210public:
1211 minimal_language ()
0e25e767 1212 : language_defn (language_minimal)
0874fd07 1213 { /* Nothing. */ }
1fb314aa 1214
6f7664a9
AB
1215 /* See language.h. */
1216
1217 const char *name () const override
1218 { return "minimal"; }
1219
1220 /* See language.h. */
1221
1222 const char *natural_name () const override
1223 { return "Minimal"; }
1224
1fb314aa
AB
1225 /* See language.h. */
1226 void language_arch_info (struct gdbarch *gdbarch,
1227 struct language_arch_info *lai) const override
1228 {
1229 c_language_arch_info (gdbarch, lai);
1230 }
fbfb0a46
AB
1231
1232 /* See language.h. */
1233
1234 void print_type (struct type *type, const char *varstring,
1235 struct ui_file *stream, int show, int level,
1236 const struct type_print_options *flags) const override
1237 {
1238 c_print_type (type, varstring, stream, show, level, flags);
1239 }
d3355e4d
AB
1240
1241 /* See language.h. */
1242
1243 bool store_sym_names_in_linkage_form_p () const override
1244 { return true; }
1ac14a04
AB
1245
1246 /* See language.h. */
1247
1248 enum macro_expansion macro_expansion () const override
1249 { return macro_expansion_c; }
5aba6ebe
AB
1250
1251 /* See language.h. */
1252
1253 const struct exp_descriptor *expression_ops () const override
1254 { return &exp_descriptor_c; }
b7c6e27d
AB
1255
1256 /* See language.h. */
1257
1258 const struct op_print *opcode_print_table () const override
1259 { return c_op_print_tab; }
0874fd07
AB
1260};
1261
1262/* The single instance of the minimal language class. */
1263static minimal_language minimal_language_defn;
This page took 1.397992 seconds and 4 git commands to generate.