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) :
generator(SM,exported_types,exported_functions,functions)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 	enum method_part {
28 		decl,
29 		impl,
30 	};
31 
32 	virtual void generate();
33 private:
34 	void print_forward_declarations(ostream &os);
35 	void print_declarations(ostream &os);
36 	void print_class(ostream &os, const isl_class &clazz);
37 	void print_subclass_type(ostream &os, const isl_class &clazz);
38 	void print_class_forward_decl(ostream &os, const isl_class &clazz);
39 	void print_class_factory_decl(ostream &os, const isl_class &clazz,
40 		const std::string &prefix = std::string());
41 	void print_protected_constructors_decl(ostream &os,
42 		const isl_class &clazz);
43 	void print_copy_assignment_decl(ostream &os, const isl_class &clazz);
44 	void print_public_constructors_decl(ostream &os,
45 		const isl_class &clazz);
46 	void print_constructors_decl(ostream &os, const isl_class &clazz);
47 	void print_destructor_decl(ostream &os, const isl_class &clazz);
48 	void print_ptr_decl(ostream &os, const isl_class &clazz);
49 	void print_isa_type_template(ostream &os, int indent,
50 		const isl_class &super);
51 	void print_downcast_decl(ostream &os, const isl_class &clazz);
52 	void print_ctx_decl(ostream &os);
53 	void print_persistent_callback_prototype(ostream &os,
54 		const isl_class &clazz, FunctionDecl *method,
55 		bool is_declaration);
56 	void print_persistent_callback_setter_prototype(ostream &os,
57 		const isl_class &clazz, FunctionDecl *method,
58 		bool is_declaration);
59 	void print_persistent_callback_data(ostream &os, const isl_class &clazz,
60 		FunctionDecl *method);
61 	void print_persistent_callbacks_decl(ostream &os,
62 		const isl_class &clazz);
63 	void print_methods_decl(ostream &os, const isl_class &clazz);
64 	bool next_variant(FunctionDecl *fd, std::vector<bool> &convert);
65 	template <enum method_part>
66 	void print_method_variants(ostream &os, const isl_class &clazz,
67 		FunctionDecl *fd);
68 	void print_method_group_decl(ostream &os, const isl_class &clazz,
69 		const function_set &methods);
70 	void print_named_method_decl(ostream &os, const isl_class &clazz,
71 		FunctionDecl *fd, const string &name, function_kind kind,
72 		const std::vector<bool> &convert = {});
73 	template <enum method_part>
74 	void print_method(ostream &os, const isl_class &clazz,
75 		FunctionDecl *method, function_kind kind);
76 	template <enum method_part>
77 	void print_method(ostream &os, const isl_class &clazz,
78 		FunctionDecl *method, function_kind kind,
79 		const std::vector<bool> &convert);
80 	void print_set_enum_decl(ostream &os, const isl_class &clazz,
81 		FunctionDecl *fd, const string &name);
82 	void print_set_enums_decl(ostream &os, const isl_class &clazz,
83 		FunctionDecl *fd);
84 	void print_set_enums_decl(ostream &os, const isl_class &clazz);
85 	void print_implementations(ostream &os);
86 	void print_class_impl(ostream &os, const isl_class &clazz);
87 	void print_check_ptr(ostream &os, const char *ptr);
88 	void print_check_ptr_start(ostream &os, const isl_class &clazz,
89 		const char *ptr);
90 	void print_check_ptr_end(ostream &os, const char *ptr);
91 	void print_class_factory_impl(ostream &os, const isl_class &clazz);
92 	void print_protected_constructors_impl(ostream &os,
93 		const isl_class &clazz);
94 	void print_public_constructors_impl(ostream &os,
95 		const isl_class &clazz);
96 	void print_constructors_impl(ostream &os, const isl_class &clazz);
97 	void print_copy_assignment_impl(ostream &os, const isl_class &clazz);
98 	void print_destructor_impl(ostream &os, const isl_class &clazz);
99 	void print_check_no_persistent_callback(ostream &os,
100 		const isl_class &clazz, FunctionDecl *fd);
101 	void print_ptr_impl(ostream &os, const isl_class &clazz);
102 	void print_downcast_impl(ostream &os, const isl_class &clazz);
103 	void print_ctx_impl(ostream &os, const isl_class &clazz);
104 	void print_persistent_callbacks_impl(ostream &os,
105 		const isl_class &clazz);
106 	void print_methods_impl(ostream &os, const isl_class &clazz);
107 	void print_method_group_impl(ostream &os, const isl_class &clazz,
108 		const function_set &methods);
109 	void print_argument_validity_check(ostream &os, FunctionDecl *method,
110 		function_kind kind);
111 	void print_save_ctx(ostream &os, FunctionDecl *method,
112 		function_kind kind);
113 	void print_on_error_continue(ostream &os);
114 	void print_exceptional_execution_check(ostream &os,
115 		const isl_class &clazz, FunctionDecl *method,
116 		function_kind kind);
117 	void print_set_persistent_callback(ostream &os, const isl_class &clazz,
118 		FunctionDecl *method, function_kind kind);
119 	void print_method_return(ostream &os, const isl_class &clazz,
120 		FunctionDecl *method);
121 	void print_set_enum_impl(ostream &os, const isl_class &clazz,
122 		FunctionDecl *fd, const string &enum_name,
123 		const string &method_name);
124 	void print_set_enums_impl(ostream &os, const isl_class &clazz,
125 		FunctionDecl *fd);
126 	void print_set_enums_impl(ostream &os, const isl_class &clazz);
127 	template <enum method_part>
128 	void print_get_method(ostream &os, const isl_class &clazz,
129 		FunctionDecl *fd);
130 	void print_invalid(ostream &os, int indent, const char *msg,
131 		const char *checked_code);
132 	void print_stream_insertion(ostream &os, const isl_class &clazz);
133 	void print_method_param_use(ostream &os, ParmVarDecl *param,
134 		bool load_from_this_ptr);
135 	std::string get_return_type(const isl_class &clazz, FunctionDecl *fd);
136 	ParmVarDecl *get_param(FunctionDecl *fd, int pos,
137 		const std::vector<bool> &convert);
138 	void print_method_header(ostream &os, const isl_class &clazz,
139 		FunctionDecl *method, const string &cname, int num_params,
140 		bool is_declaration, function_kind kind,
141 		const std::vector<bool> &convert = {});
142 	void print_named_method_header(ostream &os, const isl_class &clazz,
143 		FunctionDecl *method, string name, bool is_declaration,
144 		function_kind kind, const std::vector<bool> &convert = {});
145 	void print_method_header(ostream &os, const isl_class &clazz,
146 		FunctionDecl *method, bool is_declaration, function_kind kind);
147 	string generate_callback_args(QualType type, bool cpp);
148 	string generate_callback_type(QualType type);
149 	void print_wrapped_call_checked(std::ostream &os, int indent,
150 		const std::string &call);
151 	void print_wrapped_call(std::ostream &os, int indent,
152 		const std::string &call, QualType rtype);
153 	void print_callback_data_decl(ostream &os, ParmVarDecl *param,
154 		const string &name);
155 	void print_callback_body(ostream &os, int indent, ParmVarDecl *param,
156 		const string &name);
157 	void print_callback_local(ostream &os, ParmVarDecl *param);
158 	std::string rename_method(std::string name);
159 	string isl_bool2cpp();
160 	string isl_namespace();
161 	string type2cpp(QualType type);
162 	bool is_implicit_conversion(const isl_class &clazz, FunctionDecl *cons);
163 	bool is_subclass(QualType subclass_type, const isl_class &class_type);
164 	function_kind get_method_kind(const isl_class &clazz,
165 		FunctionDecl *method);
166 public:
167 	static string type2cpp(const isl_class &clazz);
168 	static string type2cpp(string type_string);
169 };
170