Path: blob/master/modules/gdscript/editor/gdscript_docgen.cpp
10278 views
/**************************************************************************/1/* gdscript_docgen.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gdscript_docgen.h"3132#include "../gdscript.h"3334#include "core/config/project_settings.h"3536HashMap<String, String> GDScriptDocGen::singletons;3738String GDScriptDocGen::_get_script_name(const String &p_path) {39const HashMap<String, String>::ConstIterator E = singletons.find(p_path);40if (E) {41return E->value;42}43return p_path.trim_prefix("res://").quote();44}4546String GDScriptDocGen::_get_class_name(const GDP::ClassNode &p_class) {47const GDP::ClassNode *curr_class = &p_class;48if (!curr_class->identifier) { // All inner classes have an identifier, so this is the outer class.49return _get_script_name(curr_class->fqcn);50}5152String full_name = curr_class->identifier->name;53while (curr_class->outer) {54curr_class = curr_class->outer;55if (!curr_class->identifier) { // All inner classes have an identifier, so this is the outer class.56return vformat("%s.%s", _get_script_name(curr_class->fqcn), full_name);57}58full_name = vformat("%s.%s", curr_class->identifier->name, full_name);59}60return full_name;61}6263void GDScriptDocGen::_doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return) {64if (!p_gdtype.is_hard_type()) {65r_type = "Variant";66return;67}68switch (p_gdtype.kind) {69case GDType::BUILTIN:70if (p_gdtype.builtin_type == Variant::NIL) {71r_type = p_is_return ? "void" : "null";72return;73}74if (p_gdtype.builtin_type == Variant::ARRAY && p_gdtype.has_container_element_type(0)) {75_doctype_from_gdtype(p_gdtype.get_container_element_type(0), r_type, r_enum);76if (!r_enum.is_empty()) {77r_type = "int[]";78r_enum += "[]";79return;80}81if (!r_type.is_empty() && r_type != "Variant") {82r_type += "[]";83return;84}85}86if (p_gdtype.builtin_type == Variant::DICTIONARY && p_gdtype.has_container_element_types()) {87String key, value;88_doctype_from_gdtype(p_gdtype.get_container_element_type_or_variant(0), key, r_enum);89_doctype_from_gdtype(p_gdtype.get_container_element_type_or_variant(1), value, r_enum);90if (key != "Variant" || value != "Variant") {91r_type = "Dictionary[" + key + ", " + value + "]";92return;93}94}95r_type = Variant::get_type_name(p_gdtype.builtin_type);96return;97case GDType::NATIVE:98if (p_gdtype.is_meta_type) {99//r_type = GDScriptNativeClass::get_class_static();100r_type = "Object"; // "GDScriptNativeClass" refers to a blank page.101return;102}103r_type = p_gdtype.native_type;104return;105case GDType::SCRIPT:106if (p_gdtype.is_meta_type) {107r_type = p_gdtype.script_type.is_valid() ? p_gdtype.script_type->get_class_name() : Script::get_class_static();108return;109}110if (p_gdtype.script_type.is_valid()) {111if (p_gdtype.script_type->get_global_name() != StringName()) {112r_type = p_gdtype.script_type->get_global_name();113return;114}115if (!p_gdtype.script_type->get_path().is_empty()) {116r_type = _get_script_name(p_gdtype.script_type->get_path());117return;118}119}120if (!p_gdtype.script_path.is_empty()) {121r_type = _get_script_name(p_gdtype.script_path);122return;123}124r_type = "Object";125return;126case GDType::CLASS:127if (p_gdtype.is_meta_type) {128r_type = GDScript::get_class_static();129return;130}131r_type = _get_class_name(*p_gdtype.class_type);132return;133case GDType::ENUM:134if (p_gdtype.is_meta_type) {135r_type = "Dictionary";136return;137}138r_type = "int";139r_enum = String(p_gdtype.native_type).replace("::", ".");140if (r_enum.begins_with("res://")) {141int dot_pos = r_enum.rfind_char('.');142if (dot_pos >= 0) {143r_enum = _get_script_name(r_enum.left(dot_pos)) + r_enum.substr(dot_pos);144} else {145r_enum = _get_script_name(r_enum);146}147}148return;149case GDType::VARIANT:150case GDType::RESOLVING:151case GDType::UNRESOLVED:152r_type = "Variant";153return;154}155}156157String GDScriptDocGen::_docvalue_from_variant(const Variant &p_variant, int p_recursion_level) {158constexpr int MAX_RECURSION_LEVEL = 2;159160switch (p_variant.get_type()) {161case Variant::STRING:162return String(p_variant).c_escape().quote();163case Variant::OBJECT:164return "<Object>";165case Variant::DICTIONARY: {166const Dictionary dict = p_variant;167String result;168169if (dict.is_typed()) {170result += "Dictionary[";171172Ref<Script> key_script = dict.get_typed_key_script();173if (key_script.is_valid()) {174if (key_script->get_global_name() != StringName()) {175result += key_script->get_global_name();176} else if (!key_script->get_path().get_file().is_empty()) {177result += key_script->get_path().get_file();178} else {179result += dict.get_typed_key_class_name();180}181} else if (dict.get_typed_key_class_name() != StringName()) {182result += dict.get_typed_key_class_name();183} else if (dict.is_typed_key()) {184result += Variant::get_type_name((Variant::Type)dict.get_typed_key_builtin());185} else {186result += "Variant";187}188189result += ", ";190191Ref<Script> value_script = dict.get_typed_value_script();192if (value_script.is_valid()) {193if (value_script->get_global_name() != StringName()) {194result += value_script->get_global_name();195} else if (!value_script->get_path().get_file().is_empty()) {196result += value_script->get_path().get_file();197} else {198result += dict.get_typed_value_class_name();199}200} else if (dict.get_typed_value_class_name() != StringName()) {201result += dict.get_typed_value_class_name();202} else if (dict.is_typed_value()) {203result += Variant::get_type_name((Variant::Type)dict.get_typed_value_builtin());204} else {205result += "Variant";206}207208result += "](";209}210211if (dict.is_empty()) {212result += "{}";213} else if (p_recursion_level > MAX_RECURSION_LEVEL) {214result += "{...}";215} else {216result += "{";217218LocalVector<Variant> keys = dict.get_key_list();219keys.sort_custom<StringLikeVariantOrder>();220221for (uint32_t i = 0; i < keys.size(); i++) {222const Variant &key = keys[i];223if (i > 0) {224result += ", ";225}226result += _docvalue_from_variant(key, p_recursion_level + 1) + ": " + _docvalue_from_variant(dict[key], p_recursion_level + 1);227}228229result += "}";230}231232if (dict.is_typed()) {233result += ")";234}235236return result;237} break;238case Variant::ARRAY: {239const Array array = p_variant;240String result;241242if (array.is_typed()) {243result += "Array[";244245Ref<Script> script = array.get_typed_script();246if (script.is_valid()) {247if (script->get_global_name() != StringName()) {248result += script->get_global_name();249} else if (!script->get_path().get_file().is_empty()) {250result += script->get_path().get_file();251} else {252result += array.get_typed_class_name();253}254} else if (array.get_typed_class_name() != StringName()) {255result += array.get_typed_class_name();256} else {257result += Variant::get_type_name((Variant::Type)array.get_typed_builtin());258}259260result += "](";261}262263if (array.is_empty()) {264result += "[]";265} else if (p_recursion_level > MAX_RECURSION_LEVEL) {266result += "[...]";267} else {268result += "[";269270for (int i = 0; i < array.size(); i++) {271if (i > 0) {272result += ", ";273}274result += _docvalue_from_variant(array[i], p_recursion_level + 1);275}276277result += "]";278}279280if (array.is_typed()) {281result += ")";282}283284return result;285} break;286default:287return p_variant.get_construct_string();288}289}290291String GDScriptDocGen::docvalue_from_expression(const GDP::ExpressionNode *p_expression) {292ERR_FAIL_NULL_V(p_expression, String());293294if (p_expression->is_constant) {295return _docvalue_from_variant(p_expression->reduced_value);296}297298switch (p_expression->type) {299case GDP::Node::ARRAY: {300const GDP::ArrayNode *array = static_cast<const GDP::ArrayNode *>(p_expression);301return array->elements.is_empty() ? "[]" : "[...]";302} break;303case GDP::Node::CALL: {304const GDP::CallNode *call = static_cast<const GDP::CallNode *>(p_expression);305if (call->get_callee_type() == GDP::Node::IDENTIFIER) {306return call->function_name.operator String() + (call->arguments.is_empty() ? "()" : "(...)");307}308} break;309case GDP::Node::DICTIONARY: {310const GDP::DictionaryNode *dict = static_cast<const GDP::DictionaryNode *>(p_expression);311return dict->elements.is_empty() ? "{}" : "{...}";312} break;313case GDP::Node::IDENTIFIER: {314const GDP::IdentifierNode *id = static_cast<const GDP::IdentifierNode *>(p_expression);315return id->name;316} break;317default: {318// Nothing to do.319} break;320}321322return "<unknown>";323}324325void GDScriptDocGen::_generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {326p_script->_clear_doc();327328DocData::ClassDoc &doc = p_script->doc;329330doc.is_script_doc = true;331332if (p_script->local_name == StringName()) {333// This is an outer unnamed class.334doc.name = _get_script_name(p_script->get_script_path());335} else {336// This is an inner or global outer class.337doc.name = p_script->local_name;338if (p_script->_owner) {339doc.name = p_script->_owner->doc.name + "." + doc.name;340}341}342343doc.script_path = p_script->get_script_path();344345if (p_script->base.is_valid() && p_script->base->is_valid()) {346if (!p_script->base->doc.name.is_empty()) {347doc.inherits = p_script->base->doc.name;348} else {349doc.inherits = p_script->base->get_instance_base_type();350}351} else if (p_script->native.is_valid()) {352doc.inherits = p_script->native->get_name();353}354355doc.brief_description = p_class->doc_data.brief;356doc.description = p_class->doc_data.description;357for (const Pair<String, String> &p : p_class->doc_data.tutorials) {358DocData::TutorialDoc td;359td.title = p.first;360td.link = p.second;361doc.tutorials.append(td);362}363doc.is_deprecated = p_class->doc_data.is_deprecated;364doc.deprecated_message = p_class->doc_data.deprecated_message;365doc.is_experimental = p_class->doc_data.is_experimental;366doc.experimental_message = p_class->doc_data.experimental_message;367368for (const GDP::ClassNode::Member &member : p_class->members) {369switch (member.type) {370case GDP::ClassNode::Member::CLASS: {371const GDP::ClassNode *inner_class = member.m_class;372const StringName &class_name = inner_class->identifier->name;373374p_script->member_lines[class_name] = inner_class->start_line;375376// Recursively generate inner class docs.377// Needs inner GDScripts to exist: previously generated in GDScriptCompiler::make_scripts().378GDScriptDocGen::_generate_docs(*p_script->subclasses[class_name], inner_class);379} break;380381case GDP::ClassNode::Member::CONSTANT: {382const GDP::ConstantNode *m_const = member.constant;383const StringName &const_name = member.constant->identifier->name;384385p_script->member_lines[const_name] = m_const->start_line;386387DocData::ConstantDoc const_doc;388const_doc.name = const_name;389const_doc.value = _docvalue_from_variant(m_const->initializer->reduced_value);390const_doc.is_value_valid = true;391_doctype_from_gdtype(m_const->get_datatype(), const_doc.type, const_doc.enumeration);392const_doc.description = m_const->doc_data.description;393const_doc.is_deprecated = m_const->doc_data.is_deprecated;394const_doc.deprecated_message = m_const->doc_data.deprecated_message;395const_doc.is_experimental = m_const->doc_data.is_experimental;396const_doc.experimental_message = m_const->doc_data.experimental_message;397doc.constants.push_back(const_doc);398} break;399400case GDP::ClassNode::Member::FUNCTION: {401const GDP::FunctionNode *m_func = member.function;402const StringName &func_name = m_func->identifier->name;403404p_script->member_lines[func_name] = m_func->start_line;405406DocData::MethodDoc method_doc;407method_doc.name = func_name;408method_doc.description = m_func->doc_data.description;409method_doc.is_deprecated = m_func->doc_data.is_deprecated;410method_doc.deprecated_message = m_func->doc_data.deprecated_message;411method_doc.is_experimental = m_func->doc_data.is_experimental;412method_doc.experimental_message = m_func->doc_data.experimental_message;413414if (m_func->is_vararg()) {415if (!method_doc.qualifiers.is_empty()) {416method_doc.qualifiers += " ";417}418method_doc.qualifiers += "vararg";419method_doc.rest_argument.name = m_func->rest_parameter->identifier->name;420_doctype_from_gdtype(m_func->rest_parameter->get_datatype(), method_doc.rest_argument.type, method_doc.rest_argument.enumeration);421}422if (m_func->is_abstract) {423if (!method_doc.qualifiers.is_empty()) {424method_doc.qualifiers += " ";425}426method_doc.qualifiers += "abstract";427}428if (m_func->is_static) {429if (!method_doc.qualifiers.is_empty()) {430method_doc.qualifiers += " ";431}432method_doc.qualifiers += "static";433}434435if (func_name == "_init") {436method_doc.return_type = "void";437} else if (m_func->return_type) {438// `m_func->return_type->get_datatype()` is a metatype.439_doctype_from_gdtype(m_func->get_datatype(), method_doc.return_type, method_doc.return_enum, true);440} else if (!m_func->body->has_return) {441// If no `return` statement, then return type is `void`, not `Variant`.442method_doc.return_type = "void";443} else {444method_doc.return_type = "Variant";445}446447for (const GDP::ParameterNode *p : m_func->parameters) {448DocData::ArgumentDoc arg_doc;449arg_doc.name = p->identifier->name;450_doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration);451if (p->initializer != nullptr) {452arg_doc.default_value = docvalue_from_expression(p->initializer);453}454method_doc.arguments.push_back(arg_doc);455}456457doc.methods.push_back(method_doc);458} break;459460case GDP::ClassNode::Member::SIGNAL: {461const GDP::SignalNode *m_signal = member.signal;462const StringName &signal_name = m_signal->identifier->name;463464p_script->member_lines[signal_name] = m_signal->start_line;465466DocData::MethodDoc signal_doc;467signal_doc.name = signal_name;468signal_doc.description = m_signal->doc_data.description;469signal_doc.is_deprecated = m_signal->doc_data.is_deprecated;470signal_doc.deprecated_message = m_signal->doc_data.deprecated_message;471signal_doc.is_experimental = m_signal->doc_data.is_experimental;472signal_doc.experimental_message = m_signal->doc_data.experimental_message;473474for (const GDP::ParameterNode *p : m_signal->parameters) {475DocData::ArgumentDoc arg_doc;476arg_doc.name = p->identifier->name;477_doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration);478signal_doc.arguments.push_back(arg_doc);479}480481doc.signals.push_back(signal_doc);482} break;483484case GDP::ClassNode::Member::VARIABLE: {485const GDP::VariableNode *m_var = member.variable;486const StringName &var_name = m_var->identifier->name;487488p_script->member_lines[var_name] = m_var->start_line;489490DocData::PropertyDoc prop_doc;491prop_doc.name = var_name;492prop_doc.description = m_var->doc_data.description;493prop_doc.is_deprecated = m_var->doc_data.is_deprecated;494prop_doc.deprecated_message = m_var->doc_data.deprecated_message;495prop_doc.is_experimental = m_var->doc_data.is_experimental;496prop_doc.experimental_message = m_var->doc_data.experimental_message;497_doctype_from_gdtype(m_var->get_datatype(), prop_doc.type, prop_doc.enumeration);498499switch (m_var->property) {500case GDP::VariableNode::PROP_NONE:501break;502case GDP::VariableNode::PROP_INLINE:503if (m_var->setter != nullptr) {504prop_doc.setter = m_var->setter->identifier->name;505}506if (m_var->getter != nullptr) {507prop_doc.getter = m_var->getter->identifier->name;508}509break;510case GDP::VariableNode::PROP_SETGET:511if (m_var->setter_pointer != nullptr) {512prop_doc.setter = m_var->setter_pointer->name;513}514if (m_var->getter_pointer != nullptr) {515prop_doc.getter = m_var->getter_pointer->name;516}517break;518}519520if (m_var->initializer != nullptr) {521prop_doc.default_value = docvalue_from_expression(m_var->initializer);522}523524prop_doc.overridden = false;525526doc.properties.push_back(prop_doc);527} break;528529case GDP::ClassNode::Member::ENUM: {530const GDP::EnumNode *m_enum = member.m_enum;531StringName name = m_enum->identifier->name;532533p_script->member_lines[name] = m_enum->start_line;534535DocData::EnumDoc enum_doc;536enum_doc.description = m_enum->doc_data.description;537enum_doc.is_deprecated = m_enum->doc_data.is_deprecated;538enum_doc.deprecated_message = m_enum->doc_data.deprecated_message;539enum_doc.is_experimental = m_enum->doc_data.is_experimental;540enum_doc.experimental_message = m_enum->doc_data.experimental_message;541doc.enums[name] = enum_doc;542543for (const GDP::EnumNode::Value &val : m_enum->values) {544DocData::ConstantDoc const_doc;545const_doc.name = val.identifier->name;546const_doc.value = _docvalue_from_variant(val.value);547const_doc.is_value_valid = true;548const_doc.type = "int";549const_doc.enumeration = name;550const_doc.description = val.doc_data.description;551const_doc.is_deprecated = val.doc_data.is_deprecated;552const_doc.deprecated_message = val.doc_data.deprecated_message;553const_doc.is_experimental = val.doc_data.is_experimental;554const_doc.experimental_message = val.doc_data.experimental_message;555556doc.constants.push_back(const_doc);557}558559} break;560561case GDP::ClassNode::Member::ENUM_VALUE: {562const GDP::EnumNode::Value &m_enum_val = member.enum_value;563const StringName &name = m_enum_val.identifier->name;564565p_script->member_lines[name] = m_enum_val.identifier->start_line;566567DocData::ConstantDoc const_doc;568const_doc.name = name;569const_doc.value = _docvalue_from_variant(m_enum_val.value);570const_doc.is_value_valid = true;571const_doc.type = "int";572const_doc.enumeration = "@unnamed_enums";573const_doc.description = m_enum_val.doc_data.description;574const_doc.is_deprecated = m_enum_val.doc_data.is_deprecated;575const_doc.deprecated_message = m_enum_val.doc_data.deprecated_message;576const_doc.is_experimental = m_enum_val.doc_data.is_experimental;577const_doc.experimental_message = m_enum_val.doc_data.experimental_message;578doc.constants.push_back(const_doc);579} break;580581default:582break;583}584}585586// Add doc to the outer-most class.587p_script->_add_doc(doc);588}589590void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {591for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {592if (E.value.is_singleton) {593singletons[E.value.path] = E.key;594}595}596_generate_docs(p_script, p_class);597singletons.clear();598}599600// This method is needed for the editor, since during autocompletion the script is not compiled, only analyzed.601void GDScriptDocGen::doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return) {602for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {603if (E.value.is_singleton) {604singletons[E.value.path] = E.key;605}606}607_doctype_from_gdtype(p_gdtype, r_type, r_enum, p_is_return);608singletons.clear();609}610611612