Move rust_language::lookup_symbol_nonlocal
[binutils-gdb.git] / gdb / rust-lang.c
index cca2baa6f63a3478c3a53ef3c00772971f5e9823..57bef01d3438f727b18f064ac1556b93fbcfc565 100644 (file)
@@ -29,7 +29,6 @@
 #include "gdbarch.h"
 #include "infcall.h"
 #include "objfiles.h"
-#include "psymtab.h"
 #include "rust-lang.h"
 #include "typeprint.h"
 #include "valprint.h"
@@ -58,7 +57,7 @@ rust_last_path_segment (const char *path)
 std::string
 rust_crate_for_block (const struct block *block)
 {
-  const char *scope = block_scope (block);
+  const char *scope = block->scope ();
 
   if (scope[0] == '\0')
     return std::string ();
@@ -94,7 +93,7 @@ rust_enum_variant (struct type *type)
 {
   /* The active variant is simply the first non-artificial field.  */
   for (int i = 0; i < type->num_fields (); ++i)
-    if (!TYPE_FIELD_ARTIFICIAL (type, i))
+    if (!type->field (i).is_artificial ())
       return i;
 
   /* Perhaps we could get here by trying to print an Ada variant
@@ -130,7 +129,7 @@ rust_underscore_fields (struct type *type)
     return false;
   for (i = 0; i < type->num_fields (); ++i)
     {
-      if (!field_is_static (&type->field (i)))
+      if (!type->field (i).is_static ())
        {
          char buf[20];
 
@@ -350,7 +349,7 @@ rust_val_print_slice (struct value *val, struct ui_file *stream, int recurse,
          struct type *array_type = lookup_array_range_type (elt_type, 0,
                                                             llen - 1);
          struct value *array = value::allocate_lazy (array_type);
-         VALUE_LVAL (array) = lval_memory;
+         array->set_lval (lval_memory);
          array->set_address (value_as_address (base));
          array->fetch_lazy ();
          generic_value_print (array, stream, recurse, options,
@@ -403,7 +402,7 @@ rust_language::val_print_struct
   first_field = 1;
   for (i = 0; i < type->num_fields (); ++i)
     {
-      if (field_is_static (&type->field (i)))
+      if (type->field (i).is_static ())
        continue;
 
       if (!first_field)
@@ -456,7 +455,7 @@ rust_language::print_enum (struct value *val, struct ui_file *stream,
 
   gdb_assert (rust_enum_p (type));
   gdb::array_view<const gdb_byte> view
-    (value_contents_for_printing (val).data (),
+    (val->contents_for_printing ().data (),
      val->type ()->length ());
   type = resolve_dynamic_type (type, view, val->address ());
 
@@ -470,7 +469,7 @@ rust_language::print_enum (struct value *val, struct ui_file *stream,
     }
 
   int variant_fieldno = rust_enum_variant (type);
-  val = value_field (val, variant_fieldno);
+  val = val->primitive_field (0, variant_fieldno, type);
   struct type *variant_type = type->field (variant_fieldno).type ();
 
   int nfields = variant_type->num_fields ();
@@ -585,7 +584,7 @@ rust_language::value_print_inner
           encoding.  */
        gdb_puts ("b", stream);
        printstr (stream, type->target_type (),
-                 value_contents_for_printing (val).data (),
+                 val->contents_for_printing ().data (),
                  high_bound - low_bound + 1, "ASCII", 0, &opts);
       }
       break;
@@ -723,9 +722,9 @@ rust_print_struct_def (struct type *type, const char *varstring,
   std::vector<int> fields;
   for (int i = 0; i < type->num_fields (); ++i)
     {
-      if (field_is_static (&type->field (i)))
+      if (type->field (i).is_static ())
        continue;
-      if (is_enum && TYPE_FIELD_ARTIFICIAL (type, i))
+      if (is_enum && type->field (i).is_artificial ())
        continue;
       fields.push_back (i);
     }
@@ -741,8 +740,8 @@ rust_print_struct_def (struct type *type, const char *varstring,
     {
       QUIT;
 
-      gdb_assert (!field_is_static (&type->field (i)));
-      gdb_assert (! (is_enum && TYPE_FIELD_ARTIFICIAL (type, i)));
+      gdb_assert (!type->field (i).is_static ());
+      gdb_assert (! (is_enum && type->field (i).is_artificial ()));
 
       if (flags->print_offsets)
        podata->update (type, i, stream);
@@ -939,7 +938,7 @@ rust_composite_type (struct type *original,
                     const char *field1, struct type *type1,
                     const char *field2, struct type *type2)
 {
-  struct type *result = alloc_type_copy (original);
+  struct type *result = type_allocator (original).new_type ();
   int i, nfields, bitpos;
 
   nfields = 0;
@@ -951,9 +950,7 @@ rust_composite_type (struct type *original,
   result->set_code (TYPE_CODE_STRUCT);
   result->set_name (name);
 
-  result->set_num_fields (nfields);
-  result->set_fields
-    ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
+  result->alloc_fields (nfields);
 
   i = 0;
   bitpos = 0;
@@ -1204,7 +1201,7 @@ rust_subscript (struct type *expect_type, struct expression *exp,
       else
        new_type = base_type;
 
-      return value::zero (new_type, VALUE_LVAL (lhs));
+      return value::zero (new_type, lhs->lval ());
     }
   else
     {
@@ -1339,14 +1336,7 @@ eval_op_rust_array (struct type *expect_type, struct expression *exp,
     error (_("Array with negative number of elements"));
 
   if (noside == EVAL_NORMAL)
-    {
-      int i;
-      std::vector<struct value *> eltvec (copies);
-
-      for (i = 0; i < copies; ++i)
-       eltvec[i] = elt;
-      return value_array (0, copies - 1, eltvec.data ());
-    }
+    return value_array (0, std::vector<value *> (copies, elt));
   else
     {
       struct type *arraytype
@@ -1374,7 +1364,7 @@ rust_struct_anon::evaluate (struct type *expect_type,
 
       if (rust_enum_p (type))
        {
-         type = resolve_dynamic_type (type, value_contents (lhs),
+         type = resolve_dynamic_type (type, lhs->contents (),
                                       lhs->address ());
 
          if (rust_empty_enum_p (type))
@@ -1382,7 +1372,7 @@ rust_struct_anon::evaluate (struct type *expect_type,
                   field_number, type->name ());
 
          int fieldno = rust_enum_variant (type);
-         lhs = value_primitive_field (lhs, 0, fieldno, type);
+         lhs = lhs->primitive_field (0, fieldno, type);
          outer_type = type;
          type = lhs->type ();
        }
@@ -1418,7 +1408,7 @@ rust_struct_anon::evaluate (struct type *expect_type,
                  field_number, type->name ());
        }
 
-      return value_primitive_field (lhs, 0, field_number, type);
+      return lhs->primitive_field (0, field_number, type);
     }
   else
     error(_("Anonymous field access is only allowed on tuples, \
@@ -1437,7 +1427,7 @@ rust_structop::evaluate (struct type *expect_type,
   struct type *type = lhs->type ();
   if (type->code () == TYPE_CODE_STRUCT && rust_enum_p (type))
     {
-      type = resolve_dynamic_type (type, value_contents (lhs),
+      type = resolve_dynamic_type (type, lhs->contents (),
                                   lhs->address ());
 
       if (rust_empty_enum_p (type))
@@ -1445,7 +1435,7 @@ rust_structop::evaluate (struct type *expect_type,
               field_name, type->name ());
 
       int fieldno = rust_enum_variant (type);
-      lhs = value_primitive_field (lhs, 0, fieldno, type);
+      lhs = lhs->primitive_field (0, fieldno, type);
 
       struct type *outer_type = type;
       type = lhs->type ();
@@ -1470,7 +1460,7 @@ rust_structop::evaluate (struct type *expect_type,
   else
     result = value_struct_elt (&lhs, {}, field_name, NULL, "structure");
   if (noside == EVAL_AVOID_SIDE_EFFECTS)
-    result = value::zero (result->type (), VALUE_LVAL (result));
+    result = value::zero (result->type (), result->lval ());
   return result;
 }
 
@@ -1594,27 +1584,30 @@ rust_language::language_arch_info (struct gdbarch *gdbarch,
     return t;
   };
 
+  type_allocator alloc (gdbarch);
   struct type *bool_type
-    = add (arch_boolean_type (gdbarch, 8, 1, "bool"));
-  add (arch_character_type (gdbarch, 32, 1, "char"));
-  add (arch_integer_type (gdbarch, 8, 0, "i8"));
+    = add (init_boolean_type (alloc, 8, 1, "bool"));
+  add (init_character_type (alloc, 32, 1, "char"));
+  add (init_integer_type (alloc, 8, 0, "i8"));
   struct type *u8_type
-    = add (arch_integer_type (gdbarch, 8, 1, "u8"));
-  add (arch_integer_type (gdbarch, 16, 0, "i16"));
-  add (arch_integer_type (gdbarch, 16, 1, "u16"));
-  add (arch_integer_type (gdbarch, 32, 0, "i32"));
-  add (arch_integer_type (gdbarch, 32, 1, "u32"));
-  add (arch_integer_type (gdbarch, 64, 0, "i64"));
-  add (arch_integer_type (gdbarch, 64, 1, "u64"));
+    = add (init_integer_type (alloc, 8, 1, "u8"));
+  add (init_integer_type (alloc, 16, 0, "i16"));
+  add (init_integer_type (alloc, 16, 1, "u16"));
+  add (init_integer_type (alloc, 32, 0, "i32"));
+  add (init_integer_type (alloc, 32, 1, "u32"));
+  add (init_integer_type (alloc, 64, 0, "i64"));
+  add (init_integer_type (alloc, 64, 1, "u64"));
+  add (init_integer_type (alloc, 128, 0, "i128"));
+  add (init_integer_type (alloc, 128, 1, "u128"));
 
   unsigned int length = 8 * builtin->builtin_data_ptr->length ();
-  add (arch_integer_type (gdbarch, length, 0, "isize"));
+  add (init_integer_type (alloc, length, 0, "isize"));
   struct type *usize_type
-    = add (arch_integer_type (gdbarch, length, 1, "usize"));
+    = add (init_integer_type (alloc, length, 1, "usize"));
 
-  add (arch_float_type (gdbarch, 32, "f32", floatformats_ieee_single));
-  add (arch_float_type (gdbarch, 64, "f64", floatformats_ieee_double));
-  add (arch_integer_type (gdbarch, 0, 1, "()"));
+  add (init_float_type (alloc, 32, "f32", floatformats_ieee_single));
+  add (init_float_type (alloc, 64, "f64", floatformats_ieee_double));
+  add (init_integer_type (alloc, 0, 1, "()"));
 
   struct type *tem = make_cv_type (1, 0, u8_type, NULL);
   add (rust_slice_type ("&str", tem, usize_type));
@@ -1682,6 +1675,43 @@ rust_language::is_string_type_p (struct type *type) const
              && strcmp (type->name (), "&str") == 0));
 }
 
+/* See language.h.  */
+
+struct block_symbol
+rust_language::lookup_symbol_nonlocal
+     (const char *name, const struct block *block,
+      const domain_enum domain) const
+{
+  struct block_symbol result = {};
+
+  const char *scope = block == nullptr ? "" : block->scope ();
+  symbol_lookup_debug_printf
+    ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
+     name, host_address_to_string (block), scope,
+     domain_name (domain));
+
+  /* Look up bare names in the block's scope.  */
+  std::string scopedname;
+  if (name[cp_find_first_component (name)] == '\0')
+    {
+      if (scope[0] != '\0')
+       {
+         scopedname = std::string (scope) + "::" + name;
+         name = scopedname.c_str ();
+       }
+      else
+       name = NULL;
+    }
+
+  if (name != NULL)
+    {
+      result = lookup_symbol_in_static_block (name, block, domain);
+      if (result.symbol == NULL)
+       result = lookup_global_symbol (name, block, domain);
+    }
+  return result;
+}
+
 /* Single instance of the Rust language class.  */
 
 static rust_language rust_language_defn;