39#include <clang/AST/Attr.h>
40#include <clang/Basic/SourceManager.h>
42#include "isl_config.h"
56 if (method->getNumParams() < 1)
59 param = method->getParamDecl(0);
60 type = param->getOriginalType();
96 QualType
type, return_type;
98 if (fd->getNumParams() < 1)
105 param = fd->getParamDecl(0);
108 type = param->getOriginalType();
109 return_type = fd->getReturnType();
110 return return_type ==
type;
119 map<string, FunctionDecl *>::iterator i;
125 die(
"No " +
name +
" function found");
133 "isl_id_read_from_str",
134 "isl_val_int_from_si",
147 QualType return_type = fd->getReturnType();
148 const Type *
type = return_type.getTypePtr();
150 if (fd->getNumParams() != 2)
151 die(
"Expecting two arguments");
152 if (!
is_isl_ctx(fd->getParamDecl(0)->getOriginalType()))
153 die(
"Expecting isl_ctx first argument");
155 die(
"Expecting isl object return type");
168 function_set::iterator fi;
170 for (fi = constructors.begin(); fi != constructors.end(); ++fi) {
171 FunctionDecl *fd = *fi;
172 string name = fd->getName().str();
183 map<string, isl_class>::iterator ci;
197 const string &sub_name)
199 string name = decl->getName().str();
202 classes[sub_name].superclass_name = super_name;
203 classes[sub_name].subclass_name = sub_name;
227 QualType return_type = fn_type->getReturnType();
228 const EnumType *enum_type = return_type->getAs<EnumType>();
229 EnumDecl *decl = enum_type->getDecl();
231 DeclContext::decl_iterator i;
234 for (i = decl->decls_begin(); i != decl->decls_end(); ++i) {
235 EnumConstantDecl *ecd = dyn_cast<EnumConstantDecl>(*i);
236 int val = (int) ecd->getInitVal().getSExtValue();
237 string name = ecd->getNameAsString();
256 DeclContext::decl_iterator i;
258 for (i = decl->decls_begin(); i != decl->decls_end(); ++i) {
259 EnumConstantDecl *ecd = dyn_cast<EnumConstantDecl>(*i);
260 int val = (int) ecd->getInitVal().getSExtValue();
261 string name = ecd->getNameAsString();
266 method_name = prefix +
name.substr(4);
283 const EnumType *enum_type;
292 n = fd->getNumParams();
295 param = fd->getParamDecl(
n - 1);
296 enum_type = param->getType()->getAs<EnumType>();
299 decl = enum_type->getDecl();
300 enum_name = decl->getName().str();
301 enum_name = enum_name.substr(4);
303 pos = fd_name.find(enum_name);
304 if (
pos == std::string::npos)
306 prefix = fd_name.substr(0,
pos);
319 return fd->getParamDecl(1);
335 if (fd->getNumParams() != 3)
350 n = fd->getNumParams();
351 for (
unsigned i = 0; i <
n; ++i) {
352 ParmVarDecl *param = fd->getParamDecl(i);
353 if (param->getType()->getAs<EnumType>())
362static bool less_name(
const FunctionDecl *
a,
const FunctionDecl *
b)
364 return a->getName().size() <
b->getName().size();
386 set<FunctionDecl *> exported_functions, set<FunctionDecl *> functions) :
389 set<FunctionDecl *>::iterator in;
390 set<RecordDecl *>::iterator it;
391 vector<FunctionDecl *> type_subclasses;
392 vector<FunctionDecl *>::iterator iv;
394 for (in = functions.begin(); in != functions.end(); ++in) {
395 FunctionDecl *decl = *in;
399 for (it = exported_types.begin(); it != exported_types.end(); ++it)
402 for (in = exported_functions.begin(); in != exported_functions.end();
405 type_subclasses.push_back(*in);
407 std::sort(type_subclasses.begin(), type_subclasses.end(), &
less_name);
408 for (iv = type_subclasses.begin(); iv != type_subclasses.end(); ++iv) {
412 for (in = exported_functions.begin(); in != exported_functions.end();
414 FunctionDecl *method = *in;
429 std::string
name = method->getName().str();
430 die(
name +
" has unhandled enum argument");
444 fprintf(stderr,
"%s\n", msg);
473 vector<string> super;
474 bool reversed =
false;
476 if (!decl->hasAttrs())
479 string sub =
"isl_subclass";
480 size_t len =
sub.length();
481 AttrVec attrs = decl->getAttrs();
482 for (AttrVec::const_iterator i = attrs.begin(); i != attrs.end(); ++i) {
483 const AnnotateAttr *ann = dyn_cast<AnnotateAttr>(*i);
486 string s = ann->getAnnotation().str();
487 if (s ==
"isl_export" && super.size() == 0)
489 if (s.substr(0, len) ==
sub) {
490 s = s.substr(len + 1, s.length() - len - 2);
494 super.insert(super.begin(), s);
549 map<string, isl_class>::iterator ci;
550 string name = fd->getNameAsString();
553 size_t len = ci->first.length();
554 if (len > best.length() &&
name.substr(0, len) == ci->first &&
560 cerr <<
"Unable to find class of " <<
name << endl;
571 if (!
type->isPointerType())
574 if (
type.getAsString() !=
"isl_ctx")
586 if (fd->getNumParams() < 1)
589 param = fd->getParamDecl(0);
601 static SourceLocation range_begin(
602 const std::pair<SourceLocation,SourceLocation> &
p) {
605 static SourceLocation range_begin(
const CharSourceRange &
range) {
606 return range.getBegin();
632 const char *s, *end, *next;
635 loc = param->getSourceRange().getBegin();
636 if (!
SM.getFileEntryForID(
SM.getFileID(
SM.getSpellingLoc(loc))))
637 loc = ClangAPI::range_begin(
SM.getImmediateExpansionRange(loc));
638 s =
SM.getCharacterData(loc);
640 die(
"No character data");
643 die(
"Cannot find function pointer");
644 s = strchr(s + 1,
'(');
646 die(
"Cannot find function pointer arguments");
647 end = strchr(s + 1,
')');
649 die(
"Cannot find end of function pointer arguments");
651 s = strchr(s + 1,
',');
653 die(
"Cannot find function pointer argument");
655 next = strchr(s + 1,
',');
656 if (next && next < end)
658 s = strchr(s + 1,
'_');
660 die(
"Cannot find function pointer argument annotation");
664 die(
"Cannot find function pointer argument annotation");
673 if (
type->isPointerType()) {
677 if (
type->isFunctionType())
679 s =
type.getAsString();
680 return s.substr(0, 4) ==
"isl_";
700 if (
type->isPointerType())
703 s =
type.getAsString();
732 if (!
type->isPointerType())
735 return type->isFunctionType();
742 ParmVarDecl *param = fd->getParamDecl(i);
743 QualType
type = param->getOriginalType();
752 if (
type->isPointerType()) {
753 string s =
type->getPointeeType().getAsString();
754 return s ==
"const char" || s ==
"char";
764 const BuiltinType *builtin =
type->getAs<BuiltinType>();
765 return builtin && builtin->getKind() == BuiltinType::Long;
772 const BuiltinType *builtin =
type->getAs<BuiltinType>();
773 return builtin && builtin->getKind() == BuiltinType::UInt;
781 if (
type->isPointerType())
782 return type->getPointeeType().getAsString();
783 die(
"Cannot extract type from non-pointer type");
791 return type->getPointeeType()->getAs<FunctionProtoType>();
813 type = param->getOriginalType();
825 const std::string &suffix)
827 size_t len, suffix_len;
830 suffix_len = suffix.length();
832 if (len >= suffix_len && s.substr(len - suffix_len) == suffix)
833 return s.substr(0, len - suffix_len);
850 name = method->getName().str();
854 num_params = method->getNumParams();
855 for (
int i = num_params - 1; i >= 0; --i) {
859 param = method->getParamDecl(i);
static bool keeps(Decl *decl)
static string extract_type(QualType type)
void add_subclass(RecordDecl *decl, const string &name, const string &sub_name)
static void die(const char *msg) __attribute__((noreturn))
std::map< const Type *, ParmVarDecl * > conversions
bool callback_takes_argument(ParmVarDecl *param, int pos)
static bool is_overload(Decl *decl)
static bool is_subclass(FunctionDecl *decl)
static bool is_callback(QualType type)
FunctionDecl * find_by_name(const string &name, bool required)
static bool is_isl_type(QualType type)
void extract_automatic_conversions()
void add_class(RecordDecl *decl)
static bool takes(Decl *decl)
static ParmVarDecl * persistent_callback_arg(FunctionDecl *fd)
static int prototype_n_args(QualType type)
static bool is_isl_size(QualType type)
void extract_class_automatic_conversions(const isl_class &clazz)
static bool gives(Decl *decl)
isl_class * method2class(FunctionDecl *fd)
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)
map< string, FunctionDecl * > functions_by_name
void extract_automatic_conversion(FunctionDecl *fd)
static bool is_callback_arg(FunctionDecl *fd, int i)
static bool is_isl_neg_error(QualType type)
static std::string drop_suffix(const std::string &s, const std::string &suffix)
static const std::set< std::string > automatic_conversion_functions
generator(SourceManager &SM, set< RecordDecl * > &exported_types, set< FunctionDecl * > exported_functions, set< FunctionDecl * > functions)
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)
void add_type_subclasses(FunctionDecl *method)
map< string, isl_class > classes
static bool is_constructor(Decl *decl)
static const FunctionProtoType * extract_prototype(QualType type)
static vector< string > find_superclasses(Decl *decl)
static bool handled_sets_enum(isl_class *c, FunctionDecl *fd)
static std::string type_suffix(ParmVarDecl *param)
static bool sets_persistent_callback(isl_class *c, FunctionDecl *fd)
static bool is_isl_primitive(QualType type, const char *name)
static bool is_unsigned_int(QualType type)
static bool takes_enums(FunctionDecl *fd)
static bool less_name(const FunctionDecl *a, const FunctionDecl *b)
static void add_set_enum(isl_class *c, const string &prefix, EnumDecl *decl, FunctionDecl *fd)
std::set< FunctionDecl *, function_name_less > function_set
int prefixcmp(const char *s, const char *prefix)
__isl_export __isl_give ISL_HMAP __isl_take ISL_KEY __isl_take ISL_VAL * val
void GMPZAPI() sub(mp_int rop, mp_int op1, mp_int op2)
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
bool first_arg_matches_class(FunctionDecl *method) const
string base_method_name(FunctionDecl *fd) const
static const char * get_prefix
map< FunctionDecl *, vector< set_enum > > set_enums
map< string, function_set > methods
static const char * set_callback_prefix
static string name_without_type_suffixes(FunctionDecl *fd)
bool is_get_method_name(FunctionDecl *fd, const string &name) const
map< int, string > type_subclasses
std::map< clang::FunctionDecl *, const isl_class & > copied_from
bool is_static(FunctionDecl *method) const
set< FunctionDecl * > persistent_callbacks
string method_name(FunctionDecl *fd) const
function_set constructors