CSClassWriter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 (klass_->IsInterface())
  84. {
  85. if ((prop->getter_ && prop->getter_->IsInheritedInterface()) ||
  86. (prop->setter_ && prop->setter_->IsInheritedInterface()))
  87. {
  88. // note possibility of mismatched inheritance on get/set
  89. // if possible to trip this in C#, will need to be addressed here
  90. continue;
  91. }
  92. }
  93. if (prop->getter_ && !prop->getter_->Skip())
  94. {
  95. fType = getType = prop->getter_->GetReturnType();
  96. }
  97. if (prop->setter_ && !prop->setter_->Skip())
  98. {
  99. setType = prop->setter_->GetParameters()[0];
  100. if (!fType)
  101. fType = setType;
  102. else if (fType->type_->ToString() != setType->type_->ToString())
  103. continue;
  104. }
  105. if (!fType)
  106. continue;
  107. String line = klass_->IsInterface() ? "" : "public ";
  108. JSBClass* baseClass = klass_->GetBaseClass();
  109. if (baseClass)
  110. {
  111. if (baseClass->MatchProperty(prop, true))
  112. {
  113. // always new so we don't have to deal with virtual/override on properties
  114. line += "new ";
  115. }
  116. }
  117. String type = CSTypeHelper::GetManagedTypeString(fType, false);
  118. line += ToString("%s %s\n", type.CString(), prop->name_.CString());
  119. source += IndentLine(line);
  120. source += IndentLine("{\n");
  121. Indent();
  122. if (prop->getter_)
  123. {
  124. if (klass_->IsInterface())
  125. {
  126. source += IndentLine("get;\n");
  127. }
  128. else
  129. {
  130. source += IndentLine("get\n");
  131. source += IndentLine("{\n");
  132. Indent();
  133. source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString()));
  134. Dedent();
  135. source += IndentLine("}\n");
  136. }
  137. }
  138. if (prop->setter_)
  139. {
  140. if (klass_->IsInterface())
  141. {
  142. source += IndentLine("set;\n");
  143. }
  144. else
  145. {
  146. source += IndentLine("set\n");
  147. source += IndentLine("{\n");
  148. Indent();
  149. source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString()));
  150. Dedent();
  151. source += IndentLine("}\n");
  152. }
  153. }
  154. Dedent();
  155. source += IndentLine("}\n\n");
  156. }
  157. }
  158. sourceOut += source;
  159. }
  160. void CSClassWriter::GenerateManagedSource(String& sourceOut)
  161. {
  162. String source = "";
  163. if (klass_->IsNumberArray())
  164. return;
  165. Indent();
  166. source += "\n";
  167. String line;
  168. if (klass_->GetDocString().Length())
  169. {
  170. // monodocer -assembly:NETCore.dll -path:en -pretty
  171. // mdoc export-html -o htmldocs en
  172. source += IndentLine("/// <summary>\n");
  173. if (klass_->GetDocString().Contains('\n'))
  174. source += IndentLine("/* " + klass_->GetDocString() + "*/\n");
  175. else
  176. source += IndentLine("/// " + klass_->GetDocString() + "\n");
  177. source += IndentLine("/// </summary>\n");
  178. }
  179. JSBClass* baseClass = klass_->GetBaseClass();
  180. const StringVector& csharpInterfaces = klass_->GetCSharpInterfaces();
  181. const PODVector<JSBClass*>& nativeInterfaces = klass_->GetInterfaces();
  182. String classString = "class";
  183. if (klass_->IsInterface())
  184. classString = "interface";
  185. if (baseClass || csharpInterfaces.Size() || nativeInterfaces.Size())
  186. {
  187. StringVector baseStrings;
  188. if (baseClass)
  189. {
  190. baseStrings.Push(baseClass->GetName());
  191. }
  192. for (unsigned i = 0; i < nativeInterfaces.Size(); i++)
  193. {
  194. baseStrings.Push(nativeInterfaces.At(i)->GetName());
  195. }
  196. for (unsigned i = 0; i < csharpInterfaces.Size(); i++)
  197. {
  198. baseStrings.Push(csharpInterfaces[i]);
  199. }
  200. String baseString = String::Joined(baseStrings, ", ");
  201. line = ToString("public partial %s %s%s : %s\n", classString.CString(), klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "", baseString.CString());
  202. }
  203. else
  204. {
  205. line = ToString("public partial %s %s%s\n", classString.CString(), klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "");
  206. }
  207. source += IndentLine(line);
  208. source += IndentLine("{\n");
  209. Indent();
  210. WriteManagedProperties(source);
  211. JSBPackage* package = klass_->GetPackage();
  212. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  213. // https://github.com/dotnet/coreclr/issues/1605
  214. // line = "[SuppressUnmanagedCodeSecurity]\n";
  215. // source += IndentLine(line);
  216. if (!klass_->IsInterface())
  217. {
  218. line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  219. source += IndentLine(line);
  220. line = ToString("public static extern IntPtr csb_%s_%s_GetClassIDStatic();\n", package->GetName().CString(), klass_->GetName().CString());
  221. source += IndentLine(line);
  222. source += "\n";
  223. }
  224. Dedent();
  225. // managed functions
  226. CSFunctionWriter::SetWroteConstructor(false);
  227. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  228. {
  229. JSBFunction* function = klass_->functions_.At(i);
  230. if (function->Skip())
  231. continue;
  232. if (klass_->IsInterface() && function->IsConstructor())
  233. continue;
  234. if (function->IsDestructor())
  235. continue;
  236. if (klass_->IsInterface() && function->IsInheritedInterface())
  237. continue;
  238. if (CSTypeHelper::OmitFunction(function))
  239. continue;
  240. CSFunctionWriter fwriter(function);
  241. fwriter.GenerateManagedSource(source);
  242. }
  243. // There are some constructors being skipped (like HTTPRequest as it uses a vector of strings in args)
  244. // Make sure we have at least a IntPtr version
  245. if (!klass_->IsInterface() && !CSFunctionWriter::GetWroteConstructor() && klass_->GetName() != "RefCounted")
  246. {
  247. ATOMIC_LOGINFOF("WARNING: %s class didn't write a constructor, filling in generated native constructor", klass_->GetName().CString());
  248. line = ToString("public %s (IntPtr native) : base (native)\n", klass_->GetName().CString());
  249. source += IndentLine(line);
  250. source += IndentLine("{\n");
  251. source += IndentLine("}\n\n");
  252. }
  253. CSFunctionWriter::SetWroteConstructor(false);
  254. source += IndentLine("}\n");
  255. Dedent();
  256. sourceOut += source;
  257. }
  258. void CSClassWriter::GenerateSource(String& sourceOut)
  259. {
  260. }
  261. }