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