Generator.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifndef GENERATOR_H_
  2. #define GENERATOR_H_
  3. #include "Base.h"
  4. #include "ClassBinding.h"
  5. #include "EnumBinding.h"
  6. #include "FunctionBinding.h"
  7. #include "Generator.h"
  8. #include "TypedefBinding.h"
  9. #include <tinyxml2.h>
  10. using namespace tinyxml2;
  11. /**
  12. * Generates script bindings.
  13. */
  14. class Generator
  15. {
  16. public:
  17. /**
  18. * Retrieves the global instance of the generator.
  19. *
  20. * @return The global instance of the generator.
  21. */
  22. static Generator* getInstance();
  23. /**
  24. * Releases the global instance of the generator.
  25. */
  26. static void releaseInstance();
  27. /**
  28. * Retrieves the unique name for the given identifier (class, struct, enum, etc.).
  29. *
  30. * @param name The identifier's name.
  31. * @return The unique name.
  32. */
  33. static string getUniqueName(string name);
  34. /**
  35. * Retrieves the unique name for the given ref id.
  36. *
  37. * @param refId The ref id.
  38. * @return The unique name.
  39. */
  40. string getUniqueNameFromRef(string refId);
  41. /**
  42. * Gets the scope path for a given fully resolved classname.
  43. *
  44. * @param classname The class name.
  45. * @param ns The namespace of the class if it has one.
  46. * @return The scope path.
  47. */
  48. static vector<string> getScopePath(string classname, string ns = string());
  49. /**
  50. * Retrieves the class/struct/enum name for the given ref id.
  51. *
  52. * @param refId The ref id.
  53. * @return The class/struct/enum name.
  54. */
  55. string getIdentifier(string refId);
  56. /**
  57. * Gets the given class name (without the namespace) and stores the class's namespace in the given string pointer (if non-null).
  58. *
  59. * @param classname The fully qualified name of the class.
  60. * @param ns Output variable for the namespace of the class.
  61. * @return The class name without the namespace.
  62. */
  63. string getClassNameAndNamespace(string classname, string* ns);
  64. /**
  65. * Sets the given ref id and class/struct/enum name pair.
  66. *
  67. * @param refId The ref id.
  68. * @param classname The class/struct/enum name.
  69. */
  70. void setIdentifier(string refId, string classname);
  71. /**
  72. * Generates Lua bindings for all classes defined within the given input directory
  73. * and places the generated class bindings in the given output directory.
  74. * Note that the input directory must point to a folder containing the Doxygen XML
  75. * output files for the classes that the user wants to generate bindings for.
  76. *
  77. * @param inDir The input directory (must contain Doxygen XML output files).
  78. * @param outDir The output directory.
  79. * @param bindingNS The namespace to generate the bindings within.
  80. */
  81. void run(string inDir, string outDir, string* bindingNS = NULL);
  82. /**
  83. * Retrieves whether the given class is derived from Ref.
  84. *
  85. * @param classname The name of the class.
  86. * @return True if the class is derived from Ref; false otherwise.
  87. */
  88. bool isRef(string classname);
  89. protected:
  90. /**
  91. * Constructor.
  92. */
  93. Generator();
  94. /**
  95. * Destructor.
  96. */
  97. ~Generator();
  98. // Checks if the given class name specifies a class derived from the class represented by the given class binding.
  99. bool isDerived(const ClassBinding& c, string classname);
  100. // Gets the name of the namespace, file, class, or struct.
  101. string getCompoundName(XMLElement* node);
  102. // Parses the non-member functions and adds them to the list (optionally within the given namespace).
  103. void getFunctions(XMLElement* classNode, string ns = "");
  104. // Parses the namespace (with the given name).
  105. void getNamespace(XMLElement* nsNode, const string& name);
  106. // Parses the class (with the given name) and adds it to the list.
  107. void getClass(XMLElement* classNode, const string& name);
  108. // Parses all enumerations within a file (optionally within a namespace).
  109. void getEnums(XMLElement* fileNode, string ns = "");
  110. // Parses the enumeration type and adds it to the global list.
  111. void getEnum(XMLElement* e, string classname = "", string ns = "", string include = "");
  112. // Parses all typedefs within a file (optionall within a namespace).
  113. void getTypedefs(XMLElement* fileNode, string ns = "");
  114. // Parses the typedef and adds it to the global list.
  115. void getTypedef(XMLElement* e, string classname = "", string ns = "");
  116. // Gets the script flag if it is specified.
  117. string getScriptFlag(XMLElement* e);
  118. // Gets whether the current function has been marked as a creation function
  119. // (indicating that the return value is owned by the script runtime and not C++).
  120. void getCreateFlag(XMLElement* e, FunctionBinding& b);
  121. // Gets the name of the binding.
  122. string getName(XMLElement* e);
  123. // Gets whether the type is const or not.
  124. bool getIsConstType(XMLElement* e);
  125. // Gets the parameter or return value, optionally within the scope of the given class.
  126. FunctionBinding::Param getParam(XMLElement* e, bool isVariable = false, string classname = "");
  127. // Gets all of the parameters for a function.
  128. void getParams(XMLElement* e, FunctionBinding& b);
  129. // Resolves members for all classes' derived from class 'c'.
  130. void resolveMembers(const ClassBinding& c);
  131. // Resolves the inheritance.
  132. void resolveInheritance();
  133. // Resolves includes for all classes' derived from class 'c'.
  134. void resolveIncludes(const ClassBinding& c);
  135. // Resolves inherited include files.
  136. void resolveInheritedIncludes();
  137. // Resolves the type for a single parameter/return value
  138. // (also takes the function that the parameter/return value is for along
  139. // with the header file that the parameter/return value belongs to-the original
  140. // class header file for class bindings or the global Lua bindings header file
  141. // for global bindings).
  142. void resolveType(FunctionBinding::Param* param, string functionName, string header);
  143. // Resolves all unrecognized types.
  144. void resolveTypes();
  145. // Generates the bindings to C++ header and source files.
  146. void generateBindings(string* bindingNS);
  147. // Gets the set off all classes that derives from the given class.
  148. void getAllDerived(set<string>& derived, string classname);
  149. // Gets the included files for a cpp file.
  150. void getIncludes(XMLElement* e, string filename);
  151. private:
  152. static Generator* __instance;
  153. const char* _file;
  154. map<string, string> _refIds;
  155. string _outDir;
  156. map<string, ClassBinding> _classes;
  157. vector<string> _topLevelBaseClasses;
  158. map<string, set<string> > _includes;
  159. map<string, vector<FunctionBinding> > _functions;
  160. map<string, EnumBinding> _enums;
  161. map<string, set<string> > _namespaces;
  162. map<string, TypedefBinding> _typedefs;
  163. set<string> __warnings;
  164. };
  165. #endif