]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/parse.c
C++-ify parser_state
[thirdparty/binutils-gdb.git] / gdb / parse.c
index 6bbf25f69900e774289cf4ed0df64a4a66dfbc56..127ce7ff2915890575af269846d8eebbfcdebe2b 100644 (file)
@@ -44,7 +44,7 @@
 #include "gdbcmd.h"
 #include "symfile.h"           /* for overlay functions */
 #include "inferior.h"
-#include "doublest.h"
+#include "target-float.h"
 #include "block.h"
 #include "source.h"
 #include "objfiles.h"
@@ -152,34 +152,32 @@ end_arglist (void)
 
 /* See definition in parser-defs.h.  */
 
-void
-initialize_expout (struct parser_state *ps, size_t initial_size,
-                  const struct language_defn *lang,
-                  struct gdbarch *gdbarch)
+parser_state::parser_state (size_t initial_size,
+                           const struct language_defn *lang,
+                           struct gdbarch *gdbarch)
+  : expout_size (initial_size),
+    expout (XNEWVAR (expression,
+                    (sizeof (expression)
+                     + EXP_ELEM_TO_BYTES (expout_size)))),
+    expout_ptr (0)
 {
-  ps->expout_size = initial_size;
-  ps->expout_ptr = 0;
-  ps->expout
-    = (struct expression *) xmalloc (sizeof (struct expression)
-                                    + EXP_ELEM_TO_BYTES (ps->expout_size));
-  ps->expout->language_defn = lang;
-  ps->expout->gdbarch = gdbarch;
+  expout->language_defn = lang;
+  expout->gdbarch = gdbarch;
 }
 
-/* See definition in parser-defs.h.  */
-
-void
-reallocate_expout (struct parser_state *ps)
+expression_up
+parser_state::release ()
 {
   /* Record the actual number of expression elements, and then
      reallocate the expression memory so that we free up any
      excess elements.  */
 
-  ps->expout->nelts = ps->expout_ptr;
-  ps->expout = (struct expression *)
-     xrealloc (ps->expout,
-              sizeof (struct expression)
-              + EXP_ELEM_TO_BYTES (ps->expout_ptr));
+  expout->nelts = expout_ptr;
+  expout.reset (XRESIZEVAR (expression, expout.release (),
+                           (sizeof (expression)
+                            + EXP_ELEM_TO_BYTES (expout_ptr))));
+
+  return std::move (expout);
 }
 
 /* This page contains the functions for adding data to the struct expression
@@ -196,9 +194,9 @@ write_exp_elt (struct parser_state *ps, const union exp_element *expelt)
   if (ps->expout_ptr >= ps->expout_size)
     {
       ps->expout_size *= 2;
-      ps->expout = (struct expression *)
-       xrealloc (ps->expout, sizeof (struct expression)
-                 + EXP_ELEM_TO_BYTES (ps->expout_size));
+      ps->expout.reset (XRESIZEVAR (expression, ps->expout.release (),
+                                   (sizeof (expression)
+                                    + EXP_ELEM_TO_BYTES (ps->expout_size))));
     }
   ps->expout->elts[ps->expout_ptr++] = *expelt;
 }
@@ -264,23 +262,13 @@ write_exp_elt_longcst (struct parser_state *ps, LONGEST expelt)
 }
 
 void
-write_exp_elt_dblcst (struct parser_state *ps, DOUBLEST expelt)
-{
-  union exp_element tmp;
-
-  memset (&tmp, 0, sizeof (union exp_element));
-  tmp.doubleconst = expelt;
-  write_exp_elt (ps, &tmp);
-}
-
-void
-write_exp_elt_decfloatcst (struct parser_state *ps, gdb_byte expelt[16])
+write_exp_elt_floatcst (struct parser_state *ps, const gdb_byte expelt[16])
 {
   union exp_element tmp;
   int index;
 
   for (index = 0; index < 16; index++)
-    tmp.decfloatconst[index] = expelt[index];
+    tmp.floatconst[index] = expelt[index];
 
   write_exp_elt (ps, &tmp);
 }
@@ -512,8 +500,6 @@ find_minsym_type_and_address (minimal_symbol *msymbol,
   if (address_p != NULL)
     *address_p = addr;
 
-  struct type *the_type;
-
   switch (type)
     {
     case mst_text:
@@ -870,8 +856,7 @@ operator_length_standard (const struct expression *expr, int endpos,
       break;
 
     case OP_LONG:
-    case OP_DOUBLE:
-    case OP_DECFLOAT:
+    case OP_FLOAT:
     case OP_VAR_VALUE:
     case OP_VAR_MSYM_VALUE:
       oplen = 4;
@@ -1129,7 +1114,6 @@ parse_exp_in_context_1 (const char **stringptr, CORE_ADDR pc,
                        int comma, int void_context_p, int *out_subexp)
 {
   const struct language_defn *lang = NULL;
-  struct parser_state ps;
   int subexp;
 
   lexptr = *stringptr;
@@ -1205,7 +1189,7 @@ parse_exp_in_context_1 (const char **stringptr, CORE_ADDR pc,
      and others called from *.y) ensure CURRENT_LANGUAGE gets restored
      to the value matching SELECTED_FRAME as set by get_current_arch.  */
 
-  initialize_expout (&ps, 10, lang, get_current_arch ());
+  parser_state ps (10, lang, get_current_arch ());
 
   scoped_restore_current_language lang_saver;
   set_language (lang->la_language);
