]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.h
add pure virtual function parsing; give more informative warnings; fix the initial...
[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 { SetFromString(type); }
36
37 void SetFromString(const wxString& t);
38 wxString GetAsString() const
39 { return m_strType; }
40
41 wxString GetClean() const;
42
43 bool IsConst() const
44 { return m_strType.Contains("const"); }
45 bool IsStatic() const
46 { return m_strType.Contains("static"); }
47 bool IsPointer() const
48 { return m_strType.Contains("*"); }
49 bool IsReference() const
50 { return m_strType.Contains("&"); }
51
52 bool operator==(const wxType& m) const;
53 bool operator!=(const wxType& m) const
54 { return !(*this == m); }
55
56 bool IsOk() const;
57
58 protected:
59 wxString m_strType;
60 };
61
62 extern wxType wxEmptyType;
63 WX_DECLARE_OBJARRAY(wxType, wxTypeArray);
64
65
66
67 // ----------------------------------------------------------------------------
68 // Represents a single prototype of a class' member.
69 // ----------------------------------------------------------------------------
70 class wxMethod
71 {
72 public:
73 wxMethod()
74 { m_bConst=m_bVirtual=m_bPureVirtual=m_bStatic=m_bDeprecated=false; m_nLine=-1; }
75
76 wxMethod(const wxType& rettype, const wxString& name,
77 const wxTypeArray& arguments, const wxArrayString& defaults,
78 bool isConst, bool isStatic, bool isVirtual)
79 : m_retType(rettype), m_strName(name.Strip(wxString::both)),
80 m_bConst(isConst), m_bStatic(isStatic), m_bVirtual(isVirtual)
81 { SetArgumentTypes(arguments,defaults); m_nLine=-1; }
82
83
84 public: // getters
85
86 //void SetFromString(const wxString& proto);
87 wxString GetAsString() const;
88
89 // parser of the prototype:
90 // all these functions return strings with spaces stripped
91 wxType GetReturnType() const
92 { return m_retType; }
93 wxString GetName() const
94 { return m_strName; }
95 wxTypeArray GetArgumentTypes() const
96 { return m_args; }
97 wxArrayString GetArgumentDefaults() const
98 { return m_argDefaults; }
99 int GetLocation() const
100 { return m_nLine; }
101
102 bool IsConst() const
103 { return m_bConst; }
104 bool IsStatic() const
105 { return m_bStatic; }
106 bool IsVirtual() const
107 { return m_bVirtual; }
108 bool IsPureVirtual() const
109 { return m_bPureVirtual; }
110
111 bool IsOk() const;
112 bool IsCtor() const
113 { return m_retType==wxEmptyType && !m_strName.StartsWith("~"); }
114 bool IsDtor() const
115 { return m_retType==wxEmptyType && m_strName.StartsWith("~"); }
116
117 bool IsDeprecated() const
118 { return m_bDeprecated; }
119
120
121 public: // setters
122
123 void SetReturnType(const wxType& t)
124 { m_retType=t; }
125 void SetName(const wxString& name)
126 { m_strName=name; }
127 void SetArgumentTypes(const wxTypeArray& arr, const wxArrayString& defaults);
128 void SetConst(bool c = true)
129 { m_bConst=c; }
130 void SetStatic(bool c = true)
131 { m_bStatic=c; }
132 void SetVirtual(bool c = true)
133 { m_bVirtual=c; }
134 void SetPureVirtual(bool c = true)
135 { m_bPureVirtual=c; }
136 void SetDeprecated(bool c = true)
137 { m_bDeprecated=c; }
138 void SetLocation(int lineNumber)
139 { m_nLine=lineNumber; }
140
141 public: // misc
142
143 bool operator==(const wxMethod&) const;
144 bool operator!=(const wxMethod& m) const
145 { return !(*this == m); }
146
147 void Dump(wxTextOutputStream& stream) const;
148
149 protected:
150 wxType m_retType;
151 wxString m_strName;
152 wxTypeArray m_args;
153 wxArrayString m_argDefaults;
154 bool m_bConst;
155 bool m_bStatic;
156 bool m_bVirtual;
157 bool m_bPureVirtual;
158 bool m_bDeprecated;
159 int m_nLine;
160 };
161
162 WX_DECLARE_OBJARRAY(wxMethod, wxMethodArray);
163 WX_DEFINE_ARRAY(const wxMethod*, wxMethodPtrArray);
164
165
166 // ----------------------------------------------------------------------------
167 // Represents a class of the wx API/interface.
168 // ----------------------------------------------------------------------------
169 class wxClass
170 {
171 public:
172 wxClass() {}
173 wxClass(const wxString& name, const wxString& headername)
174 : m_strName(name), m_strHeader(headername) {}
175
176 void AddMethod(const wxMethod& func)
177 { m_methods.Add(func); }
178
179 void SetHeader(const wxString& header)
180 { m_strHeader=header; }
181 void SetName(const wxString& name)
182 { m_strName=name; }
183 wxString GetName() const
184 { return m_strName; }
185 wxString GetHeader() const
186 { return m_strHeader; }
187 wxString GetNameWithoutTemplate() const;
188
189 bool IsOk() const
190 { return !m_strName.IsEmpty() && !m_methods.IsEmpty(); }
191
192 bool IsValidCtorForThisClass(const wxMethod& m) const;
193 bool IsValidDtorForThisClass(const wxMethod& m) const;
194
195 unsigned int GetMethodCount() const
196 { return m_methods.GetCount(); }
197 wxMethod& GetMethod(unsigned int n) const
198 { return m_methods[n]; }
199 wxMethod& GetLastMethod() const
200 { return m_methods.Last(); }
201
202 // returns a single result (the first, which is also the only
203 // one if CheckConsistency() return true)
204 const wxMethod* FindMethod(const wxMethod& m) const;
205
206 // returns an array of pointers to the overloaded methods with the
207 // same given name
208 wxMethodPtrArray FindMethodNamed(const wxString& m) const;
209
210 // dumps all methods to the given output stream
211 void Dump(wxTextOutputStream& stream) const;
212
213 // slow check
214 bool CheckConsistency() const;
215
216 protected:
217 wxString m_strName;
218 wxString m_strHeader;
219 wxMethodArray m_methods;
220 };
221
222 WX_DECLARE_OBJARRAY(wxClass, wxClassArray);
223 WX_DEFINE_ARRAY(const wxClass*, wxClassPtrArray);
224
225
226
227 // ----------------------------------------------------------------------------
228 // wxXmlInterface
229 // ----------------------------------------------------------------------------
230 class wxXmlInterface
231 {
232 public:
233 wxXmlInterface() {}
234
235 const wxClass* FindClass(const wxString& classname) const
236 {
237 for (unsigned int i=0; i<m_classes.GetCount(); i++)
238 if (m_classes[i].GetName() == classname)
239 return &m_classes[i];
240 return NULL;
241 }
242
243 void Dump(const wxString& filename);
244
245 const wxClassArray& GetClasses() const
246 { return m_classes; }
247
248 unsigned int GetClassesCount() const
249 { return m_classes.GetCount(); }
250
251 unsigned int GetMethodCount() const
252 {
253 unsigned int methods = 0;
254 for (unsigned i=0; i < m_classes.GetCount(); i++)
255 methods += m_classes[i].GetMethodCount();
256 return methods;
257 }
258
259 void ShowProgress()
260 { /*wxPrint(".");*/ }
261
262 bool CheckParseResults() const;
263
264 protected:
265 wxClassArray m_classes;
266 };
267
268 #if 1
269 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
270 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
271 WX_DECLARE_HASH_MAP( unsigned long, wxString,
272 wxIntegerHash, wxIntegerEqual,
273 wxTypeIdHashMap );
274 #else
275 #include <map>
276 typedef std::basic_string<char> stlString;
277 typedef std::map<unsigned long, stlString> wxTypeIdHashMap;
278 #endif
279
280
281 // ----------------------------------------------------------------------------
282 // Represents the real interface of wxWidgets
283 // Loads it from the XML produced by gccXML: http://www.gccxml.org
284 // ----------------------------------------------------------------------------
285 class wxXmlGccInterface : public wxXmlInterface
286 {
287 public:
288 wxXmlGccInterface() {}
289
290 // !!SPEEDUP-TODO!!
291 // Using wxXmlDocument::Load as is, all the types contained in the
292 // the entire gccXML file are stored in memory while parsing;
293 // however we are only interested to wx's own structs/classes/funcs/etc
294 // so that we could use the file IDs to avoid loading stuff which does
295 // not belong to wx. See the very end of the gccXML file: it contains
296 // a set of <File> nodes referenced by all nodes above.
297
298 bool Parse(const wxString& filename);
299 bool ParseMethod(const wxXmlNode *p,
300 const wxTypeIdHashMap& types,
301 wxMethod& m);
302 };
303
304
305 // ----------------------------------------------------------------------------
306 // Represents the interface of the doxygen headers of wxWidgets
307 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
308 // ----------------------------------------------------------------------------
309 class wxXmlDoxygenInterface : public wxXmlInterface
310 {
311 public:
312 wxXmlDoxygenInterface() {}
313
314 // !!SPEEDUP-TODO!!
315 // Using wxXmlDocument::Load as is, the entire XML file is parsed
316 // and an entire tree of wxXmlNodes is built in memory.
317 // We need however only small portions of the Doxygen-generated XML: to speedup the
318 // processing we could detach the expat callbacks when we detect the beginning of a
319 // node we're not interested about, or just don't create a wxXmlNode for it!
320 // This needs a modification of wxXml API.
321
322 bool Parse(const wxString& filename);
323 bool ParseCompoundDefinition(const wxString& filename);
324 bool ParseMethod(const wxXmlNode*, wxMethod&, wxString& header);
325 };
326
327
328
329 #endif // _XMLPARSER_H_
330