#ifndef ISL_INTERFACE_GENERATOR_H
#define ISL_INTERFACE_GENERATOR_H
#include <map>
#include <set>
#include <string>
#include <vector>
#include <clang/AST/Decl.h>
using namespace std;
using namespace clang;
inline int prefixcmp(const char *s, const char *prefix)
{
return strncmp(s, prefix, strlen(prefix));
}
struct set_enum {
int value;
string name;
string method_name;
set_enum(int value, string name, string method_name) :
value(value), name(name), method_name(method_name) {}
};
struct function_name_less {
bool operator()(FunctionDecl *x, FunctionDecl *y) const {
return x->getName() < y->getName();
}
};
typedef std::set<FunctionDecl *, function_name_less> function_set;
struct isl_class {
string name;
string superclass_name;
string subclass_name;
RecordDecl *type;
function_set constructors;
set<FunctionDecl *> persistent_callbacks;
map<FunctionDecl *, vector<set_enum> > set_enums;
map<string, function_set> methods;
map<int, string> type_subclasses;
FunctionDecl *fn_type;
FunctionDecl *fn_to_str;
FunctionDecl *fn_copy;
FunctionDecl *fn_free;
std::map<clang::FunctionDecl *, const isl_class &> copied_from;
std::map<clang::FunctionDecl *, int> copy_depth;
std::map<std::string, clang::ParmVarDecl *> construction_types;
bool first_arg_matches_class(FunctionDecl *method) const;
bool is_static(FunctionDecl *method) const;
bool is_type_subclass() const { return name != subclass_name; }
static string name_without_type_suffixes(FunctionDecl *fd);
string base_method_name(FunctionDecl *fd) const {
string m_name = name_without_type_suffixes(fd);
return m_name.substr(subclass_name.length() + 1);
}
static const char *get_prefix;
bool is_get_method_name(FunctionDecl *fd, const string &name) const;
bool is_get_method(FunctionDecl *fd) const {
return is_get_method_name(fd, base_method_name(fd));
}
string method_name(FunctionDecl *fd) const;
static const char *set_callback_prefix;
string persistent_callback_name(FunctionDecl *fd) const {
return method_name(fd).substr(strlen(set_callback_prefix));
}
bool has_persistent_callbacks() const {
return persistent_callbacks.size() != 0;
}
};
class generator {
protected:
SourceManager &SM;
map<string,isl_class> classes;
map<string, FunctionDecl *> functions_by_name;
public:
generator(SourceManager &SM, set<RecordDecl *> &exported_types,
set<FunctionDecl *> exported_functions,
set<FunctionDecl *> functions);
virtual void generate() = 0;
virtual ~generator() {};
protected:
void add_subclass(RecordDecl *decl, const string &name,
const string &sub_name);
void add_class(RecordDecl *decl);
void add_type_subclasses(FunctionDecl *method);
isl_class *method2class(FunctionDecl *fd);
bool callback_takes_argument(ParmVarDecl *param, int pos);
FunctionDecl *find_by_name(const string &name, bool required);
std::map<const Type *, ParmVarDecl *> conversions;
private:
static const std::set<std::string> automatic_conversion_functions;
void extract_automatic_conversion(FunctionDecl *fd);
void extract_class_automatic_conversions(const isl_class &clazz);
void extract_automatic_conversions();
public:
static std::string drop_suffix(const std::string &s,
const std::string &suffix);
static void die(const char *msg) __attribute__((noreturn));
static void die(string msg) __attribute__((noreturn));
static vector<string> find_superclasses(Decl *decl);
static bool is_subclass(FunctionDecl *decl);
static bool is_overload(Decl *decl);
static bool is_constructor(Decl *decl);
static bool takes(Decl *decl);
static bool keeps(Decl *decl);
static bool gives(Decl *decl);
static bool is_isl_ctx(QualType type);
static bool first_arg_is_isl_ctx(FunctionDecl *fd);
static bool is_isl_type(QualType type);
static bool is_isl_neg_error(QualType type);
static bool is_isl_bool(QualType type);
static bool is_isl_stat(QualType type);
static bool is_isl_size(QualType type);
static bool is_long(QualType type);
static bool is_callback(QualType type);
static bool is_callback_arg(FunctionDecl *fd, int i);
static bool is_string(QualType type);
static bool is_static(const isl_class &clazz, FunctionDecl *method);
static bool is_mutator(const isl_class &clazz, FunctionDecl *fd);
static string extract_type(QualType type);
static const FunctionProtoType *extract_prototype(QualType type);
static int prototype_n_args(QualType type);
static ParmVarDecl *persistent_callback_arg(FunctionDecl *fd);
};
#endif