@@ -1218,33 +1202,32 @@ parse_exp_in_context_1 (const char **stringptr, CORE_ADDR pc,
   CATCH (except, RETURN_MASK_ALL)
     {
       if (! parse_completion)
-       {
-         xfree (ps.expout);
-         throw_exception (except);
-       }
+       throw_exception (except);
     }
   END_CATCH
 
-  reallocate_expout (&ps);
+  /* We have to operate on an "expression *", due to la_post_parser,
+     which explains this funny-looking double release.  */
+  expression_up result = ps.release ();
 
   /* Convert expression from postfix form as generated by yacc
      parser, to a prefix form.  */
 
   if (expressiondebug)
-    dump_raw_expression (ps.expout, gdb_stdlog,
+    dump_raw_expression (result.get (), gdb_stdlog,
                         "before conversion to prefix form");
 
-  subexp = prefixify_expression (ps.expout);
+  subexp = prefixify_expression (result.get ());
   if (out_subexp)
     *out_subexp = subexp;
 
-  lang->la_post_parser (&ps.expout, void_context_p);
+  lang->la_post_parser (&result, void_context_p);
 
   if (expressiondebug)
-    dump_prefix_expression (ps.expout, gdb_stdlog);
+    dump_prefix_expression (result.get (), gdb_stdlog);
 
   *stringptr = lexptr;
-  return expression_up (ps.expout);
+  return result;
 }
 
 /* Parse STRING as an expression, and complain if this fails
@@ -1333,74 +1316,22 @@ parse_expression_for_completion (const char *string, char **name,
 /* A post-parser that does nothing.  */
 
 void
-null_post_parser (struct expression **exp, int void_context_p)
+null_post_parser (expression_up *exp, int void_context_p)
 {
 }
 
 /* Parse floating point value P of length LEN.
-   Return 0 (false) if invalid, 1 (true) if valid.
-   The successfully parsed number is stored in D.
-   *SUFFIX points to the suffix of the number in P.
+   Return false if invalid, true if valid.
+   The successfully parsed number is stored in DATA in
+   target format for floating-point type TYPE.
 
    NOTE: This accepts the floating point syntax that sscanf accepts.  */
 
-int
-parse_float (const char *p, int len, DOUBLEST *d, const char **suffix)
+bool
+parse_float (const char *p, int len,
+            const struct type *type, gdb_byte *data)
 {
-  char *copy;
-  int n, num;
-
-  copy = (char *) xmalloc (len + 1);
-  memcpy (copy, p, len);
-  copy[len] = 0;
-
-  num = sscanf (copy, "%" DOUBLEST_SCAN_FORMAT "%n", d, &n);
-  xfree (copy);
-
-  /* The sscanf man page suggests not making any assumptions on the effect
-     of %n on the result, so we don't.
-     That is why we simply test num == 0.  */
-  if (num == 0)
-    return 0;
-
-  *suffix = p + n;
-  return 1;
-}
-
-/* Parse floating point value P of length LEN, using the C syntax for floats.
-   Return 0 (false) if invalid, 1 (true) if valid.
-   The successfully parsed number is stored in *D.
-   Its type is taken from builtin_type (gdbarch) and is stored in *T.  */
-
-int
-parse_c_float (struct gdbarch *gdbarch, const char *p, int len,
-              DOUBLEST *d, struct type **t)
-{
-  const char *suffix;
-  int suffix_len;
-  const struct builtin_type *builtin_types = builtin_type (gdbarch);
-
-  if (! parse_float (p, len, d, &suffix))
-    return 0;
-
-  suffix_len = p + len - suffix;
-
-  if (suffix_len == 0)
-    *t = builtin_types->builtin_double;
-  else if (suffix_len == 1)
-    {
-      /* Handle suffixes: 'f' for float, 'l' for long double.  */
-      if (tolower (*suffix) == 'f')
-       *t = builtin_types->builtin_float;
-      else if (tolower (*suffix) == 'l')
-       *t = builtin_types->builtin_long_double;
-      else
-       return 0;
-    }
-  else
-    return 0;
-
-  return 1;
+  return target_float_from_string (data, type, std::string (p, len));
 }
 \f
 /* Stuff for maintaining a stack of types.  Currently just used by C, but
@@ -1808,8 +1739,7 @@ operator_check_standard (struct expression *exp, int pos,
     {
     case BINOP_VAL:
     case OP_COMPLEX:
-    case OP_DECFLOAT:
-    case OP_DOUBLE:
+    case OP_FLOAT:
     case OP_LONG:
     case OP_SCOPE:
     case OP_TYPE:
@@ -1932,10 +1862,11 @@ increase_expout_size (struct parser_state *ps, size_t lenelt)
   if ((ps->expout_ptr + lenelt) >= ps->expout_size)
     {
       ps->expout_size = std::max (ps->expout_size * 2,
-                            ps->expout_ptr + lenelt + 10);
-      ps->expout = (struct expression *)
-       xrealloc (ps->expout, (sizeof (struct expression)
-                              + EXP_ELEM_TO_BYTES (ps->expout_size)));
+                                 ps->expout_ptr + lenelt + 10);
+      ps->expout.reset (XRESIZEVAR (expression,
+                                   ps->expout.release (),
+                                   (sizeof (struct expression)
+                                    + EXP_ELEM_TO_BYTES (ps->expout_size))));
     }
 }