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