ASUtils.cpp 28 KB

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