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