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