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 /* 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
)
51 if (!decl
->hasAttrs())
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
);
61 string s
= ann
->getAnnotation().str();
62 if (s
.substr(0, len
) == sub
) {
63 s
= s
.substr(len
+ 1, s
.length() - len
- 2);
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.
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
,
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
)
128 if (classes
.find(best
) == classes
.end()) {
129 cerr
<< "Unable to find class of " << name
<< endl
;
133 return &classes
[best
];
136 /* Is "type" the type "isl_ctx *"?
138 static bool is_isl_ctx(QualType type
)
140 if (!type
->isPointerType())
142 type
= type
->getPointeeType();
143 if (type
.getAsString() != "isl_ctx")
149 /* Is the first argument of "fd" of type "isl_ctx *"?
151 static bool first_arg_is_isl_ctx(FunctionDecl
*fd
)
155 if (fd
->getNumParams() < 1)
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()) {
169 type
= type
->getPointeeType();
170 if (type
->isFunctionType())
172 s
= type
.getAsString();
173 return s
.substr(0, 4) == "isl_";
179 /* Is "type" the type isl_bool?
181 static bool is_isl_bool(QualType type
)
185 if (type
->isPointerType())
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())
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";
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();
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
)
241 size_t name_len
, type_len
;
243 if (!is_overload(method
))
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);
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
))
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
)
284 printf(" @staticmethod\n");
290 printf(" def %s(", s
);
291 for (int i
= 0; i
< n_arg
; ++i
) {
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
) {
324 printf("cb_arg%d", i
);
327 for (int i
= 0; i
< n_arg
- 1; ++i
) {
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
);
334 printf(" arg%d(", arg
);
335 for (int i
= 0; i
< n_arg
- 1; ++i
) {
338 printf("cb_arg%d", i
);
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
)) {
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
);
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();
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
))
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
);
425 if (!is_isl_type(param
->getOriginalType()))
427 type
= type2python(extract_type(param
->getOriginalType()));
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
) {
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
))
451 print_callback(type
->getPointeeType(), i
- drop_ctx
);
454 printf(" ctx = Context.getDefaultInstance()\n");
456 printf(" ctx = arg0.ctx\n");
457 printf(" res = isl.%s(", fullname
.c_str());
461 print_arg_in_call(method
, 0, 0);
462 for (int i
= 1; i
< num_params
- drop_user
; ++i
) {
464 print_arg_in_call(method
, i
, drop_ctx
);
470 if (is_isl_type(method
->getReturnType())) {
472 type
= type2python(extract_type(method
->getReturnType()));
473 printf(" return %s(ctx=ctx, ptr=res)\n",
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");
484 printf(" return bool(res)\n");
486 printf(" return res\n");
491 /* Print part of an overloaded python method corresponding to the C function
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();
507 first
= is_static(method
) ? 0 : 1;
510 for (int i
= first
; i
< num_params
; ++i
) {
513 ParmVarDecl
*param
= method
->getParamDecl(i
);
514 if (is_isl_type(param
->getOriginalType())) {
516 type
= extract_type(param
->getOriginalType());
517 type
= type2python(type
);
518 printf("arg%d.__class__ is %s", i
, type
.c_str());
520 printf("type(arg%d) == str", i
);
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
) {
527 print_arg_in_call(method
, i
, 0);
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
)
547 set
<FunctionDecl
*>::const_iterator it
;
549 FunctionDecl
*any_method
;
551 any_method
= *methods
.begin();
552 if (methods
.size() == 1 && !is_overload(any_method
)) {
553 print_method(any_method
, super
);
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
)) {
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
);
594 printf(" and type(args[%d]) == int", i
- drop_ctx
);
598 printf(" self.ctx = Context.getDefaultInstance()\n");
599 printf(" self.ptr = isl.%s(", fullname
.c_str());
602 for (int i
= drop_ctx
; i
< num_params
; ++i
) {
603 ParmVarDecl
*param
= cons
->getParamDecl(i
);
606 if (is_isl_type(param
->getOriginalType())) {
609 type
= extract_type(param
->getOriginalType());
610 printf("isl.%s_copy(args[%d].ptr)",
611 type
.c_str(), i
- drop_ctx
);
613 printf("args[%d].ptr", i
- drop_ctx
);
615 printf("args[%d]", i
- drop_ctx
);
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) {
628 for (int i
= 0; i
< super
.size(); ++i
) {
631 printf("%s", type2python(super
[i
]).c_str());
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();
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
))
670 if (is_isl_ctx(type
))
672 else if (is_isl_type(type
) || is_callback(type
))
674 else if (is_string(type
))
680 printf(", c_void_p");
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
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
);
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");
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",
738 printf(" return 'isl.%s(\"%%s\")' %% s\n",
741 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
742 print_method(it
->first
, it
->second
, super
);
745 for (in
= constructors
.begin(); in
!= constructors
.end(); ++in
) {
749 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
750 for (in
= it
->second
.begin(); in
!= it
->second
.end(); ++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
;
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
);
787 if (is_constructor(*in
)) {
788 c
->constructors
.insert(*in
);
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
);