]> git.saurik.com Git - wxWidgets.git/blob - utils/ifacecheck/src/xmlparser.cpp
speedup a little the parser by converting gccXML ID attributes to numbers, instead...
[wxWidgets.git] / utils / ifacecheck / src / xmlparser.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xmlparser.cpp
3 // Purpose: Parser of the API/interface XML files
4 // Author: Francesco Montorsi
5 // Created: 2008/03/17
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 // for all others, include the necessary headers
19 #ifndef WX_PRECOMP
20 #endif
21
22 #include "wx/xml/xml.h"
23 #include "wx/wfstream.h"
24 #include "wx/arrimpl.cpp"
25 #include "wx/dynarray.h"
26 #include "wx/filename.h"
27
28 #include "xmlparser.h"
29
30 #define PROGRESS_RATE 1000 // each PROGRESS_RATE nodes processed print a dot
31 #define ESTIMATED_NUM_CLASSES 600 // used by both wxXmlInterface-derived classes to prealloc mem
32 #define ESTIMATED_NUM_TYPES 50000 // used only by wxGccXmlInterface to prealloc mem
33 #define ESTIMATED_NUM_FILES 800 // used only by wxGccXmlInterface to prealloc mem
34
35 WX_DEFINE_OBJARRAY(wxTypeArray)
36 WX_DEFINE_OBJARRAY(wxMethodArray)
37 WX_DEFINE_OBJARRAY(wxClassArray)
38
39
40 // declared in ifacecheck.cpp
41 extern bool g_verbose;
42
43
44
45 // ----------------------------------------------------------------------------
46 // wxType
47 // ----------------------------------------------------------------------------
48
49 wxType wxEmptyType;
50
51 void wxType::SetFromString(const wxString& t)
52 {
53 m_strType = t.Strip(wxString::both);
54
55 // [] is the same as * for gccxml
56 m_strType.Replace("[]", "*");
57 }
58
59 bool wxType::IsOk() const
60 {
61 // NB: m_strType can contain the :: operator; think to e.g. the
62 // "reverse_iterator_impl<wxString::const_iterator>" type
63 // It can also contain commas, * and & operators etc
64
65 return !GetClean().IsEmpty();
66 }
67
68 wxString wxType::GetClean() const
69 {
70 wxString ret(m_strType);
71 ret.Replace("const", "");
72 ret.Replace("static", "");
73 ret.Replace("*", "");
74 ret.Replace("&", "");
75 ret.Replace("[]", "");
76 return ret.Strip(wxString::both);
77 }
78
79 bool wxType::operator==(const wxType& m) const
80 {
81 // brain-dead comparison:
82
83 if (GetClean() == m.GetClean() &&
84 IsConst() == m.IsConst() &&
85 IsStatic() == m.IsStatic() &&
86 IsPointer() == m.IsPointer() &&
87 IsReference() == m.IsReference())
88 return true;
89
90 return false;
91 }
92
93 // ----------------------------------------------------------------------------
94 // wxMethod
95 // ----------------------------------------------------------------------------
96
97 bool wxMethod::IsOk() const
98 {
99 // NOTE: m_retType can be a wxEmptyType, and means that this method
100 // is a ctor or a dtor.
101 if (!m_retType.IsOk() && m_retType!=wxEmptyType) {
102 LogError("'%s' method has invalid return type: %s", m_retType.GetAsString());
103 return false;
104 }
105
106 if (m_strName.IsEmpty())
107 return false;
108
109 // a function can't be both const and static or virtual and static!
110 if ((m_bConst && m_bStatic) || (m_bVirtual && m_bStatic)) {
111 LogError("'%s' method can't be both const/static or virtual/static", m_strName);
112 return false;
113 }
114
115 for (unsigned int i=0; i<m_args.GetCount(); i++)
116 if (!m_args[i].IsOk()) {
117 LogError("'%s' method has invalid %d-th argument type: %s",
118 m_strName, i, m_args[i].GetAsString());
119 return false;
120 }
121
122 // NB: the default value of the arguments can contain pretty much everything
123 // (think to e.g. wxPoint(3+4/2,0) or *wxBLACK or someClass<type>)
124 // so we don't do any test on them.
125
126 return true;
127 }
128
129 void wxMethod::SetArgumentTypes(const wxTypeArray& arr, const wxArrayString& defaults)
130 {
131 wxASSERT(arr.GetCount()==defaults.GetCount());
132
133 m_args=arr;
134 m_argDefaults=defaults;
135
136 // in order to make valid&simple comparison on argument defaults,
137 // we reduce some of the multiple forms in which the same things may appear
138 // to a single form
139 for (unsigned int i=0; i<m_argDefaults.GetCount(); i++)
140 {
141 m_argDefaults[i].Replace("NULL", "0");
142 m_argDefaults[i].Replace("0u", "0");
143 }
144 }
145
146 bool wxMethod::operator==(const wxMethod& m) const
147 {
148 if (GetReturnType() != m.GetReturnType() ||
149 GetName() != m.GetName() ||
150 IsConst() != m.IsConst() ||
151 IsStatic() != m.IsStatic() ||
152 IsVirtual() != m.IsVirtual())
153 return false;
154
155 if (m_args.GetCount()!=m.m_args.GetCount())
156 return false;
157
158 for (unsigned int i=0; i<m_args.GetCount(); i++)
159 if (m_args[i] != m.m_args[i] || m_argDefaults[i] != m.m_argDefaults[i])
160 return false;
161
162 return true;
163 }
164
165 wxString wxMethod::GetAsString() const
166 {
167 wxString ret;
168
169 if (m_retType!=wxEmptyType)
170 ret += m_retType.GetAsString() + " ";
171 //else; this is a ctor or dtor
172
173 ret += m_strName + "(";
174
175 for (unsigned int i=0; i<m_args.GetCount(); i++)
176 {
177 ret += m_args[i].GetAsString();
178 if (!m_argDefaults[i].IsEmpty())
179 ret += " = " + m_argDefaults[i];
180 ret += ",";
181 }
182
183 if (m_args.GetCount()>0)
184 ret.RemoveLast();
185
186 ret += ")";
187
188 if (m_bConst)
189 ret += " const";
190 if (m_bStatic)
191 ret = "static " + ret;
192 if (m_bVirtual)
193 ret = "virtual " + ret;
194
195 return ret;
196 }
197
198 void wxMethod::Dump(wxTextOutputStream& stream) const
199 {
200 stream << "[" + m_retType.GetAsString() + "]";
201 stream << "[" + m_strName + "]";
202
203 for (unsigned int i=0; i<m_args.GetCount(); i++)
204 stream << "[" + m_args[i].GetAsString() + "=" + m_argDefaults[i] + "]";
205
206 if (IsConst())
207 stream << " CONST";
208 if (IsStatic())
209 stream << " STATIC";
210 if (IsVirtual())
211 stream << " VIRTUAL";
212
213 // no final newline
214 }
215
216 // ----------------------------------------------------------------------------
217 // wxClass
218 // ----------------------------------------------------------------------------
219
220 wxString wxClass::GetNameWithoutTemplate() const
221 {
222 // NB: I'm not sure this is the right terminology for this function!
223
224 if (m_strName.Contains("<"))
225 return m_strName.Left(m_strName.Find("<"));
226 return m_strName;
227 }
228
229 bool wxClass::IsValidCtorForThisClass(const wxMethod& m) const
230 {
231 // remember that e.g. the ctor for wxWritableCharTypeBuffer<wchar_t> is
232 // named wxWritableCharTypeBuffer, without the <...> part!
233
234 if (m.IsCtor() && m.GetName() == GetNameWithoutTemplate())
235 return true;
236
237 return false;
238 }
239
240 bool wxClass::IsValidDtorForThisClass(const wxMethod& m) const
241 {
242 if (m.IsDtor() && m.GetName() == "~" + GetNameWithoutTemplate())
243 return true;
244
245 return false;
246 }
247
248 void wxClass::Dump(wxTextOutputStream& out) const
249 {
250 out << m_strName + "\n";
251
252 for (unsigned int i=0; i<m_methods.GetCount(); i++) {
253
254 // dump all our methods
255 out << "|- ";
256 m_methods[i].Dump(out);
257 out << "\n";
258 }
259
260 out << "\n";
261 }
262
263 bool wxClass::CheckConsistency() const
264 {
265 for (unsigned int i=0; i<m_methods.GetCount(); i++)
266 for (unsigned int j=0; j<m_methods.GetCount(); j++)
267 if (i!=j && m_methods[i] == m_methods[j])
268 {
269 LogError("class %s has two methods with the same prototype: '%s'",
270 m_strName, m_methods[i].GetAsString());
271 return false;
272 }
273
274 return true;
275 }
276
277 const wxMethod* wxClass::FindMethod(const wxMethod& m) const
278 {
279 for (unsigned int i=0; i<m_methods.GetCount(); i++)
280 if (m_methods[i] == m)
281 return &m_methods[i];
282 return NULL;
283 }
284
285 wxMethodPtrArray wxClass::FindMethodNamed(const wxString& name) const
286 {
287 wxMethodPtrArray ret;
288
289 for (unsigned int i=0; i<m_methods.GetCount(); i++)
290 if (m_methods[i].GetName() == name)
291 ret.Add(&m_methods[i]);
292
293 return ret;
294 }
295
296
297 // ----------------------------------------------------------------------------
298 // wxXmlInterface
299 // ----------------------------------------------------------------------------
300
301 WX_DEFINE_SORTED_ARRAY(wxClass*, wxSortedClassArray);
302
303 int CompareWxClassObjects(wxClass *item1, wxClass *item2)
304 {
305 // sort alphabetically
306 return item1->GetName().Cmp(item2->GetName());
307 }
308
309 void wxXmlInterface::Dump(const wxString& filename)
310 {
311 wxFFileOutputStream apioutput( filename );
312 wxTextOutputStream apiout( apioutput );
313
314 // dump the classes in alphabetical order
315 wxSortedClassArray sorted(CompareWxClassObjects);
316 sorted.Alloc(m_classes.GetCount());
317 for (unsigned int i=0; i<m_classes.GetCount(); i++)
318 sorted.Add(&m_classes[i]);
319
320 // now they have been sorted
321 for (unsigned int i=0; i<sorted.GetCount(); i++)
322 sorted[i]->Dump(apiout);
323 }
324
325 bool wxXmlInterface::CheckParseResults() const
326 {
327 // this check can be quite slow, so do it only for debug releases:
328 #ifdef __WXDEBUG__
329 for (unsigned int i=0; i<m_classes.GetCount(); i++)
330 if (!m_classes[i].CheckConsistency())
331 return false;
332 #endif
333
334 return true;
335 }
336
337 // ----------------------------------------------------------------------------
338 // wxXmlGccInterface
339 // ----------------------------------------------------------------------------
340
341 #define ATTRIB_CONST 1
342 #define ATTRIB_REFERENCE 2
343 #define ATTRIB_POINTER 4
344 #define ATTRIB_ARRAY 8
345
346 #define GCCXML_BASE 35
347
348 class toResolveTypeItem
349 {
350 public:
351 toResolveTypeItem() { attribs=0; }
352 toResolveTypeItem(unsigned int refID, unsigned int attribint)
353 : ref(refID), attribs(attribint) {}
354
355 unsigned long ref, attribs;
356 };
357
358 #if 1
359 WX_DECLARE_HASH_MAP( unsigned long, toResolveTypeItem,
360 wxIntegerHash, wxIntegerEqual,
361 wxToResolveTypeHashMap );
362 #else
363 #include <map>
364 typedef std::map<unsigned long, toResolveTypeItem> wxToResolveTypeHashMap;
365 #endif
366
367 bool wxXmlGccInterface::Parse(const wxString& filename)
368 {
369 wxXmlDocument doc;
370 wxXmlNode *child;
371 int nodes = 0;
372
373 LogMessage("Parsing %s...", filename);
374
375 if (!doc.Load(filename)) {
376 LogError("can't load %s", filename);
377 return false;
378 }
379
380 // start processing the XML file
381 if (doc.GetRoot()->GetName() != "GCC_XML") {
382 LogError("invalid root node for %s", filename);
383 return false;
384 }
385
386 wxToResolveTypeHashMap toResolveTypes;
387 wxArrayString arrMemberIds;
388 wxTypeIdHashMap types;
389 wxTypeIdHashMap files;
390
391 // prealloc quite a lot of memory!
392 m_classes.Alloc(ESTIMATED_NUM_CLASSES);
393 arrMemberIds.Alloc(ESTIMATED_NUM_TYPES);
394
395 // build a list of wx classes and in general of all existent types
396 child = doc.GetRoot()->GetChildren();
397 while (child)
398 {
399 const wxString& n = child->GetName();
400 //const wxString& id = child->GetAttribute("id");
401 unsigned long id = 0;
402 if (!child->GetAttribute("id").Mid(1).ToULong(&id, GCCXML_BASE) ||
403 (id == 0 && n != "File")) {
404
405 // NOTE: <File> nodes can have an id == "f0"...
406
407 LogError("Invalid id for node %s: %s", n, child->GetAttribute("id"));
408 return false;
409 }
410
411 if (n == "Class")
412 {
413 wxString cname = child->GetAttribute("name");
414 if (cname.IsEmpty()) {
415 LogError("Invalid empty name for '%s' node", n);
416 return false;
417 }
418
419 // only register wx classes (do remember also the IDs of their members)
420 if (cname.StartsWith("wx")) {
421 arrMemberIds.Add(child->GetAttribute("members"));
422
423 // NB: "file" attribute contains an ID value that we'll resolve later
424 m_classes.Add(wxClass(cname, child->GetAttribute("file")));
425 }
426
427 // register this class also as possible return/argument type:
428 types[id] = cname;
429 }
430 else if (n == "PointerType" || n == "ReferenceType" ||
431 n == "CvQualifiedType" || n == "ArrayType")
432 {
433 unsigned long type = 0;
434 if (!child->GetAttribute("type").Mid(1).ToULong(&type, GCCXML_BASE) || type == 0) {
435 LogError("Invalid type for node %s: %s", n, child->GetAttribute("type"));
436 return false;
437 }
438
439 unsigned long attr = 0;
440 if (n == "PointerType")
441 attr = ATTRIB_POINTER;
442 else if (n == "ReferenceType")
443 attr = ATTRIB_REFERENCE;
444 else if (n == "CvQualifiedType" && child->GetAttribute("const") == "1")
445 attr = ATTRIB_CONST;
446 else if (n == "ArrayType")
447 attr = ATTRIB_ARRAY;
448
449 // these nodes make reference to other types... we'll resolve them later
450 toResolveTypes[id] = toResolveTypeItem(type, attr);
451 }
452 else if (n == "FunctionType" || n == "MethodType")
453 {
454 /* TODO: incomplete */
455
456 unsigned long ret = 0;
457 if (!child->GetAttribute("returns").Mid(1).ToULong(&ret, GCCXML_BASE) || ret == 0) {
458 LogError("Invalid empty returns value for '%s' node", n);
459 return false;
460 }
461
462 // these nodes make reference to other types... we'll resolve them later
463 toResolveTypes[id] = toResolveTypeItem(ret, 0);
464 }
465 else if (n == "File")
466 {
467 if (!child->GetAttribute("id").StartsWith("f")) {
468 LogError("Unexpected file ID: %s", id);
469 return false;
470 }
471
472 // just ignore this node... all file IDs/names were already parsed
473 files[id] = child->GetAttribute("name");
474 }
475 else
476 {
477 // we register everything else as a possible return/argument type:
478 const wxString& name = child->GetAttribute("name");
479
480 if (!name.IsEmpty())
481 {
482 //typeIds.Add(id);
483 //typeNames.Add(name);
484 types[id] = name;
485 }
486 else
487 {
488 // this may happen with unnamed structs/union, special ctors,
489 // or other exotic things which we are not interested to, since
490 // they're never used as return/argument types by wxWidgets methods
491
492 if (g_verbose)
493 LogWarning("Type '%s' with ID '%s' does not have name attribute", n, id);
494
495 types[id] = "TOFIX";
496 }
497 }
498
499 child = child->GetNext();
500
501 // give feedback to the user about the progress...
502 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
503 }
504
505 // some nodes with IDs referenced by methods as return/argument types, do reference
506 // in turn o ther nodes (see PointerType, ReferenceType and CvQualifierType above);
507 // thus we need to resolve their name iteratively:
508 while (toResolveTypes.size()>0)
509 {
510 if (g_verbose)
511 LogMessage("%d types were collected; %d types need yet to be resolved...",
512 types.size(), toResolveTypes.size());
513
514 for (wxToResolveTypeHashMap::iterator i = toResolveTypes.begin();
515 i != toResolveTypes.end();)
516 {
517 unsigned long id = i->first;
518 unsigned long referenced = i->second.ref;
519
520 wxTypeIdHashMap::iterator primary = types.find(referenced);
521 if (primary != types.end())
522 {
523 // this to-resolve-type references a "primary" type
524
525 wxString newtype;
526 int attribs = i->second.attribs;
527
528 if (attribs & ATTRIB_CONST)
529 newtype = "const " + primary->second;
530 if (attribs & ATTRIB_REFERENCE)
531 newtype = primary->second + "&";
532 if (attribs & ATTRIB_POINTER)
533 newtype = primary->second + "*";
534 if (attribs & ATTRIB_ARRAY)
535 newtype = primary->second + "[]";
536
537 // add the resolved type to the list of "primary" types
538 types[id] = newtype;
539
540 // this one has been resolved; erase it through its iterator!
541 toResolveTypes.erase(i);
542
543 // now iterator i is invalid; assign it again to the beginning
544 i = toResolveTypes.begin();
545 }
546 else
547 {
548 // then search in the referenced types themselves:
549 wxToResolveTypeHashMap::iterator idx2 = toResolveTypes.find(referenced);
550 if (idx2 != toResolveTypes.end())
551 {
552 // merge this to-resolve-type with the idx2->second type
553 i->second.ref = idx2->second.ref;
554 i->second.attribs |= idx2->second.attribs;
555
556 // this type will eventually be solved in the next while() iteration
557 i++;
558 }
559 else
560 {
561 #if 1
562 LogError("Cannot solve '%s' reference type!", referenced);
563 return false;
564 #else
565 typeIds.Add(toResolveTypeIds[i]);
566 typeNames.Add("TOFIX");
567
568 // this one has been resolved!
569 toResolveTypeIds.RemoveAt(i);
570 toResolveRefType.RemoveAt(i);
571 toResolveAttrib.RemoveAt(i);
572 n--;
573 #endif
574 }
575 }
576 }
577 }
578
579 // resolve header names
580 for (unsigned int i=0; i<m_classes.GetCount(); i++)
581 {
582 unsigned long fileID = 0;
583 if (!m_classes[i].GetHeader().Mid(1).ToULong(&fileID, GCCXML_BASE) || fileID == 0) {
584 LogError("invalid header id: %s", m_classes[i].GetHeader());
585 return false;
586 }
587
588 // search this file
589 wxTypeIdHashMap::const_iterator idx = files.find(fileID);
590 if (idx == files.end())
591 {
592 // this is an error!
593 LogError("couldn't find file ID '%s'", m_classes[i].GetHeader());
594 }
595 else
596 m_classes[i].SetHeader(idx->second);
597 }
598
599 // build the list of the wx methods
600 child = doc.GetRoot()->GetChildren();
601 while (child)
602 {
603 wxString n = child->GetName();
604
605 if (n == "Method" || n == "Constructor" || n == "Destructor" || n == "OperatorMethod")
606 {
607 wxString id = child->GetAttribute("id");
608
609 // only register public methods
610 if (child->GetAttribute("access") == "public")
611 {
612 wxASSERT(arrMemberIds.GetCount()==m_classes.GetCount());
613
614 for (unsigned int i=0; i<m_classes.GetCount(); i++)
615 {
616 if (arrMemberIds[i].Contains(id))
617 {
618 // this <Method> node is a method of the i-th class!
619 wxMethod newfunc;
620 if (!ParseMethod(child, types, newfunc))
621 return false;
622
623 if (newfunc.IsCtor() && !m_classes[i].IsValidCtorForThisClass(newfunc)) {
624 LogError("The method '%s' does not seem to be a ctor for '%s'",
625 newfunc.GetName(), m_classes[i].GetName());
626 return false;
627 }
628 if (newfunc.IsDtor() && !m_classes[i].IsValidDtorForThisClass(newfunc)) {
629 LogError("The method '%s' does not seem to be a dtor for '%s'",
630 newfunc.GetName(), m_classes[i].GetName());
631 return false;
632 }
633
634 m_classes[i].AddMethod(newfunc);
635 }
636 }
637 }
638 }
639
640 child = child->GetNext();
641
642 // give feedback to the user about the progress...
643 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
644 }
645
646 //wxPrint("\n");
647 if (!CheckParseResults())
648 return false;
649
650 return true;
651 }
652
653 bool wxXmlGccInterface::ParseMethod(const wxXmlNode *p,
654 const wxTypeIdHashMap& types,
655 wxMethod& m)
656 {
657 // get the real name
658 wxString name = p->GetAttribute("name").Strip(wxString::both);
659 if (p->GetName() == "Destructor")
660 name = "~" + name;
661 else if (p->GetName() == "OperatorMethod")
662 name = "operator" + name;
663
664 // resolve return type
665 wxType ret;
666 unsigned long retid = 0;
667 if (!p->GetAttribute("returns").Mid(1).ToULong(&retid, GCCXML_BASE) || retid == 0)
668 {
669 if (p->GetName() != "Destructor" && p->GetName() != "Constructor") {
670 LogError("Empty return ID for method '%s', with ID '%s'",
671 name, p->GetAttribute("id"));
672 return false;
673 }
674 }
675 else
676 {
677 wxTypeIdHashMap::const_iterator retidx = types.find(retid);
678 if (retidx == types.end()) {
679 LogError("Could not find return type ID '%s'", retid);
680 return false;
681 }
682
683 ret = wxType(retidx->second);
684 if (!ret.IsOk()) {
685 LogError("Invalid return type '%s' for method '%s', with ID '%s'",
686 retidx->second, name, p->GetAttribute("id"));
687 return false;
688 }
689 }
690
691 // resolve argument types
692 wxTypeArray argtypes;
693 wxArrayString argdefs;
694 wxXmlNode *arg = p->GetChildren();
695 while (arg)
696 {
697 if (arg->GetName() == "Argument")
698 {
699 unsigned long id = 0;
700 if (!arg->GetAttribute("type").Mid(1).ToULong(&id, GCCXML_BASE) || id == 0) {
701 LogError("Invalid argument type ID '%s' for method '%s' with ID %s",
702 arg->GetAttribute("type"), name, p->GetAttribute("id"));
703 return false;
704 }
705
706 wxTypeIdHashMap::const_iterator idx = types.find(id);
707 if (idx == types.end()) {
708 LogError("Could not find argument type ID '%s'", id);
709 return false;
710 }
711
712 argtypes.Add(wxType(idx->second));
713
714 wxString def = arg->GetAttribute("default");
715 if (def.Contains("wxGetTranslation"))
716 argdefs.Add(wxEmptyString); // TODO: wxGetTranslation gives problems to gccxml
717 else
718 argdefs.Add(def);
719 }
720
721 arg = arg->GetNext();
722 }
723
724 m.SetReturnType(ret);
725 m.SetName(name);
726 m.SetArgumentTypes(argtypes, argdefs);
727 m.SetConst(p->GetAttribute("const") == "1");
728 m.SetStatic(p->GetAttribute("static") == "1");
729 m.SetVirtual(p->GetAttribute("virtual") == "1");
730
731 if (!m.IsOk()) {
732 LogError("The prototype '%s' is not valid!", m.GetAsString());
733 return false;
734 }
735
736 return true;
737 }
738
739
740 // ----------------------------------------------------------------------------
741 // wxXmlDoxygenInterface
742 // ----------------------------------------------------------------------------
743
744 bool wxXmlDoxygenInterface::Parse(const wxString& filename)
745 {
746 wxXmlDocument index;
747 wxXmlNode *compound;
748
749 LogMessage("Parsing %s...", filename);
750
751 if (!index.Load(filename)) {
752 LogError("can't load %s", filename);
753 return false;
754 }
755
756 // start processing the index:
757 if (index.GetRoot()->GetName() != "doxygenindex") {
758 LogError("invalid root node for %s", filename);
759 return false;
760 }
761
762 m_classes.Alloc(ESTIMATED_NUM_CLASSES);
763
764 // process files referenced by this index file
765 compound = index.GetRoot()->GetChildren();
766 while (compound)
767 {
768 if (compound->GetName() == "compound" &&
769 compound->GetAttribute("kind") == "class")
770 {
771 wxString refid = compound->GetAttribute("refid");
772
773 wxFileName fn(filename);
774 if (!ParseCompoundDefinition(fn.GetPath(wxPATH_GET_SEPARATOR) + refid + ".xml"))
775 return false;
776 }
777
778 compound = compound->GetNext();
779 }
780 //wxPrint("\n");
781
782 if (!CheckParseResults())
783 return false;
784
785 return true;
786 }
787
788 bool wxXmlDoxygenInterface::ParseCompoundDefinition(const wxString& filename)
789 {
790 wxXmlDocument doc;
791 wxXmlNode *child;
792 int nodes = 0;
793
794 if (g_verbose)
795 LogMessage("Parsing %s...", filename);
796
797 if (!doc.Load(filename)) {
798 LogError("can't load %s", filename);
799 return false;
800 }
801
802 // start processing this compound definition XML
803 if (doc.GetRoot()->GetName() != "doxygen") {
804 LogError("invalid root node for %s", filename);
805 return false;
806 }
807
808 // build a list of wx classes
809 child = doc.GetRoot()->GetChildren();
810 while (child)
811 {
812 if (child->GetName() == "compounddef" &&
813 child->GetAttribute("kind") == "class")
814 {
815 // parse this class
816 wxClass klass;
817 wxString absoluteFile, header;
818
819 wxXmlNode *subchild = child->GetChildren();
820 while (subchild)
821 {
822 if (subchild->GetName() == "sectiondef" &&
823 subchild->GetAttribute("kind") == "public-func")
824 {
825
826 wxXmlNode *membernode = subchild->GetChildren();
827 while (membernode)
828 {
829 if (membernode->GetName() == "memberdef" &&
830 membernode->GetAttribute("kind") == "function")
831 {
832
833 wxMethod m;
834 if (ParseMethod(membernode, m, header))
835 {
836 if (absoluteFile.IsEmpty())
837 absoluteFile = header;
838 else if (header != absoluteFile)
839 {
840 LogError("The method '%s' is documented in a different "
841 "file from others (which belong to '%s') ?",
842 header, absoluteFile);
843 return false;
844 }
845
846 klass.AddMethod(m);
847 }
848 }
849
850 membernode = membernode->GetNext();
851 }
852
853 // all methods of this class were taken from the header "absoluteFile":
854 klass.SetHeader(absoluteFile);
855 }
856 else if (subchild->GetName() == "compoundname")
857 {
858 klass.SetName(subchild->GetNodeContent());
859 }
860 /*else if (subchild->GetName() == "includes")
861 {
862 // NOTE: we'll get the header from the <location> tags
863 // scattered inside <memberdef> tags instead of
864 // this <includes> tag since it does not contain
865 // the absolute path of the header
866
867 klass.SetHeader(subchild->GetNodeContent());
868 }*/
869
870 subchild = subchild->GetNext();
871 }
872
873 // add a new class
874 if (klass.IsOk())
875 m_classes.Add(klass);
876 else if (g_verbose)
877 LogWarning("discarding class '%s' with %d methods...",
878 klass.GetName(), klass.GetMethodCount());
879 }
880
881 child = child->GetNext();
882
883 // give feedback to the user about the progress...
884 if ((++nodes%PROGRESS_RATE)==0) ShowProgress();
885 }
886
887 return true;
888 }
889
890 static wxString GetTextFromChildren(const wxXmlNode *n)
891 {
892 wxString text;
893
894 // consider the tree
895 //
896 // <a><b>this</b> is a <b>string</b></a>
897 //
898 // <a>
899 // |- <b>
900 // | |- this
901 // |- is a
902 // |- <b>
903 // |- string
904 //
905 // unlike wxXmlNode::GetNodeContent() which would return " is a "
906 // this function returns "this is a string"
907
908 wxXmlNode *ref = n->GetChildren();
909 while (ref) {
910 if (ref->GetType() == wxXML_ELEMENT_NODE)
911 text += ref->GetNodeContent();
912 else if (ref->GetType() == wxXML_TEXT_NODE)
913 text += ref->GetContent();
914 else
915 LogWarning("Unexpected node type while getting text from '%s' node", n->GetName());
916
917 ref = ref->GetNext();
918 }
919
920 return text;
921 }
922
923 bool wxXmlDoxygenInterface::ParseMethod(const wxXmlNode* p, wxMethod& m, wxString& header)
924 {
925 wxTypeArray args;
926 wxArrayString defs;
927 long line;
928
929 wxXmlNode *child = p->GetChildren();
930 while (child)
931 {
932 if (child->GetName() == "name")
933 m.SetName(child->GetNodeContent());
934 else if (child->GetName() == "type")
935 m.SetReturnType(wxType(GetTextFromChildren(child)));
936 else if (child->GetName() == "param")
937 {
938 wxString typestr, defstr, arrstr;
939 wxXmlNode *n = child->GetChildren();
940 while (n)
941 {
942 if (n->GetName() == "type")
943 // if the <type> node has children, they should be all TEXT and <ref> nodes
944 // and we need to take the text they contain, in the order they appear
945 typestr = GetTextFromChildren(n);
946 else if (n->GetName() == "defval")
947 // same for the <defval> node
948 defstr = GetTextFromChildren(n);
949 else if (n->GetName() == "array")
950 arrstr = GetTextFromChildren(n);
951
952 n = n->GetNext();
953 }
954
955 if (typestr.IsEmpty()) {
956 LogError("cannot find type node for a param in method '%s'", m.GetName());
957 return false;
958 }
959
960 args.Add(wxType(typestr + arrstr));
961 defs.Add(defstr);
962 }
963 else if (child->GetName() == "location")
964 {
965 if (child->GetAttribute("line").ToLong(&line))
966 m.SetLocation((int)line);
967 header = child->GetAttribute("file");
968 }
969
970 child = child->GetNext();
971 }
972
973 m.SetArgumentTypes(args, defs);
974 m.SetConst(p->GetAttribute("const")=="yes");
975 m.SetStatic(p->GetAttribute("static")=="yes");
976 m.SetVirtual(p->GetAttribute("virt")=="virtual");
977
978 if (!m.IsOk()) {
979 LogError("The prototype '%s' is not valid!", m.GetAsString());
980 return false;
981 }
982
983 return true;
984 }