2
0

api_generator.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. String name = class_name;
  209. if (name.begins_with("_")) {
  210. name.remove(0);
  211. }
  212. class_api.is_singleton = Engine::get_singleton()->has_singleton(name);
  213. if (class_api.is_singleton) {
  214. class_api.singleton_name = name;
  215. }
  216. }
  217. class_api.is_instantiable = !class_api.is_singleton && ClassDB::can_instance(class_name);
  218. {
  219. List<StringName> inheriters;
  220. ClassDB::get_inheriters_from_class("RefCounted", &inheriters);
  221. bool is_ref_counted = !!inheriters.find(class_name) || class_name == "RefCounted";
  222. // @Unclear
  223. class_api.is_ref_counted = !class_api.is_singleton && is_ref_counted;
  224. }
  225. // constants
  226. {
  227. List<String> constant;
  228. ClassDB::get_integer_constant_list(class_name, &constant, true);
  229. constant.sort_custom<NoCaseComparator>();
  230. for (List<String>::Element *c = constant.front(); c != nullptr; c = c->next()) {
  231. ConstantAPI constant_api;
  232. constant_api.constant_name = c->get();
  233. constant_api.constant_value = ClassDB::get_integer_constant(class_name, c->get());
  234. class_api.constants.push_back(constant_api);
  235. }
  236. }
  237. // signals
  238. {
  239. List<MethodInfo> signals_;
  240. ClassDB::get_signal_list(class_name, &signals_, true);
  241. signals_.sort_custom<MethodInfoComparator>();
  242. for (int i = 0; i < signals_.size(); i++) {
  243. SignalAPI signal;
  244. MethodInfo method_info = signals_[i];
  245. signal.name = method_info.name;
  246. for (int j = 0; j < method_info.arguments.size(); j++) {
  247. PropertyInfo argument = method_info.arguments[j];
  248. String type;
  249. String name = argument.name;
  250. if (argument.name.find(":") != -1) {
  251. type = argument.name.get_slice(":", 1);
  252. name = argument.name.get_slice(":", 0);
  253. } else {
  254. type = get_type_name(argument);
  255. }
  256. signal.argument_names.push_back(name);
  257. signal.argument_types.push_back(type);
  258. }
  259. Vector<Variant> default_arguments = method_info.default_arguments;
  260. int default_start = signal.argument_names.size() - default_arguments.size();
  261. for (int j = 0; j < default_arguments.size(); j++) {
  262. signal.default_arguments[default_start + j] = default_arguments[j];
  263. }
  264. class_api.signals_.push_back(signal);
  265. }
  266. }
  267. //properties
  268. {
  269. List<PropertyInfo> properties;
  270. ClassDB::get_property_list(class_name, &properties, true);
  271. properties.sort_custom<PropertyInfoComparator>();
  272. for (List<PropertyInfo>::Element *p = properties.front(); p != nullptr; p = p->next()) {
  273. PropertyAPI property_api;
  274. property_api.name = p->get().name;
  275. property_api.getter = ClassDB::get_property_getter(class_name, p->get().name);
  276. property_api.setter = ClassDB::get_property_setter(class_name, p->get().name);
  277. if (p->get().name.find(":") != -1) {
  278. property_api.type = p->get().name.get_slice(":", 1);
  279. property_api.name = p->get().name.get_slice(":", 0);
  280. } else {
  281. MethodInfo minfo;
  282. ClassDB::get_method_info(class_name, property_api.getter, &minfo, true, false);
  283. property_api.type = get_type_name(minfo.return_val);
  284. }
  285. property_api.index = ClassDB::get_property_index(class_name, p->get().name);
  286. if (!property_api.setter.is_empty() || !property_api.getter.is_empty()) {
  287. class_api.properties.push_back(property_api);
  288. }
  289. }
  290. }
  291. //methods
  292. {
  293. List<MethodInfo> methods;
  294. ClassDB::get_method_list(class_name, &methods, true);
  295. methods.sort_custom<MethodInfoComparator>();
  296. for (List<MethodInfo>::Element *m = methods.front(); m != nullptr; m = m->next()) {
  297. MethodAPI method_api;
  298. MethodBind *method_bind = ClassDB::get_method(class_name, m->get().name);
  299. MethodInfo &method_info = m->get();
  300. //method name
  301. method_api.method_name = method_info.name;
  302. //method return type
  303. if (method_api.method_name.find(":") != -1) {
  304. method_api.return_type = method_api.method_name.get_slice(":", 1);
  305. method_api.method_name = method_api.method_name.get_slice(":", 0);
  306. } else {
  307. method_api.return_type = get_type_name(m->get().return_val);
  308. }
  309. method_api.argument_count = method_info.arguments.size();
  310. method_api.has_varargs = method_bind && method_bind->is_vararg();
  311. // Method flags
  312. method_api.is_virtual = false;
  313. if (method_info.flags) {
  314. const uint32_t flags = method_info.flags;
  315. method_api.is_editor = flags & METHOD_FLAG_EDITOR;
  316. method_api.is_noscript = flags & METHOD_FLAG_NOSCRIPT;
  317. method_api.is_const = flags & METHOD_FLAG_CONST;
  318. method_api.is_reverse = flags & METHOD_FLAG_REVERSE;
  319. method_api.is_virtual = flags & METHOD_FLAG_VIRTUAL;
  320. method_api.is_from_script = flags & METHOD_FLAG_FROM_SCRIPT;
  321. }
  322. method_api.is_virtual = method_api.is_virtual || method_api.method_name[0] == '_';
  323. // method argument name and type
  324. for (int i = 0; i < method_api.argument_count; i++) {
  325. String arg_name;
  326. String arg_type;
  327. PropertyInfo arg_info = method_info.arguments[i];
  328. arg_name = arg_info.name;
  329. if (arg_info.name.find(":") != -1) {
  330. arg_type = arg_info.name.get_slice(":", 1);
  331. arg_name = arg_info.name.get_slice(":", 0);
  332. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  333. arg_type = arg_info.class_name;
  334. } else if (arg_info.type == Variant::NIL) {
  335. arg_type = "Variant";
  336. } else if (arg_info.type == Variant::OBJECT) {
  337. arg_type = arg_info.class_name;
  338. if (arg_type == "") {
  339. arg_type = Variant::get_type_name(arg_info.type);
  340. }
  341. } else {
  342. arg_type = Variant::get_type_name(arg_info.type);
  343. }
  344. method_api.argument_names.push_back(arg_name);
  345. method_api.argument_types.push_back(arg_type);
  346. if (method_bind && method_bind->has_default_argument(i)) {
  347. method_api.default_arguments[i] = method_bind->get_default_argument(i);
  348. }
  349. }
  350. class_api.methods.push_back(method_api);
  351. }
  352. }
  353. // enums
  354. {
  355. List<EnumAPI> enums;
  356. List<StringName> enum_names;
  357. ClassDB::get_enum_list(class_name, &enum_names, true);
  358. for (List<StringName>::Element *E = enum_names.front(); E; E = E->next()) {
  359. List<StringName> value_names;
  360. EnumAPI enum_api;
  361. enum_api.name = E->get();
  362. ClassDB::get_enum_constants(class_name, E->get(), &value_names, true);
  363. for (List<StringName>::Element *val_e = value_names.front(); val_e; val_e = val_e->next()) {
  364. int int_val = ClassDB::get_integer_constant(class_name, val_e->get(), nullptr);
  365. enum_api.values.push_back(Pair<int, String>(int_val, val_e->get()));
  366. }
  367. enum_api.values.sort_custom<PairSort<int, String>>();
  368. class_api.enums.push_back(enum_api);
  369. }
  370. }
  371. api.push_back(class_api);
  372. }
  373. return api;
  374. }
  375. /*
  376. * Reads the builtin Variant API to a list
  377. */
  378. List<ClassAPI> generate_c_builtin_api_types() {
  379. List<ClassAPI> api;
  380. // Special class for the utility methods.
  381. {
  382. ClassAPI utility_api;
  383. utility_api.class_name = "Utilities";
  384. utility_api.is_instantiable = false;
  385. List<StringName> utility_functions;
  386. Variant::get_utility_function_list(&utility_functions);
  387. for (const List<StringName>::Element *E = utility_functions.front(); E; E = E->next()) {
  388. const StringName &function_name = E->get();
  389. MethodAPI function_api;
  390. function_api.method_name = function_name;
  391. function_api.has_varargs = Variant::is_utility_function_vararg(function_name);
  392. function_api.argument_count = function_api.has_varargs ? 0 : Variant::get_utility_function_argument_count(function_name);
  393. function_api.is_const = Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH;
  394. for (int i = 0; i < function_api.argument_count; i++) {
  395. function_api.argument_names.push_back(Variant::get_utility_function_argument_name(function_name, i));
  396. Variant::Type arg_type = Variant::get_utility_function_argument_type(function_name, i);
  397. function_api.argument_types.push_back(arg_type == Variant::NIL ? "Variant" : Variant::get_type_name(arg_type));
  398. }
  399. if (Variant::has_utility_function_return_value(function_name)) {
  400. Variant::Type ret_type = Variant::get_utility_function_return_type(function_name);
  401. function_api.return_type = ret_type == Variant::NIL ? "Variant" : Variant::get_type_name(ret_type);
  402. } else {
  403. function_api.return_type = "void";
  404. }
  405. utility_api.methods.push_back(function_api);
  406. }
  407. api.push_back(utility_api);
  408. }
  409. for (int t = 0; t < Variant::VARIANT_MAX; t++) {
  410. Variant::Type type = (Variant::Type)t;
  411. ClassAPI class_api;
  412. class_api.class_name = Variant::get_type_name(type);
  413. class_api.is_instantiable = true;
  414. class_api.has_indexing = Variant::has_indexing(type);
  415. class_api.indexed_type = Variant::get_type_name(Variant::get_indexed_element_type(type));
  416. class_api.is_keyed = Variant::is_keyed(type);
  417. // Types that are passed by reference.
  418. switch (type) {
  419. case Variant::OBJECT:
  420. case Variant::DICTIONARY:
  421. case Variant::ARRAY:
  422. case Variant::PACKED_BYTE_ARRAY:
  423. case Variant::PACKED_INT32_ARRAY:
  424. case Variant::PACKED_INT64_ARRAY:
  425. case Variant::PACKED_FLOAT32_ARRAY:
  426. case Variant::PACKED_FLOAT64_ARRAY:
  427. case Variant::PACKED_STRING_ARRAY:
  428. case Variant::PACKED_VECTOR2_ARRAY:
  429. case Variant::PACKED_VECTOR3_ARRAY:
  430. case Variant::PACKED_COLOR_ARRAY:
  431. class_api.is_ref_counted = true;
  432. break;
  433. default:
  434. class_api.is_ref_counted = false;
  435. break;
  436. }
  437. // Methods.
  438. List<StringName> methods;
  439. Variant::get_builtin_method_list(type, &methods);
  440. for (const List<StringName>::Element *E = methods.front(); E; E = E->next()) {
  441. const StringName &method_name = E->get();
  442. MethodAPI method_api;
  443. method_api.method_name = method_name;
  444. method_api.argument_count = Variant::get_builtin_method_argument_count(type, method_name);
  445. method_api.has_varargs = Variant::is_builtin_method_vararg(type, method_name);
  446. method_api.is_const = Variant::is_builtin_method_const(type, method_name);
  447. method_api.is_static = Variant::is_builtin_method_static(type, method_name);
  448. for (int i = 0; i < method_api.argument_count; i++) {
  449. method_api.argument_names.push_back(Variant::get_builtin_method_argument_name(type, method_name, i));
  450. Variant::Type arg_type = Variant::get_builtin_method_argument_type(type, method_name, i);
  451. method_api.argument_types.push_back(arg_type == Variant::NIL ? "Variant" : Variant::get_type_name(arg_type));
  452. }
  453. Vector<Variant> default_arguments = Variant::get_builtin_method_default_arguments(type, method_name);
  454. int default_start = method_api.argument_names.size() - default_arguments.size();
  455. for (int i = 0; i < default_arguments.size(); i++) {
  456. method_api.default_arguments[default_start + i] = default_arguments[i];
  457. }
  458. if (Variant::has_builtin_method_return_value(type, method_name)) {
  459. Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);
  460. method_api.return_type = ret_type == Variant::NIL ? "Variant" : Variant::get_type_name(ret_type);
  461. } else {
  462. method_api.return_type = "void";
  463. }
  464. class_api.methods.push_back(method_api);
  465. }
  466. // Constructors.
  467. for (int c = 0; c < Variant::get_constructor_count(type); c++) {
  468. MethodAPI constructor_api;
  469. constructor_api.method_name = Variant::get_type_name(type);
  470. constructor_api.argument_count = Variant::get_constructor_argument_count(type, c);
  471. constructor_api.return_type = Variant::get_type_name(type);
  472. for (int i = 0; i < constructor_api.argument_count; i++) {
  473. constructor_api.argument_names.push_back(Variant::get_constructor_argument_name(type, c, i));
  474. Variant::Type arg_type = Variant::get_constructor_argument_type(type, c, i);
  475. constructor_api.argument_types.push_back(arg_type == Variant::NIL ? "Variant" : Variant::get_type_name(arg_type));
  476. }
  477. class_api.constructors.push_back(constructor_api);
  478. }
  479. // Constants.
  480. List<StringName> constants;
  481. Variant::get_constants_for_type(type, &constants);
  482. for (const List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  483. const StringName &constant_name = E->get();
  484. ConstantAPI constant_api;
  485. constant_api.constant_name = constant_name;
  486. constant_api.builtin_constant_value = Variant::get_constant_value(type, constant_name);
  487. constant_api.builtin_constant_type = Variant::get_type_name(constant_api.builtin_constant_value.get_type());
  488. class_api.constants.push_back(constant_api);
  489. }
  490. // Members.
  491. List<StringName> members;
  492. Variant::get_member_list(type, &members);
  493. for (const List<StringName>::Element *E = members.front(); E; E = E->next()) {
  494. const StringName &member_name = E->get();
  495. PropertyAPI member_api;
  496. member_api.name = member_name;
  497. Variant::Type member_type = Variant::get_member_type(type, member_name);
  498. member_api.type = member_type == Variant::NIL ? "Variant" : Variant::get_type_name(member_type);
  499. class_api.properties.push_back(member_api);
  500. }
  501. // Operators.
  502. for (int op = 0; op < Variant::OP_MAX; op++) {
  503. Variant::Operator oper = (Variant::Operator)op;
  504. for (int ot = 0; ot < Variant::VARIANT_MAX; ot++) {
  505. Variant::Type other_type = (Variant::Type)ot;
  506. if (!Variant::get_validated_operator_evaluator(oper, type, other_type)) {
  507. continue;
  508. }
  509. OperatorAPI oper_api;
  510. oper_api.name = Variant::get_operator_name(oper);
  511. oper_api.oper = oper;
  512. oper_api.other_type = Variant::get_type_name(other_type);
  513. oper_api.return_type = Variant::get_type_name(Variant::get_operator_return_type(oper, type, other_type));
  514. class_api.operators.push_back(oper_api);
  515. }
  516. }
  517. api.push_back(class_api);
  518. }
  519. return api;
  520. }
  521. /*
  522. * Generates the JSON source from the API in p_api
  523. */
  524. static List<String> generate_c_api_json(const List<ClassAPI> &p_api) {
  525. // I'm sorry for the \t mess
  526. List<String> source;
  527. VariantWriter writer;
  528. source.push_back("[\n");
  529. for (const List<ClassAPI>::Element *c = p_api.front(); c != nullptr; c = c->next()) {
  530. ClassAPI api = c->get();
  531. source.push_back("\t{\n");
  532. source.push_back("\t\t\"name\": \"" + api.class_name + "\",\n");
  533. source.push_back("\t\t\"base_class\": \"" + api.super_class_name + "\",\n");
  534. source.push_back(String("\t\t\"api_type\": \"") + (api.api_type == ClassDB::API_CORE ? "core" : (api.api_type == ClassDB::API_EDITOR ? "tools" : "none")) + "\",\n");
  535. source.push_back(String("\t\t\"singleton\": ") + (api.is_singleton ? "true" : "false") + ",\n");
  536. source.push_back("\t\t\"singleton_name\": \"" + api.singleton_name + "\",\n");
  537. source.push_back(String("\t\t\"instantiable\": ") + (api.is_instantiable ? "true" : "false") + ",\n");
  538. source.push_back(String("\t\t\"is_ref_counted\": ") + (api.is_ref_counted ? "true" : "false") + ",\n");
  539. source.push_back("\t\t\"constants\": {\n");
  540. for (List<ConstantAPI>::Element *e = api.constants.front(); e; e = e->next()) {
  541. source.push_back("\t\t\t\"" + e->get().constant_name + "\": " + String::num_int64(e->get().constant_value) + (e->next() ? "," : "") + "\n");
  542. }
  543. source.push_back("\t\t},\n");
  544. source.push_back("\t\t\"properties\": [\n");
  545. for (List<PropertyAPI>::Element *e = api.properties.front(); e; e = e->next()) {
  546. source.push_back("\t\t\t{\n");
  547. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  548. source.push_back("\t\t\t\t\"type\": \"" + e->get().type + "\",\n");
  549. source.push_back("\t\t\t\t\"getter\": \"" + e->get().getter + "\",\n");
  550. source.push_back("\t\t\t\t\"setter\": \"" + e->get().setter + "\",\n");
  551. source.push_back(String("\t\t\t\t\"index\": ") + itos(e->get().index) + "\n");
  552. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  553. }
  554. source.push_back("\t\t],\n");
  555. source.push_back("\t\t\"signals\": [\n");
  556. for (List<SignalAPI>::Element *e = api.signals_.front(); e; e = e->next()) {
  557. source.push_back("\t\t\t{\n");
  558. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  559. source.push_back("\t\t\t\t\"arguments\": [\n");
  560. for (int i = 0; i < e->get().argument_names.size(); i++) {
  561. source.push_back("\t\t\t\t\t{\n");
  562. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  563. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  564. source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
  565. String default_value;
  566. if (e->get().default_arguments.has(i)) {
  567. writer.write_to_string(e->get().default_arguments[i], default_value);
  568. default_value = default_value.replace("\n", "").json_escape();
  569. }
  570. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + default_value + "\"\n");
  571. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  572. }
  573. source.push_back("\t\t\t\t]\n");
  574. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  575. }
  576. source.push_back("\t\t],\n");
  577. source.push_back("\t\t\"methods\": [\n");
  578. for (List<MethodAPI>::Element *e = api.methods.front(); e; e = e->next()) {
  579. source.push_back("\t\t\t{\n");
  580. source.push_back("\t\t\t\t\"name\": \"" + e->get().method_name + "\",\n");
  581. source.push_back("\t\t\t\t\"return_type\": \"" + e->get().return_type + "\",\n");
  582. source.push_back(String("\t\t\t\t\"is_editor\": ") + (e->get().is_editor ? "true" : "false") + ",\n");
  583. source.push_back(String("\t\t\t\t\"is_noscript\": ") + (e->get().is_noscript ? "true" : "false") + ",\n");
  584. source.push_back(String("\t\t\t\t\"is_const\": ") + (e->get().is_const ? "true" : "false") + ",\n");
  585. source.push_back(String("\t\t\t\t\"is_reverse\": ") + (e->get().is_reverse ? "true" : "false") + ",\n");
  586. source.push_back(String("\t\t\t\t\"is_virtual\": ") + (e->get().is_virtual ? "true" : "false") + ",\n");
  587. source.push_back(String("\t\t\t\t\"has_varargs\": ") + (e->get().has_varargs ? "true" : "false") + ",\n");
  588. source.push_back(String("\t\t\t\t\"is_from_script\": ") + (e->get().is_from_script ? "true" : "false") + ",\n");
  589. source.push_back("\t\t\t\t\"arguments\": [\n");
  590. for (int i = 0; i < e->get().argument_names.size(); i++) {
  591. source.push_back("\t\t\t\t\t{\n");
  592. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  593. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  594. source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
  595. String default_value;
  596. if (e->get().default_arguments.has(i)) {
  597. writer.write_to_string(e->get().default_arguments[i], default_value);
  598. default_value = default_value.replace("\n", "").json_escape();
  599. }
  600. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + default_value + "\"\n");
  601. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  602. }
  603. source.push_back("\t\t\t\t]\n");
  604. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  605. }
  606. source.push_back("\t\t],\n");
  607. source.push_back("\t\t\"enums\": [\n");
  608. for (List<EnumAPI>::Element *e = api.enums.front(); e; e = e->next()) {
  609. source.push_back("\t\t\t{\n");
  610. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  611. source.push_back("\t\t\t\t\"values\": {\n");
  612. for (List<Pair<int, String>>::Element *val_e = e->get().values.front(); val_e; val_e = val_e->next()) {
  613. source.push_back("\t\t\t\t\t\"" + val_e->get().second + "\": " + itos(val_e->get().first));
  614. source.push_back(String((val_e->next() ? "," : "")) + "\n");
  615. }
  616. source.push_back("\t\t\t\t}\n");
  617. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  618. }
  619. source.push_back("\t\t]\n");
  620. source.push_back(String("\t}") + (c->next() ? "," : "") + "\n");
  621. }
  622. source.push_back("]");
  623. return source;
  624. }
  625. static int indent_level = 0;
  626. static void append_indented(StringBuilder &p_source, const String &p_text) {
  627. for (int i = 0; i < indent_level; i++) {
  628. p_source.append("\t");
  629. }
  630. p_source.append(p_text);
  631. p_source.append("\n");
  632. }
  633. static void append_indented(StringBuilder &p_source, const char *p_text) {
  634. for (int i = 0; i < indent_level; i++) {
  635. p_source.append("\t");
  636. }
  637. p_source.append(p_text);
  638. p_source.append("\n");
  639. }
  640. static void write_builtin_method(StringBuilder &p_source, const MethodAPI &p_method) {
  641. VariantWriter writer;
  642. append_indented(p_source, vformat(R"("name": "%s",)", p_method.method_name));
  643. append_indented(p_source, vformat(R"("return_type": "%s",)", p_method.return_type));
  644. append_indented(p_source, vformat(R"("is_const": %s,)", p_method.is_const ? "true" : "false"));
  645. append_indented(p_source, vformat(R"("is_static": %s,)", p_method.is_static ? "true" : "false"));
  646. append_indented(p_source, vformat(R"("has_varargs": %s,)", p_method.has_varargs ? "true" : "false"));
  647. append_indented(p_source, R"("arguments": [)");
  648. indent_level++;
  649. for (int i = 0; i < p_method.argument_count; i++) {
  650. append_indented(p_source, "{");
  651. indent_level++;
  652. append_indented(p_source, vformat(R"("name": "%s",)", p_method.argument_names[i]));
  653. append_indented(p_source, vformat(R"("type": "%s",)", p_method.argument_types[i]));
  654. append_indented(p_source, vformat(R"("has_default_value": %s,)", p_method.default_arguments.has(i) ? "true" : "false"));
  655. String default_value;
  656. if (p_method.default_arguments.has(i)) {
  657. writer.write_to_string(p_method.default_arguments[i], default_value);
  658. default_value = default_value.replace("\n", "").json_escape();
  659. }
  660. append_indented(p_source, vformat(R"("default_value": "%s")", default_value));
  661. indent_level--;
  662. append_indented(p_source, i < p_method.argument_count - 1 ? "}," : "}");
  663. }
  664. indent_level--;
  665. append_indented(p_source, "]");
  666. }
  667. static List<String> generate_c_builtin_api_json(const List<ClassAPI> &p_api) {
  668. StringBuilder source;
  669. source.append("[\n");
  670. indent_level = 1;
  671. for (const List<ClassAPI>::Element *C = p_api.front(); C; C = C->next()) {
  672. const ClassAPI &class_api = C->get();
  673. append_indented(source, "{");
  674. indent_level++;
  675. append_indented(source, vformat(R"("name": "%s",)", class_api.class_name));
  676. append_indented(source, vformat(R"("is_instantiable": %s,)", class_api.is_instantiable ? "true" : "false"));
  677. append_indented(source, vformat(R"("is_ref_counted": %s,)", class_api.is_ref_counted ? "true" : "false"));
  678. append_indented(source, vformat(R"("has_indexing": %s,)", class_api.has_indexing ? "true" : "false"));
  679. append_indented(source, vformat(R"("indexed_type": "%s",)", class_api.has_indexing && class_api.indexed_type == "Nil" ? "Variant" : class_api.indexed_type));
  680. append_indented(source, vformat(R"("is_keyed": %s,)", class_api.is_keyed ? "true" : "false"));
  681. // Constructors.
  682. append_indented(source, R"("constructors": [)");
  683. indent_level++;
  684. for (const List<MethodAPI>::Element *E = class_api.constructors.front(); E; E = E->next()) {
  685. const MethodAPI &constructor = E->get();
  686. append_indented(source, "{");
  687. indent_level++;
  688. write_builtin_method(source, constructor);
  689. indent_level--;
  690. append_indented(source, E->next() ? "}," : "}");
  691. }
  692. indent_level--;
  693. append_indented(source, "],");
  694. // Constants.
  695. append_indented(source, R"("constants": [)");
  696. indent_level++;
  697. for (const List<ConstantAPI>::Element *E = class_api.constants.front(); E; E = E->next()) {
  698. const ConstantAPI &constant = E->get();
  699. append_indented(source, "{");
  700. indent_level++;
  701. append_indented(source, vformat(R"("name": "%s",)", constant.constant_name));
  702. append_indented(source, vformat(R"("type": "%s",)", constant.builtin_constant_type));
  703. append_indented(source, vformat(R"("value": "%s")", constant.builtin_constant_value.operator String()));
  704. indent_level--;
  705. append_indented(source, E->next() ? "}," : "}");
  706. }
  707. indent_level--;
  708. append_indented(source, "],");
  709. // Methods.
  710. append_indented(source, R"("methods": [)");
  711. indent_level++;
  712. for (const List<MethodAPI>::Element *E = class_api.methods.front(); E; E = E->next()) {
  713. const MethodAPI &method = E->get();
  714. append_indented(source, "{");
  715. indent_level++;
  716. write_builtin_method(source, method);
  717. indent_level--;
  718. append_indented(source, E->next() ? "}," : "}");
  719. }
  720. indent_level--;
  721. append_indented(source, "],");
  722. // Members.
  723. append_indented(source, R"("members": [)");
  724. indent_level++;
  725. for (const List<PropertyAPI>::Element *E = class_api.properties.front(); E; E = E->next()) {
  726. const PropertyAPI &member = E->get();
  727. append_indented(source, "{");
  728. indent_level++;
  729. append_indented(source, vformat(R"("name": "%s",)", member.name));
  730. append_indented(source, vformat(R"("type": "%s")", member.type));
  731. indent_level--;
  732. append_indented(source, E->next() ? "}," : "}");
  733. }
  734. indent_level--;
  735. append_indented(source, "],");
  736. // Operators.
  737. append_indented(source, R"("operators": [)");
  738. indent_level++;
  739. for (const List<OperatorAPI>::Element *E = class_api.operators.front(); E; E = E->next()) {
  740. const OperatorAPI &oper = E->get();
  741. append_indented(source, "{");
  742. indent_level++;
  743. append_indented(source, vformat(R"("name": "%s",)", oper.name));
  744. append_indented(source, vformat(R"("operator": %d,)", oper.oper));
  745. append_indented(source, vformat(R"("other_type": "%s",)", oper.other_type));
  746. append_indented(source, vformat(R"("return_type": "%s")", oper.return_type));
  747. indent_level--;
  748. append_indented(source, E->next() ? "}," : "}");
  749. }
  750. indent_level--;
  751. append_indented(source, "]");
  752. indent_level--;
  753. append_indented(source, C->next() ? "}," : "}");
  754. }
  755. indent_level--;
  756. source.append("]\n");
  757. List<String> result;
  758. result.push_back(source.as_string());
  759. return result;
  760. }
  761. #endif
  762. /*
  763. * Saves the whole Godot API to a JSON file located at
  764. * p_path
  765. */
  766. Error generate_c_api(const String &p_path) {
  767. #ifndef TOOLS_ENABLED
  768. return ERR_BUG;
  769. #else
  770. List<ClassAPI> api = generate_c_api_classes();
  771. List<String> json_source = generate_c_api_json(api);
  772. return save_file(p_path, json_source);
  773. #endif
  774. }
  775. /*
  776. * Saves the builtin Godot API to a JSON file located at
  777. * p_path
  778. */
  779. Error generate_c_builtin_api(const String &p_path) {
  780. #ifndef TOOLS_ENABLED
  781. return ERR_BUG;
  782. #else
  783. List<ClassAPI> api = generate_c_builtin_api_types();
  784. List<String> json_source = generate_c_builtin_api_json(api);
  785. return save_file(p_path, json_source);
  786. #endif
  787. }