]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/python/libstdcxx/v6/xmethods.py
hook.in: Load the xmethods.
[thirdparty/gcc.git] / libstdc++-v3 / python / libstdcxx / v6 / xmethods.py
1 # Xmethods for libstc++.
2
3 # Copyright (C) 2014 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import gdb
19 import gdb.xmethod
20 import re
21
22 matcher_name_prefix = 'libstdc++::'
23
24 # Xmethods for std::vector
25
26 class VectorSizeWorker(gdb.xmethod.XMethodWorker):
27 def __init__(self):
28 self.name = 'size'
29 self.enabled = True
30
31 def get_arg_types(self):
32 return None
33
34 def __call__(self, obj):
35 return obj['_M_impl']['_M_finish'] - obj['_M_impl']['_M_start']
36
37 class VectorSubscriptWorker(gdb.xmethod.XMethodWorker):
38 def __init__(self):
39 self.name = 'operator[]'
40 self.enabled = True
41
42 def get_arg_types(self):
43 return gdb.lookup_type('std::size_t')
44
45 def __call__(self, obj, subscript):
46 return obj['_M_impl']['_M_start'][subscript]
47
48 class VectorMethodsMatcher(gdb.xmethod.XMethodMatcher):
49 def __init__(self):
50 gdb.xmethod.XMethodMatcher.__init__(self,
51 matcher_name_prefix + 'vector')
52 self._subscript_worker = VectorSubscriptWorker()
53 self._size_worker = VectorSizeWorker()
54 self.methods = [self._subscript_worker, self._size_worker]
55
56 def match(self, class_type, method_name):
57 if not re.match('^std::vector<.*>$', class_type.tag):
58 return None
59 if method_name == 'operator[]' and self._subscript_worker.enabled:
60 return self._subscript_worker
61 elif method_name == 'size' and self._size_worker.enabled:
62 return self._size_worker
63
64 # Xmethods for std::unique_ptr
65
66 class UniquePtrGetWorker(gdb.xmethod.XMethodWorker):
67 def __init__(self):
68 self.name = 'get'
69 self.enabled = True
70
71 def get_arg_types(self):
72 return None
73
74 def __call__(self, obj):
75 return obj['_M_t']['_M_head_impl']
76
77 class UniquePtrDerefWorker(UniquePtrGetWorker):
78 def __init__(self):
79 UniquePtrGetWorker.__init__(self)
80 self.name = 'operator*'
81
82 def __call__(self, obj):
83 return UniquePtrGetWorker.__call__(self, obj).dereference()
84
85 class UniquePtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
86 def __init__(self):
87 gdb.xmethod.XMethodMatcher.__init__(self,
88 matcher_name_prefix + 'unique_ptr')
89 self._get_worker = UniquePtrGetWorker()
90 self._deref_worker = UniquePtrDerefWorker()
91 self.methods = [self._get_worker, self._deref_worker]
92
93 def match(self, class_type, method_name):
94 if not re.match('^std::unique_ptr<.*>$', class_type.tag):
95 return None
96 if method_name == 'operator*' and self._deref_worker.enabled:
97 return self._deref_worker
98 elif method_name == 'get' and self._get_worker.enabled:
99 return self._get_worker
100 \f
101 def register_libstdcxx_xmethods(locus):
102 gdb.xmethod.register_xmethod_matcher(locus, VectorMethodsMatcher())
103 gdb.xmethod.register_xmethod_matcher(locus, UniquePtrMethodsMatcher())