Debian Codegraph @

rpython/annotator/listdef.py

raw symbols SCIP
1 from rpython.annotator.model import s_ImpossibleValue
2 from rpython.annotator.model import SomeList, SomeString
3 from rpython.annotator.model import unionof, TLS, UnionError, AnnotatorError
4
5
6 class TooLateForChange(AnnotatorError):
7 pass
8
9 class ListChangeUnallowed(AnnotatorError):
10 pass
11
12 class ListItem(object):
13 mutated = False # True for lists mutated after creation
14 resized = False # True for lists resized after creation
15 range_step = None # the step -- only for lists only created by a range()
16 dont_change_any_more = False # set to True when too late for changes
17 immutable = False # for getattr out of _immutable_fields_ = ['attr[*]']
18 must_not_resize = False # make_sure_not_resized()
19
20 # what to do if range_step is different in merge.
21 # - if one is a list (range_step is None), unify to a list.
22 # - if both have a step, unify to use a variable step (indicated by 0)
23 _step_map = {
24 (type(None), int): None,
25 (int, type(None)): None,
26 (int, int) : 0,
27 }
28
29 def __init__(self, bookkeeper, s_value):
30 self.s_value = s_value
31 self.bookkeeper = bookkeeper
32 self.itemof = {} # set of all ListDefs using this ListItem
33 self.read_locations = set()
34 if bookkeeper is None:
35 self.dont_change_any_more = True
36
37 def mutate(self):
38 if not self.mutated:
39 if self.dont_change_any_more:
40 raise TooLateForChange
41 self.immutable = False
42 self.mutated = True
43
44 def resize(self):
45 if not self.resized:
46 if self.dont_change_any_more:
47 raise TooLateForChange
48 if self.must_not_resize:
49 raise ListChangeUnallowed("resizing list")
50 self.resized = True
51
52 def setrangestep(self, step):
53 if step != self.range_step:
54 if self.dont_change_any_more:
55 raise TooLateForChange
56 self.range_step = step
57
58 def merge(self, other):
59 if self is not other:
60 if getattr(TLS, 'no_side_effects_in_union', 0):
61 raise UnionError(self, other)
62
63 if other.dont_change_any_more:
64 if self.dont_change_any_more:
65 raise TooLateForChange
66 else:
67 # lists using 'other' don't expect it to change any more,
68 # so we try merging into 'other', which will give
69 # TooLateForChange if it actually tries to make
70 # things more general
71 self, other = other, self
72
73 self.immutable &= other.immutable
74 if other.must_not_resize:
75 if self.resized:
76 raise ListChangeUnallowed("list merge with a resized")
77 self.must_not_resize = True
78 if other.mutated:
79 self.mutate()
80 if other.resized:
81 self.resize()
82 if other.range_step != self.range_step:
83 self.setrangestep(self._step_map[type(self.range_step),
84 type(other.range_step)])
85 self.itemof.update(other.itemof)
86 s_value = self.s_value
87 s_other_value = other.s_value
88 s_new_value = unionof(s_value, s_other_value)
89 if s_new_value != s_value:
90 if self.dont_change_any_more:
91 raise TooLateForChange
92 self.patch() # which should patch all refs to 'other'
93 if s_new_value != s_value:
94 self.s_value = s_new_value
95 self.notify_update()
96 if s_new_value != s_other_value:
97 other.notify_update()
98 self.read_locations |= other.read_locations
99
100 def patch(self):
101 for listdef in self.itemof:
102 listdef.listitem = self
103
104 def notify_update(self):
105 '''Reflow from all reading points'''
106 for position_key in self.read_locations:
107 self.bookkeeper.annotator.reflowfromposition(position_key)
108
109 def generalize(self, s_other_value):
110 s_new_value = unionof(self.s_value, s_other_value)
111 updated = s_new_value != self.s_value
112 if updated:
113 if self.dont_change_any_more:
114 raise TooLateForChange
115 self.s_value = s_new_value
116 self.notify_update()
117 return updated
118
119
120 class ListDef(object):
121 """A list definition remembers how general the items in that particular
122 list have to be. Every list creation makes a new ListDef, and the union
123 of two lists merges the ListItems that each ListDef stores."""
124
125 def __init__(self, bookkeeper, s_item=s_ImpossibleValue,
126 mutated=False, resized=False):
127 self.listitem = ListItem(bookkeeper, s_item)
128 self.listitem.mutated = mutated | resized
129 self.listitem.resized = resized
130 self.listitem.itemof[self] = True
131
132 def read_item(self, position_key):
133 self.listitem.read_locations.add(position_key)
134 return self.listitem.s_value
135
136 def same_as(self, other):
137 return self.listitem is other.listitem
138
139 def union(self, other):
140 self.listitem.merge(other.listitem)
141 return self
142
143 def agree(self, bookkeeper, other):
144 position = bookkeeper.position_key
145 s_self_value = self.read_item(position)
146 s_other_value = other.read_item(position)
147 self.generalize(s_other_value)
148 other.generalize(s_self_value)
149 if self.listitem.range_step is not None:
150 self.generalize_range_step(other.listitem.range_step)
151 if other.listitem.range_step is not None:
152 other.generalize_range_step(self.listitem.range_step)
153
154 def offspring(self, bookkeeper, *others):
155 position = bookkeeper.position_key
156 s_self_value = self.read_item(position)
157 s_other_values = []
158 for other in others:
159 s_other_values.append(other.read_item(position))
160 s_newlst = bookkeeper.newlist(s_self_value, *s_other_values)
161 s_newvalue = s_newlst.listdef.read_item(position)
162 self.generalize(s_newvalue)
163 for other in others:
164 other.generalize(s_newvalue)
165 return s_newlst
166
167 def generalize(self, s_value):
168 self.listitem.generalize(s_value)
169
170 def generalize_range_step(self, range_step):
171 newlistitem = ListItem(self.listitem.bookkeeper, s_ImpossibleValue)
172 newlistitem.range_step = range_step
173 self.listitem.merge(newlistitem)
174
175 def __repr__(self):
176 return '<[%r]%s%s%s%s>' % (self.listitem.s_value,
177 self.listitem.mutated and 'm' or '',
178 self.listitem.resized and 'r' or '',
179 self.listitem.immutable and 'I' or '',
180 self.listitem.must_not_resize and '!R' or '')
181
182 def mutate(self):
183 self.listitem.mutate()
184
185 def resize(self):
186 self.listitem.mutate()
187 self.listitem.resize()
188
189 def never_resize(self):
190 if self.listitem.resized:
191 raise ListChangeUnallowed("list already resized")
192 self.listitem.must_not_resize = True
193
194 def mark_as_immutable(self):
195 # Sets the 'immutable' flag. Note that unlike "never resized",
196 # the immutable flag is only a hint. It is cleared again e.g.
197 # when we merge with a "normal" list that doesn't have it. It
198 # is thus expected to live only shortly, mostly for the case
199 # of writing 'x.list[n]'.
200 self.never_resize()
201 if not self.listitem.mutated:
202 self.listitem.immutable = True
203 #else: it's fine, don't set immutable=True at all (see
204 # test_can_merge_immutable_list_with_regular_list)
205
206 s_list_of_strings = SomeList(ListDef(None, SomeString(no_nul=True),
207 resized = True))