binding_generator.py 25 KB

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