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