api_generator.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*************************************************************************/
  2. /* api_generator.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "api_generator.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "core/config/engine.h"
  33. #include "core/core_constants.h"
  34. #include "core/io/file_access.h"
  35. #include "core/object/class_db.h"
  36. #include "core/string/string_builder.h"
  37. #include "core/templates/pair.h"
  38. #include "core/variant/variant_parser.h"
  39. // helper stuff
  40. static Error save_file(const String &p_path, const List<String> &p_content) {
  41. FileAccessRef file = FileAccess::open(p_path, FileAccess::WRITE);
  42. ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE);
  43. for (const List<String>::Element *e = p_content.front(); e != nullptr; e = e->next()) {
  44. file->store_string(e->get());
  45. }
  46. file->close();
  47. return OK;
  48. }
  49. // helper stuff end
  50. struct MethodAPI {
  51. String method_name;
  52. String return_type;
  53. List<String> argument_types;
  54. List<String> argument_names;
  55. Map<int, Variant> default_arguments;
  56. int argument_count = 0;
  57. bool has_varargs = false;
  58. bool is_editor = false;
  59. bool is_noscript = false;
  60. bool is_const = false;
  61. bool is_static = false; // For builtin types.
  62. bool is_reverse = false;
  63. bool is_virtual = false;
  64. bool is_from_script = false;
  65. };
  66. struct PropertyAPI {
  67. String name;
  68. String getter;
  69. String setter;
  70. String type;
  71. int index = 0;
  72. };
  73. struct ConstantAPI {
  74. String constant_name;
  75. int constant_value = 0;
  76. Variant builtin_constant_value; // For builtin types;
  77. String builtin_constant_type; // For builtin types;
  78. };
  79. struct SignalAPI {
  80. String name;
  81. List<String> argument_types;
  82. List<String> argument_names;
  83. Map<int, Variant> default_arguments;
  84. };
  85. struct EnumAPI {
  86. String name;
  87. List<Pair<int, String>> values;
  88. };
  89. struct OperatorAPI { // For builtin types;
  90. String name;
  91. int oper = Variant::OP_MAX;
  92. String other_type;
  93. String return_type;
  94. };
  95. struct ClassAPI {
  96. String class_name;
  97. String super_class_name;
  98. ClassDB::APIType api_type = ClassDB::API_NONE;
  99. bool is_singleton = false;
  100. String singleton_name;
  101. bool is_instantiable = false;
  102. // @Unclear
  103. bool is_ref_counted = false;
  104. bool has_indexing = false; // For builtin types.
  105. String indexed_type; // For builtin types.
  106. bool is_keyed = false; // For builtin types.
  107. List<MethodAPI> methods;
  108. List<MethodAPI> constructors; // For builtin types.
  109. List<PropertyAPI> properties;
  110. List<ConstantAPI> constants;
  111. List<SignalAPI> signals_;
  112. List<EnumAPI> enums;
  113. List<OperatorAPI> operators; // For builtin types.
  114. };
  115. static String get_type_name(const PropertyInfo &info) {
  116. if (info.type == Variant::INT && (info.usage & PROPERTY_USAGE_CLASS_IS_ENUM)) {
  117. return String("enum.") + String(info.class_name).replace(".", "::");
  118. }
  119. if (info.class_name != StringName()) {
  120. return info.class_name;
  121. }
  122. if (info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  123. return info.class_name;
  124. }
  125. if (info.type == Variant::NIL && (info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  126. return "Variant";
  127. }
  128. if (info.type == Variant::NIL) {
  129. return "void";
  130. }
  131. return Variant::get_type_name(info.type);
  132. }
  133. /*
  134. * Some comparison helper functions we need
  135. */
  136. struct MethodInfoComparator {
  137. StringName::AlphCompare compare;
  138. bool operator()(const MethodInfo &p_a, const MethodInfo &p_b) const {
  139. return compare(p_a.name, p_b.name);
  140. }
  141. };
  142. struct PropertyInfoComparator {
  143. StringName::AlphCompare compare;
  144. bool operator()(const PropertyInfo &p_a, const PropertyInfo &p_b) const {
  145. return compare(p_a.name, p_b.name);
  146. }
  147. };
  148. struct ConstantAPIComparator {
  149. NoCaseComparator compare;
  150. bool operator()(const ConstantAPI &p_a, const ConstantAPI &p_b) const {
  151. return compare(p_a.constant_name, p_b.constant_name);
  152. }
  153. };
  154. /*
  155. * Reads the entire Godot API to a list
  156. */
  157. List<ClassAPI> generate_c_api_classes() {
  158. List<ClassAPI> api;
  159. List<StringName> classes;
  160. ClassDB::get_class_list(&classes);
  161. classes.sort_custom<StringName::AlphCompare>();
  162. // Register global constants as a fake CoreConstants singleton class
  163. {
  164. ClassAPI global_constants_api;
  165. global_constants_api.class_name = "CoreConstants";
  166. global_constants_api.api_type = ClassDB::API_CORE;
  167. global_constants_api.is_singleton = true;
  168. global_constants_api.singleton_name = "CoreConstants";
  169. global_constants_api.is_instantiable = false;
  170. const int constants_count = CoreConstants::get_global_constant_count();
  171. Map<StringName, EnumAPI> enum_api_map;
  172. for (int i = 0; i < constants_count; ++i) {
  173. StringName enum_name = CoreConstants::get_global_constant_enum(i);
  174. String name = String(CoreConstants::get_global_constant_name(i));
  175. int value = CoreConstants::get_global_constant_value(i);
  176. if (enum_name == StringName()) {
  177. ConstantAPI constant_api;
  178. constant_api.constant_name = name;
  179. constant_api.constant_value = value;
  180. global_constants_api.constants.push_back(constant_api);
  181. } else {
  182. EnumAPI enum_api;
  183. if (enum_api_map.has(enum_name)) {
  184. enum_api = enum_api_map[enum_name];
  185. } else {
  186. enum_api.name = String(enum_name);
  187. }
  188. enum_api.values.push_back(Pair(value, name));
  189. enum_api_map[enum_name] = enum_api;
  190. }
  191. }
  192. for (const Map<StringName, EnumAPI>::Element *E = enum_api_map.front(); E; E = E->next()) {
  193. global_constants_api.enums.push_back(E->get());
  194. }
  195. global_constants_api.constants.sort_custom<ConstantAPIComparator>();
  196. api.push_back(global_constants_api);
  197. }
  198. for (List<StringName>::Element *e = classes.front(); e != nullptr; e = e->next()) {
  199. StringName class_name = e->get();
  200. if (!ClassDB::is_class_exposed(class_name)) {
  201. continue;
  202. }
  203. ClassAPI class_api;
  204. class_api.api_type = ClassDB::get_api_type(e->get());
  205. class_api.class_name = class_name;
  206. class_api.super_class_name = ClassDB::get_parent_class(class_name);
  207. {
  208. class_api.is_singleton = Engine::get_singleton()->has_singleton(class_name);
  209. if (class_api.is_singleton) {
  210. class_api.singleton_name = class_name;
  211. }
  212. }
  213. class_api.is_instantiable = !class_api.is_singleton && ClassDB::can_instantiate(class_name);
  214. {
  215. List<StringName> inheriters;
  216. ClassDB::get_inheriters_from_class("RefCounted", &inheriters);
  217. bool is_ref_counted = !!inheriters.find(class_name) || class_name == "RefCounted";
  218. // @Unclear
  219. class_api.is_ref_counted = !class_api.is_singleton && is_ref_counted;
  220. }
  221. // constants
  222. {
  223. List<String> constant;
  224. ClassDB::get_integer_constant_list(class_name, &constant, true);
  225. constant.sort_custom<NoCaseComparator>();
  226. for (List<String>::Element *c = constant.front(); c != nullptr; c = c->next()) {
  227. ConstantAPI constant_api;
  228. constant_api.constant_name = c->get();
  229. constant_api.constant_value = ClassDB::get_integer_constant(class_name, c->get());
  230. class_api.constants.push_back(constant_api);
  231. }
  232. }
  233. // signals
  234. {
  235. List<MethodInfo> signals_;
  236. ClassDB::get_signal_list(class_name, &signals_, true);
  237. signals_.sort_custom<MethodInfoComparator>();
  238. for (int i = 0; i < signals_.size(); i++) {
  239. SignalAPI signal;
  240. MethodInfo method_info = signals_[i];
  241. signal.name = method_info.name;
  242. for (int j = 0; j < method_info.arguments.size(); j++) {
  243. PropertyInfo argument = method_info.arguments[j];
  244. String type;
  245. String name = argument.name;
  246. if (argument.name.find(":") != -1) {
  247. type = argument.name.get_slice(":", 1);
  248. name = argument.name.get_slice(":", 0);
  249. } else {
  250. type = get_type_name(argument);
  251. }
  252. signal.argument_names.push_back(name);
  253. signal.argument_types.push_back(type);
  254. }
  255. Vector<Variant> default_arguments = method_info.default_arguments;
  256. int default_start = signal.argument_names.size() - default_arguments.size();
  257. for (int j = 0; j < default_arguments.size(); j++) {
  258. signal.default_arguments[default_start + j] = default_arguments[j];
  259. }
  260. class_api.signals_.push_back(signal);
  261. }
  262. }
  263. //properties
  264. {
  265. List<PropertyInfo> properties;
  266. ClassDB::get_property_list(class_name, &properties, true);
  267. properties.sort_custom<PropertyInfoComparator>();
  268. for (List<PropertyInfo>::Element *p = properties.front(); p != nullptr; p = p->next()) {
  269. PropertyAPI property_api;
  270. property_api.name = p->get().name;
  271. property_api.getter = ClassDB::get_property_getter(class_name, p->get().name);
  272. property_api.setter = ClassDB::get_property_setter(class_name, p->get().name);
  273. if (p->get().name.find(":") != -1) {
  274. property_api.type = p->get().name.get_slice(":", 1);
  275. property_api.name = p->get().name.get_slice(":", 0);
  276. } else {
  277. MethodInfo minfo;
  278. ClassDB::get_method_info(class_name, property_api.getter, &minfo, true, false);
  279. property_api.type = get_type_name(minfo.return_val);
  280. }
  281. property_api.index = ClassDB::get_property_index(class_name, p->get().name);
  282. if (!property_api.setter.is_empty() || !property_api.getter.is_empty()) {
  283. class_api.properties.push_back(property_api);
  284. }
  285. }
  286. }
  287. //methods
  288. {
  289. List<MethodInfo> methods;
  290. ClassDB::get_method_list(class_name, &methods, true);
  291. methods.sort_custom<MethodInfoComparator>();
  292. for (List<MethodInfo>::Element *m = methods.front(); m != nullptr; m = m->next()) {
  293. MethodAPI method_api;
  294. MethodBind *method_bind = ClassDB::get_method(class_name, m->get().name);
  295. MethodInfo &method_info = m->get();
  296. //method name
  297. method_api.method_name = method_info.name;
  298. //method return type
  299. if (method_api.method_name.find(":") != -1) {
  300. method_api.return_type = method_api.method_name.get_slice(":", 1);
  301. method_api.method_name = method_api.method_name.get_slice(":", 0);
  302. } else {
  303. method_api.return_type = get_type_name(m->get().return_val);
  304. }
  305. method_api.argument_count = method_info.arguments.size();
  306. method_api.has_varargs = method_bind && method_bind->is_vararg();
  307. // Method flags
  308. method_api.is_virtual = false;
  309. if (method_info.flags) {
  310. const uint32_t flags = method_info.flags;
  311. method_api.is_editor = flags & METHOD_FLAG_EDITOR;
  312. method_api.is_noscript = flags & METHOD_FLAG_NOSCRIPT;
  313. method_api.is_const = flags & METHOD_FLAG_CONST;
  314. method_api.is_reverse = flags & METHOD_FLAG_REVERSE;
  315. method_api.is_virtual = flags & METHOD_FLAG_VIRTUAL;
  316. method_api.is_from_script = flags & METHOD_FLAG_FROM_SCRIPT;
  317. }
  318. method_api.is_virtual = method_api.is_virtual || method_api.method_name[0] == '_';
  319. // method argument name and type
  320. for (int i = 0; i < method_api.argument_count; i++) {
  321. String arg_name;
  322. String arg_type;
  323. PropertyInfo arg_info = method_info.arguments[i];
  324. arg_name = arg_info.name;
  325. if (arg_info.name.find(":") != -1) {
  326. arg_type = arg_info.name.get_slice(":", 1);
  327. arg_name = arg_info.name.get_slice(":", 0);
  328. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  329. arg_type = arg_info.class_name;
  330. } else if (arg_info.type == Variant::NIL) {
  331. arg_type = "Variant";
  332. } else if (arg_info.type == Variant::OBJECT) {
  333. arg_type = arg_info.class_name;
  334. if (arg_type == "") {
  335. arg_type = Variant::get_type_name(arg_info.type);
  336. }
  337. } else {
  338. arg_type = get_type_name(arg_info);
  339. }
  340. method_api.argument_names.push_back(arg_name);
  341. method_api.argument_types.push_back(arg_type);
  342. if (method_bind && method_bind->has_default_argument(i)) {
  343. method_api.default_arguments[i] = method_bind->get_default_argument(i);
  344. }
  345. }
  346. class_api.methods.push_back(method_api);
  347. }
  348. }
  349. // enums
  350. {
  351. List<EnumAPI> enums;
  352. List<StringName> enum_names;
  353. ClassDB::get_enum_list(class_name, &enum_names, true);
  354. for (const StringName &E : enum_names) {
  355. List<StringName> value_names;
  356. EnumAPI enum_api;
  357. enum_api.name = E;
  358. ClassDB::get_enum_constants(class_name, E, &value_names, true);
  359. for (List<StringName>::Element *val_e = value_names.front(); val_e; val_e = val_e->next()) {
  360. int int_val = ClassDB::get_integer_constant(class_name, val_e->get(), nullptr);
  361. enum_api.values.push_back(Pair<int, String>(int_val, val_e->get()));
  362. }
  363. enum_api.values.sort_custom<PairSort<int, String>>();
  364. class_api.enums.push_back(enum_api);
  365. }
  366. }
  367. api.push_back(class_api);
  368. }
  369. return api;
  370. }
  371. /*
  372. * Reads the builtin Variant API to a list
  373. */
  374. List<ClassAPI> generate_c_builtin_api_types() {
  375. List<ClassAPI> api;
  376. // Special class for the utility methods.
  377. {
  378. ClassAPI utility_api;
  379. utility_api.class_name = "Utilities";
  380. utility_api.is_instantiable = false;
  381. List<StringName> utility_functions;
  382. Variant::get_utility_function_list(&utility_functions);
  383. for (const StringName &E : utility_functions) {
  384. const StringName &function_name = E;
  385. MethodAPI function_api;
  386. function_api.method_name = function_name;
  387. function_api.has_varargs = Variant::is_utility_function_vararg(function_name);
  388. function_api.argument_count = function_api.has_varargs ? 0 : Variant::get_utility_function_argument_count(function_name);
  389. function_api.is_const = Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH;
  390. for (int i = 0; i < function_api.argument_count; i++) {
  391. function_api.argument_names.push_back(Variant::get_utility_function_argument_name(function_name, i));
  392. Variant::Type arg_type = Variant::get_utility_function_argument_type(function_name, i);
  393. function_api.argument_types.push_back(arg_type == Variant::NIL ? "Variant" : Variant::get_type_name(arg_type));
  394. }
  395. if (Variant::has_utility_function_return_value(function_name)) {
  396. Variant::Type ret_type = Variant::get_utility_function_return_type(function_name);
  397. function_api.return_type = ret_type == Variant::NIL ? "Variant" : Variant::get_type_name(ret_type);
  398. } else {
  399. function_api.return_type = "void";
  400. }
  401. utility_api.methods.push_back(function_api);
  402. }
  403. api.push_back(utility_api);
  404. }
  405. for (int t = 0; t < Variant::VARIANT_MAX; t++) {
  406. Variant::Type type = (Variant::Type)t;
  407. ClassAPI class_api;
  408. class_api.class_name = Variant::get_type_name(type);
  409. class_api.is_instantiable = true;
  410. class_api.has_indexing = Variant::has_indexing(type);
  411. class_api.indexed_type = Variant::get_type_name(Variant::get_indexed_element_type(type));
  412. class_api.is_keyed = Variant::is_keyed(type);
  413. // Types that are passed by reference.
  414. switch (type) {
  415. case Variant::OBJECT:
  416. case Variant::DICTIONARY:
  417. case Variant::ARRAY:
  418. case Variant::PACKED_BYTE_ARRAY:
  419. case Variant::PACKED_INT32_ARRAY:
  420. case Variant::PACKED_INT64_ARRAY:
  421. case Variant::PACKED_FLOAT32_ARRAY:
  422. case Variant::PACKED_FLOAT64_ARRAY:
  423. case Variant::PACKED_STRING_ARRAY:
  424. case Variant::PACKED_VECTOR2_ARRAY:
  425. case Variant::PACKED_VECTOR3_ARRAY:
  426. case Variant::PACKED_COLOR_ARRAY:
  427. class_api.is_ref_counted = true;
  428. break;
  429. default:
  430. class_api.is_ref_counted = false;
  431. break;
  432. }
  433. // Methods.
  434. List<StringName> methods;
  435. Variant::get_builtin_method_list(type, &methods);
  436. for (const StringName &E : methods) {
  437. const StringName &method_name = E;
  438. MethodAPI method_api;
  439. method_api.method_name = method_name;
  440. method_api.argument_count = Variant::get_builtin_method_argument_count(type, method_name);
  441. method_api.has_varargs = Variant::is_builtin_method_vararg(type, method_name);
  442. method_api.is_const = Variant::is_builtin_method_const(type, method_name);
  443. method_api.is_static = Variant::is_builtin_method_static(type, method_name);
  444. for (int i = 0; i < method_api.argument_count; i++) {
  445. method_api.argument_names.push_back(Variant::get_builtin_method_argument_name(type, method_name, i));
  446. Variant::Type arg_type = Variant::get_builtin_method_argument_type(type, method_name, i);
  447. method_api.argument_types.push_back(arg_type == Variant::NIL ? "Variant" : Variant::get_type_name(arg_type));
  448. }
  449. Vector<Variant> default_arguments = Variant::get_builtin_method_default_arguments(type, method_name);
  450. int default_start = method_api.argument_names.size() - default_arguments.size();
  451. for (int i = 0; i < default_arguments.size(); i++) {
  452. method_api.default_arguments[default_start + i] = default_arguments[i];
  453. }
  454. if (Variant::has_builtin_method_return_value(type, method_name)) {
  455. Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);
  456. method_api.return_type = ret_type == Variant::NIL ? "Variant" : Variant::get_type_name(ret_type);
  457. } else {
  458. method_api.return_type = "void";
  459. }
  460. class_api.methods.push_back(method_api);
  461. }
  462. // Constructors.
  463. for (int c = 0; c < Variant::get_constructor_count(type); c++) {
  464. MethodAPI constructor_api;
  465. constructor_api.method_name = Variant::get_type_name(type);
  466. constructor_api.argument_count = Variant::get_constructor_argument_count(type, c);
  467. constructor_api.return_type = Variant::get_type_name(type);
  468. for (int i = 0; i < constructor_api.argument_count; i++) {
  469. constructor_api.argument_names.push_back(Variant::get_constructor_argument_name(type, c, i));
  470. Variant::Type arg_type = Variant::get_constructor_argument_type(type, c, i);
  471. constructor_api.argument_types.push_back(arg_type == Variant::NIL ? "Variant" : Variant::get_type_name(arg_type));
  472. }
  473. class_api.constructors.push_back(constructor_api);
  474. }
  475. // Constants.
  476. List<StringName> constants;
  477. Variant::get_constants_for_type(type, &constants);
  478. for (const StringName &E : constants) {
  479. const StringName &constant_name = E;
  480. ConstantAPI constant_api;
  481. constant_api.constant_name = constant_name;
  482. constant_api.builtin_constant_value = Variant::get_constant_value(type, constant_name);
  483. constant_api.builtin_constant_type = Variant::get_type_name(constant_api.builtin_constant_value.get_type());
  484. class_api.constants.push_back(constant_api);
  485. }
  486. // Members.
  487. List<StringName> members;
  488. Variant::get_member_list(type, &members);
  489. for (const StringName &E : members) {
  490. const StringName &member_name = E;
  491. PropertyAPI member_api;
  492. member_api.name = member_name;
  493. Variant::Type member_type = Variant::get_member_type(type, member_name);
  494. member_api.type = member_type == Variant::NIL ? "Variant" : Variant::get_type_name(member_type);
  495. class_api.properties.push_back(member_api);
  496. }
  497. // Operators.
  498. for (int op = 0; op < Variant::OP_MAX; op++) {
  499. Variant::Operator oper = (Variant::Operator)op;
  500. for (int ot = 0; ot < Variant::VARIANT_MAX; ot++) {
  501. Variant::Type other_type = (Variant::Type)ot;
  502. if (!Variant::get_validated_operator_evaluator(oper, type, other_type)) {
  503. continue;
  504. }
  505. OperatorAPI oper_api;
  506. oper_api.name = Variant::get_operator_name(oper);
  507. oper_api.oper = oper;
  508. oper_api.other_type = Variant::get_type_name(other_type);
  509. oper_api.return_type = Variant::get_type_name(Variant::get_operator_return_type(oper, type, other_type));
  510. class_api.operators.push_back(oper_api);
  511. }
  512. }
  513. api.push_back(class_api);
  514. }
  515. return api;
  516. }
  517. /*
  518. * Generates the JSON source from the API in p_api
  519. */
  520. static List<String> generate_c_api_json(const List<ClassAPI> &p_api) {
  521. // I'm sorry for the \t mess
  522. List<String> source;
  523. VariantWriter writer;
  524. source.push_back("[\n");
  525. for (const List<ClassAPI>::Element *c = p_api.front(); c != nullptr; c = c->next()) {
  526. ClassAPI api = c->get();
  527. source.push_back("\t{\n");
  528. source.push_back("\t\t\"name\": \"" + api.class_name + "\",\n");
  529. source.push_back("\t\t\"base_class\": \"" + api.super_class_name + "\",\n");
  530. source.push_back(String("\t\t\"api_type\": \"") + (api.api_type == ClassDB::API_CORE ? "core" : (api.api_type == ClassDB::API_EDITOR ? "tools" : "none")) + "\",\n");
  531. source.push_back(String("\t\t\"singleton\": ") + (api.is_singleton ? "true" : "false") + ",\n");
  532. source.push_back("\t\t\"singleton_name\": \"" + api.singleton_name + "\",\n");
  533. source.push_back(String("\t\t\"instantiable\": ") + (api.is_instantiable ? "true" : "false") + ",\n");
  534. source.push_back(String("\t\t\"is_ref_counted\": ") + (api.is_ref_counted ? "true" : "false") + ",\n");
  535. source.push_back("\t\t\"constants\": {\n");
  536. for (List<ConstantAPI>::Element *e = api.constants.front(); e; e = e->next()) {
  537. source.push_back("\t\t\t\"" + e->get().constant_name + "\": " + String::num_int64(e->get().constant_value) + (e->next() ? "," : "") + "\n");
  538. }
  539. source.push_back("\t\t},\n");
  540. source.push_back("\t\t\"properties\": [\n");
  541. for (List<PropertyAPI>::Element *e = api.properties.front(); e; e = e->next()) {
  542. source.push_back("\t\t\t{\n");
  543. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  544. source.push_back("\t\t\t\t\"type\": \"" + e->get().type + "\",\n");
  545. source.push_back("\t\t\t\t\"getter\": \"" + e->get().getter + "\",\n");
  546. source.push_back("\t\t\t\t\"setter\": \"" + e->get().setter + "\",\n");
  547. source.push_back(String("\t\t\t\t\"index\": ") + itos(e->get().index) + "\n");
  548. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  549. }
  550. source.push_back("\t\t],\n");
  551. source.push_back("\t\t\"signals\": [\n");
  552. for (List<SignalAPI>::Element *e = api.signals_.front(); e; e = e->next()) {
  553. source.push_back("\t\t\t{\n");
  554. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  555. source.push_back("\t\t\t\t\"arguments\": [\n");
  556. for (int i = 0; i < e->get().argument_names.size(); i++) {
  557. source.push_back("\t\t\t\t\t{\n");
  558. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  559. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  560. source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
  561. String default_value;
  562. if (e->get().default_arguments.has(i)) {
  563. writer.write_to_string(e->get().default_arguments[i], default_value);
  564. default_value = default_value.replace("\n", "").json_escape();
  565. }
  566. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + default_value + "\"\n");
  567. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  568. }
  569. source.push_back("\t\t\t\t]\n");
  570. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  571. }
  572. source.push_back("\t\t],\n");
  573. source.push_back("\t\t\"methods\": [\n");
  574. for (List<MethodAPI>::Element *e = api.methods.front(); e; e = e->next()) {
  575. source.push_back("\t\t\t{\n");
  576. source.push_back("\t\t\t\t\"name\": \"" + e->get().method_name + "\",\n");
  577. source.push_back("\t\t\t\t\"return_type\": \"" + e->get().return_type + "\",\n");
  578. source.push_back(String("\t\t\t\t\"is_editor\": ") + (e->get().is_editor ? "true" : "false") + ",\n");
  579. source.push_back(String("\t\t\t\t\"is_noscript\": ") + (e->get().is_noscript ? "true" : "false") + ",\n");
  580. source.push_back(String("\t\t\t\t\"is_const\": ") + (e->get().is_const ? "true" : "false") + ",\n");
  581. source.push_back(String("\t\t\t\t\"is_reverse\": ") + (e->get().is_reverse ? "true" : "false") + ",\n");
  582. source.push_back(String("\t\t\t\t\"is_virtual\": ") + (e->get().is_virtual ? "true" : "false") + ",\n");
  583. source.push_back(String("\t\t\t\t\"has_varargs\": ") + (e->get().has_varargs ? "true" : "false") + ",\n");
  584. source.push_back(String("\t\t\t\t\"is_from_script\": ") + (e->get().is_from_script ? "true" : "false") + ",\n");
  585. source.push_back("\t\t\t\t\"arguments\": [\n");
  586. for (int i = 0; i < e->get().argument_names.size(); i++) {
  587. source.push_back("\t\t\t\t\t{\n");
  588. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  589. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  590. source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
  591. String default_value;
  592. if (e->get().default_arguments.has(i)) {
  593. writer.write_to_string(e->get().default_arguments[i], default_value);
  594. default_value = default_value.replace("\n", "").json_escape();
  595. }
  596. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + default_value + "\"\n");
  597. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  598. }
  599. source.push_back("\t\t\t\t]\n");
  600. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  601. }
  602. source.push_back("\t\t],\n");
  603. source.push_back("\t\t\"enums\": [\n");
  604. for (List<EnumAPI>::Element *e = api.enums.front(); e; e = e->next()) {
  605. source.push_back("\t\t\t{\n");
  606. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  607. source.push_back("\t\t\t\t\"values\": {\n");
  608. for (List<Pair<int, String>>::Element *val_e = e->get().values.front(); val_e; val_e = val_e->next()) {
  609. source.push_back("\t\t\t\t\t\"" + val_e->get().second + "\": " + itos(val_e->get().first));
  610. source.push_back(String((val_e->next() ? "," : "")) + "\n");
  611. }
  612. source.push_back("\t\t\t\t}\n");
  613. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  614. }
  615. source.push_back("\t\t]\n");
  616. source.push_back(String("\t}") + (c->next() ? "," : "") + "\n");
  617. }
  618. source.push_back("]");
  619. return source;
  620. }
  621. static int indent_level = 0;
  622. static void append_indented(StringBuilder &p_source, const String &p_text) {
  623. for (int i = 0; i < indent_level; i++) {
  624. p_source.append("\t");
  625. }
  626. p_source.append(p_text);
  627. p_source.append("\n");
  628. }
  629. static void append_indented(StringBuilder &p_source, const char *p_text) {
  630. for (int i = 0; i < indent_level; i++) {
  631. p_source.append("\t");
  632. }
  633. p_source.append(p_text);
  634. p_source.append("\n");
  635. }
  636. static void write_builtin_method(StringBuilder &p_source, const MethodAPI &p_method) {
  637. VariantWriter writer;
  638. append_indented(p_source, vformat(R"("name": "%s",)", p_method.method_name));
  639. append_indented(p_source, vformat(R"("return_type": "%s",)", p_method.return_type));
  640. append_indented(p_source, vformat(R"("is_const": %s,)", p_method.is_const ? "true" : "false"));
  641. append_indented(p_source, vformat(R"("is_static": %s,)", p_method.is_static ? "true" : "false"));
  642. append_indented(p_source, vformat(R"("has_varargs": %s,)", p_method.has_varargs ? "true" : "false"));
  643. append_indented(p_source, R"("arguments": [)");
  644. indent_level++;
  645. for (int i = 0; i < p_method.argument_count; i++) {
  646. append_indented(p_source, "{");
  647. indent_level++;
  648. append_indented(p_source, vformat(R"("name": "%s",)", p_method.argument_names[i]));
  649. append_indented(p_source, vformat(R"("type": "%s",)", p_method.argument_types[i]));
  650. append_indented(p_source, vformat(R"("has_default_value": %s,)", p_method.default_arguments.has(i) ? "true" : "false"));
  651. String default_value;
  652. if (p_method.default_arguments.has(i)) {
  653. writer.write_to_string(p_method.default_arguments[i], default_value);
  654. default_value = default_value.replace("\n", "").json_escape();
  655. }
  656. append_indented(p_source, vformat(R"("default_value": "%s")", default_value));
  657. indent_level--;
  658. append_indented(p_source, i < p_method.argument_count - 1 ? "}," : "}");
  659. }
  660. indent_level--;
  661. append_indented(p_source, "]");
  662. }
  663. static List<String> generate_c_builtin_api_json(const List<ClassAPI> &p_api) {
  664. StringBuilder source;
  665. source.append("[\n");
  666. indent_level = 1;
  667. for (const List<ClassAPI>::Element *C = p_api.front(); C; C = C->next()) {
  668. const ClassAPI &class_api = C->get();
  669. append_indented(source, "{");
  670. indent_level++;
  671. append_indented(source, vformat(R"("name": "%s",)", class_api.class_name));
  672. append_indented(source, vformat(R"("is_instantiable": %s,)", class_api.is_instantiable ? "true" : "false"));
  673. append_indented(source, vformat(R"("is_ref_counted": %s,)", class_api.is_ref_counted ? "true" : "false"));
  674. append_indented(source, vformat(R"("has_indexing": %s,)", class_api.has_indexing ? "true" : "false"));
  675. append_indented(source, vformat(R"("indexed_type": "%s",)", class_api.has_indexing && class_api.indexed_type == "Nil" ? "Variant" : class_api.indexed_type));
  676. append_indented(source, vformat(R"("is_keyed": %s,)", class_api.is_keyed ? "true" : "false"));
  677. // Constructors.
  678. append_indented(source, R"("constructors": [)");
  679. indent_level++;
  680. for (const List<MethodAPI>::Element *E = class_api.constructors.front(); E; E = E->next()) {
  681. const MethodAPI &constructor = E->get();
  682. append_indented(source, "{");
  683. indent_level++;
  684. write_builtin_method(source, constructor);
  685. indent_level--;
  686. append_indented(source, E->next() ? "}," : "}");
  687. }
  688. indent_level--;
  689. append_indented(source, "],");
  690. // Constants.
  691. append_indented(source, R"("constants": [)");
  692. indent_level++;
  693. for (const List<ConstantAPI>::Element *E = class_api.constants.front(); E; E = E->next()) {
  694. const ConstantAPI &constant = E->get();
  695. append_indented(source, "{");
  696. indent_level++;
  697. append_indented(source, vformat(R"("name": "%s",)", constant.constant_name));
  698. append_indented(source, vformat(R"("type": "%s",)", constant.builtin_constant_type));
  699. append_indented(source, vformat(R"("value": "%s")", constant.builtin_constant_value.operator String()));
  700. indent_level--;
  701. append_indented(source, E->next() ? "}," : "}");
  702. }
  703. indent_level--;
  704. append_indented(source, "],");
  705. // Methods.
  706. append_indented(source, R"("methods": [)");
  707. indent_level++;
  708. for (const List<MethodAPI>::Element *E = class_api.methods.front(); E; E = E->next()) {
  709. const MethodAPI &method = E->get();
  710. append_indented(source, "{");
  711. indent_level++;
  712. write_builtin_method(source, method);
  713. indent_level--;
  714. append_indented(source, E->next() ? "}," : "}");
  715. }
  716. indent_level--;
  717. append_indented(source, "],");
  718. // Members.
  719. append_indented(source, R"("members": [)");
  720. indent_level++;
  721. for (const List<PropertyAPI>::Element *E = class_api.properties.front(); E; E = E->next()) {
  722. const PropertyAPI &member = E->get();
  723. append_indented(source, "{");
  724. indent_level++;
  725. append_indented(source, vformat(R"("name": "%s",)", member.name));
  726. append_indented(source, vformat(R"("type": "%s")", member.type));
  727. indent_level--;
  728. append_indented(source, E->next() ? "}," : "}");
  729. }
  730. indent_level--;
  731. append_indented(source, "],");
  732. // Operators.
  733. append_indented(source, R"("operators": [)");
  734. indent_level++;
  735. for (const List<OperatorAPI>::Element *E = class_api.operators.front(); E; E = E->next()) {
  736. const OperatorAPI &oper = E->get();
  737. append_indented(source, "{");
  738. indent_level++;
  739. append_indented(source, vformat(R"("name": "%s",)", oper.name));
  740. append_indented(source, vformat(R"("operator": %d,)", oper.oper));
  741. append_indented(source, vformat(R"("other_type": "%s",)", oper.other_type));
  742. append_indented(source, vformat(R"("return_type": "%s")", oper.return_type));
  743. indent_level--;
  744. append_indented(source, E->next() ? "}," : "}");
  745. }
  746. indent_level--;
  747. append_indented(source, "]");
  748. indent_level--;
  749. append_indented(source, C->next() ? "}," : "}");
  750. }
  751. indent_level--;
  752. source.append("]\n");
  753. List<String> result;
  754. result.push_back(source.as_string());
  755. return result;
  756. }
  757. #endif
  758. /*
  759. * Saves the whole Godot API to a JSON file located at
  760. * p_path
  761. */
  762. Error generate_c_api(const String &p_path) {
  763. #ifndef TOOLS_ENABLED
  764. return ERR_BUG;
  765. #else
  766. List<ClassAPI> api = generate_c_api_classes();
  767. List<String> json_source = generate_c_api_json(api);
  768. return save_file(p_path, json_source);
  769. #endif
  770. }
  771. /*
  772. * Saves the builtin Godot API to a JSON file located at
  773. * p_path
  774. */
  775. Error generate_c_builtin_api(const String &p_path) {
  776. #ifndef TOOLS_ENABLED
  777. return ERR_BUG;
  778. #else
  779. List<ClassAPI> api = generate_c_builtin_api_types();
  780. List<String> json_source = generate_c_builtin_api_json(api);
  781. return save_file(p_path, json_source);
  782. #endif
  783. }