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>
18 #include <wx/platinfo.h>
21 #define LogMessage(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); }
22 #define LogWarning(fmt, ...) { wxPrintf(fmt "\n", __VA_ARGS__); fflush(stdout); }
23 #define LogError(fmt, ...) { wxPrintf("ERROR: " fmt "\n", __VA_ARGS__); fflush(stdout); }
24 #define wxPrint(str) { wxPrintf(str); fflush(stdout); }
27 // ----------------------------------------------------------------------------
28 // Represents a type with or without const/static/ qualifier
29 // and with or without & and * operators
30 // ----------------------------------------------------------------------------
35 wxType(const wxString
& type
)
36 { SetTypeFromString(type
); }
38 void SetTypeFromString(const wxString
& t
);
39 wxString
GetAsString() const
43 { return m_strType
.Contains("const"); }
45 { return m_strType
.Contains("static"); }
46 bool IsPointer() const
47 { return m_strType
.Contains("*"); }
48 bool IsReference() const
49 { return m_strType
.Contains("&"); }
51 bool operator==(const wxType
& m
) const;
52 bool operator!=(const wxType
& m
) const
53 { return !(*this == m
); }
59 m_strTypeClean
; // m_strType "cleaned" of its attributes
60 // (only for internal use)
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;
118 m_nLine
=-1; m_nAvailability
=wxPORT_UNKNOWN
; }
120 wxMethod(const wxType
& rettype
, const wxString
& name
,
121 const wxArgumentTypeArray
& arguments
,
122 bool isConst
, bool isStatic
, bool isVirtual
)
123 : m_retType(rettype
), m_strName(name
.Strip(wxString::both
)),
124 m_bConst(isConst
), m_bStatic(isStatic
), m_bVirtual(isVirtual
)
125 { SetArgumentTypes(arguments
); m_nLine
=-1; }
130 wxString
GetAsString(bool bWithArgumentNames
= true) const;
132 // parser of the prototype:
133 // all these functions return strings with spaces stripped
134 wxType
GetReturnType() const
135 { return m_retType
; }
136 wxString
GetName() const
137 { return m_strName
; }
138 wxArgumentTypeArray
GetArgumentTypes() const
140 int GetLocation() const
142 int GetAvailability() const
143 { return m_nAvailability
; }
147 bool IsStatic() const
148 { return m_bStatic
; }
149 bool IsVirtual() const
150 { return m_bVirtual
; }
151 bool IsPureVirtual() const
152 { return m_bPureVirtual
; }
156 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
158 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
160 bool IsDeprecated() const
161 { return m_bDeprecated
; }
166 void SetReturnType(const wxType
& t
)
168 void SetName(const wxString
& name
)
170 void SetArgumentTypes(const wxArgumentTypeArray
& arr
)
172 void SetConst(bool c
= true)
174 void SetStatic(bool c
= true)
176 void SetVirtual(bool c
= true)
178 void SetPureVirtual(bool c
= true)
181 if (c
) m_bVirtual
=c
; // pure virtual => virtual
183 void SetDeprecated(bool c
= true)
185 void SetLocation(int lineNumber
)
186 { m_nLine
=lineNumber
; }
187 void SetAvailability(int nAvail
)
188 { m_nAvailability
=nAvail
; }
192 bool operator==(const wxMethod
&) const;
193 bool operator!=(const wxMethod
& m
) const
194 { return !(*this == m
); }
196 void Dump(wxTextOutputStream
& stream
) const;
201 wxArgumentTypeArray m_args
;
210 // m_nLine can be -1 if no location infos are available
213 // this is a combination of wxPORT_* flags (see wxPortId) or wxPORT_UNKNOWN
214 // if this method should be available for all wxWidgets ports.
215 // NOTE: this is not used for comparing wxMethod objects
216 // (gccXML never gives this kind of info).
220 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
221 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
224 // ----------------------------------------------------------------------------
225 // Represents a class of the wx API/interface.
226 // ----------------------------------------------------------------------------
231 wxClass(const wxString
& name
, const wxString
& headername
)
232 : m_strName(name
), m_strHeader(headername
) {}
237 void SetHeader(const wxString
& header
)
238 { m_strHeader
=header
; }
239 void SetName(const wxString
& name
)
241 void SetAvailability(int nAvail
)
242 { m_nAvailability
=nAvail
; }
248 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
250 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
251 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
253 wxString
GetName() const
254 { return m_strName
; }
255 wxString
GetHeader() const
256 { return m_strHeader
; }
257 wxString
GetNameWithoutTemplate() const;
259 unsigned int GetMethodCount() const
260 { return m_methods
.GetCount(); }
261 wxMethod
& GetMethod(unsigned int n
) const
262 { return m_methods
[n
]; }
263 wxMethod
& GetLastMethod() const
264 { return m_methods
.Last(); }
266 int GetAvailability() const
267 { return m_nAvailability
; }
271 void AddMethod(const wxMethod
& func
)
272 { m_methods
.Add(func
); }
274 // returns a single result (the first, which is also the only
275 // one if CheckConsistency() return true)
276 const wxMethod
* FindMethod(const wxMethod
& m
) const;
278 // returns an array of pointers to the overloaded methods with the
280 wxMethodPtrArray
FindMethodsNamed(const wxString
& m
) const;
282 // dumps all methods to the given output stream
283 void Dump(wxTextOutputStream
& stream
) const;
286 bool CheckConsistency() const;
290 wxString m_strHeader
;
291 wxMethodArray m_methods
;
293 // see the wxMethod::m_nAvailability field for more info
297 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
298 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
310 const wxClass
* FindClass(const wxString
& classname
) const
312 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
313 if (m_classes
[i
].GetName() == classname
)
314 return &m_classes
[i
];
318 void Dump(const wxString
& filename
);
320 const wxClassArray
& GetClasses() const
321 { return m_classes
; }
323 unsigned int GetClassesCount() const
324 { return m_classes
.GetCount(); }
326 unsigned int GetMethodCount() const
328 unsigned int methods
= 0;
329 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
330 methods
+= m_classes
[i
].GetMethodCount();
334 // pass a full-path header filename:
335 wxClassPtrArray
FindClassesDefinedIn(const wxString
& headerfile
) const;
338 { /*wxPrint(".");*/ }
340 bool CheckParseResults() const;
343 wxClassArray m_classes
;
347 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
348 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
349 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
350 wxIntegerHash
, wxIntegerEqual
,
353 WX_DECLARE_STRING_HASH_MAP( wxString
, wxStringHashMap
);
356 typedef std::basic_string
<char> stlString
;
357 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
361 // ----------------------------------------------------------------------------
362 // Represents the real interface of wxWidgets
363 // Loads it from the XML produced by gccXML: http://www.gccxml.org
364 // ----------------------------------------------------------------------------
365 class wxXmlGccInterface
: public wxXmlInterface
370 // FIXME: we should retrieve this from the XML file!
371 // here we suppose the XML was created for the currently-running port
372 m_portId
= wxPlatformInfo::Get().GetPortId();
375 bool Parse(const wxString
& filename
);
376 bool ParseMethod(const wxXmlNode
*p
,
377 const wxTypeIdHashMap
& types
,
380 wxPortId
GetInterfacePort() const
383 wxString
GetInterfacePortName() const
384 { return wxPlatformInfo::GetPortIdName(m_portId
, false); }
387 // the port for which the gcc XML was generated
392 // ----------------------------------------------------------------------------
393 // Represents the interface of the doxygen headers of wxWidgets
394 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
395 // ----------------------------------------------------------------------------
396 class wxXmlDoxygenInterface
: public wxXmlInterface
399 wxXmlDoxygenInterface() {}
402 // Using wxXmlDocument::Load as is, the entire XML file is parsed
403 // and an entire tree of wxXmlNodes is built in memory.
404 // We need however only small portions of the Doxygen-generated XML: to speedup the
405 // processing we could detach the expat callbacks when we detect the beginning of a
406 // node we're not interested about, or just don't create a wxXmlNode for it!
407 // This needs a modification of wxXml API.
409 bool Parse(const wxString
& filename
);
410 bool ParseCompoundDefinition(const wxString
& filename
);
411 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
413 // this class can take advantage of the preprocessor output to give
414 // a minor number of false positive warnings in the final comparison
415 void AddPreprocessorValue(const wxString
& name
, const wxString
& val
)
416 { m_preproc
[name
]=val
; }
419 wxStringHashMap m_preproc
;
424 #endif // _XMLPARSER_H_