create_docu.cpp 3.2 KB

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