miniswig suspend is now an attribute appended to functions
[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     // declare and retrieve arguments
268     int i = 0;
269     int arg_offset = 2;
270     for(std::vector<Parameter>::iterator p = function->parameters.begin();
271             p != function->parameters.end(); ++p) {
272         if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
273             out << ind << "HSQUIRRELVM arg0 = v;\n";
274             arg_offset--;
275         } else {
276             char argname[64];
277             snprintf(argname, sizeof(argname), "arg%d", i);
278             prepare_argument(p->type, i + arg_offset, argname);
279         }
280         ++i;
281     }
282     
283     // call function
284     out << ind << "\n";
285     out << ind;
286     if(!function->return_type.is_void()) {
287         function->return_type.write_c_type(out);
288         out << " return_value = ";
289     }
290     if(_class != 0) {
291         if(function->type == Function::CONSTRUCTOR) {
292             out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
293         } else {
294             out << "_this->";
295         }
296     } else {
297         out << ns_prefix;
298     }
299     if(function->type == Function::CONSTRUCTOR) {
300         out << _class->name << "(";
301     } else {
302         out << function->name << "(";
303     }
304     for(size_t i = 0; i < function->parameters.size(); ++i) {
305         if(i != 0)
306             out << ", ";
307         out << "arg" << i;
308     }
309     out << ");\n";
310     if(function->type == Function::CONSTRUCTOR) {
311         out << ind << "sq_setinstanceup(v, 1, _this);\n";
312         out << ind << "sq_setreleasehook(v, 1, " 
313             << _class->name << "_release_hook);\n";
314     }
315     out << ind << "\n";
316     // push return value back on stack and return
317     if(function->suspend) {
318         if(!function->return_type.is_void()) {
319             std::stringstream msg;
320             msg << "Function '" << function->name << "' declared as suspend"
321                 << " but has a return value.";
322             throw std::runtime_error(msg.str());
323         }
324         out << ind << "return sq_suspendvm(v);\n";
325     } else if(function->return_type.is_void()) {
326         out << ind << "return 0;\n";
327     } else {
328         push_to_stack(function->return_type, "return_value");
329         out << ind << "return 1;\n";
330     }
331     out << "}\n";
332     out << "\n";
333 }
334
335 void
336 WrapperCreator::prepare_argument(const Type& type, size_t index,
337         const std::string& var)
338 {
339     if(type.ref > 0 && type.atomic_type != StringType::instance())
340         throw std::runtime_error("References not handled yet");
341     if(type.pointer > 0)
342         throw std::runtime_error("Pointers not handled yet");
343     if(type.atomic_type == &BasicType::INT) {
344         out << ind << "int " << var << ";\n";
345         out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
346     } else if(type.atomic_type == &BasicType::FLOAT) {
347         out << ind << "float " << var << ";\n";
348         out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
349     } else if(type.atomic_type == &BasicType::BOOL) {
350         out << ind << "SQBool " << var << ";\n";
351         out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
352     } else if(type.atomic_type == StringType::instance()) {
353         out << ind << "const char* " << var << ";\n";
354         out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
355     } else {
356         std::ostringstream msg;
357         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
358         throw std::runtime_error(msg.str());
359     }
360 }
361
362 void
363 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
364 {
365     if(type.ref > 0 && type.atomic_type != StringType::instance())
366         throw std::runtime_error("References not handled yet");
367     if(type.pointer > 0)
368         throw std::runtime_error("Pointers not handled yet");
369     out << ind;
370     if(type.atomic_type == &BasicType::INT) {
371         out << "sq_pushinteger(v, " << var << ");\n";
372     } else if(type.atomic_type == &BasicType::FLOAT) {
373         out << "sq_pushfloat(v, " << var << ");\n";
374     } else if(type.atomic_type == &BasicType::BOOL) {
375         out << "sq_pushbool(v, " << var << ");\n";
376     } else if(type.atomic_type == StringType::instance()) {
377         out << "sq_pushstring(v, " << var << ".c_str(), " 
378             << var << ".size());\n";
379     } else {
380         std::ostringstream msg;
381         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
382         throw std::runtime_error(msg.str());
383     }
384 }
385
386 void
387 WrapperCreator::create_class_wrapper(Class* _class)
388 {
389     create_class_release_hook(_class);
390     create_squirrel_instance(_class);
391     for(std::vector<ClassMember*>::iterator i = _class->members.begin();
392             i != _class->members.end(); ++i) {
393         ClassMember* member = *i;
394         if(member->visibility != ClassMember::PUBLIC)
395             continue;
396         Function* function = dynamic_cast<Function*> (member);
397         if(!function)
398             continue;
399         // don't wrap destructors
400         if(function->type == Function::DESTRUCTOR)
401             continue;
402         create_function_wrapper(_class, function);
403     }
404 }
405
406 void
407 WrapperCreator::create_squirrel_instance(Class* _class)
408 {
409     out << "void create_squirrel_instance(HSQUIRRELVM v, "
410         << ns_prefix << _class->name 
411         << "* object, bool setup_releasehook)\n"
412         << "{\n"
413         << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
414         << ind << "if(sq_get(v, -2) < 0) {\n"
415         << ind << ind << "std::ostringstream msg;\n"
416         << ind << ind << "msg << \"Couldn't resolved squirrel type '"
417         << _class->name << "'\";\n"
418         << ind << ind << "throw SquirrelError(v, msg.str());\n"
419         << ind << "}\n"
420         << "\n"
421         << ind << "if(sq_createinstance(v, -1) < 0 || "
422         << "sq_setinstanceup(v, -1, object) < 0) {\n"
423         << ind << ind << "std::ostringstream msg;\n"
424         << ind << ind << "msg << \"Couldn't setup squirrel instance for "
425         << "object of type '" << _class->name << "'\";\n"
426         << ind << ind << "throw SquirrelError(v, msg.str());\n"
427         << ind << "}\n"
428         << ind << "sq_remove(v, -2);\n"
429         << "\n"
430         << ind << "if(setup_releasehook) {\n"
431         << ind << ind << "sq_setreleasehook(v, -1, "
432         << _class->name << "_release_hook);\n"
433         << ind << "}\n"
434         << "}\n";
435 }
436
437 void
438 WrapperCreator::create_class_release_hook(Class* _class)
439 {
440     out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
441         << "{\n"
442         << ind << ns_prefix << _class->name 
443         << "* _this = reinterpret_cast<" << ns_prefix << _class->name 
444         << "*> (ptr);\n"
445         << ind << "delete _this;\n"
446         << ind << "return 0;\n"
447         << "}\n"
448         << "\n";
449 }
450