* core.c (memory_error), symtab.c (cplusplus_hint, decode_line_1),
[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_unfiltered("The currently understood settings are:\n\n");
165 printf_unfiltered ("local or auto Automatic setting based on source file\n");
166 printf_unfiltered ("c Use the C language\n");
167 printf_unfiltered ("c++ Use the C++ language\n");
168 printf_unfiltered ("chill Use the Chill language\n");
169 printf_unfiltered ("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 (STREQ (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_unfiltered(
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 (STREQ(type,"on"))
226 {
227 type_check = type_check_on;
228 type_mode = type_mode_manual;
229 }
230 else if (STREQ(type,"warn"))
231 {
232 type_check = type_check_warn;
233 type_mode = type_mode_manual;
234 }
235 else if (STREQ(type,"off"))
236 {
237 type_check = type_check_off;
238 type_mode = type_mode_manual;
239 }
240 else if (STREQ(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_unfiltered(
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 (STREQ(range,"on"))
272 {
273 range_check = range_check_on;
274 range_mode = range_mode_manual;
275 }
276 else if (STREQ(range,"warn"))
277 {
278 range_check = range_check_warn;
279 range_mode = range_mode_manual;
280 }
281 else if (STREQ(range,"off"))
282 {
283 range_check = range_check_off;
284 range_mode = range_mode_manual;
285 }
286 else if (STREQ(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_unfiltered("Current language: %s\n",language);
415 show_language_command((char *)0, 1);
416
417 if (!quietly)
418 {
419 printf_unfiltered("Type checking: %s\n",type);
420 show_type_command((char *)0, 1);
421 printf_unfiltered("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_ptr 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, local_hex_format_prefix ());
483 strcat (form, "%");
484 strcat (form, pre);
485 strcat (form, local_hex_format_specifier ());
486 strcat (form, local_hex_format_suffix ());
487 return form;
488 }
489
490 /* Converts a number to hexadecimal and stores it in a static
491 string. Returns a pointer to this string. */
492 char *
493 local_hex_string (num)
494 unsigned long num;
495 {
496 static char res[50];
497
498 sprintf (res, local_hex_format(), num);
499 return res;
500 }
501
502 /* Converts a number to custom hexadecimal and stores it in a static
503 string. Returns a pointer to this string. */
504 char *
505 local_hex_string_custom(num,pre)
506 unsigned long num;
507 char *pre;
508 {
509 static char res[50];
510
511 sprintf (res, local_hex_format_custom(pre), num);
512 return res;
513 }
514
515 /* Returns the appropriate printf format for octal
516 numbers. */
517 char *
518 local_octal_format_custom(pre)
519 char *pre;
520 {
521 static char form[50];
522
523 strcpy (form, local_octal_format_prefix ());
524 strcat (form, "%");
525 strcat (form, pre);
526 strcat (form, local_octal_format_specifier ());
527 strcat (form, local_octal_format_suffix ());
528 return form;
529 }
530
531 /* Returns the appropriate printf format for decimal numbers. */
532 char *
533 local_decimal_format_custom(pre)
534 char *pre;
535 {
536 static char form[50];
537
538 strcpy (form, local_decimal_format_prefix ());
539 strcat (form, "%");
540 strcat (form, pre);
541 strcat (form, local_decimal_format_specifier ());
542 strcat (form, local_decimal_format_suffix ());
543 return form;
544 }
545 \f
546 /* This page contains functions that are used in type/range checking.
547 They all return zero if the type/range check fails.
548
549 It is hoped that these will make extending GDB to parse different
550 languages a little easier. These are primarily used in eval.c when
551 evaluating expressions and making sure that their types are correct.
552 Instead of having a mess of conjucted/disjuncted expressions in an "if",
553 the ideas of type can be wrapped up in the following functions.
554
555 Note that some of them are not currently dependent upon which language
556 is currently being parsed. For example, floats are the same in
557 C and Modula-2 (ie. the only floating point type has TYPE_CODE of
558 TYPE_CODE_FLT), while booleans are different. */
559
560 /* Returns non-zero if its argument is a simple type. This is the same for
561 both Modula-2 and for C. In the C case, TYPE_CODE_CHAR will never occur,
562 and thus will never cause the failure of the test. */
563 int
564 simple_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 case TYPE_CODE_BOOL:
574 return 1;
575
576 default:
577 return 0;
578 }
579 }
580
581 /* Returns non-zero if its argument is of an ordered type.
582 An ordered type is one in which the elements can be tested for the
583 properties of "greater than", "less than", etc, or for which the
584 operations "increment" or "decrement" make sense. */
585 int
586 ordered_type (type)
587 struct type *type;
588 {
589 switch (TYPE_CODE (type)) {
590 case TYPE_CODE_INT:
591 case TYPE_CODE_CHAR:
592 case TYPE_CODE_ENUM:
593 case TYPE_CODE_FLT:
594 case TYPE_CODE_RANGE:
595 return 1;
596
597 default:
598 return 0;
599 }
600 }
601
602 /* Returns non-zero if the two types are the same */
603 int
604 same_type (arg1, arg2)
605 struct type *arg1, *arg2;
606 {
607 if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
608 /* One is structured and one isn't */
609 return 0;
610 else if (structured_type(arg1) && structured_type(arg2))
611 return arg1 == arg2;
612 else if (numeric_type(arg1) && numeric_type(arg2))
613 return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
614 (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
615 ? 1 : 0;
616 else
617 return arg1==arg2;
618 }
619
620 /* Returns non-zero if the type is integral */
621 int
622 integral_type (type)
623 struct type *type;
624 {
625 switch(current_language->la_language)
626 {
627 case language_c:
628 case language_cplus:
629 return (TYPE_CODE(type) != TYPE_CODE_INT) &&
630 (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
631 case language_m2:
632 return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
633 case language_chill:
634 error ("Missing Chill support in function integral_type."); /*FIXME*/
635 default:
636 error ("Language not supported.");
637 }
638 }
639
640 /* Returns non-zero if the value is numeric */
641 int
642 numeric_type (type)
643 struct type *type;
644 {
645 switch (TYPE_CODE (type)) {
646 case TYPE_CODE_INT:
647 case TYPE_CODE_FLT:
648 return 1;
649
650 default:
651 return 0;
652 }
653 }
654
655 /* Returns non-zero if the value is a character type */
656 int
657 character_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_CHAR ? 0 : 1;
665
666 case language_c:
667 case language_cplus:
668 return (TYPE_CODE(type) == TYPE_CODE_INT) &&
669 TYPE_LENGTH(type) == sizeof(char)
670 ? 1 : 0;
671 default:
672 return (0);
673 }
674 }
675
676 /* Returns non-zero if the value is a string type */
677 int
678 string_type (type)
679 struct type *type;
680 {
681 switch(current_language->la_language)
682 {
683 case language_chill:
684 case language_m2:
685 return TYPE_CODE(type) != TYPE_CODE_STRING ? 0 : 1;
686
687 case language_c:
688 case language_cplus:
689 /* C does not have distinct string type. */
690 return (0);
691 default:
692 return (0);
693 }
694 }
695
696 /* Returns non-zero if the value is a boolean type */
697 int
698 boolean_type (type)
699 struct type *type;
700 {
701 if (TYPE_CODE (type) == TYPE_CODE_BOOL)
702 return 1;
703 switch(current_language->la_language)
704 {
705 case language_c:
706 case language_cplus:
707 /* Might be more cleanly handled by having a TYPE_CODE_INT_NOT_BOOL
708 for CHILL and such languages, or a TYPE_CODE_INT_OR_BOOL for C. */
709 if (TYPE_CODE (type) == TYPE_CODE_INT)
710 return 1;
711 default:
712 break;
713 }
714 return 0;
715 }
716
717 /* Returns non-zero if the value is a floating-point type */
718 int
719 float_type (type)
720 struct type *type;
721 {
722 return TYPE_CODE(type) == TYPE_CODE_FLT;
723 }
724
725 /* Returns non-zero if the value is a pointer type */
726 int
727 pointer_type(type)
728 struct type *type;
729 {
730 return TYPE_CODE(type) == TYPE_CODE_PTR ||
731 TYPE_CODE(type) == TYPE_CODE_REF;
732 }
733
734 /* Returns non-zero if the value is a structured type */
735 int
736 structured_type(type)
737 struct type *type;
738 {
739 switch(current_language->la_language)
740 {
741 case language_c:
742 case language_cplus:
743 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
744 (TYPE_CODE(type) == TYPE_CODE_UNION) ||
745 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
746 case language_m2:
747 return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
748 (TYPE_CODE(type) == TYPE_CODE_SET) ||
749 (TYPE_CODE(type) == TYPE_CODE_ARRAY);
750 case language_chill:
751 error ("Missing Chill support in function structured_type."); /*FIXME*/
752 default:
753 return (0);
754 }
755 }
756 \f
757 /* This page contains functions that return info about
758 (struct value) values used in GDB. */
759
760 /* Returns non-zero if the value VAL represents a true value. */
761 int
762 value_true (val)
763 value_ptr val;
764 {
765 /* It is possible that we should have some sort of error if a non-boolean
766 value is used in this context. Possibly dependent on some kind of
767 "boolean-checking" option like range checking. But it should probably
768 not depend on the language except insofar as is necessary to identify
769 a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
770 should be an error, probably). */
771 return !value_logical_not (val);
772 }
773 \f
774 /* Returns non-zero if the operator OP is defined on
775 the values ARG1 and ARG2. */
776
777 #if 0 /* Currently unused */
778
779 void
780 binop_type_check(arg1,arg2,op)
781 value_ptr arg1,arg2;
782 int op;
783 {
784 struct type *t1, *t2;
785
786 /* If we're not checking types, always return success. */
787 if (!STRICT_TYPE)
788 return;
789
790 t1=VALUE_TYPE(arg1);
791 if (arg2 != NULL)
792 t2=VALUE_TYPE(arg2);
793 else
794 t2=NULL;
795
796 switch(op)
797 {
798 case BINOP_ADD:
799 case BINOP_SUB:
800 if ((numeric_type(t1) && pointer_type(t2)) ||
801 (pointer_type(t1) && numeric_type(t2)))
802 {
803 warning ("combining pointer and integer.\n");
804 break;
805 }
806 case BINOP_MUL:
807 case BINOP_LSH:
808 case BINOP_RSH:
809 if (!numeric_type(t1) || !numeric_type(t2))
810 type_op_error ("Arguments to %s must be numbers.",op);
811 else if (!same_type(t1,t2))
812 type_op_error ("Arguments to %s must be of the same type.",op);
813 break;
814
815 case BINOP_LOGICAL_AND:
816 case BINOP_LOGICAL_OR:
817 if (!boolean_type(t1) || !boolean_type(t2))
818 type_op_error ("Arguments to %s must be of boolean type.",op);
819 break;
820
821 case BINOP_EQUAL:
822 if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
823 (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
824 type_op_error ("A pointer can only be compared to an integer or pointer.",op);
825 else if ((pointer_type(t1) && integral_type(t2)) ||
826 (integral_type(t1) && pointer_type(t2)))
827 {
828 warning ("combining integer and pointer.\n");
829 break;
830 }
831 else if (!simple_type(t1) || !simple_type(t2))
832 type_op_error ("Arguments to %s must be of simple type.",op);
833 else if (!same_type(t1,t2))
834 type_op_error ("Arguments to %s must be of the same type.",op);
835 break;
836
837 case BINOP_REM:
838 case BINOP_MOD:
839 if (!integral_type(t1) || !integral_type(t2))
840 type_op_error ("Arguments to %s must be of integral type.",op);
841 break;
842
843 case BINOP_LESS:
844 case BINOP_GTR:
845 case BINOP_LEQ:
846 case BINOP_GEQ:
847 if (!ordered_type(t1) || !ordered_type(t2))
848 type_op_error ("Arguments to %s must be of ordered 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 case BINOP_ASSIGN:
854 if (pointer_type(t1) && !integral_type(t2))
855 type_op_error ("A pointer can only be assigned an integer.",op);
856 else if (pointer_type(t1) && integral_type(t2))
857 {
858 warning ("combining integer and pointer.");
859 break;
860 }
861 else if (!simple_type(t1) || !simple_type(t2))
862 type_op_error ("Arguments to %s must be of simple type.",op);
863 else if (!same_type(t1,t2))
864 type_op_error ("Arguments to %s must be of the same type.",op);
865 break;
866
867 case BINOP_CONCAT:
868 /* FIXME: Needs to handle bitstrings as well. */
869 if (!(string_type(t1) || character_type(t1) || integral_type(t1))
870 || !(string_type(t2) || character_type(t2) || integral_type(t2)))
871 type_op_error ("Arguments to %s must be strings or characters.", op);
872 break;
873
874 /* Unary checks -- arg2 is null */
875
876 case UNOP_LOGICAL_NOT:
877 if (!boolean_type(t1))
878 type_op_error ("Argument to %s must be of boolean type.",op);
879 break;
880
881 case UNOP_PLUS:
882 case UNOP_NEG:
883 if (!numeric_type(t1))
884 type_op_error ("Argument to %s must be of numeric type.",op);
885 break;
886
887 case UNOP_IND:
888 if (integral_type(t1))
889 {
890 warning ("combining pointer and integer.\n");
891 break;
892 }
893 else if (!pointer_type(t1))
894 type_op_error ("Argument to %s must be a pointer.",op);
895 break;
896
897 case UNOP_PREINCREMENT:
898 case UNOP_POSTINCREMENT:
899 case UNOP_PREDECREMENT:
900 case UNOP_POSTDECREMENT:
901 if (!ordered_type(t1))
902 type_op_error ("Argument to %s must be of an ordered type.",op);
903 break;
904
905 default:
906 /* Ok. The following operators have different meanings in
907 different languages. */
908 switch(current_language->la_language)
909 {
910 #ifdef _LANG_c
911 case language_c:
912 case language_cplus:
913 switch(op)
914 {
915 case BINOP_DIV:
916 if (!numeric_type(t1) || !numeric_type(t2))
917 type_op_error ("Arguments to %s must be numbers.",op);
918 break;
919 }
920 break;
921 #endif
922
923 #ifdef _LANG_m2
924 case language_m2:
925 switch(op)
926 {
927 case BINOP_DIV:
928 if (!float_type(t1) || !float_type(t2))
929 type_op_error ("Arguments to %s must be floating point numbers.",op);
930 break;
931 case BINOP_INTDIV:
932 if (!integral_type(t1) || !integral_type(t2))
933 type_op_error ("Arguments to %s must be of integral type.",op);
934 break;
935 }
936 #endif
937
938 #ifdef _LANG_chill
939 case language_chill:
940 error ("Missing Chill support in function binop_type_check.");/*FIXME*/
941 #endif
942
943 }
944 }
945 }
946
947 #endif /* 0 */
948
949 \f
950 /* This page contains functions for the printing out of
951 error messages that occur during type- and range-
952 checking. */
953
954 /* Prints the format string FMT with the operator as a string
955 corresponding to the opcode OP. If FATAL is non-zero, then
956 this is an error and error () is called. Otherwise, it is
957 a warning and printf() is called. */
958 void
959 op_error (fmt,op,fatal)
960 char *fmt;
961 enum exp_opcode op;
962 int fatal;
963 {
964 if (fatal)
965 error (fmt,op_string(op));
966 else
967 {
968 warning (fmt,op_string(op));
969 }
970 }
971
972 /* These are called when a language fails a type- or range-check.
973 The first argument should be a printf()-style format string, and
974 the rest of the arguments should be its arguments. If
975 [type|range]_check is [type|range]_check_on, then return_to_top_level()
976 is called in the style of error (). Otherwise, the message is prefixed
977 by the value of warning_pre_print and we do not return to the top level. */
978
979 void
980 type_error (va_alist)
981 va_dcl
982 {
983 va_list args;
984 char *string;
985
986 if (type_check == type_check_warn)
987 fprintf_filtered (gdb_stderr, warning_pre_print);
988 else
989 error_begin ();
990
991 va_start (args);
992 string = va_arg (args, char *);
993 vfprintf_filtered (gdb_stderr, string, args);
994 fprintf_filtered (gdb_stderr, "\n");
995 va_end (args);
996 if (type_check == type_check_on)
997 return_to_top_level (RETURN_ERROR);
998 }
999
1000 void
1001 range_error (va_alist)
1002 va_dcl
1003 {
1004 va_list args;
1005 char *string;
1006
1007 if (range_check == range_check_warn)
1008 fprintf_filtered (gdb_stderr, warning_pre_print);
1009 else
1010 error_begin ();
1011
1012 va_start (args);
1013 string = va_arg (args, char *);
1014 vfprintf_filtered (gdb_stderr, string, args);
1015 fprintf_filtered (gdb_stderr, "\n");
1016 va_end (args);
1017 if (range_check == range_check_on)
1018 return_to_top_level (RETURN_ERROR);
1019 }
1020
1021 \f
1022 /* This page contains miscellaneous functions */
1023
1024 /* Return the language struct for a given language enum. */
1025
1026 const struct language_defn *
1027 language_def(lang)
1028 enum language lang;
1029 {
1030 int i;
1031
1032 for (i = 0; i < languages_size; i++) {
1033 if (languages[i]->la_language == lang) {
1034 return languages[i];
1035 }
1036 }
1037 return NULL;
1038 }
1039
1040 /* Return the language as a string */
1041 char *
1042 language_str(lang)
1043 enum language lang;
1044 {
1045 int i;
1046
1047 for (i = 0; i < languages_size; i++) {
1048 if (languages[i]->la_language == lang) {
1049 return languages[i]->la_name;
1050 }
1051 }
1052 return "Unknown";
1053 }
1054
1055 static void
1056 set_check (ignore, from_tty)
1057 char *ignore;
1058 int from_tty;
1059 {
1060 printf_unfiltered(
1061 "\"set check\" must be followed by the name of a check subcommand.\n");
1062 help_list(setchecklist, "set check ", -1, gdb_stdout);
1063 }
1064
1065 static void
1066 show_check (ignore, from_tty)
1067 char *ignore;
1068 int from_tty;
1069 {
1070 cmd_show_list(showchecklist, from_tty, "");
1071 }
1072 \f
1073 /* Add a language to the set of known languages. */
1074
1075 void
1076 add_language (lang)
1077 const struct language_defn *lang;
1078 {
1079 if (lang->la_magic != LANG_MAGIC)
1080 {
1081 fprintf_unfiltered(gdb_stderr, "Magic number of %s language struct wrong\n",
1082 lang->la_name);
1083 abort();
1084 }
1085
1086 if (!languages)
1087 {
1088 languages_allocsize = DEFAULT_ALLOCSIZE;
1089 languages = (const struct language_defn **) xmalloc
1090 (languages_allocsize * sizeof (*languages));
1091 }
1092 if (languages_size >= languages_allocsize)
1093 {
1094 languages_allocsize *= 2;
1095 languages = (const struct language_defn **) xrealloc ((char *) languages,
1096 languages_allocsize * sizeof (*languages));
1097 }
1098 languages[languages_size++] = lang;
1099 }
1100
1101 /* Define the language that is no language. */
1102
1103 static int
1104 unk_lang_parser ()
1105 {
1106 return 1;
1107 }
1108
1109 static void
1110 unk_lang_error (msg)
1111 char *msg;
1112 {
1113 error ("Attempted to parse an expression with unknown language");
1114 }
1115
1116 static void
1117 unk_lang_printchar (c, stream)
1118 register int c;
1119 GDB_FILE *stream;
1120 {
1121 error ("internal error - unimplemented function unk_lang_printchar called.");
1122 }
1123
1124 static void
1125 unk_lang_printstr (stream, string, length, force_ellipses)
1126 GDB_FILE *stream;
1127 char *string;
1128 unsigned int length;
1129 int force_ellipses;
1130 {
1131 error ("internal error - unimplemented function unk_lang_printstr called.");
1132 }
1133
1134 static struct type *
1135 unk_lang_create_fundamental_type (objfile, typeid)
1136 struct objfile *objfile;
1137 int typeid;
1138 {
1139 error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1140 }
1141
1142 void
1143 unk_lang_print_type (type, varstring, stream, show, level)
1144 struct type *type;
1145 char *varstring;
1146 GDB_FILE *stream;
1147 int show;
1148 int level;
1149 {
1150 error ("internal error - unimplemented function unk_lang_print_type called.");
1151 }
1152
1153 int
1154 unk_lang_val_print (type, valaddr, address, stream, format, deref_ref,
1155 recurse, pretty)
1156 struct type *type;
1157 char *valaddr;
1158 CORE_ADDR address;
1159 GDB_FILE *stream;
1160 int format;
1161 int deref_ref;
1162 int recurse;
1163 enum val_prettyprint pretty;
1164 {
1165 error ("internal error - unimplemented function unk_lang_val_print called.");
1166 }
1167
1168 static struct type ** const (unknown_builtin_types[]) = { 0 };
1169 static const struct op_print unk_op_print_tab[] = {
1170 {NULL, OP_NULL, PREC_NULL, 0}
1171 };
1172
1173 const struct language_defn unknown_language_defn = {
1174 "unknown",
1175 language_unknown,
1176 &unknown_builtin_types[0],
1177 range_check_off,
1178 type_check_off,
1179 unk_lang_parser,
1180 unk_lang_error,
1181 unk_lang_printchar, /* Print character constant */
1182 unk_lang_printstr,
1183 unk_lang_create_fundamental_type,
1184 unk_lang_print_type, /* Print a type using appropriate syntax */
1185 unk_lang_val_print, /* Print a value using appropriate syntax */
1186 &builtin_type_error, /* longest floating point type */
1187 {"", "", "", ""}, /* Binary format info */
1188 {"0%lo", "0", "o", ""}, /* Octal format info */
1189 {"%ld", "", "d", ""}, /* Decimal format info */
1190 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1191 unk_op_print_tab, /* expression operators for printing */
1192 LANG_MAGIC
1193 };
1194
1195 /* These two structs define fake entries for the "local" and "auto" options. */
1196 const struct language_defn auto_language_defn = {
1197 "auto",
1198 language_auto,
1199 &unknown_builtin_types[0],
1200 range_check_off,
1201 type_check_off,
1202 unk_lang_parser,
1203 unk_lang_error,
1204 unk_lang_printchar, /* Print character constant */
1205 unk_lang_printstr,
1206 unk_lang_create_fundamental_type,
1207 unk_lang_print_type, /* Print a type using appropriate syntax */
1208 unk_lang_val_print, /* Print a value using appropriate syntax */
1209 &builtin_type_error, /* longest floating point type */
1210 {"", "", "", ""}, /* Binary format info */
1211 {"0%lo", "0", "o", ""}, /* Octal format info */
1212 {"%ld", "", "d", ""}, /* Decimal format info */
1213 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1214 unk_op_print_tab, /* expression operators for printing */
1215 LANG_MAGIC
1216 };
1217
1218 const struct language_defn local_language_defn = {
1219 "local",
1220 language_auto,
1221 &unknown_builtin_types[0],
1222 range_check_off,
1223 type_check_off,
1224 unk_lang_parser,
1225 unk_lang_error,
1226 unk_lang_printchar, /* Print character constant */
1227 unk_lang_printstr,
1228 unk_lang_create_fundamental_type,
1229 unk_lang_print_type, /* Print a type using appropriate syntax */
1230 unk_lang_val_print, /* Print a value using appropriate syntax */
1231 &builtin_type_error, /* longest floating point type */
1232 {"", "", "", ""}, /* Binary format info */
1233 {"0%lo", "0", "o", ""}, /* Octal format info */
1234 {"%ld", "", "d", ""}, /* Decimal format info */
1235 {"0x%lx", "0x", "x", ""}, /* Hex format info */
1236 unk_op_print_tab, /* expression operators for printing */
1237 LANG_MAGIC
1238 };
1239 \f
1240 /* Initialize the language routines */
1241
1242 void
1243 _initialize_language()
1244 {
1245 struct cmd_list_element *set, *show;
1246
1247 /* GDB commands for language specific stuff */
1248
1249 set = add_set_cmd ("language", class_support, var_string_noescape,
1250 (char *)&language,
1251 "Set the current source language.",
1252 &setlist);
1253 show = add_show_from_set (set, &showlist);
1254 set->function.cfunc = set_language_command;
1255 show->function.cfunc = show_language_command;
1256
1257 add_prefix_cmd ("check", no_class, set_check,
1258 "Set the status of the type/range checker",
1259 &setchecklist, "set check ", 0, &setlist);
1260 add_alias_cmd ("c", "check", no_class, 1, &setlist);
1261 add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1262
1263 add_prefix_cmd ("check", no_class, show_check,
1264 "Show the status of the type/range checker",
1265 &showchecklist, "show check ", 0, &showlist);
1266 add_alias_cmd ("c", "check", no_class, 1, &showlist);
1267 add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1268
1269 set = add_set_cmd ("type", class_support, var_string_noescape,
1270 (char *)&type,
1271 "Set type checking. (on/warn/off/auto)",
1272 &setchecklist);
1273 show = add_show_from_set (set, &showchecklist);
1274 set->function.cfunc = set_type_command;
1275 show->function.cfunc = show_type_command;
1276
1277 set = add_set_cmd ("range", class_support, var_string_noescape,
1278 (char *)&range,
1279 "Set range checking. (on/warn/off/auto)",
1280 &setchecklist);
1281 show = add_show_from_set (set, &showchecklist);
1282 set->function.cfunc = set_range_command;
1283 show->function.cfunc = show_range_command;
1284
1285 add_language (&unknown_language_defn);
1286 add_language (&local_language_defn);
1287 add_language (&auto_language_defn);
1288
1289 language = savestring ("auto",strlen("auto"));
1290 range = savestring ("auto",strlen("auto"));
1291 type = savestring ("auto",strlen("auto"));
1292
1293 /* Have the above take effect */
1294
1295 set_language_command (language, 0);
1296 set_type_command (NULL, 0);
1297 set_range_command (NULL, 0);
1298 }