1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Parser of the API/interface XML files
4 // Author: Francesco Montorsi
7 // Copyright: (c) 2008 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
15 #include <wx/txtstrm.h>
16 #include <wx/dynarray.h>
17 #include <wx/xml/xml.h>
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); }
26 // ----------------------------------------------------------------------------
27 // Represents a type with or without const/static/ qualifier
28 // and with or without & and * operators
29 // ----------------------------------------------------------------------------
34 wxType(const wxString
& type
)
35 { SetFromString(type
); }
37 void SetFromString(const wxString
& t
);
38 wxString
GetAsString() const
41 wxString
GetClean() const;
44 { return m_strType
.Contains("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("&"); }
52 bool operator==(const wxType
& m
) const;
53 bool operator!=(const wxType
& m
) const
54 { return !(*this == m
); }
62 extern wxType wxEmptyType
;
63 WX_DECLARE_OBJARRAY(wxType
, wxTypeArray
);
67 // ----------------------------------------------------------------------------
68 // Represents a single prototype of a class' member.
69 // ----------------------------------------------------------------------------
74 { m_bConst
=m_bVirtual
=m_bPureVirtual
=m_bStatic
=m_bDeprecated
=false; m_nLine
=-1; }
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; }
86 //void SetFromString(const wxString& proto);
87 wxString
GetAsString() const;
89 // parser of the prototype:
90 // all these functions return strings with spaces stripped
91 wxType
GetReturnType() const
93 wxString
GetName() const
95 wxTypeArray
GetArgumentTypes() const
97 wxArrayString
GetArgumentDefaults() const
98 { return m_argDefaults
; }
99 int GetLocation() const
104 bool IsStatic() const
105 { return m_bStatic
; }
106 bool IsVirtual() const
107 { return m_bVirtual
; }
108 bool IsPureVirtual() const
109 { return m_bPureVirtual
; }
113 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
115 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
117 bool IsDeprecated() const
118 { return m_bDeprecated
; }
123 void SetReturnType(const wxType
& t
)
125 void SetName(const wxString
& name
)
127 void SetArgumentTypes(const wxTypeArray
& arr
, const wxArrayString
& defaults
);
128 void SetConst(bool c
= true)
130 void SetStatic(bool c
= true)
132 void SetVirtual(bool c
= true)
134 void SetPureVirtual(bool c
= true)
135 { m_bPureVirtual
=c
; }
136 void SetDeprecated(bool c
= true)
138 void SetLocation(int lineNumber
)
139 { m_nLine
=lineNumber
; }
143 bool operator==(const wxMethod
&) const;
144 bool operator!=(const wxMethod
& m
) const
145 { return !(*this == m
); }
147 void Dump(wxTextOutputStream
& stream
) const;
153 wxArrayString m_argDefaults
;
162 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
163 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
166 // ----------------------------------------------------------------------------
167 // Represents a class of the wx API/interface.
168 // ----------------------------------------------------------------------------
173 wxClass(const wxString
& name
, const wxString
& headername
)
174 : m_strName(name
), m_strHeader(headername
) {}
176 void AddMethod(const wxMethod
& func
)
177 { m_methods
.Add(func
); }
179 void SetHeader(const wxString
& header
)
180 { m_strHeader
=header
; }
181 void SetName(const wxString
& name
)
183 wxString
GetName() const
184 { return m_strName
; }
185 wxString
GetHeader() const
186 { return m_strHeader
; }
187 wxString
GetNameWithoutTemplate() const;
190 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
192 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
193 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
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(); }
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;
206 // returns an array of pointers to the overloaded methods with the
208 wxMethodPtrArray
FindMethodNamed(const wxString
& m
) const;
210 // dumps all methods to the given output stream
211 void Dump(wxTextOutputStream
& stream
) const;
214 bool CheckConsistency() const;
218 wxString m_strHeader
;
219 wxMethodArray m_methods
;
222 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
223 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
235 const wxClass
* FindClass(const wxString
& classname
) const
237 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
238 if (m_classes
[i
].GetName() == classname
)
239 return &m_classes
[i
];
243 void Dump(const wxString
& filename
);
245 const wxClassArray
& GetClasses() const
246 { return m_classes
; }
248 unsigned int GetClassesCount() const
249 { return m_classes
.GetCount(); }
251 unsigned int GetMethodCount() const
253 unsigned int methods
= 0;
254 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
255 methods
+= m_classes
[i
].GetMethodCount();
260 { /*wxPrint(".");*/ }
262 bool CheckParseResults() const;
265 wxClassArray m_classes
;
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
,
276 typedef std::basic_string
<char> stlString
;
277 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
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
288 wxXmlGccInterface() {}
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.
298 bool Parse(const wxString
& filename
);
299 bool ParseMethod(const wxXmlNode
*p
,
300 const wxTypeIdHashMap
& types
,
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
312 wxXmlDoxygenInterface() {}
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.
322 bool Parse(const wxString
& filename
);
323 bool ParseCompoundDefinition(const wxString
& filename
);
324 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
329 #endif // _XMLPARSER_H_