-Weffc++ cleanup for miniswig
[supertux.git] / tools / miniswig / tree.hpp
1 #ifndef __TREE_H__
2 #define __TREE_H__
3
4 #include <vector>
5 #include <string>
6 #include <iostream>
7 #include <sstream>
8 #include <stdexcept>
9 #include <assert.h>
10
11 class Namespace;
12
13 class AtomicType {
14 public:
15     AtomicType() :
16       name(),
17       parent(0)
18     { }
19     virtual ~AtomicType()
20     { }
21
22     virtual void write_c(std::ostream& out)
23     {
24         out << name;
25     }
26
27     std::string name;
28     Namespace* parent;
29
30 private:
31     AtomicType(const AtomicType&);
32     AtomicType& operator=(const AtomicType&);
33 };
34
35 class BasicType : public AtomicType {
36 public:
37     static BasicType VOID;
38     static BasicType BOOL;
39     static BasicType CHAR;
40     static BasicType SHORT;
41     static BasicType INT;
42     static BasicType LONG;
43     static BasicType FLOAT;
44     static BasicType DOUBLE;
45
46 private:
47     BasicType(const std::string& name)
48     {
49         this->name = name;
50     }
51 };
52
53 class Type {
54 public:
55     Type()
56         : atomic_type(0), _unsigned(false), _const(false), _static(false),
57         pointer(0), ref(0)
58     { }
59
60     void write_c_type(std::ostream& out)
61     {
62         if(_static)
63             out << "static ";
64         if(_const)
65             out << "const ";
66         atomic_type->write_c(out);
67         for(int i = 0; i < pointer; ++i)
68             out << "*";
69         for(int i = 0; i < ref; ++i)
70             out << "&";
71     }
72
73     bool is_void() const
74     {
75         if(atomic_type == 0)
76             return true;
77         if(atomic_type == &BasicType::VOID && pointer == 0)
78             return true;
79         return false;
80     }
81
82     AtomicType* atomic_type;
83     bool _unsigned;
84     bool _const;
85     bool _static;
86     // number of '*' in the type declaration...
87     int pointer;
88     // number of '&' in the type declaration...
89     int ref;
90 };
91
92 class SQIntegerType : public AtomicType {
93 public:
94     SQIntegerType()
95     {
96         this->name = "SQInteger";
97         assert(_instance == 0);
98         _instance = this;
99     }
100     virtual ~SQIntegerType()
101     {
102         assert(_instance == this);
103         _instance = NULL;
104     }
105
106     static SQIntegerType* instance()
107     {
108         return _instance;
109     }
110 private:
111     static SQIntegerType* _instance;
112 };
113
114 class HSQUIRRELVMType : public AtomicType {
115 public:
116     HSQUIRRELVMType()
117     {
118         this->name = "HSQUIRRELVM";
119         assert(_instance == 0);
120         _instance = this;
121     }
122     virtual ~HSQUIRRELVMType()
123     {
124         assert(_instance == this);
125         _instance = NULL;
126     }
127
128     static HSQUIRRELVMType* instance()
129     {
130         return _instance;
131     }
132 private:
133     static HSQUIRRELVMType* _instance;
134 };
135
136 class StringType : public AtomicType {
137 public:
138     StringType()
139     {
140         this->name = "string";
141         assert(_instance == 0);
142         _instance = this;
143     }
144     virtual ~StringType()
145     {
146         assert(_instance == this);
147         _instance = 0;
148     }
149
150     static StringType* instance()
151     {
152         return _instance;
153     }
154
155     virtual void write_c(std::ostream& out)
156     {
157         out << "std::string";
158     }
159
160 private:
161     static StringType* _instance;
162 };
163
164 class Parameter
165 {
166 public:
167     Parameter() :
168         name(),
169         type()
170     { }
171
172     std::string name;
173     Type type;
174 };
175
176 class ClassMember {
177 public:
178     ClassMember() :
179         visibility()
180     { }
181     virtual ~ClassMember()
182     { }
183
184     enum Visibility {
185         PUBLIC,
186         PROTECTED,
187         PRIVATE
188     };
189     Visibility visibility;
190 };
191
192 class Function : public ClassMember {
193 public:
194     Function() :
195         type(),
196         suspend(),
197         custom(),
198         parameter_spec(),
199         docu_comment(),
200         name(),
201         return_type(),
202         parameters()
203     {
204         type = FUNCTION;
205         suspend = false;
206         custom = false;
207     }
208
209     enum FuncType {
210         FUNCTION,
211         CONSTRUCTOR,
212         DESTRUCTOR
213     };
214     FuncType type;
215     /// function should suspend squirrel VM after execution
216     bool suspend;
217     /// a custom wrapper (just pass along HSQUIRRELVM)
218     bool custom;
219     std::string parameter_spec;
220     std::string docu_comment;
221     std::string name;
222     Type return_type;
223     std::vector<Parameter> parameters;
224 };
225
226 class Field : public ClassMember {
227 public:
228     Field() :
229         type(),
230         docu_comment(),
231         name(),
232         has_const_value(),
233         const_float_value(),
234         const_int_value(),
235         const_string_value()
236     {
237         has_const_value = false;
238     }
239
240     Type* type;
241     std::string docu_comment;
242     std::string name;
243     bool has_const_value;
244
245     float const_float_value;
246     int const_int_value;
247     std::string const_string_value;
248
249 private:
250     Field(const Field&);
251     Field& operator=(const Field&);
252 };
253
254 class Class : public AtomicType {
255 public:
256     Class() :
257         members(),
258         super_classes(),
259         sub_classes(),
260         docu_comment()
261     { }
262     ~Class() {
263         for(std::vector<ClassMember*>::iterator i = members.begin(); i != members.end(); ++i)
264             delete *i;
265     }
266
267     std::vector<ClassMember*> members;
268     std::vector<Class*> super_classes;
269     std::vector<Class*> sub_classes;
270     std::string docu_comment;
271 };
272
273 class Namespace {
274 public:
275     Namespace() :
276         functions(),
277         fields(),
278         types(),
279         namespaces(),
280         parent(),
281         name()
282     {
283         parent = 0;
284     }
285     virtual ~Namespace() {
286         for(std::vector<Function*>::iterator i = functions.begin();
287                 i != functions.end(); ++i)
288             delete *i;
289         for(std::vector<AtomicType*>::iterator i = types.begin();
290                 i != types.end(); ++i)
291             delete *i;
292         for(std::vector<Namespace*>::iterator i = namespaces.begin();
293                 i != namespaces.end(); ++i)
294             delete *i;
295     }
296     void add_type(AtomicType* type)
297     {
298         types.push_back(type);
299         type->parent = this;
300     }
301     void add_namespace(Namespace* ns)
302     {
303         namespaces.push_back(ns);
304         ns->parent = this;
305     }
306     AtomicType* _findType(const std::string& name, bool godown = false) {
307         for(std::vector<AtomicType*>::iterator i = types.begin();
308                 i != types.end(); ++i) {
309             AtomicType* type = *i;
310             if(type->name == name)
311                 return type;
312         }
313         if(godown && parent)
314             return parent->_findType(name, true);
315
316         return 0;
317     }
318
319     Namespace* _findNamespace(const std::string& name, bool godown = false) {
320         for(std::vector<Namespace*>::iterator i = namespaces.begin();
321                 i != namespaces.end(); ++i) {
322             Namespace* ns = *i;
323             if(ns->name == name)
324                 return ns;
325         }
326         if(godown && parent)
327             return parent->_findNamespace(name, true);
328
329         return 0;
330     }
331
332     Namespace* findNamespace(const std::string& name, bool godown = false) {
333         Namespace* ret = _findNamespace(name, godown);
334         if(!ret) {
335             std::ostringstream msg;
336             msg << "Couldn't find namespace '" << name << "'.";
337             throw std::runtime_error(msg.str());
338         }
339
340         return ret;
341     }
342
343     std::vector<Function*> functions;
344     std::vector<Field*> fields;
345     std::vector<AtomicType*> types;
346     std::vector<Namespace*> namespaces;
347
348     Namespace* parent;
349     std::string name;
350
351 private:
352     Namespace(const Namespace&);
353     Namespace& operator=(const Namespace&);
354 };
355
356 class CompilationUnit : public Namespace {
357 public:
358 };
359
360 #endif