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
, const wxString
& defvalForCmp
= wxEmptyString
);
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
;
97 // this string may differ from m_strDefaultValue if there were
98 // preprocessor substitutions; can be wxEmptyString.
99 wxString m_strDefaultValueForCmp
;
101 wxString m_strArgName
; // this one only makes sense when this wxType is
102 // used as argument type (and not as return type)
106 extern wxArgumentType wxEmptyArgumentType
;
107 WX_DECLARE_OBJARRAY(wxArgumentType
, wxArgumentTypeArray
);
110 // ----------------------------------------------------------------------------
111 // Represents a single prototype of a class' member.
112 // ----------------------------------------------------------------------------
117 { m_bConst
=m_bVirtual
=m_bPureVirtual
=m_bStatic
=m_bDeprecated
=false; m_nLine
=-1; }
119 wxMethod(const wxType
& rettype
, const wxString
& name
,
120 const wxArgumentTypeArray
& arguments
,
121 bool isConst
, bool isStatic
, bool isVirtual
)
122 : m_retType(rettype
), m_strName(name
.Strip(wxString::both
)),
123 m_bConst(isConst
), m_bStatic(isStatic
), m_bVirtual(isVirtual
)
124 { SetArgumentTypes(arguments
); m_nLine
=-1; }
129 wxString
GetAsString(bool bWithArgumentNames
= true) const;
131 // parser of the prototype:
132 // all these functions return strings with spaces stripped
133 wxType
GetReturnType() const
134 { return m_retType
; }
135 wxString
GetName() const
136 { return m_strName
; }
137 wxArgumentTypeArray
GetArgumentTypes() const
139 int GetLocation() const
144 bool IsStatic() const
145 { return m_bStatic
; }
146 bool IsVirtual() const
147 { return m_bVirtual
; }
148 bool IsPureVirtual() const
149 { return m_bPureVirtual
; }
153 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
155 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
157 bool IsDeprecated() const
158 { return m_bDeprecated
; }
163 void SetReturnType(const wxType
& t
)
165 void SetName(const wxString
& name
)
167 void SetArgumentTypes(const wxArgumentTypeArray
& arr
)
169 void SetConst(bool c
= true)
171 void SetStatic(bool c
= true)
173 void SetVirtual(bool c
= true)
175 void SetPureVirtual(bool c
= true)
178 if (c
) m_bVirtual
=c
; // pure virtual => virtual
180 void SetDeprecated(bool c
= true)
182 void SetLocation(int lineNumber
)
183 { m_nLine
=lineNumber
; }
187 bool operator==(const wxMethod
&) const;
188 bool operator!=(const wxMethod
& m
) const
189 { return !(*this == m
); }
191 void Dump(wxTextOutputStream
& stream
) const;
196 wxArgumentTypeArray m_args
;
205 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
206 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
209 // ----------------------------------------------------------------------------
210 // Represents a class of the wx API/interface.
211 // ----------------------------------------------------------------------------
216 wxClass(const wxString
& name
, const wxString
& headername
)
217 : m_strName(name
), m_strHeader(headername
) {}
219 void AddMethod(const wxMethod
& func
)
220 { m_methods
.Add(func
); }
222 void SetHeader(const wxString
& header
)
223 { m_strHeader
=header
; }
224 void SetName(const wxString
& name
)
226 wxString
GetName() const
227 { return m_strName
; }
228 wxString
GetHeader() const
229 { return m_strHeader
; }
230 wxString
GetNameWithoutTemplate() const;
233 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
235 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
236 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
238 unsigned int GetMethodCount() const
239 { return m_methods
.GetCount(); }
240 wxMethod
& GetMethod(unsigned int n
) const
241 { return m_methods
[n
]; }
242 wxMethod
& GetLastMethod() const
243 { return m_methods
.Last(); }
245 // returns a single result (the first, which is also the only
246 // one if CheckConsistency() return true)
247 const wxMethod
* FindMethod(const wxMethod
& m
) const;
249 // returns an array of pointers to the overloaded methods with the
251 wxMethodPtrArray
FindMethodNamed(const wxString
& m
) const;
253 // dumps all methods to the given output stream
254 void Dump(wxTextOutputStream
& stream
) const;
257 bool CheckConsistency() const;
261 wxString m_strHeader
;
262 wxMethodArray m_methods
;
265 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
266 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
278 const wxClass
* FindClass(const wxString
& classname
) const
280 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
281 if (m_classes
[i
].GetName() == classname
)
282 return &m_classes
[i
];
286 void Dump(const wxString
& filename
);
288 const wxClassArray
& GetClasses() const
289 { return m_classes
; }
291 unsigned int GetClassesCount() const
292 { return m_classes
.GetCount(); }
294 unsigned int GetMethodCount() const
296 unsigned int methods
= 0;
297 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
298 methods
+= m_classes
[i
].GetMethodCount();
302 // pass a full-path header filename:
303 wxClassPtrArray
FindClassesDefinedIn(const wxString
& headerfile
) const;
306 { /*wxPrint(".");*/ }
308 bool CheckParseResults() const;
311 wxClassArray m_classes
;
315 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
316 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
317 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
318 wxIntegerHash
, wxIntegerEqual
,
321 WX_DECLARE_STRING_HASH_MAP( wxString
, wxStringHashMap
);
324 typedef std::basic_string
<char> stlString
;
325 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
329 // ----------------------------------------------------------------------------
330 // Represents the real interface of wxWidgets
331 // Loads it from the XML produced by gccXML: http://www.gccxml.org
332 // ----------------------------------------------------------------------------
333 class wxXmlGccInterface
: public wxXmlInterface
336 wxXmlGccInterface() {}
338 bool Parse(const wxString
& filename
);
339 bool ParseMethod(const wxXmlNode
*p
,
340 const wxTypeIdHashMap
& types
,
345 // ----------------------------------------------------------------------------
346 // Represents the interface of the doxygen headers of wxWidgets
347 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
348 // ----------------------------------------------------------------------------
349 class wxXmlDoxygenInterface
: public wxXmlInterface
352 wxXmlDoxygenInterface() {}
355 // Using wxXmlDocument::Load as is, the entire XML file is parsed
356 // and an entire tree of wxXmlNodes is built in memory.
357 // We need however only small portions of the Doxygen-generated XML: to speedup the
358 // processing we could detach the expat callbacks when we detect the beginning of a
359 // node we're not interested about, or just don't create a wxXmlNode for it!
360 // This needs a modification of wxXml API.
362 bool Parse(const wxString
& filename
);
363 bool ParseCompoundDefinition(const wxString
& filename
);
364 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
366 // this class can take advantage of the preprocessor output to give
367 // a minor number of false positive warnings in the final comparison
368 void AddPreprocessorValue(const wxString
& name
, const wxString
& val
)
369 { m_preproc
[name
]=val
; }
372 wxStringHashMap m_preproc
;
377 #endif // _XMLPARSER_H_