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