]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/python/libstdcxx/v6/xmethods.py
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / python / libstdcxx / v6 / xmethods.py
index 01819592ff27b2ec9f00d15550ded46e0fc08915..436c866e00194cfb4aeec213d58a5d17e9ff66b4 100644 (file)
@@ -1,6 +1,6 @@
 # Xmethods for libstdc++.
 
-# Copyright (C) 2014-2018 Free Software Foundation, Inc.
+# Copyright (C) 2014-2024 Free Software Foundation, Inc.
 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -21,12 +21,27 @@ import re
 
 matcher_name_prefix = 'libstdc++::'
 
+
 def get_bool_type():
     return gdb.lookup_type('bool')
 
 def get_std_size_type():
     return gdb.lookup_type('std::size_t')
 
+_versioned_namespace = '__8::'
+
+def is_specialization_of(x, template_name):
+    """
+    Test whether a type is a specialization of the named class template.
+    The type can be specified as a string or a gdb.Type object.
+    The template should be the name of a class template as a string,
+    without any 'std' qualification.
+    """
+    if isinstance(x, gdb.Type):
+        x = x.tag
+    template_name = '(%s)?%s' % (_versioned_namespace, template_name)
+    return re.match(r'^std::(__\d::)?%s<.*>$' % template_name, x) is not None
+
 class LibStdCxxXMethod(gdb.xmethod.XMethod):
     def __init__(self, name, worker_class):
         gdb.xmethod.XMethod.__init__(self, name)
@@ -34,6 +49,7 @@ class LibStdCxxXMethod(gdb.xmethod.XMethod):
 
 # Xmethods for std::array
 
+
 class ArrayWorkerBase(gdb.xmethod.XMethodWorker):
     def __init__(self, val_type, size):
         self._val_type = val_type
@@ -43,6 +59,7 @@ class ArrayWorkerBase(gdb.xmethod.XMethodWorker):
         nullptr = gdb.parse_and_eval('(void *) 0')
         return nullptr.cast(self._val_type.pointer()).dereference()
 
+
 class ArraySizeWorker(ArrayWorkerBase):
     def __init__(self, val_type, size):
         ArrayWorkerBase.__init__(self, val_type, size)
@@ -56,6 +73,7 @@ class ArraySizeWorker(ArrayWorkerBase):
     def __call__(self, obj):
         return self._size
 
+
 class ArrayEmptyWorker(ArrayWorkerBase):
     def __init__(self, val_type, size):
         ArrayWorkerBase.__init__(self, val_type, size)
@@ -69,6 +87,7 @@ class ArrayEmptyWorker(ArrayWorkerBase):
     def __call__(self, obj):
         return (int(self._size) == 0)
 
+
 class ArrayFrontWorker(ArrayWorkerBase):
     def __init__(self, val_type, size):
         ArrayWorkerBase.__init__(self, val_type, size)
@@ -85,6 +104,7 @@ class ArrayFrontWorker(ArrayWorkerBase):
         else:
             return self.null_value()
 
+
 class ArrayBackWorker(ArrayWorkerBase):
     def __init__(self, val_type, size):
         ArrayWorkerBase.__init__(self, val_type, size)
@@ -101,6 +121,7 @@ class ArrayBackWorker(ArrayWorkerBase):
         else:
             return self.null_value()
 
+
 class ArrayAtWorker(ArrayWorkerBase):
     def __init__(self, val_type, size):
         ArrayWorkerBase.__init__(self, val_type, size)
@@ -117,6 +138,7 @@ class ArrayAtWorker(ArrayWorkerBase):
                              ((int(index), self._size)))
         return obj['_M_elems'][index]
 
+
 class ArraySubscriptWorker(ArrayWorkerBase):
     def __init__(self, val_type, size):
         ArrayWorkerBase.__init__(self, val_type, size)
@@ -133,6 +155,7 @@ class ArraySubscriptWorker(ArrayWorkerBase):
         else:
             return self.null_value()
 
