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 { SetTypeFromString(type
); }
37 void SetTypeFromString(const wxString
& t
);
38 wxString
GetAsString() const
42 { return m_strType
.Contains("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("&"); }
50 bool operator==(const wxType
& m
) const;
51 bool operator!=(const wxType
& m
) const
52 { return !(*this == m
); }
59 // utility for doing comparisons
60 wxString
GetClean() const;
63 extern wxType wxEmptyType
;
64 WX_DECLARE_OBJARRAY(wxType
, wxTypeArray
);
67 // ----------------------------------------------------------------------------
68 // Represents a type used as argument for some wxMethod
69 // ----------------------------------------------------------------------------
70 class wxArgumentType
: public wxType
74 wxArgumentType(const wxString
& type
, const wxString
& defVal
,
75 const wxString
& argName
= wxEmptyString
)
76 { SetTypeFromString(type
); SetDefaultValue(defVal
); m_strArgName
= argName
; }
78 void SetArgumentName(const wxString
& name
)
79 { m_strArgName
=name
.Strip(wxString::both
); }
80 wxString
GetArgumentName() const
81 { return m_strArgName
; }
83 void SetDefaultValue(const wxString
& defval
);
84 wxString
GetDefaultValue() const
85 { return m_strDefaultValue
; }
87 bool HasDefaultValue() const
88 { return !m_strDefaultValue
.IsEmpty(); }
90 bool operator==(const wxArgumentType
& m
) const;
91 bool operator!=(const wxArgumentType
& m
) const
92 { return !(*this == m
); }
95 wxString m_strDefaultValue
;
96 wxString m_strArgName
; // this one only makes sense when this wxType is
97 // used as argument type (and not as return type)
101 extern wxArgumentType wxEmptyArgumentType
;
102 WX_DECLARE_OBJARRAY(wxArgumentType
, wxArgumentTypeArray
);
105 // ----------------------------------------------------------------------------
106 // Represents a single prototype of a class' member.
107 // ----------------------------------------------------------------------------
112 { m_bConst
=m_bVirtual
=m_bPureVirtual
=m_bStatic
=m_bDeprecated
=false; m_nLine
=-1; }
114 wxMethod(const wxType
& rettype
, const wxString
& name
,
115 const wxArgumentTypeArray
& arguments
,
116 bool isConst
, bool isStatic
, bool isVirtual
)
117 : m_retType(rettype
), m_strName(name
.Strip(wxString::both
)),
118 m_bConst(isConst
), m_bStatic(isStatic
), m_bVirtual(isVirtual
)
119 { SetArgumentTypes(arguments
); m_nLine
=-1; }
124 wxString
GetAsString(bool bWithArgumentNames
= true) const;
126 // parser of the prototype:
127 // all these functions return strings with spaces stripped
128 wxType
GetReturnType() const
129 { return m_retType
; }
130 wxString
GetName() const
131 { return m_strName
; }
132 wxArgumentTypeArray
GetArgumentTypes() const
134 int GetLocation() const
139 bool IsStatic() const
140 { return m_bStatic
; }
141 bool IsVirtual() const
142 { return m_bVirtual
; }
143 bool IsPureVirtual() const
144 { return m_bPureVirtual
; }
148 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
150 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
152 bool IsDeprecated() const
153 { return m_bDeprecated
; }
158 void SetReturnType(const wxType
& t
)
160 void SetName(const wxString
& name
)
162 void SetArgumentTypes(const wxArgumentTypeArray
& arr
)
164 void SetConst(bool c
= true)
166 void SetStatic(bool c
= true)
168 void SetVirtual(bool c
= true)
170 void SetPureVirtual(bool c
= true)
173 if (c
) m_bVirtual
=c
; // pure virtual => virtual
175 void SetDeprecated(bool c
= true)
177 void SetLocation(int lineNumber
)
178 { m_nLine
=lineNumber
; }
182 bool operator==(const wxMethod
&) const;
183 bool operator!=(const wxMethod
& m
) const
184 { return !(*this == m
); }
186 void Dump(wxTextOutputStream
& stream
) const;
191 wxArgumentTypeArray m_args
;
200 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
201 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
204 // ----------------------------------------------------------------------------
205 // Represents a class of the wx API/interface.
206 // ----------------------------------------------------------------------------
211 wxClass(const wxString
& name
, const wxString
& headername
)
212 : m_strName(name
), m_strHeader(headername
) {}
214 void AddMethod(const wxMethod
& func
)
215 { m_methods
.Add(func
); }
217 void SetHeader(const wxString
& header
)
218 { m_strHeader
=header
; }
219 void SetName(const wxString
& name
)
221 wxString
GetName() const
222 { return m_strName
; }
223 wxString
GetHeader() const
224 { return m_strHeader
; }
225 wxString
GetNameWithoutTemplate() const;
228 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
230 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
231 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
233 unsigned int GetMethodCount() const
234 { return m_methods
.GetCount(); }
235 wxMethod
& GetMethod(unsigned int n
) const
236 { return m_methods
[n
]; }
237 wxMethod
& GetLastMethod() const
238 { return m_methods
.Last(); }
240 // returns a single result (the first, which is also the only
241 // one if CheckConsistency() return true)
242 const wxMethod
* FindMethod(const wxMethod
& m
) const;
244 // returns an array of pointers to the overloaded methods with the
246 wxMethodPtrArray
FindMethodNamed(const wxString
& m
) const;
248 // dumps all methods to the given output stream
249 void Dump(wxTextOutputStream
& stream
) const;
252 bool CheckConsistency() const;
256 wxString m_strHeader
;
257 wxMethodArray m_methods
;
260 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
261 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
273 const wxClass
* FindClass(const wxString
& classname
) const
275 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
276 if (m_classes
[i
].GetName() == classname
)
277 return &m_classes
[i
];
281 void Dump(const wxString
& filename
);
283 const wxClassArray
& GetClasses() const
284 { return m_classes
; }
286 unsigned int GetClassesCount() const
287 { return m_classes
.GetCount(); }
289 unsigned int GetMethodCount() const
291 unsigned int methods
= 0;
292 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
293 methods
+= m_classes
[i
].GetMethodCount();
297 // pass a full-path header filename:
298 wxClassPtrArray
FindClassesDefinedIn(const wxString
& headerfile
) const;
301 { /*wxPrint(".");*/ }
303 bool CheckParseResults() const;
306 wxClassArray m_classes
;
310 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
311 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
312 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
313 wxIntegerHash
, wxIntegerEqual
,
317 typedef std::basic_string
<char> stlString
;
318 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
322 // ----------------------------------------------------------------------------
323 // Represents the real interface of wxWidgets
324 // Loads it from the XML produced by gccXML: http://www.gccxml.org
325 // ----------------------------------------------------------------------------
326 class wxXmlGccInterface
: public wxXmlInterface
329 wxXmlGccInterface() {}
332 // Using wxXmlDocument::Load as is, all the types contained in the
333 // the entire gccXML file are stored in memory while parsing;
334 // however we are only interested to wx's own structs/classes/funcs/etc
335 // so that we could use the file IDs to avoid loading stuff which does
336 // not belong to wx. See the very end of the gccXML file: it contains
337 // a set of <File> nodes referenced by all nodes above.
339 bool Parse(const wxString
& filename
);
340 bool ParseMethod(const wxXmlNode
*p
,
341 const wxTypeIdHashMap
& types
,
346 // ----------------------------------------------------------------------------
347 // Represents the interface of the doxygen headers of wxWidgets
348 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
349 // ----------------------------------------------------------------------------
350 class wxXmlDoxygenInterface
: public wxXmlInterface
353 wxXmlDoxygenInterface() {}
356 // Using wxXmlDocument::Load as is, the entire XML file is parsed
357 // and an entire tree of wxXmlNodes is built in memory.
358 // We need however only small portions of the Doxygen-generated XML: to speedup the
359 // processing we could detach the expat callbacks when we detect the beginning of a
360 // node we're not interested about, or just don't create a wxXmlNode for it!
361 // This needs a modification of wxXml API.
363 bool Parse(const wxString
& filename
);
364 bool ParseCompoundDefinition(const wxString
& filename
);
365 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
370 #endif // _XMLPARSER_H_