gdscript_docgen.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**************************************************************************/
  2. /* gdscript_docgen.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gdscript_docgen.h"
  31. #include "../gdscript.h"
  32. using GDP = GDScriptParser;
  33. using GDType = GDP::DataType;
  34. static String _get_script_path(const String &p_path) {
  35. return vformat(R"("%s")", p_path.get_slice("://", 1));
  36. }
  37. static String _get_class_name(const GDP::ClassNode &p_class) {
  38. const GDP::ClassNode *curr_class = &p_class;
  39. if (!curr_class->identifier) { // All inner classes have a identifier, so this is the outer class
  40. return _get_script_path(curr_class->fqcn);
  41. }
  42. String full_name = curr_class->identifier->name;
  43. while (curr_class->outer) {
  44. curr_class = curr_class->outer;
  45. if (!curr_class->identifier) { // All inner classes have a identifier, so this is the outer class
  46. return vformat("%s.%s", _get_script_path(curr_class->fqcn), full_name);
  47. }
  48. full_name = vformat("%s.%s", curr_class->identifier->name, full_name);
  49. }
  50. return full_name;
  51. }
  52. static PropertyInfo _property_info_from_datatype(const GDType &p_type) {
  53. PropertyInfo pi;
  54. pi.type = p_type.builtin_type;
  55. if (p_type.kind == GDType::CLASS) {
  56. pi.class_name = _get_class_name(*p_type.class_type);
  57. } else if (p_type.kind == GDType::ENUM && p_type.enum_type != StringName()) {
  58. pi.type = Variant::INT; // Only int types are recognized as enums by the EditorHelp
  59. pi.usage |= PROPERTY_USAGE_CLASS_IS_ENUM;
  60. // Replace :: from enum's use of fully qualified class names with regular .
  61. pi.class_name = String(p_type.native_type).replace("::", ".");
  62. } else if (p_type.kind == GDType::NATIVE) {
  63. pi.class_name = p_type.native_type;
  64. }
  65. return pi;
  66. }
  67. void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {
  68. p_script->_clear_doc();
  69. DocData::ClassDoc &doc = p_script->doc;
  70. doc.script_path = _get_script_path(p_script->get_script_path());
  71. if (p_script->name.is_empty()) {
  72. doc.name = doc.script_path;
  73. } else {
  74. doc.name = p_script->name;
  75. }
  76. if (p_script->_owner) {
  77. doc.name = p_script->_owner->doc.name + "." + doc.name;
  78. doc.script_path = doc.script_path + "." + doc.name;
  79. }
  80. doc.is_script_doc = true;
  81. if (p_script->base.is_valid() && p_script->base->is_valid()) {
  82. if (!p_script->base->doc.name.is_empty()) {
  83. doc.inherits = p_script->base->doc.name;
  84. } else {
  85. doc.inherits = p_script->base->get_instance_base_type();
  86. }
  87. } else if (p_script->native.is_valid()) {
  88. doc.inherits = p_script->native->get_name();
  89. }
  90. doc.brief_description = p_class->doc_brief_description;
  91. doc.description = p_class->doc_description;
  92. for (const Pair<String, String> &p : p_class->doc_tutorials) {
  93. DocData::TutorialDoc td;
  94. td.title = p.first;
  95. td.link = p.second;
  96. doc.tutorials.append(td);
  97. }
  98. for (const GDP::ClassNode::Member &member : p_class->members) {
  99. switch (member.type) {
  100. case GDP::ClassNode::Member::CLASS: {
  101. const GDP::ClassNode *inner_class = member.m_class;
  102. const StringName &class_name = inner_class->identifier->name;
  103. p_script->member_lines[class_name] = inner_class->start_line;
  104. // Recursively generate inner class docs
  105. // Needs inner GDScripts to exist: previously generated in GDScriptCompiler::make_scripts()
  106. GDScriptDocGen::generate_docs(*p_script->subclasses[class_name], inner_class);
  107. } break;
  108. case GDP::ClassNode::Member::CONSTANT: {
  109. const GDP::ConstantNode *m_const = member.constant;
  110. const StringName &const_name = member.constant->identifier->name;
  111. p_script->member_lines[const_name] = m_const->start_line;
  112. DocData::ConstantDoc const_doc;
  113. DocData::constant_doc_from_variant(const_doc, const_name, m_const->initializer->reduced_value, m_const->doc_description);
  114. doc.constants.push_back(const_doc);
  115. } break;
  116. case GDP::ClassNode::Member::FUNCTION: {
  117. const GDP::FunctionNode *m_func = member.function;
  118. const StringName &func_name = m_func->identifier->name;
  119. p_script->member_lines[func_name] = m_func->start_line;
  120. MethodInfo mi;
  121. mi.name = func_name;
  122. if (m_func->return_type) {
  123. mi.return_val = _property_info_from_datatype(m_func->return_type->get_datatype());
  124. }
  125. for (const GDScriptParser::ParameterNode *p : m_func->parameters) {
  126. PropertyInfo pi = _property_info_from_datatype(p->get_datatype());
  127. pi.name = p->identifier->name;
  128. mi.arguments.push_back(pi);
  129. }
  130. DocData::MethodDoc method_doc;
  131. DocData::method_doc_from_methodinfo(method_doc, mi, m_func->doc_description);
  132. doc.methods.push_back(method_doc);
  133. } break;
  134. case GDP::ClassNode::Member::SIGNAL: {
  135. const GDP::SignalNode *m_signal = member.signal;
  136. const StringName &signal_name = m_signal->identifier->name;
  137. p_script->member_lines[signal_name] = m_signal->start_line;
  138. MethodInfo mi;
  139. mi.name = signal_name;
  140. for (const GDScriptParser::ParameterNode *p : m_signal->parameters) {
  141. PropertyInfo pi = _property_info_from_datatype(p->get_datatype());
  142. pi.name = p->identifier->name;
  143. mi.arguments.push_back(pi);
  144. }
  145. DocData::MethodDoc signal_doc;
  146. DocData::signal_doc_from_methodinfo(signal_doc, mi, m_signal->doc_description);
  147. doc.signals.push_back(signal_doc);
  148. } break;
  149. case GDP::ClassNode::Member::VARIABLE: {
  150. const GDP::VariableNode *m_var = member.variable;
  151. const StringName &var_name = m_var->identifier->name;
  152. p_script->member_lines[var_name] = m_var->start_line;
  153. DocData::PropertyDoc prop_doc;
  154. prop_doc.name = var_name;
  155. prop_doc.description = m_var->doc_description;
  156. GDType dt = m_var->get_datatype();
  157. switch (dt.kind) {
  158. case GDType::CLASS:
  159. prop_doc.type = _get_class_name(*dt.class_type);
  160. break;
  161. case GDType::VARIANT:
  162. prop_doc.type = "Variant";
  163. break;
  164. case GDType::ENUM:
  165. prop_doc.type = Variant::get_type_name(dt.builtin_type);
  166. // Replace :: from enum's use of fully qualified class names with regular .
  167. prop_doc.enumeration = String(dt.native_type).replace("::", ".");
  168. break;
  169. case GDType::NATIVE:;
  170. prop_doc.type = dt.native_type;
  171. break;
  172. case GDType::BUILTIN:
  173. prop_doc.type = Variant::get_type_name(dt.builtin_type);
  174. break;
  175. default:
  176. // SCRIPT: can be preload()'d and perhaps used as types directly?
  177. // RESOLVING & UNRESOLVED should never happen since docgen requires analyzing w/o errors
  178. break;
  179. }
  180. if (m_var->property == GDP::VariableNode::PROP_SETGET) {
  181. if (m_var->setter_pointer != nullptr) {
  182. prop_doc.setter = m_var->setter_pointer->name;
  183. }
  184. if (m_var->getter_pointer != nullptr) {
  185. prop_doc.getter = m_var->getter_pointer->name;
  186. }
  187. }
  188. if (m_var->initializer && m_var->initializer->is_constant) {
  189. prop_doc.default_value = m_var->initializer->reduced_value.get_construct_string().replace("\n", "");
  190. }
  191. prop_doc.overridden = false;
  192. doc.properties.push_back(prop_doc);
  193. } break;
  194. case GDP::ClassNode::Member::ENUM: {
  195. const GDP::EnumNode *m_enum = member.m_enum;
  196. StringName name = m_enum->identifier->name;
  197. p_script->member_lines[name] = m_enum->start_line;
  198. doc.enums[name] = m_enum->doc_description;
  199. for (const GDP::EnumNode::Value &val : m_enum->values) {
  200. DocData::ConstantDoc const_doc;
  201. const_doc.name = val.identifier->name;
  202. const_doc.value = String(Variant(val.value));
  203. const_doc.is_value_valid = true;
  204. const_doc.description = val.doc_description;
  205. const_doc.enumeration = name;
  206. doc.constants.push_back(const_doc);
  207. }
  208. } break;
  209. case GDP::ClassNode::Member::ENUM_VALUE: {
  210. const GDP::EnumNode::Value &m_enum_val = member.enum_value;
  211. const StringName &name = m_enum_val.identifier->name;
  212. p_script->member_lines[name] = m_enum_val.identifier->start_line;
  213. DocData::ConstantDoc constant_doc;
  214. constant_doc.enumeration = "@unnamed_enums";
  215. DocData::constant_doc_from_variant(constant_doc, name, m_enum_val.value, m_enum_val.doc_description);
  216. doc.constants.push_back(constant_doc);
  217. } break;
  218. case GDP::ClassNode::Member::GROUP:
  219. case GDP::ClassNode::Member::UNDEFINED:
  220. default:
  221. break;
  222. }
  223. }
  224. // Add doc to the outer-most class.
  225. p_script->_add_doc(doc);
  226. }