Debian Codegraph @

pypy/module/_cppyy/helper.py

raw symbols SCIP
1 import sys
2 from rpython.rlib import rstring
3
4
5 #- type name manipulations --------------------------------------------------
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 # only replace const qualifying the class name, not in the template parameters
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("]"): # array type?
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("]"): # array type?
27 idx = name.rfind("[")
28 if 0 < idx:
29 end = len(name)-1 # len rather than -1 for rpython
30 if 0 < end and (idx+1) < end: # guarantee non-neg for rpython
31 return int(name[idx+1:end])
32 return -1
33
34 def _find_qualifier_index(name):
35 i = len(name)
36 # search from the back; note len(name) > 0 (so rtyper can use uint)
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 # can't strip const early b/c name could be a template ...
45 i = _find_qualifier_index(name)
46 name = name[:i].strip(" ")
47
48 idx = -1
49 if name.endswith("]"): # array type?
50 idx = name.rfind("[")
51 if 0 < idx:
52 name = name[:idx]
53 elif name.endswith(">"): # template type?
54 idx = name.find("<")
55 if 0 < idx: # always true, but just so that the translater knows
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 #- operator mappings --------------------------------------------------------
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 # look for known mapping
74 try:
75 return _operator_mappings[op]
76 except KeyError:
77 pass
78
79 # return-type dependent mapping
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 # a couple more cases that depend on whether args were given
88
89 if op == "*": # dereference (not python) vs. multiplication
90 return nargs and "__mul__" or "__deref__"
91
92 if op == "+": # unary positive vs. binary addition
93 return nargs and "__add__" or "__pos__"
94
95 if op == "-": # unary negative vs. binary subtraction
96 return nargs and "__sub__" or "__neg__"
97
98 if op == "++": # prefix v.s. postfix increment (not python)
99 return nargs and "__postinc__" or "__preinc__"
100
101 if op == "--": # prefix v.s. postfix decrement (not python)
102 return nargs and "__postdec__" or "__predec__"
103
104 # operator could have been a conversion using a typedef (this lookup
105 # is put at the end only as it is unlikely and may trigger unwanted
106 # errors in class loaders in the backend, because a typical operator
107 # name is illegal as a class name)
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 # might get here, as not all operator methods handled (although some with
116 # no python equivalent, such as new, delete, etc., are simply retained)
117 # TODO: perhaps absorb or "pythonify" these operators?
118 return cppname
119
120 CPPYY__div__ = "__div__"
121 CPPYY__idiv__ = "__idiv__"
122 CPPYY__long__ = "__long__"
123 CPPYY__bool__ = "__nonzero__"
124
125 # _operator_mappings["[]"] = "__setitem__" # depends on return type
126 # _operator_mappings["+"] = "__add__" # depends on # of args (see __pos__)
127 # _operator_mappings["-"] = "__sub__" # id. (eq. __neg__)
128 # _operator_mappings["*"] = "__mul__" # double meaning in C++
129
130 # _operator_mappings["[]"] = "__getitem__" # depends on return type
131 _operator_mappings["()"] = "__call__"
132 _operator_mappings["/"] = CPPYY__div__
133 _operator_mappings["%"] = "__mod__"
134 _operator_mappings["**"] = "__pow__" # not C++
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 # the following type mappings are "exact"
161 _operator_mappings["const char*"] = "__str__"
162 _operator_mappings["int"] = "__int__"
163 _operator_mappings["long"] = CPPYY__long__
164 _operator_mappings["double"] = "__float__"
165
166 # the following type mappings are "okay"; the assumption is that they
167 # are not mixed up with the ones above or between themselves (and if
168 # they are, that it is done consistently)
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 # the following are not python, but useful to expose
181 _operator_mappings["->"] = "__follow__"
182 _operator_mappings["="] = "__assign__"
183
184 # a bundle of operators that have no equivalent and are left "as-is" for now:
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[]"