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