Add `set print array-indexes' tests for C/C++ arrays
[binutils-gdb.git] / gdb / ada-exp.y
index 3652376b62329d0fc9d15af0e9bc8ef1b9d9079b..16ba336df4aa45b93c8368fa812e4b3e991978dd 100644 (file)
@@ -1,5 +1,5 @@
 /* YACC parser for Ada expressions, for GDB.
-   Copyright (C) 1986-2021 Free Software Foundation, Inc.
+   Copyright (C) 1986-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -68,10 +68,6 @@ struct name_info {
 
 static struct parser_state *pstate = NULL;
 
-/* If expression is in the context of TYPE'(...), then TYPE, else
- * NULL.  */
-static struct type *type_qualifier;
-
 int yyparse (void);
 
 static int yylex (void);
@@ -91,10 +87,8 @@ static void write_name_assoc (struct parser_state *, struct stoken);
 
 static const struct block *block_lookup (const struct block *, const char *);
 
-static LONGEST convert_char_literal (struct type *, LONGEST);
-
 static void write_ambiguous_var (struct parser_state *,
-                                const struct block *, char *, int);
+                                const struct block *, const char *, int);
 
 static struct type *type_int (struct parser_state *);
 
@@ -119,16 +113,13 @@ resolve (operation_up &&op, bool deprocedure_p, struct type *context_type)
 {
   operation_up result = std::move (op);
   ada_resolvable *res = dynamic_cast<ada_resolvable *> (result.get ());
-  if (res != nullptr
-      && res->resolve (pstate->expout.get (),
-                      deprocedure_p,
-                      pstate->parse_completion,
-                      pstate->block_tracker,
-                      context_type))
-    result
-      = make_operation<ada_funcall_operation> (std::move (result),
-                                              std::vector<operation_up> ());
-
+  if (res != nullptr)
+    return res->replace (std::move (result),
+                        pstate->expout.get (),
+                        deprocedure_p,
+                        pstate->parse_completion,
+                        pstate->block_tracker,
+                        context_type);
   return result;
 }
 
@@ -433,8 +424,6 @@ pop_associations (int n)
 %type <bval> block
 %type <lval> arglist tick_arglist
 
-%type <tval> save_qualifier
-
 %token DOT_ALL
 
 /* Special type cases, put in to allow the parser to distinguish different
@@ -521,8 +510,7 @@ primary :   primary '(' arglist ')'
                        }
        ;
 
-primary :      var_or_type '\'' save_qualifier { type_qualifier = $1; } 
-                  '(' exp ')'
+primary :      var_or_type '\'' '(' exp ')'
                        {
                          if ($1 == NULL)
                            error (_("Type required for qualification"));
@@ -530,13 +518,9 @@ primary :  var_or_type '\'' save_qualifier { type_qualifier = $1; }
                                                      check_typedef ($1));
                          pstate->push_new<ada_qual_operation>
                            (std::move (arg), $1);
-                         type_qualifier = $3;
                        }
        ;
 
-save_qualifier :       { $$ = type_qualifier; }
-       ;
-
 primary :
                primary '(' simple_exp DOTDOT simple_exp ')'
                        { ada_wrap3<ada_ternop_slice_operation> (); }
@@ -872,11 +856,9 @@ primary    :       INT
        ;
 
 primary        :       CHARLIT
-                 { write_int (pstate,
-                              convert_char_literal (type_qualifier, $1.val),
-                              (type_qualifier == NULL) 
-                              ? $1.type : type_qualifier);
-                 }
+                       {
+                         pstate->push_new<ada_char_operation> ($1.type, $1.val);
+                       }
        ;
 
 primary        :       FLOAT
@@ -1100,7 +1082,6 @@ ada_parse (struct parser_state *par_state)
   pstate = par_state;
 
   lexer_init (yyin);           /* (Re-)initialize lexer.  */
-  type_qualifier = NULL;
   obstack_free (&temp_parse_space, NULL);
   obstack_init (&temp_parse_space);
   components.clear ();
@@ -1145,15 +1126,15 @@ write_int (struct parser_state *par_state, LONGEST arg, struct type *type)
 }
 
 /* Emit expression corresponding to the renamed object named 
* designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
* context of ORIG_LEFT_CONTEXT, to which is applied the operations
* encoded by RENAMING_EXPR.  MAX_DEPTH is the maximum number of
* cascaded renamings to allow.  If ORIG_LEFT_CONTEXT is null, it
* defaults to the currently selected block. ORIG_SYMBOL is the 
* symbol that originally encoded the renaming.  It is needed only
* because its prefix also qualifies any index variables used to index
* or slice an array.  It should not be necessary once we go to the
* new encoding entirely (FIXME pnh 7/20/2007).  */
  designated by RENAMED_ENTITY[0 .. RENAMED_ENTITY_LEN-1] in the
  context of ORIG_LEFT_CONTEXT, to which is applied the operations
  encoded by RENAMING_EXPR.  MAX_DEPTH is the maximum number of
  cascaded renamings to allow.  If ORIG_LEFT_CONTEXT is null, it
  defaults to the currently selected block. ORIG_SYMBOL is the 
  symbol that originally encoded the renaming.  It is needed only
  because its prefix also qualifies any index variables used to index
  or slice an array.  It should not be necessary once we go to the
  new encoding entirely (FIXME pnh 7/20/2007).  */
 
 static void
 write_object_renaming (struct parser_state *par_state,
@@ -1400,7 +1381,7 @@ find_primitive_type (struct parser_state *par_state, const char *name)
 }
 
 static int
-chop_selector (char *name, int end)
+chop_selector (const char *name, int end)
 {
   int i;
   for (i = end - 1; i > 0; i -= 1)
@@ -1413,8 +1394,8 @@ chop_selector (char *name, int end)
    '.'), chop this separator and return the result; else, return
    NAME.  */
 
-static char *
-chop_separator (char *name)
+static const char *
+chop_separator (const char *name)
 {
   if (*name == '.')
    return name + 1;
@@ -1429,11 +1410,11 @@ chop_separator (char *name)
    <sep> is '__' or '.', write the indicated sequence of
    STRUCTOP_STRUCT expression operators. */
 static void
-write_selectors (struct parser_state *par_state, char *sels)
+write_selectors (struct parser_state *par_state, const char *sels)
 {
   while (*sels != '\0')
     {
-      char *p = chop_separator (sels);
+      const char *p = chop_separator (sels);
       sels = p;
       while (*sels != '\0' && *sels != '.' 
             && (sels[0] != '_' || sels[1] != '_'))
@@ -1450,7 +1431,7 @@ write_selectors (struct parser_state *par_state, char *sels)
    */
 static void
 write_ambiguous_var (struct parser_state *par_state,
-                    const struct block *block, char *name, int len)
+                    const struct block *block, const char *name, int len)
 {
   struct symbol *sym = new (&temp_parse_space) symbol ();
 
@@ -1489,10 +1470,10 @@ ada_nget_field_index (const struct type *type, const char *field_name0,
    In case of failure, we return NULL.  */
 
 static struct type *
-get_symbol_field_type (struct symbol *sym, char *encoded_field_name)
+get_symbol_field_type (struct symbol *sym, const char *encoded_field_name)
 {
-  char *field_name = encoded_field_name;
-  char *subfield_name;
+  const char *field_name = encoded_field_name;
+  const char *subfield_name;
   struct type *type = SYMBOL_TYPE (sym);
   int fieldno;
 
@@ -1721,43 +1702,6 @@ write_name_assoc (struct parser_state *par_state, struct stoken name)
   push_association<ada_name_association> (ada_pop ());
 }
 
-/* Convert the character literal whose ASCII value would be VAL to the
-   appropriate value of type TYPE, if there is a translation.
-   Otherwise return VAL.  Hence, in an enumeration type ('A', 'B'),
-   the literal 'A' (VAL == 65), returns 0.  */
-
-static LONGEST
-convert_char_literal (struct type *type, LONGEST val)
-{
-  char name[7];
-  int f;
-
-  if (type == NULL)
-    return val;
-  type = check_typedef (type);
-  if (type->code () != TYPE_CODE_ENUM)
-    return val;
-
-  if ((val >= 'a' && val <= 'z') || (val >= '0' && val <= '9'))
-    xsnprintf (name, sizeof (name), "Q%c", (int) val);
-  else
-    xsnprintf (name, sizeof (name), "QU%02x", (int) val);
-  size_t len = strlen (name);
-  for (f = 0; f < type->num_fields (); f += 1)
-    {
-      /* Check the suffix because an enum constant in a package will
-        have a name like "pkg__QUxx".  This is safe enough because we
-        already have the correct type, and because mangling means
-        there can't be clashes.  */
-      const char *ename = TYPE_FIELD_NAME (type, f);
-      size_t elen = strlen (ename);
-
-      if (elen >= len && strcmp (name, ename + elen - len) == 0)
-       return TYPE_FIELD_ENUMVAL (type, f);
-    }
-  return val;
-}
-
 static struct type *
 type_int (struct parser_state *par_state)
 {