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