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