xref: /dflybsd-src/contrib/gdb-7/gdb/parse.c (revision c50c785cb49e9377ca78104c5540c7b33f768771)
15796c8dcSSimon Schubert /* Parse expressions for GDB.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4*c50c785cSJohn Marino    1998, 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2009, 2010, 2011
55796c8dcSSimon Schubert    Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    Modified from expread.y by the Department of Computer Science at the
85796c8dcSSimon Schubert    State University of New York at Buffalo, 1991.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This file is part of GDB.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
135796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
145796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
155796c8dcSSimon Schubert    (at your option) any later version.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
185796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
195796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
205796c8dcSSimon Schubert    GNU General Public License for more details.
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
235796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
245796c8dcSSimon Schubert 
255796c8dcSSimon Schubert /* Parse an expression from text in a string,
265796c8dcSSimon Schubert    and return the result as a  struct expression  pointer.
275796c8dcSSimon Schubert    That structure contains arithmetic operations in reverse polish,
285796c8dcSSimon Schubert    with constants represented by operations that are followed by special data.
295796c8dcSSimon Schubert    See expression.h for the details of the format.
305796c8dcSSimon Schubert    What is important here is that it can be built up sequentially
315796c8dcSSimon Schubert    during the process of parsing; the lower levels of the tree always
325796c8dcSSimon Schubert    come first in the result.  */
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert #include "defs.h"
35cf7f2e2dSJohn Marino #include <ctype.h>
365796c8dcSSimon Schubert #include "arch-utils.h"
375796c8dcSSimon Schubert #include "gdb_string.h"
385796c8dcSSimon Schubert #include "symtab.h"
395796c8dcSSimon Schubert #include "gdbtypes.h"
405796c8dcSSimon Schubert #include "frame.h"
415796c8dcSSimon Schubert #include "expression.h"
425796c8dcSSimon Schubert #include "value.h"
435796c8dcSSimon Schubert #include "command.h"
445796c8dcSSimon Schubert #include "language.h"
455796c8dcSSimon Schubert #include "f-lang.h"
465796c8dcSSimon Schubert #include "parser-defs.h"
475796c8dcSSimon Schubert #include "gdbcmd.h"
485796c8dcSSimon Schubert #include "symfile.h"		/* for overlay functions */
495796c8dcSSimon Schubert #include "inferior.h"
505796c8dcSSimon Schubert #include "doublest.h"
515796c8dcSSimon Schubert #include "gdb_assert.h"
525796c8dcSSimon Schubert #include "block.h"
535796c8dcSSimon Schubert #include "source.h"
545796c8dcSSimon Schubert #include "objfiles.h"
555796c8dcSSimon Schubert #include "exceptions.h"
565796c8dcSSimon Schubert #include "user-regs.h"
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert /* Standard set of definitions for printing, dumping, prefixifying,
595796c8dcSSimon Schubert  * and evaluating expressions.  */
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert const struct exp_descriptor exp_descriptor_standard =
625796c8dcSSimon Schubert   {
635796c8dcSSimon Schubert     print_subexp_standard,
645796c8dcSSimon Schubert     operator_length_standard,
65cf7f2e2dSJohn Marino     operator_check_standard,
665796c8dcSSimon Schubert     op_name_standard,
675796c8dcSSimon Schubert     dump_subexp_body_standard,
685796c8dcSSimon Schubert     evaluate_subexp_standard
695796c8dcSSimon Schubert   };
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert /* Global variables declared in parser-defs.h (and commented there).  */
725796c8dcSSimon Schubert struct expression *expout;
735796c8dcSSimon Schubert int expout_size;
745796c8dcSSimon Schubert int expout_ptr;
755796c8dcSSimon Schubert struct block *expression_context_block;
765796c8dcSSimon Schubert CORE_ADDR expression_context_pc;
775796c8dcSSimon Schubert struct block *innermost_block;
785796c8dcSSimon Schubert int arglist_len;
795796c8dcSSimon Schubert union type_stack_elt *type_stack;
805796c8dcSSimon Schubert int type_stack_depth, type_stack_size;
815796c8dcSSimon Schubert char *lexptr;
825796c8dcSSimon Schubert char *prev_lexptr;
835796c8dcSSimon Schubert int paren_depth;
845796c8dcSSimon Schubert int comma_terminates;
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert /* True if parsing an expression to find a field reference.  This is
875796c8dcSSimon Schubert    only used by completion.  */
885796c8dcSSimon Schubert int in_parse_field;
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert /* The index of the last struct expression directly before a '.' or
915796c8dcSSimon Schubert    '->'.  This is set when parsing and is only used when completing a
925796c8dcSSimon Schubert    field name.  It is -1 if no dereference operation was found.  */
935796c8dcSSimon Schubert static int expout_last_struct = -1;
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert /* A temporary buffer for identifiers, so we can null-terminate them.
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert    We allocate this with xrealloc.  parse_exp_1 used to allocate with
985796c8dcSSimon Schubert    alloca, using the size of the whole expression as a conservative
995796c8dcSSimon Schubert    estimate of the space needed.  However, macro expansion can
1005796c8dcSSimon Schubert    introduce names longer than the original expression; there's no
1015796c8dcSSimon Schubert    practical way to know beforehand how large that might be.  */
1025796c8dcSSimon Schubert char *namecopy;
1035796c8dcSSimon Schubert size_t namecopy_size;
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert static int expressiondebug = 0;
1065796c8dcSSimon Schubert static void
1075796c8dcSSimon Schubert show_expressiondebug (struct ui_file *file, int from_tty,
1085796c8dcSSimon Schubert 		      struct cmd_list_element *c, const char *value)
1095796c8dcSSimon Schubert {
1105796c8dcSSimon Schubert   fprintf_filtered (file, _("Expression debugging is %s.\n"), value);
1115796c8dcSSimon Schubert }
1125796c8dcSSimon Schubert 
113cf7f2e2dSJohn Marino 
114cf7f2e2dSJohn Marino /* Non-zero if an expression parser should set yydebug.  */
115cf7f2e2dSJohn Marino int parser_debug;
116cf7f2e2dSJohn Marino 
117cf7f2e2dSJohn Marino static void
118cf7f2e2dSJohn Marino show_parserdebug (struct ui_file *file, int from_tty,
119cf7f2e2dSJohn Marino 		  struct cmd_list_element *c, const char *value)
120cf7f2e2dSJohn Marino {
121cf7f2e2dSJohn Marino   fprintf_filtered (file, _("Parser debugging is %s.\n"), value);
122cf7f2e2dSJohn Marino }
123cf7f2e2dSJohn Marino 
124cf7f2e2dSJohn Marino 
1255796c8dcSSimon Schubert static void free_funcalls (void *ignore);
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert static int prefixify_expression (struct expression *);
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert static int prefixify_subexp (struct expression *, struct expression *, int,
1305796c8dcSSimon Schubert 			     int);
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert static struct expression *parse_exp_in_context (char **, struct block *, int,
1335796c8dcSSimon Schubert 						int, int *);
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert void _initialize_parse (void);
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert /* Data structure for saving values of arglist_len for function calls whose
1385796c8dcSSimon Schubert    arguments contain other function calls.  */
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert struct funcall
1415796c8dcSSimon Schubert   {
1425796c8dcSSimon Schubert     struct funcall *next;
1435796c8dcSSimon Schubert     int arglist_len;
1445796c8dcSSimon Schubert   };
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert static struct funcall *funcall_chain;
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert /* Begin counting arguments for a function call,
1495796c8dcSSimon Schubert    saving the data about any containing call.  */
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert void
1525796c8dcSSimon Schubert start_arglist (void)
1535796c8dcSSimon Schubert {
1545796c8dcSSimon Schubert   struct funcall *new;
1555796c8dcSSimon Schubert 
1565796c8dcSSimon Schubert   new = (struct funcall *) xmalloc (sizeof (struct funcall));
1575796c8dcSSimon Schubert   new->next = funcall_chain;
1585796c8dcSSimon Schubert   new->arglist_len = arglist_len;
1595796c8dcSSimon Schubert   arglist_len = 0;
1605796c8dcSSimon Schubert   funcall_chain = new;
1615796c8dcSSimon Schubert }
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert /* Return the number of arguments in a function call just terminated,
1645796c8dcSSimon Schubert    and restore the data for the containing function call.  */
1655796c8dcSSimon Schubert 
1665796c8dcSSimon Schubert int
1675796c8dcSSimon Schubert end_arglist (void)
1685796c8dcSSimon Schubert {
1695796c8dcSSimon Schubert   int val = arglist_len;
1705796c8dcSSimon Schubert   struct funcall *call = funcall_chain;
171cf7f2e2dSJohn Marino 
1725796c8dcSSimon Schubert   funcall_chain = call->next;
1735796c8dcSSimon Schubert   arglist_len = call->arglist_len;
1745796c8dcSSimon Schubert   xfree (call);
1755796c8dcSSimon Schubert   return val;
1765796c8dcSSimon Schubert }
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert /* Free everything in the funcall chain.
1795796c8dcSSimon Schubert    Used when there is an error inside parsing.  */
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert static void
1825796c8dcSSimon Schubert free_funcalls (void *ignore)
1835796c8dcSSimon Schubert {
1845796c8dcSSimon Schubert   struct funcall *call, *next;
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert   for (call = funcall_chain; call; call = next)
1875796c8dcSSimon Schubert     {
1885796c8dcSSimon Schubert       next = call->next;
1895796c8dcSSimon Schubert       xfree (call);
1905796c8dcSSimon Schubert     }
1915796c8dcSSimon Schubert }
1925796c8dcSSimon Schubert 
1935796c8dcSSimon Schubert /* This page contains the functions for adding data to the  struct expression
1945796c8dcSSimon Schubert    being constructed.  */
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert /* Add one element to the end of the expression.  */
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
199*c50c785cSJohn Marino    a register through here.  */
2005796c8dcSSimon Schubert 
2015796c8dcSSimon Schubert void
2025796c8dcSSimon Schubert write_exp_elt (union exp_element expelt)
2035796c8dcSSimon Schubert {
2045796c8dcSSimon Schubert   if (expout_ptr >= expout_size)
2055796c8dcSSimon Schubert     {
2065796c8dcSSimon Schubert       expout_size *= 2;
2075796c8dcSSimon Schubert       expout = (struct expression *)
2085796c8dcSSimon Schubert 	xrealloc ((char *) expout, sizeof (struct expression)
2095796c8dcSSimon Schubert 		  + EXP_ELEM_TO_BYTES (expout_size));
2105796c8dcSSimon Schubert     }
2115796c8dcSSimon Schubert   expout->elts[expout_ptr++] = expelt;
2125796c8dcSSimon Schubert }
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert void
2155796c8dcSSimon Schubert write_exp_elt_opcode (enum exp_opcode expelt)
2165796c8dcSSimon Schubert {
2175796c8dcSSimon Schubert   union exp_element tmp;
218cf7f2e2dSJohn Marino 
2195796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2205796c8dcSSimon Schubert   tmp.opcode = expelt;
2215796c8dcSSimon Schubert   write_exp_elt (tmp);
2225796c8dcSSimon Schubert }
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert void
2255796c8dcSSimon Schubert write_exp_elt_sym (struct symbol *expelt)
2265796c8dcSSimon Schubert {
2275796c8dcSSimon Schubert   union exp_element tmp;
228cf7f2e2dSJohn Marino 
2295796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2305796c8dcSSimon Schubert   tmp.symbol = expelt;
2315796c8dcSSimon Schubert   write_exp_elt (tmp);
2325796c8dcSSimon Schubert }
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert void
2355796c8dcSSimon Schubert write_exp_elt_block (struct block *b)
2365796c8dcSSimon Schubert {
2375796c8dcSSimon Schubert   union exp_element tmp;
238cf7f2e2dSJohn Marino 
2395796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2405796c8dcSSimon Schubert   tmp.block = b;
2415796c8dcSSimon Schubert   write_exp_elt (tmp);
2425796c8dcSSimon Schubert }
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert void
2455796c8dcSSimon Schubert write_exp_elt_objfile (struct objfile *objfile)
2465796c8dcSSimon Schubert {
2475796c8dcSSimon Schubert   union exp_element tmp;
248cf7f2e2dSJohn Marino 
2495796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2505796c8dcSSimon Schubert   tmp.objfile = objfile;
2515796c8dcSSimon Schubert   write_exp_elt (tmp);
2525796c8dcSSimon Schubert }
2535796c8dcSSimon Schubert 
2545796c8dcSSimon Schubert void
2555796c8dcSSimon Schubert write_exp_elt_longcst (LONGEST expelt)
2565796c8dcSSimon Schubert {
2575796c8dcSSimon Schubert   union exp_element tmp;
258cf7f2e2dSJohn Marino 
2595796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2605796c8dcSSimon Schubert   tmp.longconst = expelt;
2615796c8dcSSimon Schubert   write_exp_elt (tmp);
2625796c8dcSSimon Schubert }
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert void
2655796c8dcSSimon Schubert write_exp_elt_dblcst (DOUBLEST expelt)
2665796c8dcSSimon Schubert {
2675796c8dcSSimon Schubert   union exp_element tmp;
268cf7f2e2dSJohn Marino 
2695796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2705796c8dcSSimon Schubert   tmp.doubleconst = expelt;
2715796c8dcSSimon Schubert   write_exp_elt (tmp);
2725796c8dcSSimon Schubert }
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert void
2755796c8dcSSimon Schubert write_exp_elt_decfloatcst (gdb_byte expelt[16])
2765796c8dcSSimon Schubert {
2775796c8dcSSimon Schubert   union exp_element tmp;
2785796c8dcSSimon Schubert   int index;
2795796c8dcSSimon Schubert 
2805796c8dcSSimon Schubert   for (index = 0; index < 16; index++)
2815796c8dcSSimon Schubert     tmp.decfloatconst[index] = expelt[index];
2825796c8dcSSimon Schubert 
2835796c8dcSSimon Schubert   write_exp_elt (tmp);
2845796c8dcSSimon Schubert }
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert void
2875796c8dcSSimon Schubert write_exp_elt_type (struct type *expelt)
2885796c8dcSSimon Schubert {
2895796c8dcSSimon Schubert   union exp_element tmp;
290cf7f2e2dSJohn Marino 
2915796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2925796c8dcSSimon Schubert   tmp.type = expelt;
2935796c8dcSSimon Schubert   write_exp_elt (tmp);
2945796c8dcSSimon Schubert }
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert void
2975796c8dcSSimon Schubert write_exp_elt_intern (struct internalvar *expelt)
2985796c8dcSSimon Schubert {
2995796c8dcSSimon Schubert   union exp_element tmp;
300cf7f2e2dSJohn Marino 
3015796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
3025796c8dcSSimon Schubert   tmp.internalvar = expelt;
3035796c8dcSSimon Schubert   write_exp_elt (tmp);
3045796c8dcSSimon Schubert }
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert /* Add a string constant to the end of the expression.
3075796c8dcSSimon Schubert 
3085796c8dcSSimon Schubert    String constants are stored by first writing an expression element
3095796c8dcSSimon Schubert    that contains the length of the string, then stuffing the string
3105796c8dcSSimon Schubert    constant itself into however many expression elements are needed
3115796c8dcSSimon Schubert    to hold it, and then writing another expression element that contains
312*c50c785cSJohn Marino    the length of the string.  I.e. an expression element at each end of
3135796c8dcSSimon Schubert    the string records the string length, so you can skip over the
3145796c8dcSSimon Schubert    expression elements containing the actual string bytes from either
3155796c8dcSSimon Schubert    end of the string.  Note that this also allows gdb to handle
3165796c8dcSSimon Schubert    strings with embedded null bytes, as is required for some languages.
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert    Don't be fooled by the fact that the string is null byte terminated,
3195796c8dcSSimon Schubert    this is strictly for the convenience of debugging gdb itself.
3205796c8dcSSimon Schubert    Gdb does not depend up the string being null terminated, since the
3215796c8dcSSimon Schubert    actual length is recorded in expression elements at each end of the
3225796c8dcSSimon Schubert    string.  The null byte is taken into consideration when computing how
3235796c8dcSSimon Schubert    many expression elements are required to hold the string constant, of
3245796c8dcSSimon Schubert    course.  */
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert 
3275796c8dcSSimon Schubert void
3285796c8dcSSimon Schubert write_exp_string (struct stoken str)
3295796c8dcSSimon Schubert {
3305796c8dcSSimon Schubert   int len = str.length;
3315796c8dcSSimon Schubert   int lenelt;
3325796c8dcSSimon Schubert   char *strdata;
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert   /* Compute the number of expression elements required to hold the string
3355796c8dcSSimon Schubert      (including a null byte terminator), along with one expression element
3365796c8dcSSimon Schubert      at each end to record the actual string length (not including the
3375796c8dcSSimon Schubert      null byte terminator).  */
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert   lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert   /* Ensure that we have enough available expression elements to store
3425796c8dcSSimon Schubert      everything.  */
3435796c8dcSSimon Schubert 
3445796c8dcSSimon Schubert   if ((expout_ptr + lenelt) >= expout_size)
3455796c8dcSSimon Schubert     {
3465796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
3475796c8dcSSimon Schubert       expout = (struct expression *)
3485796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
3495796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
3505796c8dcSSimon Schubert     }
3515796c8dcSSimon Schubert 
3525796c8dcSSimon Schubert   /* Write the leading length expression element (which advances the current
3535796c8dcSSimon Schubert      expression element index), then write the string constant followed by a
3545796c8dcSSimon Schubert      terminating null byte, and then write the trailing length expression
3555796c8dcSSimon Schubert      element.  */
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) len);
3585796c8dcSSimon Schubert   strdata = (char *) &expout->elts[expout_ptr];
3595796c8dcSSimon Schubert   memcpy (strdata, str.ptr, len);
3605796c8dcSSimon Schubert   *(strdata + len) = '\0';
3615796c8dcSSimon Schubert   expout_ptr += lenelt - 2;
3625796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) len);
3635796c8dcSSimon Schubert }
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert /* Add a vector of string constants to the end of the expression.
3665796c8dcSSimon Schubert 
3675796c8dcSSimon Schubert    This adds an OP_STRING operation, but encodes the contents
3685796c8dcSSimon Schubert    differently from write_exp_string.  The language is expected to
3695796c8dcSSimon Schubert    handle evaluation of this expression itself.
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert    After the usual OP_STRING header, TYPE is written into the
3725796c8dcSSimon Schubert    expression as a long constant.  The interpretation of this field is
3735796c8dcSSimon Schubert    up to the language evaluator.
3745796c8dcSSimon Schubert 
3755796c8dcSSimon Schubert    Next, each string in VEC is written.  The length is written as a
3765796c8dcSSimon Schubert    long constant, followed by the contents of the string.  */
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert void
3795796c8dcSSimon Schubert write_exp_string_vector (int type, struct stoken_vector *vec)
3805796c8dcSSimon Schubert {
3815796c8dcSSimon Schubert   int i, n_slots, len;
3825796c8dcSSimon Schubert 
3835796c8dcSSimon Schubert   /* Compute the size.  We compute the size in number of slots to
3845796c8dcSSimon Schubert      avoid issues with string padding.  */
3855796c8dcSSimon Schubert   n_slots = 0;
3865796c8dcSSimon Schubert   for (i = 0; i < vec->len; ++i)
3875796c8dcSSimon Schubert     {
3885796c8dcSSimon Schubert       /* One slot for the length of this element, plus the number of
3895796c8dcSSimon Schubert 	 slots needed for this string.  */
3905796c8dcSSimon Schubert       n_slots += 1 + BYTES_TO_EXP_ELEM (vec->tokens[i].length);
3915796c8dcSSimon Schubert     }
3925796c8dcSSimon Schubert 
3935796c8dcSSimon Schubert   /* One more slot for the type of the string.  */
3945796c8dcSSimon Schubert   ++n_slots;
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert   /* Now compute a phony string length.  */
3975796c8dcSSimon Schubert   len = EXP_ELEM_TO_BYTES (n_slots) - 1;
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert   n_slots += 4;
4005796c8dcSSimon Schubert   if ((expout_ptr + n_slots) >= expout_size)
4015796c8dcSSimon Schubert     {
4025796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + n_slots + 10);
4035796c8dcSSimon Schubert       expout = (struct expression *)
4045796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
4055796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
4065796c8dcSSimon Schubert     }
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert   write_exp_elt_opcode (OP_STRING);
4095796c8dcSSimon Schubert   write_exp_elt_longcst (len);
4105796c8dcSSimon Schubert   write_exp_elt_longcst (type);
4115796c8dcSSimon Schubert 
4125796c8dcSSimon Schubert   for (i = 0; i < vec->len; ++i)
4135796c8dcSSimon Schubert     {
4145796c8dcSSimon Schubert       write_exp_elt_longcst (vec->tokens[i].length);
4155796c8dcSSimon Schubert       memcpy (&expout->elts[expout_ptr], vec->tokens[i].ptr,
4165796c8dcSSimon Schubert 	      vec->tokens[i].length);
4175796c8dcSSimon Schubert       expout_ptr += BYTES_TO_EXP_ELEM (vec->tokens[i].length);
4185796c8dcSSimon Schubert     }
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   write_exp_elt_longcst (len);
4215796c8dcSSimon Schubert   write_exp_elt_opcode (OP_STRING);
4225796c8dcSSimon Schubert }
4235796c8dcSSimon Schubert 
4245796c8dcSSimon Schubert /* Add a bitstring constant to the end of the expression.
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert    Bitstring constants are stored by first writing an expression element
4275796c8dcSSimon Schubert    that contains the length of the bitstring (in bits), then stuffing the
4285796c8dcSSimon Schubert    bitstring constant itself into however many expression elements are
4295796c8dcSSimon Schubert    needed to hold it, and then writing another expression element that
430*c50c785cSJohn Marino    contains the length of the bitstring.  I.e. an expression element at
4315796c8dcSSimon Schubert    each end of the bitstring records the bitstring length, so you can skip
4325796c8dcSSimon Schubert    over the expression elements containing the actual bitstring bytes from
4335796c8dcSSimon Schubert    either end of the bitstring.  */
4345796c8dcSSimon Schubert 
4355796c8dcSSimon Schubert void
4365796c8dcSSimon Schubert write_exp_bitstring (struct stoken str)
4375796c8dcSSimon Schubert {
4385796c8dcSSimon Schubert   int bits = str.length;	/* length in bits */
4395796c8dcSSimon Schubert   int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
4405796c8dcSSimon Schubert   int lenelt;
4415796c8dcSSimon Schubert   char *strdata;
4425796c8dcSSimon Schubert 
4435796c8dcSSimon Schubert   /* Compute the number of expression elements required to hold the bitstring,
4445796c8dcSSimon Schubert      along with one expression element at each end to record the actual
4455796c8dcSSimon Schubert      bitstring length in bits.  */
4465796c8dcSSimon Schubert 
4475796c8dcSSimon Schubert   lenelt = 2 + BYTES_TO_EXP_ELEM (len);
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert   /* Ensure that we have enough available expression elements to store
4505796c8dcSSimon Schubert      everything.  */
4515796c8dcSSimon Schubert 
4525796c8dcSSimon Schubert   if ((expout_ptr + lenelt) >= expout_size)
4535796c8dcSSimon Schubert     {
4545796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
4555796c8dcSSimon Schubert       expout = (struct expression *)
4565796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
4575796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
4585796c8dcSSimon Schubert     }
4595796c8dcSSimon Schubert 
4605796c8dcSSimon Schubert   /* Write the leading length expression element (which advances the current
4615796c8dcSSimon Schubert      expression element index), then write the bitstring constant, and then
4625796c8dcSSimon Schubert      write the trailing length expression element.  */
4635796c8dcSSimon Schubert 
4645796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) bits);
4655796c8dcSSimon Schubert   strdata = (char *) &expout->elts[expout_ptr];
4665796c8dcSSimon Schubert   memcpy (strdata, str.ptr, len);
4675796c8dcSSimon Schubert   expout_ptr += lenelt - 2;
4685796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) bits);
4695796c8dcSSimon Schubert }
4705796c8dcSSimon Schubert 
4715796c8dcSSimon Schubert /* Add the appropriate elements for a minimal symbol to the end of
4725796c8dcSSimon Schubert    the expression.  */
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert void
4755796c8dcSSimon Schubert write_exp_msymbol (struct minimal_symbol *msymbol)
4765796c8dcSSimon Schubert {
4775796c8dcSSimon Schubert   struct objfile *objfile = msymbol_objfile (msymbol);
4785796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
4795796c8dcSSimon Schubert 
4805796c8dcSSimon Schubert   CORE_ADDR addr = SYMBOL_VALUE_ADDRESS (msymbol);
4815796c8dcSSimon Schubert   struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
4825796c8dcSSimon Schubert   enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
4835796c8dcSSimon Schubert   CORE_ADDR pc;
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert   /* The minimal symbol might point to a function descriptor;
4865796c8dcSSimon Schubert      resolve it to the actual code address instead.  */
4875796c8dcSSimon Schubert   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
4885796c8dcSSimon Schubert   if (pc != addr)
4895796c8dcSSimon Schubert     {
490*c50c785cSJohn Marino       struct minimal_symbol *ifunc_msym = lookup_minimal_symbol_by_pc (pc);
491*c50c785cSJohn Marino 
4925796c8dcSSimon Schubert       /* In this case, assume we have a code symbol instead of
4935796c8dcSSimon Schubert 	 a data symbol.  */
494*c50c785cSJohn Marino 
495*c50c785cSJohn Marino       if (ifunc_msym != NULL && MSYMBOL_TYPE (ifunc_msym) == mst_text_gnu_ifunc
496*c50c785cSJohn Marino 	  && SYMBOL_VALUE_ADDRESS (ifunc_msym) == pc)
497*c50c785cSJohn Marino 	{
498*c50c785cSJohn Marino 	  /* A function descriptor has been resolved but PC is still in the
499*c50c785cSJohn Marino 	     STT_GNU_IFUNC resolver body (such as because inferior does not
500*c50c785cSJohn Marino 	     run to be able to call it).  */
501*c50c785cSJohn Marino 
502*c50c785cSJohn Marino 	  type = mst_text_gnu_ifunc;
503*c50c785cSJohn Marino 	}
504*c50c785cSJohn Marino       else
5055796c8dcSSimon Schubert 	type = mst_text;
5065796c8dcSSimon Schubert       section = NULL;
5075796c8dcSSimon Schubert       addr = pc;
5085796c8dcSSimon Schubert     }
5095796c8dcSSimon Schubert 
5105796c8dcSSimon Schubert   if (overlay_debugging)
5115796c8dcSSimon Schubert     addr = symbol_overlayed_address (addr, section);
5125796c8dcSSimon Schubert 
5135796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LONG);
5145796c8dcSSimon Schubert   /* Let's make the type big enough to hold a 64-bit address.  */
5155796c8dcSSimon Schubert   write_exp_elt_type (objfile_type (objfile)->builtin_core_addr);
5165796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) addr);
5175796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LONG);
5185796c8dcSSimon Schubert 
5195796c8dcSSimon Schubert   if (section && section->the_bfd_section->flags & SEC_THREAD_LOCAL)
5205796c8dcSSimon Schubert     {
5215796c8dcSSimon Schubert       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
5225796c8dcSSimon Schubert       write_exp_elt_objfile (objfile);
5235796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_tls_symbol);
5245796c8dcSSimon Schubert       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
5255796c8dcSSimon Schubert       return;
5265796c8dcSSimon Schubert     }
5275796c8dcSSimon Schubert 
5285796c8dcSSimon Schubert   write_exp_elt_opcode (UNOP_MEMVAL);
5295796c8dcSSimon Schubert   switch (type)
5305796c8dcSSimon Schubert     {
5315796c8dcSSimon Schubert     case mst_text:
5325796c8dcSSimon Schubert     case mst_file_text:
5335796c8dcSSimon Schubert     case mst_solib_trampoline:
5345796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_text_symbol);
5355796c8dcSSimon Schubert       break;
5365796c8dcSSimon Schubert 
537*c50c785cSJohn Marino     case mst_text_gnu_ifunc:
538*c50c785cSJohn Marino       write_exp_elt_type (objfile_type (objfile)
539*c50c785cSJohn Marino 					       ->nodebug_text_gnu_ifunc_symbol);
540*c50c785cSJohn Marino       break;
541*c50c785cSJohn Marino 
5425796c8dcSSimon Schubert     case mst_data:
5435796c8dcSSimon Schubert     case mst_file_data:
5445796c8dcSSimon Schubert     case mst_bss:
5455796c8dcSSimon Schubert     case mst_file_bss:
5465796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_data_symbol);
5475796c8dcSSimon Schubert       break;
5485796c8dcSSimon Schubert 
549*c50c785cSJohn Marino     case mst_slot_got_plt:
550*c50c785cSJohn Marino       write_exp_elt_type (objfile_type (objfile)->nodebug_got_plt_symbol);
551*c50c785cSJohn Marino       break;
552*c50c785cSJohn Marino 
5535796c8dcSSimon Schubert     default:
5545796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_unknown_symbol);
5555796c8dcSSimon Schubert       break;
5565796c8dcSSimon Schubert     }
5575796c8dcSSimon Schubert   write_exp_elt_opcode (UNOP_MEMVAL);
5585796c8dcSSimon Schubert }
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert /* Mark the current index as the starting location of a structure
5615796c8dcSSimon Schubert    expression.  This is used when completing on field names.  */
5625796c8dcSSimon Schubert 
5635796c8dcSSimon Schubert void
5645796c8dcSSimon Schubert mark_struct_expression (void)
5655796c8dcSSimon Schubert {
5665796c8dcSSimon Schubert   expout_last_struct = expout_ptr;
5675796c8dcSSimon Schubert }
5685796c8dcSSimon Schubert 
5695796c8dcSSimon Schubert 
5705796c8dcSSimon Schubert /* Recognize tokens that start with '$'.  These include:
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert    $regname     A native register name or a "standard
5735796c8dcSSimon Schubert    register name".
5745796c8dcSSimon Schubert 
5755796c8dcSSimon Schubert    $variable    A convenience variable with a name chosen
5765796c8dcSSimon Schubert    by the user.
5775796c8dcSSimon Schubert 
5785796c8dcSSimon Schubert    $digits              Value history with index <digits>, starting
5795796c8dcSSimon Schubert    from the first value which has index 1.
5805796c8dcSSimon Schubert 
5815796c8dcSSimon Schubert    $$digits     Value history with index <digits> relative
582*c50c785cSJohn Marino    to the last value.  I.e. $$0 is the last
5835796c8dcSSimon Schubert    value, $$1 is the one previous to that, $$2
5845796c8dcSSimon Schubert    is the one previous to $$1, etc.
5855796c8dcSSimon Schubert 
5865796c8dcSSimon Schubert    $ | $0 | $$0 The last value in the value history.
5875796c8dcSSimon Schubert 
5885796c8dcSSimon Schubert    $$           An abbreviation for the second to the last
589*c50c785cSJohn Marino    value in the value history, I.e. $$1  */
5905796c8dcSSimon Schubert 
5915796c8dcSSimon Schubert void
5925796c8dcSSimon Schubert write_dollar_variable (struct stoken str)
5935796c8dcSSimon Schubert {
5945796c8dcSSimon Schubert   struct symbol *sym = NULL;
5955796c8dcSSimon Schubert   struct minimal_symbol *msym = NULL;
5965796c8dcSSimon Schubert   struct internalvar *isym = NULL;
5975796c8dcSSimon Schubert 
5985796c8dcSSimon Schubert   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
5995796c8dcSSimon Schubert      and $$digits (equivalent to $<-digits> if you could type that).  */
6005796c8dcSSimon Schubert 
6015796c8dcSSimon Schubert   int negate = 0;
6025796c8dcSSimon Schubert   int i = 1;
6035796c8dcSSimon Schubert   /* Double dollar means negate the number and add -1 as well.
6045796c8dcSSimon Schubert      Thus $$ alone means -1.  */
6055796c8dcSSimon Schubert   if (str.length >= 2 && str.ptr[1] == '$')
6065796c8dcSSimon Schubert     {
6075796c8dcSSimon Schubert       negate = 1;
6085796c8dcSSimon Schubert       i = 2;
6095796c8dcSSimon Schubert     }
6105796c8dcSSimon Schubert   if (i == str.length)
6115796c8dcSSimon Schubert     {
612*c50c785cSJohn Marino       /* Just dollars (one or two).  */
6135796c8dcSSimon Schubert       i = -negate;
6145796c8dcSSimon Schubert       goto handle_last;
6155796c8dcSSimon Schubert     }
6165796c8dcSSimon Schubert   /* Is the rest of the token digits?  */
6175796c8dcSSimon Schubert   for (; i < str.length; i++)
6185796c8dcSSimon Schubert     if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
6195796c8dcSSimon Schubert       break;
6205796c8dcSSimon Schubert   if (i == str.length)
6215796c8dcSSimon Schubert     {
6225796c8dcSSimon Schubert       i = atoi (str.ptr + 1 + negate);
6235796c8dcSSimon Schubert       if (negate)
6245796c8dcSSimon Schubert 	i = -i;
6255796c8dcSSimon Schubert       goto handle_last;
6265796c8dcSSimon Schubert     }
6275796c8dcSSimon Schubert 
6285796c8dcSSimon Schubert   /* Handle tokens that refer to machine registers:
6295796c8dcSSimon Schubert      $ followed by a register name.  */
6305796c8dcSSimon Schubert   i = user_reg_map_name_to_regnum (parse_gdbarch,
6315796c8dcSSimon Schubert 				   str.ptr + 1, str.length - 1);
6325796c8dcSSimon Schubert   if (i >= 0)
6335796c8dcSSimon Schubert     goto handle_register;
6345796c8dcSSimon Schubert 
6355796c8dcSSimon Schubert   /* Any names starting with $ are probably debugger internal variables.  */
6365796c8dcSSimon Schubert 
6375796c8dcSSimon Schubert   isym = lookup_only_internalvar (copy_name (str) + 1);
6385796c8dcSSimon Schubert   if (isym)
6395796c8dcSSimon Schubert     {
6405796c8dcSSimon Schubert       write_exp_elt_opcode (OP_INTERNALVAR);
6415796c8dcSSimon Schubert       write_exp_elt_intern (isym);
6425796c8dcSSimon Schubert       write_exp_elt_opcode (OP_INTERNALVAR);
6435796c8dcSSimon Schubert       return;
6445796c8dcSSimon Schubert     }
6455796c8dcSSimon Schubert 
6465796c8dcSSimon Schubert   /* On some systems, such as HP-UX and hppa-linux, certain system routines
6475796c8dcSSimon Schubert      have names beginning with $ or $$.  Check for those, first.  */
6485796c8dcSSimon Schubert 
6495796c8dcSSimon Schubert   sym = lookup_symbol (copy_name (str), (struct block *) NULL,
6505796c8dcSSimon Schubert 		       VAR_DOMAIN, (int *) NULL);
6515796c8dcSSimon Schubert   if (sym)
6525796c8dcSSimon Schubert     {
6535796c8dcSSimon Schubert       write_exp_elt_opcode (OP_VAR_VALUE);
6545796c8dcSSimon Schubert       write_exp_elt_block (block_found);	/* set by lookup_symbol */
6555796c8dcSSimon Schubert       write_exp_elt_sym (sym);
6565796c8dcSSimon Schubert       write_exp_elt_opcode (OP_VAR_VALUE);
6575796c8dcSSimon Schubert       return;
6585796c8dcSSimon Schubert     }
6595796c8dcSSimon Schubert   msym = lookup_minimal_symbol (copy_name (str), NULL, NULL);
6605796c8dcSSimon Schubert   if (msym)
6615796c8dcSSimon Schubert     {
6625796c8dcSSimon Schubert       write_exp_msymbol (msym);
6635796c8dcSSimon Schubert       return;
6645796c8dcSSimon Schubert     }
6655796c8dcSSimon Schubert 
6665796c8dcSSimon Schubert   /* Any other names are assumed to be debugger internal variables.  */
6675796c8dcSSimon Schubert 
6685796c8dcSSimon Schubert   write_exp_elt_opcode (OP_INTERNALVAR);
6695796c8dcSSimon Schubert   write_exp_elt_intern (create_internalvar (copy_name (str) + 1));
6705796c8dcSSimon Schubert   write_exp_elt_opcode (OP_INTERNALVAR);
6715796c8dcSSimon Schubert   return;
6725796c8dcSSimon Schubert handle_last:
6735796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LAST);
6745796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) i);
6755796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LAST);
6765796c8dcSSimon Schubert   return;
6775796c8dcSSimon Schubert handle_register:
6785796c8dcSSimon Schubert   write_exp_elt_opcode (OP_REGISTER);
6795796c8dcSSimon Schubert   str.length--;
6805796c8dcSSimon Schubert   str.ptr++;
6815796c8dcSSimon Schubert   write_exp_string (str);
6825796c8dcSSimon Schubert   write_exp_elt_opcode (OP_REGISTER);
6835796c8dcSSimon Schubert   return;
6845796c8dcSSimon Schubert }
6855796c8dcSSimon Schubert 
6865796c8dcSSimon Schubert 
6875796c8dcSSimon Schubert char *
6885796c8dcSSimon Schubert find_template_name_end (char *p)
6895796c8dcSSimon Schubert {
6905796c8dcSSimon Schubert   int depth = 1;
6915796c8dcSSimon Schubert   int just_seen_right = 0;
6925796c8dcSSimon Schubert   int just_seen_colon = 0;
6935796c8dcSSimon Schubert   int just_seen_space = 0;
6945796c8dcSSimon Schubert 
6955796c8dcSSimon Schubert   if (!p || (*p != '<'))
6965796c8dcSSimon Schubert     return 0;
6975796c8dcSSimon Schubert 
6985796c8dcSSimon Schubert   while (*++p)
6995796c8dcSSimon Schubert     {
7005796c8dcSSimon Schubert       switch (*p)
7015796c8dcSSimon Schubert 	{
7025796c8dcSSimon Schubert 	case '\'':
7035796c8dcSSimon Schubert 	case '\"':
7045796c8dcSSimon Schubert 	case '{':
7055796c8dcSSimon Schubert 	case '}':
7065796c8dcSSimon Schubert 	  /* In future, may want to allow these??  */
7075796c8dcSSimon Schubert 	  return 0;
7085796c8dcSSimon Schubert 	case '<':
7095796c8dcSSimon Schubert 	  depth++;		/* start nested template */
7105796c8dcSSimon Schubert 	  if (just_seen_colon || just_seen_right || just_seen_space)
7115796c8dcSSimon Schubert 	    return 0;		/* but not after : or :: or > or space */
7125796c8dcSSimon Schubert 	  break;
7135796c8dcSSimon Schubert 	case '>':
7145796c8dcSSimon Schubert 	  if (just_seen_colon || just_seen_right)
7155796c8dcSSimon Schubert 	    return 0;		/* end a (nested?) template */
7165796c8dcSSimon Schubert 	  just_seen_right = 1;	/* but not after : or :: */
7175796c8dcSSimon Schubert 	  if (--depth == 0)	/* also disallow >>, insist on > > */
7185796c8dcSSimon Schubert 	    return ++p;		/* if outermost ended, return */
7195796c8dcSSimon Schubert 	  break;
7205796c8dcSSimon Schubert 	case ':':
7215796c8dcSSimon Schubert 	  if (just_seen_space || (just_seen_colon > 1))
7225796c8dcSSimon Schubert 	    return 0;		/* nested class spec coming up */
7235796c8dcSSimon Schubert 	  just_seen_colon++;	/* we allow :: but not :::: */
7245796c8dcSSimon Schubert 	  break;
7255796c8dcSSimon Schubert 	case ' ':
7265796c8dcSSimon Schubert 	  break;
7275796c8dcSSimon Schubert 	default:
7285796c8dcSSimon Schubert 	  if (!((*p >= 'a' && *p <= 'z') ||	/* allow token chars */
7295796c8dcSSimon Schubert 		(*p >= 'A' && *p <= 'Z') ||
7305796c8dcSSimon Schubert 		(*p >= '0' && *p <= '9') ||
7315796c8dcSSimon Schubert 		(*p == '_') || (*p == ',') ||	/* commas for template args */
7325796c8dcSSimon Schubert 		(*p == '&') || (*p == '*') ||	/* pointer and ref types */
7335796c8dcSSimon Schubert 		(*p == '(') || (*p == ')') ||	/* function types */
7345796c8dcSSimon Schubert 		(*p == '[') || (*p == ']')))	/* array types */
7355796c8dcSSimon Schubert 	    return 0;
7365796c8dcSSimon Schubert 	}
7375796c8dcSSimon Schubert       if (*p != ' ')
7385796c8dcSSimon Schubert 	just_seen_space = 0;
7395796c8dcSSimon Schubert       if (*p != ':')
7405796c8dcSSimon Schubert 	just_seen_colon = 0;
7415796c8dcSSimon Schubert       if (*p != '>')
7425796c8dcSSimon Schubert 	just_seen_right = 0;
7435796c8dcSSimon Schubert     }
7445796c8dcSSimon Schubert   return 0;
7455796c8dcSSimon Schubert }
7465796c8dcSSimon Schubert 
7475796c8dcSSimon Schubert 
7485796c8dcSSimon Schubert 
7495796c8dcSSimon Schubert /* Return a null-terminated temporary copy of the name
7505796c8dcSSimon Schubert    of a string token.  */
7515796c8dcSSimon Schubert 
7525796c8dcSSimon Schubert char *
7535796c8dcSSimon Schubert copy_name (struct stoken token)
7545796c8dcSSimon Schubert {
7555796c8dcSSimon Schubert   /* Make sure there's enough space for the token.  */
7565796c8dcSSimon Schubert   if (namecopy_size < token.length + 1)
7575796c8dcSSimon Schubert     {
7585796c8dcSSimon Schubert       namecopy_size = token.length + 1;
7595796c8dcSSimon Schubert       namecopy = xrealloc (namecopy, token.length + 1);
7605796c8dcSSimon Schubert     }
7615796c8dcSSimon Schubert 
7625796c8dcSSimon Schubert   memcpy (namecopy, token.ptr, token.length);
7635796c8dcSSimon Schubert   namecopy[token.length] = 0;
7645796c8dcSSimon Schubert 
7655796c8dcSSimon Schubert   return namecopy;
7665796c8dcSSimon Schubert }
7675796c8dcSSimon Schubert 
7685796c8dcSSimon Schubert /* Reverse an expression from suffix form (in which it is constructed)
7695796c8dcSSimon Schubert    to prefix form (in which we can conveniently print or execute it).
7705796c8dcSSimon Schubert    Ordinarily this always returns -1.  However, if EXPOUT_LAST_STRUCT
7715796c8dcSSimon Schubert    is not -1 (i.e., we are trying to complete a field name), it will
7725796c8dcSSimon Schubert    return the index of the subexpression which is the left-hand-side
7735796c8dcSSimon Schubert    of the struct operation at EXPOUT_LAST_STRUCT.  */
7745796c8dcSSimon Schubert 
7755796c8dcSSimon Schubert static int
7765796c8dcSSimon Schubert prefixify_expression (struct expression *expr)
7775796c8dcSSimon Schubert {
7785796c8dcSSimon Schubert   int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
7795796c8dcSSimon Schubert   struct expression *temp;
7805796c8dcSSimon Schubert   int inpos = expr->nelts, outpos = 0;
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert   temp = (struct expression *) alloca (len);
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert   /* Copy the original expression into temp.  */
7855796c8dcSSimon Schubert   memcpy (temp, expr, len);
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert   return prefixify_subexp (temp, expr, inpos, outpos);
7885796c8dcSSimon Schubert }
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert /* Return the number of exp_elements in the postfix subexpression
7915796c8dcSSimon Schubert    of EXPR whose operator is at index ENDPOS - 1 in EXPR.  */
7925796c8dcSSimon Schubert 
7935796c8dcSSimon Schubert int
7945796c8dcSSimon Schubert length_of_subexp (struct expression *expr, int endpos)
7955796c8dcSSimon Schubert {
796cf7f2e2dSJohn Marino   int oplen, args;
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert   operator_length (expr, endpos, &oplen, &args);
7995796c8dcSSimon Schubert 
8005796c8dcSSimon Schubert   while (args > 0)
8015796c8dcSSimon Schubert     {
8025796c8dcSSimon Schubert       oplen += length_of_subexp (expr, endpos - oplen);
8035796c8dcSSimon Schubert       args--;
8045796c8dcSSimon Schubert     }
8055796c8dcSSimon Schubert 
8065796c8dcSSimon Schubert   return oplen;
8075796c8dcSSimon Schubert }
8085796c8dcSSimon Schubert 
8095796c8dcSSimon Schubert /* Sets *OPLENP to the length of the operator whose (last) index is
8105796c8dcSSimon Schubert    ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
8115796c8dcSSimon Schubert    operator takes.  */
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert void
814cf7f2e2dSJohn Marino operator_length (const struct expression *expr, int endpos, int *oplenp,
815cf7f2e2dSJohn Marino 		 int *argsp)
8165796c8dcSSimon Schubert {
8175796c8dcSSimon Schubert   expr->language_defn->la_exp_desc->operator_length (expr, endpos,
8185796c8dcSSimon Schubert 						     oplenp, argsp);
8195796c8dcSSimon Schubert }
8205796c8dcSSimon Schubert 
8215796c8dcSSimon Schubert /* Default value for operator_length in exp_descriptor vectors.  */
8225796c8dcSSimon Schubert 
8235796c8dcSSimon Schubert void
824cf7f2e2dSJohn Marino operator_length_standard (const struct expression *expr, int endpos,
8255796c8dcSSimon Schubert 			  int *oplenp, int *argsp)
8265796c8dcSSimon Schubert {
8275796c8dcSSimon Schubert   int oplen = 1;
8285796c8dcSSimon Schubert   int args = 0;
8295796c8dcSSimon Schubert   enum f90_range_type range_type;
8305796c8dcSSimon Schubert   int i;
8315796c8dcSSimon Schubert 
8325796c8dcSSimon Schubert   if (endpos < 1)
8335796c8dcSSimon Schubert     error (_("?error in operator_length_standard"));
8345796c8dcSSimon Schubert 
8355796c8dcSSimon Schubert   i = (int) expr->elts[endpos - 1].opcode;
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert   switch (i)
8385796c8dcSSimon Schubert     {
8395796c8dcSSimon Schubert       /* C++  */
8405796c8dcSSimon Schubert     case OP_SCOPE:
8415796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
8425796c8dcSSimon Schubert       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
8435796c8dcSSimon Schubert       break;
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert     case OP_LONG:
8465796c8dcSSimon Schubert     case OP_DOUBLE:
8475796c8dcSSimon Schubert     case OP_DECFLOAT:
8485796c8dcSSimon Schubert     case OP_VAR_VALUE:
8495796c8dcSSimon Schubert       oplen = 4;
8505796c8dcSSimon Schubert       break;
8515796c8dcSSimon Schubert 
8525796c8dcSSimon Schubert     case OP_TYPE:
8535796c8dcSSimon Schubert     case OP_BOOL:
8545796c8dcSSimon Schubert     case OP_LAST:
8555796c8dcSSimon Schubert     case OP_INTERNALVAR:
8565796c8dcSSimon Schubert       oplen = 3;
8575796c8dcSSimon Schubert       break;
8585796c8dcSSimon Schubert 
8595796c8dcSSimon Schubert     case OP_COMPLEX:
8605796c8dcSSimon Schubert       oplen = 3;
8615796c8dcSSimon Schubert       args = 2;
8625796c8dcSSimon Schubert       break;
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert     case OP_FUNCALL:
8655796c8dcSSimon Schubert     case OP_F77_UNDETERMINED_ARGLIST:
8665796c8dcSSimon Schubert       oplen = 3;
8675796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
8685796c8dcSSimon Schubert       break;
8695796c8dcSSimon Schubert 
870cf7f2e2dSJohn Marino     case TYPE_INSTANCE:
871cf7f2e2dSJohn Marino       oplen = 4 + longest_to_int (expr->elts[endpos - 2].longconst);
872cf7f2e2dSJohn Marino       args = 1;
873cf7f2e2dSJohn Marino       break;
874cf7f2e2dSJohn Marino 
875*c50c785cSJohn Marino     case OP_OBJC_MSGCALL:	/* Objective C message (method) call.  */
8765796c8dcSSimon Schubert       oplen = 4;
8775796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
8785796c8dcSSimon Schubert       break;
8795796c8dcSSimon Schubert 
8805796c8dcSSimon Schubert     case UNOP_MAX:
8815796c8dcSSimon Schubert     case UNOP_MIN:
8825796c8dcSSimon Schubert       oplen = 3;
8835796c8dcSSimon Schubert       break;
8845796c8dcSSimon Schubert 
8855796c8dcSSimon Schubert     case BINOP_VAL:
8865796c8dcSSimon Schubert     case UNOP_CAST:
887cf7f2e2dSJohn Marino     case UNOP_DYNAMIC_CAST:
888cf7f2e2dSJohn Marino     case UNOP_REINTERPRET_CAST:
8895796c8dcSSimon Schubert     case UNOP_MEMVAL:
8905796c8dcSSimon Schubert       oplen = 3;
8915796c8dcSSimon Schubert       args = 1;
8925796c8dcSSimon Schubert       break;
8935796c8dcSSimon Schubert 
8945796c8dcSSimon Schubert     case UNOP_MEMVAL_TLS:
8955796c8dcSSimon Schubert       oplen = 4;
8965796c8dcSSimon Schubert       args = 1;
8975796c8dcSSimon Schubert       break;
8985796c8dcSSimon Schubert 
8995796c8dcSSimon Schubert     case UNOP_ABS:
9005796c8dcSSimon Schubert     case UNOP_CAP:
9015796c8dcSSimon Schubert     case UNOP_CHR:
9025796c8dcSSimon Schubert     case UNOP_FLOAT:
9035796c8dcSSimon Schubert     case UNOP_HIGH:
9045796c8dcSSimon Schubert     case UNOP_ODD:
9055796c8dcSSimon Schubert     case UNOP_ORD:
9065796c8dcSSimon Schubert     case UNOP_TRUNC:
9075796c8dcSSimon Schubert       oplen = 1;
9085796c8dcSSimon Schubert       args = 1;
9095796c8dcSSimon Schubert       break;
9105796c8dcSSimon Schubert 
911cf7f2e2dSJohn Marino     case OP_ADL_FUNC:
912cf7f2e2dSJohn Marino       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
913cf7f2e2dSJohn Marino       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
914cf7f2e2dSJohn Marino       oplen++;
915cf7f2e2dSJohn Marino       oplen++;
916cf7f2e2dSJohn Marino       break;
917cf7f2e2dSJohn Marino 
9185796c8dcSSimon Schubert     case OP_LABELED:
9195796c8dcSSimon Schubert     case STRUCTOP_STRUCT:
9205796c8dcSSimon Schubert     case STRUCTOP_PTR:
9215796c8dcSSimon Schubert       args = 1;
9225796c8dcSSimon Schubert       /* fall through */
9235796c8dcSSimon Schubert     case OP_REGISTER:
9245796c8dcSSimon Schubert     case OP_M2_STRING:
9255796c8dcSSimon Schubert     case OP_STRING:
926*c50c785cSJohn Marino     case OP_OBJC_NSSTRING:	/* Objective C Foundation Class
927*c50c785cSJohn Marino 				   NSString constant.  */
928*c50c785cSJohn Marino     case OP_OBJC_SELECTOR:	/* Objective C "@selector" pseudo-op.  */
9295796c8dcSSimon Schubert     case OP_NAME:
9305796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
9315796c8dcSSimon Schubert       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
9325796c8dcSSimon Schubert       break;
9335796c8dcSSimon Schubert 
9345796c8dcSSimon Schubert     case OP_BITSTRING:
9355796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
9365796c8dcSSimon Schubert       oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
9375796c8dcSSimon Schubert       oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
9385796c8dcSSimon Schubert       break;
9395796c8dcSSimon Schubert 
9405796c8dcSSimon Schubert     case OP_ARRAY:
9415796c8dcSSimon Schubert       oplen = 4;
9425796c8dcSSimon Schubert       args = longest_to_int (expr->elts[endpos - 2].longconst);
9435796c8dcSSimon Schubert       args -= longest_to_int (expr->elts[endpos - 3].longconst);
9445796c8dcSSimon Schubert       args += 1;
9455796c8dcSSimon Schubert       break;
9465796c8dcSSimon Schubert 
9475796c8dcSSimon Schubert     case TERNOP_COND:
9485796c8dcSSimon Schubert     case TERNOP_SLICE:
9495796c8dcSSimon Schubert     case TERNOP_SLICE_COUNT:
9505796c8dcSSimon Schubert       args = 3;
9515796c8dcSSimon Schubert       break;
9525796c8dcSSimon Schubert 
9535796c8dcSSimon Schubert       /* Modula-2 */
9545796c8dcSSimon Schubert     case MULTI_SUBSCRIPT:
9555796c8dcSSimon Schubert       oplen = 3;
9565796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
9575796c8dcSSimon Schubert       break;
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert     case BINOP_ASSIGN_MODIFY:
9605796c8dcSSimon Schubert       oplen = 3;
9615796c8dcSSimon Schubert       args = 2;
9625796c8dcSSimon Schubert       break;
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert       /* C++ */
9655796c8dcSSimon Schubert     case OP_THIS:
9665796c8dcSSimon Schubert     case OP_OBJC_SELF:
9675796c8dcSSimon Schubert       oplen = 2;
9685796c8dcSSimon Schubert       break;
9695796c8dcSSimon Schubert 
9705796c8dcSSimon Schubert     case OP_F90_RANGE:
9715796c8dcSSimon Schubert       oplen = 3;
9725796c8dcSSimon Schubert 
9735796c8dcSSimon Schubert       range_type = longest_to_int (expr->elts[endpos - 2].longconst);
9745796c8dcSSimon Schubert       switch (range_type)
9755796c8dcSSimon Schubert 	{
9765796c8dcSSimon Schubert 	case LOW_BOUND_DEFAULT:
9775796c8dcSSimon Schubert 	case HIGH_BOUND_DEFAULT:
9785796c8dcSSimon Schubert 	  args = 1;
9795796c8dcSSimon Schubert 	  break;
9805796c8dcSSimon Schubert 	case BOTH_BOUND_DEFAULT:
9815796c8dcSSimon Schubert 	  args = 0;
9825796c8dcSSimon Schubert 	  break;
9835796c8dcSSimon Schubert 	case NONE_BOUND_DEFAULT:
9845796c8dcSSimon Schubert 	  args = 2;
9855796c8dcSSimon Schubert 	  break;
9865796c8dcSSimon Schubert 	}
9875796c8dcSSimon Schubert 
9885796c8dcSSimon Schubert       break;
9895796c8dcSSimon Schubert 
9905796c8dcSSimon Schubert     default:
9915796c8dcSSimon Schubert       args = 1 + (i < (int) BINOP_END);
9925796c8dcSSimon Schubert     }
9935796c8dcSSimon Schubert 
9945796c8dcSSimon Schubert   *oplenp = oplen;
9955796c8dcSSimon Schubert   *argsp = args;
9965796c8dcSSimon Schubert }
9975796c8dcSSimon Schubert 
9985796c8dcSSimon Schubert /* Copy the subexpression ending just before index INEND in INEXPR
9995796c8dcSSimon Schubert    into OUTEXPR, starting at index OUTBEG.
10005796c8dcSSimon Schubert    In the process, convert it from suffix to prefix form.
10015796c8dcSSimon Schubert    If EXPOUT_LAST_STRUCT is -1, then this function always returns -1.
10025796c8dcSSimon Schubert    Otherwise, it returns the index of the subexpression which is the
10035796c8dcSSimon Schubert    left-hand-side of the expression at EXPOUT_LAST_STRUCT.  */
10045796c8dcSSimon Schubert 
10055796c8dcSSimon Schubert static int
10065796c8dcSSimon Schubert prefixify_subexp (struct expression *inexpr,
10075796c8dcSSimon Schubert 		  struct expression *outexpr, int inend, int outbeg)
10085796c8dcSSimon Schubert {
10095796c8dcSSimon Schubert   int oplen;
10105796c8dcSSimon Schubert   int args;
10115796c8dcSSimon Schubert   int i;
10125796c8dcSSimon Schubert   int *arglens;
10135796c8dcSSimon Schubert   int result = -1;
10145796c8dcSSimon Schubert 
10155796c8dcSSimon Schubert   operator_length (inexpr, inend, &oplen, &args);
10165796c8dcSSimon Schubert 
10175796c8dcSSimon Schubert   /* Copy the final operator itself, from the end of the input
10185796c8dcSSimon Schubert      to the beginning of the output.  */
10195796c8dcSSimon Schubert   inend -= oplen;
10205796c8dcSSimon Schubert   memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
10215796c8dcSSimon Schubert 	  EXP_ELEM_TO_BYTES (oplen));
10225796c8dcSSimon Schubert   outbeg += oplen;
10235796c8dcSSimon Schubert 
10245796c8dcSSimon Schubert   if (expout_last_struct == inend)
10255796c8dcSSimon Schubert     result = outbeg - oplen;
10265796c8dcSSimon Schubert 
10275796c8dcSSimon Schubert   /* Find the lengths of the arg subexpressions.  */
10285796c8dcSSimon Schubert   arglens = (int *) alloca (args * sizeof (int));
10295796c8dcSSimon Schubert   for (i = args - 1; i >= 0; i--)
10305796c8dcSSimon Schubert     {
10315796c8dcSSimon Schubert       oplen = length_of_subexp (inexpr, inend);
10325796c8dcSSimon Schubert       arglens[i] = oplen;
10335796c8dcSSimon Schubert       inend -= oplen;
10345796c8dcSSimon Schubert     }
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert   /* Now copy each subexpression, preserving the order of
10375796c8dcSSimon Schubert      the subexpressions, but prefixifying each one.
10385796c8dcSSimon Schubert      In this loop, inend starts at the beginning of
10395796c8dcSSimon Schubert      the expression this level is working on
10405796c8dcSSimon Schubert      and marches forward over the arguments.
10415796c8dcSSimon Schubert      outbeg does similarly in the output.  */
10425796c8dcSSimon Schubert   for (i = 0; i < args; i++)
10435796c8dcSSimon Schubert     {
10445796c8dcSSimon Schubert       int r;
1045cf7f2e2dSJohn Marino 
10465796c8dcSSimon Schubert       oplen = arglens[i];
10475796c8dcSSimon Schubert       inend += oplen;
10485796c8dcSSimon Schubert       r = prefixify_subexp (inexpr, outexpr, inend, outbeg);
10495796c8dcSSimon Schubert       if (r != -1)
10505796c8dcSSimon Schubert 	{
10515796c8dcSSimon Schubert 	  /* Return immediately.  We probably have only parsed a
10525796c8dcSSimon Schubert 	     partial expression, so we don't want to try to reverse
10535796c8dcSSimon Schubert 	     the other operands.  */
10545796c8dcSSimon Schubert 	  return r;
10555796c8dcSSimon Schubert 	}
10565796c8dcSSimon Schubert       outbeg += oplen;
10575796c8dcSSimon Schubert     }
10585796c8dcSSimon Schubert 
10595796c8dcSSimon Schubert   return result;
10605796c8dcSSimon Schubert }
10615796c8dcSSimon Schubert 
10625796c8dcSSimon Schubert /* Read an expression from the string *STRINGPTR points to,
10635796c8dcSSimon Schubert    parse it, and return a pointer to a  struct expression  that we malloc.
10645796c8dcSSimon Schubert    Use block BLOCK as the lexical context for variable names;
10655796c8dcSSimon Schubert    if BLOCK is zero, use the block of the selected stack frame.
10665796c8dcSSimon Schubert    Meanwhile, advance *STRINGPTR to point after the expression,
10675796c8dcSSimon Schubert    at the first nonwhite character that is not part of the expression
10685796c8dcSSimon Schubert    (possibly a null character).
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert    If COMMA is nonzero, stop if a comma is reached.  */
10715796c8dcSSimon Schubert 
10725796c8dcSSimon Schubert struct expression *
10735796c8dcSSimon Schubert parse_exp_1 (char **stringptr, struct block *block, int comma)
10745796c8dcSSimon Schubert {
10755796c8dcSSimon Schubert   return parse_exp_in_context (stringptr, block, comma, 0, NULL);
10765796c8dcSSimon Schubert }
10775796c8dcSSimon Schubert 
10785796c8dcSSimon Schubert /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
10795796c8dcSSimon Schubert    no value is expected from the expression.
10805796c8dcSSimon Schubert    OUT_SUBEXP is set when attempting to complete a field name; in this
10815796c8dcSSimon Schubert    case it is set to the index of the subexpression on the
10825796c8dcSSimon Schubert    left-hand-side of the struct op.  If not doing such completion, it
10835796c8dcSSimon Schubert    is left untouched.  */
10845796c8dcSSimon Schubert 
10855796c8dcSSimon Schubert static struct expression *
10865796c8dcSSimon Schubert parse_exp_in_context (char **stringptr, struct block *block, int comma,
10875796c8dcSSimon Schubert 		      int void_context_p, int *out_subexp)
10885796c8dcSSimon Schubert {
10895796c8dcSSimon Schubert   volatile struct gdb_exception except;
10905796c8dcSSimon Schubert   struct cleanup *old_chain;
1091cf7f2e2dSJohn Marino   const struct language_defn *lang = NULL;
10925796c8dcSSimon Schubert   int subexp;
10935796c8dcSSimon Schubert 
10945796c8dcSSimon Schubert   lexptr = *stringptr;
10955796c8dcSSimon Schubert   prev_lexptr = NULL;
10965796c8dcSSimon Schubert 
10975796c8dcSSimon Schubert   paren_depth = 0;
10985796c8dcSSimon Schubert   type_stack_depth = 0;
10995796c8dcSSimon Schubert   expout_last_struct = -1;
11005796c8dcSSimon Schubert 
11015796c8dcSSimon Schubert   comma_terminates = comma;
11025796c8dcSSimon Schubert 
11035796c8dcSSimon Schubert   if (lexptr == 0 || *lexptr == 0)
11045796c8dcSSimon Schubert     error_no_arg (_("expression to compute"));
11055796c8dcSSimon Schubert 
11065796c8dcSSimon Schubert   old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
11075796c8dcSSimon Schubert   funcall_chain = 0;
11085796c8dcSSimon Schubert 
11095796c8dcSSimon Schubert   expression_context_block = block;
11105796c8dcSSimon Schubert 
11115796c8dcSSimon Schubert   /* If no context specified, try using the current frame, if any.  */
11125796c8dcSSimon Schubert   if (!expression_context_block)
11135796c8dcSSimon Schubert     expression_context_block = get_selected_block (&expression_context_pc);
11145796c8dcSSimon Schubert   else
11155796c8dcSSimon Schubert     expression_context_pc = BLOCK_START (expression_context_block);
11165796c8dcSSimon Schubert 
11175796c8dcSSimon Schubert   /* Fall back to using the current source static context, if any.  */
11185796c8dcSSimon Schubert 
11195796c8dcSSimon Schubert   if (!expression_context_block)
11205796c8dcSSimon Schubert     {
11215796c8dcSSimon Schubert       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
11225796c8dcSSimon Schubert       if (cursal.symtab)
11235796c8dcSSimon Schubert 	expression_context_block
11245796c8dcSSimon Schubert 	  = BLOCKVECTOR_BLOCK (BLOCKVECTOR (cursal.symtab), STATIC_BLOCK);
11255796c8dcSSimon Schubert       if (expression_context_block)
11265796c8dcSSimon Schubert 	expression_context_pc = BLOCK_START (expression_context_block);
11275796c8dcSSimon Schubert     }
11285796c8dcSSimon Schubert 
1129cf7f2e2dSJohn Marino   if (language_mode == language_mode_auto && block != NULL)
1130cf7f2e2dSJohn Marino     {
1131cf7f2e2dSJohn Marino       /* Find the language associated to the given context block.
1132cf7f2e2dSJohn Marino          Default to the current language if it can not be determined.
1133cf7f2e2dSJohn Marino 
1134cf7f2e2dSJohn Marino          Note that using the language corresponding to the current frame
1135cf7f2e2dSJohn Marino          can sometimes give unexpected results.  For instance, this
1136cf7f2e2dSJohn Marino          routine is often called several times during the inferior
1137cf7f2e2dSJohn Marino          startup phase to re-parse breakpoint expressions after
1138cf7f2e2dSJohn Marino          a new shared library has been loaded.  The language associated
1139cf7f2e2dSJohn Marino          to the current frame at this moment is not relevant for
1140cf7f2e2dSJohn Marino          the breakpoint.  Using it would therefore be silly, so it seems
1141cf7f2e2dSJohn Marino          better to rely on the current language rather than relying on
1142cf7f2e2dSJohn Marino          the current frame language to parse the expression.  That's why
1143cf7f2e2dSJohn Marino          we do the following language detection only if the context block
1144cf7f2e2dSJohn Marino          has been specifically provided.  */
1145cf7f2e2dSJohn Marino       struct symbol *func = block_linkage_function (block);
1146cf7f2e2dSJohn Marino 
1147cf7f2e2dSJohn Marino       if (func != NULL)
1148cf7f2e2dSJohn Marino         lang = language_def (SYMBOL_LANGUAGE (func));
1149cf7f2e2dSJohn Marino       if (lang == NULL || lang->la_language == language_unknown)
1150cf7f2e2dSJohn Marino         lang = current_language;
1151cf7f2e2dSJohn Marino     }
1152cf7f2e2dSJohn Marino   else
1153cf7f2e2dSJohn Marino     lang = current_language;
1154cf7f2e2dSJohn Marino 
11555796c8dcSSimon Schubert   expout_size = 10;
11565796c8dcSSimon Schubert   expout_ptr = 0;
11575796c8dcSSimon Schubert   expout = (struct expression *)
11585796c8dcSSimon Schubert     xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
1159cf7f2e2dSJohn Marino   expout->language_defn = lang;
11605796c8dcSSimon Schubert   expout->gdbarch = get_current_arch ();
11615796c8dcSSimon Schubert 
11625796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
11635796c8dcSSimon Schubert     {
1164cf7f2e2dSJohn Marino       if (lang->la_parser ())
1165cf7f2e2dSJohn Marino         lang->la_error (NULL);
11665796c8dcSSimon Schubert     }
11675796c8dcSSimon Schubert   if (except.reason < 0)
11685796c8dcSSimon Schubert     {
11695796c8dcSSimon Schubert       if (! in_parse_field)
11705796c8dcSSimon Schubert 	{
11715796c8dcSSimon Schubert 	  xfree (expout);
11725796c8dcSSimon Schubert 	  throw_exception (except);
11735796c8dcSSimon Schubert 	}
11745796c8dcSSimon Schubert     }
11755796c8dcSSimon Schubert 
11765796c8dcSSimon Schubert   discard_cleanups (old_chain);
11775796c8dcSSimon Schubert 
11785796c8dcSSimon Schubert   /* Record the actual number of expression elements, and then
11795796c8dcSSimon Schubert      reallocate the expression memory so that we free up any
11805796c8dcSSimon Schubert      excess elements.  */
11815796c8dcSSimon Schubert 
11825796c8dcSSimon Schubert   expout->nelts = expout_ptr;
11835796c8dcSSimon Schubert   expout = (struct expression *)
11845796c8dcSSimon Schubert     xrealloc ((char *) expout,
1185*c50c785cSJohn Marino 	      sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));
11865796c8dcSSimon Schubert 
11875796c8dcSSimon Schubert   /* Convert expression from postfix form as generated by yacc
11885796c8dcSSimon Schubert      parser, to a prefix form.  */
11895796c8dcSSimon Schubert 
11905796c8dcSSimon Schubert   if (expressiondebug)
11915796c8dcSSimon Schubert     dump_raw_expression (expout, gdb_stdlog,
11925796c8dcSSimon Schubert 			 "before conversion to prefix form");
11935796c8dcSSimon Schubert 
11945796c8dcSSimon Schubert   subexp = prefixify_expression (expout);
11955796c8dcSSimon Schubert   if (out_subexp)
11965796c8dcSSimon Schubert     *out_subexp = subexp;
11975796c8dcSSimon Schubert 
1198cf7f2e2dSJohn Marino   lang->la_post_parser (&expout, void_context_p);
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert   if (expressiondebug)
12015796c8dcSSimon Schubert     dump_prefix_expression (expout, gdb_stdlog);
12025796c8dcSSimon Schubert 
12035796c8dcSSimon Schubert   *stringptr = lexptr;
12045796c8dcSSimon Schubert   return expout;
12055796c8dcSSimon Schubert }
12065796c8dcSSimon Schubert 
12075796c8dcSSimon Schubert /* Parse STRING as an expression, and complain if this fails
12085796c8dcSSimon Schubert    to use up all of the contents of STRING.  */
12095796c8dcSSimon Schubert 
12105796c8dcSSimon Schubert struct expression *
12115796c8dcSSimon Schubert parse_expression (char *string)
12125796c8dcSSimon Schubert {
12135796c8dcSSimon Schubert   struct expression *exp;
1214cf7f2e2dSJohn Marino 
12155796c8dcSSimon Schubert   exp = parse_exp_1 (&string, 0, 0);
12165796c8dcSSimon Schubert   if (*string)
12175796c8dcSSimon Schubert     error (_("Junk after end of expression."));
12185796c8dcSSimon Schubert   return exp;
12195796c8dcSSimon Schubert }
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert /* Parse STRING as an expression.  If parsing ends in the middle of a
12225796c8dcSSimon Schubert    field reference, return the type of the left-hand-side of the
12235796c8dcSSimon Schubert    reference; furthermore, if the parsing ends in the field name,
1224*c50c785cSJohn Marino    return the field name in *NAME.  If the parsing ends in the middle
1225*c50c785cSJohn Marino    of a field reference, but the reference is somehow invalid, throw
1226*c50c785cSJohn Marino    an exception.  In all other cases, return NULL.  Returned non-NULL
1227*c50c785cSJohn Marino    *NAME must be freed by the caller.  */
12285796c8dcSSimon Schubert 
12295796c8dcSSimon Schubert struct type *
12305796c8dcSSimon Schubert parse_field_expression (char *string, char **name)
12315796c8dcSSimon Schubert {
12325796c8dcSSimon Schubert   struct expression *exp = NULL;
12335796c8dcSSimon Schubert   struct value *val;
12345796c8dcSSimon Schubert   int subexp;
12355796c8dcSSimon Schubert   volatile struct gdb_exception except;
12365796c8dcSSimon Schubert 
1237*c50c785cSJohn Marino   TRY_CATCH (except, RETURN_MASK_ERROR)
12385796c8dcSSimon Schubert     {
12395796c8dcSSimon Schubert       in_parse_field = 1;
12405796c8dcSSimon Schubert       exp = parse_exp_in_context (&string, 0, 0, 0, &subexp);
12415796c8dcSSimon Schubert     }
12425796c8dcSSimon Schubert   in_parse_field = 0;
12435796c8dcSSimon Schubert   if (except.reason < 0 || ! exp)
12445796c8dcSSimon Schubert     return NULL;
12455796c8dcSSimon Schubert   if (expout_last_struct == -1)
12465796c8dcSSimon Schubert     {
12475796c8dcSSimon Schubert       xfree (exp);
12485796c8dcSSimon Schubert       return NULL;
12495796c8dcSSimon Schubert     }
12505796c8dcSSimon Schubert 
12515796c8dcSSimon Schubert   *name = extract_field_op (exp, &subexp);
12525796c8dcSSimon Schubert   if (!*name)
12535796c8dcSSimon Schubert     {
12545796c8dcSSimon Schubert       xfree (exp);
12555796c8dcSSimon Schubert       return NULL;
12565796c8dcSSimon Schubert     }
1257*c50c785cSJohn Marino 
1258*c50c785cSJohn Marino   /* This might throw an exception.  If so, we want to let it
1259*c50c785cSJohn Marino      propagate.  */
1260*c50c785cSJohn Marino   val = evaluate_subexpression_type (exp, subexp);
12615796c8dcSSimon Schubert   /* (*NAME) is a part of the EXP memory block freed below.  */
12625796c8dcSSimon Schubert   *name = xstrdup (*name);
12635796c8dcSSimon Schubert   xfree (exp);
12645796c8dcSSimon Schubert 
12655796c8dcSSimon Schubert   return value_type (val);
12665796c8dcSSimon Schubert }
12675796c8dcSSimon Schubert 
1268*c50c785cSJohn Marino /* A post-parser that does nothing.  */
12695796c8dcSSimon Schubert 
12705796c8dcSSimon Schubert void
12715796c8dcSSimon Schubert null_post_parser (struct expression **exp, int void_context_p)
12725796c8dcSSimon Schubert {
12735796c8dcSSimon Schubert }
1274*c50c785cSJohn Marino 
1275*c50c785cSJohn Marino /* Parse floating point value P of length LEN.
1276*c50c785cSJohn Marino    Return 0 (false) if invalid, 1 (true) if valid.
1277*c50c785cSJohn Marino    The successfully parsed number is stored in D.
1278*c50c785cSJohn Marino    *SUFFIX points to the suffix of the number in P.
1279*c50c785cSJohn Marino 
1280*c50c785cSJohn Marino    NOTE: This accepts the floating point syntax that sscanf accepts.  */
1281*c50c785cSJohn Marino 
1282*c50c785cSJohn Marino int
1283*c50c785cSJohn Marino parse_float (const char *p, int len, DOUBLEST *d, const char **suffix)
1284*c50c785cSJohn Marino {
1285*c50c785cSJohn Marino   char *copy;
1286*c50c785cSJohn Marino   char *s;
1287*c50c785cSJohn Marino   int n, num;
1288*c50c785cSJohn Marino 
1289*c50c785cSJohn Marino   copy = xmalloc (len + 1);
1290*c50c785cSJohn Marino   memcpy (copy, p, len);
1291*c50c785cSJohn Marino   copy[len] = 0;
1292*c50c785cSJohn Marino 
1293*c50c785cSJohn Marino   num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
1294*c50c785cSJohn Marino   xfree (copy);
1295*c50c785cSJohn Marino 
1296*c50c785cSJohn Marino   /* The sscanf man page suggests not making any assumptions on the effect
1297*c50c785cSJohn Marino      of %n on the result, so we don't.
1298*c50c785cSJohn Marino      That is why we simply test num == 0.  */
1299*c50c785cSJohn Marino   if (num == 0)
1300*c50c785cSJohn Marino     return 0;
1301*c50c785cSJohn Marino 
1302*c50c785cSJohn Marino   *suffix = p + n;
1303*c50c785cSJohn Marino   return 1;
1304*c50c785cSJohn Marino }
1305*c50c785cSJohn Marino 
1306*c50c785cSJohn Marino /* Parse floating point value P of length LEN, using the C syntax for floats.
1307*c50c785cSJohn Marino    Return 0 (false) if invalid, 1 (true) if valid.
1308*c50c785cSJohn Marino    The successfully parsed number is stored in *D.
1309*c50c785cSJohn Marino    Its type is taken from builtin_type (gdbarch) and is stored in *T.  */
1310*c50c785cSJohn Marino 
1311*c50c785cSJohn Marino int
1312*c50c785cSJohn Marino parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
1313*c50c785cSJohn Marino 	       DOUBLEST *d, struct type **t)
1314*c50c785cSJohn Marino {
1315*c50c785cSJohn Marino   const char *suffix;
1316*c50c785cSJohn Marino   int suffix_len;
1317*c50c785cSJohn Marino   const struct builtin_type *builtin_types = builtin_type (gdbarch);
1318*c50c785cSJohn Marino 
1319*c50c785cSJohn Marino   if (! parse_float (p, len, d, &suffix))
1320*c50c785cSJohn Marino     return 0;
1321*c50c785cSJohn Marino 
1322*c50c785cSJohn Marino   suffix_len = p + len - suffix;
1323*c50c785cSJohn Marino 
1324*c50c785cSJohn Marino   if (suffix_len == 0)
1325*c50c785cSJohn Marino     *t = builtin_types->builtin_double;
1326*c50c785cSJohn Marino   else if (suffix_len == 1)
1327*c50c785cSJohn Marino     {
1328*c50c785cSJohn Marino       /* Handle suffixes: 'f' for float, 'l' for long double.  */
1329*c50c785cSJohn Marino       if (tolower (*suffix) == 'f')
1330*c50c785cSJohn Marino 	*t = builtin_types->builtin_float;
1331*c50c785cSJohn Marino       else if (tolower (*suffix) == 'l')
1332*c50c785cSJohn Marino 	*t = builtin_types->builtin_long_double;
1333*c50c785cSJohn Marino       else
1334*c50c785cSJohn Marino 	return 0;
1335*c50c785cSJohn Marino     }
1336*c50c785cSJohn Marino   else
1337*c50c785cSJohn Marino     return 0;
1338*c50c785cSJohn Marino 
1339*c50c785cSJohn Marino   return 1;
1340*c50c785cSJohn Marino }
13415796c8dcSSimon Schubert 
13425796c8dcSSimon Schubert /* Stuff for maintaining a stack of types.  Currently just used by C, but
13435796c8dcSSimon Schubert    probably useful for any language which declares its types "backwards".  */
13445796c8dcSSimon Schubert 
13455796c8dcSSimon Schubert static void
13465796c8dcSSimon Schubert check_type_stack_depth (void)
13475796c8dcSSimon Schubert {
13485796c8dcSSimon Schubert   if (type_stack_depth == type_stack_size)
13495796c8dcSSimon Schubert     {
13505796c8dcSSimon Schubert       type_stack_size *= 2;
13515796c8dcSSimon Schubert       type_stack = (union type_stack_elt *)
13525796c8dcSSimon Schubert 	xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
13535796c8dcSSimon Schubert     }
13545796c8dcSSimon Schubert }
13555796c8dcSSimon Schubert 
13565796c8dcSSimon Schubert void
13575796c8dcSSimon Schubert push_type (enum type_pieces tp)
13585796c8dcSSimon Schubert {
13595796c8dcSSimon Schubert   check_type_stack_depth ();
13605796c8dcSSimon Schubert   type_stack[type_stack_depth++].piece = tp;
13615796c8dcSSimon Schubert }
13625796c8dcSSimon Schubert 
13635796c8dcSSimon Schubert void
13645796c8dcSSimon Schubert push_type_int (int n)
13655796c8dcSSimon Schubert {
13665796c8dcSSimon Schubert   check_type_stack_depth ();
13675796c8dcSSimon Schubert   type_stack[type_stack_depth++].int_val = n;
13685796c8dcSSimon Schubert }
13695796c8dcSSimon Schubert 
13705796c8dcSSimon Schubert void
13715796c8dcSSimon Schubert push_type_address_space (char *string)
13725796c8dcSSimon Schubert {
13735796c8dcSSimon Schubert   push_type_int (address_space_name_to_int (parse_gdbarch, string));
13745796c8dcSSimon Schubert }
13755796c8dcSSimon Schubert 
13765796c8dcSSimon Schubert enum type_pieces
13775796c8dcSSimon Schubert pop_type (void)
13785796c8dcSSimon Schubert {
13795796c8dcSSimon Schubert   if (type_stack_depth)
13805796c8dcSSimon Schubert     return type_stack[--type_stack_depth].piece;
13815796c8dcSSimon Schubert   return tp_end;
13825796c8dcSSimon Schubert }
13835796c8dcSSimon Schubert 
13845796c8dcSSimon Schubert int
13855796c8dcSSimon Schubert pop_type_int (void)
13865796c8dcSSimon Schubert {
13875796c8dcSSimon Schubert   if (type_stack_depth)
13885796c8dcSSimon Schubert     return type_stack[--type_stack_depth].int_val;
13895796c8dcSSimon Schubert   /* "Can't happen".  */
13905796c8dcSSimon Schubert   return 0;
13915796c8dcSSimon Schubert }
13925796c8dcSSimon Schubert 
13935796c8dcSSimon Schubert /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
13945796c8dcSSimon Schubert    as modified by all the stuff on the stack.  */
13955796c8dcSSimon Schubert struct type *
13965796c8dcSSimon Schubert follow_types (struct type *follow_type)
13975796c8dcSSimon Schubert {
13985796c8dcSSimon Schubert   int done = 0;
13995796c8dcSSimon Schubert   int make_const = 0;
14005796c8dcSSimon Schubert   int make_volatile = 0;
14015796c8dcSSimon Schubert   int make_addr_space = 0;
14025796c8dcSSimon Schubert   int array_size;
14035796c8dcSSimon Schubert 
14045796c8dcSSimon Schubert   while (!done)
14055796c8dcSSimon Schubert     switch (pop_type ())
14065796c8dcSSimon Schubert       {
14075796c8dcSSimon Schubert       case tp_end:
14085796c8dcSSimon Schubert 	done = 1;
14095796c8dcSSimon Schubert 	if (make_const)
14105796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
14115796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
14125796c8dcSSimon Schubert 				      follow_type, 0);
14135796c8dcSSimon Schubert 	if (make_volatile)
14145796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
14155796c8dcSSimon Schubert 				      make_volatile,
14165796c8dcSSimon Schubert 				      follow_type, 0);
14175796c8dcSSimon Schubert 	if (make_addr_space)
14185796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
14195796c8dcSSimon Schubert 						      make_addr_space);
14205796c8dcSSimon Schubert 	make_const = make_volatile = 0;
14215796c8dcSSimon Schubert 	make_addr_space = 0;
14225796c8dcSSimon Schubert 	break;
14235796c8dcSSimon Schubert       case tp_const:
14245796c8dcSSimon Schubert 	make_const = 1;
14255796c8dcSSimon Schubert 	break;
14265796c8dcSSimon Schubert       case tp_volatile:
14275796c8dcSSimon Schubert 	make_volatile = 1;
14285796c8dcSSimon Schubert 	break;
14295796c8dcSSimon Schubert       case tp_space_identifier:
14305796c8dcSSimon Schubert 	make_addr_space = pop_type_int ();
14315796c8dcSSimon Schubert 	break;
14325796c8dcSSimon Schubert       case tp_pointer:
14335796c8dcSSimon Schubert 	follow_type = lookup_pointer_type (follow_type);
14345796c8dcSSimon Schubert 	if (make_const)
14355796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
14365796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
14375796c8dcSSimon Schubert 				      follow_type, 0);
14385796c8dcSSimon Schubert 	if (make_volatile)
14395796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
14405796c8dcSSimon Schubert 				      make_volatile,
14415796c8dcSSimon Schubert 				      follow_type, 0);
14425796c8dcSSimon Schubert 	if (make_addr_space)
14435796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
14445796c8dcSSimon Schubert 						      make_addr_space);
14455796c8dcSSimon Schubert 	make_const = make_volatile = 0;
14465796c8dcSSimon Schubert 	make_addr_space = 0;
14475796c8dcSSimon Schubert 	break;
14485796c8dcSSimon Schubert       case tp_reference:
14495796c8dcSSimon Schubert 	follow_type = lookup_reference_type (follow_type);
14505796c8dcSSimon Schubert 	if (make_const)
14515796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
14525796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
14535796c8dcSSimon Schubert 				      follow_type, 0);
14545796c8dcSSimon Schubert 	if (make_volatile)
14555796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
14565796c8dcSSimon Schubert 				      make_volatile,
14575796c8dcSSimon Schubert 				      follow_type, 0);
14585796c8dcSSimon Schubert 	if (make_addr_space)
14595796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
14605796c8dcSSimon Schubert 						      make_addr_space);
14615796c8dcSSimon Schubert 	make_const = make_volatile = 0;
14625796c8dcSSimon Schubert 	make_addr_space = 0;
14635796c8dcSSimon Schubert 	break;
14645796c8dcSSimon Schubert       case tp_array:
14655796c8dcSSimon Schubert 	array_size = pop_type_int ();
14665796c8dcSSimon Schubert 	/* FIXME-type-allocation: need a way to free this type when we are
14675796c8dcSSimon Schubert 	   done with it.  */
14685796c8dcSSimon Schubert 	follow_type =
14695796c8dcSSimon Schubert 	  lookup_array_range_type (follow_type,
14705796c8dcSSimon Schubert 				   0, array_size >= 0 ? array_size - 1 : 0);
14715796c8dcSSimon Schubert 	if (array_size < 0)
14725796c8dcSSimon Schubert 	  TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (follow_type) = 1;
14735796c8dcSSimon Schubert 	break;
14745796c8dcSSimon Schubert       case tp_function:
14755796c8dcSSimon Schubert 	/* FIXME-type-allocation: need a way to free this type when we are
14765796c8dcSSimon Schubert 	   done with it.  */
14775796c8dcSSimon Schubert 	follow_type = lookup_function_type (follow_type);
14785796c8dcSSimon Schubert 	break;
14795796c8dcSSimon Schubert       }
14805796c8dcSSimon Schubert   return follow_type;
14815796c8dcSSimon Schubert }
14825796c8dcSSimon Schubert 
14835796c8dcSSimon Schubert /* This function avoids direct calls to fprintf
14845796c8dcSSimon Schubert    in the parser generated debug code.  */
14855796c8dcSSimon Schubert void
14865796c8dcSSimon Schubert parser_fprintf (FILE *x, const char *y, ...)
14875796c8dcSSimon Schubert {
14885796c8dcSSimon Schubert   va_list args;
1489cf7f2e2dSJohn Marino 
14905796c8dcSSimon Schubert   va_start (args, y);
14915796c8dcSSimon Schubert   if (x == stderr)
14925796c8dcSSimon Schubert     vfprintf_unfiltered (gdb_stderr, y, args);
14935796c8dcSSimon Schubert   else
14945796c8dcSSimon Schubert     {
14955796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
14965796c8dcSSimon Schubert       vfprintf_unfiltered (gdb_stderr, y, args);
14975796c8dcSSimon Schubert     }
14985796c8dcSSimon Schubert   va_end (args);
14995796c8dcSSimon Schubert }
15005796c8dcSSimon Schubert 
1501cf7f2e2dSJohn Marino /* Implementation of the exp_descriptor method operator_check.  */
1502cf7f2e2dSJohn Marino 
1503cf7f2e2dSJohn Marino int
1504cf7f2e2dSJohn Marino operator_check_standard (struct expression *exp, int pos,
1505cf7f2e2dSJohn Marino 			 int (*objfile_func) (struct objfile *objfile,
1506cf7f2e2dSJohn Marino 					      void *data),
1507cf7f2e2dSJohn Marino 			 void *data)
1508cf7f2e2dSJohn Marino {
1509cf7f2e2dSJohn Marino   const union exp_element *const elts = exp->elts;
1510cf7f2e2dSJohn Marino   struct type *type = NULL;
1511cf7f2e2dSJohn Marino   struct objfile *objfile = NULL;
1512cf7f2e2dSJohn Marino 
1513cf7f2e2dSJohn Marino   /* Extended operators should have been already handled by exp_descriptor
1514cf7f2e2dSJohn Marino      iterate method of its specific language.  */
1515cf7f2e2dSJohn Marino   gdb_assert (elts[pos].opcode < OP_EXTENDED0);
1516cf7f2e2dSJohn Marino 
1517cf7f2e2dSJohn Marino   /* Track the callers of write_exp_elt_type for this table.  */
1518cf7f2e2dSJohn Marino 
1519cf7f2e2dSJohn Marino   switch (elts[pos].opcode)
1520cf7f2e2dSJohn Marino     {
1521cf7f2e2dSJohn Marino     case BINOP_VAL:
1522cf7f2e2dSJohn Marino     case OP_COMPLEX:
1523cf7f2e2dSJohn Marino     case OP_DECFLOAT:
1524cf7f2e2dSJohn Marino     case OP_DOUBLE:
1525cf7f2e2dSJohn Marino     case OP_LONG:
1526cf7f2e2dSJohn Marino     case OP_SCOPE:
1527cf7f2e2dSJohn Marino     case OP_TYPE:
1528cf7f2e2dSJohn Marino     case UNOP_CAST:
1529cf7f2e2dSJohn Marino     case UNOP_DYNAMIC_CAST:
1530cf7f2e2dSJohn Marino     case UNOP_REINTERPRET_CAST:
1531cf7f2e2dSJohn Marino     case UNOP_MAX:
1532cf7f2e2dSJohn Marino     case UNOP_MEMVAL:
1533cf7f2e2dSJohn Marino     case UNOP_MIN:
1534cf7f2e2dSJohn Marino       type = elts[pos + 1].type;
1535cf7f2e2dSJohn Marino       break;
1536cf7f2e2dSJohn Marino 
1537cf7f2e2dSJohn Marino     case TYPE_INSTANCE:
1538cf7f2e2dSJohn Marino       {
1539cf7f2e2dSJohn Marino 	LONGEST arg, nargs = elts[pos + 1].longconst;
1540cf7f2e2dSJohn Marino 
1541cf7f2e2dSJohn Marino 	for (arg = 0; arg < nargs; arg++)
1542cf7f2e2dSJohn Marino 	  {
1543cf7f2e2dSJohn Marino 	    struct type *type = elts[pos + 2 + arg].type;
1544cf7f2e2dSJohn Marino 	    struct objfile *objfile = TYPE_OBJFILE (type);
1545cf7f2e2dSJohn Marino 
1546cf7f2e2dSJohn Marino 	    if (objfile && (*objfile_func) (objfile, data))
1547cf7f2e2dSJohn Marino 	      return 1;
1548cf7f2e2dSJohn Marino 	  }
1549cf7f2e2dSJohn Marino       }
1550cf7f2e2dSJohn Marino       break;
1551cf7f2e2dSJohn Marino 
1552cf7f2e2dSJohn Marino     case UNOP_MEMVAL_TLS:
1553cf7f2e2dSJohn Marino       objfile = elts[pos + 1].objfile;
1554cf7f2e2dSJohn Marino       type = elts[pos + 2].type;
1555cf7f2e2dSJohn Marino       break;
1556cf7f2e2dSJohn Marino 
1557cf7f2e2dSJohn Marino     case OP_VAR_VALUE:
1558cf7f2e2dSJohn Marino       {
1559cf7f2e2dSJohn Marino 	const struct block *const block = elts[pos + 1].block;
1560cf7f2e2dSJohn Marino 	const struct symbol *const symbol = elts[pos + 2].symbol;
1561cf7f2e2dSJohn Marino 
1562cf7f2e2dSJohn Marino 	/* Check objfile where the variable itself is placed.
1563cf7f2e2dSJohn Marino 	   SYMBOL_OBJ_SECTION (symbol) may be NULL.  */
1564cf7f2e2dSJohn Marino 	if ((*objfile_func) (SYMBOL_SYMTAB (symbol)->objfile, data))
1565cf7f2e2dSJohn Marino 	  return 1;
1566cf7f2e2dSJohn Marino 
1567cf7f2e2dSJohn Marino 	/* Check objfile where is placed the code touching the variable.  */
1568cf7f2e2dSJohn Marino 	objfile = lookup_objfile_from_block (block);
1569cf7f2e2dSJohn Marino 
1570cf7f2e2dSJohn Marino 	type = SYMBOL_TYPE (symbol);
1571cf7f2e2dSJohn Marino       }
1572cf7f2e2dSJohn Marino       break;
1573cf7f2e2dSJohn Marino     }
1574cf7f2e2dSJohn Marino 
1575cf7f2e2dSJohn Marino   /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL.  */
1576cf7f2e2dSJohn Marino 
1577cf7f2e2dSJohn Marino   if (type && TYPE_OBJFILE (type)
1578cf7f2e2dSJohn Marino       && (*objfile_func) (TYPE_OBJFILE (type), data))
1579cf7f2e2dSJohn Marino     return 1;
1580cf7f2e2dSJohn Marino   if (objfile && (*objfile_func) (objfile, data))
1581cf7f2e2dSJohn Marino     return 1;
1582cf7f2e2dSJohn Marino 
1583cf7f2e2dSJohn Marino   return 0;
1584cf7f2e2dSJohn Marino }
1585cf7f2e2dSJohn Marino 
1586cf7f2e2dSJohn Marino /* Call OBJFILE_FUNC for any TYPE and OBJFILE found being referenced by EXP.
1587cf7f2e2dSJohn Marino    The functions are never called with NULL OBJFILE.  Functions get passed an
1588cf7f2e2dSJohn Marino    arbitrary caller supplied DATA pointer.  If any of the functions returns
1589cf7f2e2dSJohn Marino    non-zero value then (any other) non-zero value is immediately returned to
1590cf7f2e2dSJohn Marino    the caller.  Otherwise zero is returned after iterating through whole EXP.
1591cf7f2e2dSJohn Marino    */
1592cf7f2e2dSJohn Marino 
1593cf7f2e2dSJohn Marino static int
1594cf7f2e2dSJohn Marino exp_iterate (struct expression *exp,
1595cf7f2e2dSJohn Marino 	     int (*objfile_func) (struct objfile *objfile, void *data),
1596cf7f2e2dSJohn Marino 	     void *data)
1597cf7f2e2dSJohn Marino {
1598cf7f2e2dSJohn Marino   int endpos;
1599cf7f2e2dSJohn Marino 
1600cf7f2e2dSJohn Marino   for (endpos = exp->nelts; endpos > 0; )
1601cf7f2e2dSJohn Marino     {
1602cf7f2e2dSJohn Marino       int pos, args, oplen = 0;
1603cf7f2e2dSJohn Marino 
1604cf7f2e2dSJohn Marino       operator_length (exp, endpos, &oplen, &args);
1605cf7f2e2dSJohn Marino       gdb_assert (oplen > 0);
1606cf7f2e2dSJohn Marino 
1607cf7f2e2dSJohn Marino       pos = endpos - oplen;
1608cf7f2e2dSJohn Marino       if (exp->language_defn->la_exp_desc->operator_check (exp, pos,
1609cf7f2e2dSJohn Marino 							   objfile_func, data))
1610cf7f2e2dSJohn Marino 	return 1;
1611cf7f2e2dSJohn Marino 
1612cf7f2e2dSJohn Marino       endpos = pos;
1613cf7f2e2dSJohn Marino     }
1614cf7f2e2dSJohn Marino 
1615cf7f2e2dSJohn Marino   return 0;
1616cf7f2e2dSJohn Marino }
1617cf7f2e2dSJohn Marino 
1618cf7f2e2dSJohn Marino /* Helper for exp_uses_objfile.  */
1619cf7f2e2dSJohn Marino 
1620cf7f2e2dSJohn Marino static int
1621cf7f2e2dSJohn Marino exp_uses_objfile_iter (struct objfile *exp_objfile, void *objfile_voidp)
1622cf7f2e2dSJohn Marino {
1623cf7f2e2dSJohn Marino   struct objfile *objfile = objfile_voidp;
1624cf7f2e2dSJohn Marino 
1625cf7f2e2dSJohn Marino   if (exp_objfile->separate_debug_objfile_backlink)
1626cf7f2e2dSJohn Marino     exp_objfile = exp_objfile->separate_debug_objfile_backlink;
1627cf7f2e2dSJohn Marino 
1628cf7f2e2dSJohn Marino   return exp_objfile == objfile;
1629cf7f2e2dSJohn Marino }
1630cf7f2e2dSJohn Marino 
1631cf7f2e2dSJohn Marino /* Return 1 if EXP uses OBJFILE (and will become dangling when OBJFILE
1632cf7f2e2dSJohn Marino    is unloaded), otherwise return 0.  OBJFILE must not be a separate debug info
1633cf7f2e2dSJohn Marino    file.  */
1634cf7f2e2dSJohn Marino 
1635cf7f2e2dSJohn Marino int
1636cf7f2e2dSJohn Marino exp_uses_objfile (struct expression *exp, struct objfile *objfile)
1637cf7f2e2dSJohn Marino {
1638cf7f2e2dSJohn Marino   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
1639cf7f2e2dSJohn Marino 
1640cf7f2e2dSJohn Marino   return exp_iterate (exp, exp_uses_objfile_iter, objfile);
1641cf7f2e2dSJohn Marino }
1642cf7f2e2dSJohn Marino 
16435796c8dcSSimon Schubert void
16445796c8dcSSimon Schubert _initialize_parse (void)
16455796c8dcSSimon Schubert {
16465796c8dcSSimon Schubert   type_stack_size = 80;
16475796c8dcSSimon Schubert   type_stack_depth = 0;
16485796c8dcSSimon Schubert   type_stack = (union type_stack_elt *)
16495796c8dcSSimon Schubert     xmalloc (type_stack_size * sizeof (*type_stack));
16505796c8dcSSimon Schubert 
16515796c8dcSSimon Schubert   add_setshow_zinteger_cmd ("expression", class_maintenance,
1652*c50c785cSJohn Marino 			    &expressiondebug,
1653*c50c785cSJohn Marino 			    _("Set expression debugging."),
1654*c50c785cSJohn Marino 			    _("Show expression debugging."),
1655*c50c785cSJohn Marino 			    _("When non-zero, the internal representation "
1656*c50c785cSJohn Marino 			      "of expressions will be printed."),
16575796c8dcSSimon Schubert 			    NULL,
16585796c8dcSSimon Schubert 			    show_expressiondebug,
16595796c8dcSSimon Schubert 			    &setdebuglist, &showdebuglist);
1660cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("parser", class_maintenance,
1661*c50c785cSJohn Marino 			    &parser_debug,
1662*c50c785cSJohn Marino 			   _("Set parser debugging."),
1663*c50c785cSJohn Marino 			   _("Show parser debugging."),
1664*c50c785cSJohn Marino 			   _("When non-zero, expression parser "
1665*c50c785cSJohn Marino 			     "tracing will be enabled."),
1666cf7f2e2dSJohn Marino 			    NULL,
1667cf7f2e2dSJohn Marino 			    show_parserdebug,
1668cf7f2e2dSJohn Marino 			    &setdebuglist, &showdebuglist);
16695796c8dcSSimon Schubert }
1670