Blame isl-0.14/interface/python.cc

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2011 Sven Verdoolaege. All rights reserved.
Packit fb9d21
 * 
Packit fb9d21
 * Redistribution and use in source and binary forms, with or without
Packit fb9d21
 * modification, are permitted provided that the following conditions
Packit fb9d21
 * are met:
Packit fb9d21
 * 
Packit fb9d21
 *    1. Redistributions of source code must retain the above copyright
Packit fb9d21
 *       notice, this list of conditions and the following disclaimer.
Packit fb9d21
 * 
Packit fb9d21
 *    2. Redistributions in binary form must reproduce the above
Packit fb9d21
 *       copyright notice, this list of conditions and the following
Packit fb9d21
 *       disclaimer in the documentation and/or other materials provided
Packit fb9d21
 *       with the distribution.
Packit fb9d21
 * 
Packit fb9d21
 * THIS SOFTWARE IS PROVIDED BY SVEN VERDOOLAEGE ''AS IS'' AND ANY
Packit fb9d21
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit fb9d21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Packit fb9d21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SVEN VERDOOLAEGE OR
Packit fb9d21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Packit fb9d21
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit fb9d21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
Packit fb9d21
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit fb9d21
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit fb9d21
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit fb9d21
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit fb9d21
 * 
Packit fb9d21
 * The views and conclusions contained in the software and documentation
Packit fb9d21
 * are those of the authors and should not be interpreted as
Packit fb9d21
 * representing official policies, either expressed or implied, of
Packit fb9d21
 * Sven Verdoolaege.
Packit fb9d21
 */ 
Packit fb9d21
Packit fb9d21
#include "isl_config.h"
Packit fb9d21
Packit fb9d21
#include <stdio.h>
Packit fb9d21
#include <iostream>
Packit fb9d21
#include <map>
Packit fb9d21
#include <clang/AST/Attr.h>
Packit fb9d21
#include "extract_interface.h"
Packit fb9d21
#include "python.h"
Packit fb9d21
Packit fb9d21
/* Is the given type declaration marked as being a subtype of some other
Packit fb9d21
 * type?  If so, return that other type in "super".
Packit fb9d21
 */
