ASUtils.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "ASUtils.h"
  4. #include "Tuning.h"
  5. #include "Utils.h"
  6. #include "XmlAnalyzer.h"
  7. #include "XmlSourceData.h"
  8. #include <cassert>
  9. #include <regex>
  10. using namespace std;
  11. using namespace pugi;
  12. namespace ASBindingGenerator
  13. {
  14. // https://www.angelcode.com/angelscript/sdk/docs/manual/doc_datatypes_primitives.html
  15. // https://en.cppreference.com/w/cpp/language/types
  16. string CppPrimitiveTypeToAS(const string& cppType)
  17. {
  18. if (cppType == "bool")
  19. return "bool";
  20. if (cppType == "char" || cppType == "signed char" || cppType == "i8")
  21. return "int8";
  22. if (cppType == "unsigned char" || cppType == "u8")
  23. return "uint8";
  24. if (cppType == "short" || cppType == "i16")
  25. return "int16";
  26. if (cppType == "unsigned short" || cppType == "u16")
  27. return "uint16";
  28. if (cppType == "int" || cppType == "i32")
  29. return "int";
  30. if (cppType == "unsigned" || cppType == "unsigned int" || cppType == "u32")
  31. return "uint";
  32. if (cppType == "long long" || cppType == "i64")
  33. return "int64";
  34. if (cppType == "unsigned long long" || cppType == "u64")
  35. return "uint64";
  36. if (cppType == "float")
  37. return "float";
  38. if (cppType == "double")
  39. return "double";
  40. // Types below are registered in Manual.cpp
  41. if (cppType == "long") // Size is compiler-dependent
  42. return "long";
  43. if (cppType == "unsigned long") // Size is compiler-dependent
  44. return "ulong";
  45. if (cppType == "size_t")
  46. return "size_t";
  47. if (cppType == "SDL_JoystickID")
  48. return "SDL_JoystickID";
  49. if (cppType == "c32")
  50. return "c32";
  51. if (cppType == "hash32")
  52. return "hash32";
  53. if (cppType == "hash64")
  54. return "hash64";
  55. throw Exception(cppType + " not a primitive type");
  56. }
  57. static bool IsKnownPrimitiveCppType(const string& cppType)
  58. {
  59. try
  60. {
  61. CppPrimitiveTypeToAS(cppType);
  62. }
  63. catch (...)
  64. {
  65. return false;
  66. }
  67. return true;
  68. }
  69. string JoinASDeclarations(const vector<ConvertedVariable>& vars)
  70. {
  71. string result;
  72. for (const ConvertedVariable& var : vars)
  73. {
  74. if (!var.asDeclaration_.empty())
  75. {
  76. if (!result.empty())
  77. result += ", ";
  78. result += var.asDeclaration_;
  79. }
  80. }
  81. return result;
  82. }
  83. string JoinCppDeclarations(const string& firstCppDeclaration, const vector<ConvertedVariable>& vars)
  84. {
  85. string result = firstCppDeclaration;
  86. for (const ConvertedVariable& var : vars)
  87. {
  88. if (!var.cppDeclaration_.empty())
  89. {
  90. if (!result.empty())
  91. result += ", ";
  92. result += var.cppDeclaration_;
  93. }
  94. }
  95. return result;
  96. }
  97. string JoinCppDeclarations(const vector<ConvertedVariable>& vars)
  98. {
  99. return JoinCppDeclarations("", vars);
  100. }
  101. shared_ptr<EnumAnalyzer> FindEnum(const string& name)
  102. {
  103. NamespaceAnalyzer namespaceAnalyzer(SourceData::namespaceUrho3D_);
  104. vector<EnumAnalyzer> enumAnalyzers = namespaceAnalyzer.GetEnums();
  105. for (const EnumAnalyzer& enumAnalyzer : enumAnalyzers)
  106. {
  107. if (enumAnalyzer.GetTypeName() == name)
  108. return make_shared<EnumAnalyzer>(enumAnalyzer);
  109. }
  110. return shared_ptr<EnumAnalyzer>();
  111. }
  112. static bool IsUsing(const string& identifier)
  113. {
  114. for (xml_node memberdef : SourceData::usings_)
  115. {
  116. UsingAnalyzer usingAnalyzer(memberdef);
  117. if (usingAnalyzer.GetName() == identifier)
  118. return true;
  119. }
  120. return false;
  121. }
  122. bool IsKnownCppType(const string& name)
  123. {
  124. static vector<string> _knownTypes = {
  125. "void",
  126. // TODO: Remove
  127. "VariantMap",
  128. };
  129. if (CONTAINS(_knownTypes, name))
  130. return true;
  131. if (IsKnownPrimitiveCppType(name))
  132. return true;
  133. if (SourceData::classesByName_.find(name) != SourceData::classesByName_.end())
  134. return true;
  135. if (SourceData::enums_.find(name) != SourceData::enums_.end())
  136. return true;
  137. if (EndsWith(name, "Flags"))
  138. return true;
  139. return false;
  140. }
  141. shared_ptr<ClassAnalyzer> FindClassByName(const string& name)
  142. {
  143. auto it = SourceData::classesByName_.find(name);
  144. if (it != SourceData::classesByName_.end())
  145. {
  146. xml_node compounddef = it->second;
  147. return make_shared<ClassAnalyzer>(compounddef);
  148. }
  149. // using VariantVector = Vector<Variant>
  150. return shared_ptr<ClassAnalyzer>();
  151. }
  152. shared_ptr<ClassAnalyzer> FindClassByID(const string& id)
  153. {
  154. auto it = SourceData::classesByID_.find(id);
  155. if (it != SourceData::classesByID_.end())
  156. {
  157. xml_node compounddef = it->second;
  158. return make_shared<ClassAnalyzer>(compounddef);
  159. }
  160. // using VariantVector = Vector<Variant>
  161. return shared_ptr<ClassAnalyzer>();
  162. }
  163. // Variable name can be empty for function return type
  164. ConvertedVariable CppVariableToAS(const TypeAnalyzer& type, VariableUsage usage, const string& name, const string& defaultValue)
  165. {
  166. ConvertedVariable result;
  167. if (usage == VariableUsage::PostfixIncDecParameter)
  168. {
  169. result.glue_ = " " + type.ToString() + " " + name + "{};\n";
  170. return result;
  171. }
  172. if (type.IsRvalueReference() || type.IsDoublePointer() || type.IsRefToPointer())
  173. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  174. string cppTypeName = type.GetNameWithTemplateParams();
  175. if (cppTypeName == "void")
  176. {
  177. if (usage == VariableUsage::FunctionReturn && !type.IsPointer())
  178. {
  179. result.asDeclaration_ = "void";
  180. result.cppDeclaration_ = "void";
  181. return result;
  182. }
  183. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  184. }
  185. if (cppTypeName == "Context")
  186. {
  187. if (usage == VariableUsage::FunctionParameter && type.IsPointer())
  188. {
  189. result.glue_ = " " + type.ToString() + " " + name + " = GetScriptContext();\n";
  190. return result;
  191. }
  192. throw Exception("Error: type \"" + type.ToString() + "\" can used only as function parameter");
  193. }
  194. // Works with both Vector<String> and Vector<String>&
  195. if (cppTypeName == "Vector<String>" || cppTypeName == "StringVector")
  196. {
  197. // Works with both Vector<String> and Vector<String>&
  198. if (usage == VariableUsage::FunctionReturn && !type.IsPointer())
  199. {
  200. result.asDeclaration_ = "Array<String>@";
  201. result.cppDeclaration_ = "CScriptArray*";
  202. result.glue_ = "return VectorToArray<String>(result, \"Array<String>\");\n";
  203. return result;
  204. }
  205. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  206. {
  207. string newCppVarName = name + "_conv";
  208. //result->asDecl_ = "String[]&";
  209. result.asDeclaration_ = "Array<String>@+";
  210. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  211. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToVector<String>(" + newCppVarName + ");\n";
  212. if (!defaultValue.empty())
  213. {
  214. assert(defaultValue == "Vector< String >()");
  215. //result->asDecl_ += " = Array<String>()";
  216. result.asDeclaration_ += " = null";
  217. }
  218. return result;
  219. }
  220. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  221. }
  222. smatch match;
  223. regex_match(cppTypeName, match, regex("SharedPtr<(\\w+)>"));
  224. if (!match.empty())
  225. {
  226. string cppSubtypeName = match[1].str();
  227. string asSubtypeName;
  228. try
  229. {
  230. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  231. }
  232. catch (...)
  233. {
  234. asSubtypeName = cppSubtypeName;
  235. }
  236. if (cppSubtypeName == "WorkItem") // TODO autodetect
  237. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  238. if (usage == VariableUsage::FunctionReturn)
  239. {
  240. result.asDeclaration_ = asSubtypeName + "@+";
  241. result.cppDeclaration_ = cppSubtypeName + "*";
  242. result.glue_ = "return result.Detach();\n";
  243. return result;
  244. }
  245. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  246. }
  247. regex_match(cppTypeName, match, regex("Vector<SharedPtr<(\\w+)>>"));
  248. if (!match.empty())
  249. {
  250. string cppSubtypeName = match[1].str();
  251. string asSubtypeName;
  252. try
  253. {
  254. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  255. }
  256. catch (...)
  257. {
  258. asSubtypeName = cppSubtypeName;
  259. }
  260. if (cppSubtypeName == "WorkItem") // TODO autodetect
  261. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  262. if (usage == VariableUsage::FunctionReturn)
  263. {
  264. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  265. result.cppDeclaration_ = "CScriptArray*";
  266. // Which variant is correct/better?
  267. #if 0
  268. result->glueResult_ = "return VectorToArray<SharedPtr<" + cppTypeName + ">>(result, \"Array<" + asTypeName + "@>@\");\n";
  269. #else
  270. result.glue_ = "return VectorToHandleArray(result, \"Array<" + asSubtypeName + "@>\");\n";
  271. #endif
  272. return result;
  273. }
  274. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  275. {
  276. string newCppVarName = name + "_conv";
  277. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@+";
  278. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  279. result.glue_ = " " + cppTypeName + " " + name + " = HandleArrayToVector<" + cppSubtypeName + ">(" + newCppVarName + ");\n";
  280. assert(defaultValue.empty()); // TODO: make
  281. return result;
  282. }
  283. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  284. }
  285. regex_match(cppTypeName, match, regex("Vector<(\\w+)\\*>"));
  286. if (!match.empty())
  287. {
  288. string cppSubtypeName = match[1].str();
  289. string asSubtypeName;
  290. try
  291. {
  292. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  293. }
  294. catch (...)
  295. {
  296. asSubtypeName = cppSubtypeName;
  297. }
  298. if (usage == VariableUsage::FunctionReturn)
  299. {
  300. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  301. result.cppDeclaration_ = "CScriptArray*";
  302. result.glue_ = "return VectorToHandleArray(result, \"Array<" + asSubtypeName + "@>\");\n";
  303. return result;
  304. }
  305. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  306. {
  307. string newCppVarName = name + "_conv";
  308. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  309. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  310. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToVector<" + cppSubtypeName + "*>(" + newCppVarName + ");\n";
  311. assert(defaultValue.empty()); // TODO: make
  312. return result;
  313. }
  314. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  315. }
  316. regex_match(cppTypeName, match, regex("Vector<(\\w+)>"));
  317. if (!match.empty())
  318. {
  319. string cppSubtypeName = match[1].str();
  320. string asSubtypeName;
  321. try
  322. {
  323. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  324. }
  325. catch (...)
  326. {
  327. asSubtypeName = cppSubtypeName;
  328. }
  329. if (usage == VariableUsage::FunctionReturn && type.IsConst() == type.IsReference())
  330. {
  331. result.asDeclaration_ = "Array<" + asSubtypeName + ">@";
  332. result.cppDeclaration_ = "CScriptArray*";
  333. result.glue_ = "return VectorToArray(result, \"Array<" + asSubtypeName + ">\");\n";
  334. return result;
  335. }
  336. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  337. {
  338. string newCppVarName = name + "_conv";
  339. result.asDeclaration_ = "Array<" + asSubtypeName + ">@+";
  340. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  341. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToVector<" + cppSubtypeName + ">(" + newCppVarName + ");\n";
  342. assert(defaultValue.empty()); // TODO: make
  343. return result;
  344. }
  345. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  346. }
  347. if (!IsKnownCppType(cppTypeName))
  348. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  349. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(cppTypeName);
  350. if (analyzer && analyzer->IsInternal())
  351. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  352. if (analyzer && Contains(analyzer->GetComment(), "NO_BIND"))
  353. throw Exception("Error: type \"" + cppTypeName + "\" can not automatically bind bacause have @nobind mark");
  354. if (analyzer && analyzer->IsAbstract() && !(analyzer->IsRefCounted() || analyzer->IsNoCount()))
  355. throw Exception("Error: type \"" + cppTypeName + "\" can not bind bacause abstract value");
  356. // analyzer can be null for simple types (int, float) or if type "using VariantVector = Vector<Variant>"
  357. // TODO add to type info "IsUsing"
  358. // TODO add description to TypeAnalyzer::GetClass()
  359. if (IsUsing(cppTypeName) && cppTypeName != "VariantMap")
  360. throw Exception("Using \"" + cppTypeName + "\" can not automatically bind");
  361. string asTypeName;
  362. try
  363. {
  364. asTypeName = CppPrimitiveTypeToAS(cppTypeName);
  365. }
  366. catch (...)
  367. {
  368. asTypeName = cppTypeName;
  369. }
  370. if (Contains(asTypeName, '<'))
  371. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  372. if (Contains(type.ToString(), "::"))
  373. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  374. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  375. {
  376. result.asDeclaration_ = "const " + asTypeName + "&in";
  377. result.cppDeclaration_ = type.ToString();
  378. if (!name.empty())
  379. result.cppDeclaration_ += " " + name;
  380. if (!defaultValue.empty())
  381. {
  382. string asDefaultValue = CppValueToAS(defaultValue);
  383. asDefaultValue = ReplaceAll(asDefaultValue, "\"", "\\\"");
  384. result.asDeclaration_ += " = " + asDefaultValue;
  385. }
  386. return result;
  387. }
  388. result.asDeclaration_ = asTypeName;
  389. if (type.IsReference())
  390. {
  391. result.asDeclaration_ += "&";
  392. }
  393. else if (type.IsPointer())
  394. {
  395. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(cppTypeName);
  396. if (analyzer && (analyzer->IsRefCounted() || analyzer->IsNoCount()))
  397. result.asDeclaration_ = result.asDeclaration_ + "@" + (analyzer->IsNoCount() ? "" : "+");
  398. else
  399. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  400. }
  401. result.cppDeclaration_ = type.ToString();
  402. if (!name.empty())
  403. result.cppDeclaration_ += " " + name;
  404. if (usage == VariableUsage::FunctionReturn && type.IsConst() && !type.IsPointer())
  405. result.asDeclaration_ = "const " + result.asDeclaration_;
  406. if (!defaultValue.empty())
  407. {
  408. string asDefaultValue = CppValueToAS(defaultValue);
  409. asDefaultValue = ReplaceAll(asDefaultValue, "\"", "\\\"");
  410. result.asDeclaration_ += " = " + asDefaultValue;
  411. }
  412. return result;
  413. }
  414. string HideUnnamedType(const string& name)
  415. {
  416. regex rgx("@\\d+");
  417. return regex_replace(name, rgx, "@?");
  418. }
  419. string CppTypeToAS(const TypeAnalyzer& type, TypeUsage typeUsage)
  420. {
  421. if (Contains(type.GetName(), "@")) // Unnamed union inside class
  422. throw Exception("Error: type \"" + HideUnnamedType(type.ToString()) + "\" can not automatically bind");
  423. if (type.IsRvalueReference() || type.IsDoublePointer() || type.IsRefToPointer())
  424. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  425. string cppTypeName = type.GetNameWithTemplateParams();
  426. if (cppTypeName == "Context" && typeUsage == TypeUsage::FunctionReturn)
  427. throw Exception("Error: type \"" + type.ToString() + "\" can not be returned");
  428. if (!IsKnownCppType(type.GetNameWithTemplateParams()))
  429. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  430. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(type.GetNameWithTemplateParams());
  431. if (analyzer && analyzer->IsInternal())
  432. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  433. if (analyzer && Contains(analyzer->GetComment(), "NO_BIND"))
  434. throw Exception("Error: type \"" + cppTypeName + "\" can not automatically bind bacause have @nobind mark");
  435. // analyzer can be null for simple types (int, float) or if type "using VariantVector = Vector<Variant>"
  436. // TODO add to type info "IsUsing"
  437. // TODO add description to TypeAnalyzer::GetClass()
  438. if (IsUsing(cppTypeName) && cppTypeName != "VariantMap")
  439. throw Exception("Using \"" + cppTypeName + "\" can not automatically bind");
  440. string asTypeName;
  441. try
  442. {
  443. asTypeName = CppPrimitiveTypeToAS(cppTypeName);
  444. }
  445. catch (...)
  446. {
  447. asTypeName = cppTypeName;
  448. }
  449. if (asTypeName == "void" && type.IsPointer())
  450. throw Exception("Error: type \"void*\" can not automatically bind");
  451. if (Contains(asTypeName, '<'))
  452. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  453. if (Contains(type.ToString(), "::"))
  454. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  455. if (type.IsConst() && type.IsReference() && typeUsage == TypeUsage::FunctionParameter)
  456. return "const " + asTypeName + "&in";
  457. string result = asTypeName;
  458. if (type.IsReference())
  459. {
  460. result += "&";
  461. }
  462. else if (type.IsPointer())
  463. {
  464. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(type.GetNameWithTemplateParams());
  465. if (analyzer && (analyzer->IsRefCounted() || analyzer->IsNoCount()))
  466. result = result + "@" + (analyzer->IsNoCount() ? "" : "+");
  467. else
  468. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  469. }
  470. if (typeUsage == TypeUsage::FunctionReturn && type.IsConst() && !type.IsPointer())
  471. result = "const " + result;
  472. return result;
  473. }
  474. string CppValueToAS(const string& cppValue)
  475. {
  476. if (cppValue == "nullptr")
  477. return "null";
  478. if (cppValue == "Variant::emptyVariantMap")
  479. return "VariantMap()";
  480. if (cppValue == "NPOS")
  481. return "String::NPOS";
  482. return cppValue;
  483. }
  484. // =================================================================================
  485. static string GenerateFunctionWrapperName(xml_node memberdef)
  486. {
  487. TypeAnalyzer returnType = ExtractType(memberdef);
  488. string result = ExtractType(memberdef).ToString() + "_" + ExtractName(memberdef);
  489. vector<ParamAnalyzer> params = ExtractParams(memberdef);
  490. if (params.size() == 0)
  491. {
  492. result += "_void";
  493. }
  494. else
  495. {
  496. for (ParamAnalyzer param : params)
  497. {
  498. result += "_" + param.GetType().ToString();
  499. }
  500. }
  501. result = ReplaceAll(result, "=", "eq");
  502. result = ReplaceAll(result, "<", "les");
  503. result = ReplaceAll(result, ">", "gre");
  504. result = ReplaceAll(result, "*", "star");
  505. result = ReplaceAll(result, "/", "div");
  506. result = ReplaceAll(result, "+", "plus");
  507. result = ReplaceAll(result, "-", "min");
  508. result = ReplaceAll(result, "[", "lbr");
  509. result = ReplaceAll(result, "]", "rbr");
  510. result = ReplaceAll(result, ":", "col");
  511. result = ReplaceAll(result, " ", "sp");
  512. result = ReplaceAll(result, "&", "amp");
  513. return result;
  514. }
  515. string GenerateWrapperName(const GlobalFunctionAnalyzer& functionAnalyzer)
  516. {
  517. return GenerateFunctionWrapperName(functionAnalyzer.GetMemberdef());
  518. }
  519. string GenerateWrapperName(const ClassStaticFunctionAnalyzer& functionAnalyzer)
  520. {
  521. return functionAnalyzer.GetClassName() + "_" + GenerateFunctionWrapperName(functionAnalyzer.GetMemberdef());
  522. }
  523. string GenerateWrapperName(const MethodAnalyzer& methodAnalyzer, bool templateVersion)
  524. {
  525. if (templateVersion)
  526. return methodAnalyzer.GetClassName() + "_" + GenerateFunctionWrapperName(methodAnalyzer.GetMemberdef()) + "_template";
  527. else
  528. return methodAnalyzer.GetClassName() + "_" + GenerateFunctionWrapperName(methodAnalyzer.GetMemberdef());
  529. }
  530. // =================================================================================
  531. string GenerateWrapper(const GlobalFunctionAnalyzer& functionAnalyzer, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn)
  532. {
  533. string result;
  534. string glueReturnType = convertedReturn.cppDeclaration_;
  535. result =
  536. "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(" + JoinCppDeclarations(convertedParams) + ")\n"
  537. "{\n";
  538. for (size_t i = 0; i < convertedParams.size(); i++)
  539. result += convertedParams[i].glue_;
  540. if (glueReturnType != "void")
  541. result += " " + functionAnalyzer.GetReturnType().ToString() + " result = ";
  542. else
  543. result += " ";
  544. result += functionAnalyzer.GetName() + "(" + functionAnalyzer.JoinParamsNames() + ");\n";
  545. if (!convertedReturn.glue_.empty())
  546. result += " " + convertedReturn.glue_;
  547. else if (glueReturnType != "void")
  548. result += " return result;\n";
  549. result += "}";
  550. return result;
  551. }
  552. string GenerateWrapper(const ClassStaticFunctionAnalyzer& functionAnalyzer, bool templateVersion, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn)
  553. {
  554. string result = templateVersion ? "template <class T> " : "static ";
  555. string className = templateVersion ? "T" : functionAnalyzer.GetClassName();
  556. string glueReturnType = convertedReturn.cppDeclaration_;
  557. result +=
  558. glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(" + JoinCppDeclarations(convertedParams) + ")\n"
  559. "{\n";
  560. for (size_t i = 0; i < convertedParams.size(); i++)
  561. result += convertedParams[i].glue_;
  562. if (glueReturnType != "void")
  563. result += " " + functionAnalyzer.GetReturnType().ToString() + " result = ";
  564. else
  565. result += " ";
  566. result += className + "::" + functionAnalyzer.GetName() + "(" + functionAnalyzer.JoinParamsNames() + ");\n";
  567. if (!convertedReturn.glue_.empty())
  568. result += " " + convertedReturn.glue_;
  569. else if (glueReturnType != "void")
  570. result += " return result;\n";
  571. result += "}\n";
  572. return result;
  573. }
  574. string GenerateWrapper(const MethodAnalyzer& methodAnalyzer, bool templateVersion, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn)
  575. {
  576. string result = templateVersion ? "template <class T> " : "static ";
  577. string className = templateVersion ? "T" : methodAnalyzer.GetClassName();
  578. string glueReturnType = convertedReturn.cppDeclaration_;
  579. result +=
  580. glueReturnType + " " + GenerateWrapperName(methodAnalyzer, templateVersion) + "(" + JoinCppDeclarations(className + "* _ptr", convertedParams) + ")\n"
  581. "{\n";
  582. for (size_t i = 0; i < convertedParams.size(); i++)
  583. result += convertedParams[i].glue_;
  584. if (glueReturnType != "void")
  585. result += " " + methodAnalyzer.GetReturnType().ToString() + " result = ";
  586. else
  587. result += " ";
  588. result += "_ptr->" + methodAnalyzer.GetName() + "(" + methodAnalyzer.JoinParamsNames() + ");\n";
  589. if (!convertedReturn.glue_.empty())
  590. result += " " + convertedReturn.glue_;
  591. else if (glueReturnType != "void")
  592. result += " return result;\n";
  593. result += "}\n";
  594. return result;
  595. }
  596. string GenerateConstructorWrapper(const MethodAnalyzer& methodAnalyzer, const vector<ConvertedVariable>& convertedParams)
  597. {
  598. string className = methodAnalyzer.GetClassName();
  599. string result =
  600. "static void " + GenerateWrapperName(methodAnalyzer) + "(" + JoinCppDeclarations(className + "* _ptr", convertedParams) + ")\n"
  601. "{\n";
  602. for (size_t i = 0; i < convertedParams.size(); i++)
  603. result += convertedParams[i].glue_;
  604. result +=
  605. " new(_ptr) " + className + "(" + methodAnalyzer.JoinParamsNames() + ");\n"
  606. "}\n";
  607. return result;
  608. }
  609. string GenerateFactoryWrapper(const MethodAnalyzer& methodAnalyzer, const vector<ConvertedVariable>& convertedParams)
  610. {
  611. string className = methodAnalyzer.GetClassName();
  612. string result =
  613. "static " + className + "* " + GenerateWrapperName(methodAnalyzer) + "(" + JoinCppDeclarations(convertedParams) + ")\n"
  614. "{\n";
  615. for (size_t i = 0; i < convertedParams.size(); i++)
  616. result += convertedParams[i].glue_;
  617. result +=
  618. " return new " + className + "(" + methodAnalyzer.JoinParamsNames() + ");\n"
  619. "}\n";
  620. return result;
  621. }
  622. // =================================================================================
  623. string Generate_asFUNCTIONPR(const GlobalFunctionAnalyzer& functionAnalyzer)
  624. {
  625. string functionName = functionAnalyzer.GetName();
  626. string cppParams = "(" + JoinParamsTypes(functionAnalyzer.GetMemberdef(), functionAnalyzer.GetSpecialization()) + ")";
  627. string returnType = functionAnalyzer.GetReturnType().ToString();
  628. return "AS_FUNCTIONPR(" + functionName + ", " + cppParams + ", " + returnType + ")";
  629. }
  630. string Generate_asFUNCTIONPR(const ClassStaticFunctionAnalyzer& functionAnalyzer, bool templateVersion)
  631. {
  632. string className = functionAnalyzer.GetClassName();
  633. string functionName = functionAnalyzer.GetName();
  634. string cppParams = "(" + JoinParamsTypes(functionAnalyzer.GetMemberdef(), functionAnalyzer.GetSpecialization()) + ")";
  635. string returnType = functionAnalyzer.GetReturnType().ToString();
  636. if (templateVersion)
  637. return "AS_FUNCTIONPR(T::" + functionName + ", " + cppParams + ", " + returnType + ")";
  638. else
  639. return "AS_FUNCTIONPR(" + className + "::" + functionName + ", " + cppParams + ", " + returnType + ")";
  640. }
  641. string Generate_asMETHODPR(const MethodAnalyzer& methodAnalyzer, bool templateVersion)
  642. {
  643. string className = methodAnalyzer.GetClassName();
  644. string functionName = methodAnalyzer.GetName();
  645. string cppParams = "(" + JoinParamsTypes(methodAnalyzer.GetMemberdef(), methodAnalyzer.GetSpecialization()) + ")";
  646. if (methodAnalyzer.IsConst())
  647. cppParams += " const";
  648. string returnType = methodAnalyzer.GetReturnType().ToString();
  649. if (templateVersion)
  650. return "AS_METHODPR(T, " + functionName + ", " + cppParams + ", " + returnType + ")";
  651. else
  652. return "AS_METHODPR(" + className + ", " + functionName + ", " + cppParams + ", " + returnType + ")";
  653. }
  654. } // namespace ASBindingGenerator