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