ASUtils.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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") && !type.IsPointer() && usage == VariableUsage::FunctionReturn)
  173. {
  174. result.asDeclaration_ = "Array<String>@";
  175. result.cppDeclaration_ = "CScriptArray*";
  176. result.glue_ = "return VectorToArray<String>(result, \"Array<String>\");\n";
  177. return result;
  178. }
  179. smatch match;
  180. regex_match(cppTypeName, match, regex("SharedPtr<(\\w+)>"));
  181. if (match.size() == 2 && usage == VariableUsage::FunctionReturn)
  182. {
  183. string cppSubtypeName = match[1].str();
  184. string asSubtypeName;
  185. try
  186. {
  187. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  188. }
  189. catch (...)
  190. {
  191. asSubtypeName = cppSubtypeName;
  192. }
  193. if (cppSubtypeName == "WorkItem") // TODO autodetect
  194. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  195. result.asDeclaration_ = asSubtypeName + "@+";
  196. result.cppDeclaration_ = cppSubtypeName + "*";
  197. result.glue_ = "return result.Detach();\n";
  198. return result;
  199. }
  200. regex_match(cppTypeName, match, regex("Vector<SharedPtr<(\\w+)>>"));
  201. if (match.size() == 2 && usage == VariableUsage::FunctionReturn)
  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. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  214. result.cppDeclaration_ = "CScriptArray*";
  215. // Which variant is correct/better?
  216. #if 0
  217. result->glueResult_ = "return VectorToArray<SharedPtr<" + cppTypeName + "> >(result, \"Array<" + asTypeName + "@>@\");\n";
  218. #else
  219. result.glue_ = "return VectorToHandleArray(result, \"Array<" + asSubtypeName + "@>\");\n";
  220. #endif
  221. return result;
  222. }
  223. regex_match(cppTypeName, match, regex("PODVector<(\\w+)\\*>"));
  224. if (match.size() == 2 && usage == VariableUsage::FunctionReturn)
  225. {
  226. string cppSubtypeName = match[1].str();
  227. string asSubtypeName;
  228. try
  229. {
  230. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  231. }
  232. catch (...)
  233. {
  234. asSubtypeName = cppSubtypeName;
  235. }
  236. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  237. result.cppDeclaration_ = "CScriptArray*";
  238. result.glue_ = "return VectorToHandleArray(result, \"Array<" + asSubtypeName + "@>\");\n";
  239. return result;
  240. }
  241. regex_match(cppTypeName, match, regex("PODVector<(\\w+)>"));
  242. if (match.size() == 2 && type.IsConst() == type.IsReference() && usage == VariableUsage::FunctionReturn)
  243. {
  244. string cppSubtypeName = match[1].str();
  245. string asSubtypeName;
  246. try
  247. {
  248. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  249. }
  250. catch (...)
  251. {
  252. asSubtypeName = cppSubtypeName;
  253. }
  254. result.asDeclaration_ = "Array<" + asSubtypeName + ">@";
  255. result.cppDeclaration_ = "CScriptArray*";
  256. result.glue_ = "return VectorToArray(result, \"Array<" + asSubtypeName + ">\");\n";
  257. return result;
  258. }
  259. // =============================================================================
  260. if (cppTypeName == "Vector<String>" && type.IsConst() && type.IsReference() && usage == VariableUsage::FunctionParameter)
  261. {
  262. string newCppVarName = name + "_conv";
  263. //result->asDecl_ = "String[]&";
  264. result.asDeclaration_ = "Array<String>@+";
  265. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  266. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToVector<String>(" + newCppVarName + ");\n";
  267. if (!defaultValue.empty())
  268. {
  269. assert(defaultValue == "Vector< String >()");
  270. //result->asDecl_ += " = Array<String>()";
  271. result.asDeclaration_ += " = null";
  272. }
  273. return result;
  274. }
  275. regex_match(cppTypeName, match, regex("PODVector<(\\w+)>"));
  276. if (match.size() == 2 && type.IsConst() && type.IsReference() && usage == VariableUsage::FunctionParameter)
  277. {
  278. string cppSubtypeName = match[1].str();
  279. string asSubtypeName;
  280. try
  281. {
  282. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  283. }
  284. catch (...)
  285. {
  286. asSubtypeName = cppSubtypeName;
  287. }
  288. string newCppVarName = name + "_conv";
  289. result.asDeclaration_ = "Array<" + asSubtypeName + ">@+";
  290. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  291. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToPODVector<" + cppSubtypeName + ">(" + newCppVarName + ");\n";
  292. assert(defaultValue.empty()); // TODO: make
  293. return result;
  294. }
  295. regex_match(cppTypeName, match, regex("PODVector<(\\w+)\\*>"));
  296. // TODO check \\w is refcounted
  297. if (match.size() == 2 && type.IsConst() && type.IsReference() && usage == VariableUsage::FunctionParameter)
  298. {
  299. string cppSubtypeName = match[1].str();
  300. string asSubtypeName;
  301. try
  302. {
  303. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  304. }
  305. catch (...)
  306. {
  307. asSubtypeName = cppSubtypeName;
  308. }
  309. string newCppVarName = name + "_conv";
  310. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@";
  311. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  312. result.glue_ = " " + cppTypeName + " " + name + " = ArrayToPODVector<" + cppSubtypeName + "*>(" + newCppVarName + ");\n";
  313. assert(defaultValue.empty()); // TODO: make
  314. return result;
  315. }
  316. regex_match(cppTypeName, match, regex("Vector<SharedPtr<(\\w+)>>"));
  317. if (match.size() == 2 && type.IsConst() && type.IsReference() && usage == VariableUsage::FunctionParameter)
  318. {
  319. string cppSubtypeName = match[1].str();
  320. string asSubtypeName;
  321. try
  322. {
  323. asSubtypeName = CppPrimitiveTypeToAS(cppSubtypeName);
  324. }
  325. catch (...)
  326. {
  327. asSubtypeName = cppSubtypeName;
  328. }
  329. if (cppSubtypeName == "WorkItem") // TODO autodetect
  330. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  331. string newCppVarName = name + "_conv";
  332. result.asDeclaration_ = "Array<" + asSubtypeName + "@>@+";
  333. result.cppDeclaration_ = "CScriptArray* " + newCppVarName;
  334. result.glue_ = " " + cppTypeName + " " + name + " = HandleArrayToVector<" + cppSubtypeName + ">(" + newCppVarName + ");\n";
  335. assert(defaultValue.empty()); // TODO: make
  336. return result;
  337. }
  338. // =============================================================================
  339. if (!IsKnownCppType(cppTypeName))
  340. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  341. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(cppTypeName);
  342. if (analyzer && analyzer->IsInternal())
  343. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  344. if (analyzer && Contains(analyzer->GetComment(), "NO_BIND"))
  345. throw Exception("Error: type \"" + cppTypeName + "\" can not automatically bind bacause have @nobind mark");
  346. // analyzer can be null for simple types (int, float) or if type "using VariantVector = Vector<Variant>"
  347. // TODO add to type info "IsUsing"
  348. // TODO add description to TypeAnalyzer::GetClass()
  349. if (IsUsing(cppTypeName) && cppTypeName != "VariantMap")
  350. throw Exception("Using \"" + cppTypeName + "\" can not automatically bind");
  351. string asTypeName;
  352. try
  353. {
  354. asTypeName = CppPrimitiveTypeToAS(cppTypeName);
  355. }
  356. catch (...)
  357. {
  358. asTypeName = cppTypeName;
  359. }
  360. if (asTypeName.find('<') != string::npos)
  361. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  362. if (Contains(type.ToString(), "::"))
  363. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  364. if (type.IsConst() && type.IsReference() && usage == VariableUsage::FunctionParameter)
  365. {
  366. result.asDeclaration_ = "const " + asTypeName + "&in";
  367. result.cppDeclaration_ = type.ToString();
  368. if (!name.empty())
  369. result.cppDeclaration_ += " " + name;
  370. if (!defaultValue.empty())
  371. {
  372. string asDefaultValue = CppValueToAS(defaultValue);
  373. asDefaultValue = ReplaceAll(asDefaultValue, "\"", "\\\"");
  374. result.asDeclaration_ += " = " + asDefaultValue;
  375. }
  376. //if (!name.empty())
  377. // result.asDeclaration_ += result.asDeclaration_ + " " + name;
  378. return result;
  379. }
  380. result.asDeclaration_ = asTypeName;
  381. if (type.IsReference())
  382. {
  383. result.asDeclaration_ += "&";
  384. }
  385. else if (type.IsPointer())
  386. {
  387. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(cppTypeName);
  388. if (analyzer && (analyzer->IsRefCounted() || Contains(analyzer->GetComment(), "FAKE_REF")))
  389. result.asDeclaration_ += "@+";
  390. else
  391. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  392. }
  393. result.cppDeclaration_ = type.ToString();
  394. if (!name.empty())
  395. result.cppDeclaration_ += " " + name;
  396. if (usage == VariableUsage::FunctionReturn && type.IsConst() && !type.IsPointer())
  397. result.asDeclaration_ = "const " + result.asDeclaration_;
  398. //if (!name.empty())
  399. // result.asDeclaration_ += result.asDeclaration_ + " " + name;
  400. if (!defaultValue.empty())
  401. {
  402. string asDefaultValue = CppValueToAS(defaultValue);
  403. asDefaultValue = ReplaceAll(asDefaultValue, "\"", "\\\"");
  404. result.asDeclaration_ += " = " + asDefaultValue;
  405. }
  406. return result;
  407. }
  408. string CppTypeToAS(const TypeAnalyzer& type, TypeUsage typeUsage)
  409. {
  410. if (type.IsRvalueReference() || type.IsDoublePointer() || type.IsRefToPointer())
  411. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  412. string cppTypeName = type.GetNameWithTemplateParams();
  413. if (cppTypeName == "Context" && typeUsage == TypeUsage::FunctionReturn)
  414. throw Exception("Error: type \"" + type.ToString() + "\" can not be returned");
  415. if (!IsKnownCppType(type.GetNameWithTemplateParams()))
  416. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  417. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(type.GetNameWithTemplateParams());
  418. if (analyzer && analyzer->IsInternal())
  419. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  420. if (analyzer && Contains(analyzer->GetComment(), "NO_BIND"))
  421. throw Exception("Error: type \"" + cppTypeName + "\" can not automatically bind bacause have @nobind mark");
  422. // analyzer can be null for simple types (int, float) or if type "using VariantVector = Vector<Variant>"
  423. // TODO add to type info "IsUsing"
  424. // TODO add description to TypeAnalyzer::GetClass()
  425. if (IsUsing(cppTypeName) && cppTypeName != "VariantMap")
  426. throw Exception("Using \"" + cppTypeName + "\" can not automatically bind");
  427. string asTypeName;
  428. try
  429. {
  430. asTypeName = CppPrimitiveTypeToAS(cppTypeName);
  431. }
  432. catch (...)
  433. {
  434. asTypeName = cppTypeName;
  435. }
  436. if (asTypeName == "void" && type.IsPointer())
  437. throw Exception("Error: type \"void*\" can not automatically bind");
  438. if (asTypeName.find('<') != string::npos)
  439. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  440. if (Contains(type.ToString(), "::"))
  441. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind bacause internal");
  442. if (type.IsConst() && type.IsReference() && typeUsage == TypeUsage::FunctionParameter)
  443. return "const " + asTypeName + "&in";
  444. string result = asTypeName;
  445. if (type.IsReference())
  446. {
  447. result += "&";
  448. }
  449. else if (type.IsPointer())
  450. {
  451. shared_ptr<ClassAnalyzer> analyzer = FindClassByName(type.GetNameWithTemplateParams());
  452. if (analyzer && (analyzer->IsRefCounted() || Contains(analyzer->GetComment(), "FAKE_REF")))
  453. result += "@+";
  454. else
  455. throw Exception("Error: type \"" + type.ToString() + "\" can not automatically bind");
  456. }
  457. if (typeUsage == TypeUsage::FunctionReturn && type.IsConst() && !type.IsPointer())
  458. result = "const " + result;
  459. return result;
  460. }
  461. string CppValueToAS(const string& cppValue)
  462. {
  463. if (cppValue == "nullptr")
  464. return "null";
  465. if (cppValue == "Variant::emptyVariantMap")
  466. return "VariantMap()";
  467. if (cppValue == "NPOS")
  468. return "String::NPOS";
  469. return cppValue;
  470. }
  471. // =================================================================================
  472. static string GenerateFunctionWrapperName(xml_node memberdef)
  473. {
  474. string result = ExtractName(memberdef);
  475. vector<ParamAnalyzer> params = ExtractParams(memberdef);
  476. if (params.size() == 0)
  477. {
  478. result += "_void";
  479. }
  480. else
  481. {
  482. for (ParamAnalyzer param : params)
  483. {
  484. string t = param.GetType().GetNameWithTemplateParams();
  485. t = ReplaceAll(t, " ", "");
  486. t = ReplaceAll(t, "::", "");
  487. t = ReplaceAll(t, "<", "");
  488. t = ReplaceAll(t, ">", "");
  489. t = ReplaceAll(t, "*", "");
  490. result += "_" + t;
  491. }
  492. }
  493. return result;
  494. }
  495. string GenerateWrapperName(const GlobalFunctionAnalyzer& functionAnalyzer)
  496. {
  497. return GenerateFunctionWrapperName(functionAnalyzer.GetMemberdef());
  498. }
  499. string GenerateWrapperName(const ClassStaticFunctionAnalyzer& functionAnalyzer)
  500. {
  501. return functionAnalyzer.GetClassName() + "_" + GenerateFunctionWrapperName(functionAnalyzer.GetMemberdef());
  502. }
  503. string GenerateWrapperName(const ClassFunctionAnalyzer& functionAnalyzer, bool templateVersion)
  504. {
  505. if (templateVersion)
  506. return functionAnalyzer.GetClassName() + "_" + GenerateFunctionWrapperName(functionAnalyzer.GetMemberdef()) + "_template";
  507. else
  508. return functionAnalyzer.GetClassName() + "_" + GenerateFunctionWrapperName(functionAnalyzer.GetMemberdef());
  509. }
  510. // =================================================================================
  511. string GenerateWrapper(const GlobalFunctionAnalyzer& functionAnalyzer, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn)
  512. {
  513. string result;
  514. string glueReturnType = convertedReturn.cppDeclaration_;
  515. vector<ParamAnalyzer> params = functionAnalyzer.GetParams();
  516. result = "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(";
  517. string cppDecl;
  518. for (size_t i = 0; i < convertedParams.size(); i++)
  519. {
  520. if (!convertedParams[i].cppDeclaration_.empty())
  521. {
  522. if (!cppDecl.empty())
  523. cppDecl += ", ";
  524. cppDecl += convertedParams[i].cppDeclaration_;
  525. }
  526. }
  527. result += cppDecl;
  528. result +=
  529. ")\n"
  530. "{\n";
  531. for (size_t i = 0; i < convertedParams.size(); i++)
  532. result += convertedParams[i].glue_;
  533. if (glueReturnType != "void")
  534. result += " " + functionAnalyzer.GetReturnType().ToString() + " result = ";
  535. else
  536. result += " ";
  537. result += functionAnalyzer.GetName() + "(";
  538. for (size_t i = 0; i < convertedParams.size(); i++)
  539. {
  540. if (i != 0)
  541. result += ", ";
  542. result += params[i].GetDeclname();
  543. }
  544. result += ");\n";
  545. if (!convertedReturn.glue_.empty())
  546. result += " " + convertedReturn.glue_;
  547. else if (glueReturnType != "void")
  548. result += " return result;\n";
  549. result += "}";
  550. return result;
  551. }
  552. string GenerateWrapper(const ClassStaticFunctionAnalyzer& functionAnalyzer, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn)
  553. {
  554. string result;
  555. string glueReturnType = convertedReturn.cppDeclaration_;
  556. string insideDefine = InsideDefine(functionAnalyzer.GetHeaderFile());
  557. if (!insideDefine.empty())
  558. result += "#ifdef " + insideDefine + "\n";
  559. result +=
  560. "// " + functionAnalyzer.GetLocation() + "\n"
  561. "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer) + "(";
  562. vector<ParamAnalyzer> params = functionAnalyzer.GetParams();
  563. string cppDecl;
  564. for (size_t i = 0; i < convertedParams.size(); i++)
  565. {
  566. if (!convertedParams[i].cppDeclaration_.empty())
  567. {
  568. if (!cppDecl.empty())
  569. cppDecl += ", ";
  570. cppDecl += convertedParams[i].cppDeclaration_;
  571. }
  572. }
  573. result += cppDecl;
  574. result +=
  575. ")\n"
  576. "{\n";
  577. for (size_t i = 0; i < convertedParams.size(); i++)
  578. result += convertedParams[i].glue_;
  579. if (glueReturnType != "void")
  580. result += " " + functionAnalyzer.GetReturnType().ToString() + " result = ";
  581. else
  582. result += " ";
  583. result += functionAnalyzer.GetClassName() + "::" + functionAnalyzer.GetName() + "(";
  584. for (size_t i = 0; i < convertedParams.size(); i++)
  585. {
  586. if (i != 0)
  587. result += ", ";
  588. result += params[i].GetDeclname();
  589. }
  590. result += ");\n";
  591. if (!convertedReturn.glue_.empty())
  592. result += " " + convertedReturn.glue_;
  593. else if (glueReturnType != "void")
  594. result += " return result;\n";
  595. result += "}\n";
  596. if (!insideDefine.empty())
  597. result += "#endif\n";
  598. result += "\n";
  599. return result;
  600. }
  601. string GenerateWrapper(const ClassFunctionAnalyzer& functionAnalyzer, bool templateVersion, const vector<ConvertedVariable>& convertedParams, const ConvertedVariable& convertedReturn)
  602. {
  603. string result;
  604. string insideDefine = InsideDefine(functionAnalyzer.GetClass().GetHeaderFile());
  605. if (!insideDefine.empty())
  606. result += "#ifdef " + insideDefine + "\n";
  607. string glueReturnType = convertedReturn.cppDeclaration_;
  608. result +=
  609. "// " + functionAnalyzer.GetLocation() + "\n"
  610. "static " + glueReturnType + " " + GenerateWrapperName(functionAnalyzer, templateVersion) + "(";
  611. vector<ParamAnalyzer> params = functionAnalyzer.GetParams();
  612. string cppDecl = functionAnalyzer.GetClassName() + string("* ptr");
  613. for (size_t i = 0; i < convertedParams.size(); i++)
  614. {
  615. if (!convertedParams[i].cppDeclaration_.empty())
  616. {
  617. if (!cppDecl.empty())
  618. cppDecl += ", ";
  619. cppDecl += convertedParams[i].cppDeclaration_;
  620. }
  621. }
  622. result += cppDecl;
  623. result +=
  624. ")\n"
  625. "{\n";
  626. for (size_t i = 0; i < convertedParams.size(); i++)
  627. result += convertedParams[i].glue_;
  628. if (glueReturnType != "void")
  629. result += " " + functionAnalyzer.GetReturnType().ToString() + " result = ";
  630. else
  631. result += " ";
  632. result += "ptr->" + functionAnalyzer.GetName() + "(";
  633. for (size_t i = 0; i < convertedParams.size(); i++)
  634. {
  635. if (i != 0)
  636. result += ", ";
  637. result += params[i].GetDeclname();
  638. }
  639. result += ");\n";
  640. if (!convertedReturn.glue_.empty())
  641. result += " " + convertedReturn.glue_;
  642. else if (glueReturnType != "void")
  643. result += " return result;\n";
  644. result += "}\n";
  645. if (!insideDefine.empty())
  646. result += "#endif\n";
  647. result += "\n";
  648. return result;
  649. }
  650. // =================================================================================
  651. string Generate_asFUNCTIONPR(const GlobalFunctionAnalyzer& functionAnalyzer)
  652. {
  653. string functionName = functionAnalyzer.GetName();
  654. string cppParams = "(" + JoinParamsTypes(functionAnalyzer.GetMemberdef(), functionAnalyzer.GetSpecialization()) + ")";
  655. string returnType = functionAnalyzer.GetReturnType().ToString();
  656. return "asFUNCTIONPR(" + functionName + ", " + cppParams + ", " + returnType + ")";
  657. }
  658. string Generate_asFUNCTIONPR(const ClassStaticFunctionAnalyzer& functionAnalyzer)
  659. {
  660. string className = functionAnalyzer.GetClassName();
  661. string functionName = functionAnalyzer.GetName();
  662. string cppParams = "(" + JoinParamsTypes(functionAnalyzer.GetMemberdef(), functionAnalyzer.GetSpecialization()) + ")";
  663. string returnType = functionAnalyzer.GetReturnType().ToString();
  664. return "asFUNCTIONPR(" + className + "::" + functionName + ", " + cppParams + ", " + returnType + ")";
  665. }
  666. string Generate_asMETHODPR(const ClassFunctionAnalyzer& functionAnalyzer, bool templateVersion)
  667. {
  668. string className = functionAnalyzer.GetClassName();
  669. string functionName = functionAnalyzer.GetName();
  670. string cppParams = "(" + JoinParamsTypes(functionAnalyzer.GetMemberdef(), functionAnalyzer.GetSpecialization()) + ")";
  671. if (functionAnalyzer.IsConst())
  672. cppParams += " const";
  673. string returnType = functionAnalyzer.GetReturnType().ToString();
  674. if (templateVersion)
  675. return "asMETHODPR(T, " + functionName + ", " + cppParams + ", " + returnType + ")";
  676. else
  677. return "asMETHODPR(" + className + ", " + functionName + ", " + cppParams + ", " + returnType + ")";
  678. }
  679. } // namespace ASBindingGenerator