plain_cpp_generator::class_printer: extract out plain_printer
[isl.git] / interface / plain_cpp.h
blob8cfa3c0ebf62dfc8fd2ec3fa920355eeb05c1d0b
1 #ifndef ISL_INTERFACE_PLAIN_CPP_H
2 #define ISL_INTERFACE_PLAIN_CPP_H
4 #include <functional>
5 #include <memory>
7 #include "generator.h"
9 using namespace std;
10 using namespace clang;
12 /* A generated C++ method derived from an isl function.
14 * "clazz" is the class to which the method belongs.
15 * "fd" is the original isl function.
16 * "name" is the name of the method, which may be different
17 * from the default name derived from "fd".
18 * "kind" is the type of the method.
19 * "callback" stores the callback argument, if any, or NULL.
21 struct Method {
22 enum Kind {
23 static_method,
24 member_method,
25 constructor,
28 Method(const isl_class &clazz, FunctionDecl *fd,
29 const std::string &name);
30 Method(const isl_class &clazz, FunctionDecl *fd);
32 int c_num_params() const;
33 virtual int num_params() const;
34 virtual bool param_needs_copy(int pos) const;
35 virtual clang::ParmVarDecl *get_param(int pos) const;
36 virtual void print_param_use(ostream &os, int pos) const;
37 bool is_subclass_mutator() const;
38 static void print_arg_list(std::ostream &os, int start, int end,
39 const std::function<void(int i)> &print_arg);
40 void print_cpp_arg_list(std::ostream &os,
41 const std::function<void(int i)> &print_arg) const;
43 const isl_class &clazz;
44 FunctionDecl *const fd;
45 const std::string name;
46 const enum Kind kind;
47 ParmVarDecl *const callback;
50 /* A generated method that performs one or more argument conversions and
51 * then calls the original method.
53 * "this_type" is the name of the type to which "this" should be converted
54 * (if different from clazz.name).
55 * "get_param_fn" returns the method argument at position "pos".
57 struct ConversionMethod : Method {
58 ConversionMethod(const Method &method, const std::string &this_type,
59 const std::function<clang::ParmVarDecl *(int pos)> &get_param);
60 ConversionMethod(const Method &method, const std::string &this_type);
61 ConversionMethod(const Method &method,
62 const std::function<clang::ParmVarDecl *(int pos)> &get_param);
63 virtual bool param_needs_copy(int pos) const override;
64 virtual clang::ParmVarDecl *get_param(int pos) const override;
66 void print_call(std::ostream &os, const std::string &ns) const;
68 const std::string this_type;
69 const std::function<clang::ParmVarDecl *(int pos)> get_param_fn;
72 /* A specialized generated C++ method for setting an enum.
74 * "enum_name" is a string representation of the enum value
75 * set by this method.
77 struct EnumMethod : public Method {
78 EnumMethod(const isl_class &clazz, FunctionDecl *fd,
79 const std::string &method_name, const std::string &enum_name);
81 virtual int num_params() const override;
82 virtual void print_param_use(ostream &os, int pos) const override;
84 std::string enum_name;
87 /* A type printer for converting argument and return types
88 * to string representations of the corresponding types
89 * in the C++ interface.
91 struct cpp_type_printer {
92 cpp_type_printer() {}
94 virtual std::string isl_bool() const;
95 virtual std::string isl_stat() const;
96 virtual std::string isl_size() const;
97 virtual std::string isl_namespace() const;
98 std::string isl_type(QualType type) const;
99 std::string generate_callback_args(QualType type, bool cpp) const;
100 std::string generate_callback_type(QualType type) const;
101 std::string param(QualType type) const;
102 std::string return_type(const Method &method) const;
105 /* A type printer for converting argument and return types
106 * to string representations of the corresponding types
107 * in the checked C++ interface.
109 struct checked_cpp_type_printer : public cpp_type_printer {
110 virtual std::string isl_bool() const override;
111 virtual std::string isl_stat() const override;
112 virtual std::string isl_size() const override;
113 virtual std::string isl_namespace() const override;
116 /* Generator for plain C++ bindings.
118 * "checked" is set if C++ bindings should be generated
119 * that rely on the user to check for error conditions.
121 class plain_cpp_generator : public generator {
122 struct class_printer;
123 struct plain_printer;
124 struct decl_printer;
125 struct impl_printer;
126 protected:
127 bool checked;
128 public:
129 plain_cpp_generator(SourceManager &SM,
130 set<RecordDecl *> &exported_types,
131 set<FunctionDecl *> exported_functions,
132 set<FunctionDecl *> functions,
133 bool checked = false);
135 virtual void generate();
136 private:
137 void set_class_construction_types(isl_class &clazz);
138 void set_construction_types();
139 void copy_methods(isl_class &clazz, const std::string &name,
140 const isl_class &super, const function_set &methods);
141 void copy_super_methods(isl_class &clazz, const isl_class &super);
142 void copy_super_methods(isl_class &clazz, set<string> &done);
143 void copy_super_methods();
144 void print_forward_declarations(ostream &os);
145 void print_declarations(ostream &os);
146 void print_class(ostream &os, const isl_class &clazz);
147 void print_class_forward_decl(ostream &os, const isl_class &clazz);
148 void print_implementations(ostream &os);
149 void print_class_impl(ostream &os, const isl_class &clazz);
150 void print_check_no_persistent_callback(ostream &os,
151 const isl_class &clazz, FunctionDecl *fd);
152 void print_invalid(ostream &os, int indent, const char *msg,
153 const char *checked_code);
154 void print_method_param_use(ostream &os, ParmVarDecl *param,
155 bool load_from_this_ptr);
156 std::unique_ptr<cpp_type_printer> type_printer();
157 std::string get_return_type(const Method &method);
158 string generate_callback_args(QualType type, bool cpp);
159 string generate_callback_type(QualType type);
160 string isl_bool2cpp();
161 string isl_namespace();
162 string param2cpp(QualType type);
163 bool is_implicit_conversion(const Method &cons);
164 bool is_subclass(QualType subclass_type, const isl_class &class_type);
165 public:
166 static string type2cpp(const isl_class &clazz);
167 static string type2cpp(string type_string);
170 /* A helper class for printing method declarations and definitions
171 * of a class.
173 * "os" is the stream onto which the methods are printed.
174 * "clazz" describes the methods of the class.
175 * "cppstring" is the C++ name of the class.
176 * "generator" is the C++ interface generator printing the classes.
177 * "declarations" is set if this object is used to print declarations.
179 struct plain_cpp_generator::class_printer {
180 std::ostream &os;
181 const isl_class &clazz;
182 const std::string cppstring;
183 plain_cpp_generator &generator;
184 const bool declarations;
186 class_printer(std::ostream &os, const isl_class &clazz,
187 plain_cpp_generator &generator, bool declarations);
189 void print_constructors();
190 void print_methods();
191 bool next_variant(FunctionDecl *fd, std::vector<bool> &convert);
192 void print_method_variants(FunctionDecl *fd, const std::string &name);
193 void print_descendent_overloads(FunctionDecl *fd,
194 const std::string &name);
195 void print_method_group(const function_set &methods,
196 const std::string &name);
197 virtual void print_method(const Method &method) = 0;
198 virtual void print_method(const ConversionMethod &method) = 0;
199 virtual void print_get_method(FunctionDecl *fd) = 0;
200 void print_set_enums(FunctionDecl *fd);
201 void print_set_enums();
202 ParmVarDecl *get_param(FunctionDecl *fd, int pos,
203 const std::vector<bool> &convert);
204 void print_method_header(const Method &method);
207 /* A helper class for printing method declarations and definitions
208 * of a class for the plain C++ interface.
210 * "generator" is the C++ interface generator printing the classes.
212 struct plain_cpp_generator::plain_printer :
213 public plain_cpp_generator::class_printer
215 plain_cpp_generator &generator;
217 plain_printer(std::ostream &os, const isl_class &clazz,
218 plain_cpp_generator &generator, bool is_declaration) :
219 class_printer(os, clazz, generator, is_declaration),
220 generator(generator) {}
222 void print_persistent_callback_prototype(FunctionDecl *method);
223 void print_persistent_callback_setter_prototype(FunctionDecl *method);
224 void print_full_method_header(const Method &method);
225 void print_callback_data_decl(ParmVarDecl *param, const string &name);
228 /* A helper class for printing method declarations of a class.
230 struct plain_cpp_generator::decl_printer :
231 public plain_cpp_generator::plain_printer
233 decl_printer(std::ostream &os, const isl_class &clazz,
234 plain_cpp_generator &generator) :
235 plain_printer(os, clazz, generator, true) {}
237 void print_subclass_type();
238 void print_class_factory(const std::string &prefix = std::string());
239 void print_protected_constructors();
240 void print_copy_assignment();
241 void print_public_constructors();
242 void print_destructor();
243 void print_ptr();
244 void print_isa_type_template(int indent, const isl_class &super);
245 void print_downcast();
246 void print_ctx();
247 void print_persistent_callback_data(FunctionDecl *method);
248 void print_persistent_callbacks();
249 virtual void print_method(const Method &method) override;
250 virtual void print_method(const ConversionMethod &method) override;
251 virtual void print_get_method(FunctionDecl *fd) override;
254 /* A helper class for printing method definitions of a class.
256 struct plain_cpp_generator::impl_printer :
257 public plain_cpp_generator::plain_printer
259 impl_printer(std::ostream &os, const isl_class &clazz,
260 plain_cpp_generator &generator) :
261 plain_printer(os, clazz, generator, false) {}
263 void print_arg_conversion(ParmVarDecl *dst, ParmVarDecl *src);
264 virtual void print_method(const Method &method) override;
265 virtual void print_method(const ConversionMethod &method) override;
266 virtual void print_get_method(FunctionDecl *fd) override;
267 void print_check_ptr(const char *ptr);
268 void print_check_ptr_start(const char *ptr);
269 void print_check_ptr_end(const char *ptr);
270 void print_class_factory();
271 void print_protected_constructors();
272 void print_public_constructors();
273 void print_copy_assignment();
274 void print_destructor();
275 void print_ptr();
276 void print_downcast();
277 void print_ctx();
278 void print_set_persistent_callback(const Method &method);
279 void print_persistent_callbacks();
280 void print_argument_validity_check(const Method &method);
281 void print_save_ctx(const Method &method);
282 void print_on_error_continue();
283 void print_exceptional_execution_check(const Method &method);
284 void print_method_return(const Method &method);
285 void print_stream_insertion();
286 void print_wrapped_call_checked(int indent, const std::string &call);
287 void print_wrapped_call(int indent, const std::string &call,
288 QualType rtype);
289 void print_callback_body(int indent, ParmVarDecl *param,
290 const string &name);
291 void print_callback_local(ParmVarDecl *param);
294 #endif