create_docu.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <config.h>
  2. #include "tree.hpp"
  3. #include <iostream>
  4. #include <sstream>
  5. #include <stdexcept>
  6. #include "create_docu.hpp"
  7. #include "globals.hpp"
  8. void
  9. DocuCreator::create_docu(Namespace* ns)
  10. {
  11. writer.openTag("documentation");
  12. writer.openTag("namespace");
  13. writer.writeAttribute("name", ns->name);
  14. for(std::vector<AtomicType*>::iterator i = ns->types.begin();
  15. i != ns->types.end(); ++i) {
  16. AtomicType* type = *i;
  17. Class* _class = dynamic_cast<Class*> (type);
  18. if(_class != 0)
  19. create_class_docu(_class);
  20. }
  21. for(std::vector<Function*>::iterator i = ns->functions.begin();
  22. i != ns->functions.end(); ++i) {
  23. create_function_docu(0, *i);
  24. }
  25. writer.closeTag("namespace");
  26. writer.closeTag("documentation");
  27. }
  28. void
  29. DocuCreator::create_class_docu(Class* _class)
  30. {
  31. writer.openTag("class");
  32. writer.writeAttribute("name", _class->name);
  33. if(_class->docu_comment != "") {
  34. writer.writeTag("documentation");
  35. writer.write(_class->docu_comment);
  36. }
  37. for(std::vector<ClassMember*>::iterator i = _class->members.begin();
  38. i != _class->members.end(); ++i) {
  39. ClassMember* member = *i;
  40. if(member->visibility != ClassMember::PUBLIC)
  41. continue;
  42. Function* function = dynamic_cast<Function*> (member);
  43. if(!function)
  44. continue;
  45. if(function->type != Function::FUNCTION)
  46. continue;
  47. create_function_docu(_class, function);
  48. }
  49. writer.closeTag("class");
  50. }
  51. void
  52. DocuCreator::create_function_docu(Class* _class, Function* function)
  53. {
  54. writer.openTag("function");
  55. writer.writeAttribute("return_type",
  56. get_type(function->return_type));
  57. writer.writeAttribute("name", function->name);
  58. if(function->docu_comment != "") {
  59. writer.writeTag("documentation");
  60. writer.write(function->docu_comment);
  61. }
  62. for(std::vector<Parameter>::iterator p = function->parameters.begin();
  63. p != function->parameters.end(); ++p) {
  64. if(p == function->parameters.begin()
  65. && p->type.atomic_type == HSQUIRRELVMType::instance())
  66. continue;
  67. writer.writeTag("param");
  68. writer.writeAttribute("type", get_type(p->type));
  69. writer.writeAttribute("name", p->name);
  70. }
  71. writer.closeTag("function");
  72. }
  73. std::string
  74. DocuCreator::get_type(const Type& type)
  75. {
  76. if(type.ref > 0 && type.atomic_type != StringType::instance())
  77. throw std::runtime_error("References not handled yet");
  78. if(type.pointer > 0)
  79. throw std::runtime_error("Pointers not handled yet");
  80. if(type.atomic_type == &BasicType::VOID) {
  81. return "void";
  82. } else if(type.atomic_type == &BasicType::INT) {
  83. return "int";
  84. } else if(type.atomic_type == &BasicType::FLOAT) {
  85. return "float";
  86. } else if(type.atomic_type == &BasicType::BOOL) {
  87. return "bool";
  88. } else if(type.atomic_type == StringType::instance()) {
  89. return "string";
  90. }
  91. std::ostringstream msg;
  92. msg << "Type '" << type.atomic_type->name << "' not supported yet.";
  93. throw std::runtime_error(msg.str());
  94. }