* eval.c (evaluate_subexp): Fix OP_ARRAY, remove code that
[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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 /* This file contains functions that return things that are specific
23 to languages. Each function should examine current_language if necessary,
24 and return the appropriate result. */
25
26 /* FIXME: Most of these would be better organized as macros which
27 return data out of a "language-specific" struct pointer that is set
28 whenever the working language changes. That would be a lot faster. */
29
30 #include "defs.h"
31 #include <string.h>
32 #include <varargs.h>
33
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "value.h"
37 #include "gdbcmd.h"
38 #include "frame.h"
39 #include "expression.h"
40 #include "language.h"
41 #include "target.h"
42 #include "parser-defs.h"
43
44 static void
45 show_language_command PARAMS ((char *, int));
46
47 static void
48 set_language_command PARAMS ((char *, int));
49
50 static void
51 show_type_command PARAMS ((char *, int));
52
53 static void
54 set_type_command PARAMS ((char *, int));
55
56 static void
57 show_range_command PARAMS ((char *, int));
58
59 static void
60 set_range_command PARAMS ((char *, int));
61
62 static void
63 set_range_str PARAMS ((void));
64
65 static void
66 set_type_str PARAMS ((void));
67
68 static void
69 set_lang_str PARAMS ((void));
70
71 static void
72 unk_lang_error PARAMS ((char *));
73
74 static int
75 unk_lang_parser PARAMS ((void));
76
77 static void
78 show_check PARAMS ((char *, int));
79
80 static void
81 set_check PARAMS ((char *, int));
82
83 static void
84 set_type_range PARAMS ((void));
85
86 /* Forward declaration */
87 extern const struct language_defn unknown_language_defn;
88 extern char *warning_pre_print;
89
90 /* The current (default at startup) state of type and range checking.
91 (If the modes are set to "auto", though, these are changed based
92 on the default language at startup, and then again based on the
93 language of the first source file. */
94
95 enum range_mode range_mode = range_mode_auto;
96 enum range_check range_check = range_check_off;
97 enum type_mode type_mode = type_mode_auto;
98 enum type_check type_check = type_check_off;
99
100 /* The current language and language_mode (see language.h) */
101
102 const struct language_defn *current_language = &unknown_language_defn;
103 enum language_mode language_mode = language_mode_auto;
104
105 /* The language that the user expects to be typing in (the language
106 of main(), or the last language we notified them about, or C). */
107
108 const struct language_defn *expected_language;
109
110 /* The list of supported languages. The list itself is malloc'd. */
111
112 static const struct language_defn **languages;
113 static unsigned languages_size;
114 static unsigned languages_allocsize;
115 #define DEFAULT_ALLOCSIZE 4
116
117 /* The "set language/type/range" commands all put stuff in these
118 buffers. This is to make them work as set/show commands. The
119 user's string is copied here, then the set_* commands look at
120 them and update them to something that looks nice when it is
121 printed out. */
122
123 static char *language;
124 static char *type;
125 static char *range;
126
127 /* Warning issued when current_language and the language of the current
128 frame do not match. */
129 char lang_frame_mismatch_warn[] =
130 "Warning: the current language does not match this frame.";
131
132 \f
133 /* This page contains the functions corresponding to GDB commands
134 and their helpers. */
135
136 /* Show command. Display a warning if the language set
137 does not match the frame. */
138 static void
139 show_language_command (ignore, from_tty)
140 char *ignore;
141 int from_tty;
142 {
143 enum language flang; /* The language of the current frame */
144
145 flang = get_frame_language();
146 if (flang != language_unknown &&
147 language_mode == language_mode_manual &&
148 current_language->la_language != flang)
149 printf_filtered("%s\n",lang_frame_mismatch_warn);
150 }
151
152 /* Set command. Change the current working language. */
153 static void
154 set_language_command (ignore, from_tty)
155 char *ignore;
156 int from_tty;
157 {
158 int i;
159 enum language flang;
160 char *err_lang;
161
162 /* FIXME -- do this from the list, with HELP. */
163 if (!language || !language[0]) {
164 printf("The currently understood settings are:\n\n");
165 printf ("local or auto Automatic setting based on source file\n");
166 printf ("c Use the C language\n");
167 printf ("c++ Use the C++ language\n");
168 /* start-sanitize-chill */
169 printf ("chill Use the Chill language\n");
170 /* end-sanitize-chill */
171 printf ("modula-2 Use the Modula-2 language\n");
172 /* Restore the silly string. */
173 set_language(current_language->la_language);
174 return;
175 }
176
177 /* Search the list of languages for a match. */
178 for (i = 0; i < languages_size; i++) {
179 if (STREQ (languages[i]->la_name, language)) {
180 /* Found it! Go into manual mode, and use this language. */
181 if (languages[i]->la_language == language_auto) {
182 /* Enter auto mode. Set to the current frame's language, if known. */
183 language_mode = language_mode_auto;
184 flang = get_frame_language();
185 if (flang!=language_unknown)
186 set_language(flang);
187 expected_language = current_language;
188 return;
189 } else {
190 /* Enter manual mode. Set the specified language. */
191 language_mode = language_mode_manual;
192 current_language = languages[i];
193 set_type_range ();
194 set_lang_str();
195 expected_language = current_language;
196 return;
197 }
198 }
199 }
200
201 /* Reset the language (esp. the global string "language") to the
202 correct values. */
203 err_lang=savestring(language,strlen(language));
204 make_cleanup (free, err_lang); /* Free it after error */
205 set_language(current_language->la_language);
206 error ("Unknown language `%s'.",err_lang);
207 }
208
209 /* Show command. Display a warning if the type setting does
210 not match the current language. */
211 static void
212 show_type_command(ignore, from_tty)
213 char *ignore;
214 int from_tty;
215 {
216 if (type_check != current_language->la_type_check)
217 printf(
218 "Warning: the current type check setting does not match the language.\n");
219 }
220
221 /* Set command. Change the setting for type checking. */
222 static void
223 set_type_command(ignore, from_tty)
224 char *ignore;
225 int from_tty;
226 {
227 if (STREQ(type,"on"))
228 {
229 type_check = type_check_on;
230 type_mode = type_mode_manual;
231 }
232 else if (STREQ(type,"warn"))
233 {
234 type_check = type_check_warn;
235 type_mode = type_mode_manual;
236 }
237 else if (STREQ(type,"off"))
238 {
239 type_check = type_check_off;
240 type_mode = type_mode_manual;
241 }
242 else if (STREQ(type,"auto"))
243 {
244 type_mode = type_mode_auto;
245 set_type_range();
246 /* Avoid hitting the set_type_str call below. We
247 did it in set_type_range. */
248 return;
249 }
250 set_type_str();
251 show_type_command((char *)NULL, from_tty);
252 }
253
254 /* Show command. Display a warning if the range setting does
255 not match the current language. */
256 static void
257 show_range_command(ignore, from_tty)
258 char *ignore;
259 int from_tty;
260 {
261
262 if (range_check != current_language->la_range_check)
263 printf(
264 "Warning: the current range check setting does not match the language.\n");
265 }
266
267 /* Set command. Change the setting for range checking. */
268 static void
269 set_range_command(ignore, from_tty)
270 char *ignore;
271 int from_tty;
272 {
273 if (STREQ(range,"on"))
274 {
275 range_check = range_check_on;
276 range_mode = range_mode_manual;
277 }
278 else if (STREQ(range,"warn"))
279 {
280 range_check = range_check_warn;
281 range_mode = range_mode_manual;
282 }
283 else if (STREQ(range,"off"))
284 {
285 range_check = range_check_off;
286 range_mode = range_mode_manual;
287 }
288 else if (STREQ(range,"auto"))
289 {
290 range_mode = range_mode_auto;
291 set_type_range();
292 /* Avoid hitting the set_range_str call below. We
293 did it in set_type_range. */
294 return;
295 }
296 set_range_str();
297 show_range_command((char *)0, from_tty);
298 }
299
300 /* Set the status of range and type checking based on
301 the current modes and the current language.
302 If SHOW is non-zero, then print out the current language,
303 type and range checking status. */
304 static void
305 set_type_range()
306 {
307
308 if (range_mode == range_mode_auto)
309 range_check = current_language->la_range_check;
310
311 if (type_mode == type_mode_auto)
312 type_check = current_language->la_type_check;
313
314 set_type_str();
315 set_range_str();
316 }
317
318 /* Set current language to (enum language) LANG. */
319
320 void
321 set_language(lang)
322 enum language lang;
323 {
324 int i;
325
326 for (i = 0; i < languages_size; i++) {
327 if (languages[i]->la_language == lang) {
328 current_language = languages[i];
329 set_type_range ();
330 set_lang_str();
331 break;
332 }
333 }
334 }
335 \f
336 /* This page contains functions that update the global vars
337 language, type and range. */
338 static void
339 set_lang_str()
340 {
341 char *prefix = "";
342
343 free (language);
344 if (language_mode == language_mode_auto)
345 prefix = "auto; currently ";
346
347 language = concat(prefix, current_language->la_name, NULL);
348 }
349
350 static void
351 set_type_str()
352 {
353 char *tmp, *prefix = "";
354
355 free (type);
356 if (type_mode==type_mode_auto)
357 prefix = "auto; currently ";
358
359 switch(type_check)
360 {
361 case type_check_on:
362 tmp = "on";
363 break;
364 case type_check_off:
365 tmp = "off";
366 break;
367 case type_check_warn:
368 tmp = "warn";
369 break;
370 default:
371 error ("Unrecognized type check setting.");
372 }
373
374 type = concat(prefix,tmp,NULL);
375 }
376
377 static void
378 set_range_str()
379 {
380 char *tmp, *pref = "";
381
382 free (range);
383 if (range_mode==range_mode_auto)
384 pref = "auto; currently ";
385
386 switch(range_check)
387 {
388 case range_check_on:
389 tmp = "on";
390 break;
391 case range_check_off:
392 tmp = "off";
393 break;
394 case range_check_warn:
395 tmp = "warn";
396 break;
397 default:
398 error ("Unrecognized range check setting.");
399 }
400
401 range = concat(pref,tmp,NULL);
402 }
403
404
405 /* Print out the current language settings: language, range and
406 type checking. If QUIETLY, print only what has changed. */
407
408 void
409 language_info (quietly)
410 int quietly;
411 {
412 if (quietly && expected_language == current_language)
413 return;
414
415 expected_language = current_language;
416 printf("Current language: %s\n",language);
417 show_language_command((char *)0, 1);
418
419 if (!quietly)
420 {
421 printf("Type checking: %s\n",type);
422 show_type_command((char *)0, 1);
423 printf("Range checking: %s\n",range);
424 show_range_command((char *)0, 1);
425 }
426 }
427 \f
428 /* Return the result of a binary operation. */
429
430 #if 0 /* Currently unused */
431
432 struct type *
433 binop_result_type(v1,v2)
434 value v1,v2;
435 {
436 int l1,l2,size,uns;
437
438 l1 = TYPE_LENGTH(VALUE_TYPE(v1));
439 l2 = TYPE_LENGTH(VALUE_TYPE(v2));
440
441 switch(current_language->la_language)
442 {
443 case language_c:
444 case language_cplus:
445 if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
446 return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
447 VALUE_TYPE(v2) : VALUE_TYPE(v1);
448 else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
449 return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
450 VALUE_TYPE(v1) : VALUE_TYPE(v2);
451 else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
452 return VALUE_TYPE(v1);
453 else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
454 return VALUE_TYPE(v2);
455 else /* Both are signed. Result is the longer type */
456 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
457 break;
458 case language_m2:
459 /* If we are doing type-checking, l1 should equal l2, so this is
460 not needed. */
461 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
462 break;
463 /* start-sanitize-chill */
464 case language_chill:
465 error ("Missing Chill support in function binop_result_check.");/*FIXME*/
466 /* end-sanitize-chill */
467 }
468 abort();
469 return (struct type *)0; /* For lint */
470 }
471
472 #endif /* 0 */
473
474 \f
475 /* This page contains functions that return format strings for
476 printf for printing out numbers in different formats */
477
478 /* Returns the appropriate printf format for hexadecimal
479 numbers. */
480 char *
481 local_hex_format_custom(pre)
482 char *pre;
483 {
484 static char form[50];
485
486 strcpy (form, local_hex_format_prefix ());
487 strcat (form, "%");
488 strcat (form, pre);
489 strcat (form, local_hex_format_specifier ());
490 strcat (form, local_hex_format_suffix ());
491 return form;
492 }
493
494 /* Converts a number to hexadecimal and stores it in a static
495 string. Returns a pointer to this string. */
496 char *
497 local_hex_string (num)
498 int num;
499 {
500 static char res[50];
501
502 sprintf (res, local_hex_format(), num);
503 return res;
504 }
505
506 /* Converts a number to custom hexadecimal and stores it in a static
507 string. Returns a pointer to this string. */
508 char *
509 local_hex_string_custom(num,pre)
510 int num;
511 char *pre;
512 {
513 static char res[50];
514
515 sprintf (res, local_hex_format_custom(pre), num);
516 return res;
517 }
518
519 /* Returns the appropriate printf format for octal
520 numbers. */
521 char *
522 local_octal_format_custom(pre)
523 char *pre;
524 {
525 static char form[50];
526
527 strcpy (form, local_octal_format_prefix ());
528 strcat (form, "%");
529 strcat (form, pre);
530 strcat (form, local_octal_format_specifier ());
531 strcat (form, local_octal_format_suffix ());
532 return form;
533 }
534 \f
535 /* This page contains functions that are used in type/range checking.
536 They all return zero if the type/range check fails.
537
538 It is hoped that these will make extending GDB to parse different
539 languages a little easier. These are primarily used in eval.c when
540 evaluating expressions and making sure that their types are correct.
541 Instead of having a mess of conjucted/disjuncted expressions in an "if",
542 the ideas of type can be wrapped up in the following functions.
543
544 Note that some of them are not currently dependent upon which language
545 is currently being parsed. For example, floats are the same in
546 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
547 TYPE_CODE_FLT), while booleans are different. */
548
549 /* Returns non-zero if its argument is a simple type. This is the same for
550 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
551 and thus will never cause the failure of the test. */
552 int
553 simple_type(type)
554 struct type *type;
555 {
556 switch (TYPE_CODE (type)) {
557 case TYPE_CODE_INT:
558 case TYPE_CODE_CHAR:
559 case TYPE_CODE_ENUM:
560 case TYPE_CODE_FLT:
561 case TYPE_CODE_RANGE:
562 case TYPE_CODE_BOOL:
563 return 1;
564
565 default:
566 return 0;
567 }
568 }
569
570 /* Returns non-zero if its argument is of an ordered type.
571 An ordered type is one in which the elements can be tested for the
572 properties of "greater than", "less than", etc, or for which the
573 operations "increment" or "decrement" make sense. */
574 int
575 ordered_type (type)
576 struct type *type;
577 {
578 switch (TYPE_CODE (type)) {
579 case TYPE_CODE_INT:
580 case TYPE_CODE_CHAR:
581 case TYPE_CODE_ENUM:
582 case TYPE_CODE_FLT:
583 case TYPE_CODE_RANGE:
584 return 1;
585
586 default:
587 return 0;
588 }
589 }
590
591 /* Returns non-zero if the two types are the same */
592 int
593 same_type (arg1, arg2)
594 struct type *arg1, *arg2;
595 {
596 if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
597 /* One is structured and one isn't */
598 return 0;
599 else if (structured_type(arg1) && structured_type(arg2))
600 return arg1 == arg2;
601 else if (numeric_type(arg1) && numeric_type(arg2))
602 return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
603 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
604 ? 1 : 0;
605 else
606 return arg1==arg2;
607 }
608
609 /* Returns non-zero if the type is integral */
610 int
611 integral_type (type)
612 struct type *type;
613 {
614 switch(current_language->la_language)
615 {
616 case language_c:
617 case language_cplus:
618 return (TYPE_CODE(type) != TYPE_CODE_INT) &&
619 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
620 case language_m2:
621 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
622 /* start-sanitize-chill */
623 case language_chill:
624 error ("Missing Chill support in function integral_type."); /*FIXME*/
625 /* end-sanitize-chill */
626 default:
627 error ("Language not supported.");
628 }
629 }
630
631 /* Returns non-zero if the value is numeric */
632 int
633 numeric_type (type)
634 struct type *type;
635 {
636 switch (TYPE_CODE (type)) {
637 case TYPE_CODE_INT:
638 case TYPE_CODE_FLT:
639 return 1;
640
641 default:
642 return 0;
643 }
644 }
645
646 /* Returns non-zero if the value is a character type */
647 int
648 character_type (type)
649 struct type *type;
650 {
651 switch(current_language->la_language)
652 {
653 /* start-sanitize-chill */
654 case language_chill:
655 /* end-sanitize-chill */
656 case language_m2:
657 return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
658
659 case language_c:
660 case language_cplus:
661 return (TYPE_CODE(type) == TYPE_CODE_INT) &&
662 TYPE_LENGTH(type) == sizeof(char)
663 ? 1 : 0;
664 default:
665 return (0);
666 }
667 }
668
669 /* Returns non-zero if the value is a string type */
670 int
671 string_type (type)
672 struct type *type;
673 {
674 switch(current_language->la_language)
675 {
676 /* start-sanitize-chill */
677 case language_chill:
678 /* end-sanitize-chill */
679 case language_m2:
680 return TYPE_CODE(type) != TYPE_CODE_STRING ? 0 : 1;
681
682 case language_c:
683 case language_cplus:
684 /* C does not have distinct string type. */
685 return (0);
686 default:
687 return (0);
688 }
689 }
690
691 /* Returns non-zero if the value is a boolean type */
692 int
693 boolean_type (type)
694 struct type *type;
695 {
696 switch(current_language->la_language)
697 {
698 /* start-sanitize-chill */
699 case language_chill:
700 /* end-sanitize-chill */
701 case language_m2:
702 return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1;
703
704 case language_c:
705 case language_cplus:
706 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
707 default:
708 return (0);
709 }
710 }
711
712 /* Returns non-zero if the value is a floating-point type */
713 int
714 float_type (type)
715 struct type *type;
716 {
717 return TYPE_CODE(type) == TYPE_CODE_FLT;
718 }
719
720 /* Returns non-zero if the value is a pointer type */
721 int
722 pointer_type(type)
723 struct type *type;
724 {
725 return TYPE_CODE(type) == TYPE_CODE_PTR ||
726 TYPE_CODE(type) == TYPE_CODE_REF;
727 }
728
729 /* Returns non-zero if the value is a structured type */
730 int
731 structured_type(type)
732 struct type *type;
733 {
734 switch(current_language->la_language)
735 {
736 case language_c:
737 case language_cplus:
738 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
739 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
740 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
741 case language_m2:
742 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
743 (TYPE_CODE(type) == TYPE_CODE_SET) ||
744 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
745 /* start-sanitize-chill */
746 case language_chill:
747 error ("Missing Chill support in function structured_type."); /*FIXME*/
748 /* end-sanitize-chill */
749 default:
750 return (0);
751 }
752 }
753 \f
754 /* This page contains functions that return info about
755 (struct value) values used in GDB. */
756
757 /* Returns non-zero if the value VAL represents a true value. */
758 int
759 value_true(val)
760 value val;
761 {
762 int len, i;
763 struct type *type;
764 LONGEST v;
765
766 switch (current_language->la_language) {
767
768 case language_c:
769 case language_cplus:
770 return !value_logical_not (val);
771
772 case language_m2:
773 type = VALUE_TYPE(val);
774 if (TYPE_CODE (type) != TYPE_CODE_BOOL)
775 return 0; /* Not a BOOLEAN at all */
776 /* Search the fields for one that matches the current value. */
777 len = TYPE_NFIELDS (type);
778 v = value_as_long (val);
779 for (i = 0; i < len; i++)
780 {
781 QUIT;
782 if (v == TYPE_FIELD_BITPOS (type, i))
783 break;
784 }
785 if (i >= len)
786 return 0; /* Not a valid BOOLEAN value */
787 if (STREQ ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i)))
788 return 1; /* BOOLEAN with value TRUE */
789 else
790 return 0; /* BOOLEAN with value FALSE */
791 break;
792
793 /* start-sanitize-chill */
794 case language_chill:
795 error ("Missing Chill support in function value_type."); /*FIXME*/
796 /* end-sanitize-chill */
797
798 default:
799 error ("Language not supported.");
800 }
801 }
802 \f
803 /* Returns non-zero if the operator OP is defined on
804 the values ARG1 and ARG2. */
805
806 #if 0 /* Currently unused */
807
808 void
809 binop_type_check(arg1,arg2,op)
810 value arg1,arg2;
811 int op;
812 {
813 struct type *t1, *t2;
814
815 /* If we're not checking types, always return success. */
816 if (!STRICT_TYPE)
817 return;
818
819 t1=VALUE_TYPE(arg1);
820 if (arg2!=(value)NULL)
821 t2=VALUE_TYPE(arg2);
822 else
823 t2=NULL;
824
825 switch(op)
826 {
827 case BINOP_ADD:
828 case BINOP_SUB:
829 if ((numeric_type(t1) && pointer_type(t2)) ||
830 (pointer_type(t1) && numeric_type(t2)))
831 {
832 warning ("combining pointer and integer.\n");
833 break;
834 }
835 case BINOP_MUL:
836 case BINOP_LSH:
837 case BINOP_RSH:
838 if (!numeric_type(t1) || !numeric_type(t2))
839 type_op_error ("Arguments to %s must be numbers.",op);
840 else if (!same_type(t1,t2))
841 type_op_error ("Arguments to %s must be of the same type.",op);
842 break;
843
844 case BINOP_LOGICAL_AND:
845 case BINOP_LOGICAL_OR:
846 if (!boolean_type(t1) || !boolean_type(t2))
847 type_op_error ("Arguments to %s must be of boolean type.",op);
848 break;
849
850 case BINOP_EQUAL:
851 if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
852 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
853 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
854 else if ((pointer_type(t1) && integral_type(t2)) ||
855 (integral_type(t1) && pointer_type(t2)))
856 {
857 warning ("combining integer and pointer.\n");
858 break;
859 }
860 else if (!simple_type(t1) || !simple_type(t2))
861 type_op_error ("Arguments to %s must be of simple type.",op);
862 else if (!same_type(t1,t2))
863 type_op_error ("Arguments to %s must be of the same type.",op);
864 break;
865
866 case BINOP_REM:
867 case BINOP_MOD:
868 if (!integral_type(t1) || !integral_type(t2))
869 type_op_error ("Arguments to %s must be of integral type.",op);
870 break;
871
872 case BINOP_LESS:
873 case BINOP_GTR:
874 case BINOP_LEQ:
875 case BINOP_GEQ:
876 if (!ordered_type(t1) || !ordered_type(t2))
877 type_op_error ("Arguments to %s must be of ordered type.",op);
878 else if (!same_type(t1,t2))
879 type_op_error ("Arguments to %s must be of the same type.",op);
880 break;
881
882 case BINOP_ASSIGN:
883 if (pointer_type(t1) && !integral_type(t2))
884 type_op_error ("A pointer can only be assigned an integer.",op);
885 else if (pointer_type(t1) && integral_type(t2))
886 {
887 warning ("combining integer and pointer.");
888 break;
889 }
890 else if (!simple_type(t1) || !simple_type(t2))
891 type_op_error ("Arguments to %s must be of simple type.",op);
892 else if (!same_type(t1,t2))
893 type_op_error ("Arguments to %s must be of the same type.",op);
894 break;
895
896 case BINOP_CONCAT:
897 if (!(string_type(t1) || character_type(t1))
898 || !(string_type(t2) || character_type(t2)))
899 type_op_error ("Arguments to %s must be strings or characters.", op);
900 break;
901
902 /* Unary checks -- arg2 is null */
903
904 case UNOP_LOGICAL_NOT:
905 if (!boolean_type(t1))
906 type_op_error ("Argument to %s must be of boolean type.",op);
907 break;
908
909 case UNOP_PLUS:
910 case UNOP_NEG:
911 if (!numeric_type(t1))
912 type_op_error ("Argument to %s must be of numeric type.",op);
913 break;
914
915 case UNOP_IND:
916 if (integral_type(t1))
917 {
918 warning ("combining pointer and integer.\n");
919 break;
920 }
921 else if (!pointer_type(t1))
922 type_op_error ("Argument to %s must be a pointer.",op);
923 break;
924
925 case UNOP_PREINCREMENT:
926 case UNOP_POSTINCREMENT:
927 case UNOP_PREDECREMENT:
928 case UNOP_POSTDECREMENT:
929 if (!ordered_type(t1))
930 type_op_error ("Argument to %s must be of an ordered type.",op);
931 break;
932
933 default:
934 /* Ok. The following operators have different meanings in
935 different languages. */
936 switch(current_language->la_language)
937 {
938 #ifdef _LANG_c
939 case language_c:
940 case language_cplus:
941 switch(op)
942 {
943 case BINOP_DIV:
944 if (!numeric_type(t1) || !numeric_type(t2))
945 type_op_error ("Arguments to %s must be numbers.",op);
946 break;
947 }
948 break;
949 #endif
950
951 #ifdef _LANG_m2
952 case language_m2:
953 switch(op)
954 {
955 case BINOP_DIV:
956 if (!float_type(t1) || !float_type(t2))
957 type_op_error ("Arguments to %s must be floating point numbers.",op);
958 break;
959 case BINOP_INTDIV:
960 if (!integral_type(t1) || !integral_type(t2))
961 type_op_error ("Arguments to %s must be of integral type.",op);
962 break;
963 }
964 #endif
965
966 /* start-sanitize-chill */
967 #ifdef _LANG_chill
968 case language_chill:
969 error ("Missing Chill support in function binop_type_check.");/*FIXME*/
970 #endif
971 /* end-sanitize-chill */
972
973 }
974 }
975 }
976
977 #endif /* 0 */
978
979 \f
980 /* This page contains functions for the printing out of
981 error messages that occur during type- and range-
982 checking. */
983
984 /* Prints the format string FMT with the operator as a string
985 corresponding to the opcode OP. If FATAL is non-zero, then
986 this is an error and error () is called. Otherwise, it is
987 a warning and printf() is called. */
988 void
989 op_error (fmt,op,fatal)
990 char *fmt;
991 enum exp_opcode op;
992 int fatal;
993 {
994 if (fatal)
995 error (fmt,op_string(op));
996 else
997 {
998 warning (fmt,op_string(op));
999 }
1000 }
1001
1002 /* These are called when a language fails a type- or range-check.
1003 The first argument should be a printf()-style format string, and
1004 the rest of the arguments should be its arguments. If
1005 [type|range]_check is [type|range]_check_on, then return_to_top_level()
1006 is called in the style of error (). Otherwise, the message is prefixed
1007 by the value of warning_pre_print and we do not return to the top level. */
1008
1009 void
1010 type_error (va_alist)
1011 va_dcl
1012 {
1013 va_list args;
1014 char *string;
1015
1016 if (type_check==type_check_warn)
1017 fprintf(stderr,warning_pre_print);
1018 else
1019 target_terminal_ours();
1020
1021 va_start (args);
1022 string = va_arg (args, char *);
1023 vfprintf (stderr, string, args);
1024 fprintf (stderr, "\n");
1025 va_end (args);
1026 if (type_check==type_check_on)
1027 return_to_top_level();
1028 }
1029
1030 void
1031 range_error (va_alist)
1032 va_dcl
1033 {
1034 va_list args;
1035 char *string;
1036
1037 if (range_check==range_check_warn)
1038 fprintf(stderr,warning_pre_print);
1039 else
1040 target_terminal_ours();
1041
1042 va_start (args);
1043 string = va_arg (args, char *);
1044 vfprintf (stderr, string, args);
1045 fprintf (stderr, "\n");
1046 va_end (args);
1047 if (range_check==range_check_on)
1048 return_to_top_level();
1049 }
1050
1051 \f
1052 /* This page contains miscellaneous functions */
1053
1054 /* Return the language struct for a given language enum. */
1055
1056 const struct language_defn *
1057 language_def(lang)
1058 enum language lang;
1059 {
1060 int i;
1061
1062 for (i = 0; i < languages_size; i++) {
1063 if (languages[i]->la_language == lang) {
1064 return languages[i];
1065 }
1066 }
1067 return NULL;
1068 }
1069
1070 /* Return the language as a string */
1071 char *
1072 language_str(lang)
1073 enum language lang;
1074 {
1075 int i;
1076
1077 for (i = 0; i < languages_size; i++) {
1078 if (languages[i]->la_language == lang) {
1079 return languages[i]->la_name;
1080 }
1081 }
1082 return "Unknown";
1083 }
1084
1085 static void
1086 set_check (ignore, from_tty)
1087 char *ignore;
1088 int from_tty;
1089 {
1090 printf(
1091 "\"set check\" must be followed by the name of a check subcommand.\n");
1092 help_list(setchecklist, "set check ", -1, stdout);
1093 }
1094
1095 static void
1096 show_check (ignore, from_tty)
1097 char *ignore;
1098 int from_tty;
1099 {
1100 cmd_show_list(showchecklist, from_tty, "");
1101 }
1102 \f
1103 /* Add a language to the set of known languages. */
1104
1105 void
1106 add_language (lang)
1107 const struct language_defn *lang;
1108 {
1109 if (lang->la_magic != LANG_MAGIC)
1110 {
1111 fprintf(stderr, "Magic number of %s language struct wrong\n",
1112 lang->la_name);
1113 abort();
1114 }
1115
1116 if (!languages)
1117 {
1118 languages_allocsize = DEFAULT_ALLOCSIZE;
1119 languages = (const struct language_defn **) xmalloc
1120 (languages_allocsize * sizeof (*languages));
1121 }
1122 if (languages_size >= languages_allocsize)
1123 {
1124 languages_allocsize *= 2;
1125 languages = (const struct language_defn **) xrealloc ((char *) languages,
1126 languages_allocsize * sizeof (*languages));
1127 }
1128 languages[languages_size++] = lang;
1129 }
1130
1131 /* Define the language that is no language. */
1132
1133 static int
1134 unk_lang_parser ()
1135 {
1136 return 1;
1137 }
1138
1139 static void
1140 unk_lang_error (msg)
1141 char *msg;
1142 {
1143 error ("Attempted to parse an expression with unknown language");
1144 }
1145
1146 static void
1147 unk_lang_printchar (c, stream)
1148 register int c;
1149 FILE *stream;
1150 {
1151 error ("internal error - unimplemented function unk_lang_printchar called.");
1152 }
1153
1154 static void
1155 unk_lang_printstr (stream, string, length, force_ellipses)
1156 FILE *stream;
1157 char *string;
1158 unsigned int length;
1159 int force_ellipses;
1160 {
1161 error ("internal error - unimplemented function unk_lang_printstr called.");
1162 }
1163
1164 static struct type *
1165 unk_lang_create_fundamental_type (objfile, typeid)
1166 struct objfile *objfile;
1167 int typeid;
1168 {
1169 error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1170 }
1171
1172 void
1173 unk_lang_print_type (type, varstring, stream, show, level)
1174 struct type *type;
1175 char *varstring;
1176 FILE *stream;
1177 int show;
1178 int level;
1179 {
1180 error ("internal error - unimplemented function unk_lang_print_type called.");
1181 }
1182
1183 int
1184 unk_lang_val_print (type, valaddr, address, stream, format, deref_ref,
1185 recurse, pretty)
1186 struct type *type;
1187 char *valaddr;
1188 CORE_ADDR address;
1189 FILE *stream;
1190 int format;
1191 int deref_ref;
1192 int recurse;
1193 enum val_prettyprint pretty;
1194 {
1195 error ("internal error - unimplemented function unk_lang_val_print called.");
1196 }
1197
1198 static struct type ** const (unknown_builtin_types[]) = { 0 };
1199 static const struct op_print unk_op_print_tab[] = {
1200 {NULL, 0, 0, 0}
1201 };
1202
1203 const struct language_defn unknown_language_defn = {
1204 "unknown",
1205 language_unknown,
1206 &unknown_builtin_types[0],
1207 range_check_off,
1208 type_check_off,
1209 unk_lang_parser,
1210 unk_lang_error,
1211 unk_lang_printchar, /* Print character constant */
1212 unk_lang_printstr,
1213 unk_lang_create_fundamental_type,
1214 unk_lang_print_type, /* Print a type using appropriate syntax */
1215 unk_lang_val_print, /* Print a value using appropriate syntax */
1216 &builtin_type_error, /* longest signed integral type */
1217 &builtin_type_error, /* longest unsigned integral type */
1218 &builtin_type_error, /* longest floating point type */
1219 {"", "", "", ""}, /* Binary format info */
1220 {"0%o", "0", "o", ""}, /* Octal format info */
1221 {"%d", "", "d", ""}, /* Decimal format info */
1222 {"0x%x", "0x", "x", ""}, /* Hex format info */
1223 unk_op_print_tab, /* expression operators for printing */
1224 LANG_MAGIC
1225 };
1226
1227 /* These two structs define fake entries for the "local" and "auto" options. */
1228 const struct language_defn auto_language_defn = {
1229 "auto",
1230 language_auto,
1231 &unknown_builtin_types[0],
1232 range_check_off,
1233 type_check_off,
1234 unk_lang_parser,
1235 unk_lang_error,
1236 unk_lang_printchar, /* Print character constant */
1237 unk_lang_printstr,
1238 unk_lang_create_fundamental_type,
1239 unk_lang_print_type, /* Print a type using appropriate syntax */
1240 unk_lang_val_print, /* Print a value using appropriate syntax */
1241 &builtin_type_error, /* longest signed integral type */
1242 &builtin_type_error, /* longest unsigned integral type */
1243 &builtin_type_error, /* longest floating point type */
1244 {"", "", "", ""}, /* Binary format info */
1245 {"0%o", "0", "o", ""}, /* Octal format info */
1246 {"%d", "", "d", ""}, /* Decimal format info */
1247 {"0x%x", "0x", "x", ""}, /* Hex format info */
1248 unk_op_print_tab, /* expression operators for printing */
1249 LANG_MAGIC
1250 };
1251
1252 const struct language_defn local_language_defn = {
1253 "local",
1254 language_auto,
1255 &unknown_builtin_types[0],
1256 range_check_off,
1257 type_check_off,
1258 unk_lang_parser,
1259 unk_lang_error,
1260 unk_lang_printchar, /* Print character constant */
1261 unk_lang_printstr,
1262 unk_lang_create_fundamental_type,
1263 unk_lang_print_type, /* Print a type using appropriate syntax */
1264 unk_lang_val_print, /* Print a value using appropriate syntax */
1265 &builtin_type_error, /* longest signed integral type */
1266 &builtin_type_error, /* longest unsigned integral type */
1267 &builtin_type_error, /* longest floating point type */
1268 {"", "", "", ""}, /* Binary format info */
1269 {"0%o", "0", "o", ""}, /* Octal format info */
1270 {"%d", "", "d", ""}, /* Decimal format info */
1271 {"0x%x", "0x", "x", ""}, /* Hex format info */
1272 unk_op_print_tab, /* expression operators for printing */
1273 LANG_MAGIC
1274 };
1275 \f
1276 /* Initialize the language routines */
1277
1278 void
1279 _initialize_language()
1280 {
1281 struct cmd_list_element *set, *show;
1282
1283 /* GDB commands for language specific stuff */
1284
1285 set = add_set_cmd ("language", class_support, var_string_noescape,
1286 (char *)&language,
1287 "Set the current source language.",
1288 &setlist);
1289 show = add_show_from_set (set, &showlist);
1290 set->function.cfunc = set_language_command;
1291 show->function.cfunc = show_language_command;
1292
1293 add_prefix_cmd ("check", no_class, set_check,
1294 "Set the status of the type/range checker",
1295 &setchecklist, "set check ", 0, &setlist);
1296 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1297 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1298
1299 add_prefix_cmd ("check", no_class, show_check,
1300 "Show the status of the type/range checker",
1301 &showchecklist, "show check ", 0, &showlist);
1302 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1303 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1304
1305 set = add_set_cmd ("type", class_support, var_string_noescape,
1306 (char *)&type,
1307 "Set type checking. (on/warn/off/auto)",
1308 &setchecklist);
1309 show = add_show_from_set (set, &showchecklist);
1310 set->function.cfunc = set_type_command;
1311 show->function.cfunc = show_type_command;
1312
1313 set = add_set_cmd ("range", class_support, var_string_noescape,
1314 (char *)&range,
1315 "Set range checking. (on/warn/off/auto)",
1316 &setchecklist);
1317 show = add_show_from_set (set, &showchecklist);
1318 set->function.cfunc = set_range_command;
1319 show->function.cfunc = show_range_command;
1320
1321 add_language (&unknown_language_defn);
1322 add_language (&local_language_defn);
1323 add_language (&auto_language_defn);
1324
1325 language = savestring ("auto",strlen("auto"));
1326 range = savestring ("auto",strlen("auto"));
1327 type = savestring ("auto",strlen("auto"));
1328
1329 /* Have the above take effect */
1330
1331 set_language_command (language, 0);
1332 set_type_command (NULL, 0);
1333 set_range_command (NULL, 0);
1334 }