api_generator.cpp 33 KB

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