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