binding_generator.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #!/usr/bin/env python
  2. import json
  3. # comment.
  4. classes = []
  5. def generate_bindings(path):
  6. global classes
  7. classes = json.load(open(path))
  8. icalls = set()
  9. for c in classes:
  10. # print c['name']
  11. used_classes = get_used_classes(c)
  12. header = generate_class_header(used_classes, c)
  13. impl = generate_class_implementation(icalls, used_classes, c)
  14. header_file = open("include/gen/" + strip_name(c["name"]) + ".hpp", "w+")
  15. header_file.write(header)
  16. source_file = open("src/gen/" + strip_name(c["name"]) + ".cpp", "w+")
  17. source_file.write(impl)
  18. icall_header_file = open("include/gen/__icalls.hpp", "w+")
  19. icall_header_file.write(generate_icall_header(icalls))
  20. register_types_file = open("src/gen/__register_types.cpp", "w+")
  21. register_types_file.write(generate_type_registry(classes))
  22. init_method_bindings_file = open("src/gen/__init_method_bindings.cpp", "w+")
  23. init_method_bindings_file.write(generate_init_method_bindings(classes))
  24. def is_reference_type(t):
  25. for c in classes:
  26. if c['name'] != t:
  27. continue
  28. if c['is_reference']:
  29. return True
  30. return False
  31. def make_gdnative_type(t, ref_allowed):
  32. if is_enum(t):
  33. return remove_enum_prefix(t) + " "
  34. elif is_class_type(t):
  35. if is_reference_type(t) and ref_allowed:
  36. return "Ref<" + strip_name(t) + "> "
  37. else:
  38. return strip_name(t) + " *"
  39. else:
  40. if t == "int":
  41. return "int64_t "
  42. if t == "float" or t == "real":
  43. return "real_t "
  44. return strip_name(t) + " "
  45. def generate_class_header(used_classes, c):
  46. source = []
  47. source.append("#ifndef GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
  48. source.append("#define GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
  49. source.append("")
  50. source.append("")
  51. source.append("#include <gdnative_api_struct.gen.h>")
  52. source.append("#include <stdint.h>")
  53. source.append("")
  54. source.append("#include <core/CoreTypes.hpp>")
  55. class_name = strip_name(c["name"])
  56. # Ref<T> is not included in object.h in Godot either,
  57. # so don't include it here because it's not needed
  58. if class_name != "Object" and class_name != "Reference":
  59. source.append("#include <core/Ref.hpp>")
  60. ref_allowed = True
  61. else:
  62. source.append("#include <core/TagDB.hpp>")
  63. ref_allowed = False
  64. included = []
  65. for used_class in used_classes:
  66. if is_enum(used_class) and is_nested_type(used_class):
  67. used_class_name = remove_enum_prefix(extract_nested_type(used_class))
  68. if used_class_name not in included:
  69. included.append(used_class_name)
  70. source.append("#include \"" + used_class_name + ".hpp\"")
  71. elif is_enum(used_class) and is_nested_type(used_class) and not is_nested_type(used_class, class_name):
  72. used_class_name = remove_enum_prefix(used_class)
  73. if used_class_name not in included:
  74. included.append(used_class_name)
  75. source.append("#include \"" + used_class_name + ".hpp\"")
  76. source.append("")
  77. if c["base_class"] != "":
  78. source.append("#include \"" + strip_name(c["base_class"]) + ".hpp\"")
  79. source.append("namespace godot {")
  80. source.append("")
  81. for used_type in used_classes:
  82. if is_enum(used_type) or is_nested_type(used_type, class_name):
  83. continue
  84. else:
  85. source.append("class " + strip_name(used_type) + ";")
  86. source.append("")
  87. vararg_templates = ""
  88. # generate the class definition here
  89. source.append("class " + class_name + (" : public _Wrapped" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {")
  90. if c["base_class"] == "":
  91. source.append("public: enum { ___CLASS_IS_SCRIPT = 0, };")
  92. source.append("")
  93. source.append("private:")
  94. if c["singleton"]:
  95. source.append("\tstatic " + class_name + " *_singleton;")
  96. source.append("")
  97. source.append("\t" + class_name + "();")
  98. source.append("")
  99. # Generate method table
  100. source.append("\tstruct ___method_bindings {")
  101. for method in c["methods"]:
  102. source.append("\t\tgodot_method_bind *mb_" + method["name"] + ";")
  103. source.append("\t};")
  104. source.append("\tstatic ___method_bindings ___mb;")
  105. source.append("\tstatic void *_detail_class_tag;")
  106. source.append("")
  107. source.append("public:")
  108. source.append("\tstatic void ___init_method_bindings();")
  109. # class id from core engine for casting
  110. source.append("\tinline static size_t ___get_id() { return (size_t)_detail_class_tag; }")
  111. source.append("")
  112. if c["singleton"]:
  113. source.append("\tstatic inline " + class_name + " *get_singleton()")
  114. source.append("\t{")
  115. source.append("\t\tif (!" + class_name + "::_singleton) {")
  116. source.append("\t\t\t" + class_name + "::_singleton = new " + class_name + ";")
  117. source.append("\t\t}")
  118. source.append("\t\treturn " + class_name + "::_singleton;")
  119. source.append("\t}")
  120. source.append("")
  121. # godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");"
  122. # ___get_class_name
  123. source.append("\tstatic inline const char *___get_class_name() { return (const char *) \"" + strip_name(c["name"]) + "\"; }")
  124. source.append("\tstatic inline Object *___get_from_variant(Variant a) { godot_object *o = (godot_object*) a; return (o) ? (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, o) : nullptr; }")
  125. enum_values = []
  126. source.append("\n\t// enums")
  127. for enum in c["enums"]:
  128. source.append("\tenum " + strip_name(enum["name"]) + " {")
  129. for value in enum["values"]:
  130. source.append("\t\t" + remove_nested_type_prefix(value) + " = " + str(enum["values"][value]) + ",")
  131. enum_values.append(value)
  132. source.append("\t};")
  133. source.append("\n\t// constants")
  134. for name in c["constants"]:
  135. if name not in enum_values:
  136. source.append("\tconst static int " + name + " = " + str(c["constants"][name]) + ";")
  137. if c["instanciable"]:
  138. source.append("")
  139. source.append("")
  140. source.append("\tstatic " + class_name + " *_new();")
  141. source.append("\n\t// methods")
  142. if class_name == "Object":
  143. source.append("#ifndef GODOT_CPP_NO_OBJECT_CAST")
  144. source.append("\ttemplate<class T>")
  145. source.append("\tstatic T *cast_to(const Object *obj);")
  146. source.append("#endif")
  147. source.append("")
  148. for method in c["methods"]:
  149. method_signature = ""
  150. # TODO decide what to do about virtual methods
  151. # method_signature += "virtual " if method["is_virtual"] else ""
  152. method_signature += make_gdnative_type(method["return_type"], ref_allowed)
  153. method_name = escape_cpp(method["name"])
  154. method_signature += method_name + "("
  155. has_default_argument = False
  156. method_arguments = ""
  157. for i, argument in enumerate(method["arguments"]):
  158. method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
  159. argument_name = escape_cpp(argument["name"])
  160. method_signature += argument_name
  161. method_arguments += argument_name
  162. # default arguments
  163. def escape_default_arg(_type, default_value):
  164. if _type == "Color":
  165. return "Color(" + default_value + ")"
  166. if _type == "bool" or _type == "int":
  167. return default_value.lower()
  168. if _type == "Array":
  169. return "Array()"
  170. if _type in ["PoolVector2Array", "PoolStringArray", "PoolVector3Array", "PoolColorArray", "PoolIntArray", "PoolRealArray"]:
  171. return _type + "()"
  172. if _type == "Vector2":
  173. return "Vector2" + default_value
  174. if _type == "Vector3":
  175. return "Vector3" + default_value
  176. if _type == "Transform":
  177. return "Transform()"
  178. if _type == "Transform2D":
  179. return "Transform2D()"
  180. if _type == "Rect2":
  181. return "Rect2" + default_value
  182. if _type == "Variant":
  183. return "Variant()" if default_value == "Null" else default_value
  184. if _type == "String":
  185. return "\"" + default_value + "\""
  186. if _type == "RID":
  187. return "RID()"
  188. if default_value == "Null" or default_value == "[Object:null]":
  189. return "nullptr"
  190. return default_value
  191. if argument["has_default_value"] or has_default_argument:
  192. method_signature += " = " + escape_default_arg(argument["type"], argument["default_value"])
  193. has_default_argument = True
  194. if i != len(method["arguments"]) - 1:
  195. method_signature += ", "
  196. method_arguments += ","
  197. if method["has_varargs"]:
  198. if len(method["arguments"]) > 0:
  199. method_signature += ", "
  200. method_arguments += ", "
  201. vararg_templates += "\ttemplate <class... Args> " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "Array::make(args...));\n\t}\n"""
  202. method_signature += "const Array& __var_args = Array()"
  203. method_signature += ")" + (" const" if method["is_const"] else "")
  204. source.append("\t" + method_signature + ";")
  205. source.append(vararg_templates)
  206. source.append("};")
  207. source.append("")
  208. source.append("}")
  209. source.append("")
  210. source.append("#endif")
  211. return "\n".join(source)
  212. def generate_class_implementation(icalls, used_classes, c):
  213. class_name = strip_name(c["name"])
  214. ref_allowed = class_name != "Object" and class_name != "Reference"
  215. source = []
  216. source.append("#include \"" + class_name + ".hpp\"")
  217. source.append("")
  218. source.append("")
  219. source.append("#include <core/GodotGlobal.hpp>")
  220. source.append("#include <core/CoreTypes.hpp>")
  221. source.append("#include <core/Ref.hpp>")
  222. source.append("#include <core/Godot.hpp>")
  223. source.append("")
  224. source.append("#include \"__icalls.hpp\"")
  225. source.append("")
  226. source.append("")
  227. for used_class in used_classes:
  228. if is_enum(used_class):
  229. continue
  230. else:
  231. source.append("#include \"" + strip_name(used_class) + ".hpp\"")
  232. source.append("")
  233. source.append("")
  234. source.append("namespace godot {")
  235. core_object_name = "this"
  236. source.append("")
  237. source.append("")
  238. if c["singleton"]:
  239. source.append("" + class_name + " *" + class_name + "::_singleton = NULL;")
  240. source.append("")
  241. source.append("")
  242. # FIXME Test if inlining has a huge impact on binary size
  243. source.append(class_name + "::" + class_name + "() {")
  244. source.append("\t_owner = godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");")
  245. source.append("}")
  246. source.append("")
  247. source.append("")
  248. # Method table initialization
  249. source.append(class_name + "::___method_bindings " + class_name + "::___mb = {};")
  250. source.append("")
  251. source.append("void *" + class_name + "::_detail_class_tag = nullptr;")
  252. source.append("")
  253. source.append("void " + class_name + "::___init_method_bindings() {")
  254. for method in c["methods"]:
  255. source.append("\t___mb.mb_" + method["name"] + " = godot::api->godot_method_bind_get_method(\"" + c["name"] + "\", \"" + method["name"] + "\");")
  256. source.append("\tgodot_string_name class_name;")
  257. source.append("\tgodot::api->godot_string_name_new_data(&class_name, \"" + c["name"] + "\");")
  258. source.append("\t_detail_class_tag = godot::core_1_2_api->godot_get_class_tag(&class_name);")
  259. source.append("\tgodot::api->godot_string_name_destroy(&class_name);")
  260. source.append("}")
  261. source.append("")
  262. if c["instanciable"]:
  263. source.append(class_name + " *" + strip_name(c["name"]) + "::_new()")
  264. source.append("{")
  265. source.append("\treturn (" + class_name + " *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, godot::api->godot_get_class_constructor((char *)\"" + c["name"] + "\")());")
  266. source.append("}")
  267. for method in c["methods"]:
  268. method_signature = ""
  269. method_signature += make_gdnative_type(method["return_type"], ref_allowed)
  270. method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "("
  271. for i, argument in enumerate(method["arguments"]):
  272. method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
  273. method_signature += escape_cpp(argument["name"])
  274. if i != len(method["arguments"]) - 1:
  275. method_signature += ", "
  276. if method["has_varargs"]:
  277. if len(method["arguments"]) > 0:
  278. method_signature += ", "
  279. method_signature += "const Array& __var_args"
  280. method_signature += ")" + (" const" if method["is_const"] else "")
  281. source.append(method_signature + " {")
  282. if method["name"] == "free":
  283. # dirty hack because Object::free is marked virtual but doesn't actually exist...
  284. source.append("\tgodot::api->godot_object_destroy(_owner);")
  285. source.append("}")
  286. source.append("")
  287. continue
  288. return_statement = ""
  289. return_type_is_ref = is_reference_type(method["return_type"]) and ref_allowed
  290. if method["return_type"] != "void":
  291. if is_class_type(method["return_type"]):
  292. if is_enum(method["return_type"]):
  293. return_statement += "return (" + remove_enum_prefix(method["return_type"]) + ") "
  294. elif return_type_is_ref:
  295. return_statement += "return Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(";
  296. else:
  297. return_statement += "return " + ("(" + strip_name(method["return_type"]) + " *) " if is_class_type(method["return_type"]) else "")
  298. else:
  299. return_statement += "return "
  300. def get_icall_type_name(name):
  301. if is_enum(name):
  302. return "int"
  303. if is_class_type(name):
  304. return "Object"
  305. return name
  306. if method["has_varargs"]:
  307. if len(method["arguments"]) != 0:
  308. source.append("\tVariant __given_args[" + str(len(method["arguments"])) + "];")
  309. for i, argument in enumerate(method["arguments"]):
  310. source.append("\tgodot::api->godot_variant_new_nil((godot_variant *) &__given_args[" + str(i) + "]);")
  311. source.append("")
  312. for i, argument in enumerate(method["arguments"]):
  313. source.append("\t__given_args[" + str(i) + "] = " + escape_cpp(argument["name"]) + ";")
  314. source.append("")
  315. size = ""
  316. if method["has_varargs"]:
  317. size = "(__var_args.size() + " + str(len(method["arguments"])) + ")"
  318. else:
  319. size = "(" + str(len(method["arguments"])) + ")"
  320. source.append("\tgodot_variant **__args = (godot_variant **) alloca(sizeof(godot_variant *) * " + size + ");")
  321. source.append("")
  322. for i, argument in enumerate(method["arguments"]):
  323. source.append("\t__args[" + str(i) + "] = (godot_variant *) &__given_args[" + str(i) + "];")
  324. source.append("")
  325. if method["has_varargs"]:
  326. source.append("\tfor (int i = 0; i < __var_args.size(); i++) {")
  327. source.append("\t\t__args[i + " + str(len(method["arguments"])) + "] = (godot_variant *) &((Array &) __var_args)[i];")
  328. source.append("\t}")
  329. source.append("")
  330. source.append("\tVariant __result;")
  331. source.append("\t*(godot_variant *) &__result = godot::api->godot_method_bind_call(___mb.mb_" + method["name"] + ", ((const Object *) " + core_object_name + ")->_owner, (const godot_variant **) __args, " + size + ", nullptr);")
  332. source.append("")
  333. if is_class_type(method["return_type"]):
  334. source.append("\tObject *obj = Object::___get_from_variant(__result);")
  335. source.append("\tif (obj->has_method(\"reference\"))")
  336. source.append("\t\tobj->callv(\"reference\", Array());")
  337. source.append("")
  338. for i, argument in enumerate(method["arguments"]):
  339. source.append("\tgodot::api->godot_variant_destroy((godot_variant *) &__given_args[" + str(i) + "]);")
  340. source.append("")
  341. if method["return_type"] != "void":
  342. cast = ""
  343. if is_class_type(method["return_type"]):
  344. if return_type_is_ref:
  345. cast += "Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(__result);"
  346. else:
  347. cast += "(" + strip_name(method["return_type"]) + " *) " + strip_name(method["return_type"] + "::___get_from_variant(") + "__result);"
  348. else:
  349. cast += "__result;"
  350. source.append("\treturn " + cast)
  351. else:
  352. args = []
  353. for arg in method["arguments"]:
  354. args.append(get_icall_type_name(arg["type"]))
  355. icall_ret_type = get_icall_type_name(method["return_type"])
  356. icall_sig = tuple((icall_ret_type, tuple(args)))
  357. icalls.add(icall_sig)
  358. icall_name = get_icall_name(icall_sig)
  359. return_statement += icall_name + "(___mb.mb_" + method["name"] + ", (const Object *) " + core_object_name
  360. for arg in method["arguments"]:
  361. arg_is_ref = is_reference_type(arg["type"]) and ref_allowed
  362. return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if arg_is_ref else "")
  363. return_statement += ")"
  364. if return_type_is_ref:
  365. return_statement += ")"
  366. source.append("\t" + return_statement + ";")
  367. source.append("}")
  368. source.append("")
  369. source.append("}")
  370. return "\n".join(source)
  371. def generate_icall_header(icalls):
  372. source = []
  373. source.append("#ifndef GODOT_CPP__ICALLS_HPP")
  374. source.append("#define GODOT_CPP__ICALLS_HPP")
  375. source.append("")
  376. source.append("#include <gdnative_api_struct.gen.h>")
  377. source.append("#include <stdint.h>")
  378. source.append("")
  379. source.append("#include <core/GodotGlobal.hpp>")
  380. source.append("#include <core/CoreTypes.hpp>")
  381. source.append("#include \"Object.hpp\"")
  382. source.append("")
  383. source.append("")
  384. source.append("namespace godot {")
  385. source.append("")
  386. for icall in icalls:
  387. ret_type = icall[0]
  388. args = icall[1]
  389. method_signature = "static inline "
  390. method_signature += get_icall_return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst"
  391. for i, arg in enumerate(args):
  392. method_signature += ", const "
  393. if is_core_type(arg):
  394. method_signature += arg + "&"
  395. elif arg == "int":
  396. method_signature += "int64_t "
  397. elif arg == "float":
  398. method_signature += "double "
  399. elif is_primitive(arg):
  400. method_signature += arg + " "
  401. else:
  402. method_signature += "Object *"
  403. method_signature += "arg" + str(i)
  404. method_signature += ")"
  405. source.append(method_signature + " {")
  406. if ret_type != "void":
  407. source.append("\t" + ("godot_object *" if is_class_type(ret_type) else get_icall_return_type(ret_type)) + "ret;")
  408. if is_class_type(ret_type):
  409. source.append("\tret = nullptr;")
  410. source.append("\tconst void *args[" + ("1" if len(args) == 0 else "") + "] = {")
  411. for i, arg in enumerate(args):
  412. wrapped_argument = "\t\t"
  413. if is_primitive(arg) or is_core_type(arg):
  414. wrapped_argument += "(void *) &arg" + str(i)
  415. else:
  416. wrapped_argument += "(void *) (arg" + str(i) + ") ? arg" + str(i) + "->_owner : nullptr"
  417. wrapped_argument += ","
  418. source.append(wrapped_argument)
  419. source.append("\t};")
  420. source.append("")
  421. source.append("\tgodot::api->godot_method_bind_ptrcall(mb, inst->_owner, args, " + ("nullptr" if ret_type == "void" else "&ret") + ");")
  422. if ret_type != "void":
  423. if is_class_type(ret_type):
  424. source.append("\tif (ret) {")
  425. source.append("\t\treturn (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, ret);")
  426. source.append("\t}")
  427. source.append("")
  428. source.append("\treturn (Object *) ret;")
  429. else:
  430. source.append("\treturn ret;")
  431. source.append("}")
  432. source.append("")
  433. source.append("}")
  434. source.append("")
  435. source.append("#endif")
  436. return "\n".join(source)
  437. def generate_type_registry(classes):
  438. source = []
  439. source.append("#include \"TagDB.hpp\"")
  440. source.append("#include <typeinfo>")
  441. source.append("\n")
  442. for c in classes:
  443. source.append("#include <" + strip_name(c["name"]) + ".hpp>")
  444. source.append("")
  445. source.append("")
  446. source.append("namespace godot {")
  447. source.append("void ___register_types()")
  448. source.append("{")
  449. for c in classes:
  450. class_name = strip_name(c["name"])
  451. base_class_name = strip_name(c["base_class"])
  452. class_type_hash = "typeid(" + class_name + ").hash_code()"
  453. base_class_type_hash = "typeid(" + base_class_name + ").hash_code()"
  454. if base_class_name == "":
  455. base_class_type_hash = "0"
  456. source.append("\tgodot::_TagDB::register_global_type(\"" + c["name"] + "\", " + class_type_hash + ", " + base_class_type_hash + ");")
  457. source.append("}")
  458. source.append("")
  459. source.append("}")
  460. return "\n".join(source)
  461. def generate_init_method_bindings(classes):
  462. source = []
  463. for c in classes:
  464. source.append("#include <" + strip_name(c["name"]) + ".hpp>")
  465. source.append("")
  466. source.append("")
  467. source.append("namespace godot {")
  468. source.append("void ___init_method_bindings()")
  469. source.append("{")
  470. for c in classes:
  471. class_name = strip_name(c["name"])
  472. source.append("\t" + strip_name(c["name"]) + "::___init_method_bindings();")
  473. source.append("}")
  474. source.append("")
  475. source.append("}")
  476. return "\n".join(source)
  477. def get_icall_return_type(t):
  478. if is_class_type(t):
  479. return "Object *"
  480. if t == "int":
  481. return "int64_t "
  482. if t == "float" or t == "real":
  483. return "double "
  484. return t + " "
  485. def get_icall_name(sig):
  486. ret_type = sig[0]
  487. args = sig[1]
  488. name = "___godot_icall_"
  489. name += strip_name(ret_type)
  490. for arg in args:
  491. name += "_" + strip_name(arg)
  492. return name
  493. def get_used_classes(c):
  494. classes = []
  495. for method in c["methods"]:
  496. if is_class_type(method["return_type"]) and not (method["return_type"] in classes):
  497. classes.append(method["return_type"])
  498. for arg in method["arguments"]:
  499. if is_class_type(arg["type"]) and not (arg["type"] in classes):
  500. classes.append(arg["type"])
  501. return classes
  502. def strip_name(name):
  503. if len(name) == 0:
  504. return name
  505. if name[0] == '_':
  506. return name[1:]
  507. return name
  508. def extract_nested_type(nested_type):
  509. return strip_name(nested_type[:nested_type.find("::")])
  510. def remove_nested_type_prefix(name):
  511. return name if name.find("::") == -1 else strip_name(name[name.find("::") + 2:])
  512. def remove_enum_prefix(name):
  513. return strip_name(name[name.find("enum.") + 5:])
  514. def is_nested_type(name, type = ""):
  515. return name.find(type + "::") != -1
  516. def is_enum(name):
  517. return name.find("enum.") == 0
  518. def is_class_type(name):
  519. return not is_core_type(name) and not is_primitive(name)
  520. def is_core_type(name):
  521. core_types = ["Array",
  522. "Basis",
  523. "Color",
  524. "Dictionary",
  525. "Error",
  526. "NodePath",
  527. "Plane",
  528. "PoolByteArray",
  529. "PoolIntArray",
  530. "PoolRealArray",
  531. "PoolStringArray",
  532. "PoolVector2Array",
  533. "PoolVector3Array",
  534. "PoolColorArray",
  535. "PoolIntArray",
  536. "PoolRealArray",
  537. "Quat",
  538. "Rect2",
  539. "AABB",
  540. "RID",
  541. "String",
  542. "Transform",
  543. "Transform2D",
  544. "Variant",
  545. "Vector2",
  546. "Vector3"]
  547. return name in core_types
  548. def is_primitive(name):
  549. core_types = ["int", "bool", "real", "float", "void"]
  550. return name in core_types
  551. def escape_cpp(name):
  552. escapes = {
  553. "class": "_class",
  554. "char": "_char",
  555. "short": "_short",
  556. "bool": "_bool",
  557. "int": "_int",
  558. "default": "_default",
  559. "case": "_case",
  560. "switch": "_switch",
  561. "export": "_export",
  562. "template": "_template",
  563. "new": "new_",
  564. "operator": "_operator",
  565. "typename": "_typename"
  566. }
  567. if name in escapes:
  568. return escapes[name]
  569. return name