gdb/language.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Multiple source language support for GDB.

  2.    Copyright (C) 1991-2015 Free Software Foundation, Inc.

  3.    Contributed by the Department of Computer Science at the State University
  4.    of New York at Buffalo.

  5.    This file is part of GDB.

  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. /* This file contains functions that return things that are specific
  17.    to languages.  Each function should examine current_language if necessary,
  18.    and return the appropriate result.  */

  19. /* FIXME:  Most of these would be better organized as macros which
  20.    return data out of a "language-specific" struct pointer that is set
  21.    whenever the working language changes.  That would be a lot faster.  */

  22. #include "defs.h"
  23. #include <ctype.h>
  24. #include "symtab.h"
  25. #include "gdbtypes.h"
  26. #include "value.h"
  27. #include "gdbcmd.h"
  28. #include "expression.h"
  29. #include "language.h"
  30. #include "varobj.h"
  31. #include "target.h"
  32. #include "parser-defs.h"
  33. #include "jv-lang.h"
  34. #include "demangle.h"
  35. #include "symfile.h"
  36. #include "cp-support.h"

  37. extern void _initialize_language (void);

  38. static void unk_lang_error (char *);

  39. static int unk_lang_parser (struct parser_state *);

  40. static void show_check (char *, int);

  41. static void set_check (char *, int);

  42. static void set_range_case (void);

  43. static void unk_lang_emit_char (int c, struct type *type,
  44.                                 struct ui_file *stream, int quoter);

  45. static void unk_lang_printchar (int c, struct type *type,
  46.                                 struct ui_file *stream);

  47. static void unk_lang_value_print (struct value *, struct ui_file *,
  48.                                   const struct value_print_options *);

  49. static CORE_ADDR unk_lang_trampoline (struct frame_info *, CORE_ADDR pc);

  50. /* Forward declaration */
  51. extern const struct language_defn unknown_language_defn;

  52. /* The current (default at startup) state of type and range checking.
  53.    (If the modes are set to "auto", though, these are changed based
  54.    on the default language at startup, and then again based on the
  55.    language of the first source file.  */

  56. enum range_mode range_mode = range_mode_auto;
  57. enum range_check range_check = range_check_off;
  58. enum case_mode case_mode = case_mode_auto;
  59. enum case_sensitivity case_sensitivity = case_sensitive_on;

  60. /* The current language and language_mode (see language.h).  */

  61. const struct language_defn *current_language = &unknown_language_defn;
  62. enum language_mode language_mode = language_mode_auto;

  63. /* The language that the user expects to be typing in (the language
  64.    of main(), or the last language we notified them about, or C).  */

  65. const struct language_defn *expected_language;

  66. /* The list of supported languages.  The list itself is malloc'd.  */

  67. static const struct language_defn **languages;
  68. static unsigned languages_size;
  69. static unsigned languages_allocsize;
  70. #define        DEFAULT_ALLOCSIZE 4

  71. /* The current values of the "set language/type/range" enum
  72.    commands.  */
  73. static const char *language;
  74. static const char *type;
  75. static const char *range;
  76. static const char *case_sensitive;

  77. /* Warning issued when current_language and the language of the current
  78.    frame do not match.  */
  79. char lang_frame_mismatch_warn[] =
  80. "Warning: the current language does not match this frame.";

  81. /* This page contains the functions corresponding to GDB commands
  82.    and their helpers.  */

  83. /* Show command.  Display a warning if the language set
  84.    does not match the frame.  */
  85. static void
  86. show_language_command (struct ui_file *file, int from_tty,
  87.                        struct cmd_list_element *c, const char *value)
  88. {
  89.   enum language flang;                /* The language of the current frame.  */

  90.   if (language_mode == language_mode_auto)
  91.     fprintf_filtered (gdb_stdout,
  92.                       _("The current source language is "
  93.                         "\"auto; currently %s\".\n"),
  94.                       current_language->la_name);
  95.   else
  96.     fprintf_filtered (gdb_stdout,
  97.                       _("The current source language is \"%s\".\n"),
  98.                       current_language->la_name);

  99.   flang = get_frame_language ();
  100.   if (flang != language_unknown &&
  101.       language_mode == language_mode_manual &&
  102.       current_language->la_language != flang)
  103.     printf_filtered ("%s\n", lang_frame_mismatch_warn);
  104. }

  105. /* Set command.  Change the current working language.  */
  106. static void
  107. set_language_command (char *ignore, int from_tty, struct cmd_list_element *c)
  108. {
  109.   int i;
  110.   enum language flang;

  111.   /* Search the list of languages for a match.  */
  112.   for (i = 0; i < languages_size; i++)
  113.     {
  114.       if (strcmp (languages[i]->la_name, language) == 0)
  115.         {
  116.           /* Found it!  Go into manual mode, and use this language.  */
  117.           if (languages[i]->la_language == language_auto)
  118.             {
  119.               /* Enter auto mode.  Set to the current frame's language, if
  120.                  known, or fallback to the initial language.  */
  121.               language_mode = language_mode_auto;
  122.               flang = get_frame_language ();
  123.               if (flang != language_unknown)
  124.                 set_language (flang);
  125.               else
  126.                 set_initial_language ();
  127.               expected_language = current_language;
  128.               return;
  129.             }
  130.           else
  131.             {
  132.               /* Enter manual mode.  Set the specified language.  */
  133.               language_mode = language_mode_manual;
  134.               current_language = languages[i];
  135.               set_range_case ();
  136.               expected_language = current_language;
  137.               return;
  138.             }
  139.         }
  140.     }

  141.   internal_error (__FILE__, __LINE__,
  142.                   "Couldn't find language `%s' in known languages list.",
  143.                   language);
  144. }

  145. /* Show command.  Display a warning if the range setting does
  146.    not match the current language.  */
  147. static void
  148. show_range_command (struct ui_file *file, int from_tty,
  149.                     struct cmd_list_element *c, const char *value)
  150. {
  151.   if (range_mode == range_mode_auto)
  152.     {
  153.       char *tmp;

  154.       switch (range_check)
  155.         {
  156.         case range_check_on:
  157.           tmp = "on";
  158.           break;
  159.         case range_check_off:
  160.           tmp = "off";
  161.           break;
  162.         case range_check_warn:
  163.           tmp = "warn";
  164.           break;
  165.         default:
  166.           internal_error (__FILE__, __LINE__,
  167.                           "Unrecognized range check setting.");
  168.         }

  169.       fprintf_filtered (gdb_stdout,
  170.                         _("Range checking is \"auto; currently %s\".\n"),
  171.                         tmp);
  172.     }
  173.   else
  174.     fprintf_filtered (gdb_stdout, _("Range checking is \"%s\".\n"),
  175.                       value);

  176.   if (range_check != current_language->la_range_check)
  177.     warning (_("the current range check setting "
  178.                "does not match the language.\n"));
  179. }

  180. /* Set command.  Change the setting for range checking.  */
  181. static void
  182. set_range_command (char *ignore, int from_tty, struct cmd_list_element *c)
  183. {
  184.   if (strcmp (range, "on") == 0)
  185.     {
  186.       range_check = range_check_on;
  187.       range_mode = range_mode_manual;
  188.     }
  189.   else if (strcmp (range, "warn") == 0)
  190.     {
  191.       range_check = range_check_warn;
  192.       range_mode = range_mode_manual;
  193.     }
  194.   else if (strcmp (range, "off") == 0)
  195.     {
  196.       range_check = range_check_off;
  197.       range_mode = range_mode_manual;
  198.     }
  199.   else if (strcmp (range, "auto") == 0)
  200.     {
  201.       range_mode = range_mode_auto;
  202.       set_range_case ();
  203.       return;
  204.     }
  205.   else
  206.     {
  207.       internal_error (__FILE__, __LINE__,
  208.                       _("Unrecognized range check setting: \"%s\""), range);
  209.     }
  210.   if (range_check != current_language->la_range_check)
  211.     warning (_("the current range check setting "
  212.                "does not match the language.\n"));
  213. }

  214. /* Show command.  Display a warning if the case sensitivity setting does
  215.    not match the current language.  */
  216. static void
  217. show_case_command (struct ui_file *file, int from_tty,
  218.                    struct cmd_list_element *c, const char *value)
  219. {
  220.   if (case_mode == case_mode_auto)
  221.     {
  222.       char *tmp = NULL;

  223.       switch (case_sensitivity)
  224.         {
  225.         case case_sensitive_on:
  226.           tmp = "on";
  227.           break;
  228.         case case_sensitive_off:
  229.           tmp = "off";
  230.           break;
  231.         default:
  232.           internal_error (__FILE__, __LINE__,
  233.                           "Unrecognized case-sensitive setting.");
  234.         }

  235.       fprintf_filtered (gdb_stdout,
  236.                         _("Case sensitivity in "
  237.                           "name search is \"auto; currently %s\".\n"),
  238.                         tmp);
  239.     }
  240.   else
  241.     fprintf_filtered (gdb_stdout,
  242.                       _("Case sensitivity in name search is \"%s\".\n"),
  243.                       value);

  244.   if (case_sensitivity != current_language->la_case_sensitivity)
  245.     warning (_("the current case sensitivity setting does not match "
  246.                "the language.\n"));
  247. }

  248. /* Set command.  Change the setting for case sensitivity.  */

  249. static void
  250. set_case_command (char *ignore, int from_tty, struct cmd_list_element *c)
  251. {
  252.    if (strcmp (case_sensitive, "on") == 0)
  253.      {
  254.        case_sensitivity = case_sensitive_on;
  255.        case_mode = case_mode_manual;
  256.      }
  257.    else if (strcmp (case_sensitive, "off") == 0)
  258.      {
  259.        case_sensitivity = case_sensitive_off;
  260.        case_mode = case_mode_manual;
  261.      }
  262.    else if (strcmp (case_sensitive, "auto") == 0)
  263.      {
  264.        case_mode = case_mode_auto;
  265.        set_range_case ();
  266.        return;
  267.      }
  268.    else
  269.      {
  270.        internal_error (__FILE__, __LINE__,
  271.                        "Unrecognized case-sensitive setting: \"%s\"",
  272.                        case_sensitive);
  273.      }

  274.    if (case_sensitivity != current_language->la_case_sensitivity)
  275.      warning (_("the current case sensitivity setting does not match "
  276.                 "the language.\n"));
  277. }

  278. /* Set the status of range and type checking and case sensitivity based on
  279.    the current modes and the current language.
  280.    If SHOW is non-zero, then print out the current language,
  281.    type and range checking status.  */
  282. static void
  283. set_range_case (void)
  284. {
  285.   if (range_mode == range_mode_auto)
  286.     range_check = current_language->la_range_check;

  287.   if (case_mode == case_mode_auto)
  288.     case_sensitivity = current_language->la_case_sensitivity;
  289. }

  290. /* Set current language to (enum language) LANG.  Returns previous
  291.    language.  */

  292. enum language
  293. set_language (enum language lang)
  294. {
  295.   int i;
  296.   enum language prev_language;

  297.   prev_language = current_language->la_language;

  298.   for (i = 0; i < languages_size; i++)
  299.     {
  300.       if (languages[i]->la_language == lang)
  301.         {
  302.           current_language = languages[i];
  303.           set_range_case ();
  304.           break;
  305.         }
  306.     }

  307.   return prev_language;
  308. }


  309. /* Print out the current language settings: language, range and
  310.    type checking.  If QUIETLY, print only what has changed.  */

  311. void
  312. language_info (int quietly)
  313. {
  314.   if (quietly && expected_language == current_language)
  315.     return;

  316.   expected_language = current_language;
  317.   printf_unfiltered (_("Current language%s\n"), language);
  318.   show_language_command (NULL, 1, NULL, NULL);

  319.   if (!quietly)
  320.     {
  321.       printf_unfiltered (_("Range checking:    %s\n"), range);
  322.       show_range_command (NULL, 1, NULL, NULL);
  323.       printf_unfiltered (_("Case sensitivity:  %s\n"), case_sensitive);
  324.       show_case_command (NULL, 1, NULL, NULL);
  325.     }
  326. }


  327. /* Returns non-zero if the value is a pointer type.  */
  328. int
  329. pointer_type (struct type *type)
  330. {
  331.   return TYPE_CODE (type) == TYPE_CODE_PTR ||
  332.     TYPE_CODE (type) == TYPE_CODE_REF;
  333. }


  334. /* This page contains functions that return info about
  335.    (struct value) values used in GDB.  */

  336. /* Returns non-zero if the value VAL represents a true value.  */
  337. int
  338. value_true (struct value *val)
  339. {
  340.   /* It is possible that we should have some sort of error if a non-boolean
  341.      value is used in this context.  Possibly dependent on some kind of
  342.      "boolean-checking" option like range checking.  But it should probably
  343.      not depend on the language except insofar as is necessary to identify
  344.      a "boolean" value (i.e. in C using a float, pointer, etc., as a boolean
  345.      should be an error, probably).  */
  346.   return !value_logical_not (val);
  347. }

  348. /* This page contains functions for the printing out of
  349.    error messages that occur during type- and range-
  350.    checking.  */

  351. /* This is called when a language fails a range-check.  The
  352.    first argument should be a printf()-style format string, and the
  353.    rest of the arguments should be its arguments.  If range_check is
  354.    range_check_on, an error is printed;  if range_check_warn, a warning;
  355.    otherwise just the message.  */

  356. void
  357. range_error (const char *string,...)
  358. {
  359.   va_list args;

  360.   va_start (args, string);
  361.   switch (range_check)
  362.     {
  363.     case range_check_warn:
  364.       vwarning (string, args);
  365.       break;
  366.     case range_check_on:
  367.       verror (string, args);
  368.       break;
  369.     case range_check_off:
  370.       /* FIXME: cagney/2002-01-30: Should this function print anything
  371.          when range error is off?  */
  372.       vfprintf_filtered (gdb_stderr, string, args);
  373.       fprintf_filtered (gdb_stderr, "\n");
  374.       break;
  375.     default:
  376.       internal_error (__FILE__, __LINE__, _("bad switch"));
  377.     }
  378.   va_end (args);
  379. }


  380. /* This page contains miscellaneous functions.  */

  381. /* Return the language enum for a given language string.  */

  382. enum language
  383. language_enum (char *str)
  384. {
  385.   int i;

  386.   for (i = 0; i < languages_size; i++)
  387.     if (strcmp (languages[i]->la_name, str) == 0)
  388.       return languages[i]->la_language;

  389.   return language_unknown;
  390. }

  391. /* Return the language struct for a given language enum.  */

  392. const struct language_defn *
  393. language_def (enum language lang)
  394. {
  395.   int i;

  396.   for (i = 0; i < languages_size; i++)
  397.     {
  398.       if (languages[i]->la_language == lang)
  399.         {
  400.           return languages[i];
  401.         }
  402.     }
  403.   return NULL;
  404. }

  405. /* Return the language as a string.  */
  406. const char *
  407. language_str (enum language lang)
  408. {
  409.   int i;

  410.   for (i = 0; i < languages_size; i++)
  411.     {
  412.       if (languages[i]->la_language == lang)
  413.         {
  414.           return languages[i]->la_name;
  415.         }
  416.     }
  417.   return "Unknown";
  418. }

  419. static void
  420. set_check (char *ignore, int from_tty)
  421. {
  422.   printf_unfiltered (
  423.      "\"set check\" must be followed by the name of a check subcommand.\n");
  424.   help_list (setchecklist, "set check ", all_commands, gdb_stdout);
  425. }

  426. static void
  427. show_check (char *ignore, int from_tty)
  428. {
  429.   cmd_show_list (showchecklist, from_tty, "");
  430. }

  431. /* Add a language to the set of known languages.  */

  432. void
  433. add_language (const struct language_defn *lang)
  434. {
  435.   /* For the "set language" command.  */
  436.   static const char **language_names = NULL;
  437.   /* For the "help set language" command.  */
  438.   char *language_set_doc = NULL;

  439.   int i;
  440.   struct ui_file *tmp_stream;

  441.   if (lang->la_magic != LANG_MAGIC)
  442.     {
  443.       fprintf_unfiltered (gdb_stderr,
  444.                           "Magic number of %s language struct wrong\n",
  445.                           lang->la_name);
  446.       internal_error (__FILE__, __LINE__,
  447.                       _("failed internal consistency check"));
  448.     }

  449.   if (!languages)
  450.     {
  451.       languages_allocsize = DEFAULT_ALLOCSIZE;
  452.       languages = (const struct language_defn **) xmalloc
  453.         (languages_allocsize * sizeof (*languages));
  454.     }
  455.   if (languages_size >= languages_allocsize)
  456.     {
  457.       languages_allocsize *= 2;
  458.       languages = (const struct language_defn **) xrealloc ((char *) languages,
  459.                                  languages_allocsize * sizeof (*languages));
  460.     }
  461.   languages[languages_size++] = lang;

  462.   /* Build the language names array, to be used as enumeration in the
  463.      set language" enum command.  */
  464.   language_names = xrealloc (language_names,
  465.                              (languages_size + 1) * sizeof (const char *));
  466.   for (i = 0; i < languages_size; ++i)
  467.     language_names[i] = languages[i]->la_name;
  468.   language_names[i] = NULL;

  469.   /* Build the "help set language" docs.  */
  470.   tmp_stream = mem_fileopen ();

  471.   fprintf_unfiltered (tmp_stream,
  472.                       _("Set the current source language.\n"
  473.                         "The currently understood settings are:\n\nlocal or "
  474.                         "auto    Automatic setting based on source file\n"));

  475.   for (i = 0; i < languages_size; ++i)
  476.     {
  477.       /* Already dealt with these above.  */
  478.       if (languages[i]->la_language == language_unknown
  479.           || languages[i]->la_language == language_auto)
  480.         continue;

  481.       /* FIXME: i18n: for now assume that the human-readable name
  482.          is just a capitalization of the internal name.  */
  483.       fprintf_unfiltered (tmp_stream, "%-16s Use the %c%s language\n",
  484.                           languages[i]->la_name,
  485.                           /* Capitalize first letter of language
  486.                              name.  */
  487.                           toupper (languages[i]->la_name[0]),
  488.                           languages[i]->la_name + 1);
  489.     }

  490.   language_set_doc = ui_file_xstrdup (tmp_stream, NULL);
  491.   ui_file_delete (tmp_stream);

  492.   add_setshow_enum_cmd ("language", class_support,
  493.                         (const char **) language_names,
  494.                         &language,
  495.                         language_set_doc,
  496.                         _("Show the current source language."),
  497.                         NULL, set_language_command,
  498.                         show_language_command,
  499.                         &setlist, &showlist);

  500.   xfree (language_set_doc);
  501. }

  502. /* Iterate through all registered languages looking for and calling
  503.    any non-NULL struct language_defn.skip_trampoline() functions.
  504.    Return the result from the first that returns non-zero, or 0 if all
  505.    `fail'.  */
  506. CORE_ADDR
  507. skip_language_trampoline (struct frame_info *frame, CORE_ADDR pc)
  508. {
  509.   int i;

  510.   for (i = 0; i < languages_size; i++)
  511.     {
  512.       if (languages[i]->skip_trampoline)
  513.         {
  514.           CORE_ADDR real_pc = (languages[i]->skip_trampoline) (frame, pc);

  515.           if (real_pc)
  516.             return real_pc;
  517.         }
  518.     }

  519.   return 0;
  520. }

  521. /* Return demangled language symbol, or NULL.
  522.    FIXME: Options are only useful for certain languages and ignored
  523.    by others, so it would be better to remove them here and have a
  524.    more flexible demangler for the languages that need it.
  525.    FIXME: Sometimes the demangler is invoked when we don't know the
  526.    language, so we can't use this everywhere.  */
  527. char *
  528. language_demangle (const struct language_defn *current_language,
  529.                                 const char *mangled, int options)
  530. {
  531.   if (current_language != NULL && current_language->la_demangle)
  532.     return current_language->la_demangle (mangled, options);
  533.   return NULL;
  534. }

  535. /* Return class name from physname or NULL.  */
  536. char *
  537. language_class_name_from_physname (const struct language_defn *lang,
  538.                                    const char *physname)
  539. {
  540.   if (lang != NULL && lang->la_class_name_from_physname)
  541.     return lang->la_class_name_from_physname (physname);
  542.   return NULL;
  543. }

  544. /* Return non-zero if TYPE should be passed (and returned) by
  545.    reference at the language level.  */
  546. int
  547. language_pass_by_reference (struct type *type)
  548. {
  549.   return current_language->la_pass_by_reference (type);
  550. }

  551. /* Return zero; by default, types are passed by value at the language
  552.    level.  The target ABI may pass or return some structs by reference
  553.    independent of this.  */
  554. int
  555. default_pass_by_reference (struct type *type)
  556. {
  557.   return 0;
  558. }

  559. /* Return the default string containing the list of characters
  560.    delimiting words.  This is a reasonable default value that
  561.    most languages should be able to use.  */

  562. char *
  563. default_word_break_characters (void)
  564. {
  565.   return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
  566. }

  567. /* Print the index of array elements using the C99 syntax.  */

  568. void
  569. default_print_array_index (struct value *index_value, struct ui_file *stream,
  570.                            const struct value_print_options *options)
  571. {
  572.   fprintf_filtered (stream, "[");
  573.   LA_VALUE_PRINT (index_value, stream, options);
  574.   fprintf_filtered (stream, "] = ");
  575. }

  576. void
  577. default_get_string (struct value *value, gdb_byte **buffer, int *length,
  578.                     struct type **char_type, const char **charset)
  579. {
  580.   error (_("Getting a string is unsupported in this language."));
  581. }

  582. /* Define the language that is no language.  */

  583. static int
  584. unk_lang_parser (struct parser_state *ps)
  585. {
  586.   return 1;
  587. }

  588. static void
  589. unk_lang_error (char *msg)
  590. {
  591.   error (_("Attempted to parse an expression with unknown language"));
  592. }

  593. static void
  594. unk_lang_emit_char (int c, struct type *type, struct ui_file *stream,
  595.                     int quoter)
  596. {
  597.   error (_("internal error - unimplemented "
  598.            "function unk_lang_emit_char called."));
  599. }

  600. static void
  601. unk_lang_printchar (int c, struct type *type, struct ui_file *stream)
  602. {
  603.   error (_("internal error - unimplemented "
  604.            "function unk_lang_printchar called."));
  605. }

  606. static void
  607. unk_lang_printstr (struct ui_file *stream, struct type *type,
  608.                    const gdb_byte *string, unsigned int length,
  609.                    const char *encoding, int force_ellipses,
  610.                    const struct value_print_options *options)
  611. {
  612.   error (_("internal error - unimplemented "
  613.            "function unk_lang_printstr called."));
  614. }

  615. static void
  616. unk_lang_print_type (struct type *type, const char *varstring,
  617.                      struct ui_file *stream, int show, int level,
  618.                      const struct type_print_options *flags)
  619. {
  620.   error (_("internal error - unimplemented "
  621.            "function unk_lang_print_type called."));
  622. }

  623. static void
  624. unk_lang_val_print (struct type *type, const gdb_byte *valaddr,
  625.                     int embedded_offset, CORE_ADDR address,
  626.                     struct ui_file *stream, int recurse,
  627.                     const struct value *val,
  628.                     const struct value_print_options *options)
  629. {
  630.   error (_("internal error - unimplemented "
  631.            "function unk_lang_val_print called."));
  632. }

  633. static void
  634. unk_lang_value_print (struct value *val, struct ui_file *stream,
  635.                       const struct value_print_options *options)
  636. {
  637.   error (_("internal error - unimplemented "
  638.            "function unk_lang_value_print called."));
  639. }

  640. static CORE_ADDR unk_lang_trampoline (struct frame_info *frame, CORE_ADDR pc)
  641. {
  642.   return 0;
  643. }

  644. /* Unknown languages just use the cplus demangler.  */
  645. static char *unk_lang_demangle (const char *mangled, int options)
  646. {
  647.   return gdb_demangle (mangled, options);
  648. }

  649. static char *unk_lang_class_name (const char *mangled)
  650. {
  651.   return NULL;
  652. }

  653. static const struct op_print unk_op_print_tab[] =
  654. {
  655.   {NULL, OP_NULL, PREC_NULL, 0}
  656. };

  657. static void
  658. unknown_language_arch_info (struct gdbarch *gdbarch,
  659.                             struct language_arch_info *lai)
  660. {
  661.   lai->string_char_type = builtin_type (gdbarch)->builtin_char;
  662.   lai->bool_type_default = builtin_type (gdbarch)->builtin_int;
  663.   lai->primitive_type_vector = GDBARCH_OBSTACK_CALLOC (gdbarch, 1,
  664.                                                        struct type *);
  665. }

  666. const struct language_defn unknown_language_defn =
  667. {
  668.   "unknown",
  669.   "Unknown",
  670.   language_unknown,
  671.   range_check_off,
  672.   case_sensitive_on,
  673.   array_row_major,
  674.   macro_expansion_no,
  675.   &exp_descriptor_standard,
  676.   unk_lang_parser,
  677.   unk_lang_error,
  678.   null_post_parser,
  679.   unk_lang_printchar,                /* Print character constant */
  680.   unk_lang_printstr,
  681.   unk_lang_emit_char,
  682.   unk_lang_print_type,                /* Print a type using appropriate syntax */
  683.   default_print_typedef,        /* Print a typedef using appropriate syntax */
  684.   unk_lang_val_print,                /* Print a value using appropriate syntax */
  685.   unk_lang_value_print,                /* Print a top-level value */
  686.   default_read_var_value,        /* la_read_var_value */
  687.   unk_lang_trampoline,                /* Language specific skip_trampoline */
  688.   "this",                            /* name_of_this */
  689.   basic_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
  690.   basic_lookup_transparent_type,/* lookup_transparent_type */
  691.   unk_lang_demangle,                /* Language specific symbol demangler */
  692.   unk_lang_class_name,                /* Language specific
  693.                                    class_name_from_physname */
  694.   unk_op_print_tab,                /* expression operators for printing */
  695.   1,                                /* c-style arrays */
  696.   0,                                /* String lower bound */
  697.   default_word_break_characters,
  698.   default_make_symbol_completion_list,
  699.   unknown_language_arch_info,        /* la_language_arch_info.  */
  700.   default_print_array_index,
  701.   default_pass_by_reference,
  702.   default_get_string,
  703.   NULL,                                /* la_get_symbol_name_cmp */
  704.   iterate_over_symbols,
  705.   &default_varobj_ops,
  706.   NULL,
  707.   NULL,
  708.   LANG_MAGIC
  709. };

  710. /* These two structs define fake entries for the "local" and "auto"
  711.    options.  */
  712. const struct language_defn auto_language_defn =
  713. {
  714.   "auto",
  715.   "Auto",
  716.   language_auto,
  717.   range_check_off,
  718.   case_sensitive_on,
  719.   array_row_major,
  720.   macro_expansion_no,
  721.   &exp_descriptor_standard,
  722.   unk_lang_parser,
  723.   unk_lang_error,
  724.   null_post_parser,
  725.   unk_lang_printchar,                /* Print character constant */
  726.   unk_lang_printstr,
  727.   unk_lang_emit_char,
  728.   unk_lang_print_type,                /* Print a type using appropriate syntax */
  729.   default_print_typedef,        /* Print a typedef using appropriate syntax */
  730.   unk_lang_val_print,                /* Print a value using appropriate syntax */
  731.   unk_lang_value_print,                /* Print a top-level value */
  732.   default_read_var_value,        /* la_read_var_value */
  733.   unk_lang_trampoline,                /* Language specific skip_trampoline */
  734.   "this",                        /* name_of_this */
  735.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  736.   basic_lookup_transparent_type,/* lookup_transparent_type */
  737.   unk_lang_demangle,                /* Language specific symbol demangler */
  738.   unk_lang_class_name,                /* Language specific
  739.                                    class_name_from_physname */
  740.   unk_op_print_tab,                /* expression operators for printing */
  741.   1,                                /* c-style arrays */
  742.   0,                                /* String lower bound */
  743.   default_word_break_characters,
  744.   default_make_symbol_completion_list,
  745.   unknown_language_arch_info,        /* la_language_arch_info.  */
  746.   default_print_array_index,
  747.   default_pass_by_reference,
  748.   default_get_string,
  749.   NULL,                                /* la_get_symbol_name_cmp */
  750.   iterate_over_symbols,
  751.   &default_varobj_ops,
  752.   NULL,
  753.   NULL,
  754.   LANG_MAGIC
  755. };

  756. const struct language_defn local_language_defn =
  757. {
  758.   "local",
  759.   "Local",
  760.   language_auto,
  761.   range_check_off,
  762.   case_sensitive_on,
  763.   array_row_major,
  764.   macro_expansion_no,
  765.   &exp_descriptor_standard,
  766.   unk_lang_parser,
  767.   unk_lang_error,
  768.   null_post_parser,
  769.   unk_lang_printchar,                /* Print character constant */
  770.   unk_lang_printstr,
  771.   unk_lang_emit_char,
  772.   unk_lang_print_type,                /* Print a type using appropriate syntax */
  773.   default_print_typedef,        /* Print a typedef using appropriate syntax */
  774.   unk_lang_val_print,                /* Print a value using appropriate syntax */
  775.   unk_lang_value_print,                /* Print a top-level value */
  776.   default_read_var_value,        /* la_read_var_value */
  777.   unk_lang_trampoline,                /* Language specific skip_trampoline */
  778.   "this",                         /* name_of_this */
  779.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  780.   basic_lookup_transparent_type,/* lookup_transparent_type */
  781.   unk_lang_demangle,                /* Language specific symbol demangler */
  782.   unk_lang_class_name,                /* Language specific
  783.                                    class_name_from_physname */
  784.   unk_op_print_tab,                /* expression operators for printing */
  785.   1,                                /* c-style arrays */
  786.   0,                                /* String lower bound */
  787.   default_word_break_characters,
  788.   default_make_symbol_completion_list,
  789.   unknown_language_arch_info,        /* la_language_arch_info.  */
  790.   default_print_array_index,
  791.   default_pass_by_reference,
  792.   default_get_string,
  793.   NULL,                                /* la_get_symbol_name_cmp */
  794.   iterate_over_symbols,
  795.   &default_varobj_ops,
  796.   NULL,
  797.   NULL,
  798.   LANG_MAGIC
  799. };

  800. /* Per-architecture language information.  */

  801. static struct gdbarch_data *language_gdbarch_data;

  802. struct language_gdbarch
  803. {
  804.   /* A vector of per-language per-architecture info.  Indexed by "enum
  805.      language".  */
  806.   struct language_arch_info arch_info[nr_languages];
  807. };

  808. static void *
  809. language_gdbarch_post_init (struct gdbarch *gdbarch)
  810. {
  811.   struct language_gdbarch *l;
  812.   int i;

  813.   l = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct language_gdbarch);
  814.   for (i = 0; i < languages_size; i++)
  815.     {
  816.       if (languages[i] != NULL
  817.           && languages[i]->la_language_arch_info != NULL)
  818.         languages[i]->la_language_arch_info
  819.           (gdbarch, l->arch_info + languages[i]->la_language);
  820.     }
  821.   return l;
  822. }

  823. struct type *
  824. language_string_char_type (const struct language_defn *la,
  825.                            struct gdbarch *gdbarch)
  826. {
  827.   struct language_gdbarch *ld = gdbarch_data (gdbarch,
  828.                                               language_gdbarch_data);

  829.   return ld->arch_info[la->la_language].string_char_type;
  830. }

  831. struct type *
  832. language_bool_type (const struct language_defn *la,
  833.                     struct gdbarch *gdbarch)
  834. {
  835.   struct language_gdbarch *ld = gdbarch_data (gdbarch,
  836.                                               language_gdbarch_data);

  837.   if (ld->arch_info[la->la_language].bool_type_symbol)
  838.     {
  839.       struct symbol *sym;

  840.       sym = lookup_symbol (ld->arch_info[la->la_language].bool_type_symbol,
  841.                            NULL, VAR_DOMAIN, NULL);
  842.       if (sym)
  843.         {
  844.           struct type *type = SYMBOL_TYPE (sym);

  845.           if (type && TYPE_CODE (type) == TYPE_CODE_BOOL)
  846.             return type;
  847.         }
  848.     }

  849.   return ld->arch_info[la->la_language].bool_type_default;
  850. }

  851. /* Helper function for primitive type lookup.  */

  852. static struct type **
  853. language_lookup_primitive_type_1 (const struct language_arch_info *lai,
  854.                                   const char *name)
  855. {
  856.   struct type **p;

  857.   for (p = lai->primitive_type_vector; (*p) != NULL; p++)
  858.     {
  859.       if (strcmp (TYPE_NAME (*p), name) == 0)
  860.         return p;
  861.     }
  862.   return NULL;
  863. }

  864. /* See language.h.  */

  865. struct type *
  866. language_lookup_primitive_type (const struct language_defn *la,
  867.                                 struct gdbarch *gdbarch,
  868.                                 const char *name)
  869. {
  870.   struct language_gdbarch *ld = gdbarch_data (gdbarch,
  871.                                               language_gdbarch_data);
  872.   struct type **typep;

  873.   typep = language_lookup_primitive_type_1 (&ld->arch_info[la->la_language],
  874.                                             name);
  875.   if (typep == NULL)
  876.     return NULL;
  877.   return *typep;
  878. }

  879. /* Helper function for type lookup as a symbol.
  880.    Create the symbol corresponding to type TYPE in language LANG.  */

  881. static struct symbol *
  882. language_alloc_type_symbol (enum language lang, struct type *type)
  883. {
  884.   struct symbol *symbol;
  885.   struct gdbarch *gdbarch;

  886.   gdb_assert (!TYPE_OBJFILE_OWNED (type));

  887.   gdbarch = TYPE_OWNER (type).gdbarch;
  888.   symbol = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct symbol);

  889.   symbol->ginfo.name = TYPE_NAME (type);
  890.   symbol->ginfo.language = lang;
  891.   symbol->owner.arch = gdbarch;
  892.   SYMBOL_OBJFILE_OWNED (symbol) = 0;
  893.   SYMBOL_TYPE (symbol) = type;
  894.   SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
  895.   SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;

  896.   return symbol;
  897. }

  898. /* Initialize the primitive type symbols of language LD.
  899.    The primitive type vector must have already been initialized.  */

  900. static void
  901. language_init_primitive_type_symbols (struct language_arch_info *lai,
  902.                                       const struct language_defn *la,
  903.                                       struct gdbarch *gdbarch)
  904. {
  905.   int n;
  906.   struct compunit_symtab *cust;
  907.   struct symtab *symtab;
  908.   struct block *static_block, *global_block;

  909.   gdb_assert (lai->primitive_type_vector != NULL);

  910.   for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
  911.     continue;

  912.   lai->primitive_type_symbols
  913.     = GDBARCH_OBSTACK_CALLOC (gdbarch, n + 1, struct symbol *);

  914.   for (n = 0; lai->primitive_type_vector[n] != NULL; ++n)
  915.     {
  916.       lai->primitive_type_symbols[n]
  917.         = language_alloc_type_symbol (la->la_language,
  918.                                       lai->primitive_type_vector[n]);
  919.     }

  920.   /* Note: The result of symbol lookup is normally a symbol *and* the block
  921.      it was found in (returned in global block_found).  Builtin types don't
  922.      live in blocks.  We *could* give them one, but there is no current need
  923.      so to keep things simple symbol lookup is extended to allow for
  924.      BLOCK_FOUND to be NULL.  */
  925. }

  926. /* See language.h.  */

  927. struct symbol *
  928. language_lookup_primitive_type_as_symbol (const struct language_defn *la,
  929.                                           struct gdbarch *gdbarch,
  930.                                           const char *name)
  931. {
  932.   struct language_gdbarch *ld = gdbarch_data (gdbarch,
  933.                                               language_gdbarch_data);
  934.   struct language_arch_info *lai = &ld->arch_info[la->la_language];
  935.   struct type **typep;
  936.   struct symbol *sym;

  937.   if (symbol_lookup_debug)
  938.     {
  939.       fprintf_unfiltered (gdb_stdlog,
  940.                           "language_lookup_primitive_type_as_symbol"
  941.                           " (%s, %s, %s)",
  942.                           la->la_name, host_address_to_string (gdbarch), name);
  943.     }

  944.   typep = language_lookup_primitive_type_1 (lai, name);
  945.   if (typep == NULL)
  946.     {
  947.       if (symbol_lookup_debug)
  948.         fprintf_unfiltered (gdb_stdlog, " = NULL\n");
  949.       return NULL;
  950.     }

  951.   /* The set of symbols is lazily initialized.  */
  952.   if (lai->primitive_type_symbols == NULL)
  953.     language_init_primitive_type_symbols (lai, la, gdbarch);

  954.   sym = lai->primitive_type_symbols[typep - lai->primitive_type_vector];

  955.   if (symbol_lookup_debug)
  956.     fprintf_unfiltered (gdb_stdlog, " = %s\n", host_address_to_string (sym));
  957.   return sym;
  958. }

  959. /* Initialize the language routines.  */

  960. void
  961. _initialize_language (void)
  962. {
  963.   static const char *const type_or_range_names[]
  964.     = { "on", "off", "warn", "auto", NULL };

  965.   static const char *const case_sensitive_names[]
  966.     = { "on", "off", "auto", NULL };

  967.   language_gdbarch_data
  968.     = gdbarch_data_register_post_init (language_gdbarch_post_init);

  969.   /* GDB commands for language specific stuff.  */

  970.   add_prefix_cmd ("check", no_class, set_check,
  971.                   _("Set the status of the type/range checker."),
  972.                   &setchecklist, "set check ", 0, &setlist);
  973.   add_alias_cmd ("c", "check", no_class, 1, &setlist);
  974.   add_alias_cmd ("ch", "check", no_class, 1, &setlist);

  975.   add_prefix_cmd ("check", no_class, show_check,
  976.                   _("Show the status of the type/range checker."),
  977.                   &showchecklist, "show check ", 0, &showlist);
  978.   add_alias_cmd ("c", "check", no_class, 1, &showlist);
  979.   add_alias_cmd ("ch", "check", no_class, 1, &showlist);

  980.   add_setshow_enum_cmd ("range", class_support, type_or_range_names,
  981.                         &range,
  982.                         _("Set range checking.  (on/warn/off/auto)"),
  983.                         _("Show range checking.  (on/warn/off/auto)"),
  984.                         NULL, set_range_command,
  985.                         show_range_command,
  986.                         &setchecklist, &showchecklist);

  987.   add_setshow_enum_cmd ("case-sensitive", class_support, case_sensitive_names,
  988.                         &case_sensitive, _("\
  989. Set case sensitivity in name search.  (on/off/auto)"), _("\
  990. Show case sensitivity in name search.  (on/off/auto)"), _("\
  991. For Fortran the default is off; for other languages the default is on."),
  992.                         set_case_command,
  993.                         show_case_command,
  994.                         &setlist, &showlist);

  995.   add_language (&auto_language_defn);
  996.   add_language (&local_language_defn);
  997.   add_language (&unknown_language_defn);

  998.   language = xstrdup ("auto");
  999.   type = xstrdup ("auto");
  1000.   range = xstrdup ("auto");
  1001.   case_sensitive = xstrdup ("auto");

  1002.   /* Have the above take effect.  */
  1003.   set_language (language_auto);
  1004. }