Introduce a block iterator wrapper
authorTom Tromey <tom@tromey.com>
Fri, 20 Jan 2023 01:44:38 +0000 (18:44 -0700)
committerTom Tromey <tom@tromey.com>
Sun, 19 Feb 2023 19:51:06 +0000 (12:51 -0700)
This introduces a C++-style iterator that wraps the existing
block_iterator.  It also adds a range adapter.  These will be used in
a later patch.

gdb/block.h

index 03aeebddc6b13152b20094d5d8dab6a33d19a57a..7341d03a07e51fed8fa9120a0211e53c675c887d 100644 (file)
@@ -486,6 +486,56 @@ extern struct symbol *block_iterator_first
 
 extern struct symbol *block_iterator_next (struct block_iterator *iterator);
 
+/* An iterator that wraps a block_iterator.  The naming here is
+   unfortunate, but block_iterator was named before gdb switched to
+   C++.  */
+struct block_iterator_wrapper
+{
+  typedef block_iterator_wrapper self_type;
+  typedef struct symbol *value_type;
+
+  explicit block_iterator_wrapper (const struct block *block,
+                                  const lookup_name_info *name = nullptr)
+    : m_sym (block_iterator_first (block, &m_iter, name))
+  {
+  }
+
+  block_iterator_wrapper ()
+    : m_sym (nullptr)
+  {
+  }
+
+  value_type operator* () const
+  {
+    return m_sym;
+  }
+
+  bool operator== (const self_type &other) const
+  {
+    return m_sym == other.m_sym;
+  }
+
+  bool operator!= (const self_type &other) const
+  {
+    return m_sym != other.m_sym;
+  }
+
+  self_type &operator++ ()
+  {
+    m_sym = block_iterator_next (&m_iter);
+    return *this;
+  }
+
+private:
+
+  struct symbol *m_sym;
+  struct block_iterator m_iter;
+};
+
+/* An iterator range for block_iterator_wrapper.  */
+
+typedef iterator_range<block_iterator_wrapper> block_iterator_range;
+
 /* Return true if symbol A is the best match possible for DOMAIN.  */
 
 extern bool best_symbol (struct symbol *a, const domain_enum domain);