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