/* Rust language support routines for GDB, the GNU debugger.
- Copyright (C) 2016-2018 Free Software Foundation, Inc.
+ Copyright (C) 2016-2020 Free Software Foundation, Inc.
This file is part of GDB.
#include <algorithm>
#include <string>
#include <vector>
+#include "cli/cli-style.h"
/* See rust-lang.h. */
&& TYPE_FLAG_DISCRIMINATED_UNION (TYPE_FIELD_TYPE (type, 0)));
}
+/* Return true if TYPE, which must be an enum type, has no
+ variants. */
+
+static bool
+rust_empty_enum_p (const struct type *type)
+{
+ gdb_assert (rust_enum_p (type));
+ /* In Rust the enum always fills the containing structure. */
+ gdb_assert (TYPE_FIELD_BITPOS (type, 0) == 0);
+
+ return TYPE_NFIELDS (TYPE_FIELD_TYPE (type, 0)) == 0;
+}
+
/* Given an enum type and contents, find which variant is active. */
-struct field *
+static struct field *
rust_enum_variant (struct type *type, const gdb_byte *contents)
{
/* In Rust the enum always fills the containing structure. */
&& TYPE_UNSIGNED (type));
}
+/* Return true if TYPE is a string type. */
+
+static bool
+rust_is_string_type_p (struct type *type)
+{
+ LONGEST low_bound, high_bound;
+
+ type = check_typedef (type);
+ return ((TYPE_CODE (type) == TYPE_CODE_STRING)
+ || (TYPE_CODE (type) == TYPE_CODE_PTR
+ && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
+ && rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
+ && get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
+ &high_bound)))
+ || (TYPE_CODE (type) == TYPE_CODE_STRUCT
+ && !rust_enum_p (type)
+ && rust_slice_type_p (type)
+ && strcmp (TYPE_NAME (type), "&str") == 0));
+}
+
/* If VALUE represents a trait object pointer, return the underlying
pointer with the correct (i.e., runtime) type. Otherwise, return
NULL. */
\f
+static void rust_value_print_inner (struct value *val, struct ui_file *stream,
+ int recurse,
+ const struct value_print_options *options);
+
/* Helper function to print a string slice. */
static void
/* rust_val_print helper for structs and untagged unions. */
static void
-val_print_struct (struct type *type, int embedded_offset,
- CORE_ADDR address, struct ui_file *stream,
- int recurse, struct value *val,
+val_print_struct (struct value *val, struct ui_file *stream, int recurse,
const struct value_print_options *options)
{
int i;
int first_field;
+ struct type *type = check_typedef (value_type (val));
if (rust_slice_type_p (type) && strcmp (TYPE_NAME (type), "&str") == 0)
{
+ /* If what we are printing here is actually a string within a
+ structure then VAL will be the original parent value, while TYPE
+ will be the type of the structure representing the string we want
+ to print.
+ However, RUST_VAL_PRINT_STR looks up the fields of the string
+ inside VAL, assuming that VAL is the string.
+ So, recreate VAL as a value representing just the string. */
+ val = value_at_lazy (type, value_address (val));
rust_val_print_str (stream, val, options);
return;
}
if (!is_tuple && !is_tuple_struct)
{
- fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
+ fputs_styled (TYPE_FIELD_NAME (type, i),
+ variable_name_style.style (), stream);
fputs_filtered (": ", stream);
}
- val_print (TYPE_FIELD_TYPE (type, i),
- embedded_offset + TYPE_FIELD_BITPOS (type, i) / 8,
- address,
- stream, recurse + 1, val, &opts,
- current_language);
+ rust_value_print_inner (value_field (val, i), stream, recurse + 1,
+ &opts);
}
if (options->prettyformat)
/* rust_val_print helper for discriminated unions (Rust enums). */
static void
-rust_print_enum (struct type *type, int embedded_offset,
- CORE_ADDR address, struct ui_file *stream,
- int recurse, struct value *val,
+rust_print_enum (struct value *val, struct ui_file *stream, int recurse,
const struct value_print_options *options)
{
struct value_print_options opts = *options;
+ struct type *type = check_typedef (value_type (val));
opts.deref_ref = 0;
+ if (rust_empty_enum_p (type))
+ {
+ /* Print the enum type name here to be more clear. */
+ fprintf_filtered (stream, _("%s {%p[<No data fields>%p]}"),
+ TYPE_NAME (type),
+ metadata_style.style ().ptr (), nullptr);
+ return;
+ }
+
const gdb_byte *valaddr = value_contents_for_printing (val);
struct field *variant_field = rust_enum_variant (type, valaddr);
- embedded_offset += FIELD_BITPOS (*variant_field) / 8;
struct type *variant_type = FIELD_TYPE (*variant_field);
int nfields = TYPE_NFIELDS (variant_type);
fprintf_filtered (stream, "{");
}
+ struct value *union_value = value_field (val, 0);
+ int fieldno = (variant_field - &TYPE_FIELD (value_type (union_value), 0));
+ val = value_field (union_value, fieldno);
+
bool first_field = true;
for (int j = 0; j < TYPE_NFIELDS (variant_type); j++)
{
first_field = false;
if (!is_tuple)
- fprintf_filtered (stream, "%s: ",
- TYPE_FIELD_NAME (variant_type, j));
-
- val_print (TYPE_FIELD_TYPE (variant_type, j),
- (embedded_offset
- + TYPE_FIELD_BITPOS (variant_type, j) / 8),
- address,
- stream, recurse + 1, val, &opts,
- current_language);
+ fprintf_filtered (stream, "%ps: ",
+ styled_string (variable_name_style.style (),
+ TYPE_FIELD_NAME (variant_type, j)));
+
+ rust_value_print_inner (value_field (val, j), stream, recurse + 1,
+ &opts);
}
if (is_tuple)
"]"
};
-/* la_val_print implementation for Rust. */
-
+/* la_value_print_inner implementation for Rust. */
static void
-rust_val_print (struct type *type, int embedded_offset,
- CORE_ADDR address, struct ui_file *stream, int recurse,
- struct value *val,
- const struct value_print_options *options)
+rust_value_print_inner (struct value *val, struct ui_file *stream,
+ int recurse,
+ const struct value_print_options *options)
{
- const gdb_byte *valaddr = value_contents_for_printing (val);
+ struct value_print_options opts = *options;
+ opts.deref_ref = 1;
- type = check_typedef (type);
+ if (opts.prettyformat == Val_prettyformat_default)
+ opts.prettyformat = (opts.prettyformat_structs
+ ? Val_prettyformat : Val_no_prettyformat);
+
+ struct type *type = check_typedef (value_type (val));
switch (TYPE_CODE (type))
{
case TYPE_CODE_PTR:
if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_ARRAY
&& rust_u8_type_p (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (type)))
&& get_array_bounds (TYPE_TARGET_TYPE (type), &low_bound,
- &high_bound)) {
- /* We have a pointer to a byte string, so just print
- that. */
- struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
- CORE_ADDR addr;
- struct gdbarch *arch = get_type_arch (type);
- int unit_size = gdbarch_addressable_memory_unit_size (arch);
+ &high_bound))
+ {
+ /* We have a pointer to a byte string, so just print
+ that. */
+ struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
+ CORE_ADDR addr = value_as_address (val);
+ struct gdbarch *arch = get_type_arch (type);
- addr = unpack_pointer (type, valaddr + embedded_offset * unit_size);
- if (options->addressprint)
- {
- fputs_filtered (paddress (arch, addr), stream);
- fputs_filtered (" ", stream);
- }
+ if (opts.addressprint)
+ {
+ fputs_filtered (paddress (arch, addr), stream);
+ fputs_filtered (" ", stream);
+ }
- fputs_filtered ("b", stream);
- val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
- high_bound - low_bound + 1, stream,
- options);
- break;
- }
+ fputs_filtered ("b", stream);
+ val_print_string (TYPE_TARGET_TYPE (elttype), "ASCII", addr,
+ high_bound - low_bound + 1, stream,
+ &opts);
+ break;
+ }
}
- /* Fall through. */
+ goto generic_print;
case TYPE_CODE_METHODPTR:
case TYPE_CODE_MEMBERPTR:
- c_val_print (type, embedded_offset, address, stream,
- recurse, val, options);
+ c_value_print_inner (val, stream, recurse, &opts);
break;
case TYPE_CODE_INT:
case TYPE_CODE_STRING:
{
- struct gdbarch *arch = get_type_arch (type);
- int unit_size = gdbarch_addressable_memory_unit_size (arch);
LONGEST low_bound, high_bound;
if (!get_array_bounds (type, &low_bound, &high_bound))
encoding. */
fputs_filtered ("b", stream);
rust_printstr (stream, TYPE_TARGET_TYPE (type),
- valaddr + embedded_offset * unit_size,
- high_bound - low_bound + 1, "ASCII", 0, options);
+ value_contents_for_printing (val),
+ high_bound - low_bound + 1, "ASCII", 0, &opts);
}
break;
for printing a union is same as that for a struct, the only
difference is that the input type will have overlapping
fields. */
- val_print_struct (type, embedded_offset, address, stream,
- recurse, val, options);
+ val_print_struct (val, stream, recurse, &opts);
break;
case TYPE_CODE_STRUCT:
if (rust_enum_p (type))
- rust_print_enum (type, embedded_offset, address, stream,
- recurse, val, options);
+ rust_print_enum (val, stream, recurse, &opts);
else
- val_print_struct (type, embedded_offset, address, stream,
- recurse, val, options);
+ val_print_struct (val, stream, recurse, &opts);
break;
default:
generic_print:
/* Nothing special yet. */
- generic_val_print (type, embedded_offset, address, stream,
- recurse, val, options, &rust_decorations);
+ generic_value_print (val, stream, recurse, &opts, &rust_decorations);
}
}
if (is_enum)
{
fputs_filtered ("enum ", stream);
+
+ if (rust_empty_enum_p (type))
+ {
+ if (tagname != NULL)
+ {
+ fputs_filtered (tagname, stream);
+ fputs_filtered (" ", stream);
+ }
+ fputs_filtered ("{}", stream);
+ return;
+ }
+
type = TYPE_FIELD_TYPE (type, 0);
struct dynamic_prop *discriminant_prop
if (!for_rust_enum || flags->print_offsets)
print_spaces_filtered (level + 2, stream);
if (is_enum)
- fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
+ fputs_styled (TYPE_FIELD_NAME (type, i), variable_name_style.style (),
+ stream);
else if (!is_tuple_struct)
- fprintf_filtered (stream, "%s: ", TYPE_FIELD_NAME (type, i));
+ fprintf_filtered (stream, "%ps: ",
+ styled_string (variable_name_style.style (),
+ TYPE_FIELD_NAME (type, i)));
rust_internal_print_type (TYPE_FIELD_TYPE (type, i), NULL,
stream, (is_enum ? show : show - 1),
struct ui_file *stream)
{
type = check_typedef (type);
- fprintf_filtered (stream, "type %s = ", SYMBOL_PRINT_NAME (new_symbol));
+ fprintf_filtered (stream, "type %s = ", new_symbol->print_name ());
type_print (type, "", stream, 0);
- fprintf_filtered (stream, ";\n");
+ fprintf_filtered (stream, ";");
}
/* la_print_type implementation for Rust. */
const struct type_print_options *flags,
bool for_rust_enum, print_offset_data *podata)
{
- int i;
-
QUIT;
if (show <= 0
&& TYPE_NAME (type) != NULL)
if (varstring != NULL)
fputs_filtered (varstring, stream);
fputs_filtered ("(", stream);
- for (i = 0; i < TYPE_NFIELDS (type); ++i)
+ for (int i = 0; i < TYPE_NFIELDS (type); ++i)
{
QUIT;
if (i > 0)
case TYPE_CODE_ENUM:
{
- int i, len = 0;
+ int len = 0;
fputs_filtered ("enum ", stream);
if (TYPE_NAME (type) != NULL)
}
fputs_filtered ("{\n", stream);
- for (i = 0; i < TYPE_NFIELDS (type); ++i)
+ for (int i = 0; i < TYPE_NFIELDS (type); ++i)
{
const char *name = TYPE_FIELD_NAME (type, i);
&& name[len] == ':'
&& name[len + 1] == ':')
name += len + 2;
- fprintfi_filtered (level + 2, stream, "%s,\n", name);
+ fprintfi_filtered (level + 2, stream, "%ps,\n",
+ styled_string (variable_name_style.style (),
+ name));
}
fputs_filtered ("}", stream);
}
break;
+ case TYPE_CODE_PTR:
+ {
+ if (TYPE_NAME (type) != nullptr)
+ fputs_filtered (TYPE_NAME (type), stream);
+ else
+ {
+ /* We currently can't distinguish between pointers and
+ references. */
+ fputs_filtered ("*mut ", stream);
+ type_print (TYPE_TARGET_TYPE (type), "", stream, 0);
+ }
+ }
+ break;
+
default:
c_printer:
c_print_type (type, varstring, stream, show, level, flags);
if (noside == EVAL_AVOID_SIDE_EFFECTS)
result = value_zero (TYPE_TARGET_TYPE (fn_type), not_lval);
else
- result = call_function_by_hand (function, NULL, num_args + 1, args.data ());
+ result = call_function_by_hand (function, NULL, args);
return result;
}
case OP_RUST_ARRAY:
{
- int pc = (*pos)++;
+ (*pos)++;
int copies;
struct value *elt;
struct value *ncopies;
/* Anonymous field access, i.e. foo.1. */
struct value *lhs;
int pc, field_number, nfields;
- struct type *type, *variant_type;
+ struct type *type;
pc = (*pos)++;
field_number = longest_to_int (exp->elts[pc + 1].longconst);
if (rust_enum_p (type))
{
+ if (rust_empty_enum_p (type))
+ error (_("Cannot access field %d of empty enum %s"),
+ field_number, TYPE_NAME (type));
+
const gdb_byte *valaddr = value_contents (lhs);
struct field *variant_field = rust_enum_variant (type, valaddr);
type = value_type (lhs);
if (TYPE_CODE (type) == TYPE_CODE_STRUCT && rust_enum_p (type))
{
+ if (rust_empty_enum_p (type))
+ error (_("Cannot access field %s of empty enum %s"),
+ field_name, TYPE_NAME (type));
+
const gdb_byte *valaddr = value_contents (lhs);
struct field *variant_field = rust_enum_variant (type, valaddr);
struct type *outer_type = type;
type = value_type (lhs);
if (rust_tuple_type_p (type) || rust_tuple_struct_type_p (type))
- error (_("Attempting to access named field foo of tuple "
+ error (_("Attempting to access named field %s of tuple "
"variant %s::%s, which has only anonymous fields"),
- TYPE_NAME (outer_type),
+ field_name, TYPE_NAME (outer_type),
rust_last_path_segment (TYPE_NAME (type)));
- TRY
+ try
{
result = value_struct_elt (&lhs, NULL, field_name,
NULL, "structure");
}
- CATCH (except, RETURN_MASK_ERROR)
+ catch (const gdb_exception_error &except)
{
error (_("Could not find field %s of struct variant %s::%s"),
field_name, TYPE_NAME (outer_type),
rust_last_path_segment (TYPE_NAME (type)));
}
- END_CATCH
}
else
result = value_struct_elt (&lhs, NULL, field_name, NULL, "structure");
const struct block *block,
const domain_enum domain)
{
- struct block_symbol result = {NULL, NULL};
+ struct block_symbol result = {};
if (symbol_lookup_debug)
{
rust_emitchar, /* Print a single char */
rust_print_type, /* Print a type using appropriate syntax */
rust_print_typedef, /* Print a typedef using appropriate syntax */
- rust_val_print, /* Print a value using appropriate syntax */
+ rust_value_print_inner, /* la_value_print_inner */
c_value_print, /* Print a top-level value */
default_read_var_value, /* la_read_var_value */
NULL, /* Language specific skip_trampoline */
rust_language_arch_info,
default_print_array_index,
default_pass_by_reference,
- c_get_string,
rust_watch_location_expression,
NULL, /* la_get_symbol_name_matcher */
iterate_over_symbols,
&default_varobj_ops,
NULL,
NULL,
- LANG_MAGIC
+ rust_is_string_type_p,
+ "{...}" /* la_struct_too_deep_ellipsis */
};