119370Spst/* Parse expressions for GDB.
298944Sobrien   Copyright 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
398944Sobrien   1998, 1999, 2000, 2001 Free Software Foundation, Inc.
419370Spst   Modified from expread.y by the Department of Computer Science at the
519370Spst   State University of New York at Buffalo, 1991.
619370Spst
798944Sobrien   This file is part of GDB.
819370Spst
998944Sobrien   This program is free software; you can redistribute it and/or modify
1098944Sobrien   it under the terms of the GNU General Public License as published by
1198944Sobrien   the Free Software Foundation; either version 2 of the License, or
1298944Sobrien   (at your option) any later version.
1319370Spst
1498944Sobrien   This program is distributed in the hope that it will be useful,
1598944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798944Sobrien   GNU General Public License for more details.
1819370Spst
1998944Sobrien   You should have received a copy of the GNU General Public License
2098944Sobrien   along with this program; if not, write to the Free Software
2198944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298944Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2419370Spst/* Parse an expression from text in a string,
2519370Spst   and return the result as a  struct expression  pointer.
2619370Spst   That structure contains arithmetic operations in reverse polish,
2719370Spst   with constants represented by operations that are followed by special data.
2819370Spst   See expression.h for the details of the format.
2919370Spst   What is important here is that it can be built up sequentially
3019370Spst   during the process of parsing; the lower levels of the tree always
3119370Spst   come first in the result.  */
3298944Sobrien
3398944Sobrien#include <ctype.h>
3498944Sobrien
3519370Spst#include "defs.h"
3619370Spst#include "gdb_string.h"
3719370Spst#include "symtab.h"
3819370Spst#include "gdbtypes.h"
3919370Spst#include "frame.h"
4019370Spst#include "expression.h"
4119370Spst#include "value.h"
4219370Spst#include "command.h"
4319370Spst#include "language.h"
4419370Spst#include "parser-defs.h"
4546283Sdfr#include "gdbcmd.h"
4698944Sobrien#include "symfile.h"		/* for overlay functions */
4798944Sobrien#include "inferior.h"		/* for NUM_PSEUDO_REGS.  NOTE: replace
4898944Sobrien				   with "gdbarch.h" when appropriate.  */
4998944Sobrien#include "doublest.h"
50130803Smarcel#include "gdb_assert.h"
51130803Smarcel#include "block.h"
5298944Sobrien
53130803Smarcel/* Standard set of definitions for printing, dumping, prefixifying,
54130803Smarcel * and evaluating expressions.  */
55130803Smarcel
56130803Smarcelconst struct exp_descriptor exp_descriptor_standard =
57130803Smarcel  {
58130803Smarcel    print_subexp_standard,
59130803Smarcel    operator_length_standard,
60130803Smarcel    op_name_standard,
61130803Smarcel    dump_subexp_body_standard,
62130803Smarcel    evaluate_subexp_standard
63130803Smarcel  };
6419370Spst
6598944Sobrien/* Symbols which architectures can redefine.  */
6698944Sobrien
6798944Sobrien/* Some systems have routines whose names start with `$'.  Giving this
6898944Sobrien   macro a non-zero value tells GDB's expression parser to check for
6998944Sobrien   such routines when parsing tokens that begin with `$'.
7098944Sobrien
7198944Sobrien   On HP-UX, certain system routines (millicode) have names beginning
7298944Sobrien   with `$' or `$$'.  For example, `$$dyncall' is a millicode routine
7398944Sobrien   that handles inter-space procedure calls on PA-RISC.  */
7498944Sobrien#ifndef SYMBOLS_CAN_START_WITH_DOLLAR
7598944Sobrien#define SYMBOLS_CAN_START_WITH_DOLLAR (0)
7698944Sobrien#endif
7798944Sobrien
7898944Sobrien
7998944Sobrien
8019370Spst/* Global variables declared in parser-defs.h (and commented there).  */
8119370Spststruct expression *expout;
8219370Spstint expout_size;
8319370Spstint expout_ptr;
8419370Spststruct block *expression_context_block;
85130803SmarcelCORE_ADDR expression_context_pc;
8619370Spststruct block *innermost_block;
8719370Spstint arglist_len;
8819370Spstunion type_stack_elt *type_stack;
8919370Spstint type_stack_depth, type_stack_size;
9019370Spstchar *lexptr;
91130803Smarcelchar *prev_lexptr;
9219370Spstchar *namecopy;
9319370Spstint paren_depth;
9419370Spstint comma_terminates;
9519370Spst
9646283Sdfrstatic int expressiondebug = 0;
9746283Sdfr
9846283Sdfrextern int hp_som_som_object_present;
9946283Sdfr
10098944Sobrienstatic void free_funcalls (void *ignore);
10119370Spst
10298944Sobrienstatic void prefixify_expression (struct expression *);
10319370Spst
104130803Smarcelstatic void prefixify_subexp (struct expression *, struct expression *, int,
105130803Smarcel			      int);
10619370Spst
10798944Sobrienvoid _initialize_parse (void);
10898944Sobrien
10919370Spst/* Data structure for saving values of arglist_len for function calls whose
11019370Spst   arguments contain other function calls.  */
11119370Spst
11219370Spststruct funcall
11319370Spst  {
11419370Spst    struct funcall *next;
11519370Spst    int arglist_len;
11619370Spst  };
11719370Spst
11819370Spststatic struct funcall *funcall_chain;
11919370Spst
12019370Spst/* Begin counting arguments for a function call,
12119370Spst   saving the data about any containing call.  */
12219370Spst
12319370Spstvoid
12498944Sobrienstart_arglist (void)
12519370Spst{
126130803Smarcel  struct funcall *new;
12719370Spst
12819370Spst  new = (struct funcall *) xmalloc (sizeof (struct funcall));
12919370Spst  new->next = funcall_chain;
13019370Spst  new->arglist_len = arglist_len;
13119370Spst  arglist_len = 0;
13219370Spst  funcall_chain = new;
13319370Spst}
13419370Spst
13519370Spst/* Return the number of arguments in a function call just terminated,
13619370Spst   and restore the data for the containing function call.  */
13719370Spst
13819370Spstint
13998944Sobrienend_arglist (void)
14019370Spst{
141130803Smarcel  int val = arglist_len;
142130803Smarcel  struct funcall *call = funcall_chain;
14319370Spst  funcall_chain = call->next;
14419370Spst  arglist_len = call->arglist_len;
14598944Sobrien  xfree (call);
14619370Spst  return val;
14719370Spst}
14819370Spst
14919370Spst/* Free everything in the funcall chain.
15019370Spst   Used when there is an error inside parsing.  */
15119370Spst
15219370Spststatic void
15398944Sobrienfree_funcalls (void *ignore)
15419370Spst{
155130803Smarcel  struct funcall *call, *next;
15619370Spst
15719370Spst  for (call = funcall_chain; call; call = next)
15819370Spst    {
15919370Spst      next = call->next;
16098944Sobrien      xfree (call);
16119370Spst    }
16219370Spst}
16319370Spst
16419370Spst/* This page contains the functions for adding data to the  struct expression
16519370Spst   being constructed.  */
16619370Spst
16719370Spst/* Add one element to the end of the expression.  */
16819370Spst
16919370Spst/* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
17019370Spst   a register through here */
17119370Spst
17219370Spstvoid
17398944Sobrienwrite_exp_elt (union exp_element expelt)
17419370Spst{
17519370Spst  if (expout_ptr >= expout_size)
17619370Spst    {
17719370Spst      expout_size *= 2;
17819370Spst      expout = (struct expression *)
17919370Spst	xrealloc ((char *) expout, sizeof (struct expression)
18019370Spst		  + EXP_ELEM_TO_BYTES (expout_size));
18119370Spst    }
18219370Spst  expout->elts[expout_ptr++] = expelt;
18319370Spst}
18419370Spst
18519370Spstvoid
18698944Sobrienwrite_exp_elt_opcode (enum exp_opcode expelt)
18719370Spst{
18819370Spst  union exp_element tmp;
18919370Spst
19019370Spst  tmp.opcode = expelt;
19119370Spst
19219370Spst  write_exp_elt (tmp);
19319370Spst}
19419370Spst
19519370Spstvoid
19698944Sobrienwrite_exp_elt_sym (struct symbol *expelt)
19719370Spst{
19819370Spst  union exp_element tmp;
19919370Spst
20019370Spst  tmp.symbol = expelt;
20119370Spst
20219370Spst  write_exp_elt (tmp);
20319370Spst}
20419370Spst
20519370Spstvoid
20698944Sobrienwrite_exp_elt_block (struct block *b)
20719370Spst{
20819370Spst  union exp_element tmp;
20919370Spst  tmp.block = b;
21019370Spst  write_exp_elt (tmp);
21119370Spst}
21219370Spst
21319370Spstvoid
21498944Sobrienwrite_exp_elt_longcst (LONGEST expelt)
21519370Spst{
21619370Spst  union exp_element tmp;
21719370Spst
21819370Spst  tmp.longconst = expelt;
21919370Spst
22019370Spst  write_exp_elt (tmp);
22119370Spst}
22219370Spst
22319370Spstvoid
22498944Sobrienwrite_exp_elt_dblcst (DOUBLEST expelt)
22519370Spst{
22619370Spst  union exp_element tmp;
22719370Spst
22819370Spst  tmp.doubleconst = expelt;
22919370Spst
23019370Spst  write_exp_elt (tmp);
23119370Spst}
23219370Spst
23319370Spstvoid
23498944Sobrienwrite_exp_elt_type (struct type *expelt)
23519370Spst{
23619370Spst  union exp_element tmp;
23719370Spst
23819370Spst  tmp.type = expelt;
23919370Spst
24019370Spst  write_exp_elt (tmp);
24119370Spst}
24219370Spst
24319370Spstvoid
24498944Sobrienwrite_exp_elt_intern (struct internalvar *expelt)
24519370Spst{
24619370Spst  union exp_element tmp;
24719370Spst
24819370Spst  tmp.internalvar = expelt;
24919370Spst
25019370Spst  write_exp_elt (tmp);
25119370Spst}
25219370Spst
25319370Spst/* Add a string constant to the end of the expression.
25419370Spst
25519370Spst   String constants are stored by first writing an expression element
25619370Spst   that contains the length of the string, then stuffing the string
25719370Spst   constant itself into however many expression elements are needed
25819370Spst   to hold it, and then writing another expression element that contains
25919370Spst   the length of the string.  I.E. an expression element at each end of
26019370Spst   the string records the string length, so you can skip over the
26119370Spst   expression elements containing the actual string bytes from either
26219370Spst   end of the string.  Note that this also allows gdb to handle
26319370Spst   strings with embedded null bytes, as is required for some languages.
26419370Spst
26519370Spst   Don't be fooled by the fact that the string is null byte terminated,
26619370Spst   this is strictly for the convenience of debugging gdb itself.  Gdb
26719370Spst   Gdb does not depend up the string being null terminated, since the
26819370Spst   actual length is recorded in expression elements at each end of the
26919370Spst   string.  The null byte is taken into consideration when computing how
27019370Spst   many expression elements are required to hold the string constant, of
27119370Spst   course. */
27219370Spst
27319370Spst
27419370Spstvoid
27598944Sobrienwrite_exp_string (struct stoken str)
27619370Spst{
277130803Smarcel  int len = str.length;
278130803Smarcel  int lenelt;
279130803Smarcel  char *strdata;
28019370Spst
28119370Spst  /* Compute the number of expression elements required to hold the string
28219370Spst     (including a null byte terminator), along with one expression element
28319370Spst     at each end to record the actual string length (not including the
28419370Spst     null byte terminator). */
28519370Spst
28619370Spst  lenelt = 2 + BYTES_TO_EXP_ELEM (len + 1);
28719370Spst
28819370Spst  /* Ensure that we have enough available expression elements to store
28919370Spst     everything. */
29019370Spst
29119370Spst  if ((expout_ptr + lenelt) >= expout_size)
29219370Spst    {
29319370Spst      expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
29419370Spst      expout = (struct expression *)
29519370Spst	xrealloc ((char *) expout, (sizeof (struct expression)
29619370Spst				    + EXP_ELEM_TO_BYTES (expout_size)));
29719370Spst    }
29819370Spst
29919370Spst  /* Write the leading length expression element (which advances the current
30019370Spst     expression element index), then write the string constant followed by a
30119370Spst     terminating null byte, and then write the trailing length expression
30219370Spst     element. */
30319370Spst
30419370Spst  write_exp_elt_longcst ((LONGEST) len);
30519370Spst  strdata = (char *) &expout->elts[expout_ptr];
30619370Spst  memcpy (strdata, str.ptr, len);
30719370Spst  *(strdata + len) = '\0';
30819370Spst  expout_ptr += lenelt - 2;
30919370Spst  write_exp_elt_longcst ((LONGEST) len);
31019370Spst}
31119370Spst
31219370Spst/* Add a bitstring constant to the end of the expression.
31319370Spst
31419370Spst   Bitstring constants are stored by first writing an expression element
31519370Spst   that contains the length of the bitstring (in bits), then stuffing the
31619370Spst   bitstring constant itself into however many expression elements are
31719370Spst   needed to hold it, and then writing another expression element that
31819370Spst   contains the length of the bitstring.  I.E. an expression element at
31919370Spst   each end of the bitstring records the bitstring length, so you can skip
32019370Spst   over the expression elements containing the actual bitstring bytes from
32119370Spst   either end of the bitstring. */
32219370Spst
32319370Spstvoid
32498944Sobrienwrite_exp_bitstring (struct stoken str)
32519370Spst{
326130803Smarcel  int bits = str.length;	/* length in bits */
327130803Smarcel  int len = (bits + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
328130803Smarcel  int lenelt;
329130803Smarcel  char *strdata;
33019370Spst
33119370Spst  /* Compute the number of expression elements required to hold the bitstring,
33219370Spst     along with one expression element at each end to record the actual
33319370Spst     bitstring length in bits. */
33419370Spst
33519370Spst  lenelt = 2 + BYTES_TO_EXP_ELEM (len);
33619370Spst
33719370Spst  /* Ensure that we have enough available expression elements to store
33819370Spst     everything. */
33919370Spst
34019370Spst  if ((expout_ptr + lenelt) >= expout_size)
34119370Spst    {
34219370Spst      expout_size = max (expout_size * 2, expout_ptr + lenelt + 10);
34319370Spst      expout = (struct expression *)
34419370Spst	xrealloc ((char *) expout, (sizeof (struct expression)
34519370Spst				    + EXP_ELEM_TO_BYTES (expout_size)));
34619370Spst    }
34719370Spst
34819370Spst  /* Write the leading length expression element (which advances the current
34919370Spst     expression element index), then write the bitstring constant, and then
35019370Spst     write the trailing length expression element. */
35119370Spst
35219370Spst  write_exp_elt_longcst ((LONGEST) bits);
35319370Spst  strdata = (char *) &expout->elts[expout_ptr];
35419370Spst  memcpy (strdata, str.ptr, len);
35519370Spst  expout_ptr += lenelt - 2;
35619370Spst  write_exp_elt_longcst ((LONGEST) bits);
35719370Spst}
35819370Spst
35919370Spst/* Add the appropriate elements for a minimal symbol to the end of
36019370Spst   the expression.  The rationale behind passing in text_symbol_type and
36119370Spst   data_symbol_type was so that Modula-2 could pass in WORD for
36219370Spst   data_symbol_type.  Perhaps it still is useful to have those types vary
36319370Spst   based on the language, but they no longer have names like "int", so
36419370Spst   the initial rationale is gone.  */
36519370Spst
36619370Spststatic struct type *msym_text_symbol_type;
36719370Spststatic struct type *msym_data_symbol_type;
36819370Spststatic struct type *msym_unknown_symbol_type;
36919370Spst
37019370Spstvoid
37198944Sobrienwrite_exp_msymbol (struct minimal_symbol *msymbol,
37298944Sobrien		   struct type *text_symbol_type,
37398944Sobrien		   struct type *data_symbol_type)
37419370Spst{
37546283Sdfr  CORE_ADDR addr;
37646283Sdfr
37719370Spst  write_exp_elt_opcode (OP_LONG);
37898944Sobrien  /* Let's make the type big enough to hold a 64-bit address.  */
37998944Sobrien  write_exp_elt_type (builtin_type_CORE_ADDR);
38046283Sdfr
38146283Sdfr  addr = SYMBOL_VALUE_ADDRESS (msymbol);
38246283Sdfr  if (overlay_debugging)
38346283Sdfr    addr = symbol_overlayed_address (addr, SYMBOL_BFD_SECTION (msymbol));
38446283Sdfr  write_exp_elt_longcst ((LONGEST) addr);
38598944Sobrien
38619370Spst  write_exp_elt_opcode (OP_LONG);
38719370Spst
38819370Spst  write_exp_elt_opcode (UNOP_MEMVAL);
38998944Sobrien  switch (msymbol->type)
39019370Spst    {
39119370Spst    case mst_text:
39219370Spst    case mst_file_text:
39319370Spst    case mst_solib_trampoline:
39419370Spst      write_exp_elt_type (msym_text_symbol_type);
39519370Spst      break;
39619370Spst
39719370Spst    case mst_data:
39819370Spst    case mst_file_data:
39919370Spst    case mst_bss:
40019370Spst    case mst_file_bss:
40119370Spst      write_exp_elt_type (msym_data_symbol_type);
40219370Spst      break;
40319370Spst
40419370Spst    default:
40519370Spst      write_exp_elt_type (msym_unknown_symbol_type);
40619370Spst      break;
40719370Spst    }
40819370Spst  write_exp_elt_opcode (UNOP_MEMVAL);
40919370Spst}
41019370Spst
41119370Spst/* Recognize tokens that start with '$'.  These include:
41219370Spst
41398944Sobrien   $regname     A native register name or a "standard
41498944Sobrien   register name".
41519370Spst
41698944Sobrien   $variable    A convenience variable with a name chosen
41798944Sobrien   by the user.
41819370Spst
41998944Sobrien   $digits              Value history with index <digits>, starting
42098944Sobrien   from the first value which has index 1.
42119370Spst
42298944Sobrien   $$digits     Value history with index <digits> relative
42398944Sobrien   to the last value.  I.E. $$0 is the last
42498944Sobrien   value, $$1 is the one previous to that, $$2
42598944Sobrien   is the one previous to $$1, etc.
42619370Spst
42798944Sobrien   $ | $0 | $$0 The last value in the value history.
42819370Spst
42998944Sobrien   $$           An abbreviation for the second to the last
43098944Sobrien   value in the value history, I.E. $$1
43119370Spst
43298944Sobrien */
43319370Spst
43419370Spstvoid
43598944Sobrienwrite_dollar_variable (struct stoken str)
43619370Spst{
43719370Spst  /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
43819370Spst     and $$digits (equivalent to $<-digits> if you could type that). */
43919370Spst
44019370Spst  int negate = 0;
44119370Spst  int i = 1;
44219370Spst  /* Double dollar means negate the number and add -1 as well.
44319370Spst     Thus $$ alone means -1.  */
44419370Spst  if (str.length >= 2 && str.ptr[1] == '$')
44519370Spst    {
44619370Spst      negate = 1;
44719370Spst      i = 2;
44819370Spst    }
44919370Spst  if (i == str.length)
45019370Spst    {
45119370Spst      /* Just dollars (one or two) */
45298944Sobrien      i = -negate;
45319370Spst      goto handle_last;
45419370Spst    }
45519370Spst  /* Is the rest of the token digits?  */
45619370Spst  for (; i < str.length; i++)
45719370Spst    if (!(str.ptr[i] >= '0' && str.ptr[i] <= '9'))
45819370Spst      break;
45919370Spst  if (i == str.length)
46019370Spst    {
46119370Spst      i = atoi (str.ptr + 1 + negate);
46219370Spst      if (negate)
46398944Sobrien	i = -i;
46419370Spst      goto handle_last;
46519370Spst    }
46698944Sobrien
46719370Spst  /* Handle tokens that refer to machine registers:
46819370Spst     $ followed by a register name.  */
469130803Smarcel  i = frame_map_name_to_regnum (deprecated_selected_frame,
470130803Smarcel				str.ptr + 1, str.length - 1);
47198944Sobrien  if (i >= 0)
47246283Sdfr    goto handle_register;
47319370Spst
47498944Sobrien  if (SYMBOLS_CAN_START_WITH_DOLLAR)
47598944Sobrien    {
47698944Sobrien      struct symbol *sym = NULL;
47798944Sobrien      struct minimal_symbol *msym = NULL;
47846283Sdfr
47998944Sobrien      /* On HP-UX, certain system routines (millicode) have names beginning
48098944Sobrien	 with $ or $$, e.g. $$dyncall, which handles inter-space procedure
48198944Sobrien	 calls on PA-RISC. Check for those, first. */
48298944Sobrien
48398944Sobrien      /* This code is not enabled on non HP-UX systems, since worst case
48498944Sobrien	 symbol table lookup performance is awful, to put it mildly. */
48598944Sobrien
48698944Sobrien      sym = lookup_symbol (copy_name (str), (struct block *) NULL,
487130803Smarcel			   VAR_DOMAIN, (int *) NULL, (struct symtab **) NULL);
48898944Sobrien      if (sym)
48998944Sobrien	{
49098944Sobrien	  write_exp_elt_opcode (OP_VAR_VALUE);
49198944Sobrien	  write_exp_elt_block (block_found);	/* set by lookup_symbol */
49298944Sobrien	  write_exp_elt_sym (sym);
49398944Sobrien	  write_exp_elt_opcode (OP_VAR_VALUE);
49498944Sobrien	  return;
49598944Sobrien	}
49698944Sobrien      msym = lookup_minimal_symbol (copy_name (str), NULL, NULL);
49798944Sobrien      if (msym)
49898944Sobrien	{
49998944Sobrien	  write_exp_msymbol (msym,
50098944Sobrien			     lookup_function_type (builtin_type_int),
50198944Sobrien			     builtin_type_int);
50298944Sobrien	  return;
50398944Sobrien	}
50446283Sdfr    }
50598944Sobrien
50619370Spst  /* Any other names starting in $ are debugger internal variables.  */
50719370Spst
50819370Spst  write_exp_elt_opcode (OP_INTERNALVAR);
50919370Spst  write_exp_elt_intern (lookup_internalvar (copy_name (str) + 1));
51098944Sobrien  write_exp_elt_opcode (OP_INTERNALVAR);
51119370Spst  return;
51298944Sobrienhandle_last:
51319370Spst  write_exp_elt_opcode (OP_LAST);
51419370Spst  write_exp_elt_longcst ((LONGEST) i);
51519370Spst  write_exp_elt_opcode (OP_LAST);
51619370Spst  return;
51798944Sobrienhandle_register:
51819370Spst  write_exp_elt_opcode (OP_REGISTER);
51919370Spst  write_exp_elt_longcst (i);
52098944Sobrien  write_exp_elt_opcode (OP_REGISTER);
52119370Spst  return;
52219370Spst}
52346283Sdfr
52446283Sdfr
52546283Sdfr/* Parse a string that is possibly a namespace / nested class
52646283Sdfr   specification, i.e., something of the form A::B::C::x.  Input
52746283Sdfr   (NAME) is the entire string; LEN is the current valid length; the
52846283Sdfr   output is a string, TOKEN, which points to the largest recognized
52946283Sdfr   prefix which is a series of namespaces or classes.  CLASS_PREFIX is
53046283Sdfr   another output, which records whether a nested class spec was
53146283Sdfr   recognized (= 1) or a fully qualified variable name was found (=
53246283Sdfr   0).  ARGPTR is side-effected (if non-NULL) to point to beyond the
53346283Sdfr   string recognized and consumed by this routine.
53446283Sdfr
53546283Sdfr   The return value is a pointer to the symbol for the base class or
53646283Sdfr   variable if found, or NULL if not found.  Callers must check this
53746283Sdfr   first -- if NULL, the outputs may not be correct.
53846283Sdfr
53946283Sdfr   This function is used c-exp.y.  This is used specifically to get
54046283Sdfr   around HP aCC (and possibly other compilers), which insists on
54146283Sdfr   generating names with embedded colons for namespace or nested class
54246283Sdfr   members.
54346283Sdfr
54446283Sdfr   (Argument LEN is currently unused. 1997-08-27)
54546283Sdfr
54646283Sdfr   Callers must free memory allocated for the output string TOKEN.  */
54746283Sdfr
54898944Sobrienstatic const char coloncolon[2] =
54998944Sobrien{':', ':'};
55046283Sdfr
55146283Sdfrstruct symbol *
55298944Sobrienparse_nested_classes_for_hpacc (char *name, int len, char **token,
55398944Sobrien				int *class_prefix, char **argptr)
55446283Sdfr{
55598944Sobrien  /* Comment below comes from decode_line_1 which has very similar
55698944Sobrien     code, which is called for "break" command parsing. */
55798944Sobrien
55898944Sobrien  /* We have what looks like a class or namespace
55946283Sdfr     scope specification (A::B), possibly with many
56046283Sdfr     levels of namespaces or classes (A::B::C::D).
56146283Sdfr
56246283Sdfr     Some versions of the HP ANSI C++ compiler (as also possibly
56346283Sdfr     other compilers) generate class/function/member names with
56446283Sdfr     embedded double-colons if they are inside namespaces. To
56546283Sdfr     handle this, we loop a few times, considering larger and
56646283Sdfr     larger prefixes of the string as though they were single
56746283Sdfr     symbols.  So, if the initially supplied string is
56846283Sdfr     A::B::C::D::foo, we have to look up "A", then "A::B",
56946283Sdfr     then "A::B::C", then "A::B::C::D", and finally
57046283Sdfr     "A::B::C::D::foo" as single, monolithic symbols, because
57146283Sdfr     A, B, C or D may be namespaces.
57246283Sdfr
57346283Sdfr     Note that namespaces can nest only inside other
57446283Sdfr     namespaces, and not inside classes.  So we need only
57546283Sdfr     consider *prefixes* of the string; there is no need to look up
57646283Sdfr     "B::C" separately as a symbol in the previous example. */
57746283Sdfr
578130803Smarcel  char *p;
57998944Sobrien  char *start, *end;
58098944Sobrien  char *prefix = NULL;
58198944Sobrien  char *tmp;
58298944Sobrien  struct symbol *sym_class = NULL;
58398944Sobrien  struct symbol *sym_var = NULL;
58498944Sobrien  struct type *t;
58546283Sdfr  int prefix_len = 0;
58646283Sdfr  int done = 0;
58798944Sobrien  char *q;
58846283Sdfr
58946283Sdfr  /* Check for HP-compiled executable -- in other cases
59046283Sdfr     return NULL, and caller must default to standard GDB
59146283Sdfr     behaviour. */
59246283Sdfr
59346283Sdfr  if (!hp_som_som_object_present)
59446283Sdfr    return (struct symbol *) NULL;
59546283Sdfr
59646283Sdfr  p = name;
59746283Sdfr
59898944Sobrien  /* Skip over whitespace and possible global "::" */
59998944Sobrien  while (*p && (*p == ' ' || *p == '\t'))
60098944Sobrien    p++;
60146283Sdfr  if (p[0] == ':' && p[1] == ':')
60246283Sdfr    p += 2;
60398944Sobrien  while (*p && (*p == ' ' || *p == '\t'))
60498944Sobrien    p++;
60598944Sobrien
60646283Sdfr  while (1)
60746283Sdfr    {
60846283Sdfr      /* Get to the end of the next namespace or class spec. */
60946283Sdfr      /* If we're looking at some non-token, fail immediately */
61046283Sdfr      start = p;
61146283Sdfr      if (!(isalpha (*p) || *p == '$' || *p == '_'))
61298944Sobrien	return (struct symbol *) NULL;
61346283Sdfr      p++;
61498944Sobrien      while (*p && (isalnum (*p) || *p == '$' || *p == '_'))
61598944Sobrien	p++;
61646283Sdfr
61798944Sobrien      if (*p == '<')
61898944Sobrien	{
61998944Sobrien	  /* If we have the start of a template specification,
62098944Sobrien	     scan right ahead to its end */
62198944Sobrien	  q = find_template_name_end (p);
62298944Sobrien	  if (q)
62398944Sobrien	    p = q;
62498944Sobrien	}
62598944Sobrien
62646283Sdfr      end = p;
62746283Sdfr
62898944Sobrien      /* Skip over "::" and whitespace for next time around */
62998944Sobrien      while (*p && (*p == ' ' || *p == '\t'))
63098944Sobrien	p++;
63146283Sdfr      if (p[0] == ':' && p[1] == ':')
63298944Sobrien	p += 2;
63398944Sobrien      while (*p && (*p == ' ' || *p == '\t'))
63498944Sobrien	p++;
63546283Sdfr
63698944Sobrien      /* Done with tokens? */
63746283Sdfr      if (!*p || !(isalpha (*p) || *p == '$' || *p == '_'))
63898944Sobrien	done = 1;
63946283Sdfr
64046283Sdfr      tmp = (char *) alloca (prefix_len + end - start + 3);
64146283Sdfr      if (prefix)
64298944Sobrien	{
64398944Sobrien	  memcpy (tmp, prefix, prefix_len);
64498944Sobrien	  memcpy (tmp + prefix_len, coloncolon, 2);
64598944Sobrien	  memcpy (tmp + prefix_len + 2, start, end - start);
64698944Sobrien	  tmp[prefix_len + 2 + end - start] = '\000';
64798944Sobrien	}
64846283Sdfr      else
64998944Sobrien	{
65098944Sobrien	  memcpy (tmp, start, end - start);
65198944Sobrien	  tmp[end - start] = '\000';
65298944Sobrien	}
65398944Sobrien
65446283Sdfr      prefix = tmp;
65546283Sdfr      prefix_len = strlen (prefix);
65646283Sdfr
65746283Sdfr      /* See if the prefix we have now is something we know about */
65846283Sdfr
65998944Sobrien      if (!done)
66098944Sobrien	{
66198944Sobrien	  /* More tokens to process, so this must be a class/namespace */
662130803Smarcel	  sym_class = lookup_symbol (prefix, 0, STRUCT_DOMAIN,
66398944Sobrien				     0, (struct symtab **) NULL);
66498944Sobrien	}
66546283Sdfr      else
66698944Sobrien	{
66798944Sobrien	  /* No more tokens, so try as a variable first */
668130803Smarcel	  sym_var = lookup_symbol (prefix, 0, VAR_DOMAIN,
66998944Sobrien				   0, (struct symtab **) NULL);
67098944Sobrien	  /* If failed, try as class/namespace */
67198944Sobrien	  if (!sym_var)
672130803Smarcel	    sym_class = lookup_symbol (prefix, 0, STRUCT_DOMAIN,
67398944Sobrien				       0, (struct symtab **) NULL);
67498944Sobrien	}
67546283Sdfr
67646283Sdfr      if (sym_var ||
67798944Sobrien	  (sym_class &&
67898944Sobrien	   (t = check_typedef (SYMBOL_TYPE (sym_class)),
67998944Sobrien	    (TYPE_CODE (t) == TYPE_CODE_STRUCT
68098944Sobrien	     || TYPE_CODE (t) == TYPE_CODE_UNION))))
68198944Sobrien	{
68298944Sobrien	  /* We found a valid token */
68398944Sobrien	  *token = (char *) xmalloc (prefix_len + 1);
68498944Sobrien	  memcpy (*token, prefix, prefix_len);
68598944Sobrien	  (*token)[prefix_len] = '\000';
68698944Sobrien	  break;
68798944Sobrien	}
68846283Sdfr
68998944Sobrien      /* No variable or class/namespace found, no more tokens */
69046283Sdfr      if (done)
69198944Sobrien	return (struct symbol *) NULL;
69246283Sdfr    }
69346283Sdfr
69446283Sdfr  /* Out of loop, so we must have found a valid token */
69546283Sdfr  if (sym_var)
69646283Sdfr    *class_prefix = 0;
69746283Sdfr  else
69846283Sdfr    *class_prefix = 1;
69946283Sdfr
70046283Sdfr  if (argptr)
70146283Sdfr    *argptr = done ? p : end;
70246283Sdfr
70398944Sobrien  return sym_var ? sym_var : sym_class;		/* found */
70446283Sdfr}
70546283Sdfr
70646283Sdfrchar *
70798944Sobrienfind_template_name_end (char *p)
70846283Sdfr{
70946283Sdfr  int depth = 1;
71046283Sdfr  int just_seen_right = 0;
71146283Sdfr  int just_seen_colon = 0;
71246283Sdfr  int just_seen_space = 0;
71398944Sobrien
71446283Sdfr  if (!p || (*p != '<'))
71546283Sdfr    return 0;
71646283Sdfr
71746283Sdfr  while (*++p)
71846283Sdfr    {
71946283Sdfr      switch (*p)
72098944Sobrien	{
72198944Sobrien	case '\'':
72298944Sobrien	case '\"':
72398944Sobrien	case '{':
72498944Sobrien	case '}':
72598944Sobrien	  /* In future, may want to allow these?? */
72698944Sobrien	  return 0;
72798944Sobrien	case '<':
72898944Sobrien	  depth++;		/* start nested template */
72998944Sobrien	  if (just_seen_colon || just_seen_right || just_seen_space)
73098944Sobrien	    return 0;		/* but not after : or :: or > or space */
73198944Sobrien	  break;
73298944Sobrien	case '>':
73398944Sobrien	  if (just_seen_colon || just_seen_right)
73498944Sobrien	    return 0;		/* end a (nested?) template */
73598944Sobrien	  just_seen_right = 1;	/* but not after : or :: */
73698944Sobrien	  if (--depth == 0)	/* also disallow >>, insist on > > */
73798944Sobrien	    return ++p;		/* if outermost ended, return */
73898944Sobrien	  break;
73998944Sobrien	case ':':
74098944Sobrien	  if (just_seen_space || (just_seen_colon > 1))
74198944Sobrien	    return 0;		/* nested class spec coming up */
74298944Sobrien	  just_seen_colon++;	/* we allow :: but not :::: */
74398944Sobrien	  break;
74498944Sobrien	case ' ':
74598944Sobrien	  break;
74698944Sobrien	default:
74798944Sobrien	  if (!((*p >= 'a' && *p <= 'z') ||	/* allow token chars */
74898944Sobrien		(*p >= 'A' && *p <= 'Z') ||
74998944Sobrien		(*p >= '0' && *p <= '9') ||
75098944Sobrien		(*p == '_') || (*p == ',') ||	/* commas for template args */
75198944Sobrien		(*p == '&') || (*p == '*') ||	/* pointer and ref types */
75298944Sobrien		(*p == '(') || (*p == ')') ||	/* function types */
75398944Sobrien		(*p == '[') || (*p == ']')))	/* array types */
75498944Sobrien	    return 0;
75598944Sobrien	}
75646283Sdfr      if (*p != ' ')
75798944Sobrien	just_seen_space = 0;
75846283Sdfr      if (*p != ':')
75998944Sobrien	just_seen_colon = 0;
76046283Sdfr      if (*p != '>')
76198944Sobrien	just_seen_right = 0;
76246283Sdfr    }
76346283Sdfr  return 0;
76446283Sdfr}
76598944Sobrien
76646283Sdfr
76746283Sdfr
76819370Spst/* Return a null-terminated temporary copy of the name
76919370Spst   of a string token.  */
77019370Spst
77119370Spstchar *
77298944Sobriencopy_name (struct stoken token)
77319370Spst{
77419370Spst  memcpy (namecopy, token.ptr, token.length);
77519370Spst  namecopy[token.length] = 0;
77619370Spst  return namecopy;
77719370Spst}
77819370Spst
77919370Spst/* Reverse an expression from suffix form (in which it is constructed)
78019370Spst   to prefix form (in which we can conveniently print or execute it).  */
78119370Spst
78219370Spststatic void
783130803Smarcelprefixify_expression (struct expression *expr)
78419370Spst{
785130803Smarcel  int len =
78698944Sobrien  sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
787130803Smarcel  struct expression *temp;
788130803Smarcel  int inpos = expr->nelts, outpos = 0;
78919370Spst
79019370Spst  temp = (struct expression *) alloca (len);
79119370Spst
79219370Spst  /* Copy the original expression into temp.  */
79319370Spst  memcpy (temp, expr, len);
79419370Spst
79519370Spst  prefixify_subexp (temp, expr, inpos, outpos);
79619370Spst}
79719370Spst
798130803Smarcel/* Return the number of exp_elements in the postfix subexpression
799130803Smarcel   of EXPR whose operator is at index ENDPOS - 1 in EXPR.  */
80019370Spst
80146283Sdfrint
802130803Smarcellength_of_subexp (struct expression *expr, int endpos)
80319370Spst{
804130803Smarcel  int oplen, args, i;
80519370Spst
806130803Smarcel  operator_length (expr, endpos, &oplen, &args);
807130803Smarcel
808130803Smarcel  while (args > 0)
809130803Smarcel    {
810130803Smarcel      oplen += length_of_subexp (expr, endpos - oplen);
811130803Smarcel      args--;
812130803Smarcel    }
813130803Smarcel
814130803Smarcel  return oplen;
815130803Smarcel}
816130803Smarcel
817130803Smarcel/* Sets *OPLENP to the length of the operator whose (last) index is
818130803Smarcel   ENDPOS - 1 in EXPR, and sets *ARGSP to the number of arguments that
819130803Smarcel   operator takes.  */
820130803Smarcel
821130803Smarcelvoid
822130803Smarceloperator_length (struct expression *expr, int endpos, int *oplenp, int *argsp)
823130803Smarcel{
824130803Smarcel  expr->language_defn->la_exp_desc->operator_length (expr, endpos,
825130803Smarcel						     oplenp, argsp);
826130803Smarcel}
827130803Smarcel
828130803Smarcel/* Default value for operator_length in exp_descriptor vectors.  */
829130803Smarcel
830130803Smarcelvoid
831130803Smarceloperator_length_standard (struct expression *expr, int endpos,
832130803Smarcel			  int *oplenp, int *argsp)
833130803Smarcel{
834130803Smarcel  int oplen = 1;
835130803Smarcel  int args = 0;
836130803Smarcel  int i;
837130803Smarcel
83819370Spst  if (endpos < 1)
839130803Smarcel    error ("?error in operator_length_standard");
84019370Spst
84119370Spst  i = (int) expr->elts[endpos - 1].opcode;
84219370Spst
84319370Spst  switch (i)
84419370Spst    {
84519370Spst      /* C++  */
84619370Spst    case OP_SCOPE:
84719370Spst      oplen = longest_to_int (expr->elts[endpos - 2].longconst);
84819370Spst      oplen = 5 + BYTES_TO_EXP_ELEM (oplen + 1);
84919370Spst      break;
85019370Spst
85119370Spst    case OP_LONG:
85219370Spst    case OP_DOUBLE:
85319370Spst    case OP_VAR_VALUE:
85419370Spst      oplen = 4;
85519370Spst      break;
85619370Spst
85719370Spst    case OP_TYPE:
85819370Spst    case OP_BOOL:
85919370Spst    case OP_LAST:
86019370Spst    case OP_REGISTER:
86119370Spst    case OP_INTERNALVAR:
86219370Spst      oplen = 3;
86319370Spst      break;
86419370Spst
86519370Spst    case OP_COMPLEX:
86698944Sobrien      oplen = 1;
86719370Spst      args = 2;
86898944Sobrien      break;
86919370Spst
87019370Spst    case OP_FUNCALL:
87119370Spst    case OP_F77_UNDETERMINED_ARGLIST:
87219370Spst      oplen = 3;
87319370Spst      args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
87419370Spst      break;
87519370Spst
876130803Smarcel    case OP_OBJC_MSGCALL:	/* Objective C message (method) call */
877130803Smarcel      oplen = 4;
878130803Smarcel      args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
879130803Smarcel      break;
880130803Smarcel
88119370Spst    case UNOP_MAX:
88219370Spst    case UNOP_MIN:
88319370Spst      oplen = 3;
88419370Spst      break;
88519370Spst
88698944Sobrien    case BINOP_VAL:
88798944Sobrien    case UNOP_CAST:
88898944Sobrien    case UNOP_MEMVAL:
88919370Spst      oplen = 3;
89019370Spst      args = 1;
89119370Spst      break;
89219370Spst
89319370Spst    case UNOP_ABS:
89419370Spst    case UNOP_CAP:
89519370Spst    case UNOP_CHR:
89619370Spst    case UNOP_FLOAT:
89719370Spst    case UNOP_HIGH:
89819370Spst    case UNOP_ODD:
89919370Spst    case UNOP_ORD:
90019370Spst    case UNOP_TRUNC:
90119370Spst      oplen = 1;
90219370Spst      args = 1;
90319370Spst      break;
90419370Spst
90519370Spst    case OP_LABELED:
90619370Spst    case STRUCTOP_STRUCT:
90719370Spst    case STRUCTOP_PTR:
90819370Spst      args = 1;
90919370Spst      /* fall through */
91019370Spst    case OP_M2_STRING:
91119370Spst    case OP_STRING:
912130803Smarcel    case OP_OBJC_NSSTRING:	/* Objective C Foundation Class NSString constant */
913130803Smarcel    case OP_OBJC_SELECTOR:	/* Objective C "@selector" pseudo-op */
91419370Spst    case OP_NAME:
91519370Spst    case OP_EXPRSTRING:
91619370Spst      oplen = longest_to_int (expr->elts[endpos - 2].longconst);
91719370Spst      oplen = 4 + BYTES_TO_EXP_ELEM (oplen + 1);
91819370Spst      break;
91919370Spst
92019370Spst    case OP_BITSTRING:
92119370Spst      oplen = longest_to_int (expr->elts[endpos - 2].longconst);
92219370Spst      oplen = (oplen + HOST_CHAR_BIT - 1) / HOST_CHAR_BIT;
92319370Spst      oplen = 4 + BYTES_TO_EXP_ELEM (oplen);
92419370Spst      break;
92519370Spst
92619370Spst    case OP_ARRAY:
92719370Spst      oplen = 4;
92819370Spst      args = longest_to_int (expr->elts[endpos - 2].longconst);
92919370Spst      args -= longest_to_int (expr->elts[endpos - 3].longconst);
93019370Spst      args += 1;
93119370Spst      break;
93219370Spst
93319370Spst    case TERNOP_COND:
93419370Spst    case TERNOP_SLICE:
93519370Spst    case TERNOP_SLICE_COUNT:
93619370Spst      args = 3;
93719370Spst      break;
93819370Spst
93919370Spst      /* Modula-2 */
94098944Sobrien    case MULTI_SUBSCRIPT:
94119370Spst      oplen = 3;
94298944Sobrien      args = 1 + longest_to_int (expr->elts[endpos - 2].longconst);
94319370Spst      break;
94419370Spst
94519370Spst    case BINOP_ASSIGN_MODIFY:
94619370Spst      oplen = 3;
94719370Spst      args = 2;
94819370Spst      break;
94919370Spst
95019370Spst      /* C++ */
95119370Spst    case OP_THIS:
952130803Smarcel    case OP_OBJC_SELF:
95319370Spst      oplen = 2;
95419370Spst      break;
95519370Spst
95619370Spst    default:
95719370Spst      args = 1 + (i < (int) BINOP_END);
95819370Spst    }
95919370Spst
960130803Smarcel  *oplenp = oplen;
961130803Smarcel  *argsp = args;
96219370Spst}
96319370Spst
96419370Spst/* Copy the subexpression ending just before index INEND in INEXPR
96519370Spst   into OUTEXPR, starting at index OUTBEG.
96619370Spst   In the process, convert it from suffix to prefix form.  */
96719370Spst
96819370Spststatic void
969130803Smarcelprefixify_subexp (struct expression *inexpr,
970130803Smarcel		  struct expression *outexpr, int inend, int outbeg)
97119370Spst{
972130803Smarcel  int oplen;
973130803Smarcel  int args;
974130803Smarcel  int i;
97519370Spst  int *arglens;
97619370Spst  enum exp_opcode opcode;
97719370Spst
978130803Smarcel  operator_length (inexpr, inend, &oplen, &args);
97919370Spst
98019370Spst  /* Copy the final operator itself, from the end of the input
98119370Spst     to the beginning of the output.  */
98219370Spst  inend -= oplen;
98319370Spst  memcpy (&outexpr->elts[outbeg], &inexpr->elts[inend],
98419370Spst	  EXP_ELEM_TO_BYTES (oplen));
98519370Spst  outbeg += oplen;
98619370Spst
98719370Spst  /* Find the lengths of the arg subexpressions.  */
98819370Spst  arglens = (int *) alloca (args * sizeof (int));
98919370Spst  for (i = args - 1; i >= 0; i--)
99019370Spst    {
99119370Spst      oplen = length_of_subexp (inexpr, inend);
99219370Spst      arglens[i] = oplen;
99319370Spst      inend -= oplen;
99419370Spst    }
99519370Spst
99619370Spst  /* Now copy each subexpression, preserving the order of
99719370Spst     the subexpressions, but prefixifying each one.
99819370Spst     In this loop, inend starts at the beginning of
99919370Spst     the expression this level is working on
100019370Spst     and marches forward over the arguments.
100119370Spst     outbeg does similarly in the output.  */
100219370Spst  for (i = 0; i < args; i++)
100319370Spst    {
100419370Spst      oplen = arglens[i];
100519370Spst      inend += oplen;
100619370Spst      prefixify_subexp (inexpr, outexpr, inend, outbeg);
100719370Spst      outbeg += oplen;
100819370Spst    }
100919370Spst}
101019370Spst
101119370Spst/* This page contains the two entry points to this file.  */
101219370Spst
101319370Spst/* Read an expression from the string *STRINGPTR points to,
101419370Spst   parse it, and return a pointer to a  struct expression  that we malloc.
101519370Spst   Use block BLOCK as the lexical context for variable names;
101619370Spst   if BLOCK is zero, use the block of the selected stack frame.
101719370Spst   Meanwhile, advance *STRINGPTR to point after the expression,
101819370Spst   at the first nonwhite character that is not part of the expression
101919370Spst   (possibly a null character).
102019370Spst
102119370Spst   If COMMA is nonzero, stop if a comma is reached.  */
102219370Spst
102319370Spststruct expression *
102498944Sobrienparse_exp_1 (char **stringptr, struct block *block, int comma)
102519370Spst{
102619370Spst  struct cleanup *old_chain;
102719370Spst
102819370Spst  lexptr = *stringptr;
1029130803Smarcel  prev_lexptr = NULL;
103019370Spst
103119370Spst  paren_depth = 0;
103219370Spst  type_stack_depth = 0;
103319370Spst
103419370Spst  comma_terminates = comma;
103519370Spst
103619370Spst  if (lexptr == 0 || *lexptr == 0)
103719370Spst    error_no_arg ("expression to compute");
103819370Spst
103998944Sobrien  old_chain = make_cleanup (free_funcalls, 0 /*ignore*/);
104019370Spst  funcall_chain = 0;
104119370Spst
1042130803Smarcel  if (block)
1043130803Smarcel    {
1044130803Smarcel      expression_context_block = block;
1045130803Smarcel      expression_context_pc = BLOCK_START (block);
1046130803Smarcel    }
1047130803Smarcel  else
1048130803Smarcel    expression_context_block = get_selected_block (&expression_context_pc);
104919370Spst
105019370Spst  namecopy = (char *) alloca (strlen (lexptr) + 1);
105119370Spst  expout_size = 10;
105219370Spst  expout_ptr = 0;
105319370Spst  expout = (struct expression *)
105419370Spst    xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
105519370Spst  expout->language_defn = current_language;
105698944Sobrien  make_cleanup (free_current_contents, &expout);
105719370Spst
105819370Spst  if (current_language->la_parser ())
105919370Spst    current_language->la_error (NULL);
106019370Spst
106119370Spst  discard_cleanups (old_chain);
106219370Spst
106319370Spst  /* Record the actual number of expression elements, and then
106419370Spst     reallocate the expression memory so that we free up any
106519370Spst     excess elements. */
106619370Spst
106719370Spst  expout->nelts = expout_ptr;
106819370Spst  expout = (struct expression *)
106919370Spst    xrealloc ((char *) expout,
107019370Spst	      sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));;
107119370Spst
107219370Spst  /* Convert expression from postfix form as generated by yacc
107319370Spst     parser, to a prefix form. */
107419370Spst
107546283Sdfr  if (expressiondebug)
1076130803Smarcel    dump_raw_expression (expout, gdb_stdlog,
1077130803Smarcel			 "before conversion to prefix form");
107846283Sdfr
107919370Spst  prefixify_expression (expout);
108019370Spst
108146283Sdfr  if (expressiondebug)
1082130803Smarcel    dump_prefix_expression (expout, gdb_stdlog);
108346283Sdfr
108419370Spst  *stringptr = lexptr;
108519370Spst  return expout;
108619370Spst}
108719370Spst
108819370Spst/* Parse STRING as an expression, and complain if this fails
108919370Spst   to use up all of the contents of STRING.  */
109019370Spst
109119370Spststruct expression *
109298944Sobrienparse_expression (char *string)
109319370Spst{
1094130803Smarcel  struct expression *exp;
109519370Spst  exp = parse_exp_1 (&string, 0, 0);
109619370Spst  if (*string)
109719370Spst    error ("Junk after end of expression.");
109819370Spst  return exp;
109919370Spst}
110019370Spst
110119370Spst/* Stuff for maintaining a stack of types.  Currently just used by C, but
110219370Spst   probably useful for any language which declares its types "backwards".  */
110319370Spst
110498944Sobrienstatic void
110598944Sobriencheck_type_stack_depth (void)
110619370Spst{
110719370Spst  if (type_stack_depth == type_stack_size)
110819370Spst    {
110919370Spst      type_stack_size *= 2;
111019370Spst      type_stack = (union type_stack_elt *)
111119370Spst	xrealloc ((char *) type_stack, type_stack_size * sizeof (*type_stack));
111219370Spst    }
111398944Sobrien}
111498944Sobrien
111598944Sobrienvoid
111698944Sobrienpush_type (enum type_pieces tp)
111798944Sobrien{
111898944Sobrien  check_type_stack_depth ();
111919370Spst  type_stack[type_stack_depth++].piece = tp;
112019370Spst}
112119370Spst
112219370Spstvoid
112398944Sobrienpush_type_int (int n)
112419370Spst{
112598944Sobrien  check_type_stack_depth ();
112619370Spst  type_stack[type_stack_depth++].int_val = n;
112719370Spst}
112819370Spst
112998944Sobrienvoid
113098944Sobrienpush_type_address_space (char *string)
113119370Spst{
113298944Sobrien  push_type_int (address_space_name_to_int (string));
113398944Sobrien}
113498944Sobrien
113598944Sobrienenum type_pieces
113698944Sobrienpop_type (void)
113798944Sobrien{
113819370Spst  if (type_stack_depth)
113919370Spst    return type_stack[--type_stack_depth].piece;
114019370Spst  return tp_end;
114119370Spst}
114219370Spst
114319370Spstint
114498944Sobrienpop_type_int (void)
114519370Spst{
114619370Spst  if (type_stack_depth)
114719370Spst    return type_stack[--type_stack_depth].int_val;
114819370Spst  /* "Can't happen".  */
114919370Spst  return 0;
115019370Spst}
115119370Spst
115219370Spst/* Pop the type stack and return the type which corresponds to FOLLOW_TYPE
115319370Spst   as modified by all the stuff on the stack.  */
115419370Spststruct type *
115598944Sobrienfollow_types (struct type *follow_type)
115619370Spst{
115719370Spst  int done = 0;
115898944Sobrien  int make_const = 0;
115998944Sobrien  int make_volatile = 0;
116098944Sobrien  int make_addr_space = 0;
116119370Spst  int array_size;
116219370Spst  struct type *range_type;
116319370Spst
116419370Spst  while (!done)
116519370Spst    switch (pop_type ())
116619370Spst      {
116719370Spst      case tp_end:
116819370Spst	done = 1;
116998944Sobrien	if (make_const)
1170244437Semaste	  follow_type = make_cvr_type (make_const,
1171244437Semaste				       TYPE_VOLATILE (follow_type),
1172244437Semaste				       TYPE_RESTRICT (follow_type),
1173244437Semaste				       follow_type, 0);
117498944Sobrien	if (make_volatile)
1175244437Semaste	  follow_type = make_cvr_type (TYPE_CONST (follow_type),
1176244437Semaste				       make_volatile,
1177244437Semaste				       TYPE_RESTRICT (follow_type),
1178244437Semaste				       follow_type, 0);
117998944Sobrien	if (make_addr_space)
118098944Sobrien	  follow_type = make_type_with_address_space (follow_type,
118198944Sobrien						      make_addr_space);
118298944Sobrien	make_const = make_volatile = 0;
118398944Sobrien	make_addr_space = 0;
118419370Spst	break;
118598944Sobrien      case tp_const:
118698944Sobrien	make_const = 1;
118798944Sobrien	break;
118898944Sobrien      case tp_volatile:
118998944Sobrien	make_volatile = 1;
119098944Sobrien	break;
119198944Sobrien      case tp_space_identifier:
119298944Sobrien	make_addr_space = pop_type_int ();
119398944Sobrien	break;
119419370Spst      case tp_pointer:
119519370Spst	follow_type = lookup_pointer_type (follow_type);
119698944Sobrien	if (make_const)
1197244437Semaste	  follow_type = make_cvr_type (make_const,
1198244437Semaste				       TYPE_VOLATILE (follow_type),
1199244437Semaste				       TYPE_RESTRICT (follow_type),
1200244437Semaste				       follow_type, 0);
120198944Sobrien	if (make_volatile)
1202244437Semaste	  follow_type = make_cvr_type (TYPE_CONST (follow_type),
1203244437Semaste				       make_volatile,
1204244437Semaste				       TYPE_RESTRICT (follow_type),
1205244437Semaste				       follow_type, 0);
120698944Sobrien	if (make_addr_space)
120798944Sobrien	  follow_type = make_type_with_address_space (follow_type,
120898944Sobrien						      make_addr_space);
120998944Sobrien	make_const = make_volatile = 0;
121098944Sobrien	make_addr_space = 0;
121119370Spst	break;
121219370Spst      case tp_reference:
121319370Spst	follow_type = lookup_reference_type (follow_type);
121498944Sobrien	if (make_const)
1215244437Semaste	  follow_type = make_cvr_type (make_const,
1216244437Semaste				       TYPE_VOLATILE (follow_type),
1217244437Semaste				       TYPE_RESTRICT (follow_type),
1218244437Semaste				       follow_type, 0);
121998944Sobrien	if (make_volatile)
1220244437Semaste	  follow_type = make_cvr_type (TYPE_CONST (follow_type),
1221244437Semaste				       make_volatile,
1222244437Semaste				       TYPE_RESTRICT (follow_type),
1223244437Semaste				       follow_type, 0);
122498944Sobrien	if (make_addr_space)
122598944Sobrien	  follow_type = make_type_with_address_space (follow_type,
122698944Sobrien						      make_addr_space);
122798944Sobrien	make_const = make_volatile = 0;
122898944Sobrien	make_addr_space = 0;
122919370Spst	break;
123019370Spst      case tp_array:
123119370Spst	array_size = pop_type_int ();
123219370Spst	/* FIXME-type-allocation: need a way to free this type when we are
123319370Spst	   done with it.  */
123419370Spst	range_type =
123519370Spst	  create_range_type ((struct type *) NULL,
123619370Spst			     builtin_type_int, 0,
123719370Spst			     array_size >= 0 ? array_size - 1 : 0);
123819370Spst	follow_type =
123919370Spst	  create_array_type ((struct type *) NULL,
124019370Spst			     follow_type, range_type);
124119370Spst	if (array_size < 0)
124298944Sobrien	  TYPE_ARRAY_UPPER_BOUND_TYPE (follow_type)
124319370Spst	    = BOUND_CANNOT_BE_DETERMINED;
124419370Spst	break;
124519370Spst      case tp_function:
124619370Spst	/* FIXME-type-allocation: need a way to free this type when we are
124719370Spst	   done with it.  */
124819370Spst	follow_type = lookup_function_type (follow_type);
124919370Spst	break;
125019370Spst      }
125119370Spst  return follow_type;
125219370Spst}
125319370Spst
125498944Sobrienstatic void build_parse (void);
125598944Sobrienstatic void
125698944Sobrienbuild_parse (void)
125719370Spst{
125898944Sobrien  int i;
125919370Spst
126019370Spst  msym_text_symbol_type =
126119370Spst    init_type (TYPE_CODE_FUNC, 1, 0, "<text variable, no debug info>", NULL);
126219370Spst  TYPE_TARGET_TYPE (msym_text_symbol_type) = builtin_type_int;
126319370Spst  msym_data_symbol_type =
126419370Spst    init_type (TYPE_CODE_INT, TARGET_INT_BIT / HOST_CHAR_BIT, 0,
126519370Spst	       "<data variable, no debug info>", NULL);
126619370Spst  msym_unknown_symbol_type =
126719370Spst    init_type (TYPE_CODE_INT, 1, 0,
126819370Spst	       "<variable (not text or data), no debug info>",
126919370Spst	       NULL);
1270130803Smarcel}
127146283Sdfr
1272130803Smarcel/* This function avoids direct calls to fprintf
1273130803Smarcel   in the parser generated debug code.  */
1274130803Smarcelvoid
1275130803Smarcelparser_fprintf (FILE *x, const char *y, ...)
1276130803Smarcel{
1277130803Smarcel  va_list args;
1278130803Smarcel  va_start (args, y);
1279130803Smarcel  if (x == stderr)
1280130803Smarcel    vfprintf_unfiltered (gdb_stderr, y, args);
1281130803Smarcel  else
128298944Sobrien    {
1283130803Smarcel      fprintf_unfiltered (gdb_stderr, " Unknown FILE used.\n");
1284130803Smarcel      vfprintf_unfiltered (gdb_stderr, y, args);
128598944Sobrien    }
1286130803Smarcel  va_end (args);
128798944Sobrien}
128898944Sobrien
128998944Sobrienvoid
129098944Sobrien_initialize_parse (void)
129198944Sobrien{
129298944Sobrien  type_stack_size = 80;
129398944Sobrien  type_stack_depth = 0;
129498944Sobrien  type_stack = (union type_stack_elt *)
129598944Sobrien    xmalloc (type_stack_size * sizeof (*type_stack));
129698944Sobrien
129798944Sobrien  build_parse ();
129898944Sobrien
129998944Sobrien  /* FIXME - For the moment, handle types by swapping them in and out.
130098944Sobrien     Should be using the per-architecture data-pointer and a large
130198944Sobrien     struct. */
1302130803Smarcel  DEPRECATED_REGISTER_GDBARCH_SWAP (msym_text_symbol_type);
1303130803Smarcel  DEPRECATED_REGISTER_GDBARCH_SWAP (msym_data_symbol_type);
1304130803Smarcel  DEPRECATED_REGISTER_GDBARCH_SWAP (msym_unknown_symbol_type);
1305130803Smarcel  deprecated_register_gdbarch_swap (NULL, 0, build_parse);
130698944Sobrien
130746283Sdfr  add_show_from_set (
130898944Sobrien	    add_set_cmd ("expression", class_maintenance, var_zinteger,
130998944Sobrien			 (char *) &expressiondebug,
131098944Sobrien			 "Set expression debugging.\n\
131146283SdfrWhen non-zero, the internal representation of expressions will be printed.",
131298944Sobrien			 &setdebuglist),
131398944Sobrien		      &showdebuglist);
131419370Spst}
1315