isl 0.16
[isl.git] / interface / python.cc
bloba3b48286d6e62a819a26104e6ed6ebd6ec05e706
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 /* Return a sequence of the types of which the given type declaration is
45 * marked as being a subtype.
47 static vector<string> find_superclasses(RecordDecl *decl)
49 vector<string> super;
51 if (!decl->hasAttrs())
52 return super;
54 string sub = "isl_subclass";
55 size_t len = sub.length();
56 AttrVec attrs = decl->getAttrs();
57 for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
58 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
59 if (!ann)
60 continue;
61 string s = ann->getAnnotation().str();
62 if (s.substr(0, len) == sub) {
63 s = s.substr(len + 1, s.length() - len - 2);
64 super.push_back(s);
68 return super;
71 /* Is decl marked as being part of an overloaded method?
73 static bool is_overload(Decl *decl)
75 return has_annotation(decl, "isl_overload");
78 /* Is decl marked as a constructor?
80 static bool is_constructor(Decl *decl)
82 return has_annotation(decl, "isl_constructor");
85 /* Is decl marked as consuming a reference?
87 static bool takes(Decl *decl)
89 return has_annotation(decl, "isl_take");
92 /* isl_class collects all constructors and methods for an isl "class".
93 * "name" is the name of the class.
94 * "type" is the declaration that introduces the type.
95 * "methods" contains the set of methods, grouped by method name.
97 struct isl_class {
98 string name;
99 RecordDecl *type;
100 set<FunctionDecl *> constructors;
101 map<string, set<FunctionDecl *> > methods;
103 bool is_static(FunctionDecl *method);
105 void print(map<string, isl_class> &classes, set<string> &done);
106 void print_constructor(FunctionDecl *method);
107 void print_method(FunctionDecl *method, vector<string> super);
108 void print_method_overload(FunctionDecl *method, vector<string> super);
109 void print_method(const string &fullname,
110 const set<FunctionDecl *> &methods, vector<string> super);
113 /* Return the class that has a name that matches the initial part
114 * of the name of function "fd" or NULL if no such class could be found.
116 static isl_class *method2class(map<string, isl_class> &classes,
117 FunctionDecl *fd)
119 string best;
120 map<string, isl_class>::iterator ci;
121 string name = fd->getNameAsString();
123 for (ci = classes.begin(); ci != classes.end(); ++ci) {
124 if (name.substr(0, ci->first.length()) == ci->first)
125 best = ci->first;
128 if (classes.find(best) == classes.end()) {
129 cerr << "Unable to find class of " << name << endl;
130 return NULL;
133 return &classes[best];
136 /* Is "type" the type "isl_ctx *"?
138 static bool is_isl_ctx(QualType type)
140 if (!type->isPointerType())
141 return 0;
142 type = type->getPointeeType();
143 if (type.getAsString() != "isl_ctx")
144 return false;
146 return true;
149 /* Is the first argument of "fd" of type "isl_ctx *"?
151 static bool first_arg_is_isl_ctx(FunctionDecl *fd)
153 ParmVarDecl *param;
155 if (fd->getNumParams() < 1)
156 return false;
158 param = fd->getParamDecl(0);
159 return is_isl_ctx(param->getOriginalType());
162 /* Is "type" that of a pointer to an isl_* structure?
164 static bool is_isl_type(QualType type)
166 if (type->isPointerType()) {
167 string s;
169 type = type->getPointeeType();
170 if (type->isFunctionType())
171 return false;
172 s = type.getAsString();
173 return s.substr(0, 4) == "isl_";
176 return false;
179 /* Is "type" the type isl_bool?
181 static bool is_isl_bool(QualType type)
183 string s;
185 if (type->isPointerType())
186 return false;
188 s = type.getAsString();
189 return s == "isl_bool";
192 /* Is "type" that of a pointer to a function?
194 static bool is_callback(QualType type)
196 if (!type->isPointerType())
197 return false;
198 type = type->getPointeeType();
199 return type->isFunctionType();
202 /* Is "type" that of "char *" of "const char *"?
204 static bool is_string(QualType type)
206 if (type->isPointerType()) {
207 string s = type->getPointeeType().getAsString();
208 return s == "const char" || s == "char";
211 return false;
214 /* Return the name of the type that "type" points to.
215 * The input "type" is assumed to be a pointer type.
217 static string extract_type(QualType type)
219 if (type->isPointerType())
220 return type->getPointeeType().getAsString();
221 assert(0);
224 /* Drop the "isl_" initial part of the type name "name".
226 static string type2python(string name)
228 return name.substr(4);
231 /* If "method" is overloaded, then drop the suffix of "name"
232 * corresponding to the type of the final argument and
233 * return the modified name (or the original name if
234 * no modifications were made).
236 static string drop_type_suffix(string name, FunctionDecl *method)
238 int num_params;
239 ParmVarDecl *param;
240 string type;
241 size_t name_len, type_len;
243 if (!is_overload(method))
244 return name;
246 num_params = method->getNumParams();
247 param = method->getParamDecl(num_params - 1);
248 type = extract_type(param->getOriginalType());
249 type = type.substr(4);
250 name_len = name.length();
251 type_len = type.length();
253 if (name_len > type_len && name.substr(name_len - type_len) == type)
254 name = name.substr(0, name_len - type_len - 1);
256 return name;
259 /* Should "method" be considered to be a static method?
260 * That is, is the first argument something other than
261 * an instance of the class?
263 bool isl_class::is_static(FunctionDecl *method)
265 ParmVarDecl *param = method->getParamDecl(0);
266 QualType type = param->getOriginalType();
268 if (!is_isl_type(type))
269 return true;
270 return extract_type(type) != name;
273 /* Print the header of the method "name" with "n_arg" arguments.
274 * If "is_static" is set, then mark the python method as static.
276 * If the method is called "from", then rename it to "convert_from"
277 * because "from" is a python keyword.
279 static void print_method_header(bool is_static, const string &name, int n_arg)
281 const char *s;
283 if (is_static)
284 printf(" @staticmethod\n");
286 s = name.c_str();
287 if (name == "from")
288 s = "convert_from";
290 printf(" def %s(", s);
291 for (int i = 0; i < n_arg; ++i) {
292 if (i)
293 printf(", ");
294 printf("arg%d", i);
296 printf("):\n");
299 /* Construct a wrapper for a callback argument (at position "arg").
300 * Assign the wrapper to "cb". We assume here that a function call
301 * has at most one callback argument.
303 * The wrapper converts the arguments of the callback to python types.
304 * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
305 * and returns -1. Otherwise the wrapper returns 0.
307 static void print_callback(QualType type, int arg)
309 const FunctionProtoType *fn = type->getAs<FunctionProtoType>();
310 unsigned n_arg = fn->getNumArgs();
312 printf(" exc_info = [None]\n");
313 printf(" fn = CFUNCTYPE(c_int");
314 for (int i = 0; i < n_arg - 1; ++i) {
315 QualType arg_type = fn->getArgType(i);
316 assert(is_isl_type(arg_type));
317 printf(", c_void_p");
319 printf(", c_void_p)\n");
320 printf(" def cb_func(");
321 for (int i = 0; i < n_arg; ++i) {
322 if (i)
323 printf(", ");
324 printf("cb_arg%d", i);
326 printf("):\n");
327 for (int i = 0; i < n_arg - 1; ++i) {
328 string arg_type;
329 arg_type = type2python(extract_type(fn->getArgType(i)));
330 printf(" cb_arg%d = %s(ctx=arg0.ctx, "
331 "ptr=cb_arg%d)\n", i, arg_type.c_str(), i);
333 printf(" try:\n");
334 printf(" arg%d(", arg);
335 for (int i = 0; i < n_arg - 1; ++i) {
336 if (i)
337 printf(", ");
338 printf("cb_arg%d", i);
340 printf(")\n");
341 printf(" except:\n");
342 printf(" import sys\n");
343 printf(" exc_info[0] = sys.exc_info()\n");
344 printf(" return -1\n");
345 printf(" return 0\n");
346 printf(" cb = fn(cb_func)\n");
349 /* Print the argument at position "arg" in call to "fd".
350 * "skip" is the number of initial arguments of "fd" that are
351 * skipped in the Python method.
353 * If the argument is a callback, then print a reference to
354 * the callback wrapper "cb".
355 * Otherwise, if the argument is marked as consuming a reference,
356 * then pass a copy of the the pointer stored in the corresponding
357 * argument passed to the Python method.
358 * Otherwise, if the argument is a pointer, then pass this pointer itself.
359 * Otherwise, pass the argument directly.
361 static void print_arg_in_call(FunctionDecl *fd, int arg, int skip)
363 ParmVarDecl *param = fd->getParamDecl(arg);
364 QualType type = param->getOriginalType();
365 if (is_callback(type)) {
366 printf("cb");
367 } else if (takes(param)) {
368 string type_s = extract_type(type);
369 printf("isl.%s_copy(arg%d.ptr)", type_s.c_str(), arg - skip);
370 } else if (type->isPointerType()) {
371 printf("arg%d.ptr", arg - skip);
372 } else {
373 printf("arg%d", arg - skip);
377 /* Print a python method corresponding to the C function "method".
378 * "super" contains the superclasses of the class to which the method belongs.
380 * If the first argument of "method" is something other than an instance
381 * of the class, then mark the python method as static.
382 * If, moreover, this first argument is an isl_ctx, then remove
383 * it from the arguments of the Python method.
385 * If the function has a callback argument, then it also has a "user"
386 * argument. Since Python has closures, there is no need for such
387 * a user argument in the Python interface, so we simply drop it.
388 * We also create a wrapper ("cb") for the callback.
390 * For each argument of the function that refers to an isl structure,
391 * including the object on which the method is called,
392 * we check if the corresponding actual argument is of the right type.
393 * If not, we try to convert it to the right type.
394 * It that doesn't work and if subclass is set, we try to convert self
395 * to the type of the first superclass in "super" and
396 * call the corresponding method.
398 * If the function consumes a reference, then we pass it a copy of
399 * the actual argument.
401 * If the return type is isl_bool, then convert the result to
402 * a Python boolean, raising an error on isl_bool_error.
404 void isl_class::print_method(FunctionDecl *method, vector<string> super)
406 string fullname = method->getName();
407 string cname = fullname.substr(name.length() + 1);
408 int num_params = method->getNumParams();
409 int drop_user = 0;
410 int drop_ctx = first_arg_is_isl_ctx(method);
412 for (int i = 1; i < num_params; ++i) {
413 ParmVarDecl *param = method->getParamDecl(i);
414 QualType type = param->getOriginalType();
415 if (is_callback(type))
416 drop_user = 1;
419 print_method_header(is_static(method), cname,
420 num_params - drop_ctx - drop_user);
422 for (int i = drop_ctx; i < num_params; ++i) {
423 ParmVarDecl *param = method->getParamDecl(i);
424 string type;
425 if (!is_isl_type(param->getOriginalType()))
426 continue;
427 type = type2python(extract_type(param->getOriginalType()));
428 printf(" try:\n");
429 printf(" if not arg%d.__class__ is %s:\n",
430 i - drop_ctx, type.c_str());
431 printf(" arg%d = %s(arg%d)\n",
432 i - drop_ctx, type.c_str(), i - drop_ctx);
433 printf(" except:\n");
434 if (!drop_ctx && i > 0 && super.size() > 0) {
435 printf(" return %s(arg0).%s(",
436 type2python(super[0]).c_str(), cname.c_str());
437 for (int i = 1; i < num_params - drop_user; ++i) {
438 if (i != 1)
439 printf(", ");
440 printf("arg%d", i);
442 printf(")\n");
443 } else
444 printf(" raise\n");
446 for (int i = 1; i < num_params; ++i) {
447 ParmVarDecl *param = method->getParamDecl(i);
448 QualType type = param->getOriginalType();
449 if (!is_callback(type))
450 continue;
451 print_callback(type->getPointeeType(), i - drop_ctx);
453 if (drop_ctx)
454 printf(" ctx = Context.getDefaultInstance()\n");
455 else
456 printf(" ctx = arg0.ctx\n");
457 printf(" res = isl.%s(", fullname.c_str());
458 if (drop_ctx)
459 printf("ctx");
460 else
461 print_arg_in_call(method, 0, 0);
462 for (int i = 1; i < num_params - drop_user; ++i) {
463 printf(", ");
464 print_arg_in_call(method, i, drop_ctx);
466 if (drop_user)
467 printf(", None");
468 printf(")\n");
470 if (is_isl_type(method->getReturnType())) {
471 string type;
472 type = type2python(extract_type(method->getReturnType()));
473 printf(" return %s(ctx=ctx, ptr=res)\n",
474 type.c_str());
475 } else {
476 if (drop_user) {
477 printf(" if exc_info[0] != None:\n");
478 printf(" raise exc_info[0][0], "
479 "exc_info[0][1], exc_info[0][2]\n");
481 if (is_isl_bool(method->getReturnType())) {
482 printf(" if res < 0:\n");
483 printf(" raise\n");
484 printf(" return bool(res)\n");
485 } else {
486 printf(" return res\n");
491 /* Print part of an overloaded python method corresponding to the C function
492 * "method".
493 * "super" contains the superclasses of the class to which the method belongs.
495 * In particular, print code to test whether the arguments passed to
496 * the python method correspond to the arguments expected by "method"
497 * and to call "method" if they do.
499 void isl_class::print_method_overload(FunctionDecl *method,
500 vector<string> super)
502 string fullname = method->getName();
503 int num_params = method->getNumParams();
504 int first;
505 string type;
507 first = is_static(method) ? 0 : 1;
509 printf(" if ");
510 for (int i = first; i < num_params; ++i) {
511 if (i > first)
512 printf(" and ");
513 ParmVarDecl *param = method->getParamDecl(i);
514 if (is_isl_type(param->getOriginalType())) {
515 string type;
516 type = extract_type(param->getOriginalType());
517 type = type2python(type);
518 printf("arg%d.__class__ is %s", i, type.c_str());
519 } else
520 printf("type(arg%d) == str", i);
522 printf(":\n");
523 printf(" res = isl.%s(", fullname.c_str());
524 print_arg_in_call(method, 0, 0);
525 for (int i = 1; i < num_params; ++i) {
526 printf(", ");
527 print_arg_in_call(method, i, 0);
529 printf(")\n");
530 type = type2python(extract_type(method->getReturnType()));
531 printf(" return %s(ctx=arg0.ctx, ptr=res)\n", type.c_str());
534 /* Print a python method with a name derived from "fullname"
535 * corresponding to the C functions "methods".
536 * "super" contains the superclasses of the class to which the method belongs.
538 * If "methods" consists of a single element that is not marked overloaded,
539 * the use print_method to print the method.
540 * Otherwise, print an overloaded method with pieces corresponding
541 * to each function in "methods".
543 void isl_class::print_method(const string &fullname,
544 const set<FunctionDecl *> &methods, vector<string> super)
546 string cname;
547 set<FunctionDecl *>::const_iterator it;
548 int num_params;
549 FunctionDecl *any_method;
551 any_method = *methods.begin();
552 if (methods.size() == 1 && !is_overload(any_method)) {
553 print_method(any_method, super);
554 return;
557 cname = fullname.substr(name.length() + 1);
558 num_params = any_method->getNumParams();
560 print_method_header(is_static(any_method), cname, num_params);
562 for (it = methods.begin(); it != methods.end(); ++it)
563 print_method_overload(*it, super);
566 /* Print part of the constructor for this isl_class.
568 * In particular, check if the actual arguments correspond to the
569 * formal arguments of "cons" and if so call "cons" and put the
570 * result in self.ptr and a reference to the default context in self.ctx.
572 * If the function consumes a reference, then we pass it a copy of
573 * the actual argument.
575 void isl_class::print_constructor(FunctionDecl *cons)
577 string fullname = cons->getName();
578 string cname = fullname.substr(name.length() + 1);
579 int num_params = cons->getNumParams();
580 int drop_ctx = first_arg_is_isl_ctx(cons);
582 printf(" if len(args) == %d", num_params - drop_ctx);
583 for (int i = drop_ctx; i < num_params; ++i) {
584 ParmVarDecl *param = cons->getParamDecl(i);
585 QualType type = param->getOriginalType();
586 if (is_isl_type(type)) {
587 string s;
588 s = type2python(extract_type(type));
589 printf(" and args[%d].__class__ is %s",
590 i - drop_ctx, s.c_str());
591 } else if (type->isPointerType()) {
592 printf(" and type(args[%d]) == str", i - drop_ctx);
593 } else {
594 printf(" and type(args[%d]) == int", i - drop_ctx);
597 printf(":\n");
598 printf(" self.ctx = Context.getDefaultInstance()\n");
599 printf(" self.ptr = isl.%s(", fullname.c_str());
600 if (drop_ctx)
601 printf("self.ctx");
602 for (int i = drop_ctx; i < num_params; ++i) {
603 ParmVarDecl *param = cons->getParamDecl(i);
604 if (i)
605 printf(", ");
606 if (is_isl_type(param->getOriginalType())) {
607 if (takes(param)) {
608 string type;
609 type = extract_type(param->getOriginalType());
610 printf("isl.%s_copy(args[%d].ptr)",
611 type.c_str(), i - drop_ctx);
612 } else
613 printf("args[%d].ptr", i - drop_ctx);
614 } else
615 printf("args[%d]", i - drop_ctx);
617 printf(")\n");
618 printf(" return\n");
621 /* Print the header of the class "name" with superclasses "super".
623 static void print_class_header(const string &name, const vector<string> &super)
625 printf("class %s", name.c_str());
626 if (super.size() > 0) {
627 printf("(");
628 for (int i = 0; i < super.size(); ++i) {
629 if (i > 0)
630 printf(", ");
631 printf("%s", type2python(super[i]).c_str());
633 printf(")");
635 printf(":\n");
638 /* Tell ctypes about the return type of "fd".
639 * In particular, if "fd" returns a pointer to an isl object,
640 * then tell ctypes it returns a "c_void_p".
641 * Similarly, if "fd" returns an isl_bool,
642 * then tell ctypes it returns a "c_bool".
644 static void print_restype(FunctionDecl *fd)
646 string fullname = fd->getName();
647 QualType type = fd->getReturnType();
648 if (is_isl_type(type))
649 printf("isl.%s.restype = c_void_p\n", fullname.c_str());
650 else if (is_isl_bool(type))
651 printf("isl.%s.restype = c_bool\n", fullname.c_str());
654 /* Tell ctypes about the types of the arguments of the function "fd".
656 static void print_argtypes(FunctionDecl *fd)
658 string fullname = fd->getName();
659 int n = fd->getNumParams();
660 int drop_user = 0;
662 printf("isl.%s.argtypes = [", fullname.c_str());
663 for (int i = 0; i < n - drop_user; ++i) {
664 ParmVarDecl *param = fd->getParamDecl(i);
665 QualType type = param->getOriginalType();
666 if (is_callback(type))
667 drop_user = 1;
668 if (i)
669 printf(", ");
670 if (is_isl_ctx(type))
671 printf("Context");
672 else if (is_isl_type(type) || is_callback(type))
673 printf("c_void_p");
674 else if (is_string(type))
675 printf("c_char_p");
676 else
677 printf("c_int");
679 if (drop_user)
680 printf(", c_void_p");
681 printf("]\n");
684 /* Print out the definition of this isl_class.
686 * We first check if this isl_class is a subclass of one or more other classes.
687 * If it is, we make sure those superclasses are printed out first.
689 * Then we print a constructor with several cases, one for constructing
690 * a Python object from a return value and one for each function that
691 * was marked as a constructor.
693 * Next, we print out some common methods and the methods corresponding
694 * to functions that are not marked as constructors.
696 * Finally, we tell ctypes about the types of the arguments of the
697 * constructor functions and the return types of those function returning
698 * an isl object.
700 void isl_class::print(map<string, isl_class> &classes, set<string> &done)
702 string p_name = type2python(name);
703 set<FunctionDecl *>::iterator in;
704 map<string, set<FunctionDecl *> >::iterator it;
705 vector<string> super = find_superclasses(type);
707 for (int i = 0; i < super.size(); ++i)
708 if (done.find(super[i]) == done.end())
709 classes[super[i]].print(classes, done);
710 done.insert(name);
712 printf("\n");
713 print_class_header(p_name, super);
714 printf(" def __init__(self, *args, **keywords):\n");
716 printf(" if \"ptr\" in keywords:\n");
717 printf(" self.ctx = keywords[\"ctx\"]\n");
718 printf(" self.ptr = keywords[\"ptr\"]\n");
719 printf(" return\n");
721 for (in = constructors.begin(); in != constructors.end(); ++in)
722 print_constructor(*in);
723 printf(" raise Error\n");
724 printf(" def __del__(self):\n");
725 printf(" if hasattr(self, 'ptr'):\n");
726 printf(" isl.%s_free(self.ptr)\n", name.c_str());
727 printf(" def __str__(self):\n");
728 printf(" ptr = isl.%s_to_str(self.ptr)\n", name.c_str());
729 printf(" res = str(cast(ptr, c_char_p).value)\n");
730 printf(" libc.free(ptr)\n");
731 printf(" return res\n");
732 printf(" def __repr__(self):\n");
733 printf(" s = str(self)\n");
734 printf(" if '\"' in s:\n");
735 printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
736 p_name.c_str());
737 printf(" else:\n");
738 printf(" return 'isl.%s(\"%%s\")' %% s\n",
739 p_name.c_str());
741 for (it = methods.begin(); it != methods.end(); ++it)
742 print_method(it->first, it->second, super);
744 printf("\n");
745 for (in = constructors.begin(); in != constructors.end(); ++in) {
746 print_restype(*in);
747 print_argtypes(*in);
749 for (it = methods.begin(); it != methods.end(); ++it)
750 for (in = it->second.begin(); in != it->second.end(); ++in) {
751 print_restype(*in);
752 print_argtypes(*in);
754 printf("isl.%s_free.argtypes = [c_void_p]\n", name.c_str());
755 printf("isl.%s_to_str.argtypes = [c_void_p]\n", name.c_str());
756 printf("isl.%s_to_str.restype = POINTER(c_char)\n", name.c_str());
759 /* Generate a python interface based on the extracted types and functions.
760 * We first collect all functions that belong to a certain type,
761 * separating constructors from regular methods. If there are any
762 * overloaded functions, then they are grouped based on their name
763 * after removing the argument type suffix.
765 * Then we print out each class in turn. If one of these is a subclass
766 * of some other class, it will make sure the superclass is printed out first.
768 void generate_python(set<RecordDecl *> &types, set<FunctionDecl *> functions)
770 map<string, isl_class> classes;
771 map<string, isl_class>::iterator ci;
772 set<string> done;
774 set<RecordDecl *>::iterator it;
775 for (it = types.begin(); it != types.end(); ++it) {
776 RecordDecl *decl = *it;
777 string name = decl->getName();
778 classes[name].name = name;
779 classes[name].type = decl;
782 set<FunctionDecl *>::iterator in;
783 for (in = functions.begin(); in != functions.end(); ++in) {
784 isl_class *c = method2class(classes, *in);
785 if (!c)
786 continue;
787 if (is_constructor(*in)) {
788 c->constructors.insert(*in);
789 } else {
790 FunctionDecl *method = *in;
791 string fullname = method->getName();
792 fullname = drop_type_suffix(fullname, method);
793 c->methods[fullname].insert(method);
797 for (ci = classes.begin(); ci != classes.end(); ++ci) {
798 if (done.find(ci->first) == done.end())
799 ci->second.print(classes, done);