hopefully fixed the crash on exit, keep sectors script bundled in the sector and...
[supertux.git] / tools / miniswig / create_wrapper.cpp
1 #include "tree.hpp"
2 #include <iostream>
3 #include <sstream>
4 #include <stdexcept>
5 #include "create_wrapper.hpp"
6 #include "globals.hpp"
7
8 void
9 WrapperCreator::create_wrapper(Namespace* ns)
10 {
11     std::string fromfile = original_file != "" ? original_file : inputfile;
12
13     if(selected_namespace != "") {
14         ns_prefix = selected_namespace;
15         ns_prefix += "::";
16     }
17
18     // hpp file
19     hppout
20         << "/**\n"
21         << " * WARNING: This file is automatically generated from:\n"
22         << " *  '" << fromfile << "'\n"
23         << " * DO NOT CHANGE\n"
24         << " */\n"
25         << "#ifndef __" << modulename << "_WRAPPER_H__\n"
26         << "#define __" << modulename << "_WRAPPER_H__\n"
27         << "\n"
28         << "#include <squirrel.h>\n"
29         << "#include \"wrapper.interface.hpp\"\n"
30         << "\n"
31         << "namespace Scripting\n"
32         << "{\n"
33         << "\n";
34
35     hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
36            << "\n";
37
38     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
39             i != ns->types.end(); ++i) {
40         AtomicType* type = *i;
41         Class* _class = dynamic_cast<Class*> (type);                 
42         if(_class == 0)
43             continue;
44
45         hppout << "void create_squirrel_instance(HSQUIRRELVM v, "
46                << ns_prefix << _class->name
47                << "* object, bool setup_releasehook = false);\n";
48     }
49     hppout <<"\n"
50            << "}\n"
51            << "\n"
52            << "#endif\n"
53            << "\n";
54     
55     // cpp header
56     out << "/**\n"
57         << " * WARNING: This file is automatically generated from:\n"
58         << " *  '" << fromfile << "'\n"
59         << " * DO NOT CHANGE\n"
60         << " */\n"
61         << "#include <config.h>\n"
62         << "\n"
63         << "#include <new>\n"
64         << "#include <assert.h>\n"
65         << "#include <string>\n"
66         << "#include <sstream>\n"
67         << "#include <squirrel.h>\n"
68         << "#include \"squirrel_error.hpp\"\n"
69         << "#include \"wrapper.interface.hpp\"\n"
70         << "\n"
71         << "namespace Scripting\n"
72         << "{\n"
73         << "namespace Wrapper\n"
74         << "{\n"
75         << "\n";
76
77     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
78             i != ns->types.end(); ++i) {
79         AtomicType* type = *i;
80         Class* _class = dynamic_cast<Class*> (type);
81         if(_class != 0)
82             create_class_wrapper(_class);
83     }
84     for(std::vector<Function*>::iterator i = ns->functions.begin();
85             i != ns->functions.end(); ++i) {
86         create_function_wrapper(0, *i);
87     }
88
89     out << "} // end of namespace Wrapper\n";
90     out << "\n";
91
92     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
93             i != ns->types.end(); ++i) {                             
94         AtomicType* type = *i;
95         Class* _class = dynamic_cast<Class*> (type);
96         if(_class != 0)
97             create_squirrel_instance(_class);
98     }
99     
100     out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
101         << "{\n"
102         << ind << "using namespace Wrapper;\n"
103         << "\n";
104
105     create_register_constants_code(ns);
106     create_register_functions_code(ns);
107     create_register_classes_code(ns);
108
109     out << "}\n"
110         << "\n"
111         << "} // end of namespace Scripting\n"
112         << "\n";
113 }
114
115 void
116 WrapperCreator::create_register_function_code(Function* function, Class* _class)
117 {
118     if(function->type == Function::DESTRUCTOR)
119         return;
120     
121     out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
122     out << ind << "sq_newclosure(v, &" 
123         << (_class != 0 ? _class->name + "_" : "") << function->name 
124         << "_wrapper, 0);\n";
125     create_register_slot_code("function", function->name);
126     out << "\n";                                                              
127 }
128
129 void
130 WrapperCreator::create_register_functions_code(Namespace* ns)
131 {
132     for(std::vector<Function*>::iterator i = ns->functions.begin();
133             i != ns->functions.end(); ++i) {
134         Function* function = *i;
135         create_register_function_code(function, 0);
136     }
137 }
138
139 void
140 WrapperCreator::create_register_classes_code(Namespace* ns)
141 {
142     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
143             i != ns->types.end(); ++i) {
144         AtomicType* type = *i;
145         Class* _class = dynamic_cast<Class*> (type);                 
146         if(_class == 0)
147             continue;
148         if(_class->super_classes.size() > 0)
149             continue;
150
151         create_register_class_code(_class);
152     }
153 }
154
155 void
156 WrapperCreator::create_register_class_code(Class* _class)
157 {
158     out << ind << "// Register class " << _class->name << "\n";
159     out << ind << "sq_pushstring(v, \"" 
160         << _class->name << "\", -1);\n";    
161     
162     if(_class->super_classes.size() > 0) {
163         if(_class->super_classes.size() > 1) {
164             std::ostringstream msg;
165             msg << "Multiple inheritance not supported (at class '"
166                 << _class->name << "')";
167             throw std::runtime_error(msg.str());
168         }
169         
170         out << ind << "sq_pushstring(v, \""
171             << _class->super_classes[0]->name << "\", -1);\n";
172         out << ind << "sq_get(v, -3);\n";
173     }
174     out << ind << "if(sq_newclass(v, "
175         << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
176         << ") < 0) {\n";
177     out << ind << ind << "std::ostringstream msg;\n";
178     out << ind << ind << "msg << \"Couldn't create new class '" 
179         << _class->name << "'\";\n";
180     out << ind << ind << "throw SquirrelError(v, msg.str());\n";
181     out << ind << "}\n";
182
183     for(std::vector<ClassMember*>::iterator i = _class->members.begin();
184             i != _class->members.end(); ++i) {
185         ClassMember* member = *i;
186         if(member->visibility != ClassMember::PUBLIC)
187             continue;
188         Function* function = dynamic_cast<Function*> (member);
189         if(function) {
190             create_register_function_code(function, _class);
191         }
192         Field* field = dynamic_cast<Field*> (member);
193         if(field) {
194             create_register_constant_code(field);
195         }
196     }
197
198     create_register_slot_code("class", _class->name);
199     out << "\n";
200     
201     for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
202             i != _class->sub_classes.end(); ++i) {
203         Class* _class = *i;
204         create_register_class_code(_class);
205     }
206 }
207
208 void
209 WrapperCreator::create_register_constants_code(Namespace* ns)
210 {
211     for(std::vector<Field*>::iterator i = ns->fields.begin();
212             i != ns->fields.end(); ++i) {
213         Field* field = *i;
214         create_register_constant_code(field);
215     }
216 }
217
218 void
219 WrapperCreator::create_register_constant_code(Field* field)
220 {
221     if(!field->has_const_value)
222         return;
223     out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
224     if(field->type->atomic_type == &BasicType::INT) {
225         out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
226     } else if(field->type->atomic_type == &BasicType::FLOAT) {
227         out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
228     } else if(field->type->atomic_type == StringType::instance()) {
229         out << ind << "sq_pushstring(v, \""
230             << field->const_string_value << "\", -1);\n";
231     } else {
232       throw std::runtime_error("Constant is not int, float or string");
233     }
234     create_register_slot_code("constant", field->name);
235     out << "\n";
236 }
237
238 void
239 WrapperCreator::create_register_slot_code(const std::string& what,
240                                           const std::string& name)
241 {
242     out << ind << "if(SQ_FAILED(sq_createslot(v, -3))) {\n";
243     out << ind << ind << "throw SquirrelError(v, \""
244         << "Couldn't register " << what << " '" << name << "'\");\n";
245     out << ind << "}\n";
246 }
247
248 void
249 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
250 {
251     if(function->type == Function::DESTRUCTOR)
252         assert(false);
253
254     std::string ns_prefix;
255     if(selected_namespace != "")
256         ns_prefix = selected_namespace + "::";
257     if(function->type == Function::CONSTRUCTOR)
258         function->name = "constructor";
259
260     out << "static int ";
261     if(_class != 0) {
262         out << _class->name << "_";
263     }
264     out << function->name << "_wrapper(HSQUIRRELVM vm)\n"
265         << "{\n";
266     // avoid warning...
267     if(_class == 0 && function->parameters.empty() 
268             && function->return_type.is_void()
269             && function->type != Function::CONSTRUCTOR) {
270         out << ind << "(void) vm;\n";
271     }
272     
273     // retrieve pointer to class instance
274     if(_class != 0 && function->type != Function::CONSTRUCTOR) {
275         out << ind << ns_prefix <<  _class->name << "* _this;\n";
276         out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, reinterpret_cast<SQUserPointer*> (&_this), 0))) {\n";
277         out << ind << ind << "sq_throwerror(vm, _SC(\"'" << function->name << "' called without instance\"));\n";
278         out << ind << ind << "return SQ_ERROR;\n";
279         out << ind << "}\n";
280     }
281
282     // custom function?
283     if(function->custom) {
284         if(function->type != Function::FUNCTION)
285             throw std::runtime_error(
286                     "custom not allow constructor+destructor yet");
287         if(function->return_type.atomic_type != &BasicType::INT)
288             throw std::runtime_error("custom function has to return int");
289         if(function->parameters.size() != 1)
290             throw std::runtime_error(
291                     "custom function must have 1 HSQUIRRELVM parameter");
292
293         out << ind << "return ";
294         if(_class != 0)
295             out << "_this->";
296         else
297             out << ns_prefix;
298         out << function->name << "(vm);\n";
299         out << "}\n";
300         out << "\n";
301         return;
302     }
303     
304     // declare and retrieve arguments
305     int i = 0;
306     int arg_offset = 2;
307     for(std::vector<Parameter>::iterator p = function->parameters.begin();
308             p != function->parameters.end(); ++p) {
309         if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
310             out << ind << "HSQUIRRELVM arg0 = vm;\n";
311             arg_offset--;
312         } else {
313             char argname[64];
314             snprintf(argname, sizeof(argname), "arg%d", i);
315             prepare_argument(p->type, i + arg_offset, argname);
316         }
317         ++i;
318     }
319     
320     // call function
321     out << ind << "\n";
322     out << ind << "try {\n";
323     out << ind << ind;
324     if(!function->return_type.is_void()) {
325         function->return_type.write_c_type(out);
326         out << " return_value = ";
327     }
328     if(_class != 0) {
329         if(function->type == Function::CONSTRUCTOR) {
330             out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
331         } else {
332             out << "_this->";
333         }
334     } else {
335         out << ns_prefix;
336     }
337     if(function->type == Function::CONSTRUCTOR) {
338         out << _class->name << "(";
339     } else {
340         out << function->name << "(";
341     }
342     for(size_t i = 0; i < function->parameters.size(); ++i) {
343         if(i != 0)
344             out << ", ";
345         out << "arg" << i;
346     }
347     out << ");\n";
348     if(function->type == Function::CONSTRUCTOR) {
349         out << ind << "if(SQ_FAILED(sq_setinstanceup(vm, 1, _this))) {\n";
350         out << ind << ind << "sq_throwerror(vm, _SC(\"Couldn't setup instance of '" << _class->name << "' class\"));\n";
351         out << ind << ind << "return SQ_ERROR;\n";
352         out << ind << "}\n";
353         out << ind << "sq_setreleasehook(vm, 1, " 
354             << _class->name << "_release_hook);\n";
355     }
356     out << ind << "\n";
357     // push return value back on stack and return
358     if(function->suspend) {
359         if(!function->return_type.is_void()) {
360             std::stringstream msg;
361             msg << "Function '" << function->name << "' declared as suspend"
362                 << " but has a return value.";
363             throw std::runtime_error(msg.str());
364         }
365         out << ind << ind << "return sq_suspendvm(vm);\n";
366     } else if(function->return_type.is_void()) {
367         out << ind << ind << "return 0;\n";
368     } else {
369         push_to_stack(function->return_type, "return_value");
370         out << ind << ind << "return 1;\n";
371     }
372
373     out << ind << "\n";
374     out << ind << "} catch(std::exception& e) {\n";
375     out << ind << ind << "sq_throwerror(vm, e.what());\n";
376     out << ind << ind << "return SQ_ERROR;\n";
377     out << ind << "} catch(...) {\n";
378     out << ind << ind << "sq_throwerror(vm, _SC(\"Unexpected exception while executing function '" << function->name << "'\"));\n";
379     out << ind << ind << "return SQ_ERROR;\n";
380     out << ind << "}\n";
381     out << ind << "\n";
382     
383     out << "}\n";
384     out << "\n";
385 }
386
387 void
388 WrapperCreator::prepare_argument(const Type& type, size_t index,
389         const std::string& var)
390 {
391     if(type.ref > 0 && type.atomic_type != StringType::instance())
392         throw std::runtime_error("References not handled yet");
393     if(type.pointer > 0)
394         throw std::runtime_error("Pointers not handled yet");
395     if(type.atomic_type == &BasicType::INT) {
396         out << ind << "int " << var << ";\n";
397         out << ind << "if(SQ_FAILED(sq_getinteger(vm, " << index << ", &" << var << "))) {\n";
398         out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not an integer\"));\n";
399         out << ind << ind << "return SQ_ERROR;\n";
400         out << ind << "}\n";
401     } else if(type.atomic_type == &BasicType::FLOAT) {
402         out << ind << "float " << var << ";\n";
403         out << ind << "if(SQ_FAILED(sq_getfloat(vm, " << index << ", &" << var << "))) {\n";
404         out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a float\"));\n";
405         out << ind << ind << "return SQ_ERROR;\n";
406         out << ind << "}\n";
407     } else if(type.atomic_type == &BasicType::BOOL) {
408         out << ind << "SQBool " << var << ";\n";
409         out << ind << "if(SQ_FAILED(sq_getbool(vm, " << index << ", &" << var << "))) {\n";
410         out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a bool\"));\n";
411         out << ind << ind << "return SQ_ERROR;\n";
412         out << ind << "}\n";
413     } else if(type.atomic_type == StringType::instance()) {
414         out << ind << "const char* " << var << ";\n";
415         out << ind << "if(SQ_FAILED(sq_getstring(vm, " << index << ", &" << var << "))) {\n";
416         out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a string\"));\n";
417         out << ind << ind << "return SQ_ERROR;\n";
418         out << ind << "}\n";
419     } else {
420         std::ostringstream msg;
421         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
422         throw std::runtime_error(msg.str());
423     }
424 }
425
426 void
427 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
428 {
429     if(type.ref > 0 && type.atomic_type != StringType::instance())
430         throw std::runtime_error("References not handled yet");
431     if(type.pointer > 0)
432         throw std::runtime_error("Pointers not handled yet");
433     out << ind << ind;
434     if(type.atomic_type == &BasicType::INT) {
435         out << "sq_pushinteger(vm, " << var << ");\n";
436     } else if(type.atomic_type == &BasicType::FLOAT) {
437         out << "sq_pushfloat(vm, " << var << ");\n";
438     } else if(type.atomic_type == &BasicType::BOOL) {
439         out << "sq_pushbool(vm, " << var << ");\n";
440     } else if(type.atomic_type == StringType::instance()) {
441         out << "sq_pushstring(vm, " << var << ".c_str(), " 
442             << var << ".size());\n";
443     } else {
444         std::ostringstream msg;
445         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
446         throw std::runtime_error(msg.str());
447     }
448 }
449
450 void
451 WrapperCreator::create_class_wrapper(Class* _class)
452 {
453     create_class_release_hook(_class);
454     for(std::vector<ClassMember*>::iterator i = _class->members.begin();
455             i != _class->members.end(); ++i) {
456         ClassMember* member = *i;
457         if(member->visibility != ClassMember::PUBLIC)
458             continue;
459         Function* function = dynamic_cast<Function*> (member);
460         if(!function)
461             continue;
462         // don't wrap destructors
463         if(function->type == Function::DESTRUCTOR)
464             continue;
465         create_function_wrapper(_class, function);
466     }
467 }
468
469 void
470 WrapperCreator::create_squirrel_instance(Class* _class)
471 {
472     out << "void create_squirrel_instance(HSQUIRRELVM v, "
473         << ns_prefix << _class->name 
474         << "* object, bool setup_releasehook)\n"
475         << "{\n"
476         << ind << "using namespace Wrapper;\n"
477         << "\n"
478         << ind << "sq_pushroottable(v);\n"
479         << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
480         << ind << "if(SQ_FAILED(sq_get(v, -2))) {\n"
481         << ind << ind << "std::ostringstream msg;\n"
482         << ind << ind << "msg << \"Couldn't resolved squirrel type '"
483         << _class->name << "'\";\n"
484         << ind << ind << "throw SquirrelError(v, msg.str());\n"
485         << ind << "}\n"
486         << "\n"
487         << ind << "if(SQ_FAILED(sq_createinstance(v, -1)) || "
488         << "SQ_FAILED(sq_setinstanceup(v, -1, object))) {\n"
489         << ind << ind << "std::ostringstream msg;\n"
490         << ind << ind << "msg << \"Couldn't setup squirrel instance for "
491         << "object of type '" << _class->name << "'\";\n"
492         << ind << ind << "throw SquirrelError(v, msg.str());\n"
493         << ind << "}\n"
494         << ind << "sq_remove(v, -2); // remove object name\n"
495         << "\n"
496         << ind << "if(setup_releasehook) {\n"
497         << ind << ind << "sq_setreleasehook(v, -1, "
498         << _class->name << "_release_hook);\n"
499         << ind << "}\n"
500         << "\n"
501         << ind << "sq_remove(v, -2); // remove root table\n"
502         << "}\n"
503         << "\n";
504 }
505
506 void
507 WrapperCreator::create_class_release_hook(Class* _class)
508 {
509     out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
510         << "{\n"
511         << ind << ns_prefix << _class->name 
512         << "* _this = reinterpret_cast<" << ns_prefix << _class->name 
513         << "*> (ptr);\n"
514         << ind << "delete _this;\n"
515         << ind << "return 0;\n"
516         << "}\n"
517         << "\n";
518 }
519