xref: /dflybsd-src/contrib/gdb-7/gdb/parse.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Parse expressions for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Modified from expread.y by the Department of Computer Science at the
65796c8dcSSimon Schubert    State University of New York at Buffalo, 1991.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This file is part of GDB.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
115796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
125796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
135796c8dcSSimon Schubert    (at your option) any later version.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
165796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
175796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185796c8dcSSimon Schubert    GNU General Public License for more details.
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
215796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert /* Parse an expression from text in a string,
245796c8dcSSimon Schubert    and return the result as a struct expression pointer.
255796c8dcSSimon Schubert    That structure contains arithmetic operations in reverse polish,
265796c8dcSSimon Schubert    with constants represented by operations that are followed by special data.
275796c8dcSSimon Schubert    See expression.h for the details of the format.
285796c8dcSSimon Schubert    What is important here is that it can be built up sequentially
295796c8dcSSimon Schubert    during the process of parsing; the lower levels of the tree always
305796c8dcSSimon Schubert    come first in the result.  */
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert #include "defs.h"
33cf7f2e2dSJohn Marino #include <ctype.h>
345796c8dcSSimon Schubert #include "arch-utils.h"
355796c8dcSSimon Schubert #include "gdb_string.h"
365796c8dcSSimon Schubert #include "symtab.h"
375796c8dcSSimon Schubert #include "gdbtypes.h"
385796c8dcSSimon Schubert #include "frame.h"
395796c8dcSSimon Schubert #include "expression.h"
405796c8dcSSimon Schubert #include "value.h"
415796c8dcSSimon Schubert #include "command.h"
425796c8dcSSimon Schubert #include "language.h"
435796c8dcSSimon Schubert #include "f-lang.h"
445796c8dcSSimon Schubert #include "parser-defs.h"
455796c8dcSSimon Schubert #include "gdbcmd.h"
465796c8dcSSimon Schubert #include "symfile.h"		/* for overlay functions */
475796c8dcSSimon Schubert #include "inferior.h"
485796c8dcSSimon Schubert #include "doublest.h"
495796c8dcSSimon Schubert #include "gdb_assert.h"
505796c8dcSSimon Schubert #include "block.h"
515796c8dcSSimon Schubert #include "source.h"
525796c8dcSSimon Schubert #include "objfiles.h"
535796c8dcSSimon Schubert #include "exceptions.h"
545796c8dcSSimon Schubert #include "user-regs.h"
555796c8dcSSimon Schubert 
565796c8dcSSimon Schubert /* Standard set of definitions for printing, dumping, prefixifying,
575796c8dcSSimon Schubert  * and evaluating expressions.  */
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert const struct exp_descriptor exp_descriptor_standard =
605796c8dcSSimon Schubert   {
615796c8dcSSimon Schubert     print_subexp_standard,
625796c8dcSSimon Schubert     operator_length_standard,
63cf7f2e2dSJohn Marino     operator_check_standard,
645796c8dcSSimon Schubert     op_name_standard,
655796c8dcSSimon Schubert     dump_subexp_body_standard,
665796c8dcSSimon Schubert     evaluate_subexp_standard
675796c8dcSSimon Schubert   };
685796c8dcSSimon Schubert 
695796c8dcSSimon Schubert /* Global variables declared in parser-defs.h (and commented there).  */
705796c8dcSSimon Schubert struct expression *expout;
715796c8dcSSimon Schubert int expout_size;
725796c8dcSSimon Schubert int expout_ptr;
73*ef5ccd6cSJohn Marino const struct block *expression_context_block;
745796c8dcSSimon Schubert CORE_ADDR expression_context_pc;
75*ef5ccd6cSJohn Marino const struct block *innermost_block;
765796c8dcSSimon Schubert int arglist_len;
77*ef5ccd6cSJohn Marino static struct type_stack type_stack;
785796c8dcSSimon Schubert char *lexptr;
795796c8dcSSimon Schubert char *prev_lexptr;
805796c8dcSSimon Schubert int paren_depth;
815796c8dcSSimon Schubert int comma_terminates;
825796c8dcSSimon Schubert 
83*ef5ccd6cSJohn Marino /* True if parsing an expression to attempt completion.  */
84*ef5ccd6cSJohn Marino int parse_completion;
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert /* The index of the last struct expression directly before a '.' or
875796c8dcSSimon Schubert    '->'.  This is set when parsing and is only used when completing a
885796c8dcSSimon Schubert    field name.  It is -1 if no dereference operation was found.  */
895796c8dcSSimon Schubert static int expout_last_struct = -1;
905796c8dcSSimon Schubert 
91*ef5ccd6cSJohn Marino /* If we are completing a tagged type name, this will be nonzero.  */
92*ef5ccd6cSJohn Marino static enum type_code expout_tag_completion_type = TYPE_CODE_UNDEF;
935796c8dcSSimon Schubert 
94*ef5ccd6cSJohn Marino /* The token for tagged type name completion.  */
95*ef5ccd6cSJohn Marino static char *expout_completion_name;
96*ef5ccd6cSJohn Marino 
975796c8dcSSimon Schubert 
98*ef5ccd6cSJohn Marino static unsigned int expressiondebug = 0;
995796c8dcSSimon Schubert static void
show_expressiondebug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)1005796c8dcSSimon Schubert show_expressiondebug (struct ui_file *file, int from_tty,
1015796c8dcSSimon Schubert 		      struct cmd_list_element *c, const char *value)
1025796c8dcSSimon Schubert {
1035796c8dcSSimon Schubert   fprintf_filtered (file, _("Expression debugging is %s.\n"), value);
1045796c8dcSSimon Schubert }
1055796c8dcSSimon Schubert 
106cf7f2e2dSJohn Marino 
107cf7f2e2dSJohn Marino /* Non-zero if an expression parser should set yydebug.  */
108cf7f2e2dSJohn Marino int parser_debug;
109cf7f2e2dSJohn Marino 
110cf7f2e2dSJohn Marino static void
show_parserdebug(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)111cf7f2e2dSJohn Marino show_parserdebug (struct ui_file *file, int from_tty,
112cf7f2e2dSJohn Marino 		  struct cmd_list_element *c, const char *value)
113cf7f2e2dSJohn Marino {
114cf7f2e2dSJohn Marino   fprintf_filtered (file, _("Parser debugging is %s.\n"), value);
115cf7f2e2dSJohn Marino }
116cf7f2e2dSJohn Marino 
117cf7f2e2dSJohn Marino 
1185796c8dcSSimon Schubert static void free_funcalls (void *ignore);
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert static int prefixify_subexp (struct expression *, struct expression *, int,
1215796c8dcSSimon Schubert 			     int);
1225796c8dcSSimon Schubert 
123*ef5ccd6cSJohn Marino static struct expression *parse_exp_in_context (char **, CORE_ADDR,
124*ef5ccd6cSJohn Marino 						const struct block *, int,
1255796c8dcSSimon Schubert 						int, int *);
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert void _initialize_parse (void);
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert /* Data structure for saving values of arglist_len for function calls whose
1305796c8dcSSimon Schubert    arguments contain other function calls.  */
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert struct funcall
1335796c8dcSSimon Schubert   {
1345796c8dcSSimon Schubert     struct funcall *next;
1355796c8dcSSimon Schubert     int arglist_len;
1365796c8dcSSimon Schubert   };
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert static struct funcall *funcall_chain;
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert /* Begin counting arguments for a function call,
1415796c8dcSSimon Schubert    saving the data about any containing call.  */
1425796c8dcSSimon Schubert 
1435796c8dcSSimon Schubert void
start_arglist(void)1445796c8dcSSimon Schubert start_arglist (void)
1455796c8dcSSimon Schubert {
1465796c8dcSSimon Schubert   struct funcall *new;
1475796c8dcSSimon Schubert 
1485796c8dcSSimon Schubert   new = (struct funcall *) xmalloc (sizeof (struct funcall));
1495796c8dcSSimon Schubert   new->next = funcall_chain;
1505796c8dcSSimon Schubert   new->arglist_len = arglist_len;
1515796c8dcSSimon Schubert   arglist_len = 0;
1525796c8dcSSimon Schubert   funcall_chain = new;
1535796c8dcSSimon Schubert }
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert /* Return the number of arguments in a function call just terminated,
1565796c8dcSSimon Schubert    and restore the data for the containing function call.  */
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert int
end_arglist(void)1595796c8dcSSimon Schubert end_arglist (void)
1605796c8dcSSimon Schubert {
1615796c8dcSSimon Schubert   int val = arglist_len;
1625796c8dcSSimon Schubert   struct funcall *call = funcall_chain;
163cf7f2e2dSJohn Marino 
1645796c8dcSSimon Schubert   funcall_chain = call->next;
1655796c8dcSSimon Schubert   arglist_len = call->arglist_len;
1665796c8dcSSimon Schubert   xfree (call);
1675796c8dcSSimon Schubert   return val;
1685796c8dcSSimon Schubert }
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert /* Free everything in the funcall chain.
1715796c8dcSSimon Schubert    Used when there is an error inside parsing.  */
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert static void
free_funcalls(void * ignore)1745796c8dcSSimon Schubert free_funcalls (void *ignore)
1755796c8dcSSimon Schubert {
1765796c8dcSSimon Schubert   struct funcall *call, *next;
1775796c8dcSSimon Schubert 
1785796c8dcSSimon Schubert   for (call = funcall_chain; call; call = next)
1795796c8dcSSimon Schubert     {
1805796c8dcSSimon Schubert       next = call->next;
1815796c8dcSSimon Schubert       xfree (call);
1825796c8dcSSimon Schubert     }
1835796c8dcSSimon Schubert }
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert /* This page contains the functions for adding data to the struct expression
1865796c8dcSSimon Schubert    being constructed.  */
1875796c8dcSSimon Schubert 
188*ef5ccd6cSJohn Marino /* See definition in parser-defs.h.  */
189*ef5ccd6cSJohn Marino 
190*ef5ccd6cSJohn Marino void
initialize_expout(int initial_size,const struct language_defn * lang,struct gdbarch * gdbarch)191*ef5ccd6cSJohn Marino initialize_expout (int initial_size, const struct language_defn *lang,
192*ef5ccd6cSJohn Marino 		   struct gdbarch *gdbarch)
193*ef5ccd6cSJohn Marino {
194*ef5ccd6cSJohn Marino   expout_size = initial_size;
195*ef5ccd6cSJohn Marino   expout_ptr = 0;
196*ef5ccd6cSJohn Marino   expout = xmalloc (sizeof (struct expression)
197*ef5ccd6cSJohn Marino 		    + EXP_ELEM_TO_BYTES (expout_size));
198*ef5ccd6cSJohn Marino   expout->language_defn = lang;
199*ef5ccd6cSJohn Marino   expout->gdbarch = gdbarch;
200*ef5ccd6cSJohn Marino }
201*ef5ccd6cSJohn Marino 
202*ef5ccd6cSJohn Marino /* See definition in parser-defs.h.  */
203*ef5ccd6cSJohn Marino 
204*ef5ccd6cSJohn Marino void
reallocate_expout(void)205*ef5ccd6cSJohn Marino reallocate_expout (void)
206*ef5ccd6cSJohn Marino {
207*ef5ccd6cSJohn Marino   /* Record the actual number of expression elements, and then
208*ef5ccd6cSJohn Marino      reallocate the expression memory so that we free up any
209*ef5ccd6cSJohn Marino      excess elements.  */
210*ef5ccd6cSJohn Marino 
211*ef5ccd6cSJohn Marino   expout->nelts = expout_ptr;
212*ef5ccd6cSJohn Marino   expout = xrealloc ((char *) expout,
213*ef5ccd6cSJohn Marino 		     sizeof (struct expression)
214*ef5ccd6cSJohn Marino 		     + EXP_ELEM_TO_BYTES (expout_ptr));
215*ef5ccd6cSJohn Marino }
216*ef5ccd6cSJohn Marino 
2175796c8dcSSimon Schubert /* Add one element to the end of the expression.  */
2185796c8dcSSimon Schubert 
2195796c8dcSSimon Schubert /* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
220c50c785cSJohn Marino    a register through here.  */
2215796c8dcSSimon Schubert 
222a45ae5f8SJohn Marino static void
write_exp_elt(const union exp_element * expelt)223a45ae5f8SJohn Marino write_exp_elt (const union exp_element *expelt)
2245796c8dcSSimon Schubert {
2255796c8dcSSimon Schubert   if (expout_ptr >= expout_size)
2265796c8dcSSimon Schubert     {
2275796c8dcSSimon Schubert       expout_size *= 2;
2285796c8dcSSimon Schubert       expout = (struct expression *)
2295796c8dcSSimon Schubert 	xrealloc ((char *) expout, sizeof (struct expression)
2305796c8dcSSimon Schubert 		  + EXP_ELEM_TO_BYTES (expout_size));
2315796c8dcSSimon Schubert     }
232a45ae5f8SJohn Marino   expout->elts[expout_ptr++] = *expelt;
2335796c8dcSSimon Schubert }
2345796c8dcSSimon Schubert 
2355796c8dcSSimon Schubert void
write_exp_elt_opcode(enum exp_opcode expelt)2365796c8dcSSimon Schubert write_exp_elt_opcode (enum exp_opcode expelt)
2375796c8dcSSimon Schubert {
2385796c8dcSSimon Schubert   union exp_element tmp;
239cf7f2e2dSJohn Marino 
2405796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2415796c8dcSSimon Schubert   tmp.opcode = expelt;
242a45ae5f8SJohn Marino   write_exp_elt (&tmp);
2435796c8dcSSimon Schubert }
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert void
write_exp_elt_sym(struct symbol * expelt)2465796c8dcSSimon Schubert write_exp_elt_sym (struct symbol *expelt)
2475796c8dcSSimon Schubert {
2485796c8dcSSimon Schubert   union exp_element tmp;
249cf7f2e2dSJohn Marino 
2505796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2515796c8dcSSimon Schubert   tmp.symbol = expelt;
252a45ae5f8SJohn Marino   write_exp_elt (&tmp);
2535796c8dcSSimon Schubert }
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert void
write_exp_elt_block(const struct block * b)256*ef5ccd6cSJohn Marino write_exp_elt_block (const struct block *b)
2575796c8dcSSimon Schubert {
2585796c8dcSSimon Schubert   union exp_element tmp;
259cf7f2e2dSJohn Marino 
2605796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2615796c8dcSSimon Schubert   tmp.block = b;
262a45ae5f8SJohn Marino   write_exp_elt (&tmp);
2635796c8dcSSimon Schubert }
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert void
write_exp_elt_objfile(struct objfile * objfile)2665796c8dcSSimon Schubert write_exp_elt_objfile (struct objfile *objfile)
2675796c8dcSSimon Schubert {
2685796c8dcSSimon Schubert   union exp_element tmp;
269cf7f2e2dSJohn Marino 
2705796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2715796c8dcSSimon Schubert   tmp.objfile = objfile;
272a45ae5f8SJohn Marino   write_exp_elt (&tmp);
2735796c8dcSSimon Schubert }
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert void
write_exp_elt_longcst(LONGEST expelt)2765796c8dcSSimon Schubert write_exp_elt_longcst (LONGEST expelt)
2775796c8dcSSimon Schubert {
2785796c8dcSSimon Schubert   union exp_element tmp;
279cf7f2e2dSJohn Marino 
2805796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2815796c8dcSSimon Schubert   tmp.longconst = expelt;
282a45ae5f8SJohn Marino   write_exp_elt (&tmp);
2835796c8dcSSimon Schubert }
2845796c8dcSSimon Schubert 
2855796c8dcSSimon Schubert void
write_exp_elt_dblcst(DOUBLEST expelt)2865796c8dcSSimon Schubert write_exp_elt_dblcst (DOUBLEST expelt)
2875796c8dcSSimon Schubert {
2885796c8dcSSimon Schubert   union exp_element tmp;
289cf7f2e2dSJohn Marino 
2905796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
2915796c8dcSSimon Schubert   tmp.doubleconst = expelt;
292a45ae5f8SJohn Marino   write_exp_elt (&tmp);
2935796c8dcSSimon Schubert }
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert void
write_exp_elt_decfloatcst(gdb_byte expelt[16])2965796c8dcSSimon Schubert write_exp_elt_decfloatcst (gdb_byte expelt[16])
2975796c8dcSSimon Schubert {
2985796c8dcSSimon Schubert   union exp_element tmp;
2995796c8dcSSimon Schubert   int index;
3005796c8dcSSimon Schubert 
3015796c8dcSSimon Schubert   for (index = 0; index < 16; index++)
3025796c8dcSSimon Schubert     tmp.decfloatconst[index] = expelt[index];
3035796c8dcSSimon Schubert 
304a45ae5f8SJohn Marino   write_exp_elt (&tmp);
3055796c8dcSSimon Schubert }
3065796c8dcSSimon Schubert 
3075796c8dcSSimon Schubert void
write_exp_elt_type(struct type * expelt)3085796c8dcSSimon Schubert write_exp_elt_type (struct type *expelt)
3095796c8dcSSimon Schubert {
3105796c8dcSSimon Schubert   union exp_element tmp;
311cf7f2e2dSJohn Marino 
3125796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
3135796c8dcSSimon Schubert   tmp.type = expelt;
314a45ae5f8SJohn Marino   write_exp_elt (&tmp);
3155796c8dcSSimon Schubert }
3165796c8dcSSimon Schubert 
3175796c8dcSSimon Schubert void
write_exp_elt_intern(struct internalvar * expelt)3185796c8dcSSimon Schubert write_exp_elt_intern (struct internalvar *expelt)
3195796c8dcSSimon Schubert {
3205796c8dcSSimon Schubert   union exp_element tmp;
321cf7f2e2dSJohn Marino 
3225796c8dcSSimon Schubert   memset (&tmp, 0, sizeof (union exp_element));
3235796c8dcSSimon Schubert   tmp.internalvar = expelt;
324a45ae5f8SJohn Marino   write_exp_elt (&tmp);
3255796c8dcSSimon Schubert }
3265796c8dcSSimon Schubert 
3275796c8dcSSimon Schubert /* Add a string constant to the end of the expression.
3285796c8dcSSimon Schubert 
3295796c8dcSSimon Schubert    String constants are stored by first writing an expression element
3305796c8dcSSimon Schubert    that contains the length of the string, then stuffing the string
3315796c8dcSSimon Schubert    constant itself into however many expression elements are needed
3325796c8dcSSimon Schubert    to hold it, and then writing another expression element that contains
333c50c785cSJohn Marino    the length of the string.  I.e. an expression element at each end of
3345796c8dcSSimon Schubert    the string records the string length, so you can skip over the
3355796c8dcSSimon Schubert    expression elements containing the actual string bytes from either
3365796c8dcSSimon Schubert    end of the string.  Note that this also allows gdb to handle
3375796c8dcSSimon Schubert    strings with embedded null bytes, as is required for some languages.
3385796c8dcSSimon Schubert 
3395796c8dcSSimon Schubert    Don't be fooled by the fact that the string is null byte terminated,
3405796c8dcSSimon Schubert    this is strictly for the convenience of debugging gdb itself.
3415796c8dcSSimon Schubert    Gdb does not depend up the string being null terminated, since the
3425796c8dcSSimon Schubert    actual length is recorded in expression elements at each end of the
3435796c8dcSSimon Schubert    string.  The null byte is taken into consideration when computing how
3445796c8dcSSimon Schubert    many expression elements are required to hold the string constant, of
3455796c8dcSSimon Schubert    course.  */
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert 
3485796c8dcSSimon Schubert void
write_exp_string(struct stoken str)3495796c8dcSSimon Schubert write_exp_string (struct stoken str)
3505796c8dcSSimon Schubert {
3515796c8dcSSimon Schubert   int len = str.length;
3525796c8dcSSimon Schubert   int lenelt;
3535796c8dcSSimon Schubert   char *strdata;
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert   /* Compute the number of expression elements required to hold the string
3565796c8dcSSimon Schubert      (including a null byte terminator), along with one expression element
3575796c8dcSSimon Schubert      at each end to record the actual string length (not including the
3585796c8dcSSimon Schubert      null byte terminator).  */
3595796c8dcSSimon Schubert 
3605796c8dcSSimon Schubert   lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
3615796c8dcSSimon Schubert 
3625796c8dcSSimon Schubert   /* Ensure that we have enough available expression elements to store
3635796c8dcSSimon Schubert      everything.  */
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert   if ((expout_ptr + lenelt) >= expout_size)
3665796c8dcSSimon Schubert     {
3675796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
3685796c8dcSSimon Schubert       expout = (struct expression *)
3695796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
3705796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
3715796c8dcSSimon Schubert     }
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert   /* Write the leading length expression element (which advances the current
3745796c8dcSSimon Schubert      expression element index), then write the string constant followed by a
3755796c8dcSSimon Schubert      terminating null byte, and then write the trailing length expression
3765796c8dcSSimon Schubert      element.  */
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) len);
3795796c8dcSSimon Schubert   strdata = (char *) &expout->elts[expout_ptr];
3805796c8dcSSimon Schubert   memcpy (strdata, str.ptr, len);
3815796c8dcSSimon Schubert   *(strdata + len) = '\0';
3825796c8dcSSimon Schubert   expout_ptr += lenelt - 2;
3835796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) len);
3845796c8dcSSimon Schubert }
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert /* Add a vector of string constants to the end of the expression.
3875796c8dcSSimon Schubert 
3885796c8dcSSimon Schubert    This adds an OP_STRING operation, but encodes the contents
3895796c8dcSSimon Schubert    differently from write_exp_string.  The language is expected to
3905796c8dcSSimon Schubert    handle evaluation of this expression itself.
3915796c8dcSSimon Schubert 
3925796c8dcSSimon Schubert    After the usual OP_STRING header, TYPE is written into the
3935796c8dcSSimon Schubert    expression as a long constant.  The interpretation of this field is
3945796c8dcSSimon Schubert    up to the language evaluator.
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert    Next, each string in VEC is written.  The length is written as a
3975796c8dcSSimon Schubert    long constant, followed by the contents of the string.  */
3985796c8dcSSimon Schubert 
3995796c8dcSSimon Schubert void
write_exp_string_vector(int type,struct stoken_vector * vec)4005796c8dcSSimon Schubert write_exp_string_vector (int type, struct stoken_vector *vec)
4015796c8dcSSimon Schubert {
4025796c8dcSSimon Schubert   int i, n_slots, len;
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert   /* Compute the size.  We compute the size in number of slots to
4055796c8dcSSimon Schubert      avoid issues with string padding.  */
4065796c8dcSSimon Schubert   n_slots = 0;
4075796c8dcSSimon Schubert   for (i = 0; i < vec->len; ++i)
4085796c8dcSSimon Schubert     {
4095796c8dcSSimon Schubert       /* One slot for the length of this element, plus the number of
4105796c8dcSSimon Schubert 	 slots needed for this string.  */
4115796c8dcSSimon Schubert       n_slots += 1 + BYTES_TO_EXP_ELEM (vec->tokens[i].length);
4125796c8dcSSimon Schubert     }
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert   /* One more slot for the type of the string.  */
4155796c8dcSSimon Schubert   ++n_slots;
4165796c8dcSSimon Schubert 
4175796c8dcSSimon Schubert   /* Now compute a phony string length.  */
4185796c8dcSSimon Schubert   len = EXP_ELEM_TO_BYTES (n_slots) - 1;
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   n_slots += 4;
4215796c8dcSSimon Schubert   if ((expout_ptr + n_slots) >= expout_size)
4225796c8dcSSimon Schubert     {
4235796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + n_slots + 10);
4245796c8dcSSimon Schubert       expout = (struct expression *)
4255796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
4265796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
4275796c8dcSSimon Schubert     }
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   write_exp_elt_opcode (OP_STRING);
4305796c8dcSSimon Schubert   write_exp_elt_longcst (len);
4315796c8dcSSimon Schubert   write_exp_elt_longcst (type);
4325796c8dcSSimon Schubert 
4335796c8dcSSimon Schubert   for (i = 0; i < vec->len; ++i)
4345796c8dcSSimon Schubert     {
4355796c8dcSSimon Schubert       write_exp_elt_longcst (vec->tokens[i].length);
4365796c8dcSSimon Schubert       memcpy (&expout->elts[expout_ptr], vec->tokens[i].ptr,
4375796c8dcSSimon Schubert 	      vec->tokens[i].length);
4385796c8dcSSimon Schubert       expout_ptr += BYTES_TO_EXP_ELEM (vec->tokens[i].length);
4395796c8dcSSimon Schubert     }
4405796c8dcSSimon Schubert 
4415796c8dcSSimon Schubert   write_exp_elt_longcst (len);
4425796c8dcSSimon Schubert   write_exp_elt_opcode (OP_STRING);
4435796c8dcSSimon Schubert }
4445796c8dcSSimon Schubert 
4455796c8dcSSimon Schubert /* Add a bitstring constant to the end of the expression.
4465796c8dcSSimon Schubert 
4475796c8dcSSimon Schubert    Bitstring constants are stored by first writing an expression element
4485796c8dcSSimon Schubert    that contains the length of the bitstring (in bits), then stuffing the
4495796c8dcSSimon Schubert    bitstring constant itself into however many expression elements are
4505796c8dcSSimon Schubert    needed to hold it, and then writing another expression element that
451c50c785cSJohn Marino    contains the length of the bitstring.  I.e. an expression element at
4525796c8dcSSimon Schubert    each end of the bitstring records the bitstring length, so you can skip
4535796c8dcSSimon Schubert    over the expression elements containing the actual bitstring bytes from
4545796c8dcSSimon Schubert    either end of the bitstring.  */
4555796c8dcSSimon Schubert 
4565796c8dcSSimon Schubert void
write_exp_bitstring(struct stoken str)4575796c8dcSSimon Schubert write_exp_bitstring (struct stoken str)
4585796c8dcSSimon Schubert {
4595796c8dcSSimon Schubert   int bits = str.length;	/* length in bits */
4605796c8dcSSimon Schubert   int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
4615796c8dcSSimon Schubert   int lenelt;
4625796c8dcSSimon Schubert   char *strdata;
4635796c8dcSSimon Schubert 
4645796c8dcSSimon Schubert   /* Compute the number of expression elements required to hold the bitstring,
4655796c8dcSSimon Schubert      along with one expression element at each end to record the actual
4665796c8dcSSimon Schubert      bitstring length in bits.  */
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert   lenelt = 2 + BYTES_TO_EXP_ELEM (len);
4695796c8dcSSimon Schubert 
4705796c8dcSSimon Schubert   /* Ensure that we have enough available expression elements to store
4715796c8dcSSimon Schubert      everything.  */
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert   if ((expout_ptr + lenelt) >= expout_size)
4745796c8dcSSimon Schubert     {
4755796c8dcSSimon Schubert       expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
4765796c8dcSSimon Schubert       expout = (struct expression *)
4775796c8dcSSimon Schubert 	xrealloc ((char *) expout, (sizeof (struct expression)
4785796c8dcSSimon Schubert 				    + EXP_ELEM_TO_BYTES (expout_size)));
4795796c8dcSSimon Schubert     }
4805796c8dcSSimon Schubert 
4815796c8dcSSimon Schubert   /* Write the leading length expression element (which advances the current
4825796c8dcSSimon Schubert      expression element index), then write the bitstring constant, and then
4835796c8dcSSimon Schubert      write the trailing length expression element.  */
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) bits);
4865796c8dcSSimon Schubert   strdata = (char *) &expout->elts[expout_ptr];
4875796c8dcSSimon Schubert   memcpy (strdata, str.ptr, len);
4885796c8dcSSimon Schubert   expout_ptr += lenelt - 2;
4895796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) bits);
4905796c8dcSSimon Schubert }
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert /* Add the appropriate elements for a minimal symbol to the end of
4935796c8dcSSimon Schubert    the expression.  */
4945796c8dcSSimon Schubert 
4955796c8dcSSimon Schubert void
write_exp_msymbol(struct minimal_symbol * msymbol)4965796c8dcSSimon Schubert write_exp_msymbol (struct minimal_symbol *msymbol)
4975796c8dcSSimon Schubert {
4985796c8dcSSimon Schubert   struct objfile *objfile = msymbol_objfile (msymbol);
4995796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5005796c8dcSSimon Schubert 
5015796c8dcSSimon Schubert   CORE_ADDR addr = SYMBOL_VALUE_ADDRESS (msymbol);
5025796c8dcSSimon Schubert   struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
5035796c8dcSSimon Schubert   enum minimal_symbol_type type = MSYMBOL_TYPE (msymbol);
5045796c8dcSSimon Schubert   CORE_ADDR pc;
5055796c8dcSSimon Schubert 
5065796c8dcSSimon Schubert   /* The minimal symbol might point to a function descriptor;
5075796c8dcSSimon Schubert      resolve it to the actual code address instead.  */
5085796c8dcSSimon Schubert   pc = gdbarch_convert_from_func_ptr_addr (gdbarch, addr, &current_target);
5095796c8dcSSimon Schubert   if (pc != addr)
5105796c8dcSSimon Schubert     {
511c50c785cSJohn Marino       struct minimal_symbol *ifunc_msym = lookup_minimal_symbol_by_pc (pc);
512c50c785cSJohn Marino 
5135796c8dcSSimon Schubert       /* In this case, assume we have a code symbol instead of
5145796c8dcSSimon Schubert 	 a data symbol.  */
515c50c785cSJohn Marino 
516c50c785cSJohn Marino       if (ifunc_msym != NULL && MSYMBOL_TYPE (ifunc_msym) == mst_text_gnu_ifunc
517c50c785cSJohn Marino 	  && SYMBOL_VALUE_ADDRESS (ifunc_msym) == pc)
518c50c785cSJohn Marino 	{
519c50c785cSJohn Marino 	  /* A function descriptor has been resolved but PC is still in the
520c50c785cSJohn Marino 	     STT_GNU_IFUNC resolver body (such as because inferior does not
521c50c785cSJohn Marino 	     run to be able to call it).  */
522c50c785cSJohn Marino 
523c50c785cSJohn Marino 	  type = mst_text_gnu_ifunc;
524c50c785cSJohn Marino 	}
525c50c785cSJohn Marino       else
5265796c8dcSSimon Schubert 	type = mst_text;
5275796c8dcSSimon Schubert       section = NULL;
5285796c8dcSSimon Schubert       addr = pc;
5295796c8dcSSimon Schubert     }
5305796c8dcSSimon Schubert 
5315796c8dcSSimon Schubert   if (overlay_debugging)
5325796c8dcSSimon Schubert     addr = symbol_overlayed_address (addr, section);
5335796c8dcSSimon Schubert 
5345796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LONG);
5355796c8dcSSimon Schubert   /* Let's make the type big enough to hold a 64-bit address.  */
5365796c8dcSSimon Schubert   write_exp_elt_type (objfile_type (objfile)->builtin_core_addr);
5375796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) addr);
5385796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LONG);
5395796c8dcSSimon Schubert 
5405796c8dcSSimon Schubert   if (section && section->the_bfd_section->flags & SEC_THREAD_LOCAL)
5415796c8dcSSimon Schubert     {
5425796c8dcSSimon Schubert       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
5435796c8dcSSimon Schubert       write_exp_elt_objfile (objfile);
5445796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_tls_symbol);
5455796c8dcSSimon Schubert       write_exp_elt_opcode (UNOP_MEMVAL_TLS);
5465796c8dcSSimon Schubert       return;
5475796c8dcSSimon Schubert     }
5485796c8dcSSimon Schubert 
5495796c8dcSSimon Schubert   write_exp_elt_opcode (UNOP_MEMVAL);
5505796c8dcSSimon Schubert   switch (type)
5515796c8dcSSimon Schubert     {
5525796c8dcSSimon Schubert     case mst_text:
5535796c8dcSSimon Schubert     case mst_file_text:
5545796c8dcSSimon Schubert     case mst_solib_trampoline:
5555796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_text_symbol);
5565796c8dcSSimon Schubert       break;
5575796c8dcSSimon Schubert 
558c50c785cSJohn Marino     case mst_text_gnu_ifunc:
559c50c785cSJohn Marino       write_exp_elt_type (objfile_type (objfile)
560c50c785cSJohn Marino 					       ->nodebug_text_gnu_ifunc_symbol);
561c50c785cSJohn Marino       break;
562c50c785cSJohn Marino 
5635796c8dcSSimon Schubert     case mst_data:
5645796c8dcSSimon Schubert     case mst_file_data:
5655796c8dcSSimon Schubert     case mst_bss:
5665796c8dcSSimon Schubert     case mst_file_bss:
5675796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_data_symbol);
5685796c8dcSSimon Schubert       break;
5695796c8dcSSimon Schubert 
570c50c785cSJohn Marino     case mst_slot_got_plt:
571c50c785cSJohn Marino       write_exp_elt_type (objfile_type (objfile)->nodebug_got_plt_symbol);
572c50c785cSJohn Marino       break;
573c50c785cSJohn Marino 
5745796c8dcSSimon Schubert     default:
5755796c8dcSSimon Schubert       write_exp_elt_type (objfile_type (objfile)->nodebug_unknown_symbol);
5765796c8dcSSimon Schubert       break;
5775796c8dcSSimon Schubert     }
5785796c8dcSSimon Schubert   write_exp_elt_opcode (UNOP_MEMVAL);
5795796c8dcSSimon Schubert }
5805796c8dcSSimon Schubert 
5815796c8dcSSimon Schubert /* Mark the current index as the starting location of a structure
5825796c8dcSSimon Schubert    expression.  This is used when completing on field names.  */
5835796c8dcSSimon Schubert 
5845796c8dcSSimon Schubert void
mark_struct_expression(void)5855796c8dcSSimon Schubert mark_struct_expression (void)
5865796c8dcSSimon Schubert {
587*ef5ccd6cSJohn Marino   gdb_assert (parse_completion
588*ef5ccd6cSJohn Marino 	      && expout_tag_completion_type == TYPE_CODE_UNDEF);
5895796c8dcSSimon Schubert   expout_last_struct = expout_ptr;
5905796c8dcSSimon Schubert }
5915796c8dcSSimon Schubert 
592*ef5ccd6cSJohn Marino /* Indicate that the current parser invocation is completing a tag.
593*ef5ccd6cSJohn Marino    TAG is the type code of the tag, and PTR and LENGTH represent the
594*ef5ccd6cSJohn Marino    start of the tag name.  */
595*ef5ccd6cSJohn Marino 
596*ef5ccd6cSJohn Marino void
mark_completion_tag(enum type_code tag,const char * ptr,int length)597*ef5ccd6cSJohn Marino mark_completion_tag (enum type_code tag, const char *ptr, int length)
598*ef5ccd6cSJohn Marino {
599*ef5ccd6cSJohn Marino   gdb_assert (parse_completion
600*ef5ccd6cSJohn Marino 	      && expout_tag_completion_type == TYPE_CODE_UNDEF
601*ef5ccd6cSJohn Marino 	      && expout_completion_name == NULL
602*ef5ccd6cSJohn Marino 	      && expout_last_struct == -1);
603*ef5ccd6cSJohn Marino   gdb_assert (tag == TYPE_CODE_UNION
604*ef5ccd6cSJohn Marino 	      || tag == TYPE_CODE_STRUCT
605*ef5ccd6cSJohn Marino 	      || tag == TYPE_CODE_CLASS
606*ef5ccd6cSJohn Marino 	      || tag == TYPE_CODE_ENUM);
607*ef5ccd6cSJohn Marino   expout_tag_completion_type = tag;
608*ef5ccd6cSJohn Marino   expout_completion_name = xmalloc (length + 1);
609*ef5ccd6cSJohn Marino   memcpy (expout_completion_name, ptr, length);
610*ef5ccd6cSJohn Marino   expout_completion_name[length] = '\0';
611*ef5ccd6cSJohn Marino }
612*ef5ccd6cSJohn Marino 
6135796c8dcSSimon Schubert 
6145796c8dcSSimon Schubert /* Recognize tokens that start with '$'.  These include:
6155796c8dcSSimon Schubert 
6165796c8dcSSimon Schubert    $regname     A native register name or a "standard
6175796c8dcSSimon Schubert    register name".
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert    $variable    A convenience variable with a name chosen
6205796c8dcSSimon Schubert    by the user.
6215796c8dcSSimon Schubert 
6225796c8dcSSimon Schubert    $digits              Value history with index <digits>, starting
6235796c8dcSSimon Schubert    from the first value which has index 1.
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert    $$digits     Value history with index <digits> relative
626c50c785cSJohn Marino    to the last value.  I.e. $$0 is the last
6275796c8dcSSimon Schubert    value, $$1 is the one previous to that, $$2
6285796c8dcSSimon Schubert    is the one previous to $$1, etc.
6295796c8dcSSimon Schubert 
6305796c8dcSSimon Schubert    $ | $0 | $$0 The last value in the value history.
6315796c8dcSSimon Schubert 
6325796c8dcSSimon Schubert    $$           An abbreviation for the second to the last
633c50c785cSJohn Marino    value in the value history, I.e. $$1  */
6345796c8dcSSimon Schubert 
6355796c8dcSSimon Schubert void
write_dollar_variable(struct stoken str)6365796c8dcSSimon Schubert write_dollar_variable (struct stoken str)
6375796c8dcSSimon Schubert {
6385796c8dcSSimon Schubert   struct symbol *sym = NULL;
6395796c8dcSSimon Schubert   struct minimal_symbol *msym = NULL;
6405796c8dcSSimon Schubert   struct internalvar *isym = NULL;
6415796c8dcSSimon Schubert 
6425796c8dcSSimon Schubert   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
6435796c8dcSSimon Schubert      and $$digits (equivalent to $<-digits> if you could type that).  */
6445796c8dcSSimon Schubert 
6455796c8dcSSimon Schubert   int negate = 0;
6465796c8dcSSimon Schubert   int i = 1;
6475796c8dcSSimon Schubert   /* Double dollar means negate the number and add -1 as well.
6485796c8dcSSimon Schubert      Thus $$ alone means -1.  */
6495796c8dcSSimon Schubert   if (str.length >= 2 && str.ptr[1] == '$')
6505796c8dcSSimon Schubert     {
6515796c8dcSSimon Schubert       negate = 1;
6525796c8dcSSimon Schubert       i = 2;
6535796c8dcSSimon Schubert     }
6545796c8dcSSimon Schubert   if (i == str.length)
6555796c8dcSSimon Schubert     {
656c50c785cSJohn Marino       /* Just dollars (one or two).  */
6575796c8dcSSimon Schubert       i = -negate;
6585796c8dcSSimon Schubert       goto handle_last;
6595796c8dcSSimon Schubert     }
6605796c8dcSSimon Schubert   /* Is the rest of the token digits?  */
6615796c8dcSSimon Schubert   for (; i < str.length; i++)
6625796c8dcSSimon Schubert     if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
6635796c8dcSSimon Schubert       break;
6645796c8dcSSimon Schubert   if (i == str.length)
6655796c8dcSSimon Schubert     {
6665796c8dcSSimon Schubert       i = atoi (str.ptr + 1 + negate);
6675796c8dcSSimon Schubert       if (negate)
6685796c8dcSSimon Schubert 	i = -i;
6695796c8dcSSimon Schubert       goto handle_last;
6705796c8dcSSimon Schubert     }
6715796c8dcSSimon Schubert 
6725796c8dcSSimon Schubert   /* Handle tokens that refer to machine registers:
6735796c8dcSSimon Schubert      $ followed by a register name.  */
6745796c8dcSSimon Schubert   i = user_reg_map_name_to_regnum (parse_gdbarch,
6755796c8dcSSimon Schubert 				   str.ptr + 1, str.length - 1);
6765796c8dcSSimon Schubert   if (i >= 0)
6775796c8dcSSimon Schubert     goto handle_register;
6785796c8dcSSimon Schubert 
6795796c8dcSSimon Schubert   /* Any names starting with $ are probably debugger internal variables.  */
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert   isym = lookup_only_internalvar (copy_name (str) + 1);
6825796c8dcSSimon Schubert   if (isym)
6835796c8dcSSimon Schubert     {
6845796c8dcSSimon Schubert       write_exp_elt_opcode (OP_INTERNALVAR);
6855796c8dcSSimon Schubert       write_exp_elt_intern (isym);
6865796c8dcSSimon Schubert       write_exp_elt_opcode (OP_INTERNALVAR);
6875796c8dcSSimon Schubert       return;
6885796c8dcSSimon Schubert     }
6895796c8dcSSimon Schubert 
6905796c8dcSSimon Schubert   /* On some systems, such as HP-UX and hppa-linux, certain system routines
6915796c8dcSSimon Schubert      have names beginning with $ or $$.  Check for those, first.  */
6925796c8dcSSimon Schubert 
6935796c8dcSSimon Schubert   sym = lookup_symbol (copy_name (str), (struct block *) NULL,
694*ef5ccd6cSJohn Marino 		       VAR_DOMAIN, NULL);
6955796c8dcSSimon Schubert   if (sym)
6965796c8dcSSimon Schubert     {
6975796c8dcSSimon Schubert       write_exp_elt_opcode (OP_VAR_VALUE);
6985796c8dcSSimon Schubert       write_exp_elt_block (block_found);	/* set by lookup_symbol */
6995796c8dcSSimon Schubert       write_exp_elt_sym (sym);
7005796c8dcSSimon Schubert       write_exp_elt_opcode (OP_VAR_VALUE);
7015796c8dcSSimon Schubert       return;
7025796c8dcSSimon Schubert     }
7035796c8dcSSimon Schubert   msym = lookup_minimal_symbol (copy_name (str), NULL, NULL);
7045796c8dcSSimon Schubert   if (msym)
7055796c8dcSSimon Schubert     {
7065796c8dcSSimon Schubert       write_exp_msymbol (msym);
7075796c8dcSSimon Schubert       return;
7085796c8dcSSimon Schubert     }
7095796c8dcSSimon Schubert 
7105796c8dcSSimon Schubert   /* Any other names are assumed to be debugger internal variables.  */
7115796c8dcSSimon Schubert 
7125796c8dcSSimon Schubert   write_exp_elt_opcode (OP_INTERNALVAR);
7135796c8dcSSimon Schubert   write_exp_elt_intern (create_internalvar (copy_name (str) + 1));
7145796c8dcSSimon Schubert   write_exp_elt_opcode (OP_INTERNALVAR);
7155796c8dcSSimon Schubert   return;
7165796c8dcSSimon Schubert handle_last:
7175796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LAST);
7185796c8dcSSimon Schubert   write_exp_elt_longcst ((LONGEST) i);
7195796c8dcSSimon Schubert   write_exp_elt_opcode (OP_LAST);
7205796c8dcSSimon Schubert   return;
7215796c8dcSSimon Schubert handle_register:
7225796c8dcSSimon Schubert   write_exp_elt_opcode (OP_REGISTER);
7235796c8dcSSimon Schubert   str.length--;
7245796c8dcSSimon Schubert   str.ptr++;
7255796c8dcSSimon Schubert   write_exp_string (str);
7265796c8dcSSimon Schubert   write_exp_elt_opcode (OP_REGISTER);
7275796c8dcSSimon Schubert   return;
7285796c8dcSSimon Schubert }
7295796c8dcSSimon Schubert 
7305796c8dcSSimon Schubert 
7315796c8dcSSimon Schubert char *
find_template_name_end(char * p)7325796c8dcSSimon Schubert find_template_name_end (char *p)
7335796c8dcSSimon Schubert {
7345796c8dcSSimon Schubert   int depth = 1;
7355796c8dcSSimon Schubert   int just_seen_right = 0;
7365796c8dcSSimon Schubert   int just_seen_colon = 0;
7375796c8dcSSimon Schubert   int just_seen_space = 0;
7385796c8dcSSimon Schubert 
7395796c8dcSSimon Schubert   if (!p || (*p != '<'))
7405796c8dcSSimon Schubert     return 0;
7415796c8dcSSimon Schubert 
7425796c8dcSSimon Schubert   while (*++p)
7435796c8dcSSimon Schubert     {
7445796c8dcSSimon Schubert       switch (*p)
7455796c8dcSSimon Schubert 	{
7465796c8dcSSimon Schubert 	case '\'':
7475796c8dcSSimon Schubert 	case '\"':
7485796c8dcSSimon Schubert 	case '{':
7495796c8dcSSimon Schubert 	case '}':
7505796c8dcSSimon Schubert 	  /* In future, may want to allow these??  */
7515796c8dcSSimon Schubert 	  return 0;
7525796c8dcSSimon Schubert 	case '<':
7535796c8dcSSimon Schubert 	  depth++;		/* start nested template */
7545796c8dcSSimon Schubert 	  if (just_seen_colon || just_seen_right || just_seen_space)
7555796c8dcSSimon Schubert 	    return 0;		/* but not after : or :: or > or space */
7565796c8dcSSimon Schubert 	  break;
7575796c8dcSSimon Schubert 	case '>':
7585796c8dcSSimon Schubert 	  if (just_seen_colon || just_seen_right)
7595796c8dcSSimon Schubert 	    return 0;		/* end a (nested?) template */
7605796c8dcSSimon Schubert 	  just_seen_right = 1;	/* but not after : or :: */
7615796c8dcSSimon Schubert 	  if (--depth == 0)	/* also disallow >>, insist on > > */
7625796c8dcSSimon Schubert 	    return ++p;		/* if outermost ended, return */
7635796c8dcSSimon Schubert 	  break;
7645796c8dcSSimon Schubert 	case ':':
7655796c8dcSSimon Schubert 	  if (just_seen_space || (just_seen_colon > 1))
7665796c8dcSSimon Schubert 	    return 0;		/* nested class spec coming up */
7675796c8dcSSimon Schubert 	  just_seen_colon++;	/* we allow :: but not :::: */
7685796c8dcSSimon Schubert 	  break;
7695796c8dcSSimon Schubert 	case ' ':
7705796c8dcSSimon Schubert 	  break;
7715796c8dcSSimon Schubert 	default:
7725796c8dcSSimon Schubert 	  if (!((*p >= 'a' && *p <= 'z') ||	/* allow token chars */
7735796c8dcSSimon Schubert 		(*p >= 'A' && *p <= 'Z') ||
7745796c8dcSSimon Schubert 		(*p >= '0' && *p <= '9') ||
7755796c8dcSSimon Schubert 		(*p == '_') || (*p == ',') ||	/* commas for template args */
7765796c8dcSSimon Schubert 		(*p == '&') || (*p == '*') ||	/* pointer and ref types */
7775796c8dcSSimon Schubert 		(*p == '(') || (*p == ')') ||	/* function types */
7785796c8dcSSimon Schubert 		(*p == '[') || (*p == ']')))	/* array types */
7795796c8dcSSimon Schubert 	    return 0;
7805796c8dcSSimon Schubert 	}
7815796c8dcSSimon Schubert       if (*p != ' ')
7825796c8dcSSimon Schubert 	just_seen_space = 0;
7835796c8dcSSimon Schubert       if (*p != ':')
7845796c8dcSSimon Schubert 	just_seen_colon = 0;
7855796c8dcSSimon Schubert       if (*p != '>')
7865796c8dcSSimon Schubert 	just_seen_right = 0;
7875796c8dcSSimon Schubert     }
7885796c8dcSSimon Schubert   return 0;
7895796c8dcSSimon Schubert }
7905796c8dcSSimon Schubert 
7915796c8dcSSimon Schubert 
792*ef5ccd6cSJohn Marino /* Return a null-terminated temporary copy of the name of a string token.
7935796c8dcSSimon Schubert 
794*ef5ccd6cSJohn Marino    Tokens that refer to names do so with explicit pointer and length,
795*ef5ccd6cSJohn Marino    so they can share the storage that lexptr is parsing.
796*ef5ccd6cSJohn Marino    When it is necessary to pass a name to a function that expects
797*ef5ccd6cSJohn Marino    a null-terminated string, the substring is copied out
798*ef5ccd6cSJohn Marino    into a separate block of storage.
799*ef5ccd6cSJohn Marino 
800*ef5ccd6cSJohn Marino    N.B. A single buffer is reused on each call.  */
8015796c8dcSSimon Schubert 
8025796c8dcSSimon Schubert char *
copy_name(struct stoken token)8035796c8dcSSimon Schubert copy_name (struct stoken token)
8045796c8dcSSimon Schubert {
805*ef5ccd6cSJohn Marino   /* A temporary buffer for identifiers, so we can null-terminate them.
806*ef5ccd6cSJohn Marino      We allocate this with xrealloc.  parse_exp_1 used to allocate with
807*ef5ccd6cSJohn Marino      alloca, using the size of the whole expression as a conservative
808*ef5ccd6cSJohn Marino      estimate of the space needed.  However, macro expansion can
809*ef5ccd6cSJohn Marino      introduce names longer than the original expression; there's no
810*ef5ccd6cSJohn Marino      practical way to know beforehand how large that might be.  */
811*ef5ccd6cSJohn Marino   static char *namecopy;
812*ef5ccd6cSJohn Marino   static size_t namecopy_size;
813*ef5ccd6cSJohn Marino 
8145796c8dcSSimon Schubert   /* Make sure there's enough space for the token.  */
8155796c8dcSSimon Schubert   if (namecopy_size < token.length + 1)
8165796c8dcSSimon Schubert     {
8175796c8dcSSimon Schubert       namecopy_size = token.length + 1;
8185796c8dcSSimon Schubert       namecopy = xrealloc (namecopy, token.length + 1);
8195796c8dcSSimon Schubert     }
8205796c8dcSSimon Schubert 
8215796c8dcSSimon Schubert   memcpy (namecopy, token.ptr, token.length);
8225796c8dcSSimon Schubert   namecopy[token.length] = 0;
8235796c8dcSSimon Schubert 
8245796c8dcSSimon Schubert   return namecopy;
8255796c8dcSSimon Schubert }
8265796c8dcSSimon Schubert 
8275796c8dcSSimon Schubert 
828*ef5ccd6cSJohn Marino /* See comments on parser-defs.h.  */
829*ef5ccd6cSJohn Marino 
830*ef5ccd6cSJohn Marino int
prefixify_expression(struct expression * expr)8315796c8dcSSimon Schubert prefixify_expression (struct expression *expr)
8325796c8dcSSimon Schubert {
8335796c8dcSSimon Schubert   int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
8345796c8dcSSimon Schubert   struct expression *temp;
8355796c8dcSSimon Schubert   int inpos = expr->nelts, outpos = 0;
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert   temp = (struct expression *) alloca (len);
8385796c8dcSSimon Schubert 
8395796c8dcSSimon Schubert   /* Copy the original expression into temp.  */
8405796c8dcSSimon Schubert   memcpy (temp, expr, len);
8415796c8dcSSimon Schubert 
8425796c8dcSSimon Schubert   return prefixify_subexp (temp, expr, inpos, outpos);
8435796c8dcSSimon Schubert }
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert /* Return the number of exp_elements in the postfix subexpression
8465796c8dcSSimon Schubert    of EXPR whose operator is at index ENDPOS - 1 in EXPR.  */
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert int
length_of_subexp(struct expression * expr,int endpos)8495796c8dcSSimon Schubert length_of_subexp (struct expression *expr, int endpos)
8505796c8dcSSimon Schubert {
851cf7f2e2dSJohn Marino   int oplen, args;
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert   operator_length (expr, endpos, &oplen, &args);
8545796c8dcSSimon Schubert 
8555796c8dcSSimon Schubert   while (args > 0)
8565796c8dcSSimon Schubert     {
8575796c8dcSSimon Schubert       oplen += length_of_subexp (expr, endpos - oplen);
8585796c8dcSSimon Schubert       args--;
8595796c8dcSSimon Schubert     }
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert   return oplen;
8625796c8dcSSimon Schubert }
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert /* Sets *OPLENP to the length of the operator whose (last) index is
8655796c8dcSSimon Schubert    ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
8665796c8dcSSimon Schubert    operator takes.  */
8675796c8dcSSimon Schubert 
8685796c8dcSSimon Schubert void
operator_length(const struct expression * expr,int endpos,int * oplenp,int * argsp)869cf7f2e2dSJohn Marino operator_length (const struct expression *expr, int endpos, int *oplenp,
870cf7f2e2dSJohn Marino 		 int *argsp)
8715796c8dcSSimon Schubert {
8725796c8dcSSimon Schubert   expr->language_defn->la_exp_desc->operator_length (expr, endpos,
8735796c8dcSSimon Schubert 						     oplenp, argsp);
8745796c8dcSSimon Schubert }
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert /* Default value for operator_length in exp_descriptor vectors.  */
8775796c8dcSSimon Schubert 
8785796c8dcSSimon Schubert void
operator_length_standard(const struct expression * expr,int endpos,int * oplenp,int * argsp)879cf7f2e2dSJohn Marino operator_length_standard (const struct expression *expr, int endpos,
8805796c8dcSSimon Schubert 			  int *oplenp, int *argsp)
8815796c8dcSSimon Schubert {
8825796c8dcSSimon Schubert   int oplen = 1;
8835796c8dcSSimon Schubert   int args = 0;
8845796c8dcSSimon Schubert   enum f90_range_type range_type;
8855796c8dcSSimon Schubert   int i;
8865796c8dcSSimon Schubert 
8875796c8dcSSimon Schubert   if (endpos < 1)
8885796c8dcSSimon Schubert     error (_("?error in operator_length_standard"));
8895796c8dcSSimon Schubert 
8905796c8dcSSimon Schubert   i = (int) expr->elts[endpos - 1].opcode;
8915796c8dcSSimon Schubert 
8925796c8dcSSimon Schubert   switch (i)
8935796c8dcSSimon Schubert     {
8945796c8dcSSimon Schubert       /* C++  */
8955796c8dcSSimon Schubert     case OP_SCOPE:
8965796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
8975796c8dcSSimon Schubert       oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
8985796c8dcSSimon Schubert       break;
8995796c8dcSSimon Schubert 
9005796c8dcSSimon Schubert     case OP_LONG:
9015796c8dcSSimon Schubert     case OP_DOUBLE:
9025796c8dcSSimon Schubert     case OP_DECFLOAT:
9035796c8dcSSimon Schubert     case OP_VAR_VALUE:
9045796c8dcSSimon Schubert       oplen = 4;
9055796c8dcSSimon Schubert       break;
9065796c8dcSSimon Schubert 
9075796c8dcSSimon Schubert     case OP_TYPE:
9085796c8dcSSimon Schubert     case OP_BOOL:
9095796c8dcSSimon Schubert     case OP_LAST:
9105796c8dcSSimon Schubert     case OP_INTERNALVAR:
911a45ae5f8SJohn Marino     case OP_VAR_ENTRY_VALUE:
9125796c8dcSSimon Schubert       oplen = 3;
9135796c8dcSSimon Schubert       break;
9145796c8dcSSimon Schubert 
9155796c8dcSSimon Schubert     case OP_COMPLEX:
9165796c8dcSSimon Schubert       oplen = 3;
9175796c8dcSSimon Schubert       args = 2;
9185796c8dcSSimon Schubert       break;
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert     case OP_FUNCALL:
9215796c8dcSSimon Schubert     case OP_F77_UNDETERMINED_ARGLIST:
9225796c8dcSSimon Schubert       oplen = 3;
9235796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
9245796c8dcSSimon Schubert       break;
9255796c8dcSSimon Schubert 
926cf7f2e2dSJohn Marino     case TYPE_INSTANCE:
927cf7f2e2dSJohn Marino       oplen = 4 + longest_to_int (expr->elts[endpos - 2].longconst);
928cf7f2e2dSJohn Marino       args = 1;
929cf7f2e2dSJohn Marino       break;
930cf7f2e2dSJohn Marino 
931c50c785cSJohn Marino     case OP_OBJC_MSGCALL:	/* Objective C message (method) call.  */
9325796c8dcSSimon Schubert       oplen = 4;
9335796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
9345796c8dcSSimon Schubert       break;
9355796c8dcSSimon Schubert 
9365796c8dcSSimon Schubert     case UNOP_MAX:
9375796c8dcSSimon Schubert     case UNOP_MIN:
9385796c8dcSSimon Schubert       oplen = 3;
9395796c8dcSSimon Schubert       break;
9405796c8dcSSimon Schubert 
941*ef5ccd6cSJohn Marino     case UNOP_CAST_TYPE:
942cf7f2e2dSJohn Marino     case UNOP_DYNAMIC_CAST:
943cf7f2e2dSJohn Marino     case UNOP_REINTERPRET_CAST:
944*ef5ccd6cSJohn Marino     case UNOP_MEMVAL_TYPE:
945*ef5ccd6cSJohn Marino       oplen = 1;
946*ef5ccd6cSJohn Marino       args = 2;
947*ef5ccd6cSJohn Marino       break;
948*ef5ccd6cSJohn Marino 
949*ef5ccd6cSJohn Marino     case BINOP_VAL:
950*ef5ccd6cSJohn Marino     case UNOP_CAST:
9515796c8dcSSimon Schubert     case UNOP_MEMVAL:
9525796c8dcSSimon Schubert       oplen = 3;
9535796c8dcSSimon Schubert       args = 1;
9545796c8dcSSimon Schubert       break;
9555796c8dcSSimon Schubert 
9565796c8dcSSimon Schubert     case UNOP_MEMVAL_TLS:
9575796c8dcSSimon Schubert       oplen = 4;
9585796c8dcSSimon Schubert       args = 1;
9595796c8dcSSimon Schubert       break;
9605796c8dcSSimon Schubert 
9615796c8dcSSimon Schubert     case UNOP_ABS:
9625796c8dcSSimon Schubert     case UNOP_CAP:
9635796c8dcSSimon Schubert     case UNOP_CHR:
9645796c8dcSSimon Schubert     case UNOP_FLOAT:
9655796c8dcSSimon Schubert     case UNOP_HIGH:
9665796c8dcSSimon Schubert     case UNOP_ODD:
9675796c8dcSSimon Schubert     case UNOP_ORD:
9685796c8dcSSimon Schubert     case UNOP_TRUNC:
969*ef5ccd6cSJohn Marino     case OP_TYPEOF:
970*ef5ccd6cSJohn Marino     case OP_DECLTYPE:
9715796c8dcSSimon Schubert       oplen = 1;
9725796c8dcSSimon Schubert       args = 1;
9735796c8dcSSimon Schubert       break;
9745796c8dcSSimon Schubert 
975cf7f2e2dSJohn Marino     case OP_ADL_FUNC:
976cf7f2e2dSJohn Marino       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
977cf7f2e2dSJohn Marino       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
978cf7f2e2dSJohn Marino       oplen++;
979cf7f2e2dSJohn Marino       oplen++;
980cf7f2e2dSJohn Marino       break;
981cf7f2e2dSJohn Marino 
9825796c8dcSSimon Schubert     case STRUCTOP_STRUCT:
9835796c8dcSSimon Schubert     case STRUCTOP_PTR:
9845796c8dcSSimon Schubert       args = 1;
9855796c8dcSSimon Schubert       /* fall through */
9865796c8dcSSimon Schubert     case OP_REGISTER:
9875796c8dcSSimon Schubert     case OP_M2_STRING:
9885796c8dcSSimon Schubert     case OP_STRING:
989c50c785cSJohn Marino     case OP_OBJC_NSSTRING:	/* Objective C Foundation Class
990c50c785cSJohn Marino 				   NSString constant.  */
991c50c785cSJohn Marino     case OP_OBJC_SELECTOR:	/* Objective C "@selector" pseudo-op.  */
9925796c8dcSSimon Schubert     case OP_NAME:
9935796c8dcSSimon Schubert       oplen = longest_to_int (expr->elts[endpos - 2].longconst);
9945796c8dcSSimon Schubert       oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
9955796c8dcSSimon Schubert       break;
9965796c8dcSSimon Schubert 
9975796c8dcSSimon Schubert     case OP_ARRAY:
9985796c8dcSSimon Schubert       oplen = 4;
9995796c8dcSSimon Schubert       args = longest_to_int (expr->elts[endpos - 2].longconst);
10005796c8dcSSimon Schubert       args -= longest_to_int (expr->elts[endpos - 3].longconst);
10015796c8dcSSimon Schubert       args += 1;
10025796c8dcSSimon Schubert       break;
10035796c8dcSSimon Schubert 
10045796c8dcSSimon Schubert     case TERNOP_COND:
10055796c8dcSSimon Schubert     case TERNOP_SLICE:
10065796c8dcSSimon Schubert       args = 3;
10075796c8dcSSimon Schubert       break;
10085796c8dcSSimon Schubert 
10095796c8dcSSimon Schubert       /* Modula-2 */
10105796c8dcSSimon Schubert     case MULTI_SUBSCRIPT:
10115796c8dcSSimon Schubert       oplen = 3;
10125796c8dcSSimon Schubert       args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
10135796c8dcSSimon Schubert       break;
10145796c8dcSSimon Schubert 
10155796c8dcSSimon Schubert     case BINOP_ASSIGN_MODIFY:
10165796c8dcSSimon Schubert       oplen = 3;
10175796c8dcSSimon Schubert       args = 2;
10185796c8dcSSimon Schubert       break;
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert       /* C++ */
10215796c8dcSSimon Schubert     case OP_THIS:
10225796c8dcSSimon Schubert       oplen = 2;
10235796c8dcSSimon Schubert       break;
10245796c8dcSSimon Schubert 
10255796c8dcSSimon Schubert     case OP_F90_RANGE:
10265796c8dcSSimon Schubert       oplen = 3;
10275796c8dcSSimon Schubert 
10285796c8dcSSimon Schubert       range_type = longest_to_int (expr->elts[endpos - 2].longconst);
10295796c8dcSSimon Schubert       switch (range_type)
10305796c8dcSSimon Schubert 	{
10315796c8dcSSimon Schubert 	case LOW_BOUND_DEFAULT:
10325796c8dcSSimon Schubert 	case HIGH_BOUND_DEFAULT:
10335796c8dcSSimon Schubert 	  args = 1;
10345796c8dcSSimon Schubert 	  break;
10355796c8dcSSimon Schubert 	case BOTH_BOUND_DEFAULT:
10365796c8dcSSimon Schubert 	  args = 0;
10375796c8dcSSimon Schubert 	  break;
10385796c8dcSSimon Schubert 	case NONE_BOUND_DEFAULT:
10395796c8dcSSimon Schubert 	  args = 2;
10405796c8dcSSimon Schubert 	  break;
10415796c8dcSSimon Schubert 	}
10425796c8dcSSimon Schubert 
10435796c8dcSSimon Schubert       break;
10445796c8dcSSimon Schubert 
10455796c8dcSSimon Schubert     default:
10465796c8dcSSimon Schubert       args = 1 + (i < (int) BINOP_END);
10475796c8dcSSimon Schubert     }
10485796c8dcSSimon Schubert 
10495796c8dcSSimon Schubert   *oplenp = oplen;
10505796c8dcSSimon Schubert   *argsp = args;
10515796c8dcSSimon Schubert }
10525796c8dcSSimon Schubert 
10535796c8dcSSimon Schubert /* Copy the subexpression ending just before index INEND in INEXPR
10545796c8dcSSimon Schubert    into OUTEXPR, starting at index OUTBEG.
10555796c8dcSSimon Schubert    In the process, convert it from suffix to prefix form.
10565796c8dcSSimon Schubert    If EXPOUT_LAST_STRUCT is -1, then this function always returns -1.
10575796c8dcSSimon Schubert    Otherwise, it returns the index of the subexpression which is the
10585796c8dcSSimon Schubert    left-hand-side of the expression at EXPOUT_LAST_STRUCT.  */
10595796c8dcSSimon Schubert 
10605796c8dcSSimon Schubert static int
prefixify_subexp(struct expression * inexpr,struct expression * outexpr,int inend,int outbeg)10615796c8dcSSimon Schubert prefixify_subexp (struct expression *inexpr,
10625796c8dcSSimon Schubert 		  struct expression *outexpr, int inend, int outbeg)
10635796c8dcSSimon Schubert {
10645796c8dcSSimon Schubert   int oplen;
10655796c8dcSSimon Schubert   int args;
10665796c8dcSSimon Schubert   int i;
10675796c8dcSSimon Schubert   int *arglens;
10685796c8dcSSimon Schubert   int result = -1;
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert   operator_length (inexpr, inend, &oplen, &args);
10715796c8dcSSimon Schubert 
10725796c8dcSSimon Schubert   /* Copy the final operator itself, from the end of the input
10735796c8dcSSimon Schubert      to the beginning of the output.  */
10745796c8dcSSimon Schubert   inend -= oplen;
10755796c8dcSSimon Schubert   memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
10765796c8dcSSimon Schubert 	  EXP_ELEM_TO_BYTES (oplen));
10775796c8dcSSimon Schubert   outbeg += oplen;
10785796c8dcSSimon Schubert 
10795796c8dcSSimon Schubert   if (expout_last_struct == inend)
10805796c8dcSSimon Schubert     result = outbeg - oplen;
10815796c8dcSSimon Schubert 
10825796c8dcSSimon Schubert   /* Find the lengths of the arg subexpressions.  */
10835796c8dcSSimon Schubert   arglens = (int *) alloca (args * sizeof (int));
10845796c8dcSSimon Schubert   for (i = args - 1; i >= 0; i--)
10855796c8dcSSimon Schubert     {
10865796c8dcSSimon Schubert       oplen = length_of_subexp (inexpr, inend);
10875796c8dcSSimon Schubert       arglens[i] = oplen;
10885796c8dcSSimon Schubert       inend -= oplen;
10895796c8dcSSimon Schubert     }
10905796c8dcSSimon Schubert 
10915796c8dcSSimon Schubert   /* Now copy each subexpression, preserving the order of
10925796c8dcSSimon Schubert      the subexpressions, but prefixifying each one.
10935796c8dcSSimon Schubert      In this loop, inend starts at the beginning of
10945796c8dcSSimon Schubert      the expression this level is working on
10955796c8dcSSimon Schubert      and marches forward over the arguments.
10965796c8dcSSimon Schubert      outbeg does similarly in the output.  */
10975796c8dcSSimon Schubert   for (i = 0; i < args; i++)
10985796c8dcSSimon Schubert     {
10995796c8dcSSimon Schubert       int r;
1100cf7f2e2dSJohn Marino 
11015796c8dcSSimon Schubert       oplen = arglens[i];
11025796c8dcSSimon Schubert       inend += oplen;
11035796c8dcSSimon Schubert       r = prefixify_subexp (inexpr, outexpr, inend, outbeg);
11045796c8dcSSimon Schubert       if (r != -1)
11055796c8dcSSimon Schubert 	{
11065796c8dcSSimon Schubert 	  /* Return immediately.  We probably have only parsed a
11075796c8dcSSimon Schubert 	     partial expression, so we don't want to try to reverse
11085796c8dcSSimon Schubert 	     the other operands.  */
11095796c8dcSSimon Schubert 	  return r;
11105796c8dcSSimon Schubert 	}
11115796c8dcSSimon Schubert       outbeg += oplen;
11125796c8dcSSimon Schubert     }
11135796c8dcSSimon Schubert 
11145796c8dcSSimon Schubert   return result;
11155796c8dcSSimon Schubert }
11165796c8dcSSimon Schubert 
11175796c8dcSSimon Schubert /* Read an expression from the string *STRINGPTR points to,
11185796c8dcSSimon Schubert    parse it, and return a pointer to a struct expression that we malloc.
11195796c8dcSSimon Schubert    Use block BLOCK as the lexical context for variable names;
11205796c8dcSSimon Schubert    if BLOCK is zero, use the block of the selected stack frame.
11215796c8dcSSimon Schubert    Meanwhile, advance *STRINGPTR to point after the expression,
11225796c8dcSSimon Schubert    at the first nonwhite character that is not part of the expression
11235796c8dcSSimon Schubert    (possibly a null character).
11245796c8dcSSimon Schubert 
11255796c8dcSSimon Schubert    If COMMA is nonzero, stop if a comma is reached.  */
11265796c8dcSSimon Schubert 
11275796c8dcSSimon Schubert struct expression *
parse_exp_1(const char ** stringptr,CORE_ADDR pc,const struct block * block,int comma)1128*ef5ccd6cSJohn Marino parse_exp_1 (const char **stringptr, CORE_ADDR pc, const struct block *block,
1129*ef5ccd6cSJohn Marino 	     int comma)
11305796c8dcSSimon Schubert {
1131*ef5ccd6cSJohn Marino   struct expression *expr;
1132*ef5ccd6cSJohn Marino   char *const_hack = *stringptr ? xstrdup (*stringptr) : NULL;
1133*ef5ccd6cSJohn Marino   char *orig = const_hack;
1134*ef5ccd6cSJohn Marino   struct cleanup *back_to = make_cleanup (xfree, const_hack);
1135*ef5ccd6cSJohn Marino 
1136*ef5ccd6cSJohn Marino   expr = parse_exp_in_context (&const_hack, pc, block, comma, 0, NULL);
1137*ef5ccd6cSJohn Marino   (*stringptr) += const_hack - orig;
1138*ef5ccd6cSJohn Marino   do_cleanups (back_to);
1139*ef5ccd6cSJohn Marino   return expr;
11405796c8dcSSimon Schubert }
11415796c8dcSSimon Schubert 
11425796c8dcSSimon Schubert /* As for parse_exp_1, except that if VOID_CONTEXT_P, then
11435796c8dcSSimon Schubert    no value is expected from the expression.
11445796c8dcSSimon Schubert    OUT_SUBEXP is set when attempting to complete a field name; in this
11455796c8dcSSimon Schubert    case it is set to the index of the subexpression on the
11465796c8dcSSimon Schubert    left-hand-side of the struct op.  If not doing such completion, it
11475796c8dcSSimon Schubert    is left untouched.  */
11485796c8dcSSimon Schubert 
11495796c8dcSSimon Schubert static struct expression *
parse_exp_in_context(char ** stringptr,CORE_ADDR pc,const struct block * block,int comma,int void_context_p,int * out_subexp)1150*ef5ccd6cSJohn Marino parse_exp_in_context (char **stringptr, CORE_ADDR pc, const struct block *block,
1151*ef5ccd6cSJohn Marino 		      int comma, int void_context_p, int *out_subexp)
11525796c8dcSSimon Schubert {
11535796c8dcSSimon Schubert   volatile struct gdb_exception except;
1154*ef5ccd6cSJohn Marino   struct cleanup *old_chain, *inner_chain;
1155cf7f2e2dSJohn Marino   const struct language_defn *lang = NULL;
11565796c8dcSSimon Schubert   int subexp;
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert   lexptr = *stringptr;
11595796c8dcSSimon Schubert   prev_lexptr = NULL;
11605796c8dcSSimon Schubert 
11615796c8dcSSimon Schubert   paren_depth = 0;
1162*ef5ccd6cSJohn Marino   type_stack.depth = 0;
11635796c8dcSSimon Schubert   expout_last_struct = -1;
1164*ef5ccd6cSJohn Marino   expout_tag_completion_type = TYPE_CODE_UNDEF;
1165*ef5ccd6cSJohn Marino   xfree (expout_completion_name);
1166*ef5ccd6cSJohn Marino   expout_completion_name = NULL;
11675796c8dcSSimon Schubert 
11685796c8dcSSimon Schubert   comma_terminates = comma;
11695796c8dcSSimon Schubert 
11705796c8dcSSimon Schubert   if (lexptr == 0 || *lexptr == 0)
11715796c8dcSSimon Schubert     error_no_arg (_("expression to compute"));
11725796c8dcSSimon Schubert 
11735796c8dcSSimon Schubert   old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
11745796c8dcSSimon Schubert   funcall_chain = 0;
11755796c8dcSSimon Schubert 
11765796c8dcSSimon Schubert   expression_context_block = block;
11775796c8dcSSimon Schubert 
11785796c8dcSSimon Schubert   /* If no context specified, try using the current frame, if any.  */
11795796c8dcSSimon Schubert   if (!expression_context_block)
11805796c8dcSSimon Schubert     expression_context_block = get_selected_block (&expression_context_pc);
1181*ef5ccd6cSJohn Marino   else if (pc == 0)
11825796c8dcSSimon Schubert     expression_context_pc = BLOCK_START (expression_context_block);
1183*ef5ccd6cSJohn Marino   else
1184*ef5ccd6cSJohn Marino     expression_context_pc = pc;
11855796c8dcSSimon Schubert 
11865796c8dcSSimon Schubert   /* Fall back to using the current source static context, if any.  */
11875796c8dcSSimon Schubert 
11885796c8dcSSimon Schubert   if (!expression_context_block)
11895796c8dcSSimon Schubert     {
11905796c8dcSSimon Schubert       struct symtab_and_line cursal = get_current_source_symtab_and_line ();
11915796c8dcSSimon Schubert       if (cursal.symtab)
11925796c8dcSSimon Schubert 	expression_context_block
11935796c8dcSSimon Schubert 	  = BLOCKVECTOR_BLOCK (BLOCKVECTOR (cursal.symtab), STATIC_BLOCK);
11945796c8dcSSimon Schubert       if (expression_context_block)
11955796c8dcSSimon Schubert 	expression_context_pc = BLOCK_START (expression_context_block);
11965796c8dcSSimon Schubert     }
11975796c8dcSSimon Schubert 
1198cf7f2e2dSJohn Marino   if (language_mode == language_mode_auto && block != NULL)
1199cf7f2e2dSJohn Marino     {
1200cf7f2e2dSJohn Marino       /* Find the language associated to the given context block.
1201cf7f2e2dSJohn Marino          Default to the current language if it can not be determined.
1202cf7f2e2dSJohn Marino 
1203cf7f2e2dSJohn Marino          Note that using the language corresponding to the current frame
1204cf7f2e2dSJohn Marino          can sometimes give unexpected results.  For instance, this
1205cf7f2e2dSJohn Marino          routine is often called several times during the inferior
1206cf7f2e2dSJohn Marino          startup phase to re-parse breakpoint expressions after
1207cf7f2e2dSJohn Marino          a new shared library has been loaded.  The language associated
1208cf7f2e2dSJohn Marino          to the current frame at this moment is not relevant for
1209cf7f2e2dSJohn Marino          the breakpoint.  Using it would therefore be silly, so it seems
1210cf7f2e2dSJohn Marino          better to rely on the current language rather than relying on
1211cf7f2e2dSJohn Marino          the current frame language to parse the expression.  That's why
1212cf7f2e2dSJohn Marino          we do the following language detection only if the context block
1213cf7f2e2dSJohn Marino          has been specifically provided.  */
1214cf7f2e2dSJohn Marino       struct symbol *func = block_linkage_function (block);
1215cf7f2e2dSJohn Marino 
1216cf7f2e2dSJohn Marino       if (func != NULL)
1217cf7f2e2dSJohn Marino         lang = language_def (SYMBOL_LANGUAGE (func));
1218cf7f2e2dSJohn Marino       if (lang == NULL || lang->la_language == language_unknown)
1219cf7f2e2dSJohn Marino         lang = current_language;
1220cf7f2e2dSJohn Marino     }
1221cf7f2e2dSJohn Marino   else
1222cf7f2e2dSJohn Marino     lang = current_language;
1223cf7f2e2dSJohn Marino 
1224*ef5ccd6cSJohn Marino   /* get_current_arch may reset CURRENT_LANGUAGE via select_frame.
1225*ef5ccd6cSJohn Marino      While we need CURRENT_LANGUAGE to be set to LANG (for lookup_symbol
1226*ef5ccd6cSJohn Marino      and others called from *.y) ensure CURRENT_LANGUAGE gets restored
1227*ef5ccd6cSJohn Marino      to the value matching SELECTED_FRAME as set by get_current_arch.  */
1228*ef5ccd6cSJohn Marino   initialize_expout (10, lang, get_current_arch ());
1229*ef5ccd6cSJohn Marino   inner_chain = make_cleanup_restore_current_language ();
1230*ef5ccd6cSJohn Marino   set_language (lang->la_language);
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
12335796c8dcSSimon Schubert     {
1234cf7f2e2dSJohn Marino       if (lang->la_parser ())
1235cf7f2e2dSJohn Marino         lang->la_error (NULL);
12365796c8dcSSimon Schubert     }
12375796c8dcSSimon Schubert   if (except.reason < 0)
12385796c8dcSSimon Schubert     {
1239*ef5ccd6cSJohn Marino       if (! parse_completion)
12405796c8dcSSimon Schubert 	{
12415796c8dcSSimon Schubert 	  xfree (expout);
12425796c8dcSSimon Schubert 	  throw_exception (except);
12435796c8dcSSimon Schubert 	}
12445796c8dcSSimon Schubert     }
12455796c8dcSSimon Schubert 
1246*ef5ccd6cSJohn Marino   reallocate_expout ();
12475796c8dcSSimon Schubert 
12485796c8dcSSimon Schubert   /* Convert expression from postfix form as generated by yacc
12495796c8dcSSimon Schubert      parser, to a prefix form.  */
12505796c8dcSSimon Schubert 
12515796c8dcSSimon Schubert   if (expressiondebug)
12525796c8dcSSimon Schubert     dump_raw_expression (expout, gdb_stdlog,
12535796c8dcSSimon Schubert 			 "before conversion to prefix form");
12545796c8dcSSimon Schubert 
12555796c8dcSSimon Schubert   subexp = prefixify_expression (expout);
12565796c8dcSSimon Schubert   if (out_subexp)
12575796c8dcSSimon Schubert     *out_subexp = subexp;
12585796c8dcSSimon Schubert 
1259cf7f2e2dSJohn Marino   lang->la_post_parser (&expout, void_context_p);
12605796c8dcSSimon Schubert 
12615796c8dcSSimon Schubert   if (expressiondebug)
12625796c8dcSSimon Schubert     dump_prefix_expression (expout, gdb_stdlog);
12635796c8dcSSimon Schubert 
1264*ef5ccd6cSJohn Marino   do_cleanups (inner_chain);
1265*ef5ccd6cSJohn Marino   discard_cleanups (old_chain);
1266*ef5ccd6cSJohn Marino 
12675796c8dcSSimon Schubert   *stringptr = lexptr;
12685796c8dcSSimon Schubert   return expout;
12695796c8dcSSimon Schubert }
12705796c8dcSSimon Schubert 
12715796c8dcSSimon Schubert /* Parse STRING as an expression, and complain if this fails
12725796c8dcSSimon Schubert    to use up all of the contents of STRING.  */
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert struct expression *
parse_expression(const char * string)1275*ef5ccd6cSJohn Marino parse_expression (const char *string)
12765796c8dcSSimon Schubert {
12775796c8dcSSimon Schubert   struct expression *exp;
1278cf7f2e2dSJohn Marino 
1279*ef5ccd6cSJohn Marino   exp = parse_exp_1 (&string, 0, 0, 0);
12805796c8dcSSimon Schubert   if (*string)
12815796c8dcSSimon Schubert     error (_("Junk after end of expression."));
12825796c8dcSSimon Schubert   return exp;
12835796c8dcSSimon Schubert }
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert /* Parse STRING as an expression.  If parsing ends in the middle of a
12865796c8dcSSimon Schubert    field reference, return the type of the left-hand-side of the
12875796c8dcSSimon Schubert    reference; furthermore, if the parsing ends in the field name,
1288c50c785cSJohn Marino    return the field name in *NAME.  If the parsing ends in the middle
1289c50c785cSJohn Marino    of a field reference, but the reference is somehow invalid, throw
1290c50c785cSJohn Marino    an exception.  In all other cases, return NULL.  Returned non-NULL
1291c50c785cSJohn Marino    *NAME must be freed by the caller.  */
12925796c8dcSSimon Schubert 
12935796c8dcSSimon Schubert struct type *
parse_expression_for_completion(char * string,char ** name,enum type_code * code)1294*ef5ccd6cSJohn Marino parse_expression_for_completion (char *string, char **name,
1295*ef5ccd6cSJohn Marino 				 enum type_code *code)
12965796c8dcSSimon Schubert {
12975796c8dcSSimon Schubert   struct expression *exp = NULL;
12985796c8dcSSimon Schubert   struct value *val;
12995796c8dcSSimon Schubert   int subexp;
13005796c8dcSSimon Schubert   volatile struct gdb_exception except;
13015796c8dcSSimon Schubert 
1302c50c785cSJohn Marino   TRY_CATCH (except, RETURN_MASK_ERROR)
13035796c8dcSSimon Schubert     {
1304*ef5ccd6cSJohn Marino       parse_completion = 1;
1305*ef5ccd6cSJohn Marino       exp = parse_exp_in_context (&string, 0, 0, 0, 0, &subexp);
13065796c8dcSSimon Schubert     }
1307*ef5ccd6cSJohn Marino   parse_completion = 0;
13085796c8dcSSimon Schubert   if (except.reason < 0 || ! exp)
13095796c8dcSSimon Schubert     return NULL;
1310*ef5ccd6cSJohn Marino 
1311*ef5ccd6cSJohn Marino   if (expout_tag_completion_type != TYPE_CODE_UNDEF)
1312*ef5ccd6cSJohn Marino     {
1313*ef5ccd6cSJohn Marino       *code = expout_tag_completion_type;
1314*ef5ccd6cSJohn Marino       *name = expout_completion_name;
1315*ef5ccd6cSJohn Marino       expout_completion_name = NULL;
1316*ef5ccd6cSJohn Marino       return NULL;
1317*ef5ccd6cSJohn Marino     }
1318*ef5ccd6cSJohn Marino 
13195796c8dcSSimon Schubert   if (expout_last_struct == -1)
13205796c8dcSSimon Schubert     {
13215796c8dcSSimon Schubert       xfree (exp);
13225796c8dcSSimon Schubert       return NULL;
13235796c8dcSSimon Schubert     }
13245796c8dcSSimon Schubert 
13255796c8dcSSimon Schubert   *name = extract_field_op (exp, &subexp);
13265796c8dcSSimon Schubert   if (!*name)
13275796c8dcSSimon Schubert     {
13285796c8dcSSimon Schubert       xfree (exp);
13295796c8dcSSimon Schubert       return NULL;
13305796c8dcSSimon Schubert     }
1331c50c785cSJohn Marino 
1332c50c785cSJohn Marino   /* This might throw an exception.  If so, we want to let it
1333c50c785cSJohn Marino      propagate.  */
1334c50c785cSJohn Marino   val = evaluate_subexpression_type (exp, subexp);
13355796c8dcSSimon Schubert   /* (*NAME) is a part of the EXP memory block freed below.  */
13365796c8dcSSimon Schubert   *name = xstrdup (*name);
13375796c8dcSSimon Schubert   xfree (exp);
13385796c8dcSSimon Schubert 
13395796c8dcSSimon Schubert   return value_type (val);
13405796c8dcSSimon Schubert }
13415796c8dcSSimon Schubert 
1342c50c785cSJohn Marino /* A post-parser that does nothing.  */
13435796c8dcSSimon Schubert 
13445796c8dcSSimon Schubert void
null_post_parser(struct expression ** exp,int void_context_p)13455796c8dcSSimon Schubert null_post_parser (struct expression **exp, int void_context_p)
13465796c8dcSSimon Schubert {
13475796c8dcSSimon Schubert }
1348c50c785cSJohn Marino 
1349c50c785cSJohn Marino /* Parse floating point value P of length LEN.
1350c50c785cSJohn Marino    Return 0 (false) if invalid, 1 (true) if valid.
1351c50c785cSJohn Marino    The successfully parsed number is stored in D.
1352c50c785cSJohn Marino    *SUFFIX points to the suffix of the number in P.
1353c50c785cSJohn Marino 
1354c50c785cSJohn Marino    NOTE: This accepts the floating point syntax that sscanf accepts.  */
1355c50c785cSJohn Marino 
1356c50c785cSJohn Marino int
parse_float(const char * p,int len,DOUBLEST * d,const char ** suffix)1357c50c785cSJohn Marino parse_float (const char *p, int len, DOUBLEST *d, const char **suffix)
1358c50c785cSJohn Marino {
1359c50c785cSJohn Marino   char *copy;
1360c50c785cSJohn Marino   int n, num;
1361c50c785cSJohn Marino 
1362c50c785cSJohn Marino   copy = xmalloc (len + 1);
1363c50c785cSJohn Marino   memcpy (copy, p, len);
1364c50c785cSJohn Marino   copy[len] = 0;
1365c50c785cSJohn Marino 
1366c50c785cSJohn Marino   num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
1367c50c785cSJohn Marino   xfree (copy);
1368c50c785cSJohn Marino 
1369c50c785cSJohn Marino   /* The sscanf man page suggests not making any assumptions on the effect
1370c50c785cSJohn Marino      of %n on the result, so we don't.
1371c50c785cSJohn Marino      That is why we simply test num == 0.  */
1372c50c785cSJohn Marino   if (num == 0)
1373c50c785cSJohn Marino     return 0;
1374c50c785cSJohn Marino 
1375c50c785cSJohn Marino   *suffix = p + n;
1376c50c785cSJohn Marino   return 1;
1377c50c785cSJohn Marino }
1378c50c785cSJohn Marino 
1379c50c785cSJohn Marino /* Parse floating point value P of length LEN, using the C syntax for floats.
1380c50c785cSJohn Marino    Return 0 (false) if invalid, 1 (true) if valid.
1381c50c785cSJohn Marino    The successfully parsed number is stored in *D.
1382c50c785cSJohn Marino    Its type is taken from builtin_type (gdbarch) and is stored in *T.  */
1383c50c785cSJohn Marino 
1384c50c785cSJohn Marino int
parse_c_float(struct gdbarch * gdbarch,const char * p,int len,DOUBLEST * d,struct type ** t)1385c50c785cSJohn Marino parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
1386c50c785cSJohn Marino 	       DOUBLEST *d, struct type **t)
1387c50c785cSJohn Marino {
1388c50c785cSJohn Marino   const char *suffix;
1389c50c785cSJohn Marino   int suffix_len;
1390c50c785cSJohn Marino   const struct builtin_type *builtin_types = builtin_type (gdbarch);
1391c50c785cSJohn Marino 
1392c50c785cSJohn Marino   if (! parse_float (p, len, d, &suffix))
1393c50c785cSJohn Marino     return 0;
1394c50c785cSJohn Marino 
1395c50c785cSJohn Marino   suffix_len = p + len - suffix;
1396c50c785cSJohn Marino 
1397c50c785cSJohn Marino   if (suffix_len == 0)
1398c50c785cSJohn Marino     *t = builtin_types->builtin_double;
1399c50c785cSJohn Marino   else if (suffix_len == 1)
1400c50c785cSJohn Marino     {
1401c50c785cSJohn Marino       /* Handle suffixes: 'f' for float, 'l' for long double.  */
1402c50c785cSJohn Marino       if (tolower (*suffix) == 'f')
1403c50c785cSJohn Marino 	*t = builtin_types->builtin_float;
1404c50c785cSJohn Marino       else if (tolower (*suffix) == 'l')
1405c50c785cSJohn Marino 	*t = builtin_types->builtin_long_double;
1406c50c785cSJohn Marino       else
1407c50c785cSJohn Marino 	return 0;
1408c50c785cSJohn Marino     }
1409c50c785cSJohn Marino   else
1410c50c785cSJohn Marino     return 0;
1411c50c785cSJohn Marino 
1412c50c785cSJohn Marino   return 1;
1413c50c785cSJohn Marino }
14145796c8dcSSimon Schubert 
14155796c8dcSSimon Schubert /* Stuff for maintaining a stack of types.  Currently just used by C, but
14165796c8dcSSimon Schubert    probably useful for any language which declares its types "backwards".  */
14175796c8dcSSimon Schubert 
1418*ef5ccd6cSJohn Marino /* Ensure that there are HOWMUCH open slots on the type stack STACK.  */
1419*ef5ccd6cSJohn Marino 
1420*ef5ccd6cSJohn Marino static void
type_stack_reserve(struct type_stack * stack,int howmuch)1421*ef5ccd6cSJohn Marino type_stack_reserve (struct type_stack *stack, int howmuch)
1422*ef5ccd6cSJohn Marino {
1423*ef5ccd6cSJohn Marino   if (stack->depth + howmuch >= stack->size)
1424*ef5ccd6cSJohn Marino     {
1425*ef5ccd6cSJohn Marino       stack->size *= 2;
1426*ef5ccd6cSJohn Marino       if (stack->size < howmuch)
1427*ef5ccd6cSJohn Marino 	stack->size = howmuch;
1428*ef5ccd6cSJohn Marino       stack->elements = xrealloc (stack->elements,
1429*ef5ccd6cSJohn Marino 				  stack->size * sizeof (union type_stack_elt));
1430*ef5ccd6cSJohn Marino     }
1431*ef5ccd6cSJohn Marino }
1432*ef5ccd6cSJohn Marino 
1433*ef5ccd6cSJohn Marino /* Ensure that there is a single open slot in the global type stack.  */
1434*ef5ccd6cSJohn Marino 
14355796c8dcSSimon Schubert static void
check_type_stack_depth(void)14365796c8dcSSimon Schubert check_type_stack_depth (void)
14375796c8dcSSimon Schubert {
1438*ef5ccd6cSJohn Marino   type_stack_reserve (&type_stack, 1);
14395796c8dcSSimon Schubert }
1440*ef5ccd6cSJohn Marino 
1441*ef5ccd6cSJohn Marino /* A helper function for insert_type and insert_type_address_space.
1442*ef5ccd6cSJohn Marino    This does work of expanding the type stack and inserting the new
1443*ef5ccd6cSJohn Marino    element, ELEMENT, into the stack at location SLOT.  */
1444*ef5ccd6cSJohn Marino 
1445*ef5ccd6cSJohn Marino static void
insert_into_type_stack(int slot,union type_stack_elt element)1446*ef5ccd6cSJohn Marino insert_into_type_stack (int slot, union type_stack_elt element)
1447*ef5ccd6cSJohn Marino {
1448*ef5ccd6cSJohn Marino   check_type_stack_depth ();
1449*ef5ccd6cSJohn Marino 
1450*ef5ccd6cSJohn Marino   if (slot < type_stack.depth)
1451*ef5ccd6cSJohn Marino     memmove (&type_stack.elements[slot + 1], &type_stack.elements[slot],
1452*ef5ccd6cSJohn Marino 	     (type_stack.depth - slot) * sizeof (union type_stack_elt));
1453*ef5ccd6cSJohn Marino   type_stack.elements[slot] = element;
1454*ef5ccd6cSJohn Marino   ++type_stack.depth;
1455*ef5ccd6cSJohn Marino }
1456*ef5ccd6cSJohn Marino 
1457*ef5ccd6cSJohn Marino /* Insert a new type, TP, at the bottom of the type stack.  If TP is
1458*ef5ccd6cSJohn Marino    tp_pointer or tp_reference, it is inserted at the bottom.  If TP is
1459*ef5ccd6cSJohn Marino    a qualifier, it is inserted at slot 1 (just above a previous
1460*ef5ccd6cSJohn Marino    tp_pointer) if there is anything on the stack, or simply pushed if
1461*ef5ccd6cSJohn Marino    the stack is empty.  Other values for TP are invalid.  */
1462*ef5ccd6cSJohn Marino 
1463*ef5ccd6cSJohn Marino void
insert_type(enum type_pieces tp)1464*ef5ccd6cSJohn Marino insert_type (enum type_pieces tp)
1465*ef5ccd6cSJohn Marino {
1466*ef5ccd6cSJohn Marino   union type_stack_elt element;
1467*ef5ccd6cSJohn Marino   int slot;
1468*ef5ccd6cSJohn Marino 
1469*ef5ccd6cSJohn Marino   gdb_assert (tp == tp_pointer || tp == tp_reference
1470*ef5ccd6cSJohn Marino 	      || tp == tp_const || tp == tp_volatile);
1471*ef5ccd6cSJohn Marino 
1472*ef5ccd6cSJohn Marino   /* If there is anything on the stack (we know it will be a
1473*ef5ccd6cSJohn Marino      tp_pointer), insert the qualifier above it.  Otherwise, simply
1474*ef5ccd6cSJohn Marino      push this on the top of the stack.  */
1475*ef5ccd6cSJohn Marino   if (type_stack.depth && (tp == tp_const || tp == tp_volatile))
1476*ef5ccd6cSJohn Marino     slot = 1;
1477*ef5ccd6cSJohn Marino   else
1478*ef5ccd6cSJohn Marino     slot = 0;
1479*ef5ccd6cSJohn Marino 
1480*ef5ccd6cSJohn Marino   element.piece = tp;
1481*ef5ccd6cSJohn Marino   insert_into_type_stack (slot, element);
14825796c8dcSSimon Schubert }
14835796c8dcSSimon Schubert 
14845796c8dcSSimon Schubert void
push_type(enum type_pieces tp)14855796c8dcSSimon Schubert push_type (enum type_pieces tp)
14865796c8dcSSimon Schubert {
14875796c8dcSSimon Schubert   check_type_stack_depth ();
1488*ef5ccd6cSJohn Marino   type_stack.elements[type_stack.depth++].piece = tp;
14895796c8dcSSimon Schubert }
14905796c8dcSSimon Schubert 
14915796c8dcSSimon Schubert void
push_type_int(int n)14925796c8dcSSimon Schubert push_type_int (int n)
14935796c8dcSSimon Schubert {
14945796c8dcSSimon Schubert   check_type_stack_depth ();
1495*ef5ccd6cSJohn Marino   type_stack.elements[type_stack.depth++].int_val = n;
14965796c8dcSSimon Schubert }
14975796c8dcSSimon Schubert 
1498*ef5ccd6cSJohn Marino /* Insert a tp_space_identifier and the corresponding address space
1499*ef5ccd6cSJohn Marino    value into the stack.  STRING is the name of an address space, as
1500*ef5ccd6cSJohn Marino    recognized by address_space_name_to_int.  If the stack is empty,
1501*ef5ccd6cSJohn Marino    the new elements are simply pushed.  If the stack is not empty,
1502*ef5ccd6cSJohn Marino    this function assumes that the first item on the stack is a
1503*ef5ccd6cSJohn Marino    tp_pointer, and the new values are inserted above the first
1504*ef5ccd6cSJohn Marino    item.  */
1505*ef5ccd6cSJohn Marino 
15065796c8dcSSimon Schubert void
insert_type_address_space(char * string)1507*ef5ccd6cSJohn Marino insert_type_address_space (char *string)
15085796c8dcSSimon Schubert {
1509*ef5ccd6cSJohn Marino   union type_stack_elt element;
1510*ef5ccd6cSJohn Marino   int slot;
1511*ef5ccd6cSJohn Marino 
1512*ef5ccd6cSJohn Marino   /* If there is anything on the stack (we know it will be a
1513*ef5ccd6cSJohn Marino      tp_pointer), insert the address space qualifier above it.
1514*ef5ccd6cSJohn Marino      Otherwise, simply push this on the top of the stack.  */
1515*ef5ccd6cSJohn Marino   if (type_stack.depth)
1516*ef5ccd6cSJohn Marino     slot = 1;
1517*ef5ccd6cSJohn Marino   else
1518*ef5ccd6cSJohn Marino     slot = 0;
1519*ef5ccd6cSJohn Marino 
1520*ef5ccd6cSJohn Marino   element.piece = tp_space_identifier;
1521*ef5ccd6cSJohn Marino   insert_into_type_stack (slot, element);
1522*ef5ccd6cSJohn Marino   element.int_val = address_space_name_to_int (parse_gdbarch, string);
1523*ef5ccd6cSJohn Marino   insert_into_type_stack (slot, element);
15245796c8dcSSimon Schubert }
15255796c8dcSSimon Schubert 
15265796c8dcSSimon Schubert enum type_pieces
pop_type(void)15275796c8dcSSimon Schubert pop_type (void)
15285796c8dcSSimon Schubert {
1529*ef5ccd6cSJohn Marino   if (type_stack.depth)
1530*ef5ccd6cSJohn Marino     return type_stack.elements[--type_stack.depth].piece;
15315796c8dcSSimon Schubert   return tp_end;
15325796c8dcSSimon Schubert }
15335796c8dcSSimon Schubert 
15345796c8dcSSimon Schubert int
pop_type_int(void)15355796c8dcSSimon Schubert pop_type_int (void)
15365796c8dcSSimon Schubert {
1537*ef5ccd6cSJohn Marino   if (type_stack.depth)
1538*ef5ccd6cSJohn Marino     return type_stack.elements[--type_stack.depth].int_val;
15395796c8dcSSimon Schubert   /* "Can't happen".  */
15405796c8dcSSimon Schubert   return 0;
15415796c8dcSSimon Schubert }
15425796c8dcSSimon Schubert 
1543*ef5ccd6cSJohn Marino /* Pop a type list element from the global type stack.  */
1544*ef5ccd6cSJohn Marino 
VEC(type_ptr)1545*ef5ccd6cSJohn Marino static VEC (type_ptr) *
1546*ef5ccd6cSJohn Marino pop_typelist (void)
1547*ef5ccd6cSJohn Marino {
1548*ef5ccd6cSJohn Marino   gdb_assert (type_stack.depth);
1549*ef5ccd6cSJohn Marino   return type_stack.elements[--type_stack.depth].typelist_val;
1550*ef5ccd6cSJohn Marino }
1551*ef5ccd6cSJohn Marino 
1552*ef5ccd6cSJohn Marino /* Pop a type_stack element from the global type stack.  */
1553*ef5ccd6cSJohn Marino 
1554*ef5ccd6cSJohn Marino static struct type_stack *
pop_type_stack(void)1555*ef5ccd6cSJohn Marino pop_type_stack (void)
1556*ef5ccd6cSJohn Marino {
1557*ef5ccd6cSJohn Marino   gdb_assert (type_stack.depth);
1558*ef5ccd6cSJohn Marino   return type_stack.elements[--type_stack.depth].stack_val;
1559*ef5ccd6cSJohn Marino }
1560*ef5ccd6cSJohn Marino 
1561*ef5ccd6cSJohn Marino /* Append the elements of the type stack FROM to the type stack TO.
1562*ef5ccd6cSJohn Marino    Always returns TO.  */
1563*ef5ccd6cSJohn Marino 
1564*ef5ccd6cSJohn Marino struct type_stack *
append_type_stack(struct type_stack * to,struct type_stack * from)1565*ef5ccd6cSJohn Marino append_type_stack (struct type_stack *to, struct type_stack *from)
1566*ef5ccd6cSJohn Marino {
1567*ef5ccd6cSJohn Marino   type_stack_reserve (to, from->depth);
1568*ef5ccd6cSJohn Marino 
1569*ef5ccd6cSJohn Marino   memcpy (&to->elements[to->depth], &from->elements[0],
1570*ef5ccd6cSJohn Marino 	  from->depth * sizeof (union type_stack_elt));
1571*ef5ccd6cSJohn Marino   to->depth += from->depth;
1572*ef5ccd6cSJohn Marino 
1573*ef5ccd6cSJohn Marino   return to;
1574*ef5ccd6cSJohn Marino }
1575*ef5ccd6cSJohn Marino 
1576*ef5ccd6cSJohn Marino /* Push the type stack STACK as an element on the global type stack.  */
1577*ef5ccd6cSJohn Marino 
1578*ef5ccd6cSJohn Marino void
push_type_stack(struct type_stack * stack)1579*ef5ccd6cSJohn Marino push_type_stack (struct type_stack *stack)
1580*ef5ccd6cSJohn Marino {
1581*ef5ccd6cSJohn Marino   check_type_stack_depth ();
1582*ef5ccd6cSJohn Marino   type_stack.elements[type_stack.depth++].stack_val = stack;
1583*ef5ccd6cSJohn Marino   push_type (tp_type_stack);
1584*ef5ccd6cSJohn Marino }
1585*ef5ccd6cSJohn Marino 
1586*ef5ccd6cSJohn Marino /* Copy the global type stack into a newly allocated type stack and
1587*ef5ccd6cSJohn Marino    return it.  The global stack is cleared.  The returned type stack
1588*ef5ccd6cSJohn Marino    must be freed with type_stack_cleanup.  */
1589*ef5ccd6cSJohn Marino 
1590*ef5ccd6cSJohn Marino struct type_stack *
get_type_stack(void)1591*ef5ccd6cSJohn Marino get_type_stack (void)
1592*ef5ccd6cSJohn Marino {
1593*ef5ccd6cSJohn Marino   struct type_stack *result = XNEW (struct type_stack);
1594*ef5ccd6cSJohn Marino 
1595*ef5ccd6cSJohn Marino   *result = type_stack;
1596*ef5ccd6cSJohn Marino   type_stack.depth = 0;
1597*ef5ccd6cSJohn Marino   type_stack.size = 0;
1598*ef5ccd6cSJohn Marino   type_stack.elements = NULL;
1599*ef5ccd6cSJohn Marino 
1600*ef5ccd6cSJohn Marino   return result;
1601*ef5ccd6cSJohn Marino }
1602*ef5ccd6cSJohn Marino 
1603*ef5ccd6cSJohn Marino /* A cleanup function that destroys a single type stack.  */
1604*ef5ccd6cSJohn Marino 
1605*ef5ccd6cSJohn Marino void
type_stack_cleanup(void * arg)1606*ef5ccd6cSJohn Marino type_stack_cleanup (void *arg)
1607*ef5ccd6cSJohn Marino {
1608*ef5ccd6cSJohn Marino   struct type_stack *stack = arg;
1609*ef5ccd6cSJohn Marino 
1610*ef5ccd6cSJohn Marino   xfree (stack->elements);
1611*ef5ccd6cSJohn Marino   xfree (stack);
1612*ef5ccd6cSJohn Marino }
1613*ef5ccd6cSJohn Marino 
1614*ef5ccd6cSJohn Marino /* Push a function type with arguments onto the global type stack.
1615*ef5ccd6cSJohn Marino    LIST holds the argument types.  If the final item in LIST is NULL,
1616*ef5ccd6cSJohn Marino    then the function will be varargs.  */
1617*ef5ccd6cSJohn Marino 
1618*ef5ccd6cSJohn Marino void
push_typelist(VEC (type_ptr)* list)1619*ef5ccd6cSJohn Marino push_typelist (VEC (type_ptr) *list)
1620*ef5ccd6cSJohn Marino {
1621*ef5ccd6cSJohn Marino   check_type_stack_depth ();
1622*ef5ccd6cSJohn Marino   type_stack.elements[type_stack.depth++].typelist_val = list;
1623*ef5ccd6cSJohn Marino   push_type (tp_function_with_arguments);
1624*ef5ccd6cSJohn Marino }
1625*ef5ccd6cSJohn Marino 
16265796c8dcSSimon Schubert /* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
16275796c8dcSSimon Schubert    as modified by all the stuff on the stack.  */
16285796c8dcSSimon Schubert struct type *
follow_types(struct type * follow_type)16295796c8dcSSimon Schubert follow_types (struct type *follow_type)
16305796c8dcSSimon Schubert {
16315796c8dcSSimon Schubert   int done = 0;
16325796c8dcSSimon Schubert   int make_const = 0;
16335796c8dcSSimon Schubert   int make_volatile = 0;
16345796c8dcSSimon Schubert   int make_addr_space = 0;
16355796c8dcSSimon Schubert   int array_size;
16365796c8dcSSimon Schubert 
16375796c8dcSSimon Schubert   while (!done)
16385796c8dcSSimon Schubert     switch (pop_type ())
16395796c8dcSSimon Schubert       {
16405796c8dcSSimon Schubert       case tp_end:
16415796c8dcSSimon Schubert 	done = 1;
16425796c8dcSSimon Schubert 	if (make_const)
16435796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
16445796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
16455796c8dcSSimon Schubert 				      follow_type, 0);
16465796c8dcSSimon Schubert 	if (make_volatile)
16475796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
16485796c8dcSSimon Schubert 				      make_volatile,
16495796c8dcSSimon Schubert 				      follow_type, 0);
16505796c8dcSSimon Schubert 	if (make_addr_space)
16515796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
16525796c8dcSSimon Schubert 						      make_addr_space);
16535796c8dcSSimon Schubert 	make_const = make_volatile = 0;
16545796c8dcSSimon Schubert 	make_addr_space = 0;
16555796c8dcSSimon Schubert 	break;
16565796c8dcSSimon Schubert       case tp_const:
16575796c8dcSSimon Schubert 	make_const = 1;
16585796c8dcSSimon Schubert 	break;
16595796c8dcSSimon Schubert       case tp_volatile:
16605796c8dcSSimon Schubert 	make_volatile = 1;
16615796c8dcSSimon Schubert 	break;
16625796c8dcSSimon Schubert       case tp_space_identifier:
16635796c8dcSSimon Schubert 	make_addr_space = pop_type_int ();
16645796c8dcSSimon Schubert 	break;
16655796c8dcSSimon Schubert       case tp_pointer:
16665796c8dcSSimon Schubert 	follow_type = lookup_pointer_type (follow_type);
16675796c8dcSSimon Schubert 	if (make_const)
16685796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
16695796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
16705796c8dcSSimon Schubert 				      follow_type, 0);
16715796c8dcSSimon Schubert 	if (make_volatile)
16725796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
16735796c8dcSSimon Schubert 				      make_volatile,
16745796c8dcSSimon Schubert 				      follow_type, 0);
16755796c8dcSSimon Schubert 	if (make_addr_space)
16765796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
16775796c8dcSSimon Schubert 						      make_addr_space);
16785796c8dcSSimon Schubert 	make_const = make_volatile = 0;
16795796c8dcSSimon Schubert 	make_addr_space = 0;
16805796c8dcSSimon Schubert 	break;
16815796c8dcSSimon Schubert       case tp_reference:
16825796c8dcSSimon Schubert 	follow_type = lookup_reference_type (follow_type);
16835796c8dcSSimon Schubert 	if (make_const)
16845796c8dcSSimon Schubert 	  follow_type = make_cv_type (make_const,
16855796c8dcSSimon Schubert 				      TYPE_VOLATILE (follow_type),
16865796c8dcSSimon Schubert 				      follow_type, 0);
16875796c8dcSSimon Schubert 	if (make_volatile)
16885796c8dcSSimon Schubert 	  follow_type = make_cv_type (TYPE_CONST (follow_type),
16895796c8dcSSimon Schubert 				      make_volatile,
16905796c8dcSSimon Schubert 				      follow_type, 0);
16915796c8dcSSimon Schubert 	if (make_addr_space)
16925796c8dcSSimon Schubert 	  follow_type = make_type_with_address_space (follow_type,
16935796c8dcSSimon Schubert 						      make_addr_space);
16945796c8dcSSimon Schubert 	make_const = make_volatile = 0;
16955796c8dcSSimon Schubert 	make_addr_space = 0;
16965796c8dcSSimon Schubert 	break;
16975796c8dcSSimon Schubert       case tp_array:
16985796c8dcSSimon Schubert 	array_size = pop_type_int ();
16995796c8dcSSimon Schubert 	/* FIXME-type-allocation: need a way to free this type when we are
17005796c8dcSSimon Schubert 	   done with it.  */
17015796c8dcSSimon Schubert 	follow_type =
17025796c8dcSSimon Schubert 	  lookup_array_range_type (follow_type,
17035796c8dcSSimon Schubert 				   0, array_size >= 0 ? array_size - 1 : 0);
17045796c8dcSSimon Schubert 	if (array_size < 0)
17055796c8dcSSimon Schubert 	  TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (follow_type) = 1;
17065796c8dcSSimon Schubert 	break;
17075796c8dcSSimon Schubert       case tp_function:
17085796c8dcSSimon Schubert 	/* FIXME-type-allocation: need a way to free this type when we are
17095796c8dcSSimon Schubert 	   done with it.  */
17105796c8dcSSimon Schubert 	follow_type = lookup_function_type (follow_type);
17115796c8dcSSimon Schubert 	break;
1712*ef5ccd6cSJohn Marino 
1713*ef5ccd6cSJohn Marino       case tp_function_with_arguments:
1714*ef5ccd6cSJohn Marino 	{
1715*ef5ccd6cSJohn Marino 	  VEC (type_ptr) *args = pop_typelist ();
1716*ef5ccd6cSJohn Marino 
1717*ef5ccd6cSJohn Marino 	  follow_type
1718*ef5ccd6cSJohn Marino 	    = lookup_function_type_with_arguments (follow_type,
1719*ef5ccd6cSJohn Marino 						   VEC_length (type_ptr, args),
1720*ef5ccd6cSJohn Marino 						   VEC_address (type_ptr,
1721*ef5ccd6cSJohn Marino 								args));
1722*ef5ccd6cSJohn Marino 	  VEC_free (type_ptr, args);
1723*ef5ccd6cSJohn Marino 	}
1724*ef5ccd6cSJohn Marino 	break;
1725*ef5ccd6cSJohn Marino 
1726*ef5ccd6cSJohn Marino       case tp_type_stack:
1727*ef5ccd6cSJohn Marino 	{
1728*ef5ccd6cSJohn Marino 	  struct type_stack *stack = pop_type_stack ();
1729*ef5ccd6cSJohn Marino 	  /* Sort of ugly, but not really much worse than the
1730*ef5ccd6cSJohn Marino 	     alternatives.  */
1731*ef5ccd6cSJohn Marino 	  struct type_stack save = type_stack;
1732*ef5ccd6cSJohn Marino 
1733*ef5ccd6cSJohn Marino 	  type_stack = *stack;
1734*ef5ccd6cSJohn Marino 	  follow_type = follow_types (follow_type);
1735*ef5ccd6cSJohn Marino 	  gdb_assert (type_stack.depth == 0);
1736*ef5ccd6cSJohn Marino 
1737*ef5ccd6cSJohn Marino 	  type_stack = save;
1738*ef5ccd6cSJohn Marino 	}
1739*ef5ccd6cSJohn Marino 	break;
1740*ef5ccd6cSJohn Marino       default:
1741*ef5ccd6cSJohn Marino 	gdb_assert_not_reached ("unrecognized tp_ value in follow_types");
17425796c8dcSSimon Schubert       }
17435796c8dcSSimon Schubert   return follow_type;
17445796c8dcSSimon Schubert }
17455796c8dcSSimon Schubert 
17465796c8dcSSimon Schubert /* This function avoids direct calls to fprintf
17475796c8dcSSimon Schubert    in the parser generated debug code.  */
17485796c8dcSSimon Schubert void
parser_fprintf(FILE * x,const char * y,...)17495796c8dcSSimon Schubert parser_fprintf (FILE *x, const char *y, ...)
17505796c8dcSSimon Schubert {
17515796c8dcSSimon Schubert   va_list args;
1752cf7f2e2dSJohn Marino 
17535796c8dcSSimon Schubert   va_start (args, y);
17545796c8dcSSimon Schubert   if (x == stderr)
17555796c8dcSSimon Schubert     vfprintf_unfiltered (gdb_stderr, y, args);
17565796c8dcSSimon Schubert   else
17575796c8dcSSimon Schubert     {
17585796c8dcSSimon Schubert       fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
17595796c8dcSSimon Schubert       vfprintf_unfiltered (gdb_stderr, y, args);
17605796c8dcSSimon Schubert     }
17615796c8dcSSimon Schubert   va_end (args);
17625796c8dcSSimon Schubert }
17635796c8dcSSimon Schubert 
1764cf7f2e2dSJohn Marino /* Implementation of the exp_descriptor method operator_check.  */
1765cf7f2e2dSJohn Marino 
1766cf7f2e2dSJohn Marino int
operator_check_standard(struct expression * exp,int pos,int (* objfile_func)(struct objfile * objfile,void * data),void * data)1767cf7f2e2dSJohn Marino operator_check_standard (struct expression *exp, int pos,
1768cf7f2e2dSJohn Marino 			 int (*objfile_func) (struct objfile *objfile,
1769cf7f2e2dSJohn Marino 					      void *data),
1770cf7f2e2dSJohn Marino 			 void *data)
1771cf7f2e2dSJohn Marino {
1772cf7f2e2dSJohn Marino   const union exp_element *const elts = exp->elts;
1773cf7f2e2dSJohn Marino   struct type *type = NULL;
1774cf7f2e2dSJohn Marino   struct objfile *objfile = NULL;
1775cf7f2e2dSJohn Marino 
1776cf7f2e2dSJohn Marino   /* Extended operators should have been already handled by exp_descriptor
1777cf7f2e2dSJohn Marino      iterate method of its specific language.  */
1778cf7f2e2dSJohn Marino   gdb_assert (elts[pos].opcode < OP_EXTENDED0);
1779cf7f2e2dSJohn Marino 
1780cf7f2e2dSJohn Marino   /* Track the callers of write_exp_elt_type for this table.  */
1781cf7f2e2dSJohn Marino 
1782cf7f2e2dSJohn Marino   switch (elts[pos].opcode)
1783cf7f2e2dSJohn Marino     {
1784cf7f2e2dSJohn Marino     case BINOP_VAL:
1785cf7f2e2dSJohn Marino     case OP_COMPLEX:
1786cf7f2e2dSJohn Marino     case OP_DECFLOAT:
1787cf7f2e2dSJohn Marino     case OP_DOUBLE:
1788cf7f2e2dSJohn Marino     case OP_LONG:
1789cf7f2e2dSJohn Marino     case OP_SCOPE:
1790cf7f2e2dSJohn Marino     case OP_TYPE:
1791cf7f2e2dSJohn Marino     case UNOP_CAST:
1792cf7f2e2dSJohn Marino     case UNOP_MAX:
1793cf7f2e2dSJohn Marino     case UNOP_MEMVAL:
1794cf7f2e2dSJohn Marino     case UNOP_MIN:
1795cf7f2e2dSJohn Marino       type = elts[pos + 1].type;
1796cf7f2e2dSJohn Marino       break;
1797cf7f2e2dSJohn Marino 
1798cf7f2e2dSJohn Marino     case TYPE_INSTANCE:
1799cf7f2e2dSJohn Marino       {
1800cf7f2e2dSJohn Marino 	LONGEST arg, nargs = elts[pos + 1].longconst;
1801cf7f2e2dSJohn Marino 
1802cf7f2e2dSJohn Marino 	for (arg = 0; arg < nargs; arg++)
1803cf7f2e2dSJohn Marino 	  {
1804cf7f2e2dSJohn Marino 	    struct type *type = elts[pos + 2 + arg].type;
1805cf7f2e2dSJohn Marino 	    struct objfile *objfile = TYPE_OBJFILE (type);
1806cf7f2e2dSJohn Marino 
1807cf7f2e2dSJohn Marino 	    if (objfile && (*objfile_func) (objfile, data))
1808cf7f2e2dSJohn Marino 	      return 1;
1809cf7f2e2dSJohn Marino 	  }
1810cf7f2e2dSJohn Marino       }
1811cf7f2e2dSJohn Marino       break;
1812cf7f2e2dSJohn Marino 
1813cf7f2e2dSJohn Marino     case UNOP_MEMVAL_TLS:
1814cf7f2e2dSJohn Marino       objfile = elts[pos + 1].objfile;
1815cf7f2e2dSJohn Marino       type = elts[pos + 2].type;
1816cf7f2e2dSJohn Marino       break;
1817cf7f2e2dSJohn Marino 
1818cf7f2e2dSJohn Marino     case OP_VAR_VALUE:
1819cf7f2e2dSJohn Marino       {
1820cf7f2e2dSJohn Marino 	const struct block *const block = elts[pos + 1].block;
1821cf7f2e2dSJohn Marino 	const struct symbol *const symbol = elts[pos + 2].symbol;
1822cf7f2e2dSJohn Marino 
1823cf7f2e2dSJohn Marino 	/* Check objfile where the variable itself is placed.
1824cf7f2e2dSJohn Marino 	   SYMBOL_OBJ_SECTION (symbol) may be NULL.  */
1825cf7f2e2dSJohn Marino 	if ((*objfile_func) (SYMBOL_SYMTAB (symbol)->objfile, data))
1826cf7f2e2dSJohn Marino 	  return 1;
1827cf7f2e2dSJohn Marino 
1828cf7f2e2dSJohn Marino 	/* Check objfile where is placed the code touching the variable.  */
1829cf7f2e2dSJohn Marino 	objfile = lookup_objfile_from_block (block);
1830cf7f2e2dSJohn Marino 
1831cf7f2e2dSJohn Marino 	type = SYMBOL_TYPE (symbol);
1832cf7f2e2dSJohn Marino       }
1833cf7f2e2dSJohn Marino       break;
1834cf7f2e2dSJohn Marino     }
1835cf7f2e2dSJohn Marino 
1836cf7f2e2dSJohn Marino   /* Invoke callbacks for TYPE and OBJFILE if they were set as non-NULL.  */
1837cf7f2e2dSJohn Marino 
1838cf7f2e2dSJohn Marino   if (type && TYPE_OBJFILE (type)
1839cf7f2e2dSJohn Marino       && (*objfile_func) (TYPE_OBJFILE (type), data))
1840cf7f2e2dSJohn Marino     return 1;
1841cf7f2e2dSJohn Marino   if (objfile && (*objfile_func) (objfile, data))
1842cf7f2e2dSJohn Marino     return 1;
1843cf7f2e2dSJohn Marino 
1844cf7f2e2dSJohn Marino   return 0;
1845cf7f2e2dSJohn Marino }
1846cf7f2e2dSJohn Marino 
1847cf7f2e2dSJohn Marino /* Call OBJFILE_FUNC for any TYPE and OBJFILE found being referenced by EXP.
1848cf7f2e2dSJohn Marino    The functions are never called with NULL OBJFILE.  Functions get passed an
1849cf7f2e2dSJohn Marino    arbitrary caller supplied DATA pointer.  If any of the functions returns
1850cf7f2e2dSJohn Marino    non-zero value then (any other) non-zero value is immediately returned to
1851cf7f2e2dSJohn Marino    the caller.  Otherwise zero is returned after iterating through whole EXP.
1852cf7f2e2dSJohn Marino    */
1853cf7f2e2dSJohn Marino 
1854cf7f2e2dSJohn Marino static int
exp_iterate(struct expression * exp,int (* objfile_func)(struct objfile * objfile,void * data),void * data)1855cf7f2e2dSJohn Marino exp_iterate (struct expression *exp,
1856cf7f2e2dSJohn Marino 	     int (*objfile_func) (struct objfile *objfile, void *data),
1857cf7f2e2dSJohn Marino 	     void *data)
1858cf7f2e2dSJohn Marino {
1859cf7f2e2dSJohn Marino   int endpos;
1860cf7f2e2dSJohn Marino 
1861cf7f2e2dSJohn Marino   for (endpos = exp->nelts; endpos > 0; )
1862cf7f2e2dSJohn Marino     {
1863cf7f2e2dSJohn Marino       int pos, args, oplen = 0;
1864cf7f2e2dSJohn Marino 
1865cf7f2e2dSJohn Marino       operator_length (exp, endpos, &oplen, &args);
1866cf7f2e2dSJohn Marino       gdb_assert (oplen > 0);
1867cf7f2e2dSJohn Marino 
1868cf7f2e2dSJohn Marino       pos = endpos - oplen;
1869cf7f2e2dSJohn Marino       if (exp->language_defn->la_exp_desc->operator_check (exp, pos,
1870cf7f2e2dSJohn Marino 							   objfile_func, data))
1871cf7f2e2dSJohn Marino 	return 1;
1872cf7f2e2dSJohn Marino 
1873cf7f2e2dSJohn Marino       endpos = pos;
1874cf7f2e2dSJohn Marino     }
1875cf7f2e2dSJohn Marino 
1876cf7f2e2dSJohn Marino   return 0;
1877cf7f2e2dSJohn Marino }
1878cf7f2e2dSJohn Marino 
1879cf7f2e2dSJohn Marino /* Helper for exp_uses_objfile.  */
1880cf7f2e2dSJohn Marino 
1881cf7f2e2dSJohn Marino static int
exp_uses_objfile_iter(struct objfile * exp_objfile,void * objfile_voidp)1882cf7f2e2dSJohn Marino exp_uses_objfile_iter (struct objfile *exp_objfile, void *objfile_voidp)
1883cf7f2e2dSJohn Marino {
1884cf7f2e2dSJohn Marino   struct objfile *objfile = objfile_voidp;
1885cf7f2e2dSJohn Marino 
1886cf7f2e2dSJohn Marino   if (exp_objfile->separate_debug_objfile_backlink)
1887cf7f2e2dSJohn Marino     exp_objfile = exp_objfile->separate_debug_objfile_backlink;
1888cf7f2e2dSJohn Marino 
1889cf7f2e2dSJohn Marino   return exp_objfile == objfile;
1890cf7f2e2dSJohn Marino }
1891cf7f2e2dSJohn Marino 
1892cf7f2e2dSJohn Marino /* Return 1 if EXP uses OBJFILE (and will become dangling when OBJFILE
1893cf7f2e2dSJohn Marino    is unloaded), otherwise return 0.  OBJFILE must not be a separate debug info
1894cf7f2e2dSJohn Marino    file.  */
1895cf7f2e2dSJohn Marino 
1896cf7f2e2dSJohn Marino int
exp_uses_objfile(struct expression * exp,struct objfile * objfile)1897cf7f2e2dSJohn Marino exp_uses_objfile (struct expression *exp, struct objfile *objfile)
1898cf7f2e2dSJohn Marino {
1899cf7f2e2dSJohn Marino   gdb_assert (objfile->separate_debug_objfile_backlink == NULL);
1900cf7f2e2dSJohn Marino 
1901cf7f2e2dSJohn Marino   return exp_iterate (exp, exp_uses_objfile_iter, objfile);
1902cf7f2e2dSJohn Marino }
1903cf7f2e2dSJohn Marino 
19045796c8dcSSimon Schubert void
_initialize_parse(void)19055796c8dcSSimon Schubert _initialize_parse (void)
19065796c8dcSSimon Schubert {
1907*ef5ccd6cSJohn Marino   type_stack.size = 0;
1908*ef5ccd6cSJohn Marino   type_stack.depth = 0;
1909*ef5ccd6cSJohn Marino   type_stack.elements = NULL;
19105796c8dcSSimon Schubert 
1911*ef5ccd6cSJohn Marino   add_setshow_zuinteger_cmd ("expression", class_maintenance,
1912c50c785cSJohn Marino 			     &expressiondebug,
1913c50c785cSJohn Marino 			     _("Set expression debugging."),
1914c50c785cSJohn Marino 			     _("Show expression debugging."),
1915c50c785cSJohn Marino 			     _("When non-zero, the internal representation "
1916c50c785cSJohn Marino 			       "of expressions will be printed."),
19175796c8dcSSimon Schubert 			     NULL,
19185796c8dcSSimon Schubert 			     show_expressiondebug,
19195796c8dcSSimon Schubert 			     &setdebuglist, &showdebuglist);
1920cf7f2e2dSJohn Marino   add_setshow_boolean_cmd ("parser", class_maintenance,
1921c50c785cSJohn Marino 			    &parser_debug,
1922c50c785cSJohn Marino 			   _("Set parser debugging."),
1923c50c785cSJohn Marino 			   _("Show parser debugging."),
1924c50c785cSJohn Marino 			   _("When non-zero, expression parser "
1925c50c785cSJohn Marino 			     "tracing will be enabled."),
1926cf7f2e2dSJohn Marino 			    NULL,
1927cf7f2e2dSJohn Marino 			    show_parserdebug,
1928cf7f2e2dSJohn Marino 			    &setdebuglist, &showdebuglist);
19295796c8dcSSimon Schubert }
1930