introduce isl_size return type
[isl.git] / interface / python.cc
blobff3f927c90650fc484f55a27d9d4f79e81c1c608
1 /*
2 * Copyright 2011,2015 Sven Verdoolaege. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY SVEN VERDOOLAEGE ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SVEN VERDOOLAEGE OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Sven Verdoolaege.
32 */
34 #include "isl_config.h"
36 #include <stdio.h>
37 #include <iostream>
38 #include <map>
39 #include <vector>
41 #include "python.h"
42 #include "generator.h"
44 /* Drop the "isl_" initial part of the type name "name".
46 static string type2python(string name)
48 return name.substr(4);
51 /* Print the header of the method "name" with "n_arg" arguments.
52 * If "is_static" is set, then mark the python method as static.
54 * If the method is called "from", then rename it to "convert_from"
55 * because "from" is a python keyword.
57 void python_generator::print_method_header(bool is_static, const string &name,
58 int n_arg)
60 const char *s;
62 if (is_static)
63 printf(" @staticmethod\n");
65 s = name.c_str();
66 if (name == "from")
67 s = "convert_from";
69 printf(" def %s(", s);
70 for (int i = 0; i < n_arg; ++i) {
71 if (i)
72 printf(", ");
73 printf("arg%d", i);
75 printf("):\n");
78 /* Print a check that the argument in position "pos" is of type "type".
79 * If this fails and if "upcast" is set, then convert the first
80 * argument to "super" and call the method "name" on it, passing
81 * the remaining of the "n" arguments.
82 * If the check fails and "upcast" is not set, then simply raise
83 * an exception.
84 * If "upcast" is not set, then the "super", "name" and "n" arguments
85 * to this function are ignored.
87 void python_generator::print_type_check(const string &type, int pos,
88 bool upcast, const string &super, const string &name, int n)
90 printf(" try:\n");
91 printf(" if not arg%d.__class__ is %s:\n",
92 pos, type.c_str());
93 printf(" arg%d = %s(arg%d)\n",
94 pos, type.c_str(), pos);
95 printf(" except:\n");
96 if (upcast) {
97 printf(" return %s(arg0).%s(",
98 type2python(super).c_str(), name.c_str());
99 for (int i = 1; i < n; ++i) {
100 if (i != 1)
101 printf(", ");
102 printf("arg%d", i);
104 printf(")\n");
105 } else
106 printf(" raise\n");
109 /* Print a call to the *_copy function corresponding to "type".
111 void python_generator::print_copy(QualType type)
113 string type_s = extract_type(type);
115 printf("isl.%s_copy", type_s.c_str());
118 /* Construct a wrapper for callback argument "param" (at position "arg").
119 * Assign the wrapper to "cb". We assume here that a function call
120 * has at most one callback argument.
122 * The wrapper converts the arguments of the callback to python types,
123 * taking a copy if the C callback does not take its arguments.
124 * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
125 * and returns -1. Otherwise the wrapper returns 0.
127 void python_generator::print_callback(ParmVarDecl *param, int arg)
129 QualType type = param->getOriginalType();
130 const FunctionProtoType *fn = extract_prototype(type);
131 unsigned n_arg = fn->getNumArgs();
133 printf(" exc_info = [None]\n");
134 printf(" fn = CFUNCTYPE(c_int");
135 for (unsigned i = 0; i < n_arg - 1; ++i) {
136 if (!is_isl_type(fn->getArgType(i)))
137 die("Argument has non-isl type");
138 printf(", c_void_p");
140 printf(", c_void_p)\n");
141 printf(" def cb_func(");
142 for (unsigned i = 0; i < n_arg; ++i) {
143 if (i)
144 printf(", ");
145 printf("cb_arg%d", i);
147 printf("):\n");
148 for (unsigned i = 0; i < n_arg - 1; ++i) {
149 string arg_type;
150 arg_type = type2python(extract_type(fn->getArgType(i)));
151 printf(" cb_arg%d = %s(ctx=arg0.ctx, ptr=",
152 i, arg_type.c_str());
153 if (!callback_takes_argument(param, i))
154 print_copy(fn->getArgType(i));
155 printf("(cb_arg%d))\n", i);
157 printf(" try:\n");
158 printf(" arg%d(", arg);
159 for (unsigned i = 0; i < n_arg - 1; ++i) {
160 if (i)
161 printf(", ");
162 printf("cb_arg%d", i);
164 printf(")\n");
165 printf(" except:\n");
166 printf(" import sys\n");
167 printf(" exc_info[0] = sys.exc_info()\n");
168 printf(" return -1\n");
169 printf(" return 0\n");
170 printf(" cb = fn(cb_func)\n");
173 /* Print the argument at position "arg" in call to "fd".
174 * "skip" is the number of initial arguments of "fd" that are
175 * skipped in the Python method.
177 * If the argument is a callback, then print a reference to
178 * the callback wrapper "cb".
179 * Otherwise, if the argument is marked as consuming a reference,
180 * then pass a copy of the pointer stored in the corresponding
181 * argument passed to the Python method.
182 * Otherwise, if the argument is a pointer, then pass this pointer itself.
183 * Otherwise, pass the argument directly.
185 void python_generator::print_arg_in_call(FunctionDecl *fd, int arg, int skip)
187 ParmVarDecl *param = fd->getParamDecl(arg);
188 QualType type = param->getOriginalType();
189 if (is_callback(type)) {
190 printf("cb");
191 } else if (takes(param)) {
192 print_copy(type);
193 printf("(arg%d.ptr)", arg - skip);
194 } else if (type->isPointerType()) {
195 printf("arg%d.ptr", arg - skip);
196 } else {
197 printf("arg%d", arg - skip);
201 /* Print the return statement of the python method corresponding
202 * to the C function "method".
204 * If the return type is a (const) char *, then convert the result
205 * to a Python string, raising an error on NULL and freeing
206 * the C string if needed. For python 3 compatibility, the string returned
207 * by isl is explicitly decoded as an 'ascii' string. This is correct
208 * as all strings returned by isl are expected to be 'ascii'.
210 * If the return type is isl_stat, isl_bool or isl_size, then
211 * raise an error on isl_stat_error, isl_bool_error or isl_size_error.
212 * In case of isl_bool, the result is converted to
213 * a Python boolean.
214 * In case of isl_size, the result is converted to a Python int.
216 void python_generator::print_method_return(FunctionDecl *method)
218 QualType return_type = method->getReturnType();
220 if (is_isl_type(return_type)) {
221 string type;
223 type = type2python(extract_type(return_type));
224 printf(" return %s(ctx=ctx, ptr=res)\n", type.c_str());
225 } else if (is_string(return_type)) {
226 printf(" if res == 0:\n");
227 printf(" raise\n");
228 printf(" string = "
229 "cast(res, c_char_p).value.decode('ascii')\n");
231 if (gives(method))
232 printf(" libc.free(res)\n");
234 printf(" return string\n");
235 } else if (is_isl_neg_error(return_type)) {
236 printf(" if res < 0:\n");
237 printf(" raise\n");
238 if (is_isl_bool(return_type))
239 printf(" return bool(res)\n");
240 else if (is_isl_size(return_type))
241 printf(" return int(res)\n");
242 } else {
243 printf(" return res\n");
247 /* Print a python method corresponding to the C function "method".
248 * "super" contains the superclasses of the class to which the method belongs,
249 * with the first element corresponding to the annotation that appears
250 * closest to the annotated type. This superclass is the least
251 * general extension of the annotated type in the linearization
252 * of the class hierarchy.
254 * If the first argument of "method" is something other than an instance
255 * of the class, then mark the python method as static.
256 * If, moreover, this first argument is an isl_ctx, then remove
257 * it from the arguments of the Python method.
259 * If the function has a callback argument, then it also has a "user"
260 * argument. Since Python has closures, there is no need for such
261 * a user argument in the Python interface, so we simply drop it.
262 * We also create a wrapper ("cb") for the callback.
264 * For each argument of the function that refers to an isl structure,
265 * including the object on which the method is called,
266 * we check if the corresponding actual argument is of the right type.
267 * If not, we try to convert it to the right type.
268 * If that doesn't work and if "super" contains at least one element, we try
269 * to convert self to the type of the first superclass in "super" and
270 * call the corresponding method.
272 * If the function consumes a reference, then we pass it a copy of
273 * the actual argument.
275 void python_generator::print_method(const isl_class &clazz,
276 FunctionDecl *method, vector<string> super)
278 string fullname = method->getName();
279 string cname = clazz.method_name(method);
280 int num_params = method->getNumParams();
281 int drop_user = 0;
282 int drop_ctx = first_arg_is_isl_ctx(method);
284 for (int i = 1; i < num_params; ++i) {
285 ParmVarDecl *param = method->getParamDecl(i);
286 QualType type = param->getOriginalType();
287 if (is_callback(type))
288 drop_user = 1;
291 print_method_header(is_static(clazz, method), cname,
292 num_params - drop_ctx - drop_user);
294 for (int i = drop_ctx; i < num_params; ++i) {
295 ParmVarDecl *param = method->getParamDecl(i);
296 string type;
297 if (!is_isl_type(param->getOriginalType()))
298 continue;
299 type = type2python(extract_type(param->getOriginalType()));
300 if (!drop_ctx && i > 0 && super.size() > 0)
301 print_type_check(type, i - drop_ctx, true, super[0],
302 cname, num_params - drop_user);
303 else
304 print_type_check(type, i - drop_ctx, false, "",
305 cname, -1);
307 for (int i = 1; i < num_params; ++i) {
308 ParmVarDecl *param = method->getParamDecl(i);
309 QualType type = param->getOriginalType();
310 if (!is_callback(type))
311 continue;
312 print_callback(param, i - drop_ctx);
314 if (drop_ctx)
315 printf(" ctx = Context.getDefaultInstance()\n");
316 else
317 printf(" ctx = arg0.ctx\n");
318 printf(" res = isl.%s(", fullname.c_str());
319 if (drop_ctx)
320 printf("ctx");
321 else
322 print_arg_in_call(method, 0, 0);
323 for (int i = 1; i < num_params - drop_user; ++i) {
324 printf(", ");
325 print_arg_in_call(method, i, drop_ctx);
327 if (drop_user)
328 printf(", None");
329 printf(")\n");
331 if (drop_user) {
332 printf(" if exc_info[0] != None:\n");
333 printf(" raise (exc_info[0][0], "
334 "exc_info[0][1], exc_info[0][2])\n");
337 print_method_return(method);
340 /* Print part of an overloaded python method corresponding to the C function
341 * "method".
343 * In particular, print code to test whether the arguments passed to
344 * the python method correspond to the arguments expected by "method"
345 * and to call "method" if they do.
347 void python_generator::print_method_overload(const isl_class &clazz,
348 FunctionDecl *method)
350 string fullname = method->getName();
351 int num_params = method->getNumParams();
352 int first;
353 string type;
355 first = is_static(clazz, method) ? 0 : 1;
357 printf(" if ");
358 for (int i = first; i < num_params; ++i) {
359 if (i > first)
360 printf(" and ");
361 ParmVarDecl *param = method->getParamDecl(i);
362 if (is_isl_type(param->getOriginalType())) {
363 string type;
364 type = extract_type(param->getOriginalType());
365 type = type2python(type);
366 printf("arg%d.__class__ is %s", i, type.c_str());
367 } else
368 printf("type(arg%d) == str", i);
370 printf(":\n");
371 printf(" res = isl.%s(", fullname.c_str());
372 print_arg_in_call(method, 0, 0);
373 for (int i = 1; i < num_params; ++i) {
374 printf(", ");
375 print_arg_in_call(method, i, 0);
377 printf(")\n");
378 type = type2python(extract_type(method->getReturnType()));
379 printf(" return %s(ctx=arg0.ctx, ptr=res)\n", type.c_str());
382 /* Print a python method with a name derived from "fullname"
383 * corresponding to the C functions "methods".
384 * "super" contains the superclasses of the class to which the method belongs.
386 * If "methods" consists of a single element that is not marked overloaded,
387 * the use print_method to print the method.
388 * Otherwise, print an overloaded method with pieces corresponding
389 * to each function in "methods".
391 void python_generator::print_method(const isl_class &clazz,
392 const string &fullname, const set<FunctionDecl *> &methods,
393 vector<string> super)
395 string cname;
396 set<FunctionDecl *>::const_iterator it;
397 int num_params;
398 FunctionDecl *any_method;
400 any_method = *methods.begin();
401 if (methods.size() == 1 && !is_overload(any_method)) {
402 print_method(clazz, any_method, super);
403 return;
406 cname = clazz.method_name(any_method);
407 num_params = any_method->getNumParams();
409 print_method_header(is_static(clazz, any_method), cname, num_params);
411 for (it = methods.begin(); it != methods.end(); ++it)
412 print_method_overload(clazz, *it);
415 /* Print part of the constructor for this isl_class.
417 * In particular, check if the actual arguments correspond to the
418 * formal arguments of "cons" and if so call "cons" and put the
419 * result in self.ptr and a reference to the default context in self.ctx.
421 * If the function consumes a reference, then we pass it a copy of
422 * the actual argument.
424 * If the function takes a string argument, the python string is first
425 * encoded as a byte sequence, using 'ascii' as encoding. This assumes
426 * that all strings passed to isl can be converted to 'ascii'.
428 void python_generator::print_constructor(const isl_class &clazz,
429 FunctionDecl *cons)
431 string fullname = cons->getName();
432 string cname = clazz.method_name(cons);
433 int num_params = cons->getNumParams();
434 int drop_ctx = first_arg_is_isl_ctx(cons);
436 printf(" if len(args) == %d", num_params - drop_ctx);
437 for (int i = drop_ctx; i < num_params; ++i) {
438 ParmVarDecl *param = cons->getParamDecl(i);
439 QualType type = param->getOriginalType();
440 if (is_isl_type(type)) {
441 string s;
442 s = type2python(extract_type(type));
443 printf(" and args[%d].__class__ is %s",
444 i - drop_ctx, s.c_str());
445 } else if (type->isPointerType()) {
446 printf(" and type(args[%d]) == str", i - drop_ctx);
447 } else {
448 printf(" and type(args[%d]) == int", i - drop_ctx);
451 printf(":\n");
452 printf(" self.ctx = Context.getDefaultInstance()\n");
453 printf(" self.ptr = isl.%s(", fullname.c_str());
454 if (drop_ctx)
455 printf("self.ctx");
456 for (int i = drop_ctx; i < num_params; ++i) {
457 ParmVarDecl *param = cons->getParamDecl(i);
458 QualType type = param->getOriginalType();
459 if (i)
460 printf(", ");
461 if (is_isl_type(type)) {
462 if (takes(param))
463 print_copy(param->getOriginalType());
464 printf("(args[%d].ptr)", i - drop_ctx);
465 } else if (is_string(type)) {
466 printf("args[%d].encode('ascii')", i - drop_ctx);
467 } else {
468 printf("args[%d]", i - drop_ctx);
471 printf(")\n");
472 printf(" return\n");
475 /* Print the header of the class "name" with superclasses "super".
476 * The order of the superclasses is the opposite of the order
477 * in which the corresponding annotations appear in the source code.
479 void python_generator::print_class_header(const isl_class &clazz,
480 const string &name, const vector<string> &super)
482 printf("class %s", name.c_str());
483 if (super.size() > 0) {
484 printf("(");
485 for (unsigned i = 0; i < super.size(); ++i) {
486 if (i > 0)
487 printf(", ");
488 printf("%s", type2python(super[i]).c_str());
490 printf(")");
491 } else {
492 printf("(object)");
494 printf(":\n");
497 /* Tell ctypes about the return type of "fd".
498 * In particular, if "fd" returns a pointer to an isl object,
499 * then tell ctypes it returns a "c_void_p".
500 * If "fd" returns a char *, then simply tell ctypes.
502 * Nothing needs to be done for functions returning
503 * isl_bool, isl_stat or isl_size since they are represented by an int and
504 * ctypes assumes that a function returns int by default.
506 void python_generator::print_restype(FunctionDecl *fd)
508 string fullname = fd->getName();
509 QualType type = fd->getReturnType();
510 if (is_isl_type(type))
511 printf("isl.%s.restype = c_void_p\n", fullname.c_str());
512 else if (is_string(type))
513 printf("isl.%s.restype = POINTER(c_char)\n", fullname.c_str());
516 /* Tell ctypes about the types of the arguments of the function "fd".
518 void python_generator::print_argtypes(FunctionDecl *fd)
520 string fullname = fd->getName();
521 int n = fd->getNumParams();
522 int drop_user = 0;
524 printf("isl.%s.argtypes = [", fullname.c_str());
525 for (int i = 0; i < n - drop_user; ++i) {
526 ParmVarDecl *param = fd->getParamDecl(i);
527 QualType type = param->getOriginalType();
528 if (is_callback(type))
529 drop_user = 1;
530 if (i)
531 printf(", ");
532 if (is_isl_ctx(type))
533 printf("Context");
534 else if (is_isl_type(type) || is_callback(type))
535 printf("c_void_p");
536 else if (is_string(type))
537 printf("c_char_p");
538 else if (is_long(type))
539 printf("c_long");
540 else
541 printf("c_int");
543 if (drop_user)
544 printf(", c_void_p");
545 printf("]\n");
548 /* Print type definitions for the method 'fd'.
550 void python_generator::print_method_type(FunctionDecl *fd)
552 print_restype(fd);
553 print_argtypes(fd);
556 /* Print declarations for methods printing the class representation,
557 * provided there is a corresponding *_to_str function.
559 * In particular, provide an implementation of __str__ and __repr__ methods to
560 * override the default representation used by python. Python uses __str__ to
561 * pretty print the class (e.g., when calling print(obj)) and uses __repr__
562 * when printing a precise representation of an object (e.g., when dumping it
563 * in the REPL console).
565 * Check the type of the argument before calling the *_to_str function
566 * on it in case the method was called on an object from a subclass.
568 * The return value of the *_to_str function is decoded to a python string
569 * assuming an 'ascii' encoding. This is necessary for python 3 compatibility.
571 void python_generator::print_representation(const isl_class &clazz,
572 const string &python_name)
574 if (!clazz.fn_to_str)
575 return;
577 printf(" def __str__(arg0):\n");
578 print_type_check(python_name, 0, false, "", "", -1);
579 printf(" ptr = isl.%s(arg0.ptr)\n",
580 string(clazz.fn_to_str->getName()).c_str());
581 printf(" res = cast(ptr, c_char_p).value.decode('ascii')\n");
582 printf(" libc.free(ptr)\n");
583 printf(" return res\n");
584 printf(" def __repr__(self):\n");
585 printf(" s = str(self)\n");
586 printf(" if '\"' in s:\n");
587 printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
588 python_name.c_str());
589 printf(" else:\n");
590 printf(" return 'isl.%s(\"%%s\")' %% s\n",
591 python_name.c_str());
594 /* Print code to set method type signatures.
596 * To be able to call C functions it is necessary to explicitly set their
597 * argument and result types. Do this for all exported constructors and
598 * methods, as well as for the *_to_str method, if it exists.
599 * Assuming each exported class has a *_copy and a *_free method,
600 * also unconditionally set the type of such methods.
602 void python_generator::print_method_types(const isl_class &clazz)
604 set<FunctionDecl *>::const_iterator in;
605 map<string, set<FunctionDecl *> >::const_iterator it;
607 for (in = clazz.constructors.begin(); in != clazz.constructors.end();
608 ++in)
609 print_method_type(*in);
611 for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it)
612 for (in = it->second.begin(); in != it->second.end(); ++in)
613 print_method_type(*in);
615 print_method_type(clazz.fn_copy);
616 print_method_type(clazz.fn_free);
617 if (clazz.fn_to_str)
618 print_method_type(clazz.fn_to_str);
621 /* Print out the definition of this isl_class.
623 * We first check if this isl_class is a subclass of one or more other classes.
624 * If it is, we make sure those superclasses are printed out first.
626 * Then we print a constructor with several cases, one for constructing
627 * a Python object from a return value and one for each function that
628 * was marked as a constructor.
630 * Next, we print out some common methods and the methods corresponding
631 * to functions that are not marked as constructors.
633 * Finally, we tell ctypes about the types of the arguments of the
634 * constructor functions and the return types of those function returning
635 * an isl object.
637 void python_generator::print(const isl_class &clazz)
639 string p_name = type2python(clazz.name);
640 set<FunctionDecl *>::const_iterator in;
641 map<string, set<FunctionDecl *> >::const_iterator it;
642 vector<string> super = find_superclasses(clazz.type);
644 for (unsigned i = 0; i < super.size(); ++i)
645 if (done.find(super[i]) == done.end())
646 print(classes[super[i]]);
647 done.insert(clazz.name);
649 printf("\n");
650 print_class_header(clazz, p_name, super);
651 printf(" def __init__(self, *args, **keywords):\n");
653 printf(" if \"ptr\" in keywords:\n");
654 printf(" self.ctx = keywords[\"ctx\"]\n");
655 printf(" self.ptr = keywords[\"ptr\"]\n");
656 printf(" return\n");
658 for (in = clazz.constructors.begin(); in != clazz.constructors.end();
659 ++in)
660 print_constructor(clazz, *in);
661 printf(" raise Error\n");
662 printf(" def __del__(self):\n");
663 printf(" if hasattr(self, 'ptr'):\n");
664 printf(" isl.%s_free(self.ptr)\n", clazz.name.c_str());
666 print_representation(clazz, p_name);
668 for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it)
669 print_method(clazz, it->first, it->second, super);
671 printf("\n");
673 print_method_types(clazz);
676 /* Generate a python interface based on the extracted types and
677 * functions.
679 * Print out each class in turn. If one of these is a subclass of some
680 * other class, make sure the superclass is printed out first.
681 * functions.
683 void python_generator::generate()
685 map<string, isl_class>::iterator ci;
687 for (ci = classes.begin(); ci != classes.end(); ++ci) {
688 if (done.find(ci->first) == done.end())
689 print(ci->second);