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