7e44cd678a5576a9b568f0e45d303d16e81fb454
[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 SquirrelWrapper\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 <squirrel.h>\n"
67         << "#include \"wrapper_util.hpp\"\n"
68         << "#include \"wrapper.interface.hpp\"\n"
69         << "\n"
70         << "namespace SquirrelWrapper\n"
71         << "{\n"
72         << "\n";
73
74     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
75             i != ns->types.end(); ++i) {
76         AtomicType* type = *i;
77         Class* _class = dynamic_cast<Class*> (type);
78         if(_class != 0)
79             create_class_wrapper(_class);
80     }
81     for(std::vector<Function*>::iterator i = ns->functions.begin();
82             i != ns->functions.end(); ++i) {
83         create_function_wrapper(0, *i);
84     }
85
86     out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
87         << "{\n"
88         << ind << "sq_pushroottable(v);\n";
89
90     create_register_constants_code(ns);
91     create_register_functions_code(ns);
92     create_register_classes_code(ns);
93
94     out << ind << "sq_pop(v, 1);\n"
95         << "}\n"
96         << "\n"
97         << "}\n"
98         << "\n";
99 }
100
101 void
102 WrapperCreator::create_register_function_code(Function* function, Class* _class)
103 {
104     if(function->type == Function::DESTRUCTOR)
105         return;
106     
107     out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
108     out << ind << "sq_newclosure(v, &" 
109         << (_class != 0 ? _class->name + "_" : "") << function->name 
110         << "_wrapper, 0);\n";
111     create_register_slot_code("function", function->name);
112     out << "\n";                                                              
113 }
114
115 void
116 WrapperCreator::create_register_functions_code(Namespace* ns)
117 {
118     for(std::vector<Function*>::iterator i = ns->functions.begin();
119             i != ns->functions.end(); ++i) {
120         Function* function = *i;
121         create_register_function_code(function, 0);
122     }
123 }
124
125 void
126 WrapperCreator::create_register_classes_code(Namespace* ns)
127 {
128     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
129             i != ns->types.end(); ++i) {
130         AtomicType* type = *i;
131         Class* _class = dynamic_cast<Class*> (type);                 
132         if(_class == 0)
133             continue;
134         if(_class->super_classes.size() > 0)
135             continue;
136
137         create_register_class_code(_class);
138     }
139 }
140
141 void
142 WrapperCreator::create_register_class_code(Class* _class)
143 {
144     out << ind << "// Register class " << _class->name << "\n";
145     out << ind << "sq_pushstring(v, \"" 
146         << _class->name << "\", -1);\n";    
147     
148     if(_class->super_classes.size() > 0) {
149         if(_class->super_classes.size() > 1) {
150             std::ostringstream msg;
151             msg << "Multiple inheritance not supported (at class '"
152                 << _class->name << "')";
153             throw std::runtime_error(msg.str());
154         }
155         
156         out << ind << "sq_pushstring(v, \""
157             << _class->super_classes[0]->name << "\", -1);\n";
158         out << ind << "sq_get(v, -3);\n";
159     }
160     out << ind << "if(sq_newclass(v, "
161         << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
162         << ") < 0) {\n";
163     out << ind << ind << "std::ostringstream msg;\n";
164     out << ind << ind << "msg << \"Couldn't create new class '" 
165         << _class->name << "'\";\n";
166     out << ind << ind << "throw SquirrelError(v, msg.str());\n";
167     out << ind << "}\n";
168
169     for(std::vector<ClassMember*>::iterator i = _class->members.begin();
170             i != _class->members.end(); ++i) {
171         ClassMember* member = *i;
172         if(member->visibility != ClassMember::PUBLIC)
173             continue;
174         Function* function = dynamic_cast<Function*> (member);
175         if(function) {
176             create_register_function_code(function, _class);
177         }
178         Field* field = dynamic_cast<Field*> (member);
179         if(field) {
180             create_register_constant_code(field);
181         }
182     }
183
184     create_register_slot_code("class", _class->name);
185     out << "\n";
186     
187     for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
188             i != _class->sub_classes.end(); ++i) {
189         Class* _class = *i;
190         create_register_class_code(_class);
191     }
192 }
193
194 void
195 WrapperCreator::create_register_constants_code(Namespace* ns)
196 {
197     for(std::vector<Field*>::iterator i = ns->fields.begin();
198             i != ns->fields.end(); ++i) {
199         Field* field = *i;
200         create_register_constant_code(field);
201     }
202 }
203
204 void
205 WrapperCreator::create_register_constant_code(Field* field)
206 {
207     if(!field->has_const_value)
208         return;
209     out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
210     if(field->type->atomic_type == &BasicType::INT) {
211         out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
212     } else if(field->type->atomic_type == &BasicType::FLOAT) {
213         out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
214     } else if(field->type->atomic_type == StringType::instance()) {
215         out << ind << "sq_pushstring(v, \""
216             << field->const_string_value << "\", -1);\n";
217     } else {
218       throw std::runtime_error("Constant is not int, float or string");
219     }
220     create_register_slot_code("constant", field->name);
221     out << "\n";
222 }
223
224 void
225 WrapperCreator::create_register_slot_code(const std::string& what,
226                                           const std::string& name)
227 {
228     out << ind << "if(sq_createslot(v, -3) < 0) {\n";
229     out << ind << ind << "std::ostringstream msg;\n";
230     out << ind << ind << "msg << \"Couldn't register " << what << "'"
231         << name << "'\";\n";
232     out << ind << ind << "throw SquirrelError(v, msg.str());\n";
233     out << ind << "}\n";
234 }
235
236 void
237 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
238 {
239     if(function->type == Function::DESTRUCTOR)
240         assert(false);
241
242     std::string ns_prefix;
243     if(selected_namespace != "")
244         ns_prefix = selected_namespace + "::";
245     if(function->type == Function::CONSTRUCTOR)
246         function->name = "constructor";
247
248     out << "static int ";
249     if(_class != 0) {
250         out << _class->name << "_";
251     }
252     out << function->name << "_wrapper(HSQUIRRELVM v)\n"
253         << "{\n";
254     // avoid warning...
255     if(_class == 0 && function->parameters.empty() 
256             && function->return_type.is_void()
257             && function->type != Function::CONSTRUCTOR) {
258         out << ind << "(void) v;\n";
259     }
260     
261     // eventually retrieve pointer to class instance
262     if(_class != 0 && function->type != Function::CONSTRUCTOR) {
263         out << ind << ns_prefix <<  _class->name << "* _this;\n";
264         out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n";
265     }
266
267     // custom function?
268     if(function->custom) {
269         if(function->type != Function::FUNCTION)
270             throw std::runtime_error(
271                     "custom not allow constructor+destructor yet");
272         if(function->return_type.atomic_type != &BasicType::INT)
273             throw std::runtime_error("custom function has to return int");
274         if(function->parameters.size() != 1)
275             throw std::runtime_error(
276                     "custom function must have 1 HSQUIRRELVM parameter");
277
278         out << ind << "return ";
279         if(_class != 0)
280             out << "_this->";
281         else
282             out << ns_prefix;
283         out << function->name << "(v);\n";
284         out << "}\n";
285         out << "\n";
286         return;
287     }
288     
289     // declare and retrieve arguments
290     int i = 0;
291     int arg_offset = 2;
292     for(std::vector<Parameter>::iterator p = function->parameters.begin();
293             p != function->parameters.end(); ++p) {
294         if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
295             out << ind << "HSQUIRRELVM arg0 = v;\n";
296             arg_offset--;
297         } else {
298             char argname[64];
299             snprintf(argname, sizeof(argname), "arg%d", i);
300             prepare_argument(p->type, i + arg_offset, argname);
301         }
302         ++i;
303     }
304     
305     // call function
306     out << ind << "\n";
307     out << ind;
308     if(!function->return_type.is_void()) {
309         function->return_type.write_c_type(out);
310         out << " return_value = ";
311     }
312     if(_class != 0) {
313         if(function->type == Function::CONSTRUCTOR) {
314             out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
315         } else {
316             out << "_this->";
317         }
318     } else {
319         out << ns_prefix;
320     }
321     if(function->type == Function::CONSTRUCTOR) {
322         out << _class->name << "(";
323     } else {
324         out << function->name << "(";
325     }
326     for(size_t i = 0; i < function->parameters.size(); ++i) {
327         if(i != 0)
328             out << ", ";
329         out << "arg" << i;
330     }
331     out << ");\n";
332     if(function->type == Function::CONSTRUCTOR) {
333         out << ind << "sq_setinstanceup(v, 1, _this);\n";
334         out << ind << "sq_setreleasehook(v, 1, " 
335             << _class->name << "_release_hook);\n";
336     }
337     out << ind << "\n";
338     // push return value back on stack and return
339     if(function->suspend) {
340         if(!function->return_type.is_void()) {
341             std::stringstream msg;
342             msg << "Function '" << function->name << "' declared as suspend"
343                 << " but has a return value.";
344             throw std::runtime_error(msg.str());
345         }
346         out << ind << "return sq_suspendvm(v);\n";
347     } else if(function->return_type.is_void()) {
348         out << ind << "return 0;\n";
349     } else {
350         push_to_stack(function->return_type, "return_value");
351         out << ind << "return 1;\n";
352     }
353     out << "}\n";
354     out << "\n";
355 }
356
357 void
358 WrapperCreator::prepare_argument(const Type& type, size_t index,
359         const std::string& var)
360 {
361     if(type.ref > 0 && type.atomic_type != StringType::instance())
362         throw std::runtime_error("References not handled yet");
363     if(type.pointer > 0)
364         throw std::runtime_error("Pointers not handled yet");
365     if(type.atomic_type == &BasicType::INT) {
366         out << ind << "int " << var << ";\n";
367         out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
368     } else if(type.atomic_type == &BasicType::FLOAT) {
369         out << ind << "float " << var << ";\n";
370         out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
371     } else if(type.atomic_type == &BasicType::BOOL) {
372         out << ind << "SQBool " << var << ";\n";
373         out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
374     } else if(type.atomic_type == StringType::instance()) {
375         out << ind << "const char* " << var << ";\n";
376         out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
377     } else {
378         std::ostringstream msg;
379         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
380         throw std::runtime_error(msg.str());
381     }
382 }
383
384 void
385 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
386 {
387     if(type.ref > 0 && type.atomic_type != StringType::instance())
388         throw std::runtime_error("References not handled yet");
389     if(type.pointer > 0)
390         throw std::runtime_error("Pointers not handled yet");
391     out << ind;
392     if(type.atomic_type == &BasicType::INT) {
393         out << "sq_pushinteger(v, " << var << ");\n";
394     } else if(type.atomic_type == &BasicType::FLOAT) {
395         out << "sq_pushfloat(v, " << var << ");\n";
396     } else if(type.atomic_type == &BasicType::BOOL) {
397         out << "sq_pushbool(v, " << var << ");\n";
398     } else if(type.atomic_type == StringType::instance()) {
399         out << "sq_pushstring(v, " << var << ".c_str(), " 
400             << var << ".size());\n";
401     } else {
402         std::ostringstream msg;
403         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
404         throw std::runtime_error(msg.str());
405     }
406 }
407
408 void
409 WrapperCreator::create_class_wrapper(Class* _class)
410 {
411     create_class_release_hook(_class);
412     create_squirrel_instance(_class);
413     for(std::vector<ClassMember*>::iterator i = _class->members.begin();
414             i != _class->members.end(); ++i) {
415         ClassMember* member = *i;
416         if(member->visibility != ClassMember::PUBLIC)
417             continue;
418         Function* function = dynamic_cast<Function*> (member);
419         if(!function)
420             continue;
421         // don't wrap destructors
422         if(function->type == Function::DESTRUCTOR)
423             continue;
424         create_function_wrapper(_class, function);
425     }
426 }
427
428 void
429 WrapperCreator::create_squirrel_instance(Class* _class)
430 {
431     out << "void create_squirrel_instance(HSQUIRRELVM v, "
432         << ns_prefix << _class->name 
433         << "* object, bool setup_releasehook)\n"
434         << "{\n"
435         << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
436         << ind << "if(sq_get(v, -2) < 0) {\n"
437         << ind << ind << "std::ostringstream msg;\n"
438         << ind << ind << "msg << \"Couldn't resolved squirrel type '"
439         << _class->name << "'\";\n"
440         << ind << ind << "throw SquirrelError(v, msg.str());\n"
441         << ind << "}\n"
442         << "\n"
443         << ind << "if(sq_createinstance(v, -1) < 0 || "
444         << "sq_setinstanceup(v, -1, object) < 0) {\n"
445         << ind << ind << "std::ostringstream msg;\n"
446         << ind << ind << "msg << \"Couldn't setup squirrel instance for "
447         << "object of type '" << _class->name << "'\";\n"
448         << ind << ind << "throw SquirrelError(v, msg.str());\n"
449         << ind << "}\n"
450         << ind << "sq_remove(v, -2);\n"
451         << "\n"
452         << ind << "if(setup_releasehook) {\n"
453         << ind << ind << "sq_setreleasehook(v, -1, "
454         << _class->name << "_release_hook);\n"
455         << ind << "}\n"
456         << "}\n"
457         << "\n";
458 }
459
460 void
461 WrapperCreator::create_class_release_hook(Class* _class)
462 {
463     out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
464         << "{\n"
465         << ind << ns_prefix << _class->name 
466         << "* _this = reinterpret_cast<" << ns_prefix << _class->name 
467         << "*> (ptr);\n"
468         << ind << "delete _this;\n"
469         << ind << "return 0;\n"
470         << "}\n"
471         << "\n";
472 }
473