ASUtils.cpp 26 KB

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