doc_data.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /**************************************************************************/
  2. /* doc_data.h */
  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. #ifndef DOC_DATA_H
  31. #define DOC_DATA_H
  32. #include "core/io/xml_parser.h"
  33. #include "core/variant/variant.h"
  34. struct ScriptMemberInfo {
  35. PropertyInfo propinfo;
  36. String doc_string;
  37. StringName setter;
  38. StringName getter;
  39. bool has_default_value = false;
  40. Variant default_value;
  41. };
  42. class DocData {
  43. public:
  44. struct ArgumentDoc {
  45. String name;
  46. String type;
  47. String enumeration;
  48. String default_value;
  49. bool operator<(const ArgumentDoc &p_arg) const {
  50. if (name == p_arg.name) {
  51. return type < p_arg.type;
  52. }
  53. return name < p_arg.name;
  54. }
  55. static ArgumentDoc from_dict(const Dictionary &p_dict) {
  56. ArgumentDoc doc;
  57. if (p_dict.has("name")) {
  58. doc.name = p_dict["name"];
  59. }
  60. if (p_dict.has("type")) {
  61. doc.type = p_dict["type"];
  62. }
  63. if (p_dict.has("enumeration")) {
  64. doc.enumeration = p_dict["enumeration"];
  65. }
  66. if (p_dict.has("default_value")) {
  67. doc.default_value = p_dict["default_value"];
  68. }
  69. return doc;
  70. }
  71. };
  72. struct MethodDoc {
  73. String name;
  74. String return_type;
  75. String return_enum;
  76. String qualifiers;
  77. String description;
  78. bool is_deprecated = false;
  79. bool is_experimental = false;
  80. Vector<ArgumentDoc> arguments;
  81. Vector<int> errors_returned;
  82. bool operator<(const MethodDoc &p_method) const {
  83. if (name == p_method.name) {
  84. // Must be an operator or a constructor since there is no other overloading
  85. if (name.left(8) == "operator") {
  86. if (arguments.size() == p_method.arguments.size()) {
  87. if (arguments.size() == 0) {
  88. return false;
  89. }
  90. return arguments[0].type < p_method.arguments[0].type;
  91. }
  92. return arguments.size() < p_method.arguments.size();
  93. } else {
  94. // Must be a constructor
  95. // We want this arbitrary order for a class "Foo":
  96. // - 1. Default constructor: Foo()
  97. // - 2. Copy constructor: Foo(Foo)
  98. // - 3+. Other constructors Foo(Bar, ...) based on first argument's name
  99. if (arguments.size() == 0 || p_method.arguments.size() == 0) { // 1.
  100. return arguments.size() < p_method.arguments.size();
  101. }
  102. if (arguments[0].type == return_type || p_method.arguments[0].type == p_method.return_type) { // 2.
  103. return (arguments[0].type == return_type) || (p_method.arguments[0].type != p_method.return_type);
  104. }
  105. return arguments[0] < p_method.arguments[0];
  106. }
  107. }
  108. return name < p_method.name;
  109. }
  110. static MethodDoc from_dict(const Dictionary &p_dict) {
  111. MethodDoc doc;
  112. if (p_dict.has("name")) {
  113. doc.name = p_dict["name"];
  114. }
  115. if (p_dict.has("return_type")) {
  116. doc.return_type = p_dict["return_type"];
  117. }
  118. if (p_dict.has("return_enum")) {
  119. doc.return_enum = p_dict["return_enum"];
  120. }
  121. if (p_dict.has("qualifiers")) {
  122. doc.qualifiers = p_dict["qualifiers"];
  123. }
  124. if (p_dict.has("description")) {
  125. doc.description = p_dict["description"];
  126. }
  127. if (p_dict.has("is_deprecated")) {
  128. doc.is_deprecated = p_dict["is_deprecated"];
  129. }
  130. if (p_dict.has("is_experimental")) {
  131. doc.is_experimental = p_dict["is_experimental"];
  132. }
  133. Array arguments;
  134. if (p_dict.has("arguments")) {
  135. arguments = p_dict["arguments"];
  136. }
  137. for (int i = 0; i < arguments.size(); i++) {
  138. doc.arguments.push_back(ArgumentDoc::from_dict(arguments[i]));
  139. }
  140. Array errors_returned;
  141. if (p_dict.has("errors_returned")) {
  142. errors_returned = p_dict["errors_returned"];
  143. }
  144. for (int i = 0; i < errors_returned.size(); i++) {
  145. doc.errors_returned.push_back(errors_returned[i]);
  146. }
  147. return doc;
  148. }
  149. };
  150. struct ConstantDoc {
  151. String name;
  152. String value;
  153. bool is_value_valid = false;
  154. String enumeration;
  155. bool is_bitfield = false;
  156. String description;
  157. bool is_deprecated = false;
  158. bool is_experimental = false;
  159. bool operator<(const ConstantDoc &p_const) const {
  160. return name < p_const.name;
  161. }
  162. static ConstantDoc from_dict(const Dictionary &p_dict) {
  163. ConstantDoc doc;
  164. if (p_dict.has("name")) {
  165. doc.name = p_dict["name"];
  166. }
  167. if (p_dict.has("value")) {
  168. doc.value = p_dict["value"];
  169. }
  170. if (p_dict.has("is_value_valid")) {
  171. doc.is_value_valid = p_dict["is_value_valid"];
  172. }
  173. if (p_dict.has("enumeration")) {
  174. doc.enumeration = p_dict["enumeration"];
  175. }
  176. if (p_dict.has("is_bitfield")) {
  177. doc.is_bitfield = p_dict["is_bitfield"];
  178. }
  179. if (p_dict.has("description")) {
  180. doc.description = p_dict["description"];
  181. }
  182. if (p_dict.has("is_deprecated")) {
  183. doc.is_deprecated = p_dict["is_deprecated"];
  184. }
  185. if (p_dict.has("is_experimental")) {
  186. doc.is_experimental = p_dict["is_experimental"];
  187. }
  188. return doc;
  189. }
  190. };
  191. struct EnumDoc {
  192. String name = "@unnamed_enum";
  193. bool is_bitfield = false;
  194. String description;
  195. Vector<DocData::ConstantDoc> values;
  196. static EnumDoc from_dict(const Dictionary &p_dict) {
  197. EnumDoc doc;
  198. if (p_dict.has("name")) {
  199. doc.name = p_dict["name"];
  200. }
  201. if (p_dict.has("is_bitfield")) {
  202. doc.is_bitfield = p_dict["is_bitfield"];
  203. }
  204. if (p_dict.has("description")) {
  205. doc.description = p_dict["description"];
  206. }
  207. Array values;
  208. if (p_dict.has("values")) {
  209. values = p_dict["values"];
  210. }
  211. for (int i = 0; i < values.size(); i++) {
  212. doc.values.push_back(ConstantDoc::from_dict(values[i]));
  213. }
  214. return doc;
  215. }
  216. };
  217. struct PropertyDoc {
  218. String name;
  219. String type;
  220. String enumeration;
  221. String description;
  222. String setter, getter;
  223. String default_value;
  224. bool overridden = false;
  225. String overrides;
  226. bool is_deprecated = false;
  227. bool is_experimental = false;
  228. bool operator<(const PropertyDoc &p_prop) const {
  229. return name < p_prop.name;
  230. }
  231. static PropertyDoc from_dict(const Dictionary &p_dict) {
  232. PropertyDoc doc;
  233. if (p_dict.has("name")) {
  234. doc.name = p_dict["name"];
  235. }
  236. if (p_dict.has("type")) {
  237. doc.type = p_dict["type"];
  238. }
  239. if (p_dict.has("enumeration")) {
  240. doc.enumeration = p_dict["enumeration"];
  241. }
  242. if (p_dict.has("description")) {
  243. doc.description = p_dict["description"];
  244. }
  245. if (p_dict.has("setter")) {
  246. doc.setter = p_dict["setter"];
  247. }
  248. if (p_dict.has("getter")) {
  249. doc.getter = p_dict["getter"];
  250. }
  251. if (p_dict.has("default_value")) {
  252. doc.default_value = p_dict["default_value"];
  253. }
  254. if (p_dict.has("overridden")) {
  255. doc.overridden = p_dict["overridden"];
  256. }
  257. if (p_dict.has("overrides")) {
  258. doc.overrides = p_dict["overrides"];
  259. }
  260. if (p_dict.has("is_deprecated")) {
  261. doc.is_deprecated = p_dict["is_deprecated"];
  262. }
  263. if (p_dict.has("is_experimental")) {
  264. doc.is_experimental = p_dict["is_experimental"];
  265. }
  266. return doc;
  267. }
  268. };
  269. struct ThemeItemDoc {
  270. String name;
  271. String type;
  272. String data_type;
  273. String description;
  274. String default_value;
  275. bool operator<(const ThemeItemDoc &p_theme_item) const {
  276. // First sort by the data type, then by name.
  277. if (data_type == p_theme_item.data_type) {
  278. return name < p_theme_item.name;
  279. }
  280. return data_type < p_theme_item.data_type;
  281. }
  282. static ThemeItemDoc from_dict(const Dictionary &p_dict) {
  283. ThemeItemDoc doc;
  284. if (p_dict.has("name")) {
  285. doc.name = p_dict["name"];
  286. }
  287. if (p_dict.has("type")) {
  288. doc.type = p_dict["type"];
  289. }
  290. if (p_dict.has("data_type")) {
  291. doc.data_type = p_dict["data_type"];
  292. }
  293. if (p_dict.has("description")) {
  294. doc.description = p_dict["description"];
  295. }
  296. if (p_dict.has("default_value")) {
  297. doc.default_value = p_dict["default_value"];
  298. }
  299. return doc;
  300. }
  301. };
  302. struct TutorialDoc {
  303. String link;
  304. String title;
  305. static TutorialDoc from_dict(const Dictionary &p_dict) {
  306. TutorialDoc doc;
  307. if (p_dict.has("link")) {
  308. doc.link = p_dict["link"];
  309. }
  310. if (p_dict.has("title")) {
  311. doc.title = p_dict["title"];
  312. }
  313. return doc;
  314. }
  315. };
  316. struct ClassDoc {
  317. String name;
  318. String inherits;
  319. String brief_description;
  320. String description;
  321. Vector<TutorialDoc> tutorials;
  322. Vector<MethodDoc> constructors;
  323. Vector<MethodDoc> methods;
  324. Vector<MethodDoc> operators;
  325. Vector<MethodDoc> signals;
  326. Vector<ConstantDoc> constants;
  327. HashMap<String, String> enums;
  328. Vector<PropertyDoc> properties;
  329. Vector<MethodDoc> annotations;
  330. Vector<ThemeItemDoc> theme_properties;
  331. bool is_deprecated = false;
  332. bool is_experimental = false;
  333. bool is_script_doc = false;
  334. String script_path;
  335. bool operator<(const ClassDoc &p_class) const {
  336. return name < p_class.name;
  337. }
  338. static ClassDoc from_dict(const Dictionary &p_dict) {
  339. ClassDoc doc;
  340. if (p_dict.has("name")) {
  341. doc.name = p_dict["name"];
  342. }
  343. if (p_dict.has("inherits")) {
  344. doc.inherits = p_dict["inherits"];
  345. }
  346. if (p_dict.has("brief_description")) {
  347. doc.brief_description = p_dict["brief_description"];
  348. }
  349. if (p_dict.has("description")) {
  350. doc.description = p_dict["description"];
  351. }
  352. Array tutorials;
  353. if (p_dict.has("tutorials")) {
  354. tutorials = p_dict["tutorials"];
  355. }
  356. for (int i = 0; i < tutorials.size(); i++) {
  357. doc.tutorials.push_back(TutorialDoc::from_dict(tutorials[i]));
  358. }
  359. Array constructors;
  360. if (p_dict.has("constructors")) {
  361. constructors = p_dict["constructors"];
  362. }
  363. for (int i = 0; i < constructors.size(); i++) {
  364. doc.constructors.push_back(MethodDoc::from_dict(constructors[i]));
  365. }
  366. Array methods;
  367. if (p_dict.has("methods")) {
  368. methods = p_dict["methods"];
  369. }
  370. for (int i = 0; i < methods.size(); i++) {
  371. doc.methods.push_back(MethodDoc::from_dict(methods[i]));
  372. }
  373. Array operators;
  374. if (p_dict.has("operators")) {
  375. operators = p_dict["operators"];
  376. }
  377. for (int i = 0; i < operators.size(); i++) {
  378. doc.operators.push_back(MethodDoc::from_dict(operators[i]));
  379. }
  380. Array signals;
  381. if (p_dict.has("signals")) {
  382. signals = p_dict["signals"];
  383. }
  384. for (int i = 0; i < signals.size(); i++) {
  385. doc.signals.push_back(MethodDoc::from_dict(signals[i]));
  386. }
  387. Array constants;
  388. if (p_dict.has("constants")) {
  389. constants = p_dict["constants"];
  390. }
  391. for (int i = 0; i < constants.size(); i++) {
  392. doc.constants.push_back(ConstantDoc::from_dict(constants[i]));
  393. }
  394. Dictionary enums;
  395. if (p_dict.has("enums")) {
  396. enums = p_dict["enums"];
  397. }
  398. for (int i = 0; i < enums.size(); i++) {
  399. doc.enums[enums.get_key_at_index(i)] = enums.get_value_at_index(i);
  400. }
  401. Array properties;
  402. if (p_dict.has("properties")) {
  403. properties = p_dict["properties"];
  404. }
  405. for (int i = 0; i < properties.size(); i++) {
  406. doc.properties.push_back(PropertyDoc::from_dict(properties[i]));
  407. }
  408. Array annotations;
  409. if (p_dict.has("annotations")) {
  410. annotations = p_dict["annotations"];
  411. }
  412. for (int i = 0; i < annotations.size(); i++) {
  413. doc.annotations.push_back(MethodDoc::from_dict(annotations[i]));
  414. }
  415. Array theme_properties;
  416. if (p_dict.has("theme_properties")) {
  417. theme_properties = p_dict["theme_properties"];
  418. }
  419. for (int i = 0; i < theme_properties.size(); i++) {
  420. doc.theme_properties.push_back(ThemeItemDoc::from_dict(theme_properties[i]));
  421. }
  422. if (p_dict.has("is_deprecated")) {
  423. doc.is_deprecated = p_dict["is_deprecated"];
  424. }
  425. if (p_dict.has("is_experimental")) {
  426. doc.is_experimental = p_dict["is_experimental"];
  427. }
  428. if (p_dict.has("is_script_doc")) {
  429. doc.is_script_doc = p_dict["is_script_doc"];
  430. }
  431. if (p_dict.has("script_path")) {
  432. doc.script_path = p_dict["script_path"];
  433. }
  434. return doc;
  435. }
  436. };
  437. static String get_default_value_string(const Variant &p_value);
  438. static void return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo);
  439. static void argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo);
  440. static void property_doc_from_scriptmemberinfo(DocData::PropertyDoc &p_property, const ScriptMemberInfo &p_memberinfo);
  441. static void method_doc_from_methodinfo(DocData::MethodDoc &p_method, const MethodInfo &p_methodinfo, const String &p_desc);
  442. static void constant_doc_from_variant(DocData::ConstantDoc &p_const, const StringName &p_name, const Variant &p_value, const String &p_desc);
  443. static void signal_doc_from_methodinfo(DocData::MethodDoc &p_signal, const MethodInfo &p_methodinfo, const String &p_desc);
  444. };
  445. #endif // DOC_DATA_H