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("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); }
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_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
; }
111 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
113 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
115 bool IsDeprecated() const
116 { return m_bDeprecated
; }
121 void SetReturnType(const wxType
& t
)
123 void SetName(const wxString
& name
)
125 void SetArgumentTypes(const wxTypeArray
& arr
, const wxArrayString
& defaults
);
126 void SetConst(bool c
= true)
128 void SetStatic(bool c
= true)
130 void SetVirtual(bool c
= true)
132 void SetDeprecated(bool c
= true)
134 void SetLocation(int lineNumber
)
135 { m_nLine
=lineNumber
; }
139 bool operator==(const wxMethod
&) const;
140 bool operator!=(const wxMethod
& m
) const
141 { return !(*this == m
); }
143 void Dump(wxTextOutputStream
& stream
) const;
149 wxArrayString m_argDefaults
;
157 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
158 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
161 // ----------------------------------------------------------------------------
162 // Represents a class of the wx API/interface.
163 // ----------------------------------------------------------------------------
168 wxClass(const wxString
& name
, const wxString
& headername
)
169 : m_strName(name
), m_strHeader(headername
) {}
171 void AddMethod(const wxMethod
& func
)
172 { m_methods
.Add(func
); }
174 void SetHeader(const wxString
& header
)
175 { m_strHeader
=header
; }
176 void SetName(const wxString
& name
)
178 wxString
GetName() const
179 { return m_strName
; }
180 wxString
GetHeader() const
181 { return m_strHeader
; }
182 wxString
GetNameWithoutTemplate() const;
185 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
187 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
188 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
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(); }
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;
201 // returns an array of pointers to the overloaded methods with the
203 wxMethodPtrArray
FindMethodNamed(const wxString
& m
) const;
205 // dumps all methods to the given output stream
206 void Dump(wxTextOutputStream
& stream
) const;
209 bool CheckConsistency() const;
213 wxString m_strHeader
;
214 wxMethodArray m_methods
;
217 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
218 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
222 // ----------------------------------------------------------------------------
224 // ----------------------------------------------------------------------------
230 const wxClass
* FindClass(const wxString
& classname
) const
232 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
233 if (m_classes
[i
].GetName() == classname
)
234 return &m_classes
[i
];
238 void Dump(const wxString
& filename
);
240 const wxClassArray
& GetClasses() const
241 { return m_classes
; }
243 unsigned int GetClassesCount() const
244 { return m_classes
.GetCount(); }
246 unsigned int GetMethodCount() const
248 unsigned int methods
= 0;
249 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
250 methods
+= m_classes
[i
].GetMethodCount();
255 { /*wxPrint(".");*/ }
257 bool CheckParseResults() const;
260 wxClassArray m_classes
;
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
,
271 typedef std::basic_string
<char> stlString
;
272 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
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
283 wxXmlGccInterface() {}
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.
293 bool Parse(const wxString
& filename
);
294 bool ParseMethod(const wxXmlNode
*p
,
295 const wxTypeIdHashMap
& types
,
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
307 wxXmlDoxygenInterface() {}
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.
317 bool Parse(const wxString
& filename
);
318 bool ParseCompoundDefinition(const wxString
& filename
);
319 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
324 #endif // _XMLPARSER_H_