python interface: extract method print_representation
[isl.git] / interface / python.cc
blobc0dad7b1bf5d37261e3260dadca55850fa686b02
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(FunctionDecl *method, vector<string> super);
119 void print_method_overload(FunctionDecl *method, vector<string> super);
120 void print_method(const string &fullname,
121 const set<FunctionDecl *> &methods, vector<string> super);
124 /* Return the class that has a name that matches the initial part
125 * of the name of function "fd" or NULL if no such class could be found.
127 static isl_class *method2class(map<string, isl_class> &classes,
128 FunctionDecl *fd)
130 string best;
131 map<string, isl_class>::iterator ci;
132 string name = fd->getNameAsString();
134 for (ci = classes.begin(); ci != classes.end(); ++ci) {
135 if (name.substr(0, ci->first.length()) == ci->first)
136 best = ci->first;
139 if (classes.find(best) == classes.end()) {
140 cerr << "Unable to find class of " << name << endl;
141 return NULL;
144 return &classes[best];
147 /* Is "type" the type "isl_ctx *"?
149 static bool is_isl_ctx(QualType type)
151 if (!type->isPointerType())
152 return 0;
153 type = type->getPointeeType();
154 if (type.getAsString() != "isl_ctx")
155 return false;
157 return true;
160 /* Is the first argument of "fd" of type "isl_ctx *"?
162 static bool first_arg_is_isl_ctx(FunctionDecl *fd)
164 ParmVarDecl *param;
166 if (fd->getNumParams() < 1)
167 return false;
169 param = fd->getParamDecl(0);
170 return is_isl_ctx(param->getOriginalType());
173 /* Is "type" that of a pointer to an isl_* structure?
175 static bool is_isl_type(QualType type)
177 if (type->isPointerType()) {
178 string s;
180 type = type->getPointeeType();
181 if (type->isFunctionType())
182 return false;
183 s = type.getAsString();
184 return s.substr(0, 4) == "isl_";
187 return false;
190 /* Is "type" the type isl_bool?
192 static bool is_isl_bool(QualType type)
194 string s;
196 if (type->isPointerType())
197 return false;
199 s = type.getAsString();
200 return s == "isl_bool";
203 /* Is "type" that of a pointer to a function?
205 static bool is_callback(QualType type)
207 if (!type->isPointerType())
208 return false;
209 type = type->getPointeeType();
210 return type->isFunctionType();
213 /* Is "type" that of "char *" of "const char *"?
215 static bool is_string(QualType type)
217 if (type->isPointerType()) {
218 string s = type->getPointeeType().getAsString();
219 return s == "const char" || s == "char";
222 return false;
225 /* Return the name of the type that "type" points to.
226 * The input "type" is assumed to be a pointer type.
228 static string extract_type(QualType type)
230 if (type->isPointerType())
231 return type->getPointeeType().getAsString();
232 die("Cannot extract type from non-pointer type");
235 /* Drop the "isl_" initial part of the type name "name".
237 static string type2python(string name)
239 return name.substr(4);
242 /* If "method" is overloaded, then drop the suffix of "name"
243 * corresponding to the type of the final argument and
244 * return the modified name (or the original name if
245 * no modifications were made).
247 static string drop_type_suffix(string name, FunctionDecl *method)
249 int num_params;
250 ParmVarDecl *param;
251 string type;
252 size_t name_len, type_len;
254 if (!is_overload(method))
255 return name;
257 num_params = method->getNumParams();
258 param = method->getParamDecl(num_params - 1);
259 type = extract_type(param->getOriginalType());
260 type = type.substr(4);
261 name_len = name.length();
262 type_len = type.length();
264 if (name_len > type_len && name.substr(name_len - type_len) == type)
265 name = name.substr(0, name_len - type_len - 1);
267 return name;
270 /* Should "method" be considered to be a static method?
271 * That is, is the first argument something other than
272 * an instance of the class?
274 bool isl_class::is_static(FunctionDecl *method)
276 ParmVarDecl *param = method->getParamDecl(0);
277 QualType type = param->getOriginalType();
279 if (!is_isl_type(type))
280 return true;
281 return extract_type(type) != name;
284 /* Print the header of the method "name" with "n_arg" arguments.
285 * If "is_static" is set, then mark the python method as static.
287 * If the method is called "from", then rename it to "convert_from"
288 * because "from" is a python keyword.
290 static void print_method_header(bool is_static, const string &name, int n_arg)
292 const char *s;
294 if (is_static)
295 printf(" @staticmethod\n");
297 s = name.c_str();
298 if (name == "from")
299 s = "convert_from";
301 printf(" def %s(", s);
302 for (int i = 0; i < n_arg; ++i) {
303 if (i)
304 printf(", ");
305 printf("arg%d", i);
307 printf("):\n");
310 /* Construct a wrapper for a callback argument (at position "arg").
311 * Assign the wrapper to "cb". We assume here that a function call
312 * has at most one callback argument.
314 * The wrapper converts the arguments of the callback to python types.
315 * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
316 * and returns -1. Otherwise the wrapper returns 0.
318 static void print_callback(QualType type, int arg)
320 const FunctionProtoType *fn = type->getAs<FunctionProtoType>();
321 unsigned n_arg = fn->getNumArgs();
323 printf(" exc_info = [None]\n");
324 printf(" fn = CFUNCTYPE(c_int");
325 for (unsigned i = 0; i < n_arg - 1; ++i) {
326 if (!is_isl_type(fn->getArgType(i)))
327 die("Argument has non-isl type");
328 printf(", c_void_p");
330 printf(", c_void_p)\n");
331 printf(" def cb_func(");
332 for (unsigned i = 0; i < n_arg; ++i) {
333 if (i)
334 printf(", ");
335 printf("cb_arg%d", i);
337 printf("):\n");
338 for (unsigned i = 0; i < n_arg - 1; ++i) {
339 string arg_type;
340 arg_type = type2python(extract_type(fn->getArgType(i)));
341 printf(" cb_arg%d = %s(ctx=arg0.ctx, "
342 "ptr=cb_arg%d)\n", i, arg_type.c_str(), i);
344 printf(" try:\n");
345 printf(" arg%d(", arg);
346 for (unsigned i = 0; i < n_arg - 1; ++i) {
347 if (i)
348 printf(", ");
349 printf("cb_arg%d", i);
351 printf(")\n");
352 printf(" except:\n");
353 printf(" import sys\n");
354 printf(" exc_info[0] = sys.exc_info()\n");
355 printf(" return -1\n");
356 printf(" return 0\n");
357 printf(" cb = fn(cb_func)\n");
360 /* Print the argument at position "arg" in call to "fd".
361 * "skip" is the number of initial arguments of "fd" that are
362 * skipped in the Python method.
364 * If the argument is a callback, then print a reference to
365 * the callback wrapper "cb".
366 * Otherwise, if the argument is marked as consuming a reference,
367 * then pass a copy of the the pointer stored in the corresponding
368 * argument passed to the Python method.
369 * Otherwise, if the argument is a pointer, then pass this pointer itself.
370 * Otherwise, pass the argument directly.
372 static void print_arg_in_call(FunctionDecl *fd, int arg, int skip)
374 ParmVarDecl *param = fd->getParamDecl(arg);
375 QualType type = param->getOriginalType();
376 if (is_callback(type)) {
377 printf("cb");
378 } else if (takes(param)) {
379 string type_s = extract_type(type);
380 printf("isl.%s_copy(arg%d.ptr)", type_s.c_str(), arg - skip);
381 } else if (type->isPointerType()) {
382 printf("arg%d.ptr", arg - skip);
383 } else {
384 printf("arg%d", arg - skip);
388 /* Print a python method corresponding to the C function "method".
389 * "super" contains the superclasses of the class to which the method belongs.
391 * If the first argument of "method" is something other than an instance
392 * of the class, then mark the python method as static.
393 * If, moreover, this first argument is an isl_ctx, then remove
394 * it from the arguments of the Python method.
396 * If the function has a callback argument, then it also has a "user"
397 * argument. Since Python has closures, there is no need for such
398 * a user argument in the Python interface, so we simply drop it.
399 * We also create a wrapper ("cb") for the callback.
401 * For each argument of the function that refers to an isl structure,
402 * including the object on which the method is called,
403 * we check if the corresponding actual argument is of the right type.
404 * If not, we try to convert it to the right type.
405 * It that doesn't work and if subclass is set, we try to convert self
406 * to the type of the first superclass in "super" and
407 * call the corresponding method.
409 * If the function consumes a reference, then we pass it a copy of
410 * the actual argument.
412 * If the return type is isl_bool, then convert the result to
413 * a Python boolean, raising an error on isl_bool_error.
415 void isl_class::print_method(FunctionDecl *method, vector<string> super)
417 string fullname = method->getName();
418 string cname = fullname.substr(name.length() + 1);
419 int num_params = method->getNumParams();
420 int drop_user = 0;
421 int drop_ctx = first_arg_is_isl_ctx(method);
423 for (int i = 1; i < num_params; ++i) {
424 ParmVarDecl *param = method->getParamDecl(i);
425 QualType type = param->getOriginalType();
426 if (is_callback(type))
427 drop_user = 1;
430 print_method_header(is_static(method), cname,
431 num_params - drop_ctx - drop_user);
433 for (int i = drop_ctx; i < num_params; ++i) {
434 ParmVarDecl *param = method->getParamDecl(i);
435 string type;
436 if (!is_isl_type(param->getOriginalType()))
437 continue;
438 type = type2python(extract_type(param->getOriginalType()));
439 printf(" try:\n");
440 printf(" if not arg%d.__class__ is %s:\n",
441 i - drop_ctx, type.c_str());
442 printf(" arg%d = %s(arg%d)\n",
443 i - drop_ctx, type.c_str(), i - drop_ctx);
444 printf(" except:\n");
445 if (!drop_ctx && i > 0 && super.size() > 0) {
446 printf(" return %s(arg0).%s(",
447 type2python(super[0]).c_str(), cname.c_str());
448 for (int i = 1; i < num_params - drop_user; ++i) {
449 if (i != 1)
450 printf(", ");
451 printf("arg%d", i);
453 printf(")\n");
454 } else
455 printf(" raise\n");
457 for (int i = 1; i < num_params; ++i) {
458 ParmVarDecl *param = method->getParamDecl(i);
459 QualType type = param->getOriginalType();
460 if (!is_callback(type))
461 continue;
462 print_callback(type->getPointeeType(), i - drop_ctx);
464 if (drop_ctx)
465 printf(" ctx = Context.getDefaultInstance()\n");
466 else
467 printf(" ctx = arg0.ctx\n");
468 printf(" res = isl.%s(", fullname.c_str());
469 if (drop_ctx)
470 printf("ctx");
471 else
472 print_arg_in_call(method, 0, 0);
473 for (int i = 1; i < num_params - drop_user; ++i) {
474 printf(", ");
475 print_arg_in_call(method, i, drop_ctx);
477 if (drop_user)
478 printf(", None");
479 printf(")\n");
481 if (is_isl_type(method->getReturnType())) {
482 string type;
483 type = type2python(extract_type(method->getReturnType()));
484 printf(" return %s(ctx=ctx, ptr=res)\n",
485 type.c_str());
486 } else {
487 if (drop_user) {
488 printf(" if exc_info[0] != None:\n");
489 printf(" raise exc_info[0][0], "
490 "exc_info[0][1], exc_info[0][2]\n");
492 if (is_isl_bool(method->getReturnType())) {
493 printf(" if res < 0:\n");
494 printf(" raise\n");
495 printf(" return bool(res)\n");
496 } else {
497 printf(" return res\n");
502 /* Print part of an overloaded python method corresponding to the C function
503 * "method".
504 * "super" contains the superclasses of the class to which the method belongs.
506 * In particular, print code to test whether the arguments passed to
507 * the python method correspond to the arguments expected by "method"
508 * and to call "method" if they do.
510 void isl_class::print_method_overload(FunctionDecl *method,
511 vector<string> super)
513 string fullname = method->getName();
514 int num_params = method->getNumParams();
515 int first;
516 string type;
518 first = is_static(method) ? 0 : 1;
520 printf(" if ");
521 for (int i = first; i < num_params; ++i) {
522 if (i > first)
523 printf(" and ");
524 ParmVarDecl *param = method->getParamDecl(i);
525 if (is_isl_type(param->getOriginalType())) {
526 string type;
527 type = extract_type(param->getOriginalType());
528 type = type2python(type);
529 printf("arg%d.__class__ is %s", i, type.c_str());
530 } else
531 printf("type(arg%d) == str", i);
533 printf(":\n");
534 printf(" res = isl.%s(", fullname.c_str());
535 print_arg_in_call(method, 0, 0);
536 for (int i = 1; i < num_params; ++i) {
537 printf(", ");
538 print_arg_in_call(method, i, 0);
540 printf(")\n");
541 type = type2python(extract_type(method->getReturnType()));
542 printf(" return %s(ctx=arg0.ctx, ptr=res)\n", type.c_str());
545 /* Print a python method with a name derived from "fullname"
546 * corresponding to the C functions "methods".
547 * "super" contains the superclasses of the class to which the method belongs.
549 * If "methods" consists of a single element that is not marked overloaded,
550 * the use print_method to print the method.
551 * Otherwise, print an overloaded method with pieces corresponding
552 * to each function in "methods".
554 void isl_class::print_method(const string &fullname,
555 const set<FunctionDecl *> &methods, vector<string> super)
557 string cname;
558 set<FunctionDecl *>::const_iterator it;
559 int num_params;
560 FunctionDecl *any_method;
562 any_method = *methods.begin();
563 if (methods.size() == 1 && !is_overload(any_method)) {
564 print_method(any_method, super);
565 return;
568 cname = fullname.substr(name.length() + 1);
569 num_params = any_method->getNumParams();
571 print_method_header(is_static(any_method), cname, num_params);
573 for (it = methods.begin(); it != methods.end(); ++it)
574 print_method_overload(*it, super);
577 /* Print part of the constructor for this isl_class.
579 * In particular, check if the actual arguments correspond to the
580 * formal arguments of "cons" and if so call "cons" and put the
581 * result in self.ptr and a reference to the default context in self.ctx.
583 * If the function consumes a reference, then we pass it a copy of
584 * the actual argument.
586 void isl_class::print_constructor(FunctionDecl *cons)
588 string fullname = cons->getName();
589 string cname = fullname.substr(name.length() + 1);
590 int num_params = cons->getNumParams();
591 int drop_ctx = first_arg_is_isl_ctx(cons);
593 printf(" if len(args) == %d", num_params - drop_ctx);
594 for (int i = drop_ctx; i < num_params; ++i) {
595 ParmVarDecl *param = cons->getParamDecl(i);
596 QualType type = param->getOriginalType();
597 if (is_isl_type(type)) {
598 string s;
599 s = type2python(extract_type(type));
600 printf(" and args[%d].__class__ is %s",
601 i - drop_ctx, s.c_str());
602 } else if (type->isPointerType()) {
603 printf(" and type(args[%d]) == str", i - drop_ctx);
604 } else {
605 printf(" and type(args[%d]) == int", i - drop_ctx);
608 printf(":\n");
609 printf(" self.ctx = Context.getDefaultInstance()\n");
610 printf(" self.ptr = isl.%s(", fullname.c_str());
611 if (drop_ctx)
612 printf("self.ctx");
613 for (int i = drop_ctx; i < num_params; ++i) {
614 ParmVarDecl *param = cons->getParamDecl(i);
615 if (i)
616 printf(", ");
617 if (is_isl_type(param->getOriginalType())) {
618 if (takes(param)) {
619 string type;
620 type = extract_type(param->getOriginalType());
621 printf("isl.%s_copy(args[%d].ptr)",
622 type.c_str(), i - drop_ctx);
623 } else
624 printf("args[%d].ptr", i - drop_ctx);
625 } else
626 printf("args[%d]", i - drop_ctx);
628 printf(")\n");
629 printf(" return\n");
632 /* Print the header of the class "name" with superclasses "super".
634 static void print_class_header(const string &name, const vector<string> &super)
636 printf("class %s", name.c_str());
637 if (super.size() > 0) {
638 printf("(");
639 for (unsigned i = 0; i < super.size(); ++i) {
640 if (i > 0)
641 printf(", ");
642 printf("%s", type2python(super[i]).c_str());
644 printf(")");
646 printf(":\n");
649 /* Tell ctypes about the return type of "fd".
650 * In particular, if "fd" returns a pointer to an isl object,
651 * then tell ctypes it returns a "c_void_p".
652 * Similarly, if "fd" returns an isl_bool,
653 * then tell ctypes it returns a "c_bool".
655 static void print_restype(FunctionDecl *fd)
657 string fullname = fd->getName();
658 QualType type = fd->getReturnType();
659 if (is_isl_type(type))
660 printf("isl.%s.restype = c_void_p\n", fullname.c_str());
661 else if (is_isl_bool(type))
662 printf("isl.%s.restype = c_bool\n", fullname.c_str());
665 /* Tell ctypes about the types of the arguments of the function "fd".
667 static void print_argtypes(FunctionDecl *fd)
669 string fullname = fd->getName();
670 int n = fd->getNumParams();
671 int drop_user = 0;
673 printf("isl.%s.argtypes = [", fullname.c_str());
674 for (int i = 0; i < n - drop_user; ++i) {
675 ParmVarDecl *param = fd->getParamDecl(i);
676 QualType type = param->getOriginalType();
677 if (is_callback(type))
678 drop_user = 1;
679 if (i)
680 printf(", ");
681 if (is_isl_ctx(type))
682 printf("Context");
683 else if (is_isl_type(type) || is_callback(type))
684 printf("c_void_p");
685 else if (is_string(type))
686 printf("c_char_p");
687 else
688 printf("c_int");
690 if (drop_user)
691 printf(", c_void_p");
692 printf("]\n");
695 /* Print declarations for methods printing the class representation.
697 * In particular, provide an implementation of __str__ and __repr__ methods to
698 * override the default representation used by python. Python uses __str__ to
699 * pretty print the class (e.g., when calling print(obj)) and uses __repr__
700 * when printing a precise representation of an object (e.g., when dumping it
701 * in the REPL console).
703 void isl_class::print_representation(const string &python_name)
705 printf(" def __str__(self):\n");
706 printf(" ptr = isl.%s_to_str(self.ptr)\n", name.c_str());
707 printf(" res = str(cast(ptr, c_char_p).value)\n");
708 printf(" libc.free(ptr)\n");
709 printf(" return res\n");
710 printf(" def __repr__(self):\n");
711 printf(" s = str(self)\n");
712 printf(" if '\"' in s:\n");
713 printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
714 python_name.c_str());
715 printf(" else:\n");
716 printf(" return 'isl.%s(\"%%s\")' %% s\n",
717 python_name.c_str());
720 /* Print out the definition of this isl_class.
722 * We first check if this isl_class is a subclass of one or more other classes.
723 * If it is, we make sure those superclasses are printed out first.
725 * Then we print a constructor with several cases, one for constructing
726 * a Python object from a return value and one for each function that
727 * was marked as a constructor.
729 * Next, we print out some common methods and the methods corresponding
730 * to functions that are not marked as constructors.
732 * Finally, we tell ctypes about the types of the arguments of the
733 * constructor functions and the return types of those function returning
734 * an isl object.
736 void isl_class::print(map<string, isl_class> &classes, set<string> &done)
738 string p_name = type2python(name);
739 set<FunctionDecl *>::iterator in;
740 map<string, set<FunctionDecl *> >::iterator it;
741 vector<string> super = find_superclasses(type);
743 for (unsigned i = 0; i < super.size(); ++i)
744 if (done.find(super[i]) == done.end())
745 classes[super[i]].print(classes, done);
746 done.insert(name);
748 printf("\n");
749 print_class_header(p_name, super);
750 printf(" def __init__(self, *args, **keywords):\n");
752 printf(" if \"ptr\" in keywords:\n");
753 printf(" self.ctx = keywords[\"ctx\"]\n");
754 printf(" self.ptr = keywords[\"ptr\"]\n");
755 printf(" return\n");
757 for (in = constructors.begin(); in != constructors.end(); ++in)
758 print_constructor(*in);
759 printf(" raise Error\n");
760 printf(" def __del__(self):\n");
761 printf(" if hasattr(self, 'ptr'):\n");
762 printf(" isl.%s_free(self.ptr)\n", name.c_str());
764 print_representation(p_name);
766 for (it = methods.begin(); it != methods.end(); ++it)
767 print_method(it->first, it->second, super);
769 printf("\n");
770 for (in = constructors.begin(); in != constructors.end(); ++in) {
771 print_restype(*in);
772 print_argtypes(*in);
774 for (it = methods.begin(); it != methods.end(); ++it)
775 for (in = it->second.begin(); in != it->second.end(); ++in) {
776 print_restype(*in);
777 print_argtypes(*in);
779 printf("isl.%s_free.argtypes = [c_void_p]\n", name.c_str());
780 printf("isl.%s_to_str.argtypes = [c_void_p]\n", name.c_str());
781 printf("isl.%s_to_str.restype = POINTER(c_char)\n", name.c_str());
784 /* Generate a python interface based on the extracted types and functions.
785 * We first collect all functions that belong to a certain type,
786 * separating constructors from regular methods. If there are any
787 * overloaded functions, then they are grouped based on their name
788 * after removing the argument type suffix.
790 * Then we print out each class in turn. If one of these is a subclass
791 * of some other class, it will make sure the superclass is printed out first.
793 void generate_python(set<RecordDecl *> &types, set<FunctionDecl *> functions)
795 map<string, isl_class> classes;
796 map<string, isl_class>::iterator ci;
797 set<string> done;
799 set<RecordDecl *>::iterator it;
800 for (it = types.begin(); it != types.end(); ++it) {
801 RecordDecl *decl = *it;
802 string name = decl->getName();
803 classes[name].name = name;
804 classes[name].type = decl;
807 set<FunctionDecl *>::iterator in;
808 for (in = functions.begin(); in != functions.end(); ++in) {
809 isl_class *c = method2class(classes, *in);
810 if (!c)
811 continue;
812 if (is_constructor(*in)) {
813 c->constructors.insert(*in);
814 } else {
815 FunctionDecl *method = *in;
816 string fullname = method->getName();
817 fullname = drop_type_suffix(fullname, method);
818 c->methods[fullname].insert(method);
822 for (ci = classes.begin(); ci != classes.end(); ++ci) {
823 if (done.find(ci->first) == done.end())
824 ci->second.print(classes, done);