5 #include "create_wrapper.hpp"
9 WrapperCreator::create_wrapper(Namespace* ns)
11 std::string fromfile = original_file != "" ? original_file : inputfile;
16 << " * WARNING: This file is automatically generated from:\n"
17 << " * '" << fromfile << "'\n"
18 << " * DO NOT CHANGE\n"
20 << "#ifndef __" << modulename << "_WRAPPER_H__\n"
21 << "#define __" << modulename << "_WRAPPER_H__\n"
23 << "#include \"wrapper_util.hpp\"\n"
25 << "extern WrappedFunction " << modulename << "_global_functions[];\n"
26 << "extern WrappedClass " << modulename << "_classes[];\n"
27 << "extern WrappedConstant<int> "
28 << modulename << "_int_constants[];\n"
29 << "extern WrappedConstant<float> "
30 << modulename << "_float_constants[];\n"
31 << "extern WrappedConstant<const char*> "
32 << modulename << "_string_constants[];\n"
34 << "static inline void register_"
35 << modulename << "_wrapper(HSQUIRRELVM v)\n"
37 << " register_functions(v, "
38 << modulename << "_global_functions);\n"
39 << " register_classes(v, " << modulename << "_classes);\n"
40 << " register_constants(v, " << modulename << "_int_constants);\n"
41 << " register_constants(v, " << modulename << "_float_constants);\n"
42 << " register_constants(v, "<< modulename << "_string_constants);\n"
50 << " * WARNING: This file is automatically generated from:\n"
51 << " * '" << fromfile << "'\n"
52 << " * DO NOT CHANGE\n"
54 << "#include <config.h>\n"
57 << "#include <assert.h>\n"
58 << "#include <string>\n"
59 << "#include <squirrel.h>\n"
60 << "#include \"wrapper_util.hpp\"\n"
61 << "#include \"wrapper.interface.hpp\"\n"
63 if(selected_namespace != "") {
64 out << "using namespace " << selected_namespace << ";\n";
68 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
69 i != ns->types.end(); ++i) {
70 AtomicType* type = *i;
71 Class* _class = dynamic_cast<Class*> (type);
73 create_class_wrapper(_class);
75 for(std::vector<Function*>::iterator i = ns->functions.begin();
76 i != ns->functions.end(); ++i) {
77 create_function_wrapper(0, *i);
80 // create function list...
81 create_function_list(ns);
82 create_const_lists(ns);
84 // create class list...
85 std::ostringstream classlist;
86 classlist << "WrappedClass " << modulename << "_classes[] = {\n";
88 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
89 i != ns->types.end(); ++i) {
90 AtomicType* type = *i;
91 Class* _class = dynamic_cast<Class*> (type);
95 classlist << ind << "{ \"" << _class->name << "\", "
96 << modulename << "_" << _class->name << "_methods, "
97 << modulename << "_" << _class->name << "_int_consts, "
98 << modulename << "_" << _class->name << "_float_consts, "
99 << modulename << "_" << _class->name << "_string_consts "
102 out << "static WrappedFunction " << modulename << "_"
103 << _class->name << "_methods[] = {\n";
104 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
105 i != _class->members.end(); ++i) {
106 ClassMember* member = *i;
107 if(member->visibility != ClassMember::PUBLIC)
109 Function* function = dynamic_cast<Function*> (member);
110 if(!function || function->type == Function::DESTRUCTOR)
113 out << ind << "{ \"" << function->name << "\", &"
114 << _class->name << "_" << function->name << "_wrapper },\n";
118 create_class_const_lists(_class);
120 classlist << ind << "{ 0, 0, 0, 0, 0 }\n";
122 out << classlist.str();
127 WrapperCreator::create_function_list(Namespace* ns)
129 out << "WrappedFunction " << modulename << "_global_functions[] = {\n";
130 for(std::vector<Function*>::iterator i = ns->functions.begin();
131 i != ns->functions.end(); ++i) {
132 Function* function = *i;
133 out << ind << "{ \"" << function->name << "\", &"
134 << function->name << "_wrapper },\n";
136 out << ind << "{ 0, 0 }\n"
142 WrapperCreator::create_const_lists(Namespace* ns)
144 out << "WrappedConstant<int> " << modulename << "_int_constants[] = {\n";
145 for(std::vector<Field*>::iterator i = ns->fields.begin();
146 i != ns->fields.end(); ++i) {
148 if(!field->has_const_value
149 || field->type->atomic_type != &BasicType::INT)
151 out << ind << "{ \"" << field->name << "\", "
152 << field->const_int_value << "},\n";
154 out << ind << "{ 0, 0}\n";
158 out << "WrappedConstant<float> "
159 << modulename << "_float_constants[] = {\n";
160 for(std::vector<Field*>::iterator i = ns->fields.begin();
161 i != ns->fields.end(); ++i) {
163 if(!field->has_const_value
164 || field->type->atomic_type != &BasicType::FLOAT)
166 out << ind << "{ \"" << field->name << "\", "
167 << field->const_float_value << "},\n";
169 out << ind << "{ 0, 0}\n";
173 out << "WrappedConstant<const char*> "
174 << modulename << "_string_constants[] = {\n";
175 for(std::vector<Field*>::iterator i = ns->fields.begin();
176 i != ns->fields.end(); ++i) {
178 if(!field->has_const_value
179 || field->type->atomic_type != StringType::instance())
181 out << ind << "{ \"" << field->name << "\", "
182 << field->const_float_value << "},\n";
184 out << ind << "{ 0, 0}\n";
190 WrapperCreator::create_class_const_lists(Class* _class)
192 out << "static WrappedConstant<int> "
193 << modulename << "_" << _class->name << "_int_consts[] = {\n";
194 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
195 i != _class->members.end(); ++i) {
196 ClassMember* member = *i;
197 if(member->visibility != ClassMember::PUBLIC)
199 Field* field = dynamic_cast<Field*> (member);
202 if(!field->has_const_value
203 || field->type->atomic_type != &BasicType::INT)
205 out << ind << "{ \"" << field->name << "\", "
206 << field->const_int_value << "},\n";
208 out << ind << "{ 0, 0}\n";
212 out << "WrappedConstant<float> "
213 << modulename << "_" << _class->name << "_float_consts[] = {\n";
214 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
215 i != _class->members.end(); ++i) {
216 ClassMember* member = *i;
217 if(member->visibility != ClassMember::PUBLIC)
219 Field* field = dynamic_cast<Field*> (member);
222 if(!field->has_const_value
223 || field->type->atomic_type != &BasicType::FLOAT)
225 out << ind << "{ \"" << field->name << "\", "
226 << field->const_float_value << "},\n";
228 out << ind << "{ 0, 0}\n";
232 out << "WrappedConstant<const char*> "
233 << modulename << "_" << _class->name << "_string_consts[] = {\n";
234 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
235 i != _class->members.end(); ++i) {
236 ClassMember* member = *i;
237 if(member->visibility != ClassMember::PUBLIC)
239 Field* field = dynamic_cast<Field*> (member);
242 if(!field->has_const_value
243 || field->type->atomic_type != StringType::instance())
245 out << ind << "{ \"" << field->name << "\", "
246 << field->const_float_value << "},\n";
248 out << ind << "{ 0, 0}\n";
254 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
256 if(function->type == Function::DESTRUCTOR)
259 std::string ns_prefix;
260 if(selected_namespace != "")
261 ns_prefix = selected_namespace + "::";
262 if(function->type == Function::CONSTRUCTOR)
263 function->name = "constructor";
265 out << "static int ";
267 out << _class->name << "_";
269 out << function->name << "_wrapper(HSQUIRRELVM v)\n"
272 if(_class == 0 && function->parameters.empty()
273 && function->return_type.is_void()
274 && function->type != Function::CONSTRUCTOR) {
275 out << ind << "(void) v;\n";
278 // eventually retrieve pointer to class instance
279 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
280 out << ind << ns_prefix << _class->name << "* _this;\n";
281 out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n";
284 // declare and retrieve arguments
287 for(std::vector<Parameter>::iterator p = function->parameters.begin();
288 p != function->parameters.end(); ++p) {
289 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
290 out << ind << "HSQUIRRELVM arg0 = v;\n";
294 snprintf(argname, sizeof(argname), "arg%d", i);
295 prepare_argument(p->type, i + arg_offset, argname);
303 if(!function->return_type.is_void()) {
304 function->return_type.write_c_type(out);
305 out << " return_value = ";
308 if(function->type == Function::CONSTRUCTOR) {
309 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
316 if(function->type == Function::CONSTRUCTOR) {
317 out << _class->name << "(";
319 out << function->name << "(";
321 for(size_t i = 0; i < function->parameters.size(); ++i) {
327 if(function->type == Function::CONSTRUCTOR) {
328 out << ind << "sq_setinstanceup(v, 1, _this);\n";
329 out << ind << "sq_setreleasehook(v, 1, "
330 << _class->name << "_release_hook);\n";
333 // push return value back on stack and return
334 if(function->return_type.is_void()) {
335 if(function->docu_comment.find("@SUSPEND@") != std::string::npos) {
336 out << ind << "return sq_suspendvm(v);\n";
338 out << ind << "return 0;\n";
341 push_to_stack(function->return_type, "return_value");
342 out << ind << "return 1;\n";
349 WrapperCreator::prepare_argument(const Type& type, size_t index,
350 const std::string& var)
352 if(type.ref > 0 && type.atomic_type != StringType::instance())
353 throw std::runtime_error("References not handled yet");
355 throw std::runtime_error("Pointers not handled yet");
356 if(type.atomic_type == &BasicType::INT) {
357 out << ind << "int " << var << ";\n";
358 out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
359 } else if(type.atomic_type == &BasicType::FLOAT) {
360 out << ind << "float " << var << ";\n";
361 out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
362 } else if(type.atomic_type == &BasicType::BOOL) {
363 out << ind << "SQBool " << var << ";\n";
364 out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
365 } else if(type.atomic_type == StringType::instance()) {
366 out << ind << "const char* " << var << ";\n";
367 out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
369 std::ostringstream msg;
370 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
371 throw std::runtime_error(msg.str());
376 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
378 if(type.ref > 0 && type.atomic_type != StringType::instance())
379 throw std::runtime_error("References not handled yet");
381 throw std::runtime_error("Pointers not handled yet");
383 if(type.atomic_type == &BasicType::INT) {
384 out << "sq_pushinteger(v, " << var << ");\n";
385 } else if(type.atomic_type == &BasicType::FLOAT) {
386 out << "sq_pushfloat(v, " << var << ");\n";
387 } else if(type.atomic_type == &BasicType::BOOL) {
388 out << "sq_pushbool(v, " << var << ");\n";
389 } else if(type.atomic_type == StringType::instance()) {
390 out << "sq_pushstring(v, " << var << ".c_str(), "
391 << var << ".size());\n";
393 std::ostringstream msg;
394 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
395 throw std::runtime_error(msg.str());
400 WrapperCreator::create_class_wrapper(Class* _class)
402 bool release_hook_created = false;
403 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
404 i != _class->members.end(); ++i) {
405 ClassMember* member = *i;
406 if(member->visibility != ClassMember::PUBLIC)
408 Function* function = dynamic_cast<Function*> (member);
411 if(function->type == Function::CONSTRUCTOR
412 && !release_hook_created) {
413 create_class_release_hook(_class);
414 release_hook_created = true;
416 // don't wrap destructors
417 if(function->type == Function::DESTRUCTOR)
419 create_function_wrapper(_class, function);
424 WrapperCreator::create_class_release_hook(Class* _class)
426 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
428 << ind << _class->name
429 << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n"
430 << ind << "delete _this;\n"
431 << ind << "return 0;\n"