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)
168 { m_bPureVirtual
=c
; }
169 void SetDeprecated(bool c
= true)
171 void SetLocation(int lineNumber
)
172 { m_nLine
=lineNumber
; }
176 bool operator==(const wxMethod
&) const;
177 bool operator!=(const wxMethod
& m
) const
178 { return !(*this == m
); }
180 void Dump(wxTextOutputStream
& stream
) const;
185 wxArgumentTypeArray m_args
;
194 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
195 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
198 // ----------------------------------------------------------------------------
199 // Represents a class of the wx API/interface.
200 // ----------------------------------------------------------------------------
205 wxClass(const wxString
& name
, const wxString
& headername
)
206 : m_strName(name
), m_strHeader(headername
) {}
208 void AddMethod(const wxMethod
& func
)
209 { m_methods
.Add(func
); }
211 void SetHeader(const wxString
& header
)
212 { m_strHeader
=header
; }
213 void SetName(const wxString
& name
)
215 wxString
GetName() const
216 { return m_strName
; }
217 wxString
GetHeader() const
218 { return m_strHeader
; }
219 wxString
GetNameWithoutTemplate() const;
222 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
224 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
225 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
227 unsigned int GetMethodCount() const
228 { return m_methods
.GetCount(); }
229 wxMethod
& GetMethod(unsigned int n
) const
230 { return m_methods
[n
]; }
231 wxMethod
& GetLastMethod() const
232 { return m_methods
.Last(); }
234 // returns a single result (the first, which is also the only
235 // one if CheckConsistency() return true)
236 const wxMethod
* FindMethod(const wxMethod
& m
) const;
238 // returns an array of pointers to the overloaded methods with the
240 wxMethodPtrArray
FindMethodNamed(const wxString
& m
) const;
242 // dumps all methods to the given output stream
243 void Dump(wxTextOutputStream
& stream
) const;
246 bool CheckConsistency() const;
250 wxString m_strHeader
;
251 wxMethodArray m_methods
;
254 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
255 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
267 const wxClass
* FindClass(const wxString
& classname
) const
269 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
270 if (m_classes
[i
].GetName() == classname
)
271 return &m_classes
[i
];
275 void Dump(const wxString
& filename
);
277 const wxClassArray
& GetClasses() const
278 { return m_classes
; }
280 unsigned int GetClassesCount() const
281 { return m_classes
.GetCount(); }
283 unsigned int GetMethodCount() const
285 unsigned int methods
= 0;
286 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
287 methods
+= m_classes
[i
].GetMethodCount();
292 { /*wxPrint(".");*/ }
294 bool CheckParseResults() const;
297 wxClassArray m_classes
;
301 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
302 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
303 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
304 wxIntegerHash
, wxIntegerEqual
,
308 typedef std::basic_string
<char> stlString
;
309 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
313 // ----------------------------------------------------------------------------
314 // Represents the real interface of wxWidgets
315 // Loads it from the XML produced by gccXML: http://www.gccxml.org
316 // ----------------------------------------------------------------------------
317 class wxXmlGccInterface
: public wxXmlInterface
320 wxXmlGccInterface() {}
323 // Using wxXmlDocument::Load as is, all the types contained in the
324 // the entire gccXML file are stored in memory while parsing;
325 // however we are only interested to wx's own structs/classes/funcs/etc
326 // so that we could use the file IDs to avoid loading stuff which does
327 // not belong to wx. See the very end of the gccXML file: it contains
328 // a set of <File> nodes referenced by all nodes above.
330 bool Parse(const wxString
& filename
);
331 bool ParseMethod(const wxXmlNode
*p
,
332 const wxTypeIdHashMap
& types
,
337 // ----------------------------------------------------------------------------
338 // Represents the interface of the doxygen headers of wxWidgets
339 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
340 // ----------------------------------------------------------------------------
341 class wxXmlDoxygenInterface
: public wxXmlInterface
344 wxXmlDoxygenInterface() {}
347 // Using wxXmlDocument::Load as is, the entire XML file is parsed
348 // and an entire tree of wxXmlNodes is built in memory.
349 // We need however only small portions of the Doxygen-generated XML: to speedup the
350 // processing we could detach the expat callbacks when we detect the beginning of a
351 // node we're not interested about, or just don't create a wxXmlNode for it!
352 // This needs a modification of wxXml API.
354 bool Parse(const wxString
& filename
);
355 bool ParseCompoundDefinition(const wxString
& filename
);
356 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
361 #endif // _XMLPARSER_H_