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