bindings: drop "get_" prefix of methods that start this way
[isl.git] / interface / generator.h
blob4959e6a913f6bb67acc2a517f2b11cedf79f277f
1 #ifndef ISL_INTERFACE_GENERATOR_H
2 #define ISL_INTERFACE_GENERATOR_H
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
9 #include <clang/AST/Decl.h>
11 using namespace std;
12 using namespace clang;
14 /* Compare the prefix of "s" to "prefix" up to the length of "prefix".
16 inline int prefixcmp(const char *s, const char *prefix)
18 return strncmp(s, prefix, strlen(prefix));
21 /* Information about a single enum value of an enum set by a function.
22 * "value" is the enum value.
23 * "name" is the corresponding name.
24 * "method_name" is the the name of the method that sets this value.
26 struct set_enum {
27 int value;
28 string name;
29 string method_name;
30 set_enum(int value, string name, string method_name) :
31 value(value), name(name), method_name(method_name) {}
34 /* isl_class collects all constructors and methods for an isl "class".
35 * "name" is the name of the class.
36 * If this object describes a subclass of a C type, then
37 * "subclass_name" is the name of that subclass and "superclass_name"
38 * is the name of the immediate superclass of that subclass. Otherwise,
39 * "subclass_name" is equal to "name" and "superclass_name" is undefined.
40 * "type" is the declaration that introduces the type.
41 * "persistent_callbacks" contains the set of functions that
42 * set a persistent callback.
43 * "set_enums" maps the set of functions that set an enum value
44 * to information associated to each value.
45 * A function is considered to set an enum value if it returns
46 * an object of the same type and if its last argument is of an enum type.
47 * "methods" contains the set of methods, grouped by method name.
48 * "fn_to_str" is a reference to the *_to_str method of this class, if any.
49 * "fn_copy" is a reference to the *_copy method of this class, if any.
50 * "fn_free" is a reference to the *_free method of this class, if any.
51 * "fn_type" is a reference to a function that described subclasses, if any.
52 * If "fn_type" is set, then "type_subclasses" maps the values returned
53 * by that function to the names of the corresponding subclasses.
55 struct isl_class {
56 string name;
57 string superclass_name;
58 string subclass_name;
59 RecordDecl *type;
60 set<FunctionDecl *> constructors;
61 set<FunctionDecl *> persistent_callbacks;
62 map<FunctionDecl *, vector<set_enum> > set_enums;
63 map<string, set<FunctionDecl *> > methods;
64 map<int, string> type_subclasses;
65 FunctionDecl *fn_type;
66 FunctionDecl *fn_to_str;
67 FunctionDecl *fn_copy;
68 FunctionDecl *fn_free;
70 /* Does "method" correspond to a static method? */
71 bool is_static(FunctionDecl *method) const;
72 /* Is this class a subclass based on a type function? */
73 bool is_type_subclass() const { return name != subclass_name; }
74 /* Return name of "fd" without type suffix, if any. */
75 static string name_without_type_suffix(FunctionDecl *fd);
76 /* Extract the method name corresponding to "fd"
77 * (including "get" method prefix if any).
79 string base_method_name(FunctionDecl *fd) const {
80 string m_name = name_without_type_suffix(fd);
81 return m_name.substr(subclass_name.length() + 1);
83 /* The prefix of a "get" method. */
84 static const char *get_prefix;
85 /* Is function "fd" with the given name a "get" method? */
86 bool is_get_method_name(FunctionDecl *fd, const string &name) const;
87 /* Is function "fd" a "get" method? */
88 bool is_get_method(FunctionDecl *fd) const {
89 return is_get_method_name(fd, base_method_name(fd));
91 /* Extract the method name corresponding to "fd". */
92 string method_name(FunctionDecl *fd) const;
93 /* The prefix of any method that may set a (persistent) callback. */
94 static const char *set_callback_prefix;
95 /* Given a function that sets a persistent callback,
96 * return the name of the callback.
98 string persistent_callback_name(FunctionDecl *fd) const {
99 return method_name(fd).substr(strlen(set_callback_prefix));
101 /* Does this class have any functions that set a persistent callback?
103 bool has_persistent_callbacks() const {
104 return persistent_callbacks.size() != 0;
108 /* Base class for interface generators.
110 class generator {
111 protected:
112 SourceManager &SM;
113 map<string,isl_class> classes;
114 map<string, FunctionDecl *> functions_by_name;
116 public:
117 generator(SourceManager &SM, set<RecordDecl *> &exported_types,
118 set<FunctionDecl *> exported_functions,
119 set<FunctionDecl *> functions);
121 virtual void generate() = 0;
122 virtual ~generator() {};
124 protected:
125 void add_subclass(RecordDecl *decl, const string &name,
126 const string &sub_name);
127 void add_class(RecordDecl *decl);
128 void add_type_subclasses(FunctionDecl *method);
129 isl_class *method2class(FunctionDecl *fd);
130 bool callback_takes_argument(ParmVarDecl *param, int pos);
131 FunctionDecl *find_by_name(const string &name, bool required);
132 public:
133 static void die(const char *msg) __attribute__((noreturn));
134 static void die(string msg) __attribute__((noreturn));
135 static vector<string> find_superclasses(Decl *decl);
136 static bool is_subclass(FunctionDecl *decl);
137 static bool is_overload(Decl *decl);
138 static bool is_constructor(Decl *decl);
139 static bool takes(Decl *decl);
140 static bool keeps(Decl *decl);
141 static bool gives(Decl *decl);
142 static bool is_isl_ctx(QualType type);
143 static bool first_arg_is_isl_ctx(FunctionDecl *fd);
144 static bool is_isl_type(QualType type);
145 static bool is_isl_neg_error(QualType type);
146 static bool is_isl_bool(QualType type);
147 static bool is_isl_stat(QualType type);
148 static bool is_isl_size(QualType type);
149 static bool is_long(QualType type);
150 static bool is_callback(QualType type);
151 static bool is_string(QualType type);
152 static bool is_static(const isl_class &clazz, FunctionDecl *method);
153 static bool is_mutator(const isl_class &clazz, FunctionDecl *fd);
154 static string extract_type(QualType type);
155 static const FunctionProtoType *extract_prototype(QualType type);
156 static ParmVarDecl *persistent_callback_arg(FunctionDecl *fd);
159 #endif /* ISL_INTERFACE_GENERATOR_H */