ASUtils.cpp 26 KB

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