| 1 | from rpython.annotator.model import ( |
| 2 | s_ImpossibleValue, SomeInteger, s_Bool, union, AnnotatorError) |
| 3 | from rpython.annotator.listdef import ListItem |
| 4 | from rpython.rlib.objectmodel import compute_hash |
| 5 | |
| 6 | |
| 7 | class DictKey(ListItem): |
| 8 | s_rdict_eqfn = s_ImpossibleValue |
| 9 | s_rdict_hashfn = s_ImpossibleValue |
| 10 | |
| 11 | def __init__(self, bookkeeper, s_value, is_r_dict=False): |
| 12 | ListItem.__init__(self, bookkeeper, s_value) |
| 13 | self.custom_eq_hash = is_r_dict |
| 14 | |
| 15 | def patch(self): |
| 16 | for dictdef in self.itemof: |
| 17 | dictdef.dictkey = self |
| 18 | |
| 19 | def merge(self, other): |
| 20 | if self is not other: |
| 21 | assert self.custom_eq_hash == other.custom_eq_hash, ( |
| 22 | "mixing plain dictionaries with r_dict()") |
| 23 | ListItem.merge(self, other) |
| 24 | if self.custom_eq_hash: |
| 25 | self.update_rdict_annotations(other.s_rdict_eqfn, |
| 26 | other.s_rdict_hashfn, |
| 27 | other=other) |
| 28 | |
| 29 | def generalize(self, s_other_value): |
| 30 | updated = ListItem.generalize(self, s_other_value) |
| 31 | if updated and self.custom_eq_hash: |
| 32 | self.emulate_rdict_calls() |
| 33 | return updated |
| 34 | |
| 35 | def update_rdict_annotations(self, s_eqfn, s_hashfn, other=None): |
| 36 | assert self.custom_eq_hash |
| 37 | s_eqfn = union(s_eqfn, self.s_rdict_eqfn) |
| 38 | s_hashfn = union(s_hashfn, self.s_rdict_hashfn) |
| 39 | self.s_rdict_eqfn = s_eqfn |
| 40 | self.s_rdict_hashfn = s_hashfn |
| 41 | self.emulate_rdict_calls(other=other) |
| 42 | |
| 43 | def emulate_rdict_calls(self, other=None): |
| 44 | myeq = (self, 'eq') |
| 45 | myhash = (self, 'hash') |
| 46 | if other: |
| 47 | replace_othereq = [(other, 'eq')] |
| 48 | replace_otherhash = [(other, 'hash')] |
| 49 | else: |
| 50 | replace_othereq = replace_otherhash = () |
| 51 | |
| 52 | s_key = self.s_value |
| 53 | |
| 54 | s = self.bookkeeper.emulate_pbc_call( |
| 55 | myeq, self.s_rdict_eqfn, [s_key, s_key], replace=replace_othereq) |
| 56 | if not s_Bool.contains(s): |
| 57 | raise AnnotatorError( |
| 58 | "the custom eq function of an r_dict must return a boolean" |
| 59 | " (got %r)" % (s,)) |
| 60 | |
| 61 | s = self.bookkeeper.emulate_pbc_call( |
| 62 | myhash, self.s_rdict_hashfn, [s_key], replace=replace_otherhash) |
| 63 | if not SomeInteger().contains(s): |
| 64 | raise AnnotatorError( |
| 65 | "the custom hash function of an r_dict must return an integer" |
| 66 | " (got %r)" % (s,)) |
| 67 | |
| 68 | |
| 69 | class DictValue(ListItem): |
| 70 | def patch(self): |
| 71 | for dictdef in self.itemof: |
| 72 | dictdef.dictvalue = self |
| 73 | |
| 74 | |
| 75 | class DictDef(object): |
| 76 | """A dict definition remembers how general the keys and values in that |
| 77 | particular dict have to be. Every dict creation makes a new DictDef, |
| 78 | and the union of two dicts merges the DictKeys and DictValues that each |
| 79 | DictDef stores.""" |
| 80 | |
| 81 | def __init__(self, bookkeeper, s_key = s_ImpossibleValue, |
| 82 | s_value = s_ImpossibleValue, |
| 83 | is_r_dict = False, |
| 84 | force_non_null = False, |
| 85 | simple_hash_eq = False): |
| 86 | self.dictkey = DictKey(bookkeeper, s_key, is_r_dict) |
| 87 | self.dictkey.itemof[self] = True |
| 88 | self.dictvalue = DictValue(bookkeeper, s_value) |
| 89 | self.dictvalue.itemof[self] = True |
| 90 | self.force_non_null = force_non_null |
| 91 | self.simple_hash_eq = simple_hash_eq |
| 92 | |
| 93 | def read_key(self, position_key): |
| 94 | self.dictkey.read_locations.add(position_key) |
| 95 | return self.dictkey.s_value |
| 96 | |
| 97 | def read_value(self, position_key): |
| 98 | self.dictvalue.read_locations.add(position_key) |
| 99 | return self.dictvalue.s_value |
| 100 | |
| 101 | def same_as(self, other): |
| 102 | return (self.dictkey is other.dictkey and |
| 103 | self.dictvalue is other.dictvalue) |
| 104 | |
| 105 | def union(self, other): |
| 106 | self.dictkey.merge(other.dictkey) |
| 107 | self.dictvalue.merge(other.dictvalue) |
| 108 | return self |
| 109 | |
| 110 | def generalize_key(self, s_key): |
| 111 | self.dictkey.generalize(s_key) |
| 112 | |
| 113 | def generalize_value(self, s_value): |
| 114 | self.dictvalue.generalize(s_value) |
| 115 | |
| 116 | def __repr__(self): |
| 117 | return '<{%r: %r}>' % (self.dictkey.s_value, self.dictvalue.s_value) |