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