ASUtils.cpp 27 KB

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