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.
57 static vector
<string
> find_superclasses(RecordDecl
*decl
)
61 if (!decl
->hasAttrs())
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
);
71 string s
= ann
->getAnnotation().str();
72 if (s
.substr(0, len
) == sub
) {
73 s
= s
.substr(len
+ 1, s
.length() - len
- 2);
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.
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_method(FunctionDecl
*method
, vector
<string
> super
);
118 void print_method_overload(FunctionDecl
*method
, vector
<string
> super
);
119 void print_method(const string
&fullname
,
120 const set
<FunctionDecl
*> &methods
, vector
<string
> super
);
123 /* Return the class that has a name that matches the initial part
124 * of the name of function "fd" or NULL if no such class could be found.
126 static isl_class
*method2class(map
<string
, isl_class
> &classes
,
130 map
<string
, isl_class
>::iterator ci
;
131 string name
= fd
->getNameAsString();
133 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
) {
134 if (name
.substr(0, ci
->first
.length()) == ci
->first
)
138 if (classes
.find(best
) == classes
.end()) {
139 cerr
<< "Unable to find class of " << name
<< endl
;
143 return &classes
[best
];
146 /* Is "type" the type "isl_ctx *"?
148 static bool is_isl_ctx(QualType type
)
150 if (!type
->isPointerType())
152 type
= type
->getPointeeType();
153 if (type
.getAsString() != "isl_ctx")
159 /* Is the first argument of "fd" of type "isl_ctx *"?
161 static bool first_arg_is_isl_ctx(FunctionDecl
*fd
)
165 if (fd
->getNumParams() < 1)
168 param
= fd
->getParamDecl(0);
169 return is_isl_ctx(param
->getOriginalType());
172 /* Is "type" that of a pointer to an isl_* structure?
174 static bool is_isl_type(QualType type
)
176 if (type
->isPointerType()) {
179 type
= type
->getPointeeType();
180 if (type
->isFunctionType())
182 s
= type
.getAsString();
183 return s
.substr(0, 4) == "isl_";
189 /* Is "type" the type isl_bool?
191 static bool is_isl_bool(QualType type
)
195 if (type
->isPointerType())
198 s
= type
.getAsString();
199 return s
== "isl_bool";
202 /* Is "type" that of a pointer to a function?
204 static bool is_callback(QualType type
)
206 if (!type
->isPointerType())
208 type
= type
->getPointeeType();
209 return type
->isFunctionType();
212 /* Is "type" that of "char *" of "const char *"?
214 static bool is_string(QualType type
)
216 if (type
->isPointerType()) {
217 string s
= type
->getPointeeType().getAsString();
218 return s
== "const char" || s
== "char";
224 /* Return the name of the type that "type" points to.
225 * The input "type" is assumed to be a pointer type.
227 static string
extract_type(QualType type
)
229 if (type
->isPointerType())
230 return type
->getPointeeType().getAsString();
231 die("Cannot extract type from non-pointer type");
234 /* Drop the "isl_" initial part of the type name "name".
236 static string
type2python(string name
)
238 return name
.substr(4);
241 /* If "method" is overloaded, then drop the suffix of "name"
242 * corresponding to the type of the final argument and
243 * return the modified name (or the original name if
244 * no modifications were made).
246 static string
drop_type_suffix(string name
, FunctionDecl
*method
)
251 size_t name_len
, type_len
;
253 if (!is_overload(method
))
256 num_params
= method
->getNumParams();
257 param
= method
->getParamDecl(num_params
- 1);
258 type
= extract_type(param
->getOriginalType());
259 type
= type
.substr(4);
260 name_len
= name
.length();
261 type_len
= type
.length();
263 if (name_len
> type_len
&& name
.substr(name_len
- type_len
) == type
)
264 name
= name
.substr(0, name_len
- type_len
- 1);
269 /* Should "method" be considered to be a static method?
270 * That is, is the first argument something other than
271 * an instance of the class?
273 bool isl_class::is_static(FunctionDecl
*method
)
275 ParmVarDecl
*param
= method
->getParamDecl(0);
276 QualType type
= param
->getOriginalType();
278 if (!is_isl_type(type
))
280 return extract_type(type
) != name
;
283 /* Print the header of the method "name" with "n_arg" arguments.
284 * If "is_static" is set, then mark the python method as static.
286 * If the method is called "from", then rename it to "convert_from"
287 * because "from" is a python keyword.
289 static void print_method_header(bool is_static
, const string
&name
, int n_arg
)
294 printf(" @staticmethod\n");
300 printf(" def %s(", s
);
301 for (int i
= 0; i
< n_arg
; ++i
) {
309 /* Construct a wrapper for a callback argument (at position "arg").
310 * Assign the wrapper to "cb". We assume here that a function call
311 * has at most one callback argument.
313 * The wrapper converts the arguments of the callback to python types.
314 * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
315 * and returns -1. Otherwise the wrapper returns 0.
317 static void print_callback(QualType type
, int arg
)
319 const FunctionProtoType
*fn
= type
->getAs
<FunctionProtoType
>();
320 unsigned n_arg
= fn
->getNumArgs();
322 printf(" exc_info = [None]\n");
323 printf(" fn = CFUNCTYPE(c_int");
324 for (unsigned i
= 0; i
< n_arg
- 1; ++i
) {
325 if (!is_isl_type(fn
->getArgType(i
)))
326 die("Argument has non-isl type");
327 printf(", c_void_p");
329 printf(", c_void_p)\n");
330 printf(" def cb_func(");
331 for (unsigned i
= 0; i
< n_arg
; ++i
) {
334 printf("cb_arg%d", i
);
337 for (unsigned i
= 0; i
< n_arg
- 1; ++i
) {
339 arg_type
= type2python(extract_type(fn
->getArgType(i
)));
340 printf(" cb_arg%d = %s(ctx=arg0.ctx, "
341 "ptr=cb_arg%d)\n", i
, arg_type
.c_str(), i
);
344 printf(" arg%d(", arg
);
345 for (unsigned i
= 0; i
< n_arg
- 1; ++i
) {
348 printf("cb_arg%d", i
);
351 printf(" except:\n");
352 printf(" import sys\n");
353 printf(" exc_info[0] = sys.exc_info()\n");
354 printf(" return -1\n");
355 printf(" return 0\n");
356 printf(" cb = fn(cb_func)\n");
359 /* Print the argument at position "arg" in call to "fd".
360 * "skip" is the number of initial arguments of "fd" that are
361 * skipped in the Python method.
363 * If the argument is a callback, then print a reference to
364 * the callback wrapper "cb".
365 * Otherwise, if the argument is marked as consuming a reference,
366 * then pass a copy of the the pointer stored in the corresponding
367 * argument passed to the Python method.
368 * Otherwise, if the argument is a pointer, then pass this pointer itself.
369 * Otherwise, pass the argument directly.
371 static void print_arg_in_call(FunctionDecl
*fd
, int arg
, int skip
)
373 ParmVarDecl
*param
= fd
->getParamDecl(arg
);
374 QualType type
= param
->getOriginalType();
375 if (is_callback(type
)) {
377 } else if (takes(param
)) {
378 string type_s
= extract_type(type
);
379 printf("isl.%s_copy(arg%d.ptr)", type_s
.c_str(), arg
- skip
);
380 } else if (type
->isPointerType()) {
381 printf("arg%d.ptr", arg
- skip
);
383 printf("arg%d", arg
- skip
);
387 /* Print a python method corresponding to the C function "method".
388 * "super" contains the superclasses of the class to which the method belongs.
390 * If the first argument of "method" is something other than an instance
391 * of the class, then mark the python method as static.
392 * If, moreover, this first argument is an isl_ctx, then remove
393 * it from the arguments of the Python method.
395 * If the function has a callback argument, then it also has a "user"
396 * argument. Since Python has closures, there is no need for such
397 * a user argument in the Python interface, so we simply drop it.
398 * We also create a wrapper ("cb") for the callback.
400 * For each argument of the function that refers to an isl structure,
401 * including the object on which the method is called,
402 * we check if the corresponding actual argument is of the right type.
403 * If not, we try to convert it to the right type.
404 * It that doesn't work and if subclass is set, we try to convert self
405 * to the type of the first superclass in "super" and
406 * call the corresponding method.
408 * If the function consumes a reference, then we pass it a copy of
409 * the actual argument.
411 * If the return type is isl_bool, then convert the result to
412 * a Python boolean, raising an error on isl_bool_error.
414 void isl_class::print_method(FunctionDecl
*method
, vector
<string
> super
)
416 string fullname
= method
->getName();
417 string cname
= fullname
.substr(name
.length() + 1);
418 int num_params
= method
->getNumParams();
420 int drop_ctx
= first_arg_is_isl_ctx(method
);
422 for (int i
= 1; i
< num_params
; ++i
) {
423 ParmVarDecl
*param
= method
->getParamDecl(i
);
424 QualType type
= param
->getOriginalType();
425 if (is_callback(type
))
429 print_method_header(is_static(method
), cname
,
430 num_params
- drop_ctx
- drop_user
);
432 for (int i
= drop_ctx
; i
< num_params
; ++i
) {
433 ParmVarDecl
*param
= method
->getParamDecl(i
);
435 if (!is_isl_type(param
->getOriginalType()))
437 type
= type2python(extract_type(param
->getOriginalType()));
439 printf(" if not arg%d.__class__ is %s:\n",
440 i
- drop_ctx
, type
.c_str());
441 printf(" arg%d = %s(arg%d)\n",
442 i
- drop_ctx
, type
.c_str(), i
- drop_ctx
);
443 printf(" except:\n");
444 if (!drop_ctx
&& i
> 0 && super
.size() > 0) {
445 printf(" return %s(arg0).%s(",
446 type2python(super
[0]).c_str(), cname
.c_str());
447 for (int i
= 1; i
< num_params
- drop_user
; ++i
) {
456 for (int i
= 1; i
< num_params
; ++i
) {
457 ParmVarDecl
*param
= method
->getParamDecl(i
);
458 QualType type
= param
->getOriginalType();
459 if (!is_callback(type
))
461 print_callback(type
->getPointeeType(), i
- drop_ctx
);
464 printf(" ctx = Context.getDefaultInstance()\n");
466 printf(" ctx = arg0.ctx\n");
467 printf(" res = isl.%s(", fullname
.c_str());
471 print_arg_in_call(method
, 0, 0);
472 for (int i
= 1; i
< num_params
- drop_user
; ++i
) {
474 print_arg_in_call(method
, i
, drop_ctx
);
480 if (is_isl_type(method
->getReturnType())) {
482 type
= type2python(extract_type(method
->getReturnType()));
483 printf(" return %s(ctx=ctx, ptr=res)\n",
487 printf(" if exc_info[0] != None:\n");
488 printf(" raise exc_info[0][0], "
489 "exc_info[0][1], exc_info[0][2]\n");
491 if (is_isl_bool(method
->getReturnType())) {
492 printf(" if res < 0:\n");
494 printf(" return bool(res)\n");
496 printf(" return res\n");
501 /* Print part of an overloaded python method corresponding to the C function
503 * "super" contains the superclasses of the class to which the method belongs.
505 * In particular, print code to test whether the arguments passed to
506 * the python method correspond to the arguments expected by "method"
507 * and to call "method" if they do.
509 void isl_class::print_method_overload(FunctionDecl
*method
,
510 vector
<string
> super
)
512 string fullname
= method
->getName();
513 int num_params
= method
->getNumParams();
517 first
= is_static(method
) ? 0 : 1;
520 for (int i
= first
; i
< num_params
; ++i
) {
523 ParmVarDecl
*param
= method
->getParamDecl(i
);
524 if (is_isl_type(param
->getOriginalType())) {
526 type
= extract_type(param
->getOriginalType());
527 type
= type2python(type
);
528 printf("arg%d.__class__ is %s", i
, type
.c_str());
530 printf("type(arg%d) == str", i
);
533 printf(" res = isl.%s(", fullname
.c_str());
534 print_arg_in_call(method
, 0, 0);
535 for (int i
= 1; i
< num_params
; ++i
) {
537 print_arg_in_call(method
, i
, 0);
540 type
= type2python(extract_type(method
->getReturnType()));
541 printf(" return %s(ctx=arg0.ctx, ptr=res)\n", type
.c_str());
544 /* Print a python method with a name derived from "fullname"
545 * corresponding to the C functions "methods".
546 * "super" contains the superclasses of the class to which the method belongs.
548 * If "methods" consists of a single element that is not marked overloaded,
549 * the use print_method to print the method.
550 * Otherwise, print an overloaded method with pieces corresponding
551 * to each function in "methods".
553 void isl_class::print_method(const string
&fullname
,
554 const set
<FunctionDecl
*> &methods
, vector
<string
> super
)
557 set
<FunctionDecl
*>::const_iterator it
;
559 FunctionDecl
*any_method
;
561 any_method
= *methods
.begin();
562 if (methods
.size() == 1 && !is_overload(any_method
)) {
563 print_method(any_method
, super
);
567 cname
= fullname
.substr(name
.length() + 1);
568 num_params
= any_method
->getNumParams();
570 print_method_header(is_static(any_method
), cname
, num_params
);
572 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
573 print_method_overload(*it
, super
);
576 /* Print part of the constructor for this isl_class.
578 * In particular, check if the actual arguments correspond to the
579 * formal arguments of "cons" and if so call "cons" and put the
580 * result in self.ptr and a reference to the default context in self.ctx.
582 * If the function consumes a reference, then we pass it a copy of
583 * the actual argument.
585 void isl_class::print_constructor(FunctionDecl
*cons
)
587 string fullname
= cons
->getName();
588 string cname
= fullname
.substr(name
.length() + 1);
589 int num_params
= cons
->getNumParams();
590 int drop_ctx
= first_arg_is_isl_ctx(cons
);
592 printf(" if len(args) == %d", num_params
- drop_ctx
);
593 for (int i
= drop_ctx
; i
< num_params
; ++i
) {
594 ParmVarDecl
*param
= cons
->getParamDecl(i
);
595 QualType type
= param
->getOriginalType();
596 if (is_isl_type(type
)) {
598 s
= type2python(extract_type(type
));
599 printf(" and args[%d].__class__ is %s",
600 i
- drop_ctx
, s
.c_str());
601 } else if (type
->isPointerType()) {
602 printf(" and type(args[%d]) == str", i
- drop_ctx
);
604 printf(" and type(args[%d]) == int", i
- drop_ctx
);
608 printf(" self.ctx = Context.getDefaultInstance()\n");
609 printf(" self.ptr = isl.%s(", fullname
.c_str());
612 for (int i
= drop_ctx
; i
< num_params
; ++i
) {
613 ParmVarDecl
*param
= cons
->getParamDecl(i
);
616 if (is_isl_type(param
->getOriginalType())) {
619 type
= extract_type(param
->getOriginalType());
620 printf("isl.%s_copy(args[%d].ptr)",
621 type
.c_str(), i
- drop_ctx
);
623 printf("args[%d].ptr", i
- drop_ctx
);
625 printf("args[%d]", i
- drop_ctx
);
631 /* Print the header of the class "name" with superclasses "super".
633 static void print_class_header(const string
&name
, const vector
<string
> &super
)
635 printf("class %s", name
.c_str());
636 if (super
.size() > 0) {
638 for (unsigned i
= 0; i
< super
.size(); ++i
) {
641 printf("%s", type2python(super
[i
]).c_str());
648 /* Tell ctypes about the return type of "fd".
649 * In particular, if "fd" returns a pointer to an isl object,
650 * then tell ctypes it returns a "c_void_p".
651 * Similarly, if "fd" returns an isl_bool,
652 * then tell ctypes it returns a "c_bool".
654 static void print_restype(FunctionDecl
*fd
)
656 string fullname
= fd
->getName();
657 QualType type
= fd
->getReturnType();
658 if (is_isl_type(type
))
659 printf("isl.%s.restype = c_void_p\n", fullname
.c_str());
660 else if (is_isl_bool(type
))
661 printf("isl.%s.restype = c_bool\n", fullname
.c_str());
664 /* Tell ctypes about the types of the arguments of the function "fd".
666 static void print_argtypes(FunctionDecl
*fd
)
668 string fullname
= fd
->getName();
669 int n
= fd
->getNumParams();
672 printf("isl.%s.argtypes = [", fullname
.c_str());
673 for (int i
= 0; i
< n
- drop_user
; ++i
) {
674 ParmVarDecl
*param
= fd
->getParamDecl(i
);
675 QualType type
= param
->getOriginalType();
676 if (is_callback(type
))
680 if (is_isl_ctx(type
))
682 else if (is_isl_type(type
) || is_callback(type
))
684 else if (is_string(type
))
690 printf(", c_void_p");
694 /* Print out the definition of this isl_class.
696 * We first check if this isl_class is a subclass of one or more other classes.
697 * If it is, we make sure those superclasses are printed out first.
699 * Then we print a constructor with several cases, one for constructing
700 * a Python object from a return value and one for each function that
701 * was marked as a constructor.
703 * Next, we print out some common methods and the methods corresponding
704 * to functions that are not marked as constructors.
706 * Finally, we tell ctypes about the types of the arguments of the
707 * constructor functions and the return types of those function returning
710 void isl_class::print(map
<string
, isl_class
> &classes
, set
<string
> &done
)
712 string p_name
= type2python(name
);
713 set
<FunctionDecl
*>::iterator in
;
714 map
<string
, set
<FunctionDecl
*> >::iterator it
;
715 vector
<string
> super
= find_superclasses(type
);
717 for (unsigned i
= 0; i
< super
.size(); ++i
)
718 if (done
.find(super
[i
]) == done
.end())
719 classes
[super
[i
]].print(classes
, done
);
723 print_class_header(p_name
, super
);
724 printf(" def __init__(self, *args, **keywords):\n");
726 printf(" if \"ptr\" in keywords:\n");
727 printf(" self.ctx = keywords[\"ctx\"]\n");
728 printf(" self.ptr = keywords[\"ptr\"]\n");
731 for (in
= constructors
.begin(); in
!= constructors
.end(); ++in
)
732 print_constructor(*in
);
733 printf(" raise Error\n");
734 printf(" def __del__(self):\n");
735 printf(" if hasattr(self, 'ptr'):\n");
736 printf(" isl.%s_free(self.ptr)\n", name
.c_str());
737 printf(" def __str__(self):\n");
738 printf(" ptr = isl.%s_to_str(self.ptr)\n", name
.c_str());
739 printf(" res = str(cast(ptr, c_char_p).value)\n");
740 printf(" libc.free(ptr)\n");
741 printf(" return res\n");
742 printf(" def __repr__(self):\n");
743 printf(" s = str(self)\n");
744 printf(" if '\"' in s:\n");
745 printf(" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
748 printf(" return 'isl.%s(\"%%s\")' %% s\n",
751 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
752 print_method(it
->first
, it
->second
, super
);
755 for (in
= constructors
.begin(); in
!= constructors
.end(); ++in
) {
759 for (it
= methods
.begin(); it
!= methods
.end(); ++it
)
760 for (in
= it
->second
.begin(); in
!= it
->second
.end(); ++in
) {
764 printf("isl.%s_free.argtypes = [c_void_p]\n", name
.c_str());
765 printf("isl.%s_to_str.argtypes = [c_void_p]\n", name
.c_str());
766 printf("isl.%s_to_str.restype = POINTER(c_char)\n", name
.c_str());
769 /* Generate a python interface based on the extracted types and functions.
770 * We first collect all functions that belong to a certain type,
771 * separating constructors from regular methods. If there are any
772 * overloaded functions, then they are grouped based on their name
773 * after removing the argument type suffix.
775 * Then we print out each class in turn. If one of these is a subclass
776 * of some other class, it will make sure the superclass is printed out first.
778 void generate_python(set
<RecordDecl
*> &types
, set
<FunctionDecl
*> functions
)
780 map
<string
, isl_class
> classes
;
781 map
<string
, isl_class
>::iterator ci
;
784 set
<RecordDecl
*>::iterator it
;
785 for (it
= types
.begin(); it
!= types
.end(); ++it
) {
786 RecordDecl
*decl
= *it
;
787 string name
= decl
->getName();
788 classes
[name
].name
= name
;
789 classes
[name
].type
= decl
;
792 set
<FunctionDecl
*>::iterator in
;
793 for (in
= functions
.begin(); in
!= functions
.end(); ++in
) {
794 isl_class
*c
= method2class(classes
, *in
);
797 if (is_constructor(*in
)) {
798 c
->constructors
.insert(*in
);
800 FunctionDecl
*method
= *in
;
801 string fullname
= method
->getName();
802 fullname
= drop_type_suffix(fullname
, method
);
803 c
->methods
[fullname
].insert(method
);
807 for (ci
= classes
.begin(); ci
!= classes
.end(); ++ci
) {
808 if (done
.find(ci
->first
) == done
.end())
809 ci
->second
.print(classes
, done
);