| 1 |
from rpython.annotator import model as annmodel |
| 2 |
from rpython.rtyper import rmodel |
| 3 |
from rpython.rtyper.lltypesystem import lltype |
| 4 |
|
| 5 |
|
| 6 |
class __extend__(annmodel.SomeDict): |
| 7 |
def get_dict_repr(self): |
| 8 |
from rpython.rtyper.lltypesystem.rdict import DictRepr |
| 9 |
|
| 10 |
return DictRepr |
| 11 |
|
| 12 |
def rtyper_makerepr(self, rtyper): |
| 13 |
dictkey = self.dictdef.dictkey |
| 14 |
dictvalue = self.dictdef.dictvalue |
| 15 |
s_key = dictkey.s_value |
| 16 |
s_value = dictvalue.s_value |
| 17 |
force_non_null = self.dictdef.force_non_null |
| 18 |
simple_hash_eq = self.dictdef.simple_hash_eq |
| 19 |
if dictkey.custom_eq_hash: |
| 20 |
custom_eq_hash = lambda: (rtyper.getrepr(dictkey.s_rdict_eqfn), |
| 21 |
rtyper.getrepr(dictkey.s_rdict_hashfn)) |
| 22 |
else: |
| 23 |
custom_eq_hash = None |
| 24 |
return self.get_dict_repr()(rtyper, lambda: rtyper.getrepr(s_key), |
| 25 |
lambda: rtyper.getrepr(s_value), dictkey, dictvalue, |
| 26 |
custom_eq_hash, force_non_null, simple_hash_eq) |
| 27 |
|
| 28 |
def rtyper_makekey(self): |
| 29 |
self.dictdef.dictkey .dont_change_any_more = True |
| 30 |
self.dictdef.dictvalue.dont_change_any_more = True |
| 31 |
return (self.__class__, self.dictdef.dictkey, self.dictdef.dictvalue) |
| 32 |
|
| 33 |
class __extend__(annmodel.SomeOrderedDict): |
| 34 |
def get_dict_repr(self): |
| 35 |
from rpython.rtyper.lltypesystem.rordereddict import OrderedDictRepr |
| 36 |
|
| 37 |
return OrderedDictRepr |
| 38 |
|
| 39 |
class AbstractDictRepr(rmodel.Repr): |
| 40 |
|
| 41 |
def pickrepr(self, item_repr): |
| 42 |
if self.custom_eq_hash: |
| 43 |
return item_repr, item_repr |
| 44 |
else: |
| 45 |
return self._externalvsinternal(self.rtyper, item_repr) |
| 46 |
|
| 47 |
pickkeyrepr = pickrepr |
| 48 |
|
| 49 |
def compact_repr(self): |
| 50 |
return 'DictR %s %s' % (self.key_repr.compact_repr(), |
| 51 |
self.value_repr.compact_repr()) |
| 52 |
|
| 53 |
def recast_value(self, llops, v): |
| 54 |
return llops.convertvar(v, self.value_repr, self.external_value_repr) |
| 55 |
|
| 56 |
def recast_key(self, llops, v): |
| 57 |
return llops.convertvar(v, self.key_repr, self.external_key_repr) |
| 58 |
|
| 59 |
|
| 60 |
def rtype_newdict(hop): |
| 61 |
hop.inputargs() |
| 62 |
r_dict = hop.r_result |
| 63 |
cDICT = hop.inputconst(lltype.Void, r_dict.DICT) |
| 64 |
v_result = hop.gendirectcall(r_dict.ll_newdict, cDICT) |
| 65 |
return v_result |
| 66 |
|
| 67 |
|
| 68 |
class AbstractDictIteratorRepr(rmodel.IteratorRepr): |
| 69 |
|
| 70 |
def newiter(self, hop): |
| 71 |
v_dict, = hop.inputargs(self.r_dict) |
| 72 |
citerptr = hop.inputconst(lltype.Void, self.lowleveltype) |
| 73 |
return hop.gendirectcall(self.ll_dictiter, citerptr, v_dict) |
| 74 |
|
| 75 |
def rtype_next(self, hop): |
| 76 |
v_iter, = hop.inputargs(self) |
| 77 |
|
| 78 |
hop.has_implicit_exception(StopIteration) |
| 79 |
hop.has_implicit_exception(RuntimeError) |
| 80 |
hop.exception_is_here() |
| 81 |
v_index = hop.gendirectcall(self._ll_dictnext, v_iter) |
| 82 |
|
| 83 |
|
| 84 |
DICT = self.lowleveltype.TO.dict |
| 85 |
c_dict = hop.inputconst(lltype.Void, 'dict') |
| 86 |
v_dict = hop.genop('getfield', [v_iter, c_dict], resulttype=DICT) |
| 87 |
ENTRIES = DICT.TO.entries |
| 88 |
c_entries = hop.inputconst(lltype.Void, 'entries') |
| 89 |
v_entries = hop.genop('getfield', [v_dict, c_entries], |
| 90 |
resulttype=ENTRIES) |
| 91 |
|
| 92 |
method = getattr(self, 'variant_' + self.variant) |
| 93 |
return method(hop, ENTRIES, v_entries, v_dict, v_index) |
| 94 |
|
| 95 |
def get_tuple_result(self, hop, items_v): |
| 96 |
|
| 97 |
|
| 98 |
if hop.r_result.lowleveltype is lltype.Void: |
| 99 |
return hop.inputconst(lltype.Void, None) |
| 100 |
c1 = hop.inputconst(lltype.Void, hop.r_result.lowleveltype.TO) |
| 101 |
cflags = hop.inputconst(lltype.Void, {'flavor': 'gc'}) |
| 102 |
v_result = hop.genop('malloc', [c1, cflags], |
| 103 |
resulttype = hop.r_result.lowleveltype) |
| 104 |
for i, v_item in enumerate(items_v): |
| 105 |
ITEM = getattr(v_result.concretetype.TO, 'item%d' % i) |
| 106 |
if ITEM != v_item.concretetype: |
| 107 |
assert isinstance(ITEM, lltype.Ptr) |
| 108 |
v_item = hop.genop('cast_pointer', [v_item], resulttype=ITEM) |
| 109 |
c_item = hop.inputconst(lltype.Void, 'item%d' % i) |
| 110 |
hop.genop('setfield', [v_result, c_item, v_item]) |
| 111 |
return v_result |
| 112 |
|
| 113 |
def variant_keys(self, hop, ENTRIES, v_entries, v_dict, v_index): |
| 114 |
KEY = ENTRIES.TO.OF.key |
| 115 |
c_key = hop.inputconst(lltype.Void, 'key') |
| 116 |
v_key = hop.genop('getinteriorfield', [v_entries, v_index, c_key], |
| 117 |
resulttype=KEY) |
| 118 |
return self.r_dict.recast_key(hop.llops, v_key) |
| 119 |
|
| 120 |
variant_reversed = variant_keys |
| 121 |
|
| 122 |
def variant_values(self, hop, ENTRIES, v_entries, v_dict, v_index): |
| 123 |
VALUE = ENTRIES.TO.OF.value |
| 124 |
c_value = hop.inputconst(lltype.Void, 'value') |
| 125 |
v_value = hop.genop('getinteriorfield', [v_entries,v_index,c_value], |
| 126 |
resulttype=VALUE) |
| 127 |
return self.r_dict.recast_value(hop.llops, v_value) |
| 128 |
|
| 129 |
def variant_items(self, hop, ENTRIES, v_entries, v_dict, v_index): |
| 130 |
v_key = self.variant_keys(hop, ENTRIES, v_entries, v_dict, v_index) |
| 131 |
v_value = self.variant_values(hop, ENTRIES, v_entries, v_dict, v_index) |
| 132 |
return self.get_tuple_result(hop, (v_key, v_value)) |
| 133 |
|
| 134 |
def variant_hashes(self, hop, ENTRIES, v_entries, v_dict, v_index): |
| 135 |
|
| 136 |
|
| 137 |
return hop.gendirectcall(ENTRIES.TO.entry_hash, v_entries, v_dict, v_index) |
| 138 |
|
| 139 |
def variant_keys_with_hash(self, hop, ENTRIES, v_entries, v_dict, v_index): |
| 140 |
v_key = self.variant_keys(hop, ENTRIES, v_entries, v_dict, v_index) |
| 141 |
v_hash = self.variant_hashes(hop, ENTRIES, v_entries, v_dict, v_index) |
| 142 |
return self.get_tuple_result(hop, (v_key, v_hash)) |
| 143 |
|
| 144 |
def variant_items_with_hash(self, hop, ENTRIES, v_entries, v_dict, v_index): |
| 145 |
v_key = self.variant_keys(hop, ENTRIES, v_entries, v_dict, v_index) |
| 146 |
v_value = self.variant_values(hop, ENTRIES, v_entries, v_dict, v_index) |
| 147 |
v_hash = self.variant_hashes(hop, ENTRIES, v_entries, v_dict, v_index) |
| 148 |
return self.get_tuple_result(hop, (v_key, v_value, v_hash)) |