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