gdb/parse.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Parse expressions for GDB.

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

  3.    Modified from expread.y by the Department of Computer Science at the
  4.    State University of New York at Buffalo, 1991.

  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. /* Parse an expression from text in a string,
  17.    and return the result as a struct expression pointer.
  18.    That structure contains arithmetic operations in reverse polish,
  19.    with constants represented by operations that are followed by special data.
  20.    See expression.h for the details of the format.
  21.    What is important here is that it can be built up sequentially
  22.    during the process of parsing; the lower levels of the tree always
  23.    come first in the result.  */

  24. #include "defs.h"
  25. #include <ctype.h>
  26. #include "arch-utils.h"
  27. #include "symtab.h"
  28. #include "gdbtypes.h"
  29. #include "frame.h"
  30. #include "expression.h"
  31. #include "value.h"
  32. #include "command.h"
  33. #include "language.h"
  34. #include "f-lang.h"
  35. #include "parser-defs.h"
  36. #include "gdbcmd.h"
  37. #include "symfile.h"                /* for overlay functions */
  38. #include "inferior.h"
  39. #include "doublest.h"
  40. #include "block.h"
  41. #include "source.h"
  42. #include "objfiles.h"
  43. #include "user-regs.h"

  44. /* Standard set of definitions for printing, dumping, prefixifying,
  45. * and evaluating expressions.  */

  46. const struct exp_descriptor exp_descriptor_standard =
  47.   {
  48.     print_subexp_standard,
  49.     operator_length_standard,
  50.     operator_check_standard,
  51.     op_name_standard,
  52.     dump_subexp_body_standard,
  53.     evaluate_subexp_standard
  54.   };

  55. /* Global variables declared in parser-defs.h (and commented there).  */
  56. const struct block *expression_context_block;
  57. CORE_ADDR expression_context_pc;
  58. const struct block *innermost_block;
  59. int arglist_len;
  60. static struct type_stack type_stack;
  61. const char *lexptr;
  62. const char *prev_lexptr;
  63. int paren_depth;
  64. int comma_terminates;

  65. /* True if parsing an expression to attempt completion.  */
  66. int parse_completion;

  67. /* The index of the last struct expression directly before a '.' or
  68.    '->'.  This is set when parsing and is only used when completing a
  69.    field name.  It is -1 if no dereference operation was found.  */
  70. static int expout_last_struct = -1;

  71. /* If we are completing a tagged type name, this will be nonzero.  */
  72. static enum type_code expout_tag_completion_type = TYPE_CODE_UNDEF;

  73. /* The token for tagged type name completion.  */
  74. static char *expout_completion_name;


  75. static unsigned int expressiondebug = 0;
  76. static void
  77. show_expressiondebug (struct ui_file *file, int from_tty,
  78.                       struct cmd_list_element *c, const char *value)
  79. {
  80.   fprintf_filtered (file, _("Expression debugging is %s.\n"), value);
  81. }


  82. /* Non-zero if an expression parser should set yydebug.  */
  83. int parser_debug;

  84. static void
  85. show_parserdebug (struct ui_file *file, int from_tty,
  86.                   struct cmd_list_element *c, const char *value)
  87. {
  88.   fprintf_filtered (file, _("Parser debugging is %s.\n"), value);
  89. }


  90. static void free_funcalls (void *ignore);

  91. static int prefixify_subexp (struct expression *, struct expression *, int,
  92.                              int);

  93. static struct expression *parse_exp_in_context (const char **, CORE_ADDR,
  94.                                                 const struct block *, int,
  95.                                                 int, int *);
  96. static struct expression *parse_exp_in_context_1 (const char **, CORE_ADDR,
  97.                                                   const struct block *, int,
  98.                                                   int, int *);

  99. void _initialize_parse (void);

  100. /* Data structure for saving values of arglist_len for function calls whose
  101.    arguments contain other function calls.  */

  102. struct funcall
  103.   {
  104.     struct funcall *next;
  105.     int arglist_len;
  106.   };

  107. static struct funcall *funcall_chain;

  108. /* Begin counting arguments for a function call,
  109.    saving the data about any containing call.  */

  110. void
  111. start_arglist (void)
  112. {
  113.   struct funcall *new;

  114.   new = (struct funcall *) xmalloc (sizeof (struct funcall));
  115.   new->next = funcall_chain;
  116.   new->arglist_len = arglist_len;
  117.   arglist_len = 0;
  118.   funcall_chain = new;
  119. }

  120. /* Return the number of arguments in a function call just terminated,
  121.    and restore the data for the containing function call.  */

  122. int
  123. end_arglist (void)
  124. {
  125.   int val = arglist_len;
  126.   struct funcall *call = funcall_chain;

  127.   funcall_chain = call->next;
  128.   arglist_len = call->arglist_len;
  129.   xfree (call);
  130.   return val;
  131. }

  132. /* Free everything in the funcall chain.
  133.    Used when there is an error inside parsing.  */

  134. static void
  135. free_funcalls (void *ignore)
  136. {
  137.   struct funcall *call, *next;

  138.   for (call = funcall_chain; call; call = next)
  139.     {
  140.       next = call->next;
  141.       xfree (call);
  142.     }
  143. }


  144. /* See definition in parser-defs.h.  */

  145. void
  146. initialize_expout (struct parser_state *ps, size_t initial_size,
  147.                    const struct language_defn *lang,
  148.                    struct gdbarch *gdbarch)
  149. {
  150.   ps->expout_size = initial_size;
  151.   ps->expout_ptr = 0;
  152.   ps->expout = xmalloc (sizeof (struct expression)
  153.                         + EXP_ELEM_TO_BYTES (ps->expout_size));
  154.   ps->expout->language_defn = lang;
  155.   ps->expout->gdbarch = gdbarch;
  156. }

  157. /* See definition in parser-defs.h.  */

  158. void
  159. reallocate_expout (struct parser_state *ps)
  160. {
  161.   /* Record the actual number of expression elements, and then
  162.      reallocate the expression memory so that we free up any
  163.      excess elements.  */

  164.   ps->expout->nelts = ps->expout_ptr;
  165.   ps->expout = (struct expression *)
  166.      xrealloc (ps->expout,
  167.                sizeof (struct expression)
  168.                + EXP_ELEM_TO_BYTES (ps->expout_ptr));
  169. }

  170. /* This page contains the functions for adding data to the struct expression
  171.    being constructed.  */

  172. /* Add one element to the end of the expression.  */

  173. /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
  174.    a register through here.  */

  175. static void
  176. write_exp_elt (struct parser_state *ps, const union exp_element *expelt)
  177. {
  178.   if (ps->expout_ptr >= ps->expout_size)
  179.     {
  180.       ps->expout_size *= 2;
  181.       ps->expout = (struct expression *)
  182.         xrealloc (ps->expout, sizeof (struct expression)
  183.                   + EXP_ELEM_TO_BYTES (ps->expout_size));
  184.     }
  185.   ps->expout->elts[ps->expout_ptr++] = *expelt;
  186. }

  187. void
  188. write_exp_elt_opcode (struct parser_state *ps, enum exp_opcode expelt)
  189. {
  190.   union exp_element tmp;

  191.   memset (&tmp, 0, sizeof (union exp_element));
  192.   tmp.opcode = expelt;
  193.   write_exp_elt (ps, &tmp);
  194. }

  195. void
  196. write_exp_elt_sym (struct parser_state *ps, struct symbol *expelt)
  197. {
  198.   union exp_element tmp;

  199.   memset (&tmp, 0, sizeof (union exp_element));
  200.   tmp.symbol = expelt;
  201.   write_exp_elt (ps, &tmp);
  202. }

  203. void
  204. write_exp_elt_block (struct parser_state *ps, const struct block *b)
  205. {
  206.   union exp_element tmp;

  207.   memset (&tmp, 0, sizeof (union exp_element));
  208.   tmp.block = b;
  209.   write_exp_elt (ps, &tmp);
  210. }

  211. void
  212. write_exp_elt_objfile (struct parser_state *ps, struct objfile *objfile)
  213. {
  214.   union exp_element tmp;

  215.   memset (&tmp, 0, sizeof (union exp_element));
  216.   tmp.objfile = objfile;
  217.   write_exp_elt (ps, &tmp);
  218. }

  219. void
  220. write_exp_elt_longcst (struct parser_state *ps, LONGEST expelt)
  221. {
  222.   union exp_element tmp;

  223.   memset (&tmp, 0, sizeof (union exp_element));
  224.   tmp.longconst = expelt;
  225.   write_exp_elt (ps, &tmp);
  226. }

  227. void
  228. write_exp_elt_dblcst (struct parser_state *ps, DOUBLEST expelt)
  229. {
  230.   union exp_element tmp;

  231.   memset (&tmp, 0, sizeof (union exp_element));
  232.   tmp.doubleconst = expelt;
  233.   write_exp_elt (ps, &tmp);
  234. }

  235. void
  236. write_exp_elt_decfloatcst (struct parser_state *ps, gdb_byte expelt[16])
  237. {
  238.   union exp_element tmp;
  239.   int index;

  240.   for (index = 0; index < 16; index++)
  241.     tmp.decfloatconst[index] = expelt[index];

  242.   write_exp_elt (ps, &tmp);
  243. }

  244. void
  245. write_exp_elt_type (struct parser_state *ps, struct type *expelt)
  246. {
  247.   union exp_element tmp;

  248.   memset (&tmp, 0, sizeof (union exp_element));
  249.   tmp.type = expelt;
  250.   write_exp_elt (ps, &tmp);
  251. }

  252. void
  253. write_exp_elt_intern (struct parser_state *ps, struct internalvar *expelt)
  254. {
  255.   union exp_element tmp;

  256.   memset (&tmp, 0, sizeof (union exp_element));
  257.   tmp.internalvar = expelt;
  258.   write_exp_elt (ps, &tmp);
  259. }

  260. /* Add a string constant to the end of the expression.

  261.    String constants are stored by first writing an expression element
  262.    that contains the length of the string, then stuffing the string
  263.    constant itself into however many expression elements are needed
  264.    to hold it, and then writing another expression element that contains
  265.    the length of the stringI.e. an expression element at each end of
  266.    the string records the string length, so you can skip over the
  267.    expression elements containing the actual string bytes from either
  268.    end of the string.  Note that this also allows gdb to handle
  269.    strings with embedded null bytes, as is required for some languages.

  270.    Don't be fooled by the fact that the string is null byte terminated,
  271.    this is strictly for the convenience of debugging gdb itself.
  272.    Gdb does not depend up the string being null terminated, since the
  273.    actual length is recorded in expression elements at each end of the
  274.    string.  The null byte is taken into consideration when computing how
  275.    many expression elements are required to hold the string constant, of
  276.    course.  */


  277. void
  278. write_exp_string (struct parser_state *ps, struct stoken str)
  279. {
  280.   int len = str.length;
  281.   size_t lenelt;
  282.   char *strdata;

  283.   /* Compute the number of expression elements required to hold the string
  284.      (including a null byte terminator), along with one expression element
  285.      at each end to record the actual string length (not including the
  286.      null byte terminator).  */

  287.   lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);

  288.   increase_expout_size (ps, lenelt);

  289.   /* Write the leading length expression element (which advances the current
  290.      expression element index), then write the string constant followed by a
  291.      terminating null byte, and then write the trailing length expression
  292.      element.  */

  293.   write_exp_elt_longcst (ps, (LONGEST) len);
  294.   strdata = (char *) &ps->expout->elts[ps->expout_ptr];
  295.   memcpy (strdata, str.ptr, len);
  296.   *(strdata + len) = '\0';
  297.   ps->expout_ptr += lenelt - 2;
  298.   write_exp_elt_longcst (ps, (LONGEST) len);
  299. }

  300. /* Add a vector of string constants to the end of the expression.

  301.    This adds an OP_STRING operation, but encodes the contents
  302.    differently from write_exp_string.  The language is expected to
  303.    handle evaluation of this expression itself.

  304.    After the usual OP_STRING header, TYPE is written into the
  305.    expression as a long constant.  The interpretation of this field is
  306.    up to the language evaluator.

  307.    Next, each string in VEC is written.  The length is written as a
  308.    long constant, followed by the contents of the string.  */

  309. void
  310. write_exp_string_vector (struct parser_state *ps, int type,
  311.                          struct stoken_vector *vec)
  312. {
  313.   int i, len;
  314.   size_t n_slots;

  315.   /* Compute the size.  We compute the size in number of slots to
  316.      avoid issues with string padding.  */
  317.   n_slots = 0;
  318.   for (i = 0; i < vec->len; ++i)
  319.     {
  320.       /* One slot for the length of this element, plus the number of
  321.          slots needed for this string.  */
  322.       n_slots += 1 + BYTES_TO_EXP_ELEM (vec->tokens[i].length);
  323.     }

  324.   /* One more slot for the type of the string.  */
  325.   ++n_slots;

  326.   /* Now compute a phony string length.  */
  327.   len = EXP_ELEM_TO_BYTES (n_slots) - 1;

  328.   n_slots += 4;
  329.   increase_expout_size (ps, n_slots);

  330.   write_exp_elt_opcode (ps, OP_STRING);
  331.   write_exp_elt_longcst (ps, len);
  332.   write_exp_elt_longcst (ps, type);

  333.   for (i = 0; i < vec->len; ++i)
  334.     {
  335.       write_exp_elt_longcst (ps, vec->tokens[i].length);
  336.       memcpy (&ps->expout->elts[ps->expout_ptr], vec->tokens[i].ptr,
  337.               vec->tokens[i].length);
  338.       ps->expout_ptr += BYTES_TO_EXP_ELEM (vec->tokens[i].length);
  339.     }

  340.   write_exp_elt_longcst (ps, len);
  341.   write_exp_elt_opcode (ps, OP_STRING);
  342. }

  343. /* Add a bitstring constant to the end of the expression.

  344.    Bitstring constants are stored by first writing an expression element
  345.    that contains the length of the bitstring (in bits), then stuffing the
  346.    bitstring constant itself into however many expression elements are
  347.    needed to hold it, and then writing another expression element that
  348.    contains the length of the bitstring.  I.e. an expression element at
  349.    each end of the bitstring records the bitstring length, so you can skip
  350.    over the expression elements containing the actual bitstring bytes from
  351.    either end of the bitstring.  */

  352. void
  353. write_exp_bitstring (struct parser_state *ps, struct stoken str)
  354. {
  355.   int bits = str.length;        /* length in bits */
  356.   int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
  357.   size_t lenelt;
  358.   char *strdata;

  359.   /* Compute the number of expression elements required to hold the bitstring,
  360.      along with one expression element at each end to record the actual
  361.      bitstring length in bits.  */

  362.   lenelt = 2 + BYTES_TO_EXP_ELEM (len);

  363.   increase_expout_size (ps, lenelt);

  364.   /* Write the leading length expression element (which advances the current
  365.      expression element index), then write the bitstring constant, and then
  366.      write the trailing length expression element.  */

  367.   write_exp_elt_longcst (ps, (LONGEST) bits);
  368.   strdata = (char *) &ps->expout->elts[ps->expout_ptr];
  369.   memcpy (strdata, str.ptr, len);
  370.   ps->expout_ptr += lenelt - 2;
  371.   write_exp_elt_longcst (ps, (LONGEST) bits);
  372. }

  373. /* Add the appropriate elements for a minimal symbol to the end of
  374.    the expression.  */

  375. void
  376. write_exp_msymbol (struct parser_state *ps,
  377.                    struct bound_minimal_symbol bound_msym)
  378. {
  379.   struct minimal_symbol *msymbol = bound_msym.minsym;
  380.   struct objfile *objfile = bound_msym.objfile;
  381.   struct gdbarch *gdbarch = get_objfile_arch (objfile);

  382.   CORE_ADDR addr = BMSYMBOL_VALUE_ADDRESS (bound_msym);
  383.   struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
  384.   enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
  385.   CORE_ADDR pc;

  386.   /* The minimal symbol might point to a function descriptor;
  387.      resolve it to the actual code address instead.  */
  388.   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
  389.   if (pc != addr)
  390.     {
  391.       struct bound_minimal_symbol ifunc_msym = lookup_minimal_symbol_by_pc (pc);

  392.       /* In this case, assume we have a code symbol instead of
  393.          a data symbol.  */

  394.       if (ifunc_msym.minsym != NULL
  395.           && MSYMBOL_TYPE (ifunc_msym.minsym) == mst_text_gnu_ifunc
  396.           && BMSYMBOL_VALUE_ADDRESS (ifunc_msym) == pc)
  397.         {
  398.           /* A function descriptor has been resolved but PC is still in the
  399.              STT_GNU_IFUNC resolver body (such as because inferior does not
  400.              run to be able to call it).  */

  401.           type = mst_text_gnu_ifunc;
  402.         }
  403.       else
  404.         type = mst_text;
  405.       section = NULL;
  406.       addr = pc;
  407.     }

  408.   if (overlay_debugging)
  409.     addr = symbol_overlayed_address (addr, section);

  410.   write_exp_elt_opcode (ps, OP_LONG);
  411.   /* Let's make the type big enough to hold a 64-bit address.  */
  412.   write_exp_elt_type (ps, objfile_type (objfile)->builtin_core_addr);
  413.   write_exp_elt_longcst (ps, (LONGEST) addr);
  414.   write_exp_elt_opcode (ps, OP_LONG);

  415.   if (section && section->the_bfd_section->flags & SEC_THREAD_LOCAL)
  416.     {
  417.       write_exp_elt_opcode (ps, UNOP_MEMVAL_TLS);
  418.       write_exp_elt_objfile (ps, objfile);
  419.       write_exp_elt_type (ps, objfile_type (objfile)->nodebug_tls_symbol);
  420.       write_exp_elt_opcode (ps, UNOP_MEMVAL_TLS);
  421.       return;
  422.     }

  423.   write_exp_elt_opcode (ps, UNOP_MEMVAL);
  424.   switch (type)
  425.     {
  426.     case mst_text:
  427.     case mst_file_text:
  428.     case mst_solib_trampoline:
  429.       write_exp_elt_type (ps, objfile_type (objfile)->nodebug_text_symbol);
  430.       break;

  431.     case mst_text_gnu_ifunc:
  432.       write_exp_elt_type (ps, objfile_type (objfile)
  433.                           ->nodebug_text_gnu_ifunc_symbol);
  434.       break;

  435.     case mst_data:
  436.     case mst_file_data:
  437.     case mst_bss:
  438.     case mst_file_bss:
  439.       write_exp_elt_type (ps, objfile_type (objfile)->nodebug_data_symbol);
  440.       break;

  441.     case mst_slot_got_plt:
  442.       write_exp_elt_type (ps, objfile_type (objfile)->nodebug_got_plt_symbol);
  443.       break;

  444.     default:
  445.       write_exp_elt_type (ps, objfile_type (objfile)->nodebug_unknown_symbol);
  446.       break;
  447.     }
  448.   write_exp_elt_opcode (ps, UNOP_MEMVAL);
  449. }

  450. /* Mark the current index as the starting location of a structure
  451.    expression.  This is used when completing on field names.  */

  452. void
  453. mark_struct_expression (struct parser_state *ps)
  454. {
  455.   gdb_assert (parse_completion
  456.               && expout_tag_completion_type == TYPE_CODE_UNDEF);
  457.   expout_last_struct = ps->expout_ptr;
  458. }

  459. /* Indicate that the current parser invocation is completing a tag.
  460.    TAG is the type code of the tag, and PTR and LENGTH represent the
  461.    start of the tag name.  */

  462. void
  463. mark_completion_tag (enum type_code tag, const char *ptr, int length)
  464. {
  465.   gdb_assert (parse_completion
  466.               && expout_tag_completion_type == TYPE_CODE_UNDEF
  467.               && expout_completion_name == NULL
  468.               && expout_last_struct == -1);
  469.   gdb_assert (tag == TYPE_CODE_UNION
  470.               || tag == TYPE_CODE_STRUCT
  471.               || tag == TYPE_CODE_ENUM);
  472.   expout_tag_completion_type = tag;
  473.   expout_completion_name = xmalloc (length + 1);
  474.   memcpy (expout_completion_name, ptr, length);
  475.   expout_completion_name[length] = '\0';
  476. }


  477. /* Recognize tokens that start with '$'.  These include:

  478.    $regname     A native register name or a "standard
  479.    register name".

  480.    $variable    A convenience variable with a name chosen
  481.    by the user.

  482.    $digits              Value history with index <digits>, starting
  483.    from the first value which has index 1.

  484.    $$digits     Value history with index <digits> relative
  485.    to the last valueI.e. $$0 is the last
  486.    value, $$1 is the one previous to that, $$2
  487.    is the one previous to $$1, etc.

  488.    $ | $0 | $$0 The last value in the value history.

  489.    $$           An abbreviation for the second to the last
  490.    value in the value history, I.e. $$1  */

  491. void
  492. write_dollar_variable (struct parser_state *ps, struct stoken str)
  493. {
  494.   struct symbol *sym = NULL;
  495.   struct bound_minimal_symbol msym;
  496.   struct internalvar *isym = NULL;

  497.   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
  498.      and $$digits (equivalent to $<-digits> if you could type that).  */

  499.   int negate = 0;
  500.   int i = 1;
  501.   /* Double dollar means negate the number and add -1 as well.
  502.      Thus $$ alone means -1.  */
  503.   if (str.length >= 2 && str.ptr[1] == '$')
  504.     {
  505.       negate = 1;
  506.       i = 2;
  507.     }
  508.   if (i == str.length)
  509.     {
  510.       /* Just dollars (one or two).  */
  511.       i = -negate;
  512.       goto handle_last;
  513.     }
  514.   /* Is the rest of the token digits?  */
  515.   for (; i < str.length; i++)
  516.     if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
  517.       break;
  518.   if (i == str.length)
  519.     {
  520.       i = atoi (str.ptr + 1 + negate);
  521.       if (negate)
  522.         i = -i;
  523.       goto handle_last;
  524.     }

  525.   /* Handle tokens that refer to machine registers:
  526.      $ followed by a register name.  */
  527.   i = user_reg_map_name_to_regnum (parse_gdbarch (ps),
  528.                                    str.ptr + 1, str.length - 1);
  529.   if (i >= 0)
  530.     goto handle_register;

  531.   /* Any names starting with $ are probably debugger internal variables.  */

  532.   isym = lookup_only_internalvar (copy_name (str) + 1);
  533.   if (isym)
  534.     {
  535.       write_exp_elt_opcode (ps, OP_INTERNALVAR);
  536.       write_exp_elt_intern (ps, isym);
  537.       write_exp_elt_opcode (ps, OP_INTERNALVAR);
  538.       return;
  539.     }

  540.   /* On some systems, such as HP-UX and hppa-linux, certain system routines
  541.      have names beginning with $ or $$.  Check for those, first.  */

  542.   sym = lookup_symbol (copy_name (str), (struct block *) NULL,
  543.                        VAR_DOMAIN, NULL);
  544.   if (sym)
  545.     {
  546.       write_exp_elt_opcode (ps, OP_VAR_VALUE);
  547.       write_exp_elt_block (ps, block_found);        /* set by lookup_symbol */
  548.       write_exp_elt_sym (ps, sym);
  549.       write_exp_elt_opcode (ps, OP_VAR_VALUE);
  550.       return;
  551.     }
  552.   msym = lookup_bound_minimal_symbol (copy_name (str));
  553.   if (msym.minsym)
  554.     {
  555.       write_exp_msymbol (ps, msym);
  556.       return;
  557.     }

  558.   /* Any other names are assumed to be debugger internal variables.  */

  559.   write_exp_elt_opcode (ps, OP_INTERNALVAR);
  560.   write_exp_elt_intern (ps, create_internalvar (copy_name (str) + 1));
  561.   write_exp_elt_opcode (ps, OP_INTERNALVAR);
  562.   return;
  563. handle_last:
  564.   write_exp_elt_opcode (ps, OP_LAST);
  565.   write_exp_elt_longcst (ps, (LONGEST) i);
  566.   write_exp_elt_opcode (ps, OP_LAST);
  567.   return;
  568. handle_register:
  569.   write_exp_elt_opcode (ps, OP_REGISTER);
  570.   str.length--;
  571.   str.ptr++;
  572.   write_exp_string (ps, str);
  573.   write_exp_elt_opcode (ps, OP_REGISTER);
  574.   return;
  575. }


  576. const char *
  577. find_template_name_end (const char *p)
  578. {
  579.   int depth = 1;
  580.   int just_seen_right = 0;
  581.   int just_seen_colon = 0;
  582.   int just_seen_space = 0;

  583.   if (!p || (*p != '<'))
  584.     return 0;

  585.   while (*++p)
  586.     {
  587.       switch (*p)
  588.         {
  589.         case '\'':
  590.         case '\"':
  591.         case '{':
  592.         case '}':
  593.           /* In future, may want to allow these??  */
  594.           return 0;
  595.         case '<':
  596.           depth++;                /* start nested template */
  597.           if (just_seen_colon || just_seen_right || just_seen_space)
  598.             return 0;                /* but not after : or :: or > or space */
  599.           break;
  600.         case '>':
  601.           if (just_seen_colon || just_seen_right)
  602.             return 0;                /* end a (nested?) template */
  603.           just_seen_right = 1;        /* but not after : or :: */
  604.           if (--depth == 0)        /* also disallow >>, insist on > > */
  605.             return ++p;                /* if outermost ended, return */
  606.           break;
  607.         case ':':
  608.           if (just_seen_space || (just_seen_colon > 1))
  609.             return 0;                /* nested class spec coming up */
  610.           just_seen_colon++;        /* we allow :: but not :::: */
  611.           break;
  612.         case ' ':
  613.           break;
  614.         default:
  615.           if (!((*p >= 'a' && *p <= 'z') ||        /* allow token chars */
  616.                 (*p >= 'A' && *p <= 'Z') ||
  617.                 (*p >= '0' && *p <= '9') ||
  618.                 (*p == '_') || (*p == ',') ||        /* commas for template args */
  619.                 (*p == '&') || (*p == '*') ||        /* pointer and ref types */
  620.                 (*p == '(') || (*p == ')') ||        /* function types */
  621.                 (*p == '[') || (*p == ']')))        /* array types */
  622.             return 0;
  623.         }
  624.       if (*p != ' ')
  625.         just_seen_space = 0;
  626.       if (*p != ':')
  627.         just_seen_colon = 0;
  628.       if (*p != '>')
  629.         just_seen_right = 0;
  630.     }
  631.   return 0;
  632. }


  633. /* Return a null-terminated temporary copy of the name of a string token.

  634.    Tokens that refer to names do so with explicit pointer and length,
  635.    so they can share the storage that lexptr is parsing.
  636.    When it is necessary to pass a name to a function that expects
  637.    a null-terminated string, the substring is copied out
  638.    into a separate block of storage.

  639.    N.B. A single buffer is reused on each call.  */

  640. char *
  641. copy_name (struct stoken token)
  642. {
  643.   /* A temporary buffer for identifiers, so we can null-terminate them.
  644.      We allocate this with xreallocparse_exp_1 used to allocate with
  645.      alloca, using the size of the whole expression as a conservative
  646.      estimate of the space needed.  However, macro expansion can
  647.      introduce names longer than the original expression; there's no
  648.      practical way to know beforehand how large that might be.  */
  649.   static char *namecopy;
  650.   static size_t namecopy_size;

  651.   /* Make sure there's enough space for the token.  */
  652.   if (namecopy_size < token.length + 1)
  653.     {
  654.       namecopy_size = token.length + 1;
  655.       namecopy = xrealloc (namecopy, token.length + 1);
  656.     }

  657.   memcpy (namecopy, token.ptr, token.length);
  658.   namecopy[token.length] = 0;

  659.   return namecopy;
  660. }


  661. /* See comments on parser-defs.h.  */

  662. int
  663. prefixify_expression (struct expression *expr)
  664. {
  665.   int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
  666.   struct expression *temp;
  667.   int inpos = expr->nelts, outpos = 0;

  668.   temp = (struct expression *) alloca (len);

  669.   /* Copy the original expression into temp.  */
  670.   memcpy (temp, expr, len);

  671.   return prefixify_subexp (temp, expr, inpos, outpos);
  672. }

  673. /* Return the number of exp_elements in the postfix subexpression
  674.    of EXPR whose operator is at index ENDPOS - 1 in EXPR.  */

  675. int
  676. length_of_subexp (struct expression *expr, int endpos)
  677. {
  678.   int oplen, args;

  679.   operator_length (expr, endpos, &oplen, &args);

  680.   while (args > 0)
  681.     {
  682.       oplen += length_of_subexp (expr, endpos - oplen);
  683.       args--;
  684.     }

  685.   return oplen;
  686. }

  687. /* Sets *OPLENP to the length of the operator whose (last) index is
  688.    ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
  689.    operator takes.  */

  690. void
  691. operator_length (const struct expression *expr, int endpos, int *oplenp,
  692.                  int *argsp)
  693. {
  694.   expr->language_defn->la_exp_desc->operator_length (expr, endpos,
  695.                                                      oplenp, argsp);
  696. }

  697. /* Default value for operator_length in exp_descriptor vectors.  */

  698. void
  699. operator_length_standard (const struct expression *expr, int endpos,
  700.                           int *oplenp, int *argsp)
  701. {
  702.   int oplen = 1;
  703.   int args = 0;
  704.   enum f90_range_type range_type;
  705.   int i;

  706.   if (endpos < 1)
  707.     error (_("?error in operator_length_standard"));

  708.   i = (int) expr->elts[endpos - 1].opcode;

  709.   switch (i)
  710.     {
  711.       /* C++  */
  712.     case OP_SCOPE:
  713.       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
  714.       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
  715.       break;

  716.     case OP_LONG:
  717.     case OP_DOUBLE:
  718.     case OP_DECFLOAT:
  719.     case OP_VAR_VALUE:
  720.       oplen = 4;
  721.       break;

  722.     case OP_TYPE:
  723.     case OP_BOOL:
  724.     case OP_LAST:
  725.     case OP_INTERNALVAR:
  726.     case OP_VAR_ENTRY_VALUE:
  727.       oplen = 3;
  728.       break;

  729.     case OP_COMPLEX:
  730.       oplen = 3;
  731.       args = 2;
  732.       break;

  733.     case OP_FUNCALL:
  734.     case OP_F77_UNDETERMINED_ARGLIST:
  735.       oplen = 3;
  736.       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
  737.       break;

  738.     case TYPE_INSTANCE:
  739.       oplen = 4 + longest_to_int (expr->elts[endpos - 2].longconst);
  740.       args = 1;
  741.       break;

  742.     case OP_OBJC_MSGCALL:        /* Objective C message (method) call.  */
  743.       oplen = 4;
  744.       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
  745.       break;

  746.     case UNOP_MAX:
  747.     case UNOP_MIN:
  748.       oplen = 3;
  749.       break;

  750.     case UNOP_CAST_TYPE:
  751.     case UNOP_DYNAMIC_CAST:
  752.     case UNOP_REINTERPRET_CAST:
  753.     case UNOP_MEMVAL_TYPE:
  754.       oplen = 1;
  755.       args = 2;
  756.       break;

  757.     case BINOP_VAL:
  758.     case UNOP_CAST:
  759.     case UNOP_MEMVAL:
  760.       oplen = 3;
  761.       args = 1;
  762.       break;

  763.     case UNOP_MEMVAL_TLS:
  764.       oplen = 4;
  765.       args = 1;
  766.       break;

  767.     case UNOP_ABS:
  768.     case UNOP_CAP:
  769.     case UNOP_CHR:
  770.     case UNOP_FLOAT:
  771.     case UNOP_HIGH:
  772.     case UNOP_ODD:
  773.     case UNOP_ORD:
  774.     case UNOP_TRUNC:
  775.     case OP_TYPEOF:
  776.     case OP_DECLTYPE:
  777.     case OP_TYPEID:
  778.       oplen = 1;
  779.       args = 1;
  780.       break;

  781.     case OP_ADL_FUNC:
  782.       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
  783.       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
  784.       oplen++;
  785.       oplen++;
  786.       break;

  787.     case STRUCTOP_STRUCT:
  788.     case STRUCTOP_PTR:
  789.       args = 1;
  790.       /* fall through */
  791.     case OP_REGISTER:
  792.     case OP_M2_STRING:
  793.     case OP_STRING:
  794.     case OP_OBJC_NSSTRING:        /* Objective C Foundation Class
  795.                                    NSString constant.  */
  796.     case OP_OBJC_SELECTOR:        /* Objective C "@selector" pseudo-op.  */
  797.     case OP_NAME:
  798.       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
  799.       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
  800.       break;

  801.     case OP_ARRAY:
  802.       oplen = 4;
  803.       args = longest_to_int (expr->elts[endpos - 2].longconst);
  804.       args -= longest_to_int (expr->elts[endpos - 3].longconst);
  805.       args += 1;
  806.       break;

  807.     case TERNOP_COND:
  808.     case TERNOP_SLICE:
  809.       args = 3;
  810.       break;

  811.       /* Modula-2 */
  812.     case MULTI_SUBSCRIPT:
  813.       oplen = 3;
  814.       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
  815.       break;

  816.     case BINOP_ASSIGN_MODIFY:
  817.       oplen = 3;
  818.       args = 2;
  819.       break;

  820.       /* C++ */
  821.     case OP_THIS:
  822.       oplen = 2;
  823.       break;

  824.     case OP_F90_RANGE:
  825.       oplen = 3;

  826.       range_type = longest_to_int (expr->elts[endpos - 2].longconst);
  827.       switch (range_type)
  828.         {
  829.         case LOW_BOUND_DEFAULT:
  830.         case HIGH_BOUND_DEFAULT:
  831.           args = 1;
  832.           break;
  833.         case BOTH_BOUND_DEFAULT:
  834.           args = 0;
  835.           break;
  836.         case NONE_BOUND_DEFAULT:
  837.           args = 2;
  838.           break;
  839.         }

  840.       break;

  841.     default:
  842.       args = 1 + (i < (int) BINOP_END);
  843.     }

  844.   *oplenp = oplen;
  845.   *argsp = args;
  846. }

  847. /* Copy the subexpression ending just before index INEND in INEXPR
  848.    into OUTEXPR, starting at index OUTBEG.
  849.    In the process, convert it from suffix to prefix form.
  850.    If EXPOUT_LAST_STRUCT is -1, then this function always returns -1.
  851.    Otherwise, it returns the index of the subexpression which is the
  852.    left-hand-side of the expression at EXPOUT_LAST_STRUCT.  */

  853. static int
  854. prefixify_subexp (struct expression *inexpr,
  855.                   struct expression *outexpr, int inend, int outbeg)
  856. {
  857.   int oplen;
  858.   int args;
  859.   int i;
  860.   int *arglens;
  861.   int result = -1;

  862.   operator_length (inexpr, inend, &oplen, &args);

  863.   /* Copy the final operator itself, from the end of the input
  864.      to the beginning of the output.  */
  865.   inend -= oplen;
  866.   memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
  867.           EXP_ELEM_TO_BYTES (oplen));
  868.   outbeg += oplen;

  869.   if (expout_last_struct == inend)
  870.     result = outbeg - oplen;

  871.   /* Find the lengths of the arg subexpressions.  */
  872.   arglens = (int *) alloca (args * sizeof (int));
  873.   for (i = args - 1; i >= 0; i--)
  874.     {
  875.       oplen = length_of_subexp (inexpr, inend);
  876.       arglens[i] = oplen;
  877.       inend -= oplen;
  878.     }

  879.   /* Now copy each subexpression, preserving the order of
  880.      the subexpressions, but prefixifying each one.
  881.      In this loop, inend starts at the beginning of
  882.      the expression this level is working on
  883.      and marches forward over the arguments.
  884.      outbeg does similarly in the output.  */
  885.   for (i = 0; i < args; i++)
  886.     {
  887.       int r;

  888.       oplen = arglens[i];
  889.       inend += oplen;
  890.       r = prefixify_subexp (inexpr, outexpr, inend, outbeg);
  891.       if (r != -1)
  892.         {
  893.           /* Return immediately.  We probably have only parsed a
  894.              partial expression, so we don't want to try to reverse
  895.              the other operands.  */
  896.           return r;
  897.         }
  898.       outbeg += oplen;
  899.     }

  900.   return result;
  901. }

  902. /* Read an expression from the string *STRINGPTR points to,
  903.    parse it, and return a pointer to a struct expression that we malloc.
  904.    Use block BLOCK as the lexical context for variable names;
  905.    if BLOCK is zero, use the block of the selected stack frame.
  906.    Meanwhile, advance *STRINGPTR to point after the expression,
  907.    at the first nonwhite character that is not part of the expression
  908.    (possibly a null character).

  909.    If COMMA is nonzero, stop if a comma is reached.  */

  910. struct expression *
  911. parse_exp_1 (const char **stringptr, CORE_ADDR pc, const struct block *block,
  912.              int comma)
  913. {
  914.   return parse_exp_in_context (stringptr, pc, block, comma, 0, NULL);
  915. }

  916. static struct expression *
  917. parse_exp_in_context (const char **stringptr, CORE_ADDR pc,
  918.                       const struct block *block,
  919.                       int comma, int void_context_p, int *out_subexp)
  920. {
  921.   return parse_exp_in_context_1 (stringptr, pc, block, comma,
  922.                                  void_context_p, out_subexp);
  923. }

  924. /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
  925.    no value is expected from the expression.
  926.    OUT_SUBEXP is set when attempting to complete a field name; in this
  927.    case it is set to the index of the subexpression on the
  928.    left-hand-side of the struct op.  If not doing such completion, it
  929.    is left untouched.  */

  930. static struct expression *
  931. parse_exp_in_context_1 (const char **stringptr, CORE_ADDR pc,
  932.                         const struct block *block,
  933.                         int comma, int void_context_p, int *out_subexp)
  934. {
  935.   volatile struct gdb_exception except;
  936.   struct cleanup *old_chain, *inner_chain;
  937.   const struct language_defn *lang = NULL;
  938.   struct parser_state ps;
  939.   int subexp;

  940.   lexptr = *stringptr;
  941.   prev_lexptr = NULL;

  942.   paren_depth = 0;
  943.   type_stack.depth = 0;
  944.   expout_last_struct = -1;
  945.   expout_tag_completion_type = TYPE_CODE_UNDEF;
  946.   xfree (expout_completion_name);
  947.   expout_completion_name = NULL;

  948.   comma_terminates = comma;

  949.   if (lexptr == 0 || *lexptr == 0)
  950.     error_no_arg (_("expression to compute"));

  951.   old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
  952.   funcall_chain = 0;

  953.   expression_context_block = block;

  954.   /* If no context specified, try using the current frame, if any.  */
  955.   if (!expression_context_block)
  956.     expression_context_block = get_selected_block (&expression_context_pc);
  957.   else if (pc == 0)
  958.     expression_context_pc = BLOCK_START (expression_context_block);
  959.   else
  960.     expression_context_pc = pc;

  961.   /* Fall back to using the current source static context, if any.  */

  962.   if (!expression_context_block)
  963.     {
  964.       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
  965.       if (cursal.symtab)
  966.         expression_context_block
  967.           = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab),
  968.                                STATIC_BLOCK);
  969.       if (expression_context_block)
  970.         expression_context_pc = BLOCK_START (expression_context_block);
  971.     }

  972.   if (language_mode == language_mode_auto && block != NULL)
  973.     {
  974.       /* Find the language associated to the given context block.
  975.          Default to the current language if it can not be determined.

  976.          Note that using the language corresponding to the current frame
  977.          can sometimes give unexpected results.  For instance, this
  978.          routine is often called several times during the inferior
  979.          startup phase to re-parse breakpoint expressions after
  980.          a new shared library has been loaded.  The language associated
  981.          to the current frame at this moment is not relevant for
  982.          the breakpoint.  Using it would therefore be silly, so it seems
  983.          better to rely on the current language rather than relying on
  984.          the current frame language to parse the expression.  That's why
  985.          we do the following language detection only if the context block
  986.          has been specifically provided.  */
  987.       struct symbol *func = block_linkage_function (block);

  988.       if (func != NULL)
  989.         lang = language_def (SYMBOL_LANGUAGE (func));
  990.       if (lang == NULL || lang->la_language == language_unknown)
  991.         lang = current_language;
  992.     }
  993.   else
  994.     lang = current_language;

  995.   /* get_current_arch may reset CURRENT_LANGUAGE via select_frame.
  996.      While we need CURRENT_LANGUAGE to be set to LANG (for lookup_symbol
  997.      and others called from *.y) ensure CURRENT_LANGUAGE gets restored
  998.      to the value matching SELECTED_FRAME as set by get_current_arch.  */

  999.   initialize_expout (&ps, 10, lang, get_current_arch ());
  1000.   inner_chain = make_cleanup_restore_current_language ();
  1001.   set_language (lang->la_language);

  1002.   TRY_CATCH (except, RETURN_MASK_ALL)
  1003.     {
  1004.       if (lang->la_parser (&ps))
  1005.         lang->la_error (NULL);
  1006.     }
  1007.   if (except.reason < 0)
  1008.     {
  1009.       if (! parse_completion)
  1010.         {
  1011.           xfree (ps.expout);
  1012.           throw_exception (except);
  1013.         }
  1014.     }

  1015.   reallocate_expout (&ps);

  1016.   /* Convert expression from postfix form as generated by yacc
  1017.      parser, to a prefix form.  */

  1018.   if (expressiondebug)
  1019.     dump_raw_expression (ps.expout, gdb_stdlog,
  1020.                          "before conversion to prefix form");

  1021.   subexp = prefixify_expression (ps.expout);
  1022.   if (out_subexp)
  1023.     *out_subexp = subexp;

  1024.   lang->la_post_parser (&ps.expout, void_context_p);

  1025.   if (expressiondebug)
  1026.     dump_prefix_expression (ps.expout, gdb_stdlog);

  1027.   do_cleanups (inner_chain);
  1028.   discard_cleanups (old_chain);

  1029.   *stringptr = lexptr;
  1030.   return ps.expout;
  1031. }

  1032. /* Parse STRING as an expression, and complain if this fails
  1033.    to use up all of the contents of STRING.  */

  1034. struct expression *
  1035. parse_expression (const char *string)
  1036. {
  1037.   struct expression *exp;

  1038.   exp = parse_exp_1 (&string, 0, 0, 0);
  1039.   if (*string)
  1040.     error (_("Junk after end of expression."));
  1041.   return exp;
  1042. }

  1043. /* Parse STRING as an expression.  If parsing ends in the middle of a
  1044.    field reference, return the type of the left-hand-side of the
  1045.    reference; furthermore, if the parsing ends in the field name,
  1046.    return the field name in *NAME.  If the parsing ends in the middle
  1047.    of a field reference, but the reference is somehow invalid, throw
  1048.    an exception.  In all other cases, return NULL.  Returned non-NULL
  1049.    *NAME must be freed by the caller.  */

  1050. struct type *
  1051. parse_expression_for_completion (const char *string, char **name,
  1052.                                  enum type_code *code)
  1053. {
  1054.   struct expression *exp = NULL;
  1055.   struct value *val;
  1056.   int subexp;
  1057.   volatile struct gdb_exception except;

  1058.   TRY_CATCH (except, RETURN_MASK_ERROR)
  1059.     {
  1060.       parse_completion = 1;
  1061.       exp = parse_exp_in_context (&string, 0, 0, 0, 0, &subexp);
  1062.     }
  1063.   parse_completion = 0;
  1064.   if (except.reason < 0 || ! exp)
  1065.     return NULL;

  1066.   if (expout_tag_completion_type != TYPE_CODE_UNDEF)
  1067.     {
  1068.       *code = expout_tag_completion_type;
  1069.       *name = expout_completion_name;
  1070.       expout_completion_name = NULL;
  1071.       return NULL;
  1072.     }

  1073.   if (expout_last_struct == -1)
  1074.     {
  1075.       xfree (exp);
  1076.       return NULL;
  1077.     }

  1078.   *name = extract_field_op (exp, &subexp);
  1079.   if (!*name)
  1080.     {
  1081.       xfree (exp);
  1082.       return NULL;
  1083.     }

  1084.   /* This might throw an exception.  If so, we want to let it
  1085.      propagate.  */
  1086.   val = evaluate_subexpression_type (exp, subexp);
  1087.   /* (*NAME) is a part of the EXP memory block freed below.  */
  1088.   *name = xstrdup (*name);
  1089.   xfree (exp);

  1090.   return value_type (val);
  1091. }

  1092. /* A post-parser that does nothing.  */

  1093. void
  1094. null_post_parser (struct expression **exp, int void_context_p)
  1095. {
  1096. }

  1097. /* Parse floating point value P of length LEN.
  1098.    Return 0 (false) if invalid, 1 (true) if valid.
  1099.    The successfully parsed number is stored in D.
  1100.    *SUFFIX points to the suffix of the number in P.

  1101.    NOTE: This accepts the floating point syntax that sscanf accepts.  */

  1102. int
  1103. parse_float (const char *p, int len, DOUBLEST *d, const char **suffix)
  1104. {
  1105.   char *copy;
  1106.   int n, num;

  1107.   copy = xmalloc (len + 1);
  1108.   memcpy (copy, p, len);
  1109.   copy[len] = 0;

  1110.   num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
  1111.   xfree (copy);

  1112.   /* The sscanf man page suggests not making any assumptions on the effect
  1113.      of %n on the result, so we don't.
  1114.      That is why we simply test num == 0.  */
  1115.   if (num == 0)
  1116.     return 0;

  1117.   *suffix = p + n;
  1118.   return 1;
  1119. }

  1120. /* Parse floating point value P of length LEN, using the C syntax for floats.
  1121.    Return 0 (false) if invalid, 1 (true) if valid.
  1122.    The successfully parsed number is stored in *D.
  1123.    Its type is taken from builtin_type (gdbarch) and is stored in *T.  */

  1124. int
  1125. parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
  1126.                DOUBLEST *d, struct type **t)
  1127. {
  1128.   const char *suffix;
  1129.   int suffix_len;
  1130.   const struct builtin_type *builtin_types = builtin_type (gdbarch);

  1131.   if (! parse_float (p, len, d, &suffix))
  1132.     return 0;

  1133.   suffix_len = p + len - suffix;

  1134.   if (suffix_len == 0)
  1135.     *t = builtin_types->builtin_double;
  1136.   else if (suffix_len == 1)
  1137.     {
  1138.       /* Handle suffixes: 'f' for float, 'l' for long double.  */
  1139.       if (tolower (*suffix) == 'f')
  1140.         *t = builtin_types->builtin_float;
  1141.       else if (tolower (*suffix) == 'l')
  1142.         *t = builtin_types->builtin_long_double;
  1143.       else
  1144.         return 0;
  1145.     }
  1146.   else
  1147.     return 0;

  1148.   return 1;
  1149. }

  1150. /* Stuff for maintaining a stack of types.  Currently just used by C, but
  1151.    probably useful for any language which declares its types "backwards".  */

  1152. /* Ensure that there are HOWMUCH open slots on the type stack STACK.  */

  1153. static void
  1154. type_stack_reserve (struct type_stack *stack, int howmuch)
  1155. {
  1156.   if (stack->depth + howmuch >= stack->size)
  1157.     {
  1158.       stack->size *= 2;
  1159.       if (stack->size < howmuch)
  1160.         stack->size = howmuch;
  1161.       stack->elements = xrealloc (stack->elements,
  1162.                                   stack->size * sizeof (union type_stack_elt));
  1163.     }
  1164. }

  1165. /* Ensure that there is a single open slot in the global type stack.  */

  1166. static void
  1167. check_type_stack_depth (void)
  1168. {
  1169.   type_stack_reserve (&type_stack, 1);
  1170. }

  1171. /* A helper function for insert_type and insert_type_address_space.
  1172.    This does work of expanding the type stack and inserting the new
  1173.    element, ELEMENT, into the stack at location SLOT.  */

  1174. static void
  1175. insert_into_type_stack (int slot, union type_stack_elt element)
  1176. {
  1177.   check_type_stack_depth ();

  1178.   if (slot < type_stack.depth)
  1179.     memmove (&type_stack.elements[slot + 1], &type_stack.elements[slot],
  1180.              (type_stack.depth - slot) * sizeof (union type_stack_elt));
  1181.   type_stack.elements[slot] = element;
  1182.   ++type_stack.depth;
  1183. }

  1184. /* Insert a new type, TP, at the bottom of the type stack.  If TP is
  1185.    tp_pointer or tp_reference, it is inserted at the bottom.  If TP is
  1186.    a qualifier, it is inserted at slot 1 (just above a previous
  1187.    tp_pointer) if there is anything on the stack, or simply pushed if
  1188.    the stack is empty.  Other values for TP are invalid.  */

  1189. void
  1190. insert_type (enum type_pieces tp)
  1191. {
  1192.   union type_stack_elt element;
  1193.   int slot;

  1194.   gdb_assert (tp == tp_pointer || tp == tp_reference
  1195.               || tp == tp_const || tp == tp_volatile);

  1196.   /* If there is anything on the stack (we know it will be a
  1197.      tp_pointer), insert the qualifier above it.  Otherwise, simply
  1198.      push this on the top of the stack.  */
  1199.   if (type_stack.depth && (tp == tp_const || tp == tp_volatile))
  1200.     slot = 1;
  1201.   else
  1202.     slot = 0;

  1203.   element.piece = tp;
  1204.   insert_into_type_stack (slot, element);
  1205. }

  1206. void
  1207. push_type (enum type_pieces tp)
  1208. {
  1209.   check_type_stack_depth ();
  1210.   type_stack.elements[type_stack.depth++].piece = tp;
  1211. }

  1212. void
  1213. push_type_int (int n)
  1214. {
  1215.   check_type_stack_depth ();
  1216.   type_stack.elements[type_stack.depth++].int_val = n;
  1217. }

  1218. /* Insert a tp_space_identifier and the corresponding address space
  1219.    value into the stack.  STRING is the name of an address space, as
  1220.    recognized by address_space_name_to_int.  If the stack is empty,
  1221.    the new elements are simply pushed.  If the stack is not empty,
  1222.    this function assumes that the first item on the stack is a
  1223.    tp_pointer, and the new values are inserted above the first
  1224.    item.  */

  1225. void
  1226. insert_type_address_space (struct parser_state *pstate, char *string)
  1227. {
  1228.   union type_stack_elt element;
  1229.   int slot;

  1230.   /* If there is anything on the stack (we know it will be a
  1231.      tp_pointer), insert the address space qualifier above it.
  1232.      Otherwise, simply push this on the top of the stack.  */
  1233.   if (type_stack.depth)
  1234.     slot = 1;
  1235.   else
  1236.     slot = 0;

  1237.   element.piece = tp_space_identifier;
  1238.   insert_into_type_stack (slot, element);
  1239.   element.int_val = address_space_name_to_int (parse_gdbarch (pstate),
  1240.                                                string);
  1241.   insert_into_type_stack (slot, element);
  1242. }

  1243. enum type_pieces
  1244. pop_type (void)
  1245. {
  1246.   if (type_stack.depth)
  1247.     return type_stack.elements[--type_stack.depth].piece;
  1248.   return tp_end;
  1249. }

  1250. int
  1251. pop_type_int (void)
  1252. {
  1253.   if (type_stack.depth)
  1254.     return type_stack.elements[--type_stack.depth].int_val;
  1255.   /* "Can't happen".  */
  1256.   return 0;
  1257. }

  1258. /* Pop a type list element from the global type stack.  */

  1259. static VEC (type_ptr) *
  1260. pop_typelist (void)
  1261. {
  1262.   gdb_assert (type_stack.depth);
  1263.   return type_stack.elements[--type_stack.depth].typelist_val;
  1264. }

  1265. /* Pop a type_stack element from the global type stack.  */

  1266. static struct type_stack *
  1267. pop_type_stack (void)
  1268. {
  1269.   gdb_assert (type_stack.depth);
  1270.   return type_stack.elements[--type_stack.depth].stack_val;
  1271. }

  1272. /* Append the elements of the type stack FROM to the type stack TO.
  1273.    Always returns TO.  */

  1274. struct type_stack *
  1275. append_type_stack (struct type_stack *to, struct type_stack *from)
  1276. {
  1277.   type_stack_reserve (to, from->depth);

  1278.   memcpy (&to->elements[to->depth], &from->elements[0],
  1279.           from->depth * sizeof (union type_stack_elt));
  1280.   to->depth += from->depth;

  1281.   return to;
  1282. }

  1283. /* Push the type stack STACK as an element on the global type stack.  */

  1284. void
  1285. push_type_stack (struct type_stack *stack)
  1286. {
  1287.   check_type_stack_depth ();
  1288.   type_stack.elements[type_stack.depth++].stack_val = stack;
  1289.   push_type (tp_type_stack);
  1290. }

  1291. /* Copy the global type stack into a newly allocated type stack and
  1292.    return it.  The global stack is cleared.  The returned type stack
  1293.    must be freed with type_stack_cleanup.  */

  1294. struct type_stack *
  1295. get_type_stack (void)
  1296. {
  1297.   struct type_stack *result = XNEW (struct type_stack);

  1298.   *result = type_stack;
  1299.   type_stack.depth = 0;
  1300.   type_stack.size = 0;
  1301.   type_stack.elements = NULL;

  1302.   return result;
  1303. }

  1304. /* A cleanup function that destroys a single type stack.  */

  1305. void
  1306. type_stack_cleanup (void *arg)
  1307. {
  1308.   struct type_stack *stack = arg;

  1309.   xfree (stack->elements);
  1310.   xfree (stack);
  1311. }

  1312. /* Push a function type with arguments onto the global type stack.
  1313.    LIST holds the argument types.  If the final item in LIST is NULL,
  1314.    then the function will be varargs.  */

  1315. void
  1316. push_typelist (VEC (type_ptr) *list)
  1317. {
  1318.   check_type_stack_depth ();
  1319.   type_stack.elements[type_stack.depth++].typelist_val = list;
  1320.   push_type (tp_function_with_arguments);
  1321. }

  1322. /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
  1323.    as modified by all the stuff on the stack.  */
  1324. struct type *
  1325. follow_types (struct type *follow_type)
  1326. {
  1327.   int done = 0;
  1328.   int make_const = 0;
  1329.   int make_volatile = 0;
  1330.   int make_addr_space = 0;
  1331.   int array_size;

  1332.   while (!done)
  1333.     switch (pop_type ())
  1334.       {
  1335.       case tp_end:
  1336.         done = 1;
  1337.         if (make_const)
  1338.           follow_type = make_cv_type (make_const,
  1339.                                       TYPE_VOLATILE (follow_type),
  1340.                                       follow_type, 0);
  1341.         if (make_volatile)
  1342.           follow_type = make_cv_type (TYPE_CONST (follow_type),
  1343.                                       make_volatile,
  1344.                                       follow_type, 0);
  1345.         if (make_addr_space)
  1346.           follow_type = make_type_with_address_space (follow_type,
  1347.                                                       make_addr_space);
  1348.         make_const = make_volatile = 0;
  1349.         make_addr_space = 0;
  1350.         break;
  1351.       case tp_const:
  1352.         make_const = 1;
  1353.         break;
  1354.       case tp_volatile:
  1355.         make_volatile = 1;
  1356.         break;
  1357.       case tp_space_identifier:
  1358.         make_addr_space = pop_type_int ();
  1359.         break;
  1360.       case tp_pointer:
  1361.         follow_type = lookup_pointer_type (follow_type);
  1362.         if (make_const)
  1363.           follow_type = make_cv_type (make_const,
  1364.                                       TYPE_VOLATILE (follow_type),
  1365.                                       follow_type, 0);
  1366.         if (make_volatile)
  1367.           follow_type = make_cv_type (TYPE_CONST (follow_type),
  1368.                                       make_volatile,
  1369.                                       follow_type, 0);
  1370.         if (make_addr_space)
  1371.           follow_type = make_type_with_address_space (follow_type,
  1372.                                                       make_addr_space);
  1373.         make_const = make_volatile = 0;
  1374.         make_addr_space = 0;
  1375.         break;
  1376.       case tp_reference:
  1377.         follow_type = lookup_reference_type (follow_type);
  1378.         if (make_const)
  1379.           follow_type = make_cv_type (make_const,
  1380.                                       TYPE_VOLATILE (follow_type),
  1381.                                       follow_type, 0);
  1382.         if (make_volatile)
  1383.           follow_type = make_cv_type (TYPE_CONST (follow_type),
  1384.                                       make_volatile,
  1385.                                       follow_type, 0);
  1386.         if (make_addr_space)
  1387.           follow_type = make_type_with_address_space (follow_type,
  1388.                                                       make_addr_space);
  1389.         make_const = make_volatile = 0;
  1390.         make_addr_space = 0;
  1391.         break;
  1392.       case tp_array:
  1393.         array_size = pop_type_int ();
  1394.         /* FIXME-type-allocation: need a way to free this type when we are
  1395.            done with it.  */
  1396.         follow_type =
  1397.           lookup_array_range_type (follow_type,
  1398.                                    0, array_size >= 0 ? array_size - 1 : 0);
  1399.         if (array_size < 0)
  1400.           TYPE_HIGH_BOUND_KIND (TYPE_INDEX_TYPE (follow_type))
  1401.             = PROP_UNDEFINED;
  1402.         break;
  1403.       case tp_function:
  1404.         /* FIXME-type-allocation: need a way to free this type when we are
  1405.            done with it.  */
  1406.         follow_type = lookup_function_type (follow_type);
  1407.         break;

  1408.       case tp_function_with_arguments:
  1409.         {
  1410.           VEC (type_ptr) *args = pop_typelist ();

  1411.           follow_type
  1412.             = lookup_function_type_with_arguments (follow_type,
  1413.                                                    VEC_length (type_ptr, args),
  1414.                                                    VEC_address (type_ptr,
  1415.                                                                 args));
  1416.           VEC_free (type_ptr, args);
  1417.         }
  1418.         break;

  1419.       case tp_type_stack:
  1420.         {
  1421.           struct type_stack *stack = pop_type_stack ();
  1422.           /* Sort of ugly, but not really much worse than the
  1423.              alternatives.  */
  1424.           struct type_stack save = type_stack;

  1425.           type_stack = *stack;
  1426.           follow_type = follow_types (follow_type);
  1427.           gdb_assert (type_stack.depth == 0);

  1428.           type_stack = save;
  1429.         }
  1430.         break;
  1431.       default:
  1432.         gdb_assert_not_reached ("unrecognized tp_ value in follow_types");
  1433.       }
  1434.   return follow_type;
  1435. }

  1436. /* This function avoids direct calls to fprintf
  1437.    in the parser generated debug code.  */
  1438. void
  1439. parser_fprintf (FILE *x, const char *y, ...)
  1440. {
  1441.   va_list args;

  1442.   va_start (args, y);
  1443.   if (x == stderr)
  1444.     vfprintf_unfiltered (gdb_stderr, y, args);
  1445.   else
  1446.     {
  1447.       fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
  1448.       vfprintf_unfiltered (gdb_stderr, y, args);
  1449.     }
  1450.   va_end (args);
  1451. }

  1452. /* Implementation of the exp_descriptor method operator_check.  */

  1453. int
  1454. operator_check_standard (struct expression *exp, int pos,
  1455.                          int (*objfile_func) (struct objfile *objfile,
  1456.                                               void *data),
  1457.                          void *data)
  1458. {
  1459.   const union exp_element *const elts = exp->elts;
  1460.   struct type *type = NULL;
  1461.   struct objfile *objfile = NULL;

  1462.   /* Extended operators should have been already handled by exp_descriptor
  1463.      iterate method of its specific language.  */
  1464.   gdb_assert (elts[pos].opcode < OP_EXTENDED0);

  1465.   /* Track the callers of write_exp_elt_type for this table.  */

  1466.   switch (elts[pos].opcode)
  1467.     {
  1468.     case BINOP_VAL:
  1469.     case OP_COMPLEX:
  1470.     case OP_DECFLOAT:
  1471.     case OP_DOUBLE:
  1472.     case OP_LONG:
  1473.     case OP_SCOPE:
  1474.     case OP_TYPE:
  1475.     case UNOP_CAST:
  1476.     case UNOP_MAX:
  1477.     case UNOP_MEMVAL:
  1478.     case UNOP_MIN:
  1479.       type = elts[pos + 1].type;
  1480.       break;

  1481.     case TYPE_INSTANCE:
  1482.       {
  1483.         LONGEST arg, nargs = elts[pos + 1].longconst;

  1484.         for (arg = 0; arg < nargs; arg++)
  1485.           {
  1486.             struct type *type = elts[pos + 2 + arg].type;
  1487.             struct objfile *objfile = TYPE_OBJFILE (type);

  1488.             if (objfile && (*objfile_func) (objfile, data))
  1489.               return 1;
  1490.           }
  1491.       }
  1492.       break;

  1493.     case UNOP_MEMVAL_TLS:
  1494.       objfile = elts[pos + 1].objfile;
  1495.       type = elts[pos + 2].type;
  1496.       break;

  1497.     case OP_VAR_VALUE:
  1498.       {
  1499.         const struct block *const block = elts[pos + 1].block;
  1500.         const struct symbol *const symbol = elts[pos + 2].symbol;

  1501.         /* Check objfile where the variable itself is placed.
  1502.            SYMBOL_OBJ_SECTION (symbol) may be NULL.  */
  1503.         if ((*objfile_func) (symbol_objfile (symbol), data))
  1504.           return 1;

  1505.         /* Check objfile where is placed the code touching the variable.  */
  1506.         objfile = lookup_objfile_from_block (block);

  1507.         type = SYMBOL_TYPE (symbol);
  1508.       }
  1509.       break;
  1510.     }

  1511.   /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL.  */

  1512.   if (type && TYPE_OBJFILE (type)
  1513.       && (*objfile_func) (TYPE_OBJFILE (type), data))
  1514.     return 1;
  1515.   if (objfile && (*objfile_func) (objfile, data))
  1516.     return 1;

  1517.   return 0;
  1518. }

  1519. /* Call OBJFILE_FUNC for any objfile found being referenced by EXP.
  1520.    OBJFILE_FUNC is never called with NULL OBJFILE.  OBJFILE_FUNC get
  1521.    passed an arbitrary caller supplied DATA pointer.  If OBJFILE_FUNC
  1522.    returns non-zero value then (any other) non-zero value is immediately
  1523.    returned to the caller.  Otherwise zero is returned after iterating
  1524.    through whole EXP.  */

  1525. static int
  1526. exp_iterate (struct expression *exp,
  1527.              int (*objfile_func) (struct objfile *objfile, void *data),
  1528.              void *data)
  1529. {
  1530.   int endpos;

  1531.   for (endpos = exp->nelts; endpos > 0; )
  1532.     {
  1533.       int pos, args, oplen = 0;

  1534.       operator_length (exp, endpos, &oplen, &args);
  1535.       gdb_assert (oplen > 0);

  1536.       pos = endpos - oplen;
  1537.       if (exp->language_defn->la_exp_desc->operator_check (exp, pos,
  1538.                                                            objfile_func, data))
  1539.         return 1;

  1540.       endpos = pos;
  1541.     }

  1542.   return 0;
  1543. }

  1544. /* Helper for exp_uses_objfile.  */

  1545. static int
  1546. exp_uses_objfile_iter (struct objfile *exp_objfile, void *objfile_voidp)
  1547. {
  1548.   struct objfile *objfile = objfile_voidp;

  1549.   if (exp_objfile->separate_debug_objfile_backlink)
  1550.     exp_objfile = exp_objfile->separate_debug_objfile_backlink;

  1551.   return exp_objfile == objfile;
  1552. }

  1553. /* Return 1 if EXP uses OBJFILE (and will become dangling when OBJFILE
  1554.    is unloaded), otherwise return 0.  OBJFILE must not be a separate debug info
  1555.    file.  */

  1556. int
  1557. exp_uses_objfile (struct expression *exp, struct objfile *objfile)
  1558. {
  1559.   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);

  1560.   return exp_iterate (exp, exp_uses_objfile_iter, objfile);
  1561. }

  1562. /* See definition in parser-defs.h.  */

  1563. void
  1564. increase_expout_size (struct parser_state *ps, size_t lenelt)
  1565. {
  1566.   if ((ps->expout_ptr + lenelt) >= ps->expout_size)
  1567.     {
  1568.       ps->expout_size = max (ps->expout_size * 2,
  1569.                              ps->expout_ptr + lenelt + 10);
  1570.       ps->expout = (struct expression *)
  1571.         xrealloc (ps->expout, (sizeof (struct expression)
  1572.                                + EXP_ELEM_TO_BYTES (ps->expout_size)));
  1573.     }
  1574. }

  1575. void
  1576. _initialize_parse (void)
  1577. {
  1578.   type_stack.size = 0;
  1579.   type_stack.depth = 0;
  1580.   type_stack.elements = NULL;

  1581.   add_setshow_zuinteger_cmd ("expression", class_maintenance,
  1582.                              &expressiondebug,
  1583.                              _("Set expression debugging."),
  1584.                              _("Show expression debugging."),
  1585.                              _("When non-zero, the internal representation "
  1586.                                "of expressions will be printed."),
  1587.                              NULL,
  1588.                              show_expressiondebug,
  1589.                              &setdebuglist, &showdebuglist);
  1590.   add_setshow_boolean_cmd ("parser", class_maintenance,
  1591.                             &parser_debug,
  1592.                            _("Set parser debugging."),
  1593.                            _("Show parser debugging."),
  1594.                            _("When non-zero, expression parser "
  1595.                              "tracing will be enabled."),
  1596.                             NULL,
  1597.                             show_parserdebug,
  1598.                             &setdebuglist, &showdebuglist);
  1599. }