Re: [libstdc++] Add xmethods for array, deque, forward_list, list and vector.

2014-10-13 Thread Jonathan Wakely

On 12/10/14 06:49 -0700, Siva Chandra wrote:

Hello,

Attached is a patch which adds xmethods for std::array, std::deque,
std::forward_list, std::list and std::vector.  There were already
couple of xmethods existing for std::vector, but this patch adds more
over them.


Committed - thanks!


Re: [libstdc++] Add xmethods for array, deque, forward_list, list and vector.

2014-10-13 Thread Siva Chandra
On Mon, Oct 13, 2014 at 4:23 AM, Jonathan Wakely jwak...@redhat.com wrote:
 On 12/10/14 06:49 -0700, Siva Chandra wrote:

 Hello,

 Attached is a patch which adds xmethods for std::array, std::deque,
 std::forward_list, std::list and std::vector.  There were already
 couple of xmethods existing for std::vector, but this patch adds more
 over them.

 Committed - thanks!

That was fast. Thanks a lot!

- Siva Chandra


[libstdc++] Add xmethods for array, deque, forward_list, list and vector.

2014-10-12 Thread Siva Chandra
Hello,

Attached is a patch which adds xmethods for std::array, std::deque,
std::forward_list, std::list and std::vector.  There were already
couple of xmethods existing for std::vector, but this patch adds more
over them.

libstdc++-v3/ChangeLog:

2014-10-12  Siva Chandra Reddy  sivachan...@google.com

* python/libstdcxx/v6/xmethods.py: Add xmethods for std::array,
std::deque, std::forward_list, std::list, std::vector.
* testsuite/libstdc++-xmethods/array.cc: New file.
* testsuite/libstdc++-xmethods/deque.cc: Likewise.
* testsuite/libstdc++-xmethods/deque.cc: Likewise.
* testsuite/libstdc++-xmethods/forwardlist.cc: Likewise.
* testsuite/libstdc++-xmethods/list.cc: Likewise.
* testsuite/libstdc++-xmethods/vector.cc: Likewise.

Thanks,
Siva Chandra
diff --git a/libstdc++-v3/python/libstdcxx/v6/xmethods.py 
b/libstdc++-v3/python/libstdcxx/v6/xmethods.py
index f20f411..6af1c95 100644
--- a/libstdc++-v3/python/libstdcxx/v6/xmethods.py
+++ b/libstdc++-v3/python/libstdcxx/v6/xmethods.py
@@ -1,4 +1,4 @@
-# Xmethods for libstc++.
+# Xmethods for libstdc++.
 
 # Copyright (C) 2014 Free Software Foundation, Inc.
 
@@ -21,45 +21,406 @@ import re
 
 matcher_name_prefix = 'libstdc++::'
 
-# Xmethods for std::vector
+class LibStdCxxXMethod(gdb.xmethod.XMethod):
+def __init__(self, name, worker_class):
+gdb.xmethod.XMethod.__init__(self, name)
+self.worker_class = worker_class
+
+# Xmethods for std::array
+
+class ArrayWorkerBase(gdb.xmethod.XMethodWorker):
+def __init__(self, valtype, size):
+self._valtype = valtype
+self._size = size
+
+def null_value(self):
+nullptr = gdb.parse_and_eval('(void *) 0')
+return nullptr.cast(self._valtype.pointer()).dereference()
+
+class ArraySizeWorker(ArrayWorkerBase):
+def __init__(self, valtype, size):
+ArrayWorkerBase.__init__(self, valtype, size)
+
+def get_arg_types(self):
+return None
+
+def __call__(self, obj):
+return self._size
+
+class ArrayEmptyWorker(ArrayWorkerBase):
+def __init__(self, valtype, size):
+ArrayWorkerBase.__init__(self, valtype, size)
+
+def get_arg_types(self):
+return None
+
+def __call__(self, obj):
+return (int(self._size) == 0)
+
+class ArrayFrontWorker(ArrayWorkerBase):
+def __init__(self, valtype, size):
+ArrayWorkerBase.__init__(self, valtype, size)
+
+def get_arg_types(self):
+return None
+
+def __call__(self, obj):
+if int(self._size)  0:
+return obj['_M_elems'][0]
+else:
+return self.null_value()
+
+class ArrayBackWorker(ArrayWorkerBase):
+def __init__(self, valtype, size):
+ArrayWorkerBase.__init__(self, valtype, size)
+
+def get_arg_types(self):
+return None
+
+def __call__(self, obj):
+if int(self._size)  0:
+return obj['_M_elems'][self._size - 1]
+else:
+return self.null_value()
+
+class ArrayAtWorker(ArrayWorkerBase):
+def __init__(self, valtype, size):
+ArrayWorkerBase.__init__(self, valtype, size)
+
+def get_arg_types(self):
+return gdb.lookup_type('std::size_t')
+
+def __call__(self, obj, index):
+if int(index) = int(self._size):
+raise IndexError('Array index %d should not be = %d.' %
+ ((int(index), self._size)))
+return obj['_M_elems'][index]
+
+class ArraySubscriptWorker(ArrayWorkerBase):
+def __init__(self, valtype, size):
+ArrayWorkerBase.__init__(self, valtype, size)
+
+def get_arg_types(self):
+return gdb.lookup_type('std::size_t')
+
+def __call__(self, obj, index):
+if int(self._size)  0:
+return obj['_M_elems'][index]
+else:
+return self.null_value()
 
-class VectorSizeWorker(gdb.xmethod.XMethodWorker):
+class ArrayMethodsMatcher(gdb.xmethod.XMethodMatcher):
 def __init__(self):
-self.name = 'size'
-self.enabled = True
+gdb.xmethod.XMethodMatcher.__init__(self,
+matcher_name_prefix + 'array')
+self._method_dict = {
+'size': LibStdCxxXMethod('size', ArraySizeWorker),
+'empty': LibStdCxxXMethod('empty', ArrayEmptyWorker),
+'front': LibStdCxxXMethod('front', ArrayFrontWorker),
+'back': LibStdCxxXMethod('back', ArrayBackWorker),
+'at': LibStdCxxXMethod('at', ArrayAtWorker),
+'operator[]': LibStdCxxXMethod('operator[]', ArraySubscriptWorker),
+}
+self.methods = [self._method_dict[m] for m in self._method_dict]
+
+def match(self, class_type, method_name):
+if not re.match('^std::array.*$', class_type.tag):
+return None
+method = self._method_dict.get(method_name)
+if method is None or not method.enabled:
+return None
+try:
+