ASUtils.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "ASUtils.h"
  4. #include "Tuning.h"
  5. #include "Utils.h"
  6. #include "XmlAnalyzer.h"
  7. #include "XmlSourceData.h"
  8. #include <cassert>
  9. #include <regex>
  10. using namespace std;
  11. using namespace pugi;
  12. namespace ASBindingGenerator
  13. {
  14. // https://www.angelcode.com/angelscript/sdk/docs/manual/doc_datatypes_primitives.html
  15. // https://en.cppreference.com/w/cpp/language/types
  16. string CppPrimitiveTypeToAS(const string& cppType)
  17. {
  18. if (cppType == "bool")
  19. return "bool";
  20. if (cppType == "char" || cppType == "signed char" || cppType == "i8")
  21. return "int8";
  22. if (cppType == "unsigned char" || cppType == "u8")
  23. return "uint8";
  24. if (cppType == "short" || cppType == "i16")
  25. return "int16";
  26. if (cppType == "unsigned short" || cppType == "u16")
  27. return "uint16";
  28. if (cppType == "int" || cppType == "i32")
  29. return "int";
  30. if (cppType == "unsigned" || cppType == "unsigned int" || cppType == "u32")
  31. return "uint";
  32. if (cppType == "long long" || cppType == "i64")
  33. return "int64";
  34. if (cppType == "unsigned long long" || cppType == "u64")
  35. return "uint64";
  36. if (cppType == "float")
  37. return "float";
  38. if (cppType == "double")
  39. return "double";
  40. // Types below are registered in Manual.cpp
  41. if (cppType == "long") // Size is compiler-dependent
  42. return "long";
  43. if (cppType == "unsigned long") // Size is compiler-dependent
  44. return "ulong";
  45. if (cppType == "size_t")
  46. return "size_t";
  47. if (cppType == "SDL_JoystickID")
  48. return "SDL_JoystickID";
  49. if (cppType == "c32")
  50. return "c32";
  51. if (cppType == "byte")
  52. return "byte";
  53. if (cppType == "hash16")
  54. return "hash16";
  55. if (cppType == "hash32")
  56. return "hash32";
  57. if (cppType == "hash64")
  58. return "hash64";
  59. throw Exception(cppType + " not a primitive type");
  60. }
  61. static bool IsKnownPrimitiveCppType(const string& cppType)
  62. {
  63. try
  64. {
  65. CppPrimitiveTypeToAS(cppType);
  66. }
  67. catch (...)
  68. {
  69. return false;
  70. }
  71. return true;
  72. }
  73. string JoinASDeclarations(const vector<ConvertedVariable>& vars)
  74. {
  75. string result;
  76. for (const ConvertedVariable& var : vars)
  77. {
  78. if (!var.asDeclaration_.empty())
  79. {
  80. if (!result.empty())
  81. result += ", ";
  82. result += var.asDeclaration_;
  83. }
  84. }
  85. return result;
  86. }
  87. string JoinCppDeclarations(const string& firstCppDeclaration, const vector<ConvertedVariable>& vars)
  88. {
  89. string result = firstCppDeclaration;
  90. for (const ConvertedVariable& var : vars)
  91. {
  92. if (!var.cppDeclaration_.empty())
  93. {
  94. if (!result.empty())
  95. result += ", ";
  96. result += var.cppDeclaration_;
  97. }
  98. }
  99. return result;
  100. }
  101. string JoinCppDeclarations(const vector<ConvertedVariable>& vars)
  102. {
  103. return JoinCppDeclarations("", vars);
  104. }
  105. shared_ptr<EnumAnalyzer> FindEnum(const string& name)
  106. {
  107. NamespaceAnalyzer namespaceAnalyzer(SourceData::namespaceUrho3D_);
  108. vector<EnumAnalyzer> enumAnalyzers = namespaceAnalyzer.GetEnums();
  109. for (const EnumAnalyzer& enumAnalyzer : enumAnalyzers)
  110. {
  111. if (enumAnalyzer.GetTypeName() == name)
  112. return make_shared<EnumAnalyzer>(enumAnalyzer);
  113. }
  114. return shared_ptr<EnumAnalyzer>();
  115. }
  116. static bool IsUsing(const string& identifier)
  117. {
  118. for (xml_node memberdef : SourceData::usings_)
  119. {
  120. UsingAnalyzer usingAnalyzer(memberdef);
  121. if (usingAnalyzer.GetName() == identifier)
  122. return true;
  123. }
  124. return false;
  125. }
  126. bool IsKnownCppType(const string& name)
  127. {
  128. static vector<string> _knownTypes = {
  129. "void",
  130. // TODO: Remove
  131. "VariantMap",
  132. };
  133. if (CONTAINS(_knownTypes, name))
  134. return true;
  135. if (IsKnownPrimitiveCppType(name))
  136. return true;
  137. if (SourceData::classesByName_.find(name) != SourceData::classesByName_.end())
  138. return true;
  139. if (SourceData::enums_.find(name) != SourceData::enums_.end())
  140. return true;
  141. if (EndsWith(name, "Flags"))
  142. return true;
  143. return false;
  144. }
  145. shared_ptr<ClassAnalyzer> FindClassByName(const string& name)
  146. {
  147. auto it = SourceData::classesByName_.find(name);
  148. if (it != SourceData::classesByName_.end())
  149. {
  150. xml_node compounddef = it->second;
  151. return make_shared<ClassAnalyzer>(compounddef);
  152. }
  153. // using VariantVector = Vector<Variant>
  154. return shared_ptr<ClassAnalyzer>();
  155. }
  156. shared_ptr<ClassAnalyzer> FindClassByID(const string& id)
  157. {
  158. auto it = SourceData::classesByID_.find(id);
  159. if (it != SourceData::classesByID_.end())
  160. {
  161. xml_node compounddef = it->second;
  162. return make_shared<ClassAnalyzer>(compounddef);
  163. }
  164. // using VariantVector = Vector<Variant>
  165. return shared_ptr<ClassAnalyzer>();
  166. }
  167. // Variable name can be empty for function return type
  168. ConvertedVariable CppVariableToAS(const TypeAnalyzer& type, VariableUsage usage, const string& name, const string& defaultValue)
  169. {
  170. ConvertedVariable result;
  171. if (usage == VariableUsage::PostfixIncDecParameter)
  172. {
  173. result.glue_ = " " + type.ToString() + " " + name + "{};\n";
  174. return result;
  175. }
  176. if (type.IsRvalueReference() || type.IsDoublePointer() || type.IsRefToPointer())
  177. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  178. string cppTypeName = type.GetNameWithTemplateParams();
  179. if (cppTypeName == "void")
  180. {
  181. if (usage == VariableUsage::FunctionReturn && !type.IsPointer())
  182. {
  183. result.asDeclaration_ = "void";
  184. result.cppDeclaration_ = "void";
  185. return result;
  186. }
  187. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  188. }
  189. if (cppTypeName == "Context")
  190. {
  191. if (usage == VariableUsage::FunctionParameter && type.IsPointer())
  192. {
  193. result.glue_ = " " + type.ToString() + " " + name + " = GetScriptContext();\n";
  194. return result;
  195. }
  196. throw Exception("Error: type \"" + type.ToString() + "\" can used only as function parameter");
  197. }
  198. // Works with both Vector<String> and Vector<String>&
  199. if (cppTypeName == "Vector<String>" || cppTypeName == "StringVector")
  200. {
  201. // Works with both Vector<String> and Vector<String>&
  202. if (usage == VariableUsage::FunctionReturn && !type.IsPointer())
  203. {
  204. result.asDeclaration_ = "Array<String>@";
  205. result.cppDeclaration_ = "CScriptArray*";
  206. result.glue_ = "return VectorToArray<String>(result, \"Array<String>\");\n";
  207. return result;
  208. }
  209. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  210. {
  211. string newCppVarName = name + "_conv";
  212. //result->asDecl_ = "String[]&";
  213. result.asDeclaration_ = "Array<String>@+";
  214. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  215. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToVector<String>(" + newCppVarName + ");\n";
  216. if (!defaultValue.empty())
  217. {
  218. assert(defaultValue == "Vector< String >()");
  219. //result->asDecl_ += " = Array<String>()";
  220. result.asDeclaration_ += " = null";
  221. }
  222. return result;
  223. }
  224. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  225. }
  226. smatch match;
  227. regex_match(cppTypeName, match, regex("SharedPtr<(\\w+)>"));
  228. if (!match.empty())
  229. {
  230. string cppSubtypeName = match[1].str();
  231. string asSubtypeName;
  232. try
  233. {
  234. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  235. }
  236. catch (...)
  237. {
  238. asSubtypeName = cppSubtypeName;
  239. }
  240. if (cppSubtypeName == "WorkItem") // TODO autodetect
  241. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  242. if (usage == VariableUsage::FunctionReturn)
  243. {
  244. result.asDeclaration_ = asSubtypeName + "@+";
  245. result.cppDeclaration_ = cppSubtypeName + "*";
  246. result.glue_ = "return result.Detach();\n";
  247. return result;
  248. }
  249. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  250. }
  251. regex_match(cppTypeName, match, regex("Vector<SharedPtr<(\\w+)>>"));
  252. if (!match.empty())
  253. {
  254. string cppSubtypeName = match[1].str();
  255. string asSubtypeName;
  256. try
  257. {
  258. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  259. }
  260. catch (...)
  261. {
  262. asSubtypeName = cppSubtypeName;
  263. }
  264. if (cppSubtypeName == "WorkItem") // TODO autodetect
  265. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  266. if (usage == VariableUsage::FunctionReturn)
  267. {
  268. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  269. result.cppDeclaration_ = "CScriptArray*";
  270. // Which variant is correct/better?
  271. #if 0
  272. result->glueResult_ = "return VectorToArray<SharedPtr<" + cppTypeName + ">>(result, \"Array<" + asTypeName + "@>@\");\n";
  273. #else
  274. result.glue_ = "return VectorToHandleArray(result, \"Array<" + asSubtypeName + "@>\");\n";
  275. #endif
  276. return result;
  277. }
  278. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  279. {
  280. string newCppVarName = name + "_conv";
  281. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@+";
  282. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  283. result.glue_ = " " + cppTypeName + " " + name + " = HandleArrayToVector<" + cppSubtypeName + ">(" + newCppVarName + ");\n";
  284. assert(defaultValue.empty()); // TODO: make
  285. return result;
  286. }
  287. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  288. }
  289. regex_match(cppTypeName, match, regex("Vector<(\\w+)\\*>"));
  290. if (!match.empty())
  291. {
  292. string cppSubtypeName = match[1].str();
  293. string asSubtypeName;
  294. try
  295. {
  296. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  297. }
  298. catch (...)
  299. {
  300. asSubtypeName = cppSubtypeName;
  301. }
  302. if (usage == VariableUsage::FunctionReturn)
  303. {
  304. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  305. result.cppDeclaration_ = "CScriptArray*";
  306. result.glue_ = "return VectorToHandleArray(result, \"Array<" + asSubtypeName + "@>\");\n";
  307. return result;
  308. }
  309. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  310. {
  311. string newCppVarName = name + "_conv";
  312. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  313. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  314. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToVector<" + cppSubtypeName + "*>(" + newCppVarName + ");\n";
  315. assert(defaultValue.empty()); // TODO: make
  316. return result;
  317. }
  318. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  319. }
  320. regex_match(cppTypeName, match, regex("Vector<([\\w ]+)>"));
  321. if (!match.empty())
  322. {
  323. string cppSubtypeName = match[1].str();
  324. string asSubtypeName;
  325. try
  326. {
  327. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  328. }
  329. catch (...)
  330. {
  331. asSubtypeName = cppSubtypeName;
  332. }
  333. if (usage == VariableUsage::FunctionReturn && type.IsConst() == type.IsReference() && !type.IsPointer())
  334. {
  335. result.asDeclaration_ = "Array<" + asSubtypeName + ">@";
  336. result.cppDeclaration_ = "CScriptArray*";
  337. result.glue_ = "return VectorToArray(result, \"Array<" + asSubtypeName + ">\");\n";
  338. return result;
  339. }
  340. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  341. {
  342. string newCppVarName = name + "_conv";
  343. result.asDeclaration_ = "Array<" + asSubtypeName + ">@+";
  344. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  345. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToVector<" + cppSubtypeName + ">(" + newCppVarName + ");\n";
  346. assert(defaultValue.empty()); // TODO: make
  347. return result;
  348. }
  349. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  350. }
  351. if (!IsKnownCppType(cppTypeName))
  352. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  353. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(cppTypeName);
  354. if (analyzer && analyzer->IsInternal())
  355. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  356. if (analyzer && Contains(analyzer->GetComment(), "NO_BIND"))
  357. throw Exception("Error: type \"" + cppTypeName + "\" can not automatically bind bacause have @nobind mark");
  358. if (analyzer && analyzer->IsAbstract() && !(analyzer->IsRefCounted() || analyzer->IsNoCount()))
  359. throw Exception("Error: type \"" + cppTypeName + "\" can not bind bacause abstract value");
  360. // analyzer can be null for simple types (int, float) or if type "using VariantVector = Vector<Variant>"
  361. // TODO add to type info "IsUsing"
  362. // TODO add description to TypeAnalyzer::GetClass()
  363. if (IsUsing(cppTypeName) && cppTypeName != "VariantMap")
  364. throw Exception("Using \"" + cppTypeName + "\" can not automatically bind");
  365. string asTypeName;
  366. try
  367. {
  368. asTypeName = CppPrimitiveTypeToAS(cppTypeName);
  369. }
  370. catch (...)
  371. {
  372. asTypeName = cppTypeName;
  373. }
  374. if (Contains(asTypeName, '<'))
  375. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  376. if (Contains(type.ToString(), "::"))
  377. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  378. if (usage == VariableUsage::FunctionParameter && type.IsConst() && type.IsReference())
  379. {
  380. result.asDeclaration_ = "const " + asTypeName + "&in";
  381. result.cppDeclaration_ = type.ToString();
  382. if (!name.empty())
  383. result.cppDeclaration_ += " " + name;
  384. if (!defaultValue.empty())
  385. {
  386. string asDefaultValue = CppValueToAS(defaultValue);
  387. asDefaultValue = ReplaceAll(asDefaultValue, "\"", "\\\"");
  388. result.asDeclaration_ += " = " + asDefaultValue;
  389. }
  390. return result;
  391. }
  392. result.asDeclaration_ = asTypeName;
  393. if (type.IsReference())
  394. {
  395. result.asDeclaration_ += "&";
  396. }
  397. else if (type.IsPointer())
  398. {
  399. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(cppTypeName);
  400. if (analyzer && (analyzer->IsRefCounted() || analyzer->IsNoCount()))
  401. result.asDeclaration_ = result.asDeclaration_ + "@" + (analyzer->IsNoCount() ? "" : "+");
  402. else
  403. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  404. }
  405. result.cppDeclaration_ = type.ToString();
  406. if (!name.empty())
  407. result.cppDeclaration_ += " " + name;
  408. if (usage == VariableUsage::FunctionReturn && type.IsConst() && !type.IsPointer())
  409. result.asDeclaration_ = "const " + result.asDeclaration_;
  410. if (!defaultValue.empty())
  411. {
  412. string asDefaultValue = CppValueToAS(defaultValue);
  413. asDefaultValue = ReplaceAll(asDefaultValue, "\"", "\\\"");
  414. result.asDeclaration_ += " = " + asDefaultValue;
  415. }
  416. return result;
  417. }
  418. string HideUnnamedType(const string& name)
  419. {
  420. regex rgx("@\\d+");
  421. return regex_replace(name, rgx, "@?");
  422. }
  423. string CppTypeToAS(const TypeAnalyzer& type, TypeUsage typeUsage)
  424. {
  425. if (Contains(type.GetName(), "@")) // Unnamed union inside class
  426. throw Exception("Error: type \"" + HideUnnamedType(type.ToString()) + "\" can not automatically bind");
  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