[08/10] gdb: Convert language_data::string_lower_bound to a method

Message ID 3bf5fb9dd1cab5625807dc8e17ef7dbaeb4370fe.1594293733.git.andrew.burgess@embecosm.com
State Superseded
Headers show
Series
  • Further reduction of language_data struct
Related show

Commit Message

Andrew Burgess July 9, 2020, 11:37 a.m.
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(-)

-- 
2.25.4

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 0323b525577..22c24485767 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13674,7 +13674,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 8cf61722fdc..eb05818c496 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 7c2d43d854b..22575b44a4f 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 @@  class m2_language : public language_defn
 
   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 8f930294937..e11c4a86fdb 100644
--- a/gdb/opencl-lang.c
+++ b/gdb/opencl-lang.c
@@ -1014,7 +1014,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 3b515fa003a..261530be9b9 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -1913,7 +1913,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 f6d5cc6f963..03786d9574e 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -1640,7 +1640,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);
@@ -1663,7 +1663,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 bb3111531a5..3f89e44bfab 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