]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.h
6e1de99a42fede5c8c12a40ff9f090bb2eae5c37
[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, const wxString& defvalForCmp = wxEmptyString);
84 wxString GetDefaultValue() const
85 { return m_strDefaultValue; }
86
87 bool HasDefaultValue() const
88 { return !m_strDefaultValue.IsEmpty(); }
89
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;
96
97 // this string may differ from m_strDefaultValue if there were
98 // preprocessor substitutions; can be wxEmptyString.
99 wxString m_strDefaultValueForCmp;
100
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
109
110 // ----------------------------------------------------------------------------
111 // Represents a single prototype of a class' member.
112 // ----------------------------------------------------------------------------
113 class wxMethod
114 {
115 public:
116 wxMethod()
117 { m_bConst=m_bVirtual=m_bPureVirtual=m_bStatic=m_bDeprecated=false; m_nLine=-1; }
118
119 wxMethod(const wxType& rettype, const wxString& name,
120 const wxArgumentTypeArray& arguments,
121 bool isConst, bool isStatic, bool isVirtual)
122 : m_retType(rettype), m_strName(name.Strip(wxString::both)),
123 m_bConst(isConst), m_bStatic(isStatic), m_bVirtual(isVirtual)
124 { SetArgumentTypes(arguments); m_nLine=-1; }
125
126
127 public: // getters
128
129 wxString GetAsString(bool bWithArgumentNames = true) const;
130
131 // parser of the prototype:
132 // all these functions return strings with spaces stripped
133 wxType GetReturnType() const
134 { return m_retType; }
135 wxString GetName() const
136 { return m_strName; }
137 wxArgumentTypeArray GetArgumentTypes() const
138 { return m_args; }
139 int GetLocation() const
140 { return m_nLine; }
141
142 bool IsConst() const
143 { return m_bConst; }
144 bool IsStatic() const
145 { return m_bStatic; }
146 bool IsVirtual() const
147 { return m_bVirtual; }
148 bool IsPureVirtual() const
149 { return m_bPureVirtual; }
150
151 bool IsOk() const;
152 bool IsCtor() const
153 { return m_retType==wxEmptyType && !m_strName.StartsWith("~"); }
154 bool IsDtor() const
155 { return m_retType==wxEmptyType && m_strName.StartsWith("~"); }
156
157 bool IsDeprecated() const
158 { return m_bDeprecated; }
159
160
161 public: // setters
162
163 void SetReturnType(const wxType& t)
164 { m_retType=t; }
165 void SetName(const wxString& name)
166 { m_strName=name; }
167 void SetArgumentTypes(const wxArgumentTypeArray& arr)
168 { m_args=arr; }
169 void SetConst(bool c = true)
170 { m_bConst=c; }
171 void SetStatic(bool c = true)
172 { m_bStatic=c; }
173 void SetVirtual(bool c = true)
174 { m_bVirtual=c; }
175 void SetPureVirtual(bool c = true)
176 {
177 m_bPureVirtual=c;
178 if (c) m_bVirtual=c; // pure virtual => virtual
179 }
180 void SetDeprecated(bool c = true)
181 { m_bDeprecated=c; }
182 void SetLocation(int lineNumber)
183 { m_nLine=lineNumber; }
184
185 public: // misc
186
187 bool operator==(const wxMethod&) const;
188 bool operator!=(const wxMethod& m) const
189 { return !(*this == m); }
190
191 void Dump(wxTextOutputStream& stream) const;
192
193 protected:
194 wxType m_retType;
195 wxString m_strName;
196 wxArgumentTypeArray m_args;
197 bool m_bConst;
198 bool m_bStatic;
199 bool m_bVirtual;
200 bool m_bPureVirtual;
201 bool m_bDeprecated;
202 int m_nLine;
203 };
204
205 WX_DECLARE_OBJARRAY(wxMethod, wxMethodArray);
206 WX_DEFINE_ARRAY(const wxMethod*, wxMethodPtrArray);
207
208
209 // ----------------------------------------------------------------------------
210 // Represents a class of the wx API/interface.
211 // ----------------------------------------------------------------------------
212 class wxClass
213 {
214 public:
215 wxClass() {}
216 wxClass(const wxString& name, const wxString& headername)
217 : m_strName(name), m_strHeader(headername) {}
218
219 void AddMethod(const wxMethod& func)
220 { m_methods.Add(func); }
221
222 void SetHeader(const wxString& header)
223 { m_strHeader=header; }
224 void SetName(const wxString& name)
225 { m_strName=name; }
226 wxString GetName() const
227 { return m_strName; }
228 wxString GetHeader() const
229 { return m_strHeader; }
230 wxString GetNameWithoutTemplate() const;
231
232 bool IsOk() const
233 { return !m_strName.IsEmpty() && !m_methods.IsEmpty(); }
234
235 bool IsValidCtorForThisClass(const wxMethod& m) const;
236 bool IsValidDtorForThisClass(const wxMethod& m) const;
237
238 unsigned int GetMethodCount() const
239 { return m_methods.GetCount(); }
240 wxMethod& GetMethod(unsigned int n) const
241 { return m_methods[n]; }
242 wxMethod& GetLastMethod() const
243 { return m_methods.Last(); }
244
245 // returns a single result (the first, which is also the only
246 // one if CheckConsistency() return true)
247 const wxMethod* FindMethod(const wxMethod& m) const;
248
249 // returns an array of pointers to the overloaded methods with the
250 // same given name
251 wxMethodPtrArray FindMethodNamed(const wxString& m) const;
252
253 // dumps all methods to the given output stream
254 void Dump(wxTextOutputStream& stream) const;
255
256 // slow check
257 bool CheckConsistency() const;
258
259 protected:
260 wxString m_strName;
261 wxString m_strHeader;
262 wxMethodArray m_methods;
263 };
264
265 WX_DECLARE_OBJARRAY(wxClass, wxClassArray);
266 WX_DEFINE_ARRAY(const wxClass*, wxClassPtrArray);
267
268
269
270 // ----------------------------------------------------------------------------
271 // wxXmlInterface
272 // ----------------------------------------------------------------------------
273 class wxXmlInterface
274 {
275 public:
276 wxXmlInterface() {}
277
278 const wxClass* FindClass(const wxString& classname) const
279 {
280 for (unsigned int i=0; i<m_classes.GetCount(); i++)
281 if (m_classes[i].GetName() == classname)
282 return &m_classes[i];
283 return NULL;
284 }
285
286 void Dump(const wxString& filename);
287
288 const wxClassArray& GetClasses() const
289 { return m_classes; }
290
291 unsigned int GetClassesCount() const
292 { return m_classes.GetCount(); }
293
294 unsigned int GetMethodCount() const
295 {
296 unsigned int methods = 0;
297 for (unsigned i=0; i < m_classes.GetCount(); i++)
298 methods += m_classes[i].GetMethodCount();
299 return methods;
300 }
301
302 // pass a full-path header filename:
303 wxClassPtrArray FindClassesDefinedIn(const wxString& headerfile) const;
304
305 void ShowProgress()
306 { /*wxPrint(".");*/ }
307
308 bool CheckParseResults() const;
309
310 protected:
311 wxClassArray m_classes;
312 };
313
314 #if 1
315 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
316 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
317 WX_DECLARE_HASH_MAP( unsigned long, wxString,
318 wxIntegerHash, wxIntegerEqual,
319 wxTypeIdHashMap );
320
321 WX_DECLARE_STRING_HASH_MAP( wxString, wxStringHashMap );
322 #else
323 #include <map>
324 typedef std::basic_string<char> stlString;
325 typedef std::map<unsigned long, stlString> wxTypeIdHashMap;
326 #endif
327
328
329 // ----------------------------------------------------------------------------
330 // Represents the real interface of wxWidgets
331 // Loads it from the XML produced by gccXML: http://www.gccxml.org
332 // ----------------------------------------------------------------------------
333 class wxXmlGccInterface : public wxXmlInterface
334 {
335 public:
336 wxXmlGccInterface() {}
337
338 bool Parse(const wxString& filename);
339 bool ParseMethod(const wxXmlNode *p,
340 const wxTypeIdHashMap& types,
341 wxMethod& m);
342 };
343
344
345 // ----------------------------------------------------------------------------
346 // Represents the interface of the doxygen headers of wxWidgets
347 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
348 // ----------------------------------------------------------------------------
349 class wxXmlDoxygenInterface : public wxXmlInterface
350 {
351 public:
352 wxXmlDoxygenInterface() {}
353
354 // !!SPEEDUP-TODO!!
355 // Using wxXmlDocument::Load as is, the entire XML file is parsed
356 // and an entire tree of wxXmlNodes is built in memory.
357 // We need however only small portions of the Doxygen-generated XML: to speedup the
358 // processing we could detach the expat callbacks when we detect the beginning of a
359 // node we're not interested about, or just don't create a wxXmlNode for it!
360 // This needs a modification of wxXml API.
361
362 bool Parse(const wxString& filename);
363 bool ParseCompoundDefinition(const wxString& filename);
364 bool ParseMethod(const wxXmlNode*, wxMethod&, wxString& header);
365
366 // this class can take advantage of the preprocessor output to give
367 // a minor number of false positive warnings in the final comparison
368 void AddPreprocessorValue(const wxString& name, const wxString& val)
369 { m_preproc[name]=val; }
370
371 protected:
372 wxStringHashMap m_preproc;
373 };
374
375
376
377 #endif // _XMLPARSER_H_
378