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