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