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