ASUtils.cpp 28 KB

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