]>
Commit | Line | Data |
---|---|---|
5934cda1 FM |
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> | |
d2367a0f | 18 | #include <wx/platinfo.h> |
5934cda1 FM |
19 | |
20 | // helper macros | |
21 | #define LogMessage(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); } | |
4168bc45 | 22 | #define LogWarning(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); } |
5934cda1 FM |
23 | #define LogError(fmt, ...) { wxPrintf("ERROR: " fmt "\n", __VA_ARGS__); fflush(stdout); } |
24 | #define wxPrint(str) { wxPrintf(str); fflush(stdout); } | |
25 | ||
26 | ||
27 | // ---------------------------------------------------------------------------- | |
28 | // Represents a type with or without const/static/ qualifier | |
29 | // and with or without & and * operators | |
30 | // ---------------------------------------------------------------------------- | |
31 | class wxType | |
32 | { | |
33 | public: | |
34 | wxType() {} | |
35 | wxType(const wxString& type) | |
f270e1dd | 36 | { SetTypeFromString(type); } |
5934cda1 | 37 | |
f270e1dd | 38 | void SetTypeFromString(const wxString& t); |
5934cda1 FM |
39 | wxString GetAsString() const |
40 | { return m_strType; } | |
41 | ||
5934cda1 FM |
42 | bool IsConst() const |
43 | { return m_strType.Contains("const"); } | |
44 | bool IsStatic() const | |
45 | { return m_strType.Contains("static"); } | |
46 | bool IsPointer() const | |
47 | { return m_strType.Contains("*"); } | |
48 | bool IsReference() const | |
49 | { return m_strType.Contains("&"); } | |
50 | ||
51 | bool operator==(const wxType& m) const; | |
52 | bool operator!=(const wxType& m) const | |
53 | { return !(*this == m); } | |
54 | ||
55 | bool IsOk() const; | |
56 | ||
57 | protected: | |
658d9804 FM |
58 | wxString m_strType, |
59 | m_strTypeClean; // m_strType "cleaned" of its attributes | |
60 | // (only for internal use) | |
5934cda1 FM |
61 | }; |
62 | ||
63 | extern wxType wxEmptyType; | |
64 | WX_DECLARE_OBJARRAY(wxType, wxTypeArray); | |
65 | ||
66 | ||
f270e1dd FM |
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 | ||
5570107a | 83 | void SetDefaultValue(const wxString& defval, const wxString& defvalForCmp = wxEmptyString); |
f270e1dd FM |
84 | wxString GetDefaultValue() const |
85 | { return m_strDefaultValue; } | |
86 | ||
203ba76a FM |
87 | bool HasDefaultValue() const |
88 | { return !m_strDefaultValue.IsEmpty(); } | |
89 | ||
f270e1dd FM |
90 | bool operator==(const wxArgumentType& m) const; |
91 | bool operator!=(const wxArgumentType& m) const | |
92 | { return !(*this == m); } | |
93 | ||
94 | protected: | |
95 | wxString m_strDefaultValue; | |
5570107a FM |
96 | |
97 | // this string may differ from m_strDefaultValue if there were | |
98 | // preprocessor substitutions; can be wxEmptyString. | |
99 | wxString m_strDefaultValueForCmp; | |
100 | ||
f270e1dd FM |
101 | wxString m_strArgName; // this one only makes sense when this wxType is |
102 | // used as argument type (and not as return type) | |
103 | // and can be empty. | |
104 | }; | |
105 | ||
106 | extern wxArgumentType wxEmptyArgumentType; | |
107 | WX_DECLARE_OBJARRAY(wxArgumentType, wxArgumentTypeArray); | |
108 | ||
5934cda1 FM |
109 | |
110 | // ---------------------------------------------------------------------------- | |
111 | // Represents a single prototype of a class' member. | |
112 | // ---------------------------------------------------------------------------- | |
113 | class wxMethod | |
114 | { | |
115 | public: | |
0403e5dc | 116 | wxMethod() |
03d4f7b9 FM |
117 | { m_bConst=m_bVirtual=m_bPureVirtual=m_bStatic=m_bDeprecated=false; |
118 | m_nLine=-1; m_nAvailability=wxPORT_UNKNOWN; } | |
0403e5dc | 119 | |
5934cda1 | 120 | wxMethod(const wxType& rettype, const wxString& name, |
f270e1dd | 121 | const wxArgumentTypeArray& arguments, |
5934cda1 FM |
122 | bool isConst, bool isStatic, bool isVirtual) |
123 | : m_retType(rettype), m_strName(name.Strip(wxString::both)), | |
124 | m_bConst(isConst), m_bStatic(isStatic), m_bVirtual(isVirtual) | |
f270e1dd | 125 | { SetArgumentTypes(arguments); m_nLine=-1; } |
5934cda1 FM |
126 | |
127 | ||
128 | public: // getters | |
129 | ||
f270e1dd | 130 | wxString GetAsString(bool bWithArgumentNames = true) const; |
5934cda1 FM |
131 | |
132 | // parser of the prototype: | |
133 | // all these functions return strings with spaces stripped | |
134 | wxType GetReturnType() const | |
135 | { return m_retType; } | |
136 | wxString GetName() const | |
137 | { return m_strName; } | |
f270e1dd | 138 | wxArgumentTypeArray GetArgumentTypes() const |
5934cda1 | 139 | { return m_args; } |
5934cda1 FM |
140 | int GetLocation() const |
141 | { return m_nLine; } | |
03d4f7b9 FM |
142 | int GetAvailability() const |
143 | { return m_nAvailability; } | |
5934cda1 FM |
144 | |
145 | bool IsConst() const | |
146 | { return m_bConst; } | |
147 | bool IsStatic() const | |
148 | { return m_bStatic; } | |
149 | bool IsVirtual() const | |
150 | { return m_bVirtual; } | |
a7be99c8 FM |
151 | bool IsPureVirtual() const |
152 | { return m_bPureVirtual; } | |
5934cda1 FM |
153 | |
154 | bool IsOk() const; | |
155 | bool IsCtor() const | |
156 | { return m_retType==wxEmptyType && !m_strName.StartsWith("~"); } | |
157 | bool IsDtor() const | |
158 | { return m_retType==wxEmptyType && m_strName.StartsWith("~"); } | |
159 | ||
0403e5dc FM |
160 | bool IsDeprecated() const |
161 | { return m_bDeprecated; } | |
162 | ||
5934cda1 FM |
163 | |
164 | public: // setters | |
165 | ||
166 | void SetReturnType(const wxType& t) | |
167 | { m_retType=t; } | |
168 | void SetName(const wxString& name) | |
169 | { m_strName=name; } | |
f270e1dd FM |
170 | void SetArgumentTypes(const wxArgumentTypeArray& arr) |
171 | { m_args=arr; } | |
5934cda1 FM |
172 | void SetConst(bool c = true) |
173 | { m_bConst=c; } | |
174 | void SetStatic(bool c = true) | |
175 | { m_bStatic=c; } | |
176 | void SetVirtual(bool c = true) | |
177 | { m_bVirtual=c; } | |
a7be99c8 | 178 | void SetPureVirtual(bool c = true) |
d5978709 FM |
179 | { |
180 | m_bPureVirtual=c; | |
181 | if (c) m_bVirtual=c; // pure virtual => virtual | |
182 | } | |
0403e5dc FM |
183 | void SetDeprecated(bool c = true) |
184 | { m_bDeprecated=c; } | |
5934cda1 FM |
185 | void SetLocation(int lineNumber) |
186 | { m_nLine=lineNumber; } | |
03d4f7b9 FM |
187 | void SetAvailability(int nAvail) |
188 | { m_nAvailability=nAvail; } | |
5934cda1 FM |
189 | |
190 | public: // misc | |
191 | ||
192 | bool operator==(const wxMethod&) const; | |
193 | bool operator!=(const wxMethod& m) const | |
194 | { return !(*this == m); } | |
195 | ||
196 | void Dump(wxTextOutputStream& stream) const; | |
197 | ||
198 | protected: | |
199 | wxType m_retType; | |
200 | wxString m_strName; | |
f270e1dd | 201 | wxArgumentTypeArray m_args; |
03d4f7b9 FM |
202 | |
203 | // misc attributes: | |
5934cda1 FM |
204 | bool m_bConst; |
205 | bool m_bStatic; | |
206 | bool m_bVirtual; | |
a7be99c8 | 207 | bool m_bPureVirtual; |
0403e5dc | 208 | bool m_bDeprecated; |
03d4f7b9 FM |
209 | |
210 | // m_nLine can be -1 if no location infos are available | |
5934cda1 | 211 | int m_nLine; |
03d4f7b9 FM |
212 | |
213 | // this is a combination of wxPORT_* flags (see wxPortId) or wxPORT_UNKNOWN | |
214 | // if this method should be available for all wxWidgets ports. | |
215 | // NOTE: this is not used for comparing wxMethod objects | |
216 | // (gccXML never gives this kind of info). | |
217 | int m_nAvailability; | |
5934cda1 FM |
218 | }; |
219 | ||
220 | WX_DECLARE_OBJARRAY(wxMethod, wxMethodArray); | |
221 | WX_DEFINE_ARRAY(const wxMethod*, wxMethodPtrArray); | |
222 | ||
223 | ||
224 | // ---------------------------------------------------------------------------- | |
225 | // Represents a class of the wx API/interface. | |
226 | // ---------------------------------------------------------------------------- | |
227 | class wxClass | |
228 | { | |
229 | public: | |
230 | wxClass() {} | |
231 | wxClass(const wxString& name, const wxString& headername) | |
232 | : m_strName(name), m_strHeader(headername) {} | |
233 | ||
03d4f7b9 FM |
234 | |
235 | public: // setters | |
5934cda1 FM |
236 | |
237 | void SetHeader(const wxString& header) | |
238 | { m_strHeader=header; } | |
239 | void SetName(const wxString& name) | |
240 | { m_strName=name; } | |
03d4f7b9 FM |
241 | void SetAvailability(int nAvail) |
242 | { m_nAvailability=nAvail; } | |
243 | ||
244 | ||
245 | public: // getters | |
5934cda1 FM |
246 | |
247 | bool IsOk() const | |
248 | { return !m_strName.IsEmpty() && !m_methods.IsEmpty(); } | |
249 | ||
250 | bool IsValidCtorForThisClass(const wxMethod& m) const; | |
251 | bool IsValidDtorForThisClass(const wxMethod& m) const; | |
252 | ||
03d4f7b9 FM |
253 | wxString GetName() const |
254 | { return m_strName; } | |
255 | wxString GetHeader() const | |
256 | { return m_strHeader; } | |
257 | wxString GetNameWithoutTemplate() const; | |
258 | ||
5934cda1 FM |
259 | unsigned int GetMethodCount() const |
260 | { return m_methods.GetCount(); } | |
261 | wxMethod& GetMethod(unsigned int n) const | |
262 | { return m_methods[n]; } | |
263 | wxMethod& GetLastMethod() const | |
264 | { return m_methods.Last(); } | |
265 | ||
03d4f7b9 FM |
266 | int GetAvailability() const |
267 | { return m_nAvailability; } | |
268 | ||
269 | public: // misc | |
270 | ||
271 | void AddMethod(const wxMethod& func) | |
272 | { m_methods.Add(func); } | |
273 | ||
5934cda1 FM |
274 | // returns a single result (the first, which is also the only |
275 | // one if CheckConsistency() return true) | |
276 | const wxMethod* FindMethod(const wxMethod& m) const; | |
277 | ||
278 | // returns an array of pointers to the overloaded methods with the | |
279 | // same given name | |
280 | wxMethodPtrArray FindMethodNamed(const wxString& m) const; | |
281 | ||
282 | // dumps all methods to the given output stream | |
283 | void Dump(wxTextOutputStream& stream) const; | |
284 | ||
285 | // slow check | |
286 | bool CheckConsistency() const; | |
287 | ||
288 | protected: | |
289 | wxString m_strName; | |
290 | wxString m_strHeader; | |
291 | wxMethodArray m_methods; | |
03d4f7b9 FM |
292 | |
293 | // see the wxMethod::m_nAvailability field for more info | |
294 | int m_nAvailability; | |
5934cda1 FM |
295 | }; |
296 | ||
297 | WX_DECLARE_OBJARRAY(wxClass, wxClassArray); | |
298 | WX_DEFINE_ARRAY(const wxClass*, wxClassPtrArray); | |
299 | ||
300 | ||
301 | ||
302 | // ---------------------------------------------------------------------------- | |
303 | // wxXmlInterface | |
304 | // ---------------------------------------------------------------------------- | |
305 | class wxXmlInterface | |
306 | { | |
307 | public: | |
308 | wxXmlInterface() {} | |
309 | ||
310 | const wxClass* FindClass(const wxString& classname) const | |
311 | { | |
312 | for (unsigned int i=0; i<m_classes.GetCount(); i++) | |
313 | if (m_classes[i].GetName() == classname) | |
314 | return &m_classes[i]; | |
315 | return NULL; | |
316 | } | |
317 | ||
318 | void Dump(const wxString& filename); | |
319 | ||
320 | const wxClassArray& GetClasses() const | |
321 | { return m_classes; } | |
322 | ||
323 | unsigned int GetClassesCount() const | |
324 | { return m_classes.GetCount(); } | |
325 | ||
326 | unsigned int GetMethodCount() const | |
327 | { | |
328 | unsigned int methods = 0; | |
329 | for (unsigned i=0; i < m_classes.GetCount(); i++) | |
330 | methods += m_classes[i].GetMethodCount(); | |
331 | return methods; | |
332 | } | |
333 | ||
7fbadf87 FM |
334 | // pass a full-path header filename: |
335 | wxClassPtrArray FindClassesDefinedIn(const wxString& headerfile) const; | |
336 | ||
5934cda1 FM |
337 | void ShowProgress() |
338 | { /*wxPrint(".");*/ } | |
339 | ||
340 | bool CheckParseResults() const; | |
341 | ||
342 | protected: | |
343 | wxClassArray m_classes; | |
344 | }; | |
345 | ||
346 | #if 1 | |
74bda203 FM |
347 | // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings |
348 | // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&" | |
fdd4a897 FM |
349 | WX_DECLARE_HASH_MAP( unsigned long, wxString, |
350 | wxIntegerHash, wxIntegerEqual, | |
351 | wxTypeIdHashMap ); | |
5570107a FM |
352 | |
353 | WX_DECLARE_STRING_HASH_MAP( wxString, wxStringHashMap ); | |
5934cda1 FM |
354 | #else |
355 | #include <map> | |
356 | typedef std::basic_string<char> stlString; | |
fdd4a897 | 357 | typedef std::map<unsigned long, stlString> wxTypeIdHashMap; |
5934cda1 FM |
358 | #endif |
359 | ||
360 | ||
361 | // ---------------------------------------------------------------------------- | |
362 | // Represents the real interface of wxWidgets | |
363 | // Loads it from the XML produced by gccXML: http://www.gccxml.org | |
364 | // ---------------------------------------------------------------------------- | |
365 | class wxXmlGccInterface : public wxXmlInterface | |
366 | { | |
367 | public: | |
03d4f7b9 FM |
368 | wxXmlGccInterface() |
369 | { | |
370 | // FIXME: we should retrieve this from the XML file! | |
371 | // here we suppose the XML was created for the currently-running port | |
372 | m_portId = wxPlatformInfo::Get().GetPortId(); | |
373 | } | |
5934cda1 | 374 | |
5934cda1 FM |
375 | bool Parse(const wxString& filename); |
376 | bool ParseMethod(const wxXmlNode *p, | |
fdd4a897 | 377 | const wxTypeIdHashMap& types, |
5934cda1 | 378 | wxMethod& m); |
03d4f7b9 FM |
379 | |
380 | wxPortId GetInterfacePort() const | |
381 | { return m_portId; } | |
382 | ||
383 | wxString GetInterfacePortName() const | |
384 | { return wxPlatformInfo::GetPortIdName(m_portId, false); } | |
385 | ||
386 | protected: | |
387 | // the port for which the gcc XML was generated | |
388 | wxPortId m_portId; | |
5934cda1 FM |
389 | }; |
390 | ||
391 | ||
392 | // ---------------------------------------------------------------------------- | |
393 | // Represents the interface of the doxygen headers of wxWidgets | |
394 | // Loads it from the XML produced by Doxygen: http://www.doxygen.org | |
395 | // ---------------------------------------------------------------------------- | |
396 | class wxXmlDoxygenInterface : public wxXmlInterface | |
397 | { | |
398 | public: | |
399 | wxXmlDoxygenInterface() {} | |
400 | ||
401 | // !!SPEEDUP-TODO!! | |
402 | // Using wxXmlDocument::Load as is, the entire XML file is parsed | |
403 | // and an entire tree of wxXmlNodes is built in memory. | |
404 | // We need however only small portions of the Doxygen-generated XML: to speedup the | |
405 | // processing we could detach the expat callbacks when we detect the beginning of a | |
406 | // node we're not interested about, or just don't create a wxXmlNode for it! | |
407 | // This needs a modification of wxXml API. | |
408 | ||
409 | bool Parse(const wxString& filename); | |
410 | bool ParseCompoundDefinition(const wxString& filename); | |
411 | bool ParseMethod(const wxXmlNode*, wxMethod&, wxString& header); | |
5570107a FM |
412 | |
413 | // this class can take advantage of the preprocessor output to give | |
414 | // a minor number of false positive warnings in the final comparison | |
415 | void AddPreprocessorValue(const wxString& name, const wxString& val) | |
416 | { m_preproc[name]=val; } | |
417 | ||
418 | protected: | |
419 | wxStringHashMap m_preproc; | |
5934cda1 FM |
420 | }; |
421 | ||
422 | ||
423 | ||
424 | #endif // _XMLPARSER_H_ | |
425 |