fix namespace problems
[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->return_type.is_void()) {
318         if(function->docu_comment.find("@SUSPEND@") != std::string::npos) {
319           out << ind << "return sq_suspendvm(v);\n";
320         } else {
321           out << ind << "return 0;\n";
322         }
323     } else {
324         push_to_stack(function->return_type, "return_value");
325         out << ind << "return 1;\n";
326     }
327     out << "}\n";
328     out << "\n";
329 }
330
331 void
332 WrapperCreator::prepare_argument(const Type& type, size_t index,
333         const std::string& var)
334 {
335     if(type.ref > 0 && type.atomic_type != StringType::instance())
336         throw std::runtime_error("References not handled yet");
337     if(type.pointer > 0)
338         throw std::runtime_error("Pointers not handled yet");
339     if(type.atomic_type == &BasicType::INT) {
340         out << ind << "int " << var << ";\n";
341         out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
342     } else if(type.atomic_type == &BasicType::FLOAT) {
343         out << ind << "float " << var << ";\n";
344         out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
345     } else if(type.atomic_type == &BasicType::BOOL) {
346         out << ind << "SQBool " << var << ";\n";
347         out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
348     } else if(type.atomic_type == StringType::instance()) {
349         out << ind << "const char* " << var << ";\n";
350         out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
351     } else {
352         std::ostringstream msg;
353         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
354         throw std::runtime_error(msg.str());
355     }
356 }
357
358 void
359 WrapperCreator::push_to_stack(const Type& type, 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     out << ind;
366     if(type.atomic_type == &BasicType::INT) {
367         out << "sq_pushinteger(v, " << var << ");\n";
368     } else if(type.atomic_type == &BasicType::FLOAT) {
369         out << "sq_pushfloat(v, " << var << ");\n";
370     } else if(type.atomic_type == &BasicType::BOOL) {
371         out << "sq_pushbool(v, " << var << ");\n";
372     } else if(type.atomic_type == StringType::instance()) {
373         out << "sq_pushstring(v, " << var << ".c_str(), " 
374             << var << ".size());\n";
375     } else {
376         std::ostringstream msg;
377         msg << "Type '" << type.atomic_type->name << "' not supported yet.";
378         throw std::runtime_error(msg.str());
379     }
380 }
381
382 void
383 WrapperCreator::create_class_wrapper(Class* _class)
384 {
385     create_class_release_hook(_class);
386     create_squirrel_instance(_class);
387     for(std::vector<ClassMember*>::iterator i = _class->members.begin();
388             i != _class->members.end(); ++i) {
389         ClassMember* member = *i;
390         if(member->visibility != ClassMember::PUBLIC)
391             continue;
392         Function* function = dynamic_cast<Function*> (member);
393         if(!function)
394             continue;
395         // don't wrap destructors
396         if(function->type == Function::DESTRUCTOR)
397             continue;
398         create_function_wrapper(_class, function);
399     }
400 }
401
402 void
403 WrapperCreator::create_squirrel_instance(Class* _class)
404 {
405     out << "void create_squirrel_instance(HSQUIRRELVM v, "
406         << ns_prefix << _class->name 
407         << "* object, bool setup_releasehook)\n"
408         << "{\n"
409         << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
410         << ind << "if(sq_get(v, -2) < 0) {\n"
411         << ind << ind << "std::ostringstream msg;\n"
412         << ind << ind << "msg << \"Couldn't resolved squirrel type '"
413         << _class->name << "'\";\n"
414         << ind << ind << "throw SquirrelError(v, msg.str());\n"
415         << ind << "}\n"
416         << "\n"
417         << ind << "if(sq_createinstance(v, -1) < 0 || "
418         << "sq_setinstanceup(v, -1, object) < 0) {\n"
419         << ind << ind << "std::ostringstream msg;\n"
420         << ind << ind << "msg << \"Couldn't setup squirrel instance for "
421         << "object of type '" << _class->name << "'\";\n"
422         << ind << ind << "throw SquirrelError(v, msg.str());\n"
423         << ind << "}\n"
424         << ind << "sq_remove(v, -2);\n"
425         << "\n"
426         << ind << "if(setup_releasehook) {\n"
427         << ind << ind << "sq_setreleasehook(v, -1, "
428         << _class->name << "_release_hook);\n"
429         << ind << "}\n"
430         << "}\n";
431 }
432
433 void
434 WrapperCreator::create_class_release_hook(Class* _class)
435 {
436     out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
437         << "{\n"
438         << ind << ns_prefix << _class->name 
439         << "* _this = reinterpret_cast<" << ns_prefix << _class->name 
440         << "*> (ptr);\n"
441         << ind << "delete _this;\n"
442         << ind << "return 0;\n"
443         << "}\n"
444         << "\n";
445 }
446