+
 class ArrayMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self):
         gdb.xmethod.XMethodMatcher.__init__(self,
@@ -148,7 +171,7 @@ class ArrayMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?array<.*>$', class_type.tag):
+        if not is_specialization_of(class_type, 'array'):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -160,24 +183,34 @@ class ArrayMethodsMatcher(gdb.xmethod.XMethodMatcher):
             return None
         return method.worker_class(value_type, size)
 
+
 # Xmethods for std::deque
 
+
 class DequeWorkerBase(gdb.xmethod.XMethodWorker):
     def __init__(self, val_type):
         self._val_type = val_type
         self._bufsize = 512 // val_type.sizeof or 1
 
     def size(self, obj):
-        first_node = obj['_M_impl']['_M_start']['_M_node']
-        last_node = obj['_M_impl']['_M_finish']['_M_node']
-        cur = obj['_M_impl']['_M_finish']['_M_cur']
-        first = obj['_M_impl']['_M_finish']['_M_first']
-        return (last_node - first_node) * self._bufsize + (cur - first)
+        start = obj['_M_impl']['_M_start']
+        finish = obj['_M_impl']['_M_finish']
+        if start['_M_cur'] == finish['_M_cur']:
+            return 0
+        return (self._bufsize
+                * (finish['_M_node'] - start['_M_node'] - 1)
+                + (finish['_M_cur'] - finish['_M_first'])
+                + (start['_M_last'] - start['_M_cur']))
 
     def index(self, obj, idx):
-        first_node = obj['_M_impl']['_M_start']['_M_node']
-        index_node = first_node + int(idx) // self._bufsize
-        return index_node[0][idx % self._bufsize]
+        start = obj['_M_impl']['_M_start']
+        first_node_size = start['_M_last'] - start['_M_cur']
+        if idx < first_node_size:
+            return start['_M_cur'][idx]
+        idx = idx - first_node_size
+        index_node = start['_M_node'][1 + int(idx) // self._bufsize]
+        return index_node[idx % self._bufsize]
+
 
 class DequeEmptyWorker(DequeWorkerBase):
     def get_arg_types(self):
@@ -190,6 +223,7 @@ class DequeEmptyWorker(DequeWorkerBase):
         return (obj['_M_impl']['_M_start']['_M_cur'] ==
                 obj['_M_impl']['_M_finish']['_M_cur'])
 
+
 class DequeSizeWorker(DequeWorkerBase):
     def get_arg_types(self):
         return None
@@ -200,6 +234,7 @@ class DequeSizeWorker(DequeWorkerBase):
     def __call__(self, obj):
         return self.size(obj)
 
+
 class DequeFrontWorker(DequeWorkerBase):
     def get_arg_types(self):
         return None
@@ -210,6 +245,7 @@ class DequeFrontWorker(DequeWorkerBase):
     def __call__(self, obj):
         return obj['_M_impl']['_M_start']['_M_cur'][0]
 
+
 class DequeBackWorker(DequeWorkerBase):
     def get_arg_types(self):
         return None
@@ -219,12 +255,13 @@ class DequeBackWorker(DequeWorkerBase):
 
     def __call__(self, obj):
         if (obj['_M_impl']['_M_finish']['_M_cur'] ==
-            obj['_M_impl']['_M_finish']['_M_first']):
+                obj['_M_impl']['_M_finish']['_M_first']):
             prev_node = obj['_M_impl']['_M_finish']['_M_node'] - 1
             return prev_node[0][self._bufsize - 1]
         else:
             return obj['_M_impl']['_M_finish']['_M_cur'][-1]
 
+
 class DequeSubscriptWorker(DequeWorkerBase):
     def get_arg_types(self):
         return get_std_size_type()
@@ -235,6 +272,7 @@ class DequeSubscriptWorker(DequeWorkerBase):
     def __call__(self, obj, subscript):
         return self.index(obj, subscript)
 
+
 class DequeAtWorker(DequeWorkerBase):
     def get_arg_types(self):
         return get_std_size_type()
@@ -248,7 +286,8 @@ class DequeAtWorker(DequeWorkerBase):
             raise IndexError('Deque index "%d" should not be >= %d.' %
                              (int(index), deque_size))
         else:
-           return self.index(obj, index)
+            return self.index(obj, index)
+
 
 class DequeMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self):