Packit fb9d21
static bool is_subclass(RecordDecl *decl, string &super)
Packit fb9d21
{
Packit fb9d21
	if (!decl->hasAttrs())
Packit fb9d21
		return false;
Packit fb9d21
Packit fb9d21
	string sub = "isl_subclass";
Packit fb9d21
	size_t len = sub.length();
Packit fb9d21
	AttrVec attrs = decl->getAttrs();
Packit fb9d21
	for (AttrVec::const_iterator i = attrs.begin() ; i != attrs.end(); ++i) {
Packit fb9d21
		const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
Packit fb9d21
		if (!ann)
Packit fb9d21
			continue;
Packit fb9d21
		string s = ann->getAnnotation().str();
Packit fb9d21
		if (s.substr(0, len) == sub) {
Packit fb9d21
			super = s.substr(len + 1, s.length() - len  - 2);
Packit fb9d21
			return true;
Packit fb9d21
		}
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return false;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is decl marked as a constructor?
Packit fb9d21
 */
Packit fb9d21
static bool is_constructor(Decl *decl)
Packit fb9d21
{
Packit fb9d21
	return has_annotation(decl, "isl_constructor");
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is decl marked as consuming a reference?
Packit fb9d21
 */
Packit fb9d21
static bool takes(Decl *decl)
Packit fb9d21
{
Packit fb9d21
	return has_annotation(decl, "isl_take");
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* isl_class collects all constructors and methods for an isl "class".
Packit fb9d21
 * "name" is the name of the class.
Packit fb9d21
 * "type" is the declaration that introduces the type.
Packit fb9d21
 */
Packit fb9d21
struct isl_class {
Packit fb9d21
	string name;
Packit fb9d21
	RecordDecl *type;
Packit fb9d21
	set<FunctionDecl *> constructors;
Packit fb9d21
	set<FunctionDecl *> methods;
Packit fb9d21
Packit fb9d21
	void print(map<string, isl_class> &classes, set<string> &done);
Packit fb9d21
	void print_constructor(FunctionDecl *method);
Packit fb9d21
	void print_method(FunctionDecl *method, bool subclass, string super);
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Return the class that has a name that matches the initial part
Packit fb9d21
 * of the namd of function "fd".
Packit fb9d21
 */
Packit fb9d21
static isl_class &method2class(map<string, isl_class> &classes,
Packit fb9d21
	FunctionDecl *fd)
Packit fb9d21
{
Packit fb9d21
	string best;
Packit fb9d21
	map<string, isl_class>::iterator ci;
Packit fb9d21
	string name = fd->getNameAsString();
Packit fb9d21
Packit fb9d21
	for (ci = classes.begin(); ci != classes.end(); ++ci) {
Packit fb9d21
		if (name.substr(0, ci->first.length()) == ci->first)
Packit fb9d21
			best = ci->first;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return classes[best];
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is "type" the type "isl_ctx *"?
Packit fb9d21
 */
Packit fb9d21
static bool is_isl_ctx(QualType type)
Packit fb9d21
{
Packit fb9d21
	if (!type->isPointerType())
Packit fb9d21
		return 0;
Packit fb9d21
	type = type->getPointeeType();
Packit fb9d21
	if (type.getAsString() != "isl_ctx")
Packit fb9d21
		return false;
Packit fb9d21
Packit fb9d21
	return true;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is the first argument of "fd" of type "isl_ctx *"?
Packit fb9d21
 */
Packit fb9d21
static bool first_arg_is_isl_ctx(FunctionDecl *fd)
Packit fb9d21
{
Packit fb9d21
	ParmVarDecl *param;
Packit fb9d21
Packit fb9d21
	if (fd->getNumParams() < 1)
Packit fb9d21
		return false;
Packit fb9d21
Packit fb9d21
	param = fd->getParamDecl(0);
Packit fb9d21
	return is_isl_ctx(param->getOriginalType());
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is "type" that of a pointer to an isl_* structure?
Packit fb9d21
 */
Packit fb9d21
static bool is_isl_type(QualType type)
Packit fb9d21
{
Packit fb9d21
	if (type->isPointerType()) {
Packit fb9d21
		string s = type->getPointeeType().getAsString();
Packit fb9d21
		return s.substr(0, 4) == "isl_";
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return false;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is "type" that of a pointer to a function?
Packit fb9d21
 */
Packit fb9d21
static bool is_callback(QualType type)
Packit fb9d21
{
Packit fb9d21
	if (!type->isPointerType())
Packit fb9d21
		return false;
Packit fb9d21
	type = type->getPointeeType();
Packit fb9d21
	return type->isFunctionType();
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Is "type" that of "char *" of "const char *"?
Packit fb9d21
 */
Packit fb9d21
static bool is_string(QualType type)
Packit fb9d21
{
Packit fb9d21
	if (type->isPointerType()) {
Packit fb9d21
		string s = type->getPointeeType().getAsString();
Packit fb9d21
		return s == "const char" || s == "char";
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return false;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the name of the type that "type" points to.
Packit fb9d21
 * The input "type" is assumed to be a pointer type.
Packit fb9d21
 */
Packit fb9d21
static string extract_type(QualType type)
Packit fb9d21
{
Packit fb9d21
	if (type->isPointerType())
Packit fb9d21
		return type->getPointeeType().getAsString();
Packit fb9d21
	assert(0);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Drop the "isl_" initial part of the type name "name".
Packit fb9d21
 */
Packit fb9d21
static string type2python(string name)
Packit fb9d21
{
Packit fb9d21
	return name.substr(4);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Construct a wrapper for a callback argument (at position "arg").
Packit fb9d21
 * Assign the wrapper to "cb".  We assume here that a function call
Packit fb9d21
 * has at most one callback argument.
Packit fb9d21
 *
Packit fb9d21
 * The wrapper converts the arguments of the callback to python types.
Packit fb9d21
 * If any exception is thrown, the wrapper keeps track of it in exc_info[0]
Packit fb9d21
 * and returns -1.  Otherwise the wrapper returns 0.
Packit fb9d21
 */
Packit fb9d21
static void print_callback(QualType type, int arg)
Packit fb9d21
{
Packit fb9d21
	const FunctionProtoType *fn = type->getAs<FunctionProtoType>();
Packit fb9d21
	unsigned n_arg = fn->getNumArgs();
Packit fb9d21
Packit fb9d21
	printf("        exc_info = [None]\n");
Packit fb9d21
	printf("        fn = CFUNCTYPE(c_int");
Packit fb9d21
	for (int i = 0; i < n_arg - 1; ++i) {
Packit fb9d21
		QualType arg_type = fn->getArgType(i);
Packit fb9d21
		assert(is_isl_type(arg_type));
Packit fb9d21
		printf(", c_void_p");
Packit fb9d21
	}
Packit fb9d21
	printf(", c_void_p)\n");
Packit fb9d21
	printf("        def cb_func(");
Packit fb9d21
	for (int i = 0; i < n_arg; ++i) {
Packit fb9d21
		if (i)
Packit fb9d21
			printf(", ");
Packit fb9d21
		printf("cb_arg%d", i);
Packit fb9d21
	}
Packit fb9d21
	printf("):\n");
Packit fb9d21
	for (int i = 0; i < n_arg - 1; ++i) {
Packit fb9d21
		string arg_type;
Packit fb9d21
		arg_type = type2python(extract_type(fn->getArgType(i)));
Packit fb9d21
		printf("            cb_arg%d = %s(ctx=arg0.ctx, ptr=cb_arg%d)\n",
Packit fb9d21
			i, arg_type.c_str(), i);
Packit fb9d21
	}
Packit fb9d21
	printf("            try:\n");
Packit fb9d21
	printf("                arg%d(", arg);
Packit fb9d21
	for (int i = 0; i < n_arg - 1; ++i) {
Packit fb9d21
		if (i)
Packit fb9d21
			printf(", ");
Packit fb9d21
		printf("cb_arg%d", i);
Packit fb9d21
	}
Packit fb9d21
	printf(")\n");
Packit fb9d21
	printf("            except:\n");
Packit fb9d21
	printf("                import sys\n");
Packit fb9d21
	printf("                exc_info[0] = sys.exc_info()\n");
Packit fb9d21
	printf("                return -1\n");
Packit fb9d21
	printf("            return 0\n");
Packit fb9d21
	printf("        cb = fn(cb_func)\n");
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Print a python method corresponding to the C function "method".
Packit fb9d21
 * "subclass" is set if the method belongs to a class that is a subclass
Packit fb9d21
 * of some other class ("super").
Packit fb9d21
 *
Packit fb9d21
 * If the function has a callback argument, then it also has a "user"
Packit fb9d21
 * argument.  Since Python has closures, there is no need for such
Packit fb9d21
 * a user argument in the Python interface, so we simply drop it.
Packit fb9d21
 * We also create a wrapper ("cb") for the callback.
Packit fb9d21
 *
Packit fb9d21
 * For each argument of the function that refers to an isl structure,
Packit fb9d21
 * including the object on which the method is called,
Packit fb9d21
 * we check if the corresponding actual argument is of the right type.
Packit fb9d21
 * If not, we try to convert it to the right type.
Packit fb9d21
 * It that doesn't work and if subclass is set, we try to convert self
Packit fb9d21
 * to the type of the superclass and call the corresponding method.
Packit fb9d21
 *
Packit fb9d21
 * If the function consumes a reference, then we pass it a copy of
Packit fb9d21
 * the actual argument.
Packit fb9d21
 */
Packit fb9d21
void isl_class::print_method(FunctionDecl *method, bool subclass, string super)
Packit fb9d21
{
Packit fb9d21
	string fullname = method->getName();
Packit fb9d21
	string cname = fullname.substr(name.length() + 1);
Packit fb9d21
	int num_params = method->getNumParams();
Packit fb9d21
	int drop_user = 0;
Packit fb9d21
Packit fb9d21
	for (int i = 1; i < num_params; ++i) {
Packit fb9d21
		ParmVarDecl *param = method->getParamDecl(i);
Packit fb9d21
		QualType type = param->getOriginalType();
Packit fb9d21
		if (is_callback(type))
Packit fb9d21
			drop_user = 1;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	printf("    def %s(arg0", cname.c_str());
Packit fb9d21
	for (int i = 1; i < num_params - drop_user; ++i)
Packit fb9d21
		printf(", arg%d", i);
Packit fb9d21
	printf("):\n");
Packit fb9d21
Packit fb9d21
	for (int i = 0; i < num_params; ++i) {
Packit fb9d21
		ParmVarDecl *param = method->getParamDecl(i);
Packit fb9d21
		string type;
Packit fb9d21
		if (!is_isl_type(param->getOriginalType()))
Packit fb9d21
			continue;
Packit fb9d21
		type = type2python(extract_type(param->getOriginalType()));
Packit fb9d21
		printf("        try:\n");
Packit fb9d21
		printf("            if not arg%d.__class__ is %s:\n",
Packit fb9d21
			i, type.c_str());
Packit fb9d21
		printf("                arg%d = %s(arg%d)\n",
Packit fb9d21
			i, type.c_str(), i);
Packit fb9d21
		printf("        except:\n");
Packit fb9d21
		if (i > 0 && subclass) {
Packit fb9d21
			printf("            return %s(arg0).%s(",
Packit fb9d21
				type2python(super).c_str(), cname.c_str());
Packit fb9d21
			for (int i = 1; i < num_params - drop_user; ++i) {
Packit fb9d21
				if (i != 1)
Packit fb9d21
					printf(", ");
Packit fb9d21
				printf("arg%d", i);
Packit fb9d21
			}
Packit fb9d21
			printf(")\n");
Packit fb9d21
		} else
Packit fb9d21
			printf("            raise\n");
Packit fb9d21
	}
Packit fb9d21
	for (int i = 1; i < num_params; ++i) {
Packit fb9d21
		ParmVarDecl *param = method->getParamDecl(i);
Packit fb9d21
		QualType type = param->getOriginalType();
Packit fb9d21
		if (!is_callback(type))
Packit fb9d21
			continue;
Packit fb9d21
		print_callback(type->getPointeeType(), i);
Packit fb9d21
	}
Packit fb9d21
	printf("        res = isl.%s(", fullname.c_str());
Packit fb9d21
	if (takes(method->getParamDecl(0)))
Packit fb9d21
		printf("isl.%s_copy(arg0.ptr)", name.c_str());
Packit fb9d21
	else
Packit fb9d21
		printf("arg0.ptr");
Packit fb9d21
	for (int i = 1; i < num_params - drop_user; ++i) {
Packit fb9d21
		ParmVarDecl *param = method->getParamDecl(i);
Packit fb9d21
		QualType type = param->getOriginalType();
Packit fb9d21
		if (is_callback(type))
Packit fb9d21
			printf(", cb");
Packit fb9d21
		else if (takes(param)) {
Packit fb9d21
			string type_s = extract_type(type);
Packit fb9d21
			printf(", isl.%s_copy(arg%d.ptr)", type_s.c_str(), i);
Packit fb9d21
		} else
Packit fb9d21
			printf(", arg%d.ptr", i);
Packit fb9d21
	}
Packit fb9d21
	if (drop_user)
Packit fb9d21
		printf(", None");
Packit fb9d21
	printf(")\n");
Packit fb9d21
Packit fb9d21
	if (is_isl_type(method->getReturnType())) {
Packit fb9d21
		string type;
Packit fb9d21
		type = type2python(extract_type(method->getReturnType()));
Packit fb9d21
		printf("        return %s(ctx=arg0.ctx, ptr=res)\n",
Packit fb9d21
			type.c_str());
Packit fb9d21
	} else {
Packit fb9d21
		if (drop_user) {
Packit fb9d21
			printf("        if exc_info[0] != None:\n");
Packit fb9d21
			printf("            raise exc_info[0][0], "
Packit fb9d21
				"exc_info[0][1], exc_info[0][2]\n");
Packit fb9d21
		}
Packit fb9d21
		printf("        return res\n");
Packit fb9d21
	}
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Print part of the constructor for this isl_class.
Packit fb9d21
 *
Packit fb9d21
 * In particular, check if the actual arguments correspond to the
Packit fb9d21
 * formal arguments of "cons" and if so call "cons" and put the
Packit fb9d21
 * result in self.ptr and a reference to the default context in self.ctx.
Packit fb9d21
 *
Packit fb9d21
 * If the function consumes a reference, then we pass it a copy of
Packit fb9d21
 * the actual argument.
Packit fb9d21
 */
Packit fb9d21
void isl_class::print_constructor(FunctionDecl *cons)
Packit fb9d21
{
Packit fb9d21
	string fullname = cons->getName();
Packit fb9d21
	string cname = fullname.substr(name.length() + 1);
Packit fb9d21
	int num_params = cons->getNumParams();
Packit fb9d21
	int drop_ctx = first_arg_is_isl_ctx(cons);
Packit fb9d21
Packit fb9d21
	printf("        if len(args) == %d", num_params - drop_ctx);
Packit fb9d21
	for (int i = drop_ctx; i < num_params; ++i) {
Packit fb9d21
		ParmVarDecl *param = cons->getParamDecl(i);
Packit fb9d21
		if (is_isl_type(param->getOriginalType())) {
Packit fb9d21
			string type;
Packit fb9d21
			type = extract_type(param->getOriginalType());
Packit fb9d21
			type = type2python(type);
Packit fb9d21
			printf(" and args[%d].__class__ is %s",
Packit fb9d21
				i - drop_ctx, type.c_str());
Packit fb9d21
		} else
Packit fb9d21
			printf(" and type(args[%d]) == str", i - drop_ctx);
Packit fb9d21
	}
Packit fb9d21
	printf(":\n");
Packit fb9d21
	printf("            self.ctx = Context.getDefaultInstance()\n");
Packit fb9d21
	printf("            self.ptr = isl.%s(", fullname.c_str());
Packit fb9d21
	if (drop_ctx)
Packit fb9d21
		printf("self.ctx");
Packit fb9d21
	for (int i = drop_ctx; i < num_params; ++i) {
Packit fb9d21
		ParmVarDecl *param = cons->getParamDecl(i);
Packit fb9d21
		if (i)
Packit fb9d21
			printf(", ");
Packit fb9d21
		if (is_isl_type(param->getOriginalType())) {
Packit fb9d21
			if (takes(param)) {
Packit fb9d21
				string type;
Packit fb9d21
				type = extract_type(param->getOriginalType());
Packit fb9d21
				printf("isl.%s_copy(args[%d].ptr)",
Packit fb9d21
					type.c_str(), i - drop_ctx);
Packit fb9d21
			} else
Packit fb9d21
				printf("args[%d].ptr", i - drop_ctx);
Packit fb9d21
		} else
Packit fb9d21
			printf("args[%d]", i - drop_ctx);
Packit fb9d21
	}
Packit fb9d21
	printf(")\n");
Packit fb9d21
	printf("            return\n");
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Print out the definition of this isl_class.
Packit fb9d21
 *
Packit fb9d21
 * We first check if this isl_class is a subclass of some other class.
Packit fb9d21
 * If it is, we make sure the superclass is printed out first.
Packit fb9d21
 *
Packit fb9d21
 * Then we print a constructor with several cases, one for constructing
Packit fb9d21
 * a Python object from a return value and one for each function that
Packit fb9d21
 * was marked as a constructor.
Packit fb9d21
 *
Packit fb9d21
 * Next, we print out some common methods and the methods corresponding
Packit fb9d21
 * to functions that are not marked as constructors.
Packit fb9d21
 *
Packit fb9d21
 * Finally, we tell ctypes about the types of the arguments of the
Packit fb9d21
 * constructor functions and the return types of those function returning
Packit fb9d21
 * an isl object.
Packit fb9d21
 */
Packit fb9d21
void isl_class::print(map<string, isl_class> &classes, set<string> &done)
Packit fb9d21
{
Packit fb9d21
	string super;
Packit fb9d21
	string p_name = type2python(name);
Packit fb9d21
	set<FunctionDecl *>::iterator in;
Packit fb9d21
	bool subclass = is_subclass(type, super);
Packit fb9d21
Packit fb9d21
	if (subclass && done.find(super) == done.end())
Packit fb9d21
		classes[super].print(classes, done);
Packit fb9d21
	done.insert(name);
Packit fb9d21
Packit fb9d21
	printf("\n");
Packit fb9d21
	printf("class %s", p_name.c_str());
Packit fb9d21
	if (subclass)
Packit fb9d21
		printf("(%s)", type2python(super).c_str());
Packit fb9d21
	printf(":\n");
Packit fb9d21
	printf("    def __init__(self, *args, **keywords):\n");
Packit fb9d21
Packit fb9d21
	printf("        if \"ptr\" in keywords:\n");
Packit fb9d21
	printf("            self.ctx = keywords[\"ctx\"]\n");
Packit fb9d21
	printf("            self.ptr = keywords[\"ptr\"]\n");
Packit fb9d21
	printf("            return\n");
Packit fb9d21
Packit fb9d21
	for (in = constructors.begin(); in != constructors.end(); ++in)
Packit fb9d21
		print_constructor(*in);
Packit fb9d21
	printf("        raise Error\n");
Packit fb9d21
	printf("    def __del__(self):\n");
Packit fb9d21
	printf("        if hasattr(self, 'ptr'):\n");
Packit fb9d21
	printf("            isl.%s_free(self.ptr)\n", name.c_str());
Packit fb9d21
	printf("    def __str__(self):\n");
Packit fb9d21
	printf("        ptr = isl.%s_to_str(self.ptr)\n", name.c_str());
Packit fb9d21
	printf("        res = str(cast(ptr, c_char_p).value)\n");
Packit fb9d21
	printf("        libc.free(ptr)\n");
Packit fb9d21
	printf("        return res\n");
Packit fb9d21
	printf("    def __repr__(self):\n");
Packit fb9d21
	printf("        return 'isl.%s(\"%%s\")' %% str(self)\n", p_name.c_str());
Packit fb9d21
Packit fb9d21
	for (in = methods.begin(); in != methods.end(); ++in)
Packit fb9d21
		print_method(*in, subclass, super);
Packit fb9d21
Packit fb9d21
	printf("\n");
Packit fb9d21
	for (in = constructors.begin(); in != constructors.end(); ++in) {
Packit fb9d21
		string fullname = (*in)->getName();
Packit fb9d21
		printf("isl.%s.restype = c_void_p\n", fullname.c_str());
Packit fb9d21
		printf("isl.%s.argtypes = [", fullname.c_str());
Packit fb9d21
		for (int i = 0; i < (*in)->getNumParams(); ++i) {
Packit fb9d21
			ParmVarDecl *param = (*in)->getParamDecl(i);
Packit fb9d21
			QualType type = param->getOriginalType();
Packit fb9d21
			if (i)
Packit fb9d21
				printf(", ");
Packit fb9d21
			if (is_isl_ctx(type))
Packit fb9d21
				printf("Context");
Packit fb9d21
			else if (is_isl_type(type))
Packit fb9d21
				printf("c_void_p");
Packit fb9d21
			else if (is_string(type))
Packit fb9d21
				printf("c_char_p");
Packit fb9d21
			else
Packit fb9d21
				printf("c_int");
Packit fb9d21
		}
Packit fb9d21
		printf("]\n");
Packit fb9d21
	}
Packit fb9d21
	for (in = methods.begin(); in != methods.end(); ++in) {
Packit fb9d21
		string fullname = (*in)->getName();
Packit fb9d21
		if (is_isl_type((*in)->getReturnType()))
Packit fb9d21
			printf("isl.%s.restype = c_void_p\n", fullname.c_str());
Packit fb9d21
	}
Packit fb9d21
	printf("isl.%s_free.argtypes = [c_void_p]\n", name.c_str());
Packit fb9d21
	printf("isl.%s_to_str.argtypes = [c_void_p]\n", name.c_str());
Packit fb9d21
	printf("isl.%s_to_str.restype = POINTER(c_char)\n", name.c_str());
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Generate a python interface based on the extracted types and functions.
Packit fb9d21
 * We first collect all functions that belong to a certain type,
Packit fb9d21
 * separating constructors from regular methods.
Packit fb9d21
 *
Packit fb9d21
 * Then we print out each class in turn.  If one of these is a subclass
Packit fb9d21
 * of some other class, it will make sure the superclass is printed out first.
Packit fb9d21
 */
Packit fb9d21
void generate_python(set<RecordDecl *> &types, set<FunctionDecl *> functions)
Packit fb9d21
{
Packit fb9d21
	map<string, isl_class> classes;
Packit fb9d21
	map<string, isl_class>::iterator ci;
Packit fb9d21
	set<string> done;
Packit fb9d21
Packit fb9d21
	set<RecordDecl *>::iterator it;
Packit fb9d21
	for (it = types.begin(); it != types.end(); ++it) {
Packit fb9d21
		RecordDecl *decl = *it;
Packit fb9d21
		string name = decl->getName();
Packit fb9d21
		classes[name].name = name;
Packit fb9d21
		classes[name].type = decl;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	set<FunctionDecl *>::iterator in;
Packit fb9d21
	for (in = functions.begin(); in != functions.end(); ++in) {
Packit fb9d21
		isl_class &c = method2class(classes, *in);
Packit fb9d21
		if (is_constructor(*in))
Packit fb9d21
			c.constructors.insert(*in);
Packit fb9d21
		else
Packit fb9d21
			c.methods.insert(*in);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	for (ci = classes.begin(); ci != classes.end(); ++ci) {
Packit fb9d21
		if (done.find(ci->first) == done.end())
Packit fb9d21
			ci->second.print(classes, done);
Packit fb9d21
	}
Packit fb9d21
}