]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.h
prototype fixes (in particular fix missing default value initializers)
[wxWidgets.git] / utils / ifacecheck / src / xmlparser.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xmlparser.h
3 // Purpose: Parser of the API/interface XML files
4 // Author: Francesco Montorsi
5 // Created: 2008/03/17
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #ifndef _XMLPARSER_H_
13 #define _XMLPARSER_H_
14
15 #include <wx/txtstrm.h>
16 #include <wx/dynarray.h>
17 #include <wx/xml/xml.h>
18
19 // helper macros
20 #define LogMessage(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); }
21 #define LogWarning(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); }
22 #define LogError(fmt, ...) { wxPrintf("ERROR: " fmt "\n", __VA_ARGS__); fflush(stdout); }
23 #define wxPrint(str) { wxPrintf(str); fflush(stdout); }
24
25
26 // ----------------------------------------------------------------------------
27 // Represents a type with or without const/static/ qualifier
28 // and with or without & and * operators
29 // ----------------------------------------------------------------------------
30 class wxType
31 {
32 public:
33 wxType() {}
34 wxType(const wxString& type)
35 { SetTypeFromString(type); }
36
37 void SetTypeFromString(const wxString& t);
38 wxString GetAsString() const
39 { return m_strType; }
40
41 bool IsConst() const
42 { return m_strType.Contains("const"); }
43 bool IsStatic() const
44 { return m_strType.Contains("static"); }
45 bool IsPointer() const
46 { return m_strType.Contains("*"); }
47 bool IsReference() const
48 { return m_strType.Contains("&"); }
49
50 bool operator==(const wxType& m) const;
51 bool operator!=(const wxType& m) const
52 { return !(*this == m); }
53
54 bool IsOk() const;
55
56 protected:
57 wxString m_strType;
58
59 // utility for doing comparisons
60 wxString GetClean() const;
61 };
62
63 extern wxType wxEmptyType;
64 WX_DECLARE_OBJARRAY(wxType, wxTypeArray);
65
66
67 // ----------------------------------------------------------------------------
68 // Represents a type used as argument for some wxMethod
69 // ----------------------------------------------------------------------------
70 class wxArgumentType : public wxType
71 {
72 public:
73 wxArgumentType() {}
74 wxArgumentType(const wxString& type, const wxString& defVal,
75 const wxString& argName = wxEmptyString)
76 { SetTypeFromString(type); SetDefaultValue(defVal); m_strArgName = argName; }
77
78 void SetArgumentName(const wxString& name)
79 { m_strArgName=name.Strip(wxString::both); }
80 wxString GetArgumentName() const
81 { return m_strArgName; }
82
83 void SetDefaultValue(const wxString& defval);
84 wxString GetDefaultValue() const
85 { return m_strDefaultValue; }
86
87 bool operator==(const wxArgumentType& m) const;
88 bool operator!=(const wxArgumentType& m) const
89 { return !(*this == m); }
90
91 protected:
92 wxString m_strDefaultValue;
93 wxString m_strArgName; // this one only makes sense when this wxType is
94 // used as argument type (and not as return type)
95 // and can be empty.
96 };
97
98 extern wxArgumentType wxEmptyArgumentType;
99 WX_DECLARE_OBJARRAY(wxArgumentType, wxArgumentTypeArray);
100
101
102 // ----------------------------------------------------------------------------
103 // Represents a single prototype of a class' member.
104 // ----------------------------------------------------------------------------
105 class wxMethod
106 {
107 public:
108 wxMethod()
109 { m_bConst=m_bVirtual=m_bPureVirtual=m_bStatic=m_bDeprecated=false; m_nLine=-1; }
110
111 wxMethod(const wxType& rettype, const wxString& name,
112 const wxArgumentTypeArray& arguments,
113 bool isConst, bool isStatic, bool isVirtual)
114 : m_retType(rettype), m_strName(name.Strip(wxString::both)),
115 m_bConst(isConst), m_bStatic(isStatic), m_bVirtual(isVirtual)
116 { SetArgumentTypes(arguments); m_nLine=-1; }
117
118
119 public: // getters
120
121 wxString GetAsString(bool bWithArgumentNames = true) const;
122
123 // parser of the prototype:
124 // all these functions return strings with spaces stripped
125 wxType GetReturnType() const
126 { return m_retType; }
127 wxString GetName() const
128 { return m_strName; }
129 wxArgumentTypeArray GetArgumentTypes() const
130 { return m_args; }
131 int GetLocation() const
132 { return m_nLine; }
133
134 bool IsConst() const
135 { return m_bConst; }
136 bool IsStatic() const
137 { return m_bStatic; }
138 bool IsVirtual() const
139 { return m_bVirtual; }
140 bool IsPureVirtual() const
141 { return m_bPureVirtual; }
142
143 bool IsOk() const;
144 bool IsCtor() const
145 { return m_retType==wxEmptyType && !m_strName.StartsWith("~"); }
146 bool IsDtor() const
147 { return m_retType==wxEmptyType && m_strName.StartsWith("~"); }
148
149 bool IsDeprecated() const
150 { return m_bDeprecated; }
151
152
153 public: // setters
154
155 void SetReturnType(const wxType& t)
156 { m_retType=t; }
157 void SetName(const wxString& name)
158 { m_strName=name; }
159 void SetArgumentTypes(const wxArgumentTypeArray& arr)
160 { m_args=arr; }
161 void SetConst(bool c = true)
162 { m_bConst=c; }
163 void SetStatic(bool c = true)
164 { m_bStatic=c; }
165 void SetVirtual(bool c = true)
166 { m_bVirtual=c; }
167 void SetPureVirtual(bool c = true)
168 {
169 m_bPureVirtual=c;
170 if (c) m_bVirtual=c; // pure virtual => virtual
171 }
172 void SetDeprecated(bool c = true)
173 { m_bDeprecated=c; }
174 void SetLocation(int lineNumber)
175 { m_nLine=lineNumber; }
176
177 public: // misc
178
179 bool operator==(const wxMethod&) const;
180 bool operator!=(const wxMethod& m) const
181 { return !(*this == m); }
182
183 void Dump(wxTextOutputStream& stream) const;
184
185 protected:
186 wxType m_retType;
187 wxString m_strName;
188 wxArgumentTypeArray m_args;
189 bool m_bConst;
190 bool m_bStatic;
191 bool m_bVirtual;
192 bool m_bPureVirtual;
193 bool m_bDeprecated;
194 int m_nLine;
195 };
196
197 WX_DECLARE_OBJARRAY(wxMethod, wxMethodArray);
198 WX_DEFINE_ARRAY(const wxMethod*, wxMethodPtrArray);
199
200
201 // ----------------------------------------------------------------------------
202 // Represents a class of the wx API/interface.
203 // ----------------------------------------------------------------------------
204 class wxClass
205 {
206 public:
207 wxClass() {}
208 wxClass(const wxString& name, const wxString& headername)
209 : m_strName(name), m_strHeader(headername) {}
210
211 void AddMethod(const wxMethod& func)
212 { m_methods.Add(func); }
213
214 void SetHeader(const wxString& header)
215 { m_strHeader=header; }
216 void SetName(const wxString& name)
217 { m_strName=name; }
218 wxString GetName() const
219 { return m_strName; }
220 wxString GetHeader() const
221 { return m_strHeader; }
222 wxString GetNameWithoutTemplate() const;
223
224 bool IsOk() const
225 { return !m_strName.IsEmpty() && !m_methods.IsEmpty(); }
226
227 bool IsValidCtorForThisClass(const wxMethod& m) const;
228 bool IsValidDtorForThisClass(const wxMethod& m) const;
229
230 unsigned int GetMethodCount() const
231 { return m_methods.GetCount(); }
232 wxMethod& GetMethod(unsigned int n) const
233 { return m_methods[n]; }
234 wxMethod& GetLastMethod() const
235 { return m_methods.Last(); }
236
237 // returns a single result (the first, which is also the only
238 // one if CheckConsistency() return true)
239 const wxMethod* FindMethod(const wxMethod& m) const;
240
241 // returns an array of pointers to the overloaded methods with the
242 // same given name
243 wxMethodPtrArray FindMethodNamed(const wxString& m) const;
244
245 // dumps all methods to the given output stream
246 void Dump(wxTextOutputStream& stream) const;
247
248 // slow check
249 bool CheckConsistency() const;
250
251 protected:
252 wxString m_strName;
253 wxString m_strHeader;
254 wxMethodArray m_methods;
255 };
256
257 WX_DECLARE_OBJARRAY(wxClass, wxClassArray);
258 WX_DEFINE_ARRAY(const wxClass*, wxClassPtrArray);
259
260
261
262 // ----------------------------------------------------------------------------
263 // wxXmlInterface
264 // ----------------------------------------------------------------------------
265 class wxXmlInterface
266 {
267 public:
268 wxXmlInterface() {}
269
270 const wxClass* FindClass(const wxString& classname) const
271 {
272 for (unsigned int i=0; i<m_classes.GetCount(); i++)
273 if (m_classes[i].GetName() == classname)
274 return &m_classes[i];
275 return NULL;
276 }
277
278 void Dump(const wxString& filename);
279
280 const wxClassArray& GetClasses() const
281 { return m_classes; }
282
283 unsigned int GetClassesCount() const
284 { return m_classes.GetCount(); }
285
286 unsigned int GetMethodCount() const
287 {
288 unsigned int methods = 0;
289 for (unsigned i=0; i < m_classes.GetCount(); i++)
290 methods += m_classes[i].GetMethodCount();
291 return methods;
292 }
293
294 void ShowProgress()
295 { /*wxPrint(".");*/ }
296
297 bool CheckParseResults() const;
298
299 protected:
300 wxClassArray m_classes;
301 };
302
303 #if 1
304 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
305 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
306 WX_DECLARE_HASH_MAP( unsigned long, wxString,
307 wxIntegerHash, wxIntegerEqual,
308 wxTypeIdHashMap );
309 #else
310 #include <map>
311 typedef std::basic_string<char> stlString;
312 typedef std::map<unsigned long, stlString> wxTypeIdHashMap;
313 #endif
314
315
316 // ----------------------------------------------------------------------------
317 // Represents the real interface of wxWidgets
318 // Loads it from the XML produced by gccXML: http://www.gccxml.org
319 // ----------------------------------------------------------------------------
320 class wxXmlGccInterface : public wxXmlInterface
321 {
322 public:
323 wxXmlGccInterface() {}
324
325 // !!SPEEDUP-TODO!!
326 // Using wxXmlDocument::Load as is, all the types contained in the
327 // the entire gccXML file are stored in memory while parsing;
328 // however we are only interested to wx's own structs/classes/funcs/etc
329 // so that we could use the file IDs to avoid loading stuff which does
330 // not belong to wx. See the very end of the gccXML file: it contains
331 // a set of <File> nodes referenced by all nodes above.
332
333 bool Parse(const wxString& filename);
334 bool ParseMethod(const wxXmlNode *p,
335 const wxTypeIdHashMap& types,
336 wxMethod& m);
337 };
338
339
340 // ----------------------------------------------------------------------------
341 // Represents the interface of the doxygen headers of wxWidgets
342 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
343 // ----------------------------------------------------------------------------
344 class wxXmlDoxygenInterface : public wxXmlInterface
345 {
346 public:
347 wxXmlDoxygenInterface() {}
348
349 // !!SPEEDUP-TODO!!
350 // Using wxXmlDocument::Load as is, the entire XML file is parsed
351 // and an entire tree of wxXmlNodes is built in memory.
352 // We need however only small portions of the Doxygen-generated XML: to speedup the
353 // processing we could detach the expat callbacks when we detect the beginning of a
354 // node we're not interested about, or just don't create a wxXmlNode for it!
355 // This needs a modification of wxXml API.
356
357 bool Parse(const wxString& filename);
358 bool ParseCompoundDefinition(const wxString& filename);
359 bool ParseMethod(const wxXmlNode*, wxMethod&, wxString& header);
360 };
361
362
363
364 #endif // _XMLPARSER_H_
365