@@ -265,7 +304,7 @@ class DequeMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?deque<.*>$', class_type.tag):
+        if not is_specialization_of(class_type, 'deque'):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -274,6 +313,7 @@ class DequeMethodsMatcher(gdb.xmethod.XMethodMatcher):
 
 # Xmethods for std::forward_list
 
+
 class ForwardListWorkerBase(gdb.xmethod.XMethodMatcher):
     def __init__(self, val_type, node_type):
         self._val_type = val_type
@@ -282,6 +322,7 @@ class ForwardListWorkerBase(gdb.xmethod.XMethodMatcher):
     def get_arg_types(self):
         return None
 
+
 class ForwardListEmptyWorker(ForwardListWorkerBase):
     def get_result_type(self, obj):
         return get_bool_type()
@@ -289,6 +330,7 @@ class ForwardListEmptyWorker(ForwardListWorkerBase):
     def __call__(self, obj):
         return obj['_M_impl']['_M_head']['_M_next'] == 0
 
+
 class ForwardListFrontWorker(ForwardListWorkerBase):
     def get_result_type(self, obj):
         return self._val_type
@@ -298,6 +340,7 @@ class ForwardListFrontWorker(ForwardListWorkerBase):
         val_address = node['_M_storage']['_M_storage'].address
         return val_address.cast(self._val_type.pointer()).dereference()
 
+
 class ForwardListMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self):
         matcher_name = matcher_name_prefix + 'forward_list'
@@ -309,7 +352,7 @@ class ForwardListMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?forward_list<.*>$', class_type.tag):
+        if not is_specialization_of(class_type, 'forward_list'):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -320,6 +363,7 @@ class ForwardListMethodsMatcher(gdb.xmethod.XMethodMatcher):
 
 # Xmethods for std::list
 
+
 class ListWorkerBase(gdb.xmethod.XMethodWorker):
     def __init__(self, val_type, node_type):
         self._val_type = val_type
@@ -337,6 +381,7 @@ class ListWorkerBase(gdb.xmethod.XMethodWorker):
         addr = node['_M_storage'].address
         return addr.cast(self._val_type.pointer()).dereference()
 
+
 class ListEmptyWorker(ListWorkerBase):
     def get_result_type(self, obj):
         return get_bool_type()
@@ -348,6 +393,7 @@ class ListEmptyWorker(ListWorkerBase):
         else:
             return False
 
+
 class ListSizeWorker(ListWorkerBase):
     def get_result_type(self, obj):
         return get_std_size_type()
@@ -361,6 +407,7 @@ class ListSizeWorker(ListWorkerBase):
             size += 1
         return size
 
+
 class ListFrontWorker(ListWorkerBase):
     def get_result_type(self, obj):
         return self._val_type
@@ -369,6 +416,7 @@ class ListFrontWorker(ListWorkerBase):
         node = obj['_M_impl']['_M_node']['_M_next'].cast(self._node_type)
         return self.get_value_from_node(node)
 
+
 class ListBackWorker(ListWorkerBase):
     def get_result_type(self, obj):
         return self._val_type
@@ -377,6 +425,7 @@ class ListBackWorker(ListWorkerBase):
         prev_node = obj['_M_impl']['_M_node']['_M_prev'].cast(self._node_type)
         return self.get_value_from_node(prev_node)
 
