* Makefile.in (SFILES_MAINDIR): Add ch-exp.y.
[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 local or auto Automatic setting based on source file\n\
166 c Use the C language\n\
167 c++ Use the C++ language\n\
168 chill Use the Chill language\n\
169 modula-2 Use the Modula-2 language\n");
170 /* Restore the silly string. */
171 set_language(current_language->la_language);
172 return;
173 }
174
175 /* Search the list of languages for a match. */
176 for (i = 0; i < languages_size; i++) {
177 if (!strcmp (languages[i]->la_name, language)) {
178 /* Found it! Go into manual mode, and use this language. */
179 if (languages[i]->la_language == language_auto) {
180 /* Enter auto mode. Set to the current frame's language, if known. */
181 language_mode = language_mode_auto;
182 flang = get_frame_language();
183 if (flang!=language_unknown)
184 set_language(flang);
185 expected_language = current_language;
186 return;
187 } else {
188 /* Enter manual mode. Set the specified language. */
189 language_mode = language_mode_manual;
190 current_language = languages[i];
191 set_type_range ();
192 set_lang_str();
193 expected_language = current_language;
194 return;
195 }
196 }
197 }
198
199 /* Reset the language (esp. the global string "language") to the
200 correct values. */
201 err_lang=savestring(language,strlen(language));
202 make_cleanup (free, err_lang); /* Free it after error */
203 set_language(current_language->la_language);
204 error ("Unknown language `%s'.",err_lang);
205 }
206
207 /* Show command. Display a warning if the type setting does
208 not match the current language. */
209 static void
210 show_type_command(ignore, from_tty)
211 char *ignore;
212 int from_tty;
213 {
214 if (type_check != current_language->la_type_check)
215 printf(
216 "Warning: the current type check setting does not match the language.\n");
217 }
218
219 /* Set command. Change the setting for type checking. */
220 static void
221 set_type_command(ignore, from_tty)
222 char *ignore;
223 int from_tty;
224 {
225 if (!strcmp(type,"on"))
226 {
227 type_check = type_check_on;
228 type_mode = type_mode_manual;
229 }
230 else if (!strcmp(type,"warn"))
231 {
232 type_check = type_check_warn;
233 type_mode = type_mode_manual;
234 }
235 else if (!strcmp(type,"off"))
236 {
237 type_check = type_check_off;
238 type_mode = type_mode_manual;
239 }
240 else if (!strcmp(type,"auto"))
241 {
242 type_mode = type_mode_auto;
243 set_type_range();
244 /* Avoid hitting the set_type_str call below. We
245 did it in set_type_range. */
246 return;
247 }
248 set_type_str();
249 show_type_command((char *)NULL, from_tty);
250 }
251
252 /* Show command. Display a warning if the range setting does
253 not match the current language. */
254 static void
255 show_range_command(ignore, from_tty)
256 char *ignore;
257 int from_tty;
258 {
259
260 if (range_check != current_language->la_range_check)
261 printf(
262 "Warning: the current range check setting does not match the language.\n");
263 }
264
265 /* Set command. Change the setting for range checking. */
266 static void
267 set_range_command(ignore, from_tty)
268 char *ignore;
269 int from_tty;
270 {
271 if (!strcmp(range,"on"))
272 {
273 range_check = range_check_on;
274 range_mode = range_mode_manual;
275 }
276 else if (!strcmp(range,"warn"))
277 {
278 range_check = range_check_warn;
279 range_mode = range_mode_manual;
280 }
281 else if (!strcmp(range,"off"))
282 {
283 range_check = range_check_off;
284 range_mode = range_mode_manual;
285 }
286 else if (!strcmp(range,"auto"))
287 {
288 range_mode = range_mode_auto;
289 set_type_range();
290 /* Avoid hitting the set_range_str call below. We
291 did it in set_type_range. */
292 return;
293 }
294 set_range_str();
295 show_range_command((char *)0, from_tty);
296 }
297
298 /* Set the status of range and type checking based on
299 the current modes and the current language.
300 If SHOW is non-zero, then print out the current language,
301 type and range checking status. */
302 static void
303 set_type_range()
304 {
305
306 if (range_mode == range_mode_auto)
307 range_check = current_language->la_range_check;
308
309 if (type_mode == type_mode_auto)
310 type_check = current_language->la_type_check;
311
312 set_type_str();
313 set_range_str();
314 }
315
316 /* Set current language to (enum language) LANG. */
317
318 void
319 set_language(lang)
320 enum language lang;
321 {
322 int i;
323
324 for (i = 0; i < languages_size; i++) {
325 if (languages[i]->la_language == lang) {
326 current_language = languages[i];
327 set_type_range ();
328 set_lang_str();
329 break;
330 }
331 }
332 }
333 \f
334 /* This page contains functions that update the global vars
335 language, type and range. */
336 static void
337 set_lang_str()
338 {
339 char *prefix = "";
340
341 free (language);
342 if (language_mode == language_mode_auto)
343 prefix = "auto; currently ";
344
345 language = concat(prefix, current_language->la_name, NULL);
346 }
347
348 static void
349 set_type_str()
350 {
351 char *tmp, *prefix = "";
352
353 free (type);
354 if (type_mode==type_mode_auto)
355 prefix = "auto; currently ";
356
357 switch(type_check)
358 {
359 case type_check_on:
360 tmp = "on";
361 break;
362 case type_check_off:
363 tmp = "off";
364 break;
365 case type_check_warn:
366 tmp = "warn";
367 break;
368 default:
369 error ("Unrecognized type check setting.");
370 }
371
372 type = concat(prefix,tmp,NULL);
373 }
374
375 static void
376 set_range_str()
377 {
378 char *tmp, *pref = "";
379
380 free (range);
381 if (range_mode==range_mode_auto)
382 pref = "auto; currently ";
383
384 switch(range_check)
385 {
386 case range_check_on:
387 tmp = "on";
388 break;
389 case range_check_off:
390 tmp = "off";
391 break;
392 case range_check_warn:
393 tmp = "warn";
394 break;
395 default:
396 error ("Unrecognized range check setting.");
397 }
398
399 range = concat(pref,tmp,NULL);
400 }
401
402
403 /* Print out the current language settings: language, range and
404 type checking. If QUIETLY, print only what has changed. */
405
406 void
407 language_info (quietly)
408 int quietly;
409 {
410 if (quietly && expected_language == current_language)
411 return;
412
413 expected_language = current_language;
414 printf("Current language: %s\n",language);
415 show_language_command((char *)0, 1);
416
417 if (!quietly)
418 {
419 printf("Type checking: %s\n",type);
420 show_type_command((char *)0, 1);
421 printf("Range checking: %s\n",range);
422 show_range_command((char *)0, 1);
423 }
424 }
425 \f
426 /* Return the result of a binary operation. */
427
428 #if 0 /* Currently unused */
429
430 struct type *
431 binop_result_type(v1,v2)
432 value v1,v2;
433 {
434 int l1,l2,size,uns;
435
436 l1 = TYPE_LENGTH(VALUE_TYPE(v1));
437 l2 = TYPE_LENGTH(VALUE_TYPE(v2));
438
439 switch(current_language->la_language)
440 {
441 case language_c:
442 case language_cplus:
443 if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
444 return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
445 VALUE_TYPE(v2) : VALUE_TYPE(v1);
446 else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
447 return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
448 VALUE_TYPE(v1) : VALUE_TYPE(v2);
449 else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
450 return VALUE_TYPE(v1);
451 else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
452 return VALUE_TYPE(v2);
453 else /* Both are signed. Result is the longer type */
454 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
455 break;
456 case language_m2:
457 /* If we are doing type-checking, l1 should equal l2, so this is
458 not needed. */
459 return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
460 break;
461 case language_chill:
462 error ("Missing Chill support in function binop_result_check.");/*FIXME*/
463 }
464 abort();
465 return (struct type *)0; /* For lint */
466 }
467
468 #endif /* 0 */
469
470 \f
471 /* This page contains functions that return format strings for
472 printf for printing out numbers in different formats */
473
474 /* Returns the appropriate printf format for hexadecimal
475 numbers. */
476 char *
477 local_hex_format_custom(pre)
478 char *pre;
479 {
480 static char form[50];
481
482 strcpy (form, current_language->la_hex_format_pre);
483 strcat (form, pre);
484 strcat (form, current_language->la_hex_format_suf);
485 return form;
486 }
487
488 /* Converts a number to hexadecimal and stores it in a static
489 string. Returns a pointer to this string. */
490 char *
491 local_hex_string (num)
492 int num;
493 {
494 static char res[50];
495
496 sprintf (res, current_language->la_hex_format, num);
497 return res;
498 }
499
500 /* Converts a number to custom hexadecimal and stores it in a static
501 string. Returns a pointer to this string. */
502 char *
503 local_hex_string_custom(num,pre)
504 int num;
505 char *pre;
506 {
507 static char res[50];
508
509 sprintf (res, local_hex_format_custom(pre), num);
510 return res;
511 }
512
513 /* Returns the appropriate printf format for octal
514 numbers. */
515 char *
516 local_octal_format_custom(pre)
517 char *pre;
518 {
519 static char form[50];
520
521 strcpy (form, current_language->la_octal_format_pre);
522 strcat (form, pre);
523 strcat (form, current_language->la_octal_format_suf);
524 return form;
525 }
526 \f
527 /* This page contains functions that are used in type/range checking.
528 They all return zero if the type/range check fails.
529
530 It is hoped that these will make extending GDB to parse different
531 languages a little easier. These are primarily used in eval.c when
532 evaluating expressions and making sure that their types are correct.
533 Instead of having a mess of conjucted/disjuncted expressions in an "if",
534 the ideas of type can be wrapped up in the following functions.
535
536 Note that some of them are not currently dependent upon which language
537 is currently being parsed. For example, floats are the same in
538 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
539 TYPE_CODE_FLT), while booleans are different. */
540
541 /* Returns non-zero if its argument is a simple type. This is the same for
542 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
543 and thus will never cause the failure of the test. */
544 int
545 simple_type(type)
546 struct type *type;
547 {
548 switch (TYPE_CODE (type)) {
549 case TYPE_CODE_INT:
550 case TYPE_CODE_CHAR:
551 case TYPE_CODE_ENUM:
552 case TYPE_CODE_FLT:
553 case TYPE_CODE_RANGE:
554 case TYPE_CODE_BOOL:
555 return 1;
556
557 default:
558 return 0;
559 }
560 }
561
562 /* Returns non-zero if its argument is of an ordered type. */
563 int
564 ordered_type (type)
565 struct type *type;
566 {
567 switch (TYPE_CODE (type)) {
568 case TYPE_CODE_INT:
569 case TYPE_CODE_CHAR:
570 case TYPE_CODE_ENUM:
571 case TYPE_CODE_FLT:
572 case TYPE_CODE_RANGE:
573 return 1;
574
575 default:
576 return 0;
577 }
578 }
579
580 /* Returns non-zero if the two types are the same */
581 int
582 same_type (arg1, arg2)
583 struct type *arg1, *arg2;
584 {
585 if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
586 /* One is structured and one isn't */
587 return 0;
588 else if (structured_type(arg1) && structured_type(arg2))
589 return arg1 == arg2;
590 else if (numeric_type(arg1) && numeric_type(arg2))
591 return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
592 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
593 ? 1 : 0;
594 else
595 return arg1==arg2;
596 }
597
598 /* Returns non-zero if the type is integral */
599 int
600 integral_type (type)
601 struct type *type;
602 {
603 switch(current_language->la_language)
604 {
605 case language_c:
606 case language_cplus:
607 return (TYPE_CODE(type) != TYPE_CODE_INT) &&
608 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
609 case language_m2:
610 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
611 case language_chill:
612 error ("Missing Chill support in function integral_type."); /*FIXME*/
613 default:
614 error ("Language not supported.");
615 }
616 }
617
618 /* Returns non-zero if the value is numeric */
619 int
620 numeric_type (type)
621 struct type *type;
622 {
623 switch (TYPE_CODE (type)) {
624 case TYPE_CODE_INT:
625 case TYPE_CODE_FLT:
626 return 1;
627
628 default:
629 return 0;
630 }
631 }
632
633 /* Returns non-zero if the value is a character type */
634 int
635 character_type (type)
636 struct type *type;
637 {
638 switch(current_language->la_language)
639 {
640 case language_m2:
641 return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
642
643 case language_c:
644 case language_cplus:
645 return (TYPE_CODE(type) == TYPE_CODE_INT) &&
646 TYPE_LENGTH(type) == sizeof(char)
647 ? 1 : 0;
648 case language_chill:
649 error ("Missing Chill support in function character_type."); /*FIXME*/
650 default:
651 return (0);
652 }
653 }
654
655 /* Returns non-zero if the value is a boolean type */
656 int
657 boolean_type (type)
658 struct type *type;
659 {
660 switch(current_language->la_language)
661 {
662 case language_chill:
663 case language_m2:
664 return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1;
665
666 case language_c:
667 case language_cplus:
668 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
669 default:
670 return (0);
671 }
672 }
673
674 /* Returns non-zero if the value is a floating-point type */
675 int
676 float_type (type)
677 struct type *type;
678 {
679 return TYPE_CODE(type) == TYPE_CODE_FLT;
680 }
681
682 /* Returns non-zero if the value is a pointer type */
683 int
684 pointer_type(type)
685 struct type *type;
686 {
687 return TYPE_CODE(type) == TYPE_CODE_PTR ||
688 TYPE_CODE(type) == TYPE_CODE_REF;
689 }
690
691 /* Returns non-zero if the value is a structured type */
692 int
693 structured_type(type)
694 struct type *type;
695 {
696 switch(current_language->la_language)
697 {
698 case language_c:
699 case language_cplus:
700 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
701 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
702 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
703 case language_m2:
704 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
705 (TYPE_CODE(type) == TYPE_CODE_SET) ||
706 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
707 case language_chill:
708 error ("Missing Chill support in function structured_type."); /*FIXME*/
709 default:
710 return (0);
711 }
712 }
713 \f
714 /* This page contains functions that return info about
715 (struct value) values used in GDB. */
716
717 /* Returns non-zero if the value VAL represents a true value. */
718 int
719 value_true(val)
720 value val;
721 {
722 int len, i;
723 struct type *type;
724 LONGEST v;
725
726 switch (current_language->la_language) {
727
728 case language_c:
729 case language_cplus:
730 return !value_logical_not (val);
731
732 case language_m2:
733 type = VALUE_TYPE(val);
734 if (TYPE_CODE (type) != TYPE_CODE_BOOL)
735 return 0; /* Not a BOOLEAN at all */
736 /* Search the fields for one that matches the current value. */
737 len = TYPE_NFIELDS (type);
738 v = value_as_long (val);
739 for (i = 0; i < len; i++)
740 {
741 QUIT;
742 if (v == TYPE_FIELD_BITPOS (type, i))
743 break;
744 }
745 if (i >= len)
746 return 0; /* Not a valid BOOLEAN value */
747 if (!strcmp ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i)))
748 return 1; /* BOOLEAN with value TRUE */
749 else
750 return 0; /* BOOLEAN with value FALSE */
751 break;
752
753 case language_chill:
754 error ("Missing Chill support in function value_type."); /*FIXME*/
755
756 default:
757 error ("Language not supported.");
758 }
759 }
760 \f
761 /* Returns non-zero if the operator OP is defined on
762 the values ARG1 and ARG2. */
763
764 #if 0 /* Currently unused */
765
766 void
767 binop_type_check(arg1,arg2,op)
768 value arg1,arg2;
769 int op;
770 {
771 struct type *t1, *t2;
772
773 /* If we're not checking types, always return success. */
774 if (!STRICT_TYPE)
775 return;
776
777 t1=VALUE_TYPE(arg1);
778 if (arg2!=(value)NULL)
779 t2=VALUE_TYPE(arg2);
780 else
781 t2=NULL;
782
783 switch(op)
784 {
785 case BINOP_ADD:
786 case BINOP_SUB:
787 if ((numeric_type(t1) && pointer_type(t2)) ||
788 (pointer_type(t1) && numeric_type(t2)))
789 {
790 warning ("combining pointer and integer.\n");
791 break;
792 }
793 case BINOP_MUL:
794 case BINOP_LSH:
795 case BINOP_RSH:
796 if (!numeric_type(t1) || !numeric_type(t2))
797 type_op_error ("Arguments to %s must be numbers.",op);
798 else if (!same_type(t1,t2))
799 type_op_error ("Arguments to %s must be of the same type.",op);
800 break;
801
802 case BINOP_LOGICAL_AND:
803 case BINOP_LOGICAL_OR:
804 if (!boolean_type(t1) || !boolean_type(t2))
805 type_op_error ("Arguments to %s must be of boolean type.",op);
806 break;
807
808 case BINOP_EQUAL:
809 if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
810 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
811 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
812 else if ((pointer_type(t1) && integral_type(t2)) ||
813 (integral_type(t1) && pointer_type(t2)))
814 {
815 warning ("combining integer and pointer.\n");
816 break;
817 }
818 else if (!simple_type(t1) || !simple_type(t2))
819 type_op_error ("Arguments to %s must be of simple type.",op);
820 else if (!same_type(t1,t2))
821 type_op_error ("Arguments to %s must be of the same type.",op);
822 break;
823
824 case BINOP_REM:
825 if (!integral_type(t1) || !integral_type(t2))
826 type_op_error ("Arguments to %s must be of integral type.",op);
827 break;
828
829 case BINOP_LESS:
830 case BINOP_GTR:
831 case BINOP_LEQ:
832 case BINOP_GEQ:
833 if (!ordered_type(t1) || !ordered_type(t2))
834 type_op_error ("Arguments to %s must be of ordered type.",op);
835 else if (!same_type(t1,t2))
836 type_op_error ("Arguments to %s must be of the same type.",op);
837 break;
838
839 case BINOP_ASSIGN:
840 if (pointer_type(t1) && !integral_type(t2))
841 type_op_error ("A pointer can only be assigned an integer.",op);
842 else if (pointer_type(t1) && integral_type(t2))
843 {
844 warning ("combining integer and pointer.");
845 break;
846 }
847 else if (!simple_type(t1) || !simple_type(t2))
848 type_op_error ("Arguments to %s must be of simple type.",op);
849 else if (!same_type(t1,t2))
850 type_op_error ("Arguments to %s must be of the same type.",op);
851 break;
852
853 /* Unary checks -- arg2 is null */
854
855 case UNOP_LOGICAL_NOT:
856 if (!boolean_type(t1))
857 type_op_error ("Argument to %s must be of boolean type.",op);
858 break;
859
860 case UNOP_PLUS:
861 case UNOP_NEG:
862 if (!numeric_type(t1))
863 type_op_error ("Argument to %s must be of numeric type.",op);
864 break;
865
866 case UNOP_IND:
867 if (integral_type(t1))
868 {
869 warning ("combining pointer and integer.\n");
870 break;
871 }
872 else if (!pointer_type(t1))
873 type_op_error ("Argument to %s must be a pointer.",op);
874 break;
875
876 case UNOP_PREINCREMENT:
877 case UNOP_POSTINCREMENT:
878 case UNOP_PREDECREMENT:
879 case UNOP_POSTDECREMENT:
880 if (!ordered_type(t1))
881 type_op_error ("Argument to %s must be of an ordered type.",op);
882 break;
883
884 default:
885 /* Ok. The following operators have different meanings in
886 different languages. */
887 switch(current_language->la_language)
888 {
889 #ifdef _LANG_c
890 case language_c:
891 case language_cplus:
892 switch(op)
893 {
894 case BINOP_DIV:
895 if (!numeric_type(t1) || !numeric_type(t2))
896 type_op_error ("Arguments to %s must be numbers.",op);
897 break;
898 }
899 break;
900 #endif
901
902 #ifdef _LANG_m2
903 case language_m2:
904 switch(op)
905 {
906 case BINOP_DIV:
907 if (!float_type(t1) || !float_type(t2))
908 type_op_error ("Arguments to %s must be floating point numbers.",op);
909 break;
910 case BINOP_INTDIV:
911 if (!integral_type(t1) || !integral_type(t2))
912 type_op_error ("Arguments to %s must be of integral type.",op);
913 break;
914 }
915 #endif
916
917 #ifdef _LANG_chill
918 case language_chill:
919 error ("Missing Chill support in function binop_type_check.");/*FIXME*/
920 #endif
921
922 }
923 }
924 }
925
926 #endif /* 0 */
927
928 \f
929 /* This page contains functions for the printing out of
930 error messages that occur during type- and range-
931 checking. */
932
933 /* Prints the format string FMT with the operator as a string
934 corresponding to the opcode OP. If FATAL is non-zero, then
935 this is an error and error () is called. Otherwise, it is
936 a warning and printf() is called. */
937 void
938 op_error (fmt,op,fatal)
939 char *fmt;
940 enum exp_opcode op;
941 int fatal;
942 {
943 if (fatal)
944 error (fmt,op_string(op));
945 else
946 {
947 warning (fmt,op_string(op));
948 }
949 }
950
951 /* These are called when a language fails a type- or range-check.
952 The first argument should be a printf()-style format string, and
953 the rest of the arguments should be its arguments. If
954 [type|range]_check is [type|range]_check_on, then return_to_top_level()
955 is called in the style of error (). Otherwise, the message is prefixed
956 by the value of warning_pre_print and we do not return to the top level. */
957
958 void
959 type_error (va_alist)
960 va_dcl
961 {
962 va_list args;
963 char *string;
964
965 if (type_check==type_check_warn)
966 fprintf(stderr,warning_pre_print);
967 else
968 target_terminal_ours();
969
970 va_start (args);
971 string = va_arg (args, char *);
972 vfprintf (stderr, string, args);
973 fprintf (stderr, "\n");
974 va_end (args);
975 if (type_check==type_check_on)
976 return_to_top_level();
977 }
978
979 void
980 range_error (va_alist)
981 va_dcl
982 {
983 va_list args;
984 char *string;
985
986 if (range_check==range_check_warn)
987 fprintf(stderr,warning_pre_print);
988 else
989 target_terminal_ours();
990
991 va_start (args);
992 string = va_arg (args, char *);
993 vfprintf (stderr, string, args);
994 fprintf (stderr, "\n");
995 va_end (args);
996 if (range_check==range_check_on)
997 return_to_top_level();
998 }
999
1000 \f
1001 /* This page contains miscellaneous functions */
1002
1003 /* Return the language as a string */
1004 char *
1005 language_str(lang)
1006 enum language lang;
1007 {
1008 int i;
1009
1010 for (i = 0; i < languages_size; i++) {
1011 if (languages[i]->la_language == lang) {
1012 return languages[i]->la_name;
1013 }
1014 }
1015 return "Unknown";
1016 }
1017
1018 static void
1019 set_check (ignore, from_tty)
1020 char *ignore;
1021 int from_tty;
1022 {
1023 printf(
1024 "\"set check\" must be followed by the name of a check subcommand.\n");
1025 help_list(setchecklist, "set check ", -1, stdout);
1026 }
1027
1028 static void
1029 show_check (ignore, from_tty)
1030 char *ignore;
1031 int from_tty;
1032 {
1033 cmd_show_list(showchecklist, from_tty, "");
1034 }
1035 \f
1036 /* Add a language to the set of known languages. */
1037
1038 void
1039 add_language (lang)
1040 const struct language_defn *lang;
1041 {
1042 if (lang->la_magic != LANG_MAGIC)
1043 {
1044 fprintf(stderr, "Magic number of %s language struct wrong\n",
1045 lang->la_name);
1046 abort();
1047 }
1048
1049 if (!languages)
1050 {
1051 languages_allocsize = DEFAULT_ALLOCSIZE;
1052 languages = (const struct language_defn **) xmalloc
1053 (languages_allocsize * sizeof (*languages));
1054 }
1055 if (languages_size >= languages_allocsize)
1056 {
1057 languages_allocsize *= 2;
1058 languages = (const struct language_defn **) xrealloc ((char *) languages,
1059 languages_allocsize * sizeof (*languages));
1060 }
1061 languages[languages_size++] = lang;
1062 }
1063
1064 /* Define the language that is no language. */
1065
1066 static int
1067 unk_lang_parser ()
1068 {
1069 return 1;
1070 }
1071
1072 static void
1073 unk_lang_error (msg)
1074 char *msg;
1075 {
1076 error ("Attempted to parse an expression with unknown language");
1077 }
1078
1079 static struct type ** const (unknown_builtin_types[]) = { 0 };
1080 static const struct op_print unk_op_print_tab[] = { 0 };
1081
1082 const struct language_defn unknown_language_defn = {
1083 "unknown",
1084 language_unknown,
1085 &unknown_builtin_types[0],
1086 range_check_off,
1087 type_check_off,
1088 unk_lang_parser,
1089 unk_lang_error,
1090 &builtin_type_error, /* longest signed integral type */
1091 &builtin_type_error, /* longest unsigned integral type */
1092 &builtin_type_error, /* longest floating point type */
1093 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1094 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1095 unk_op_print_tab, /* expression operators for printing */
1096 LANG_MAGIC
1097 };
1098
1099 /* These two structs define fake entries for the "local" and "auto" options. */
1100 const struct language_defn auto_language_defn = {
1101 "auto",
1102 language_auto,
1103 &unknown_builtin_types[0],
1104 range_check_off,
1105 type_check_off,
1106 unk_lang_parser,
1107 unk_lang_error,
1108 &builtin_type_error, /* longest signed integral type */
1109 &builtin_type_error, /* longest unsigned integral type */
1110 &builtin_type_error, /* longest floating point type */
1111 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1112 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1113 unk_op_print_tab, /* expression operators for printing */
1114 LANG_MAGIC
1115 };
1116
1117 const struct language_defn local_language_defn = {
1118 "local",
1119 language_auto,
1120 &unknown_builtin_types[0],
1121 range_check_off,
1122 type_check_off,
1123 unk_lang_parser,
1124 unk_lang_error,
1125 &builtin_type_error, /* longest signed integral type */
1126 &builtin_type_error, /* longest unsigned integral type */
1127 &builtin_type_error, /* longest floating point type */
1128 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1129 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1130 unk_op_print_tab, /* expression operators for printing */
1131 LANG_MAGIC
1132 };
1133 \f
1134 /* Initialize the language routines */
1135
1136 void
1137 _initialize_language()
1138 {
1139 struct cmd_list_element *set, *show;
1140
1141 /* GDB commands for language specific stuff */
1142
1143 set = add_set_cmd ("language", class_support, var_string_noescape,
1144 (char *)&language,
1145 "Set the current source language.",
1146 &setlist);
1147 show = add_show_from_set (set, &showlist);
1148 set->function.cfunc = set_language_command;
1149 show->function.cfunc = show_language_command;
1150
1151 add_prefix_cmd ("check", no_class, set_check,
1152 "Set the status of the type/range checker",
1153 &setchecklist, "set check ", 0, &setlist);
1154 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1155 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1156
1157 add_prefix_cmd ("check", no_class, show_check,
1158 "Show the status of the type/range checker",
1159 &showchecklist, "show check ", 0, &showlist);
1160 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1161 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1162
1163 set = add_set_cmd ("type", class_support, var_string_noescape,
1164 (char *)&type,
1165 "Set type checking. (on/warn/off/auto)",
1166 &setchecklist);
1167 show = add_show_from_set (set, &showchecklist);
1168 set->function.cfunc = set_type_command;
1169 show->function.cfunc = show_type_command;
1170
1171 set = add_set_cmd ("range", class_support, var_string_noescape,
1172 (char *)&range,
1173 "Set range checking. (on/warn/off/auto)",
1174 &setchecklist);
1175 show = add_show_from_set (set, &showchecklist);
1176 set->function.cfunc = set_range_command;
1177 show->function.cfunc = show_range_command;
1178
1179 add_language (&unknown_language_defn);
1180 add_language (&local_language_defn);
1181 add_language (&auto_language_defn);
1182
1183 language = savestring ("auto",strlen("auto"));
1184 range = savestring ("auto",strlen("auto"));
1185 type = savestring ("auto",strlen("auto"));
1186
1187 /* Have the above take effect */
1188
1189 set_language_command (language, 0);
1190 set_type_command (NULL, 0);
1191 set_range_command (NULL, 0);
1192 }