python interface: extract method print_method_types
[isl.git] / interface / python.cc
blobc0d6938acb908d0681f0cf93adcbf73f93aa84ae
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>
40 #include <clang/AST/Attr.h>
41 #include "extract_interface.h"
42 #include "python.h"
44 static void die(const char *msg) __attribute__((noreturn));
46 /* Print error message "msg" and abort.
48 static void die(const char *msg)
50 fprintf(stderr, "%s", msg);
51 abort();
54 /* Return a sequence of the types of which the given type declaration is
55 * marked as being a subtype.
57 static vector<string> find_superclasses(RecordDecl *decl)
59 vector<string> super;
61 if (!decl->hasAttrs())
62 return super;
64 string sub = "isl_subclass";
65 size_t len = sub.length();
66 AttrVec attrs = decl->getAttrs();
67 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
68 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
69 if (!ann)
70 continue;
71 string s = ann->getAnnotation().str();
72 if (s.substr(0, len) == sub) {
73 s = s.substr(len + 1, s.length() - len - 2);
74 super.push_back(s);
78 return super;
81 /* Is decl marked as being part of an overloaded method?
83 static bool is_overload(Decl *decl)
85 return has_annotation(decl, "isl_overload");
88 /* Is decl marked as a constructor?
90 static bool is_constructor(Decl *decl)
92 return has_annotation(decl, "isl_constructor");
95 /* Is decl marked as consuming a reference?
97 static bool takes(Decl *decl)
99 return has_annotation(decl, "isl_take");
102 /* isl_class collects all constructors and methods for an isl "class".
103 * "name" is the name of the class.
104 * "type" is the declaration that introduces the type.
105 * "methods" contains the set of methods, grouped by method name.
107 struct isl_class {
108 string name;
109 RecordDecl *type;
110 set<FunctionDecl *> constructors;
111 map<string, set<FunctionDecl *> > methods;
113 bool is_static(FunctionDecl *method);
115 void print(map<string, isl_class> &classes, set<string> &done);
116 void print_constructor(FunctionDecl *method);
117 void print_representation(const string &python_name);
118 void print_method_types();
119 void print_method(FunctionDecl *method, vector<string> super);
120 void print_method_overload(FunctionDecl *method, vector<string> super);
121 void print_method(const string &fullname,
122 const set<FunctionDecl *> &methods, vector<string> super);
125 /* Return the class that has a name that matches the initial part
126 * of the name of function "fd" or NULL if no such class could be found.
128 static isl_class *method2class(map<string, isl_class> &classes,
129 FunctionDecl *fd)
131 string best;
132 map<string, isl_class>::iterator ci;
133 string name = fd->getNameAsString();
135 for (ci = classes.begin(); ci != classes.end(); ++ci) {
136 if (name.substr(0, ci->first.length()) == ci->first)
137 best = ci->first;
140 if (classes.find(best) == classes.end()) {
141 cerr << "Unable to find class of " << name << endl;
142 return NULL;
145 return &classes[best];
148 /* Is "type" the type "isl_ctx *"?
150 static bool is_isl_ctx(QualType type)
152 if (!type->isPointerType())
153 return 0;
154 type = type->getPointeeType();
155 if (type.getAsString() != "isl_ctx")
156 return false;
158 return true;
161 /* Is the first argument of "fd" of type "isl_ctx *"?
163 static bool first_arg_is_isl_ctx(FunctionDecl *fd)
165 ParmVarDecl *param;
167 if (fd->getNumParams() < 1)
168 return false;
170 param = fd->getParamDecl(0);
171 return is_isl_ctx(param->getOriginalType());
174 /* Is "type" that of a pointer to an isl_* structure?
176 static bool is_isl_type(QualType type)
178 if (type->isPointerType()) {
179 string s;
181 type = type->getPointeeType();
182 if (type->isFunctionType())
183 return false;
184 s = type.getAsString();
185 return s.substr(0, 4) == "isl_";
188 return false;
191 /* Is "type" the type isl_bool?
193 static bool is_isl_bool(QualType type)
195 string s;
197 if (type->isPointerType())
198 return false;
200 s = type.getAsString();
201 return s == "isl_bool";
204 /* Is "type" that of a pointer to a function?
206 static bool is_callback(QualType type)
208 if (!type->isPointerType())
209 return false;
210 type = type->getPointeeType();
211 return type->isFunctionType();
214 /* Is "type" that of "char *" of "const char *"?
216 static bool is_string(QualType type)
218 if (type->isPointerType()) {
219 string s = type->getPointeeType().getAsString();
220 return s == "const char" || s == "char";
223 return false;
226 /* Return the name of the type that "type" points to.
227 * The input "type" is assumed to be a pointer type.
229 static string extract_type(QualType type)
231 if (type->isPointerType())
232 return type->getPointeeType().getAsString();
233 die("Cannot extract type from non-pointer type");
236 /* Drop the "isl_" initial part of the type name "name".
238 static string type2python(string name)
240 return name.substr(4);
243 /* If "method" is overloaded, then drop the suffix of "name"
244 * corresponding to the type of the final argument and
245 * return the modified name (or the original name if
246 * no modifications were made).
248 static string drop_type_suffix(string name, FunctionDecl *method)
250 int num_params;
251 ParmVarDecl *param;
252 string type;
253 size_t name_len, type_len;
255 if (!is_overload(method))
256 return name;
258 num_params = method->getNumParams();
259 param = method->getParamDecl(num_params - 1);
260 type = extract_type(param->getOriginalType());
261 type = type.substr(4);
262 name_len = name.length();
263 type_len = type.length();
265 if (name_len > type_len && name.substr(name_len - type_len) == type)
266 name = name.substr(0, name_len - type_len - 1);
268 return name;
271 /* Should "method" be considered to be a static method?
272 * That is, is the first argument something other than
273 * an instance of the class?
275 bool isl_class::is_static(FunctionDecl *method)
277 ParmVarDecl *param = method->getParamDecl(0);
278 QualType type = param->getOriginalType();
280 if (!is_isl_type(type))
281 return true;
282 return extract_type(type) != name;
285 /* Print the header of the method "name" with "n_arg" arguments.
286 * If "is_static" is set, then mark the python method as static.
288 * If the method is called "from", then rename it to "convert_from"
289 * because "from" is a python keyword.
291 static void print_method_header(bool is_static, const string &name, int n_arg)
293 const char *s;
295 if (is_static)
296 printf(" @staticmethod\n");
298 s = name.c_str();
299 if (name == "from")
300 s = "convert_from";
302 printf(" def %s(", s);
303 for (int i = 0; i < n_arg; ++i) {
304 if (i)
305 printf(", ");
306 printf("arg%d", i);
308 printf("):\n");
311 /* Construct a wrapper for a callback argument (at position "arg").
312 * Assign the wrapper to "cb". We assume here that a function call
313 * has at most one callback argument.
315 * The wrapper converts the arguments of the callback to python types.
316 * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
317 * and returns -1. Otherwise the wrapper returns 0.
319 static void print_callback(QualType type, int arg)
321 const FunctionProtoType *fn = type->getAs<FunctionProtoType>();
322 unsigned n_arg = fn->getNumArgs();
324 printf(" exc_info = [None]\n");
325 printf(" fn = CFUNCTYPE(c_int");
326 for (unsigned i = 0; i < n_arg - 1; ++i) {
327 if (!is_isl_type(fn->getArgType(i)))
328 die("Argument has non-isl type");
329 printf(", c_void_p");
331 printf(", c_void_p)\n");
332 printf(" def cb_func(");
333 for (unsigned i = 0; i < n_arg; ++i) {
334 if (i)
335 printf(", ");
336 printf("cb_arg%d", i);
338 printf("):\n");
339 for (unsigned i = 0; i < n_arg - 1; ++i) {
340 string arg_type;
341 arg_type = type2python(extract_type(fn->getArgType(i)));
342 printf(" cb_arg%d = %s(ctx=arg0.ctx, "
343 "ptr=cb_arg%d)\n", i, arg_type.c_str(), i);
345 printf(" try:\n");
346 printf(" arg%d(", arg);
347 for (unsigned i = 0; i < n_arg - 1; ++i) {
348 if (i)
349 printf(", ");
350 printf("cb_arg%d", i);
352 printf(")\n");
353 printf(" except:\n");
354 printf(" import sys\n");
355 printf(" exc_info[0] = sys.exc_info()\n");
356 printf(" return -1\n");
357 printf(" return 0\n");
358 printf(" cb = fn(cb_func)\n");
361 /* Print the argument at position "arg" in call to "fd".
362 * "skip" is the number of initial arguments of "fd" that are
363 * skipped in the Python method.
365 * If the argument is a callback, then print a reference to
366 * the callback wrapper "cb".
367 * Otherwise, if the argument is marked as consuming a reference,
368 * then pass a copy of the the pointer stored in the corresponding
369 * argument passed to the Python method.
370 * Otherwise, if the argument is a pointer, then pass this pointer itself.
371 * Otherwise, pass the argument directly.
373 static void print_arg_in_call(FunctionDecl *fd, int arg, int skip)
375 ParmVarDecl *param = fd->getParamDecl(arg);
376 QualType type = param->getOriginalType();
377 if (is_callback(type)) {
378 printf("cb");
379 } else if (takes(param)) {
380 string type_s = extract_type(type);
381 printf("isl.%s_copy(arg%d.ptr)", type_s.c_str(), arg - skip);
382 } else if (type->isPointerType()) {
383 printf("arg%d.ptr", arg - skip);
384 } else {
385 printf("arg%d", arg - skip);
389 /* Print a python method corresponding to the C function "method".
390 * "super" contains the superclasses of the class to which the method belongs.
392 * If the first argument of "method" is something other than an instance
393 * of the class, then mark the python method as static.
394 * If, moreover, this first argument is an isl_ctx, then remove
395 * it from the arguments of the Python method.
397 * If the function has a callback argument, then it also has a "user"
398 * argument. Since Python has closures, there is no need for such
399 * a user argument in the Python interface, so we simply drop it.
400 * We also create a wrapper ("cb") for the callback.
402 * For each argument of the function that refers to an isl structure,
403 * including the object on which the method is called,
404 * we check if the corresponding actual argument is of the right type.
405 * If not, we try to convert it to the right type.
406 * It that doesn't work and if subclass is set, we try to convert self
407 * to the type of the first superclass in "super" and
408 * call the corresponding method.
410 * If the function consumes a reference, then we pass it a copy of
411 * the actual argument.
413 * If the return type is isl_bool, then convert the result to
414 * a Python boolean, raising an error on isl_bool_error.
416 void isl_class::print_method(FunctionDecl *method, vector<string> super)
418 string fullname = method->getName();
419 string cname = fullname.substr(name.length() + 1);
420 int num_params = method->getNumParams();
421 int drop_user = 0;
422 int drop_ctx = first_arg_is_isl_ctx(method);
424 for (int i = 1; i < num_params; ++i) {
425 ParmVarDecl *param = method->getParamDecl(i);
426 QualType type = param->getOriginalType();
427 if (is_callback(type))
428 drop_user = 1;
431 print_method_header(is_static(method), cname,
432 num_params - drop_ctx - drop_user);
434 for (int i = drop_ctx; i < num_params; ++i) {
435 ParmVarDecl *param = method->getParamDecl(i);
436 string type;
437 if (!is_isl_type(param->getOriginalType()))
438 continue;
439 type = type2python(extract_type(param->getOriginalType()));
440 printf(" try:\n");
441 printf(" if not arg%d.__class__ is %s:\n",
442 i - drop_ctx, type.c_str());
443 printf(" arg%d = %s(arg%d)\n",
444 i - drop_ctx, type.c_str(), i - drop_ctx);
445 printf(" except:\n");
446 if (!drop_ctx && i > 0 && super.size() > 0) {
447 printf(" return %s(arg0).%s(",
448 type2python(super[0]).c_str(), cname.c_str());
449 for (int i = 1; i < num_params - drop_user; ++i) {
450 if (i != 1)
451 printf(", ");
452 printf("arg%d", i);
454 printf(")\n");
455 } else
456 printf(" raise\n");
458 for (int i = 1; i < num_params; ++i) {
459 ParmVarDecl *param = method->getParamDecl(i);
460 QualType type = param->getOriginalType();
461 if (!is_callback(type))
462 continue;
463 print_callback(type->getPointeeType(), i - drop_ctx);
465 if (drop_ctx)
466 printf(" ctx = Context.getDefaultInstance()\n");
467 else
468 printf(" ctx = arg0.ctx\n");
469 printf(" res = isl.%s(", fullname.c_str());
470 if (drop_ctx)
471 printf("ctx");
472 else
473 print_arg_in_call(method, 0, 0);
474 for (int i = 1; i < num_params - drop_user; ++i) {
475 printf(", ");
476 print_arg_in_call(method, i, drop_ctx);
478 if (drop_user)
479 printf(", None");
480 printf(")\n");
482 if (is_isl_type(method->getReturnType())) {
483 string type;
484 type = type2python(extract_type(method->getReturnType()));
485 printf(" return %s(ctx=ctx, ptr=res)\n",
486 type.c_str());
487 } else {
488 if (drop_user) {
489 printf(" if exc_info[0] != None:\n");
490 printf(" raise exc_info[0][0], "
491 "exc_info[0][1], exc_info[0][2]\n");
493 if (is_isl_bool(method->getReturnType())) {
494 printf(" if res < 0:\n");
495 printf(" raise\n");
496 printf(" return bool(res)\n");
497 } else {
498 printf(" return res\n");
503 /* Print part of an overloaded python method corresponding to the C function
504 * "method".
505 * "super" contains the superclasses of the class to which the method belongs.
507 * In particular, print code to test whether the arguments passed to
508 * the python method correspond to the arguments expected by "method"
509 * and to call "method" if they do.
511 void isl_class::print_method_overload(FunctionDecl *method,
512 vector<string> super)
514 string fullname = method->getName();
515 int num_params = method->getNumParams();
516 int first;
517 string type;
519 first = is_static(method) ? 0 : 1;
521 printf(" if ");
522 for (int i = first; i < num_params; ++i) {
523 if (i > first)
524 printf(" and ");
525 ParmVarDecl *param = method->getParamDecl(i);
526 if (is_isl_type(param->getOriginalType())) {
527 string type;
528 type = extract_type(param->getOriginalType());
529 type = type2python(type);
530 printf("arg%d.__class__ is %s", i, type.c_str());
531 } else
532 printf("type(arg%d) == str", i);
534 printf(":\n");
535 printf(" res = isl.%s(", fullname.c_str());
536 print_arg_in_call(method, 0, 0);
537 for (int i = 1; i < num_params; ++i) {
538 printf(", ");
539 print_arg_in_call(method, i, 0);
541 printf(")\n");
542 type = type2python(extract_type(method->getReturnType()));
543 printf(" return %s(ctx=arg0.ctx, ptr=res)\n", type.c_str());
546 /* Print a python method with a name derived from "fullname"
547 * corresponding to the C functions "methods".
548 * "super" contains the superclasses of the class to which the method belongs.
550 * If "methods" consists of a single element that is not marked overloaded,
551 * the use print_method to print the method.
552 * Otherwise, print an overloaded method with pieces corresponding
553 * to each function in "methods".
555 void isl_class::print_method(const string &fullname,
556 const set<FunctionDecl *> &methods, vector<string> super)
558 string cname;
559 set<FunctionDecl *>::const_iterator it;
560 int num_params;
561 FunctionDecl *any_method;
563 any_method = *methods.begin();
564 if (methods.size() == 1 && !is_overload(any_method)) {
565 print_method(any_method, super);
566 return;
569 cname = fullname.substr(name.length() + 1);
570 num_params = any_method->getNumParams();
572 print_method_header(is_static(any_method), cname, num_params);
574 for (it = methods.begin(); it != methods.end(); ++it)
575 print_method_overload(*it, super);
578 /* Print part of the constructor for this isl_class.
580 * In particular, check if the actual arguments correspond to the
581 * formal arguments of "cons" and if so call "cons" and put the
582 * result in self.ptr and a reference to the default context in self.ctx.
584 * If the function consumes a reference, then we pass it a copy of
585 * the actual argument.
587 void isl_class::print_constructor(FunctionDecl *cons)
589 string fullname = cons->getName();
590 string cname = fullname.substr(name.length() + 1);
591 int num_params = cons->getNumParams();
592 int drop_ctx = first_arg_is_isl_ctx(cons);
594 printf(" if len(args) == %d", num_params - drop_ctx);
595 for (int i = drop_ctx; i < num_params; ++i) {
596 ParmVarDecl *param = cons->getParamDecl(i);
597 QualType type = param->getOriginalType();
598 if (is_isl_type(type)) {
599 string s;
600 s = type2python(extract_type(type));
601 printf(" and args[%d].__class__ is %s",
602 i - drop_ctx, s.c_str());
603 } else if (type->isPointerType()) {
604 printf(" and type(args[%d]) == str", i - drop_ctx);
605 } else {
606 printf(" and type(args[%d]) == int", i - drop_ctx);
609 printf(":\n");
610 printf(" self.ctx = Context.getDefaultInstance()\n");
611 printf(" self.ptr = isl.%s(", fullname.c_str());
612 if (drop_ctx)
613 printf("self.ctx");
614 for (int i = drop_ctx; i < num_params; ++i) {
615 ParmVarDecl *param = cons->getParamDecl(i);
616 if (i)
617 printf(", ");
618 if (is_isl_type(param->getOriginalType())) {
619 if (takes(param)) {
620 string type;
621 type = extract_type(param->getOriginalType());
622 printf("isl.%s_copy(args[%d].ptr)",
623 type.c_str(), i - drop_ctx);
624 } else
625 printf("args[%d].ptr", i - drop_ctx);
626 } else
627 printf("args[%d]", i - drop_ctx);
629 printf(")\n");
630 printf(" return\n");
633 /* Print the header of the class "name" with superclasses "super".
635 static void print_class_header(const string &name, const vector<string> &super)
637 printf("class %s", name.c_str());
638 if (super.size() > 0) {
639 printf("(");
640 for (unsigned i = 0; i < super.size(); ++i) {
641 if (i > 0)
642 printf(", ");
643 printf("%s", type2python(super[i]).c_str());
645 printf(")");
647 printf(":\n");
650 /* Tell ctypes about the return type of "fd".
651 * In particular, if "fd" returns a pointer to an isl object,
652 * then tell ctypes it returns a "c_void_p".
653 * Similarly, if "fd" returns an isl_bool,
654 * then tell ctypes it returns a "c_bool".
656 static void print_restype(FunctionDecl *fd)
658 string fullname = fd->getName();
659 QualType type = fd->getReturnType();
660 if (is_isl_type(type))
661 printf("isl.%s.restype = c_void_p\n", fullname.c_str());
662 else if (is_isl_bool(type))
663 printf("isl.%s.restype = c_bool\n", fullname.c_str());
666 /* Tell ctypes about the types of the arguments of the function "fd".
668 static void print_argtypes(FunctionDecl *fd)
670 string fullname = fd->getName();
671 int n = fd->getNumParams();
672 int drop_user = 0;
674 printf("isl.%s.argtypes = [", fullname.c_str());
675 for (int i = 0; i < n - drop_user; ++i) {
676 ParmVarDecl *param = fd->getParamDecl(i);
677 QualType type = param->getOriginalType();
678 if (is_callback(type))
679 drop_user = 1;
680 if (i)
681 printf(", ");
682 if (is_isl_ctx(type))
683 printf("Context");
684 else if (is_isl_type(type) || is_callback(type))
685 printf("c_void_p");
686 else if (is_string(type))
687 printf("c_char_p");
688 else
689 printf("c_int");
691 if (drop_user)
692 printf(", c_void_p");
693 printf("]\n");
696 /* Print declarations for methods printing the class representation.
698 * In particular, provide an implementation of __str__ and __repr__ methods to
699 * override the default representation used by python. Python uses __str__ to
700 * pretty print the class (e.g., when calling print(obj)) and uses __repr__
701 * when printing a precise representation of an object (e.g., when dumping it
702 * in the REPL console).
704 void isl_class::print_representation(const string &python_name)
706 printf(" def __str__(self):\n");
707 printf(" ptr = isl.%s_to_str(self.ptr)\n", name.c_str());
708 printf(" res = str(cast(ptr, c_char_p).value)\n");
709 printf(" libc.free(ptr)\n");
710 printf(" return res\n");
711 printf(" def __repr__(self):\n");
712 printf(" s = str(self)\n");
713 printf(" if '\"' in s:\n");
714 printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
715 python_name.c_str());
716 printf(" else:\n");
717 printf(" return 'isl.%s(\"%%s\")' %% s\n",
718 python_name.c_str());
721 /* Print code to set method type signatures.
723 * To be able to call C functions it is necessary to explicitly set their
724 * argument and result types. Do this for all exported constructors and
725 * methods. Assuming each exported class has *_to_str and *_free methods,
726 * also unconditionally set types for these methods.
728 void isl_class::print_method_types()
730 set<FunctionDecl *>::iterator in;
731 map<string, set<FunctionDecl *> >::iterator it;
733 for (in = constructors.begin(); in != constructors.end(); ++in) {
734 print_restype(*in);
735 print_argtypes(*in);
737 for (it = methods.begin(); it != methods.end(); ++it)
738 for (in = it->second.begin(); in != it->second.end(); ++in) {
739 print_restype(*in);
740 print_argtypes(*in);
742 printf("isl.%s_free.argtypes = [c_void_p]\n", name.c_str());
743 printf("isl.%s_to_str.argtypes = [c_void_p]\n", name.c_str());
744 printf("isl.%s_to_str.restype = POINTER(c_char)\n", name.c_str());
747 /* Print out the definition of this isl_class.
749 * We first check if this isl_class is a subclass of one or more other classes.
750 * If it is, we make sure those superclasses are printed out first.
752 * Then we print a constructor with several cases, one for constructing
753 * a Python object from a return value and one for each function that
754 * was marked as a constructor.
756 * Next, we print out some common methods and the methods corresponding
757 * to functions that are not marked as constructors.
759 * Finally, we tell ctypes about the types of the arguments of the
760 * constructor functions and the return types of those function returning
761 * an isl object.
763 void isl_class::print(map<string, isl_class> &classes, set<string> &done)
765 string p_name = type2python(name);
766 set<FunctionDecl *>::iterator in;
767 map<string, set<FunctionDecl *> >::iterator it;
768 vector<string> super = find_superclasses(type);
770 for (unsigned i = 0; i < super.size(); ++i)
771 if (done.find(super[i]) == done.end())
772 classes[super[i]].print(classes, done);
773 done.insert(name);
775 printf("\n");
776 print_class_header(p_name, super);
777 printf(" def __init__(self, *args, **keywords):\n");
779 printf(" if \"ptr\" in keywords:\n");
780 printf(" self.ctx = keywords[\"ctx\"]\n");
781 printf(" self.ptr = keywords[\"ptr\"]\n");
782 printf(" return\n");
784 for (in = constructors.begin(); in != constructors.end(); ++in)
785 print_constructor(*in);
786 printf(" raise Error\n");
787 printf(" def __del__(self):\n");
788 printf(" if hasattr(self, 'ptr'):\n");
789 printf(" isl.%s_free(self.ptr)\n", name.c_str());
791 print_representation(p_name);
793 for (it = methods.begin(); it != methods.end(); ++it)
794 print_method(it->first, it->second, super);
796 printf("\n");
798 print_method_types();
801 /* Generate a python interface based on the extracted types and functions.
802 * We first collect all functions that belong to a certain type,
803 * separating constructors from regular methods. If there are any
804 * overloaded functions, then they are grouped based on their name
805 * after removing the argument type suffix.
807 * Then we print out each class in turn. If one of these is a subclass
808 * of some other class, it will make sure the superclass is printed out first.
810 void generate_python(set<RecordDecl *> &types, set<FunctionDecl *> functions)
812 map<string, isl_class> classes;
813 map<string, isl_class>::iterator ci;
814 set<string> done;
816 set<RecordDecl *>::iterator it;
817 for (it = types.begin(); it != types.end(); ++it) {
818 RecordDecl *decl = *it;
819 string name = decl->getName();
820 classes[name].name = name;
821 classes[name].type = decl;
824 set<FunctionDecl *>::iterator in;
825 for (in = functions.begin(); in != functions.end(); ++in) {
826 isl_class *c = method2class(classes, *in);
827 if (!c)
828 continue;
829 if (is_constructor(*in)) {
830 c->constructors.insert(*in);
831 } else {
832 FunctionDecl *method = *in;
833 string fullname = method->getName();
834 fullname = drop_type_suffix(fullname, method);
835 c->methods[fullname].insert(method);
839 for (ci = classes.begin(); ci != classes.end(); ++ci) {
840 if (done.find(ci->first) == done.end())
841 ci->second.print(classes, done);