/* Parse expressions for GDB.
- Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
- 1998, 1999, 2000, 2001, 2004, 2005, 2007, 2008, 2009, 2010, 2011
- Free Software Foundation, Inc.
+ Copyright (C) 1986, 1989-2001, 2004-2005, 2007-2012 Free Software
+ Foundation, Inc.
Modified from expread.y by the Department of Computer Science at the
State University of New York at Buffalo, 1991.
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Parse an expression from text in a string,
- and return the result as a struct expression pointer.
+ and return the result as a struct expression pointer.
That structure contains arithmetic operations in reverse polish,
with constants represented by operations that are followed by special data.
See expression.h for the details of the format.
'->'. This is set when parsing and is only used when completing a
field name. It is -1 if no dereference operation was found. */
static int expout_last_struct = -1;
-
-/* A temporary buffer for identifiers, so we can null-terminate them.
-
- We allocate this with xrealloc. parse_exp_1 used to allocate with
- alloca, using the size of the whole expression as a conservative
- estimate of the space needed. However, macro expansion can
- introduce names longer than the original expression; there's no
- practical way to know beforehand how large that might be. */
-char *namecopy;
-size_t namecopy_size;
\f
static int expressiondebug = 0;
static void
static void free_funcalls (void *ignore);
-static int prefixify_expression (struct expression *);
-
static int prefixify_subexp (struct expression *, struct expression *, int,
int);
}
}
\f
-/* This page contains the functions for adding data to the struct expression
+/* This page contains the functions for adding data to the struct expression
being constructed. */
+/* See definition in parser-defs.h. */
+
+void
+initialize_expout (int initial_size, const struct language_defn *lang,
+ struct gdbarch *gdbarch)
+{
+ expout_size = initial_size;
+ expout_ptr = 0;
+ expout = xmalloc (sizeof (struct expression)
+ + EXP_ELEM_TO_BYTES (expout_size));
+ expout->language_defn = lang;
+ expout->gdbarch = gdbarch;
+}
+
+/* See definition in parser-defs.h. */
+
+void
+reallocate_expout (void)
+{
+ /* Record the actual number of expression elements, and then
+ reallocate the expression memory so that we free up any
+ excess elements. */
+
+ expout->nelts = expout_ptr;
+ expout = xrealloc ((char *) expout,
+ sizeof (struct expression)
+ + EXP_ELEM_TO_BYTES (expout_ptr));
+}
+
/* Add one element to the end of the expression. */
/* To avoid a bug in the Sun 4 compiler, we pass things that can fit into
a register through here. */
-void
-write_exp_elt (union exp_element expelt)
+static void
+write_exp_elt (const union exp_element *expelt)
{
if (expout_ptr >= expout_size)
{
xrealloc ((char *) expout, sizeof (struct expression)
+ EXP_ELEM_TO_BYTES (expout_size));
}
- expout->elts[expout_ptr++] = expelt;
+ expout->elts[expout_ptr++] = *expelt;
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.opcode = expelt;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.symbol = expelt;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.block = b;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.objfile = objfile;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.longconst = expelt;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.doubleconst = expelt;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
for (index = 0; index < 16; index++)
tmp.decfloatconst[index] = expelt[index];
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.type = expelt;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
void
memset (&tmp, 0, sizeof (union exp_element));
tmp.internalvar = expelt;
- write_exp_elt (tmp);
+ write_exp_elt (&tmp);
}
/* Add a string constant to the end of the expression.
}
\f
+/* Return a null-terminated temporary copy of the name of a string token.
-/* Return a null-terminated temporary copy of the name
- of a string token. */
+ Tokens that refer to names do so with explicit pointer and length,
+ so they can share the storage that lexptr is parsing.
+ When it is necessary to pass a name to a function that expects
+ a null-terminated string, the substring is copied out
+ into a separate block of storage.
+
+ N.B. A single buffer is reused on each call. */
char *
copy_name (struct stoken token)
{
+ /* A temporary buffer for identifiers, so we can null-terminate them.
+ We allocate this with xrealloc. parse_exp_1 used to allocate with
+ alloca, using the size of the whole expression as a conservative
+ estimate of the space needed. However, macro expansion can
+ introduce names longer than the original expression; there's no
+ practical way to know beforehand how large that might be. */
+ static char *namecopy;
+ static size_t namecopy_size;
+
/* Make sure there's enough space for the token. */
if (namecopy_size < token.length + 1)
{
return namecopy;
}
\f
-/* Reverse an expression from suffix form (in which it is constructed)
- to prefix form (in which we can conveniently print or execute it).
- Ordinarily this always returns -1. However, if EXPOUT_LAST_STRUCT
- is not -1 (i.e., we are trying to complete a field name), it will
- return the index of the subexpression which is the left-hand-side
- of the struct operation at EXPOUT_LAST_STRUCT. */
-static int
+/* See comments on parser-defs.h. */
+
+int
prefixify_expression (struct expression *expr)
{
int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts);
case OP_BOOL:
case OP_LAST:
case OP_INTERNALVAR:
+ case OP_VAR_ENTRY_VALUE:
oplen = 3;
break;
}
\f
/* Read an expression from the string *STRINGPTR points to,
- parse it, and return a pointer to a struct expression that we malloc.
+ parse it, and return a pointer to a struct expression that we malloc.
Use block BLOCK as the lexical context for variable names;
if BLOCK is zero, use the block of the selected stack frame.
Meanwhile, advance *STRINGPTR to point after the expression,
else
lang = current_language;
- expout_size = 10;
- expout_ptr = 0;
- expout = (struct expression *)
- xmalloc (sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_size));
- expout->language_defn = lang;
- expout->gdbarch = get_current_arch ();
+ initialize_expout (10, lang, get_current_arch ());
TRY_CATCH (except, RETURN_MASK_ALL)
{
discard_cleanups (old_chain);
- /* Record the actual number of expression elements, and then
- reallocate the expression memory so that we free up any
- excess elements. */
-
- expout->nelts = expout_ptr;
- expout = (struct expression *)
- xrealloc ((char *) expout,
- sizeof (struct expression) + EXP_ELEM_TO_BYTES (expout_ptr));
+ reallocate_expout ();
/* Convert expression from postfix form as generated by yacc
parser, to a prefix form. */