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