summaryrefslogtreecommitdiffstats
path: root/Tools/bgen/bgen/bgenGenerator.py
blob: ae5c06bcfdd482cb53eadf30e8b226b84cff27e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from bgenOutput import *
from bgenType import *
from bgenVariable import *


Error = "bgenGenerator.Error"

DEBUG=0

# Strings to specify argument transfer modes in generator calls
IN = "in"
OUT = "out"
INOUT = IN_OUT = "in-out"


class BaseFunctionGenerator:

    def __init__(self, name, condition=None, callname=None, modifiers=None):
        if DEBUG: print("<--", name)
        self.name = name
        if callname:
            self.callname = callname
        else:
            self.callname = name
        self.prefix = name
        self.objecttype = "PyObject" # Type of _self argument to function
        self.condition = condition
        self.modifiers = modifiers

    def setprefix(self, prefix):
        self.prefix = prefix

    def checkgenerate(self):
        return True

    def generate(self):
        if not self.checkgenerate():
            return
        if DEBUG: print("-->", self.name)
        if self.condition:
            Output()
            Output(self.condition)
        self.functionheader()
        self.functionbody()
        self.functiontrailer()
        if self.condition:
            Output("#endif")

    def functionheader(self):
        Output()
        Output("static PyObject *%s_%s(%s *_self, PyObject *_args)",
               self.prefix, self.name, self.objecttype)
        OutLbrace()
        Output("PyObject *_res = NULL;")

    def functionbody(self):
        Output("/* XXX To be provided */")

    def functiontrailer(self):
        OutRbrace()

    def reference(self, name = None):
        if not self.checkgenerate():
            return
        if name is None:
            name = self.name
        docstring = self.docstring()
        if self.condition:
            Output()
            Output(self.condition)
        Output("{\"%s\", (PyCFunction)%s_%s, 1,", name, self.prefix, self.name)
        Output(" PyDoc_STR(%s)},", stringify(docstring))
        if self.condition:
            Output("#endif")

    def docstring(self):
        return None

    def __cmp__(self, other):
        if not hasattr(other, 'name'):
            return cmp(id(self), id(other))
        return cmp(self.name, other.name)

_stringify_map = {'\n': '\\n', '\t': '\\t', '\r': '\\r', '\b': '\\b',
                  '\e': '\\e', '\a': '\\a', '\f': '\\f', '"': '\\"'}
def stringify(str):
    if str is None: return "NULL"
    res = '"'
    map = _stringify_map
    for c in str:
        if map.has_key(c): res = res + map[c]
        elif ' ' <= c <= '~': res = res + c
        else: res = res + '\\%03o' % ord(c)
    res = res + '"'
    return res


class ManualGenerator(BaseFunctionGenerator):

    def __init__(self, name, body, condition=None):
        BaseFunctionGenerator.__init__(self, name, condition=condition)
        self.body = body

    def functionbody(self):
        Output("%s", self.body)

    def setselftype(self, selftype, itselftype):
        self.objecttype = selftype
        self.itselftype = itselftype


