doc_data.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**************************************************************************/
  2. /* doc_data.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 "doc_data.h"
  31. String DocData::get_default_value_string(const Variant &p_value) {
  32. if (p_value.get_type() == Variant::ARRAY) {
  33. return Variant(Array(p_value, 0, StringName(), Variant())).get_construct_string().replace("\n", " ");
  34. } else if (p_value.get_type() == Variant::DICTIONARY) {
  35. return Variant(Dictionary(p_value, 0, StringName(), Variant(), 0, StringName(), Variant())).get_construct_string().replace("\n", " ");
  36. } else {
  37. return p_value.get_construct_string().replace("\n", " ");
  38. }
  39. }
  40. void DocData::return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo) {
  41. if (p_retinfo.type == Variant::INT && p_retinfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
  42. p_method.return_type = p_retinfo.hint_string;
  43. if (p_method.return_type.is_empty()) {
  44. p_method.return_type = "void*";
  45. } else {
  46. p_method.return_type += "*";
  47. }
  48. } else if (p_retinfo.type == Variant::INT && p_retinfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  49. p_method.return_enum = p_retinfo.class_name;
  50. if (p_method.return_enum.begins_with("_")) { //proxy class
  51. p_method.return_enum = p_method.return_enum.substr(1, p_method.return_enum.length());
  52. }
  53. p_method.return_is_bitfield = p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
  54. p_method.return_type = "int";
  55. } else if (p_retinfo.class_name != StringName()) {
  56. p_method.return_type = p_retinfo.class_name;
  57. } else if (p_retinfo.type == Variant::ARRAY && p_retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
  58. p_method.return_type = p_retinfo.hint_string + "[]";
  59. } else if (p_retinfo.type == Variant::DICTIONARY && p_retinfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
  60. p_method.return_type = "Dictionary[" + p_retinfo.hint_string.replace(";", ", ") + "]";
  61. } else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  62. p_method.return_type = p_retinfo.hint_string;
  63. } else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  64. p_method.return_type = "Variant";
  65. } else if (p_retinfo.type == Variant::NIL) {
  66. p_method.return_type = "void";
  67. } else {
  68. p_method.return_type = Variant::get_type_name(p_retinfo.type);
  69. }
  70. }
  71. void DocData::argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo) {
  72. p_argument.name = p_arginfo.name;
  73. if (p_arginfo.type == Variant::INT && p_arginfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
  74. p_argument.type = p_arginfo.hint_string;
  75. if (p_argument.type.is_empty()) {
  76. p_argument.type = "void*";
  77. } else {
  78. p_argument.type += "*";
  79. }
  80. } else if (p_arginfo.type == Variant::INT && p_arginfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  81. p_argument.enumeration = p_arginfo.class_name;
  82. if (p_argument.enumeration.begins_with("_")) { //proxy class
  83. p_argument.enumeration = p_argument.enumeration.substr(1, p_argument.enumeration.length());
  84. }
  85. p_argument.is_bitfield = p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
  86. p_argument.type = "int";
  87. } else if (p_arginfo.class_name != StringName()) {
  88. p_argument.type = p_arginfo.class_name;
  89. } else if (p_arginfo.type == Variant::ARRAY && p_arginfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
  90. p_argument.type = p_arginfo.hint_string + "[]";
  91. } else if (p_arginfo.type == Variant::DICTIONARY && p_arginfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
  92. p_argument.type = "Dictionary[" + p_arginfo.hint_string.replace(";", ", ") + "]";
  93. } else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  94. p_argument.type = p_arginfo.hint_string;
  95. } else if (p_arginfo.type == Variant::NIL) {
  96. // Parameters cannot be void, so PROPERTY_USAGE_NIL_IS_VARIANT is not necessary
  97. p_argument.type = "Variant";
  98. } else {
  99. p_argument.type = Variant::get_type_name(p_arginfo.type);
  100. }
  101. }
  102. void DocData::property_doc_from_scriptmemberinfo(DocData::PropertyDoc &p_property, const ScriptMemberInfo &p_memberinfo) {
  103. p_property.name = p_memberinfo.propinfo.name;
  104. p_property.description = p_memberinfo.doc_string;
  105. if (p_memberinfo.propinfo.type == Variant::OBJECT) {
  106. p_property.type = p_memberinfo.propinfo.class_name;
  107. } else if (p_memberinfo.propinfo.type == Variant::NIL && p_memberinfo.propinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  108. p_property.type = "Variant";
  109. } else {
  110. p_property.type = Variant::get_type_name(p_memberinfo.propinfo.type);
  111. }
  112. p_property.setter = p_memberinfo.setter;
  113. p_property.getter = p_memberinfo.getter;
  114. if (p_memberinfo.has_default_value && p_memberinfo.default_value.get_type() != Variant::OBJECT) {
  115. p_property.default_value = get_default_value_string(p_memberinfo.default_value);
  116. }
  117. p_property.overridden = false;
  118. }
  119. void DocData::method_doc_from_methodinfo(DocData::MethodDoc &p_method, const MethodInfo &p_methodinfo, const String &p_desc) {
  120. p_method.name = p_methodinfo.name;
  121. p_method.description = p_desc;
  122. if (p_methodinfo.flags & METHOD_FLAG_VIRTUAL) {
  123. p_method.qualifiers = "virtual";
  124. }
  125. if (p_methodinfo.flags & METHOD_FLAG_CONST) {
  126. if (!p_method.qualifiers.is_empty()) {
  127. p_method.qualifiers += " ";
  128. }
  129. p_method.qualifiers += "const";
  130. }
  131. if (p_methodinfo.flags & METHOD_FLAG_VARARG) {
  132. if (!p_method.qualifiers.is_empty()) {
  133. p_method.qualifiers += " ";
  134. }
  135. p_method.qualifiers += "vararg";
  136. }
  137. if (p_methodinfo.flags & METHOD_FLAG_STATIC) {
  138. if (!p_method.qualifiers.is_empty()) {
  139. p_method.qualifiers += " ";
  140. }
  141. p_method.qualifiers += "static";
  142. }
  143. return_doc_from_retinfo(p_method, p_methodinfo.return_val);
  144. int i = 0;
  145. for (List<PropertyInfo>::ConstIterator itr = p_methodinfo.arguments.begin(); itr != p_methodinfo.arguments.end(); ++itr, ++i) {
  146. DocData::ArgumentDoc argument;
  147. argument_doc_from_arginfo(argument, *itr);
  148. int default_arg_index = i - (p_methodinfo.arguments.size() - p_methodinfo.default_arguments.size());
  149. if (default_arg_index >= 0) {
  150. Variant default_arg = p_methodinfo.default_arguments[default_arg_index];
  151. argument.default_value = get_default_value_string(default_arg);
  152. }
  153. p_method.arguments.push_back(argument);
  154. }
  155. }
  156. void DocData::constant_doc_from_variant(DocData::ConstantDoc &p_const, const StringName &p_name, const Variant &p_value, const String &p_desc) {
  157. p_const.name = p_name;
  158. p_const.value = p_value;
  159. p_const.is_value_valid = (p_value.get_type() != Variant::OBJECT);
  160. p_const.description = p_desc;
  161. }
  162. void DocData::signal_doc_from_methodinfo(DocData::MethodDoc &p_signal, const MethodInfo &p_methodinfo, const String &p_desc) {
  163. return method_doc_from_methodinfo(p_signal, p_methodinfo, p_desc);
  164. }