Move filename extensions into language_defn
[external/binutils.git] / gdb / language.c
1 /* Multiple source language support for GDB.
2
3    Copyright (C) 1991-2016 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 "jv-lang.h"
43 #include "demangle.h"
44 #include "symfile.h"
45 #include "cp-support.h"
46 #include "frame.h"
47
48 extern void _initialize_language (void);
49
50 static void unk_lang_error (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       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       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 ||
414     TYPE_CODE (type) == TYPE_CODE_REF;
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 (char *str)
477 {
478   int i;
479
480   for (i = 0; i < languages_size; i++)
481     if (strcmp (languages[i]->la_name, str) == 0)
482       return languages[i]->la_language;
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   int i;
493
494   for (i = 0; i < languages_size; i++)
495     {
496       if (languages[i]->la_language == lang)
497         {
498           return languages[i];
499         }
500     }
501   return NULL;
502 }
503
504 /* Return the language as a string.  */
505 const char *
506 language_str (enum language lang)
507 {
508   int i;
509
510   for (i = 0; i < languages_size; i++)
511     {
512       if (languages[i]->la_language == lang)
513         {
514           return languages[i]->la_name;
515         }
516     }
517   return "Unknown";
518 }
519
520 static void
521 set_check (char *ignore, int from_tty)
522 {
523   printf_unfiltered (
524      "\"set check\" must be followed by the name of a check subcommand.\n");
525   help_list (setchecklist, "set check ", all_commands, gdb_stdout);
526 }
527
528 static void
529 show_check (char *ignore, int from_tty)
530 {
531   cmd_show_list (showchecklist, from_tty, "");
532 }
533 \f
534 /* Add a language to the set of known languages.  */
535
536 void
537 add_language (const struct language_defn *lang)
538 {
539   /* For the "set language" command.  */
540   static const char **language_names = NULL;
541   /* For the "help set language" command.  */
542   char *language_set_doc = NULL;
543
544   int i;
545   struct ui_file *tmp_stream;
546
547   if (lang->la_magic != LANG_MAGIC)
548     {
549       fprintf_unfiltered (gdb_stderr,
550                           "Magic number of %s language struct wrong\n",
551                           lang->la_name);
552       internal_error (__FILE__, __LINE__,
553                       _("failed internal consistency check"));
554     }
555
556   if (!languages)
557     {
558       languages_allocsize = DEFAULT_ALLOCSIZE;
559       languages = XNEWVEC (const struct language_defn *, languages_allocsize);
560     }
561   if (languages_size >= languages_allocsize)
562     {
563       languages_allocsize *= 2;
564       languages = (const struct language_defn **) xrealloc ((char *) languages,
565                                  languages_allocsize * sizeof (*languages));
566     }
567   languages[languages_size++] = lang;
568
569   /* Build the language names array, to be used as enumeration in the
570      set language" enum command.  */
571   language_names = XRESIZEVEC (const char *, language_names,
572                                languages_size + 1);
573
574   for (i = 0; i < languages_size; ++i)
575     language_names[i] = languages[i]->la_name;
576   language_names[i] = NULL;
577
578   /* Add the filename extensions.  */
579   if (lang->la_filename_extensions != NULL)
580     {
581       int i;
582
583       for (i = 0; lang->la_filename_extensions[i] != NULL; ++i)
584         add_filename_language (lang->la_filename_extensions[i],
585                                lang->la_language);
586     }
587
588   /* Build the "help set language" docs.  */
589   tmp_stream = mem_fileopen ();
590
591   fprintf_unfiltered (tmp_stream,
592                       _("Set the current source language.\n"
593                         "The currently understood settings are:\n\nlocal or "
594                         "auto    Automatic setting based on source file\n"));
595
596   for (i = 0; i < languages_size; ++i)
597     {
598       /* Already dealt with these above.  */
599       if (languages[i]->la_language == language_unknown
600           || languages[i]->la_language == language_auto)
601         continue;
602
603       /* FIXME: i18n: for now assume that the human-readable name
604          is just a capitalization of the internal name.  */
605       fprintf_unfiltered (tmp_stream, "%-16s Use the %c%s language\n",
606                           languages[i]->la_name,
607                           /* Capitalize first letter of language
608                              name.  */
609                           toupper (languages[i]->la_name[0]),
610                           languages[i]->la_name + 1);
611     }
612
613   language_set_doc = ui_file_xstrdup (tmp_stream, NULL);
614   ui_file_delete (tmp_stream);
615
616   add_setshow_enum_cmd ("language", class_support,
617                         (const char **) language_names,
618                         &language,
619                         language_set_doc,
620                         _("Show the current source language."),
621                         NULL, set_language_command,
622                         show_language_command,
623                         &setlist, &showlist);
624
625   xfree (language_set_doc);
626 }
627
628 /* Iterate through all registered languages looking for and calling
629    any non-NULL struct language_defn.skip_trampoline() functions.
630    Return the result from the first that returns non-zero, or 0 if all
631    `fail'.  */
632 CORE_ADDR 
633 skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
634 {
635   int i;
636
637   for (i = 0; i < languages_size; i++)
638     {
639       if (languages[i]->skip_trampoline)
640         {
641           CORE_ADDR real_pc = (languages[i]->skip_trampoline) (frame, pc);
642
643           if (real_pc)
644             return real_pc;
645         }
646     }
647
648   return 0;
649 }
650
651 /* Return demangled language symbol, or NULL.
652    FIXME: Options are only useful for certain languages and ignored
653    by others, so it would be better to remove them here and have a
654    more flexible demangler for the languages that need it.
655    FIXME: Sometimes the demangler is invoked when we don't know the
656    language, so we can't use this everywhere.  */
657 char *
658 language_demangle (const struct language_defn *current_language, 
659                                 const char *mangled, int options)
660 {
661   if (current_language != NULL && current_language->la_demangle)
662     return current_language->la_demangle (mangled, options);
663   return NULL;
664 }
665
666 /* Return class name from physname or NULL.  */
667 char *
668 language_class_name_from_physname (const struct language_defn *lang,
669                                    const char *physname)
670 {
671   if (lang != NULL && lang->la_class_name_from_physname)
672     return lang->la_class_name_from_physname (physname);
673   return NULL;
674 }
675
676 /* Return non-zero if TYPE should be passed (and returned) by
677    reference at the language level.  */
678 int
679 language_pass_by_reference (struct type *type)
680 {
681   return current_language->la_pass_by_reference (type);
682 }
683
684 /* Return zero; by default, types are passed by value at the language
685    level.  The target ABI may pass or return some structs by reference
686    independent of this.  */
687 int
688 default_pass_by_reference (struct type *type)
689 {
690   return 0;
691 }
692
693 /* Return the default string containing the list of characters
694    delimiting words.  This is a reasonable default value that
695    most languages should be able to use.  */
696
697 char *
698 default_word_break_characters (void)
699 {
700   return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
701 }
702
703 /* Print the index of array elements using the C99 syntax.  */
704
705 void
706 default_print_array_index (struct value *index_value, struct ui_file *stream,
707                            const struct value_print_options *options)
708 {
709   fprintf_filtered (stream, "[");
710   LA_VALUE_PRINT (index_value, stream, options);
711   fprintf_filtered (stream, "] = ");
712 }
713
714 void
715 default_get_string (struct value *value, gdb_byte **buffer, int *length,
716                     struct type **char_type, const char **charset)
717 {
718   error (_("Getting a string is unsupported in this language."));
719 }
720
721 /* Define the language that is no language.  */
722
723 static int
724 unk_lang_parser (struct parser_state *ps)
725 {
726   return 1;
727 }
728
729 static void
730 unk_lang_error (char *msg)
731 {
732   error (_("Attempted to parse an expression with unknown language"));
733 }
734
735 static void
736 unk_lang_emit_char (int c, struct type *type, struct ui_file *stream,
737                     int quoter)
738 {
739   error (_("internal error - unimplemented "
740            "function unk_lang_emit_char called."));
741 }
742
743 static void
744 unk_lang_printchar (int c, struct type *type, struct ui_file *stream)
745 {
746   error (_("internal error - unimplemented "
747            "function unk_lang_printchar called."));
748 }
749
750 static void
751 unk_lang_printstr (struct ui_file *stream, struct type *type,
752                    const gdb_byte *string, unsigned int length,
753                    const char *encoding, int force_ellipses,
754                    const struct value_print_options *options)
755 {
756   error (_("internal error - unimplemented "
757            "function unk_lang_printstr called."));
758 }
759
760 static void
761 unk_lang_print_type (struct type *type, const char *varstring,
762                      struct ui_file *stream, int show, int level,
763                      const struct type_print_options *flags)
764 {
765   error (_("internal error - unimplemented "
766            "function unk_lang_print_type called."));
767 }
768
769 static void
770 unk_lang_val_print (struct type *type, const gdb_byte *valaddr,
771                     int embedded_offset, CORE_ADDR address,
772                     struct ui_file *stream, int recurse,
773                     const struct value *val,
774                     const struct value_print_options *options)
775 {
776   error (_("internal error - unimplemented "
777            "function unk_lang_val_print called."));
778 }
779
780 static void
781 unk_lang_value_print (struct value *val, struct ui_file *stream,
782                       const struct value_print_options *options)
783 {
784   error (_("internal error - unimplemented "
785            "function unk_lang_value_print called."));
786 }
787
788 static CORE_ADDR unk_lang_trampoline (struct frame_info *frame, CORE_ADDR pc)
789 {
790   return 0;
791 }
792
793 /* Unknown languages just use the cplus demangler.  */
794 static char *unk_lang_demangle (const char *mangled, int options)
795 {
796   return gdb_demangle (mangled, options);
797 }
798
799 static char *unk_lang_class_name (const char *mangled)
800 {
801   return NULL;
802 }
803
804 static const struct op_print unk_op_print_tab[] =
805 {
806   {NULL, OP_NULL, PREC_NULL, 0}
807 };
808
809 static void
810 unknown_language_arch_info (struct gdbarch *gdbarch,
811                             struct language_arch_info *lai)
812 {
813   lai->string_char_type = builtin_type (gdbarch)->builtin_char;
814   lai->bool_type_default = builtin_type (gdbarch)->builtin_int;
815   lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
816                                                        struct type *);
817 }
818
819 const struct language_defn unknown_language_defn =
820 {
821   "unknown",
822   "Unknown",
823   language_unknown,
824   range_check_off,
825   case_sensitive_on,
826   array_row_major,
827   macro_expansion_no,
828   NULL,
829   &exp_descriptor_standard,
830   unk_lang_parser,
831   unk_lang_error,
832   null_post_parser,
833   unk_lang_printchar,           /* Print character constant */
834   unk_lang_printstr,
835   unk_lang_emit_char,
836   unk_lang_print_type,          /* Print a type using appropriate syntax */
837   default_print_typedef,        /* Print a typedef using appropriate syntax */
838   unk_lang_val_print,           /* Print a value using appropriate syntax */
839   unk_lang_value_print,         /* Print a top-level value */
840   default_read_var_value,       /* la_read_var_value */
841   unk_lang_trampoline,          /* Language specific skip_trampoline */
842   "this",                       /* name_of_this */
843   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
844   basic_lookup_transparent_type,/* lookup_transparent_type */
845   unk_lang_demangle,            /* Language specific symbol demangler */
846   unk_lang_class_name,          /* Language specific
847                                    class_name_from_physname */
848   unk_op_print_tab,             /* expression operators for printing */
849   1,                            /* c-style arrays */
850   0,                            /* String lower bound */
851   default_word_break_characters,
852   default_make_symbol_completion_list,
853   unknown_language_arch_info,   /* la_language_arch_info.  */
854   default_print_array_index,
855   default_pass_by_reference,
856   default_get_string,
857   NULL,                         /* la_get_symbol_name_cmp */
858   iterate_over_symbols,
859   &default_varobj_ops,
860   NULL,
861   NULL,
862   LANG_MAGIC
863 };
864
865 /* These two structs define fake entries for the "local" and "auto"
866    options.  */
867 const struct language_defn auto_language_defn =
868 {
869   "auto",
870   "Auto",
871   language_auto,
872   range_check_off,
873   case_sensitive_on,
874   array_row_major,
875   macro_expansion_no,
876   NULL,
877   &exp_descriptor_standard,
878   unk_lang_parser,
879   unk_lang_error,
880   null_post_parser,
881   unk_lang_printchar,           /* Print character constant */
882   unk_lang_printstr,
883   unk_lang_emit_char,
884   unk_lang_print_type,          /* Print a type using appropriate syntax */
885   default_print_typedef,        /* Print a typedef using appropriate syntax */
886   unk_lang_val_print,           /* Print a value using appropriate syntax */
887   unk_lang_value_print,         /* Print a top-level value */
888   default_read_var_value,       /* la_read_var_value */
889   unk_lang_trampoline,          /* Language specific skip_trampoline */
890   "this",                       /* name_of_this */
891   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
892   basic_lookup_transparent_type,/* lookup_transparent_type */
893   unk_lang_demangle,            /* Language specific symbol demangler */
894   unk_lang_class_name,          /* Language specific
895                                    class_name_from_physname */
896   unk_op_print_tab,             /* expression operators for printing */
897   1,                            /* c-style arrays */
898   0,                            /* String lower bound */
899   default_word_break_characters,
900   default_make_symbol_completion_list,
901   unknown_language_arch_info,   /* la_language_arch_info.  */
902   default_print_array_index,
903   default_pass_by_reference,
904   default_get_string,
905   NULL,                         /* la_get_symbol_name_cmp */
906   iterate_over_symbols,
907   &default_varobj_ops,
908   NULL,
909   NULL,
910   LANG_MAGIC
911 };
912
913 const struct language_defn local_language_defn =
914 {
915   "local",
916   "Local",
917   language_auto,
918   range_check_off,
919   case_sensitive_on,
920   array_row_major,
921   macro_expansion_no,
922   NULL,
923   &exp_descriptor_standard,
924   unk_lang_parser,
925   unk_lang_error,
926   null_post_parser,
927   unk_lang_printchar,           /* Print character constant */
928   unk_lang_printstr,
929   unk_lang_emit_char,
930   unk_lang_print_type,          /* Print a type using appropriate syntax */
931   default_print_typedef,        /* Print a typedef using appropriate syntax */
932   unk_lang_val_print,           /* Print a value using appropriate syntax */
933   unk_lang_value_print,         /* Print a top-level value */
934   default_read_var_value,       /* la_read_var_value */
935   unk_lang_trampoline,          /* Language specific skip_trampoline */
936   "this",                       /* name_of_this */
937   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
938   basic_lookup_transparent_type,/* lookup_transparent_type */
939   unk_lang_demangle,            /* Language specific symbol demangler */
940   unk_lang_class_name,          /* Language specific
941                                    class_name_from_physname */
942   unk_op_print_tab,             /* expression operators for printing */
943   1,                            /* c-style arrays */
944   0,                            /* String lower bound */
945   default_word_break_characters,
946   default_make_symbol_completion_list,
947   unknown_language_arch_info,   /* la_language_arch_info.  */
948   default_print_array_index,
949   default_pass_by_reference,
950   default_get_string,
951   NULL,                         /* la_get_symbol_name_cmp */
952   iterate_over_symbols,
953   &default_varobj_ops,
954   NULL,
955   NULL,
956   LANG_MAGIC
957 };
958 \f
959 /* Per-architecture language information.  */
960
961 static struct gdbarch_data *language_gdbarch_data;
962
963 struct language_gdbarch
964 {
965   /* A vector of per-language per-architecture info.  Indexed by "enum
966      language".  */
967   struct language_arch_info arch_info[nr_languages];
968 };
969
970 static void *
971 language_gdbarch_post_init (struct gdbarch *gdbarch)
972 {
973   struct language_gdbarch *l;
974   int i;
975
976   l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
977   for (i = 0; i < languages_size; i++)
978     {
979       if (languages[i] != NULL
980           && languages[i]->la_language_arch_info != NULL)
981         languages[i]->la_language_arch_info
982           (gdbarch, l->arch_info + languages[i]->la_language);
983     }
984   return l;
985 }
986
987 struct type *
988 language_string_char_type (const struct language_defn *la,
989                            struct gdbarch *gdbarch)
990 {
991   struct language_gdbarch *ld
992     = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
993
994   return ld->arch_info[la->la_language].string_char_type;
995 }
996
997 struct type *
998 language_bool_type (const struct language_defn *la,
999                     struct gdbarch *gdbarch)
1000 {
1001   struct language_gdbarch *ld
1002     = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1003
1004   if (ld->arch_info[la->la_language].bool_type_symbol)
1005     {
1006       struct symbol *sym;
1007
1008       sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
1009                            NULL, VAR_DOMAIN, NULL).symbol;
1010       if (sym)
1011         {
1012           struct type *type = SYMBOL_TYPE (sym);
1013
1014           if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
1015             return type;
1016         }
1017     }
1018
1019   return ld->arch_info[la->la_language].bool_type_default;
1020 }
1021
1022 /* Helper function for primitive type lookup.  */
1023
1024 static struct type **
1025 language_lookup_primitive_type_1 (const struct language_arch_info *lai,
1026                                   const char *name)
1027 {
1028   struct type **p;
1029
1030   for (p = lai->primitive_type_vector; (*p) != NULL; p++)
1031     {
1032       if (strcmp (TYPE_NAME (*p), name) == 0)
1033         return p;
1034     }
1035   return NULL;
1036 }
1037
1038 /* See language.h.  */
1039
1040 struct type *
1041 language_lookup_primitive_type (const struct language_defn *la,
1042                                 struct gdbarch *gdbarch,
1043                                 const char *name)
1044 {
1045   struct language_gdbarch *ld =
1046     (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1047   struct type **typep;
1048
1049   typep = language_lookup_primitive_type_1 (&ld->arch_info[la->la_language],
1050                                             name);
1051   if (typep == NULL)
1052     return NULL;
1053   return *typep;
1054 }
1055
1056 /* Helper function for type lookup as a symbol.
1057    Create the symbol corresponding to type TYPE in language LANG.  */
1058
1059 static struct symbol *
1060 language_alloc_type_symbol (enum language lang, struct type *type)
1061 {
1062   struct symbol *symbol;
1063   struct gdbarch *gdbarch;
1064
1065   gdb_assert (!TYPE_OBJFILE_OWNED (type));
1066
1067   gdbarch = TYPE_OWNER (type).gdbarch;
1068   symbol = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct symbol);
1069
1070   symbol->ginfo.name = TYPE_NAME (type);
1071   symbol->ginfo.language = lang;
1072   symbol->owner.arch = gdbarch;
1073   SYMBOL_OBJFILE_OWNED (symbol) = 0;
1074   SYMBOL_TYPE (symbol) = type;
1075   SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
1076   SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
1077
1078   return symbol;
1079 }
1080
1081 /* Initialize the primitive type symbols of language LD.
1082    The primitive type vector must have already been initialized.  */
1083
1084 static void
1085 language_init_primitive_type_symbols (struct language_arch_info *lai,
1086                                       const struct language_defn *la,
1087                                       struct gdbarch *gdbarch)
1088 {
1089   int n;
1090
1091   gdb_assert (lai->primitive_type_vector != NULL);
1092
1093   for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1094     continue;
1095
1096   lai->primitive_type_symbols
1097     = GDBARCH_OBSTACK_CALLOC (gdbarch, n + 1, struct symbol *);
1098
1099   for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
1100     {
1101       lai->primitive_type_symbols[n]
1102         = language_alloc_type_symbol (la->la_language,
1103                                       lai->primitive_type_vector[n]);
1104     }
1105
1106   /* Note: The result of symbol lookup is normally a symbol *and* the block
1107      it was found in.  Builtin types don't live in blocks.  We *could* give
1108      them one, but there is no current need so to keep things simple symbol
1109      lookup is extended to allow for BLOCK_FOUND to be NULL.  */
1110 }
1111
1112 /* See language.h.  */
1113
1114 struct symbol *
1115 language_lookup_primitive_type_as_symbol (const struct language_defn *la,
1116                                           struct gdbarch *gdbarch,
1117                                           const char *name)
1118 {
1119   struct language_gdbarch *ld
1120     = (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
1121   struct language_arch_info *lai = &ld->arch_info[la->la_language];
1122   struct type **typep;
1123   struct symbol *sym;
1124
1125   if (symbol_lookup_debug)
1126     {
1127       fprintf_unfiltered (gdb_stdlog,
1128                           "language_lookup_primitive_type_as_symbol"
1129                           " (%s, %s, %s)",
1130                           la->la_name, host_address_to_string (gdbarch), name);
1131     }
1132
1133   typep = language_lookup_primitive_type_1 (lai, name);
1134   if (typep == NULL)
1135     {
1136       if (symbol_lookup_debug)
1137         fprintf_unfiltered (gdb_stdlog, " = NULL\n");
1138       return NULL;
1139     }
1140
1141   /* The set of symbols is lazily initialized.  */
1142   if (lai->primitive_type_symbols == NULL)
1143     language_init_primitive_type_symbols (lai, la, gdbarch);
1144
1145   sym = lai->primitive_type_symbols[typep - lai->primitive_type_vector];
1146
1147   if (symbol_lookup_debug)
1148     fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
1149   return sym;
1150 }
1151
1152 /* Initialize the language routines.  */
1153
1154 void
1155 _initialize_language (void)
1156 {
1157   static const char *const type_or_range_names[]
1158     = { "on", "off", "warn", "auto", NULL };
1159
1160   static const char *const case_sensitive_names[]
1161     = { "on", "off", "auto", NULL };
1162
1163   language_gdbarch_data
1164     = gdbarch_data_register_post_init (language_gdbarch_post_init);
1165
1166   /* GDB commands for language specific stuff.  */
1167
1168   add_prefix_cmd ("check", no_class, set_check,
1169                   _("Set the status of the type/range checker."),
1170                   &setchecklist, "set check ", 0, &setlist);
1171   add_alias_cmd ("c", "check", no_class, 1, &setlist);
1172   add_alias_cmd ("ch", "check", no_class, 1, &setlist);
1173
1174   add_prefix_cmd ("check", no_class, show_check,
1175                   _("Show the status of the type/range checker."),
1176                   &showchecklist, "show check ", 0, &showlist);
1177   add_alias_cmd ("c", "check", no_class, 1, &showlist);
1178   add_alias_cmd ("ch", "check", no_class, 1, &showlist);
1179
1180   add_setshow_enum_cmd ("range", class_support, type_or_range_names,
1181                         &range,
1182                         _("Set range checking.  (on/warn/off/auto)"),
1183                         _("Show range checking.  (on/warn/off/auto)"),
1184                         NULL, set_range_command,
1185                         show_range_command,
1186                         &setchecklist, &showchecklist);
1187
1188   add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
1189                         &case_sensitive, _("\
1190 Set case sensitivity in name search.  (on/off/auto)"), _("\
1191 Show case sensitivity in name search.  (on/off/auto)"), _("\
1192 For Fortran the default is off; for other languages the default is on."),
1193                         set_case_command,
1194                         show_case_command,
1195                         &setlist, &showlist);
1196
1197   add_language (&auto_language_defn);
1198   add_language (&local_language_defn);
1199   add_language (&unknown_language_defn);
1200
1201   language = xstrdup ("auto");
1202   type = xstrdup ("auto");
1203   range = xstrdup ("auto");
1204   case_sensitive = xstrdup ("auto");
1205
1206   /* Have the above take effect.  */
1207   set_language (language_auto);
1208 }