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