| 1 |
import sys |
| 2 |
from rpython.rlib import rstring |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
def remove_const(name): |
| 7 |
tmplt_start = name.find("<") |
| 8 |
tmplt_stop = name.rfind(">") |
| 9 |
if 0 <= tmplt_start and 0 <= tmplt_stop: |
| 10 |
|
| 11 |
return "".join([x.strip(" ") for x in rstring.split(name[:tmplt_start], "const")])+\ |
| 12 |
name[tmplt_start:tmplt_stop]+\ |
| 13 |
"".join([x.strip(" ") for x in rstring.split(name[tmplt_stop:], "const")]) |
| 14 |
else: |
| 15 |
return "".join([x.strip(" ") for x in rstring.split(name, "const")]) |
| 16 |
|
| 17 |
def compound(name): |
| 18 |
name = remove_const(name) |
| 19 |
if name.endswith("]"): |
| 20 |
return "[]" |
| 21 |
i = _find_qualifier_index(name) |
| 22 |
return "".join(name[i:].split(" ")) |
| 23 |
|
| 24 |
def array_size(name): |
| 25 |
name = remove_const(name) |
| 26 |
if name.endswith("]"): |
| 27 |
idx = name.rfind("[") |
| 28 |
if 0 < idx: |
| 29 |
end = len(name)-1 |
| 30 |
if 0 < end and (idx+1) < end: |
| 31 |
return int(name[idx+1:end]) |
| 32 |
return -1 |
| 33 |
|
| 34 |
def _find_qualifier_index(name): |
| 35 |
i = len(name) |
| 36 |
|
| 37 |
for i in range(len(name) - 1, 0, -1): |
| 38 |
c = name[i] |
| 39 |
if c.isalnum() or c in ['_', '>', ']', ')']: |
| 40 |
break |
| 41 |
return i + 1 |
| 42 |
|
| 43 |
def clean_type(name): |
| 44 |
|
| 45 |
i = _find_qualifier_index(name) |
| 46 |
name = name[:i].strip(" ") |
| 47 |
|
| 48 |
idx = -1 |
| 49 |
if name.endswith("]"): |
| 50 |
idx = name.rfind("[") |
| 51 |
if 0 < idx: |
| 52 |
name = name[:idx] |
| 53 |
elif name.endswith(">"): |
| 54 |
idx = name.find("<") |
| 55 |
if 0 < idx: |
| 56 |
n1 = remove_const(name[:idx]) |
| 57 |
name = "".join([n1, name[idx:]]) |
| 58 |
else: |
| 59 |
name = remove_const(name) |
| 60 |
name = name[:_find_qualifier_index(name)] |
| 61 |
return name.strip(' ') |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
_operator_mappings = {} |
| 66 |
|
| 67 |
def map_operator_name(space, cppname, nargs, result_type): |
| 68 |
from pypy.module._cppyy import capi |
| 69 |
|
| 70 |
if cppname[0:8] == "operator": |
| 71 |
op = cppname[8:].strip(' ') |
| 72 |
|
| 73 |
|
| 74 |
try: |
| 75 |
return _operator_mappings[op] |
| 76 |
except KeyError: |
| 77 |
pass |
| 78 |
|
| 79 |
|
| 80 |
if op == "[]": |
| 81 |
if result_type.find("const") != 0: |
| 82 |
cpd = compound(result_type) |
| 83 |
if cpd and cpd[len(cpd)-1] == "&": |
| 84 |
return "__setitem__" |
| 85 |
return "__getitem__" |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
if op == "*": |
| 90 |
return nargs and "__mul__" or "__deref__" |
| 91 |
|
| 92 |
if op == "+": |
| 93 |
return nargs and "__add__" or "__pos__" |
| 94 |
|
| 95 |
if op == "-": |
| 96 |
return nargs and "__sub__" or "__neg__" |
| 97 |
|
| 98 |
if op == "++": |
| 99 |
return nargs and "__postinc__" or "__preinc__" |
| 100 |
|
| 101 |
if op == "--": |
| 102 |
return nargs and "__postdec__" or "__predec__" |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
true_op = capi.c_resolve_name(space, op) |
| 109 |
|
| 110 |
try: |
| 111 |
return _operator_mappings[true_op] |
| 112 |
except KeyError: |
| 113 |
pass |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
return cppname |
| 119 |
|
| 120 |
CPPYY__div__ = "__div__" |
| 121 |
CPPYY__idiv__ = "__idiv__" |
| 122 |
CPPYY__long__ = "__long__" |
| 123 |
CPPYY__bool__ = "__nonzero__" |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
_operator_mappings["()"] = "__call__" |
| 132 |
_operator_mappings["/"] = CPPYY__div__ |
| 133 |
_operator_mappings["%"] = "__mod__" |
| 134 |
_operator_mappings["**"] = "__pow__" |
| 135 |
_operator_mappings["<<"] = "__lshift__" |
| 136 |
_operator_mappings[">>"] = "__rshift__" |
| 137 |
_operator_mappings["&"] = "__and__" |
| 138 |
_operator_mappings["|"] = "__or__" |
| 139 |
_operator_mappings["^"] = "__xor__" |
| 140 |
_operator_mappings["~"] = "__inv__" |
| 141 |
_operator_mappings["!"] = "__nonzero__" |
| 142 |
_operator_mappings["+="] = "__iadd__" |
| 143 |
_operator_mappings["-="] = "__isub__" |
| 144 |
_operator_mappings["*="] = "__imul__" |
| 145 |
_operator_mappings["/="] = CPPYY__idiv__ |
| 146 |
_operator_mappings["%="] = "__imod__" |
| 147 |
_operator_mappings["**="] = "__ipow__" |
| 148 |
_operator_mappings["<<="] = "__ilshift__" |
| 149 |
_operator_mappings[">>="] = "__irshift__" |
| 150 |
_operator_mappings["&="] = "__iand__" |
| 151 |
_operator_mappings["|="] = "__ior__" |
| 152 |
_operator_mappings["^="] = "__ixor__" |
| 153 |
_operator_mappings["=="] = "__eq__" |
| 154 |
_operator_mappings["!="] = "__ne__" |
| 155 |
_operator_mappings[">"] = "__gt__" |
| 156 |
_operator_mappings["<"] = "__lt__" |
| 157 |
_operator_mappings[">="] = "__ge__" |
| 158 |
_operator_mappings["<="] = "__le__" |
| 159 |
|
| 160 |
|
| 161 |
_operator_mappings["const char*"] = "__str__" |
| 162 |
_operator_mappings["int"] = "__int__" |
| 163 |
_operator_mappings["long"] = CPPYY__long__ |
| 164 |
_operator_mappings["double"] = "__float__" |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
_operator_mappings["char*"] = "__str__" |
| 170 |
_operator_mappings["short"] = "__int__" |
| 171 |
_operator_mappings["unsigned short"] = "__int__" |
| 172 |
_operator_mappings["unsigned int"] = CPPYY__long__ |
| 173 |
_operator_mappings["unsigned long"] = CPPYY__long__ |
| 174 |
_operator_mappings["long long"] = CPPYY__long__ |
| 175 |
_operator_mappings["unsigned long long"] = CPPYY__long__ |
| 176 |
_operator_mappings["float"] = "__float__" |
| 177 |
|
| 178 |
_operator_mappings["bool"] = CPPYY__bool__ |
| 179 |
|
| 180 |
|
| 181 |
_operator_mappings["->"] = "__follow__" |
| 182 |
_operator_mappings["="] = "__assign__" |
| 183 |
|
| 184 |
|
| 185 |
_operator_mappings["&&"] = "&&" |
| 186 |
_operator_mappings["||"] = "||" |
| 187 |
_operator_mappings["new"] = "new" |
| 188 |
_operator_mappings["delete"] = "delete" |
| 189 |
_operator_mappings["new[]"] = "new[]" |
| 190 |
_operator_mappings["delete[]"] = "delete[]" |