fix handling of multiple arguments
[libt2n] / codegen / main.cpp
CommitLineData
e50cc1d6
JT
1#include <libxml++/libxml++.h>
2#include <cassert>
3#include <iostream>
060fbd87
JT
4#include <set>
5#include <fstream>
9d3993e5 6#include <list>
e50cc1d6
JT
7
8void print_indentation(unsigned int indentation)
9{
10 for(unsigned int i = 0; i < indentation; ++i)
11 std::cout << " ";
12}
13
e035276b
JT
14std::string
15toupper(std::string s) {
16 for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]);
17 return s;
18}
19
20std::string
21replace(std::string s, char f, char r) {
22 for (unsigned i=0; i<s.length(); ++i) if (s[i]==f) s[i]=r;
23 return s;
24}
25
26std::string
27strip(std::string s, std::string prefix)
28{
29 std::string error;
30 if ( (prefix.length()>s.length() ) || ( std::string(s,0,prefix.length())!=prefix ) ) return error;
31 return std::string(s, prefix.length(), s.length()-prefix.length());
32}
33
e50cc1d6
JT
34//! get child element by id
35/*!
36 \return pointer to element having id or null on error
37 \todo find libxmlpp pendant
38*/
39const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id)
40{
41 const xmlpp::Attribute* cid = element->get_attribute("id");
42 if ( cid && ( cid->get_value() == id)) return element;
43
44 //Recurse through child nodes:
45 xmlpp::Node::NodeList list = element->get_children();
46 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
47 {
48 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(*iter);
49 if (element) {
50 const xmlpp::Element* match = get_element_by_id(element, id);
51 if (match) return match;
52 }
53 }
54 return NULL;
55}
56
57//! get namespace by id
58/*!
59 \return namespace name or empty string on error
60*/
61std::string get_namespace(const xmlpp::Element* root, const std::string &id)
62{
63 std::string error;
64 const xmlpp::Element* element(get_element_by_id(root, id));
65 if ((!element)||(!element->get_attribute("name"))) return error;
66 return element->get_attribute("name")->get_value();
67}
68
e035276b
JT
69
70struct type_info
71{
72 std::string name;
73 std::string noref_name;
74 bool operator==(const type_info& o) {return (name==o.name) && (noref_name == o.noref_name);}
9d3993e5 75 std::string noref() const {return noref_name.empty() ? name : noref_name;}
e035276b
JT
76};
77
78std::ostream &operator<<(std::ostream &o, const type_info &t) {
79 o << t.name;
80 return o;
81}
82
e50cc1d6
JT
83//! get type by id
84/*!
85 \return type name or empty string on error
86*/
e035276b 87type_info get_type(const xmlpp::Element* root, const std::string &id)
e50cc1d6 88{
e035276b 89 type_info error;
e50cc1d6
JT
90 const xmlpp::Element* element(get_element_by_id(root, id));
91 if (!element) return error;
92
93 // TODO: not yet complete
94 // if we recurse - when do we stop?
95 // if it is a typedef? yes? (hmm if the typedef is in the file parsed this will not work)
060fbd87 96
9d3993e5 97 // TODO: const and reference types handling is a ugly hack
060fbd87 98
e50cc1d6
JT
99 std::string tag(element->get_name());
100 if (tag=="ReferenceType") {
101 assert(element->get_attribute("type"));
e035276b 102 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
e50cc1d6 103 if (ret==error) return error;
e035276b
JT
104 // at the moment we only support const &
105 // todo: nice error message!
106 if ((ret.noref_name=strip(ret.name,"const ")).empty()) return error;
107 ret.name=ret.name+"&";
108 return ret;
e50cc1d6
JT
109 }else if (tag=="CvQualifiedType") {
110 assert(element->get_attribute("type"));
e035276b 111 type_info ret(get_type(root, element->get_attribute("type")->get_value()));
e50cc1d6 112 if (ret==error) return error;
e035276b
JT
113 ret.name=std::string("const ")+ret.name;
114 return ret;
e50cc1d6
JT
115 }
116
117 assert(element->get_attribute("name"));
e035276b 118 type_info ret;
9d3993e5
JT
119 if (element->get_attribute("context"))
120 ret.name=get_namespace(root, element->get_attribute("context")->get_value())+"::";
121 ret.name+=element->get_attribute("name")->get_value();
e035276b 122 return ret;
e50cc1d6
JT
123}
124
060fbd87
JT
125struct t2n_procedure
126{
9d3993e5 127 typedef std::list<std::pair<std::string, type_info> > Args;
e50cc1d6 128
060fbd87 129 std::string group;
e035276b 130 type_info ret_type;
060fbd87
JT
131 std::string name;
132 Args args;
e50cc1d6 133
060fbd87
JT
134 std::string ret_classname() const {
135 return name+"_res";
136 }
137 std::string cmd_classname() const {
138 return name+"_cmd";
139 }
140};
e50cc1d6 141
060fbd87
JT
142std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) {
143 for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) {
144 if (it!=args.begin()) o << ", ";
145 o << it->second << " " << it->first;
146 }
147 return o;
148}
e50cc1d6 149
060fbd87
JT
150std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) {
151 o << f.ret_type << " " << f.name << "(" << f.args << ")";
152 return o;
153}
e50cc1d6 154
060fbd87
JT
155class Parser
156{
157public:
158 Parser(const std::string &fname) : m_fname(fname) {}
e50cc1d6 159
060fbd87
JT
160 std::list<t2n_procedure> get_procedures() {
161 xmlpp::DomParser parser;
162 // parser.set_validate();
163 parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
164 parser.parse_file(m_fname);
165 if(parser)
166 {
167 //Walk the tree:
168 const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
169 const xmlpp::Element* root = dynamic_cast<const xmlpp::Element*>(pNode);
170 assert(root);
171 visit_node(root);
e50cc1d6 172 }
060fbd87 173 return m_procedures;
e50cc1d6 174 }
060fbd87
JT
175protected:
176 std::string m_fname;
177 std::list<t2n_procedure> m_procedures;
178
179 void parse_function(const xmlpp::Element* root, const xmlpp::Node* node) {
180 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node);
181 if (!element) return;
182
183 const xmlpp::Attribute* attributes = element->get_attribute("attributes");
184 const xmlpp::Attribute* name = element->get_attribute("name");
185 const xmlpp::Attribute* returns = element->get_attribute("returns");
186 if ((!attributes)||(!name)||(!returns)) return;
187
188 // check wether the procedure is marked (TODO: improve)
189 // attributes are speparated by spaces?
190 if (attributes->get_value().find("gccxml(libt2n")==std::string::npos) return;
191
192 // TODO: extract command group
193 // something like: sed 's,gccxml(libt2n-\([a-z]*}),\1,'
194 t2n_procedure f;
195 f.group=("default");
e50cc1d6 196
060fbd87
JT
197 // we need the return type
198 f.ret_type=get_type(root, returns->get_value());
199 f.name=name->get_value();
200
201 xmlpp::Node::NodeList list = node->get_children("Argument");
202 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
203 {
204 const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter);
205 if ( arg ) {
206 assert(arg->get_name() == "Argument");
207 assert(arg->get_attribute("name"));
208 assert(arg->get_attribute("type"));
9d3993e5 209 f.args.push_back(std::pair<std::string, type_info>(arg->get_attribute("name")->get_value(), get_type(root, arg->get_attribute("type")->get_value())));
060fbd87
JT
210 }
211 }
9d3993e5 212 std::cerr << "Found function: " << f << std::endl;
060fbd87 213 m_procedures.push_back(f);
e50cc1d6 214 }
e50cc1d6 215
060fbd87
JT
216 void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0)
217 {
218 if (!node) node=root;
060fbd87
JT
219
220 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
221 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
222 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
e50cc1d6 223
060fbd87
JT
224 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
225 return;
e50cc1d6 226
060fbd87 227 std::string nodename = node->get_name();
e50cc1d6 228
060fbd87
JT
229 if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
230 {
f8b78aa8 231 // print_indentation(indentation);
060fbd87
JT
232 // std::cout << "Node name = " << node->get_name() << std::endl;
233 // std::cout << "Node name = " << nodename << std::endl;
234 if (node->get_name() == "Function") {
235 parse_function(root, node);
236 }
237 }
f8b78aa8 238#if 0
060fbd87
JT
239 else if(nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
240 {
241 print_indentation(indentation);
242 std::cout << "Text Node" << std::endl;
243 }
244
245 //Treat the various node types differently:
246 if(nodeText)
247 {
248 print_indentation(indentation);
249 std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
250 }
251 else if(nodeComment)
252 {
253 print_indentation(indentation);
254 std::cout << "comment = " << nodeComment->get_content() << std::endl;
255 }
256 else if(nodeContent)
257 {
258 print_indentation(indentation);
259 std::cout << "content = " << nodeContent->get_content() << std::endl;
260 }
261 else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
262 {
263 //A normal Element node:
264
265 //line() works only for ElementNodes.
266 print_indentation(indentation);
267 std::cout << " line = " << node->get_line() << std::endl;
268
269 //Print attributes:
270 const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
271 for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
272 {
273 const xmlpp::Attribute* attribute = *iter;
274 print_indentation(indentation);
275 std::cout << " Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
276 }
277
278 const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
279 if(attribute)
280 {
281 std::cout << "title found: =" << attribute->get_value() << std::endl;
282
283 }
284 }
f8b78aa8 285#endif
060fbd87
JT
286 if(!nodeContent)
287 {
288 //Recurse through child nodes:
289 xmlpp::Node::NodeList list = node->get_children();
290 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
291 {
292 visit_node(root, *iter, indentation + 2); //recursive
293 }
294 }
e50cc1d6 295 }
060fbd87
JT
296};
297
298//! find used groups
299std::set<std::string>
300used_groups(const std::list<t2n_procedure> &funcs) {
301 typedef std::set<std::string> Groups;
302 Groups groups;
303 for (std::list<t2n_procedure>::const_iterator it=funcs.begin();it!=funcs.end();++it)
304 // since we use std::set each group is inserted only once
305 groups.insert(it->group);
306 return groups;
307}
308
309void output_common_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
310 std::set<std::string> groups(used_groups(procs));
311
312 o << "#include \"codegen-stubhead.hxx\"\n";
313 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
314 o << "class cmd_group_" << *it << " : public libt2n::command\n"
315 << "{\n"
316 << "private:\n"
317 << " friend class boost::serialization::access;\n"
318 << " template<class Archive>\n"
319 << " void serialize(Archive & ar, const unsigned int version)\n"
320 << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n"
321 << "};\n";
e50cc1d6 322 }
060fbd87
JT
323
324 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
325 o << "class " << it->ret_classname() << " : public libt2n::result\n"
326 << "{\n"
327 << "private:\n"
328 << " " << it->ret_type << " res;\n"
329 << " friend class boost::serialization::access;\n"
330 << " template<class Archive>\n"
331 << " void serialize(Archive & ar, const unsigned int version)\n"
332 << " {\n"
333 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"
334 << " ar & BOOST_SERIALIZATION_NVP(res);\n"
335 << " }\n"
336 << "public:\n"
e035276b 337 << " " << it->ret_classname() << "() {}\n"
060fbd87
JT
338 << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n"
339 << " " << it->ret_type << " get_data() { return res; }\n"
340 << "};\n";
e50cc1d6 341 }
060fbd87
JT
342 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
343 o << "class " << it->cmd_classname() << " : public " << "cmd_group_" << it->group << "\n"
344 << "{\n"
345 << "private:\n";
346 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
e035276b 347 o << " " << ait->second.noref() << " " << ait->first << ";\n";
060fbd87
JT
348 }
349 o << " friend class boost::serialization::access;\n"
350 << " template<class Archive>\n"
351 << " void serialize(Archive & ar, const unsigned int version)\n"
352 << " {\n"
353 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_" << it->group << ");\n";
354 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
355 o << " ar & BOOST_SERIALIZATION_NVP(" << ait->first << ");\n";
356 }
e50cc1d6 357
060fbd87
JT
358 // default constructor
359 o << " }\n"
360 << "\n"
361 << "public:\n"
362 << " " << it->cmd_classname() << "() {}\n";
363
364 // constructor taking all arguments
365 o << " " << it->cmd_classname() << "(";
366 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
367 if (ait!=it->args.begin()) o << ", ";
368 o << ait->second << " _" << ait->first;
369 }
370 o << ") : ";
371 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
372 if (ait!=it->args.begin()) o << ", ";
373 o << ait->first << "(_" << ait->first << ")";
374 }
375 o << " {}\n"
376 << " libt2n::result* operator()();\n"
377 << "};\n";
e50cc1d6 378 }
060fbd87
JT
379}
380
381void output_common_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp) {
382 std::set<std::string> groups(used_groups(procs));
383
384 o << "#include \"" << common_hpp << "\"\n"
e035276b 385 << "#include <boost/serialization/export.hpp>\n"
060fbd87
JT
386 << "\n"
387 << "/* register types with boost serialization */\n";
388 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
389 o << "BOOST_CLASS_EXPORT(cmd_group_" << *it << ")\n";
e50cc1d6 390 }
060fbd87
JT
391 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
392 o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n"
393 << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n";
e50cc1d6 394 }
060fbd87 395}
e50cc1d6 396
060fbd87
JT
397void output_client_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
398 std::set<std::string> groups(used_groups(procs));
e50cc1d6 399
060fbd87
JT
400 o << "#include <command_client.hxx>\n";
401
402 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
403 o << "class cmd_group_" << *it << "_client : public libt2n::command_client\n"
404 << "{\n"
405 << "public:\n"
406 << "cmd_group_" << *it << "_client(libt2n::client_connection &_c,\n"
407 << " long long _command_timeout_usec=command_timeout_usec_default,\n"
408 << " long long _hello_timeout_usec=hello_timeout_usec_default)\n"
409 << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n"
410 << " {}\n";
411 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
412 if (pit->group==*it) {
413 o << " " << *pit << ";\n";
414 }
e50cc1d6 415 }
060fbd87
JT
416 o << "};\n";
417 }
418}
e50cc1d6 419
060fbd87
JT
420void output_client_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp, const std::string &client_hpp) {
421 std::set<std::string> groups(used_groups(procs));
422
423 o << "#include \"" << client_hpp << "\"\n"
424 << "#include \"" << common_hpp << "\"\n"
425 << "// fake\n";
426 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
427 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n";
428 }
429
430 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
431 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
432 if (pit->group==*it) {
433 o << pit->ret_type << " cmd_group_" << *it << "_client::" << pit->name << "(" << pit->args << ")\n"
434 << "{\n"
435 << " libt2n::result_container rc;\n"
436 << " send_command(new " << pit->cmd_classname() << "(";
437 for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait) {
438 if (ait!=pit->args.begin()) o << ", ";
439 o << ait->first;
440 }
441 o << "), rc);\n"
442 << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n"
443 << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n"
444 << " return res->get_data();\n"
445 << "}\n";
446 }
e50cc1d6
JT
447 }
448 }
060fbd87
JT
449
450 // include in this compilation unit to ensure the compilation unit is used
451 // see also:
452 // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library
453 o << "#include \"" << common_cpp << "\"\n";
454}
455
456void output_server_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp) {
457 o << "#include \"" << common_hpp << "\"\n";
458
459 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
460 o << *it << ";\n";
461 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return new " << it->ret_classname() << "(" << it->name << "(";
462 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
463 if (ait!=it->args.begin()) o << ", ";
464 o << ait->first;
e50cc1d6 465 }
060fbd87 466 o << ")); }\n";
e50cc1d6 467 }
060fbd87
JT
468 o << "#include \"" << common_cpp << "\"\n";
469}
470
060fbd87
JT
471struct header_file : public std::ofstream
472{
473 header_file(const char* fname) : std::ofstream(fname) {
474 std::cerr << "create header: '" << fname << "'" << std::endl;
475 std::string macro(replace(toupper(fname),'.','_'));
476 *this << "// automatically generated code - do not edit\n" << std::endl;
477 *this << "#ifndef " << macro << "\n"
478 << "#define " << macro << "\n";
479 }
480 ~header_file() {
481 *this << "#endif" << std::endl;
482 }
483};
484
485struct cpp_file : public std::ofstream
486{
487 cpp_file(const char* fname) : std::ofstream(fname) {
488 std::cerr << "create cpp: '" << fname << "'" << std::endl;
489 *this << "// automatically generated code - do not edit\n" << std::endl;
490 }
491};
492
493int
494main(int argc, char* argv[])
e50cc1d6 495{
f8b78aa8
JT
496 if (argc != 3) {
497 std::cerr << "Usage: " << argv[0] << " gccxml-file outputprefix" << std::endl;
498 return 1;
499 }
500 std::string filepath(argv[1]);
501 std::string prefix(argv[2]);
e50cc1d6 502
060fbd87
JT
503 Parser parser(filepath);
504 std::list<t2n_procedure> procedures(parser.get_procedures());
505
506 std::cerr << "Procedures:" << std::endl;
507 for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it)
508 std::cerr << *it << ";" << std::endl;
509
510 std::set<std::string> groups(used_groups(procedures));
511 std::cerr << "Used groups:" << std::endl;
512 for (std::set<std::string>::const_iterator it=groups.begin();it!=groups.end();++it)
513 std::cerr << *it << std::endl;
514
060fbd87
JT
515 std::string common_hpp_fname(prefix+"common.hxx");
516 std::string common_cpp_fname(prefix+"common.cpp");
517 std::string client_hpp_fname(prefix+"client.hxx");
518 std::string client_cpp_fname(prefix+"client.cpp");
e035276b 519 std::string server_cpp_fname(prefix+"server.cpp");
060fbd87
JT
520
521 header_file common_hpp(common_hpp_fname.c_str());
522 output_common_hpp(common_hpp, procedures);
523
524 cpp_file common_cpp(common_cpp_fname.c_str());
e035276b 525 output_common_cpp(common_cpp, procedures, common_hpp_fname);
060fbd87
JT
526
527 header_file client_hpp(client_hpp_fname.c_str());
528 output_client_hpp(client_hpp, procedures);
529
530 cpp_file client_cpp(client_cpp_fname.c_str());
531 output_client_cpp(client_cpp, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname);
e50cc1d6 532
060fbd87
JT
533 cpp_file server_cpp(server_cpp_fname.c_str());
534 output_server_cpp(server_cpp, procedures, common_hpp_fname, common_cpp_fname);
e50cc1d6
JT
535 return 0;
536}