From 22c12a6c70a0bf1c292478436f12df0a2d84c2a2 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Sun, 5 Jul 2020 09:29:34 +0100 Subject: [PATCH] gdb: Convert language_data::string_lower_bound to a method Convert language_data::string_lower_bound member variable to a virtual method language_defn::string_lower_bound. Over all of the languages we currently support there are currently only two values for the lower bound, 0 or 1. I noticed that in all cases, if a language has C style arrays then the lower bound is 0, otherwise the lower bound is 1. So the default for the virtual method in language.h makes use of this, which means languages don't have to worry about providing a string_lower_bound method at all. Except for Modula2. This language is defined to not have C style arrays, but has a string_lower_bound index of 0, this behaviour is maintained after this commit by having Modula2 be the only language that overrides the string_lower_bound method. There should be no user visible changes after this commit. gdb/ChangeLog: * ada-lang.c (ada_language_data): Remove string_lower_bound initializer. * c-lang.c (c_language_data): Likewise. (cplus_language_data): Likewise. (asm_language_data): Likewise. (minimal_language_data): Likewise. * d-lang.c (d_language_data): Likewise. * f-lang.c (f_language_data): Likewise. * go-lang.c (go_language_data): Likewise. * language.c (unknown_language_data): Likewise. (auto_language_data): Likewise. * language.h (language_data): Remove string_lower_bound field. (language_defn::string_lower_bound): New member function. * m2-lang.c (m2_language_data): Remove string_lower_bound initializer. (m2_language::string_lower_bound): New member function. * objc-lang.c (objc_language_data): Remove string_lower_bound initializer. * opencl-lang.c (opencl_language_data): Likewise. * p-lang.c (pascal_language_data): Likewise. * rust-lang.c (rust_language_data): Likewise. * valops.c (value_cstring): Update call to string_lower_bound. (value_string): Likewise. * value.c (allocate_repeated_value): Likewise. --- gdb/ChangeLog | 27 +++++++++++++++++++++++++++ gdb/ada-lang.c | 1 - gdb/c-lang.c | 4 ---- gdb/d-lang.c | 1 - gdb/f-lang.c | 1 - gdb/go-lang.c | 1 - gdb/language.c | 2 -- gdb/language.h | 11 ++++++++--- gdb/m2-lang.c | 7 ++++++- gdb/objc-lang.c | 1 - gdb/opencl-lang.c | 1 - gdb/p-lang.c | 1 - gdb/rust-lang.c | 1 - gdb/valops.c | 4 ++-- gdb/value.c | 5 ++++- 15 files changed, 47 insertions(+), 21 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 741e9bffd03..a337c605fc0 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,30 @@ +2020-09-16 Andrew Burgess + + * ada-lang.c (ada_language_data): Remove string_lower_bound + initializer. + * c-lang.c (c_language_data): Likewise. + (cplus_language_data): Likewise. + (asm_language_data): Likewise. + (minimal_language_data): Likewise. + * d-lang.c (d_language_data): Likewise. + * f-lang.c (f_language_data): Likewise. + * go-lang.c (go_language_data): Likewise. + * language.c (unknown_language_data): Likewise. + (auto_language_data): Likewise. + * language.h (language_data): Remove string_lower_bound field. + (language_defn::string_lower_bound): New member function. + * m2-lang.c (m2_language_data): Remove string_lower_bound + initializer. + (m2_language::string_lower_bound): New member function. + * objc-lang.c (objc_language_data): Remove string_lower_bound + initializer. + * opencl-lang.c (opencl_language_data): Likewise. + * p-lang.c (pascal_language_data): Likewise. + * rust-lang.c (rust_language_data): Likewise. + * valops.c (value_cstring): Update call to string_lower_bound. + (value_string): Likewise. + * value.c (allocate_repeated_value): Likewise. + 2020-09-16 Andrew Burgess * valops.c (value_repeat): Fix incorrect argument name in comment. diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 859dcec84ba..b90e7a05bcd 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -13716,7 +13716,6 @@ extern const struct language_data ada_language_data = &ada_exp_descriptor, true, /* la_store_sym_names_in_linkage_form_p */ ada_op_print_tab, /* expression operators for printing */ - 1, /* String lower bound */ &ada_varobj_ops, }; diff --git a/gdb/c-lang.c b/gdb/c-lang.c index 41eac2d43a3..ecb339c3f87 100644 --- a/gdb/c-lang.c +++ b/gdb/c-lang.c @@ -882,7 +882,6 @@ extern const struct language_data c_language_data = &exp_descriptor_c, true, /* la_store_sym_names_in_linkage_form_p */ c_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &c_varobj_ops, }; @@ -989,7 +988,6 @@ extern const struct language_data cplus_language_data = &exp_descriptor_c, false, /* la_store_sym_names_in_linkage_form_p */ c_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &cplus_varobj_ops, }; @@ -1199,7 +1197,6 @@ extern const struct language_data asm_language_data = &exp_descriptor_c, true, /* la_store_sym_names_in_linkage_form_p */ c_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; @@ -1267,7 +1264,6 @@ extern const struct language_data minimal_language_data = &exp_descriptor_c, true, /* la_store_sym_names_in_linkage_form_p */ c_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; diff --git a/gdb/d-lang.c b/gdb/d-lang.c index 67912089989..f75a973fbfb 100644 --- a/gdb/d-lang.c +++ b/gdb/d-lang.c @@ -135,7 +135,6 @@ extern const struct language_data d_language_data = &exp_descriptor_c, false, /* la_store_sym_names_in_linkage_form_p */ d_op_print_tab, /* Expression operators for printing. */ - 0, /* String lower bound. */ &default_varobj_ops, }; diff --git a/gdb/f-lang.c b/gdb/f-lang.c index 649716ba106..344bdb8f0c8 100644 --- a/gdb/f-lang.c +++ b/gdb/f-lang.c @@ -493,7 +493,6 @@ extern const struct language_data f_language_data = &exp_descriptor_f, false, /* la_store_sym_names_in_linkage_form_p */ f_op_print_tab, /* expression operators for printing */ - 1, /* String lower bound */ &default_varobj_ops, }; diff --git a/gdb/go-lang.c b/gdb/go-lang.c index c2724e3d7cc..c0ad0be1362 100644 --- a/gdb/go-lang.c +++ b/gdb/go-lang.c @@ -515,7 +515,6 @@ extern const struct language_data go_language_data = &exp_descriptor_c, false, /* la_store_sym_names_in_linkage_form_p */ go_op_print_tab, /* Expression operators for printing. */ - 0, /* String lower bound. */ &default_varobj_ops, }; diff --git a/gdb/language.c b/gdb/language.c index 9a496ae8548..1866a964599 100644 --- a/gdb/language.c +++ b/gdb/language.c @@ -782,7 +782,6 @@ extern const struct language_data unknown_language_data = &exp_descriptor_standard, true, /* store_sym_names_in_linkage_form_p */ unk_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; @@ -916,7 +915,6 @@ extern const struct language_data auto_language_data = &exp_descriptor_standard, false, /* store_sym_names_in_linkage_form_p */ unk_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; diff --git a/gdb/language.h b/gdb/language.h index 83014e47789..d83ea132a91 100644 --- a/gdb/language.h +++ b/gdb/language.h @@ -232,9 +232,6 @@ struct language_data const struct op_print *la_op_print_tab; - /* Index to use for extracting the first element of a string. */ - char string_lower_bound; - /* Various operations on varobj. */ const struct lang_varobj_ops *la_varobj_ops; }; @@ -568,6 +565,14 @@ struct language_defn : language_data virtual bool c_style_arrays_p () const { return true; } + /* Return the index to use for extracting the first element of a string, + or as the lower bound when creating a new string. The default of + choosing 0 or 1 based on C_STYLE_ARRAYS_P works for all currently + supported languages except Modula-2. */ + + virtual char string_lower_bound () const + { return c_style_arrays_p () ? 0 : 1; } + protected: /* This is the overridable part of the GET_SYMBOL_NAME_MATCHER method. diff --git a/gdb/m2-lang.c b/gdb/m2-lang.c index 9dd557777c0..7bd9e710377 100644 --- a/gdb/m2-lang.c +++ b/gdb/m2-lang.c @@ -206,7 +206,6 @@ extern const struct language_data m2_language_data = &exp_descriptor_modula2, false, /* la_store_sym_names_in_linkage_form_p */ m2_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; @@ -440,6 +439,12 @@ public: bool c_style_arrays_p () const override { return false; } + + /* See language.h. Despite not having C-style arrays, Modula-2 uses 0 + for its string lower bounds. */ + + char string_lower_bound () const override + { return 0; } }; /* Single instance of the M2 language. */ diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c index 66b7b50144b..337b7a49f3c 100644 --- a/gdb/objc-lang.c +++ b/gdb/objc-lang.c @@ -330,7 +330,6 @@ extern const struct language_data objc_language_data = &exp_descriptor_standard, false, /* la_store_sym_names_in_linkage_form_p */ objc_op_print_tab, /* Expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; diff --git a/gdb/opencl-lang.c b/gdb/opencl-lang.c index 1ac3aa42a8e..2e3e54a2233 100644 --- a/gdb/opencl-lang.c +++ b/gdb/opencl-lang.c @@ -1013,7 +1013,6 @@ extern const struct language_data opencl_language_data = &exp_descriptor_opencl, false, /* la_store_sym_names_in_linkage_form_p */ c_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; diff --git a/gdb/p-lang.c b/gdb/p-lang.c index 66ba51ac8eb..b07557151db 100644 --- a/gdb/p-lang.c +++ b/gdb/p-lang.c @@ -259,7 +259,6 @@ extern const struct language_data pascal_language_data = &exp_descriptor_standard, false, /* la_store_sym_names_in_linkage_form_p */ pascal_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c index de971e6255a..12fc138785d 100644 --- a/gdb/rust-lang.c +++ b/gdb/rust-lang.c @@ -1908,7 +1908,6 @@ extern const struct language_data rust_language_data = &exp_descriptor_rust, false, /* la_store_sym_names_in_linkage_form_p */ c_op_print_tab, /* expression operators for printing */ - 0, /* String lower bound */ &default_varobj_ops, }; diff --git a/gdb/valops.c b/gdb/valops.c index cf4cbaba02b..0995a76c02f 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -1644,7 +1644,7 @@ struct value * value_cstring (const char *ptr, ssize_t len, struct type *char_type) { struct value *val; - int lowbound = current_language->string_lower_bound; + int lowbound = current_language->string_lower_bound (); ssize_t highbound = len / TYPE_LENGTH (char_type); struct type *stringtype = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1); @@ -1667,7 +1667,7 @@ struct value * value_string (const char *ptr, ssize_t len, struct type *char_type) { struct value *val; - int lowbound = current_language->string_lower_bound; + int lowbound = current_language->string_lower_bound (); ssize_t highbound = len / TYPE_LENGTH (char_type); struct type *stringtype = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1); diff --git a/gdb/value.c b/gdb/value.c index c72b2fd6cea..c8d94149bb8 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -1041,7 +1041,10 @@ allocate_value (struct type *type) struct value * allocate_repeat_value (struct type *type, int count) { - int low_bound = current_language->string_lower_bound; /* ??? */ + /* Despite the fact that we are really creating an array of TYPE here, we + use the string lower bound as the array lower bound. This seems to + work fine for now. */ + int low_bound = current_language->string_lower_bound (); /* FIXME-type-allocation: need a way to free this type when we are done with it. */ struct type *array_type -- 2.30.2