1d169d70bSTobias Grosser /*
2d169d70bSTobias Grosser * Copyright 2011,2015 Sven Verdoolaege. All rights reserved.
3d169d70bSTobias Grosser *
4d169d70bSTobias Grosser * Redistribution and use in source and binary forms, with or without
5d169d70bSTobias Grosser * modification, are permitted provided that the following conditions
6d169d70bSTobias Grosser * are met:
7d169d70bSTobias Grosser *
8d169d70bSTobias Grosser * 1. Redistributions of source code must retain the above copyright
9d169d70bSTobias Grosser * notice, this list of conditions and the following disclaimer.
10d169d70bSTobias Grosser *
11d169d70bSTobias Grosser * 2. Redistributions in binary form must reproduce the above
12d169d70bSTobias Grosser * copyright notice, this list of conditions and the following
13d169d70bSTobias Grosser * disclaimer in the documentation and/or other materials provided
14d169d70bSTobias Grosser * with the distribution.
15d169d70bSTobias Grosser *
16d169d70bSTobias Grosser * THIS SOFTWARE IS PROVIDED BY SVEN VERDOOLAEGE ''AS IS'' AND ANY
17d169d70bSTobias Grosser * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d169d70bSTobias Grosser * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19d169d70bSTobias Grosser * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SVEN VERDOOLAEGE OR
20d169d70bSTobias Grosser * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21d169d70bSTobias Grosser * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22d169d70bSTobias Grosser * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23d169d70bSTobias Grosser * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24d169d70bSTobias Grosser * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25d169d70bSTobias Grosser * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26d169d70bSTobias Grosser * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27d169d70bSTobias Grosser *
28d169d70bSTobias Grosser * The views and conclusions contained in the software and documentation
29d169d70bSTobias Grosser * are those of the authors and should not be interpreted as
30d169d70bSTobias Grosser * representing official policies, either expressed or implied, of
31d169d70bSTobias Grosser * Sven Verdoolaege.
32d169d70bSTobias Grosser */
33d169d70bSTobias Grosser
34d169d70bSTobias Grosser #include "isl_config.h"
35d169d70bSTobias Grosser
36e8227804SMichael Kruse #include <stdarg.h>
37d169d70bSTobias Grosser #include <stdio.h>
38e8227804SMichael Kruse
39e8227804SMichael Kruse #include <algorithm>
40d169d70bSTobias Grosser #include <iostream>
41d169d70bSTobias Grosser #include <map>
42d169d70bSTobias Grosser #include <vector>
43d169d70bSTobias Grosser
44d169d70bSTobias Grosser #include "python.h"
45d169d70bSTobias Grosser #include "generator.h"
46d169d70bSTobias Grosser
47e8227804SMichael Kruse /* Argument format for Python methods with a fixed number of arguments.
48e8227804SMichael Kruse */
49e8227804SMichael Kruse static const char *fixed_arg_fmt = "arg%d";
50e8227804SMichael Kruse /* Argument format for Python methods with a variable number of arguments.
51e8227804SMichael Kruse */
52e8227804SMichael Kruse static const char *var_arg_fmt = "args[%d]";
53e8227804SMichael Kruse
54d169d70bSTobias Grosser /* Drop the "isl_" initial part of the type name "name".
55d169d70bSTobias Grosser */
type2python(string name)56d169d70bSTobias Grosser static string type2python(string name)
57d169d70bSTobias Grosser {
58d169d70bSTobias Grosser return name.substr(4);
59d169d70bSTobias Grosser }
60d169d70bSTobias Grosser
61e8227804SMichael Kruse /* Print the arguments of a method with "n_arg" arguments, starting at "first".
62e8227804SMichael Kruse */
print_method_arguments(int first,int n_arg)63e8227804SMichael Kruse void python_generator::print_method_arguments(int first, int n_arg)
64e8227804SMichael Kruse {
65e8227804SMichael Kruse for (int i = first; i < n_arg; ++i) {
66e8227804SMichael Kruse if (i > first)
67e8227804SMichael Kruse printf(", ");
68e8227804SMichael Kruse printf("arg%d", i);
69e8227804SMichael Kruse }
70e8227804SMichael Kruse }
71e8227804SMichael Kruse
72e8227804SMichael Kruse /* Print the start of a definition for method "name"
73e8227804SMichael Kruse * (without specifying the arguments).
74d169d70bSTobias Grosser * If "is_static" is set, then mark the python method as static.
75d169d70bSTobias Grosser *
76d169d70bSTobias Grosser * If the method is called "from", then rename it to "convert_from"
77d169d70bSTobias Grosser * because "from" is a python keyword.
78d169d70bSTobias Grosser */
print_method_def(bool is_static,const string & name)79e8227804SMichael Kruse static void print_method_def(bool is_static, const string &name)
80d169d70bSTobias Grosser {
81d169d70bSTobias Grosser const char *s;
82d169d70bSTobias Grosser
83d169d70bSTobias Grosser if (is_static)
84d169d70bSTobias Grosser printf(" @staticmethod\n");
85d169d70bSTobias Grosser
86d169d70bSTobias Grosser s = name.c_str();
87d169d70bSTobias Grosser if (name == "from")
88d169d70bSTobias Grosser s = "convert_from";
89d169d70bSTobias Grosser
90e8227804SMichael Kruse printf(" def %s", s);
91d169d70bSTobias Grosser }
92e8227804SMichael Kruse
93e8227804SMichael Kruse /* Print the header of the method "name" with "n_arg" arguments.
94e8227804SMichael Kruse * If "is_static" is set, then mark the python method as static.
95e8227804SMichael Kruse */
print_method_header(bool is_static,const string & name,int n_arg)96e8227804SMichael Kruse void python_generator::print_method_header(bool is_static, const string &name,
97e8227804SMichael Kruse int n_arg)
98e8227804SMichael Kruse {
99e8227804SMichael Kruse print_method_def(is_static, name);
100e8227804SMichael Kruse printf("(");
101e8227804SMichael Kruse print_method_arguments(0, n_arg);
102d169d70bSTobias Grosser printf("):\n");
103d169d70bSTobias Grosser }
104d169d70bSTobias Grosser
105e8227804SMichael Kruse /* Print formatted output with the given indentation.
106e8227804SMichael Kruse */
print_indent(int indent,const char * format,...)107e8227804SMichael Kruse static void print_indent(int indent, const char *format, ...)
108e8227804SMichael Kruse {
109e8227804SMichael Kruse va_list args;
110e8227804SMichael Kruse
111e8227804SMichael Kruse printf("%*s", indent, " ");
112e8227804SMichael Kruse va_start(args, format);
113e8227804SMichael Kruse vprintf(format, args);
114e8227804SMichael Kruse va_end(args);
115e8227804SMichael Kruse }
116e8227804SMichael Kruse
117e8227804SMichael Kruse /* Print a check that the argument in position "pos" is of type "type"
118e8227804SMichael Kruse * with the given indentation.
119d169d70bSTobias Grosser * If this fails and if "upcast" is set, then convert the first
120d169d70bSTobias Grosser * argument to "super" and call the method "name" on it, passing
121d169d70bSTobias Grosser * the remaining of the "n" arguments.
122d169d70bSTobias Grosser * If the check fails and "upcast" is not set, then simply raise
123d169d70bSTobias Grosser * an exception.
124d169d70bSTobias Grosser * If "upcast" is not set, then the "super", "name" and "n" arguments
125d169d70bSTobias Grosser * to this function are ignored.
126e8227804SMichael Kruse * "fmt" is the format for printing Python method arguments.
127d169d70bSTobias Grosser */
print_type_check(int indent,const string & type,const char * fmt,int pos,bool upcast,const string & super,const string & name,int n)128e8227804SMichael Kruse void python_generator::print_type_check(int indent, const string &type,
129e8227804SMichael Kruse const char *fmt, int pos, bool upcast, const string &super,
130e8227804SMichael Kruse const string &name, int n)
131d169d70bSTobias Grosser {
132e8227804SMichael Kruse print_indent(indent, "try:\n");
133e8227804SMichael Kruse print_indent(indent, " if not ");
134e8227804SMichael Kruse printf(fmt, pos);
135e8227804SMichael Kruse printf(".__class__ is %s:\n", type.c_str());
136e8227804SMichael Kruse print_indent(indent, " ");
137e8227804SMichael Kruse printf(fmt, pos);
138e8227804SMichael Kruse printf(" = %s(", type.c_str());
139e8227804SMichael Kruse printf(fmt, pos);
140e8227804SMichael Kruse printf(")\n");
141e8227804SMichael Kruse print_indent(indent, "except:\n");
142d169d70bSTobias Grosser if (upcast) {
143e8227804SMichael Kruse print_indent(indent, " return %s(",
144e8227804SMichael Kruse type2python(super).c_str());
145e8227804SMichael Kruse printf(fmt, 0);
146e8227804SMichael Kruse printf(").%s(", name.c_str());
147d169d70bSTobias Grosser for (int i = 1; i < n; ++i) {
148d169d70bSTobias Grosser if (i != 1)
149d169d70bSTobias Grosser printf(", ");
150e8227804SMichael Kruse printf(fmt, i);
151d169d70bSTobias Grosser }
152d169d70bSTobias Grosser printf(")\n");
153d169d70bSTobias Grosser } else
154e8227804SMichael Kruse print_indent(indent, " raise\n");
155e8227804SMichael Kruse }
156e8227804SMichael Kruse
157e8227804SMichael Kruse /* For each of the "n" initial arguments of the function "method"
158e8227804SMichael Kruse * that refer to an isl structure,
159e8227804SMichael Kruse * including the object on which the method is called,
160e8227804SMichael Kruse * check if the corresponding actual argument is of the right type.
161e8227804SMichael Kruse * If not, try and convert it to the right type.
162e8227804SMichael Kruse * If that doesn't work and if "super" contains at least one element,
163e8227804SMichael Kruse * try and convert self to the type of the first superclass in "super" and
164e8227804SMichael Kruse * call the corresponding method.
165e8227804SMichael Kruse * If "first_is_ctx" is set, then the first argument is skipped.
166e8227804SMichael Kruse */
print_type_checks(const string & cname,FunctionDecl * method,bool first_is_ctx,int n,const vector<string> & super)167e8227804SMichael Kruse void python_generator::print_type_checks(const string &cname,
168e8227804SMichael Kruse FunctionDecl *method, bool first_is_ctx, int n,
169e8227804SMichael Kruse const vector<string> &super)
170e8227804SMichael Kruse {
171e8227804SMichael Kruse for (int i = first_is_ctx; i < n; ++i) {
172e8227804SMichael Kruse ParmVarDecl *param = method->getParamDecl(i);
173e8227804SMichael Kruse string type;
174e8227804SMichael Kruse
175e8227804SMichael Kruse if (!is_isl_type(param->getOriginalType()))
176e8227804SMichael Kruse continue;
177e8227804SMichael Kruse type = type2python(extract_type(param->getOriginalType()));
178e8227804SMichael Kruse if (!first_is_ctx && i > 0 && super.size() > 0)
179e8227804SMichael Kruse print_type_check(8, type, fixed_arg_fmt,
180e8227804SMichael Kruse i - first_is_ctx, true,
181e8227804SMichael Kruse super[0], cname, n);
182e8227804SMichael Kruse else
183e8227804SMichael Kruse print_type_check(8, type, fixed_arg_fmt,
184e8227804SMichael Kruse i - first_is_ctx, false, "", cname, -1);
185e8227804SMichael Kruse }
186d169d70bSTobias Grosser }
187d169d70bSTobias Grosser
1886145b11cSTobias Grosser /* Print a call to the *_copy function corresponding to "type".
1896145b11cSTobias Grosser */
print_copy(QualType type)1906145b11cSTobias Grosser void python_generator::print_copy(QualType type)
1916145b11cSTobias Grosser {
1926145b11cSTobias Grosser string type_s = extract_type(type);
1936145b11cSTobias Grosser
1946145b11cSTobias Grosser printf("isl.%s_copy", type_s.c_str());
1956145b11cSTobias Grosser }
1966145b11cSTobias Grosser
1976145b11cSTobias Grosser /* Construct a wrapper for callback argument "param" (at position "arg").
198d169d70bSTobias Grosser * Assign the wrapper to "cb". We assume here that a function call
199d169d70bSTobias Grosser * has at most one callback argument.
200d169d70bSTobias Grosser *
2016145b11cSTobias Grosser * The wrapper converts the arguments of the callback to python types,
2026145b11cSTobias Grosser * taking a copy if the C callback does not take its arguments.
203d169d70bSTobias Grosser * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
204e8227804SMichael Kruse * and returns a value indicating an error. Otherwise the wrapper
205e8227804SMichael Kruse * returns a value indicating success.
206e8227804SMichael Kruse * In case the C callback is expected to return an isl_stat,
207e8227804SMichael Kruse * the error value is -1 and the success value is 0.
208e8227804SMichael Kruse * In case the C callback is expected to return an isl_bool,
209e8227804SMichael Kruse * the error value is -1 and the success value is 1 or 0 depending
210e8227804SMichael Kruse * on the result of the Python callback.
211e8227804SMichael Kruse * Otherwise, None is returned to indicate an error and
212e8227804SMichael Kruse * a copy of the object in case of success.
213d169d70bSTobias Grosser */
print_callback(ParmVarDecl * param,int arg)2146145b11cSTobias Grosser void python_generator::print_callback(ParmVarDecl *param, int arg)
215d169d70bSTobias Grosser {
21626269d34STobias Grosser QualType type = param->getOriginalType();
21726269d34STobias Grosser const FunctionProtoType *fn = extract_prototype(type);
218e8227804SMichael Kruse QualType return_type = fn->getReturnType();
219d169d70bSTobias Grosser unsigned n_arg = fn->getNumArgs();
220d169d70bSTobias Grosser
221d169d70bSTobias Grosser printf(" exc_info = [None]\n");
222e8227804SMichael Kruse printf(" fn = CFUNCTYPE(");
223e8227804SMichael Kruse if (is_isl_stat(return_type) || is_isl_bool(return_type))
224e8227804SMichael Kruse printf("c_int");
225e8227804SMichael Kruse else
226e8227804SMichael Kruse printf("c_void_p");
227d169d70bSTobias Grosser for (unsigned i = 0; i < n_arg - 1; ++i) {
228d169d70bSTobias Grosser if (!is_isl_type(fn->getArgType(i)))
229d169d70bSTobias Grosser die("Argument has non-isl type");
230d169d70bSTobias Grosser printf(", c_void_p");
231d169d70bSTobias Grosser }
232d169d70bSTobias Grosser printf(", c_void_p)\n");
233d169d70bSTobias Grosser printf(" def cb_func(");
234d169d70bSTobias Grosser for (unsigned i = 0; i < n_arg; ++i) {
235d169d70bSTobias Grosser if (i)
236d169d70bSTobias Grosser printf(", ");
237d169d70bSTobias Grosser printf("cb_arg%d", i);
238d169d70bSTobias Grosser }
239d169d70bSTobias Grosser printf("):\n");
240d169d70bSTobias Grosser for (unsigned i = 0; i < n_arg - 1; ++i) {
241d169d70bSTobias Grosser string arg_type;
242d169d70bSTobias Grosser arg_type = type2python(extract_type(fn->getArgType(i)));
2436145b11cSTobias Grosser printf(" cb_arg%d = %s(ctx=arg0.ctx, ptr=",
2446145b11cSTobias Grosser i, arg_type.c_str());
2456145b11cSTobias Grosser if (!callback_takes_argument(param, i))
2466145b11cSTobias Grosser print_copy(fn->getArgType(i));
2476145b11cSTobias Grosser printf("(cb_arg%d))\n", i);
248d169d70bSTobias Grosser }
249d169d70bSTobias Grosser printf(" try:\n");
250e8227804SMichael Kruse if (is_isl_stat(return_type))
251d169d70bSTobias Grosser printf(" arg%d(", arg);
252e8227804SMichael Kruse else
253e8227804SMichael Kruse printf(" res = arg%d(", arg);
254d169d70bSTobias Grosser for (unsigned i = 0; i < n_arg - 1; ++i) {
255d169d70bSTobias Grosser if (i)
256d169d70bSTobias Grosser printf(", ");
257d169d70bSTobias Grosser printf("cb_arg%d", i);
258d169d70bSTobias Grosser }
259d169d70bSTobias Grosser printf(")\n");
260*3f9bf9f4Spatacca printf(" except BaseException as e:\n");
261*3f9bf9f4Spatacca printf(" exc_info[0] = e\n");
262e8227804SMichael Kruse if (is_isl_stat(return_type) || is_isl_bool(return_type))
263d169d70bSTobias Grosser printf(" return -1\n");
264e8227804SMichael Kruse else
265e8227804SMichael Kruse printf(" return None\n");
266e8227804SMichael Kruse if (is_isl_stat(return_type)) {
267d169d70bSTobias Grosser printf(" return 0\n");
268e8227804SMichael Kruse } else if (is_isl_bool(return_type)) {
269e8227804SMichael Kruse printf(" return 1 if res else 0\n");
270e8227804SMichael Kruse } else {
271e8227804SMichael Kruse printf(" return ");
272e8227804SMichael Kruse print_copy(return_type);
273e8227804SMichael Kruse printf("(res.ptr)\n");
274e8227804SMichael Kruse }
275d169d70bSTobias Grosser printf(" cb = fn(cb_func)\n");
276d169d70bSTobias Grosser }
277d169d70bSTobias Grosser
278d169d70bSTobias Grosser /* Print the argument at position "arg" in call to "fd".
279e8227804SMichael Kruse * "fmt" is the format for printing Python method arguments.
280d169d70bSTobias Grosser * "skip" is the number of initial arguments of "fd" that are
281d169d70bSTobias Grosser * skipped in the Python method.
282d169d70bSTobias Grosser *
283e8227804SMichael Kruse * If the (first) argument is an isl_ctx, then print "ctx",
284e8227804SMichael Kruse * assuming that the caller has made the context available
285e8227804SMichael Kruse * in a "ctx" variable.
286e8227804SMichael Kruse * Otherwise, if the argument is a callback, then print a reference to
287d169d70bSTobias Grosser * the callback wrapper "cb".
288d169d70bSTobias Grosser * Otherwise, if the argument is marked as consuming a reference,
289d169d70bSTobias Grosser * then pass a copy of the pointer stored in the corresponding
290d169d70bSTobias Grosser * argument passed to the Python method.
291e8227804SMichael Kruse * Otherwise, if the argument is a string, then the python string is first
292e8227804SMichael Kruse * encoded as a byte sequence, using 'ascii' as encoding. This assumes
293e8227804SMichael Kruse * that all strings passed to isl can be converted to 'ascii'.
294d169d70bSTobias Grosser * Otherwise, if the argument is a pointer, then pass this pointer itself.
295d169d70bSTobias Grosser * Otherwise, pass the argument directly.
296d169d70bSTobias Grosser */
print_arg_in_call(FunctionDecl * fd,const char * fmt,int arg,int skip)297e8227804SMichael Kruse void python_generator::print_arg_in_call(FunctionDecl *fd, const char *fmt,
298e8227804SMichael Kruse int arg, int skip)
299d169d70bSTobias Grosser {
300d169d70bSTobias Grosser ParmVarDecl *param = fd->getParamDecl(arg);
301d169d70bSTobias Grosser QualType type = param->getOriginalType();
302e8227804SMichael Kruse if (is_isl_ctx(type)) {
303e8227804SMichael Kruse printf("ctx");
304e8227804SMichael Kruse } else if (is_callback(type)) {
305d169d70bSTobias Grosser printf("cb");
306d169d70bSTobias Grosser } else if (takes(param)) {
3076145b11cSTobias Grosser print_copy(type);
308e8227804SMichael Kruse printf("(");
309e8227804SMichael Kruse printf(fmt, arg - skip);
310e8227804SMichael Kruse printf(".ptr)");
311e8227804SMichael Kruse } else if (is_string(type)) {
312e8227804SMichael Kruse printf(fmt, arg - skip);
313e8227804SMichael Kruse printf(".encode('ascii')");
314d169d70bSTobias Grosser } else if (type->isPointerType()) {
315e8227804SMichael Kruse printf(fmt, arg - skip);
316e8227804SMichael Kruse printf(".ptr");
317d169d70bSTobias Grosser } else {
318e8227804SMichael Kruse printf(fmt, arg - skip);
319e8227804SMichael Kruse }
320e8227804SMichael Kruse }
321e8227804SMichael Kruse
322e8227804SMichael Kruse /* Generate code that raises the exception captured in "exc_info", if any,
323e8227804SMichael Kruse * with the given indentation.
324e8227804SMichael Kruse */
print_rethrow(int indent,const char * exc_info)325e8227804SMichael Kruse static void print_rethrow(int indent, const char *exc_info)
326e8227804SMichael Kruse {
327*3f9bf9f4Spatacca print_indent(indent, "if %s is not None:\n", exc_info);
328*3f9bf9f4Spatacca print_indent(indent, " raise %s\n", exc_info);
329e8227804SMichael Kruse }
330e8227804SMichael Kruse
331e8227804SMichael Kruse /* Print code with the given indentation that checks
332e8227804SMichael Kruse * whether any of the persistent callbacks of "clazz"
333e8227804SMichael Kruse * is set and if it failed with an exception. If so, the 'exc_info'
334e8227804SMichael Kruse * field contains the exception and is raised again.
335e8227804SMichael Kruse * The field is cleared because the callback and its data may get reused.
336e8227804SMichael Kruse * "fmt" is the format for printing Python method arguments.
337e8227804SMichael Kruse */
print_persistent_callback_failure_check(int indent,const isl_class & clazz,const char * fmt)338e8227804SMichael Kruse static void print_persistent_callback_failure_check(int indent,
339e8227804SMichael Kruse const isl_class &clazz, const char *fmt)
340e8227804SMichael Kruse {
341e8227804SMichael Kruse const set<FunctionDecl *> &callbacks = clazz.persistent_callbacks;
342e8227804SMichael Kruse set<FunctionDecl *>::const_iterator in;
343e8227804SMichael Kruse
344e8227804SMichael Kruse for (in = callbacks.begin(); in != callbacks.end(); ++in) {
345e8227804SMichael Kruse string callback_name = clazz.persistent_callback_name(*in);
346e8227804SMichael Kruse
347e8227804SMichael Kruse print_indent(indent, "if hasattr(");
348e8227804SMichael Kruse printf(fmt, 0);
349e8227804SMichael Kruse printf(", '%s') and ", callback_name.c_str());
350e8227804SMichael Kruse printf(fmt, 0);
351e8227804SMichael Kruse printf(".%s['exc_info'] != None:\n", callback_name.c_str());
352e8227804SMichael Kruse print_indent(indent, " exc_info = ");
353e8227804SMichael Kruse printf(fmt, 0);
354e8227804SMichael Kruse printf(".%s['exc_info'][0]\n", callback_name.c_str());
355e8227804SMichael Kruse print_indent(indent, " ");
356e8227804SMichael Kruse printf(fmt, 0);
357e8227804SMichael Kruse printf(".%s['exc_info'][0] = None\n", callback_name.c_str());
358e8227804SMichael Kruse print_rethrow(indent + 4, "exc_info");
359d169d70bSTobias Grosser }
360d169d70bSTobias Grosser }
361d169d70bSTobias Grosser
362d169d70bSTobias Grosser /* Print the return statement of the python method corresponding
363e8227804SMichael Kruse * to the C function "method" with the given indentation.
364e8227804SMichael Kruse * If the object on which the method was called
365e8227804SMichael Kruse * may have a persistent callback, then first check if any of those failed.
366e8227804SMichael Kruse * "fmt" is the format for printing Python method arguments.
367e8227804SMichael Kruse *
368e8227804SMichael Kruse * If the method returns a new instance of the same object type and
369e8227804SMichael Kruse * if the class has any persistent callbacks, then the data
370e8227804SMichael Kruse * for these callbacks are copied from the original to the new object.
371e8227804SMichael Kruse * If the method it itself setting a persistent callback,
372e8227804SMichael Kruse * then keep track of the constructed C callback (such that it doesn't
373e8227804SMichael Kruse * get destroyed) and the data structure that holds the captured exception
374e8227804SMichael Kruse * (such that it can be raised again).
375d169d70bSTobias Grosser *
376d169d70bSTobias Grosser * If the return type is a (const) char *, then convert the result
377d169d70bSTobias Grosser * to a Python string, raising an error on NULL and freeing
378d169d70bSTobias Grosser * the C string if needed. For python 3 compatibility, the string returned
379d169d70bSTobias Grosser * by isl is explicitly decoded as an 'ascii' string. This is correct
380d169d70bSTobias Grosser * as all strings returned by isl are expected to be 'ascii'.
381d169d70bSTobias Grosser *
382e8227804SMichael Kruse * If the return type is isl_stat, isl_bool or isl_size, then
383e8227804SMichael Kruse * raise an error on isl_stat_error, isl_bool_error or isl_size_error.
384e8227804SMichael Kruse * In case of isl_bool, the result is converted to
385e8227804SMichael Kruse * a Python boolean.
386e8227804SMichael Kruse * In case of isl_size, the result is converted to a Python int.
387d169d70bSTobias Grosser */
print_method_return(int indent,const isl_class & clazz,FunctionDecl * method,const char * fmt)388e8227804SMichael Kruse void python_generator::print_method_return(int indent, const isl_class &clazz,
389e8227804SMichael Kruse FunctionDecl *method, const char *fmt)
390d169d70bSTobias Grosser {
391d169d70bSTobias Grosser QualType return_type = method->getReturnType();
392d169d70bSTobias Grosser
393e8227804SMichael Kruse if (!is_static(clazz, method))
394e8227804SMichael Kruse print_persistent_callback_failure_check(indent, clazz, fmt);
395e8227804SMichael Kruse
396d169d70bSTobias Grosser if (is_isl_type(return_type)) {
397d169d70bSTobias Grosser string type;
398d169d70bSTobias Grosser
399d169d70bSTobias Grosser type = type2python(extract_type(return_type));
400e8227804SMichael Kruse print_indent(indent,
401e8227804SMichael Kruse "obj = %s(ctx=ctx, ptr=res)\n", type.c_str());
402e8227804SMichael Kruse if (is_mutator(clazz, method) &&
403e8227804SMichael Kruse clazz.has_persistent_callbacks())
404e8227804SMichael Kruse print_indent(indent, "obj.copy_callbacks(arg0)\n");
405e8227804SMichael Kruse if (clazz.persistent_callbacks.count(method)) {
406e8227804SMichael Kruse string callback_name;
407e8227804SMichael Kruse
408e8227804SMichael Kruse callback_name = clazz.persistent_callback_name(method);
409e8227804SMichael Kruse print_indent(indent, "obj.%s = { 'func': cb, "
410e8227804SMichael Kruse "'exc_info': exc_info }\n",
411e8227804SMichael Kruse callback_name.c_str());
412e8227804SMichael Kruse }
413e8227804SMichael Kruse print_indent(indent, "return obj\n");
414d169d70bSTobias Grosser } else if (is_string(return_type)) {
415e8227804SMichael Kruse print_indent(indent, "if res == 0:\n");
416e8227804SMichael Kruse print_indent(indent, " raise\n");
417e8227804SMichael Kruse print_indent(indent, "string = "
418d169d70bSTobias Grosser "cast(res, c_char_p).value.decode('ascii')\n");
419d169d70bSTobias Grosser
420d169d70bSTobias Grosser if (gives(method))
421e8227804SMichael Kruse print_indent(indent, "libc.free(res)\n");
422d169d70bSTobias Grosser
423e8227804SMichael Kruse print_indent(indent, "return string\n");
424e8227804SMichael Kruse } else if (is_isl_neg_error(return_type)) {
425e8227804SMichael Kruse print_indent(indent, "if res < 0:\n");
426e8227804SMichael Kruse print_indent(indent, " raise\n");
427e8227804SMichael Kruse if (is_isl_bool(return_type))
428e8227804SMichael Kruse print_indent(indent, "return bool(res)\n");
429e8227804SMichael Kruse else if (is_isl_size(return_type))
430e8227804SMichael Kruse print_indent(indent, "return int(res)\n");
431d169d70bSTobias Grosser } else {
432e8227804SMichael Kruse print_indent(indent, "return res\n");
433d169d70bSTobias Grosser }
434d169d70bSTobias Grosser }
435d169d70bSTobias Grosser
436e8227804SMichael Kruse /* Print a python "get" method corresponding to the C function "fd"
437e8227804SMichael Kruse * in class "clazz" using a name that includes the "get_" prefix.
438e8227804SMichael Kruse *
439e8227804SMichael Kruse * This method simply calls the variant without the "get_" prefix and
440e8227804SMichael Kruse * returns its result.
441e8227804SMichael Kruse * Note that static methods are not considered to be "get" methods.
442e8227804SMichael Kruse */
print_get_method(const isl_class & clazz,FunctionDecl * fd)443e8227804SMichael Kruse void python_generator::print_get_method(const isl_class &clazz,
444e8227804SMichael Kruse FunctionDecl *fd)
445e8227804SMichael Kruse {
446e8227804SMichael Kruse string get_name = clazz.base_method_name(fd);
447e8227804SMichael Kruse string name = clazz.method_name(fd);
448e8227804SMichael Kruse int num_params = fd->getNumParams();
449e8227804SMichael Kruse
450e8227804SMichael Kruse print_method_header(false, get_name, num_params);
451e8227804SMichael Kruse printf(" return arg0.%s(", name.c_str());
452e8227804SMichael Kruse print_method_arguments(1, num_params);
453e8227804SMichael Kruse printf(")\n");
454e8227804SMichael Kruse }
455e8227804SMichael Kruse
456e8227804SMichael Kruse /* Print a call to "method", along with the corresponding
457e8227804SMichael Kruse * return statement, with the given indentation.
458e8227804SMichael Kruse * "drop_ctx" is set if the first argument is an isl_ctx.
459e8227804SMichael Kruse * "drop_user" is set if the last argument is a "user" argument
460e8227804SMichael Kruse * corresponding to a callback argument.
461e8227804SMichael Kruse *
462e8227804SMichael Kruse * A "ctx" variable is first initialized as it may be needed
463e8227804SMichael Kruse * in the first call to print_arg_in_call and in print_method_return.
464e8227804SMichael Kruse *
465e8227804SMichael Kruse * If the method has a callback function, then any exception
466e8227804SMichael Kruse * thrown in the callback also need to be rethrown.
467e8227804SMichael Kruse */
print_method_call(int indent,const isl_class & clazz,FunctionDecl * method,const char * fmt,int drop_ctx,int drop_user)468e8227804SMichael Kruse void python_generator::print_method_call(int indent, const isl_class &clazz,
469e8227804SMichael Kruse FunctionDecl *method, const char *fmt, int drop_ctx, int drop_user)
470e8227804SMichael Kruse {
471a54eb9b7SMichael Kruse string fullname = method->getName().str();
472e8227804SMichael Kruse int num_params = method->getNumParams();
473e8227804SMichael Kruse
474e8227804SMichael Kruse if (drop_ctx) {
475e8227804SMichael Kruse print_indent(indent, "ctx = Context.getDefaultInstance()\n");
476e8227804SMichael Kruse } else {
477e8227804SMichael Kruse print_indent(indent, "ctx = ");
478e8227804SMichael Kruse printf(fmt, 0);
479e8227804SMichael Kruse printf(".ctx\n");
480e8227804SMichael Kruse }
481e8227804SMichael Kruse print_indent(indent, "res = isl.%s(", fullname.c_str());
482e8227804SMichael Kruse for (int i = 0; i < num_params - drop_user; ++i) {
483e8227804SMichael Kruse if (i > 0)
484e8227804SMichael Kruse printf(", ");
485e8227804SMichael Kruse print_arg_in_call(method, fmt, i, drop_ctx);
486e8227804SMichael Kruse }
487e8227804SMichael Kruse if (drop_user)
488e8227804SMichael Kruse printf(", None");
489e8227804SMichael Kruse printf(")\n");
490e8227804SMichael Kruse
491e8227804SMichael Kruse if (drop_user)
492e8227804SMichael Kruse print_rethrow(indent, "exc_info[0]");
493e8227804SMichael Kruse
494e8227804SMichael Kruse print_method_return(indent, clazz, method, fmt);
495e8227804SMichael Kruse }
496e8227804SMichael Kruse
497d169d70bSTobias Grosser /* Print a python method corresponding to the C function "method".
498d169d70bSTobias Grosser * "super" contains the superclasses of the class to which the method belongs,
499d169d70bSTobias Grosser * with the first element corresponding to the annotation that appears
500d169d70bSTobias Grosser * closest to the annotated type. This superclass is the least
501d169d70bSTobias Grosser * general extension of the annotated type in the linearization
502d169d70bSTobias Grosser * of the class hierarchy.
503d169d70bSTobias Grosser *
504d169d70bSTobias Grosser * If the first argument of "method" is something other than an instance
505d169d70bSTobias Grosser * of the class, then mark the python method as static.
506d169d70bSTobias Grosser * If, moreover, this first argument is an isl_ctx, then remove
507d169d70bSTobias Grosser * it from the arguments of the Python method.
508d169d70bSTobias Grosser *
509d169d70bSTobias Grosser * If the function has a callback argument, then it also has a "user"
510d169d70bSTobias Grosser * argument. Since Python has closures, there is no need for such
511d169d70bSTobias Grosser * a user argument in the Python interface, so we simply drop it.
512d169d70bSTobias Grosser * We also create a wrapper ("cb") for the callback.
513d169d70bSTobias Grosser *
514d169d70bSTobias Grosser * If the function consumes a reference, then we pass it a copy of
515d169d70bSTobias Grosser * the actual argument.
516e8227804SMichael Kruse *
517e8227804SMichael Kruse * For methods that are identified as "get" methods, also
518e8227804SMichael Kruse * print a variant of the method using a name that includes
519e8227804SMichael Kruse * the "get_" prefix.
520d169d70bSTobias Grosser */
print_method(const isl_class & clazz,FunctionDecl * method,vector<string> super)521d169d70bSTobias Grosser void python_generator::print_method(const isl_class &clazz,
522d169d70bSTobias Grosser FunctionDecl *method, vector<string> super)
523d169d70bSTobias Grosser {
5246145b11cSTobias Grosser string cname = clazz.method_name(method);
525d169d70bSTobias Grosser int num_params = method->getNumParams();
526d169d70bSTobias Grosser int drop_user = 0;
527d169d70bSTobias Grosser int drop_ctx = first_arg_is_isl_ctx(method);
528d169d70bSTobias Grosser
529d169d70bSTobias Grosser for (int i = 1; i < num_params; ++i) {
530d169d70bSTobias Grosser ParmVarDecl *param = method->getParamDecl(i);
531d169d70bSTobias Grosser QualType type = param->getOriginalType();
532d169d70bSTobias Grosser if (is_callback(type))
533d169d70bSTobias Grosser drop_user = 1;
534d169d70bSTobias Grosser }
535d169d70bSTobias Grosser
536d169d70bSTobias Grosser print_method_header(is_static(clazz, method), cname,
537d169d70bSTobias Grosser num_params - drop_ctx - drop_user);
538d169d70bSTobias Grosser
539e8227804SMichael Kruse print_type_checks(cname, method, drop_ctx,
540e8227804SMichael Kruse num_params - drop_user, super);
541d169d70bSTobias Grosser for (int i = 1; i < num_params; ++i) {
542d169d70bSTobias Grosser ParmVarDecl *param = method->getParamDecl(i);
543d169d70bSTobias Grosser QualType type = param->getOriginalType();
544d169d70bSTobias Grosser if (!is_callback(type))
545d169d70bSTobias Grosser continue;
5466145b11cSTobias Grosser print_callback(param, i - drop_ctx);
547d169d70bSTobias Grosser }
548e8227804SMichael Kruse print_method_call(8, clazz, method, fixed_arg_fmt, drop_ctx, drop_user);
549d169d70bSTobias Grosser
550e8227804SMichael Kruse if (clazz.is_get_method(method))
551e8227804SMichael Kruse print_get_method(clazz, method);
552d169d70bSTobias Grosser }
553d169d70bSTobias Grosser
554e8227804SMichael Kruse /* Print a condition that checks whether Python method argument "i"
555e8227804SMichael Kruse * corresponds to the C function argument type "type".
556e8227804SMichael Kruse */
print_argument_check(QualType type,int i)557e8227804SMichael Kruse static void print_argument_check(QualType type, int i)
558e8227804SMichael Kruse {
559e8227804SMichael Kruse if (generator::is_isl_type(type)) {
560e8227804SMichael Kruse string type_str;
561e8227804SMichael Kruse type_str = generator::extract_type(type);
562e8227804SMichael Kruse type_str = type2python(type_str);
563e8227804SMichael Kruse printf("args[%d].__class__ is %s", i, type_str.c_str());
564e8227804SMichael Kruse } else if (type->isPointerType()) {
565e8227804SMichael Kruse printf("type(args[%d]) == str", i);
566e8227804SMichael Kruse } else {
567e8227804SMichael Kruse printf("type(args[%d]) == int", i);
568e8227804SMichael Kruse }
569e8227804SMichael Kruse }
570e8227804SMichael Kruse
571e8227804SMichael Kruse /* Print a test that checks whether the arguments passed
572e8227804SMichael Kruse * to the Python method correspond to the arguments
573e8227804SMichael Kruse * expected by "fd".
574e8227804SMichael Kruse * "drop_ctx" is set if the first argument of "fd" is an isl_ctx,
575e8227804SMichael Kruse * which does not appear as an argument to the Python method.
576e8227804SMichael Kruse *
577e8227804SMichael Kruse * If an automatic conversion function is available for any
578e8227804SMichael Kruse * of the argument types, then also allow the argument
579e8227804SMichael Kruse * to be of the type as prescribed by the second input argument
580e8227804SMichael Kruse * of the conversion function.
581e8227804SMichael Kruse * The corresponding arguments are then converted to the expected types
582e8227804SMichael Kruse * if needed. The argument tuple first needs to be converted to a list
583e8227804SMichael Kruse * in order to be able to modify the entries.
584e8227804SMichael Kruse */
print_argument_checks(const isl_class & clazz,FunctionDecl * fd,int drop_ctx)585e8227804SMichael Kruse void python_generator::print_argument_checks(const isl_class &clazz,
586e8227804SMichael Kruse FunctionDecl *fd, int drop_ctx)
587e8227804SMichael Kruse {
588e8227804SMichael Kruse int num_params = fd->getNumParams();
589e8227804SMichael Kruse int first = generator::is_static(clazz, fd) ? drop_ctx : 1;
590e8227804SMichael Kruse std::vector<bool> convert(num_params);
591e8227804SMichael Kruse
592e8227804SMichael Kruse printf(" if len(args) == %d", num_params - drop_ctx);
593e8227804SMichael Kruse for (int i = first; i < num_params; ++i) {
594e8227804SMichael Kruse ParmVarDecl *param = fd->getParamDecl(i);
595e8227804SMichael Kruse QualType type = param->getOriginalType();
596e8227804SMichael Kruse const Type *ptr = type.getTypePtr();
597e8227804SMichael Kruse
598e8227804SMichael Kruse printf(" and ");
599e8227804SMichael Kruse if (conversions.count(ptr) == 0) {
600e8227804SMichael Kruse print_argument_check(type, i - drop_ctx);
601e8227804SMichael Kruse } else {
602e8227804SMichael Kruse QualType type2 = conversions.at(ptr)->getOriginalType();
603e8227804SMichael Kruse convert[i] = true;
604e8227804SMichael Kruse printf("(");
605e8227804SMichael Kruse print_argument_check(type, i - drop_ctx);
606e8227804SMichael Kruse printf(" or ");
607e8227804SMichael Kruse print_argument_check(type2, i - drop_ctx);
608e8227804SMichael Kruse printf(")");
609e8227804SMichael Kruse }
610e8227804SMichael Kruse }
611e8227804SMichael Kruse printf(":\n");
612e8227804SMichael Kruse
613e8227804SMichael Kruse if (std::find(convert.begin(), convert.end(), true) == convert.end())
614e8227804SMichael Kruse return;
615e8227804SMichael Kruse print_indent(12, "args = list(args)\n");
616e8227804SMichael Kruse for (int i = first; i < num_params; ++i) {
617e8227804SMichael Kruse ParmVarDecl *param = fd->getParamDecl(i);
618e8227804SMichael Kruse string type;
619e8227804SMichael Kruse
620e8227804SMichael Kruse if (!convert[i])
621e8227804SMichael Kruse continue;
622e8227804SMichael Kruse type = type2python(extract_type(param->getOriginalType()));
623e8227804SMichael Kruse print_type_check(12, type, var_arg_fmt,
624e8227804SMichael Kruse i - drop_ctx, false, "", "", -1);
625e8227804SMichael Kruse }
626d169d70bSTobias Grosser }
627d169d70bSTobias Grosser
628d169d70bSTobias Grosser /* Print part of an overloaded python method corresponding to the C function
629d169d70bSTobias Grosser * "method".
630e8227804SMichael Kruse * "drop_ctx" is set if the first argument of "method" is an isl_ctx.
631d169d70bSTobias Grosser *
632d169d70bSTobias Grosser * In particular, print code to test whether the arguments passed to
633d169d70bSTobias Grosser * the python method correspond to the arguments expected by "method"
634d169d70bSTobias Grosser * and to call "method" if they do.
635d169d70bSTobias Grosser */
print_method_overload(const isl_class & clazz,FunctionDecl * method)636d169d70bSTobias Grosser void python_generator::print_method_overload(const isl_class &clazz,
637d169d70bSTobias Grosser FunctionDecl *method)
638d169d70bSTobias Grosser {
639e8227804SMichael Kruse int drop_ctx = first_arg_is_isl_ctx(method);
640d169d70bSTobias Grosser
641e8227804SMichael Kruse print_argument_checks(clazz, method, drop_ctx);
642e8227804SMichael Kruse print_method_call(12, clazz, method, var_arg_fmt, drop_ctx, 0);
643d169d70bSTobias Grosser }
644d169d70bSTobias Grosser
645d169d70bSTobias Grosser /* Print a python method with a name derived from "fullname"
646d169d70bSTobias Grosser * corresponding to the C functions "methods".
647d169d70bSTobias Grosser * "super" contains the superclasses of the class to which the method belongs.
648d169d70bSTobias Grosser *
649d169d70bSTobias Grosser * If "methods" consists of a single element that is not marked overloaded,
650d169d70bSTobias Grosser * the use print_method to print the method.
651d169d70bSTobias Grosser * Otherwise, print an overloaded method with pieces corresponding
652d169d70bSTobias Grosser * to each function in "methods".
653d169d70bSTobias Grosser */
print_method(const isl_class & clazz,const string & fullname,const function_set & methods,vector<string> super)654d169d70bSTobias Grosser void python_generator::print_method(const isl_class &clazz,
655e8227804SMichael Kruse const string &fullname, const function_set &methods,
656d169d70bSTobias Grosser vector<string> super)
657d169d70bSTobias Grosser {
658d169d70bSTobias Grosser string cname;
659e8227804SMichael Kruse function_set::const_iterator it;
660d169d70bSTobias Grosser FunctionDecl *any_method;
661d169d70bSTobias Grosser
662d169d70bSTobias Grosser any_method = *methods.begin();
663d169d70bSTobias Grosser if (methods.size() == 1 && !is_overload(any_method)) {
664d169d70bSTobias Grosser print_method(clazz, any_method, super);
665d169d70bSTobias Grosser return;
666d169d70bSTobias Grosser }
667d169d70bSTobias Grosser
6686145b11cSTobias Grosser cname = clazz.method_name(any_method);
669d169d70bSTobias Grosser
670e8227804SMichael Kruse print_method_def(is_static(clazz, any_method), cname);
671e8227804SMichael Kruse printf("(*args):\n");
672d169d70bSTobias Grosser
673d169d70bSTobias Grosser for (it = methods.begin(); it != methods.end(); ++it)
674d169d70bSTobias Grosser print_method_overload(clazz, *it);
675e8227804SMichael Kruse printf(" raise Error\n");
676e8227804SMichael Kruse }
677e8227804SMichael Kruse
678e8227804SMichael Kruse /* Print a python method "name" corresponding to "fd" setting
679e8227804SMichael Kruse * the enum value "value".
680e8227804SMichael Kruse * "super" contains the superclasses of the class to which the method belongs,
681e8227804SMichael Kruse * with the first element corresponding to the annotation that appears
682e8227804SMichael Kruse * closest to the annotated type.
683e8227804SMichael Kruse *
684e8227804SMichael Kruse * The last argument of the C function does not appear in the method call,
685e8227804SMichael Kruse * but is fixed to "value" instead.
686e8227804SMichael Kruse * Other than that, the method printed here is similar to one
687e8227804SMichael Kruse * printed by python_generator::print_method, except that
688e8227804SMichael Kruse * some of the special cases do not occur.
689e8227804SMichael Kruse */
print_set_enum(const isl_class & clazz,FunctionDecl * fd,int value,const string & name,const vector<string> & super)690e8227804SMichael Kruse void python_generator::print_set_enum(const isl_class &clazz,
691e8227804SMichael Kruse FunctionDecl *fd, int value, const string &name,
692e8227804SMichael Kruse const vector<string> &super)
693e8227804SMichael Kruse {
694a54eb9b7SMichael Kruse string fullname = fd->getName().str();
695e8227804SMichael Kruse int num_params = fd->getNumParams();
696e8227804SMichael Kruse
697e8227804SMichael Kruse print_method_header(is_static(clazz, fd), name, num_params - 1);
698e8227804SMichael Kruse
699e8227804SMichael Kruse print_type_checks(name, fd, false, num_params - 1, super);
700e8227804SMichael Kruse printf(" ctx = arg0.ctx\n");
701e8227804SMichael Kruse printf(" res = isl.%s(", fullname.c_str());
702e8227804SMichael Kruse for (int i = 0; i < num_params - 1; ++i) {
703e8227804SMichael Kruse if (i)
704e8227804SMichael Kruse printf(", ");
705e8227804SMichael Kruse print_arg_in_call(fd, fixed_arg_fmt, i, 0);
706e8227804SMichael Kruse }
707e8227804SMichael Kruse printf(", %d", value);
708e8227804SMichael Kruse printf(")\n");
709e8227804SMichael Kruse print_method_return(8, clazz, fd, fixed_arg_fmt);
710e8227804SMichael Kruse }
711e8227804SMichael Kruse
712e8227804SMichael Kruse /* Print python methods corresponding to "fd", which sets an enum.
713e8227804SMichael Kruse * "super" contains the superclasses of the class to which the method belongs,
714e8227804SMichael Kruse * with the first element corresponding to the annotation that appears
715e8227804SMichael Kruse * closest to the annotated type.
716e8227804SMichael Kruse *
717e8227804SMichael Kruse * A method is generated for each value in the enum, setting
718e8227804SMichael Kruse * the enum to that value.
719e8227804SMichael Kruse */
print_set_enum(const isl_class & clazz,FunctionDecl * fd,const vector<string> & super)720e8227804SMichael Kruse void python_generator::print_set_enum(const isl_class &clazz,
721e8227804SMichael Kruse FunctionDecl *fd, const vector<string> &super)
722e8227804SMichael Kruse {
723e8227804SMichael Kruse vector<set_enum>::const_iterator it;
724e8227804SMichael Kruse const vector<set_enum> &set_enums = clazz.set_enums.at(fd);
725e8227804SMichael Kruse
726e8227804SMichael Kruse for (it = set_enums.begin(); it != set_enums.end(); ++it)
727e8227804SMichael Kruse print_set_enum(clazz, fd, it->value, it->method_name, super);
728d169d70bSTobias Grosser }
729d169d70bSTobias Grosser
730d169d70bSTobias Grosser /* Print part of the constructor for this isl_class.
731d169d70bSTobias Grosser *
732d169d70bSTobias Grosser * In particular, check if the actual arguments correspond to the
733d169d70bSTobias Grosser * formal arguments of "cons" and if so call "cons" and put the
734d169d70bSTobias Grosser * result in self.ptr and a reference to the default context in self.ctx.
735d169d70bSTobias Grosser */
print_constructor(const isl_class & clazz,FunctionDecl * cons)736d169d70bSTobias Grosser void python_generator::print_constructor(const isl_class &clazz,
737d169d70bSTobias Grosser FunctionDecl *cons)
738d169d70bSTobias Grosser {
739a54eb9b7SMichael Kruse string fullname = cons->getName().str();
7406145b11cSTobias Grosser string cname = clazz.method_name(cons);
741d169d70bSTobias Grosser int num_params = cons->getNumParams();
742d169d70bSTobias Grosser int drop_ctx = first_arg_is_isl_ctx(cons);
743d169d70bSTobias Grosser
744e8227804SMichael Kruse print_argument_checks(clazz, cons, drop_ctx);
745d169d70bSTobias Grosser printf(" self.ctx = Context.getDefaultInstance()\n");
746d169d70bSTobias Grosser printf(" self.ptr = isl.%s(", fullname.c_str());
747d169d70bSTobias Grosser if (drop_ctx)
748d169d70bSTobias Grosser printf("self.ctx");
749d169d70bSTobias Grosser for (int i = drop_ctx; i < num_params; ++i) {
750d169d70bSTobias Grosser if (i)
751d169d70bSTobias Grosser printf(", ");
752e8227804SMichael Kruse print_arg_in_call(cons, var_arg_fmt, i, drop_ctx);
753d169d70bSTobias Grosser }
754d169d70bSTobias Grosser printf(")\n");
755d169d70bSTobias Grosser printf(" return\n");
756d169d70bSTobias Grosser }
757d169d70bSTobias Grosser
758e8227804SMichael Kruse /* If "clazz" has a type function describing subclasses,
759e8227804SMichael Kruse * then add constructors that allow each of these subclasses
760e8227804SMichael Kruse * to be treated as an object to the superclass.
761e8227804SMichael Kruse */
print_upcast_constructors(const isl_class & clazz)762e8227804SMichael Kruse void python_generator::print_upcast_constructors(const isl_class &clazz)
763e8227804SMichael Kruse {
764e8227804SMichael Kruse map<int, string>::const_iterator i;
765e8227804SMichael Kruse
766e8227804SMichael Kruse if (!clazz.fn_type)
767e8227804SMichael Kruse return;
768e8227804SMichael Kruse
769e8227804SMichael Kruse for (i = clazz.type_subclasses.begin();
770e8227804SMichael Kruse i != clazz.type_subclasses.end(); ++i) {
771e8227804SMichael Kruse printf(" if len(args) == 1 and "
772e8227804SMichael Kruse "isinstance(args[0], %s):\n",
773e8227804SMichael Kruse type2python(i->second).c_str());
774e8227804SMichael Kruse printf(" self.ctx = args[0].ctx\n");
775e8227804SMichael Kruse printf(" self.ptr = isl.%s_copy(args[0].ptr)\n",
776e8227804SMichael Kruse clazz.name.c_str());
777e8227804SMichael Kruse printf(" return\n");
778e8227804SMichael Kruse }
779e8227804SMichael Kruse }
780e8227804SMichael Kruse
781d169d70bSTobias Grosser /* Print the header of the class "name" with superclasses "super".
782d169d70bSTobias Grosser * The order of the superclasses is the opposite of the order
783d169d70bSTobias Grosser * in which the corresponding annotations appear in the source code.
784e8227804SMichael Kruse * If "clazz" is a subclass derived from a type function,
785e8227804SMichael Kruse * then the immediate superclass is recorded in "clazz" itself.
786d169d70bSTobias Grosser */
print_class_header(const isl_class & clazz,const string & name,const vector<string> & super)787d169d70bSTobias Grosser void python_generator::print_class_header(const isl_class &clazz,
788d169d70bSTobias Grosser const string &name, const vector<string> &super)
789d169d70bSTobias Grosser {
790d169d70bSTobias Grosser printf("class %s", name.c_str());
791d169d70bSTobias Grosser if (super.size() > 0) {
792d169d70bSTobias Grosser printf("(");
793d169d70bSTobias Grosser for (unsigned i = 0; i < super.size(); ++i) {
794d169d70bSTobias Grosser if (i > 0)
795d169d70bSTobias Grosser printf(", ");
796d169d70bSTobias Grosser printf("%s", type2python(super[i]).c_str());
797d169d70bSTobias Grosser }
798d169d70bSTobias Grosser printf(")");
799e8227804SMichael Kruse } else if (clazz.is_type_subclass()) {
800e8227804SMichael Kruse printf("(%s)", type2python(clazz.superclass_name).c_str());
801d169d70bSTobias Grosser } else {
802d169d70bSTobias Grosser printf("(object)");
803d169d70bSTobias Grosser }
804d169d70bSTobias Grosser printf(":\n");
805d169d70bSTobias Grosser }
806d169d70bSTobias Grosser
807d169d70bSTobias Grosser /* Tell ctypes about the return type of "fd".
808d169d70bSTobias Grosser * In particular, if "fd" returns a pointer to an isl object,
809d169d70bSTobias Grosser * then tell ctypes it returns a "c_void_p".
810d169d70bSTobias Grosser * If "fd" returns a char *, then simply tell ctypes.
811e8227804SMichael Kruse *
812e8227804SMichael Kruse * Nothing needs to be done for functions returning
813e8227804SMichael Kruse * isl_bool, isl_stat or isl_size since they are represented by an int and
814e8227804SMichael Kruse * ctypes assumes that a function returns int by default.
815d169d70bSTobias Grosser */
print_restype(FunctionDecl * fd)816d169d70bSTobias Grosser void python_generator::print_restype(FunctionDecl *fd)
817d169d70bSTobias Grosser {
818a54eb9b7SMichael Kruse string fullname = fd->getName().str();
819d169d70bSTobias Grosser QualType type = fd->getReturnType();
820d169d70bSTobias Grosser if (is_isl_type(type))
821d169d70bSTobias Grosser printf("isl.%s.restype = c_void_p\n", fullname.c_str());
822d169d70bSTobias Grosser else if (is_string(type))
823d169d70bSTobias Grosser printf("isl.%s.restype = POINTER(c_char)\n", fullname.c_str());
824d169d70bSTobias Grosser }
825d169d70bSTobias Grosser
826d169d70bSTobias Grosser /* Tell ctypes about the types of the arguments of the function "fd".
827d169d70bSTobias Grosser */
print_argtypes(FunctionDecl * fd)828d169d70bSTobias Grosser void python_generator::print_argtypes(FunctionDecl *fd)
829d169d70bSTobias Grosser {
830a54eb9b7SMichael Kruse string fullname = fd->getName().str();
831d169d70bSTobias Grosser int n = fd->getNumParams();
832d169d70bSTobias Grosser int drop_user = 0;
833d169d70bSTobias Grosser
834d169d70bSTobias Grosser printf("isl.%s.argtypes = [", fullname.c_str());
835d169d70bSTobias Grosser for (int i = 0; i < n - drop_user; ++i) {
836d169d70bSTobias Grosser ParmVarDecl *param = fd->getParamDecl(i);
837d169d70bSTobias Grosser QualType type = param->getOriginalType();
838d169d70bSTobias Grosser if (is_callback(type))
839d169d70bSTobias Grosser drop_user = 1;
840d169d70bSTobias Grosser if (i)
841d169d70bSTobias Grosser printf(", ");
842d169d70bSTobias Grosser if (is_isl_ctx(type))
843d169d70bSTobias Grosser printf("Context");
844d169d70bSTobias Grosser else if (is_isl_type(type) || is_callback(type))
845d169d70bSTobias Grosser printf("c_void_p");
846d169d70bSTobias Grosser else if (is_string(type))
847d169d70bSTobias Grosser printf("c_char_p");
848d169d70bSTobias Grosser else if (is_long(type))
849d169d70bSTobias Grosser printf("c_long");
850d169d70bSTobias Grosser else
851d169d70bSTobias Grosser printf("c_int");
852d169d70bSTobias Grosser }
853d169d70bSTobias Grosser if (drop_user)
854d169d70bSTobias Grosser printf(", c_void_p");
855d169d70bSTobias Grosser printf("]\n");
856d169d70bSTobias Grosser }
857d169d70bSTobias Grosser
858d169d70bSTobias Grosser /* Print type definitions for the method 'fd'.
859d169d70bSTobias Grosser */
print_method_type(FunctionDecl * fd)860d169d70bSTobias Grosser void python_generator::print_method_type(FunctionDecl *fd)
861d169d70bSTobias Grosser {
862d169d70bSTobias Grosser print_restype(fd);
863d169d70bSTobias Grosser print_argtypes(fd);
864d169d70bSTobias Grosser }
865d169d70bSTobias Grosser
866e8227804SMichael Kruse /* If "clazz" has a type function describing subclasses or
867e8227804SMichael Kruse * if it is one of those type subclasses, then print a __new__ method.
868e8227804SMichael Kruse *
869e8227804SMichael Kruse * In the superclass, the __new__ method constructs an object
870e8227804SMichael Kruse * of the subclass type specified by the type function.
871e8227804SMichael Kruse * In the subclass, the __new__ method reverts to the original behavior.
872e8227804SMichael Kruse */
print_new(const isl_class & clazz,const string & python_name)873e8227804SMichael Kruse void python_generator::print_new(const isl_class &clazz,
874e8227804SMichael Kruse const string &python_name)
875e8227804SMichael Kruse {
876e8227804SMichael Kruse if (!clazz.fn_type && !clazz.is_type_subclass())
877e8227804SMichael Kruse return;
878e8227804SMichael Kruse
879e8227804SMichael Kruse printf(" def __new__(cls, *args, **keywords):\n");
880e8227804SMichael Kruse
881e8227804SMichael Kruse if (clazz.fn_type) {
882e8227804SMichael Kruse map<int, string>::const_iterator i;
883e8227804SMichael Kruse
884e8227804SMichael Kruse printf(" if \"ptr\" in keywords:\n");
885e8227804SMichael Kruse printf(" type = isl.%s(keywords[\"ptr\"])\n",
886e8227804SMichael Kruse clazz.fn_type->getNameAsString().c_str());
887e8227804SMichael Kruse
888e8227804SMichael Kruse for (i = clazz.type_subclasses.begin();
889e8227804SMichael Kruse i != clazz.type_subclasses.end(); ++i) {
890e8227804SMichael Kruse printf(" if type == %d:\n", i->first);
891e8227804SMichael Kruse printf(" return %s(**keywords)\n",
892e8227804SMichael Kruse type2python(i->second).c_str());
893e8227804SMichael Kruse }
894e8227804SMichael Kruse printf(" raise\n");
895e8227804SMichael Kruse }
896e8227804SMichael Kruse
897e8227804SMichael Kruse printf(" return super(%s, cls).__new__(cls)\n",
898e8227804SMichael Kruse python_name.c_str());
899e8227804SMichael Kruse }
900e8227804SMichael Kruse
901d169d70bSTobias Grosser /* Print declarations for methods printing the class representation,
902d169d70bSTobias Grosser * provided there is a corresponding *_to_str function.
903d169d70bSTobias Grosser *
904d169d70bSTobias Grosser * In particular, provide an implementation of __str__ and __repr__ methods to
905d169d70bSTobias Grosser * override the default representation used by python. Python uses __str__ to
906d169d70bSTobias Grosser * pretty print the class (e.g., when calling print(obj)) and uses __repr__
907d169d70bSTobias Grosser * when printing a precise representation of an object (e.g., when dumping it
908d169d70bSTobias Grosser * in the REPL console).
909d169d70bSTobias Grosser *
910d169d70bSTobias Grosser * Check the type of the argument before calling the *_to_str function
911d169d70bSTobias Grosser * on it in case the method was called on an object from a subclass.
912d169d70bSTobias Grosser *
913d169d70bSTobias Grosser * The return value of the *_to_str function is decoded to a python string
914d169d70bSTobias Grosser * assuming an 'ascii' encoding. This is necessary for python 3 compatibility.
915d169d70bSTobias Grosser */
print_representation(const isl_class & clazz,const string & python_name)916d169d70bSTobias Grosser void python_generator::print_representation(const isl_class &clazz,
917d169d70bSTobias Grosser const string &python_name)
918d169d70bSTobias Grosser {
919d169d70bSTobias Grosser if (!clazz.fn_to_str)
920d169d70bSTobias Grosser return;
921d169d70bSTobias Grosser
922d169d70bSTobias Grosser printf(" def __str__(arg0):\n");
923e8227804SMichael Kruse print_type_check(8, python_name, fixed_arg_fmt, 0, false, "", "", -1);
924d169d70bSTobias Grosser printf(" ptr = isl.%s(arg0.ptr)\n",
925d169d70bSTobias Grosser string(clazz.fn_to_str->getName()).c_str());
926d169d70bSTobias Grosser printf(" res = cast(ptr, c_char_p).value.decode('ascii')\n");
927d169d70bSTobias Grosser printf(" libc.free(ptr)\n");
928d169d70bSTobias Grosser printf(" return res\n");
929d169d70bSTobias Grosser printf(" def __repr__(self):\n");
930d169d70bSTobias Grosser printf(" s = str(self)\n");
931d169d70bSTobias Grosser printf(" if '\"' in s:\n");
932d169d70bSTobias Grosser printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
933d169d70bSTobias Grosser python_name.c_str());
934d169d70bSTobias Grosser printf(" else:\n");
935d169d70bSTobias Grosser printf(" return 'isl.%s(\"%%s\")' %% s\n",
936d169d70bSTobias Grosser python_name.c_str());
937d169d70bSTobias Grosser }
938d169d70bSTobias Grosser
939e8227804SMichael Kruse /* If "clazz" has any persistent callbacks, then print the definition
940e8227804SMichael Kruse * of a "copy_callbacks" function that copies the persistent callbacks
941e8227804SMichael Kruse * from one object to another.
942e8227804SMichael Kruse */
print_copy_callbacks(const isl_class & clazz)943e8227804SMichael Kruse void python_generator::print_copy_callbacks(const isl_class &clazz)
944e8227804SMichael Kruse {
945e8227804SMichael Kruse const set<FunctionDecl *> &callbacks = clazz.persistent_callbacks;
946e8227804SMichael Kruse set<FunctionDecl *>::const_iterator in;
947e8227804SMichael Kruse
948e8227804SMichael Kruse if (!clazz.has_persistent_callbacks())
949e8227804SMichael Kruse return;
950e8227804SMichael Kruse
951e8227804SMichael Kruse printf(" def copy_callbacks(self, obj):\n");
952e8227804SMichael Kruse for (in = callbacks.begin(); in != callbacks.end(); ++in) {
953e8227804SMichael Kruse string callback_name = clazz.persistent_callback_name(*in);
954e8227804SMichael Kruse
955e8227804SMichael Kruse printf(" if hasattr(obj, '%s'):\n",
956e8227804SMichael Kruse callback_name.c_str());
957e8227804SMichael Kruse printf(" self.%s = obj.%s\n",
958e8227804SMichael Kruse callback_name.c_str(), callback_name.c_str());
959e8227804SMichael Kruse }
960e8227804SMichael Kruse }
961e8227804SMichael Kruse
962d169d70bSTobias Grosser /* Print code to set method type signatures.
963d169d70bSTobias Grosser *
964d169d70bSTobias Grosser * To be able to call C functions it is necessary to explicitly set their
965d169d70bSTobias Grosser * argument and result types. Do this for all exported constructors and
966e8227804SMichael Kruse * methods (including those that set a persistent callback and
967e8227804SMichael Kruse * those that set an enum value),
968e8227804SMichael Kruse * as well as for the *_to_str and the type function, if they exist.
969d169d70bSTobias Grosser * Assuming each exported class has a *_copy and a *_free method,
970d169d70bSTobias Grosser * also unconditionally set the type of such methods.
971d169d70bSTobias Grosser */
print_method_types(const isl_class & clazz)972d169d70bSTobias Grosser void python_generator::print_method_types(const isl_class &clazz)
973d169d70bSTobias Grosser {
974e8227804SMichael Kruse function_set::const_iterator in;
975e8227804SMichael Kruse map<string, function_set>::const_iterator it;
976e8227804SMichael Kruse map<FunctionDecl *, vector<set_enum> >::const_iterator ie;
977e8227804SMichael Kruse const set<FunctionDecl *> &callbacks = clazz.persistent_callbacks;
978d169d70bSTobias Grosser
979d169d70bSTobias Grosser for (in = clazz.constructors.begin(); in != clazz.constructors.end();
980d169d70bSTobias Grosser ++in)
981d169d70bSTobias Grosser print_method_type(*in);
982d169d70bSTobias Grosser
983e8227804SMichael Kruse for (in = callbacks.begin(); in != callbacks.end(); ++in)
984e8227804SMichael Kruse print_method_type(*in);
985d169d70bSTobias Grosser for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it)
986d169d70bSTobias Grosser for (in = it->second.begin(); in != it->second.end(); ++in)
987d169d70bSTobias Grosser print_method_type(*in);
988e8227804SMichael Kruse for (ie = clazz.set_enums.begin(); ie != clazz.set_enums.end(); ++ie)
989e8227804SMichael Kruse print_method_type(ie->first);
990d169d70bSTobias Grosser
991d169d70bSTobias Grosser print_method_type(clazz.fn_copy);
992d169d70bSTobias Grosser print_method_type(clazz.fn_free);
993d169d70bSTobias Grosser if (clazz.fn_to_str)
994d169d70bSTobias Grosser print_method_type(clazz.fn_to_str);
995e8227804SMichael Kruse if (clazz.fn_type)
996e8227804SMichael Kruse print_method_type(clazz.fn_type);
997d169d70bSTobias Grosser }
998d169d70bSTobias Grosser
999d169d70bSTobias Grosser /* Print out the definition of this isl_class.
1000d169d70bSTobias Grosser *
1001d169d70bSTobias Grosser * We first check if this isl_class is a subclass of one or more other classes.
1002d169d70bSTobias Grosser * If it is, we make sure those superclasses are printed out first.
1003d169d70bSTobias Grosser *
1004d169d70bSTobias Grosser * Then we print a constructor with several cases, one for constructing
1005e8227804SMichael Kruse * a Python object from a return value, one for each function that
1006e8227804SMichael Kruse * was marked as a constructor and for each type based subclass.
1007d169d70bSTobias Grosser *
1008d169d70bSTobias Grosser * Next, we print out some common methods and the methods corresponding
1009e8227804SMichael Kruse * to functions that are not marked as constructors, including those
1010e8227804SMichael Kruse * that set a persistent callback and those that set an enum value.
1011d169d70bSTobias Grosser *
1012d169d70bSTobias Grosser * Finally, we tell ctypes about the types of the arguments of the
1013d169d70bSTobias Grosser * constructor functions and the return types of those function returning
1014d169d70bSTobias Grosser * an isl object.
1015d169d70bSTobias Grosser */
print(const isl_class & clazz)1016d169d70bSTobias Grosser void python_generator::print(const isl_class &clazz)
1017d169d70bSTobias Grosser {
1018e8227804SMichael Kruse string p_name = type2python(clazz.subclass_name);
1019e8227804SMichael Kruse function_set::const_iterator in;
1020e8227804SMichael Kruse map<string, function_set>::const_iterator it;
1021e8227804SMichael Kruse map<FunctionDecl *, vector<set_enum> >::const_iterator ie;
1022d169d70bSTobias Grosser vector<string> super = find_superclasses(clazz.type);
1023e8227804SMichael Kruse const set<FunctionDecl *> &callbacks = clazz.persistent_callbacks;
1024d169d70bSTobias Grosser
1025d169d70bSTobias Grosser for (unsigned i = 0; i < super.size(); ++i)
1026d169d70bSTobias Grosser if (done.find(super[i]) == done.end())
1027d169d70bSTobias Grosser print(classes[super[i]]);
1028e8227804SMichael Kruse if (clazz.is_type_subclass() && done.find(clazz.name) == done.end())
1029e8227804SMichael Kruse print(classes[clazz.name]);
1030e8227804SMichael Kruse done.insert(clazz.subclass_name);
1031d169d70bSTobias Grosser
1032d169d70bSTobias Grosser printf("\n");
1033d169d70bSTobias Grosser print_class_header(clazz, p_name, super);
1034d169d70bSTobias Grosser printf(" def __init__(self, *args, **keywords):\n");
1035d169d70bSTobias Grosser
1036d169d70bSTobias Grosser printf(" if \"ptr\" in keywords:\n");
1037d169d70bSTobias Grosser printf(" self.ctx = keywords[\"ctx\"]\n");
1038d169d70bSTobias Grosser printf(" self.ptr = keywords[\"ptr\"]\n");
1039d169d70bSTobias Grosser printf(" return\n");
1040d169d70bSTobias Grosser
1041d169d70bSTobias Grosser for (in = clazz.constructors.begin(); in != clazz.constructors.end();
1042d169d70bSTobias Grosser ++in)
1043d169d70bSTobias Grosser print_constructor(clazz, *in);
1044e8227804SMichael Kruse print_upcast_constructors(clazz);
1045d169d70bSTobias Grosser printf(" raise Error\n");
1046d169d70bSTobias Grosser printf(" def __del__(self):\n");
1047d169d70bSTobias Grosser printf(" if hasattr(self, 'ptr'):\n");
1048d169d70bSTobias Grosser printf(" isl.%s_free(self.ptr)\n", clazz.name.c_str());
1049d169d70bSTobias Grosser
1050e8227804SMichael Kruse print_new(clazz, p_name);
1051d169d70bSTobias Grosser print_representation(clazz, p_name);
1052e8227804SMichael Kruse print_copy_callbacks(clazz);
1053d169d70bSTobias Grosser
1054e8227804SMichael Kruse for (in = callbacks.begin(); in != callbacks.end(); ++in)
1055e8227804SMichael Kruse print_method(clazz, *in, super);
1056d169d70bSTobias Grosser for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it)
1057d169d70bSTobias Grosser print_method(clazz, it->first, it->second, super);
1058e8227804SMichael Kruse for (ie = clazz.set_enums.begin(); ie != clazz.set_enums.end(); ++ie)
1059e8227804SMichael Kruse print_set_enum(clazz, ie->first, super);
1060d169d70bSTobias Grosser
1061d169d70bSTobias Grosser printf("\n");
1062d169d70bSTobias Grosser
1063d169d70bSTobias Grosser print_method_types(clazz);
1064d169d70bSTobias Grosser }
1065d169d70bSTobias Grosser
1066d169d70bSTobias Grosser /* Generate a python interface based on the extracted types and
1067d169d70bSTobias Grosser * functions.
1068d169d70bSTobias Grosser *
1069d169d70bSTobias Grosser * Print out each class in turn. If one of these is a subclass of some
1070d169d70bSTobias Grosser * other class, make sure the superclass is printed out first.
1071d169d70bSTobias Grosser * functions.
1072d169d70bSTobias Grosser */
generate()1073d169d70bSTobias Grosser void python_generator::generate()
1074d169d70bSTobias Grosser {
1075d169d70bSTobias Grosser map<string, isl_class>::iterator ci;
1076d169d70bSTobias Grosser
1077d169d70bSTobias Grosser for (ci = classes.begin(); ci != classes.end(); ++ci) {
1078d169d70bSTobias Grosser if (done.find(ci->first) == done.end())
1079d169d70bSTobias Grosser print(ci->second);
1080d169d70bSTobias Grosser }
1081d169d70bSTobias Grosser }
1082