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