]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.h
fix wxExecute() compilation in ANSI build
[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 { m_bPureVirtual=c; }
169 void SetDeprecated(bool c = true)
170 { m_bDeprecated=c; }
171 void SetLocation(int lineNumber)
172 { m_nLine=lineNumber; }
173
174 public: // misc
175
176 bool operator==(const wxMethod&) const;
177 bool operator!=(const wxMethod& m) const
178 { return !(*this == m); }
179
180 void Dump(wxTextOutputStream& stream) const;
181
182 protected:
183 wxType m_retType;
184 wxString m_strName;
185 wxArgumentTypeArray m_args;
186 bool m_bConst;
187 bool m_bStatic;
188 bool m_bVirtual;
189 bool m_bPureVirtual;
190 bool m_bDeprecated;
191 int m_nLine;
192 };
193
194 WX_DECLARE_OBJARRAY(wxMethod, wxMethodArray);
195 WX_DEFINE_ARRAY(const wxMethod*, wxMethodPtrArray);
196
197
198 // ----------------------------------------------------------------------------
199 // Represents a class of the wx API/interface.
200 // ----------------------------------------------------------------------------
201 class wxClass
202 {
203 public:
204 wxClass() {}
205 wxClass(const wxString& name, const wxString& headername)
206 : m_strName(name), m_strHeader(headername) {}
207
208 void AddMethod(const wxMethod& func)
209 { m_methods.Add(func); }
210
211 void SetHeader(const wxString& header)
212 { m_strHeader=header; }
213 void SetName(const wxString& name)
214 { m_strName=name; }
215 wxString GetName() const
216 { return m_strName; }
217 wxString GetHeader() const
218 { return m_strHeader; }
219 wxString GetNameWithoutTemplate() const;
220
221 bool IsOk() const
222 { return !m_strName.IsEmpty() && !m_methods.IsEmpty(); }
223
224 bool IsValidCtorForThisClass(const wxMethod& m) const;
225 bool IsValidDtorForThisClass(const wxMethod& m) const;
226
227 unsigned int GetMethodCount() const
228 { return m_methods.GetCount(); }
229 wxMethod& GetMethod(unsigned int n) const
230 { return m_methods[n]; }
231 wxMethod& GetLastMethod() const
232 { return m_methods.Last(); }
233
234 // returns a single result (the first, which is also the only
235 // one if CheckConsistency() return true)
236 const wxMethod* FindMethod(const wxMethod& m) const;
237
238 // returns an array of pointers to the overloaded methods with the
239 // same given name
240 wxMethodPtrArray FindMethodNamed(const wxString& m) const;
241
242 // dumps all methods to the given output stream
243 void Dump(wxTextOutputStream& stream) const;
244
245 // slow check
246 bool CheckConsistency() const;
247
248 protected:
249 wxString m_strName;
250 wxString m_strHeader;
251 wxMethodArray m_methods;
252 };
253
254 WX_DECLARE_OBJARRAY(wxClass, wxClassArray);
255 WX_DEFINE_ARRAY(const wxClass*, wxClassPtrArray);
256
257
258
259 // ----------------------------------------------------------------------------
260 // wxXmlInterface
261 // ----------------------------------------------------------------------------
262 class wxXmlInterface
263 {
264 public:
265 wxXmlInterface() {}
266
267 const wxClass* FindClass(const wxString& classname) const
268 {
269 for (unsigned int i=0; i<m_classes.GetCount(); i++)
270 if (m_classes[i].GetName() == classname)
271 return &m_classes[i];
272 return NULL;
273 }
274
275 void Dump(const wxString& filename);
276
277 const wxClassArray& GetClasses() const
278 { return m_classes; }
279
280 unsigned int GetClassesCount() const
281 { return m_classes.GetCount(); }
282
283 unsigned int GetMethodCount() const
284 {
285 unsigned int methods = 0;
286 for (unsigned i=0; i < m_classes.GetCount(); i++)
287 methods += m_classes[i].GetMethodCount();
288 return methods;
289 }
290
291 void ShowProgress()
292 { /*wxPrint(".");*/ }
293
294 bool CheckParseResults() const;
295
296 protected:
297 wxClassArray m_classes;
298 };
299
300 #if 1
301 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
302 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
303 WX_DECLARE_HASH_MAP( unsigned long, wxString,
304 wxIntegerHash, wxIntegerEqual,
305 wxTypeIdHashMap );
306 #else
307 #include <map>
308 typedef std::basic_string<char> stlString;
309 typedef std::map<unsigned long, stlString> wxTypeIdHashMap;
310 #endif
311
312
313 // ----------------------------------------------------------------------------
314 // Represents the real interface of wxWidgets
315 // Loads it from the XML produced by gccXML: http://www.gccxml.org
316 // ----------------------------------------------------------------------------
317 class wxXmlGccInterface : public wxXmlInterface
318 {
319 public:
320 wxXmlGccInterface() {}
321
322 // !!SPEEDUP-TODO!!
323 // Using wxXmlDocument::Load as is, all the types contained in the
324 // the entire gccXML file are stored in memory while parsing;
325 // however we are only interested to wx's own structs/classes/funcs/etc
326 // so that we could use the file IDs to avoid loading stuff which does
327 // not belong to wx. See the very end of the gccXML file: it contains
328 // a set of <File> nodes referenced by all nodes above.
329
330 bool Parse(const wxString& filename);
331 bool ParseMethod(const wxXmlNode *p,
332 const wxTypeIdHashMap& types,
333 wxMethod& m);
334 };
335
336
337 // ----------------------------------------------------------------------------
338 // Represents the interface of the doxygen headers of wxWidgets
339 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
340 // ----------------------------------------------------------------------------
341 class wxXmlDoxygenInterface : public wxXmlInterface
342 {
343 public:
344 wxXmlDoxygenInterface() {}
345
346 // !!SPEEDUP-TODO!!
347 // Using wxXmlDocument::Load as is, the entire XML file is parsed
348 // and an entire tree of wxXmlNodes is built in memory.
349 // We need however only small portions of the Doxygen-generated XML: to speedup the
350 // processing we could detach the expat callbacks when we detect the beginning of a
351 // node we're not interested about, or just don't create a wxXmlNode for it!
352 // This needs a modification of wxXml API.
353
354 bool Parse(const wxString& filename);
355 bool ParseCompoundDefinition(const wxString& filename);
356 bool ParseMethod(const wxXmlNode*, wxMethod&, wxString& header);
357 };
358
359
360
361 #endif // _XMLPARSER_H_
362