Eliminate make_cleanup_obstack_free, introduce auto_obstack
[deliverable/binutils-gdb.git] / gdb / c-lang.c
CommitLineData
c906108c 1/* C language support routines for GDB, the GNU debugger.
ce27fb25 2
61baf725 3 Copyright (C) 1992-2017 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"
21#include "symtab.h"
22#include "gdbtypes.h"
23#include "expression.h"
24#include "parser-defs.h"
25#include "language.h"
a53b64ea 26#include "varobj.h"
c906108c 27#include "c-lang.h"
745b8ca0 28#include "valprint.h"
84f0252a 29#include "macroscope.h"
234b45d4 30#include "charset.h"
9a3d7dfd 31#include "demangle.h"
b18be20d 32#include "cp-abi.h"
1fcb5155 33#include "cp-support.h"
6c7a06a3
TT
34#include "gdb_obstack.h"
35#include <ctype.h>
578d3588 36#include "gdbcore.h"
c906108c 37
a14ed312 38extern void _initialize_c_language (void);
6c7a06a3
TT
39
40/* Given a C string type, STR_TYPE, return the corresponding target
41 character set name. */
42
43static const char *
0c801b96 44charset_for_string_type (c_string_type str_type, struct gdbarch *gdbarch)
6c7a06a3
TT
45{
46 switch (str_type & ~C_CHAR)
47 {
48 case C_STRING:
f870a310 49 return target_charset (gdbarch);
6c7a06a3 50 case C_WIDE_STRING:
f870a310 51 return target_wide_charset (gdbarch);
6c7a06a3 52 case C_STRING_16:
b8899f2b 53 /* FIXME: UTF-16 is not always correct. */
f870a310 54 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 55 return "UTF-16BE";
6c7a06a3 56 else
b8899f2b 57 return "UTF-16LE";
6c7a06a3 58 case C_STRING_32:
b8899f2b 59 /* FIXME: UTF-32 is not always correct. */
f870a310 60 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
b8899f2b 61 return "UTF-32BE";
6c7a06a3 62 else
b8899f2b 63 return "UTF-32LE";
6c7a06a3 64 }
9b20d036 65 internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
6c7a06a3
TT
66}
67
68/* Classify ELTTYPE according to what kind of character it is. Return
69 the enum constant representing the character type. Also set
70 *ENCODING to the name of the character set to use when converting
aff410f1
MS
71 characters of this type in target BYTE_ORDER to the host character
72 set. */
6c7a06a3 73
0c801b96 74static c_string_type
f870a310 75classify_type (struct type *elttype, struct gdbarch *gdbarch,
e17a4113 76 const char **encoding)
6c7a06a3 77{
0c801b96 78 c_string_type result;
6c7a06a3 79
85e306ed
TT
80 /* We loop because ELTTYPE may be a typedef, and we want to
81 successively peel each typedef until we reach a type we
82 understand. We don't use CHECK_TYPEDEF because that will strip
83 all typedefs at once -- but in C, wchar_t is itself a typedef, so
84 that would do the wrong thing. */
85 while (elttype)
6c7a06a3 86 {
0d5cff50 87 const char *name = TYPE_NAME (elttype);
6c7a06a3
TT
88
89 if (TYPE_CODE (elttype) == TYPE_CODE_CHAR || !name)
90 {
91 result = C_CHAR;
92 goto done;
93 }
94
95 if (!strcmp (name, "wchar_t"))
96 {
97 result = C_WIDE_CHAR;
98 goto done;
99 }
100
101 if (!strcmp (name, "char16_t"))
102 {
103 result = C_CHAR_16;
104 goto done;
105 }
106
107 if (!strcmp (name, "char32_t"))
108 {
109 result = C_CHAR_32;
110 goto done;
111 }
112
85e306ed
TT
113 if (TYPE_CODE (elttype) != TYPE_CODE_TYPEDEF)
114 break;
115
116 /* Call for side effects. */
117 check_typedef (elttype);
118
119 if (TYPE_TARGET_TYPE (elttype))
120 elttype = TYPE_TARGET_TYPE (elttype);
121 else
122 {
123 /* Perhaps check_typedef did not update the target type. In
124 this case, force the lookup again and hope it works out.
125 It never will for C, but it might for C++. */
f168693b 126 elttype = check_typedef (elttype);
85e306ed 127 }
6c7a06a3 128 }
6c7a06a3
TT
129
130 /* Punt. */
131 result = C_CHAR;
132
133 done:
e17a4113 134 if (encoding)
f870a310 135 *encoding = charset_for_string_type (result, gdbarch);
e17a4113 136
6c7a06a3
TT
137 return result;
138}
139
aff410f1
MS
140/* Print the character C on STREAM as part of the contents of a
141 literal string whose delimiter is QUOTER. Note that that format
142 for printing characters and strings is language specific. */
c906108c 143
6aecb9c2
JB
144void
145c_emit_char (int c, struct type *type,
146 struct ui_file *stream, int quoter)
c906108c 147{
6c7a06a3 148 const char *encoding;
234b45d4 149
f870a310 150 classify_type (type, get_type_arch (type), &encoding);
3b2b8fea 151 generic_emit_char (c, type, stream, quoter, encoding);
c906108c
SS
152}
153
154void
6c7a06a3 155c_printchar (int c, struct type *type, struct ui_file *stream)
c906108c 156{
0c801b96 157 c_string_type str_type;
6c7a06a3 158
f870a310 159 str_type = classify_type (type, get_type_arch (type), NULL);
6c7a06a3
TT
160 switch (str_type)
161 {
162 case C_CHAR:
163 break;
164 case C_WIDE_CHAR:
165 fputc_filtered ('L', stream);
166 break;
167 case C_CHAR_16:
168 fputc_filtered ('u', stream);
169 break;
170 case C_CHAR_32:
171 fputc_filtered ('U', stream);
172 break;
173 }
174
c906108c 175 fputc_filtered ('\'', stream);
6c7a06a3 176 LA_EMIT_CHAR (c, type, stream, '\'');
c906108c
SS
177 fputc_filtered ('\'', stream);
178}
179
aff410f1
MS
180/* Print the character string STRING, printing at most LENGTH
181 characters. LENGTH is -1 if the string is nul terminated. Each
182 character is WIDTH bytes long. Printing stops early if the number
183 hits print_max; repeat counts are printed as appropriate. Print
184 ellipses at the end if we had to stop before printing LENGTH
185 characters, or if FORCE_ELLIPSES. */
c906108c
SS
186
187void
aff410f1
MS
188c_printstr (struct ui_file *stream, struct type *type,
189 const gdb_byte *string, unsigned int length,
190 const char *user_encoding, int force_ellipses,
79a45b7d 191 const struct value_print_options *options)
c906108c 192{
0c801b96 193 c_string_type str_type;
3b2b8fea
TT
194 const char *type_encoding;
195 const char *encoding;
196
f870a310
TT
197 str_type = (classify_type (type, get_type_arch (type), &type_encoding)
198 & ~C_CHAR);
6c7a06a3
TT
199 switch (str_type)
200 {
201 case C_STRING:
202 break;
203 case C_WIDE_STRING:
204 fputs_filtered ("L", stream);
205 break;
206 case C_STRING_16:
207 fputs_filtered ("u", stream);
208 break;
209 case C_STRING_32:
210 fputs_filtered ("U", stream);
211 break;
212 }
213
3b2b8fea 214 encoding = (user_encoding && *user_encoding) ? user_encoding : type_encoding;
6c7a06a3 215
3b2b8fea
TT
216 generic_printstr (stream, type, string, length, encoding, force_ellipses,
217 '"', 1, options);
c906108c 218}
ae6a3a4c
TJB
219
220/* Obtain a C string from the inferior storing it in a newly allocated
aff410f1
MS
221 buffer in BUFFER, which should be freed by the caller. If the in-
222 and out-parameter *LENGTH is specified at -1, the string is read
fbb8f299 223 until a null character of the appropriate width is found, otherwise
aff410f1
MS
224 the string is read to the length of characters specified. The size
225 of a character is determined by the length of the target type of
0987cf35
DE
226 the pointer or array.
227
228 If VALUE is an array with a known length, and *LENGTH is -1,
229 the function will not read past the end of the array. However, any
230 declared size of the array is ignored if *LENGTH > 0.
231
232 On completion, *LENGTH will be set to the size of the string read in
fbb8f299
PM
233 characters. (If a length of -1 is specified, the length returned
234 will not include the null character). CHARSET is always set to the
235 target charset. */
ae6a3a4c
TJB
236
237void
aff410f1
MS
238c_get_string (struct value *value, gdb_byte **buffer,
239 int *length, struct type **char_type,
240 const char **charset)
ae6a3a4c
TJB
241{
242 int err, width;
243 unsigned int fetchlimit;
244 struct type *type = check_typedef (value_type (value));
245 struct type *element_type = TYPE_TARGET_TYPE (type);
fbb8f299 246 int req_length = *length;
aff410f1
MS
247 enum bfd_endian byte_order
248 = gdbarch_byte_order (get_type_arch (type));
ae6a3a4c
TJB
249
250 if (element_type == NULL)
251 goto error;
252
253 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
254 {
aff410f1
MS
255 /* If we know the size of the array, we can use it as a limit on
256 the number of characters to be fetched. */
ae6a3a4c
TJB
257 if (TYPE_NFIELDS (type) == 1
258 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_RANGE)
259 {
260 LONGEST low_bound, high_bound;
261
262 get_discrete_bounds (TYPE_FIELD_TYPE (type, 0),
263 &low_bound, &high_bound);
264 fetchlimit = high_bound - low_bound + 1;
265 }
266 else
267 fetchlimit = UINT_MAX;
268 }
269 else if (TYPE_CODE (type) == TYPE_CODE_PTR)
270 fetchlimit = UINT_MAX;
271 else
272 /* We work only with arrays and pointers. */
273 goto error;
274
96c07c5b 275 if (! c_textual_element_type (element_type, 0))
ae6a3a4c 276 goto error;
df54f8eb 277 classify_type (element_type, get_type_arch (element_type), charset);
ae6a3a4c
TJB
278 width = TYPE_LENGTH (element_type);
279
aff410f1
MS
280 /* If the string lives in GDB's memory instead of the inferior's,
281 then we just need to copy it to BUFFER. Also, since such strings
282 are arrays with known size, FETCHLIMIT will hold the size of the
283 array. */
ae6a3a4c
TJB
284 if ((VALUE_LVAL (value) == not_lval
285 || VALUE_LVAL (value) == lval_internalvar)
286 && fetchlimit != UINT_MAX)
287 {
288 int i;
289 const gdb_byte *contents = value_contents (value);
290
fbb8f299
PM
291 /* If a length is specified, use that. */
292 if (*length >= 0)
293 i = *length;
294 else
295 /* Otherwise, look for a null character. */
296 for (i = 0; i < fetchlimit; i++)
aff410f1
MS
297 if (extract_unsigned_integer (contents + i * width,
298 width, byte_order) == 0)
fbb8f299
PM
299 break;
300
301 /* I is now either a user-defined length, the number of non-null
302 characters, or FETCHLIMIT. */
ae6a3a4c 303 *length = i * width;
224c3ddb 304 *buffer = (gdb_byte *) xmalloc (*length);
ae6a3a4c
TJB
305 memcpy (*buffer, contents, *length);
306 err = 0;
307 }
308 else
309 {
621c8364
TT
310 CORE_ADDR addr = value_as_address (value);
311
0987cf35
DE
312 /* Prior to the fix for PR 16196 read_string would ignore fetchlimit
313 if length > 0. The old "broken" behaviour is the behaviour we want:
314 The caller may want to fetch 100 bytes from a variable length array
315 implemented using the common idiom of having an array of length 1 at
316 the end of a struct. In this case we want to ignore the declared
317 size of the array. However, it's counterintuitive to implement that
318 behaviour in read_string: what does fetchlimit otherwise mean if
319 length > 0. Therefore we implement the behaviour we want here:
320 If *length > 0, don't specify a fetchlimit. This preserves the
321 previous behaviour. We could move this check above where we know
322 whether the array is declared with a fixed size, but we only want
323 to apply this behaviour when calling read_string. PR 16286. */
324 if (*length > 0)
325 fetchlimit = UINT_MAX;
326
621c8364
TT
327 err = read_string (addr, *length, width, fetchlimit,
328 byte_order, buffer, length);
d09f2c3f 329 if (err != 0)
ae6a3a4c 330 {
8ea5dfdf 331 xfree (*buffer);
d09f2c3f 332 memory_error (TARGET_XFER_E_IO, addr);
ae6a3a4c
TJB
333 }
334 }
335
fbb8f299
PM
336 /* If the LENGTH is specified at -1, we want to return the string
337 length up to the terminating null character. If an actual length
338 was specified, we want to return the length of exactly what was
339 read. */
340 if (req_length == -1)
341 /* If the last character is null, subtract it from LENGTH. */
342 if (*length > 0
aff410f1
MS
343 && extract_unsigned_integer (*buffer + *length - width,
344 width, byte_order) == 0)
fbb8f299
PM
345 *length -= width;
346
347 /* The read_string function will return the number of bytes read.
348 If length returned from read_string was > 0, return the number of
349 characters read by dividing the number of bytes by width. */
350 if (*length != 0)
351 *length = *length / width;
ae6a3a4c 352
96c07c5b 353 *char_type = element_type;
ae6a3a4c
TJB
354
355 return;
356
357 error:
358 {
2f408ecb
PA
359 std::string type_str = type_to_string (type);
360 if (!type_str.empty ())
ae6a3a4c 361 {
ae6a3a4c 362 error (_("Trying to read string with inappropriate type `%s'."),
2f408ecb 363 type_str.c_str ());
ae6a3a4c
TJB
364 }
365 else
366 error (_("Trying to read string with inappropriate type."));
367 }
368}
369
c906108c 370\f
6c7a06a3
TT
371/* Evaluating C and C++ expressions. */
372
373/* Convert a UCN. The digits of the UCN start at P and extend no
374 farther than LIMIT. DEST_CHARSET is the name of the character set
375 into which the UCN should be converted. The results are written to
376 OUTPUT. LENGTH is the maximum length of the UCN, either 4 or 8.
377 Returns a pointer to just after the final digit of the UCN. */
378
379static char *
380convert_ucn (char *p, char *limit, const char *dest_charset,
381 struct obstack *output, int length)
382{
383 unsigned long result = 0;
384 gdb_byte data[4];
385 int i;
386
387 for (i = 0; i < length && p < limit && isxdigit (*p); ++i, ++p)
388 result = (result << 4) + host_hex_value (*p);
389
390 for (i = 3; i >= 0; --i)
391 {
392 data[i] = result & 0xff;
393 result >>= 8;
394 }
395
aff410f1
MS
396 convert_between_encodings ("UTF-32BE", dest_charset, data,
397 4, 4, output, translit_none);
6c7a06a3
TT
398
399 return p;
400}
401
402/* Emit a character, VALUE, which was specified numerically, to
403 OUTPUT. TYPE is the target character type. */
404
405static void
406emit_numeric_character (struct type *type, unsigned long value,
407 struct obstack *output)
408{
409 gdb_byte *buffer;
410
224c3ddb 411 buffer = (gdb_byte *) alloca (TYPE_LENGTH (type));
6c7a06a3
TT
412 pack_long (buffer, type, value);
413 obstack_grow (output, buffer, TYPE_LENGTH (type));
414}
415
416/* Convert an octal escape sequence. TYPE is the target character
417 type. The digits of the escape sequence begin at P and extend no
418 farther than LIMIT. The result is written to OUTPUT. Returns a
419 pointer to just after the final digit of the escape sequence. */
420
421static char *
aff410f1
MS
422convert_octal (struct type *type, char *p,
423 char *limit, struct obstack *output)
6c7a06a3 424{
30b66ecc 425 int i;
6c7a06a3
TT
426 unsigned long value = 0;
427
30b66ecc
TT
428 for (i = 0;
429 i < 3 && p < limit && isdigit (*p) && *p != '8' && *p != '9';
430 ++i)
6c7a06a3
TT
431 {
432 value = 8 * value + host_hex_value (*p);
433 ++p;
434 }
435
436 emit_numeric_character (type, value, output);
437
438 return p;
439}
440
441/* Convert a hex escape sequence. TYPE is the target character type.
442 The digits of the escape sequence begin at P and extend no farther
443 than LIMIT. The result is written to OUTPUT. Returns a pointer to
444 just after the final digit of the escape sequence. */
445
446static char *
aff410f1
MS
447convert_hex (struct type *type, char *p,
448 char *limit, struct obstack *output)
6c7a06a3
TT
449{
450 unsigned long value = 0;
451
452 while (p < limit && isxdigit (*p))
453 {
454 value = 16 * value + host_hex_value (*p);
455 ++p;
456 }
457
458 emit_numeric_character (type, value, output);
459
460 return p;
461}
462
463#define ADVANCE \
464 do { \
465 ++p; \
466 if (p == limit) \
467 error (_("Malformed escape sequence")); \
468 } while (0)
469
470/* Convert an escape sequence to a target format. TYPE is the target
471 character type to use, and DEST_CHARSET is the name of the target
472 character set. The backslash of the escape sequence is at *P, and
473 the escape sequence will not extend past LIMIT. The results are
474 written to OUTPUT. Returns a pointer to just past the final
475 character of the escape sequence. */
476
477static char *
478convert_escape (struct type *type, const char *dest_charset,
479 char *p, char *limit, struct obstack *output)
480{
481 /* Skip the backslash. */
482 ADVANCE;
483
484 switch (*p)
485 {
486 case '\\':
487 obstack_1grow (output, '\\');
488 ++p;
489 break;
490
491 case 'x':
492 ADVANCE;
493 if (!isxdigit (*p))
494 error (_("\\x used with no following hex digits."));
495 p = convert_hex (type, p, limit, output);
496 break;
497
498 case '0':
499 case '1':
500 case '2':
501 case '3':
502 case '4':
503 case '5':
504 case '6':
505 case '7':
506 p = convert_octal (type, p, limit, output);
507 break;
508
509 case 'u':
510 case 'U':
511 {
512 int length = *p == 'u' ? 4 : 8;
c5504eaf 513
6c7a06a3
TT
514 ADVANCE;
515 if (!isxdigit (*p))
516 error (_("\\u used with no following hex digits"));
517 p = convert_ucn (p, limit, dest_charset, output, length);
518 }
519 }
520
521 return p;
522}
523
524/* Given a single string from a (C-specific) OP_STRING list, convert
525 it to a target string, handling escape sequences specially. The
526 output is written to OUTPUT. DATA is the input string, which has
527 length LEN. DEST_CHARSET is the name of the target character set,
528 and TYPE is the type of target character to use. */
529
530static void
531parse_one_string (struct obstack *output, char *data, int len,
532 const char *dest_charset, struct type *type)
533{
534 char *limit;
535
536 limit = data + len;
537
538 while (data < limit)
539 {
540 char *p = data;
c5504eaf 541
6c7a06a3
TT
542 /* Look for next escape, or the end of the input. */
543 while (p < limit && *p != '\\')
544 ++p;
545 /* If we saw a run of characters, convert them all. */
546 if (p > data)
547 convert_between_encodings (host_charset (), dest_charset,
ac91cd70 548 (gdb_byte *) data, p - data, 1,
aff410f1 549 output, translit_none);
6c7a06a3
TT
550 /* If we saw an escape, convert it. */
551 if (p < limit)
552 p = convert_escape (type, dest_charset, p, limit, output);
553 data = p;
554 }
555}
556
557/* Expression evaluator for the C language family. Most operations
558 are delegated to evaluate_subexp_standard; see that function for a
559 description of the arguments. */
560
f4b8a18d 561struct value *
6c7a06a3
TT
562evaluate_subexp_c (struct type *expect_type, struct expression *exp,
563 int *pos, enum noside noside)
564{
565 enum exp_opcode op = exp->elts[*pos].opcode;
566
567 switch (op)
568 {
569 case OP_STRING:
570 {
571 int oplen, limit;
572 struct type *type;
6c7a06a3 573 struct value *result;
0c801b96 574 c_string_type dest_type;
6c7a06a3 575 const char *dest_charset;
c50491a7 576 int satisfy_expected = 0;
6c7a06a3 577
8268c778 578 auto_obstack output;
6c7a06a3
TT
579
580 ++*pos;
581 oplen = longest_to_int (exp->elts[*pos].longconst);
582
583 ++*pos;
584 limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1);
0c801b96
SM
585 dest_type = ((enum c_string_type_values)
586 longest_to_int (exp->elts[*pos].longconst));
6c7a06a3
TT
587 switch (dest_type & ~C_CHAR)
588 {
589 case C_STRING:
d80b854b
UW
590 type = language_string_char_type (exp->language_defn,
591 exp->gdbarch);
6c7a06a3
TT
592 break;
593 case C_WIDE_STRING:
e6c014f2
UW
594 type = lookup_typename (exp->language_defn, exp->gdbarch,
595 "wchar_t", NULL, 0);
6c7a06a3
TT
596 break;
597 case C_STRING_16:
e6c014f2
UW
598 type = lookup_typename (exp->language_defn, exp->gdbarch,
599 "char16_t", NULL, 0);
6c7a06a3
TT
600 break;
601 case C_STRING_32:
e6c014f2
UW
602 type = lookup_typename (exp->language_defn, exp->gdbarch,
603 "char32_t", NULL, 0);
6c7a06a3
TT
604 break;
605 default:
9b20d036 606 internal_error (__FILE__, __LINE__, _("unhandled c_string_type"));
6c7a06a3 607 }
546e879e
TT
608
609 /* Ensure TYPE_LENGTH is valid for TYPE. */
610 check_typedef (type);
611
c50491a7
TT
612 /* If the caller expects an array of some integral type,
613 satisfy them. If something odder is expected, rely on the
614 caller to cast. */
615 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_ARRAY)
616 {
617 struct type *element_type
618 = check_typedef (TYPE_TARGET_TYPE (expect_type));
619
620 if (TYPE_CODE (element_type) == TYPE_CODE_INT
621 || TYPE_CODE (element_type) == TYPE_CODE_CHAR)
622 {
623 type = element_type;
624 satisfy_expected = 1;
625 }
626 }
627
f870a310 628 dest_charset = charset_for_string_type (dest_type, exp->gdbarch);
6c7a06a3
TT
629
630 ++*pos;
631 while (*pos < limit)
632 {
633 int len;
634
635 len = longest_to_int (exp->elts[*pos].longconst);
636
637 ++*pos;
638 if (noside != EVAL_SKIP)
639 parse_one_string (&output, &exp->elts[*pos].string, len,
640 dest_charset, type);
641 *pos += BYTES_TO_EXP_ELEM (len);
642 }
643
644 /* Skip the trailing length and opcode. */
645 *pos += 2;
646
647 if (noside == EVAL_SKIP)
334cc82d
TT
648 {
649 /* Return a dummy value of the appropriate type. */
c50491a7
TT
650 if (expect_type != NULL)
651 result = allocate_value (expect_type);
652 else if ((dest_type & C_CHAR) != 0)
334cc82d
TT
653 result = allocate_value (type);
654 else
3b7538c0 655 result = value_cstring ("", 0, type);
334cc82d
TT
656 return result;
657 }
6c7a06a3
TT
658
659 if ((dest_type & C_CHAR) != 0)
660 {
661 LONGEST value;
662
663 if (obstack_object_size (&output) != TYPE_LENGTH (type))
3e43a32a
MS
664 error (_("Could not convert character "
665 "constant to target character set"));
51a5cd90 666 value = unpack_long (type, (gdb_byte *) obstack_base (&output));
6c7a06a3
TT
667 result = value_from_longest (type, value);
668 }
669 else
670 {
671 int i;
c5504eaf 672
6c7a06a3
TT
673 /* Write the terminating character. */
674 for (i = 0; i < TYPE_LENGTH (type); ++i)
675 obstack_1grow (&output, 0);
c50491a7
TT
676
677 if (satisfy_expected)
678 {
679 LONGEST low_bound, high_bound;
680 int element_size = TYPE_LENGTH (type);
681
682 if (get_discrete_bounds (TYPE_INDEX_TYPE (expect_type),
683 &low_bound, &high_bound) < 0)
684 {
685 low_bound = 0;
686 high_bound = (TYPE_LENGTH (expect_type) / element_size) - 1;
687 }
688 if (obstack_object_size (&output) / element_size
689 > (high_bound - low_bound + 1))
690 error (_("Too many array elements"));
691
692 result = allocate_value (expect_type);
693 memcpy (value_contents_raw (result), obstack_base (&output),
694 obstack_object_size (&output));
695 }
696 else
79f33898 697 result = value_cstring ((const char *) obstack_base (&output),
c50491a7
TT
698 obstack_object_size (&output),
699 type);
6c7a06a3 700 }
6c7a06a3
TT
701 return result;
702 }
703 break;
704
705 default:
706 break;
707 }
708 return evaluate_subexp_standard (expect_type, exp, pos, noside);
709}
43cc5389
TT
710\f
711/* la_watch_location_expression for C. */
c5aa993b 712
43cc5389
TT
713gdb::unique_xmalloc_ptr<char>
714c_watch_location_expression (struct type *type, CORE_ADDR addr)
715{
716 type = check_typedef (TYPE_TARGET_TYPE (check_typedef (type)));
717 std::string name = type_to_string (type);
718 return gdb::unique_xmalloc_ptr<char>
719 (xstrprintf ("* (%s *) %s", name.c_str (), core_addr_to_string (addr)));
720}
84f0252a 721
84f0252a 722\f
c906108c
SS
723/* Table mapping opcodes into strings for printing operators
724 and precedences of the operators. */
725
726const struct op_print c_op_print_tab[] =
c5aa993b
JM
727{
728 {",", BINOP_COMMA, PREC_COMMA, 0},
729 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
730 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
731 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
732 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
733 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
734 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
735 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
736 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
737 {"<=", BINOP_LEQ, PREC_ORDER, 0},
738 {">=", BINOP_GEQ, PREC_ORDER, 0},
739 {">", BINOP_GTR, PREC_ORDER, 0},
740 {"<", BINOP_LESS, PREC_ORDER, 0},
741 {">>", BINOP_RSH, PREC_SHIFT, 0},
742 {"<<", BINOP_LSH, PREC_SHIFT, 0},
743 {"+", BINOP_ADD, PREC_ADD, 0},
744 {"-", BINOP_SUB, PREC_ADD, 0},
745 {"*", BINOP_MUL, PREC_MUL, 0},
746 {"/", BINOP_DIV, PREC_MUL, 0},
747 {"%", BINOP_REM, PREC_MUL, 0},
748 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
a016fc87 749 {"+", UNOP_PLUS, PREC_PREFIX, 0},
c5aa993b
JM
750 {"-", UNOP_NEG, PREC_PREFIX, 0},
751 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
752 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
753 {"*", UNOP_IND, PREC_PREFIX, 0},
754 {"&", UNOP_ADDR, PREC_PREFIX, 0},
755 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
756 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
757 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
f486487f 758 {NULL, OP_NULL, PREC_PREFIX, 0}
c906108c
SS
759};
760\f
685419e2
AC
761enum c_primitive_types {
762 c_primitive_type_int,
763 c_primitive_type_long,
764 c_primitive_type_short,
765 c_primitive_type_char,
766 c_primitive_type_float,
767 c_primitive_type_double,
768 c_primitive_type_void,
769 c_primitive_type_long_long,
770 c_primitive_type_signed_char,
771 c_primitive_type_unsigned_char,
772 c_primitive_type_unsigned_short,
773 c_primitive_type_unsigned_int,
774 c_primitive_type_unsigned_long,
775 c_primitive_type_unsigned_long_long,
776 c_primitive_type_long_double,
777 c_primitive_type_complex,
778 c_primitive_type_double_complex,
213e4dc2
TJB
779 c_primitive_type_decfloat,
780 c_primitive_type_decdouble,
781 c_primitive_type_declong,
685419e2
AC
782 nr_c_primitive_types
783};
784
e9667a65 785void
685419e2
AC
786c_language_arch_info (struct gdbarch *gdbarch,
787 struct language_arch_info *lai)
788{
789 const struct builtin_type *builtin = builtin_type (gdbarch);
c5504eaf 790
e9667a65 791 lai->string_char_type = builtin->builtin_char;
685419e2
AC
792 lai->primitive_type_vector
793 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_c_primitive_types + 1,
794 struct type *);
795 lai->primitive_type_vector [c_primitive_type_int] = builtin->builtin_int;
796 lai->primitive_type_vector [c_primitive_type_long] = builtin->builtin_long;
797 lai->primitive_type_vector [c_primitive_type_short] = builtin->builtin_short;
798 lai->primitive_type_vector [c_primitive_type_char] = builtin->builtin_char;
799 lai->primitive_type_vector [c_primitive_type_float] = builtin->builtin_float;
800 lai->primitive_type_vector [c_primitive_type_double] = builtin->builtin_double;
801 lai->primitive_type_vector [c_primitive_type_void] = builtin->builtin_void;
802 lai->primitive_type_vector [c_primitive_type_long_long] = builtin->builtin_long_long;
803 lai->primitive_type_vector [c_primitive_type_signed_char] = builtin->builtin_signed_char;
804 lai->primitive_type_vector [c_primitive_type_unsigned_char] = builtin->builtin_unsigned_char;
805 lai->primitive_type_vector [c_primitive_type_unsigned_short] = builtin->builtin_unsigned_short;
806 lai->primitive_type_vector [c_primitive_type_unsigned_int] = builtin->builtin_unsigned_int;
807 lai->primitive_type_vector [c_primitive_type_unsigned_long] = builtin->builtin_unsigned_long;
808 lai->primitive_type_vector [c_primitive_type_unsigned_long_long] = builtin->builtin_unsigned_long_long;
809 lai->primitive_type_vector [c_primitive_type_long_double] = builtin->builtin_long_double;
810 lai->primitive_type_vector [c_primitive_type_complex] = builtin->builtin_complex;
811 lai->primitive_type_vector [c_primitive_type_double_complex] = builtin->builtin_double_complex;
213e4dc2
TJB
812 lai->primitive_type_vector [c_primitive_type_decfloat] = builtin->builtin_decfloat;
813 lai->primitive_type_vector [c_primitive_type_decdouble] = builtin->builtin_decdouble;
814 lai->primitive_type_vector [c_primitive_type_declong] = builtin->builtin_declong;
fbb06eb1
UW
815
816 lai->bool_type_default = builtin->builtin_int;
cad351d1 817}
685419e2 818
6aecb9c2 819const struct exp_descriptor exp_descriptor_c =
6c7a06a3
TT
820{
821 print_subexp_standard,
822 operator_length_standard,
c0201579 823 operator_check_standard,
6c7a06a3
TT
824 op_name_standard,
825 dump_subexp_body_standard,
826 evaluate_subexp_c
827};
828
56618e20
TT
829static const char *c_extensions[] =
830{
831 ".c", NULL
832};
833
c5aa993b
JM
834const struct language_defn c_language_defn =
835{
c906108c 836 "c", /* Language name */
6abde28f 837 "C",
c906108c 838 language_c,
c906108c 839 range_check_off,
63872f9d 840 case_sensitive_on,
7ca2d3a3 841 array_row_major,
9a044a89 842 macro_expansion_c,
56618e20 843 c_extensions,
6c7a06a3 844 &exp_descriptor_c,
7c8adf68 845 c_parse,
b3f11165 846 c_yyerror,
e85c3284 847 null_post_parser,
c906108c
SS
848 c_printchar, /* Print a character constant */
849 c_printstr, /* Function to print string constant */
850 c_emit_char, /* Print a single char */
c906108c 851 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 852 c_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
853 c_val_print, /* Print a value using appropriate syntax */
854 c_value_print, /* Print a top-level value */
a5ee536b 855 default_read_var_value, /* la_read_var_value */
f636b87d 856 NULL, /* Language specific skip_trampoline */
2b2d9e11 857 NULL, /* name_of_this */
5f9a71c3 858 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 859 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 860 NULL, /* Language specific symbol demangler */
8b302db8 861 NULL,
aff410f1
MS
862 NULL, /* Language specific
863 class_name_from_physname */
c906108c
SS
864 c_op_print_tab, /* expression operators for printing */
865 1, /* c-style arrays */
866 0, /* String lower bound */
6084f43a 867 default_word_break_characters,
41d27058 868 default_make_symbol_completion_list,
685419e2 869 c_language_arch_info,
e79af960 870 default_print_array_index,
41f1b697 871 default_pass_by_reference,
ae6a3a4c 872 c_get_string,
43cc5389 873 c_watch_location_expression,
1a119f36 874 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 875 iterate_over_symbols,
a53b64ea 876 &c_varobj_ops,
bb2ec1b3
TT
877 c_get_compile_context,
878 c_compute_program,
c906108c
SS
879 LANG_MAGIC
880};
881
cad351d1
UW
882enum cplus_primitive_types {
883 cplus_primitive_type_int,
884 cplus_primitive_type_long,
885 cplus_primitive_type_short,
886 cplus_primitive_type_char,
887 cplus_primitive_type_float,
888 cplus_primitive_type_double,
889 cplus_primitive_type_void,
890 cplus_primitive_type_long_long,
891 cplus_primitive_type_signed_char,
892 cplus_primitive_type_unsigned_char,
893 cplus_primitive_type_unsigned_short,
894 cplus_primitive_type_unsigned_int,
895 cplus_primitive_type_unsigned_long,
896 cplus_primitive_type_unsigned_long_long,
897 cplus_primitive_type_long_double,
898 cplus_primitive_type_complex,
899 cplus_primitive_type_double_complex,
900 cplus_primitive_type_bool,
213e4dc2
TJB
901 cplus_primitive_type_decfloat,
902 cplus_primitive_type_decdouble,
903 cplus_primitive_type_declong,
53e710ac
PA
904 cplus_primitive_type_char16_t,
905 cplus_primitive_type_char32_t,
53375380 906 cplus_primitive_type_wchar_t,
cad351d1 907 nr_cplus_primitive_types
c906108c
SS
908};
909
cad351d1
UW
910static void
911cplus_language_arch_info (struct gdbarch *gdbarch,
912 struct language_arch_info *lai)
913{
914 const struct builtin_type *builtin = builtin_type (gdbarch);
c5504eaf 915
cad351d1
UW
916 lai->string_char_type = builtin->builtin_char;
917 lai->primitive_type_vector
918 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_cplus_primitive_types + 1,
919 struct type *);
920 lai->primitive_type_vector [cplus_primitive_type_int]
921 = builtin->builtin_int;
922 lai->primitive_type_vector [cplus_primitive_type_long]
923 = builtin->builtin_long;
924 lai->primitive_type_vector [cplus_primitive_type_short]
925 = builtin->builtin_short;
926 lai->primitive_type_vector [cplus_primitive_type_char]
927 = builtin->builtin_char;
928 lai->primitive_type_vector [cplus_primitive_type_float]
929 = builtin->builtin_float;
930 lai->primitive_type_vector [cplus_primitive_type_double]
931 = builtin->builtin_double;
932 lai->primitive_type_vector [cplus_primitive_type_void]
933 = builtin->builtin_void;
934 lai->primitive_type_vector [cplus_primitive_type_long_long]
935 = builtin->builtin_long_long;
936 lai->primitive_type_vector [cplus_primitive_type_signed_char]
937 = builtin->builtin_signed_char;
938 lai->primitive_type_vector [cplus_primitive_type_unsigned_char]
939 = builtin->builtin_unsigned_char;
940 lai->primitive_type_vector [cplus_primitive_type_unsigned_short]
941 = builtin->builtin_unsigned_short;
942 lai->primitive_type_vector [cplus_primitive_type_unsigned_int]
943 = builtin->builtin_unsigned_int;
944 lai->primitive_type_vector [cplus_primitive_type_unsigned_long]
945 = builtin->builtin_unsigned_long;
946 lai->primitive_type_vector [cplus_primitive_type_unsigned_long_long]
947 = builtin->builtin_unsigned_long_long;
948 lai->primitive_type_vector [cplus_primitive_type_long_double]
949 = builtin->builtin_long_double;
950 lai->primitive_type_vector [cplus_primitive_type_complex]
951 = builtin->builtin_complex;
952 lai->primitive_type_vector [cplus_primitive_type_double_complex]
953 = builtin->builtin_double_complex;
954 lai->primitive_type_vector [cplus_primitive_type_bool]
955 = builtin->builtin_bool;
213e4dc2
TJB
956 lai->primitive_type_vector [cplus_primitive_type_decfloat]
957 = builtin->builtin_decfloat;
958 lai->primitive_type_vector [cplus_primitive_type_decdouble]
959 = builtin->builtin_decdouble;
960 lai->primitive_type_vector [cplus_primitive_type_declong]
961 = builtin->builtin_declong;
53e710ac
PA
962 lai->primitive_type_vector [cplus_primitive_type_char16_t]
963 = builtin->builtin_char16;
964 lai->primitive_type_vector [cplus_primitive_type_char32_t]
965 = builtin->builtin_char32;
53375380
PA
966 lai->primitive_type_vector [cplus_primitive_type_wchar_t]
967 = builtin->builtin_wchar;
fbb06eb1
UW
968
969 lai->bool_type_symbol = "bool";
970 lai->bool_type_default = builtin->builtin_bool;
cad351d1
UW
971}
972
56618e20
TT
973static const char *cplus_extensions[] =
974{
975 ".C", ".cc", ".cp", ".cpp", ".cxx", ".c++", NULL
976};
977
c5aa993b
JM
978const struct language_defn cplus_language_defn =
979{
980 "c++", /* Language name */
6abde28f 981 "C++",
c906108c 982 language_cplus,
c906108c 983 range_check_off,
63872f9d 984 case_sensitive_on,
7ca2d3a3 985 array_row_major,
9a044a89 986 macro_expansion_c,
56618e20 987 cplus_extensions,
6c7a06a3 988 &exp_descriptor_c,
7c8adf68 989 c_parse,
b3f11165 990 c_yyerror,
e85c3284 991 null_post_parser,
c906108c
SS
992 c_printchar, /* Print a character constant */
993 c_printstr, /* Function to print string constant */
994 c_emit_char, /* Print a single char */
c906108c 995 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 996 c_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
997 c_val_print, /* Print a value using appropriate syntax */
998 c_value_print, /* Print a top-level value */
a5ee536b 999 default_read_var_value, /* la_read_var_value */
b18be20d 1000 cplus_skip_trampoline, /* Language specific skip_trampoline */
2b2d9e11 1001 "this", /* name_of_this */
1fcb5155 1002 cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 1003 cp_lookup_transparent_type, /* lookup_transparent_type */
8de20a37 1004 gdb_demangle, /* Language specific symbol demangler */
8b302db8 1005 gdb_sniff_from_mangled_name,
aff410f1
MS
1006 cp_class_name_from_physname, /* Language specific
1007 class_name_from_physname */
c906108c
SS
1008 c_op_print_tab, /* expression operators for printing */
1009 1, /* c-style arrays */
1010 0, /* String lower bound */
6084f43a 1011 default_word_break_characters,
41d27058 1012 default_make_symbol_completion_list,
cad351d1 1013 cplus_language_arch_info,
e79af960 1014 default_print_array_index,
41f1b697 1015 cp_pass_by_reference,
ae6a3a4c 1016 c_get_string,
43cc5389 1017 c_watch_location_expression,
1a119f36 1018 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 1019 iterate_over_symbols,
a53b64ea 1020 &cplus_varobj_ops,
bb2ec1b3
TT
1021 NULL,
1022 NULL,
c906108c
SS
1023 LANG_MAGIC
1024};
1025
56618e20
TT
1026static const char *asm_extensions[] =
1027{
1028 ".s", ".sx", ".S", NULL
1029};
1030
c5aa993b
JM
1031const struct language_defn asm_language_defn =
1032{
c906108c 1033 "asm", /* Language name */
6abde28f 1034 "assembly",
c906108c 1035 language_asm,
c906108c 1036 range_check_off,
63872f9d 1037 case_sensitive_on,
7ca2d3a3 1038 array_row_major,
9a044a89 1039 macro_expansion_c,
56618e20 1040 asm_extensions,
6c7a06a3 1041 &exp_descriptor_c,
7c8adf68 1042 c_parse,
b3f11165 1043 c_yyerror,
e85c3284 1044 null_post_parser,
c906108c
SS
1045 c_printchar, /* Print a character constant */
1046 c_printstr, /* Function to print string constant */
1047 c_emit_char, /* Print a single char */
c906108c 1048 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 1049 c_print_typedef, /* Print a typedef using appropriate syntax */
c906108c
SS
1050 c_val_print, /* Print a value using appropriate syntax */
1051 c_value_print, /* Print a top-level value */
a5ee536b 1052 default_read_var_value, /* la_read_var_value */
f636b87d 1053 NULL, /* Language specific skip_trampoline */
2b2d9e11 1054 NULL, /* name_of_this */
5f9a71c3 1055 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 1056 basic_lookup_transparent_type,/* lookup_transparent_type */
9a3d7dfd 1057 NULL, /* Language specific symbol demangler */
8b302db8 1058 NULL,
aff410f1
MS
1059 NULL, /* Language specific
1060 class_name_from_physname */
c906108c
SS
1061 c_op_print_tab, /* expression operators for printing */
1062 1, /* c-style arrays */
1063 0, /* String lower bound */
6084f43a 1064 default_word_break_characters,
41d27058 1065 default_make_symbol_completion_list,
aff410f1 1066 c_language_arch_info, /* FIXME: la_language_arch_info. */
e79af960 1067 default_print_array_index,
41f1b697 1068 default_pass_by_reference,
ae6a3a4c 1069 c_get_string,
43cc5389 1070 c_watch_location_expression,
1a119f36 1071 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 1072 iterate_over_symbols,
a53b64ea 1073 &default_varobj_ops,
bb2ec1b3
TT
1074 NULL,
1075 NULL,
c906108c
SS
1076 LANG_MAGIC
1077};
1078
20a0e81d
JB
1079/* The following language_defn does not represent a real language.
1080 It just provides a minimal support a-la-C that should allow users
1081 to do some simple operations when debugging applications that use
1082 a language currently not supported by GDB. */
1083
1084const struct language_defn minimal_language_defn =
1085{
1086 "minimal", /* Language name */
6abde28f 1087 "Minimal",
20a0e81d 1088 language_minimal,
20a0e81d 1089 range_check_off,
20a0e81d 1090 case_sensitive_on,
7ca2d3a3 1091 array_row_major,
9a044a89 1092 macro_expansion_c,
56618e20 1093 NULL,
6c7a06a3 1094 &exp_descriptor_c,
7c8adf68 1095 c_parse,
b3f11165 1096 c_yyerror,
e85c3284 1097 null_post_parser,
20a0e81d
JB
1098 c_printchar, /* Print a character constant */
1099 c_printstr, /* Function to print string constant */
1100 c_emit_char, /* Print a single char */
20a0e81d 1101 c_print_type, /* Print a type using appropriate syntax */
5c6ce71d 1102 c_print_typedef, /* Print a typedef using appropriate syntax */
20a0e81d
JB
1103 c_val_print, /* Print a value using appropriate syntax */
1104 c_value_print, /* Print a top-level value */
a5ee536b 1105 default_read_var_value, /* la_read_var_value */
20a0e81d 1106 NULL, /* Language specific skip_trampoline */
2b2d9e11 1107 NULL, /* name_of_this */
5f9a71c3 1108 basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
b368761e 1109 basic_lookup_transparent_type,/* lookup_transparent_type */
20a0e81d 1110 NULL, /* Language specific symbol demangler */
8b302db8 1111 NULL,
aff410f1
MS
1112 NULL, /* Language specific
1113 class_name_from_physname */
20a0e81d
JB
1114 c_op_print_tab, /* expression operators for printing */
1115 1, /* c-style arrays */
1116 0, /* String lower bound */
6084f43a 1117 default_word_break_characters,
41d27058 1118 default_make_symbol_completion_list,
e9667a65 1119 c_language_arch_info,
e79af960 1120 default_print_array_index,
41f1b697 1121 default_pass_by_reference,
ae6a3a4c 1122 c_get_string,
43cc5389 1123 c_watch_location_expression,
1a119f36 1124 NULL, /* la_get_symbol_name_cmp */
f8eba3c6 1125 iterate_over_symbols,
a53b64ea 1126 &default_varobj_ops,
bb2ec1b3
TT
1127 NULL,
1128 NULL,
20a0e81d
JB
1129 LANG_MAGIC
1130};
1131
c906108c 1132void
fba45db2 1133_initialize_c_language (void)
c906108c
SS
1134{
1135 add_language (&c_language_defn);
1136 add_language (&cplus_language_defn);
1137 add_language (&asm_language_defn);
20a0e81d 1138 add_language (&minimal_language_defn);
c906108c 1139}
This page took 1.437173 seconds and 4 git commands to generate.