doc_data.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. const Variant::Type type = p_value.get_type();
  33. if (type == Variant::ARRAY) {
  34. return Variant(Array(p_value, 0, StringName(), Variant())).get_construct_string().replace_char('\n', ' ');
  35. } else if (type == Variant::DICTIONARY) {
  36. return Variant(Dictionary(p_value, 0, StringName(), Variant(), 0, StringName(), Variant())).get_construct_string().replace_char('\n', ' ');
  37. } else if (type == Variant::INT) {
  38. return itos(p_value);
  39. } else if (type == Variant::FLOAT) {
  40. // Since some values are 32-bit internally, use 32-bit for all
  41. // documentation values to avoid garbage digits at the end.
  42. const String s = String::num_scientific((float)p_value);
  43. // Use float literals for floats in the documentation for clarity.
  44. if (s != "inf" && s != "-inf" && s != "nan") {
  45. if (!s.contains_char('.') && !s.contains_char('e')) {
  46. return s + ".0";
  47. }
  48. }
  49. return s;
  50. } else {
  51. return p_value.get_construct_string().replace_char('\n', ' ');
  52. }
  53. }
  54. void DocData::return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo) {
  55. if (p_retinfo.type == Variant::INT && p_retinfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
  56. p_method.return_type = p_retinfo.hint_string;
  57. if (p_method.return_type.is_empty()) {
  58. p_method.return_type = "void*";
  59. } else {
  60. p_method.return_type += "*";
  61. }
  62. } else if (p_retinfo.type == Variant::INT && p_retinfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  63. p_method.return_enum = p_retinfo.class_name;
  64. if (p_method.return_enum.begins_with("_")) { //proxy class
  65. p_method.return_enum = p_method.return_enum.substr(1);
  66. }
  67. p_method.return_is_bitfield = p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
  68. p_method.return_type = "int";
  69. } else if (p_retinfo.class_name != StringName()) {
  70. p_method.return_type = p_retinfo.class_name;
  71. } else if (p_retinfo.type == Variant::ARRAY && p_retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
  72. p_method.return_type = p_retinfo.hint_string + "[]";
  73. } else if (p_retinfo.type == Variant::DICTIONARY && p_retinfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
  74. p_method.return_type = "Dictionary[" + p_retinfo.hint_string.replace(";", ", ") + "]";
  75. } else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  76. p_method.return_type = p_retinfo.hint_string;
  77. } else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  78. p_method.return_type = "Variant";
  79. } else if (p_retinfo.type == Variant::NIL) {
  80. p_method.return_type = "void";
  81. } else {
  82. p_method.return_type = Variant::get_type_name(p_retinfo.type);
  83. }
  84. }
  85. void DocData::argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo) {
  86. p_argument.name = p_arginfo.name;
  87. if (p_arginfo.type == Variant::INT && p_arginfo.hint == PROPERTY_HINT_INT_IS_POINTER) {
  88. p_argument.type = p_arginfo.hint_string;
  89. if (p_argument.type.is_empty()) {
  90. p_argument.type = "void*";
  91. } else {
  92. p_argument.type += "*";
  93. }
  94. } else if (p_arginfo.type == Variant::INT && p_arginfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  95. p_argument.enumeration = p_arginfo.class_name;
  96. if (p_argument.enumeration.begins_with("_")) { //proxy class
  97. p_argument.enumeration = p_argument.enumeration.substr(1);
  98. }
  99. p_argument.is_bitfield = p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
  100. p_argument.type = "int";
  101. } else if (p_arginfo.class_name != StringName()) {
  102. p_argument.type = p_arginfo.class_name;
  103. } else if (p_arginfo.type == Variant::ARRAY && p_arginfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
  104. p_argument.type = p_arginfo.hint_string + "[]";
  105. } else if (p_arginfo.type == Variant::DICTIONARY && p_arginfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
  106. p_argument.type = "Dictionary[" + p_arginfo.hint_string.replace(";", ", ") + "]";
  107. } else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  108. p_argument.type = p_arginfo.hint_string;
  109. } else if (p_arginfo.type == Variant::NIL) {
  110. // Parameters cannot be void, so PROPERTY_USAGE_NIL_IS_VARIANT is not necessary
  111. p_argument.type = "Variant";
  112. } else {
  113. p_argument.type = Variant::get_type_name(p_arginfo.type);
  114. }
  115. }
  116. void DocData::method_doc_from_methodinfo(DocData::MethodDoc &p_method, const MethodInfo &p_methodinfo, const String &p_desc) {
  117. p_method.name = p_methodinfo.name;
  118. p_method.description = p_desc;
  119. if (p_methodinfo.flags & METHOD_FLAG_VIRTUAL) {
  120. p_method.qualifiers = "virtual";
  121. }
  122. if (p_methodinfo.flags & METHOD_FLAG_VIRTUAL_REQUIRED) {
  123. if (!p_method.qualifiers.is_empty()) {
  124. p_method.qualifiers += " ";
  125. }
  126. p_method.qualifiers += "required";
  127. }
  128. if (p_methodinfo.flags & METHOD_FLAG_CONST) {
  129. if (!p_method.qualifiers.is_empty()) {
  130. p_method.qualifiers += " ";
  131. }
  132. p_method.qualifiers += "const";
  133. }
  134. if (p_methodinfo.flags & METHOD_FLAG_VARARG) {
  135. if (!p_method.qualifiers.is_empty()) {
  136. p_method.qualifiers += " ";
  137. }
  138. p_method.qualifiers += "vararg";
  139. }
  140. if (p_methodinfo.flags & METHOD_FLAG_STATIC) {
  141. if (!p_method.qualifiers.is_empty()) {
  142. p_method.qualifiers += " ";
  143. }
  144. p_method.qualifiers += "static";
  145. }
  146. return_doc_from_retinfo(p_method, p_methodinfo.return_val);
  147. for (int64_t i = 0; i < p_methodinfo.arguments.size(); ++i) {
  148. DocData::ArgumentDoc argument;
  149. argument_doc_from_arginfo(argument, p_methodinfo.arguments[i]);
  150. int64_t default_arg_index = i - (p_methodinfo.arguments.size() - p_methodinfo.default_arguments.size());
  151. if (default_arg_index >= 0) {
  152. Variant default_arg = p_methodinfo.default_arguments[default_arg_index];
  153. argument.default_value = get_default_value_string(default_arg);
  154. }
  155. p_method.arguments.push_back(argument);
  156. }
  157. }