+
 class ListMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self):
         gdb.xmethod.XMethodMatcher.__init__(self,
@@ -390,7 +439,7 @@ class ListMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?(__cxx11::)?list<.*>$', class_type.tag):
+        if not is_specialization_of(class_type, '(__cxx11::)?list'):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -401,6 +450,7 @@ class ListMethodsMatcher(gdb.xmethod.XMethodMatcher):
 
 # Xmethods for std::vector
 
+
 class VectorWorkerBase(gdb.xmethod.XMethodWorker):
     def __init__(self, val_type):
         self._val_type = val_type
@@ -425,6 +475,7 @@ class VectorWorkerBase(gdb.xmethod.XMethodWorker):
         else:
             return obj['_M_impl']['_M_start'][index]
 
+
 class VectorEmptyWorker(VectorWorkerBase):
     def get_arg_types(self):
         return None
@@ -435,6 +486,7 @@ class VectorEmptyWorker(VectorWorkerBase):
     def __call__(self, obj):
         return int(self.size(obj)) == 0
 
+
 class VectorSizeWorker(VectorWorkerBase):
     def get_arg_types(self):
         return None
@@ -445,6 +497,7 @@ class VectorSizeWorker(VectorWorkerBase):
     def __call__(self, obj):
         return self.size(obj)
 
+
 class VectorFrontWorker(VectorWorkerBase):
     def get_arg_types(self):
         return None
@@ -455,6 +508,7 @@ class VectorFrontWorker(VectorWorkerBase):
     def __call__(self, obj):
         return self.get(obj, 0)
 
+
 class VectorBackWorker(VectorWorkerBase):
     def get_arg_types(self):
         return None
@@ -465,6 +519,7 @@ class VectorBackWorker(VectorWorkerBase):
     def __call__(self, obj):
         return self.get(obj, int(self.size(obj)) - 1)
 
+
 class VectorAtWorker(VectorWorkerBase):
     def get_arg_types(self):
         return get_std_size_type()
@@ -479,6 +534,7 @@ class VectorAtWorker(VectorWorkerBase):
                              ((int(index), size)))
         return self.get(obj, int(index))
 
+
 class VectorSubscriptWorker(VectorWorkerBase):
     def get_arg_types(self):
         return get_std_size_type()
@@ -489,6 +545,7 @@ class VectorSubscriptWorker(VectorWorkerBase):
     def __call__(self, obj, subscript):
         return self.get(obj, int(subscript))
 
