gdscript_docgen.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 p_path.trim_prefix("res://").quote();
  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 an 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 an 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 void _doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return = false) {
  53. if (!p_gdtype.is_hard_type()) {
  54. r_type = "Variant";
  55. return;
  56. }
  57. switch (p_gdtype.kind) {
  58. case GDType::BUILTIN:
  59. if (p_gdtype.builtin_type == Variant::NIL) {
  60. r_type = p_is_return ? "void" : "null";
  61. return;
  62. }
  63. if (p_gdtype.builtin_type == Variant::ARRAY && p_gdtype.has_container_element_type()) {
  64. _doctype_from_gdtype(p_gdtype.get_container_element_type(), r_type, r_enum);
  65. if (!r_enum.is_empty()) {
  66. r_type = "int[]";
  67. r_enum += "[]";
  68. return;
  69. }
  70. if (!r_type.is_empty() && r_type != "Variant") {
  71. r_type += "[]";
  72. return;
  73. }
  74. }
  75. r_type = Variant::get_type_name(p_gdtype.builtin_type);
  76. return;
  77. case GDType::NATIVE:
  78. r_type = p_gdtype.native_type;
  79. return;
  80. case GDType::SCRIPT:
  81. if (p_gdtype.script_type.is_valid()) {
  82. if (p_gdtype.script_type->get_global_name() != StringName()) {
  83. r_type = p_gdtype.script_type->get_global_name();
  84. return;
  85. }
  86. if (!p_gdtype.script_type->get_path().is_empty()) {
  87. r_type = _get_script_path(p_gdtype.script_type->get_path());
  88. return;
  89. }
  90. }
  91. if (!p_gdtype.script_path.is_empty()) {
  92. r_type = _get_script_path(p_gdtype.script_path);
  93. return;
  94. }
  95. r_type = "Object";
  96. return;
  97. case GDType::CLASS:
  98. r_type = _get_class_name(*p_gdtype.class_type);
  99. return;
  100. case GDType::ENUM:
  101. r_type = "int";
  102. r_enum = String(p_gdtype.native_type).replace("::", ".");
  103. if (r_enum.begins_with("res://")) {
  104. r_enum = r_enum.trim_prefix("res://");
  105. int dot_pos = r_enum.rfind(".");
  106. if (dot_pos >= 0) {
  107. r_enum = r_enum.left(dot_pos).quote() + r_enum.substr(dot_pos);
  108. }
  109. }
  110. return;
  111. case GDType::VARIANT:
  112. case GDType::RESOLVING:
  113. case GDType::UNRESOLVED:
  114. r_type = "Variant";
  115. return;
  116. }
  117. }
  118. void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {
  119. p_script->_clear_doc();
  120. DocData::ClassDoc &doc = p_script->doc;
  121. doc.script_path = _get_script_path(p_script->get_script_path());
  122. if (p_script->local_name == StringName()) {
  123. doc.name = doc.script_path;
  124. } else {
  125. doc.name = p_script->local_name;
  126. }
  127. if (p_script->_owner) {
  128. doc.name = p_script->_owner->doc.name + "." + doc.name;
  129. doc.script_path = doc.script_path + "." + doc.name;
  130. }
  131. doc.is_script_doc = true;
  132. if (p_script->base.is_valid() && p_script->base->is_valid()) {
  133. if (!p_script->base->doc.name.is_empty()) {
  134. doc.inherits = p_script->base->doc.name;
  135. } else {
  136. doc.inherits = p_script->base->get_instance_base_type();
  137. }
  138. } else if (p_script->native.is_valid()) {
  139. doc.inherits = p_script->native->get_name();
  140. }
  141. doc.brief_description = p_class->doc_data.brief;
  142. doc.description = p_class->doc_data.description;
  143. for (const Pair<String, String> &p : p_class->doc_data.tutorials) {
  144. DocData::TutorialDoc td;
  145. td.title = p.first;
  146. td.link = p.second;
  147. doc.tutorials.append(td);
  148. }
  149. doc.is_deprecated = p_class->doc_data.is_deprecated;
  150. doc.is_experimental = p_class->doc_data.is_experimental;
  151. for (const GDP::ClassNode::Member &member : p_class->members) {
  152. switch (member.type) {
  153. case GDP::ClassNode::Member::CLASS: {
  154. const GDP::ClassNode *inner_class = member.m_class;
  155. const StringName &class_name = inner_class->identifier->name;
  156. p_script->member_lines[class_name] = inner_class->start_line;
  157. // Recursively generate inner class docs.
  158. // Needs inner GDScripts to exist: previously generated in GDScriptCompiler::make_scripts().
  159. GDScriptDocGen::generate_docs(*p_script->subclasses[class_name], inner_class);
  160. } break;
  161. case GDP::ClassNode::Member::CONSTANT: {
  162. const GDP::ConstantNode *m_const = member.constant;
  163. const StringName &const_name = member.constant->identifier->name;
  164. p_script->member_lines[const_name] = m_const->start_line;
  165. DocData::ConstantDoc const_doc;
  166. DocData::constant_doc_from_variant(const_doc, const_name, m_const->initializer->reduced_value, m_const->doc_data.description);
  167. const_doc.is_deprecated = m_const->doc_data.is_deprecated;
  168. const_doc.is_experimental = m_const->doc_data.is_experimental;
  169. doc.constants.push_back(const_doc);
  170. } break;
  171. case GDP::ClassNode::Member::FUNCTION: {
  172. const GDP::FunctionNode *m_func = member.function;
  173. const StringName &func_name = m_func->identifier->name;
  174. p_script->member_lines[func_name] = m_func->start_line;
  175. DocData::MethodDoc method_doc;
  176. method_doc.name = func_name;
  177. method_doc.description = m_func->doc_data.description;
  178. method_doc.is_deprecated = m_func->doc_data.is_deprecated;
  179. method_doc.is_experimental = m_func->doc_data.is_experimental;
  180. method_doc.qualifiers = m_func->is_static ? "static" : "";
  181. if (m_func->return_type) {
  182. _doctype_from_gdtype(m_func->return_type->get_datatype(), method_doc.return_type, method_doc.return_enum, true);
  183. } else if (!m_func->body->has_return) {
  184. // If no `return` statement, then return type is `void`, not `Variant`.
  185. method_doc.return_type = "void";
  186. } else {
  187. method_doc.return_type = "Variant";
  188. }
  189. for (const GDScriptParser::ParameterNode *p : m_func->parameters) {
  190. DocData::ArgumentDoc arg_doc;
  191. arg_doc.name = p->identifier->name;
  192. _doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration);
  193. if (p->initializer != nullptr) {
  194. if (p->initializer->is_constant) {
  195. arg_doc.default_value = p->initializer->reduced_value.get_construct_string().replace("\n", "\\n");
  196. } else {
  197. arg_doc.default_value = "<unknown>";
  198. }
  199. }
  200. method_doc.arguments.push_back(arg_doc);
  201. }
  202. doc.methods.push_back(method_doc);
  203. } break;
  204. case GDP::ClassNode::Member::SIGNAL: {
  205. const GDP::SignalNode *m_signal = member.signal;
  206. const StringName &signal_name = m_signal->identifier->name;
  207. p_script->member_lines[signal_name] = m_signal->start_line;
  208. DocData::MethodDoc signal_doc;
  209. signal_doc.name = signal_name;
  210. signal_doc.description = m_signal->doc_data.description;
  211. signal_doc.is_deprecated = m_signal->doc_data.is_deprecated;
  212. signal_doc.is_experimental = m_signal->doc_data.is_experimental;
  213. for (const GDScriptParser::ParameterNode *p : m_signal->parameters) {
  214. DocData::ArgumentDoc arg_doc;
  215. arg_doc.name = p->identifier->name;
  216. _doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration);
  217. signal_doc.arguments.push_back(arg_doc);
  218. }
  219. doc.signals.push_back(signal_doc);
  220. } break;
  221. case GDP::ClassNode::Member::VARIABLE: {
  222. const GDP::VariableNode *m_var = member.variable;
  223. const StringName &var_name = m_var->identifier->name;
  224. p_script->member_lines[var_name] = m_var->start_line;
  225. DocData::PropertyDoc prop_doc;
  226. prop_doc.name = var_name;
  227. prop_doc.description = m_var->doc_data.description;
  228. prop_doc.is_deprecated = m_var->doc_data.is_deprecated;
  229. prop_doc.is_experimental = m_var->doc_data.is_experimental;
  230. _doctype_from_gdtype(m_var->get_datatype(), prop_doc.type, prop_doc.enumeration);
  231. switch (m_var->property) {
  232. case GDP::VariableNode::PROP_NONE:
  233. break;
  234. case GDP::VariableNode::PROP_INLINE:
  235. if (m_var->setter != nullptr) {
  236. prop_doc.setter = m_var->setter->identifier->name;
  237. }
  238. if (m_var->getter != nullptr) {
  239. prop_doc.getter = m_var->getter->identifier->name;
  240. }
  241. break;
  242. case GDP::VariableNode::PROP_SETGET:
  243. if (m_var->setter_pointer != nullptr) {
  244. prop_doc.setter = m_var->setter_pointer->name;
  245. }
  246. if (m_var->getter_pointer != nullptr) {
  247. prop_doc.getter = m_var->getter_pointer->name;
  248. }
  249. break;
  250. }
  251. if (m_var->initializer) {
  252. if (m_var->initializer->is_constant) {
  253. prop_doc.default_value = m_var->initializer->reduced_value.get_construct_string().replace("\n", "\\n");
  254. } else {
  255. prop_doc.default_value = "<unknown>";
  256. }
  257. }
  258. prop_doc.overridden = false;
  259. doc.properties.push_back(prop_doc);
  260. } break;
  261. case GDP::ClassNode::Member::ENUM: {
  262. const GDP::EnumNode *m_enum = member.m_enum;
  263. StringName name = m_enum->identifier->name;
  264. p_script->member_lines[name] = m_enum->start_line;
  265. DocData::EnumDoc enum_doc;
  266. enum_doc.description = m_enum->doc_data.description;
  267. enum_doc.is_deprecated = m_enum->doc_data.is_deprecated;
  268. enum_doc.is_experimental = m_enum->doc_data.is_experimental;
  269. doc.enums[name] = enum_doc;
  270. for (const GDP::EnumNode::Value &val : m_enum->values) {
  271. DocData::ConstantDoc const_doc;
  272. const_doc.name = val.identifier->name;
  273. const_doc.value = String(Variant(val.value));
  274. const_doc.is_value_valid = true;
  275. const_doc.enumeration = name;
  276. const_doc.description = val.doc_data.description;
  277. const_doc.is_deprecated = val.doc_data.is_deprecated;
  278. const_doc.is_experimental = val.doc_data.is_experimental;
  279. doc.constants.push_back(const_doc);
  280. }
  281. } break;
  282. case GDP::ClassNode::Member::ENUM_VALUE: {
  283. const GDP::EnumNode::Value &m_enum_val = member.enum_value;
  284. const StringName &name = m_enum_val.identifier->name;
  285. p_script->member_lines[name] = m_enum_val.identifier->start_line;
  286. DocData::ConstantDoc const_doc;
  287. DocData::constant_doc_from_variant(const_doc, name, m_enum_val.value, m_enum_val.doc_data.description);
  288. const_doc.enumeration = "@unnamed_enums";
  289. const_doc.is_deprecated = m_enum_val.doc_data.is_deprecated;
  290. const_doc.is_experimental = m_enum_val.doc_data.is_experimental;
  291. doc.constants.push_back(const_doc);
  292. } break;
  293. default:
  294. break;
  295. }
  296. }
  297. // Add doc to the outer-most class.
  298. p_script->_add_doc(doc);
  299. }