gdscript_docgen.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_data.brief;
  91. doc.description = p_class->doc_data.description;
  92. for (const Pair<String, String> &p : p_class->doc_data.tutorials) {
  93. DocData::TutorialDoc td;
  94. td.title = p.first;
  95. td.link = p.second;
  96. doc.tutorials.append(td);
  97. }
  98. doc.is_deprecated = p_class->doc_data.is_deprecated;
  99. doc.is_experimental = p_class->doc_data.is_experimental;
  100. for (const GDP::ClassNode::Member &member : p_class->members) {
  101. switch (member.type) {
  102. case GDP::ClassNode::Member::CLASS: {
  103. const GDP::ClassNode *inner_class = member.m_class;
  104. const StringName &class_name = inner_class->identifier->name;
  105. p_script->member_lines[class_name] = inner_class->start_line;
  106. // Recursively generate inner class docs
  107. // Needs inner GDScripts to exist: previously generated in GDScriptCompiler::make_scripts()
  108. GDScriptDocGen::generate_docs(*p_script->subclasses[class_name], inner_class);
  109. } break;
  110. case GDP::ClassNode::Member::CONSTANT: {
  111. const GDP::ConstantNode *m_const = member.constant;
  112. const StringName &const_name = member.constant->identifier->name;
  113. p_script->member_lines[const_name] = m_const->start_line;
  114. DocData::ConstantDoc const_doc;
  115. DocData::constant_doc_from_variant(const_doc, const_name, m_const->initializer->reduced_value, m_const->doc_data.description);
  116. const_doc.is_deprecated = m_const->doc_data.is_deprecated;
  117. const_doc.is_experimental = m_const->doc_data.is_experimental;
  118. doc.constants.push_back(const_doc);
  119. } break;
  120. case GDP::ClassNode::Member::FUNCTION: {
  121. const GDP::FunctionNode *m_func = member.function;
  122. const StringName &func_name = m_func->identifier->name;
  123. p_script->member_lines[func_name] = m_func->start_line;
  124. MethodInfo mi;
  125. mi.name = func_name;
  126. if (m_func->return_type) {
  127. mi.return_val = _property_info_from_datatype(m_func->return_type->get_datatype());
  128. }
  129. for (const GDScriptParser::ParameterNode *p : m_func->parameters) {
  130. PropertyInfo pi = _property_info_from_datatype(p->get_datatype());
  131. pi.name = p->identifier->name;
  132. mi.arguments.push_back(pi);
  133. }
  134. DocData::MethodDoc method_doc;
  135. DocData::method_doc_from_methodinfo(method_doc, mi, m_func->doc_data.description);
  136. method_doc.is_deprecated = m_func->doc_data.is_deprecated;
  137. method_doc.is_experimental = m_func->doc_data.is_experimental;
  138. doc.methods.push_back(method_doc);
  139. } break;
  140. case GDP::ClassNode::Member::SIGNAL: {
  141. const GDP::SignalNode *m_signal = member.signal;
  142. const StringName &signal_name = m_signal->identifier->name;
  143. p_script->member_lines[signal_name] = m_signal->start_line;
  144. MethodInfo mi;
  145. mi.name = signal_name;
  146. for (const GDScriptParser::ParameterNode *p : m_signal->parameters) {
  147. PropertyInfo pi = _property_info_from_datatype(p->get_datatype());
  148. pi.name = p->identifier->name;
  149. mi.arguments.push_back(pi);
  150. }
  151. DocData::MethodDoc signal_doc;
  152. DocData::signal_doc_from_methodinfo(signal_doc, mi, m_signal->doc_data.description);
  153. signal_doc.is_deprecated = m_signal->doc_data.is_deprecated;
  154. signal_doc.is_experimental = m_signal->doc_data.is_experimental;
  155. doc.signals.push_back(signal_doc);
  156. } break;
  157. case GDP::ClassNode::Member::VARIABLE: {
  158. const GDP::VariableNode *m_var = member.variable;
  159. const StringName &var_name = m_var->identifier->name;
  160. p_script->member_lines[var_name] = m_var->start_line;
  161. DocData::PropertyDoc prop_doc;
  162. prop_doc.name = var_name;
  163. prop_doc.description = m_var->doc_data.description;
  164. prop_doc.is_deprecated = m_var->doc_data.is_deprecated;
  165. prop_doc.is_experimental = m_var->doc_data.is_experimental;
  166. GDType dt = m_var->get_datatype();
  167. switch (dt.kind) {
  168. case GDType::CLASS:
  169. prop_doc.type = _get_class_name(*dt.class_type);
  170. break;
  171. case GDType::VARIANT:
  172. prop_doc.type = "Variant";
  173. break;
  174. case GDType::ENUM:
  175. prop_doc.type = Variant::get_type_name(dt.builtin_type);
  176. // Replace :: from enum's use of fully qualified class names with regular .
  177. prop_doc.enumeration = String(dt.native_type).replace("::", ".");
  178. break;
  179. case GDType::NATIVE:;
  180. prop_doc.type = dt.native_type;
  181. break;
  182. case GDType::BUILTIN:
  183. prop_doc.type = Variant::get_type_name(dt.builtin_type);
  184. break;
  185. default:
  186. // SCRIPT: can be preload()'d and perhaps used as types directly?
  187. // RESOLVING & UNRESOLVED should never happen since docgen requires analyzing w/o errors
  188. break;
  189. }
  190. if (m_var->property == GDP::VariableNode::PROP_SETGET) {
  191. if (m_var->setter_pointer != nullptr) {
  192. prop_doc.setter = m_var->setter_pointer->name;
  193. }
  194. if (m_var->getter_pointer != nullptr) {
  195. prop_doc.getter = m_var->getter_pointer->name;
  196. }
  197. }
  198. if (m_var->initializer && m_var->initializer->is_constant) {
  199. prop_doc.default_value = m_var->initializer->reduced_value.get_construct_string().replace("\n", "");
  200. }
  201. prop_doc.overridden = false;
  202. doc.properties.push_back(prop_doc);
  203. } break;
  204. case GDP::ClassNode::Member::ENUM: {
  205. const GDP::EnumNode *m_enum = member.m_enum;
  206. StringName name = m_enum->identifier->name;
  207. p_script->member_lines[name] = m_enum->start_line;
  208. DocData::EnumDoc enum_doc;
  209. enum_doc.description = m_enum->doc_data.description;
  210. enum_doc.is_deprecated = m_enum->doc_data.is_deprecated;
  211. enum_doc.is_experimental = m_enum->doc_data.is_experimental;
  212. doc.enums[name] = enum_doc;
  213. for (const GDP::EnumNode::Value &val : m_enum->values) {
  214. DocData::ConstantDoc const_doc;
  215. const_doc.name = val.identifier->name;
  216. const_doc.value = String(Variant(val.value));
  217. const_doc.is_value_valid = true;
  218. const_doc.enumeration = name;
  219. const_doc.description = val.doc_data.description;
  220. const_doc.is_deprecated = val.doc_data.is_deprecated;
  221. const_doc.is_experimental = val.doc_data.is_experimental;
  222. doc.constants.push_back(const_doc);
  223. }
  224. } break;
  225. case GDP::ClassNode::Member::ENUM_VALUE: {
  226. const GDP::EnumNode::Value &m_enum_val = member.enum_value;
  227. const StringName &name = m_enum_val.identifier->name;
  228. p_script->member_lines[name] = m_enum_val.identifier->start_line;
  229. DocData::ConstantDoc const_doc;
  230. DocData::constant_doc_from_variant(const_doc, name, m_enum_val.value, m_enum_val.doc_data.description);
  231. const_doc.enumeration = "@unnamed_enums";
  232. const_doc.is_deprecated = m_enum_val.doc_data.is_deprecated;
  233. const_doc.is_experimental = m_enum_val.doc_data.is_experimental;
  234. doc.constants.push_back(const_doc);
  235. } break;
  236. case GDP::ClassNode::Member::GROUP:
  237. case GDP::ClassNode::Member::UNDEFINED:
  238. default:
  239. break;
  240. }
  241. }
  242. // Add doc to the outer-most class.
  243. p_script->_add_doc(doc);
  244. }