CSClassWriter.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  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 <Atomic/IO/FileSystem.h>
  23. #include "../JSBind.h"
  24. #include "../JSBModule.h"
  25. #include "../JSBPackage.h"
  26. #include "../JSBEnum.h"
  27. #include "../JSBClass.h"
  28. #include "../JSBFunction.h"
  29. #include "CSTypeHelper.h"
  30. #include "CSClassWriter.h"
  31. #include "CSFunctionWriter.h"
  32. namespace ToolCore
  33. {
  34. CSClassWriter::CSClassWriter(JSBClass *klass) : JSBClassWriter(klass)
  35. {
  36. }
  37. void CSClassWriter::WriteNativeFunctions(String& source)
  38. {
  39. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  40. {
  41. JSBFunction* function = klass_->functions_.At(i);
  42. if (function->Skip())
  43. continue;
  44. if (klass_->IsInterface() && function->IsConstructor())
  45. continue;
  46. if (function->IsDestructor())
  47. continue;
  48. if (CSTypeHelper::OmitFunction(function))
  49. continue;
  50. CSFunctionWriter writer(function);
  51. writer.GenerateNativeSource(source);
  52. }
  53. }
  54. void CSClassWriter::GenerateNativeSource(String& sourceOut)
  55. {
  56. String source = "";
  57. if (klass_->IsNumberArray())
  58. return;
  59. JSBPackage* package = klass_->GetPackage();
  60. if (!klass_->IsInterface())
  61. {
  62. source.AppendWithFormat("ATOMIC_EXPORT_API ClassID csb_%s_%s_GetClassIDStatic()\n{\n", package->GetName().CString(), klass_->GetName().CString());
  63. source.AppendWithFormat(" return %s::GetClassIDStatic();\n}\n\n", klass_->GetNativeName().CString());
  64. }
  65. WriteNativeFunctions(source);
  66. sourceOut += source;
  67. }
  68. void CSClassWriter::WriteManagedProperties(String& sourceOut)
  69. {
  70. String source;
  71. if (klass_->HasProperties())
  72. {
  73. Vector<String> pnames;
  74. klass_->GetPropertyNames(pnames);
  75. for (unsigned j = 0; j < pnames.Size(); j++)
  76. {
  77. JSBProperty* prop = klass_->GetProperty(pnames[j]);
  78. JSBFunctionType* fType = NULL;
  79. JSBFunctionType* getType = NULL;
  80. JSBFunctionType* setType = NULL;
  81. if (CSTypeHelper::OmitFunction(prop->getter_) || CSTypeHelper::OmitFunction(prop->setter_))
  82. continue;
  83. if (prop->getter_ && !prop->getter_->Skip())
  84. {
  85. fType = getType = prop->getter_->GetReturnType();
  86. }
  87. if (prop->setter_ && !prop->setter_->Skip())
  88. {
  89. setType = prop->setter_->GetParameters()[0];
  90. if (!fType)
  91. fType = setType;
  92. else if (fType->type_->ToString() != setType->type_->ToString())
  93. continue;
  94. }
  95. if (!fType)
  96. continue;
  97. String line = klass_->IsInterface() ? "" : "public ";
  98. JSBClass* baseClass = klass_->GetBaseClass();
  99. if (baseClass)
  100. {
  101. if (baseClass->MatchProperty(prop, true))
  102. {
  103. // always new so we don't have to deal with virtual/override on properties
  104. line += "new ";
  105. }
  106. }
  107. String type = CSTypeHelper::GetManagedTypeString(fType, false);
  108. line += ToString("%s %s\n", type.CString(), prop->name_.CString());
  109. source += IndentLine(line);
  110. source += IndentLine("{\n");
  111. Indent();
  112. if (prop->getter_)
  113. {
  114. if (klass_->IsInterface())
  115. {
  116. source += IndentLine("get;\n");
  117. }
  118. else
  119. {
  120. source += IndentLine("get\n");
  121. source += IndentLine("{\n");
  122. Indent();
  123. source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString()));
  124. Dedent();
  125. source += IndentLine("}\n");
  126. }
  127. }
  128. if (prop->setter_)
  129. {
  130. if (klass_->IsInterface())
  131. {
  132. source += IndentLine("set;\n");
  133. }
  134. else
  135. {
  136. source += IndentLine("set\n");
  137. source += IndentLine("{\n");
  138. Indent();
  139. source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString()));
  140. Dedent();
  141. source += IndentLine("}\n");
  142. }
  143. }
  144. Dedent();
  145. source += IndentLine("}\n\n");
  146. }
  147. }
  148. sourceOut += source;
  149. }
  150. void CSClassWriter::GenerateManagedSource(String& sourceOut)
  151. {
  152. String source = "";
  153. if (klass_->IsNumberArray())
  154. return;
  155. Indent();
  156. source += "\n";
  157. String line;
  158. if (klass_->GetDocString().Length())
  159. {
  160. // monodocer -assembly:NETCore.dll -path:en -pretty
  161. // mdoc export-html -o htmldocs en
  162. source += IndentLine("/// <summary>\n");
  163. if (klass_->GetDocString().Contains('\n'))
  164. source += IndentLine("/* " + klass_->GetDocString() + "*/\n");
  165. else
  166. source += IndentLine("/// " + klass_->GetDocString() + "\n");
  167. source += IndentLine("/// </summary>\n");
  168. }
  169. if (klass_->GetBaseClass())
  170. {
  171. String baseString = klass_->GetBaseClass()->GetName();
  172. const PODVector<JSBClass*>& interfaces = klass_->GetInterfaces();
  173. if (interfaces.Size())
  174. {
  175. StringVector baseStrings;
  176. baseStrings.Push(baseString);
  177. for (unsigned i = 0; i < interfaces.Size(); i++)
  178. {
  179. baseStrings.Push(interfaces.At(i)->GetName());
  180. }
  181. baseString = String::Joined(baseStrings, ",");
  182. }
  183. line = ToString("public partial class %s%s : %s\n", klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "", baseString.CString());
  184. }
  185. else
  186. {
  187. String classString = "class";
  188. if (klass_->IsInterface())
  189. classString = "interface";
  190. line = ToString("public partial %s %s%s\n", classString.CString(), klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "");
  191. }
  192. source += IndentLine(line);
  193. source += IndentLine("{\n");
  194. Indent();
  195. WriteManagedProperties(source);
  196. JSBPackage* package = klass_->GetPackage();
  197. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  198. // https://github.com/dotnet/coreclr/issues/1605
  199. // line = "[SuppressUnmanagedCodeSecurity]\n";
  200. // source += IndentLine(line);
  201. if (!klass_->IsInterface())
  202. {
  203. line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  204. source += IndentLine(line);
  205. line = ToString("public static extern IntPtr csb_%s_%s_GetClassIDStatic();\n", package->GetName().CString(), klass_->GetName().CString());
  206. source += IndentLine(line);
  207. source += "\n";
  208. }
  209. Dedent();
  210. // managed functions
  211. CSFunctionWriter::SetWroteConstructor(false);
  212. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  213. {
  214. JSBFunction* function = klass_->functions_.At(i);
  215. if (function->Skip())
  216. continue;
  217. if (klass_->IsInterface() && function->IsConstructor())
  218. continue;
  219. if (function->IsDestructor())
  220. continue;
  221. if (CSTypeHelper::OmitFunction(function))
  222. continue;
  223. CSFunctionWriter fwriter(function);
  224. fwriter.GenerateManagedSource(source);
  225. }
  226. // There are some constructors being skipped (like HTTPRequest as it uses a vector of strings in args)
  227. // Make sure we have at least a IntPtr version
  228. if (!klass_->IsInterface() && !CSFunctionWriter::GetWroteConstructor() && klass_->GetName() != "RefCounted")
  229. {
  230. ATOMIC_LOGINFOF("WARNING: %s class didn't write a constructor, filling in generated native constructor", klass_->GetName().CString());
  231. line = ToString("public %s (IntPtr native) : base (native)\n", klass_->GetName().CString());
  232. source += IndentLine(line);
  233. source += IndentLine("{\n");
  234. source += IndentLine("}\n\n");
  235. }
  236. CSFunctionWriter::SetWroteConstructor(false);
  237. source += IndentLine("}\n");
  238. Dedent();
  239. sourceOut += source;
  240. }
  241. void CSClassWriter::GenerateSource(String& sourceOut)
  242. {
  243. }
  244. }