class FunctionGenerator(BaseFunctionGenerator):

    def __init__(self, returntype, name, *argumentList, **conditionlist):
        BaseFunctionGenerator.__init__(self, name, **conditionlist)
        self.returntype = returntype
        self.argumentList = []
        self.setreturnvar()
        self.parseArgumentList(argumentList)
        self.prefix     = "XXX"    # Will be changed by setprefix() call
        self.itselftype = None     # Type of _self->ob_itself, if defined

    def setreturnvar(self):
        if self.returntype:
            self.rv = self.makereturnvar()
            self.argumentList.append(self.rv)
        else:
            self.rv = None

    def makereturnvar(self):
        return Variable(self.returntype, "_rv", OutMode)

    def setselftype(self, selftype, itselftype):
        self.objecttype = selftype
        self.itselftype = itselftype

    def parseArgumentList(self, argumentList):
        iarg = 0
        for type, name, mode in argumentList:
            iarg = iarg + 1
            if name is None: name = "_arg%d" % iarg
            arg = Variable(type, name, mode)
            self.argumentList.append(arg)

    def docstring(self):
        input = []
        output = []
        for arg in self.argumentList:
            if arg.flags == ErrorMode or arg.flags == SelfMode:
                continue
            if arg.type == None:
                str = 'void'
            else:
                if hasattr(arg.type, 'typeName'):
                    typeName = arg.type.typeName
                    if typeName is None: # Suppressed type
                        continue
                else:
                    typeName = "?"
                    print("Nameless type", arg.type)

                str = typeName + ' ' + arg.name
            if arg.mode in (InMode, InOutMode):
                input.append(str)
            if arg.mode in (InOutMode, OutMode):
                output.append(str)
        if not input:
            instr = "()"
        else:
            instr = "(%s)" % ", ".join(input)
        if not output or output == ["void"]:
            outstr = "None"
        else:
            outstr = "(%s)" % ", ".join(output)
        return instr + " -> " + outstr

    def functionbody(self):
        self.declarations()
        self.precheck()
        self.getargs()
        self.callit()
        self.checkit()
        self.returnvalue()

    def declarations(self):
        for arg in self.argumentList:
            arg.declare()

    def getargs(self):
        sep = ",\n" + ' '*len("if (!PyArg_ParseTuple(")
        fmt, lst = self.getargsFormatArgs(sep)
        Output("if (!PyArg_ParseTuple(_args, \"%s\"%s))", fmt, lst)
        IndentLevel()
        Output("return NULL;")
        DedentLevel()
        for arg in self.argumentList:
            if arg.flags == SelfMode:
                continue
            if arg.mode in (InMode, InOutMode):
                arg.getargsCheck()

    def getargsFormatArgs(self, sep):
        fmt = ""
        lst = ""
        for arg in self.argumentList:
            if arg.flags == SelfMode:
                continue
            if arg.mode in (InMode, InOutMode):
                arg.getargsPreCheck()
                fmt = fmt + arg.getargsFormat()
                args = arg.getargsArgs()
                if args:
                    lst = lst + sep + args
        return fmt, lst

    def precheck(self):
        pass

    def beginallowthreads(self):
        pass

    def endallowthreads(self):
        pass

    def callit(self):
        args = ""
        s = "%s%s(" % (self.getrvforcallit(), self.callname)
        sep = ",\n" + ' '*len(s)
        for arg in self.argumentList:
            if arg is self.rv:
                continue
            s = arg.passArgument()
            if args: s = sep + s
            args = args + s
        self.beginallowthreads()
        Output("%s%s(%s);",
               self.getrvforcallit(), self.callname, args)
        self.endallowthreads()

    def getrvforcallit(self):
        if self.rv:
            return "%s = " % self.rv.name
        else:
            return ""

    def checkit(self):
        for arg in self.argumentList:
            arg.errorCheck()

    def returnvalue(self):
        sep = ",\n" + ' '*len("return Py_BuildValue(")
        fmt, lst = self.mkvalueFormatArgs(sep)
        if fmt == "":
            Output("Py_INCREF(Py_None);")
            Output("_res = Py_None;");
        else:
            Output("_res = Py_BuildValue(\"%s\"%s);", fmt, lst)
        tmp = self.argumentList[:]
        tmp.reverse()
        for arg in tmp:
            if not arg: continue
            arg.cleanup()
        Output("return _res;")

    def mkvalueFormatArgs(self, sep):
        fmt = ""
        lst = ""
        for arg in self.argumentList:
            if not arg: continue
            if arg.flags == ErrorMode: continue
            if arg.mode in (OutMode, InOutMode):
                arg.mkvaluePreCheck()
                fmt = fmt + arg.mkvalueFormat()
                lst = lst + sep + arg.mkvalueArgs()
        return fmt, lst

class MethodGenerator(FunctionGenerator):

    def parseArgumentList(self, args):
        a0, args = args[0], args[1:]
        t0, n0, m0 = a0
        if m0 != InMode:
            raise ValueError, "method's 'self' must be 'InMode'"
        self.itself = Variable(t0, "_self->ob_itself", SelfMode)
        self.argumentList.append(self.itself)
        FunctionGenerator.parseArgumentList(self, args)

def _test():
    void = None
    eggs = FunctionGenerator(void, "eggs",
                 (stringptr, 'cmd', InMode),
                 (int, 'x', InMode),
                 (double, 'y', InOutMode),
                 (int, 'status', ErrorMode),
                 )
    eggs.setprefix("spam")
    print("/* START */")
    eggs.generate()


if __name__ == "__main__":
    _test()