1 #ifndef ISL_INTERFACE_GENERATOR_H
2 #define ISL_INTERFACE_GENERATOR_H
3 
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
8 
9 #include <clang/AST/Decl.h>
10 
11 using namespace std;
12 using namespace clang;
13 
14 /* Compare the prefix of "s" to "prefix" up to the length of "prefix".
15  */
prefixcmp(const char * s,const char * prefix)16 inline int prefixcmp(const char *s, const char *prefix)
17 {
18 	return strncmp(s, prefix, strlen(prefix));
19 }
20 
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.
25  */
26 struct set_enum {
27 	int	value;
28 	string	name;
29 	string	method_name;
set_enumset_enum30 	set_enum(int value, string name, string method_name) :
31 		value(value), name(name), method_name(method_name) {}
32 };
33 
34 /* Helper structure for sorting FunctionDecl pointers
35  * on the corresponding function names.
36  */
37 struct function_name_less {
operatorfunction_name_less38 	bool operator()(FunctionDecl *x, FunctionDecl *y) const {
39 		return x->getName() < y->getName();
40 	}
41 };
42 
43 /* Set of FunctionDecl pointers sorted on function name.
44  */
45 typedef std::set<FunctionDecl *, function_name_less> function_set;
46 
47 /* isl_class collects all constructors and methods for an isl "class".
48  * "name" is the name of the class.
49  * If this object describes a subclass of a C type, then
50  * "subclass_name" is the name of that subclass and "superclass_name"
51  * is the name of the immediate superclass of that subclass.  Otherwise,
52  * "subclass_name" is equal to "name" and "superclass_name" is undefined.
53  * "type" is the declaration that introduces the type.
54  * "persistent_callbacks" contains the set of functions that
55  * set a persistent callback.
56  * "set_enums" maps the set of functions that set an enum value
57  * to information associated to each value.
58  * A function is considered to set an enum value if it returns
59  * an object of the same type and if its last argument is of an enum type.
60  * "methods" contains the set of methods, grouped by method name.
61  * "fn_to_str" is a reference to the *_to_str method of this class, if any.
62  * "fn_copy" is a reference to the *_copy method of this class, if any.
63  * "fn_free" is a reference to the *_free method of this class, if any.
64  * "fn_type" is a reference to a function that described subclasses, if any.
65  * If "fn_type" is set, then "type_subclasses" maps the values returned
66  * by that function to the names of the corresponding subclasses.
67  */
68 struct isl_class {
69 	string name;
70 	string superclass_name;
71 	string subclass_name;
72 	RecordDecl *type;
73 	function_set constructors;
74 	set<FunctionDecl *> persistent_callbacks;
75 	map<FunctionDecl *, vector<set_enum> > set_enums;
76 	map<string, function_set> methods;
77 	map<int, string> type_subclasses;
78 	FunctionDecl *fn_type;
79 	FunctionDecl *fn_to_str;
80 	FunctionDecl *fn_copy;
81 	FunctionDecl *fn_free;
82 
83 	/* Does "method" correspond to a static method? */
84 	bool is_static(FunctionDecl *method) const;
85 	/* Is this class a subclass based on a type function? */
is_type_subclassisl_class86 	bool is_type_subclass() const { return name != subclass_name; }
87 	/* Return name of "fd" without type suffixes, if any. */
88 	static string name_without_type_suffixes(FunctionDecl *fd);
89 	/* Extract the method name corresponding to "fd"
90 	 * (including "get" method prefix if any).
91 	 */
base_method_nameisl_class92 	string base_method_name(FunctionDecl *fd) const {
93 		string m_name = name_without_type_suffixes(fd);
94 		return m_name.substr(subclass_name.length() + 1);
95 	}
96 	/* The prefix of a "get" method. */
97 	static const char *get_prefix;
98 	/* Is function "fd" with the given name a "get" method? */
99 	bool is_get_method_name(FunctionDecl *fd, const string &name) const;
100 	/* Is function "fd" a "get" method? */
is_get_methodisl_class101 	bool is_get_method(FunctionDecl *fd) const {
102 		return is_get_method_name(fd, base_method_name(fd));
103 	}
104 	/* Extract the method name corresponding to "fd". */
105 	string method_name(FunctionDecl *fd) const;
106 	/* The prefix of any method that may set a (persistent) callback. */
107 	static const char *set_callback_prefix;
108 	/* Given a function that sets a persistent callback,
109 	 * return the name of the callback.
110 	 */
persistent_callback_nameisl_class111 	string persistent_callback_name(FunctionDecl *fd) const {
112 		return method_name(fd).substr(strlen(set_callback_prefix));
113 	}
114 	/* Does this class have any functions that set a persistent callback?
115 	 */
has_persistent_callbacksisl_class116 	bool has_persistent_callbacks() const {
117 		return persistent_callbacks.size() != 0;
118 	}
119 };
120 
121 /* Base class for interface generators.
122  *
123  * "conversions" maps the target type of automatic conversion
124  * to the second input argument of the conversion function.
125  */
126 class generator {
127 protected:
128 	SourceManager &SM;
129 	map<string,isl_class> classes;
130 	map<string, FunctionDecl *> functions_by_name;
131 
132 public:
133 	generator(SourceManager &SM, set<RecordDecl *> &exported_types,
134 		set<FunctionDecl *> exported_functions,
135 		set<FunctionDecl *> functions);
136 
137 	virtual void generate() = 0;
~generator()138 	virtual ~generator() {};
139 
140 protected:
141 	void add_subclass(RecordDecl *decl, const string &name,
142 		const string &sub_name);
143 	void add_class(RecordDecl *decl);
144 	void add_type_subclasses(FunctionDecl *method);
145 	isl_class *method2class(FunctionDecl *fd);
146 	bool callback_takes_argument(ParmVarDecl *param, int pos);
147 	FunctionDecl *find_by_name(const string &name, bool required);
148 	std::map<const Type *, ParmVarDecl *> conversions;
149 private:
150 	static const std::set<std::string> automatic_conversion_functions;
151 	void extract_automatic_conversion(FunctionDecl *fd);
152 	void extract_class_automatic_conversions(const isl_class &clazz);
153 	void extract_automatic_conversions();
154 public:
155 	static void die(const char *msg) __attribute__((noreturn));
156 	static void die(string msg) __attribute__((noreturn));
157 	static vector<string> find_superclasses(Decl *decl);
158 	static bool is_subclass(FunctionDecl *decl);
159 	static bool is_overload(Decl *decl);
160 	static bool is_constructor(Decl *decl);
161 	static bool takes(Decl *decl);
162 	static bool keeps(Decl *decl);
163 	static bool gives(Decl *decl);
164 	static bool is_isl_ctx(QualType type);
165 	static bool first_arg_is_isl_ctx(FunctionDecl *fd);
166 	static bool is_isl_type(QualType type);
167 	static bool is_isl_neg_error(QualType type);
168 	static bool is_isl_bool(QualType type);
169 	static bool is_isl_stat(QualType type);
170 	static bool is_isl_size(QualType type);
171 	static bool is_long(QualType type);
172 	static bool is_callback(QualType type);
173 	static bool is_string(QualType type);
174 	static bool is_static(const isl_class &clazz, FunctionDecl *method);
175 	static bool is_mutator(const isl_class &clazz, FunctionDecl *fd);
176 	static string extract_type(QualType type);
177 	static const FunctionProtoType *extract_prototype(QualType type);
178 	static ParmVarDecl *persistent_callback_arg(FunctionDecl *fd);
179 };
180 
181 #endif /* ISL_INTERFACE_GENERATOR_H */
182