xref: /dflybsd-src/contrib/gdb-7/gdb/parse.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Parse expressions for GDB.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4*5796c8dcSSimon Schubert    1998, 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2009
5*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    Modified from expread.y by the Department of Computer Science at the
8*5796c8dcSSimon Schubert    State University of New York at Buffalo, 1991.
9*5796c8dcSSimon Schubert 
10*5796c8dcSSimon Schubert    This file is part of GDB.
11*5796c8dcSSimon Schubert 
12*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
13*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
14*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
15*5796c8dcSSimon Schubert    (at your option) any later version.
16*5796c8dcSSimon Schubert 
17*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
18*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
19*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20*5796c8dcSSimon Schubert    GNU General Public License for more details.
21*5796c8dcSSimon Schubert 
22*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
23*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
24*5796c8dcSSimon Schubert 
25*5796c8dcSSimon Schubert /* Parse an expression from text in a string,
26*5796c8dcSSimon Schubert    and return the result as a  struct expression  pointer.
27*5796c8dcSSimon Schubert    That structure contains arithmetic operations in reverse polish,
28*5796c8dcSSimon Schubert    with constants represented by operations that are followed by special data.
29*5796c8dcSSimon Schubert    See expression.h for the details of the format.
30*5796c8dcSSimon Schubert    What is important here is that it can be built up sequentially
31*5796c8dcSSimon Schubert    during the process of parsing; the lower levels of the tree always
32*5796c8dcSSimon Schubert    come first in the result.  */
33*5796c8dcSSimon Schubert 
34*5796c8dcSSimon Schubert #include <ctype.h>
35*5796c8dcSSimon Schubert 
36*5796c8dcSSimon Schubert #include "defs.h"
37*5796c8dcSSimon Schubert #include "arch-utils.h"
38*5796c8dcSSimon Schubert #include "gdb_string.h"
39*5796c8dcSSimon Schubert #include "symtab.h"
40*5796c8dcSSimon Schubert #include "gdbtypes.h"
41*5796c8dcSSimon Schubert #include "frame.h"
42*5796c8dcSSimon Schubert #include "expression.h"
43*5796c8dcSSimon Schubert #include "value.h"
44*5796c8dcSSimon Schubert #include "command.h"
45*5796c8dcSSimon Schubert #include "language.h"
46*5796c8dcSSimon Schubert #include "f-lang.h"
47*5796c8dcSSimon Schubert #include "parser-defs.h"
48*5796c8dcSSimon Schubert #include "gdbcmd.h"
49*5796c8dcSSimon Schubert #include "symfile.h"		/* for overlay functions */
50*5796c8dcSSimon Schubert #include "inferior.h"
51*5796c8dcSSimon Schubert #include "doublest.h"
52*5796c8dcSSimon Schubert #include "gdb_assert.h"
53*5796c8dcSSimon Schubert #include "block.h"
54*5796c8dcSSimon Schubert #include "source.h"
55*5796c8dcSSimon Schubert #include "objfiles.h"
56*5796c8dcSSimon Schubert #include "exceptions.h"
57*5796c8dcSSimon Schubert #include "user-regs.h"
58*5796c8dcSSimon Schubert 
59*5796c8dcSSimon Schubert /* Standard set of definitions for printing, dumping, prefixifying,
60*5796c8dcSSimon Schubert  * and evaluating expressions.  */
61*5796c8dcSSimon Schubert 
62*5796c8dcSSimon Schubert const struct exp_descriptor exp_descriptor_standard =
63*5796c8dcSSimon Schubert   {
64*5796c8dcSSimon Schubert     print_subexp_standard,
65*5796c8dcSSimon Schubert     operator_length_standard,
66*5796c8dcSSimon Schubert     op_name_standard,
67*5796c8dcSSimon Schubert     dump_subexp_body_standard,
68*5796c8dcSSimon Schubert     evaluate_subexp_standard
69*5796c8dcSSimon Schubert   };
70*5796c8dcSSimon Schubert 
71*5796c8dcSSimon Schubert /* Global variables declared in parser-defs.h (and commented there).  */
72*5796c8dcSSimon Schubert struct expression *expout;
73*5796c8dcSSimon Schubert int expout_size;
74*5796c8dcSSimon Schubert int expout_ptr;
75*5796c8dcSSimon Schubert struct block *expression_context_block;
76*5796c8dcSSimon Schubert CORE_ADDR expression_context_pc;
77*5796c8dcSSimon Schubert struct block *innermost_block;
78*5796c8dcSSimon Schubert int arglist_len;
79*5796c8dcSSimon Schubert union type_stack_elt *type_stack;
80*5796c8dcSSimon Schubert int type_stack_depth, type_stack_size;
81*5796c8dcSSimon Schubert char *lexptr;
82*5796c8dcSSimon Schubert char *prev_lexptr;
83*5796c8dcSSimon Schubert int paren_depth;
84*5796c8dcSSimon Schubert int comma_terminates;
85*5796c8dcSSimon Schubert 
86*5796c8dcSSimon Schubert /* True if parsing an expression to find a field reference.  This is
87*5796c8dcSSimon Schubert    only used by completion.  */
88*5796c8dcSSimon Schubert int in_parse_field;
89*5796c8dcSSimon Schubert 
90*5796c8dcSSimon Schubert /* The index of the last struct expression directly before a '.' or
91*5796c8dcSSimon Schubert    '->'.  This is set when parsing and is only used when completing a
92*5796c8dcSSimon Schubert    field name.  It is -1 if no dereference operation was found.  */
93*5796c8dcSSimon Schubert static int expout_last_struct = -1;
94*5796c8dcSSimon Schubert 
95*5796c8dcSSimon Schubert /* A temporary buffer for identifiers, so we can null-terminate them.
96*5796c8dcSSimon Schubert 
97*5796c8dcSSimon Schubert    We allocate this with xrealloc.  parse_exp_1 used to allocate with
98*5796c8dcSSimon Schubert    alloca, using the size of the whole expression as a conservative
99*5796c8dcSSimon Schubert    estimate of the space needed.  However, macro expansion can
100*5796c8dcSSimon Schubert    introduce names longer than the original expression; there's no
101*5796c8dcSSimon Schubert    practical way to know beforehand how large that might be.  */
102*5796c8dcSSimon Schubert char *namecopy;
103*5796c8dcSSimon Schubert size_t namecopy_size;
104*5796c8dcSSimon Schubert 
105*5796c8dcSSimon Schubert static int expressiondebug = 0;
106*5796c8dcSSimon Schubert static void
107*5796c8dcSSimon Schubert show_expressiondebug (struct ui_file *file, int from_tty,
108*5796c8dcSSimon Schubert 		      struct cmd_list_element *c, const char *value)
109*5796c8dcSSimon Schubert {
110*5796c8dcSSimon Schubert   fprintf_filtered (file, _("Expression debugging is %s.\n"), value);
111*5796c8dcSSimon Schubert }
112*5796c8dcSSimon Schubert 
113*5796c8dcSSimon Schubert static void free_funcalls (void *ignore);
114*5796c8dcSSimon Schubert 
115*5796c8dcSSimon Schubert static int prefixify_expression (struct expression *);
116*5796c8dcSSimon Schubert 
117*5796c8dcSSimon Schubert static int prefixify_subexp (struct expression *, struct expression *, int,
118*5796c8dcSSimon Schubert 			     int);
119*5796c8dcSSimon Schubert 
120*5796c8dcSSimon Schubert static struct expression *parse_exp_in_context (char **, struct block *, int,
121*5796c8dcSSimon Schubert 						int, int *);
122*5796c8dcSSimon Schubert 
123*5796c8dcSSimon Schubert void _initialize_parse (void);
124*5796c8dcSSimon Schubert 
125*5796c8dcSSimon Schubert /* Data structure for saving values of arglist_len for function calls whose
126*5796c8dcSSimon Schubert    arguments contain other function calls.  */
127*5796c8dcSSimon Schubert 
128*5796c8dcSSimon Schubert struct funcall
129*5796c8dcSSimon Schubert   {
130*5796c8dcSSimon Schubert     struct funcall *next;
131*5796c8dcSSimon Schubert     int arglist_len;
132*5796c8dcSSimon Schubert   };
133*5796c8dcSSimon Schubert 
134*5796c8dcSSimon Schubert static struct funcall *funcall_chain;
135*5796c8dcSSimon Schubert 
136*5796c8dcSSimon Schubert /* Begin counting arguments for a function call,
137*5796c8dcSSimon Schubert    saving the data about any containing call.  */
138*5796c8dcSSimon Schubert 
139*5796c8dcSSimon Schubert void
140*5796c8dcSSimon Schubert start_arglist (void)
141*5796c8dcSSimon Schubert {
142*5796c8dcSSimon Schubert   struct funcall *new;
143*5796c8dcSSimon Schubert 
144*5796c8dcSSimon Schubert   new = (struct funcall *) xmalloc (sizeof (struct funcall));
145*5796c8dcSSimon Schubert   new->next = funcall_chain;
146*5796c8dcSSimon Schubert   new->arglist_len = arglist_len;
147*5796c8dcSSimon Schubert   arglist_len = 0;
148*5796c8dcSSimon Schubert   funcall_chain = new;
149*5796c8dcSSimon Schubert }
150*5796c8dcSSimon Schubert 
151*5796c8dcSSimon Schubert /* Return the number of arguments in a function call just terminated,
152*5796c8dcSSimon Schubert    and restore the data for the containing function call.  */
153*5796c8dcSSimon Schubert 
154*5796c8dcSSimon Schubert int
155*5796c8dcSSimon Schubert end_arglist (void)
156*5796c8dcSSimon Schubert {
157*5796c8dcSSimon Schubert   int val = arglist_len;
158*5796c8dcSSimon Schubert   struct funcall *call = funcall_chain;
159*5796c8dcSSimon Schubert   funcall_chain = call->next;
160*5796c8dcSSimon Schubert   arglist_len = call->arglist_len;
161*5796c8dcSSimon Schubert   xfree (call);
162*5796c8dcSSimon Schubert   return val;
163*5796c8dcSSimon Schubert }
164*5796c8dcSSimon Schubert 
165*5796c8dcSSimon Schubert /* Free everything in the funcall chain.
166*5796c8dcSSimon Schubert    Used when there is an error inside parsing.  */
167*5796c8dcSSimon Schubert 
168*5796c8dcSSimon Schubert static void
169*5796c8dcSSimon Schubert free_funcalls (void *ignore)
170*5796c8dcSSimon Schubert {
171*5796c8dcSSimon Schubert   struct funcall *call, *next;
172*5796c8dcSSimon Schubert 
173*5796c8dcSSimon Schubert   for (call = funcall_chain; call; call = next)
174*5796c8dcSSimon Schubert     {
175*5796c8dcSSimon Schubert       next = call->next;
176*5796c8dcSSimon Schubert       xfree (call);
177*5796c8dcSSimon Schubert     }
178*5796c8dcSSimon Schubert }
179*5796c8dcSSimon Schubert 
180*5796c8dcSSimon Schubert /* This page contains the functions for adding data to the  struct expression
181*5796c8dcSSimon Schubert    being constructed.  */
182*5796c8dcSSimon Schubert 
183*5796c8dcSSimon Schubert /* Add one element to the end of the expression.  */
184*5796c8dcSSimon Schubert 
185*5796c8dcSSimon Schubert /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
186*5796c8dcSSimon Schubert    a register through here */
187*5796c8dcSSimon Schubert 
188*5796c8dcSSimon Schubert void
189*5796c8dcSSimon Schubert write_exp_elt (union exp_element expelt)
190*5796c8dcSSimon Schubert {
191*5796c8dcSSimon Schubert   if (expout_ptr >= expout_size)
192*5796c8dcSSimon Schubert     {
193*5796c8dcSSimon Schubert       expout_size *= 2;
194*5796c8dcSSimon Schubert       expout = (struct expression *)
195*5796c8dcSSimon Schubert 	xrealloc ((char *) expout, sizeof (struct expression)
196*5796c8dcSSimon Schubert 		  + EXP_ELEM_TO_BYTES (expout_size));
197*5796c8dcSSimon Schubert     }
198*5796c8dcSSimon Schubert   expout->elts[expout_ptr++] = expelt;
199*5796c8dcSSimon Schubert }
200*5796c8dcSSimon Schubert 
201*5796c8dcSSimon Schubert void
202*5796c8dcSSimon Schubert write_exp_elt_opcode (enum exp_opcode expelt)
203*5796c8dcSSimon Schubert {
204*5796c8dcSSimon Schubert   union exp_element tmp;
205*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
206*5796c8dcSSimon Schubert 
207*5796c8dcSSimon Schubert   tmp.opcode = expelt;
208*5796c8dcSSimon Schubert 
209*5796c8dcSSimon Schubert   write_exp_elt (tmp);
210*5796c8dcSSimon Schubert }
211*5796c8dcSSimon Schubert 
212*5796c8dcSSimon Schubert void
213*5796c8dcSSimon Schubert write_exp_elt_sym (struct symbol *expelt)
214*5796c8dcSSimon Schubert {
215*5796c8dcSSimon Schubert   union exp_element tmp;
216*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
217*5796c8dcSSimon Schubert 
218*5796c8dcSSimon Schubert   tmp.symbol = expelt;
219*5796c8dcSSimon Schubert 
220*5796c8dcSSimon Schubert   write_exp_elt (tmp);
221*5796c8dcSSimon Schubert }
222*5796c8dcSSimon Schubert 
223*5796c8dcSSimon Schubert void
224*5796c8dcSSimon Schubert write_exp_elt_block (struct block *b)
225*5796c8dcSSimon Schubert {
226*5796c8dcSSimon Schubert   union exp_element tmp;
227*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
228*5796c8dcSSimon Schubert   tmp.block = b;
229*5796c8dcSSimon Schubert   write_exp_elt (tmp);
230*5796c8dcSSimon Schubert }
231*5796c8dcSSimon Schubert 
232*5796c8dcSSimon Schubert void
233*5796c8dcSSimon Schubert write_exp_elt_objfile (struct objfile *objfile)
234*5796c8dcSSimon Schubert {
235*5796c8dcSSimon Schubert   union exp_element tmp;
236*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
237*5796c8dcSSimon Schubert   tmp.objfile = objfile;
238*5796c8dcSSimon Schubert   write_exp_elt (tmp);
239*5796c8dcSSimon Schubert }
240*5796c8dcSSimon Schubert 
241*5796c8dcSSimon Schubert void
242*5796c8dcSSimon Schubert write_exp_elt_longcst (LONGEST expelt)
243*5796c8dcSSimon Schubert {
244*5796c8dcSSimon Schubert   union exp_element tmp;
245*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
246*5796c8dcSSimon Schubert 
247*5796c8dcSSimon Schubert   tmp.longconst = expelt;
248*5796c8dcSSimon Schubert 
249*5796c8dcSSimon Schubert   write_exp_elt (tmp);
250*5796c8dcSSimon Schubert }
251*5796c8dcSSimon Schubert 
252*5796c8dcSSimon Schubert void
253*5796c8dcSSimon Schubert write_exp_elt_dblcst (DOUBLEST expelt)
254*5796c8dcSSimon Schubert {
255*5796c8dcSSimon Schubert   union exp_element tmp;
256*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
257*5796c8dcSSimon Schubert 
258*5796c8dcSSimon Schubert   tmp.doubleconst = expelt;
259*5796c8dcSSimon Schubert 
260*5796c8dcSSimon Schubert   write_exp_elt (tmp);
261*5796c8dcSSimon Schubert }
262*5796c8dcSSimon Schubert 
263*5796c8dcSSimon Schubert void
264*5796c8dcSSimon Schubert write_exp_elt_decfloatcst (gdb_byte expelt[16])
265*5796c8dcSSimon Schubert {
266*5796c8dcSSimon Schubert   union exp_element tmp;
267*5796c8dcSSimon Schubert   int index;
268*5796c8dcSSimon Schubert 
269*5796c8dcSSimon Schubert   for (index = 0; index < 16; index++)
270*5796c8dcSSimon Schubert     tmp.decfloatconst[index] = expelt[index];
271*5796c8dcSSimon Schubert 
272*5796c8dcSSimon Schubert   write_exp_elt (tmp);
273*5796c8dcSSimon Schubert }
274*5796c8dcSSimon Schubert 
275*5796c8dcSSimon Schubert void
276*5796c8dcSSimon Schubert write_exp_elt_type (struct type *expelt)
277*5796c8dcSSimon Schubert {
278*5796c8dcSSimon Schubert   union exp_element tmp;
279*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
280*5796c8dcSSimon Schubert 
281*5796c8dcSSimon Schubert   tmp.type = expelt;
282*5796c8dcSSimon Schubert 
283*5796c8dcSSimon Schubert   write_exp_elt (tmp);
284*5796c8dcSSimon Schubert }
285*5796c8dcSSimon Schubert 
286*5796c8dcSSimon Schubert void
287*5796c8dcSSimon Schubert write_exp_elt_intern (struct internalvar *expelt)
288*5796c8dcSSimon Schubert {
289*5796c8dcSSimon Schubert   union exp_element tmp;
290*5796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
291*5796c8dcSSimon Schubert 
292*5796c8dcSSimon Schubert   tmp.internalvar = expelt;
293*5796c8dcSSimon Schubert 
294*5796c8dcSSimon Schubert   write_exp_elt (tmp);
295*5796c8dcSSimon Schubert }
296*5796c8dcSSimon Schubert 
297*5796c8dcSSimon Schubert /* Add a string constant to the end of the expression.
298*5796c8dcSSimon Schubert 
299*5796c8dcSSimon Schubert    String constants are stored by first writing an expression element
300*5796c8dcSSimon Schubert    that contains the length of the string, then stuffing the string
301*5796c8dcSSimon Schubert    constant itself into however many expression elements are needed
302*5796c8dcSSimon Schubert    to hold it, and then writing another expression element that contains
303*5796c8dcSSimon Schubert    the length of the string.  I.E. an expression element at each end of
304*5796c8dcSSimon Schubert    the string records the string length, so you can skip over the
305*5796c8dcSSimon Schubert    expression elements containing the actual string bytes from either
306*5796c8dcSSimon Schubert    end of the string.  Note that this also allows gdb to handle
307*5796c8dcSSimon Schubert    strings with embedded null bytes, as is required for some languages.
308*5796c8dcSSimon Schubert 
309*5796c8dcSSimon Schubert    Don't be fooled by the fact that the string is null byte terminated,
310*5796c8dcSSimon Schubert    this is strictly for the convenience of debugging gdb itself.
311*5796c8dcSSimon Schubert    Gdb does not depend up the string being null terminated, since the
312*5796c8dcSSimon Schubert    actual length is recorded in expression elements at each end of the
313*5796c8dcSSimon Schubert    string.  The null byte is taken into consideration when computing how
314*5796c8dcSSimon Schubert    many expression elements are required to hold the string constant, of
315*5796c8dcSSimon Schubert    course. */
316*5796c8dcSSimon Schubert 
317*5796c8dcSSimon Schubert 
318*5796c8dcSSimon Schubert void
319*5796c8dcSSimon Schubert write_exp_string (struct stoken str)
320*5796c8dcSSimon Schubert {
321*5796c8dcSSimon Schubert   int len = str.length;
322*5796c8dcSSimon Schubert   int lenelt;
323*5796c8dcSSimon Schubert   char *strdata;
324*5796c8dcSSimon Schubert 
325*5796c8dcSSimon Schubert   /* Compute the number of expression elements required to hold the string
326*5796c8dcSSimon Schubert      (including a null byte terminator), along with one expression element
327*5796c8dcSSimon Schubert      at each end to record the actual string length (not including the
328*5796c8dcSSimon Schubert      null byte terminator). */
329*5796c8dcSSimon Schubert 
330*5796c8dcSSimon Schubert   lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
331*5796c8dcSSimon Schubert 
332*5796c8dcSSimon Schubert   /* Ensure that we have enough available expression elements to store
333*5796c8dcSSimon Schubert      everything. */
334*5796c8dcSSimon Schubert 
335*5796c8dcSSimon Schubert   if ((expout_ptr + lenelt) >= expout_size)
336*5796c8dcSSimon Schubert     {
337*5796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
338*5796c8dcSSimon Schubert       expout = (struct expression *)
339*5796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
340*5796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
341*5796c8dcSSimon Schubert     }
342*5796c8dcSSimon Schubert 
343*5796c8dcSSimon Schubert   /* Write the leading length expression element (which advances the current
344*5796c8dcSSimon Schubert      expression element index), then write the string constant followed by a
345*5796c8dcSSimon Schubert      terminating null byte, and then write the trailing length expression
346*5796c8dcSSimon Schubert      element. */
347*5796c8dcSSimon Schubert 
348*5796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) len);
349*5796c8dcSSimon Schubert   strdata = (char *) &expout->elts[expout_ptr];
350*5796c8dcSSimon Schubert   memcpy (strdata, str.ptr, len);
351*5796c8dcSSimon Schubert   *(strdata + len) = '\0';
352*5796c8dcSSimon Schubert   expout_ptr += lenelt - 2;
353*5796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) len);
354*5796c8dcSSimon Schubert }
355*5796c8dcSSimon Schubert 
356*5796c8dcSSimon Schubert /* Add a vector of string constants to the end of the expression.
357*5796c8dcSSimon Schubert 
358*5796c8dcSSimon Schubert    This adds an OP_STRING operation, but encodes the contents
359*5796c8dcSSimon Schubert    differently from write_exp_string.  The language is expected to
360*5796c8dcSSimon Schubert    handle evaluation of this expression itself.
361*5796c8dcSSimon Schubert 
362*5796c8dcSSimon Schubert    After the usual OP_STRING header, TYPE is written into the
363*5796c8dcSSimon Schubert    expression as a long constant.  The interpretation of this field is
364*5796c8dcSSimon Schubert    up to the language evaluator.
365*5796c8dcSSimon Schubert 
366*5796c8dcSSimon Schubert    Next, each string in VEC is written.  The length is written as a
367*5796c8dcSSimon Schubert    long constant, followed by the contents of the string.  */
368*5796c8dcSSimon Schubert 
369*5796c8dcSSimon Schubert void
370*5796c8dcSSimon Schubert write_exp_string_vector (int type, struct stoken_vector *vec)
371*5796c8dcSSimon Schubert {
372*5796c8dcSSimon Schubert   int i, n_slots, len;
373*5796c8dcSSimon Schubert 
374*5796c8dcSSimon Schubert   /* Compute the size.  We compute the size in number of slots to
375*5796c8dcSSimon Schubert      avoid issues with string padding.  */
376*5796c8dcSSimon Schubert   n_slots = 0;
377*5796c8dcSSimon Schubert   for (i = 0; i < vec->len; ++i)
378*5796c8dcSSimon Schubert     {
379*5796c8dcSSimon Schubert       /* One slot for the length of this element, plus the number of
380*5796c8dcSSimon Schubert 	 slots needed for this string.  */
381*5796c8dcSSimon Schubert       n_slots += 1 + BYTES_TO_EXP_ELEM (vec->tokens[i].length);
382*5796c8dcSSimon Schubert     }
383*5796c8dcSSimon Schubert 
384*5796c8dcSSimon Schubert   /* One more slot for the type of the string.  */
385*5796c8dcSSimon Schubert   ++n_slots;
386*5796c8dcSSimon Schubert 
387*5796c8dcSSimon Schubert   /* Now compute a phony string length.  */
388*5796c8dcSSimon Schubert   len = EXP_ELEM_TO_BYTES (n_slots) - 1;
389*5796c8dcSSimon Schubert 
390*5796c8dcSSimon Schubert   n_slots += 4;
391*5796c8dcSSimon Schubert   if ((expout_ptr + n_slots) >= expout_size)
392*5796c8dcSSimon Schubert     {
393*5796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + n_slots + 10);
394*5796c8dcSSimon Schubert       expout = (struct expression *)
395*5796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
396*5796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
397*5796c8dcSSimon Schubert     }
398*5796c8dcSSimon Schubert 
399*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_STRING);
400*5796c8dcSSimon Schubert   write_exp_elt_longcst (len);
401*5796c8dcSSimon Schubert   write_exp_elt_longcst (type);
402*5796c8dcSSimon Schubert 
403*5796c8dcSSimon Schubert   for (i = 0; i < vec->len; ++i)
404*5796c8dcSSimon Schubert     {
405*5796c8dcSSimon Schubert       write_exp_elt_longcst (vec->tokens[i].length);
406*5796c8dcSSimon Schubert       memcpy (&expout->elts[expout_ptr], vec->tokens[i].ptr,
407*5796c8dcSSimon Schubert 	      vec->tokens[i].length);
408*5796c8dcSSimon Schubert       expout_ptr += BYTES_TO_EXP_ELEM (vec->tokens[i].length);
409*5796c8dcSSimon Schubert     }
410*5796c8dcSSimon Schubert 
411*5796c8dcSSimon Schubert   write_exp_elt_longcst (len);
412*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_STRING);
413*5796c8dcSSimon Schubert }
414*5796c8dcSSimon Schubert 
415*5796c8dcSSimon Schubert /* Add a bitstring constant to the end of the expression.
416*5796c8dcSSimon Schubert 
417*5796c8dcSSimon Schubert    Bitstring constants are stored by first writing an expression element
418*5796c8dcSSimon Schubert    that contains the length of the bitstring (in bits), then stuffing the
419*5796c8dcSSimon Schubert    bitstring constant itself into however many expression elements are
420*5796c8dcSSimon Schubert    needed to hold it, and then writing another expression element that
421*5796c8dcSSimon Schubert    contains the length of the bitstring.  I.E. an expression element at
422*5796c8dcSSimon Schubert    each end of the bitstring records the bitstring length, so you can skip
423*5796c8dcSSimon Schubert    over the expression elements containing the actual bitstring bytes from
424*5796c8dcSSimon Schubert    either end of the bitstring. */
425*5796c8dcSSimon Schubert 
426*5796c8dcSSimon Schubert void
427*5796c8dcSSimon Schubert write_exp_bitstring (struct stoken str)
428*5796c8dcSSimon Schubert {
429*5796c8dcSSimon Schubert   int bits = str.length;	/* length in bits */
430*5796c8dcSSimon Schubert   int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
431*5796c8dcSSimon Schubert   int lenelt;
432*5796c8dcSSimon Schubert   char *strdata;
433*5796c8dcSSimon Schubert 
434*5796c8dcSSimon Schubert   /* Compute the number of expression elements required to hold the bitstring,
435*5796c8dcSSimon Schubert      along with one expression element at each end to record the actual
436*5796c8dcSSimon Schubert      bitstring length in bits. */
437*5796c8dcSSimon Schubert 
438*5796c8dcSSimon Schubert   lenelt = 2 + BYTES_TO_EXP_ELEM (len);
439*5796c8dcSSimon Schubert 
440*5796c8dcSSimon Schubert   /* Ensure that we have enough available expression elements to store
441*5796c8dcSSimon Schubert      everything. */
442*5796c8dcSSimon Schubert 
443*5796c8dcSSimon Schubert   if ((expout_ptr + lenelt) >= expout_size)
444*5796c8dcSSimon Schubert     {
445*5796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
446*5796c8dcSSimon Schubert       expout = (struct expression *)
447*5796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
448*5796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
449*5796c8dcSSimon Schubert     }
450*5796c8dcSSimon Schubert 
451*5796c8dcSSimon Schubert   /* Write the leading length expression element (which advances the current
452*5796c8dcSSimon Schubert      expression element index), then write the bitstring constant, and then
453*5796c8dcSSimon Schubert      write the trailing length expression element. */
454*5796c8dcSSimon Schubert 
455*5796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) bits);
456*5796c8dcSSimon Schubert   strdata = (char *) &expout->elts[expout_ptr];
457*5796c8dcSSimon Schubert   memcpy (strdata, str.ptr, len);
458*5796c8dcSSimon Schubert   expout_ptr += lenelt - 2;
459*5796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) bits);
460*5796c8dcSSimon Schubert }
461*5796c8dcSSimon Schubert 
462*5796c8dcSSimon Schubert /* Add the appropriate elements for a minimal symbol to the end of
463*5796c8dcSSimon Schubert    the expression.  */
464*5796c8dcSSimon Schubert 
465*5796c8dcSSimon Schubert void
466*5796c8dcSSimon Schubert write_exp_msymbol (struct minimal_symbol *msymbol)
467*5796c8dcSSimon Schubert {
468*5796c8dcSSimon Schubert   struct objfile *objfile = msymbol_objfile (msymbol);
469*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
470*5796c8dcSSimon Schubert 
471*5796c8dcSSimon Schubert   CORE_ADDR addr = SYMBOL_VALUE_ADDRESS (msymbol);
472*5796c8dcSSimon Schubert   struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
473*5796c8dcSSimon Schubert   enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
474*5796c8dcSSimon Schubert   CORE_ADDR pc;
475*5796c8dcSSimon Schubert 
476*5796c8dcSSimon Schubert   /* The minimal symbol might point to a function descriptor;
477*5796c8dcSSimon Schubert      resolve it to the actual code address instead.  */
478*5796c8dcSSimon Schubert   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
479*5796c8dcSSimon Schubert   if (pc != addr)
480*5796c8dcSSimon Schubert     {
481*5796c8dcSSimon Schubert       /* In this case, assume we have a code symbol instead of
482*5796c8dcSSimon Schubert 	 a data symbol.  */
483*5796c8dcSSimon Schubert       type = mst_text;
484*5796c8dcSSimon Schubert       section = NULL;
485*5796c8dcSSimon Schubert       addr = pc;
486*5796c8dcSSimon Schubert     }
487*5796c8dcSSimon Schubert 
488*5796c8dcSSimon Schubert   if (overlay_debugging)
489*5796c8dcSSimon Schubert     addr = symbol_overlayed_address (addr, section);
490*5796c8dcSSimon Schubert 
491*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LONG);
492*5796c8dcSSimon Schubert   /* Let's make the type big enough to hold a 64-bit address.  */
493*5796c8dcSSimon Schubert   write_exp_elt_type (objfile_type (objfile)->builtin_core_addr);
494*5796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) addr);
495*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LONG);
496*5796c8dcSSimon Schubert 
497*5796c8dcSSimon Schubert   if (section && section->the_bfd_section->flags & SEC_THREAD_LOCAL)
498*5796c8dcSSimon Schubert     {
499*5796c8dcSSimon Schubert       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
500*5796c8dcSSimon Schubert       write_exp_elt_objfile (objfile);
501*5796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_tls_symbol);
502*5796c8dcSSimon Schubert       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
503*5796c8dcSSimon Schubert       return;
504*5796c8dcSSimon Schubert     }
505*5796c8dcSSimon Schubert 
506*5796c8dcSSimon Schubert   write_exp_elt_opcode (UNOP_MEMVAL);
507*5796c8dcSSimon Schubert   switch (type)
508*5796c8dcSSimon Schubert     {
509*5796c8dcSSimon Schubert     case mst_text:
510*5796c8dcSSimon Schubert     case mst_file_text:
511*5796c8dcSSimon Schubert     case mst_solib_trampoline:
512*5796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_text_symbol);
513*5796c8dcSSimon Schubert       break;
514*5796c8dcSSimon Schubert 
515*5796c8dcSSimon Schubert     case mst_data:
516*5796c8dcSSimon Schubert     case mst_file_data:
517*5796c8dcSSimon Schubert     case mst_bss:
518*5796c8dcSSimon Schubert     case mst_file_bss:
519*5796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_data_symbol);
520*5796c8dcSSimon Schubert       break;
521*5796c8dcSSimon Schubert 
522*5796c8dcSSimon Schubert     default:
523*5796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_unknown_symbol);
524*5796c8dcSSimon Schubert       break;
525*5796c8dcSSimon Schubert     }
526*5796c8dcSSimon Schubert   write_exp_elt_opcode (UNOP_MEMVAL);
527*5796c8dcSSimon Schubert }
528*5796c8dcSSimon Schubert 
529*5796c8dcSSimon Schubert /* Mark the current index as the starting location of a structure
530*5796c8dcSSimon Schubert    expression.  This is used when completing on field names.  */
531*5796c8dcSSimon Schubert 
532*5796c8dcSSimon Schubert void
533*5796c8dcSSimon Schubert mark_struct_expression (void)
534*5796c8dcSSimon Schubert {
535*5796c8dcSSimon Schubert   expout_last_struct = expout_ptr;
536*5796c8dcSSimon Schubert }
537*5796c8dcSSimon Schubert 
538*5796c8dcSSimon Schubert 
539*5796c8dcSSimon Schubert /* Recognize tokens that start with '$'.  These include:
540*5796c8dcSSimon Schubert 
541*5796c8dcSSimon Schubert    $regname     A native register name or a "standard
542*5796c8dcSSimon Schubert    register name".
543*5796c8dcSSimon Schubert 
544*5796c8dcSSimon Schubert    $variable    A convenience variable with a name chosen
545*5796c8dcSSimon Schubert    by the user.
546*5796c8dcSSimon Schubert 
547*5796c8dcSSimon Schubert    $digits              Value history with index <digits>, starting
548*5796c8dcSSimon Schubert    from the first value which has index 1.
549*5796c8dcSSimon Schubert 
550*5796c8dcSSimon Schubert    $$digits     Value history with index <digits> relative
551*5796c8dcSSimon Schubert    to the last value.  I.E. $$0 is the last
552*5796c8dcSSimon Schubert    value, $$1 is the one previous to that, $$2
553*5796c8dcSSimon Schubert    is the one previous to $$1, etc.
554*5796c8dcSSimon Schubert 
555*5796c8dcSSimon Schubert    $ | $0 | $$0 The last value in the value history.
556*5796c8dcSSimon Schubert 
557*5796c8dcSSimon Schubert    $$           An abbreviation for the second to the last
558*5796c8dcSSimon Schubert    value in the value history, I.E. $$1
559*5796c8dcSSimon Schubert 
560*5796c8dcSSimon Schubert  */
561*5796c8dcSSimon Schubert 
562*5796c8dcSSimon Schubert void
563*5796c8dcSSimon Schubert write_dollar_variable (struct stoken str)
564*5796c8dcSSimon Schubert {
565*5796c8dcSSimon Schubert   struct symbol *sym = NULL;
566*5796c8dcSSimon Schubert   struct minimal_symbol *msym = NULL;
567*5796c8dcSSimon Schubert   struct internalvar *isym = NULL;
568*5796c8dcSSimon Schubert 
569*5796c8dcSSimon Schubert   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
570*5796c8dcSSimon Schubert      and $$digits (equivalent to $<-digits> if you could type that). */
571*5796c8dcSSimon Schubert 
572*5796c8dcSSimon Schubert   int negate = 0;
573*5796c8dcSSimon Schubert   int i = 1;
574*5796c8dcSSimon Schubert   /* Double dollar means negate the number and add -1 as well.
575*5796c8dcSSimon Schubert      Thus $$ alone means -1.  */
576*5796c8dcSSimon Schubert   if (str.length >= 2 && str.ptr[1] == '$')
577*5796c8dcSSimon Schubert     {
578*5796c8dcSSimon Schubert       negate = 1;
579*5796c8dcSSimon Schubert       i = 2;
580*5796c8dcSSimon Schubert     }
581*5796c8dcSSimon Schubert   if (i == str.length)
582*5796c8dcSSimon Schubert     {
583*5796c8dcSSimon Schubert       /* Just dollars (one or two) */
584*5796c8dcSSimon Schubert       i = -negate;
585*5796c8dcSSimon Schubert       goto handle_last;
586*5796c8dcSSimon Schubert     }
587*5796c8dcSSimon Schubert   /* Is the rest of the token digits?  */
588*5796c8dcSSimon Schubert   for (; i < str.length; i++)
589*5796c8dcSSimon Schubert     if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
590*5796c8dcSSimon Schubert       break;
591*5796c8dcSSimon Schubert   if (i == str.length)
592*5796c8dcSSimon Schubert     {
593*5796c8dcSSimon Schubert       i = atoi (str.ptr + 1 + negate);
594*5796c8dcSSimon Schubert       if (negate)
595*5796c8dcSSimon Schubert 	i = -i;
596*5796c8dcSSimon Schubert       goto handle_last;
597*5796c8dcSSimon Schubert     }
598*5796c8dcSSimon Schubert 
599*5796c8dcSSimon Schubert   /* Handle tokens that refer to machine registers:
600*5796c8dcSSimon Schubert      $ followed by a register name.  */
601*5796c8dcSSimon Schubert   i = user_reg_map_name_to_regnum (parse_gdbarch,
602*5796c8dcSSimon Schubert 				   str.ptr + 1, str.length - 1);
603*5796c8dcSSimon Schubert   if (i >= 0)
604*5796c8dcSSimon Schubert     goto handle_register;
605*5796c8dcSSimon Schubert 
606*5796c8dcSSimon Schubert   /* Any names starting with $ are probably debugger internal variables.  */
607*5796c8dcSSimon Schubert 
608*5796c8dcSSimon Schubert   isym = lookup_only_internalvar (copy_name (str) + 1);
609*5796c8dcSSimon Schubert   if (isym)
610*5796c8dcSSimon Schubert     {
611*5796c8dcSSimon Schubert       write_exp_elt_opcode (OP_INTERNALVAR);
612*5796c8dcSSimon Schubert       write_exp_elt_intern (isym);
613*5796c8dcSSimon Schubert       write_exp_elt_opcode (OP_INTERNALVAR);
614*5796c8dcSSimon Schubert       return;
615*5796c8dcSSimon Schubert     }
616*5796c8dcSSimon Schubert 
617*5796c8dcSSimon Schubert   /* On some systems, such as HP-UX and hppa-linux, certain system routines
618*5796c8dcSSimon Schubert      have names beginning with $ or $$.  Check for those, first. */
619*5796c8dcSSimon Schubert 
620*5796c8dcSSimon Schubert   sym = lookup_symbol (copy_name (str), (struct block *) NULL,
621*5796c8dcSSimon Schubert 		       VAR_DOMAIN, (int *) NULL);
622*5796c8dcSSimon Schubert   if (sym)
623*5796c8dcSSimon Schubert     {
624*5796c8dcSSimon Schubert       write_exp_elt_opcode (OP_VAR_VALUE);
625*5796c8dcSSimon Schubert       write_exp_elt_block (block_found);	/* set by lookup_symbol */
626*5796c8dcSSimon Schubert       write_exp_elt_sym (sym);
627*5796c8dcSSimon Schubert       write_exp_elt_opcode (OP_VAR_VALUE);
628*5796c8dcSSimon Schubert       return;
629*5796c8dcSSimon Schubert     }
630*5796c8dcSSimon Schubert   msym = lookup_minimal_symbol (copy_name (str), NULL, NULL);
631*5796c8dcSSimon Schubert   if (msym)
632*5796c8dcSSimon Schubert     {
633*5796c8dcSSimon Schubert       write_exp_msymbol (msym);
634*5796c8dcSSimon Schubert       return;
635*5796c8dcSSimon Schubert     }
636*5796c8dcSSimon Schubert 
637*5796c8dcSSimon Schubert   /* Any other names are assumed to be debugger internal variables.  */
638*5796c8dcSSimon Schubert 
639*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_INTERNALVAR);
640*5796c8dcSSimon Schubert   write_exp_elt_intern (create_internalvar (copy_name (str) + 1));
641*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_INTERNALVAR);
642*5796c8dcSSimon Schubert   return;
643*5796c8dcSSimon Schubert handle_last:
644*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LAST);
645*5796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) i);
646*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LAST);
647*5796c8dcSSimon Schubert   return;
648*5796c8dcSSimon Schubert handle_register:
649*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_REGISTER);
650*5796c8dcSSimon Schubert   str.length--;
651*5796c8dcSSimon Schubert   str.ptr++;
652*5796c8dcSSimon Schubert   write_exp_string (str);
653*5796c8dcSSimon Schubert   write_exp_elt_opcode (OP_REGISTER);
654*5796c8dcSSimon Schubert   return;
655*5796c8dcSSimon Schubert }
656*5796c8dcSSimon Schubert 
657*5796c8dcSSimon Schubert 
658*5796c8dcSSimon Schubert char *
659*5796c8dcSSimon Schubert find_template_name_end (char *p)
660*5796c8dcSSimon Schubert {
661*5796c8dcSSimon Schubert   int depth = 1;
662*5796c8dcSSimon Schubert   int just_seen_right = 0;
663*5796c8dcSSimon Schubert   int just_seen_colon = 0;
664*5796c8dcSSimon Schubert   int just_seen_space = 0;
665*5796c8dcSSimon Schubert 
666*5796c8dcSSimon Schubert   if (!p || (*p != '<'))
667*5796c8dcSSimon Schubert     return 0;
668*5796c8dcSSimon Schubert 
669*5796c8dcSSimon Schubert   while (*++p)
670*5796c8dcSSimon Schubert     {
671*5796c8dcSSimon Schubert       switch (*p)
672*5796c8dcSSimon Schubert 	{
673*5796c8dcSSimon Schubert 	case '\'':
674*5796c8dcSSimon Schubert 	case '\"':
675*5796c8dcSSimon Schubert 	case '{':
676*5796c8dcSSimon Schubert 	case '}':
677*5796c8dcSSimon Schubert 	  /* In future, may want to allow these?? */
678*5796c8dcSSimon Schubert 	  return 0;
679*5796c8dcSSimon Schubert 	case '<':
680*5796c8dcSSimon Schubert 	  depth++;		/* start nested template */
681*5796c8dcSSimon Schubert 	  if (just_seen_colon || just_seen_right || just_seen_space)
682*5796c8dcSSimon Schubert 	    return 0;		/* but not after : or :: or > or space */
683*5796c8dcSSimon Schubert 	  break;
684*5796c8dcSSimon Schubert 	case '>':
685*5796c8dcSSimon Schubert 	  if (just_seen_colon || just_seen_right)
686*5796c8dcSSimon Schubert 	    return 0;		/* end a (nested?) template */
687*5796c8dcSSimon Schubert 	  just_seen_right = 1;	/* but not after : or :: */
688*5796c8dcSSimon Schubert 	  if (--depth == 0)	/* also disallow >>, insist on > > */
689*5796c8dcSSimon Schubert 	    return ++p;		/* if outermost ended, return */
690*5796c8dcSSimon Schubert 	  break;
691*5796c8dcSSimon Schubert 	case ':':
692*5796c8dcSSimon Schubert 	  if (just_seen_space || (just_seen_colon > 1))
693*5796c8dcSSimon Schubert 	    return 0;		/* nested class spec coming up */
694*5796c8dcSSimon Schubert 	  just_seen_colon++;	/* we allow :: but not :::: */
695*5796c8dcSSimon Schubert 	  break;
696*5796c8dcSSimon Schubert 	case ' ':
697*5796c8dcSSimon Schubert 	  break;
698*5796c8dcSSimon Schubert 	default:
699*5796c8dcSSimon Schubert 	  if (!((*p >= 'a' && *p <= 'z') ||	/* allow token chars */
700*5796c8dcSSimon Schubert 		(*p >= 'A' && *p <= 'Z') ||
701*5796c8dcSSimon Schubert 		(*p >= '0' && *p <= '9') ||
702*5796c8dcSSimon Schubert 		(*p == '_') || (*p == ',') ||	/* commas for template args */
703*5796c8dcSSimon Schubert 		(*p == '&') || (*p == '*') ||	/* pointer and ref types */
704*5796c8dcSSimon Schubert 		(*p == '(') || (*p == ')') ||	/* function types */
705*5796c8dcSSimon Schubert 		(*p == '[') || (*p == ']')))	/* array types */
706*5796c8dcSSimon Schubert 	    return 0;
707*5796c8dcSSimon Schubert 	}
708*5796c8dcSSimon Schubert       if (*p != ' ')
709*5796c8dcSSimon Schubert 	just_seen_space = 0;
710*5796c8dcSSimon Schubert       if (*p != ':')
711*5796c8dcSSimon Schubert 	just_seen_colon = 0;
712*5796c8dcSSimon Schubert       if (*p != '>')
713*5796c8dcSSimon Schubert 	just_seen_right = 0;
714*5796c8dcSSimon Schubert     }
715*5796c8dcSSimon Schubert   return 0;
716*5796c8dcSSimon Schubert }
717*5796c8dcSSimon Schubert 
718*5796c8dcSSimon Schubert 
719*5796c8dcSSimon Schubert 
720*5796c8dcSSimon Schubert /* Return a null-terminated temporary copy of the name
721*5796c8dcSSimon Schubert    of a string token.  */
722*5796c8dcSSimon Schubert 
723*5796c8dcSSimon Schubert char *
724*5796c8dcSSimon Schubert copy_name (struct stoken token)
725*5796c8dcSSimon Schubert {
726*5796c8dcSSimon Schubert   /* Make sure there's enough space for the token.  */
727*5796c8dcSSimon Schubert   if (namecopy_size < token.length + 1)
728*5796c8dcSSimon Schubert     {
729*5796c8dcSSimon Schubert       namecopy_size = token.length + 1;
730*5796c8dcSSimon Schubert       namecopy = xrealloc (namecopy, token.length + 1);
731*5796c8dcSSimon Schubert     }
732*5796c8dcSSimon Schubert 
733*5796c8dcSSimon Schubert   memcpy (namecopy, token.ptr, token.length);
734*5796c8dcSSimon Schubert   namecopy[token.length] = 0;
735*5796c8dcSSimon Schubert 
736*5796c8dcSSimon Schubert   return namecopy;
737*5796c8dcSSimon Schubert }
738*5796c8dcSSimon Schubert 
739*5796c8dcSSimon Schubert /* Reverse an expression from suffix form (in which it is constructed)
740*5796c8dcSSimon Schubert    to prefix form (in which we can conveniently print or execute it).
741*5796c8dcSSimon Schubert    Ordinarily this always returns -1.  However, if EXPOUT_LAST_STRUCT
742*5796c8dcSSimon Schubert    is not -1 (i.e., we are trying to complete a field name), it will
743*5796c8dcSSimon Schubert    return the index of the subexpression which is the left-hand-side
744*5796c8dcSSimon Schubert    of the struct operation at EXPOUT_LAST_STRUCT.  */
745*5796c8dcSSimon Schubert 
746*5796c8dcSSimon Schubert static int
747*5796c8dcSSimon Schubert prefixify_expression (struct expression *expr)
748*5796c8dcSSimon Schubert {
749*5796c8dcSSimon Schubert   int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
750*5796c8dcSSimon Schubert   struct expression *temp;
751*5796c8dcSSimon Schubert   int inpos = expr->nelts, outpos = 0;
752*5796c8dcSSimon Schubert 
753*5796c8dcSSimon Schubert   temp = (struct expression *) alloca (len);
754*5796c8dcSSimon Schubert 
755*5796c8dcSSimon Schubert   /* Copy the original expression into temp.  */
756*5796c8dcSSimon Schubert   memcpy (temp, expr, len);
757*5796c8dcSSimon Schubert 
758*5796c8dcSSimon Schubert   return prefixify_subexp (temp, expr, inpos, outpos);
759*5796c8dcSSimon Schubert }
760*5796c8dcSSimon Schubert 
761*5796c8dcSSimon Schubert /* Return the number of exp_elements in the postfix subexpression
762*5796c8dcSSimon Schubert    of EXPR whose operator is at index ENDPOS - 1 in EXPR.  */
763*5796c8dcSSimon Schubert 
764*5796c8dcSSimon Schubert int
765*5796c8dcSSimon Schubert length_of_subexp (struct expression *expr, int endpos)
766*5796c8dcSSimon Schubert {
767*5796c8dcSSimon Schubert   int oplen, args, i;
768*5796c8dcSSimon Schubert 
769*5796c8dcSSimon Schubert   operator_length (expr, endpos, &oplen, &args);
770*5796c8dcSSimon Schubert 
771*5796c8dcSSimon Schubert   while (args > 0)
772*5796c8dcSSimon Schubert     {
773*5796c8dcSSimon Schubert       oplen += length_of_subexp (expr, endpos - oplen);
774*5796c8dcSSimon Schubert       args--;
775*5796c8dcSSimon Schubert     }
776*5796c8dcSSimon Schubert 
777*5796c8dcSSimon Schubert   return oplen;
778*5796c8dcSSimon Schubert }
779*5796c8dcSSimon Schubert 
780*5796c8dcSSimon Schubert /* Sets *OPLENP to the length of the operator whose (last) index is
781*5796c8dcSSimon Schubert    ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
782*5796c8dcSSimon Schubert    operator takes.  */
783*5796c8dcSSimon Schubert 
784*5796c8dcSSimon Schubert void
785*5796c8dcSSimon Schubert operator_length (struct expression *expr, int endpos, int *oplenp, int *argsp)
786*5796c8dcSSimon Schubert {
787*5796c8dcSSimon Schubert   expr->language_defn->la_exp_desc->operator_length (expr, endpos,
788*5796c8dcSSimon Schubert 						     oplenp, argsp);
789*5796c8dcSSimon Schubert }
790*5796c8dcSSimon Schubert 
791*5796c8dcSSimon Schubert /* Default value for operator_length in exp_descriptor vectors.  */
792*5796c8dcSSimon Schubert 
793*5796c8dcSSimon Schubert void
794*5796c8dcSSimon Schubert operator_length_standard (struct expression *expr, int endpos,
795*5796c8dcSSimon Schubert 			  int *oplenp, int *argsp)
796*5796c8dcSSimon Schubert {
797*5796c8dcSSimon Schubert   int oplen = 1;
798*5796c8dcSSimon Schubert   int args = 0;
799*5796c8dcSSimon Schubert   enum f90_range_type range_type;
800*5796c8dcSSimon Schubert   int i;
801*5796c8dcSSimon Schubert 
802*5796c8dcSSimon Schubert   if (endpos < 1)
803*5796c8dcSSimon Schubert     error (_("?error in operator_length_standard"));
804*5796c8dcSSimon Schubert 
805*5796c8dcSSimon Schubert   i = (int) expr->elts[endpos - 1].opcode;
806*5796c8dcSSimon Schubert 
807*5796c8dcSSimon Schubert   switch (i)
808*5796c8dcSSimon Schubert     {
809*5796c8dcSSimon Schubert       /* C++  */
810*5796c8dcSSimon Schubert     case OP_SCOPE:
811*5796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
812*5796c8dcSSimon Schubert       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
813*5796c8dcSSimon Schubert       break;
814*5796c8dcSSimon Schubert 
815*5796c8dcSSimon Schubert     case OP_LONG:
816*5796c8dcSSimon Schubert     case OP_DOUBLE:
817*5796c8dcSSimon Schubert     case OP_DECFLOAT:
818*5796c8dcSSimon Schubert     case OP_VAR_VALUE:
819*5796c8dcSSimon Schubert       oplen = 4;
820*5796c8dcSSimon Schubert       break;
821*5796c8dcSSimon Schubert 
822*5796c8dcSSimon Schubert     case OP_TYPE:
823*5796c8dcSSimon Schubert     case OP_BOOL:
824*5796c8dcSSimon Schubert     case OP_LAST:
825*5796c8dcSSimon Schubert     case OP_INTERNALVAR:
826*5796c8dcSSimon Schubert       oplen = 3;
827*5796c8dcSSimon Schubert       break;
828*5796c8dcSSimon Schubert 
829*5796c8dcSSimon Schubert     case OP_COMPLEX:
830*5796c8dcSSimon Schubert       oplen = 3;
831*5796c8dcSSimon Schubert       args = 2;
832*5796c8dcSSimon Schubert       break;
833*5796c8dcSSimon Schubert 
834*5796c8dcSSimon Schubert     case OP_FUNCALL:
835*5796c8dcSSimon Schubert     case OP_F77_UNDETERMINED_ARGLIST:
836*5796c8dcSSimon Schubert       oplen = 3;
837*5796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
838*5796c8dcSSimon Schubert       break;
839*5796c8dcSSimon Schubert 
840*5796c8dcSSimon Schubert     case OP_OBJC_MSGCALL:	/* Objective C message (method) call */
841*5796c8dcSSimon Schubert       oplen = 4;
842*5796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
843*5796c8dcSSimon Schubert       break;
844*5796c8dcSSimon Schubert 
845*5796c8dcSSimon Schubert     case UNOP_MAX:
846*5796c8dcSSimon Schubert     case UNOP_MIN:
847*5796c8dcSSimon Schubert       oplen = 3;
848*5796c8dcSSimon Schubert       break;
849*5796c8dcSSimon Schubert 
850*5796c8dcSSimon Schubert     case BINOP_VAL:
851*5796c8dcSSimon Schubert     case UNOP_CAST:
852*5796c8dcSSimon Schubert     case UNOP_MEMVAL:
853*5796c8dcSSimon Schubert       oplen = 3;
854*5796c8dcSSimon Schubert       args = 1;
855*5796c8dcSSimon Schubert       break;
856*5796c8dcSSimon Schubert 
857*5796c8dcSSimon Schubert     case UNOP_MEMVAL_TLS:
858*5796c8dcSSimon Schubert       oplen = 4;
859*5796c8dcSSimon Schubert       args = 1;
860*5796c8dcSSimon Schubert       break;
861*5796c8dcSSimon Schubert 
862*5796c8dcSSimon Schubert     case UNOP_ABS:
863*5796c8dcSSimon Schubert     case UNOP_CAP:
864*5796c8dcSSimon Schubert     case UNOP_CHR:
865*5796c8dcSSimon Schubert     case UNOP_FLOAT:
866*5796c8dcSSimon Schubert     case UNOP_HIGH:
867*5796c8dcSSimon Schubert     case UNOP_ODD:
868*5796c8dcSSimon Schubert     case UNOP_ORD:
869*5796c8dcSSimon Schubert     case UNOP_TRUNC:
870*5796c8dcSSimon Schubert       oplen = 1;
871*5796c8dcSSimon Schubert       args = 1;
872*5796c8dcSSimon Schubert       break;
873*5796c8dcSSimon Schubert 
874*5796c8dcSSimon Schubert     case OP_LABELED:
875*5796c8dcSSimon Schubert     case STRUCTOP_STRUCT:
876*5796c8dcSSimon Schubert     case STRUCTOP_PTR:
877*5796c8dcSSimon Schubert       args = 1;
878*5796c8dcSSimon Schubert       /* fall through */
879*5796c8dcSSimon Schubert     case OP_REGISTER:
880*5796c8dcSSimon Schubert     case OP_M2_STRING:
881*5796c8dcSSimon Schubert     case OP_STRING:
882*5796c8dcSSimon Schubert     case OP_OBJC_NSSTRING:	/* Objective C Foundation Class NSString constant */
883*5796c8dcSSimon Schubert     case OP_OBJC_SELECTOR:	/* Objective C "@selector" pseudo-op */
884*5796c8dcSSimon Schubert     case OP_NAME:
885*5796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
886*5796c8dcSSimon Schubert       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
887*5796c8dcSSimon Schubert       break;
888*5796c8dcSSimon Schubert 
889*5796c8dcSSimon Schubert     case OP_BITSTRING:
890*5796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
891*5796c8dcSSimon Schubert       oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
892*5796c8dcSSimon Schubert       oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
893*5796c8dcSSimon Schubert       break;
894*5796c8dcSSimon Schubert 
895*5796c8dcSSimon Schubert     case OP_ARRAY:
896*5796c8dcSSimon Schubert       oplen = 4;
897*5796c8dcSSimon Schubert       args = longest_to_int (expr->elts[endpos - 2].longconst);
898*5796c8dcSSimon Schubert       args -= longest_to_int (expr->elts[endpos - 3].longconst);
899*5796c8dcSSimon Schubert       args += 1;
900*5796c8dcSSimon Schubert       break;
901*5796c8dcSSimon Schubert 
902*5796c8dcSSimon Schubert     case TERNOP_COND:
903*5796c8dcSSimon Schubert     case TERNOP_SLICE:
904*5796c8dcSSimon Schubert     case TERNOP_SLICE_COUNT:
905*5796c8dcSSimon Schubert       args = 3;
906*5796c8dcSSimon Schubert       break;
907*5796c8dcSSimon Schubert 
908*5796c8dcSSimon Schubert       /* Modula-2 */
909*5796c8dcSSimon Schubert     case MULTI_SUBSCRIPT:
910*5796c8dcSSimon Schubert       oplen = 3;
911*5796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
912*5796c8dcSSimon Schubert       break;
913*5796c8dcSSimon Schubert 
914*5796c8dcSSimon Schubert     case BINOP_ASSIGN_MODIFY:
915*5796c8dcSSimon Schubert       oplen = 3;
916*5796c8dcSSimon Schubert       args = 2;
917*5796c8dcSSimon Schubert       break;
918*5796c8dcSSimon Schubert 
919*5796c8dcSSimon Schubert       /* C++ */
920*5796c8dcSSimon Schubert     case OP_THIS:
921*5796c8dcSSimon Schubert     case OP_OBJC_SELF:
922*5796c8dcSSimon Schubert       oplen = 2;
923*5796c8dcSSimon Schubert       break;
924*5796c8dcSSimon Schubert 
925*5796c8dcSSimon Schubert     case OP_F90_RANGE:
926*5796c8dcSSimon Schubert       oplen = 3;
927*5796c8dcSSimon Schubert 
928*5796c8dcSSimon Schubert       range_type = longest_to_int (expr->elts[endpos - 2].longconst);
929*5796c8dcSSimon Schubert       switch (range_type)
930*5796c8dcSSimon Schubert 	{
931*5796c8dcSSimon Schubert 	case LOW_BOUND_DEFAULT:
932*5796c8dcSSimon Schubert 	case HIGH_BOUND_DEFAULT:
933*5796c8dcSSimon Schubert 	  args = 1;
934*5796c8dcSSimon Schubert 	  break;
935*5796c8dcSSimon Schubert 	case BOTH_BOUND_DEFAULT:
936*5796c8dcSSimon Schubert 	  args = 0;
937*5796c8dcSSimon Schubert 	  break;
938*5796c8dcSSimon Schubert 	case NONE_BOUND_DEFAULT:
939*5796c8dcSSimon Schubert 	  args = 2;
940*5796c8dcSSimon Schubert 	  break;
941*5796c8dcSSimon Schubert 	}
942*5796c8dcSSimon Schubert 
943*5796c8dcSSimon Schubert       break;
944*5796c8dcSSimon Schubert 
945*5796c8dcSSimon Schubert     default:
946*5796c8dcSSimon Schubert       args = 1 + (i < (int) BINOP_END);
947*5796c8dcSSimon Schubert     }
948*5796c8dcSSimon Schubert 
949*5796c8dcSSimon Schubert   *oplenp = oplen;
950*5796c8dcSSimon Schubert   *argsp = args;
951*5796c8dcSSimon Schubert }
952*5796c8dcSSimon Schubert 
953*5796c8dcSSimon Schubert /* Copy the subexpression ending just before index INEND in INEXPR
954*5796c8dcSSimon Schubert    into OUTEXPR, starting at index OUTBEG.
955*5796c8dcSSimon Schubert    In the process, convert it from suffix to prefix form.
956*5796c8dcSSimon Schubert    If EXPOUT_LAST_STRUCT is -1, then this function always returns -1.
957*5796c8dcSSimon Schubert    Otherwise, it returns the index of the subexpression which is the
958*5796c8dcSSimon Schubert    left-hand-side of the expression at EXPOUT_LAST_STRUCT.  */
959*5796c8dcSSimon Schubert 
960*5796c8dcSSimon Schubert static int
961*5796c8dcSSimon Schubert prefixify_subexp (struct expression *inexpr,
962*5796c8dcSSimon Schubert 		  struct expression *outexpr, int inend, int outbeg)
963*5796c8dcSSimon Schubert {
964*5796c8dcSSimon Schubert   int oplen;
965*5796c8dcSSimon Schubert   int args;
966*5796c8dcSSimon Schubert   int i;
967*5796c8dcSSimon Schubert   int *arglens;
968*5796c8dcSSimon Schubert   enum exp_opcode opcode;
969*5796c8dcSSimon Schubert   int result = -1;
970*5796c8dcSSimon Schubert 
971*5796c8dcSSimon Schubert   operator_length (inexpr, inend, &oplen, &args);
972*5796c8dcSSimon Schubert 
973*5796c8dcSSimon Schubert   /* Copy the final operator itself, from the end of the input
974*5796c8dcSSimon Schubert      to the beginning of the output.  */
975*5796c8dcSSimon Schubert   inend -= oplen;
976*5796c8dcSSimon Schubert   memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
977*5796c8dcSSimon Schubert 	  EXP_ELEM_TO_BYTES (oplen));
978*5796c8dcSSimon Schubert   outbeg += oplen;
979*5796c8dcSSimon Schubert 
980*5796c8dcSSimon Schubert   if (expout_last_struct == inend)
981*5796c8dcSSimon Schubert     result = outbeg - oplen;
982*5796c8dcSSimon Schubert 
983*5796c8dcSSimon Schubert   /* Find the lengths of the arg subexpressions.  */
984*5796c8dcSSimon Schubert   arglens = (int *) alloca (args * sizeof (int));
985*5796c8dcSSimon Schubert   for (i = args - 1; i >= 0; i--)
986*5796c8dcSSimon Schubert     {
987*5796c8dcSSimon Schubert       oplen = length_of_subexp (inexpr, inend);
988*5796c8dcSSimon Schubert       arglens[i] = oplen;
989*5796c8dcSSimon Schubert       inend -= oplen;
990*5796c8dcSSimon Schubert     }
991*5796c8dcSSimon Schubert 
992*5796c8dcSSimon Schubert   /* Now copy each subexpression, preserving the order of
993*5796c8dcSSimon Schubert      the subexpressions, but prefixifying each one.
994*5796c8dcSSimon Schubert      In this loop, inend starts at the beginning of
995*5796c8dcSSimon Schubert      the expression this level is working on
996*5796c8dcSSimon Schubert      and marches forward over the arguments.
997*5796c8dcSSimon Schubert      outbeg does similarly in the output.  */
998*5796c8dcSSimon Schubert   for (i = 0; i < args; i++)
999*5796c8dcSSimon Schubert     {
1000*5796c8dcSSimon Schubert       int r;
1001*5796c8dcSSimon Schubert       oplen = arglens[i];
1002*5796c8dcSSimon Schubert       inend += oplen;
1003*5796c8dcSSimon Schubert       r = prefixify_subexp (inexpr, outexpr, inend, outbeg);
1004*5796c8dcSSimon Schubert       if (r != -1)
1005*5796c8dcSSimon Schubert 	{
1006*5796c8dcSSimon Schubert 	  /* Return immediately.  We probably have only parsed a
1007*5796c8dcSSimon Schubert 	     partial expression, so we don't want to try to reverse
1008*5796c8dcSSimon Schubert 	     the other operands.  */
1009*5796c8dcSSimon Schubert 	  return r;
1010*5796c8dcSSimon Schubert 	}
1011*5796c8dcSSimon Schubert       outbeg += oplen;
1012*5796c8dcSSimon Schubert     }
1013*5796c8dcSSimon Schubert 
1014*5796c8dcSSimon Schubert   return result;
1015*5796c8dcSSimon Schubert }
1016*5796c8dcSSimon Schubert 
1017*5796c8dcSSimon Schubert /* This page contains the two entry points to this file.  */
1018*5796c8dcSSimon Schubert 
1019*5796c8dcSSimon Schubert /* Read an expression from the string *STRINGPTR points to,
1020*5796c8dcSSimon Schubert    parse it, and return a pointer to a  struct expression  that we malloc.
1021*5796c8dcSSimon Schubert    Use block BLOCK as the lexical context for variable names;
1022*5796c8dcSSimon Schubert    if BLOCK is zero, use the block of the selected stack frame.
1023*5796c8dcSSimon Schubert    Meanwhile, advance *STRINGPTR to point after the expression,
1024*5796c8dcSSimon Schubert    at the first nonwhite character that is not part of the expression
1025*5796c8dcSSimon Schubert    (possibly a null character).
1026*5796c8dcSSimon Schubert 
1027*5796c8dcSSimon Schubert    If COMMA is nonzero, stop if a comma is reached.  */
1028*5796c8dcSSimon Schubert 
1029*5796c8dcSSimon Schubert struct expression *
1030*5796c8dcSSimon Schubert parse_exp_1 (char **stringptr, struct block *block, int comma)
1031*5796c8dcSSimon Schubert {
1032*5796c8dcSSimon Schubert   return parse_exp_in_context (stringptr, block, comma, 0, NULL);
1033*5796c8dcSSimon Schubert }
1034*5796c8dcSSimon Schubert 
1035*5796c8dcSSimon Schubert /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
1036*5796c8dcSSimon Schubert    no value is expected from the expression.
1037*5796c8dcSSimon Schubert    OUT_SUBEXP is set when attempting to complete a field name; in this
1038*5796c8dcSSimon Schubert    case it is set to the index of the subexpression on the
1039*5796c8dcSSimon Schubert    left-hand-side of the struct op.  If not doing such completion, it
1040*5796c8dcSSimon Schubert    is left untouched.  */
1041*5796c8dcSSimon Schubert 
1042*5796c8dcSSimon Schubert static struct expression *
1043*5796c8dcSSimon Schubert parse_exp_in_context (char **stringptr, struct block *block, int comma,
1044*5796c8dcSSimon Schubert 		      int void_context_p, int *out_subexp)
1045*5796c8dcSSimon Schubert {
1046*5796c8dcSSimon Schubert   volatile struct gdb_exception except;
1047*5796c8dcSSimon Schubert   struct cleanup *old_chain;
1048*5796c8dcSSimon Schubert   int subexp;
1049*5796c8dcSSimon Schubert 
1050*5796c8dcSSimon Schubert   lexptr = *stringptr;
1051*5796c8dcSSimon Schubert   prev_lexptr = NULL;
1052*5796c8dcSSimon Schubert 
1053*5796c8dcSSimon Schubert   paren_depth = 0;
1054*5796c8dcSSimon Schubert   type_stack_depth = 0;
1055*5796c8dcSSimon Schubert   expout_last_struct = -1;
1056*5796c8dcSSimon Schubert 
1057*5796c8dcSSimon Schubert   comma_terminates = comma;
1058*5796c8dcSSimon Schubert 
1059*5796c8dcSSimon Schubert   if (lexptr == 0 || *lexptr == 0)
1060*5796c8dcSSimon Schubert     error_no_arg (_("expression to compute"));
1061*5796c8dcSSimon Schubert 
1062*5796c8dcSSimon Schubert   old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
1063*5796c8dcSSimon Schubert   funcall_chain = 0;
1064*5796c8dcSSimon Schubert 
1065*5796c8dcSSimon Schubert   expression_context_block = block;
1066*5796c8dcSSimon Schubert 
1067*5796c8dcSSimon Schubert   /* If no context specified, try using the current frame, if any.  */
1068*5796c8dcSSimon Schubert   if (!expression_context_block)
1069*5796c8dcSSimon Schubert     expression_context_block = get_selected_block (&expression_context_pc);
1070*5796c8dcSSimon Schubert   else
1071*5796c8dcSSimon Schubert     expression_context_pc = BLOCK_START (expression_context_block);
1072*5796c8dcSSimon Schubert 
1073*5796c8dcSSimon Schubert   /* Fall back to using the current source static context, if any.  */
1074*5796c8dcSSimon Schubert 
1075*5796c8dcSSimon Schubert   if (!expression_context_block)
1076*5796c8dcSSimon Schubert     {
1077*5796c8dcSSimon Schubert       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
1078*5796c8dcSSimon Schubert       if (cursal.symtab)
1079*5796c8dcSSimon Schubert 	expression_context_block
1080*5796c8dcSSimon Schubert 	  = BLOCKVECTOR_BLOCK (BLOCKVECTOR (cursal.symtab), STATIC_BLOCK);
1081*5796c8dcSSimon Schubert       if (expression_context_block)
1082*5796c8dcSSimon Schubert 	expression_context_pc = BLOCK_START (expression_context_block);
1083*5796c8dcSSimon Schubert     }
1084*5796c8dcSSimon Schubert 
1085*5796c8dcSSimon Schubert   expout_size = 10;
1086*5796c8dcSSimon Schubert   expout_ptr = 0;
1087*5796c8dcSSimon Schubert   expout = (struct expression *)
1088*5796c8dcSSimon Schubert     xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
1089*5796c8dcSSimon Schubert   expout->language_defn = current_language;
1090*5796c8dcSSimon Schubert   expout->gdbarch = get_current_arch ();
1091*5796c8dcSSimon Schubert 
1092*5796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
1093*5796c8dcSSimon Schubert     {
1094*5796c8dcSSimon Schubert       if (current_language->la_parser ())
1095*5796c8dcSSimon Schubert 	current_language->la_error (NULL);
1096*5796c8dcSSimon Schubert     }
1097*5796c8dcSSimon Schubert   if (except.reason < 0)
1098*5796c8dcSSimon Schubert     {
1099*5796c8dcSSimon Schubert       if (! in_parse_field)
1100*5796c8dcSSimon Schubert 	{
1101*5796c8dcSSimon Schubert 	  xfree (expout);
1102*5796c8dcSSimon Schubert 	  throw_exception (except);
1103*5796c8dcSSimon Schubert 	}
1104*5796c8dcSSimon Schubert     }
1105*5796c8dcSSimon Schubert 
1106*5796c8dcSSimon Schubert   discard_cleanups (old_chain);
1107*5796c8dcSSimon Schubert 
1108*5796c8dcSSimon Schubert   /* Record the actual number of expression elements, and then
1109*5796c8dcSSimon Schubert      reallocate the expression memory so that we free up any
1110*5796c8dcSSimon Schubert      excess elements. */
1111*5796c8dcSSimon Schubert 
1112*5796c8dcSSimon Schubert   expout->nelts = expout_ptr;
1113*5796c8dcSSimon Schubert   expout = (struct expression *)
1114*5796c8dcSSimon Schubert     xrealloc ((char *) expout,
1115*5796c8dcSSimon Schubert 	      sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
1116*5796c8dcSSimon Schubert 
1117*5796c8dcSSimon Schubert   /* Convert expression from postfix form as generated by yacc
1118*5796c8dcSSimon Schubert      parser, to a prefix form. */
1119*5796c8dcSSimon Schubert 
1120*5796c8dcSSimon Schubert   if (expressiondebug)
1121*5796c8dcSSimon Schubert     dump_raw_expression (expout, gdb_stdlog,
1122*5796c8dcSSimon Schubert 			 "before conversion to prefix form");
1123*5796c8dcSSimon Schubert 
1124*5796c8dcSSimon Schubert   subexp = prefixify_expression (expout);
1125*5796c8dcSSimon Schubert   if (out_subexp)
1126*5796c8dcSSimon Schubert     *out_subexp = subexp;
1127*5796c8dcSSimon Schubert 
1128*5796c8dcSSimon Schubert   current_language->la_post_parser (&expout, void_context_p);
1129*5796c8dcSSimon Schubert 
1130*5796c8dcSSimon Schubert   if (expressiondebug)
1131*5796c8dcSSimon Schubert     dump_prefix_expression (expout, gdb_stdlog);
1132*5796c8dcSSimon Schubert 
1133*5796c8dcSSimon Schubert   *stringptr = lexptr;
1134*5796c8dcSSimon Schubert   return expout;
1135*5796c8dcSSimon Schubert }
1136*5796c8dcSSimon Schubert 
1137*5796c8dcSSimon Schubert /* Parse STRING as an expression, and complain if this fails
1138*5796c8dcSSimon Schubert    to use up all of the contents of STRING.  */
1139*5796c8dcSSimon Schubert 
1140*5796c8dcSSimon Schubert struct expression *
1141*5796c8dcSSimon Schubert parse_expression (char *string)
1142*5796c8dcSSimon Schubert {
1143*5796c8dcSSimon Schubert   struct expression *exp;
1144*5796c8dcSSimon Schubert   exp = parse_exp_1 (&string, 0, 0);
1145*5796c8dcSSimon Schubert   if (*string)
1146*5796c8dcSSimon Schubert     error (_("Junk after end of expression."));
1147*5796c8dcSSimon Schubert   return exp;
1148*5796c8dcSSimon Schubert }
1149*5796c8dcSSimon Schubert 
1150*5796c8dcSSimon Schubert /* Parse STRING as an expression.  If parsing ends in the middle of a
1151*5796c8dcSSimon Schubert    field reference, return the type of the left-hand-side of the
1152*5796c8dcSSimon Schubert    reference; furthermore, if the parsing ends in the field name,
1153*5796c8dcSSimon Schubert    return the field name in *NAME.  In all other cases, return NULL.
1154*5796c8dcSSimon Schubert    Returned non-NULL *NAME must be freed by the caller.  */
1155*5796c8dcSSimon Schubert 
1156*5796c8dcSSimon Schubert struct type *
1157*5796c8dcSSimon Schubert parse_field_expression (char *string, char **name)
1158*5796c8dcSSimon Schubert {
1159*5796c8dcSSimon Schubert   struct expression *exp = NULL;
1160*5796c8dcSSimon Schubert   struct value *val;
1161*5796c8dcSSimon Schubert   int subexp;
1162*5796c8dcSSimon Schubert   volatile struct gdb_exception except;
1163*5796c8dcSSimon Schubert 
1164*5796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
1165*5796c8dcSSimon Schubert     {
1166*5796c8dcSSimon Schubert       in_parse_field = 1;
1167*5796c8dcSSimon Schubert       exp = parse_exp_in_context (&string, 0, 0, 0, &subexp);
1168*5796c8dcSSimon Schubert     }
1169*5796c8dcSSimon Schubert   in_parse_field = 0;
1170*5796c8dcSSimon Schubert   if (except.reason < 0 || ! exp)
1171*5796c8dcSSimon Schubert     return NULL;
1172*5796c8dcSSimon Schubert   if (expout_last_struct == -1)
1173*5796c8dcSSimon Schubert     {
1174*5796c8dcSSimon Schubert       xfree (exp);
1175*5796c8dcSSimon Schubert       return NULL;
1176*5796c8dcSSimon Schubert     }
1177*5796c8dcSSimon Schubert 
1178*5796c8dcSSimon Schubert   *name = extract_field_op (exp, &subexp);
1179*5796c8dcSSimon Schubert   if (!*name)
1180*5796c8dcSSimon Schubert     {
1181*5796c8dcSSimon Schubert       xfree (exp);
1182*5796c8dcSSimon Schubert       return NULL;
1183*5796c8dcSSimon Schubert     }
1184*5796c8dcSSimon Schubert   /* (*NAME) is a part of the EXP memory block freed below.  */
1185*5796c8dcSSimon Schubert   *name = xstrdup (*name);
1186*5796c8dcSSimon Schubert 
1187*5796c8dcSSimon Schubert   val = evaluate_subexpression_type (exp, subexp);
1188*5796c8dcSSimon Schubert   xfree (exp);
1189*5796c8dcSSimon Schubert 
1190*5796c8dcSSimon Schubert   return value_type (val);
1191*5796c8dcSSimon Schubert }
1192*5796c8dcSSimon Schubert 
1193*5796c8dcSSimon Schubert /* A post-parser that does nothing */
1194*5796c8dcSSimon Schubert 
1195*5796c8dcSSimon Schubert void
1196*5796c8dcSSimon Schubert null_post_parser (struct expression **exp, int void_context_p)
1197*5796c8dcSSimon Schubert {
1198*5796c8dcSSimon Schubert }
1199*5796c8dcSSimon Schubert 
1200*5796c8dcSSimon Schubert /* Stuff for maintaining a stack of types.  Currently just used by C, but
1201*5796c8dcSSimon Schubert    probably useful for any language which declares its types "backwards".  */
1202*5796c8dcSSimon Schubert 
1203*5796c8dcSSimon Schubert static void
1204*5796c8dcSSimon Schubert check_type_stack_depth (void)
1205*5796c8dcSSimon Schubert {
1206*5796c8dcSSimon Schubert   if (type_stack_depth == type_stack_size)
1207*5796c8dcSSimon Schubert     {
1208*5796c8dcSSimon Schubert       type_stack_size *= 2;
1209*5796c8dcSSimon Schubert       type_stack = (union type_stack_elt *)
1210*5796c8dcSSimon Schubert 	xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
1211*5796c8dcSSimon Schubert     }
1212*5796c8dcSSimon Schubert }
1213*5796c8dcSSimon Schubert 
1214*5796c8dcSSimon Schubert void
1215*5796c8dcSSimon Schubert push_type (enum type_pieces tp)
1216*5796c8dcSSimon Schubert {
1217*5796c8dcSSimon Schubert   check_type_stack_depth ();
1218*5796c8dcSSimon Schubert   type_stack[type_stack_depth++].piece = tp;
1219*5796c8dcSSimon Schubert }
1220*5796c8dcSSimon Schubert 
1221*5796c8dcSSimon Schubert void
1222*5796c8dcSSimon Schubert push_type_int (int n)
1223*5796c8dcSSimon Schubert {
1224*5796c8dcSSimon Schubert   check_type_stack_depth ();
1225*5796c8dcSSimon Schubert   type_stack[type_stack_depth++].int_val = n;
1226*5796c8dcSSimon Schubert }
1227*5796c8dcSSimon Schubert 
1228*5796c8dcSSimon Schubert void
1229*5796c8dcSSimon Schubert push_type_address_space (char *string)
1230*5796c8dcSSimon Schubert {
1231*5796c8dcSSimon Schubert   push_type_int (address_space_name_to_int (parse_gdbarch, string));
1232*5796c8dcSSimon Schubert }
1233*5796c8dcSSimon Schubert 
1234*5796c8dcSSimon Schubert enum type_pieces
1235*5796c8dcSSimon Schubert pop_type (void)
1236*5796c8dcSSimon Schubert {
1237*5796c8dcSSimon Schubert   if (type_stack_depth)
1238*5796c8dcSSimon Schubert     return type_stack[--type_stack_depth].piece;
1239*5796c8dcSSimon Schubert   return tp_end;
1240*5796c8dcSSimon Schubert }
1241*5796c8dcSSimon Schubert 
1242*5796c8dcSSimon Schubert int
1243*5796c8dcSSimon Schubert pop_type_int (void)
1244*5796c8dcSSimon Schubert {
1245*5796c8dcSSimon Schubert   if (type_stack_depth)
1246*5796c8dcSSimon Schubert     return type_stack[--type_stack_depth].int_val;
1247*5796c8dcSSimon Schubert   /* "Can't happen".  */
1248*5796c8dcSSimon Schubert   return 0;
1249*5796c8dcSSimon Schubert }
1250*5796c8dcSSimon Schubert 
1251*5796c8dcSSimon Schubert /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
1252*5796c8dcSSimon Schubert    as modified by all the stuff on the stack.  */
1253*5796c8dcSSimon Schubert struct type *
1254*5796c8dcSSimon Schubert follow_types (struct type *follow_type)
1255*5796c8dcSSimon Schubert {
1256*5796c8dcSSimon Schubert   int done = 0;
1257*5796c8dcSSimon Schubert   int make_const = 0;
1258*5796c8dcSSimon Schubert   int make_volatile = 0;
1259*5796c8dcSSimon Schubert   int make_addr_space = 0;
1260*5796c8dcSSimon Schubert   int array_size;
1261*5796c8dcSSimon Schubert 
1262*5796c8dcSSimon Schubert   while (!done)
1263*5796c8dcSSimon Schubert     switch (pop_type ())
1264*5796c8dcSSimon Schubert       {
1265*5796c8dcSSimon Schubert       case tp_end:
1266*5796c8dcSSimon Schubert 	done = 1;
1267*5796c8dcSSimon Schubert 	if (make_const)
1268*5796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
1269*5796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
1270*5796c8dcSSimon Schubert 				      follow_type, 0);
1271*5796c8dcSSimon Schubert 	if (make_volatile)
1272*5796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
1273*5796c8dcSSimon Schubert 				      make_volatile,
1274*5796c8dcSSimon Schubert 				      follow_type, 0);
1275*5796c8dcSSimon Schubert 	if (make_addr_space)
1276*5796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
1277*5796c8dcSSimon Schubert 						      make_addr_space);
1278*5796c8dcSSimon Schubert 	make_const = make_volatile = 0;
1279*5796c8dcSSimon Schubert 	make_addr_space = 0;
1280*5796c8dcSSimon Schubert 	break;
1281*5796c8dcSSimon Schubert       case tp_const:
1282*5796c8dcSSimon Schubert 	make_const = 1;
1283*5796c8dcSSimon Schubert 	break;
1284*5796c8dcSSimon Schubert       case tp_volatile:
1285*5796c8dcSSimon Schubert 	make_volatile = 1;
1286*5796c8dcSSimon Schubert 	break;
1287*5796c8dcSSimon Schubert       case tp_space_identifier:
1288*5796c8dcSSimon Schubert 	make_addr_space = pop_type_int ();
1289*5796c8dcSSimon Schubert 	break;
1290*5796c8dcSSimon Schubert       case tp_pointer:
1291*5796c8dcSSimon Schubert 	follow_type = lookup_pointer_type (follow_type);
1292*5796c8dcSSimon Schubert 	if (make_const)
1293*5796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
1294*5796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
1295*5796c8dcSSimon Schubert 				      follow_type, 0);
1296*5796c8dcSSimon Schubert 	if (make_volatile)
1297*5796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
1298*5796c8dcSSimon Schubert 				      make_volatile,
1299*5796c8dcSSimon Schubert 				      follow_type, 0);
1300*5796c8dcSSimon Schubert 	if (make_addr_space)
1301*5796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
1302*5796c8dcSSimon Schubert 						      make_addr_space);
1303*5796c8dcSSimon Schubert 	make_const = make_volatile = 0;
1304*5796c8dcSSimon Schubert 	make_addr_space = 0;
1305*5796c8dcSSimon Schubert 	break;
1306*5796c8dcSSimon Schubert       case tp_reference:
1307*5796c8dcSSimon Schubert 	follow_type = lookup_reference_type (follow_type);
1308*5796c8dcSSimon Schubert 	if (make_const)
1309*5796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
1310*5796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
1311*5796c8dcSSimon Schubert 				      follow_type, 0);
1312*5796c8dcSSimon Schubert 	if (make_volatile)
1313*5796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
1314*5796c8dcSSimon Schubert 				      make_volatile,
1315*5796c8dcSSimon Schubert 				      follow_type, 0);
1316*5796c8dcSSimon Schubert 	if (make_addr_space)
1317*5796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
1318*5796c8dcSSimon Schubert 						      make_addr_space);
1319*5796c8dcSSimon Schubert 	make_const = make_volatile = 0;
1320*5796c8dcSSimon Schubert 	make_addr_space = 0;
1321*5796c8dcSSimon Schubert 	break;
1322*5796c8dcSSimon Schubert       case tp_array:
1323*5796c8dcSSimon Schubert 	array_size = pop_type_int ();
1324*5796c8dcSSimon Schubert 	/* FIXME-type-allocation: need a way to free this type when we are
1325*5796c8dcSSimon Schubert 	   done with it.  */
1326*5796c8dcSSimon Schubert 	follow_type =
1327*5796c8dcSSimon Schubert 	  lookup_array_range_type (follow_type,
1328*5796c8dcSSimon Schubert 				   0, array_size >= 0 ? array_size - 1 : 0);
1329*5796c8dcSSimon Schubert 	if (array_size < 0)
1330*5796c8dcSSimon Schubert 	  TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (follow_type) = 1;
1331*5796c8dcSSimon Schubert 	break;
1332*5796c8dcSSimon Schubert       case tp_function:
1333*5796c8dcSSimon Schubert 	/* FIXME-type-allocation: need a way to free this type when we are
1334*5796c8dcSSimon Schubert 	   done with it.  */
1335*5796c8dcSSimon Schubert 	follow_type = lookup_function_type (follow_type);
1336*5796c8dcSSimon Schubert 	break;
1337*5796c8dcSSimon Schubert       }
1338*5796c8dcSSimon Schubert   return follow_type;
1339*5796c8dcSSimon Schubert }
1340*5796c8dcSSimon Schubert 
1341*5796c8dcSSimon Schubert /* This function avoids direct calls to fprintf
1342*5796c8dcSSimon Schubert    in the parser generated debug code.  */
1343*5796c8dcSSimon Schubert void
1344*5796c8dcSSimon Schubert parser_fprintf (FILE *x, const char *y, ...)
1345*5796c8dcSSimon Schubert {
1346*5796c8dcSSimon Schubert   va_list args;
1347*5796c8dcSSimon Schubert   va_start (args, y);
1348*5796c8dcSSimon Schubert   if (x == stderr)
1349*5796c8dcSSimon Schubert     vfprintf_unfiltered (gdb_stderr, y, args);
1350*5796c8dcSSimon Schubert   else
1351*5796c8dcSSimon Schubert     {
1352*5796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
1353*5796c8dcSSimon Schubert       vfprintf_unfiltered (gdb_stderr, y, args);
1354*5796c8dcSSimon Schubert     }
1355*5796c8dcSSimon Schubert   va_end (args);
1356*5796c8dcSSimon Schubert }
1357*5796c8dcSSimon Schubert 
1358*5796c8dcSSimon Schubert void
1359*5796c8dcSSimon Schubert _initialize_parse (void)
1360*5796c8dcSSimon Schubert {
1361*5796c8dcSSimon Schubert   type_stack_size = 80;
1362*5796c8dcSSimon Schubert   type_stack_depth = 0;
1363*5796c8dcSSimon Schubert   type_stack = (union type_stack_elt *)
1364*5796c8dcSSimon Schubert     xmalloc (type_stack_size * sizeof (*type_stack));
1365*5796c8dcSSimon Schubert 
1366*5796c8dcSSimon Schubert   add_setshow_zinteger_cmd ("expression", class_maintenance,
1367*5796c8dcSSimon Schubert 			    &expressiondebug, _("\
1368*5796c8dcSSimon Schubert Set expression debugging."), _("\
1369*5796c8dcSSimon Schubert Show expression debugging."), _("\
1370*5796c8dcSSimon Schubert When non-zero, the internal representation of expressions will be printed."),
1371*5796c8dcSSimon Schubert 			    NULL,
1372*5796c8dcSSimon Schubert 			    show_expressiondebug,
1373*5796c8dcSSimon Schubert 			    &setdebuglist, &showdebuglist);
1374*5796c8dcSSimon Schubert }
1375