interface: add support for type based subclasses
[isl.git] / interface / python.cc
blob9ecee1901e49c01977702d06fd253e65983aa88d
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 a value indicating an error. Otherwise the wrapper
126 * returns a value indicating success.
127 * In case the C callback is expected to return an isl_stat,
128 * the error value is -1 and the success value is 0.
129 * In case the C callback is expected to return an isl_bool,
130 * the error value is -1 and the success value is 1 or 0 depending
131 * on the result of the Python callback.
132 * Otherwise, None is returned to indicate an error and
133 * a copy of the object in case of success.
135 void python_generator::print_callback(ParmVarDecl *param, int arg)
137 QualType type = param->getOriginalType();
138 const FunctionProtoType *fn = extract_prototype(type);
139 QualType return_type = fn->getReturnType();
140 unsigned n_arg = fn->getNumArgs();
142 printf(" exc_info = [None]\n");
143 printf(" fn = CFUNCTYPE(");
144 if (is_isl_stat(return_type) || is_isl_bool(return_type))
145 printf("c_int");
146 else
147 printf("c_void_p");
148 for (unsigned i = 0; i < n_arg - 1; ++i) {
149 if (!is_isl_type(fn->getArgType(i)))
150 die("Argument has non-isl type");
151 printf(", c_void_p");
153 printf(", c_void_p)\n");
154 printf(" def cb_func(");
155 for (unsigned i = 0; i < n_arg; ++i) {
156 if (i)
157 printf(", ");
158 printf("cb_arg%d", i);
160 printf("):\n");
161 for (unsigned i = 0; i < n_arg - 1; ++i) {
162 string arg_type;
163 arg_type = type2python(extract_type(fn->getArgType(i)));
164 printf(" cb_arg%d = %s(ctx=arg0.ctx, ptr=",
165 i, arg_type.c_str());
166 if (!callback_takes_argument(param, i))
167 print_copy(fn->getArgType(i));
168 printf("(cb_arg%d))\n", i);
170 printf(" try:\n");
171 if (is_isl_stat(return_type))
172 printf(" arg%d(", arg);
173 else
174 printf(" res = arg%d(", arg);
175 for (unsigned i = 0; i < n_arg - 1; ++i) {
176 if (i)
177 printf(", ");
178 printf("cb_arg%d", i);
180 printf(")\n");
181 printf(" except:\n");
182 printf(" import sys\n");
183 printf(" exc_info[0] = sys.exc_info()\n");
184 if (is_isl_stat(return_type) || is_isl_bool(return_type))
185 printf(" return -1\n");
186 else
187 printf(" return None\n");
188 if (is_isl_stat(return_type)) {
189 printf(" return 0\n");
190 } else if (is_isl_bool(return_type)) {
191 printf(" return 1 if res else 0\n");
192 } else {
193 printf(" return ");
194 print_copy(return_type);
195 printf("(res.ptr)\n");
197 printf(" cb = fn(cb_func)\n");
200 /* Print the argument at position "arg" in call to "fd".
201 * "skip" is the number of initial arguments of "fd" that are
202 * skipped in the Python method.
204 * If the argument is a callback, then print a reference to
205 * the callback wrapper "cb".
206 * Otherwise, if the argument is marked as consuming a reference,
207 * then pass a copy of the pointer stored in the corresponding
208 * argument passed to the Python method.
209 * Otherwise, if the argument is a pointer, then pass this pointer itself.
210 * Otherwise, pass the argument directly.
212 void python_generator::print_arg_in_call(FunctionDecl *fd, int arg, int skip)
214 ParmVarDecl *param = fd->getParamDecl(arg);
215 QualType type = param->getOriginalType();
216 if (is_callback(type)) {
217 printf("cb");
218 } else if (takes(param)) {
219 print_copy(type);
220 printf("(arg%d.ptr)", arg - skip);
221 } else if (type->isPointerType()) {
222 printf("arg%d.ptr", arg - skip);
223 } else {
224 printf("arg%d", arg - skip);
228 /* Print the return statement of the python method corresponding
229 * to the C function "method".
231 * If the return type is a (const) char *, then convert the result
232 * to a Python string, raising an error on NULL and freeing
233 * the C string if needed. For python 3 compatibility, the string returned
234 * by isl is explicitly decoded as an 'ascii' string. This is correct
235 * as all strings returned by isl are expected to be 'ascii'.
237 * If the return type is isl_stat, isl_bool or isl_size, then
238 * raise an error on isl_stat_error, isl_bool_error or isl_size_error.
239 * In case of isl_bool, the result is converted to
240 * a Python boolean.
241 * In case of isl_size, the result is converted to a Python int.
243 void python_generator::print_method_return(FunctionDecl *method)
245 QualType return_type = method->getReturnType();
247 if (is_isl_type(return_type)) {
248 string type;
250 type = type2python(extract_type(return_type));
251 printf(" return %s(ctx=ctx, ptr=res)\n", type.c_str());
252 } else if (is_string(return_type)) {
253 printf(" if res == 0:\n");
254 printf(" raise\n");
255 printf(" string = "
256 "cast(res, c_char_p).value.decode('ascii')\n");
258 if (gives(method))
259 printf(" libc.free(res)\n");
261 printf(" return string\n");
262 } else if (is_isl_neg_error(return_type)) {
263 printf(" if res < 0:\n");
264 printf(" raise\n");
265 if (is_isl_bool(return_type))
266 printf(" return bool(res)\n");
267 else if (is_isl_size(return_type))
268 printf(" return int(res)\n");
269 } else {
270 printf(" return res\n");
274 /* Print a python method corresponding to the C function "method".
275 * "super" contains the superclasses of the class to which the method belongs,
276 * with the first element corresponding to the annotation that appears
277 * closest to the annotated type. This superclass is the least
278 * general extension of the annotated type in the linearization
279 * of the class hierarchy.
281 * If the first argument of "method" is something other than an instance
282 * of the class, then mark the python method as static.
283 * If, moreover, this first argument is an isl_ctx, then remove
284 * it from the arguments of the Python method.
286 * If the function has a callback argument, then it also has a "user"
287 * argument. Since Python has closures, there is no need for such
288 * a user argument in the Python interface, so we simply drop it.
289 * We also create a wrapper ("cb") for the callback.
291 * For each argument of the function that refers to an isl structure,
292 * including the object on which the method is called,
293 * we check if the corresponding actual argument is of the right type.
294 * If not, we try to convert it to the right type.
295 * If that doesn't work and if "super" contains at least one element, we try
296 * to convert self to the type of the first superclass in "super" and
297 * call the corresponding method.
299 * If the function consumes a reference, then we pass it a copy of
300 * the actual argument.
302 void python_generator::print_method(const isl_class &clazz,
303 FunctionDecl *method, vector<string> super)
305 string fullname = method->getName();
306 string cname = clazz.method_name(method);
307 int num_params = method->getNumParams();
308 int drop_user = 0;
309 int drop_ctx = first_arg_is_isl_ctx(method);
311 for (int i = 1; i < num_params; ++i) {
312 ParmVarDecl *param = method->getParamDecl(i);
313 QualType type = param->getOriginalType();
314 if (is_callback(type))
315 drop_user = 1;
318 print_method_header(is_static(clazz, method), cname,
319 num_params - drop_ctx - drop_user);
321 for (int i = drop_ctx; i < num_params; ++i) {
322 ParmVarDecl *param = method->getParamDecl(i);
323 string type;
324 if (!is_isl_type(param->getOriginalType()))
325 continue;
326 type = type2python(extract_type(param->getOriginalType()));
327 if (!drop_ctx && i > 0 && super.size() > 0)
328 print_type_check(type, i - drop_ctx, true, super[0],
329 cname, num_params - drop_user);
330 else
331 print_type_check(type, i - drop_ctx, false, "",
332 cname, -1);
334 for (int i = 1; i < num_params; ++i) {
335 ParmVarDecl *param = method->getParamDecl(i);
336 QualType type = param->getOriginalType();
337 if (!is_callback(type))
338 continue;
339 print_callback(param, i - drop_ctx);
341 if (drop_ctx)
342 printf(" ctx = Context.getDefaultInstance()\n");
343 else
344 printf(" ctx = arg0.ctx\n");
345 printf(" res = isl.%s(", fullname.c_str());
346 if (drop_ctx)
347 printf("ctx");
348 else
349 print_arg_in_call(method, 0, 0);
350 for (int i = 1; i < num_params - drop_user; ++i) {
351 printf(", ");
352 print_arg_in_call(method, i, drop_ctx);
354 if (drop_user)
355 printf(", None");
356 printf(")\n");
358 if (drop_user) {
359 printf(" if exc_info[0] != None:\n");
360 printf(" raise (exc_info[0][0], "
361 "exc_info[0][1], exc_info[0][2])\n");
364 print_method_return(method);
367 /* Print part of an overloaded python method corresponding to the C function
368 * "method".
370 * In particular, print code to test whether the arguments passed to
371 * the python method correspond to the arguments expected by "method"
372 * and to call "method" if they do.
374 void python_generator::print_method_overload(const isl_class &clazz,
375 FunctionDecl *method)
377 string fullname = method->getName();
378 int num_params = method->getNumParams();
379 int first;
380 string type;
382 first = is_static(clazz, method) ? 0 : 1;
384 printf(" if ");
385 for (int i = first; i < num_params; ++i) {
386 if (i > first)
387 printf(" and ");
388 ParmVarDecl *param = method->getParamDecl(i);
389 if (is_isl_type(param->getOriginalType())) {
390 string type;
391 type = extract_type(param->getOriginalType());
392 type = type2python(type);
393 printf("arg%d.__class__ is %s", i, type.c_str());
394 } else
395 printf("type(arg%d) == str", i);
397 printf(":\n");
398 printf(" res = isl.%s(", fullname.c_str());
399 print_arg_in_call(method, 0, 0);
400 for (int i = 1; i < num_params; ++i) {
401 printf(", ");
402 print_arg_in_call(method, i, 0);
404 printf(")\n");
405 type = type2python(extract_type(method->getReturnType()));
406 printf(" return %s(ctx=arg0.ctx, ptr=res)\n", type.c_str());
409 /* Print a python method with a name derived from "fullname"
410 * corresponding to the C functions "methods".
411 * "super" contains the superclasses of the class to which the method belongs.
413 * If "methods" consists of a single element that is not marked overloaded,
414 * the use print_method to print the method.
415 * Otherwise, print an overloaded method with pieces corresponding
416 * to each function in "methods".
418 void python_generator::print_method(const isl_class &clazz,
419 const string &fullname, const set<FunctionDecl *> &methods,
420 vector<string> super)
422 string cname;
423 set<FunctionDecl *>::const_iterator it;
424 int num_params;
425 FunctionDecl *any_method;
427 any_method = *methods.begin();
428 if (methods.size() == 1 && !is_overload(any_method)) {
429 print_method(clazz, any_method, super);
430 return;
433 cname = clazz.method_name(any_method);
434 num_params = any_method->getNumParams();
436 print_method_header(is_static(clazz, any_method), cname, num_params);
438 for (it = methods.begin(); it != methods.end(); ++it)
439 print_method_overload(clazz, *it);
442 /* Print part of the constructor for this isl_class.
444 * In particular, check if the actual arguments correspond to the
445 * formal arguments of "cons" and if so call "cons" and put the
446 * result in self.ptr and a reference to the default context in self.ctx.
448 * If the function consumes a reference, then we pass it a copy of
449 * the actual argument.
451 * If the function takes a string argument, the python string is first
452 * encoded as a byte sequence, using 'ascii' as encoding. This assumes
453 * that all strings passed to isl can be converted to 'ascii'.
455 void python_generator::print_constructor(const isl_class &clazz,
456 FunctionDecl *cons)
458 string fullname = cons->getName();
459 string cname = clazz.method_name(cons);
460 int num_params = cons->getNumParams();
461 int drop_ctx = first_arg_is_isl_ctx(cons);
463 printf(" if len(args) == %d", num_params - drop_ctx);
464 for (int i = drop_ctx; i < num_params; ++i) {
465 ParmVarDecl *param = cons->getParamDecl(i);
466 QualType type = param->getOriginalType();
467 if (is_isl_type(type)) {
468 string s;
469 s = type2python(extract_type(type));
470 printf(" and args[%d].__class__ is %s",
471 i - drop_ctx, s.c_str());
472 } else if (type->isPointerType()) {
473 printf(" and type(args[%d]) == str", i - drop_ctx);
474 } else {
475 printf(" and type(args[%d]) == int", i - drop_ctx);
478 printf(":\n");
479 printf(" self.ctx = Context.getDefaultInstance()\n");
480 printf(" self.ptr = isl.%s(", fullname.c_str());
481 if (drop_ctx)
482 printf("self.ctx");
483 for (int i = drop_ctx; i < num_params; ++i) {
484 ParmVarDecl *param = cons->getParamDecl(i);
485 QualType type = param->getOriginalType();
486 if (i)
487 printf(", ");
488 if (is_isl_type(type)) {
489 if (takes(param))
490 print_copy(param->getOriginalType());
491 printf("(args[%d].ptr)", i - drop_ctx);
492 } else if (is_string(type)) {
493 printf("args[%d].encode('ascii')", i - drop_ctx);
494 } else {
495 printf("args[%d]", i - drop_ctx);
498 printf(")\n");
499 printf(" return\n");
502 /* If "clazz" has a type function describing subclasses,
503 * then add constructors that allow each of these subclasses
504 * to be treated as an object to the superclass.
506 void python_generator::print_upcast_constructors(const isl_class &clazz)
508 map<int, string>::const_iterator i;
510 if (!clazz.fn_type)
511 return;
513 for (i = clazz.type_subclasses.begin();
514 i != clazz.type_subclasses.end(); ++i) {
515 printf(" if len(args) == 1 and "
516 "isinstance(args[0], %s):\n",
517 type2python(i->second).c_str());
518 printf(" self.ctx = args[0].ctx\n");
519 printf(" self.ptr = isl.%s_copy(args[0].ptr)\n",
520 clazz.name.c_str());
521 printf(" return\n");
525 /* Print the header of the class "name" with superclasses "super".
526 * The order of the superclasses is the opposite of the order
527 * in which the corresponding annotations appear in the source code.
528 * If "clazz" is a subclass derived from a type function,
529 * then the immediate superclass is recorded in "clazz" itself.
531 void python_generator::print_class_header(const isl_class &clazz,
532 const string &name, const vector<string> &super)
534 printf("class %s", name.c_str());
535 if (super.size() > 0) {
536 printf("(");
537 for (unsigned i = 0; i < super.size(); ++i) {
538 if (i > 0)
539 printf(", ");
540 printf("%s", type2python(super[i]).c_str());
542 printf(")");
543 } else if (clazz.is_type_subclass()) {
544 printf("(%s)", type2python(clazz.superclass_name).c_str());
545 } else {
546 printf("(object)");
548 printf(":\n");
551 /* Tell ctypes about the return type of "fd".
552 * In particular, if "fd" returns a pointer to an isl object,
553 * then tell ctypes it returns a "c_void_p".
554 * If "fd" returns a char *, then simply tell ctypes.
556 * Nothing needs to be done for functions returning
557 * isl_bool, isl_stat or isl_size since they are represented by an int and
558 * ctypes assumes that a function returns int by default.
560 void python_generator::print_restype(FunctionDecl *fd)
562 string fullname = fd->getName();
563 QualType type = fd->getReturnType();
564 if (is_isl_type(type))
565 printf("isl.%s.restype = c_void_p\n", fullname.c_str());
566 else if (is_string(type))
567 printf("isl.%s.restype = POINTER(c_char)\n", fullname.c_str());
570 /* Tell ctypes about the types of the arguments of the function "fd".
572 void python_generator::print_argtypes(FunctionDecl *fd)
574 string fullname = fd->getName();
575 int n = fd->getNumParams();
576 int drop_user = 0;
578 printf("isl.%s.argtypes = [", fullname.c_str());
579 for (int i = 0; i < n - drop_user; ++i) {
580 ParmVarDecl *param = fd->getParamDecl(i);
581 QualType type = param->getOriginalType();
582 if (is_callback(type))
583 drop_user = 1;
584 if (i)
585 printf(", ");
586 if (is_isl_ctx(type))
587 printf("Context");
588 else if (is_isl_type(type) || is_callback(type))
589 printf("c_void_p");
590 else if (is_string(type))
591 printf("c_char_p");
592 else if (is_long(type))
593 printf("c_long");
594 else
595 printf("c_int");
597 if (drop_user)
598 printf(", c_void_p");
599 printf("]\n");
602 /* Print type definitions for the method 'fd'.
604 void python_generator::print_method_type(FunctionDecl *fd)
606 print_restype(fd);
607 print_argtypes(fd);
610 /* If "clazz" has a type function describing subclasses or
611 * if it is one of those type subclasses, then print a __new__ method.
613 * In the superclass, the __new__ method constructs an object
614 * of the subclass type specified by the type function.
615 * In the subclass, the __new__ method reverts to the original behavior.
617 void python_generator::print_new(const isl_class &clazz,
618 const string &python_name)
620 if (!clazz.fn_type && !clazz.is_type_subclass())
621 return;
623 printf(" def __new__(cls, *args, **keywords):\n");
625 if (clazz.fn_type) {
626 map<int, string>::const_iterator i;
628 printf(" if \"ptr\" in keywords:\n");
629 printf(" type = isl.%s(keywords[\"ptr\"])\n",
630 clazz.fn_type->getNameAsString().c_str());
632 for (i = clazz.type_subclasses.begin();
633 i != clazz.type_subclasses.end(); ++i) {
634 printf(" if type == %d:\n", i->first);
635 printf(" return %s(**keywords)\n",
636 type2python(i->second).c_str());
638 printf(" raise\n");
641 printf(" return super(%s, cls).__new__(cls)\n",
642 python_name.c_str());
645 /* Print declarations for methods printing the class representation,
646 * provided there is a corresponding *_to_str function.
648 * In particular, provide an implementation of __str__ and __repr__ methods to
649 * override the default representation used by python. Python uses __str__ to
650 * pretty print the class (e.g., when calling print(obj)) and uses __repr__
651 * when printing a precise representation of an object (e.g., when dumping it
652 * in the REPL console).
654 * Check the type of the argument before calling the *_to_str function
655 * on it in case the method was called on an object from a subclass.
657 * The return value of the *_to_str function is decoded to a python string
658 * assuming an 'ascii' encoding. This is necessary for python 3 compatibility.
660 void python_generator::print_representation(const isl_class &clazz,
661 const string &python_name)
663 if (!clazz.fn_to_str)
664 return;
666 printf(" def __str__(arg0):\n");
667 print_type_check(python_name, 0, false, "", "", -1);
668 printf(" ptr = isl.%s(arg0.ptr)\n",
669 string(clazz.fn_to_str->getName()).c_str());
670 printf(" res = cast(ptr, c_char_p).value.decode('ascii')\n");
671 printf(" libc.free(ptr)\n");
672 printf(" return res\n");
673 printf(" def __repr__(self):\n");
674 printf(" s = str(self)\n");
675 printf(" if '\"' in s:\n");
676 printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
677 python_name.c_str());
678 printf(" else:\n");
679 printf(" return 'isl.%s(\"%%s\")' %% s\n",
680 python_name.c_str());
683 /* Print code to set method type signatures.
685 * To be able to call C functions it is necessary to explicitly set their
686 * argument and result types. Do this for all exported constructors and
687 * methods, as well as for the *_to_str and the type function, if they exist.
688 * Assuming each exported class has a *_copy and a *_free method,
689 * also unconditionally set the type of such methods.
691 void python_generator::print_method_types(const isl_class &clazz)
693 set<FunctionDecl *>::const_iterator in;
694 map<string, set<FunctionDecl *> >::const_iterator it;
696 for (in = clazz.constructors.begin(); in != clazz.constructors.end();
697 ++in)
698 print_method_type(*in);
700 for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it)
701 for (in = it->second.begin(); in != it->second.end(); ++in)
702 print_method_type(*in);
704 print_method_type(clazz.fn_copy);
705 print_method_type(clazz.fn_free);
706 if (clazz.fn_to_str)
707 print_method_type(clazz.fn_to_str);
708 if (clazz.fn_type)
709 print_method_type(clazz.fn_type);
712 /* Print out the definition of this isl_class.
714 * We first check if this isl_class is a subclass of one or more other classes.
715 * If it is, we make sure those superclasses are printed out first.
717 * Then we print a constructor with several cases, one for constructing
718 * a Python object from a return value, one for each function that
719 * was marked as a constructor and for each type based subclass.
721 * Next, we print out some common methods and the methods corresponding
722 * to functions that are not marked as constructors.
724 * Finally, we tell ctypes about the types of the arguments of the
725 * constructor functions and the return types of those function returning
726 * an isl object.
728 void python_generator::print(const isl_class &clazz)
730 string p_name = type2python(clazz.subclass_name);
731 set<FunctionDecl *>::const_iterator in;
732 map<string, set<FunctionDecl *> >::const_iterator it;
733 vector<string> super = find_superclasses(clazz.type);
735 for (unsigned i = 0; i < super.size(); ++i)
736 if (done.find(super[i]) == done.end())
737 print(classes[super[i]]);
738 if (clazz.is_type_subclass() && done.find(clazz.name) == done.end())
739 print(classes[clazz.name]);
740 done.insert(clazz.subclass_name);
742 printf("\n");
743 print_class_header(clazz, p_name, super);
744 printf(" def __init__(self, *args, **keywords):\n");
746 printf(" if \"ptr\" in keywords:\n");
747 printf(" self.ctx = keywords[\"ctx\"]\n");
748 printf(" self.ptr = keywords[\"ptr\"]\n");
749 printf(" return\n");
751 for (in = clazz.constructors.begin(); in != clazz.constructors.end();
752 ++in)
753 print_constructor(clazz, *in);
754 print_upcast_constructors(clazz);
755 printf(" raise Error\n");
756 printf(" def __del__(self):\n");
757 printf(" if hasattr(self, 'ptr'):\n");
758 printf(" isl.%s_free(self.ptr)\n", clazz.name.c_str());
760 print_new(clazz, p_name);
761 print_representation(clazz, p_name);
763 for (it = clazz.methods.begin(); it != clazz.methods.end(); ++it)
764 print_method(clazz, it->first, it->second, super);
766 printf("\n");
768 print_method_types(clazz);
771 /* Generate a python interface based on the extracted types and
772 * functions.
774 * Print out each class in turn. If one of these is a subclass of some
775 * other class, make sure the superclass is printed out first.
776 * functions.
778 void python_generator::generate()
780 map<string, isl_class>::iterator ci;
782 for (ci = classes.begin(); ci != classes.end(); ++ci) {
783 if (done.find(ci->first) == done.end())
784 print(ci->second);