binding_generator.py 25 KB

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