1 #include "generator.h"
2 
3 using namespace std;
4 using namespace clang;
5 
6 /* Generator for C++ bindings.
7  *
8  * "checked" is set if C++ bindings should be generated
9  * that rely on the user to check for error conditions.
10  */
11 class cpp_generator : public generator {
12 protected:
13 	bool checked;
14 public:
15 	cpp_generator(SourceManager &SM, set<RecordDecl *> &exported_types,
16 		set<FunctionDecl *> exported_functions,
17 		set<FunctionDecl *> functions,
18 		bool checked = false) :
19 		generator(SM, exported_types, exported_functions, functions),
20 		checked(checked) {}
21 
22 	enum function_kind {
23 		function_kind_static_method,
24 		function_kind_member_method,
25 		function_kind_constructor,
26 	};
27 
28 	virtual void generate();
29 private:
30 	void print_file(ostream &os, std::string filename);
31 	void print_forward_declarations(ostream &os);
32 	void print_declarations(ostream &os);
33 	void print_class(ostream &os, const isl_class &clazz);
34 	void print_class_forward_decl(ostream &os, const isl_class &clazz);
35 	void print_class_factory_decl(ostream &os, const isl_class &clazz,
36 		const std::string &prefix = std::string());
37 	void print_private_constructors_decl(ostream &os,
38 		const isl_class &clazz);
39 	void print_copy_assignment_decl(ostream &os, const isl_class &clazz);
40 	void print_public_constructors_decl(ostream &os,
41 		const isl_class &clazz);
42 	void print_constructors_decl(ostream &os, const isl_class &clazz);
43 	void print_destructor_decl(ostream &os, const isl_class &clazz);
44 	void print_ptr_decl(ostream &os, const isl_class &clazz);
45 	void print_get_ctx_decl(ostream &os);
46 	void print_methods_decl(ostream &os, const isl_class &clazz);
47 	void print_method_group_decl(ostream &os, const isl_class &clazz,
48 		const set<FunctionDecl *> &methods);
49 	void print_method_decl(ostream &os, const isl_class &clazz,
50 		FunctionDecl *method, function_kind kind);
51 	void print_implementations(ostream &os);
52 	void print_class_impl(ostream &os, const isl_class &clazz);
53 	void print_class_factory_impl(ostream &os, const isl_class &clazz);
54 	void print_private_constructors_impl(ostream &os,
55 		const isl_class &clazz);
56 	void print_public_constructors_impl(ostream &os,
57 		const isl_class &clazz);
58 	void print_constructors_impl(ostream &os, const isl_class &clazz);
59 	void print_copy_assignment_impl(ostream &os, const isl_class &clazz);
60 	void print_destructor_impl(ostream &os, const isl_class &clazz);
61 	void print_ptr_impl(ostream &os, const isl_class &clazz);
62 	void print_get_ctx_impl(ostream &os, const isl_class &clazz);
63 	void print_methods_impl(ostream &os, const isl_class &clazz);
64 	void print_method_group_impl(ostream &os, const isl_class &clazz,
65 		const set<FunctionDecl *> &methods);
66 	void print_argument_validity_check(ostream &os, FunctionDecl *method,
67 		function_kind kind);
68 	void print_save_ctx(ostream &os, FunctionDecl *method,
69 		function_kind kind);
70 	void print_on_error_continue(ostream &os);
71 	void print_exceptional_execution_check(ostream &os,
72 		FunctionDecl *method);
73 	void print_method_impl(ostream &os, const isl_class &clazz,
74 		FunctionDecl *method, function_kind kind);
75 	void print_method_param_use(ostream &os, ParmVarDecl *param,
76 		bool load_from_this_ptr);
77 	void print_method_header(ostream &os, const isl_class &clazz,
78 		FunctionDecl *method, bool is_declaration, function_kind kind);
79 	string generate_callback_args(QualType type, bool cpp);
80 	string generate_callback_type(QualType type);
81 	void print_wrapped_call_checked(std::ostream &os,
82 		const std::string &call);
83 	void print_wrapped_call(std::ostream &os, const std::string &call);
84 	void print_callback_local(ostream &os, ParmVarDecl *param);
85 	std::string rename_method(std::string name);
86 	string type2cpp(const isl_class &clazz);
87 	string type2cpp(QualType type);
88 	bool is_implicit_conversion(const isl_class &clazz, FunctionDecl *cons);
89 	bool is_subclass(QualType subclass_type, const isl_class &class_type);
90 	function_kind get_method_kind(const isl_class &clazz,
91 		FunctionDecl *method);
92 public:
93 	static string type2cpp(string type_string);
94 };
95