+
 class VectorMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self):
         gdb.xmethod.XMethodMatcher.__init__(self,
@@ -505,7 +562,7 @@ class VectorMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?vector<.*>$', class_type.tag):
+        if not is_specialization_of(class_type, 'vector'):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -514,6 +571,7 @@ class VectorMethodsMatcher(gdb.xmethod.XMethodMatcher):
 
 # Xmethods for associative containers
 
+
 class AssociativeContainerWorkerBase(gdb.xmethod.XMethodWorker):
     def __init__(self, unordered):
         self._unordered = unordered
@@ -527,6 +585,7 @@ class AssociativeContainerWorkerBase(gdb.xmethod.XMethodWorker):
     def get_arg_types(self):
         return None
 
+
 class AssociativeContainerEmptyWorker(AssociativeContainerWorkerBase):
     def get_result_type(self, obj):
         return get_bool_type()
@@ -534,6 +593,7 @@ class AssociativeContainerEmptyWorker(AssociativeContainerWorkerBase):
     def __call__(self, obj):
         return int(self.node_count(obj)) == 0
 
+
 class AssociativeContainerSizeWorker(AssociativeContainerWorkerBase):
     def get_result_type(self, obj):
         return get_std_size_type()
@@ -541,6 +601,7 @@ class AssociativeContainerSizeWorker(AssociativeContainerWorkerBase):
     def __call__(self, obj):
         return self.node_count(obj)
 
+
 class AssociativeContainerMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self, name):
         gdb.xmethod.XMethodMatcher.__init__(self,
@@ -554,7 +615,7 @@ class AssociativeContainerMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?%s<.*>$' % self._name, class_type.tag):
+        if not is_specialization_of(class_type, self._name):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -564,8 +625,11 @@ class AssociativeContainerMethodsMatcher(gdb.xmethod.XMethodMatcher):
 
 # Xmethods for std::unique_ptr
 
+
 class UniquePtrGetWorker(gdb.xmethod.XMethodWorker):
-    "Implements std::unique_ptr<T>::get() and std::unique_ptr<T>::operator->()"
+    """
+    Implement std::unique_ptr<T>::get() and std::unique_ptr<T>::operator->().
+    """
 
     def __init__(self, elem_type):
         self._is_array = elem_type.code == gdb.TYPE_CODE_ARRAY
@@ -581,19 +645,31 @@ class UniquePtrGetWorker(gdb.xmethod.XMethodWorker):
         return self._elem_type.pointer()
 
     def _supports(self, method_name):
-        "operator-> is not supported for unique_ptr<T[]>"
+        # operator-> is not supported for unique_ptr<T[]>
         return method_name == 'get' or not self._is_array
 
     def __call__(self, obj):
         impl_type = obj.dereference().type.fields()[0].type.tag
-        if re.match('^std::(__\d+::)?__uniq_ptr_impl<.*>$', impl_type): # New implementation
-            return obj['_M_t']['_M_t']['_M_head_impl']
-        elif re.match('^std::(__\d+::)?tuple<.*>$', impl_type):
-            return obj['_M_t']['_M_head_impl']
-        return None
+        # Check for new implementations first:
+        if is_specialization_of(impl_type, '__uniq_ptr_(data|impl)'):
+            tuple_member = obj['_M_t']['_M_t']
+        elif is_specialization_of(impl_type, 'tuple'):
+            tuple_member = obj['_M_t']
+        else:
+            return None
+        tuple_impl_type = tuple_member.type.fields()[0].type  # _Tuple_impl
+        tuple_head_type = tuple_impl_type.fields()[1].type   # _Head_base
+        head_field = tuple_head_type.fields()[0]
+        if head_field.name == '_M_head_impl':
+            return tuple_member.cast(tuple_head_type)['_M_head_impl']
+        elif head_field.is_base_class:
+            return tuple_member.cast(head_field.type)
+        else:
+            return None
+
 
 class UniquePtrDerefWorker(UniquePtrGetWorker):
-    "Implements std::unique_ptr<T>::operator*()"
+    """Implement std::unique_ptr<T>::operator*()."""
 
     def __init__(self, elem_type):
         UniquePtrGetWorker.__init__(self, elem_type)
@@ -602,14 +678,15 @@ class UniquePtrDerefWorker(UniquePtrGetWorker):
         return self._elem_type
 
     def _supports(self, method_name):
-        "operator* is not supported for unique_ptr<T[]>"
+        # operator* is not supported for unique_ptr<T[]>
         return not self._is_array
 
     def __call__(self, obj):
         return UniquePtrGetWorker.__call__(self, obj).dereference()
 
+
 class UniquePtrSubscriptWorker(UniquePtrGetWorker):
-    "Implements std::unique_ptr<T>::operator[](size_t)"
+    """Implement std::unique_ptr<T>::operator[](size_t)."""
 
     def __init__(self, elem_type):
         UniquePtrGetWorker.__init__(self, elem_type)
@@ -621,12 +698,13 @@ class UniquePtrSubscriptWorker(UniquePtrGetWorker):
         return self._elem_type
 
     def _supports(self, method_name):
-        "operator[] is only supported for unique_ptr<T[]>"
+        # operator[] is only supported for unique_ptr<T[]>
         return self._is_array
 
     def __call__(self, obj, index):
         return UniquePtrGetWorker.__call__(self, obj)[index]
 
+
 class UniquePtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self):
         gdb.xmethod.XMethodMatcher.__init__(self,
@@ -640,7 +718,7 @@ class UniquePtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?unique_ptr<.*>$', class_type.tag):
+        if not is_specialization_of(class_type, 'unique_ptr'):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -652,8 +730,11 @@ class UniquePtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
 
 # Xmethods for std::shared_ptr
 
+
 class SharedPtrGetWorker(gdb.xmethod.XMethodWorker):
-    "Implements std::shared_ptr<T>::get() and std::shared_ptr<T>::operator->()"
+    """
+    Implements std::shared_ptr<T>::get() and std::shared_ptr<T>::operator->().
+    """
 
     def __init__(self, elem_type):
         self._is_array = elem_type.code == gdb.TYPE_CODE_ARRAY
@@ -669,14 +750,15 @@ class SharedPtrGetWorker(gdb.xmethod.XMethodWorker):
         return self._elem_type.pointer()
 
     def _supports(self, method_name):
-        "operator-> is not supported for shared_ptr<T[]>"
+        # operator-> is not supported for shared_ptr<T[]>
         return method_name == 'get' or not self._is_array
 
     def __call__(self, obj):
         return obj['_M_ptr']
 
+
 class SharedPtrDerefWorker(SharedPtrGetWorker):
-    "Implements std::shared_ptr<T>::operator*()"
+    """Implement std::shared_ptr<T>::operator*()."""
 
     def __init__(self, elem_type):
         SharedPtrGetWorker.__init__(self, elem_type)
@@ -685,14 +767,15 @@ class SharedPtrDerefWorker(SharedPtrGetWorker):
         return self._elem_type
 
     def _supports(self, method_name):
-        "operator* is not supported for shared_ptr<T[]>"
+        # operator* is not supported for shared_ptr<T[]>
         return not self._is_array
 
     def __call__(self, obj):
         return SharedPtrGetWorker.__call__(self, obj).dereference()
 
+
 class SharedPtrSubscriptWorker(SharedPtrGetWorker):
-    "Implements std::shared_ptr<T>::operator[](size_t)"
+    """Implement std::shared_ptr<T>::operator[](size_t)."""
 
     def __init__(self, elem_type):
         SharedPtrGetWorker.__init__(self, elem_type)
@@ -704,22 +787,23 @@ class SharedPtrSubscriptWorker(SharedPtrGetWorker):
         return self._elem_type
 
     def _supports(self, method_name):
-        "operator[] is only supported for shared_ptr<T[]>"
+        # operator[] is only supported for shared_ptr<T[]>
         return self._is_array
 
     def __call__(self, obj, index):
         # Check bounds if _elem_type is an array of known bound
-        m = re.match('.*\[(\d+)]$', str(self._elem_type))
+        m = re.match(r'.*\[(\d+)]$', str(self._elem_type))
         if m and index >= int(m.group(1)):
             raise IndexError('shared_ptr<%s> index "%d" should not be >= %d.' %
                              (self._elem_type, int(index), int(m.group(1))))
         return SharedPtrGetWorker.__call__(self, obj)[index]
 
+
 class SharedPtrUseCountWorker(gdb.xmethod.XMethodWorker):
-    "Implements std::shared_ptr<T>::use_count()"
+    """Implement std::shared_ptr<T>::use_count()."""
 
     def __init__(self, elem_type):
-        SharedPtrUseCountWorker.__init__(self, elem_type)
+        pass
 
     def get_arg_types(self):
         return None
@@ -727,12 +811,16 @@ class SharedPtrUseCountWorker(gdb.xmethod.XMethodWorker):
     def get_result_type(self, obj):
         return gdb.lookup_type('long')
 
+    def _supports(self, method_name):
+        return True
+
     def __call__(self, obj):
-        refcounts = ['_M_refcount']['_M_pi']
+        refcounts = obj['_M_refcount']['_M_pi']
         return refcounts['_M_use_count'] if refcounts else 0
 
+
 class SharedPtrUniqueWorker(SharedPtrUseCountWorker):
-    "Implements std::shared_ptr<T>::unique()"
+    """Implement std::shared_ptr<T>::unique()."""
 
     def __init__(self, elem_type):
         SharedPtrUseCountWorker.__init__(self, elem_type)
@@ -743,6 +831,7 @@ class SharedPtrUniqueWorker(SharedPtrUseCountWorker):
     def __call__(self, obj):
         return SharedPtrUseCountWorker.__call__(self, obj) == 1
 
+
 class SharedPtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
     def __init__(self):
         gdb.xmethod.XMethodMatcher.__init__(self,
@@ -758,7 +847,7 @@ class SharedPtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
         self.methods = [self._method_dict[m] for m in self._method_dict]
 
     def match(self, class_type, method_name):
-        if not re.match('^std::(__\d+::)?shared_ptr<.*>$', class_type.tag):
+        if not is_specialization_of(class_type, 'shared_ptr'):
             return None
         method = self._method_dict.get(method_name)
         if method is None or not method.enabled:
@@ -768,6 +857,7 @@ class SharedPtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
             return worker
         return None
 \f
+
 def register_libstdcxx_xmethods(locus):
     gdb.xmethod.register_xmethod_matcher(locus, ArrayMethodsMatcher())
     gdb.xmethod.register_xmethod_matcher(locus, ForwardListMethodsMatcher())