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>
26 Any fix aimed to reduce "false positives" which involves
27 references to a specific wxWidgets class is marked in
28 ifacecheck sources with the string:
31 // ...fix description...
34 // NOTE: all messages in this way are printed on the stderr
35 //#define wxLogWarning wxLogMessage
38 // ----------------------------------------------------------------------------
39 // Represents a type with or without const/static/ qualifier
40 // and with or without & and * operators
41 // ----------------------------------------------------------------------------
46 wxType(const wxString
& type
)
47 { SetTypeFromString(type
); }
49 void SetTypeFromString(const wxString
& t
);
50 wxString
GetAsString() const
53 // returns this type _without_ any decoration,
54 // including the & (which indicates this is a reference),
55 // the * (which indicates this is a pointer), etc.
56 wxString
GetAsCleanString() const
57 { return m_strTypeClean
; }
60 { return m_strType
.Contains("const"); }
62 { return m_strType
.Contains("static"); }
63 bool IsPointer() const
64 { return m_strType
.Contains("*"); }
65 bool IsReference() const
66 { return m_strType
.Contains("&"); }
68 bool operator==(const wxType
& m
) const;
69 bool operator!=(const wxType
& m
) const
70 { return !(*this == m
); }
76 m_strTypeClean
; // m_strType "cleaned" of its attributes
77 // (only for internal use)
80 extern wxType wxEmptyType
;
81 WX_DECLARE_OBJARRAY(wxType
, wxTypeArray
);
84 // ----------------------------------------------------------------------------
85 // Represents a type used as argument for some wxMethod
86 // ----------------------------------------------------------------------------
87 class wxArgumentType
: public wxType
91 wxArgumentType(const wxString
& type
, const wxString
& defVal
,
92 const wxString
& argName
= wxEmptyString
)
93 { SetTypeFromString(type
); SetDefaultValue(defVal
); m_strArgName
= argName
; }
95 void SetArgumentName(const wxString
& name
)
96 { m_strArgName
=name
.Strip(wxString::both
); }
97 wxString
GetArgumentName() const
98 { return m_strArgName
; }
100 void SetDefaultValue(const wxString
& defval
,
101 const wxString
& defvalForCmp
= wxEmptyString
);
102 wxString
GetDefaultValue() const
103 { return m_strDefaultValue
; }
105 // returns the default value used for comparisons
106 wxString
GetDefaultCleanValue() const
107 { return m_strDefaultValueForCmp
; }
109 bool HasDefaultValue() const
110 { return !m_strDefaultValue
.IsEmpty(); }
112 bool operator==(const wxArgumentType
& m
) const;
113 bool operator!=(const wxArgumentType
& m
) const
114 { return !(*this == m
); }
117 wxString m_strDefaultValue
;
119 // this string may differ from m_strDefaultValue if there were
120 // preprocessor substitutions or other "replacements" done to
121 // avoid false errors.
122 wxString m_strDefaultValueForCmp
;
125 wxString m_strArgName
;
128 extern wxArgumentType wxEmptyArgumentType
;
129 WX_DECLARE_OBJARRAY(wxArgumentType
, wxArgumentTypeArray
);
132 enum wxMethodAccessSpecifier
139 // ----------------------------------------------------------------------------
140 // Represents a single prototype of a class' member.
141 // ----------------------------------------------------------------------------
146 { m_bConst
=m_bVirtual
=m_bPureVirtual
=m_bStatic
=m_bDeprecated
=false;
147 m_nLine
=-1; m_nAvailability
=wxPORT_UNKNOWN
; m_access
=wxMAS_PUBLIC
; }
149 wxMethod(const wxType
& rettype
, const wxString
& name
,
150 const wxArgumentTypeArray
& arguments
,
151 bool isConst
, bool isStatic
, bool isVirtual
)
152 : m_retType(rettype
), m_strName(name
.Strip(wxString::both
)),
153 m_bConst(isConst
), m_bStatic(isStatic
), m_bVirtual(isVirtual
)
154 { SetArgumentTypes(arguments
); m_nLine
=-1; }
159 // bWithArgumentNames = output argument names?
160 // bCleanDefaultValues = output clean argument default values?
161 // bDeprecated = output [deprecated] next to deprecated methods?
162 // bAccessSpec = output [public], [protected] or [private] next to method?
164 // TODO: convert to readable flags this set of bools
165 wxString
GetAsString(bool bWithArgumentNames
= true,
166 bool bCleanDefaultValues
= false,
167 bool bDeprecated
= false,
168 bool bAccessSpec
= false) const;
170 // parser of the prototype:
171 // all these functions return strings with spaces stripped
172 wxType
GetReturnType() const
173 { return m_retType
; }
174 wxString
GetName() const
175 { return m_strName
; }
176 const wxArgumentTypeArray
& GetArgumentTypes() const
178 wxArgumentTypeArray
& GetArgumentTypes()
180 int GetLocation() const
182 int GetAvailability() const
183 { return m_nAvailability
; }
184 wxMethodAccessSpecifier
GetAccessSpecifier() const
189 bool IsStatic() const
190 { return m_bStatic
; }
191 bool IsVirtual() const
192 { return m_bVirtual
; }
193 bool IsPureVirtual() const
194 { return m_bPureVirtual
; }
198 { return m_retType
==wxEmptyType
&& !m_strName
.StartsWith("~"); }
200 { return m_retType
==wxEmptyType
&& m_strName
.StartsWith("~"); }
201 bool IsOperator() const
202 { return m_strName
.StartsWith("operator"); }
204 bool IsDeprecated() const
205 { return m_bDeprecated
; }
211 void SetReturnType(const wxType
& t
)
213 void SetName(const wxString
& name
)
215 void SetArgumentTypes(const wxArgumentTypeArray
& arr
)
217 void SetConst(bool c
= true)
219 void SetStatic(bool c
= true)
221 void SetVirtual(bool c
= true)
223 void SetPureVirtual(bool c
= true)
226 if (c
) m_bVirtual
=c
; // pure virtual => virtual
228 void SetDeprecated(bool c
= true)
230 void SetLocation(int lineNumber
)
231 { m_nLine
=lineNumber
; }
232 void SetAvailability(int nAvail
)
233 { m_nAvailability
=nAvail
; }
234 void SetAccessSpecifier(wxMethodAccessSpecifier spec
)
239 bool operator==(const wxMethod
&) const;
240 bool operator!=(const wxMethod
& m
) const
241 { return !(*this == m
); }
243 // this function works like operator== but tests everything:
247 // except for the method attributes (const,static,virtual,pureVirtual,deprecated)
248 bool MatchesExceptForAttributes(const wxMethod
& m
) const;
250 // returns true if this is a ctor which has default values for all its
251 // argument, thus is able to act also as default ctor
252 bool ActsAsDefaultCtor() const;
254 // dumps the contents of this class in the given stream
255 void Dump(wxTextOutputStream
& stream
) const;
260 wxArgumentTypeArray m_args
;
269 // m_nLine can be -1 if no location infos are available
272 // this is a combination of wxPORT_* flags (see wxPortId) or wxPORT_UNKNOWN
273 // if this method should be available for all wxWidgets ports.
274 // NOTE: this is not used for comparing wxMethod objects
275 // (gccXML never gives this kind of info).
278 // the access specifier for this method
279 wxMethodAccessSpecifier m_access
;
282 WX_DECLARE_OBJARRAY(wxMethod
, wxMethodArray
);
283 WX_DEFINE_ARRAY(const wxMethod
*, wxMethodPtrArray
);
286 // we need wxClassPtrArray to be defined _before_ wxClass itself,
287 // since wxClass uses wxClassPtrArray.
289 WX_DEFINE_ARRAY(const wxClass
*, wxClassPtrArray
);
291 class wxXmlInterface
;
294 // ----------------------------------------------------------------------------
295 // Represents a class of the wx API/interface.
296 // ----------------------------------------------------------------------------
301 wxClass(const wxString
& name
, const wxString
& headername
)
302 : m_strName(name
), m_strHeader(headername
) {}
307 void SetHeader(const wxString
& header
)
308 { m_strHeader
=header
; }
309 void SetName(const wxString
& name
)
311 void SetAvailability(int nAvail
)
312 { m_nAvailability
=nAvail
; }
313 void SetParent(unsigned int k
, const wxString
& name
)
314 { m_parents
[k
]=name
; }
319 { return !m_strName
.IsEmpty() && !m_methods
.IsEmpty(); }
321 bool IsValidCtorForThisClass(const wxMethod
& m
) const;
322 bool IsValidDtorForThisClass(const wxMethod
& m
) const;
324 wxString
GetName() const
325 { return m_strName
; }
326 wxString
GetHeader() const
327 { return m_strHeader
; }
328 wxString
GetNameWithoutTemplate() const;
330 unsigned int GetMethodCount() const
331 { return m_methods
.GetCount(); }
332 wxMethod
& GetMethod(unsigned int n
) const
333 { return m_methods
[n
]; }
334 wxMethod
& GetLastMethod() const
335 { return m_methods
.Last(); }
337 int GetAvailability() const
338 { return m_nAvailability
; }
340 //const wxClass *GetParent(unsigned int i) const
341 const wxString
& GetParent(unsigned int i
) const
342 { return m_parents
[i
]; }
343 unsigned int GetParentCount() const
344 { return m_parents
.GetCount(); }
348 void AddMethod(const wxMethod
& func
)
349 { m_methods
.Add(func
); }
351 void AddParent(const wxString
& parent
)//wxClass* parent)
352 { m_parents
.Add(parent
); }
354 // returns a single result (the first, which is also the only
355 // one if CheckConsistency() return true)
356 const wxMethod
* FindMethod(const wxMethod
& m
) const;
358 // like FindMethod() but this one searches also recursively in
359 // the parents of this class.
360 const wxMethod
* RecursiveUpwardFindMethod(const wxMethod
& m
,
361 const wxXmlInterface
* allclasses
) const;
363 // returns an array of pointers to the overloaded methods with the
365 wxMethodPtrArray
FindMethodsNamed(const wxString
& name
) const;
367 // like FindMethodsNamed() but this one searches also recursively in
368 // the parents of this class.
369 wxMethodPtrArray
RecursiveUpwardFindMethodsNamed(const wxString
& name
,
370 const wxXmlInterface
* allclasses
) const;
372 // dumps all methods to the given output stream
373 void Dump(wxTextOutputStream
& stream
) const;
376 bool CheckConsistency() const;
380 wxString m_strHeader
;
381 wxMethodArray m_methods
;
383 // name of the base classes: we store the names and not the pointers
384 // because this makes _much_ easier the parsing process!
385 // (basically because when parsing class X which derives from Y,
386 // we may have not parsed yet class Y!)
387 wxArrayString m_parents
;
389 // see the wxMethod::m_nAvailability field for more info
393 WX_DECLARE_OBJARRAY(wxClass
, wxClassArray
);
397 // ----------------------------------------------------------------------------
399 // ----------------------------------------------------------------------------
405 const wxClass
* FindClass(const wxString
& classname
) const
407 for (unsigned int i
=0; i
<m_classes
.GetCount(); i
++)
408 if (m_classes
[i
].GetName() == classname
)
409 return &m_classes
[i
];
413 void Dump(const wxString
& filename
);
415 const wxClassArray
& GetClasses() const
416 { return m_classes
; }
418 unsigned int GetClassesCount() const
419 { return m_classes
.GetCount(); }
421 unsigned int GetMethodCount() const
423 unsigned int methods
= 0;
424 for (unsigned i
=0; i
< m_classes
.GetCount(); i
++)
425 methods
+= m_classes
[i
].GetMethodCount();
429 // pass a full-path header filename:
430 wxClassPtrArray
FindClassesDefinedIn(const wxString
& headerfile
) const;
433 { /*wxFprintf(stderr, ".");*/ }
435 // is this interface coherent?
436 bool CheckConsistency() const;
439 wxClassArray m_classes
;
443 // for wxTypeIdHashMap, keys == gccxml IDs and values == associated type strings
444 // e.g. key = "0x123f" and value = "const wxAboutDialogInfo&"
445 WX_DECLARE_HASH_MAP( unsigned long, wxString
,
446 wxIntegerHash
, wxIntegerEqual
,
449 WX_DECLARE_STRING_HASH_MAP( wxString
, wxStringHashMap
);
452 typedef std::basic_string
<char> stlString
;
453 typedef std::map
<unsigned long, stlString
> wxTypeIdHashMap
;
457 // ----------------------------------------------------------------------------
458 // Represents the real interface of wxWidgets
459 // Loads it from the XML produced by gccXML: http://www.gccxml.org
460 // ----------------------------------------------------------------------------
461 class wxXmlGccInterface
: public wxXmlInterface
466 // FIXME: we should retrieve this from the XML file!
467 // here we suppose the XML was created for the currently-running port
468 m_portId
= wxPlatformInfo::Get().GetPortId();
471 bool Parse(const wxString
& filename
);
472 bool ParseMethod(const wxXmlNode
*p
,
473 const wxTypeIdHashMap
& types
,
476 wxPortId
GetInterfacePort() const
479 wxString
GetInterfacePortName() const
480 { return wxPlatformInfo::GetPortIdName(m_portId
, false); }
483 // the port for which the gcc XML was generated
488 // ----------------------------------------------------------------------------
489 // Represents the interface of the doxygen headers of wxWidgets
490 // Loads it from the XML produced by Doxygen: http://www.doxygen.org
491 // ----------------------------------------------------------------------------
492 class wxXmlDoxygenInterface
: public wxXmlInterface
495 wxXmlDoxygenInterface() {}
498 // Using wxXmlDocument::Load as is, the entire XML file is parsed
499 // and an entire tree of wxXmlNodes is built in memory.
500 // We need however only small portions of the Doxygen-generated XML: to speedup the
501 // processing we could detach the expat callbacks when we detect the beginning of a
502 // node we're not interested about, or just don't create a wxXmlNode for it!
503 // This needs a modification of wxXml API.
505 bool Parse(const wxString
& filename
);
506 bool ParseCompoundDefinition(const wxString
& filename
);
507 bool ParseMethod(const wxXmlNode
*, wxMethod
&, wxString
& header
);
509 // this class can take advantage of the preprocessor output to give
510 // a minor number of false positive warnings in the final comparison
511 void AddPreprocessorValue(const wxString
& name
, const wxString
& val
)
512 { m_preproc
[name
]=val
; }
515 wxStringHashMap m_preproc
;
520 #endif // _XMLPARSER_H_