CSClassWriter.cpp 11 KB

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