remove build_parse entry about buffer overrun.
[deliverable/binutils-gdb.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2 Copyright 1991, 1992, 2000 Free Software Foundation, Inc.
3 Contributed by the Department of Computer Science at the State University
4 of New York at Buffalo.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 /* This file contains functions that return things that are specific
24 to languages. Each function should examine current_language if necessary,
25 and return the appropriate result. */
26
27 /* FIXME: Most of these would be better organized as macros which
28 return data out of a "language-specific" struct pointer that is set
29 whenever the working language changes. That would be a lot faster. */
30
31 #include "defs.h"
32 #include <ctype.h>
33 #include "gdb_string.h"
34
35 #include "symtab.h"
36 #include "gdbtypes.h"
37 #include "value.h"
38 #include "gdbcmd.h"
39 #include "frame.h"
40 #include "expression.h"
41 #include "language.h"
42 #include "target.h"
43 #include "parser-defs.h"
44 #include "jv-lang.h"
45
46 extern void _initialize_language (void);
47
48 static void show_language_command (char *, int);
49
50 static void set_language_command (char *, int);
51
52 static void show_type_command (char *, int);
53
54 static void set_type_command (char *, int);
55
56 static void show_range_command (char *, int);
57
58 static void set_range_command (char *, int);
59
60 static void set_range_str (void);
61
62 static void set_type_str (void);
63
64 static void set_lang_str (void);
65
66 static void unk_lang_error (char *);
67
68 static int unk_lang_parser (void);
69
70 static void show_check (char *, int);
71
72 static void set_check (char *, int);
73
74 static void set_type_range (void);
75
76 static void unk_lang_emit_char (int c, struct ui_file *stream, int quoter);
77
78 static void unk_lang_printchar (int c, struct ui_file *stream);
79
80 static void unk_lang_printstr (struct ui_file * stream, char *string,
81 unsigned int length, int width,
82 int force_ellipses);
83
84 static struct type *unk_lang_create_fundamental_type (struct objfile *, int);
85
86 static void unk_lang_print_type (struct type *, char *, struct ui_file *,
87 int, int);
88
89 static int unk_lang_val_print (struct type *, char *, int, CORE_ADDR,
90 struct ui_file *, int, int, int,
91 enum val_prettyprint);
92
93 static int unk_lang_value_print (value_ptr, struct ui_file *, int, enum val_prettyprint);
94
95 /* Forward declaration */
96 extern const struct language_defn unknown_language_defn;
97 extern char *warning_pre_print;
98
99 /* The current (default at startup) state of type and range checking.
100 (If the modes are set to "auto", though, these are changed based
101 on the default language at startup, and then again based on the
102 language of the first source file. */
103
104 enum range_mode range_mode = range_mode_auto;
105 enum range_check range_check = range_check_off;
106 enum type_mode type_mode = type_mode_auto;
107 enum type_check type_check = type_check_off;
108
109 /* The current language and language_mode (see language.h) */
110
111 const struct language_defn *current_language = &unknown_language_defn;
112 enum language_mode language_mode = language_mode_auto;
113
114 /* The language that the user expects to be typing in (the language
115 of main(), or the last language we notified them about, or C). */
116
117 const struct language_defn *expected_language;
118
119 /* The list of supported languages. The list itself is malloc'd. */
120
121 static const struct language_defn **languages;
122 static unsigned languages_size;
123 static unsigned languages_allocsize;
124 #define DEFAULT_ALLOCSIZE 4
125
126 /* The "set language/type/range" commands all put stuff in these
127 buffers. This is to make them work as set/show commands. The
128 user's string is copied here, then the set_* commands look at
129 them and update them to something that looks nice when it is
130 printed out. */
131
132 static char *language;
133 static char *type;
134 static char *range;
135
136 /* Warning issued when current_language and the language of the current
137 frame do not match. */
138 char lang_frame_mismatch_warn[] =
139 "Warning: the current language does not match this frame.";
140 \f
141
142 /* This page contains the functions corresponding to GDB commands
143 and their helpers. */
144
145 /* Show command. Display a warning if the language set
146 does not match the frame. */
147 static void
148 show_language_command (char *ignore, int from_tty)
149 {
150 enum language flang; /* The language of the current frame */
151
152 flang = get_frame_language ();
153 if (flang != language_unknown &&
154 language_mode == language_mode_manual &&
155 current_language->la_language != flang)
156 printf_filtered ("%s\n", lang_frame_mismatch_warn);
157 }
158
159 /* Set command. Change the current working language. */
160 static void
161 set_language_command (char *ignore, int from_tty)
162 {
163 int i;
164 enum language flang;
165 char *err_lang;
166
167 if (!language || !language[0])
168 {
169 printf_unfiltered ("The currently understood settings are:\n\n");
170 printf_unfiltered ("local or auto Automatic setting based on source file\n");
171
172 for (i = 0; i < languages_size; ++i)
173 {
174 /* Already dealt with these above. */
175 if (languages[i]->la_language == language_unknown
176 || languages[i]->la_language == language_auto)
177 continue;
178
179 /* FIXME for now assume that the human-readable name is just
180 a capitalization of the internal name. */
181 printf_unfiltered ("%-16s Use the %c%s language\n",
182 languages[i]->la_name,
183 /* Capitalize first letter of language
184 name. */
185 toupper (languages[i]->la_name[0]),
186 languages[i]->la_name + 1);
187 }
188 /* Restore the silly string. */
189 set_language (current_language->la_language);
190 return;
191 }
192
193 /* Search the list of languages for a match. */
194 for (i = 0; i < languages_size; i++)
195 {
196 if (STREQ (languages[i]->la_name, language))
197 {
198 /* Found it! Go into manual mode, and use this language. */
199 if (languages[i]->la_language == language_auto)
200 {
201 /* Enter auto mode. Set to the current frame's language, if known. */
202 language_mode = language_mode_auto;
203 flang = get_frame_language ();
204 if (flang != language_unknown)
205 set_language (flang);
206 expected_language = current_language;
207 return;
208 }
209 else
210 {
211 /* Enter manual mode. Set the specified language. */
212 language_mode = language_mode_manual;
213 current_language = languages[i];
214 set_type_range ();
215 set_lang_str ();
216 expected_language = current_language;
217 return;
218 }
219 }
220 }
221
222 /* Reset the language (esp. the global string "language") to the
223 correct values. */
224 err_lang = savestring (language, strlen (language));
225 make_cleanup (free, err_lang); /* Free it after error */
226 set_language (current_language->la_language);
227 error ("Unknown language `%s'.", err_lang);
228 }
229
230 /* Show command. Display a warning if the type setting does
231 not match the current language. */
232 static void
233 show_type_command (char *ignore, int from_tty)
234 {
235 if (type_check != current_language->la_type_check)
236 printf_unfiltered (
237 "Warning: the current type check setting does not match the language.\n");
238 }
239
240 /* Set command. Change the setting for type checking. */
241 static void
242 set_type_command (char *ignore, int from_tty)
243 {
244 if (STREQ (type, "on"))
245 {
246 type_check = type_check_on;
247 type_mode = type_mode_manual;
248 }
249 else if (STREQ (type, "warn"))
250 {
251 type_check = type_check_warn;
252 type_mode = type_mode_manual;
253 }
254 else if (STREQ (type, "off"))
255 {
256 type_check = type_check_off;
257 type_mode = type_mode_manual;
258 }
259 else if (STREQ (type, "auto"))
260 {
261 type_mode = type_mode_auto;
262 set_type_range ();
263 /* Avoid hitting the set_type_str call below. We
264 did it in set_type_range. */
265 return;
266 }
267 else
268 {
269 warning ("Unrecognized type check setting: \"%s\"", type);
270 }
271 set_type_str ();
272 show_type_command ((char *) NULL, from_tty);
273 }
274
275 /* Show command. Display a warning if the range setting does
276 not match the current language. */
277 static void
278 show_range_command (char *ignore, int from_tty)
279 {
280
281 if (range_check != current_language->la_range_check)
282 printf_unfiltered (
283 "Warning: the current range check setting does not match the language.\n");
284 }
285
286 /* Set command. Change the setting for range checking. */
287 static void
288 set_range_command (char *ignore, int from_tty)
289 {
290 if (STREQ (range, "on"))
291 {
292 range_check = range_check_on;
293 range_mode = range_mode_manual;
294 }
295 else if (STREQ (range, "warn"))
296 {
297 range_check = range_check_warn;
298 range_mode = range_mode_manual;
299 }
300 else if (STREQ (range, "off"))
301 {
302 range_check = range_check_off;
303 range_mode = range_mode_manual;
304 }
305 else if (STREQ (range, "auto"))
306 {
307 range_mode = range_mode_auto;
308 set_type_range ();
309 /* Avoid hitting the set_range_str call below. We
310 did it in set_type_range. */
311 return;
312 }
313 else
314 {
315 warning ("Unrecognized range check setting: \"%s\"", range);
316 }
317 set_range_str ();
318 show_range_command ((char *) 0, from_tty);
319 }
320
321 /* Set the status of range and type checking based on
322 the current modes and the current language.
323 If SHOW is non-zero, then print out the current language,
324 type and range checking status. */
325 static void
326 set_type_range (void)
327 {
328
329 if (range_mode == range_mode_auto)
330 range_check = current_language->la_range_check;
331
332 if (type_mode == type_mode_auto)
333 type_check = current_language->la_type_check;
334
335 set_type_str ();
336 set_range_str ();
337 }
338
339 /* Set current language to (enum language) LANG. Returns previous language. */
340
341 enum language
342 set_language (enum language lang)
343 {
344 int i;
345 enum language prev_language;
346
347 prev_language = current_language->la_language;
348
349 for (i = 0; i < languages_size; i++)
350 {
351 if (languages[i]->la_language == lang)
352 {
353 current_language = languages[i];
354 set_type_range ();
355 set_lang_str ();
356 break;
357 }
358 }
359
360 return prev_language;
361 }
362 \f
363 /* This page contains functions that update the global vars
364 language, type and range. */
365 static void
366 set_lang_str (void)
367 {
368 char *prefix = "";
369
370 if (language)
371 free (language);
372 if (language_mode == language_mode_auto)
373 prefix = "auto; currently ";
374
375 language = concat (prefix, current_language->la_name, NULL);
376 }
377
378 static void
379 set_type_str (void)
380 {
381 char *tmp = NULL, *prefix = "";
382
383 if (type)
384 free (type);
385 if (type_mode == type_mode_auto)
386 prefix = "auto; currently ";
387
388 switch (type_check)
389 {
390 case type_check_on:
391 tmp = "on";
392 break;
393 case type_check_off:
394 tmp = "off";
395 break;
396 case type_check_warn:
397 tmp = "warn";
398 break;
399 default:
400 error ("Unrecognized type check setting.");
401 }
402
403 type = concat (prefix, tmp, NULL);
404 }
405
406 static void
407 set_range_str (void)
408 {
409 char *tmp, *pref = "";
410
411 if (range_mode == range_mode_auto)
412 pref = "auto; currently ";
413
414 switch (range_check)
415 {
416 case range_check_on:
417 tmp = "on";
418 break;
419 case range_check_off:
420 tmp = "off";
421 break;
422 case range_check_warn:
423 tmp = "warn";
424 break;
425 default:
426 error ("Unrecognized range check setting.");
427 }
428
429 if (range)
430 free (range);
431 range = concat (pref, tmp, NULL);
432 }
433
434
435 /* Print out the current language settings: language, range and
436 type checking. If QUIETLY, print only what has changed. */
437
438 void
439 language_info (int quietly)
440 {
441 if (quietly && expected_language == current_language)
442 return;
443
444 expected_language = current_language;
445 printf_unfiltered ("Current language: %s\n", language);
446 show_language_command ((char *) 0, 1);
447
448 if (!quietly)
449 {
450 printf_unfiltered ("Type checking: %s\n", type);
451 show_type_command ((char *) 0, 1);
452 printf_unfiltered ("Range checking: %s\n", range);
453 show_range_command ((char *) 0, 1);
454 }
455 }
456 \f
457 /* Return the result of a binary operation. */
458
459 #if 0 /* Currently unused */
460
461 struct type *
462 binop_result_type (value_ptr v1, value_ptr v2)
463 {
464 int size, uns;
465 struct type *t1 = check_typedef (VALUE_TYPE (v1));
466 struct type *t2 = check_typedef (VALUE_TYPE (v2));
467
468 int l1 = TYPE_LENGTH (t1);
469 int l2 = TYPE_LENGTH (t2);
470
471 switch (current_language->la_language)
472 {
473 case language_c:
474 case language_cplus:
475 if (TYPE_CODE (t1) == TYPE_CODE_FLT)
476 return TYPE_CODE (t2) == TYPE_CODE_FLT && l2 > l1 ?
477 VALUE_TYPE (v2) : VALUE_TYPE (v1);
478 else if (TYPE_CODE (t2) == TYPE_CODE_FLT)
479 return TYPE_CODE (t1) == TYPE_CODE_FLT && l1 > l2 ?
480 VALUE_TYPE (v1) : VALUE_TYPE (v2);
481 else if (TYPE_UNSIGNED (t1) && l1 > l2)
482 return VALUE_TYPE (v1);
483 else if (TYPE_UNSIGNED (t2) && l2 > l1)
484 return VALUE_TYPE (v2);
485 else /* Both are signed. Result is the longer type */
486 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
487 break;
488 case language_m2:
489 /* If we are doing type-checking, l1 should equal l2, so this is
490 not needed. */
491 return l1 > l2 ? VALUE_TYPE (v1) : VALUE_TYPE (v2);
492 break;
493 case language_chill:
494 error ("Missing Chill support in function binop_result_check."); /*FIXME */
495 }
496 abort ();
497 return (struct type *) 0; /* For lint */
498 }
499
500 #endif /* 0 */
501 \f
502
503 /* This page contains functions that return format strings for
504 printf for printing out numbers in different formats */
505
506 /* Returns the appropriate printf format for hexadecimal
507 numbers. */
508 char *
509 local_hex_format_custom (char *pre)
510 {
511 static char form[50];
512
513 strcpy (form, local_hex_format_prefix ());
514 strcat (form, "%");
515 strcat (form, pre);
516 strcat (form, local_hex_format_specifier ());
517 strcat (form, local_hex_format_suffix ());
518 return form;
519 }
520
521 #if 0
522 /* FIXME: cagney/2000-03-04: This function does not appear to be used.
523 It can be deleted once 5.0 has been released. */
524 /* FIXME: cagney/2000-03-04: This code assumes that the compiler
525 supports ``long long''. */
526 /* Converts a number to hexadecimal (without leading "0x") and stores it in a
527 static string. Returns a pointer to this string. */
528
529 char *
530 longest_raw_hex_string (LONGEST num)
531 {
532 static char res_longest_raw_hex_string[50];
533 long long ll = num; /* MERGEBUG ?? see below */
534 res_longest_raw_hex_string[0] = 0;
535 /* MERGEBUG ?? As a quick fix I am replacing this with sprintf
536 strcat_address_numeric (num, 0, res_longest_raw_hex_string, 50);
537 */
538
539 sprintf (res_longest_raw_hex_string, "%llx", ll);
540 return res_longest_raw_hex_string;
541 }
542 #endif
543
544 /* Converts a number to hexadecimal and stores it in a static
545 string. Returns a pointer to this string. */
546 char *
547 local_hex_string (unsigned long num)
548 {
549 static char res[50];
550
551 sprintf (res, local_hex_format (), num);
552 return res;
553 }
554
555 /* Converts a LONGEST number to hexadecimal and stores it in a static
556 string. Returns a pointer to this string. */
557 char *
558 longest_local_hex_string (LONGEST num)
559 {
560 return longest_local_hex_string_custom (num, "l");
561 }
562
563 /* Converts a number to custom hexadecimal and stores it in a static
564 string. Returns a pointer to this string. */
565 char *
566 local_hex_string_custom (unsigned long num, char *pre)
567 {
568 static char res[50];
569
570 sprintf (res, local_hex_format_custom (pre), num);
571 return res;
572 }
573
574 /* Converts a LONGEST number to custom hexadecimal and stores it in a static
575 string. Returns a pointer to this string. Note that the width parameter
576 should end with "l", e.g. "08l" as with calls to local_hex_string_custom */
577
578 char *
579 longest_local_hex_string_custom (LONGEST num, char *width)
580 {
581 #define RESULT_BUF_LEN 50
582 static char res2[RESULT_BUF_LEN];
583 char format[RESULT_BUF_LEN];
584 #if !defined (PRINTF_HAS_LONG_LONG)
585 int field_width;
586 int num_len;
587 int num_pad_chars;
588 char *pad_char; /* string with one character */
589 int pad_on_left;
590 char *parse_ptr;
591 char temp_nbr_buf[RESULT_BUF_LEN];
592 #endif
593
594 #ifndef CC_HAS_LONG_LONG
595 /* If there is no long long, then LONGEST should be just long and we
596 can use local_hex_string_custom
597 */
598 return local_hex_string_custom ((unsigned long) num, width);
599 #elif defined (PRINTF_HAS_LONG_LONG)
600 /* Just use printf. */
601 strcpy (format, local_hex_format_prefix ()); /* 0x */
602 strcat (format, "%");
603 strcat (format, width); /* e.g. "08l" */
604 strcat (format, "l"); /* need "ll" for long long */
605 strcat (format, local_hex_format_specifier ()); /* "x" */
606 strcat (format, local_hex_format_suffix ()); /* "" */
607 sprintf (res2, format, num);
608 return res2;
609 #else /* !defined (PRINTF_HAS_LONG_LONG) */
610 /* Use strcat_address_numeric to print the number into a string, then
611 build the result string from local_hex_format_prefix, padding and
612 the hex representation as indicated by "width". */
613
614 temp_nbr_buf[0] = 0;
615 /* With use_local == 0, we don't get the leading "0x" prefix. */
616 /* MERGEBUG ?? As a quick fix I am replacing this call to
617 strcat_address_numeric with sprintf
618 strcat_address_numeric(num, 0, temp_nbr_buf, RESULT_BUF_LEN);
619 */
620
621 {
622 long long ll = num;
623 sprintf (temp_nbr_buf, "%llx", ll);
624 }
625 /* parse width */
626 parse_ptr = width;
627 pad_on_left = 1;
628 pad_char = " ";
629 if (*parse_ptr == '-')
630 {
631 parse_ptr++;
632 pad_on_left = 0;
633 }
634 if (*parse_ptr == '0')
635 {
636 parse_ptr++;
637 if (pad_on_left)
638 pad_char = "0"; /* If padding is on the right, it is blank */
639 }
640 field_width = atoi (parse_ptr);
641 num_len = strlen (temp_nbr_buf);
642 num_pad_chars = field_width - strlen (temp_nbr_buf); /* possibly negative */
643
644 if (strlen (local_hex_format_prefix ()) + num_len + num_pad_chars
645 < RESULT_BUF_LEN) /* paranoia */
646 internal_error ("longest_local_hex_string_custom: insufficient space to store result");
647
648 strcpy (res2, local_hex_format_prefix ());
649 if (pad_on_left)
650 {
651 while (num_pad_chars > 0)
652 {
653 strcat (res2, pad_char);
654 num_pad_chars--;
655 }
656 }
657 strcat (res2, temp_nbr_buf);
658 if (!pad_on_left)
659 {
660 while (num_pad_chars > 0)
661 {
662 strcat (res2, pad_char);
663 num_pad_chars--;
664 }
665 }
666 return res2;
667 #endif
668
669 } /* longest_local_hex_string_custom */
670
671 /* Returns the appropriate printf format for octal
672 numbers. */
673 char *
674 local_octal_format_custom (char *pre)
675 {
676 static char form[50];
677
678 strcpy (form, local_octal_format_prefix ());
679 strcat (form, "%");
680 strcat (form, pre);
681 strcat (form, local_octal_format_specifier ());
682 strcat (form, local_octal_format_suffix ());
683 return form;
684 }
685
686 /* Returns the appropriate printf format for decimal numbers. */
687 char *
688 local_decimal_format_custom (char *pre)
689 {
690 static char form[50];
691
692 strcpy (form, local_decimal_format_prefix ());
693 strcat (form, "%");
694 strcat (form, pre);
695 strcat (form, local_decimal_format_specifier ());
696 strcat (form, local_decimal_format_suffix ());
697 return form;
698 }
699 \f
700 #if 0
701 /* This page contains functions that are used in type/range checking.
702 They all return zero if the type/range check fails.
703
704 It is hoped that these will make extending GDB to parse different
705 languages a little easier. These are primarily used in eval.c when
706 evaluating expressions and making sure that their types are correct.
707 Instead of having a mess of conjucted/disjuncted expressions in an "if",
708 the ideas of type can be wrapped up in the following functions.
709
710 Note that some of them are not currently dependent upon which language
711 is currently being parsed. For example, floats are the same in
712 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
713 TYPE_CODE_FLT), while booleans are different. */
714
715 /* Returns non-zero if its argument is a simple type. This is the same for
716 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
717 and thus will never cause the failure of the test. */
718 int
719 simple_type (struct type *type)
720 {
721 CHECK_TYPEDEF (type);
722 switch (TYPE_CODE (type))
723 {
724 case TYPE_CODE_INT:
725 case TYPE_CODE_CHAR:
726 case TYPE_CODE_ENUM:
727 case TYPE_CODE_FLT:
728 case TYPE_CODE_RANGE:
729 case TYPE_CODE_BOOL:
730 return 1;
731
732 default:
733 return 0;
734 }
735 }
736
737 /* Returns non-zero if its argument is of an ordered type.
738 An ordered type is one in which the elements can be tested for the
739 properties of "greater than", "less than", etc, or for which the
740 operations "increment" or "decrement" make sense. */
741 int
742 ordered_type (struct type *type)
743 {
744 CHECK_TYPEDEF (type);
745 switch (TYPE_CODE (type))
746 {
747 case TYPE_CODE_INT:
748 case TYPE_CODE_CHAR:
749 case TYPE_CODE_ENUM:
750 case TYPE_CODE_FLT:
751 case TYPE_CODE_RANGE:
752 return 1;
753
754 default:
755 return 0;
756 }
757 }
758
759 /* Returns non-zero if the two types are the same */
760 int
761 same_type (struct type *arg1, struct type *arg2)
762 {
763 CHECK_TYPEDEF (type);
764 if (structured_type (arg1) ? !structured_type (arg2) : structured_type (arg2))
765 /* One is structured and one isn't */
766 return 0;
767 else if (structured_type (arg1) && structured_type (arg2))
768 return arg1 == arg2;
769 else if (numeric_type (arg1) && numeric_type (arg2))
770 return (TYPE_CODE (arg2) == TYPE_CODE (arg1)) &&
771 (TYPE_UNSIGNED (arg1) == TYPE_UNSIGNED (arg2))
772 ? 1 : 0;
773 else
774 return arg1 == arg2;
775 }
776
777 /* Returns non-zero if the type is integral */
778 int
779 integral_type (struct type *type)
780 {
781 CHECK_TYPEDEF (type);
782 switch (current_language->la_language)
783 {
784 case language_c:
785 case language_cplus:
786 return (TYPE_CODE (type) != TYPE_CODE_INT) &&
787 (TYPE_CODE (type) != TYPE_CODE_ENUM) ? 0 : 1;
788 case language_m2:
789 case language_pascal:
790 return TYPE_CODE (type) != TYPE_CODE_INT ? 0 : 1;
791 case language_chill:
792 error ("Missing Chill support in function integral_type."); /*FIXME */
793 default:
794 error ("Language not supported.");
795 }
796 }
797
798 /* Returns non-zero if the value is numeric */
799 int
800 numeric_type (struct type *type)
801 {
802 CHECK_TYPEDEF (type);
803 switch (TYPE_CODE (type))
804 {
805 case TYPE_CODE_INT:
806 case TYPE_CODE_FLT:
807 return 1;
808
809 default:
810 return 0;
811 }
812 }
813
814 /* Returns non-zero if the value is a character type */
815 int
816 character_type (struct type *type)
817 {
818 CHECK_TYPEDEF (type);
819 switch (current_language->la_language)
820 {
821 case language_chill:
822 case language_m2:
823 case language_pascal:
824 return TYPE_CODE (type) != TYPE_CODE_CHAR ? 0 : 1;
825
826 case language_c:
827 case language_cplus:
828 return (TYPE_CODE (type) == TYPE_CODE_INT) &&
829 TYPE_LENGTH (type) == sizeof (char)
830 ? 1 : 0;
831 default:
832 return (0);
833 }
834 }
835
836 /* Returns non-zero if the value is a string type */
837 int
838 string_type (struct type *type)
839 {
840 CHECK_TYPEDEF (type);
841 switch (current_language->la_language)
842 {
843 case language_chill:
844 case language_m2:
845 case language_pascal:
846 return TYPE_CODE (type) != TYPE_CODE_STRING ? 0 : 1;
847
848 case language_c:
849 case language_cplus:
850 /* C does not have distinct string type. */
851 return (0);
852 default:
853 return (0);
854 }
855 }
856
857 /* Returns non-zero if the value is a boolean type */
858 int
859 boolean_type (struct type *type)
860 {
861 CHECK_TYPEDEF (type);
862 if (TYPE_CODE (type) == TYPE_CODE_BOOL)
863 return 1;
864 switch (current_language->la_language)
865 {
866 case language_c:
867 case language_cplus:
868 /* Might be more cleanly handled by having a TYPE_CODE_INT_NOT_BOOL
869 for CHILL and such languages, or a TYPE_CODE_INT_OR_BOOL for C. */
870 if (TYPE_CODE (type) == TYPE_CODE_INT)
871 return 1;
872 default:
873 break;
874 }
875 return 0;
876 }
877
878 /* Returns non-zero if the value is a floating-point type */
879 int
880 float_type (struct type *type)
881 {
882 CHECK_TYPEDEF (type);
883 return TYPE_CODE (type) == TYPE_CODE_FLT;
884 }
885
886 /* Returns non-zero if the value is a pointer type */
887 int
888 pointer_type (struct type *type)
889 {
890 return TYPE_CODE (type) == TYPE_CODE_PTR ||
891 TYPE_CODE (type) == TYPE_CODE_REF;
892 }
893
894 /* Returns non-zero if the value is a structured type */
895 int
896 structured_type (struct type *type)
897 {
898 CHECK_TYPEDEF (type);
899 switch (current_language->la_language)
900 {
901 case language_c:
902 case language_cplus:
903 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
904 (TYPE_CODE (type) == TYPE_CODE_UNION) ||
905 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
906 case language_pascal:
907 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
908 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
909 (TYPE_CODE(type) == TYPE_CODE_SET) ||
910 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
911 case language_m2:
912 return (TYPE_CODE (type) == TYPE_CODE_STRUCT) ||
913 (TYPE_CODE (type) == TYPE_CODE_SET) ||
914 (TYPE_CODE (type) == TYPE_CODE_ARRAY);
915 case language_chill:
916 error ("Missing Chill support in function structured_type."); /*FIXME */
917 default:
918 return (0);
919 }
920 }
921 #endif
922 \f
923 struct type *
924 lang_bool_type (void)
925 {
926 struct symbol *sym;
927 struct type *type;
928 switch (current_language->la_language)
929 {
930 case language_chill:
931 return builtin_type_chill_bool;
932 case language_fortran:
933 sym = lookup_symbol ("logical", NULL, VAR_NAMESPACE, NULL, NULL);
934 if (sym)
935 {
936 type = SYMBOL_TYPE (sym);
937 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
938 return type;
939 }
940 return builtin_type_f_logical_s2;
941 case language_cplus:
942 case language_pascal:
943 if (current_language->la_language==language_cplus)
944 {sym = lookup_symbol ("bool", NULL, VAR_NAMESPACE, NULL, NULL);}
945 else
946 {sym = lookup_symbol ("boolean", NULL, VAR_NAMESPACE, NULL, NULL);}
947 if (sym)
948 {
949 type = SYMBOL_TYPE (sym);
950 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
951 return type;
952 }
953 return builtin_type_bool;
954 case language_java:
955 sym = lookup_symbol ("boolean", NULL, VAR_NAMESPACE, NULL, NULL);
956 if (sym)
957 {
958 type = SYMBOL_TYPE (sym);
959 if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
960 return type;
961 }
962 return java_boolean_type;
963 default:
964 return builtin_type_int;
965 }
966 }
967 \f
968 /* This page contains functions that return info about
969 (struct value) values used in GDB. */
970
971 /* Returns non-zero if the value VAL represents a true value. */
972 int
973 value_true (value_ptr val)
974 {
975 /* It is possible that we should have some sort of error if a non-boolean
976 value is used in this context. Possibly dependent on some kind of
977 "boolean-checking" option like range checking. But it should probably
978 not depend on the language except insofar as is necessary to identify
979 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
980 should be an error, probably). */
981 return !value_logical_not (val);
982 }
983 \f
984 /* Returns non-zero if the operator OP is defined on
985 the values ARG1 and ARG2. */
986
987 #if 0 /* Currently unused */
988
989 void
990 binop_type_check (value_ptr arg1, value_ptr arg2, int op)
991 {
992 struct type *t1, *t2;
993
994 /* If we're not checking types, always return success. */
995 if (!STRICT_TYPE)
996 return;
997
998 t1 = VALUE_TYPE (arg1);
999 if (arg2 != NULL)
1000 t2 = VALUE_TYPE (arg2);
1001 else
1002 t2 = NULL;
1003
1004 switch (op)
1005 {
1006 case BINOP_ADD:
1007 case BINOP_SUB:
1008 if ((numeric_type (t1) && pointer_type (t2)) ||
1009 (pointer_type (t1) && numeric_type (t2)))
1010 {
1011 warning ("combining pointer and integer.\n");
1012 break;
1013 }
1014 case BINOP_MUL:
1015 case BINOP_LSH:
1016 case BINOP_RSH:
1017 if (!numeric_type (t1) || !numeric_type (t2))
1018 type_op_error ("Arguments to %s must be numbers.", op);
1019 else if (!same_type (t1, t2))
1020 type_op_error ("Arguments to %s must be of the same type.", op);
1021 break;
1022
1023 case BINOP_LOGICAL_AND:
1024 case BINOP_LOGICAL_OR:
1025 if (!boolean_type (t1) || !boolean_type (t2))
1026 type_op_error ("Arguments to %s must be of boolean type.", op);
1027 break;
1028
1029 case BINOP_EQUAL:
1030 if ((pointer_type (t1) && !(pointer_type (t2) || integral_type (t2))) ||
1031 (pointer_type (t2) && !(pointer_type (t1) || integral_type (t1))))
1032 type_op_error ("A pointer can only be compared to an integer or pointer.", op);
1033 else if ((pointer_type (t1) && integral_type (t2)) ||
1034 (integral_type (t1) && pointer_type (t2)))
1035 {
1036 warning ("combining integer and pointer.\n");
1037 break;
1038 }
1039 else if (!simple_type (t1) || !simple_type (t2))
1040 type_op_error ("Arguments to %s must be of simple type.", op);
1041 else if (!same_type (t1, t2))
1042 type_op_error ("Arguments to %s must be of the same type.", op);
1043 break;
1044
1045 case BINOP_REM:
1046 case BINOP_MOD:
1047 if (!integral_type (t1) || !integral_type (t2))
1048 type_op_error ("Arguments to %s must be of integral type.", op);
1049 break;
1050
1051 case BINOP_LESS:
1052 case BINOP_GTR:
1053 case BINOP_LEQ:
1054 case BINOP_GEQ:
1055 if (!ordered_type (t1) || !ordered_type (t2))
1056 type_op_error ("Arguments to %s must be of ordered type.", op);
1057 else if (!same_type (t1, t2))
1058 type_op_error ("Arguments to %s must be of the same type.", op);
1059 break;
1060
1061 case BINOP_ASSIGN:
1062 if (pointer_type (t1) && !integral_type (t2))
1063 type_op_error ("A pointer can only be assigned an integer.", op);
1064 else if (pointer_type (t1) && integral_type (t2))
1065 {
1066 warning ("combining integer and pointer.");
1067 break;
1068 }
1069 else if (!simple_type (t1) || !simple_type (t2))
1070 type_op_error ("Arguments to %s must be of simple type.", op);
1071 else if (!same_type (t1, t2))
1072 type_op_error ("Arguments to %s must be of the same type.", op);
1073 break;
1074
1075 case BINOP_CONCAT:
1076 /* FIXME: Needs to handle bitstrings as well. */
1077 if (!(string_type (t1) || character_type (t1) || integral_type (t1))
1078 || !(string_type (t2) || character_type (t2) || integral_type (t2)))
1079 type_op_error ("Arguments to %s must be strings or characters.", op);
1080 break;
1081
1082 /* Unary checks -- arg2 is null */
1083
1084 case UNOP_LOGICAL_NOT:
1085 if (!boolean_type (t1))
1086 type_op_error ("Argument to %s must be of boolean type.", op);
1087 break;
1088
1089 case UNOP_PLUS:
1090 case UNOP_NEG:
1091 if (!numeric_type (t1))
1092 type_op_error ("Argument to %s must be of numeric type.", op);
1093 break;
1094
1095 case UNOP_IND:
1096 if (integral_type (t1))
1097 {
1098 warning ("combining pointer and integer.\n");
1099 break;
1100 }
1101 else if (!pointer_type (t1))
1102 type_op_error ("Argument to %s must be a pointer.", op);
1103 break;
1104
1105 case UNOP_PREINCREMENT:
1106 case UNOP_POSTINCREMENT:
1107 case UNOP_PREDECREMENT:
1108 case UNOP_POSTDECREMENT:
1109 if (!ordered_type (t1))
1110 type_op_error ("Argument to %s must be of an ordered type.", op);
1111 break;
1112
1113 default:
1114 /* Ok. The following operators have different meanings in
1115 different languages. */
1116 switch (current_language->la_language)
1117 {
1118 #ifdef _LANG_c
1119 case language_c:
1120 case language_cplus:
1121 switch (op)
1122 {
1123 case BINOP_DIV:
1124 if (!numeric_type (t1) || !numeric_type (t2))
1125 type_op_error ("Arguments to %s must be numbers.", op);
1126 break;
1127 }
1128 break;
1129 #endif
1130
1131 #ifdef _LANG_m2
1132 case language_m2:
1133 switch (op)
1134 {
1135 case BINOP_DIV:
1136 if (!float_type (t1) || !float_type (t2))
1137 type_op_error ("Arguments to %s must be floating point numbers.", op);
1138 break;
1139 case BINOP_INTDIV:
1140 if (!integral_type (t1) || !integral_type (t2))
1141 type_op_error ("Arguments to %s must be of integral type.", op);
1142 break;
1143 }
1144 #endif
1145
1146 #ifdef _LANG_pascal
1147 case language_pascal:
1148 switch(op)
1149 {
1150 case BINOP_DIV:
1151 if (!float_type(t1) && !float_type(t2))
1152 type_op_error ("Arguments to %s must be floating point numbers.",op);
1153 break;
1154 case BINOP_INTDIV:
1155 if (!integral_type(t1) || !integral_type(t2))
1156 type_op_error ("Arguments to %s must be of integral type.",op);
1157 break;
1158 }
1159 #endif
1160
1161 #ifdef _LANG_chill
1162 case language_chill:
1163 error ("Missing Chill support in function binop_type_check."); /*FIXME */
1164 #endif
1165
1166 }
1167 }
1168 }
1169
1170 #endif /* 0 */
1171 \f
1172
1173 /* This page contains functions for the printing out of
1174 error messages that occur during type- and range-
1175 checking. */
1176
1177 /* Prints the format string FMT with the operator as a string
1178 corresponding to the opcode OP. If FATAL is non-zero, then
1179 this is an error and error () is called. Otherwise, it is
1180 a warning and printf() is called. */
1181 void
1182 op_error (char *fmt, enum exp_opcode op, int fatal)
1183 {
1184 if (fatal)
1185 error (fmt, op_string (op));
1186 else
1187 {
1188 warning (fmt, op_string (op));
1189 }
1190 }
1191
1192 /* These are called when a language fails a type- or range-check.
1193 The first argument should be a printf()-style format string, and
1194 the rest of the arguments should be its arguments. If
1195 [type|range]_check is [type|range]_check_on, then return_to_top_level()
1196 is called in the style of error (). Otherwise, the message is prefixed
1197 by the value of warning_pre_print and we do not return to the top level. */
1198
1199 void
1200 type_error (char *string,...)
1201 {
1202 va_list args;
1203 va_start (args, string);
1204
1205 if (type_check == type_check_warn)
1206 fprintf_filtered (gdb_stderr, warning_pre_print);
1207 else
1208 error_begin ();
1209
1210 vfprintf_filtered (gdb_stderr, string, args);
1211 fprintf_filtered (gdb_stderr, "\n");
1212 va_end (args);
1213 if (type_check == type_check_on)
1214 return_to_top_level (RETURN_ERROR);
1215 }
1216
1217 void
1218 range_error (char *string,...)
1219 {
1220 va_list args;
1221 va_start (args, string);
1222
1223 if (range_check == range_check_warn)
1224 fprintf_filtered (gdb_stderr, warning_pre_print);
1225 else
1226 error_begin ();
1227
1228 vfprintf_filtered (gdb_stderr, string, args);
1229 fprintf_filtered (gdb_stderr, "\n");
1230 va_end (args);
1231 if (range_check == range_check_on)
1232 return_to_top_level (RETURN_ERROR);
1233 }
1234 \f
1235
1236 /* This page contains miscellaneous functions */
1237
1238 /* Return the language enum for a given language string. */
1239
1240 enum language
1241 language_enum (char *str)
1242 {
1243 int i;
1244
1245 for (i = 0; i < languages_size; i++)
1246 if (STREQ (languages[i]->la_name, str))
1247 return languages[i]->la_language;
1248
1249 return language_unknown;
1250 }
1251
1252 /* Return the language struct for a given language enum. */
1253
1254 const struct language_defn *
1255 language_def (enum language lang)
1256 {
1257 int i;
1258
1259 for (i = 0; i < languages_size; i++)
1260 {
1261 if (languages[i]->la_language == lang)
1262 {
1263 return languages[i];
1264 }
1265 }
1266 return NULL;
1267 }
1268
1269 /* Return the language as a string */
1270 char *
1271 language_str (enum language lang)
1272 {
1273 int i;
1274
1275 for (i = 0; i < languages_size; i++)
1276 {
1277 if (languages[i]->la_language == lang)
1278 {
1279 return languages[i]->la_name;
1280 }
1281 }
1282 return "Unknown";
1283 }
1284
1285 static void
1286 set_check (char *ignore, int from_tty)
1287 {
1288 printf_unfiltered (
1289 "\"set check\" must be followed by the name of a check subcommand.\n");
1290 help_list (setchecklist, "set check ", -1, gdb_stdout);
1291 }
1292
1293 static void
1294 show_check (char *ignore, int from_tty)
1295 {
1296 cmd_show_list (showchecklist, from_tty, "");
1297 }
1298 \f
1299 /* Add a language to the set of known languages. */
1300
1301 void
1302 add_language (const struct language_defn *lang)
1303 {
1304 if (lang->la_magic != LANG_MAGIC)
1305 {
1306 fprintf_unfiltered (gdb_stderr, "Magic number of %s language struct wrong\n",
1307 lang->la_name);
1308 abort ();
1309 }
1310
1311 if (!languages)
1312 {
1313 languages_allocsize = DEFAULT_ALLOCSIZE;
1314 languages = (const struct language_defn **) xmalloc
1315 (languages_allocsize * sizeof (*languages));
1316 }
1317 if (languages_size >= languages_allocsize)
1318 {
1319 languages_allocsize *= 2;
1320 languages = (const struct language_defn **) xrealloc ((char *) languages,
1321 languages_allocsize * sizeof (*languages));
1322 }
1323 languages[languages_size++] = lang;
1324 }
1325
1326 /* Define the language that is no language. */
1327
1328 static int
1329 unk_lang_parser (void)
1330 {
1331 return 1;
1332 }
1333
1334 static void
1335 unk_lang_error (char *msg)
1336 {
1337 error ("Attempted to parse an expression with unknown language");
1338 }
1339
1340 static void
1341 unk_lang_emit_char (register int c, struct ui_file *stream, int quoter)
1342 {
1343 error ("internal error - unimplemented function unk_lang_emit_char called.");
1344 }
1345
1346 static void
1347 unk_lang_printchar (register int c, struct ui_file *stream)
1348 {
1349 error ("internal error - unimplemented function unk_lang_printchar called.");
1350 }
1351
1352 static void
1353 unk_lang_printstr (struct ui_file *stream, char *string, unsigned int length,
1354 int width, int force_ellipses)
1355 {
1356 error ("internal error - unimplemented function unk_lang_printstr called.");
1357 }
1358
1359 static struct type *
1360 unk_lang_create_fundamental_type (struct objfile *objfile, int typeid)
1361 {
1362 error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1363 }
1364
1365 static void
1366 unk_lang_print_type (struct type *type, char *varstring, struct ui_file *stream,
1367 int show, int level)
1368 {
1369 error ("internal error - unimplemented function unk_lang_print_type called.");
1370 }
1371
1372 static int
1373 unk_lang_val_print (struct type *type, char *valaddr, int embedded_offset,
1374 CORE_ADDR address, struct ui_file *stream, int format,
1375 int deref_ref, int recurse, enum val_prettyprint pretty)
1376 {
1377 error ("internal error - unimplemented function unk_lang_val_print called.");
1378 }
1379
1380 static int
1381 unk_lang_value_print (value_ptr val, struct ui_file *stream, int format,
1382 enum val_prettyprint pretty)
1383 {
1384 error ("internal error - unimplemented function unk_lang_value_print called.");
1385 }
1386
1387 static struct type **CONST_PTR (unknown_builtin_types[]) =
1388 {
1389 0
1390 };
1391 static const struct op_print unk_op_print_tab[] =
1392 {
1393 {NULL, OP_NULL, PREC_NULL, 0}
1394 };
1395
1396 const struct language_defn unknown_language_defn =
1397 {
1398 "unknown",
1399 language_unknown,
1400 &unknown_builtin_types[0],
1401 range_check_off,
1402 type_check_off,
1403 unk_lang_parser,
1404 unk_lang_error,
1405 evaluate_subexp_standard,
1406 unk_lang_printchar, /* Print character constant */
1407 unk_lang_printstr,
1408 unk_lang_emit_char,
1409 unk_lang_create_fundamental_type,
1410 unk_lang_print_type, /* Print a type using appropriate syntax */
1411 unk_lang_val_print, /* Print a value using appropriate syntax */
1412 unk_lang_value_print, /* Print a top-level value */
1413 {"", "", "", ""}, /* Binary format info */
1414 {"0%lo", "0", "o", ""}, /* Octal format info */
1415 {"%ld", "", "d", ""}, /* Decimal format info */
1416 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1417 unk_op_print_tab, /* expression operators for printing */
1418 1, /* c-style arrays */
1419 0, /* String lower bound */
1420 &builtin_type_char, /* Type of string elements */
1421 LANG_MAGIC
1422 };
1423
1424 /* These two structs define fake entries for the "local" and "auto" options. */
1425 const struct language_defn auto_language_defn =
1426 {
1427 "auto",
1428 language_auto,
1429 &unknown_builtin_types[0],
1430 range_check_off,
1431 type_check_off,
1432 unk_lang_parser,
1433 unk_lang_error,
1434 evaluate_subexp_standard,
1435 unk_lang_printchar, /* Print character constant */
1436 unk_lang_printstr,
1437 unk_lang_emit_char,
1438 unk_lang_create_fundamental_type,
1439 unk_lang_print_type, /* Print a type using appropriate syntax */
1440 unk_lang_val_print, /* Print a value using appropriate syntax */
1441 unk_lang_value_print, /* Print a top-level value */
1442 {"", "", "", ""}, /* Binary format info */
1443 {"0%lo", "0", "o", ""}, /* Octal format info */
1444 {"%ld", "", "d", ""}, /* Decimal format info */
1445 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1446 unk_op_print_tab, /* expression operators for printing */
1447 1, /* c-style arrays */
1448 0, /* String lower bound */
1449 &builtin_type_char, /* Type of string elements */
1450 LANG_MAGIC
1451 };
1452
1453 const struct language_defn local_language_defn =
1454 {
1455 "local",
1456 language_auto,
1457 &unknown_builtin_types[0],
1458 range_check_off,
1459 type_check_off,
1460 unk_lang_parser,
1461 unk_lang_error,
1462 evaluate_subexp_standard,
1463 unk_lang_printchar, /* Print character constant */
1464 unk_lang_printstr,
1465 unk_lang_emit_char,
1466 unk_lang_create_fundamental_type,
1467 unk_lang_print_type, /* Print a type using appropriate syntax */
1468 unk_lang_val_print, /* Print a value using appropriate syntax */
1469 unk_lang_value_print, /* Print a top-level value */
1470 {"", "", "", ""}, /* Binary format info */
1471 {"0%lo", "0", "o", ""}, /* Octal format info */
1472 {"%ld", "", "d", ""}, /* Decimal format info */
1473 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1474 unk_op_print_tab, /* expression operators for printing */
1475 1, /* c-style arrays */
1476 0, /* String lower bound */
1477 &builtin_type_char, /* Type of string elements */
1478 LANG_MAGIC
1479 };
1480 \f
1481 /* Initialize the language routines */
1482
1483 void
1484 _initialize_language (void)
1485 {
1486 struct cmd_list_element *set, *show;
1487
1488 /* GDB commands for language specific stuff */
1489
1490 set = add_set_cmd ("language", class_support, var_string_noescape,
1491 (char *) &language,
1492 "Set the current source language.",
1493 &setlist);
1494 show = add_show_from_set (set, &showlist);
1495 set->function.cfunc = set_language_command;
1496 show->function.cfunc = show_language_command;
1497
1498 add_prefix_cmd ("check", no_class, set_check,
1499 "Set the status of the type/range checker",
1500 &setchecklist, "set check ", 0, &setlist);
1501 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1502 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1503
1504 add_prefix_cmd ("check", no_class, show_check,
1505 "Show the status of the type/range checker",
1506 &showchecklist, "show check ", 0, &showlist);
1507 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1508 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1509
1510 set = add_set_cmd ("type", class_support, var_string_noescape,
1511 (char *) &type,
1512 "Set type checking. (on/warn/off/auto)",
1513 &setchecklist);
1514 show = add_show_from_set (set, &showchecklist);
1515 set->function.cfunc = set_type_command;
1516 show->function.cfunc = show_type_command;
1517
1518 set = add_set_cmd ("range", class_support, var_string_noescape,
1519 (char *) &range,
1520 "Set range checking. (on/warn/off/auto)",
1521 &setchecklist);
1522 show = add_show_from_set (set, &showchecklist);
1523 set->function.cfunc = set_range_command;
1524 show->function.cfunc = show_range_command;
1525
1526 add_language (&unknown_language_defn);
1527 add_language (&local_language_defn);
1528 add_language (&auto_language_defn);
1529
1530 language = savestring ("auto", strlen ("auto"));
1531 set_language_command (language, 0);
1532
1533 type = savestring ("auto", strlen ("auto"));
1534 set_type_command (NULL, 0);
1535
1536 range = savestring ("auto", strlen ("auto"));
1537 set_range_command (NULL, 0);
1538 }
This page took 0.065983 seconds and 4 git commands to generate.