ASUtils.cpp 27 KB

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