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 operator==(const wxArgumentType
& m
) const;
88 bool operator!=(const wxArgumentType
& m
) const
89 { return !(*this == m
); }
92 wxString m_strDefaultValue
;
93 wxString m_strArgName
; // this one only makes sense when this wxType is
94 // used as argument type (and not as return type)
98 extern wxArgumentType wxEmptyArgumentType
;
99 WX_DECLARE_OBJARRAY(wxArgumentType
, wxArgumentTypeArray
);
102 // ----------------------------------------------------------------------------
103 // Represents a single prototype of a class' member.
104 // ----------------------------------------------------------------------------
109 { m_bConst
=m_bVirtual
=m_bPureVirtual
=m_bStatic
=m_bDeprecated
=false; m_nLine
=-1; }
111 wxMethod(const wxType
& rettype
, const wxString
& name
,
112 const wxArgumentTypeArray
& arguments
,
113 bool isConst
, bool isStatic
, bool isVirtual
)
114 : m_retType(rettype
), m_strName(name
.Strip(wxString::both
)),
115 m_bConst(isConst
), m_bStatic(isStatic
), m_bVirtual(isVirtual
)
116 { SetArgumentTypes(arguments
); m_nLine
=-1; }
121 wxString
GetAsString(bool bWithArgumentNames
= true) const;
123 // parser of the prototype:
124 // all these functions return strings with spaces stripped
125 wxType
GetReturnType() const
126 { return m_retType
; }
127 wxString
GetName() const
128 { return m_strName
; }
129 wxArgumentTypeArray
GetArgumentTypes() const
131 int GetLocation() const
136 bool IsStatic() const
137 { return m_bStatic
; }
138 bool IsVirtual() const
139 { return m_bVirtual
; }
140 bool IsPureVirtual() const
141 { return m_bPureVirtual
; }
145 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
147 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
149 bool IsDeprecated() const
150 { return m_bDeprecated
; }
155 void SetReturnType(const wxType
& t
)
157 void SetName(const wxString
& name
)
159 void SetArgumentTypes(const wxArgumentTypeArray
& arr
)
161 void SetConst(bool c
= true)
163 void SetStatic(bool c
= true)
165 void SetVirtual(bool c
= true)
167 void SetPureVirtual(bool c
= true)
170 if (c
) m_bVirtual
=c
; // pure virtual => virtual
172 void SetDeprecated(bool c
= true)
174 void SetLocation(int lineNumber
)
175 { m_nLine
=lineNumber
; }
179 bool operator==(const wxMethod
&) const;
180 bool operator!=(const wxMethod
& m
) const
181 { return !(*this == m
); }
183 void Dump(wxTextOutputStream
& stream
) const;
188 wxArgumentTypeArray m_args
;
197 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
198 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
201 // ----------------------------------------------------------------------------
202 // Represents a class of the wx API/interface.
203 // ----------------------------------------------------------------------------
208 wxClass(const wxString
& name
, const wxString
& headername
)
209 : m_strName(name
), m_strHeader(headername
) {}
211 void AddMethod(const wxMethod
& func
)
212 { m_methods
.Add(func
); }
214 void SetHeader(const wxString
& header
)
215 { m_strHeader
=header
; }
216 void SetName(const wxString
& name
)
218 wxString
GetName() const
219 { return m_strName
; }
220 wxString
GetHeader() const
221 { return m_strHeader
; }
222 wxString
GetNameWithoutTemplate() const;
225 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
227 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
228 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
230 unsigned int GetMethodCount() const
231 { return m_methods
.GetCount(); }
232 wxMethod
& GetMethod(unsigned int n
) const
233 { return m_methods
[n
]; }
234 wxMethod
& GetLastMethod() const
235 { return m_methods
.Last(); }
237 // returns a single result (the first, which is also the only
238 // one if CheckConsistency() return true)
239 const wxMethod
* FindMethod(const wxMethod
& m
) const;
241 // returns an array of pointers to the overloaded methods with the
243 wxMethodPtrArray
FindMethodNamed(const wxString
& m
) const;
245 // dumps all methods to the given output stream
246 void Dump(wxTextOutputStream
& stream
) const;
249 bool CheckConsistency() const;
253 wxString m_strHeader
;
254 wxMethodArray m_methods
;
257 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
258 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
270 const wxClass
* FindClass(const wxString
& classname
) const
272 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
273 if (m_classes
[i
].GetName() == classname
)
274 return &m_classes
[i
];
278 void Dump(const wxString
& filename
);
280 const wxClassArray
& GetClasses() const
281 { return m_classes
; }
283 unsigned int GetClassesCount() const
284 { return m_classes
.GetCount(); }
286 unsigned int GetMethodCount() const
288 unsigned int methods
= 0;
289 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
290 methods
+= m_classes
[i
].GetMethodCount();
295 { /*wxPrint(".");*/ }
297 bool CheckParseResults() const;
300 wxClassArray m_classes
;
304 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
305 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
306 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
307 wxIntegerHash
, wxIntegerEqual
,
311 typedef std::basic_string
<char> stlString
;
312 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
316 // ----------------------------------------------------------------------------
317 // Represents the real interface of wxWidgets
318 // Loads it from the XML produced by gccXML: http://www.gccxml.org
319 // ----------------------------------------------------------------------------
320 class wxXmlGccInterface
: public wxXmlInterface
323 wxXmlGccInterface() {}
326 // Using wxXmlDocument::Load as is, all the types contained in the
327 // the entire gccXML file are stored in memory while parsing;
328 // however we are only interested to wx's own structs/classes/funcs/etc
329 // so that we could use the file IDs to avoid loading stuff which does
330 // not belong to wx. See the very end of the gccXML file: it contains
331 // a set of <File> nodes referenced by all nodes above.
333 bool Parse(const wxString
& filename
);
334 bool ParseMethod(const wxXmlNode
*p
,
335 const wxTypeIdHashMap
& types
,
340 // ----------------------------------------------------------------------------
341 // Represents the interface of the doxygen headers of wxWidgets
342 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
343 // ----------------------------------------------------------------------------
344 class wxXmlDoxygenInterface
: public wxXmlInterface
347 wxXmlDoxygenInterface() {}
350 // Using wxXmlDocument::Load as is, the entire XML file is parsed
351 // and an entire tree of wxXmlNodes is built in memory.
352 // We need however only small portions of the Doxygen-generated XML: to speedup the
353 // processing we could detach the expat callbacks when we detect the beginning of a
354 // node we're not interested about, or just don't create a wxXmlNode for it!
355 // This needs a modification of wxXml API.
357 bool Parse(const wxString
& filename
);
358 bool ParseCompoundDefinition(const wxString
& filename
);
359 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
364 #endif // _XMLPARSER_H_