34#include "isl_config.h"
58 return name.substr(4);
65 for (
int i = first; i <
n_arg; ++i) {
84 printf(
" @staticmethod\n");
111 printf(
"%*s", indent,
" ");
112 va_start(args, format);
113 vprintf(format, args);
129 const char *fmt,
int pos,
bool upcast,
const string &super,
130 const string &
name,
int n)
135 printf(
".__class__ is %s:\n",
type.c_str());
138 printf(
" = %s(",
type.c_str());
146 printf(
").%s(",
name.c_str());
147 for (
int i = 1; i <
n; ++i) {
168 FunctionDecl *method,
bool first_is_ctx,
int n,
169 const vector<string> &super)
171 for (
int i = first_is_ctx; i <
n; ++i) {
172 ParmVarDecl *param = method->getParamDecl(i);
178 if (!first_is_ctx && i > 0 && super.size() > 0)
180 i - first_is_ctx,
true,
184 i - first_is_ctx,
false,
"", cname, -1);
194 printf(
"isl.%s_copy", type_s.c_str());
215 QualType
type = param->getOriginalType();
217 QualType return_type =
fn->getReturnType();
218 unsigned n_arg =
fn->getNumArgs();
220 printf(
" exc_info = [None]\n");
221 printf(
" fn = CFUNCTYPE(");
226 for (
unsigned i = 0; i <
n_arg - 1; ++i) {
228 die(
"Argument has non-isl type");
229 printf(
", c_void_p");
231 printf(
", c_void_p)\n");
232 printf(
" def cb_func(");
233 for (
unsigned i = 0; i <
n_arg; ++i) {
236 printf(
"cb_arg%d", i);
239 for (
unsigned i = 0; i <
n_arg - 1; ++i) {
242 printf(
" cb_arg%d = %s(ctx=arg0.ctx, ptr=",
243 i, arg_type.c_str());
246 printf(
"(cb_arg%d))\n", i);
250 printf(
" arg%d(",
arg);
252 printf(
" res = arg%d(",
arg);
253 for (
unsigned i = 0; i <
n_arg - 1; ++i) {
256 printf(
"cb_arg%d", i);
259 printf(
" except BaseException as e:\n");
260 printf(
" exc_info[0] = e\n");
262 printf(
" return -1\n");
264 printf(
" return None\n");
266 printf(
" return 0\n");
268 printf(
" return 1 if res else 0\n");
272 printf(
"(res.ptr)\n");
274 printf(
" cb%d = fn(cb_func)\n",
arg);
299 ParmVarDecl *param = fd->getParamDecl(
arg);
300 QualType
type = param->getOriginalType();
304 printf(
"cb%d",
arg - skip);
305 }
else if (
takes(param)) {
308 printf(fmt,
arg - skip);
311 printf(fmt,
arg - skip);
312 printf(
".encode('ascii')");
313 }
else if (
type->isPointerType()) {
314 printf(fmt,
arg - skip);
317 printf(fmt,
arg - skip);
341 set<FunctionDecl *>::const_iterator in;
343 for (in = callbacks.begin(); in != callbacks.end(); ++in) {
356 printf(
".%s['exc_info'][0] = None\n",
callback_name.c_str());
390 FunctionDecl *method,
const char *fmt)
392 QualType return_type = method->getReturnType();
402 "obj = %s(ctx=ctx, ptr=res)\n",
type.c_str());
411 "'exc_info': exc_info }\n",
419 "cast(res, c_char_p).value.decode('ascii')\n");
449 int num_params = fd->getNumParams();
452 printf(
" return arg0.%s(",
name.c_str());
468 FunctionDecl *method,
const char *fmt,
int drop_ctx)
470 string fullname = method->getName().str();
471 int num_params = method->getNumParams();
475 print_indent(indent,
"ctx = Context.getDefaultInstance()\n");
481 print_indent(indent,
"res = isl.%s(", fullname.c_str());
482 for (
int i = 0; i < num_params; ++i) {
525 FunctionDecl *method, vector<string> super)
528 int num_params = method->getNumParams();
532 for (
int i = 1; i < num_params; ++i) {
538 num_params - drop_ctx - drop_user);
543 for (
int i = 1; i < num_params; ++i) {
544 ParmVarDecl *param = method->getParamDecl(i);
545 QualType
type = param->getOriginalType();
566 printf(
"args[%d].__class__ is %s", i, type_str.c_str());
567 }
else if (
type->isPointerType()) {
568 printf(
"type(args[%d]) == str", i);
570 printf(
"type(args[%d]) == int", i);
576static bool any(
const std::vector<bool> &vector)
578 return std::find(vector.begin(), vector.end(),
true) != vector.end();
600 FunctionDecl *fd,
int drop_ctx)
602 int num_params = fd->getNumParams();
605 std::vector<bool> convert(num_params);
607 printf(
" if len(args) == %d", num_params - drop_ctx);
608 for (
int i = first; i < num_params; ++i) {
609 ParmVarDecl *param = fd->getParamDecl(i);
610 QualType
type = param->getOriginalType();
611 const Type *ptr =
type.getTypePtr();
617 QualType type2 =
conversions.at(ptr)->getOriginalType();
632 for (
int i = first; i < num_params; ++i) {
634 ParmVarDecl *param = fd->getParamDecl(i);
637 if (!is_self && !convert[i])
641 i - drop_ctx,
false,
"",
"", -1);
654 FunctionDecl *method)
673 vector<string> super)
676 function_set::const_iterator it;
677 FunctionDecl *any_method;
679 any_method = *methods.begin();
680 if (methods.size() == 1 && !
is_overload(any_method)) {
688 printf(
"(*args):\n");
690 for (it = methods.begin(); it != methods.end(); ++it)
692 printf(
" raise Error\n");
708 FunctionDecl *fd,
int value,
const string &
name,
709 const vector<string> &super)
711 string fullname = fd->getName().str();
712 int num_params = fd->getNumParams();
717 printf(
" ctx = arg0.ctx\n");
718 printf(
" res = isl.%s(", fullname.c_str());
719 for (
int i = 0; i < num_params - 1; ++i) {
724 printf(
", %d", value);
738 FunctionDecl *fd,
const vector<string> &super)
740 vector<set_enum>::const_iterator it;
741 const vector<set_enum> &set_enums = clazz.
set_enums.at(fd);
743 for (it = set_enums.begin(); it != set_enums.end(); ++it)
756 string fullname = cons->getName().str();
758 int num_params = cons->getNumParams();
762 printf(
" self.ctx = Context.getDefaultInstance()\n");
763 printf(
" self.ptr = isl.%s(", fullname.c_str());
766 for (
int i = drop_ctx; i < num_params; ++i) {
794 if len(args) == 2 and type(args[0]) == str:
795 self.ctx = Context.getDefaultInstance()
796 name = args[0].encode('ascii')
797 self.ptr = isl.isl_id_alloc(self.ctx, name, args[1])
798 self.ptr = isl.isl_id_set_free_user(self.ptr, Context.free_user)
799 if self.ptr is not None:
800 pythonapi.Py_IncRef(py_object(args[1]))
811 if (clazz.
name !=
"isl_id")
832 free_user = cast(Context.free_user, c_void_p)
833 id_free_user = cast(isl.isl_id_get_free_user(self.ptr), c_void_p)
834 if id_free_user.value != free_user.value:
836 return isl.isl_id_get_user(self.ptr)
846 if (clazz.
name !=
"isl_id")
858 map<int, string>::const_iterator i;
865 printf(
" if len(args) == 1 and "
866 "isinstance(args[0], %s):\n",
868 printf(
" self.ctx = args[0].ctx\n");
869 printf(
" self.ptr = isl.%s_copy(args[0].ptr)\n",
882 const string &
name,
const vector<string> &super)
884 printf(
"class %s",
name.c_str());
885 if (super.size() > 0) {
887 for (
unsigned i = 0; i < super.size(); ++i) {
912 string fullname = fd->getName().str();
913 QualType
type = fd->getReturnType();
915 printf(
"isl.%s.restype = c_void_p\n", fullname.c_str());
917 printf(
"isl.%s.restype = POINTER(c_char)\n", fullname.c_str());
927 string fullname = fd->getName().str();
928 int n = fd->getNumParams();
930 printf(
"isl.%s.argtypes = [", fullname.c_str());
931 for (
int i = 0; i <
n; ++i) {
932 ParmVarDecl *param = fd->getParamDecl(i);
933 QualType
type = param->getOriginalType();
941 printf(
"c_void_p, c_void_p");
972 const string &python_name)
977 printf(
" def __new__(cls, *args, **keywords):\n");
980 map<int, string>::const_iterator i;
982 printf(
" if \"ptr\" in keywords:\n");
983 printf(
" type = isl.%s(keywords[\"ptr\"])\n",
984 clazz.
fn_type->getNameAsString().c_str());
988 printf(
" if type == %d:\n", i->first);
989 printf(
" return %s(**keywords)\n",
992 printf(
" raise Error\n");
995 printf(
" return super(%s, cls).__new__(cls)\n",
996 python_name.c_str());
1015 const string &python_name)
1020 printf(
" def __str__(arg0):\n");
1022 printf(
" ptr = isl.%s(arg0.ptr)\n",
1023 string(clazz.
fn_to_str->getName()).c_str());
1024 printf(
" res = cast(ptr, c_char_p).value.decode('ascii')\n");
1025 printf(
" libc.free(ptr)\n");
1026 printf(
" return res\n");
1027 printf(
" def __repr__(self):\n");
1028 printf(
" s = str(self)\n");
1029 printf(
" if '\"' in s:\n");
1030 printf(
" return 'isl.%s(\"\"\"%%s\"\"\")' %% s\n",
1031 python_name.c_str());
1033 printf(
" return 'isl.%s(\"%%s\")' %% s\n",
1034 python_name.c_str());
1044 set<FunctionDecl *>::const_iterator in;
1049 printf(
" def copy_callbacks(self, obj):\n");
1050 for (in = callbacks.begin(); in != callbacks.end(); ++in) {
1053 printf(
" if hasattr(obj, '%s'):\n",
1055 printf(
" self.%s = obj.%s\n",
1072 function_set::const_iterator in;
1073 map<string, function_set>::const_iterator it;
1074 map<FunctionDecl *, vector<set_enum> >::const_iterator ie;
1081 for (in = callbacks.begin(); in != callbacks.end(); ++in)
1083 for (it = clazz.
methods.begin(); it != clazz.
methods.end(); ++it)
1084 for (in = it->second.begin(); in != it->second.end(); ++in)
1122 for (
unsigned i = 0; i < super.size(); ++i)
1123 if (
done.find(super[i]) ==
done.end())
1131 printf(
" def __init__(self, *args, **keywords):\n");
1133 printf(
" if \"ptr\" in keywords:\n");
1134 printf(
" self.ctx = keywords[\"ctx\"]\n");
1135 printf(
" self.ptr = keywords[\"ptr\"]\n");
1136 printf(
" return\n");
1142 printf(
" raise Error\n");
1143 printf(
" def __del__(self):\n");
1144 printf(
" if hasattr(self, 'ptr'):\n");
1145 printf(
" isl.%s_free(self.ptr)\n", clazz.
name.c_str());
1152 for (
const auto &callback : callbacks)
1154 for (
const auto &kvp : clazz.
methods)
1173 map<string, isl_class>::iterator ci;
1176 if (
done.find(ci->first) ==
done.end())
static string extract_type(QualType type)
std::map< const Type *, ParmVarDecl * > conversions
bool callback_takes_argument(ParmVarDecl *param, int pos)
static bool is_overload(Decl *decl)
static bool is_callback(QualType type)
static bool is_isl_type(QualType type)
static bool takes(Decl *decl)
static bool is_isl_size(QualType type)
static bool gives(Decl *decl)
static bool is_isl_ctx(QualType type)
static bool is_mutator(const isl_class &clazz, FunctionDecl *fd)
static bool is_string(QualType type)
static bool is_static(const isl_class &clazz, FunctionDecl *method)
static bool is_callback_arg(FunctionDecl *fd, int i)
static bool is_isl_neg_error(QualType type)
static bool is_isl_bool(QualType type)
static bool is_long(QualType type)
static bool is_isl_stat(QualType type)
static bool first_arg_is_isl_ctx(FunctionDecl *fd)
map< string, isl_class > classes
static const FunctionProtoType * extract_prototype(QualType type)
static vector< string > find_superclasses(Decl *decl)
void print_upcast_constructors(const isl_class &clazz)
void print_argument_checks(const isl_class &clazz, FunctionDecl *fd, int drop_ctx)
void print_type_check(int indent, const string &type, const char *fmt, int pos, bool upcast, const string &super, const string &name, int n)
void print_set_enum(const isl_class &clazz, FunctionDecl *fd, int value, const string &name, const vector< string > &super)
void print_copy(QualType type)
void print_method_header(bool is_static, const string &name, int n_arg)
void print_representation(const isl_class &clazz, const string &python_name)
void print_method_call(int indent, const isl_class &clazz, FunctionDecl *method, const char *fmt, int drop_ctx)
void print_special_constructors(const isl_class &clazz)
void print_method_overload(const isl_class &clazz, FunctionDecl *method)
void print_type_checks(const string &cname, FunctionDecl *method, bool first_is_ctx, int n, const vector< string > &super)
void print_new(const isl_class &clazz, const string &python_name)
void print_get_method(const isl_class &clazz, FunctionDecl *fd)
void print(const isl_class &clazz)
void print_method_arguments(int first, int n_arg)
void print_arg_in_call(FunctionDecl *fd, const char *fmt, int arg, int skip)
void print_method(const isl_class &clazz, FunctionDecl *method, vector< string > super)
void print_copy_callbacks(const isl_class &clazz)
void print_constructor(const isl_class &clazz, FunctionDecl *method)
void print_method_types(const isl_class &clazz)
void print_method_return(int indent, const isl_class &clazz, FunctionDecl *method, const char *fmt)
void print_class_header(const isl_class &clazz, const string &name, const vector< string > &super)
void print_special_methods(const isl_class &clazz)
void print_method_type(FunctionDecl *fd)
void print_restype(FunctionDecl *fd)
void print_callback(ParmVarDecl *param, int arg)
void print_argtypes(FunctionDecl *fd)
std::set< FunctionDecl *, function_name_less > function_set
isl_stat isl_stat(* fn)(__isl_take ISL_KEY *key, __isl_take ISL_VAL *val, void *user)
static int n_arg(struct isl_arg *arg)
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
static void print_method_def(bool is_static, const string &name)
static void print_rethrow(int indent, const char *exc_info)
static const char * fixed_arg_fmt
static void print_persistent_callback_failure_check(int indent, const isl_class &clazz, const char *fmt)
static void print_argument_check(QualType type, int i)
static string type2python(string name)
static const char *const id_constructor_user
static void print_indent(int indent, const char *format,...)
static const char * var_arg_fmt
static const char *const id_user
static bool any(const std::vector< bool > &vector)
string base_method_name(FunctionDecl *fd) const
map< FunctionDecl *, vector< set_enum > > set_enums
map< string, function_set > methods
bool is_type_subclass() const
map< int, string > type_subclasses
bool has_persistent_callbacks() const
bool is_get_method(FunctionDecl *fd) const
string persistent_callback_name(FunctionDecl *fd) const
set< FunctionDecl * > persistent_callbacks
string method_name(FunctionDecl *fd) const
function_set constructors
const std::string callback_name(const Method &method)