]> granicus.if.org Git - icinga2/blob - tools/mkclass/classcompiler.cpp
Improve performance for icinga::Deserialize.
[icinga2] / tools / mkclass / classcompiler.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "classcompiler.h"
21 #include <iostream>
22 #include <fstream>
23 #include <stdexcept>
24 #include <map>
25 #include <vector>
26
27 using namespace icinga;
28
29 ClassCompiler::ClassCompiler(const std::string& path, std::istream *input)
30         : m_Path(path), m_Input(input)
31 {
32         InitializeScanner();
33 }
34
35 ClassCompiler::~ClassCompiler(void)
36 {
37         DestroyScanner();
38 }
39
40 std::string ClassCompiler::GetPath(void) const
41 {
42         return m_Path;
43 }
44
45 void *ClassCompiler::GetScanner(void)
46 {
47         return m_Scanner;
48 }
49
50 size_t ClassCompiler::ReadInput(char *buffer, size_t max_size)
51 {
52         m_Input->read(buffer, max_size);
53         return static_cast<size_t>(m_Input->gcount());
54 }
55
56 void ClassCompiler::HandleInclude(const std::string& path, const ClassDebugInfo& locp)
57 {
58         std::cout << "#include \"" << path << "\"" << std::endl << std::endl;
59 }
60
61 void ClassCompiler::HandleAngleInclude(const std::string& path, const ClassDebugInfo& locp)
62 {
63         std::cout << "#include <" << path << ">" << std::endl << std::endl;
64 }
65
66 void ClassCompiler::HandleNamespaceBegin(const std::string& name, const ClassDebugInfo& locp)
67 {
68         std::cout << "namespace " << name << std::endl
69                           << "{" << std::endl << std::endl;
70 }
71
72 void ClassCompiler::HandleNamespaceEnd(const ClassDebugInfo& locp)
73 {
74         std::cout << "}" << std::endl;
75 }
76
77 void ClassCompiler::HandleCode(const std::string& code, const ClassDebugInfo& locp)
78 {
79         std::cout << code << std::endl;
80 }
81
82 unsigned long ClassCompiler::SDBM(const std::string& str, size_t len = std::string::npos)
83 {
84         unsigned long hash = 0;
85         size_t current = 0;
86
87         std::string::const_iterator it;
88
89         for (it = str.begin(); it != str.end(); it++) {
90                 if (current >= len)
91                         break;
92
93                 char c = *it;
94
95                 hash = c + (hash << 6) + (hash << 16) - hash;
96
97                 current++;
98         }
99
100         return hash;
101 }
102
103 void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo& locp)
104 {
105         std::vector<Field>::const_iterator it;
106
107         /* forward declaration */
108         if (klass.Name.find_first_of(':') == std::string::npos)
109                 std::cout << "class " << klass.Name << ";" << std::endl << std::endl;
110
111         /* TypeImpl */
112         std::cout << "template<>" << std::endl
113                 << "class TypeImpl<" << klass.Name << ">"
114                 << " : public Type" << std::endl
115                 << "{" << std::endl
116                 << "public:" << std::endl;
117
118         /* GetName */
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;
123
124         /* IsAbstract */
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;
129
130         /* GetBaseType */
131         std::cout << "\t" << "virtual const Type *GetBaseType(void) const" << std::endl
132                 << "\t" << "{" << std::endl;
133
134         std::cout << "\t\t" << "return ";
135
136         if (!klass.Parent.empty())
137                 std::cout << "Type::GetByName(\"" << klass.Parent << "\")";
138         else
139                 std::cout << "NULL";
140
141         std::cout << ";" << std::endl
142                           << "\t" << "}" << std::endl << std::endl;
143
144         /* GetFieldId */
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;
149
150         /* StaticGetFieldId */
151         std::cout << "\t" << "static int StaticGetFieldId(const String& name)" << std::endl
152                 << "\t" << "{" << std::endl
153                 << "\t\t" << "int offset = ";
154
155         if (!klass.Parent.empty())
156                 std::cout << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount()";
157         else
158                 std::cout << "0";
159
160         std::cout << ";" << std::endl << std::endl;
161
162         std::map<int, std::vector<std::pair<int, std::string> > > jumptable;
163
164         int hlen = 0, collisions = 0;
165
166         do {
167                 int num = 0;
168
169                 hlen++;
170                 jumptable.clear();
171                 collisions = 0;
172
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));
176                         num++;
177
178                         if (jumptable[hash].size() > 1)
179                                 collisions++;
180                 }
181         } while (collisions >= 5 && hlen < 8);
182
183         if (!klass.Fields.empty()) {
184                 std::cout << "\t\tswitch (static_cast<int>(Utility::SDBM(name, " << hlen << "))) {" << std::endl;
185
186                 std::map<int, std::vector<std::pair<int, std::string> > >::const_iterator itj;
187
188                 for (itj = jumptable.begin(); itj != jumptable.end(); itj++) {
189                         std::cout << "\t\t\tcase " << itj->first << ":" << std::endl;
190
191                         std::vector<std::pair<int, std::string> >::const_iterator itf;
192
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;
196                         }
197
198                         std::cout << std::endl
199                                   << "\t\t\t\tbreak;" << std::endl;
200                 }
201
202                 std::cout << "\t\t}" << std::endl;
203         }
204
205         std::cout << std::endl
206                 << "\t\t" << "return ";
207
208         if (!klass.Parent.empty())
209                 std::cout << "TypeImpl<" << klass.Parent << ">::StaticGetFieldId(name)";
210         else
211                 std::cout << "-1";
212
213         std::cout << ";" << std::endl
214                 << "\t" << "}" << std::endl << std::endl;
215
216         /* GetFieldInfo */
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;
221
222         /* StaticGetFieldInfo */
223         std::cout << "\t" << "static Field StaticGetFieldInfo(int id)" << std::endl
224                 << "\t" << "{" << std::endl;
225
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;
229
230         if (klass.Fields.size() > 0) {
231                 std::cout << "\t\t" << "switch (";
232
233                 if (!klass.Parent.empty())
234                         std::cout << "real_id";
235                 else
236                         std::cout << "id";
237
238                 std::cout << ") {" << std::endl;
239
240                 size_t num = 0;
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;
244                         num++;
245                 }
246
247                 std::cout << "\t\t\t" << "default:" << std::endl
248                                   << "\t\t";
249         }
250
251         std::cout << "\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl;
252
253         if (klass.Fields.size() > 0)
254                 std::cout << "\t\t" << "}" << std::endl;
255
256         std::cout << "\t" << "}" << std::endl << std::endl;
257
258         /* GetFieldCount */
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;
263
264         /* StaticGetFieldCount */
265         std::cout << "\t" << "static int StaticGetFieldCount(void)" << std::endl
266                 << "\t" << "{" << std::endl
267                 << "\t\t" << "return " << klass.Fields.size();
268
269         if (!klass.Parent.empty())
270                 std::cout << " + " << "TypeImpl<" << klass.Parent << ">::StaticGetFieldCount()";
271
272         std::cout << ";" << std::endl
273                 << "\t" << "}" << std::endl << std::endl;
274
275         std::cout << "};" << std::endl << std::endl;
276
277         /* ObjectImpl */
278         std::cout << "template<>" << std::endl
279                   << "class ObjectImpl<" << klass.Name << ">"
280                   << " : public " << (klass.Parent.empty() ? "Object" : klass.Parent) << std::endl
281                   << "{" << std::endl
282                   << "public:" << std::endl
283                   << "\t" << "DECLARE_PTR_TYPEDEFS(ObjectImpl<" << klass.Name << ">);" << std::endl << std::endl;
284
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;
290
291         if (!klass.Fields.empty()) {
292                 /* constructor */
293                 std::cout << "public:" << std::endl
294                           << "\t" << "ObjectImpl<" << klass.Name << ">(void)" << std::endl
295                           << "\t" << "{" << std::endl;
296
297                 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
298                         std::cout << "\t\t" << "Set" << it->GetFriendlyName() << "(" << "GetDefault" << it->GetFriendlyName() << "());" << std::endl;
299                 }
300
301                 std::cout << "\t" << "}" << std::endl << std::endl;
302
303                 /* SetField */
304                 std::cout << "protected:" << std::endl
305                                   << "\t" << "virtual void SetField(int id, const Value& value)" << std::endl
306                                   << "\t" << "{" << std::endl;
307
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;
311
312                 std::cout << "\t\t" << "switch (";
313
314                 if (!klass.Parent.empty())
315                         std::cout << "real_id";
316                 else
317                         std::cout << "id";
318
319                 std::cout << ") {" << std::endl;
320
321                 size_t num = 0;
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() << "(";
325                         
326                         if (it->Attributes & FAEnum)
327                                 std::cout << "static_cast<" << it->Type << ">(static_cast<int>(";
328
329                         std::cout << "value";
330                         
331                         if (it->Attributes & FAEnum)
332                                 std::cout << "))";
333                         
334                         std::cout << ");" << std::endl
335                                           << "\t\t\t\t" << "break;" << std::endl;
336                         num++;
337                 }
338
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;
342
343                 std::cout << "\t" << "}" << std::endl << std::endl;
344
345                 /* GetField */
346                 std::cout << "protected:" << std::endl
347                                   << "\t" << "virtual Value GetField(int id) const" << std::endl
348                                   << "\t" << "{" << std::endl;
349
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;
353
354                 std::cout << "\t\t" << "switch (";
355
356                 if (!klass.Parent.empty())
357                         std::cout << "real_id";
358                 else
359                         std::cout << "id";
360
361                 std::cout << ") {" << std::endl;
362
363                 num = 0;
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;
367                         num++;
368                 }
369
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;
373
374                 std::cout << "\t" << "}" << std::endl << std::endl;
375
376                 /* getters */
377                 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
378                         std::string prot;
379
380                         if (it->Attributes & FAGetProtected)
381                                 prot = "protected";
382                         else
383                                 prot = "public";
384
385                         std::cout << prot << ":" << std::endl
386                                           << "\t" << it->Type << " Get" << it->GetFriendlyName() << "(void) const" << std::endl
387                                           << "\t" << "{" << std::endl;
388
389                         if (it->GetAccessor.empty())
390                                 std::cout << "\t\t" << "return m_" << it->GetFriendlyName() << ";" << std::endl;
391                         else
392                                 std::cout << it->GetAccessor << std::endl;
393
394                         std::cout << "\t" << "}" << std::endl << std::endl;
395                 }
396
397                 /* setters */
398                 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
399                         std::string prot;
400
401                         if (it->Attributes & FASetProtected)
402                                 prot = "protected";
403                         else if (it->Attributes & FAConfig)
404                                 prot = "private";
405                         else
406                                 prot = "public";
407
408                         std::cout << prot << ":" << std::endl
409                                           << "\t" << "void Set" << it->GetFriendlyName() << "(const " << it->Type << "& value)" << std::endl
410                                           << "\t" << "{" << std::endl;
411
412                         if (it->SetAccessor.empty())
413                                 std::cout << "\t\t" << "m_" << it->GetFriendlyName() << " = value;" << std::endl;
414                         else
415                                 std::cout << it->SetAccessor << std::endl;
416
417                         std::cout << "\t" << "}" << std::endl << std::endl;
418                 }
419
420                 /* default */
421                 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
422                         std::string prot;
423
424                         std::cout << "private:" << std::endl
425                                           << "\t" << it->Type << " GetDefault" << it->GetFriendlyName() << "(void) const" << std::endl
426                                           << "\t" << "{" << std::endl;
427
428                         if (it->DefaultAccessor.empty())
429                                 std::cout << "\t\t" << "return " << it->Type << "();" << std::endl;
430                         else
431                                 std::cout << it->DefaultAccessor << std::endl;
432
433                         std::cout << "\t" << "}" << std::endl;
434                 }
435
436                 /* instance variables */
437                 std::cout << "private:" << std::endl;
438
439                 for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
440                         std::cout << "\t" << it->Type << " m_" << it->GetFriendlyName() << ";" << std::endl;
441                 }
442         }
443
444         std::cout << "};" << std::endl << std::endl;
445
446         /* FactoryHelper */
447         if (klass.Abstract) {
448                 std::cout << "template<>" << std::endl
449                           << "struct FactoryHelper<" << klass.Name << ">" << std::endl
450                           << "{" << std::endl
451                           << "\t" << "Type::Factory GetFactory(void)" << std::endl
452                           << "\t" << "{" << std::endl
453                           << "\t\t" << "return Type::Factory();"
454                           << "\t" << "}"
455                           << "};" << std::endl << std::endl;
456         }
457 }
458
459 void ClassCompiler::CompileFile(const std::string& path)
460 {
461         std::ifstream stream;
462         stream.open(path.c_str(), std::ifstream::in);
463
464         if (!stream)
465                 throw std::invalid_argument("Could not open config file: " + path);
466
467         return CompileStream(path, &stream);
468 }
469
470 void ClassCompiler::CompileStream(const std::string& path, std::istream *stream)
471 {
472         stream->exceptions(std::istream::badbit);
473
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;
486
487         ClassCompiler ctx(path, stream);
488         ctx.Compile();
489
490         std::cout << "#ifdef _MSC_VER" << std::endl
491                           << "#pragma warning ( pop )" << std::endl
492                           << "#endif /* _MSC_VER */" << std::endl;
493 }