1 /******************************************************************************
3 * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "classcompiler.h"
27 using namespace icinga;
29 ClassCompiler::ClassCompiler(const std::string& path, std::istream *input)
30 : m_Path(path), m_Input(input)
35 ClassCompiler::~ClassCompiler(void)
40 std::string ClassCompiler::GetPath(void) const
45 void *ClassCompiler::GetScanner(void)
50 size_t ClassCompiler::ReadInput(char *buffer, size_t max_size)
52 m_Input->read(buffer, max_size);
53 return static_cast<size_t>(m_Input->gcount());
56 void ClassCompiler::HandleInclude(const std::string& path, const ClassDebugInfo& locp)
58 std::cout << "#include \"" << path << "\"" << std::endl << std::endl;
61 void ClassCompiler::HandleAngleInclude(const std::string& path, const ClassDebugInfo& locp)
63 std::cout << "#include <" << path << ">" << std::endl << std::endl;
66 void ClassCompiler::HandleNamespaceBegin(const std::string& name, const ClassDebugInfo& locp)
68 std::cout << "namespace " << name << std::endl
69 << "{" << std::endl << std::endl;
72 void ClassCompiler::HandleNamespaceEnd(const ClassDebugInfo& locp)
74 std::cout << "}" << std::endl;
77 void ClassCompiler::HandleCode(const std::string& code, const ClassDebugInfo& locp)
79 std::cout << code << std::endl;
82 unsigned long ClassCompiler::SDBM(const std::string& str, size_t len = std::string::npos)
84 unsigned long hash = 0;
87 std::string::const_iterator it;
89 for (it = str.begin(); it != str.end(); it++) {
95 hash = c + (hash << 6) + (hash << 16) - hash;
103 void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
105 std::vector<Field>::const_iterator it;
107 /* forward declaration */
108 if (klass.Name.find_first_of(':') == std::string::npos)
109 std::cout << "class " << klass.Name << ";" << std::endl << std::endl;
112 std::cout << "template<>" << std::endl
113 << "class TypeImpl<" << klass.Name << ">"
114 << " : public Type" << std::endl
116 << "public:" << std::endl;
119 std::cout << "\t" << "virtual String GetName(void) const" << std::endl
120 << "\t" << "{" << std::endl
121 << "\t\t" << "return \"" << klass.Name << "\";" << std::endl
122 << "\t" << "}" << std::endl << std::endl;
125 std::cout << "\t" << "virtual bool IsAbstract(void) const" << std::endl
126 << "\t" << "{" << std::endl
127 << "\t\t" << "return " << (klass.Abstract ? "true" : "false") << ";" << std::endl
128 << "\t" << "}" << std::endl << std::endl;
131 std::cout << "\t" << "virtual const Type *GetBaseType(void) const" << std::endl
132 << "\t" << "{" << std::endl;
134 std::cout << "\t\t" << "return ";
136 if (!klass.Parent.empty())
137 std::cout << "Type::GetByName(\"" << klass.Parent << "\")";
141 std::cout << ";" << std::endl
142 << "\t" << "}" << std::endl << std::endl;
145 std::cout << "\t" << "virtual int GetFieldId(const String& name) const" << std::endl
146 << "\t" << "{" << std::endl
147 << "\t\t" << "return StaticGetFieldId(name);" << std::endl
148 << "\t" << "}" << std::endl << std::endl;
150 /* StaticGetFieldId */
151 std::cout << "\t" << "static int StaticGetFieldId(const String& name)" << std::endl
152 << "\t" << "{" << std::endl
153 << "\t\t" << "int offset = ";
155 if (!klass.Parent.empty())
156 std::cout << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount()";
160 std::cout << ";" << std::endl << std::endl;
162 std::map<int, std::vector<std::pair<int, std::string> > > jumptable;
164 int hlen = 0, collisions = 0;
173 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
174 int hash = static_cast<int>(SDBM(it->Name, hlen));
175 jumptable[hash].push_back(std::make_pair(num, it->Name));
178 if (jumptable[hash].size() > 1)
181 } while (collisions >= 5 && hlen < 8);
183 if (!klass.Fields.empty()) {
184 std::cout << "\t\tswitch (static_cast<int>(Utility::SDBM(name, " << hlen << "))) {" << std::endl;
186 std::map<int, std::vector<std::pair<int, std::string> > >::const_iterator itj;
188 for (itj = jumptable.begin(); itj != jumptable.end(); itj++) {
189 std::cout << "\t\t\tcase " << itj->first << ":" << std::endl;
191 std::vector<std::pair<int, std::string> >::const_iterator itf;
193 for (itf = itj->second.begin(); itf != itj->second.end(); itf++) {
194 std::cout << "\t\t\t\t" << "if (name == \"" << itf->second << "\")" << std::endl
195 << "\t\t\t\t\t" << "return offset + " << itf->first << ";" << std::endl;
198 std::cout << std::endl
199 << "\t\t\t\tbreak;" << std::endl;
202 std::cout << "\t\t}" << std::endl;
205 std::cout << std::endl
206 << "\t\t" << "return ";
208 if (!klass.Parent.empty())
209 std::cout << "TypeImpl<" << klass.Parent << ">::StaticGetFieldId(name)";
213 std::cout << ";" << std::endl
214 << "\t" << "}" << std::endl << std::endl;
217 std::cout << "\t" << "virtual Field GetFieldInfo(int id) const" << std::endl
218 << "\t" << "{" << std::endl
219 << "\t\t" << "return StaticGetFieldInfo(id);" << std::endl
220 << "\t" << "}" << std::endl << std::endl;
222 /* StaticGetFieldInfo */
223 std::cout << "\t" << "static Field StaticGetFieldInfo(int id)" << std::endl
224 << "\t" << "{" << std::endl;
226 if (!klass.Parent.empty())
227 std::cout << "\t\t" << "int real_id = id - " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount();" << std::endl
228 << "\t\t" << "if (real_id < 0) { return " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldInfo(id); }" << std::endl;
230 if (klass.Fields.size() > 0) {
231 std::cout << "\t\t" << "switch (";
233 if (!klass.Parent.empty())
234 std::cout << "real_id";
238 std::cout << ") {" << std::endl;
241 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
242 std::cout << "\t\t\t" << "case " << num << ":" << std::endl
243 << "\t\t\t\t" << "return Field(" << num << ", \"" << it->Name << "\", " << it->Attributes << ");" << std::endl;
247 std::cout << "\t\t\t" << "default:" << std::endl
251 std::cout << "\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl;
253 if (klass.Fields.size() > 0)
254 std::cout << "\t\t" << "}" << std::endl;
256 std::cout << "\t" << "}" << std::endl << std::endl;
259 std::cout << "\t" << "virtual int GetFieldCount(void) const" << std::endl
260 << "\t" << "{" << std::endl
261 << "\t\t" << "return StaticGetFieldCount();" << std::endl
262 << "\t" << "}" << std::endl << std::endl;
264 /* StaticGetFieldCount */
265 std::cout << "\t" << "static int StaticGetFieldCount(void)" << std::endl
266 << "\t" << "{" << std::endl
267 << "\t\t" << "return " << klass.Fields.size();
269 if (!klass.Parent.empty())
270 std::cout << " + " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount()";
272 std::cout << ";" << std::endl
273 << "\t" << "}" << std::endl << std::endl;
275 std::cout << "};" << std::endl << std::endl;
278 std::cout << "template<>" << std::endl
279 << "class ObjectImpl<" << klass.Name << ">"
280 << " : public " << (klass.Parent.empty() ? "Object" : klass.Parent) << std::endl
282 << "public:" << std::endl
283 << "\t" << "DECLARE_PTR_TYPEDEFS(ObjectImpl<" << klass.Name << ">);" << std::endl << std::endl;
285 /* GetReflectionType */
286 std::cout << "\t" << "virtual const Type *GetReflectionType(void) const" << std::endl
287 << "\t" << "{" << std::endl
288 << "\t\t" << "return Type::GetByName(\"" << klass.Name << "\");" << std::endl
289 << "\t" << "}" << std::endl << std::endl;
291 if (!klass.Fields.empty()) {
293 std::cout << "public:" << std::endl
294 << "\t" << "ObjectImpl<" << klass.Name << ">(void)" << std::endl
295 << "\t" << "{" << std::endl;
297 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
298 std::cout << "\t\t" << "Set" << it->GetFriendlyName() << "(" << "GetDefault" << it->GetFriendlyName() << "());" << std::endl;
301 std::cout << "\t" << "}" << std::endl << std::endl;
304 std::cout << "protected:" << std::endl
305 << "\t" << "virtual void SetField(int id, const Value& value)" << std::endl
306 << "\t" << "{" << std::endl;
308 if (!klass.Parent.empty())
309 std::cout << "\t\t" << "int real_id = id - TypeImpl<" << klass.Parent << ">::StaticGetFieldCount(); " << std::endl
310 << "\t\t" << "if (real_id < 0) { " << klass.Parent << "::SetField(id, value); return; }" << std::endl;
312 std::cout << "\t\t" << "switch (";
314 if (!klass.Parent.empty())
315 std::cout << "real_id";
319 std::cout << ") {" << std::endl;
322 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
323 std::cout << "\t\t\t" << "case " << num << ":" << std::endl
324 << "\t\t\t\t" << "Set" << it->GetFriendlyName() << "(";
326 if (it->Attributes & FAEnum)
327 std::cout << "static_cast<" << it->Type << ">(static_cast<int>(";
329 std::cout << "value";
331 if (it->Attributes & FAEnum)
334 std::cout << ");" << std::endl
335 << "\t\t\t\t" << "break;" << std::endl;
339 std::cout << "\t\t\t" << "default:" << std::endl
340 << "\t\t\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl
341 << "\t\t" << "}" << std::endl;
343 std::cout << "\t" << "}" << std::endl << std::endl;
346 std::cout << "protected:" << std::endl
347 << "\t" << "virtual Value GetField(int id) const" << std::endl
348 << "\t" << "{" << std::endl;
350 if (!klass.Parent.empty())
351 std::cout << "\t\t" << "int real_id = id - TypeImpl<" << klass.Parent << ">::StaticGetFieldCount(); " << std::endl
352 << "\t\t" << "if (real_id < 0) { return " << klass.Parent << "::GetField(id); }" << std::endl;
354 std::cout << "\t\t" << "switch (";
356 if (!klass.Parent.empty())
357 std::cout << "real_id";
361 std::cout << ") {" << std::endl;
364 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
365 std::cout << "\t\t\t" << "case " << num << ":" << std::endl
366 << "\t\t\t\t" << "return Get" << it->GetFriendlyName() << "();" << std::endl;
370 std::cout << "\t\t\t" << "default:" << std::endl
371 << "\t\t\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl
372 << "\t\t" << "}" << std::endl;
374 std::cout << "\t" << "}" << std::endl << std::endl;
377 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
380 if (it->Attributes & FAGetProtected)
385 std::cout << prot << ":" << std::endl
386 << "\t" << it->Type << " Get" << it->GetFriendlyName() << "(void) const" << std::endl
387 << "\t" << "{" << std::endl;
389 if (it->GetAccessor.empty())
390 std::cout << "\t\t" << "return m_" << it->GetFriendlyName() << ";" << std::endl;
392 std::cout << it->GetAccessor << std::endl;
394 std::cout << "\t" << "}" << std::endl << std::endl;
398 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
401 if (it->Attributes & FASetProtected)
403 else if (it->Attributes & FAConfig)
408 std::cout << prot << ":" << std::endl
409 << "\t" << "void Set" << it->GetFriendlyName() << "(const " << it->Type << "& value)" << std::endl
410 << "\t" << "{" << std::endl;
412 if (it->SetAccessor.empty())
413 std::cout << "\t\t" << "m_" << it->GetFriendlyName() << " = value;" << std::endl;
415 std::cout << it->SetAccessor << std::endl;
417 std::cout << "\t" << "}" << std::endl << std::endl;
421 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
424 std::cout << "private:" << std::endl
425 << "\t" << it->Type << " GetDefault" << it->GetFriendlyName() << "(void) const" << std::endl
426 << "\t" << "{" << std::endl;
428 if (it->DefaultAccessor.empty())
429 std::cout << "\t\t" << "return " << it->Type << "();" << std::endl;
431 std::cout << it->DefaultAccessor << std::endl;
433 std::cout << "\t" << "}" << std::endl;
436 /* instance variables */
437 std::cout << "private:" << std::endl;
439 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
440 std::cout << "\t" << it->Type << " m_" << it->GetFriendlyName() << ";" << std::endl;
444 std::cout << "};" << std::endl << std::endl;
447 if (klass.Abstract) {
448 std::cout << "template<>" << std::endl
449 << "struct FactoryHelper<" << klass.Name << ">" << std::endl
451 << "\t" << "Type::Factory GetFactory(void)" << std::endl
452 << "\t" << "{" << std::endl
453 << "\t\t" << "return Type::Factory();"
455 << "};" << std::endl << std::endl;
459 void ClassCompiler::CompileFile(const std::string& path)
461 std::ifstream stream;
462 stream.open(path.c_str(), std::ifstream::in);
465 throw std::invalid_argument("Could not open config file: " + path);
467 return CompileStream(path, &stream);
470 void ClassCompiler::CompileStream(const std::string& path, std::istream *stream)
472 stream->exceptions(std::istream::badbit);
474 std::cout << "#include \"base/object.h\"" << std::endl
475 << "#include \"base/type.h\"" << std::endl
476 << "#include \"base/debug.h\"" << std::endl
477 << "#include \"base/value.h\"" << std::endl
478 << "#include \"base/array.h\"" << std::endl
479 << "#include \"base/dictionary.h\"" << std::endl
480 << "#include \"base/utility.h\"" << std::endl << std::endl
481 << "#ifdef _MSC_VER" << std::endl
482 << "#pragma warning( push )" << std::endl
483 << "#pragma warning( disable : 4244 )" << std::endl
484 << "#pragma warning( disable : 4800 )" << std::endl
485 << "#endif /* _MSC_VER */" << std::endl << std::endl;
487 ClassCompiler ctx(path, stream);
490 std::cout << "#ifdef _MSC_VER" << std::endl
491 << "#pragma warning ( pop )" << std::endl
492 << "#endif /* _MSC_VER */" << std::endl;