binding_generator.py 25 KB

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