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