* defs.h, main.c (catch_errors): Add return_mask arg.
[external/binutils.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2    Copyright 1991, 1992 Free Software Foundation, Inc.
3    Contributed by the Department of Computer Science at the State University
4    of New York at Buffalo.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22 /* This file contains functions that return things that are specific
23    to languages.  Each function should examine current_language if necessary,
24    and return the appropriate result. */
25
26 /* FIXME:  Most of these would be better organized as macros which
27    return data out of a "language-specific" struct pointer that is set
28    whenever the working language changes.  That would be a lot faster.  */
29
30 #include "defs.h"
31 #include <string.h>
32 #include <varargs.h>
33
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "value.h"
37 #include "gdbcmd.h"
38 #include "frame.h"
39 #include "expression.h"
40 #include "language.h"
41 #include "target.h"
42 #include "parser-defs.h"
43
44 static void
45 show_language_command PARAMS ((char *, int));
46
47 static void
48 set_language_command PARAMS ((char *, int));
49
50 static void
51 show_type_command PARAMS ((char *, int));
52
53 static void
54 set_type_command PARAMS ((char *, int));
55
56 static void
57 show_range_command PARAMS ((char *, int));
58
59 static void
60 set_range_command PARAMS ((char *, int));
61
62 static void
63 set_range_str PARAMS ((void));
64
65 static void
66 set_type_str PARAMS ((void));
67
68 static void
69 set_lang_str PARAMS ((void));
70
71 static void
72 unk_lang_error PARAMS ((char *));
73
74 static int
75 unk_lang_parser PARAMS ((void));
76
77 static void
78 show_check PARAMS ((char *, int));
79
80 static void
81 set_check PARAMS ((char *, int));
82
83 static void
84 set_type_range PARAMS ((void));
85
86 /* Forward declaration */
87 extern const struct language_defn unknown_language_defn;
88 extern char *warning_pre_print;
89   
90 /* The current (default at startup) state of type and range checking.
91     (If the modes are set to "auto", though, these are changed based
92     on the default language at startup, and then again based on the
93     language of the first source file.  */
94
95 enum range_mode range_mode = range_mode_auto;
96 enum range_check range_check = range_check_off;
97 enum type_mode type_mode = type_mode_auto;
98 enum type_check type_check = type_check_off;
99
100 /* The current language and language_mode (see language.h) */
101
102 const struct language_defn *current_language = &unknown_language_defn;
103 enum language_mode language_mode = language_mode_auto;
104
105 /* The language that the user expects to be typing in (the language
106    of main(), or the last language we notified them about, or C).  */
107
108 const struct language_defn *expected_language;
109
110 /* The list of supported languages.  The list itself is malloc'd.  */
111
112 static const struct language_defn **languages;
113 static unsigned languages_size;
114 static unsigned languages_allocsize;
115 #define DEFAULT_ALLOCSIZE 4
116
117 /* The "set language/type/range" commands all put stuff in these
118    buffers.  This is to make them work as set/show commands.  The
119    user's string is copied here, then the set_* commands look at
120    them and update them to something that looks nice when it is
121    printed out. */
122
123 static char *language;
124 static char *type;
125 static char *range;
126
127 /* Warning issued when current_language and the language of the current
128    frame do not match. */
129 char lang_frame_mismatch_warn[] =
130         "Warning: the current language does not match this frame.";
131
132 \f
133 /* This page contains the functions corresponding to GDB commands
134    and their helpers. */
135
136 /* Show command.  Display a warning if the language set
137    does not match the frame. */
138 static void
139 show_language_command (ignore, from_tty)
140    char *ignore;
141    int from_tty;
142 {
143    enum language flang;         /* The language of the current frame */
144
145    flang = get_frame_language();
146    if (flang != language_unknown &&
147       language_mode == language_mode_manual &&
148       current_language->la_language != flang)
149       printf_filtered("%s\n",lang_frame_mismatch_warn);
150 }
151
152 /* Set command.  Change the current working language. */
153 static void
154 set_language_command (ignore, from_tty)
155    char *ignore;
156    int from_tty;
157 {
158   int i;
159   enum language flang;
160   char *err_lang;
161
162   /* FIXME -- do this from the list, with HELP.  */
163   if (!language || !language[0]) {
164     printf("The currently understood settings are:\n\n");
165     printf ("local or auto    Automatic setting based on source file\n");
166     printf ("c                Use the C language\n");
167     printf ("c++              Use the C++ language\n");
168     printf ("chill            Use the Chill language\n");
169     printf ("modula-2         Use the Modula-2 language\n");
170     /* Restore the silly string. */
171     set_language(current_language->la_language);
172     return;
173   }
174
175   /* Search the list of languages for a match.  */
176   for (i = 0; i < languages_size; i++) {
177     if (STREQ (languages[i]->la_name, language)) {
178       /* Found it!  Go into manual mode, and use this language.  */
179       if (languages[i]->la_language == language_auto) {
180         /* Enter auto mode.  Set to the current frame's language, if known.  */
181         language_mode = language_mode_auto;
182         flang = get_frame_language();
183         if (flang!=language_unknown)
184           set_language(flang);
185         expected_language = current_language;
186         return;
187       } else {
188         /* Enter manual mode.  Set the specified language.  */
189         language_mode = language_mode_manual;
190         current_language = languages[i];
191         set_type_range ();
192         set_lang_str();
193         expected_language = current_language;
194         return;
195       }
196     }
197   }
198
199   /* Reset the language (esp. the global string "language") to the 
200      correct values. */
201   err_lang=savestring(language,strlen(language));
202   make_cleanup (free, err_lang);        /* Free it after error */
203   set_language(current_language->la_language);
204   error ("Unknown language `%s'.",err_lang);
205 }
206
207 /* Show command.  Display a warning if the type setting does
208    not match the current language. */
209 static void
210 show_type_command(ignore, from_tty)
211    char *ignore;
212    int from_tty;
213 {
214    if (type_check != current_language->la_type_check)
215       printf(
216 "Warning: the current type check setting does not match the language.\n");
217 }
218
219 /* Set command.  Change the setting for type checking. */
220 static void
221 set_type_command(ignore, from_tty)
222    char *ignore;
223    int from_tty;
224 {
225    if (STREQ(type,"on"))
226    {
227       type_check = type_check_on;
228       type_mode = type_mode_manual;
229    }
230    else if (STREQ(type,"warn"))
231    {
232       type_check = type_check_warn;
233       type_mode = type_mode_manual;
234    }
235    else if (STREQ(type,"off"))
236    {
237       type_check = type_check_off;
238       type_mode = type_mode_manual;
239    }
240    else if (STREQ(type,"auto"))
241    {
242       type_mode = type_mode_auto;
243       set_type_range();
244       /* Avoid hitting the set_type_str call below.  We
245          did it in set_type_range. */
246       return;
247    }
248    set_type_str();
249    show_type_command((char *)NULL, from_tty);
250 }
251
252 /* Show command.  Display a warning if the range setting does
253    not match the current language. */
254 static void
255 show_range_command(ignore, from_tty)
256    char *ignore;
257    int from_tty;
258 {
259
260    if (range_check != current_language->la_range_check)
261       printf(
262 "Warning: the current range check setting does not match the language.\n");
263 }
264
265 /* Set command.  Change the setting for range checking. */
266 static void
267 set_range_command(ignore, from_tty)
268    char *ignore;
269    int from_tty;
270 {
271    if (STREQ(range,"on"))
272    {
273       range_check = range_check_on;
274       range_mode = range_mode_manual;
275    }
276    else if (STREQ(range,"warn"))
277    {
278       range_check = range_check_warn;
279       range_mode = range_mode_manual;
280    }
281    else if (STREQ(range,"off"))
282    {
283       range_check = range_check_off;
284       range_mode = range_mode_manual;
285    }
286    else if (STREQ(range,"auto"))
287    {
288       range_mode = range_mode_auto;
289       set_type_range();
290       /* Avoid hitting the set_range_str call below.  We
291          did it in set_type_range. */
292       return;
293    }
294    set_range_str();
295    show_range_command((char *)0, from_tty);
296 }
297
298 /* Set the status of range and type checking based on
299    the current modes and the current language.
300    If SHOW is non-zero, then print out the current language,
301    type and range checking status. */
302 static void
303 set_type_range()
304 {
305
306   if (range_mode == range_mode_auto)
307     range_check = current_language->la_range_check;
308
309   if (type_mode == type_mode_auto)
310     type_check = current_language->la_type_check;
311
312   set_type_str();
313   set_range_str();
314 }
315
316 /* Set current language to (enum language) LANG.  */
317
318 void
319 set_language(lang)
320    enum language lang;
321 {
322   int i;
323
324   for (i = 0; i < languages_size; i++) {
325     if (languages[i]->la_language == lang) {
326       current_language = languages[i];
327       set_type_range ();
328       set_lang_str();
329       break;
330     }
331   }
332 }
333 \f
334 /* This page contains functions that update the global vars
335    language, type and range. */
336 static void
337 set_lang_str()
338 {
339    char *prefix = "";
340
341    free (language);
342    if (language_mode == language_mode_auto)
343       prefix = "auto; currently ";
344
345    language = concat(prefix, current_language->la_name, NULL);
346 }
347
348 static void
349 set_type_str()
350 {
351    char *tmp, *prefix = "";
352
353    free (type);
354    if (type_mode==type_mode_auto)
355       prefix = "auto; currently ";
356
357    switch(type_check)
358    {
359    case type_check_on:
360       tmp = "on";
361       break;
362    case type_check_off:
363       tmp = "off";
364       break;
365    case type_check_warn:
366       tmp = "warn";
367       break;
368       default:
369       error ("Unrecognized type check setting.");
370    }
371
372    type = concat(prefix,tmp,NULL);
373 }
374
375 static void
376 set_range_str()
377 {
378    char *tmp, *pref = "";
379
380    free (range);
381    if (range_mode==range_mode_auto)
382       pref = "auto; currently ";
383
384    switch(range_check)
385    {
386    case range_check_on:
387       tmp = "on";
388       break;
389    case range_check_off:
390       tmp = "off";
391       break;
392    case range_check_warn:
393       tmp = "warn";
394       break;
395       default:
396       error ("Unrecognized range check setting.");
397    }
398
399    range = concat(pref,tmp,NULL);
400 }
401
402
403 /* Print out the current language settings: language, range and
404    type checking.  If QUIETLY, print only what has changed.  */
405
406 void
407 language_info (quietly)
408      int quietly;
409 {
410   if (quietly && expected_language == current_language)
411     return;
412
413   expected_language = current_language;
414   printf("Current language:  %s\n",language);
415   show_language_command((char *)0, 1);
416
417   if (!quietly)
418     {
419        printf("Type checking:     %s\n",type);
420        show_type_command((char *)0, 1);
421        printf("Range checking:    %s\n",range);
422        show_range_command((char *)0, 1);
423     }
424 }
425 \f
426 /* Return the result of a binary operation. */
427
428 #if 0   /* Currently unused */
429
430 struct type *
431 binop_result_type(v1,v2)
432    value v1,v2;
433 {
434    int l1,l2,size,uns;
435
436    l1 = TYPE_LENGTH(VALUE_TYPE(v1));
437    l2 = TYPE_LENGTH(VALUE_TYPE(v2));
438
439    switch(current_language->la_language)
440    {
441    case language_c:
442    case language_cplus:
443       if (TYPE_CODE(VALUE_TYPE(v1))==TYPE_CODE_FLT)
444          return TYPE_CODE(VALUE_TYPE(v2)) == TYPE_CODE_FLT && l2 > l1 ?
445             VALUE_TYPE(v2) : VALUE_TYPE(v1);
446       else if (TYPE_CODE(VALUE_TYPE(v2))==TYPE_CODE_FLT)
447          return TYPE_CODE(VALUE_TYPE(v1)) == TYPE_CODE_FLT && l1 > l2 ?
448             VALUE_TYPE(v1) : VALUE_TYPE(v2);
449       else if (TYPE_UNSIGNED(VALUE_TYPE(v1)) && l1 > l2)
450          return VALUE_TYPE(v1);
451       else if (TYPE_UNSIGNED(VALUE_TYPE(v2)) && l2 > l1)
452          return VALUE_TYPE(v2);
453       else  /* Both are signed.  Result is the longer type */
454          return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
455       break;
456    case language_m2:
457       /* If we are doing type-checking, l1 should equal l2, so this is
458          not needed. */
459       return l1 > l2 ? VALUE_TYPE(v1) : VALUE_TYPE(v2);
460       break;
461    case language_chill:
462       error ("Missing Chill support in function binop_result_check.");/*FIXME*/
463    }
464    abort();
465    return (struct type *)0;     /* For lint */
466 }
467
468 #endif  /* 0 */
469
470 \f
471 /* This page contains functions that return format strings for
472    printf for printing out numbers in different formats */
473
474 /* Returns the appropriate printf format for hexadecimal
475    numbers. */
476 char *
477 local_hex_format_custom(pre)
478    char *pre;
479 {
480    static char form[50];
481
482    strcpy (form, local_hex_format_prefix ());
483    strcat (form, "%");
484    strcat (form, pre);
485    strcat (form, local_hex_format_specifier ());
486    strcat (form, local_hex_format_suffix ());
487    return form;
488 }
489
490 /* Converts a number to hexadecimal and stores it in a static
491    string.  Returns a pointer to this string. */
492 char *
493 local_hex_string (num)
494    int num;
495 {
496    static char res[50];
497
498    sprintf (res, local_hex_format(), num);
499    return res;
500 }
501
502 /* Converts a number to custom hexadecimal and stores it in a static
503    string.  Returns a pointer to this string. */
504 char *
505 local_hex_string_custom(num,pre)
506    int num;
507    char *pre;
508 {
509    static char res[50];
510
511    sprintf (res, local_hex_format_custom(pre), num);
512    return res;
513 }
514
515 /* Returns the appropriate printf format for octal
516    numbers. */
517 char *
518 local_octal_format_custom(pre)
519    char *pre;
520 {
521    static char form[50];
522
523    strcpy (form, local_octal_format_prefix ());
524    strcat (form, "%");
525    strcat (form, pre);
526    strcat (form, local_octal_format_specifier ());
527    strcat (form, local_octal_format_suffix ());
528    return form;
529 }
530
531 /* Returns the appropriate printf format for decimal numbers. */
532 char *
533 local_decimal_format_custom(pre)
534    char *pre;
535 {
536    static char form[50];
537
538    strcpy (form, local_decimal_format_prefix ());
539    strcat (form, "%");
540    strcat (form, pre);
541    strcat (form, local_decimal_format_specifier ());
542    strcat (form, local_decimal_format_suffix ());
543    return form;
544 }
545 \f
546 /* This page contains functions that are used in type/range checking.
547    They all return zero if the type/range check fails.
548
549    It is hoped that these will make extending GDB to parse different
550    languages a little easier.  These are primarily used in eval.c when
551    evaluating expressions and making sure that their types are correct.
552    Instead of having a mess of conjucted/disjuncted expressions in an "if",
553    the ideas of type can be wrapped up in the following functions.
554
555    Note that some of them are not currently dependent upon which language
556    is currently being parsed.  For example, floats are the same in
557    C and Modula-2 (ie. the only floating point type has TYPE_CODE of
558    TYPE_CODE_FLT), while booleans are different. */
559
560 /* Returns non-zero if its argument is a simple type.  This is the same for
561    both Modula-2 and for C.  In the C case, TYPE_CODE_CHAR will never occur,
562    and thus will never cause the failure of the test. */
563 int
564 simple_type(type)
565     struct type *type;
566 {
567   switch (TYPE_CODE (type)) {
568   case TYPE_CODE_INT:
569   case TYPE_CODE_CHAR:
570   case TYPE_CODE_ENUM:
571   case TYPE_CODE_FLT:
572   case TYPE_CODE_RANGE:
573   case TYPE_CODE_BOOL:
574     return 1;
575
576   default:
577     return 0;
578   }
579 }
580
581 /* Returns non-zero if its argument is of an ordered type.
582    An ordered type is one in which the elements can be tested for the
583    properties of "greater than", "less than", etc, or for which the
584    operations "increment" or "decrement" make sense. */
585 int
586 ordered_type (type)
587    struct type *type;
588 {
589   switch (TYPE_CODE (type)) {
590   case TYPE_CODE_INT:
591   case TYPE_CODE_CHAR:
592   case TYPE_CODE_ENUM:
593   case TYPE_CODE_FLT:
594   case TYPE_CODE_RANGE:
595     return 1;
596
597   default:
598     return 0;
599   }
600 }
601
602 /* Returns non-zero if the two types are the same */
603 int
604 same_type (arg1, arg2)
605    struct type *arg1, *arg2;
606 {
607    if (structured_type(arg1) ? !structured_type(arg2) : structured_type(arg2))
608       /* One is structured and one isn't */
609       return 0;
610    else if (structured_type(arg1) && structured_type(arg2))
611       return arg1 == arg2;
612    else if (numeric_type(arg1) && numeric_type(arg2))
613       return (TYPE_CODE(arg2) == TYPE_CODE(arg1)) &&
614          (TYPE_UNSIGNED(arg1) == TYPE_UNSIGNED(arg2))
615             ? 1 : 0;
616    else
617       return arg1==arg2;
618 }
619
620 /* Returns non-zero if the type is integral */
621 int
622 integral_type (type)
623    struct type *type;
624 {
625    switch(current_language->la_language)
626    {
627    case language_c:
628    case language_cplus:
629       return (TYPE_CODE(type) != TYPE_CODE_INT) &&
630          (TYPE_CODE(type) != TYPE_CODE_ENUM) ? 0 : 1;
631    case language_m2:
632       return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
633    case language_chill:
634       error ("Missing Chill support in function integral_type.");  /*FIXME*/
635    default:
636       error ("Language not supported.");
637    }
638 }
639
640 /* Returns non-zero if the value is numeric */
641 int
642 numeric_type (type)
643    struct type *type;
644 {
645   switch (TYPE_CODE (type)) {
646   case TYPE_CODE_INT:
647   case TYPE_CODE_FLT:
648     return 1;
649
650   default:
651     return 0;
652   }
653 }
654
655 /* Returns non-zero if the value is a character type */
656 int
657 character_type (type)
658    struct type *type;
659 {
660    switch(current_language->la_language)
661    {
662    case language_chill:
663    case language_m2:
664       return TYPE_CODE(type) != TYPE_CODE_CHAR ? 0 : 1;
665
666    case language_c:
667    case language_cplus:
668       return (TYPE_CODE(type) == TYPE_CODE_INT) &&
669          TYPE_LENGTH(type) == sizeof(char)
670          ? 1 : 0;
671    default:
672       return (0);
673    }
674 }
675
676 /* Returns non-zero if the value is a string type */
677 int
678 string_type (type)
679    struct type *type;
680 {
681    switch(current_language->la_language)
682    {
683    case language_chill:
684    case language_m2:
685       return TYPE_CODE(type) != TYPE_CODE_STRING ? 0 : 1;
686
687    case language_c:
688    case language_cplus:
689       /* C does not have distinct string type. */
690       return (0);
691    default:
692       return (0);
693    }
694 }
695
696 /* Returns non-zero if the value is a boolean type */
697 int
698 boolean_type (type)
699    struct type *type;
700 {
701    switch(current_language->la_language)
702    {
703    case language_chill:
704    case language_m2:
705       return TYPE_CODE(type) != TYPE_CODE_BOOL ? 0 : 1;
706
707    case language_c:
708    case language_cplus:
709       return TYPE_CODE(type) != TYPE_CODE_INT ? 0 : 1;
710    default:
711       return (0);
712    }
713 }
714
715 /* Returns non-zero if the value is a floating-point type */
716 int
717 float_type (type)
718    struct type *type;
719 {
720    return TYPE_CODE(type) == TYPE_CODE_FLT;
721 }
722
723 /* Returns non-zero if the value is a pointer type */
724 int
725 pointer_type(type)
726    struct type *type;
727 {
728    return TYPE_CODE(type) == TYPE_CODE_PTR ||
729       TYPE_CODE(type) == TYPE_CODE_REF;
730 }
731
732 /* Returns non-zero if the value is a structured type */
733 int
734 structured_type(type)
735    struct type *type;
736 {
737    switch(current_language->la_language)
738    {
739    case language_c:
740    case language_cplus:
741       return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
742          (TYPE_CODE(type) == TYPE_CODE_UNION) ||
743             (TYPE_CODE(type) == TYPE_CODE_ARRAY);
744    case language_m2:
745       return (TYPE_CODE(type) == TYPE_CODE_STRUCT) ||
746          (TYPE_CODE(type) == TYPE_CODE_SET) ||
747             (TYPE_CODE(type) == TYPE_CODE_ARRAY);
748    case language_chill:
749       error ("Missing Chill support in function structured_type.");  /*FIXME*/
750    default:
751       return (0);
752    }
753 }
754 \f
755 /* This page contains functions that return info about
756    (struct value) values used in GDB. */
757
758 /* Returns non-zero if the value VAL represents a true value. */
759 int
760 value_true(val)
761      value val;
762 {
763   int len, i;
764   struct type *type;
765   LONGEST v;
766
767   switch (current_language->la_language) {
768
769   case language_c:
770   case language_cplus:
771     return !value_logical_not (val);
772
773   case language_m2:
774     type = VALUE_TYPE(val);
775     if (TYPE_CODE (type) != TYPE_CODE_BOOL)
776       return 0;         /* Not a BOOLEAN at all */
777     /* Search the fields for one that matches the current value. */
778     len = TYPE_NFIELDS (type);
779     v = value_as_long (val);
780     for (i = 0; i < len; i++)
781       {
782         QUIT;
783         if (v == TYPE_FIELD_BITPOS (type, i))
784           break;
785       }
786     if (i >= len)
787       return 0;         /* Not a valid BOOLEAN value */
788     if (STREQ ("TRUE", TYPE_FIELD_NAME(VALUE_TYPE(val), i)))
789       return 1;         /* BOOLEAN with value TRUE */
790     else
791       return 0;         /* BOOLEAN with value FALSE */
792     break;
793
794   case language_chill:
795     error ("Missing Chill support in function value_type.");  /*FIXME*/
796
797   default:
798     error ("Language not supported.");
799   }
800 }
801 \f
802 /* Returns non-zero if the operator OP is defined on
803    the values ARG1 and ARG2. */
804
805 #if 0   /* Currently unused */
806
807 void
808 binop_type_check(arg1,arg2,op)
809    value arg1,arg2;
810    int op;
811 {
812    struct type *t1, *t2;
813
814    /* If we're not checking types, always return success. */
815    if (!STRICT_TYPE)
816       return;
817
818    t1=VALUE_TYPE(arg1);
819    if (arg2!=(value)NULL)
820       t2=VALUE_TYPE(arg2);
821    else
822       t2=NULL;
823
824    switch(op)
825    {
826    case BINOP_ADD:
827    case BINOP_SUB:
828       if ((numeric_type(t1) && pointer_type(t2)) ||
829          (pointer_type(t1) && numeric_type(t2)))
830       {
831          warning ("combining pointer and integer.\n");
832          break;
833       }
834    case BINOP_MUL:
835    case BINOP_LSH:
836    case BINOP_RSH:
837       if (!numeric_type(t1) || !numeric_type(t2))
838          type_op_error ("Arguments to %s must be numbers.",op);
839       else if (!same_type(t1,t2))
840          type_op_error ("Arguments to %s must be of the same type.",op);
841       break;
842
843    case BINOP_LOGICAL_AND:
844    case BINOP_LOGICAL_OR:
845       if (!boolean_type(t1) || !boolean_type(t2))
846          type_op_error ("Arguments to %s must be of boolean type.",op);
847       break;
848
849    case BINOP_EQUAL:
850       if ((pointer_type(t1) && !(pointer_type(t2) || integral_type(t2))) ||
851          (pointer_type(t2) && !(pointer_type(t1) || integral_type(t1))))
852          type_op_error ("A pointer can only be compared to an integer or pointer.",op);
853       else if ((pointer_type(t1) && integral_type(t2)) ||
854          (integral_type(t1) && pointer_type(t2)))
855       {
856          warning ("combining integer and pointer.\n");
857          break;
858       }
859       else if (!simple_type(t1) || !simple_type(t2))
860          type_op_error ("Arguments to %s must be of simple type.",op);
861       else if (!same_type(t1,t2))
862          type_op_error ("Arguments to %s must be of the same type.",op);
863       break;
864
865    case BINOP_REM:
866    case BINOP_MOD:
867       if (!integral_type(t1) || !integral_type(t2))
868          type_op_error ("Arguments to %s must be of integral type.",op);
869       break;
870
871    case BINOP_LESS:
872    case BINOP_GTR:
873    case BINOP_LEQ:
874    case BINOP_GEQ:
875       if (!ordered_type(t1) || !ordered_type(t2))
876          type_op_error ("Arguments to %s must be of ordered type.",op);
877       else if (!same_type(t1,t2))
878          type_op_error ("Arguments to %s must be of the same type.",op);
879       break;
880
881    case BINOP_ASSIGN:
882       if (pointer_type(t1) && !integral_type(t2))
883          type_op_error ("A pointer can only be assigned an integer.",op);
884       else if (pointer_type(t1) && integral_type(t2))
885       {
886          warning ("combining integer and pointer.");
887          break;
888       }
889       else if (!simple_type(t1) || !simple_type(t2))
890          type_op_error ("Arguments to %s must be of simple type.",op);
891       else if (!same_type(t1,t2))
892          type_op_error ("Arguments to %s must be of the same type.",op);
893       break;
894
895     case BINOP_CONCAT:
896       /* FIXME:  Needs to handle bitstrings as well. */
897       if (!(string_type(t1) || character_type(t1) || integral_type(t1))
898           || !(string_type(t2) || character_type(t2) || integral_type(t2)))
899           type_op_error ("Arguments to %s must be strings or characters.", op);
900       break;
901
902    /* Unary checks -- arg2 is null */
903
904    case UNOP_LOGICAL_NOT:
905       if (!boolean_type(t1))
906          type_op_error ("Argument to %s must be of boolean type.",op);
907       break;
908
909    case UNOP_PLUS:
910    case UNOP_NEG:
911       if (!numeric_type(t1))
912          type_op_error ("Argument to %s must be of numeric type.",op);
913       break;
914
915    case UNOP_IND:
916       if (integral_type(t1))
917       {
918          warning ("combining pointer and integer.\n");
919          break;
920       }
921       else if (!pointer_type(t1))
922          type_op_error ("Argument to %s must be a pointer.",op);
923       break;
924
925    case UNOP_PREINCREMENT:
926    case UNOP_POSTINCREMENT:
927    case UNOP_PREDECREMENT:
928    case UNOP_POSTDECREMENT:
929       if (!ordered_type(t1))
930          type_op_error ("Argument to %s must be of an ordered type.",op);
931       break;
932
933    default:
934       /* Ok.  The following operators have different meanings in
935          different languages. */
936       switch(current_language->la_language)
937       {
938 #ifdef _LANG_c
939       case language_c:
940       case language_cplus:
941          switch(op)
942          {
943          case BINOP_DIV:
944             if (!numeric_type(t1) || !numeric_type(t2))
945                type_op_error ("Arguments to %s must be numbers.",op);
946             break;
947          }
948          break;
949 #endif
950
951 #ifdef _LANG_m2
952       case language_m2:
953          switch(op)
954          {
955          case BINOP_DIV:
956             if (!float_type(t1) || !float_type(t2))
957                type_op_error ("Arguments to %s must be floating point numbers.",op);
958             break;
959          case BINOP_INTDIV:
960             if (!integral_type(t1) || !integral_type(t2))
961                type_op_error ("Arguments to %s must be of integral type.",op);
962             break;
963          }
964 #endif
965
966 #ifdef _LANG_chill
967        case language_chill:
968          error ("Missing Chill support in function binop_type_check.");/*FIXME*/
969 #endif
970
971       }
972    }
973 }
974
975 #endif  /* 0 */
976
977 \f
978 /* This page contains functions for the printing out of
979    error messages that occur during type- and range-
980    checking. */
981
982 /* Prints the format string FMT with the operator as a string
983    corresponding to the opcode OP.  If FATAL is non-zero, then
984    this is an error and error () is called.  Otherwise, it is
985    a warning and printf() is called. */
986 void
987 op_error (fmt,op,fatal)
988    char *fmt;
989    enum exp_opcode op;
990    int fatal;
991 {
992    if (fatal)
993       error (fmt,op_string(op));
994    else
995    {
996       warning (fmt,op_string(op));
997    }
998 }
999
1000 /* These are called when a language fails a type- or range-check.
1001    The first argument should be a printf()-style format string, and
1002    the rest of the arguments should be its arguments.  If
1003    [type|range]_check is [type|range]_check_on, then return_to_top_level()
1004    is called in the style of error ().  Otherwise, the message is prefixed
1005    by the value of warning_pre_print and we do not return to the top level. */
1006
1007 void
1008 type_error (va_alist)
1009      va_dcl
1010 {
1011    va_list args;
1012    char *string;
1013
1014    if (type_check==type_check_warn)
1015       fprintf(stderr,warning_pre_print);
1016    else
1017       target_terminal_ours();
1018
1019    va_start (args);
1020    string = va_arg (args, char *);
1021    vfprintf (stderr, string, args);
1022    fprintf (stderr, "\n");
1023    va_end (args);
1024    if (type_check==type_check_on)
1025       return_to_top_level (RETURN_ERROR);
1026 }
1027
1028 void
1029 range_error (va_alist)
1030      va_dcl
1031 {
1032    va_list args;
1033    char *string;
1034
1035    if (range_check==range_check_warn)
1036       fprintf(stderr,warning_pre_print);
1037    else
1038       target_terminal_ours();
1039
1040    va_start (args);
1041    string = va_arg (args, char *);
1042    vfprintf (stderr, string, args);
1043    fprintf (stderr, "\n");
1044    va_end (args);
1045    if (range_check==range_check_on)
1046       return_to_top_level (RETURN_ERROR);
1047 }
1048
1049 \f
1050 /* This page contains miscellaneous functions */
1051
1052 /* Return the language struct for a given language enum. */
1053
1054 const struct language_defn *
1055 language_def(lang)
1056    enum language lang;
1057 {
1058   int i;
1059
1060   for (i = 0; i < languages_size; i++) {
1061     if (languages[i]->la_language == lang) {
1062       return languages[i];
1063     }
1064   }
1065   return NULL;
1066 }
1067
1068 /* Return the language as a string */
1069 char *
1070 language_str(lang)
1071    enum language lang;
1072 {
1073   int i;
1074
1075   for (i = 0; i < languages_size; i++) {
1076     if (languages[i]->la_language == lang) {
1077       return languages[i]->la_name;
1078     }
1079   }
1080   return "Unknown";
1081 }
1082
1083 static void
1084 set_check (ignore, from_tty)
1085    char *ignore;
1086    int from_tty;
1087 {
1088    printf(
1089 "\"set check\" must be followed by the name of a check subcommand.\n");
1090    help_list(setchecklist, "set check ", -1, stdout);
1091 }
1092
1093 static void
1094 show_check (ignore, from_tty)
1095    char *ignore;
1096    int from_tty;
1097 {
1098    cmd_show_list(showchecklist, from_tty, "");
1099 }
1100 \f
1101 /* Add a language to the set of known languages.  */
1102
1103 void
1104 add_language (lang)
1105      const struct language_defn *lang;
1106 {
1107   if (lang->la_magic != LANG_MAGIC)
1108     {
1109       fprintf(stderr, "Magic number of %s language struct wrong\n",
1110         lang->la_name);
1111       abort();
1112     }
1113
1114   if (!languages)
1115     {
1116       languages_allocsize = DEFAULT_ALLOCSIZE;
1117       languages = (const struct language_defn **) xmalloc
1118         (languages_allocsize * sizeof (*languages));
1119     }
1120   if (languages_size >= languages_allocsize)
1121     {
1122       languages_allocsize *= 2;
1123       languages = (const struct language_defn **) xrealloc ((char *) languages,
1124         languages_allocsize * sizeof (*languages));
1125     }
1126   languages[languages_size++] = lang;
1127 }
1128
1129 /* Define the language that is no language.  */
1130
1131 static int
1132 unk_lang_parser ()
1133 {
1134   return 1;
1135 }
1136
1137 static void
1138 unk_lang_error (msg)
1139      char *msg;
1140 {
1141   error ("Attempted to parse an expression with unknown language");
1142 }
1143
1144 static void
1145 unk_lang_printchar (c, stream)
1146      register int c;
1147      FILE *stream;
1148 {
1149   error ("internal error - unimplemented function unk_lang_printchar called.");
1150 }
1151
1152 static void
1153 unk_lang_printstr (stream, string, length, force_ellipses)
1154      FILE *stream;
1155      char *string;
1156      unsigned int length;
1157      int force_ellipses;
1158 {
1159   error ("internal error - unimplemented function unk_lang_printstr called.");
1160 }
1161
1162 static struct type *
1163 unk_lang_create_fundamental_type (objfile, typeid)
1164      struct objfile *objfile;
1165      int typeid;
1166 {
1167   error ("internal error - unimplemented function unk_lang_create_fundamental_type called.");
1168 }
1169
1170 void
1171 unk_lang_print_type (type, varstring, stream, show, level)
1172      struct type *type;
1173      char *varstring;
1174      FILE *stream;
1175      int show;
1176      int level;
1177 {
1178   error ("internal error - unimplemented function unk_lang_print_type called.");
1179 }
1180
1181 int
1182 unk_lang_val_print (type, valaddr, address, stream, format, deref_ref,
1183                     recurse, pretty)
1184      struct type *type;
1185      char *valaddr;
1186      CORE_ADDR address;
1187      FILE *stream;
1188      int format;
1189      int deref_ref;
1190      int recurse;
1191      enum val_prettyprint pretty;
1192 {
1193   error ("internal error - unimplemented function unk_lang_val_print called.");
1194 }
1195
1196 static struct type ** const (unknown_builtin_types[]) = { 0 };
1197 static const struct op_print unk_op_print_tab[] = {
1198     {NULL, OP_NULL, PREC_NULL, 0}
1199 };
1200
1201 const struct language_defn unknown_language_defn = {
1202   "unknown",
1203   language_unknown,
1204   &unknown_builtin_types[0],
1205   range_check_off,
1206   type_check_off,
1207   unk_lang_parser,
1208   unk_lang_error,
1209   unk_lang_printchar,           /* Print character constant */
1210   unk_lang_printstr,
1211   unk_lang_create_fundamental_type,
1212   unk_lang_print_type,          /* Print a type using appropriate syntax */
1213   unk_lang_val_print,           /* Print a value using appropriate syntax */
1214   &builtin_type_error,          /* longest signed   integral type */
1215   &builtin_type_error,          /* longest unsigned integral type */
1216   &builtin_type_error,          /* longest floating point type */
1217   {"",      "",    "",   ""},   /* Binary format info */
1218   {"0%o",   "0",   "o",  ""},   /* Octal format info */
1219   {"%d",    "",    "d",  ""},   /* Decimal format info */
1220   {"0x%x",  "0x",  "x",  ""},   /* Hex format info */
1221   unk_op_print_tab,             /* expression operators for printing */
1222   LANG_MAGIC
1223 };
1224
1225 /* These two structs define fake entries for the "local" and "auto" options. */
1226 const struct language_defn auto_language_defn = {
1227   "auto",
1228   language_auto,
1229   &unknown_builtin_types[0],
1230   range_check_off,
1231   type_check_off,
1232   unk_lang_parser,
1233   unk_lang_error,
1234   unk_lang_printchar,           /* Print character constant */
1235   unk_lang_printstr,
1236   unk_lang_create_fundamental_type,
1237   unk_lang_print_type,          /* Print a type using appropriate syntax */
1238   unk_lang_val_print,           /* Print a value using appropriate syntax */
1239   &builtin_type_error,          /* longest signed   integral type */
1240   &builtin_type_error,          /* longest unsigned integral type */
1241   &builtin_type_error,          /* longest floating point type */
1242   {"",      "",    "",   ""},   /* Binary format info */
1243   {"0%o",   "0",   "o",  ""},   /* Octal format info */
1244   {"%d",    "",    "d",  ""},   /* Decimal format info */
1245   {"0x%x",  "0x",  "x",  ""},   /* Hex format info */
1246   unk_op_print_tab,             /* expression operators for printing */
1247   LANG_MAGIC
1248 };
1249
1250 const struct language_defn local_language_defn = {
1251   "local",
1252   language_auto,
1253   &unknown_builtin_types[0],
1254   range_check_off,
1255   type_check_off,
1256   unk_lang_parser,
1257   unk_lang_error,
1258   unk_lang_printchar,           /* Print character constant */
1259   unk_lang_printstr,
1260   unk_lang_create_fundamental_type,
1261   unk_lang_print_type,          /* Print a type using appropriate syntax */
1262   unk_lang_val_print,           /* Print a value using appropriate syntax */
1263   &builtin_type_error,          /* longest signed   integral type */
1264   &builtin_type_error,          /* longest unsigned integral type */
1265   &builtin_type_error,          /* longest floating point type */
1266   {"",      "",    "",   ""},   /* Binary format info */
1267   {"0%o",   "0",   "o",  ""},   /* Octal format info */
1268   {"%d",    "",    "d",  ""},   /* Decimal format info */
1269   {"0x%x",  "0x",  "x",  ""},   /* Hex format info */
1270   unk_op_print_tab,             /* expression operators for printing */
1271   LANG_MAGIC
1272 };
1273 \f
1274 /* Initialize the language routines */
1275
1276 void
1277 _initialize_language()
1278 {
1279    struct cmd_list_element *set, *show;
1280
1281    /* GDB commands for language specific stuff */
1282
1283    set = add_set_cmd ("language", class_support, var_string_noescape,
1284                       (char *)&language,
1285                       "Set the current source language.",
1286                       &setlist);
1287    show = add_show_from_set (set, &showlist);
1288    set->function.cfunc = set_language_command;
1289    show->function.cfunc = show_language_command;
1290
1291    add_prefix_cmd ("check", no_class, set_check,
1292                    "Set the status of the type/range checker",
1293                    &setchecklist, "set check ", 0, &setlist);
1294    add_alias_cmd ("c", "check", no_class, 1, &setlist);
1295    add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1296
1297    add_prefix_cmd ("check", no_class, show_check,
1298                    "Show the status of the type/range checker",
1299                    &showchecklist, "show check ", 0, &showlist);
1300    add_alias_cmd ("c", "check", no_class, 1, &showlist);
1301    add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1302
1303    set = add_set_cmd ("type", class_support, var_string_noescape,
1304                       (char *)&type,
1305                       "Set type checking.  (on/warn/off/auto)",
1306                       &setchecklist);
1307    show = add_show_from_set (set, &showchecklist);
1308    set->function.cfunc = set_type_command;
1309    show->function.cfunc = show_type_command;
1310
1311    set = add_set_cmd ("range", class_support, var_string_noescape,
1312                       (char *)&range,
1313                       "Set range checking.  (on/warn/off/auto)",
1314                       &setchecklist);
1315    show = add_show_from_set (set, &showchecklist);
1316    set->function.cfunc = set_range_command;
1317    show->function.cfunc = show_range_command;
1318
1319    add_language (&unknown_language_defn);
1320    add_language (&local_language_defn);
1321    add_language (&auto_language_defn);
1322
1323    language = savestring ("auto",strlen("auto"));
1324    range = savestring ("auto",strlen("auto"));
1325    type = savestring ("auto",strlen("auto"));
1326
1327    /* Have the above take effect */
1328
1329    set_language_command (language, 0);
1330    set_type_command (NULL, 0);
1331    set_range_command (NULL, 0);
1332 }