2 * Copyright 2011,2015 Sven Verdoolaege. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
34 #include "isl_config.h"
40 #include <clang/AST/Attr.h>
41 #include "extract_interface.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
);
54 /* Return a sequence of the types of which the given type declaration is
55 * marked as being a subtype.
56 * The order of the types is the opposite of the order in which they
57 * appear in the source. In particular, the first annotation
58 * is the one that is closest to the annotated type and the corresponding
59 * type is then also the first that will appear in the sequence of types.
61 static vector
<string
> find_superclasses(RecordDecl
*decl
)
65 if (!decl
->hasAttrs())
68 string sub
= "isl_subclass";
69 size_t len
= sub
.length();
70 AttrVec attrs
= decl
->getAttrs();
71 for (AttrVec::const_iterator i
= attrs
.begin() ; i
!= attrs
.end(); ++i
) {
72 const AnnotateAttr
*ann
= dyn_cast
<AnnotateAttr
>(*i
);
75 string s
= ann
->getAnnotation().str();
76 if (s
.substr(0, len
) == sub
) {
77 s
= s
.substr(len
+ 1, s
.length() - len
- 2);
85 /* Is decl marked as being part of an overloaded method?
87 static bool is_overload(Decl
*decl
)
89 return has_annotation(decl
, "isl_overload");
92 /* Is decl marked as a constructor?
94 static bool is_constructor(Decl
*decl
)
96 return has_annotation(decl
, "isl_constructor");
99 /* Is decl marked as consuming a reference?
101 static bool takes(Decl
*decl
)
103 return has_annotation(decl
, "isl_take");
106 /* Is decl marked as returning a reference that is required to be freed.
108 static bool gives(Decl
*decl
)
110 return has_annotation(decl
, "isl_give");
113 /* isl_class collects all constructors and methods for an isl "class".
114 * "name" is the name of the class.
115 * "type" is the declaration that introduces the type.
116 * "methods" contains the set of methods, grouped by method name.
117 * "fn_to_str" is a reference to the *_to_str method of this class, if any.
118 * "fn_free" is a reference to the *_free method of this class, if any.
123 set
<FunctionDecl
*> constructors
;
124 map
<string
, set
<FunctionDecl
*> > methods
;
125 FunctionDecl
*fn_to_str
;
126 FunctionDecl
*fn_free
;
128 bool is_static(FunctionDecl
*method
);
130 void print(map
<string
, isl_class
> &classes
, set
<string
> &done
);
131 void print_constructor(FunctionDecl
*method
);
132 void print_representation(const string
&python_name
);
133 void print_method_type(FunctionDecl
*fd
);
134 void print_method_types();
135 void print_method(FunctionDecl
*method
, vector
<string
> super
);
136 void print_method_overload(FunctionDecl
*method
);
137 void print_method(const string
&fullname
,
138 const set
<FunctionDecl
*> &methods
, vector
<string
> super
);
141 /* Return the class that has a name that matches the initial part
142 * of the name of function "fd" or NULL if no such class could be found.
144 static isl_class
*method2class(map
<string
, isl_class
> &classes
,
148 map
<string
, isl_class
>::iterator ci
;
149 string name
= fd
->getNameAsString();
151 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
) {
152 if (name
.substr(0, ci
->first
.length()) == ci
->first
)
156 if (classes
.find(best
) == classes
.end()) {
157 cerr
<< "Unable to find class of " << name
<< endl
;
161 return &classes
[best
];
164 /* Is "type" the type "isl_ctx *"?
166 static bool is_isl_ctx(QualType type
)
168 if (!type
->isPointerType())
170 type
= type
->getPointeeType();
171 if (type
.getAsString() != "isl_ctx")
177 /* Is the first argument of "fd" of type "isl_ctx *"?
179 static bool first_arg_is_isl_ctx(FunctionDecl
*fd
)
183 if (fd
->getNumParams() < 1)
186 param
= fd
->getParamDecl(0);
187 return is_isl_ctx(param
->getOriginalType());
190 /* Is "type" that of a pointer to an isl_* structure?
192 static bool is_isl_type(QualType type
)
194 if (type
->isPointerType()) {
197 type
= type
->getPointeeType();
198 if (type
->isFunctionType())
200 s
= type
.getAsString();
201 return s
.substr(0, 4) == "isl_";
207 /* Is "type" the type isl_bool?
209 static bool is_isl_bool(QualType type
)
213 if (type
->isPointerType())
216 s
= type
.getAsString();
217 return s
== "isl_bool";
220 /* Is "type" that of a pointer to a function?
222 static bool is_callback(QualType type
)
224 if (!type
->isPointerType())
226 type
= type
->getPointeeType();
227 return type
->isFunctionType();
230 /* Is "type" that of "char *" of "const char *"?
232 static bool is_string(QualType type
)
234 if (type
->isPointerType()) {
235 string s
= type
->getPointeeType().getAsString();
236 return s
== "const char" || s
== "char";
242 /* Return the name of the type that "type" points to.
243 * The input "type" is assumed to be a pointer type.
245 static string
extract_type(QualType type
)
247 if (type
->isPointerType())
248 return type
->getPointeeType().getAsString();
249 die("Cannot extract type from non-pointer type");
252 /* Drop the "isl_" initial part of the type name "name".
254 static string
type2python(string name
)
256 return name
.substr(4);
259 /* If "method" is overloaded, then drop the suffix of "name"
260 * corresponding to the type of the final argument and
261 * return the modified name (or the original name if
262 * no modifications were made).
264 static string
drop_type_suffix(string name
, FunctionDecl
*method
)
269 size_t name_len
, type_len
;
271 if (!is_overload(method
))
274 num_params
= method
->getNumParams();
275 param
= method
->getParamDecl(num_params
- 1);
276 type
= extract_type(param
->getOriginalType());
277 type
= type
.substr(4);
278 name_len
= name
.length();
279 type_len
= type
.length();
281 if (name_len
> type_len
&& name
.substr(name_len
- type_len
) == type
)
282 name
= name
.substr(0, name_len
- type_len
- 1);
287 /* Should "method" be considered to be a static method?
288 * That is, is the first argument something other than
289 * an instance of the class?
291 bool isl_class::is_static(FunctionDecl
*method
)
293 ParmVarDecl
*param
= method
->getParamDecl(0);
294 QualType type
= param
->getOriginalType();
296 if (!is_isl_type(type
))
298 return extract_type(type
) != name
;
301 /* Print the header of the method "name" with "n_arg" arguments.
302 * If "is_static" is set, then mark the python method as static.
304 * If the method is called "from", then rename it to "convert_from"
305 * because "from" is a python keyword.
307 static void print_method_header(bool is_static
, const string
&name
, int n_arg
)
312 printf(" @staticmethod\n");
318 printf(" def %s(", s
);
319 for (int i
= 0; i
< n_arg
; ++i
) {
327 /* Print a check that the argument in position "pos" is of type "type".
328 * If this fails and if "upcast" is set, then convert the first
329 * argument to "super" and call the method "name" on it, passing
330 * the remaining of the "n" arguments.
331 * If the check fails and "upcast" is not set, then simply raise
333 * If "upcast" is not set, then the "super", "name" and "n" arguments
334 * to this function are ignored.
336 static void print_type_check(const string
&type
, int pos
, bool upcast
,
337 const string
&super
, const string
&name
, int n
)
340 printf(" if not arg%d.__class__ is %s:\n",
342 printf(" arg%d = %s(arg%d)\n",
343 pos
, type
.c_str(), pos
);
344 printf(" except:\n");
346 printf(" return %s(arg0).%s(",
347 type2python(super
).c_str(), name
.c_str());
348 for (int i
= 1; i
< n
; ++i
) {
358 /* Construct a wrapper for a callback argument (at position "arg").
359 * Assign the wrapper to "cb". We assume here that a function call
360 * has at most one callback argument.
362 * The wrapper converts the arguments of the callback to python types.
363 * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
364 * and returns -1. Otherwise the wrapper returns 0.
366 static void print_callback(QualType type
, int arg
)
368 const FunctionProtoType
*fn
= type
->getAs
<FunctionProtoType
>();
369 unsigned n_arg
= fn
->getNumArgs();
371 printf(" exc_info = [None]\n");
372 printf(" fn = CFUNCTYPE(c_int");
373 for (unsigned i
= 0; i
< n_arg
- 1; ++i
) {
374 if (!is_isl_type(fn
->getArgType(i
)))
375 die("Argument has non-isl type");
376 printf(", c_void_p");
378 printf(", c_void_p)\n");
379 printf(" def cb_func(");
380 for (unsigned i
= 0; i
< n_arg
; ++i
) {
383 printf("cb_arg%d", i
);
386 for (unsigned i
= 0; i
< n_arg
- 1; ++i
) {
388 arg_type
= type2python(extract_type(fn
->getArgType(i
)));
389 printf(" cb_arg%d = %s(ctx=arg0.ctx, "
390 "ptr=cb_arg%d)\n", i
, arg_type
.c_str(), i
);
393 printf(" arg%d(", arg
);
394 for (unsigned i
= 0; i
< n_arg
- 1; ++i
) {
397 printf("cb_arg%d", i
);
400 printf(" except:\n");
401 printf(" import sys\n");
402 printf(" exc_info[0] = sys.exc_info()\n");
403 printf(" return -1\n");
404 printf(" return 0\n");
405 printf(" cb = fn(cb_func)\n");
408 /* Print the argument at position "arg" in call to "fd".
409 * "skip" is the number of initial arguments of "fd" that are
410 * skipped in the Python method.
412 * If the argument is a callback, then print a reference to
413 * the callback wrapper "cb".
414 * Otherwise, if the argument is marked as consuming a reference,
415 * then pass a copy of the the pointer stored in the corresponding
416 * argument passed to the Python method.
417 * Otherwise, if the argument is a pointer, then pass this pointer itself.
418 * Otherwise, pass the argument directly.
420 static void print_arg_in_call(FunctionDecl
*fd
, int arg
, int skip
)
422 ParmVarDecl
*param
= fd
->getParamDecl(arg
);
423 QualType type
= param
->getOriginalType();
424 if (is_callback(type
)) {
426 } else if (takes(param
)) {
427 string type_s
= extract_type(type
);
428 printf("isl.%s_copy(arg%d.ptr)", type_s
.c_str(), arg
- skip
);
429 } else if (type
->isPointerType()) {
430 printf("arg%d.ptr", arg
- skip
);
432 printf("arg%d", arg
- skip
);
436 /* Print the return statement of the python method corresponding
437 * to the C function "method".
439 * If the return type is a (const) char *, then convert the result
440 * to a Python string, raising an error on NULL and freeing
441 * the C string if needed.
443 * If the return type is isl_bool, then convert the result to
444 * a Python boolean, raising an error on isl_bool_error.
446 static void print_method_return(FunctionDecl
*method
)
448 QualType return_type
= method
->getReturnType();
450 if (is_isl_type(return_type
)) {
453 type
= type2python(extract_type(return_type
));
454 printf(" return %s(ctx=ctx, ptr=res)\n", type
.c_str());
455 } else if (is_string(return_type
)) {
456 printf(" if res == 0:\n");
458 printf(" string = str(cast(res, c_char_p).value)\n");
461 printf(" libc.free(res)\n");
463 printf(" return string\n");
464 } else if (is_isl_bool(return_type
)) {
465 printf(" if res < 0:\n");
467 printf(" return bool(res)\n");
469 printf(" return res\n");
473 /* Print a python method corresponding to the C function "method".
474 * "super" contains the superclasses of the class to which the method belongs,
475 * with the first element corresponding to the annotation that appears
476 * closest to the annotated type. This superclass is the least
477 * general extension of the annotated type in the linearization
478 * of the class hierarchy.
480 * If the first argument of "method" is something other than an instance
481 * of the class, then mark the python method as static.
482 * If, moreover, this first argument is an isl_ctx, then remove
483 * it from the arguments of the Python method.
485 * If the function has a callback argument, then it also has a "user"
486 * argument. Since Python has closures, there is no need for such
487 * a user argument in the Python interface, so we simply drop it.
488 * We also create a wrapper ("cb") for the callback.
490 * For each argument of the function that refers to an isl structure,
491 * including the object on which the method is called,
492 * we check if the corresponding actual argument is of the right type.
493 * If not, we try to convert it to the right type.
494 * If that doesn't work and if "super" contains at least one element, we try
495 * to convert self to the type of the first superclass in "super" and
496 * call the corresponding method.
498 * If the function consumes a reference, then we pass it a copy of
499 * the actual argument.
501 void isl_class::print_method(FunctionDecl
*method
, vector
<string
> super
)
503 string fullname
= method
->getName();
504 string cname
= fullname
.substr(name
.length() + 1);
505 int num_params
= method
->getNumParams();
507 int drop_ctx
= first_arg_is_isl_ctx(method
);
509 for (int i
= 1; i
< num_params
; ++i
) {
510 ParmVarDecl
*param
= method
->getParamDecl(i
);
511 QualType type
= param
->getOriginalType();
512 if (is_callback(type
))
516 print_method_header(is_static(method
), cname
,
517 num_params
- drop_ctx
- drop_user
);
519 for (int i
= drop_ctx
; i
< num_params
; ++i
) {
520 ParmVarDecl
*param
= method
->getParamDecl(i
);
522 if (!is_isl_type(param
->getOriginalType()))
524 type
= type2python(extract_type(param
->getOriginalType()));
525 if (!drop_ctx
&& i
> 0 && super
.size() > 0)
526 print_type_check(type
, i
- drop_ctx
, true, super
[0],
527 cname
, num_params
- drop_user
);
529 print_type_check(type
, i
- drop_ctx
, false, "",
532 for (int i
= 1; i
< num_params
; ++i
) {
533 ParmVarDecl
*param
= method
->getParamDecl(i
);
534 QualType type
= param
->getOriginalType();
535 if (!is_callback(type
))
537 print_callback(type
->getPointeeType(), i
- drop_ctx
);
540 printf(" ctx = Context.getDefaultInstance()\n");
542 printf(" ctx = arg0.ctx\n");
543 printf(" res = isl.%s(", fullname
.c_str());
547 print_arg_in_call(method
, 0, 0);
548 for (int i
= 1; i
< num_params
- drop_user
; ++i
) {
550 print_arg_in_call(method
, i
, drop_ctx
);
557 printf(" if exc_info[0] != None:\n");
558 printf(" raise (exc_info[0][0], "
559 "exc_info[0][1], exc_info[0][2])\n");
562 print_method_return(method
);
565 /* Print part of an overloaded python method corresponding to the C function
568 * In particular, print code to test whether the arguments passed to
569 * the python method correspond to the arguments expected by "method"
570 * and to call "method" if they do.
572 void isl_class::print_method_overload(FunctionDecl
*method
)
574 string fullname
= method
->getName();
575 int num_params
= method
->getNumParams();
579 first
= is_static(method
) ? 0 : 1;
582 for (int i
= first
; i
< num_params
; ++i
) {
585 ParmVarDecl
*param
= method
->getParamDecl(i
);
586 if (is_isl_type(param
->getOriginalType())) {
588 type
= extract_type(param
->getOriginalType());
589 type
= type2python(type
);
590 printf("arg%d.__class__ is %s", i
, type
.c_str());
592 printf("type(arg%d) == str", i
);
595 printf(" res = isl.%s(", fullname
.c_str());
596 print_arg_in_call(method
, 0, 0);
597 for (int i
= 1; i
< num_params
; ++i
) {
599 print_arg_in_call(method
, i
, 0);
602 type
= type2python(extract_type(method
->getReturnType()));
603 printf(" return %s(ctx=arg0.ctx, ptr=res)\n", type
.c_str());
606 /* Print a python method with a name derived from "fullname"
607 * corresponding to the C functions "methods".
608 * "super" contains the superclasses of the class to which the method belongs.
610 * If "methods" consists of a single element that is not marked overloaded,
611 * the use print_method to print the method.
612 * Otherwise, print an overloaded method with pieces corresponding
613 * to each function in "methods".
615 void isl_class::print_method(const string
&fullname
,
616 const set
<FunctionDecl
*> &methods
, vector
<string
> super
)
619 set
<FunctionDecl
*>::const_iterator it
;
621 FunctionDecl
*any_method
;
623 any_method
= *methods
.begin();
624 if (methods
.size() == 1 && !is_overload(any_method
)) {
625 print_method(any_method
, super
);
629 cname
= fullname
.substr(name
.length() + 1);
630 num_params
= any_method
->getNumParams();
632 print_method_header(is_static(any_method
), cname
, num_params
);
634 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
635 print_method_overload(*it
);
638 /* Print part of the constructor for this isl_class.
640 * In particular, check if the actual arguments correspond to the
641 * formal arguments of "cons" and if so call "cons" and put the
642 * result in self.ptr and a reference to the default context in self.ctx.
644 * If the function consumes a reference, then we pass it a copy of
645 * the actual argument.
647 void isl_class::print_constructor(FunctionDecl
*cons
)
649 string fullname
= cons
->getName();
650 string cname
= fullname
.substr(name
.length() + 1);
651 int num_params
= cons
->getNumParams();
652 int drop_ctx
= first_arg_is_isl_ctx(cons
);
654 printf(" if len(args) == %d", num_params
- drop_ctx
);
655 for (int i
= drop_ctx
; i
< num_params
; ++i
) {
656 ParmVarDecl
*param
= cons
->getParamDecl(i
);
657 QualType type
= param
->getOriginalType();
658 if (is_isl_type(type
)) {
660 s
= type2python(extract_type(type
));
661 printf(" and args[%d].__class__ is %s",
662 i
- drop_ctx
, s
.c_str());
663 } else if (type
->isPointerType()) {
664 printf(" and type(args[%d]) == str", i
- drop_ctx
);
666 printf(" and type(args[%d]) == int", i
- drop_ctx
);
670 printf(" self.ctx = Context.getDefaultInstance()\n");
671 printf(" self.ptr = isl.%s(", fullname
.c_str());
674 for (int i
= drop_ctx
; i
< num_params
; ++i
) {
675 ParmVarDecl
*param
= cons
->getParamDecl(i
);
678 if (is_isl_type(param
->getOriginalType())) {
681 type
= extract_type(param
->getOriginalType());
682 printf("isl.%s_copy(args[%d].ptr)",
683 type
.c_str(), i
- drop_ctx
);
685 printf("args[%d].ptr", i
- drop_ctx
);
687 printf("args[%d]", i
- drop_ctx
);
693 /* Print the header of the class "name" with superclasses "super".
694 * The order of the superclasses is the opposite of the order
695 * in which the corresponding annotations appear in the source code.
697 static void print_class_header(const string
&name
, const vector
<string
> &super
)
699 printf("class %s", name
.c_str());
700 if (super
.size() > 0) {
702 for (unsigned i
= 0; i
< super
.size(); ++i
) {
705 printf("%s", type2python(super
[i
]).c_str());
714 /* Tell ctypes about the return type of "fd".
715 * In particular, if "fd" returns a pointer to an isl object,
716 * then tell ctypes it returns a "c_void_p".
717 * Similarly, if "fd" returns an isl_bool,
718 * then tell ctypes it returns a "c_bool".
719 * If "fd" returns a char *, then simply tell ctypes.
721 static void print_restype(FunctionDecl
*fd
)
723 string fullname
= fd
->getName();
724 QualType type
= fd
->getReturnType();
725 if (is_isl_type(type
))
726 printf("isl.%s.restype = c_void_p\n", fullname
.c_str());
727 else if (is_isl_bool(type
))
728 printf("isl.%s.restype = c_bool\n", fullname
.c_str());
729 else if (is_string(type
))
730 printf("isl.%s.restype = POINTER(c_char)\n", fullname
.c_str());
733 /* Tell ctypes about the types of the arguments of the function "fd".
735 static void print_argtypes(FunctionDecl
*fd
)
737 string fullname
= fd
->getName();
738 int n
= fd
->getNumParams();
741 printf("isl.%s.argtypes = [", fullname
.c_str());
742 for (int i
= 0; i
< n
- drop_user
; ++i
) {
743 ParmVarDecl
*param
= fd
->getParamDecl(i
);
744 QualType type
= param
->getOriginalType();
745 if (is_callback(type
))
749 if (is_isl_ctx(type
))
751 else if (is_isl_type(type
) || is_callback(type
))
753 else if (is_string(type
))
759 printf(", c_void_p");
763 /* Print type definitions for the method 'fd'.
765 void isl_class::print_method_type(FunctionDecl
*fd
)
771 /* Print declarations for methods printing the class representation,
772 * provided there is a corresponding *_to_str function.
774 * In particular, provide an implementation of __str__ and __repr__ methods to
775 * override the default representation used by python. Python uses __str__ to
776 * pretty print the class (e.g., when calling print(obj)) and uses __repr__
777 * when printing a precise representation of an object (e.g., when dumping it
778 * in the REPL console).
780 * Check the type of the argument before calling the *_to_str function
781 * on it in case the method was called on an object from a subclass.
783 void isl_class::print_representation(const string
&python_name
)
788 printf(" def __str__(arg0):\n");
789 print_type_check(python_name
, 0, false, "", "", -1);
790 printf(" ptr = isl.%s(arg0.ptr)\n",
791 string(fn_to_str
->getName()).c_str());
792 printf(" res = str(cast(ptr, c_char_p).value)\n");
793 printf(" libc.free(ptr)\n");
794 printf(" return res\n");
795 printf(" def __repr__(self):\n");
796 printf(" s = str(self)\n");
797 printf(" if '\"' in s:\n");
798 printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
799 python_name
.c_str());
801 printf(" return 'isl.%s(\"%%s\")' %% s\n",
802 python_name
.c_str());
805 /* Print code to set method type signatures.
807 * To be able to call C functions it is necessary to explicitly set their
808 * argument and result types. Do this for all exported constructors and
809 * methods, as well as for the *_to_str method, if it exists.
810 * Assuming each exported class has a *_free method,
811 * also unconditionally set the type of such methods.
813 void isl_class::print_method_types()
815 set
<FunctionDecl
*>::iterator in
;
816 map
<string
, set
<FunctionDecl
*> >::iterator it
;
818 for (in
= constructors
.begin(); in
!= constructors
.end(); ++in
)
819 print_method_type(*in
);
821 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
822 for (in
= it
->second
.begin(); in
!= it
->second
.end(); ++in
)
823 print_method_type(*in
);
825 print_method_type(fn_free
);
827 print_method_type(fn_to_str
);
830 /* Print out the definition of this isl_class.
832 * We first check if this isl_class is a subclass of one or more other classes.
833 * If it is, we make sure those superclasses are printed out first.
835 * Then we print a constructor with several cases, one for constructing
836 * a Python object from a return value and one for each function that
837 * was marked as a constructor.
839 * Next, we print out some common methods and the methods corresponding
840 * to functions that are not marked as constructors.
842 * Finally, we tell ctypes about the types of the arguments of the
843 * constructor functions and the return types of those function returning
846 void isl_class::print(map
<string
, isl_class
> &classes
, set
<string
> &done
)
848 string p_name
= type2python(name
);
849 set
<FunctionDecl
*>::iterator in
;
850 map
<string
, set
<FunctionDecl
*> >::iterator it
;
851 vector
<string
> super
= find_superclasses(type
);
853 for (unsigned i
= 0; i
< super
.size(); ++i
)
854 if (done
.find(super
[i
]) == done
.end())
855 classes
[super
[i
]].print(classes
, done
);
859 print_class_header(p_name
, super
);
860 printf(" def __init__(self, *args, **keywords):\n");
862 printf(" if \"ptr\" in keywords:\n");
863 printf(" self.ctx = keywords[\"ctx\"]\n");
864 printf(" self.ptr = keywords[\"ptr\"]\n");
867 for (in
= constructors
.begin(); in
!= constructors
.end(); ++in
)
868 print_constructor(*in
);
869 printf(" raise Error\n");
870 printf(" def __del__(self):\n");
871 printf(" if hasattr(self, 'ptr'):\n");
872 printf(" isl.%s_free(self.ptr)\n", name
.c_str());
874 print_representation(p_name
);
876 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
877 print_method(it
->first
, it
->second
, super
);
881 print_method_types();
884 /* Generate a python interface based on the extracted types and functions.
885 * We first collect all functions that belong to a certain type,
886 * separating constructors from regular methods and keeping track
887 * of the _to_str and _free functions, if any, separately. If there are any
888 * overloaded functions, then they are grouped based on their name
889 * after removing the argument type suffix.
891 * Then we print out each class in turn. If one of these is a subclass
892 * of some other class, it will make sure the superclass is printed out first.
894 void generate_python(set
<RecordDecl
*> &exported_types
,
895 set
<FunctionDecl
*> exported_functions
, set
<FunctionDecl
*> functions
)
897 map
<string
, isl_class
> classes
;
898 map
<string
, isl_class
>::iterator ci
;
900 map
<string
, FunctionDecl
*> functions_by_name
;
902 set
<FunctionDecl
*>::iterator in
;
903 for (in
= functions
.begin(); in
!= functions
.end(); ++in
) {
904 FunctionDecl
*decl
= *in
;
905 functions_by_name
[decl
->getName()] = decl
;
908 set
<RecordDecl
*>::iterator it
;
909 for (it
= exported_types
.begin(); it
!= exported_types
.end(); ++it
) {
910 RecordDecl
*decl
= *it
;
911 map
<string
, FunctionDecl
*>::iterator i
;
913 string name
= decl
->getName();
914 classes
[name
].name
= name
;
915 classes
[name
].type
= decl
;
916 classes
[name
].fn_to_str
= NULL
;
917 classes
[name
].fn_free
= NULL
;
919 i
= functions_by_name
.find(name
+ "_to_str");
920 if (i
!= functions_by_name
.end())
921 classes
[name
].fn_to_str
= i
->second
;
923 i
= functions_by_name
.find (name
+ "_free");
924 if (i
== functions_by_name
.end())
925 die("No _free function found");
926 classes
[name
].fn_free
= i
->second
;
929 for (in
= exported_functions
.begin(); in
!= exported_functions
.end();
931 isl_class
*c
= method2class(classes
, *in
);
934 if (is_constructor(*in
)) {
935 c
->constructors
.insert(*in
);
937 FunctionDecl
*method
= *in
;
938 string fullname
= method
->getName();
939 fullname
= drop_type_suffix(fullname
, method
);
940 c
->methods
[fullname
].insert(method
);
944 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
) {
945 if (done
.find(ci
->first
) == done
.end())
946 ci
->second
.print(classes
, done
);