blob: a69ab29911cd716822493c05bef12386ebdfad01 [file] [log] [blame]
Enrico Granata979e20d2011-07-29 19:53:35 +00001class StdVectorSynthProvider:
Enrico Granatac92eb402011-08-04 01:41:02 +00002
Enrico Granata7718f3f2011-08-04 02:35:14 +00003 def __init__(self, valobj, dict):
4 self.valobj = valobj;
Enrico Granata7e655032011-08-24 01:32:46 +00005 self.update() # initialize this provider
Enrico Granatac92eb402011-08-04 01:41:02 +00006
Enrico Granata7718f3f2011-08-04 02:35:14 +00007 def num_children(self):
Enrico Granata7e655032011-08-24 01:32:46 +00008 start_val = self.start.GetValueAsUnsigned(0) # read _M_start
9 finish_val = self.finish.GetValueAsUnsigned(0) # read _M_finish
10 end_val = self.end.GetValueAsUnsigned(0) # read _M_end_of_storage
11
Enrico Granata7718f3f2011-08-04 02:35:14 +000012 # Before a vector has been constructed, it will contain bad values
13 # so we really need to be careful about the length we return since
14 # unitialized data can cause us to return a huge number. We need
15 # to also check for any of the start, finish or end of storage values
16 # being zero (NULL). If any are, then this vector has not been
17 # initialized yet and we should return zero
Enrico Granata7e655032011-08-24 01:32:46 +000018
Enrico Granata7718f3f2011-08-04 02:35:14 +000019 # Make sure nothing is NULL
20 if start_val == 0 or finish_val == 0 or end_val == 0:
21 return 0
22 # Make sure start is less than finish
23 if start_val >= finish_val:
24 return 0
25 # Make sure finish is less than or equal to end of storage
26 if finish_val > end_val:
27 return 0
Enrico Granatac92eb402011-08-04 01:41:02 +000028
Enrico Granata7e655032011-08-24 01:32:46 +000029 # pointer arithmetic: (_M_finish - _M_start) would return the number of
30 # items of type T contained in the vector. because Python has no way to know
31 # that we want to subtract two pointers instead of two integers, we have to divide
32 # by sizeof(T) to be equivalent to the C++ pointer expression
Enrico Granata7718f3f2011-08-04 02:35:14 +000033 num_children = (finish_val-start_val)/self.data_size
Enrico Granata7718f3f2011-08-04 02:35:14 +000034 return num_children
Enrico Granatac92eb402011-08-04 01:41:02 +000035
Enrico Granata7e655032011-08-24 01:32:46 +000036 # we assume we are getting children named [0] thru [N-1]
37 # if for some reason our child name is not in this format,
38 # do not bother to show it, and return an invalid value
Enrico Granata7718f3f2011-08-04 02:35:14 +000039 def get_child_index(self,name):
Enrico Granata7e655032011-08-24 01:32:46 +000040 try:
41 return int(name.lstrip('[').rstrip(']'))
42 except:
43 return -1;
Enrico Granatac92eb402011-08-04 01:41:02 +000044
Enrico Granata7718f3f2011-08-04 02:35:14 +000045 def get_child_at_index(self,index):
Enrico Granata7e655032011-08-24 01:32:46 +000046 # LLDB itself should never query for children < 0, but this might come
47 # from someone asking for a nonexisting child and getting -1 as index
48 if index < 0:
49 return None
Enrico Granata7718f3f2011-08-04 02:35:14 +000050 if index >= self.num_children():
51 return None;
Enrico Granata7e655032011-08-24 01:32:46 +000052 # *(_M_start + index), or equivalently _M_start[index] is C++ code to
53 # read the index-th item of the vector. in Python we must make an offset
54 # that is index * sizeof(T), and then grab the value at that offset from
55 # _M_start
56 offset = index * self.data_size # index * sizeof(T)
57 return self.start.CreateChildAtOffset('['+str(index)+']',offset,self.data_type) # *(_M_start + index)
Enrico Granatac92eb402011-08-04 01:41:02 +000058
Enrico Granata7e655032011-08-24 01:32:46 +000059 # an std::vector contains an object named _M_impl, which in turn contains
60 # three pointers, _M_start, _M_end and _M_end_of_storage. _M_start points to the
61 # beginning of the data area, _M_finish points to where the current vector elements
62 # finish, and _M_end_of_storage is the end of the currently alloc'ed memory portion
63 # (to allow resizing, a vector may allocate more memory than required)
Enrico Granata7718f3f2011-08-04 02:35:14 +000064 def update(self):
65 impl = self.valobj.GetChildMemberWithName('_M_impl')
66 self.start = impl.GetChildMemberWithName('_M_start')
67 self.finish = impl.GetChildMemberWithName('_M_finish')
68 self.end = impl.GetChildMemberWithName('_M_end_of_storage')
Enrico Granata7e655032011-08-24 01:32:46 +000069 self.data_type = self.start.GetType().GetPointeeType() # _M_start is defined as a T*
70 self.data_size = self.data_type.GetByteSize() # sizeof(T)
Enrico Granata7718f3f2011-08-04 02:35:14 +000071