]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.h
fix the automatic modify mode
[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("WARNING: " 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_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
109 bool IsOk() const;
110 bool IsCtor() const
111 { return m_retType==wxEmptyType && !m_strName.StartsWith("~"); }
112 bool IsDtor() const
113 { return m_retType==wxEmptyType && m_strName.StartsWith("~"); }
114
115 bool IsDeprecated() const
116 { return m_bDeprecated; }
117
118
119 public: // setters
120
121 void SetReturnType(const wxType& t)
122 { m_retType=t; }
123 void SetName(const wxString& name)
124 { m_strName=name; }
125 void SetArgumentTypes(const wxTypeArray& arr, const wxArrayString& defaults);
126 void SetConst(bool c = true)
127 { m_bConst=c; }
128 void SetStatic(bool c = true)
129 { m_bStatic=c; }
130 void SetVirtual(bool c = true)
131 { m_bVirtual=c; }
132 void SetDeprecated(bool c = true)
133 { m_bDeprecated=c; }
134 void SetLocation(int lineNumber)
135 { m_nLine=lineNumber; }
136
137 public: // misc
138
139 bool operator==(const wxMethod&) const;
140 bool operator!=(const wxMethod& m) const
141 { return !(*this == m); }
142
143 void Dump(wxTextOutputStream& stream) const;
144
145 protected:
146 wxType m_retType;
147 wxString m_strName;
148 wxTypeArray m_args;
149 wxArrayString m_argDefaults;
150 bool m_bConst;
151 bool m_bStatic;
152 bool m_bVirtual;
153 bool m_bDeprecated;
154 int m_nLine;
155 };
156
157 WX_DECLARE_OBJARRAY(wxMethod, wxMethodArray);
158 WX_DEFINE_ARRAY(const wxMethod*, wxMethodPtrArray);
159
160
161 // ----------------------------------------------------------------------------
162 // Represents a class of the wx API/interface.
163 // ----------------------------------------------------------------------------
164 class wxClass
165 {
166 public:
167 wxClass() {}
168 wxClass(const wxString& name, const wxString& headername)
169 : m_strName(name), m_strHeader(headername) {}
170
171 void AddMethod(const wxMethod& func)
172 { m_methods.Add(func); }
173
174 void SetHeader(const wxString& header)
175 { m_strHeader=header; }
176 void SetName(const wxString& name)
177 { m_strName=name; }
178 wxString GetName() const
179 { return m_strName; }
180 wxString GetHeader() const
181 { return m_strHeader; }
182 wxString GetNameWithoutTemplate() const;
183
184 bool IsOk() const
185 { return !m_strName.IsEmpty() && !m_methods.IsEmpty(); }
186
187 bool IsValidCtorForThisClass(const wxMethod& m) const;
188 bool IsValidDtorForThisClass(const wxMethod& m) const;
189
190 unsigned int GetMethodCount() const
191 { return m_methods.GetCount(); }
192 wxMethod& GetMethod(unsigned int n) const
193 { return m_methods[n]; }
194 wxMethod& GetLastMethod() const
195 { return m_methods.Last(); }
196
197 // returns a single result (the first, which is also the only
198 // one if CheckConsistency() return true)
199 const wxMethod* FindMethod(const wxMethod& m) const;
200
201 // returns an array of pointers to the overloaded methods with the
202 // same given name
203 wxMethodPtrArray FindMethodNamed(const wxString& m) const;
204
205 // dumps all methods to the given output stream
206 void Dump(wxTextOutputStream& stream) const;
207
208 // slow check
209 bool CheckConsistency() const;
210
211 protected:
212 wxString m_strName;
213 wxString m_strHeader;
214 wxMethodArray m_methods;
215 };
216
217 WX_DECLARE_OBJARRAY(wxClass, wxClassArray);
218 WX_DEFINE_ARRAY(const wxClass*, wxClassPtrArray);
219
220
221
222 // ----------------------------------------------------------------------------
223 // wxXmlInterface
224 // ----------------------------------------------------------------------------
225 class wxXmlInterface
226 {
227 public:
228 wxXmlInterface() {}
229
230 const wxClass* FindClass(const wxString& classname) const
231 {
232 for (unsigned int i=0; i<m_classes.GetCount(); i++)
233 if (m_classes[i].GetName() == classname)
234 return &m_classes[i];
235 return NULL;
236 }
237
238 void Dump(const wxString& filename);
239
240 const wxClassArray& GetClasses() const
241 { return m_classes; }
242
243 unsigned int GetClassesCount() const
244 { return m_classes.GetCount(); }
245
246 unsigned int GetMethodCount() const
247 {
248 unsigned int methods = 0;
249 for (unsigned i=0; i < m_classes.GetCount(); i++)
250 methods += m_classes[i].GetMethodCount();
251 return methods;
252 }
253
254 void ShowProgress()
255 { /*wxPrint(".");*/ }
256
257 bool CheckParseResults() const;
258
259 protected:
260 wxClassArray m_classes;
261 };
262
263 #if 1
264 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
265 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
266 WX_DECLARE_HASH_MAP( unsigned long, wxString,
267 wxIntegerHash, wxIntegerEqual,
268 wxTypeIdHashMap );
269 #else
270 #include <map>
271 typedef std::basic_string<char> stlString;
272 typedef std::map<unsigned long, stlString> wxTypeIdHashMap;
273 #endif
274
275
276 // ----------------------------------------------------------------------------
277 // Represents the real interface of wxWidgets
278 // Loads it from the XML produced by gccXML: http://www.gccxml.org
279 // ----------------------------------------------------------------------------
280 class wxXmlGccInterface : public wxXmlInterface
281 {
282 public:
283 wxXmlGccInterface() {}
284
285 // !!SPEEDUP-TODO!!
286 // Using wxXmlDocument::Load as is, all the types contained in the
287 // the entire gccXML file are stored in memory while parsing;
288 // however we are only interested to wx's own structs/classes/funcs/etc
289 // so that we could use the file IDs to avoid loading stuff which does
290 // not belong to wx. See the very end of the gccXML file: it contains
291 // a set of <File> nodes referenced by all nodes above.
292
293 bool Parse(const wxString& filename);
294 bool ParseMethod(const wxXmlNode *p,
295 const wxTypeIdHashMap& types,
296 wxMethod& m);
297 };
298
299
300 // ----------------------------------------------------------------------------
301 // Represents the interface of the doxygen headers of wxWidgets
302 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
303 // ----------------------------------------------------------------------------
304 class wxXmlDoxygenInterface : public wxXmlInterface
305 {
306 public:
307 wxXmlDoxygenInterface() {}
308
309 // !!SPEEDUP-TODO!!
310 // Using wxXmlDocument::Load as is, the entire XML file is parsed
311 // and an entire tree of wxXmlNodes is built in memory.
312 // We need however only small portions of the Doxygen-generated XML: to speedup the
313 // processing we could detach the expat callbacks when we detect the beginning of a
314 // node we're not interested about, or just don't create a wxXmlNode for it!
315 // This needs a modification of wxXml API.
316
317 bool Parse(const wxString& filename);
318 bool ParseCompoundDefinition(const wxString& filename);
319 bool ParseMethod(const wxXmlNode*, wxMethod&, wxString& header);
320 };
321
322
323
324 #endif // _XMLPARSER_H_
325