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