[14/29] Introduce "scripting" domains

Message ID 20231120-submit-domain-hacks-2-v1-14-29650d01b198@tromey.com
State New
Headers
Series Restructure symbol domains |

Commit Message

Tom Tromey Nov. 21, 2023, 3:53 a.m. UTC
  The Python and Guile code exposed the internal domain constants both
as attributes of symbols and as values to pass to lookup functions.

Now, perfect backward compatibility here can't be achieved: some
symbols are going to have domain changes by the end of this series.
However, it seemed to me that we can preserve lookups using the basic
domain values.

This patch implements this by exporting the "or"-able search constants
with an extra bit set.  Then it introduces some functions to convert
such constants to domain_search_flags.  This will be used by the
Python and Guile code, so that both old- and new-style lookups will
work properly; and while preserving the idea that the domain constants
can be compared to a symbol's domain.
---
 gdb/symtab.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 gdb/symtab.h | 31 +++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
  

Patch

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 3159e5d2cea..6020efe197b 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -329,6 +329,48 @@  domain_name (domain_search_flags flags)
 
 /* See symtab.h.  */
 
+domain_search_flags
+from_scripting_domain (int val)
+{
+  if ((val & SCRIPTING_SEARCH_FLAG) == 0)
+    {
+      /* VAL should be one of the domain constants.  Verify this and
+	 convert it to a search constant.  */
+      switch (val)
+	{
+#define DOMAIN(X)					\
+	  case X ## _DOMAIN: break;
+#include "sym-domains.def"
+#undef DOMAIN
+	default:
+	  error (_("unrecognized domain constant"));
+	}
+      domain_search_flags result = to_search_flags ((domain_enum) val);
+      if (val == VAR_DOMAIN)
+	{
+	  /* This matches the historical practice.  */
+	  result |= SEARCH_TYPE_DOMAIN | SEARCH_FUNCTION_DOMAIN;
+	}
+      return result;
+    }
+  else
+    {
+      /* VAL is several search constants or'd together.  Verify
+	 this.  */
+      val &= ~SCRIPTING_SEARCH_FLAG;
+      int check = val;
+#define DOMAIN(X)				\
+      check &= ~ (int) SEARCH_ ## X ## _DOMAIN;
+#include "sym-domains.def"
+#undef DOMAIN
+      if (check != 0)
+	error (_("unrecognized domain constant"));
+      return (domain_search_flag) val;
+    }
+}
+
+/* See symtab.h.  */
+
 CORE_ADDR
 linetable_entry::pc (const struct objfile *objfile) const
 {
diff --git a/gdb/symtab.h b/gdb/symtab.h
index d9caeb70856..5f2b17a3cee 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -941,6 +941,37 @@  search_flags_matches (domain_search_flags flags, domain_enum domain)
   return (flags & to_search_flags (domain)) != 0;
 }
 
+/* Some helpers for Python and Guile to account for backward
+   compatibility.  Those exposed the domains for lookup as well as
+   checking attributes of a symbol, so special encoding and decoding
+   is needed to continue to support both uses.  Domain constants must
+   remain unchanged, so that comparing a symbol's domain against a
+   constant yields the correct result, so search symbols are
+   distinguished by adding a flag bit.  This way, either sort of
+   constant can be used for lookup.  */
+
+/* The flag bit.  */
+constexpr int SCRIPTING_SEARCH_FLAG = 0x8000;
+gdb_static_assert (SCRIPTING_SEARCH_FLAG > SEARCH_ALL);
+
+/* Convert a domain constant to a "scripting domain".  */
+static constexpr inline int
+to_scripting_domain (domain_enum val)
+{
+  return val;
+}
+
+/* Convert a search constant to a "scripting domain".  */
+static constexpr inline int
+to_scripting_domain (domain_search_flags val)
+{
+  return SCRIPTING_SEARCH_FLAG | (int) val;
+}
+
+/* Convert from a "scripting domain" constant back to search flags.
+   Throws an exception if VAL is not one of the allowable values.  */
+extern domain_search_flags from_scripting_domain (int val);
+
 /* An address-class says where to find the value of a symbol.  */
 
 enum address_class