1 #ifndef ISL_INTERFACE_CPP_H
2 #define ISL_INTERFACE_CPP_H
3 
4 #include <iostream>
5 #include <string>
6 #include <vector>
7 
8 #include "generator.h"
9 
10 /* A generated C++ method derived from an isl function.
11  *
12  * "clazz" is the class to which the method belongs.
13  * "fd" is the original isl function.
14  * "name" is the name of the method, which may be different
15  * from the default name derived from "fd".
16  * "kind" is the type of the method.
17  * "callback" stores the callback argument, if any, or NULL.
18  */
19 struct Method {
20 	enum Kind {
21 		static_method,
22 		member_method,
23 		constructor,
24 	};
25 
26 	Method(const isl_class &clazz, FunctionDecl *fd,
27 		const std::string &name);
28 	Method(const isl_class &clazz, FunctionDecl *fd);
29 
30 	int c_num_params() const;
31 	virtual int num_params() const;
32 	virtual bool param_needs_copy(int pos) const;
33 	virtual clang::ParmVarDecl *get_param(int pos) const;
34 	virtual void print_param_use(ostream &os, int pos) const;
35 	bool is_subclass_mutator() const;
36 	static void print_arg_list(std::ostream &os, int start, int end,
37 		const std::function<void(int i)> &print_arg);
38 	void print_cpp_arg_list(std::ostream &os,
39 		const std::function<void(int i)> &print_arg) const;
40 
41 	const isl_class &clazz;
42 	FunctionDecl *const fd;
43 	const std::string name;
44 	const enum Kind kind;
45 	ParmVarDecl *const callback;
46 };
47 
48 /* A method that does not require its isl type parameters to be a copy.
49  */
50 struct NoCopyMethod : Method {
NoCopyMethodNoCopyMethod51 	NoCopyMethod(const Method &method) : Method(method) {}
52 
53 	virtual bool param_needs_copy(int pos) const override;
54 };
55 
56 /* A generated method that performs one or more argument conversions and
57  * then calls the original method.
58  *
59  * A ConversionMethod inherits from a NoCopyMethod, because
60  * unlike methods that call an isl C function,
61  * a conversion method never calls release() on an isl type argument,
62  * so they can all be passed as const references.
63  *
64  * "this_type" is the name of the type to which "this" should be converted
65  * (if different from clazz.name).
66  * "get_param_fn" returns the method argument at position "pos".
67  */
68 struct ConversionMethod : NoCopyMethod {
69 	ConversionMethod(const Method &method, const std::string &this_type,
70 		const std::function<clang::ParmVarDecl *(int pos)> &get_param);
71 	ConversionMethod(const Method &method, const std::string &this_type);
72 	ConversionMethod(const Method &method,
73 		const std::function<clang::ParmVarDecl *(int pos)> &get_param);
74 	virtual clang::ParmVarDecl *get_param(int pos) const override;
75 
76 	void print_call(std::ostream &os, const std::string &ns) const;
77 
78 	const std::string this_type;
79 	const std::function<clang::ParmVarDecl *(int pos)> get_param_fn;
80 };
81 
82 /* A specialized generated C++ method for setting an enum.
83  *
84  * "enum_name" is a string representation of the enum value
85  * set by this method.
86  */
87 struct EnumMethod : public Method {
88 	EnumMethod(const isl_class &clazz, FunctionDecl *fd,
89 		const std::string &method_name, const std::string &enum_name);
90 
91 	virtual int num_params() const override;
92 	virtual void print_param_use(ostream &os, int pos) const override;
93 
94 	std::string enum_name;
95 };
96 
97 /* A type printer for converting argument and return types,
98  * as well as the class type,
99  * to string representations of the corresponding types
100  * in the C++ interface.
101  */
102 struct cpp_type_printer {
cpp_type_printercpp_type_printer103 	cpp_type_printer() {}
104 
105 	virtual std::string isl_bool() const;
106 	virtual std::string isl_stat() const;
107 	virtual std::string isl_size() const;
108 	virtual std::string isl_namespace() const;
109 	virtual std::string class_type(const std::string &cpp_name) const;
110 	virtual std::string qualified(int arg, const std::string &cpp_type)
111 		const;
112 	std::string isl_type(int arg, QualType type) const;
113 	std::string generate_callback_args(int arg, QualType type, bool cpp)
114 		const;
115 	std::string generate_callback_type(int arg, QualType type) const;
116 	std::string param(int arg, QualType type) const;
117 	std::string return_type(const Method &method) const;
118 };
119 
120 /* Generator for C++ bindings.
121  */
122 class cpp_generator : public generator {
123 protected:
124 	struct class_printer;
125 public:
126 	cpp_generator(SourceManager &SM, set<RecordDecl *> &exported_types,
127 		set<FunctionDecl *> exported_functions,
128 		set<FunctionDecl *> functions);
129 private:
130 	void set_class_construction_types(isl_class &clazz);
131 	void set_construction_types();
132 	void copy_methods(isl_class &clazz, const std::string &name,
133 		const isl_class &super, const function_set &methods);
134 	void copy_super_methods(isl_class &clazz, const isl_class &super);
135 	void copy_super_methods(isl_class &clazz, set<string> &done);
136 	void copy_super_methods();
137 	bool is_implicit_conversion(const Method &cons);
138 	bool is_subclass(QualType subclass_type, const isl_class &class_type);
139 public:
140 	static string type2cpp(const isl_class &clazz);
141 	static string type2cpp(string type_string);
142 };
143 
144 /* A helper class for printing method declarations and definitions
145  * of a class.
146  *
147  * "os" is the stream onto which the methods are printed.
148  * "clazz" describes the methods of the class.
149  * "cppstring" is the C++ name of the class.
150  * "generator" is the C++ interface generator printing the classes.
151  * "declarations" is set if this object is used to print declarations.
152  */
153 struct cpp_generator::class_printer {
154 	std::ostream &os;
155 	const isl_class &clazz;
156 	const std::string cppstring;
157 	cpp_generator &generator;
158 	const bool declarations;
159 
160 	class_printer(std::ostream &os, const isl_class &clazz,
161 			cpp_generator &generator, bool declarations);
162 
163 	void print_constructors();
164 	void print_methods();
165 	bool next_variant(FunctionDecl *fd, std::vector<bool> &convert);
166 	void print_method_variants(FunctionDecl *fd, const std::string &name);
167 	virtual bool want_descendent_overloads(const function_set &methods) = 0;
168 	void print_descendent_overloads(FunctionDecl *fd,
169 		const std::string &name);
170 	void print_method_group(const function_set &methods,
171 		const std::string &name);
172 	virtual void print_method(const Method &method) = 0;
173 	virtual void print_method(const ConversionMethod &method) = 0;
174 	virtual void print_get_method(FunctionDecl *fd) = 0;
175 	void print_set_enums(FunctionDecl *fd);
176 	void print_set_enums();
177 	ParmVarDecl *get_param(FunctionDecl *fd, int pos,
178 		const std::vector<bool> &convert);
179 	void print_method_header(const Method &method,
180 		const cpp_type_printer &type_printer);
181 };
182 
183 #endif
184