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